2000-04-05 10:05:57 +04:00
|
|
|
// -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
//
|
|
|
|
// The contents of this file are subject to the Netscape Public
|
|
|
|
// License Version 1.1 (the "License"); you may not use this file
|
|
|
|
// except in compliance with the License. You may obtain a copy of
|
|
|
|
// the License at http://www.mozilla.org/NPL/
|
|
|
|
//
|
|
|
|
// Software distributed under the License is distributed on an "AS
|
|
|
|
// IS" basis, WITHOUT WARRANTY OF ANY KIND, either express oqr
|
|
|
|
// implied. See the License for the specific language governing
|
|
|
|
// rights and limitations under the License.
|
|
|
|
//
|
|
|
|
// The Original Code is the JavaScript 2 Prototype.
|
|
|
|
//
|
|
|
|
// The Initial Developer of the Original Code is Netscape
|
|
|
|
// Communications Corporation. Portions created by Netscape are
|
2000-04-11 07:11:00 +04:00
|
|
|
// Copyright (C) 2000 Netscape Communications Corporation. All
|
2000-04-05 10:05:57 +04:00
|
|
|
// Rights Reserved.
|
|
|
|
|
|
|
|
#include "interpreter.h"
|
2000-04-06 00:33:41 +04:00
|
|
|
#include "world.h"
|
|
|
|
|
|
|
|
#include <map>
|
2000-04-11 07:11:00 +04:00
|
|
|
#include <stack>
|
2000-04-05 10:05:57 +04:00
|
|
|
|
2000-04-05 10:10:53 +04:00
|
|
|
namespace JavaScript {
|
2000-04-05 10:05:57 +04:00
|
|
|
|
2000-04-07 08:39:50 +04:00
|
|
|
using std::map;
|
|
|
|
using std::less;
|
|
|
|
using std::pair;
|
2000-04-05 10:05:57 +04:00
|
|
|
|
2000-04-08 04:52:59 +04:00
|
|
|
#if defined(XP_MAC)
|
|
|
|
// copied from default template parameters in map.
|
|
|
|
typedef gc_allocator<pair<const String, JSValue> > gc_map_allocator;
|
|
|
|
#elif defined(XP_UNIX)
|
|
|
|
// FIXME: in libg++, they assume the map's allocator is a byte allocator,
|
|
|
|
// which is wrapped in a simple_allocator. this is crap.
|
|
|
|
typedef char _Char[1];
|
|
|
|
typedef gc_allocator<_Char> gc_map_allocator;
|
|
|
|
#elif defined(_WIN32)
|
|
|
|
// FIXME: MSVC++'s notion. this is why we had to add _Charalloc().
|
|
|
|
typedef gc_allocator<JSValue> gc_map_allocator;
|
2000-04-08 02:19:36 +04:00
|
|
|
#endif
|
2000-04-08 04:52:59 +04:00
|
|
|
|
2000-04-11 09:11:58 +04:00
|
|
|
/**
|
|
|
|
* Basic behavior of all JS objects, mapping a name to a value.
|
|
|
|
* This is provided mainly to avoid having an awkward implementation
|
|
|
|
* of JSObject & JSArray, which must each define its own
|
|
|
|
* gc_allocator. This is all in flux.
|
|
|
|
*/
|
2000-04-13 08:54:41 +04:00
|
|
|
class JSMap : public gc_base {
|
2000-04-11 07:11:00 +04:00
|
|
|
map<String, JSValue, less<String>, gc_map_allocator> properties;
|
2000-04-07 06:52:07 +04:00
|
|
|
public:
|
2000-04-13 08:54:41 +04:00
|
|
|
JSValue& operator[](const String& name)
|
2000-04-11 07:11:00 +04:00
|
|
|
{
|
|
|
|
return properties[name];
|
|
|
|
}
|
2000-04-07 06:52:07 +04:00
|
|
|
};
|
2000-04-13 08:54:41 +04:00
|
|
|
|
2000-04-11 07:11:00 +04:00
|
|
|
/**
|
|
|
|
* Private representation of a JavaScript object.
|
|
|
|
* This will change over time, so it is treated as an opaque
|
|
|
|
* type everywhere else but here.
|
|
|
|
*/
|
2000-04-13 08:54:41 +04:00
|
|
|
class JSObject : public JSMap {};
|
2000-04-07 06:52:07 +04:00
|
|
|
|
2000-04-08 07:23:44 +04:00
|
|
|
/**
|
|
|
|
* Private representation of a JavaScript array.
|
|
|
|
*/
|
2000-04-13 08:54:41 +04:00
|
|
|
class JSArray : public JSMap {
|
2000-04-11 07:11:00 +04:00
|
|
|
JSValues elements;
|
2000-04-08 07:23:44 +04:00
|
|
|
public:
|
2000-04-08 08:12:28 +04:00
|
|
|
JSArray() : elements(1) {}
|
2000-04-11 04:32:17 +04:00
|
|
|
JSArray(uint32 size) : elements(size) {}
|
|
|
|
JSArray(const JSValues &v) : elements(v) {}
|
2000-04-11 07:11:00 +04:00
|
|
|
|
|
|
|
uint32 length()
|
|
|
|
{
|
|
|
|
return elements.size();
|
|
|
|
}
|
|
|
|
|
2000-04-08 08:12:28 +04:00
|
|
|
JSValue& operator[](const JSValue& index)
|
2000-04-08 07:23:44 +04:00
|
|
|
{
|
2000-04-08 08:12:28 +04:00
|
|
|
// for now, we can only handle f64 index values.
|
|
|
|
uint32 n = (uint32)index.f64;
|
2000-04-08 07:23:44 +04:00
|
|
|
// obviously, a sparse representation might be better.
|
|
|
|
uint32 size = elements.size();
|
2000-04-11 07:11:00 +04:00
|
|
|
if (n >= size) expand(n, size);
|
2000-04-08 07:23:44 +04:00
|
|
|
return elements[n];
|
|
|
|
}
|
2000-04-11 07:11:00 +04:00
|
|
|
|
2000-04-11 04:32:17 +04:00
|
|
|
JSValue& operator[](uint32 n)
|
|
|
|
{
|
|
|
|
// obviously, a sparse representation might be better.
|
|
|
|
uint32 size = elements.size();
|
2000-04-11 07:11:00 +04:00
|
|
|
if (n >= size) expand(n, size);
|
2000-04-11 04:32:17 +04:00
|
|
|
return elements[n];
|
|
|
|
}
|
2000-04-11 07:11:00 +04:00
|
|
|
|
|
|
|
void resize(uint32 size)
|
|
|
|
{
|
|
|
|
elements.resize(size);
|
|
|
|
}
|
|
|
|
|
2000-04-08 07:23:44 +04:00
|
|
|
private:
|
2000-04-11 07:11:00 +04:00
|
|
|
void expand(uint32 n, uint32 size)
|
2000-04-08 07:23:44 +04:00
|
|
|
{
|
|
|
|
do {
|
|
|
|
size *= 2;
|
|
|
|
} while (n >= size);
|
|
|
|
elements.resize(size);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2000-04-13 08:54:41 +04:00
|
|
|
class JSFunction : public JSMap {
|
2000-04-11 09:11:58 +04:00
|
|
|
ICodeModule* mICode;
|
|
|
|
public:
|
|
|
|
JSFunction(ICodeModule* iCode) : mICode(iCode) {}
|
|
|
|
ICodeModule* getICode() { return mICode; }
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents the current function's invocation state.
|
|
|
|
*/
|
2000-04-13 08:54:41 +04:00
|
|
|
struct JSActivation : public gc_base {
|
2000-04-11 09:11:58 +04:00
|
|
|
JSValues mRegisters;
|
|
|
|
JSValues mLocals;
|
|
|
|
|
|
|
|
JSActivation(ICodeModule* iCode, const JSValues& args)
|
|
|
|
: mRegisters(iCode->itsMaxRegister + 1), mLocals(args)
|
|
|
|
{
|
|
|
|
// ensure that locals array is large enough.
|
|
|
|
uint32 localsSize = iCode->itsMaxVariable + 1;
|
|
|
|
if (localsSize > mLocals.size())
|
|
|
|
mLocals.resize(localsSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
JSActivation(ICodeModule* iCode, JSActivation* caller, const RegisterList& list)
|
|
|
|
: mRegisters(iCode->itsMaxRegister + 1), mLocals(iCode->itsMaxVariable + 1)
|
|
|
|
{
|
|
|
|
// copy caller's parameter list in to locals.
|
|
|
|
JSValues::iterator dest = mLocals.begin();
|
|
|
|
const JSValues& params = caller->mRegisters;
|
|
|
|
for (RegisterList::const_iterator src = list.begin(), end = list.end(); src != end; ++src, ++dest) {
|
|
|
|
*dest = params[*src];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2000-04-11 07:11:00 +04:00
|
|
|
/**
|
|
|
|
* Stores saved state from the *previous* activation, the current activation is alive
|
|
|
|
* and well in locals of the interpreter loop.
|
|
|
|
*/
|
2000-04-13 08:54:41 +04:00
|
|
|
struct JSFrame : public gc_base {
|
2000-04-11 09:11:58 +04:00
|
|
|
JSFrame(InstructionIterator returnPC, InstructionIterator basePC,
|
|
|
|
JSActivation* activation, Register result)
|
|
|
|
: itsReturnPC(returnPC), itsBasePC(basePC),
|
|
|
|
itsActivation(activation),
|
|
|
|
itsResult(result)
|
|
|
|
{
|
|
|
|
}
|
2000-04-11 04:32:17 +04:00
|
|
|
|
|
|
|
InstructionIterator itsReturnPC;
|
|
|
|
InstructionIterator itsBasePC;
|
2000-04-11 09:11:58 +04:00
|
|
|
JSActivation* itsActivation; // caller's activation.
|
|
|
|
Register itsResult; // the desired target register for the return value
|
2000-04-11 04:32:17 +04:00
|
|
|
};
|
|
|
|
|
2000-04-11 07:11:00 +04:00
|
|
|
// a stack of JSFrames.
|
|
|
|
typedef std::stack<JSFrame*, std::vector<JSFrame*, gc_allocator<JSFrame*> > > JSFrameStack;
|
|
|
|
|
|
|
|
// operand access macros.
|
|
|
|
#define op1(i) (i->itsOperand1)
|
|
|
|
#define op2(i) (i->itsOperand2)
|
|
|
|
#define op3(i) (i->itsOperand3)
|
|
|
|
|
|
|
|
// mnemonic names for operands.
|
|
|
|
#define dst(i) op1(i)
|
|
|
|
#define src1(i) op2(i)
|
|
|
|
#define src2(i) op3(i)
|
|
|
|
|
2000-04-11 04:32:17 +04:00
|
|
|
static JSObject globals;
|
|
|
|
|
2000-04-11 07:11:00 +04:00
|
|
|
JSValue& defineGlobalProperty(const String& name, const JSValue& value)
|
2000-04-05 10:05:57 +04:00
|
|
|
{
|
2000-04-11 07:11:00 +04:00
|
|
|
return (globals[name] = value);
|
2000-04-11 04:32:17 +04:00
|
|
|
}
|
|
|
|
|
2000-04-11 09:11:58 +04:00
|
|
|
// FIXME: need to copy the ICodeModule's instruction stream.
|
|
|
|
|
|
|
|
JSValue& defineFunction(const String& name, ICodeModule* iCode)
|
|
|
|
{
|
|
|
|
JSValue value;
|
|
|
|
value.function = new JSFunction(iCode);
|
|
|
|
return defineGlobalProperty(name, value);
|
|
|
|
}
|
|
|
|
|
2000-04-11 04:32:17 +04:00
|
|
|
JSValue interpret(ICodeModule* iCode, const JSValues& args)
|
|
|
|
{
|
2000-04-11 07:11:00 +04:00
|
|
|
// stack of JSFrames.
|
2000-04-11 09:11:58 +04:00
|
|
|
// XXX is a linked list of activation's sufficient?
|
2000-04-11 07:11:00 +04:00
|
|
|
JSFrameStack frames;
|
2000-04-08 07:23:44 +04:00
|
|
|
|
2000-04-11 09:11:58 +04:00
|
|
|
// initial activation.
|
|
|
|
JSActivation* activation = new JSActivation(iCode, args);
|
|
|
|
JSValues* locals = &activation->mLocals;
|
|
|
|
JSValues* registers = &activation->mRegisters;
|
2000-04-11 04:32:17 +04:00
|
|
|
|
2000-04-08 07:23:44 +04:00
|
|
|
InstructionIterator begin_pc = iCode->its_iCode->begin();
|
2000-04-08 05:08:01 +04:00
|
|
|
InstructionIterator pc = begin_pc;
|
2000-04-11 04:32:17 +04:00
|
|
|
|
|
|
|
while (true) {
|
2000-04-05 10:05:57 +04:00
|
|
|
Instruction* instruction = *pc;
|
2000-04-08 07:23:44 +04:00
|
|
|
switch (instruction->opcode()) {
|
2000-04-11 04:32:17 +04:00
|
|
|
case CALL:
|
|
|
|
{
|
|
|
|
Call* call = static_cast<Call*>(instruction);
|
2000-04-11 09:11:58 +04:00
|
|
|
frames.push(new JSFrame(++pc, begin_pc, activation, op1(call)));
|
|
|
|
ICodeModule* target = (*registers)[op2(call)].function->getICode();
|
|
|
|
activation = new JSActivation(target, activation, op3(call));
|
|
|
|
locals = &activation->mLocals;
|
|
|
|
registers = &activation->mRegisters;
|
|
|
|
begin_pc = pc = target->its_iCode->begin();
|
2000-04-11 04:32:17 +04:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
case RETURN:
|
|
|
|
{
|
|
|
|
Return* ret = static_cast<Return*>(instruction);
|
|
|
|
JSValue result;
|
|
|
|
if (op1(ret) != NotARegister)
|
|
|
|
result = (*registers)[op1(ret)];
|
2000-04-11 07:11:00 +04:00
|
|
|
if (frames.empty())
|
2000-04-11 04:32:17 +04:00
|
|
|
return result;
|
2000-04-11 09:11:58 +04:00
|
|
|
JSFrame *frame = frames.top();
|
2000-04-11 07:11:00 +04:00
|
|
|
frames.pop();
|
2000-04-11 09:11:58 +04:00
|
|
|
activation = frame->itsActivation;
|
|
|
|
registers = &activation->mRegisters;
|
|
|
|
locals = &activation->mLocals;
|
|
|
|
(*registers)[frame->itsResult] = result;
|
|
|
|
pc = frame->itsReturnPC;
|
|
|
|
begin_pc = frame->itsBasePC;
|
2000-04-11 04:32:17 +04:00
|
|
|
}
|
|
|
|
continue;
|
2000-04-08 07:23:44 +04:00
|
|
|
case MOVE_TO:
|
|
|
|
{
|
|
|
|
Move* mov = static_cast<Move*>(instruction);
|
2000-04-11 04:32:17 +04:00
|
|
|
(*registers)[dst(mov)] = (*registers)[src1(mov)];
|
2000-04-08 07:23:44 +04:00
|
|
|
}
|
|
|
|
break;
|
2000-04-08 04:52:59 +04:00
|
|
|
case LOAD_NAME:
|
2000-04-08 07:23:44 +04:00
|
|
|
{
|
|
|
|
LoadName* ln = static_cast<LoadName*>(instruction);
|
2000-04-11 04:32:17 +04:00
|
|
|
(*registers)[dst(ln)] = globals[*src1(ln)];
|
2000-04-08 07:23:44 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SAVE_NAME:
|
|
|
|
{
|
|
|
|
SaveName* sn = static_cast<SaveName*>(instruction);
|
2000-04-11 04:32:17 +04:00
|
|
|
globals[*dst(sn)] = (*registers)[src1(sn)];
|
2000-04-08 07:23:44 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case NEW_OBJECT:
|
2000-04-08 04:52:59 +04:00
|
|
|
{
|
|
|
|
NewObject* no = static_cast<NewObject*>(instruction);
|
2000-04-11 04:32:17 +04:00
|
|
|
(*registers)[dst(no)].object = new JSObject();
|
2000-04-08 04:52:59 +04:00
|
|
|
}
|
|
|
|
break;
|
2000-04-08 07:37:49 +04:00
|
|
|
case NEW_ARRAY:
|
|
|
|
{
|
|
|
|
NewArray* na = static_cast<NewArray*>(instruction);
|
2000-04-11 04:32:17 +04:00
|
|
|
(*registers)[dst(na)].array = new JSArray();
|
2000-04-08 07:37:49 +04:00
|
|
|
}
|
|
|
|
break;
|
2000-04-08 07:23:44 +04:00
|
|
|
case GET_PROP:
|
2000-04-08 04:52:59 +04:00
|
|
|
{
|
|
|
|
GetProp* gp = static_cast<GetProp*>(instruction);
|
2000-04-11 04:32:17 +04:00
|
|
|
JSObject* object = (*registers)[src1(gp)].object;
|
|
|
|
(*registers)[dst(gp)] = (*object)[*src2(gp)];
|
2000-04-08 04:52:59 +04:00
|
|
|
}
|
|
|
|
break;
|
2000-04-08 07:23:44 +04:00
|
|
|
case SET_PROP:
|
2000-04-08 04:52:59 +04:00
|
|
|
{
|
|
|
|
SetProp* sp = static_cast<SetProp*>(instruction);
|
2000-04-11 04:32:17 +04:00
|
|
|
JSObject* object = (*registers)[dst(sp)].object;
|
|
|
|
(*object)[*src1(sp)] = (*registers)[src2(sp)];
|
2000-04-08 07:23:44 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case GET_ELEMENT:
|
|
|
|
{
|
|
|
|
GetElement* ge = static_cast<GetElement*>(instruction);
|
2000-04-11 04:32:17 +04:00
|
|
|
JSArray* array = (*registers)[src1(ge)].array;
|
|
|
|
(*registers)[dst(ge)] = (*array)[(*registers)[src2(ge)]];
|
2000-04-08 07:23:44 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SET_ELEMENT:
|
|
|
|
{
|
|
|
|
SetElement* se = static_cast<SetElement*>(instruction);
|
2000-04-11 04:32:17 +04:00
|
|
|
JSArray* array = (*registers)[dst(se)].array;
|
|
|
|
(*array)[(*registers)[src1(se)]] = (*registers)[src2(se)];
|
2000-04-08 07:23:44 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case LOAD_IMMEDIATE:
|
|
|
|
{
|
|
|
|
LoadImmediate* li = static_cast<LoadImmediate*>(instruction);
|
2000-04-11 04:32:17 +04:00
|
|
|
(*registers)[dst(li)] = JSValue(src1(li));
|
2000-04-08 07:23:44 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case LOAD_VAR:
|
|
|
|
{
|
|
|
|
LoadVar* lv = static_cast<LoadVar*>(instruction);
|
2000-04-11 04:32:17 +04:00
|
|
|
(*registers)[dst(lv)] = (*locals)[src1(lv)];
|
2000-04-08 07:23:44 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SAVE_VAR:
|
|
|
|
{
|
|
|
|
SaveVar* sv = static_cast<SaveVar*>(instruction);
|
2000-04-11 04:32:17 +04:00
|
|
|
(*locals)[dst(sv)] = (*registers)[src1(sv)];
|
2000-04-08 07:23:44 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case BRANCH:
|
|
|
|
{
|
|
|
|
ResolvedBranch* bra = static_cast<ResolvedBranch*>(instruction);
|
|
|
|
pc = begin_pc + dst(bra);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case BRANCH_LT:
|
|
|
|
{
|
|
|
|
ResolvedBranchCond* bc = static_cast<ResolvedBranchCond*>(instruction);
|
2000-04-11 04:32:17 +04:00
|
|
|
if ((*registers)[src1(bc)].i32 < 0) {
|
2000-04-08 07:23:44 +04:00
|
|
|
pc = begin_pc + dst(bc);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case BRANCH_LE:
|
|
|
|
{
|
|
|
|
ResolvedBranchCond* bc = static_cast<ResolvedBranchCond*>(instruction);
|
2000-04-11 04:32:17 +04:00
|
|
|
if ((*registers)[src1(bc)].i32 <= 0) {
|
2000-04-08 07:23:44 +04:00
|
|
|
pc = begin_pc + dst(bc);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case BRANCH_EQ:
|
|
|
|
{
|
|
|
|
ResolvedBranchCond* bc = static_cast<ResolvedBranchCond*>(instruction);
|
2000-04-11 04:32:17 +04:00
|
|
|
if ((*registers)[src1(bc)].i32 == 0) {
|
2000-04-08 07:23:44 +04:00
|
|
|
pc = begin_pc + dst(bc);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case BRANCH_NE:
|
|
|
|
{
|
|
|
|
ResolvedBranchCond* bc = static_cast<ResolvedBranchCond*>(instruction);
|
2000-04-11 04:32:17 +04:00
|
|
|
if ((*registers)[src1(bc)].i32 != 0) {
|
2000-04-08 07:23:44 +04:00
|
|
|
pc = begin_pc + dst(bc);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case BRANCH_GE:
|
|
|
|
{
|
|
|
|
ResolvedBranchCond* bc = static_cast<ResolvedBranchCond*>(instruction);
|
2000-04-11 04:32:17 +04:00
|
|
|
if ((*registers)[src1(bc)].i32 >= 0) {
|
2000-04-08 07:23:44 +04:00
|
|
|
pc = begin_pc + dst(bc);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case BRANCH_GT:
|
|
|
|
{
|
|
|
|
ResolvedBranchCond* bc = static_cast<ResolvedBranchCond*>(instruction);
|
2000-04-11 04:32:17 +04:00
|
|
|
if ((*registers)[src1(bc)].i32 > 0) {
|
2000-04-08 07:23:44 +04:00
|
|
|
pc = begin_pc + dst(bc);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ADD:
|
|
|
|
{
|
|
|
|
// could get clever here with Functional forms.
|
|
|
|
Arithmetic* add = static_cast<Arithmetic*>(instruction);
|
2000-04-11 04:32:17 +04:00
|
|
|
(*registers)[dst(add)] = JSValue((*registers)[src1(add)].f64 + (*registers)[src2(add)].f64);
|
2000-04-08 07:23:44 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SUBTRACT:
|
|
|
|
{
|
|
|
|
Arithmetic* sub = static_cast<Arithmetic*>(instruction);
|
2000-04-11 04:32:17 +04:00
|
|
|
(*registers)[dst(sub)] = JSValue((*registers)[src1(sub)].f64 - (*registers)[src2(sub)].f64);
|
2000-04-08 07:23:44 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case MULTIPLY:
|
|
|
|
{
|
|
|
|
Arithmetic* mul = static_cast<Arithmetic*>(instruction);
|
2000-04-11 04:32:17 +04:00
|
|
|
(*registers)[dst(mul)] = JSValue((*registers)[src1(mul)].f64 * (*registers)[src2(mul)].f64);
|
2000-04-08 07:23:44 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case DIVIDE:
|
|
|
|
{
|
|
|
|
Arithmetic* div = static_cast<Arithmetic*>(instruction);
|
2000-04-11 04:32:17 +04:00
|
|
|
(*registers)[dst(div)] = JSValue((*registers)[src1(div)].f64 / (*registers)[src2(div)].f64);
|
2000-04-08 07:23:44 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case COMPARE_LT:
|
|
|
|
case COMPARE_LE:
|
|
|
|
case COMPARE_EQ:
|
|
|
|
case COMPARE_NE:
|
|
|
|
case COMPARE_GT:
|
|
|
|
case COMPARE_GE:
|
|
|
|
{
|
|
|
|
Arithmetic* cmp = static_cast<Arithmetic*>(instruction);
|
2000-04-11 04:32:17 +04:00
|
|
|
float64 diff = ((*registers)[src1(cmp)].f64 - (*registers)[src2(cmp)].f64);
|
|
|
|
(*registers)[dst(cmp)].i32 = (diff == 0.0 ? 0 : (diff > 0.0 ? 1 : -1));
|
2000-04-08 07:23:44 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case NOT:
|
|
|
|
{
|
|
|
|
Move* nt = static_cast<Move*>(instruction);
|
2000-04-11 04:32:17 +04:00
|
|
|
(*registers)[dst(nt)].i32 = !(*registers)[src1(nt)].i32;
|
2000-04-08 04:52:59 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2000-04-11 04:32:17 +04:00
|
|
|
NOT_REACHED("bad opcode");
|
2000-04-08 04:52:59 +04:00
|
|
|
break;
|
2000-04-08 07:23:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// increment the program counter.
|
|
|
|
++pc;
|
2000-04-05 10:05:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|