зеркало из https://github.com/mozilla/pjs.git
First hack at some icode generation.
This commit is contained in:
Родитель
a176a7758a
Коммит
4e4f0fd854
|
@ -0,0 +1,330 @@
|
|||
// -*- 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 "numerics.h"
|
||||
#include "world.h"
|
||||
#include "utilities.h"
|
||||
#include "icodegenerator.h"
|
||||
|
||||
#include <iomanip>
|
||||
|
||||
namespace JS = JavaScript;
|
||||
using namespace JavaScript;
|
||||
|
||||
ostream &JS::operator<<(ostream &s, ICodeGenerator &i)
|
||||
{
|
||||
return i.print(s);
|
||||
}
|
||||
|
||||
//
|
||||
// ICodeGenerator
|
||||
//
|
||||
|
||||
|
||||
Register ICodeGenerator::loadVariable(int32 frameIndex)
|
||||
{
|
||||
Register dest = getRegister();
|
||||
Instruction_2<int32, Register> *instr = new Instruction_2<int32, Register>(LOAD_VAR, frameIndex, dest);
|
||||
iCode->push_back(instr);
|
||||
return dest;
|
||||
}
|
||||
|
||||
Register ICodeGenerator::loadImmediate(double value)
|
||||
{
|
||||
Register dest = getRegister();
|
||||
Instruction_2<double, Register> *instr = new Instruction_2<double, Register>(LOAD_IMMEDIATE, value, dest);
|
||||
iCode->push_back(instr);
|
||||
return dest;
|
||||
}
|
||||
|
||||
Register ICodeGenerator::loadName(StringAtom &name)
|
||||
{
|
||||
Register dest = getRegister();
|
||||
Instruction_2<StringAtom &, Register> *instr = new Instruction_2<StringAtom &, Register>(LOAD_NAME, name, dest);
|
||||
iCode->push_back(instr);
|
||||
return dest;
|
||||
}
|
||||
|
||||
Register ICodeGenerator::getProperty(StringAtom &name, Register base)
|
||||
{
|
||||
Register dest = getRegister();
|
||||
Instruction_3<StringAtom &, Register, Register> *instr = new Instruction_3<StringAtom &, Register, Register>(GET_PROP, name, base, dest);
|
||||
iCode->push_back(instr);
|
||||
return dest;
|
||||
}
|
||||
|
||||
void ICodeGenerator::saveVariable(int32 frameIndex, Register value)
|
||||
{
|
||||
Instruction_2<int32, Register> *instr = new Instruction_2<int32, Register>(SAVE_VAR, frameIndex, value);
|
||||
iCode->push_back(instr);
|
||||
}
|
||||
|
||||
Register ICodeGenerator::op(ICodeOp op, Register source)
|
||||
{
|
||||
Register dest = getRegister();
|
||||
Instruction_2<Register, Register> *instr = new Instruction_2<Register, Register>(op, source, dest);
|
||||
iCode->push_back(instr);
|
||||
return dest;
|
||||
}
|
||||
|
||||
Register ICodeGenerator::op(ICodeOp op, Register source1, Register source2)
|
||||
{
|
||||
Register dest = getRegister();
|
||||
Instruction_3<Register, Register, Register> *instr = new Instruction_3<Register, Register, Register>(op, source1, source2, dest);
|
||||
iCode->push_back(instr);
|
||||
return dest;
|
||||
}
|
||||
|
||||
void ICodeGenerator::branch(int32 label)
|
||||
{
|
||||
Instruction_1<int32> *instr = new Instruction_1<int32>(BRANCH, label);
|
||||
iCode->push_back(instr);
|
||||
}
|
||||
|
||||
void ICodeGenerator::branchConditional(int32 label, Register condition)
|
||||
{
|
||||
Instruction_2<int32, Register> *instr = new Instruction_2<int32, Register>(BRANCH_COND, label, condition);
|
||||
iCode->push_back(instr);
|
||||
}
|
||||
|
||||
|
||||
/***********************************************************************************************/
|
||||
|
||||
int32 ICodeGenerator::getLabel()
|
||||
{
|
||||
int32 result = labels.size();
|
||||
labels.push_back(new Label(NULL, -1));
|
||||
return result;
|
||||
}
|
||||
|
||||
void ICodeGenerator::setLabel(int32 label)
|
||||
{
|
||||
labels.at(label)->itsBase = iCode;
|
||||
labels.at(label)->itsOffset = iCode->size();
|
||||
}
|
||||
|
||||
|
||||
/***********************************************************************************************/
|
||||
|
||||
void ICodeGenerator::beginWhileStatement(const SourcePosition &pos)
|
||||
{
|
||||
resetTopRegister();
|
||||
|
||||
// insert a branch to the while condition, which we're
|
||||
// moving to follow the while block
|
||||
int32 whileConditionTop = getLabel();
|
||||
int32 whileBlockStart = getLabel();
|
||||
branch(whileConditionTop);
|
||||
|
||||
// save off the current stream while we gen code for the condition
|
||||
stitcher.push(new WhileCodeState(iCode, labels.size(), whileConditionTop, whileBlockStart));
|
||||
|
||||
iCode = new InstructionStream();
|
||||
}
|
||||
|
||||
void ICodeGenerator::endWhileExpression(Register condition)
|
||||
{
|
||||
WhileCodeState *ics = (WhileCodeState *)(stitcher.top());
|
||||
|
||||
branchConditional(ics->whileBodyLabel, condition);
|
||||
resetTopRegister();
|
||||
// stash away the condition expression and switch
|
||||
// back to the main stream
|
||||
iCode = stitcher.top()->swap_iCode(iCode);
|
||||
setLabel(ics->whileBodyLabel); // mark the start of the while block
|
||||
}
|
||||
|
||||
void ICodeGenerator::endWhileStatement()
|
||||
{
|
||||
// recover the while stream
|
||||
WhileCodeState *ics = dynamic_cast<WhileCodeState *>(stitcher.top());
|
||||
stitcher.pop();
|
||||
|
||||
// mark the start of the condition code
|
||||
// and re-attach it to the main stream
|
||||
setLabel(ics->whileConditionLabel);
|
||||
|
||||
if (ics->itsTopLabel < labels.size()) {
|
||||
// labels (might) have been allocated in this stream
|
||||
// we need to adjust their position relative to the
|
||||
// size of the stream we're joining
|
||||
|
||||
// XXXX how do I start at 'ics->itsTopLabel' ???
|
||||
//
|
||||
for (LabelList::iterator i = labels.begin(); i != labels.end(); i++) {
|
||||
if ((*i)->itsBase == ics->its_iCode) {
|
||||
(*i)->itsBase = iCode;
|
||||
(*i)->itsOffset += iCode->size();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (InstructionIterator i = ics->its_iCode->begin(); i != ics->its_iCode->end(); i++)
|
||||
iCode->push_back(*i);
|
||||
|
||||
delete ics->its_iCode;
|
||||
delete ics;
|
||||
|
||||
resetTopRegister();
|
||||
}
|
||||
|
||||
|
||||
/***********************************************************************************************/
|
||||
|
||||
void ICodeGenerator::beginIfStatement(const SourcePosition &pos, Register condition)
|
||||
{
|
||||
int32 elseLabel = getLabel();
|
||||
|
||||
// save off the current stream while we gen code for the condition
|
||||
stitcher.push(new IfCodeState(iCode, labels.size(), elseLabel, -1));
|
||||
|
||||
Register notCond = op(NOT, condition);
|
||||
branchConditional(elseLabel, notCond);
|
||||
|
||||
resetTopRegister();
|
||||
}
|
||||
|
||||
void ICodeGenerator::beginElseStatement(bool hasElse)
|
||||
{
|
||||
IfCodeState *ics = (IfCodeState *)(stitcher.top());
|
||||
|
||||
if (hasElse) {
|
||||
int32 beyondElse = getLabel();
|
||||
ics->beyondElse = beyondElse;
|
||||
branch(beyondElse);
|
||||
}
|
||||
setLabel(ics->elseLabel);
|
||||
resetTopRegister();
|
||||
}
|
||||
|
||||
void ICodeGenerator::endIfStatement()
|
||||
{
|
||||
IfCodeState *ics = (IfCodeState *)(stitcher.top());
|
||||
|
||||
if (ics->beyondElse != -1) { // had an else
|
||||
setLabel(ics->beyondElse); // the beyond else label
|
||||
}
|
||||
stitcher.pop();
|
||||
resetTopRegister();
|
||||
}
|
||||
|
||||
|
||||
/***********************************************************************************************/
|
||||
|
||||
|
||||
char *opcodeName[] = {
|
||||
"load_var",
|
||||
"save_var",
|
||||
|
||||
"load_imm",
|
||||
|
||||
"load_name",
|
||||
"save_name",
|
||||
|
||||
"get_prop",
|
||||
"set_prop",
|
||||
|
||||
"add",
|
||||
"not",
|
||||
|
||||
"branch",
|
||||
"branch_cond",
|
||||
};
|
||||
|
||||
ostream &JS::operator<<(ostream &s, StringAtom &str)
|
||||
{
|
||||
for (String::iterator i = str.begin(); i != str.end(); i++)
|
||||
s << (char)*i;
|
||||
return s;
|
||||
}
|
||||
|
||||
ostream &ICodeGenerator::print(ostream &s)
|
||||
{
|
||||
s << "ICG! " << iCode->size() << "\n";
|
||||
for (InstructionIterator i = iCode->begin(); i != iCode->end(); i++) {
|
||||
for (LabelList::iterator k = labels.begin(); k != labels.end(); k++)
|
||||
if ((*k)->itsOffset == (i - iCode->begin())) {
|
||||
s << "label #" << (k - labels.begin()) << ":\n";
|
||||
}
|
||||
|
||||
Instruction *instr = *i;
|
||||
s << "\t"<< std::setiosflags( std::ostream::left ) << std::setw(16) << opcodeName[instr->itsOp];
|
||||
switch (instr->itsOp) {
|
||||
case LOAD_NAME :
|
||||
{
|
||||
Instruction_2<StringAtom &, Register> *t = static_cast<Instruction_2<StringAtom &, Register> * >(instr);
|
||||
s << "\"" << t->itsOperand1 << "\", R" << t->itsOperand2;
|
||||
}
|
||||
break;
|
||||
case LOAD_IMMEDIATE :
|
||||
{
|
||||
Instruction_2<double, Register> *t = static_cast<Instruction_2<double, Register> * >(instr);
|
||||
s << t->itsOperand1 << ", R" << t->itsOperand2;
|
||||
}
|
||||
break;
|
||||
case LOAD_VAR :
|
||||
case SAVE_VAR :
|
||||
{
|
||||
Instruction_2<int, Register> *t = static_cast<Instruction_2<int, Register> * >(instr);
|
||||
s << "Variable #" << t->itsOperand1 << ", R" << t->itsOperand2;
|
||||
}
|
||||
break;
|
||||
case BRANCH :
|
||||
{
|
||||
Instruction_1<int32> *t = static_cast<Instruction_1<int32> * >(instr);
|
||||
s << "target #" << t->itsOperand1;
|
||||
}
|
||||
break;
|
||||
case BRANCH_COND :
|
||||
{
|
||||
Instruction_2<int32, Register> *t = static_cast<Instruction_2<int32, Register> * >(instr);
|
||||
s << "target #" << t->itsOperand1 << ", R" << t->itsOperand2;
|
||||
}
|
||||
break;
|
||||
case ADD :
|
||||
{
|
||||
Instruction_3<Register, Register, Register> *t = static_cast<Instruction_3<Register, Register, Register> * >(instr);
|
||||
s << "R" << t->itsOperand1 << ", R" << t->itsOperand2 << ", R" << t->itsOperand3;
|
||||
}
|
||||
break;
|
||||
case NOT :
|
||||
{
|
||||
Instruction_3<Register, Register, Register> *t = static_cast<Instruction_3<Register, Register, Register> * >(instr);
|
||||
s << "R" << t->itsOperand1 << ", R" << t->itsOperand2;
|
||||
}
|
||||
break;
|
||||
}
|
||||
s << "\n";
|
||||
}
|
||||
for (LabelList::iterator k = labels.begin(); k != labels.end(); k++)
|
||||
if ((*k)->itsOffset == (iCode->end() - iCode->begin())) {
|
||||
s << "label #" << (k - labels.begin()) << ":\n";
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,244 @@
|
|||
// -*- 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 icodegenerator_h
|
||||
#define icodegenerator_h
|
||||
|
||||
#include "utilities.h"
|
||||
#include "parser.h"
|
||||
|
||||
#include <vector>
|
||||
#include <stack>
|
||||
#include <algorithm>
|
||||
|
||||
namespace JavaScript {
|
||||
|
||||
typedef uint32 Register;
|
||||
|
||||
enum ICodeOp {
|
||||
// Operand1 Operand2 Operand3
|
||||
|
||||
LOAD_VAR, // index of frame slot Destination Register
|
||||
SAVE_VAR, // index of frame slot Source Register
|
||||
|
||||
LOAD_IMMEDIATE, // immediate (double) Destination Register
|
||||
|
||||
LOAD_NAME, // StringAtom & Destination Register
|
||||
SAVE_NAME, // StringAtom & Source Register
|
||||
|
||||
GET_PROP, // StringAtom & Base Register Destination Register
|
||||
SET_PROP, // StringAtom & Base Register Source Register
|
||||
|
||||
ADD, // Source Register 1 Source Register 2 Destination Register
|
||||
NOT, // Source Register Destination Register
|
||||
|
||||
BRANCH, // Target label
|
||||
BRANCH_COND, // Target label Condition Register
|
||||
};
|
||||
|
||||
class Instruction {
|
||||
public:
|
||||
Instruction(ICodeOp op) : itsOp(op) { }
|
||||
ICodeOp itsOp;
|
||||
};
|
||||
|
||||
template <typename Operand1>
|
||||
class Instruction_1 : public Instruction {
|
||||
public:
|
||||
Instruction_1(ICodeOp op, Operand1 operand1) :
|
||||
Instruction(op), itsOperand1(operand1) { }
|
||||
Operand1 itsOperand1;
|
||||
};
|
||||
|
||||
template <typename Operand1, typename Operand2>
|
||||
class Instruction_2 : public Instruction {
|
||||
public:
|
||||
Instruction_2(ICodeOp op, Operand1 operand1, Operand2 operand2) :
|
||||
Instruction(op), itsOperand1(operand1), itsOperand2(operand2) { }
|
||||
Operand1 itsOperand1;
|
||||
Operand2 itsOperand2;
|
||||
};
|
||||
|
||||
template <typename Operand1, typename Operand2, typename Operand3>
|
||||
class Instruction_3 : public Instruction {
|
||||
public:
|
||||
Instruction_3(ICodeOp op, Operand1 operand1, Operand2 operand2, Operand3 operand3) :
|
||||
Instruction(op), itsOperand1(operand1), itsOperand2(operand2), itsOperand3(operand3) { }
|
||||
Operand1 itsOperand1;
|
||||
Operand2 itsOperand2;
|
||||
Operand3 itsOperand3;
|
||||
};
|
||||
|
||||
typedef std::vector<Instruction *> InstructionStream;
|
||||
typedef InstructionStream::iterator InstructionIterator;
|
||||
|
||||
/****************************************************************/
|
||||
|
||||
class Label {
|
||||
public:
|
||||
Label(InstructionStream *base, int32 offset) : itsBase(base), itsOffset(offset) { }
|
||||
|
||||
InstructionStream *itsBase;
|
||||
int32 itsOffset;
|
||||
};
|
||||
|
||||
typedef std::vector<Label *> LabelList;
|
||||
|
||||
/****************************************************************/
|
||||
/****************************************************************/
|
||||
|
||||
class ICodeState {
|
||||
public:
|
||||
ICodeState(InstructionStream *iCode, int32 topLabel) : its_iCode(iCode), itsTopLabel(topLabel) {}
|
||||
virtual ~ICodeState() { }
|
||||
|
||||
InstructionStream *swap_iCode(InstructionStream *iCode) { InstructionStream *t = its_iCode; its_iCode = iCode; return t; }
|
||||
|
||||
InstructionStream *its_iCode;
|
||||
int32 itsTopLabel; // set to the highest label allocated when this stream
|
||||
// was created. If that value changes, this stream may
|
||||
// contain labels that will need to be adjusted when
|
||||
// the streams are merged.
|
||||
};
|
||||
|
||||
class WhileCodeState : public ICodeState {
|
||||
public:
|
||||
WhileCodeState(InstructionStream *iCode, int32 topLabel, int32 a, int32 b)
|
||||
: ICodeState(iCode, topLabel), whileConditionLabel(a), whileBodyLabel(b) { }
|
||||
int32 whileConditionLabel;
|
||||
int32 whileBodyLabel;
|
||||
};
|
||||
|
||||
class IfCodeState : public ICodeState {
|
||||
public:
|
||||
IfCodeState(InstructionStream *iCode, int32 topLabel, int32 a, int32 b)
|
||||
: ICodeState(iCode, topLabel), elseLabel(a), beyondElse(b) { }
|
||||
int32 elseLabel;
|
||||
int32 beyondElse;
|
||||
};
|
||||
|
||||
/****************************************************************/
|
||||
|
||||
// An ICodeGenerator provides the interface between the parser and the interpreter.
|
||||
// The parser constructs one of these for each function/script, adds statements and
|
||||
// expressions to it and then converts it into an ICodeModule, ready for execution.
|
||||
|
||||
class ICodeGenerator {
|
||||
private:
|
||||
|
||||
InstructionStream *iCode;
|
||||
|
||||
LabelList labels;
|
||||
|
||||
std::stack<ICodeState *> stitcher;
|
||||
|
||||
Register topRegister;
|
||||
Register getRegister() { return topRegister++; }
|
||||
void resetTopRegister() { topRegister = 0; }
|
||||
|
||||
int32 getLabel();
|
||||
void setLabel(int32 label);
|
||||
|
||||
void branch(int32 label);
|
||||
void branchConditional(int32 label, Register condition);
|
||||
|
||||
public:
|
||||
ICodeGenerator() { iCode = new InstructionStream(); }
|
||||
|
||||
ostream &print(ostream &s);
|
||||
|
||||
Register op(ICodeOp op, Register source);
|
||||
Register op(ICodeOp op, Register source1, Register source2);
|
||||
|
||||
Register loadVariable(int32 frameIndex);
|
||||
Register loadImmediate(double value);
|
||||
|
||||
void saveVariable(int32 frameIndex, Register value);
|
||||
|
||||
Register loadName(StringAtom &name);
|
||||
Register getProperty(StringAtom &name, Register base);
|
||||
|
||||
|
||||
void beginStatement(const SourcePosition &pos) { resetTopRegister(); }
|
||||
|
||||
|
||||
// Rather than have the ICG client maniplate labels and branches, it
|
||||
// uses the following calls to describe the high level looping constructs
|
||||
// being generated. The functions listed below are expected to be called
|
||||
// in the order listed for each construct, (internal error otherwise).
|
||||
// The ICG will enforce correct nesting and closing.
|
||||
|
||||
|
||||
void beginWhileStatement(const SourcePosition &pos);
|
||||
void endWhileExpression(Register condition);
|
||||
void endWhileStatement();
|
||||
|
||||
|
||||
void beginDoStatement();
|
||||
void endDoStatement();
|
||||
void endDoExpression(Register condition);
|
||||
|
||||
|
||||
void beginIfStatement(const SourcePosition &pos, Register condition);
|
||||
void beginElseStatement(bool hasElse); // required, regardless of existence of else clause
|
||||
void endIfStatement();
|
||||
|
||||
|
||||
// for ( ... in ...) statements get turned into generic for statements by the parser (ok?)
|
||||
void beginForStatement(); // for initialization is emitted prior to this call
|
||||
void forCondition(Register condition); // required with optional <operand>
|
||||
void forIncrement(Register expression); // required with optional <operand>
|
||||
void endForStatement();
|
||||
|
||||
|
||||
void beginSwitchStatement(Register expression);
|
||||
|
||||
// sequences of the next three follow for each case clause
|
||||
void beginCaseStatement();
|
||||
void endCaseCondition(Register expression);
|
||||
void endCaseStatement(); // corresponds to a break and may be omitted
|
||||
|
||||
// optionally
|
||||
void beginDefaultStatement();
|
||||
void endDefaultStatement(); // the break for the default clause, may be omitted
|
||||
|
||||
void endSwitchStatement();
|
||||
|
||||
|
||||
void labelStatement(const StringAtom &label); // adds to label set for next statement,
|
||||
// removed when that statement is finished
|
||||
void continueStatement(const StringAtom &label);
|
||||
|
||||
|
||||
void throwStatement(Register expression);
|
||||
|
||||
|
||||
void returnStatement(Register expression); // optional <operand>
|
||||
|
||||
|
||||
void beginCatchStatement();
|
||||
void endCatchExpression(Register expression);
|
||||
void endCatchStatement();
|
||||
|
||||
};
|
||||
|
||||
ostream &operator<<(ostream &s, ICodeGenerator &i);
|
||||
ostream &operator<<(ostream &s, StringAtom &str);
|
||||
}
|
||||
#endif
|
|
@ -0,0 +1,330 @@
|
|||
// -*- 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 "numerics.h"
|
||||
#include "world.h"
|
||||
#include "utilities.h"
|
||||
#include "icodegenerator.h"
|
||||
|
||||
#include <iomanip>
|
||||
|
||||
namespace JS = JavaScript;
|
||||
using namespace JavaScript;
|
||||
|
||||
ostream &JS::operator<<(ostream &s, ICodeGenerator &i)
|
||||
{
|
||||
return i.print(s);
|
||||
}
|
||||
|
||||
//
|
||||
// ICodeGenerator
|
||||
//
|
||||
|
||||
|
||||
Register ICodeGenerator::loadVariable(int32 frameIndex)
|
||||
{
|
||||
Register dest = getRegister();
|
||||
Instruction_2<int32, Register> *instr = new Instruction_2<int32, Register>(LOAD_VAR, frameIndex, dest);
|
||||
iCode->push_back(instr);
|
||||
return dest;
|
||||
}
|
||||
|
||||
Register ICodeGenerator::loadImmediate(double value)
|
||||
{
|
||||
Register dest = getRegister();
|
||||
Instruction_2<double, Register> *instr = new Instruction_2<double, Register>(LOAD_IMMEDIATE, value, dest);
|
||||
iCode->push_back(instr);
|
||||
return dest;
|
||||
}
|
||||
|
||||
Register ICodeGenerator::loadName(StringAtom &name)
|
||||
{
|
||||
Register dest = getRegister();
|
||||
Instruction_2<StringAtom &, Register> *instr = new Instruction_2<StringAtom &, Register>(LOAD_NAME, name, dest);
|
||||
iCode->push_back(instr);
|
||||
return dest;
|
||||
}
|
||||
|
||||
Register ICodeGenerator::getProperty(StringAtom &name, Register base)
|
||||
{
|
||||
Register dest = getRegister();
|
||||
Instruction_3<StringAtom &, Register, Register> *instr = new Instruction_3<StringAtom &, Register, Register>(GET_PROP, name, base, dest);
|
||||
iCode->push_back(instr);
|
||||
return dest;
|
||||
}
|
||||
|
||||
void ICodeGenerator::saveVariable(int32 frameIndex, Register value)
|
||||
{
|
||||
Instruction_2<int32, Register> *instr = new Instruction_2<int32, Register>(SAVE_VAR, frameIndex, value);
|
||||
iCode->push_back(instr);
|
||||
}
|
||||
|
||||
Register ICodeGenerator::op(ICodeOp op, Register source)
|
||||
{
|
||||
Register dest = getRegister();
|
||||
Instruction_2<Register, Register> *instr = new Instruction_2<Register, Register>(op, source, dest);
|
||||
iCode->push_back(instr);
|
||||
return dest;
|
||||
}
|
||||
|
||||
Register ICodeGenerator::op(ICodeOp op, Register source1, Register source2)
|
||||
{
|
||||
Register dest = getRegister();
|
||||
Instruction_3<Register, Register, Register> *instr = new Instruction_3<Register, Register, Register>(op, source1, source2, dest);
|
||||
iCode->push_back(instr);
|
||||
return dest;
|
||||
}
|
||||
|
||||
void ICodeGenerator::branch(int32 label)
|
||||
{
|
||||
Instruction_1<int32> *instr = new Instruction_1<int32>(BRANCH, label);
|
||||
iCode->push_back(instr);
|
||||
}
|
||||
|
||||
void ICodeGenerator::branchConditional(int32 label, Register condition)
|
||||
{
|
||||
Instruction_2<int32, Register> *instr = new Instruction_2<int32, Register>(BRANCH_COND, label, condition);
|
||||
iCode->push_back(instr);
|
||||
}
|
||||
|
||||
|
||||
/***********************************************************************************************/
|
||||
|
||||
int32 ICodeGenerator::getLabel()
|
||||
{
|
||||
int32 result = labels.size();
|
||||
labels.push_back(new Label(NULL, -1));
|
||||
return result;
|
||||
}
|
||||
|
||||
void ICodeGenerator::setLabel(int32 label)
|
||||
{
|
||||
labels.at(label)->itsBase = iCode;
|
||||
labels.at(label)->itsOffset = iCode->size();
|
||||
}
|
||||
|
||||
|
||||
/***********************************************************************************************/
|
||||
|
||||
void ICodeGenerator::beginWhileStatement(const SourcePosition &pos)
|
||||
{
|
||||
resetTopRegister();
|
||||
|
||||
// insert a branch to the while condition, which we're
|
||||
// moving to follow the while block
|
||||
int32 whileConditionTop = getLabel();
|
||||
int32 whileBlockStart = getLabel();
|
||||
branch(whileConditionTop);
|
||||
|
||||
// save off the current stream while we gen code for the condition
|
||||
stitcher.push(new WhileCodeState(iCode, labels.size(), whileConditionTop, whileBlockStart));
|
||||
|
||||
iCode = new InstructionStream();
|
||||
}
|
||||
|
||||
void ICodeGenerator::endWhileExpression(Register condition)
|
||||
{
|
||||
WhileCodeState *ics = (WhileCodeState *)(stitcher.top());
|
||||
|
||||
branchConditional(ics->whileBodyLabel, condition);
|
||||
resetTopRegister();
|
||||
// stash away the condition expression and switch
|
||||
// back to the main stream
|
||||
iCode = stitcher.top()->swap_iCode(iCode);
|
||||
setLabel(ics->whileBodyLabel); // mark the start of the while block
|
||||
}
|
||||
|
||||
void ICodeGenerator::endWhileStatement()
|
||||
{
|
||||
// recover the while stream
|
||||
WhileCodeState *ics = dynamic_cast<WhileCodeState *>(stitcher.top());
|
||||
stitcher.pop();
|
||||
|
||||
// mark the start of the condition code
|
||||
// and re-attach it to the main stream
|
||||
setLabel(ics->whileConditionLabel);
|
||||
|
||||
if (ics->itsTopLabel < labels.size()) {
|
||||
// labels (might) have been allocated in this stream
|
||||
// we need to adjust their position relative to the
|
||||
// size of the stream we're joining
|
||||
|
||||
// XXXX how do I start at 'ics->itsTopLabel' ???
|
||||
//
|
||||
for (LabelList::iterator i = labels.begin(); i != labels.end(); i++) {
|
||||
if ((*i)->itsBase == ics->its_iCode) {
|
||||
(*i)->itsBase = iCode;
|
||||
(*i)->itsOffset += iCode->size();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (InstructionIterator i = ics->its_iCode->begin(); i != ics->its_iCode->end(); i++)
|
||||
iCode->push_back(*i);
|
||||
|
||||
delete ics->its_iCode;
|
||||
delete ics;
|
||||
|
||||
resetTopRegister();
|
||||
}
|
||||
|
||||
|
||||
/***********************************************************************************************/
|
||||
|
||||
void ICodeGenerator::beginIfStatement(const SourcePosition &pos, Register condition)
|
||||
{
|
||||
int32 elseLabel = getLabel();
|
||||
|
||||
// save off the current stream while we gen code for the condition
|
||||
stitcher.push(new IfCodeState(iCode, labels.size(), elseLabel, -1));
|
||||
|
||||
Register notCond = op(NOT, condition);
|
||||
branchConditional(elseLabel, notCond);
|
||||
|
||||
resetTopRegister();
|
||||
}
|
||||
|
||||
void ICodeGenerator::beginElseStatement(bool hasElse)
|
||||
{
|
||||
IfCodeState *ics = (IfCodeState *)(stitcher.top());
|
||||
|
||||
if (hasElse) {
|
||||
int32 beyondElse = getLabel();
|
||||
ics->beyondElse = beyondElse;
|
||||
branch(beyondElse);
|
||||
}
|
||||
setLabel(ics->elseLabel);
|
||||
resetTopRegister();
|
||||
}
|
||||
|
||||
void ICodeGenerator::endIfStatement()
|
||||
{
|
||||
IfCodeState *ics = (IfCodeState *)(stitcher.top());
|
||||
|
||||
if (ics->beyondElse != -1) { // had an else
|
||||
setLabel(ics->beyondElse); // the beyond else label
|
||||
}
|
||||
stitcher.pop();
|
||||
resetTopRegister();
|
||||
}
|
||||
|
||||
|
||||
/***********************************************************************************************/
|
||||
|
||||
|
||||
char *opcodeName[] = {
|
||||
"load_var",
|
||||
"save_var",
|
||||
|
||||
"load_imm",
|
||||
|
||||
"load_name",
|
||||
"save_name",
|
||||
|
||||
"get_prop",
|
||||
"set_prop",
|
||||
|
||||
"add",
|
||||
"not",
|
||||
|
||||
"branch",
|
||||
"branch_cond",
|
||||
};
|
||||
|
||||
ostream &JS::operator<<(ostream &s, StringAtom &str)
|
||||
{
|
||||
for (String::iterator i = str.begin(); i != str.end(); i++)
|
||||
s << (char)*i;
|
||||
return s;
|
||||
}
|
||||
|
||||
ostream &ICodeGenerator::print(ostream &s)
|
||||
{
|
||||
s << "ICG! " << iCode->size() << "\n";
|
||||
for (InstructionIterator i = iCode->begin(); i != iCode->end(); i++) {
|
||||
for (LabelList::iterator k = labels.begin(); k != labels.end(); k++)
|
||||
if ((*k)->itsOffset == (i - iCode->begin())) {
|
||||
s << "label #" << (k - labels.begin()) << ":\n";
|
||||
}
|
||||
|
||||
Instruction *instr = *i;
|
||||
s << "\t"<< std::setiosflags( std::ostream::left ) << std::setw(16) << opcodeName[instr->itsOp];
|
||||
switch (instr->itsOp) {
|
||||
case LOAD_NAME :
|
||||
{
|
||||
Instruction_2<StringAtom &, Register> *t = static_cast<Instruction_2<StringAtom &, Register> * >(instr);
|
||||
s << "\"" << t->itsOperand1 << "\", R" << t->itsOperand2;
|
||||
}
|
||||
break;
|
||||
case LOAD_IMMEDIATE :
|
||||
{
|
||||
Instruction_2<double, Register> *t = static_cast<Instruction_2<double, Register> * >(instr);
|
||||
s << t->itsOperand1 << ", R" << t->itsOperand2;
|
||||
}
|
||||
break;
|
||||
case LOAD_VAR :
|
||||
case SAVE_VAR :
|
||||
{
|
||||
Instruction_2<int, Register> *t = static_cast<Instruction_2<int, Register> * >(instr);
|
||||
s << "Variable #" << t->itsOperand1 << ", R" << t->itsOperand2;
|
||||
}
|
||||
break;
|
||||
case BRANCH :
|
||||
{
|
||||
Instruction_1<int32> *t = static_cast<Instruction_1<int32> * >(instr);
|
||||
s << "target #" << t->itsOperand1;
|
||||
}
|
||||
break;
|
||||
case BRANCH_COND :
|
||||
{
|
||||
Instruction_2<int32, Register> *t = static_cast<Instruction_2<int32, Register> * >(instr);
|
||||
s << "target #" << t->itsOperand1 << ", R" << t->itsOperand2;
|
||||
}
|
||||
break;
|
||||
case ADD :
|
||||
{
|
||||
Instruction_3<Register, Register, Register> *t = static_cast<Instruction_3<Register, Register, Register> * >(instr);
|
||||
s << "R" << t->itsOperand1 << ", R" << t->itsOperand2 << ", R" << t->itsOperand3;
|
||||
}
|
||||
break;
|
||||
case NOT :
|
||||
{
|
||||
Instruction_3<Register, Register, Register> *t = static_cast<Instruction_3<Register, Register, Register> * >(instr);
|
||||
s << "R" << t->itsOperand1 << ", R" << t->itsOperand2;
|
||||
}
|
||||
break;
|
||||
}
|
||||
s << "\n";
|
||||
}
|
||||
for (LabelList::iterator k = labels.begin(); k != labels.end(); k++)
|
||||
if ((*k)->itsOffset == (iCode->end() - iCode->begin())) {
|
||||
s << "label #" << (k - labels.begin()) << ":\n";
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,244 @@
|
|||
// -*- 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 icodegenerator_h
|
||||
#define icodegenerator_h
|
||||
|
||||
#include "utilities.h"
|
||||
#include "parser.h"
|
||||
|
||||
#include <vector>
|
||||
#include <stack>
|
||||
#include <algorithm>
|
||||
|
||||
namespace JavaScript {
|
||||
|
||||
typedef uint32 Register;
|
||||
|
||||
enum ICodeOp {
|
||||
// Operand1 Operand2 Operand3
|
||||
|
||||
LOAD_VAR, // index of frame slot Destination Register
|
||||
SAVE_VAR, // index of frame slot Source Register
|
||||
|
||||
LOAD_IMMEDIATE, // immediate (double) Destination Register
|
||||
|
||||
LOAD_NAME, // StringAtom & Destination Register
|
||||
SAVE_NAME, // StringAtom & Source Register
|
||||
|
||||
GET_PROP, // StringAtom & Base Register Destination Register
|
||||
SET_PROP, // StringAtom & Base Register Source Register
|
||||
|
||||
ADD, // Source Register 1 Source Register 2 Destination Register
|
||||
NOT, // Source Register Destination Register
|
||||
|
||||
BRANCH, // Target label
|
||||
BRANCH_COND, // Target label Condition Register
|
||||
};
|
||||
|
||||
class Instruction {
|
||||
public:
|
||||
Instruction(ICodeOp op) : itsOp(op) { }
|
||||
ICodeOp itsOp;
|
||||
};
|
||||
|
||||
template <typename Operand1>
|
||||
class Instruction_1 : public Instruction {
|
||||
public:
|
||||
Instruction_1(ICodeOp op, Operand1 operand1) :
|
||||
Instruction(op), itsOperand1(operand1) { }
|
||||
Operand1 itsOperand1;
|
||||
};
|
||||
|
||||
template <typename Operand1, typename Operand2>
|
||||
class Instruction_2 : public Instruction {
|
||||
public:
|
||||
Instruction_2(ICodeOp op, Operand1 operand1, Operand2 operand2) :
|
||||
Instruction(op), itsOperand1(operand1), itsOperand2(operand2) { }
|
||||
Operand1 itsOperand1;
|
||||
Operand2 itsOperand2;
|
||||
};
|
||||
|
||||
template <typename Operand1, typename Operand2, typename Operand3>
|
||||
class Instruction_3 : public Instruction {
|
||||
public:
|
||||
Instruction_3(ICodeOp op, Operand1 operand1, Operand2 operand2, Operand3 operand3) :
|
||||
Instruction(op), itsOperand1(operand1), itsOperand2(operand2), itsOperand3(operand3) { }
|
||||
Operand1 itsOperand1;
|
||||
Operand2 itsOperand2;
|
||||
Operand3 itsOperand3;
|
||||
};
|
||||
|
||||
typedef std::vector<Instruction *> InstructionStream;
|
||||
typedef InstructionStream::iterator InstructionIterator;
|
||||
|
||||
/****************************************************************/
|
||||
|
||||
class Label {
|
||||
public:
|
||||
Label(InstructionStream *base, int32 offset) : itsBase(base), itsOffset(offset) { }
|
||||
|
||||
InstructionStream *itsBase;
|
||||
int32 itsOffset;
|
||||
};
|
||||
|
||||
typedef std::vector<Label *> LabelList;
|
||||
|
||||
/****************************************************************/
|
||||
/****************************************************************/
|
||||
|
||||
class ICodeState {
|
||||
public:
|
||||
ICodeState(InstructionStream *iCode, int32 topLabel) : its_iCode(iCode), itsTopLabel(topLabel) {}
|
||||
virtual ~ICodeState() { }
|
||||
|
||||
InstructionStream *swap_iCode(InstructionStream *iCode) { InstructionStream *t = its_iCode; its_iCode = iCode; return t; }
|
||||
|
||||
InstructionStream *its_iCode;
|
||||
int32 itsTopLabel; // set to the highest label allocated when this stream
|
||||
// was created. If that value changes, this stream may
|
||||
// contain labels that will need to be adjusted when
|
||||
// the streams are merged.
|
||||
};
|
||||
|
||||
class WhileCodeState : public ICodeState {
|
||||
public:
|
||||
WhileCodeState(InstructionStream *iCode, int32 topLabel, int32 a, int32 b)
|
||||
: ICodeState(iCode, topLabel), whileConditionLabel(a), whileBodyLabel(b) { }
|
||||
int32 whileConditionLabel;
|
||||
int32 whileBodyLabel;
|
||||
};
|
||||
|
||||
class IfCodeState : public ICodeState {
|
||||
public:
|
||||
IfCodeState(InstructionStream *iCode, int32 topLabel, int32 a, int32 b)
|
||||
: ICodeState(iCode, topLabel), elseLabel(a), beyondElse(b) { }
|
||||
int32 elseLabel;
|
||||
int32 beyondElse;
|
||||
};
|
||||
|
||||
/****************************************************************/
|
||||
|
||||
// An ICodeGenerator provides the interface between the parser and the interpreter.
|
||||
// The parser constructs one of these for each function/script, adds statements and
|
||||
// expressions to it and then converts it into an ICodeModule, ready for execution.
|
||||
|
||||
class ICodeGenerator {
|
||||
private:
|
||||
|
||||
InstructionStream *iCode;
|
||||
|
||||
LabelList labels;
|
||||
|
||||
std::stack<ICodeState *> stitcher;
|
||||
|
||||
Register topRegister;
|
||||
Register getRegister() { return topRegister++; }
|
||||
void resetTopRegister() { topRegister = 0; }
|
||||
|
||||
int32 getLabel();
|
||||
void setLabel(int32 label);
|
||||
|
||||
void branch(int32 label);
|
||||
void branchConditional(int32 label, Register condition);
|
||||
|
||||
public:
|
||||
ICodeGenerator() { iCode = new InstructionStream(); }
|
||||
|
||||
ostream &print(ostream &s);
|
||||
|
||||
Register op(ICodeOp op, Register source);
|
||||
Register op(ICodeOp op, Register source1, Register source2);
|
||||
|
||||
Register loadVariable(int32 frameIndex);
|
||||
Register loadImmediate(double value);
|
||||
|
||||
void saveVariable(int32 frameIndex, Register value);
|
||||
|
||||
Register loadName(StringAtom &name);
|
||||
Register getProperty(StringAtom &name, Register base);
|
||||
|
||||
|
||||
void beginStatement(const SourcePosition &pos) { resetTopRegister(); }
|
||||
|
||||
|
||||
// Rather than have the ICG client maniplate labels and branches, it
|
||||
// uses the following calls to describe the high level looping constructs
|
||||
// being generated. The functions listed below are expected to be called
|
||||
// in the order listed for each construct, (internal error otherwise).
|
||||
// The ICG will enforce correct nesting and closing.
|
||||
|
||||
|
||||
void beginWhileStatement(const SourcePosition &pos);
|
||||
void endWhileExpression(Register condition);
|
||||
void endWhileStatement();
|
||||
|
||||
|
||||
void beginDoStatement();
|
||||
void endDoStatement();
|
||||
void endDoExpression(Register condition);
|
||||
|
||||
|
||||
void beginIfStatement(const SourcePosition &pos, Register condition);
|
||||
void beginElseStatement(bool hasElse); // required, regardless of existence of else clause
|
||||
void endIfStatement();
|
||||
|
||||
|
||||
// for ( ... in ...) statements get turned into generic for statements by the parser (ok?)
|
||||
void beginForStatement(); // for initialization is emitted prior to this call
|
||||
void forCondition(Register condition); // required with optional <operand>
|
||||
void forIncrement(Register expression); // required with optional <operand>
|
||||
void endForStatement();
|
||||
|
||||
|
||||
void beginSwitchStatement(Register expression);
|
||||
|
||||
// sequences of the next three follow for each case clause
|
||||
void beginCaseStatement();
|
||||
void endCaseCondition(Register expression);
|
||||
void endCaseStatement(); // corresponds to a break and may be omitted
|
||||
|
||||
// optionally
|
||||
void beginDefaultStatement();
|
||||
void endDefaultStatement(); // the break for the default clause, may be omitted
|
||||
|
||||
void endSwitchStatement();
|
||||
|
||||
|
||||
void labelStatement(const StringAtom &label); // adds to label set for next statement,
|
||||
// removed when that statement is finished
|
||||
void continueStatement(const StringAtom &label);
|
||||
|
||||
|
||||
void throwStatement(Register expression);
|
||||
|
||||
|
||||
void returnStatement(Register expression); // optional <operand>
|
||||
|
||||
|
||||
void beginCatchStatement();
|
||||
void endCatchExpression(Register expression);
|
||||
void endCatchStatement();
|
||||
|
||||
};
|
||||
|
||||
ostream &operator<<(ostream &s, ICodeGenerator &i);
|
||||
ostream &operator<<(ostream &s, StringAtom &str);
|
||||
}
|
||||
#endif
|
Загрузка…
Ссылка в новой задаче