first crack at an icode interpreter

This commit is contained in:
beard%netscape.com 2000-04-05 06:05:57 +00:00
parent 8394e82e58
commit 8283b89064
4 changed files with 340 additions and 0 deletions

119
js/js2/interpreter.cpp Normal file
View File

@ -0,0 +1,119 @@
// -*- 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
// Copyright (C) 1998 Netscape Communications Corporation. All
// Rights Reserved.
#include "interpreter.h"
namespace JS = JavaScript;
namespace JS {
// these should probably be in icodegenerator.h.
typedef Instruction_2<StringAtom&, Register> LoadName;
typedef Instruction_2<double, Register> LoadImmediate;
typedef Instruction_2<int32, Register> LoadVar;
typedef Instruction_2<int32, Register> SaveVar;
typedef Instruction_1<int32> Branch;
typedef Instruction_2<int32, Register> BranchCond;
typedef Instruction_3<Register, Register, Register> Arithmetic;
typedef Instruction_2<Register, Register> Move;
#define op1(i) (i->itsOperand1)
#define op2(i) (i->itsOperand2)
#define op3(i) (i->itsOperand3)
JSValue interpret(InstructionStream& iCode, const JSValues& args)
{
JSValue result = JSValue();
JSValues frame(args);
JSValues registers(32);
for (InstructionIterator pc = iCode.begin(); pc != iCode.end(); ++pc) {
Instruction* instruction = *pc;
switch (instruction->itsOp) {
case LOAD_NAME :
{
LoadName* i = static_cast<LoadName*>(instruction);
std::cout << "\"" << op1(i) << "\", R" << op2(i);
}
break;
case LOAD_IMMEDIATE :
{
LoadImmediate* i = static_cast<LoadImmediate*>(instruction);
// s << t->itsOperand1 << ", R" << t->itsOperand2;
registers[op2(i)] = JSValue(op1(i));
}
break;
case LOAD_VAR:
{
LoadVar* i = static_cast<LoadVar*>(instruction);
registers[op2(i)] = frame[op1(i)];
}
break;
case SAVE_VAR:
{
SaveVar* i = static_cast<SaveVar*>(instruction);
frame[op1(i)] = registers[op2(i)];
}
break;
case BRANCH :
{
Branch* i = static_cast<Branch*>(instruction);
pc = iCode.begin() + op1(i);
}
break;
case BRANCH_COND :
{
BranchCond* i = static_cast<BranchCond*>(instruction);
// s << "target #" << t->itsOperand1 << ", R" << t->itsOperand2;
if (registers[op2(i)].i32)
pc = iCode.begin() + op1(i);
}
break;
case ADD:
{
// could get clever here with Functional forms.
Arithmetic* i = static_cast<Arithmetic*>(instruction);
registers[op3(i)] = JSValue(registers[op1(i)].f64 + registers[op2(i)].f64);
}
break;
case COMPARE:
{
Arithmetic* i = static_cast<Arithmetic*>(instruction);
registers[op3(i)].i32 = registers[op1(i)].f64 == registers[op2(i)].f64;
}
break;
case MOVE_TO:
{
Move* i = static_cast<Move*>(instruction);
registers[op2(i)] = registers[op1(i)];
}
break;
case NOT:
{
Move* i = static_cast<Move*>(instruction);
registers[op2(i)].i32 = !registers[op1(i)].i32;
}
break;
}
}
return result;
}
}

51
js/js2/interpreter.h Normal file
View File

@ -0,0 +1,51 @@
// -*- 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
// Copyright (C) 1998 Netscape Communications Corporation. All
// Rights Reserved.
#ifndef interpreter_h
#define interpreter_h
#include "icodegenerator.h"
namespace JavaScript {
union JSValue {
int8 i8;
uint8 u8;
int16 i16;
uint16 u16;
int32 i32;
uint32 u32;
int64 i64;
uint64 u64;
float32 f32;
float64 f64;
JSValue() : i64(0) {}
explicit JSValue(int8 i8) : i8(i8) {}
explicit JSValue(float64 f64) : f64(f64) {}
};
using std::vector;
typedef vector<JSValue> JSValues;
JSValue interpret(InstructionStream& iCode, const JSValues& args);
}
#endif /* interpreter_h */

119
js2/src/interpreter.cpp Normal file
View File

@ -0,0 +1,119 @@
// -*- 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
// Copyright (C) 1998 Netscape Communications Corporation. All
// Rights Reserved.
#include "interpreter.h"
namespace JS = JavaScript;
namespace JS {
// these should probably be in icodegenerator.h.
typedef Instruction_2<StringAtom&, Register> LoadName;
typedef Instruction_2<double, Register> LoadImmediate;
typedef Instruction_2<int32, Register> LoadVar;
typedef Instruction_2<int32, Register> SaveVar;
typedef Instruction_1<int32> Branch;
typedef Instruction_2<int32, Register> BranchCond;
typedef Instruction_3<Register, Register, Register> Arithmetic;
typedef Instruction_2<Register, Register> Move;
#define op1(i) (i->itsOperand1)
#define op2(i) (i->itsOperand2)
#define op3(i) (i->itsOperand3)
JSValue interpret(InstructionStream& iCode, const JSValues& args)
{
JSValue result = JSValue();
JSValues frame(args);
JSValues registers(32);
for (InstructionIterator pc = iCode.begin(); pc != iCode.end(); ++pc) {
Instruction* instruction = *pc;
switch (instruction->itsOp) {
case LOAD_NAME :
{
LoadName* i = static_cast<LoadName*>(instruction);
std::cout << "\"" << op1(i) << "\", R" << op2(i);
}
break;
case LOAD_IMMEDIATE :
{
LoadImmediate* i = static_cast<LoadImmediate*>(instruction);
// s << t->itsOperand1 << ", R" << t->itsOperand2;
registers[op2(i)] = JSValue(op1(i));
}
break;
case LOAD_VAR:
{
LoadVar* i = static_cast<LoadVar*>(instruction);
registers[op2(i)] = frame[op1(i)];
}
break;
case SAVE_VAR:
{
SaveVar* i = static_cast<SaveVar*>(instruction);
frame[op1(i)] = registers[op2(i)];
}
break;
case BRANCH :
{
Branch* i = static_cast<Branch*>(instruction);
pc = iCode.begin() + op1(i);
}
break;
case BRANCH_COND :
{
BranchCond* i = static_cast<BranchCond*>(instruction);
// s << "target #" << t->itsOperand1 << ", R" << t->itsOperand2;
if (registers[op2(i)].i32)
pc = iCode.begin() + op1(i);
}
break;
case ADD:
{
// could get clever here with Functional forms.
Arithmetic* i = static_cast<Arithmetic*>(instruction);
registers[op3(i)] = JSValue(registers[op1(i)].f64 + registers[op2(i)].f64);
}
break;
case COMPARE:
{
Arithmetic* i = static_cast<Arithmetic*>(instruction);
registers[op3(i)].i32 = registers[op1(i)].f64 == registers[op2(i)].f64;
}
break;
case MOVE_TO:
{
Move* i = static_cast<Move*>(instruction);
registers[op2(i)] = registers[op1(i)];
}
break;
case NOT:
{
Move* i = static_cast<Move*>(instruction);
registers[op2(i)].i32 = !registers[op1(i)].i32;
}
break;
}
}
return result;
}
}

51
js2/src/interpreter.h Normal file
View File

@ -0,0 +1,51 @@
// -*- 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
// Copyright (C) 1998 Netscape Communications Corporation. All
// Rights Reserved.
#ifndef interpreter_h
#define interpreter_h
#include "icodegenerator.h"
namespace JavaScript {
union JSValue {
int8 i8;
uint8 u8;
int16 i16;
uint16 u16;
int32 i32;
uint32 u32;
int64 i64;
uint64 u64;
float32 f32;
float64 f64;
JSValue() : i64(0) {}
explicit JSValue(int8 i8) : i8(i8) {}
explicit JSValue(float64 f64) : f64(f64) {}
};
using std::vector;
typedef vector<JSValue> JSValues;
JSValue interpret(InstructionStream& iCode, const JSValues& args);
}
#endif /* interpreter_h */