2000-03-29 19:19:23 +00: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
|
|
|
|
// Copyright (C) 1998 Netscape Communications Corporation. All
|
|
|
|
// Rights Reserved.
|
|
|
|
|
|
|
|
#include "numerics.h"
|
|
|
|
#include "world.h"
|
|
|
|
#include "utilities.h"
|
|
|
|
#include "icodegenerator.h"
|
|
|
|
|
|
|
|
#include <iomanip>
|
2000-03-31 00:42:25 +00:00
|
|
|
#include <stdexcept>
|
2000-03-29 19:19:23 +00:00
|
|
|
|
2000-04-06 23:40:47 +00:00
|
|
|
|
2000-04-05 23:41:58 +00:00
|
|
|
namespace JavaScript {
|
2000-03-29 19:19:23 +00:00
|
|
|
|
2000-04-06 23:43:08 +00:00
|
|
|
std::ostream & operator<<(std::ostream &s, ICodeGenerator &i)
|
2000-04-05 23:41:58 +00:00
|
|
|
{
|
|
|
|
return i.print(s);
|
|
|
|
}
|
2000-03-29 19:19:23 +00:00
|
|
|
|
2000-04-05 23:41:58 +00:00
|
|
|
//
|
|
|
|
// ICodeGenerator
|
|
|
|
//
|
|
|
|
|
2000-04-08 01:04:55 +00:00
|
|
|
ICodeModule *ICodeGenerator::complete()
|
2000-04-05 23:41:58 +00:00
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
|
|
|
ASSERT(stitcher.empty());
|
|
|
|
for (LabelIterator i = labels.begin(); i != labels.end(); i++) {
|
|
|
|
ASSERT((*i)->itsBase == iCode);
|
|
|
|
ASSERT((*i)->itsOffset < iCode->size());
|
2000-03-31 01:54:28 +00:00
|
|
|
}
|
2000-04-06 00:04:11 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
for (InstructionIterator ii = iCode->begin(); ii != iCode->end(); ii++) {
|
2000-04-07 22:19:36 +00:00
|
|
|
if ((*ii)->itsOp == BRANCH) {
|
|
|
|
Instruction *t = *ii;
|
|
|
|
*ii = new ResolvedBranch(static_cast<Branch *>(*ii)->itsOperand1->itsOffset);
|
|
|
|
delete t;
|
|
|
|
}
|
2000-04-06 00:04:11 +00:00
|
|
|
else
|
2000-04-07 22:19:36 +00:00
|
|
|
if ((*ii)->itsOp >= BRANCH_LT && (*ii)->itsOp <= BRANCH_GT) {
|
|
|
|
Instruction *t = *ii;
|
|
|
|
*ii = new ResolvedBranchCond((*ii)->itsOp,
|
|
|
|
static_cast<BranchCond *>(*ii)->itsOperand1->itsOffset,
|
|
|
|
static_cast<BranchCond *>(*ii)->itsOperand2);
|
|
|
|
delete t;
|
|
|
|
}
|
2000-04-06 00:04:11 +00:00
|
|
|
}
|
2000-04-08 01:04:55 +00:00
|
|
|
markMaxRegister();
|
2000-04-06 00:04:11 +00:00
|
|
|
|
2000-04-08 01:04:55 +00:00
|
|
|
return new ICodeModule(iCode, maxRegister, maxVariable);
|
2000-04-05 23:41:58 +00:00
|
|
|
}
|
2000-04-06 02:57:42 +00:00
|
|
|
|
2000-04-05 23:41:58 +00:00
|
|
|
/***********************************************************************************************/
|
|
|
|
|
2000-04-06 02:57:42 +00:00
|
|
|
Register ICodeGenerator::loadVariable(uint32 frameIndex)
|
2000-04-05 23:41:58 +00:00
|
|
|
{
|
2000-04-08 01:04:55 +00:00
|
|
|
markMaxVariable(frameIndex);
|
2000-04-05 23:41:58 +00:00
|
|
|
Register dest = getRegister();
|
2000-04-07 22:19:36 +00:00
|
|
|
LoadVar *instr = new LoadVar(LOAD_VAR, dest, frameIndex);
|
2000-04-05 23:41:58 +00:00
|
|
|
iCode->push_back(instr);
|
|
|
|
return dest;
|
|
|
|
}
|
|
|
|
|
2000-04-08 01:04:55 +00:00
|
|
|
void ICodeGenerator::saveVariable(uint32 frameIndex, Register value)
|
|
|
|
{
|
|
|
|
markMaxVariable(frameIndex);
|
|
|
|
SaveVar *instr = new SaveVar(SAVE_VAR, frameIndex, value);
|
|
|
|
iCode->push_back(instr);
|
|
|
|
}
|
|
|
|
|
2000-04-05 23:41:58 +00:00
|
|
|
Register ICodeGenerator::loadImmediate(double value)
|
|
|
|
{
|
|
|
|
Register dest = getRegister();
|
2000-04-07 22:19:36 +00:00
|
|
|
LoadImmediate *instr = new LoadImmediate(LOAD_IMMEDIATE, dest, value);
|
2000-04-05 23:41:58 +00:00
|
|
|
iCode->push_back(instr);
|
|
|
|
return dest;
|
|
|
|
}
|
|
|
|
|
2000-04-07 02:41:08 +00:00
|
|
|
Register ICodeGenerator::newObject()
|
|
|
|
{
|
|
|
|
Register dest = getRegister();
|
|
|
|
NewObject *instr = new NewObject(dest);
|
|
|
|
iCode->push_back(instr);
|
|
|
|
return dest;
|
|
|
|
}
|
|
|
|
|
2000-04-05 23:41:58 +00:00
|
|
|
Register ICodeGenerator::loadName(StringAtom &name)
|
|
|
|
{
|
|
|
|
Register dest = getRegister();
|
2000-04-07 22:19:36 +00:00
|
|
|
LoadName *instr = new LoadName(LOAD_NAME, dest, &name);
|
2000-04-05 23:41:58 +00:00
|
|
|
iCode->push_back(instr);
|
|
|
|
return dest;
|
|
|
|
}
|
2000-04-07 02:41:08 +00:00
|
|
|
|
|
|
|
void ICodeGenerator::saveName(StringAtom &name, Register value)
|
|
|
|
{
|
|
|
|
SaveName *instr = new SaveName(SAVE_NAME, &name, value);
|
|
|
|
iCode->push_back(instr);
|
|
|
|
}
|
2000-04-05 23:41:58 +00:00
|
|
|
|
|
|
|
Register ICodeGenerator::getProperty(StringAtom &name, Register base)
|
|
|
|
{
|
|
|
|
Register dest = getRegister();
|
2000-04-07 22:19:36 +00:00
|
|
|
GetProp *instr = new GetProp(GET_PROP, dest, base, &name);
|
2000-04-05 23:41:58 +00:00
|
|
|
iCode->push_back(instr);
|
|
|
|
return dest;
|
2000-03-31 01:54:28 +00:00
|
|
|
}
|
|
|
|
|
2000-04-07 02:41:08 +00:00
|
|
|
void ICodeGenerator::setProperty(StringAtom &name, Register base, Register value)
|
|
|
|
{
|
|
|
|
SetProp *instr = new SetProp(SET_PROP, &name, base, value);
|
|
|
|
iCode->push_back(instr);
|
|
|
|
}
|
|
|
|
|
2000-04-05 23:41:58 +00:00
|
|
|
Register ICodeGenerator::op(ICodeOp op, Register source)
|
|
|
|
{
|
|
|
|
Register dest = getRegister();
|
2000-04-07 22:19:36 +00:00
|
|
|
Move *instr = new Move(op, dest, source);
|
2000-04-05 23:41:58 +00:00
|
|
|
iCode->push_back(instr);
|
|
|
|
return dest;
|
|
|
|
}
|
2000-03-29 19:19:23 +00:00
|
|
|
|
2000-04-05 23:41:58 +00:00
|
|
|
Register ICodeGenerator::op(ICodeOp op, Register source1, Register source2)
|
|
|
|
{
|
|
|
|
Register dest = getRegister();
|
2000-04-07 22:19:36 +00:00
|
|
|
Arithmetic *instr = new Arithmetic(op, dest, source1, source2);
|
2000-04-05 23:41:58 +00:00
|
|
|
iCode->push_back(instr);
|
|
|
|
return dest;
|
|
|
|
}
|
2000-03-29 19:19:23 +00:00
|
|
|
|
2000-04-07 22:19:36 +00:00
|
|
|
void ICodeGenerator::branch(Label *label)
|
2000-04-05 23:41:58 +00:00
|
|
|
{
|
|
|
|
Branch *instr = new Branch(BRANCH, label);
|
|
|
|
iCode->push_back(instr);
|
|
|
|
}
|
|
|
|
|
2000-04-07 22:19:36 +00:00
|
|
|
void ICodeGenerator::branchConditional(Label *label, Register condition)
|
2000-04-05 23:41:58 +00:00
|
|
|
{
|
2000-04-06 22:40:17 +00:00
|
|
|
ICodeOp branchOp = getBranchOp();
|
|
|
|
if (branchOp == NOP) {
|
|
|
|
// XXXX emit convert to boolean / Test / ...
|
|
|
|
branchOp = BRANCH_NE;
|
|
|
|
}
|
2000-04-06 02:57:42 +00:00
|
|
|
BranchCond *instr = new BranchCond(branchOp, label, condition);
|
2000-04-05 23:41:58 +00:00
|
|
|
iCode->push_back(instr);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************************************/
|
|
|
|
|
2000-04-07 22:19:36 +00:00
|
|
|
Label *ICodeGenerator::getLabel()
|
2000-04-05 23:41:58 +00:00
|
|
|
{
|
2000-04-07 22:19:36 +00:00
|
|
|
labels.push_back(new Label(NULL));
|
|
|
|
return labels.back();
|
2000-04-05 23:41:58 +00:00
|
|
|
}
|
|
|
|
|
2000-04-07 22:19:36 +00:00
|
|
|
void ICodeGenerator::setLabel(Label *l)
|
2000-04-05 23:41:58 +00:00
|
|
|
{
|
|
|
|
l->itsBase = iCode;
|
|
|
|
l->itsOffset = static_cast<int32>(iCode->size());
|
|
|
|
}
|
|
|
|
|
2000-04-07 22:19:36 +00:00
|
|
|
void ICodeGenerator::setLabel(InstructionStream *stream, Label *l)
|
2000-04-05 23:41:58 +00:00
|
|
|
{
|
|
|
|
l->itsBase = stream;
|
|
|
|
l->itsOffset = static_cast<int32>(stream->size());
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************************************/
|
|
|
|
|
|
|
|
void ICodeGenerator::mergeStream(InstructionStream *sideStream)
|
|
|
|
{
|
|
|
|
// change InstructionStream to be a class that also remembers
|
|
|
|
// if it contains any labels (maybe even remembers the labels
|
|
|
|
// themselves?) in order to avoid running this loop unnecessarily.
|
|
|
|
for (LabelList::iterator i = labels.begin(); i != labels.end(); i++) {
|
|
|
|
if ((*i)->itsBase == sideStream) {
|
|
|
|
(*i)->itsBase = iCode;
|
|
|
|
(*i)->itsOffset += iCode->size();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-04-06 22:40:17 +00:00
|
|
|
for (InstructionIterator ii = sideStream->begin(); ii != sideStream->end(); ii++) {
|
2000-04-05 23:41:58 +00:00
|
|
|
iCode->push_back(*ii);
|
2000-04-06 22:40:17 +00:00
|
|
|
}
|
2000-04-05 23:41:58 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************************************/
|
|
|
|
|
|
|
|
void ICodeGenerator::beginWhileStatement(uint32)
|
|
|
|
{
|
|
|
|
resetTopRegister();
|
2000-03-29 19:19:23 +00:00
|
|
|
|
2000-04-05 23:41:58 +00:00
|
|
|
// insert a branch to the while condition, which we're
|
|
|
|
// moving to follow the while block
|
2000-04-07 22:19:36 +00:00
|
|
|
Label *whileConditionTop = getLabel();
|
|
|
|
Label *whileBlockStart = getLabel();
|
2000-04-05 23:41:58 +00:00
|
|
|
branch(whileConditionTop);
|
2000-03-29 19:19:23 +00:00
|
|
|
|
2000-04-05 23:41:58 +00:00
|
|
|
// save off the current stream while we gen code for the condition
|
|
|
|
stitcher.push_back(new WhileCodeState(whileConditionTop, whileBlockStart, this));
|
|
|
|
|
|
|
|
iCode = new InstructionStream();
|
|
|
|
}
|
|
|
|
|
2000-04-06 22:40:17 +00:00
|
|
|
void ICodeGenerator::endWhileExpression(Register condition)
|
2000-04-05 23:41:58 +00:00
|
|
|
{
|
|
|
|
WhileCodeState *ics = static_cast<WhileCodeState *>(stitcher.back());
|
|
|
|
ASSERT(ics->stateKind == While_state);
|
|
|
|
|
2000-04-06 22:40:17 +00:00
|
|
|
branchConditional(ics->whileBody, condition);
|
2000-04-05 23:41:58 +00:00
|
|
|
resetTopRegister();
|
|
|
|
// stash away the condition expression and switch
|
|
|
|
// back to the main stream
|
|
|
|
iCode = ics->swapStream(iCode);
|
|
|
|
setLabel(ics->whileBody); // mark the start of the while block
|
|
|
|
}
|
2000-03-29 19:19:23 +00:00
|
|
|
|
2000-04-05 23:41:58 +00:00
|
|
|
void ICodeGenerator::endWhileStatement()
|
|
|
|
{
|
|
|
|
// recover the while stream
|
|
|
|
WhileCodeState *ics = static_cast<WhileCodeState *>(stitcher.back());
|
|
|
|
ASSERT(ics->stateKind == While_state);
|
|
|
|
stitcher.pop_back();
|
2000-03-29 19:19:23 +00:00
|
|
|
|
2000-04-05 23:41:58 +00:00
|
|
|
// mark the start of the condition code
|
|
|
|
setLabel(ics->whileCondition); // which is where continues will target
|
2000-04-04 01:48:35 +00:00
|
|
|
|
2000-04-05 23:41:58 +00:00
|
|
|
// and re-attach it to the main stream
|
|
|
|
mergeStream(ics->whileExpressionStream);
|
2000-03-29 19:19:23 +00:00
|
|
|
|
2000-04-07 22:19:36 +00:00
|
|
|
if (ics->breakLabel != NULL)
|
2000-04-05 23:41:58 +00:00
|
|
|
setLabel(ics->breakLabel);
|
2000-03-29 19:19:23 +00:00
|
|
|
|
2000-04-05 23:41:58 +00:00
|
|
|
delete ics;
|
2000-03-29 19:19:23 +00:00
|
|
|
|
2000-04-05 23:41:58 +00:00
|
|
|
resetTopRegister();
|
|
|
|
}
|
2000-03-29 19:19:23 +00:00
|
|
|
|
2000-04-05 23:41:58 +00:00
|
|
|
/***********************************************************************************************/
|
2000-04-01 01:30:32 +00:00
|
|
|
|
2000-04-05 23:41:58 +00:00
|
|
|
void ICodeGenerator::beginForStatement(uint32)
|
|
|
|
{
|
2000-04-07 22:19:36 +00:00
|
|
|
Label *forCondition = getLabel();
|
2000-04-01 02:53:16 +00:00
|
|
|
|
2000-04-05 23:41:58 +00:00
|
|
|
ForCodeState *ics = new ForCodeState(forCondition, getLabel(), this);
|
2000-04-01 02:53:16 +00:00
|
|
|
|
2000-04-05 23:41:58 +00:00
|
|
|
branch(forCondition);
|
2000-04-01 02:53:16 +00:00
|
|
|
|
2000-04-05 23:41:58 +00:00
|
|
|
stitcher.push_back(ics);
|
2000-04-01 02:53:16 +00:00
|
|
|
|
2000-04-05 23:41:58 +00:00
|
|
|
iCode = new InstructionStream(); // begin the stream for collecting the condition expression
|
|
|
|
setLabel(forCondition);
|
2000-04-01 02:53:16 +00:00
|
|
|
|
2000-04-05 23:41:58 +00:00
|
|
|
resetTopRegister();
|
|
|
|
}
|
2000-04-01 02:53:16 +00:00
|
|
|
|
2000-04-05 23:41:58 +00:00
|
|
|
void ICodeGenerator::forCondition(Register condition)
|
|
|
|
{
|
|
|
|
ForCodeState *ics = static_cast<ForCodeState *>(stitcher.back());
|
|
|
|
ASSERT(ics->stateKind == For_state);
|
2000-04-01 02:53:16 +00:00
|
|
|
|
2000-04-05 23:41:58 +00:00
|
|
|
// finsh off the test expression by adding the branch to the body
|
|
|
|
branchConditional(ics->forBody, condition);
|
2000-04-01 02:53:16 +00:00
|
|
|
|
2000-04-05 23:41:58 +00:00
|
|
|
iCode = ics->swapStream(iCode); // switch back to main stream
|
|
|
|
iCode = new InstructionStream(); // begin the stream for collecting the increment expression
|
2000-04-01 02:53:16 +00:00
|
|
|
|
2000-04-05 23:41:58 +00:00
|
|
|
ics->continueLabel = getLabel();
|
|
|
|
setLabel(ics->continueLabel); // can't lazily insert this since we haven't seen the body yet
|
|
|
|
// ??? could just remember the offset
|
2000-04-04 01:48:35 +00:00
|
|
|
|
2000-04-05 23:41:58 +00:00
|
|
|
resetTopRegister();
|
|
|
|
}
|
2000-04-01 02:53:16 +00:00
|
|
|
|
2000-04-05 23:41:58 +00:00
|
|
|
void ICodeGenerator::forIncrement()
|
|
|
|
{
|
|
|
|
ForCodeState *ics = static_cast<ForCodeState *>(stitcher.back());
|
|
|
|
ASSERT(ics->stateKind == For_state);
|
2000-04-01 02:53:16 +00:00
|
|
|
|
2000-04-05 23:41:58 +00:00
|
|
|
// now switch back to the main stream
|
|
|
|
iCode = ics->swapStream2(iCode);
|
|
|
|
setLabel(ics->forBody);
|
2000-04-01 02:53:16 +00:00
|
|
|
|
2000-04-05 23:41:58 +00:00
|
|
|
resetTopRegister();
|
|
|
|
}
|
2000-04-01 02:53:16 +00:00
|
|
|
|
2000-04-05 23:41:58 +00:00
|
|
|
void ICodeGenerator::endForStatement()
|
|
|
|
{
|
|
|
|
ForCodeState *ics = static_cast<ForCodeState *>(stitcher.back());
|
|
|
|
ASSERT(ics->stateKind == For_state);
|
|
|
|
stitcher.pop_back();
|
2000-04-01 02:53:16 +00:00
|
|
|
|
2000-04-05 23:41:58 +00:00
|
|
|
mergeStream(ics->forIncrementStream);
|
|
|
|
mergeStream(ics->forConditionStream);
|
2000-04-01 02:53:16 +00:00
|
|
|
|
2000-04-07 22:19:36 +00:00
|
|
|
if (ics->breakLabel != NULL)
|
2000-04-05 23:41:58 +00:00
|
|
|
setLabel(ics->breakLabel);
|
2000-04-01 02:53:16 +00:00
|
|
|
|
2000-04-05 23:41:58 +00:00
|
|
|
delete ics;
|
|
|
|
}
|
2000-04-01 02:53:16 +00:00
|
|
|
|
2000-04-05 23:41:58 +00:00
|
|
|
/***********************************************************************************************/
|
2000-04-01 02:53:16 +00:00
|
|
|
|
2000-04-05 23:41:58 +00:00
|
|
|
void ICodeGenerator::beginDoStatement(uint32)
|
|
|
|
{
|
|
|
|
resetTopRegister();
|
2000-04-01 01:30:32 +00:00
|
|
|
|
2000-04-05 23:41:58 +00:00
|
|
|
// mark the top of the loop body
|
|
|
|
// and reserve a label for the condition
|
2000-04-07 22:19:36 +00:00
|
|
|
Label *doBlock = getLabel();
|
|
|
|
Label *doCondition = getLabel();
|
2000-04-05 23:41:58 +00:00
|
|
|
setLabel(doBlock);
|
2000-04-01 01:30:32 +00:00
|
|
|
|
2000-04-05 23:41:58 +00:00
|
|
|
stitcher.push_back(new DoCodeState(doBlock, doCondition, this));
|
2000-04-01 01:30:32 +00:00
|
|
|
|
2000-04-05 23:41:58 +00:00
|
|
|
iCode = new InstructionStream();
|
|
|
|
}
|
2000-04-01 01:30:32 +00:00
|
|
|
|
2000-04-05 23:41:58 +00:00
|
|
|
void ICodeGenerator::endDoStatement()
|
|
|
|
{
|
|
|
|
DoCodeState *ics = static_cast<DoCodeState *>(stitcher.back());
|
|
|
|
ASSERT(ics->stateKind == Do_state);
|
2000-04-01 01:30:32 +00:00
|
|
|
|
2000-04-05 23:41:58 +00:00
|
|
|
// mark the start of the do conditional
|
|
|
|
setLabel(ics->doCondition);
|
2000-04-07 22:19:36 +00:00
|
|
|
if (ics->continueLabel != NULL)
|
2000-04-05 23:41:58 +00:00
|
|
|
setLabel(ics->continueLabel);
|
2000-04-01 01:30:32 +00:00
|
|
|
|
2000-04-05 23:41:58 +00:00
|
|
|
resetTopRegister();
|
2000-04-01 01:30:32 +00:00
|
|
|
}
|
2000-04-05 23:41:58 +00:00
|
|
|
|
|
|
|
void ICodeGenerator::endDoExpression(Register condition)
|
|
|
|
{
|
|
|
|
DoCodeState *ics = static_cast<DoCodeState *>(stitcher.back());
|
|
|
|
ASSERT(ics->stateKind == Do_state);
|
|
|
|
stitcher.pop_back();
|
|
|
|
|
|
|
|
// add branch to top of do block
|
|
|
|
branchConditional(ics->doBody, condition);
|
2000-04-07 22:19:36 +00:00
|
|
|
if (ics->breakLabel != NULL)
|
2000-04-05 23:41:58 +00:00
|
|
|
setLabel(ics->breakLabel);
|
|
|
|
|
|
|
|
delete ics;
|
2000-04-01 01:30:32 +00:00
|
|
|
|
2000-04-05 23:41:58 +00:00
|
|
|
resetTopRegister();
|
|
|
|
}
|
2000-04-01 01:30:32 +00:00
|
|
|
|
2000-04-05 23:41:58 +00:00
|
|
|
/***********************************************************************************************/
|
|
|
|
|
|
|
|
void ICodeGenerator::beginSwitchStatement(uint32, Register expression)
|
|
|
|
{
|
|
|
|
// stash the control expression value
|
|
|
|
resetTopRegister();
|
|
|
|
Register control = op(MOVE_TO, expression);
|
|
|
|
// build an instruction stream for the case statements, the case
|
|
|
|
// expressions are generated into the main stream directly, the
|
|
|
|
// case statements are then added back in afterwards.
|
|
|
|
InstructionStream *x = new InstructionStream();
|
|
|
|
SwitchCodeState *ics = new SwitchCodeState(control, this);
|
|
|
|
ics->swapStream(x);
|
|
|
|
stitcher.push_back(ics);
|
|
|
|
}
|
2000-04-01 01:30:32 +00:00
|
|
|
|
2000-04-05 23:41:58 +00:00
|
|
|
void ICodeGenerator::endCaseCondition(Register expression)
|
|
|
|
{
|
|
|
|
SwitchCodeState *ics = static_cast<SwitchCodeState *>(stitcher.back());
|
|
|
|
ASSERT(ics->stateKind == Switch_state);
|
2000-03-29 19:19:23 +00:00
|
|
|
|
2000-04-07 22:19:36 +00:00
|
|
|
Label *caseLabel = getLabel();
|
2000-04-06 22:40:17 +00:00
|
|
|
Register r = op(COMPARE_EQ, expression, ics->controlExpression);
|
2000-04-05 23:41:58 +00:00
|
|
|
branchConditional(caseLabel, r);
|
2000-03-29 19:19:23 +00:00
|
|
|
|
2000-04-05 23:41:58 +00:00
|
|
|
setLabel(ics->caseStatementsStream, caseLabel); // mark the case in the Case Statement stream
|
|
|
|
resetTopRegister();
|
|
|
|
}
|
2000-03-29 19:19:23 +00:00
|
|
|
|
2000-04-05 23:41:58 +00:00
|
|
|
void ICodeGenerator::beginCaseStatement()
|
|
|
|
{
|
|
|
|
SwitchCodeState *ics = static_cast<SwitchCodeState *>(stitcher.back());
|
|
|
|
ASSERT(ics->stateKind == Switch_state);
|
|
|
|
iCode = ics->swapStream(iCode); // switch to Case Statement stream
|
|
|
|
}
|
2000-03-29 19:19:23 +00:00
|
|
|
|
2000-04-05 23:41:58 +00:00
|
|
|
void ICodeGenerator::endCaseStatement()
|
|
|
|
{
|
|
|
|
SwitchCodeState *ics = static_cast<SwitchCodeState *>(stitcher.back());
|
|
|
|
ASSERT(ics->stateKind == Switch_state); // do more to guarantee correct blocking?
|
|
|
|
iCode = ics->swapStream(iCode); // switch back to Case Conditional stream
|
|
|
|
resetTopRegister();
|
|
|
|
}
|
2000-03-29 19:19:23 +00:00
|
|
|
|
2000-04-05 23:41:58 +00:00
|
|
|
void ICodeGenerator::beginDefaultStatement()
|
|
|
|
{
|
|
|
|
SwitchCodeState *ics = static_cast<SwitchCodeState *>(stitcher.back());
|
|
|
|
ASSERT(ics->stateKind == Switch_state);
|
2000-04-07 22:19:36 +00:00
|
|
|
ASSERT(ics->defaultLabel == NULL);
|
2000-04-05 23:41:58 +00:00
|
|
|
ics->defaultLabel = getLabel();
|
|
|
|
setLabel(ics->caseStatementsStream, ics->defaultLabel);
|
|
|
|
iCode = ics->swapStream(iCode); // switch to Case Statement stream
|
|
|
|
}
|
2000-03-29 19:19:23 +00:00
|
|
|
|
2000-04-05 23:41:58 +00:00
|
|
|
void ICodeGenerator::endDefaultStatement()
|
|
|
|
{
|
|
|
|
SwitchCodeState *ics = static_cast<SwitchCodeState *>(stitcher.back());
|
|
|
|
ASSERT(ics->stateKind == Switch_state);
|
2000-04-07 22:19:36 +00:00
|
|
|
ASSERT(ics->defaultLabel != NULL); // do more to guarantee correct blocking?
|
2000-04-05 23:41:58 +00:00
|
|
|
iCode = ics->swapStream(iCode); // switch to Case Statement stream
|
|
|
|
resetTopRegister();
|
2000-03-29 19:19:23 +00:00
|
|
|
}
|
|
|
|
|
2000-04-05 23:41:58 +00:00
|
|
|
void ICodeGenerator::endSwitchStatement()
|
|
|
|
{
|
|
|
|
SwitchCodeState *ics = static_cast<SwitchCodeState *>(stitcher.back());
|
|
|
|
ASSERT(ics->stateKind == Switch_state);
|
|
|
|
stitcher.pop_back();
|
|
|
|
|
|
|
|
// ground out the case chain at the default block or fall thru
|
|
|
|
// to the break label
|
2000-04-07 22:19:36 +00:00
|
|
|
if (ics->defaultLabel != NULL)
|
2000-04-05 23:41:58 +00:00
|
|
|
branch(ics->defaultLabel);
|
|
|
|
else {
|
2000-04-07 22:19:36 +00:00
|
|
|
if (ics->breakLabel == NULL)
|
2000-04-05 23:41:58 +00:00
|
|
|
ics->breakLabel = getLabel();
|
|
|
|
branch(ics->breakLabel);
|
|
|
|
}
|
|
|
|
|
|
|
|
// dump all the case statements into the main stream
|
|
|
|
mergeStream(ics->caseStatementsStream);
|
|
|
|
|
2000-04-07 22:19:36 +00:00
|
|
|
if (ics->breakLabel != NULL)
|
2000-04-05 23:41:58 +00:00
|
|
|
setLabel(ics->breakLabel);
|
2000-03-29 19:19:23 +00:00
|
|
|
|
2000-04-05 23:41:58 +00:00
|
|
|
delete ics;
|
2000-03-29 19:19:23 +00:00
|
|
|
}
|
2000-04-05 23:41:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************************************/
|
|
|
|
|
|
|
|
void ICodeGenerator::beginIfStatement(uint32, Register condition)
|
|
|
|
{
|
2000-04-07 22:19:36 +00:00
|
|
|
Label *elseLabel = getLabel();
|
2000-04-01 01:30:32 +00:00
|
|
|
|
2000-04-07 22:19:36 +00:00
|
|
|
stitcher.push_back(new IfCodeState(elseLabel, NULL, this));
|
2000-04-05 23:41:58 +00:00
|
|
|
|
|
|
|
Register notCond = op(NOT, condition);
|
|
|
|
branchConditional(elseLabel, notCond);
|
|
|
|
|
|
|
|
resetTopRegister();
|
2000-04-01 01:30:32 +00:00
|
|
|
}
|
2000-03-29 19:19:23 +00:00
|
|
|
|
2000-04-05 23:41:58 +00:00
|
|
|
void ICodeGenerator::beginElseStatement(bool hasElse)
|
|
|
|
{
|
|
|
|
IfCodeState *ics = static_cast<IfCodeState *>(stitcher.back());
|
|
|
|
ASSERT(ics->stateKind == If_state);
|
|
|
|
|
|
|
|
if (hasElse) {
|
2000-04-07 22:19:36 +00:00
|
|
|
Label *beyondElse = getLabel();
|
2000-04-05 23:41:58 +00:00
|
|
|
ics->beyondElse = beyondElse;
|
|
|
|
branch(beyondElse);
|
2000-04-04 01:48:35 +00:00
|
|
|
}
|
2000-04-05 23:41:58 +00:00
|
|
|
setLabel(ics->elseLabel);
|
|
|
|
resetTopRegister();
|
2000-04-04 01:48:35 +00:00
|
|
|
}
|
2000-04-05 23:41:58 +00:00
|
|
|
|
|
|
|
void ICodeGenerator::endIfStatement()
|
|
|
|
{
|
|
|
|
IfCodeState *ics = static_cast<IfCodeState *>(stitcher.back());
|
|
|
|
ASSERT(ics->stateKind == If_state);
|
|
|
|
stitcher.pop_back();
|
|
|
|
|
2000-04-07 22:19:36 +00:00
|
|
|
if (ics->beyondElse != NULL) { // had an else
|
2000-04-05 23:41:58 +00:00
|
|
|
setLabel(ics->beyondElse); // the beyond else label
|
2000-03-29 19:19:23 +00:00
|
|
|
}
|
2000-04-05 23:41:58 +00:00
|
|
|
|
|
|
|
delete ics;
|
|
|
|
resetTopRegister();
|
2000-03-29 19:19:23 +00:00
|
|
|
}
|
|
|
|
|
2000-04-05 23:41:58 +00:00
|
|
|
/***********************************************************************************************/
|
2000-03-29 19:19:23 +00:00
|
|
|
|
2000-04-05 23:41:58 +00:00
|
|
|
void ICodeGenerator::breakStatement()
|
|
|
|
{
|
|
|
|
for (std::vector<ICodeState *>::reverse_iterator p = stitcher.rbegin(); p != stitcher.rend(); p++) {
|
2000-04-07 22:19:36 +00:00
|
|
|
if ((*p)->breakLabel != NULL) {
|
2000-04-05 23:41:58 +00:00
|
|
|
branch((*p)->breakLabel);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (((*p)->stateKind == While_state)
|
|
|
|
|| ((*p)->stateKind == Do_state)
|
|
|
|
|| ((*p)->stateKind == For_state)
|
|
|
|
|| ((*p)->stateKind == Switch_state)) {
|
|
|
|
(*p)->breakLabel = getLabel();
|
|
|
|
branch((*p)->breakLabel);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
NOT_REACHED("no break target available");
|
|
|
|
}
|
2000-03-29 19:19:23 +00:00
|
|
|
|
2000-04-05 23:41:58 +00:00
|
|
|
void ICodeGenerator::continueStatement()
|
|
|
|
{
|
|
|
|
for (std::vector<ICodeState *>::reverse_iterator p = stitcher.rbegin(); p != stitcher.rend(); p++) {
|
2000-04-07 22:19:36 +00:00
|
|
|
if ((*p)->continueLabel != NULL) {
|
2000-04-05 23:41:58 +00:00
|
|
|
branch((*p)->continueLabel);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (((*p)->stateKind == While_state)
|
|
|
|
|| ((*p)->stateKind == Do_state)
|
|
|
|
|| ((*p)->stateKind == For_state)) {
|
|
|
|
(*p)->continueLabel = getLabel();
|
|
|
|
branch((*p)->continueLabel);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
NOT_REACHED("no continue target available");
|
|
|
|
}
|
2000-03-29 19:19:23 +00:00
|
|
|
|
2000-04-06 22:40:17 +00:00
|
|
|
void ICodeGenerator::returnStatement(Register result)
|
|
|
|
{
|
2000-04-07 02:41:08 +00:00
|
|
|
Return *instr = new Return(result);
|
2000-04-06 22:40:17 +00:00
|
|
|
iCode->push_back(instr);
|
|
|
|
}
|
|
|
|
|
2000-04-05 23:41:58 +00:00
|
|
|
/***********************************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
char *opcodeName[] = {
|
2000-04-06 22:40:17 +00:00
|
|
|
"nop",
|
2000-04-05 23:41:58 +00:00
|
|
|
"move_to",
|
|
|
|
"load_var",
|
|
|
|
"save_var",
|
|
|
|
"load_imm",
|
|
|
|
"load_name",
|
|
|
|
"save_name",
|
2000-04-07 02:41:08 +00:00
|
|
|
"new_object",
|
2000-04-05 23:41:58 +00:00
|
|
|
"get_prop",
|
|
|
|
"set_prop",
|
|
|
|
"add",
|
2000-04-06 02:57:42 +00:00
|
|
|
"subtract",
|
|
|
|
"multiply",
|
|
|
|
"divide",
|
2000-04-05 23:41:58 +00:00
|
|
|
"compare",
|
2000-04-06 22:40:17 +00:00
|
|
|
"compare",
|
|
|
|
"compare",
|
|
|
|
"compare",
|
|
|
|
"compare",
|
|
|
|
"compare",
|
2000-04-05 23:41:58 +00:00
|
|
|
"not",
|
|
|
|
"branch",
|
2000-04-06 02:57:42 +00:00
|
|
|
"branch_lt",
|
|
|
|
"branch_le",
|
|
|
|
"branch_eq",
|
|
|
|
"branch_ne",
|
|
|
|
"branch_ge",
|
|
|
|
"branch_gt",
|
|
|
|
"return"
|
2000-04-05 23:41:58 +00:00
|
|
|
};
|
|
|
|
|
2000-04-06 23:43:08 +00:00
|
|
|
std::ostream &operator<<(std::ostream &s, StringAtom &str)
|
2000-04-05 23:41:58 +00:00
|
|
|
{
|
|
|
|
for (String::iterator i = str.begin(); i != str.end(); i++)
|
|
|
|
s << (char)*i;
|
|
|
|
return s;
|
|
|
|
}
|
2000-03-29 19:19:23 +00:00
|
|
|
|
2000-04-06 23:43:08 +00:00
|
|
|
std::ostream &ICodeGenerator::print(std::ostream &s)
|
2000-04-05 23:41:58 +00:00
|
|
|
{
|
|
|
|
s << "ICG! " << iCode->size() << "\n";
|
|
|
|
for (InstructionIterator i = iCode->begin(); i != iCode->end(); i++) {
|
2000-04-06 22:40:17 +00:00
|
|
|
|
2000-04-05 23:41:58 +00:00
|
|
|
for (LabelList::iterator k = labels.begin(); k != labels.end(); k++)
|
2000-04-08 01:04:55 +00:00
|
|
|
if ((*k)->itsOffset == (ptrdiff_t)(i - iCode->begin())) {
|
2000-04-06 22:40:17 +00:00
|
|
|
//s << "label #" << (k - labels.begin()) << ":\n";
|
|
|
|
s << "#" << (i - iCode->begin());
|
|
|
|
break;
|
2000-04-05 23:41:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Instruction *instr = *i;
|
2000-04-06 22:40:17 +00:00
|
|
|
s << "\t" << std::setiosflags( std::ostream::left ) << std::setw(16) << opcodeName[instr->itsOp];
|
2000-04-05 23:41:58 +00:00
|
|
|
switch (instr->itsOp) {
|
|
|
|
case LOAD_NAME :
|
|
|
|
{
|
|
|
|
LoadName *t = static_cast<LoadName * >(instr);
|
2000-04-07 22:19:36 +00:00
|
|
|
s << "R" << t->itsOperand1 << ", \"" << *t->itsOperand2 << "\"";
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SAVE_NAME :
|
|
|
|
{
|
|
|
|
SaveName *t = static_cast<SaveName * >(instr);
|
2000-04-07 04:58:58 +00:00
|
|
|
s << "\"" << *t->itsOperand1 << "\", R" << t->itsOperand2;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case NEW_OBJECT :
|
|
|
|
{
|
|
|
|
NewObject *t = static_cast<NewObject * >(instr);
|
|
|
|
s << "R" << t->itsOperand1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case GET_PROP :
|
|
|
|
{
|
|
|
|
GetProp *t = static_cast<GetProp * >(instr);
|
2000-04-07 22:19:36 +00:00
|
|
|
s << "R" << t->itsOperand1 << ", R" << t->itsOperand2 << ", \"" << *t->itsOperand3;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SET_PROP :
|
|
|
|
{
|
|
|
|
SetProp *t = static_cast<SetProp * >(instr);
|
2000-04-07 04:58:58 +00:00
|
|
|
s << "\"" << *t->itsOperand1 << "\", R" << t->itsOperand2 << ", R" << t->itsOperand3;
|
2000-04-05 23:41:58 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case LOAD_IMMEDIATE :
|
|
|
|
{
|
|
|
|
LoadImmediate *t = static_cast<LoadImmediate * >(instr);
|
2000-04-07 22:19:36 +00:00
|
|
|
s << "R" << t->itsOperand1 << ", " << t->itsOperand2;
|
2000-04-05 23:41:58 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case LOAD_VAR :
|
2000-04-07 22:19:36 +00:00
|
|
|
{
|
|
|
|
LoadVar *t = static_cast<LoadVar * >(instr);
|
|
|
|
s << "R" << t->itsOperand1 << ", Variable #" << t->itsOperand2;
|
|
|
|
}
|
|
|
|
break;
|
2000-04-05 23:41:58 +00:00
|
|
|
case SAVE_VAR :
|
|
|
|
{
|
|
|
|
LoadVar *t = static_cast<LoadVar * >(instr);
|
|
|
|
s << "Variable #" << t->itsOperand1 << ", R" << t->itsOperand2;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case BRANCH :
|
|
|
|
{
|
2000-04-07 22:19:36 +00:00
|
|
|
ResolvedBranch *t = static_cast<ResolvedBranch * >(instr);
|
2000-04-06 22:40:17 +00:00
|
|
|
s << "instr #" << t->itsOperand1;
|
2000-04-05 23:41:58 +00:00
|
|
|
}
|
|
|
|
break;
|
2000-04-06 02:57:42 +00:00
|
|
|
case BRANCH_LT :
|
|
|
|
case BRANCH_LE :
|
|
|
|
case BRANCH_EQ :
|
|
|
|
case BRANCH_NE :
|
|
|
|
case BRANCH_GE :
|
|
|
|
case BRANCH_GT :
|
2000-04-05 23:41:58 +00:00
|
|
|
{
|
2000-04-07 22:19:36 +00:00
|
|
|
ResolvedBranchCond *t = static_cast<ResolvedBranchCond * >(instr);
|
2000-04-06 22:40:17 +00:00
|
|
|
s << "instr #" << t->itsOperand1 << ", R" << t->itsOperand2;
|
2000-04-05 23:41:58 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ADD :
|
2000-04-06 03:02:24 +00:00
|
|
|
case SUBTRACT :
|
|
|
|
case MULTIPLY :
|
|
|
|
case DIVIDE :
|
2000-04-06 22:40:17 +00:00
|
|
|
case COMPARE_LT :
|
|
|
|
case COMPARE_LE :
|
|
|
|
case COMPARE_EQ :
|
|
|
|
case COMPARE_NE :
|
|
|
|
case COMPARE_GT :
|
|
|
|
case COMPARE_GE :
|
2000-04-05 23:41:58 +00:00
|
|
|
{
|
|
|
|
Arithmetic *t = static_cast<Arithmetic * >(instr);
|
|
|
|
s << "R" << t->itsOperand1 << ", R" << t->itsOperand2 << ", R" << t->itsOperand3;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case MOVE_TO :
|
|
|
|
case NOT :
|
|
|
|
{
|
|
|
|
Move *t = static_cast<Move * >(instr);
|
|
|
|
s << "R" << t->itsOperand1 << ", R" << t->itsOperand2;
|
|
|
|
}
|
|
|
|
break;
|
2000-04-06 02:57:42 +00:00
|
|
|
case RETURN :
|
|
|
|
{
|
|
|
|
Return *t = static_cast<Return * >(instr);
|
|
|
|
s << "R" << t->itsOperand1;
|
|
|
|
}
|
|
|
|
break;
|
2000-04-05 23:41:58 +00:00
|
|
|
}
|
|
|
|
s << "\n";
|
|
|
|
}
|
|
|
|
for (LabelList::iterator k = labels.begin(); k != labels.end(); k++)
|
2000-04-08 01:04:55 +00:00
|
|
|
if ((*k)->itsOffset == (ptrdiff_t)(iCode->end() - iCode->begin())) {
|
2000-04-06 22:40:17 +00:00
|
|
|
// s << "label #" << (k - labels.begin()) << ":\n";
|
2000-04-06 23:40:47 +00:00
|
|
|
// s << "#" << (i - iCode->begin());
|
2000-04-05 23:41:58 +00:00
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
2000-03-29 19:19:23 +00:00
|
|
|
|
|
|
|
|
2000-04-05 23:41:58 +00:00
|
|
|
} // namespace JavaScript
|