2000-04-18 04:17:34 +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
|
|
|
|
* Copyright (C) 1998 Netscape Communications Corporation. All
|
|
|
|
* Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the
|
|
|
|
* terms of the GNU Public License (the "GPL"), in which case the
|
|
|
|
* provisions of the GPL are applicable instead of those above.
|
|
|
|
* If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of the GPL and not to allow others to use your
|
|
|
|
* version of this file under the NPL, indicate your decision by
|
|
|
|
* deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this
|
|
|
|
* file under either the NPL or the GPL.
|
|
|
|
*/
|
2000-03-29 23:19:23 +04:00
|
|
|
|
|
|
|
#include "numerics.h"
|
|
|
|
#include "world.h"
|
2000-04-18 04:17:34 +04:00
|
|
|
#include "vmtypes.h"
|
2000-04-28 04:10:22 +04:00
|
|
|
#include "jstypes.h"
|
2000-03-29 23:19:23 +04:00
|
|
|
#include "icodegenerator.h"
|
|
|
|
|
|
|
|
#include <iomanip>
|
2000-03-31 04:42:25 +04:00
|
|
|
#include <stdexcept>
|
2000-03-29 23:19:23 +04:00
|
|
|
|
2000-04-07 03:40:47 +04:00
|
|
|
|
2000-04-06 03:41:58 +04:00
|
|
|
namespace JavaScript {
|
2000-04-19 01:51:45 +04:00
|
|
|
namespace ICG {
|
2000-04-18 04:38:26 +04:00
|
|
|
|
2000-04-19 01:51:45 +04:00
|
|
|
using namespace VM;
|
2000-04-18 04:38:26 +04:00
|
|
|
|
2000-04-18 04:17:34 +04:00
|
|
|
|
2000-04-19 01:51:45 +04:00
|
|
|
Formatter& operator<<(Formatter &f, ICodeGenerator &i)
|
|
|
|
{
|
|
|
|
return i.print(f);
|
|
|
|
}
|
2000-04-18 04:17:34 +04:00
|
|
|
|
2000-04-19 01:51:45 +04:00
|
|
|
//
|
|
|
|
// ICodeGenerator
|
|
|
|
//
|
2000-04-18 04:17:34 +04:00
|
|
|
|
2000-04-27 05:27:09 +04:00
|
|
|
ICodeGenerator::ICodeGenerator(World *world, bool hasTryStatement, uint32 switchStatementNesting)
|
|
|
|
: topRegister(0),
|
|
|
|
registerBase(0),
|
|
|
|
maxRegister(0),
|
|
|
|
statementLabelBase(0),
|
|
|
|
exceptionRegister(NotARegister)
|
|
|
|
{
|
|
|
|
iCode = new InstructionStream();
|
|
|
|
if (hasTryStatement)
|
|
|
|
exceptionRegister = allocateVariable(world->identifiers[widenCString("__exceptionObject__")]);
|
|
|
|
for (int i = 0; i < switchStatementNesting; i++) {
|
|
|
|
String s = widenCString("__switchControlVariable__");
|
|
|
|
char num[8];
|
|
|
|
sprintf(num, "%.2d", i);
|
|
|
|
appendChars(s, num, strlen(num));
|
|
|
|
allocateVariable(world->identifiers[s]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-04-19 01:51:45 +04:00
|
|
|
ICodeModule *ICodeGenerator::complete()
|
|
|
|
{
|
2000-04-18 04:17:34 +04:00
|
|
|
#ifdef DEBUG
|
2000-04-19 01:51:45 +04:00
|
|
|
ASSERT(stitcher.empty());
|
2000-04-21 04:37:51 +04:00
|
|
|
ASSERT(statementLabels.empty());
|
2000-04-19 01:51:45 +04:00
|
|
|
for (LabelList::iterator i = labels.begin();
|
|
|
|
i != labels.end(); i++) {
|
2000-04-21 04:04:14 +04:00
|
|
|
ASSERT((*i)->mBase == iCode);
|
|
|
|
ASSERT((*i)->mOffset <= iCode->size());
|
2000-04-19 01:51:45 +04:00
|
|
|
}
|
2000-04-18 04:17:34 +04:00
|
|
|
#endif
|
2000-04-19 01:51:45 +04:00
|
|
|
/*
|
|
|
|
for (InstructionIterator ii = iCode->begin();
|
|
|
|
ii != iCode->end(); ii++) {
|
|
|
|
if ((*ii)->op() == BRANCH) {
|
|
|
|
Instruction *t = *ii;
|
|
|
|
*ii = new ResolvedBranch(static_cast<Branch *>(*ii)->operand1->itsOffset);
|
|
|
|
delete t;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if ((*ii)->itsOp >= BRANCH_LT && (*ii)->itsOp <= BRANCH_GT) {
|
2000-04-08 02:19:36 +04:00
|
|
|
Instruction *t = *ii;
|
2000-04-19 01:51:45 +04:00
|
|
|
*ii = new ResolvedBranchCond((*ii)->itsOp,
|
|
|
|
static_cast<BranchCond *>(*ii)->itsOperand1->itsOffset,
|
|
|
|
static_cast<BranchCond *>(*ii)->itsOperand2);
|
2000-04-08 02:19:36 +04:00
|
|
|
delete t;
|
|
|
|
}
|
2000-04-06 04:04:11 +04:00
|
|
|
}
|
2000-04-19 01:51:45 +04:00
|
|
|
*/
|
2000-04-27 05:27:09 +04:00
|
|
|
markMaxRegister();
|
2000-04-20 02:45:57 +04:00
|
|
|
return new ICodeModule(iCode, maxRegister);
|
2000-04-19 01:51:45 +04:00
|
|
|
}
|
2000-04-06 06:57:42 +04:00
|
|
|
|
2000-04-27 05:27:09 +04:00
|
|
|
TryCodeState::TryCodeState(Label *catchLabel, Label *finallyLabel, ICodeGenerator *icg)
|
|
|
|
: ICodeState(Try_state, icg),
|
|
|
|
catchHandler(catchLabel),
|
|
|
|
finallyHandler(finallyLabel),
|
|
|
|
finallyInvoker(NULL),
|
|
|
|
beyondCatch(NULL)
|
|
|
|
{
|
|
|
|
if (catchHandler) {
|
|
|
|
beyondCatch = icg->getLabel();
|
|
|
|
if (finallyLabel)
|
|
|
|
finallyInvoker = icg->getLabel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-04-19 01:51:45 +04:00
|
|
|
/********************************************************************/
|
|
|
|
|
|
|
|
Register ICodeGenerator::loadImmediate(double value)
|
|
|
|
{
|
|
|
|
Register dest = getRegister();
|
|
|
|
LoadImmediate *instr = new LoadImmediate(dest, value);
|
|
|
|
iCode->push_back(instr);
|
|
|
|
return dest;
|
|
|
|
}
|
|
|
|
|
|
|
|
Register ICodeGenerator::newObject()
|
|
|
|
{
|
|
|
|
Register dest = getRegister();
|
|
|
|
NewObject *instr = new NewObject(dest);
|
|
|
|
iCode->push_back(instr);
|
|
|
|
return dest;
|
|
|
|
}
|
|
|
|
|
|
|
|
Register ICodeGenerator::newArray()
|
|
|
|
{
|
|
|
|
Register dest = getRegister();
|
|
|
|
NewArray *instr = new NewArray(dest);
|
|
|
|
iCode->push_back(instr);
|
|
|
|
return dest;
|
|
|
|
}
|
|
|
|
|
|
|
|
Register ICodeGenerator::loadName(StringAtom &name)
|
|
|
|
{
|
|
|
|
Register dest = getRegister();
|
|
|
|
LoadName *instr = new LoadName(dest, &name);
|
|
|
|
iCode->push_back(instr);
|
|
|
|
return dest;
|
|
|
|
}
|
2000-04-07 06:41:08 +04:00
|
|
|
|
2000-04-19 01:51:45 +04:00
|
|
|
void ICodeGenerator::saveName(StringAtom &name, Register value)
|
|
|
|
{
|
|
|
|
SaveName *instr = new SaveName(&name, value);
|
|
|
|
iCode->push_back(instr);
|
|
|
|
}
|
|
|
|
|
|
|
|
Register ICodeGenerator::getProperty(Register base, StringAtom &name)
|
|
|
|
{
|
|
|
|
Register dest = getRegister();
|
|
|
|
GetProp *instr = new GetProp(dest, base, &name);
|
|
|
|
iCode->push_back(instr);
|
|
|
|
return dest;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ICodeGenerator::setProperty(Register base, StringAtom &name,
|
|
|
|
Register value)
|
|
|
|
{
|
|
|
|
SetProp *instr = new SetProp(base, &name, value);
|
|
|
|
iCode->push_back(instr);
|
|
|
|
}
|
|
|
|
|
|
|
|
Register ICodeGenerator::getElement(Register base, Register index)
|
|
|
|
{
|
|
|
|
Register dest = getRegister();
|
|
|
|
GetElement *instr = new GetElement(dest, base, index);
|
|
|
|
iCode->push_back(instr);
|
|
|
|
return dest;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ICodeGenerator::setElement(Register base, Register index,
|
|
|
|
Register value)
|
|
|
|
{
|
|
|
|
SetElement *instr = new SetElement(base, index, value);
|
|
|
|
iCode->push_back(instr);
|
|
|
|
}
|
|
|
|
|
|
|
|
Register ICodeGenerator::op(ICodeOp op, Register source)
|
|
|
|
{
|
|
|
|
Register dest = getRegister();
|
|
|
|
Compare *instr = new Compare (op, dest, source);
|
|
|
|
iCode->push_back(instr);
|
|
|
|
return dest;
|
|
|
|
}
|
2000-04-18 04:17:34 +04:00
|
|
|
|
2000-04-19 06:09:06 +04:00
|
|
|
void ICodeGenerator::move(Register destination, Register source)
|
|
|
|
{
|
|
|
|
Move *instr = new Move(destination, source);
|
|
|
|
iCode->push_back(instr);
|
|
|
|
}
|
|
|
|
|
2000-04-25 06:55:08 +04:00
|
|
|
void ICodeGenerator::complement(Register destination, Register source)
|
|
|
|
{
|
|
|
|
Not *instr = new Not(destination, source);
|
|
|
|
iCode->push_back(instr);
|
|
|
|
}
|
|
|
|
|
2000-04-19 01:51:45 +04:00
|
|
|
Register ICodeGenerator::op(ICodeOp op, Register source1,
|
|
|
|
Register source2)
|
|
|
|
{
|
|
|
|
Register dest = getRegister();
|
|
|
|
Arithmetic *instr = new Arithmetic(op, dest, source1, source2);
|
|
|
|
iCode->push_back(instr);
|
|
|
|
return dest;
|
|
|
|
}
|
2000-04-18 04:17:34 +04:00
|
|
|
|
2000-04-19 01:51:45 +04:00
|
|
|
Register ICodeGenerator::call(Register target, RegisterList args)
|
|
|
|
{
|
|
|
|
Register dest = getRegister();
|
|
|
|
Call *instr = new Call(dest, target, args);
|
|
|
|
iCode->push_back(instr);
|
|
|
|
return dest;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ICodeGenerator::branch(Label *label)
|
|
|
|
{
|
|
|
|
Branch *instr = new Branch(label);
|
|
|
|
iCode->push_back(instr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ICodeGenerator::branchConditional(Label *label, Register condition)
|
|
|
|
{
|
|
|
|
ICodeOp branchOp = getBranchOp();
|
|
|
|
if (branchOp == NOP) {
|
|
|
|
// XXXX emit convert to boolean / Test / ...
|
|
|
|
branchOp = BRANCH_NE;
|
|
|
|
}
|
|
|
|
GenericBranch *instr = new GenericBranch(branchOp, label, condition);
|
|
|
|
iCode->push_back(instr);
|
|
|
|
}
|
|
|
|
|
2000-04-21 04:37:51 +04:00
|
|
|
void ICodeGenerator::branchNotConditional(Label *label, Register condition)
|
|
|
|
{
|
|
|
|
ICodeOp branchOp = getBranchOp();
|
|
|
|
if (branchOp == NOP) {
|
|
|
|
// XXXX emit convert to boolean / Test / ...
|
|
|
|
branchOp = BRANCH_NE;
|
|
|
|
}
|
|
|
|
switch (branchOp) {
|
|
|
|
case BRANCH_EQ : branchOp = BRANCH_NE; break;
|
|
|
|
case BRANCH_GE : branchOp = BRANCH_LT; break;
|
|
|
|
case BRANCH_GT : branchOp = BRANCH_LE; break;
|
|
|
|
case BRANCH_LE : branchOp = BRANCH_GT; break;
|
|
|
|
case BRANCH_LT : branchOp = BRANCH_GE; break;
|
|
|
|
case BRANCH_NE : branchOp = BRANCH_EQ; break;
|
|
|
|
default : NOT_REACHED("Expected a branch op"); break;
|
|
|
|
}
|
|
|
|
GenericBranch *instr = new GenericBranch(branchOp, label, condition);
|
|
|
|
iCode->push_back(instr);
|
|
|
|
}
|
|
|
|
|
2000-04-19 01:51:45 +04:00
|
|
|
/********************************************************************/
|
|
|
|
|
|
|
|
Label *ICodeGenerator::getLabel()
|
|
|
|
{
|
|
|
|
labels.push_back(new Label(NULL));
|
|
|
|
return labels.back();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ICodeGenerator::setLabel(Label *l)
|
|
|
|
{
|
2000-04-21 04:04:14 +04:00
|
|
|
l->mBase = iCode;
|
|
|
|
l->mOffset = static_cast<int32>(iCode->size());
|
2000-04-19 01:51:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void ICodeGenerator::setLabel(InstructionStream *stream, Label *l)
|
|
|
|
{
|
2000-04-21 04:04:14 +04:00
|
|
|
l->mBase = stream;
|
|
|
|
l->mOffset = static_cast<int32>(stream->size());
|
2000-04-19 01:51:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/********************************************************************/
|
|
|
|
|
|
|
|
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++) {
|
2000-04-21 04:04:14 +04:00
|
|
|
if ((*i)->mBase == sideStream) {
|
|
|
|
(*i)->mBase = iCode;
|
|
|
|
(*i)->mOffset += iCode->size();
|
2000-04-06 03:41:58 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-04-19 01:51:45 +04:00
|
|
|
for (InstructionIterator ii = sideStream->begin();
|
|
|
|
ii != sideStream->end(); ii++) {
|
|
|
|
iCode->push_back(*ii);
|
2000-04-07 02:40:17 +04:00
|
|
|
}
|
2000-04-06 03:41:58 +04:00
|
|
|
|
2000-04-19 01:51:45 +04:00
|
|
|
}
|
2000-04-06 03:41:58 +04:00
|
|
|
|
2000-04-19 01:51:45 +04:00
|
|
|
/********************************************************************/
|
2000-04-18 04:17:34 +04:00
|
|
|
|
2000-04-19 01:51:45 +04:00
|
|
|
void ICodeGenerator::beginWhileStatement(uint32)
|
|
|
|
{
|
|
|
|
resetTopRegister();
|
2000-03-29 23:19:23 +04:00
|
|
|
|
2000-04-22 02:52:52 +04:00
|
|
|
WhileCodeState *ics = new WhileCodeState(this);
|
|
|
|
addStitcher(ics);
|
|
|
|
|
2000-04-19 01:51:45 +04:00
|
|
|
// insert a branch to the while condition, which we're
|
|
|
|
// moving to follow the while block
|
2000-04-22 02:52:52 +04:00
|
|
|
branch(ics->whileCondition);
|
2000-04-19 01:51:45 +04:00
|
|
|
|
|
|
|
iCode = new InstructionStream();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ICodeGenerator::endWhileExpression(Register condition)
|
|
|
|
{
|
|
|
|
WhileCodeState *ics = static_cast<WhileCodeState *>(stitcher.back());
|
|
|
|
ASSERT(ics->stateKind == While_state);
|
|
|
|
|
|
|
|
branchConditional(ics->whileBody, condition);
|
|
|
|
resetTopRegister();
|
|
|
|
// stash away the condition expression and switch
|
|
|
|
// back to the main stream
|
|
|
|
iCode = ics->swapStream(iCode);
|
|
|
|
// mark the start of the while block
|
|
|
|
setLabel(ics->whileBody);
|
|
|
|
}
|
2000-04-18 04:17:34 +04:00
|
|
|
|
2000-04-19 01:51:45 +04:00
|
|
|
void ICodeGenerator::endWhileStatement()
|
|
|
|
{
|
|
|
|
// recover the while stream
|
|
|
|
WhileCodeState *ics = static_cast<WhileCodeState *>(stitcher.back());
|
|
|
|
ASSERT(ics->stateKind == While_state);
|
|
|
|
stitcher.pop_back();
|
2000-04-18 04:17:34 +04:00
|
|
|
|
|
|
|
// mark the start of the condition code
|
|
|
|
// which is where continues will target
|
2000-04-19 01:51:45 +04:00
|
|
|
setLabel(ics->whileCondition);
|
2000-04-18 04:17:34 +04:00
|
|
|
|
2000-04-19 01:51:45 +04:00
|
|
|
// and re-attach it to the main stream
|
|
|
|
mergeStream(ics->whileExpressionStream);
|
2000-03-29 23:19:23 +04:00
|
|
|
|
2000-04-19 01:51:45 +04:00
|
|
|
if (ics->breakLabel != NULL)
|
|
|
|
setLabel(ics->breakLabel);
|
2000-03-29 23:19:23 +04:00
|
|
|
|
2000-04-19 01:51:45 +04:00
|
|
|
delete ics;
|
2000-03-29 23:19:23 +04:00
|
|
|
|
2000-04-19 01:51:45 +04:00
|
|
|
resetTopRegister();
|
|
|
|
}
|
2000-03-29 23:19:23 +04:00
|
|
|
|
2000-04-19 01:51:45 +04:00
|
|
|
/********************************************************************/
|
2000-04-01 05:30:32 +04:00
|
|
|
|
2000-04-19 01:51:45 +04:00
|
|
|
void ICodeGenerator::beginForStatement(uint32)
|
|
|
|
{
|
2000-04-22 02:52:52 +04:00
|
|
|
ForCodeState *ics = new ForCodeState(this);
|
2000-04-21 04:37:51 +04:00
|
|
|
addStitcher(ics);
|
2000-04-22 02:52:52 +04:00
|
|
|
branch(ics->forCondition);
|
2000-04-01 06:53:16 +04:00
|
|
|
|
2000-04-19 01:51:45 +04:00
|
|
|
// begin the stream for collecting the condition expression
|
|
|
|
iCode = new InstructionStream();
|
2000-04-22 02:52:52 +04:00
|
|
|
setLabel(ics->forCondition);
|
2000-04-01 06:53:16 +04:00
|
|
|
|
2000-04-19 01:51:45 +04:00
|
|
|
resetTopRegister();
|
|
|
|
}
|
2000-04-01 06:53:16 +04:00
|
|
|
|
2000-04-19 01:51:45 +04:00
|
|
|
void ICodeGenerator::forCondition(Register condition)
|
|
|
|
{
|
|
|
|
ForCodeState *ics = static_cast<ForCodeState *>(stitcher.back());
|
|
|
|
ASSERT(ics->stateKind == For_state);
|
2000-04-01 06:53:16 +04:00
|
|
|
|
2000-04-19 01:51:45 +04:00
|
|
|
// finsh off the test expression by adding the branch to the body
|
|
|
|
branchConditional(ics->forBody, condition);
|
2000-04-01 06:53:16 +04:00
|
|
|
|
2000-04-19 01:51:45 +04:00
|
|
|
// switch back to main stream
|
|
|
|
iCode = ics->swapStream(iCode);
|
|
|
|
// begin the stream for collecting the increment expression
|
|
|
|
iCode = new InstructionStream();
|
2000-04-01 06:53:16 +04:00
|
|
|
|
2000-04-19 01:51:45 +04:00
|
|
|
ics->continueLabel = getLabel();
|
|
|
|
// can't lazily insert this since we haven't seen the body yet
|
|
|
|
// ??? could just remember the offset
|
|
|
|
setLabel(ics->continueLabel);
|
2000-04-04 05:48:35 +04:00
|
|
|
|
2000-04-19 01:51:45 +04:00
|
|
|
resetTopRegister();
|
|
|
|
}
|
2000-04-01 06:53:16 +04:00
|
|
|
|
2000-04-19 01:51:45 +04:00
|
|
|
void ICodeGenerator::forIncrement()
|
|
|
|
{
|
|
|
|
ForCodeState *ics = static_cast<ForCodeState *>(stitcher.back());
|
|
|
|
ASSERT(ics->stateKind == For_state);
|
2000-04-01 06:53:16 +04:00
|
|
|
|
2000-04-19 01:51:45 +04:00
|
|
|
// now switch back to the main stream
|
|
|
|
iCode = ics->swapStream2(iCode);
|
|
|
|
setLabel(ics->forBody);
|
2000-04-01 06:53:16 +04:00
|
|
|
|
2000-04-19 01:51:45 +04:00
|
|
|
resetTopRegister();
|
|
|
|
}
|
2000-04-01 06:53:16 +04:00
|
|
|
|
2000-04-19 01:51:45 +04:00
|
|
|
void ICodeGenerator::endForStatement()
|
|
|
|
{
|
|
|
|
ForCodeState *ics = static_cast<ForCodeState *>(stitcher.back());
|
|
|
|
ASSERT(ics->stateKind == For_state);
|
|
|
|
stitcher.pop_back();
|
2000-04-01 06:53:16 +04:00
|
|
|
|
2000-04-19 01:51:45 +04:00
|
|
|
mergeStream(ics->forIncrementStream);
|
|
|
|
mergeStream(ics->forConditionStream);
|
2000-04-01 06:53:16 +04:00
|
|
|
|
2000-04-19 01:51:45 +04:00
|
|
|
if (ics->breakLabel != NULL)
|
|
|
|
setLabel(ics->breakLabel);
|
2000-04-01 06:53:16 +04:00
|
|
|
|
2000-04-19 01:51:45 +04:00
|
|
|
delete ics;
|
|
|
|
}
|
2000-04-01 06:53:16 +04:00
|
|
|
|
2000-04-19 01:51:45 +04:00
|
|
|
/********************************************************************/
|
2000-04-01 06:53:16 +04:00
|
|
|
|
2000-04-19 01:51:45 +04:00
|
|
|
void ICodeGenerator::beginDoStatement(uint32)
|
|
|
|
{
|
|
|
|
resetTopRegister();
|
2000-04-22 02:52:52 +04:00
|
|
|
DoCodeState *ics = new DoCodeState(this);
|
|
|
|
addStitcher(ics);
|
2000-04-01 05:30:32 +04:00
|
|
|
|
2000-04-19 01:51:45 +04:00
|
|
|
// mark the top of the loop body
|
2000-04-22 02:52:52 +04:00
|
|
|
setLabel(ics->doBody);
|
2000-04-19 01:51:45 +04:00
|
|
|
}
|
2000-04-01 05:30:32 +04:00
|
|
|
|
2000-04-19 01:51:45 +04:00
|
|
|
void ICodeGenerator::endDoStatement()
|
|
|
|
{
|
|
|
|
DoCodeState *ics = static_cast<DoCodeState *>(stitcher.back());
|
|
|
|
ASSERT(ics->stateKind == Do_state);
|
2000-04-01 05:30:32 +04:00
|
|
|
|
2000-04-19 01:51:45 +04:00
|
|
|
// mark the start of the do conditional
|
|
|
|
setLabel(ics->doCondition);
|
|
|
|
if (ics->continueLabel != NULL)
|
|
|
|
setLabel(ics->continueLabel);
|
2000-04-01 05:30:32 +04:00
|
|
|
|
2000-04-19 01:51:45 +04:00
|
|
|
resetTopRegister();
|
|
|
|
}
|
2000-04-06 03:41:58 +04:00
|
|
|
|
2000-04-19 01:51:45 +04: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);
|
|
|
|
if (ics->breakLabel != NULL)
|
|
|
|
setLabel(ics->breakLabel);
|
|
|
|
|
|
|
|
delete ics;
|
|
|
|
|
|
|
|
resetTopRegister();
|
|
|
|
}
|
|
|
|
|
|
|
|
/********************************************************************/
|
|
|
|
|
|
|
|
void ICodeGenerator::beginSwitchStatement(uint32, Register expression)
|
|
|
|
{
|
|
|
|
// stash the control expression value
|
|
|
|
resetTopRegister();
|
|
|
|
Register control = op(MOVE, expression);
|
2000-04-22 02:52:52 +04:00
|
|
|
/*
|
|
|
|
XXXX this register needs to belong to the 'variable' set so that
|
|
|
|
it is preserved across statements. Then we can drop having to
|
|
|
|
keep the registe rbase in the icodestate.
|
|
|
|
|
|
|
|
*/
|
2000-04-19 01:51:45 +04:00
|
|
|
// 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);
|
2000-04-21 04:37:51 +04:00
|
|
|
addStitcher(ics);
|
2000-04-19 01:51:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void ICodeGenerator::endCaseCondition(Register expression)
|
|
|
|
{
|
|
|
|
SwitchCodeState *ics =
|
|
|
|
static_cast<SwitchCodeState *>(stitcher.back());
|
|
|
|
ASSERT(ics->stateKind == Switch_state);
|
|
|
|
|
|
|
|
Label *caseLabel = getLabel();
|
|
|
|
Register r = op(COMPARE_EQ, expression, ics->controlExpression);
|
|
|
|
branchConditional(caseLabel, r);
|
|
|
|
|
|
|
|
// mark the case in the Case Statement stream
|
|
|
|
setLabel(ics->caseStatementsStream, caseLabel);
|
|
|
|
resetTopRegister();
|
|
|
|
}
|
|
|
|
|
2000-04-24 22:41:05 +04:00
|
|
|
void ICodeGenerator::beginCaseStatement(uint32 /* pos */)
|
2000-04-19 01:51:45 +04:00
|
|
|
{
|
|
|
|
SwitchCodeState *ics =
|
|
|
|
static_cast<SwitchCodeState *>(stitcher.back());
|
|
|
|
ASSERT(ics->stateKind == Switch_state);
|
|
|
|
// switch to Case Statement stream
|
|
|
|
iCode = ics->swapStream(iCode);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ICodeGenerator::endCaseStatement()
|
|
|
|
{
|
|
|
|
SwitchCodeState *ics =
|
|
|
|
static_cast<SwitchCodeState *>(stitcher.back());
|
|
|
|
// do more to guarantee correct blocking?
|
|
|
|
ASSERT(ics->stateKind == Switch_state);
|
|
|
|
// switch back to Case Conditional stream
|
|
|
|
iCode = ics->swapStream(iCode);
|
|
|
|
resetTopRegister();
|
|
|
|
}
|
|
|
|
|
2000-04-24 22:41:05 +04:00
|
|
|
void ICodeGenerator::beginDefaultStatement(uint32 /* pos */)
|
2000-04-19 01:51:45 +04:00
|
|
|
{
|
|
|
|
SwitchCodeState *ics =
|
|
|
|
static_cast<SwitchCodeState *>(stitcher.back());
|
|
|
|
ASSERT(ics->stateKind == Switch_state);
|
|
|
|
ASSERT(ics->defaultLabel == NULL);
|
|
|
|
ics->defaultLabel = getLabel();
|
|
|
|
setLabel(ics->caseStatementsStream, ics->defaultLabel);
|
|
|
|
// switch to Case Statement stream
|
|
|
|
iCode = ics->swapStream(iCode);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ICodeGenerator::endDefaultStatement()
|
|
|
|
{
|
|
|
|
SwitchCodeState *ics =
|
|
|
|
static_cast<SwitchCodeState *>(stitcher.back());
|
|
|
|
ASSERT(ics->stateKind == Switch_state);
|
|
|
|
// do more to guarantee correct blocking?
|
|
|
|
ASSERT(ics->defaultLabel != NULL);
|
|
|
|
// switch to Case Statement stream
|
|
|
|
iCode = ics->swapStream(iCode);
|
|
|
|
resetTopRegister();
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
if (ics->defaultLabel != NULL)
|
|
|
|
branch(ics->defaultLabel);
|
|
|
|
else {
|
|
|
|
if (ics->breakLabel == NULL)
|
|
|
|
ics->breakLabel = getLabel();
|
|
|
|
branch(ics->breakLabel);
|
2000-04-18 04:17:34 +04:00
|
|
|
}
|
2000-04-01 05:30:32 +04:00
|
|
|
|
2000-04-19 01:51:45 +04:00
|
|
|
// dump all the case statements into the main stream
|
|
|
|
mergeStream(ics->caseStatementsStream);
|
2000-04-06 03:41:58 +04:00
|
|
|
|
2000-04-19 01:51:45 +04:00
|
|
|
if (ics->breakLabel != NULL)
|
|
|
|
setLabel(ics->breakLabel);
|
2000-04-06 03:41:58 +04:00
|
|
|
|
2000-04-19 01:51:45 +04:00
|
|
|
delete ics;
|
|
|
|
}
|
2000-03-29 23:19:23 +04:00
|
|
|
|
2000-04-06 03:41:58 +04:00
|
|
|
|
2000-04-19 01:51:45 +04:00
|
|
|
/********************************************************************/
|
2000-04-18 04:17:34 +04:00
|
|
|
|
2000-04-19 01:51:45 +04:00
|
|
|
void ICodeGenerator::beginIfStatement(uint32, Register condition)
|
|
|
|
{
|
2000-04-22 02:52:52 +04:00
|
|
|
IfCodeState *ics = new IfCodeState(this);
|
|
|
|
addStitcher(ics);
|
2000-04-18 04:17:34 +04:00
|
|
|
|
2000-04-22 02:52:52 +04:00
|
|
|
branchNotConditional(ics->elseLabel, condition);
|
2000-04-18 04:17:34 +04:00
|
|
|
|
2000-04-19 01:51:45 +04:00
|
|
|
resetTopRegister();
|
|
|
|
}
|
2000-04-06 03:41:58 +04:00
|
|
|
|
2000-04-19 01:51:45 +04:00
|
|
|
void ICodeGenerator::beginElseStatement(bool hasElse)
|
|
|
|
{
|
|
|
|
IfCodeState *ics = static_cast<IfCodeState *>(stitcher.back());
|
|
|
|
ASSERT(ics->stateKind == If_state);
|
2000-04-06 03:41:58 +04:00
|
|
|
|
2000-04-19 01:51:45 +04:00
|
|
|
if (hasElse) {
|
|
|
|
Label *beyondElse = getLabel();
|
|
|
|
ics->beyondElse = beyondElse;
|
|
|
|
branch(beyondElse);
|
2000-03-29 23:19:23 +04:00
|
|
|
}
|
2000-04-19 01:51:45 +04:00
|
|
|
setLabel(ics->elseLabel);
|
|
|
|
resetTopRegister();
|
|
|
|
}
|
2000-04-18 04:17:34 +04:00
|
|
|
|
2000-04-19 01:51:45 +04:00
|
|
|
void ICodeGenerator::endIfStatement()
|
|
|
|
{
|
|
|
|
IfCodeState *ics = static_cast<IfCodeState *>(stitcher.back());
|
|
|
|
ASSERT(ics->stateKind == If_state);
|
|
|
|
stitcher.pop_back();
|
2000-04-18 04:17:34 +04:00
|
|
|
|
2000-04-19 01:51:45 +04:00
|
|
|
if (ics->beyondElse != NULL) { // had an else
|
|
|
|
setLabel(ics->beyondElse); // the beyond else label
|
2000-04-18 04:17:34 +04:00
|
|
|
}
|
2000-04-19 01:51:45 +04:00
|
|
|
|
|
|
|
delete ics;
|
|
|
|
resetTopRegister();
|
|
|
|
}
|
|
|
|
|
|
|
|
/************************************************************************/
|
|
|
|
|
2000-04-24 22:41:05 +04:00
|
|
|
void ICodeGenerator::breakStatement(uint32 /* pos */)
|
2000-04-19 01:51:45 +04:00
|
|
|
{
|
|
|
|
for (std::vector<ICodeState *>::reverse_iterator p =
|
|
|
|
stitcher.rbegin(); p != stitcher.rend(); p++) {
|
|
|
|
if ((*p)->breakLabel != NULL) {
|
|
|
|
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;
|
2000-04-06 03:41:58 +04:00
|
|
|
}
|
|
|
|
}
|
2000-04-19 01:51:45 +04:00
|
|
|
NOT_REACHED("no break target available");
|
|
|
|
}
|
2000-03-29 23:19:23 +04:00
|
|
|
|
2000-04-24 22:41:05 +04:00
|
|
|
void ICodeGenerator::breakStatement(uint32 /* pos */,
|
|
|
|
const StringAtom &label)
|
2000-04-21 04:37:51 +04:00
|
|
|
{
|
|
|
|
uint32 statementLabelCeiling = statementLabels.size();
|
|
|
|
|
|
|
|
for (std::vector<ICodeState *>::reverse_iterator p =
|
|
|
|
stitcher.rbegin(); p != stitcher.rend(); p++) {
|
|
|
|
|
|
|
|
for (std::vector<const StringAtom *>::iterator lbl =
|
|
|
|
statementLabels.begin() + (*p)->statementLabelBase;
|
|
|
|
lbl != statementLabels.begin() + statementLabelCeiling;
|
|
|
|
lbl++) {
|
|
|
|
if ((*lbl) == &label) {
|
|
|
|
if ((*p)->breakLabel == NULL)
|
|
|
|
(*p)->breakLabel = getLabel();
|
|
|
|
branch((*p)->breakLabel);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
statementLabelCeiling = (*p)->statementLabelBase;
|
|
|
|
}
|
|
|
|
NOT_REACHED("no break target available");
|
|
|
|
}
|
|
|
|
|
2000-04-24 22:41:05 +04:00
|
|
|
void ICodeGenerator::continueStatement(uint32 /* pos */)
|
2000-04-19 01:51:45 +04:00
|
|
|
{
|
|
|
|
for (std::vector<ICodeState *>::reverse_iterator p =
|
|
|
|
stitcher.rbegin(); p != stitcher.rend(); p++) {
|
|
|
|
if ((*p)->continueLabel != NULL) {
|
|
|
|
branch((*p)->continueLabel);
|
|
|
|
return;
|
2000-04-06 03:41:58 +04:00
|
|
|
}
|
2000-04-19 01:51:45 +04:00
|
|
|
if (((*p)->stateKind == While_state)
|
|
|
|
|| ((*p)->stateKind == Do_state)
|
|
|
|
|| ((*p)->stateKind == For_state)) {
|
|
|
|
(*p)->continueLabel = getLabel();
|
|
|
|
branch((*p)->continueLabel);
|
|
|
|
return;
|
2000-04-06 03:41:58 +04:00
|
|
|
}
|
2000-04-19 01:51:45 +04:00
|
|
|
}
|
|
|
|
NOT_REACHED("no continue target available");
|
|
|
|
}
|
|
|
|
|
2000-04-24 22:41:05 +04:00
|
|
|
void ICodeGenerator::continueStatement(uint32 /* pos */,
|
|
|
|
const StringAtom &label)
|
2000-04-21 04:37:51 +04:00
|
|
|
{
|
|
|
|
uint32 statementLabelCeiling = statementLabels.size();
|
|
|
|
|
|
|
|
for (std::vector<ICodeState *>::reverse_iterator p =
|
|
|
|
stitcher.rbegin(); p != stitcher.rend(); p++) {
|
|
|
|
|
|
|
|
for (std::vector<const StringAtom *>::iterator lbl =
|
|
|
|
statementLabels.begin() + (*p)->statementLabelBase;
|
|
|
|
lbl != statementLabels.begin() + statementLabelCeiling;
|
|
|
|
lbl++) {
|
|
|
|
if ((*lbl) == &label) {
|
|
|
|
if ((*p)->continueLabel == NULL)
|
|
|
|
(*p)->continueLabel = getLabel();
|
|
|
|
branch((*p)->continueLabel);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
statementLabelCeiling = (*p)->statementLabelBase;
|
|
|
|
}
|
|
|
|
NOT_REACHED("no continue target available");
|
|
|
|
}
|
2000-04-22 02:52:52 +04:00
|
|
|
/********************************************************************/
|
|
|
|
|
2000-04-24 22:41:05 +04:00
|
|
|
void ICodeGenerator::beginTryStatement(uint32 /* pos */,
|
|
|
|
bool hasCatch, bool hasFinally)
|
2000-04-22 02:52:52 +04:00
|
|
|
{
|
2000-04-27 05:27:09 +04:00
|
|
|
ASSERT(exceptionRegister != NotARegister);
|
|
|
|
TryCodeState *ics = new TryCodeState((hasCatch) ? getLabel() : NULL,
|
|
|
|
(hasFinally) ? getLabel() : NULL, this);
|
|
|
|
addStitcher(ics);
|
|
|
|
beginTry(ics->catchHandler, ics->finallyInvoker);
|
2000-04-22 02:52:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void ICodeGenerator::endTryBlock()
|
|
|
|
{
|
|
|
|
TryCodeState *ics = static_cast<TryCodeState *>(stitcher.back());
|
|
|
|
ASSERT(ics->stateKind == Try_state);
|
2000-04-21 04:37:51 +04:00
|
|
|
|
2000-04-27 05:27:09 +04:00
|
|
|
endTry();
|
|
|
|
if (ics->finallyHandler)
|
|
|
|
jsr(ics->finallyHandler);
|
2000-04-22 02:52:52 +04:00
|
|
|
if (ics->beyondCatch)
|
|
|
|
branch(ics->beyondCatch);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ICodeGenerator::endTryStatement()
|
|
|
|
{
|
|
|
|
TryCodeState *ics = static_cast<TryCodeState *>(stitcher.back());
|
|
|
|
ASSERT(ics->stateKind == Try_state);
|
|
|
|
stitcher.pop_back();
|
|
|
|
if (ics->beyondCatch)
|
|
|
|
setLabel(ics->beyondCatch);
|
|
|
|
}
|
|
|
|
|
2000-04-24 22:41:05 +04:00
|
|
|
void ICodeGenerator::beginCatchStatement(uint32 /* pos */)
|
2000-04-22 02:52:52 +04:00
|
|
|
{
|
|
|
|
TryCodeState *ics = static_cast<TryCodeState *>(stitcher.back());
|
|
|
|
ASSERT(ics->stateKind == Try_state);
|
2000-04-27 05:27:09 +04:00
|
|
|
ASSERT(ics->catchHandler);
|
|
|
|
setLabel(ics->catchHandler);
|
2000-04-22 02:52:52 +04:00
|
|
|
}
|
|
|
|
|
2000-04-27 05:27:09 +04:00
|
|
|
void ICodeGenerator::endCatchExpression(Register exceptionId)
|
2000-04-22 02:52:52 +04:00
|
|
|
{
|
|
|
|
TryCodeState *ics = static_cast<TryCodeState *>(stitcher.back());
|
|
|
|
ASSERT(ics->stateKind == Try_state);
|
2000-04-27 05:27:09 +04:00
|
|
|
move(exceptionRegister, exceptionId);
|
2000-04-22 02:52:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void ICodeGenerator::endCatchStatement()
|
|
|
|
{
|
|
|
|
TryCodeState *ics = static_cast<TryCodeState *>(stitcher.back());
|
|
|
|
ASSERT(ics->stateKind == Try_state);
|
2000-04-27 05:27:09 +04:00
|
|
|
if (ics->finallyHandler)
|
|
|
|
jsr(ics->finallyHandler);
|
|
|
|
throwStatement(0, exceptionRegister);
|
2000-04-22 02:52:52 +04:00
|
|
|
}
|
|
|
|
|
2000-04-24 22:41:05 +04:00
|
|
|
void ICodeGenerator::beginFinallyStatement(uint32 /* pos */)
|
2000-04-22 02:52:52 +04:00
|
|
|
{
|
|
|
|
TryCodeState *ics = static_cast<TryCodeState *>(stitcher.back());
|
|
|
|
ASSERT(ics->stateKind == Try_state);
|
2000-04-27 05:27:09 +04:00
|
|
|
ASSERT(ics->finallyHandler);
|
|
|
|
if (ics->finallyInvoker) {
|
|
|
|
setLabel(ics->finallyInvoker);
|
|
|
|
jsr(ics->finallyHandler);
|
|
|
|
throwStatement(0, exceptionRegister);
|
|
|
|
}
|
|
|
|
setLabel(ics->finallyHandler);
|
2000-04-22 02:52:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void ICodeGenerator::endFinallyStatement()
|
|
|
|
{
|
|
|
|
TryCodeState *ics = static_cast<TryCodeState *>(stitcher.back());
|
|
|
|
ASSERT(ics->stateKind == Try_state);
|
2000-04-27 05:27:09 +04:00
|
|
|
rts();
|
2000-04-22 02:52:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/************************************************************************/
|
2000-04-21 04:37:51 +04:00
|
|
|
|
2000-04-22 02:52:52 +04:00
|
|
|
Formatter& ICodeGenerator::print(Formatter& f)
|
2000-04-19 01:51:45 +04:00
|
|
|
{
|
|
|
|
f << "ICG! " << (uint32)iCode->size() << "\n";
|
|
|
|
for (InstructionIterator i = iCode->begin();
|
|
|
|
i != iCode->end(); i++) {
|
|
|
|
bool isLabel = false;
|
|
|
|
|
|
|
|
for (LabelList::iterator k = labels.begin();
|
|
|
|
k != labels.end(); k++)
|
2000-04-21 04:04:14 +04:00
|
|
|
if ((ptrdiff_t)(*k)->mOffset == (i - iCode->begin())) {
|
2000-04-19 01:51:45 +04:00
|
|
|
f << "#" << (uint32)(i - iCode->begin()) << "\t";
|
|
|
|
isLabel = true;
|
|
|
|
break;
|
|
|
|
}
|
2000-03-29 23:19:23 +04:00
|
|
|
|
2000-04-19 01:51:45 +04:00
|
|
|
if (!isLabel)
|
|
|
|
f << "\t";
|
|
|
|
|
|
|
|
f << **i << "\n";
|
2000-04-18 04:17:34 +04:00
|
|
|
}
|
2000-04-19 01:51:45 +04:00
|
|
|
|
|
|
|
return f;
|
|
|
|
}
|
2000-04-18 04:17:34 +04:00
|
|
|
|
2000-04-19 01:51:45 +04:00
|
|
|
} // namespace ICG
|
2000-04-18 04:17:34 +04:00
|
|
|
|
2000-04-20 10:20:31 +04:00
|
|
|
} // namespace JavaScript
|