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
|
|
|
|
|
|
|
#ifndef icodegenerator_h
|
|
|
|
#define icodegenerator_h
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
#include <stack>
|
|
|
|
|
2000-04-18 04:17:34 +04:00
|
|
|
#include "utilities.h"
|
|
|
|
#include "parser.h"
|
|
|
|
#include "vmtypes.h"
|
2000-04-07 06:41:21 +04:00
|
|
|
|
2000-04-06 00:28:17 +04:00
|
|
|
|
2000-04-19 01:51:45 +04:00
|
|
|
namespace JavaScript {
|
|
|
|
namespace ICG {
|
|
|
|
|
|
|
|
using namespace VM;
|
2000-06-21 02:45:45 +04:00
|
|
|
using namespace JSTypes;
|
2000-04-19 01:51:45 +04:00
|
|
|
|
2000-06-21 02:45:45 +04:00
|
|
|
typedef std::map<String, TypedRegister, std::less<String> > VariableList;
|
2000-05-27 02:33:05 +04:00
|
|
|
typedef std::pair<uint32, uint32> InstructionMapping;
|
|
|
|
typedef std::vector<InstructionMapping *> InstructionMap;
|
|
|
|
|
2000-05-03 01:36:28 +04:00
|
|
|
|
2000-04-19 01:51:45 +04:00
|
|
|
class ICodeModule {
|
|
|
|
public:
|
2000-05-05 02:42:49 +04:00
|
|
|
ICodeModule(InstructionStream *iCode, VariableList *variables,
|
2000-05-27 02:33:05 +04:00
|
|
|
uint32 maxRegister, uint32 maxParameter, InstructionMap *instructionMap) :
|
2000-05-05 02:42:49 +04:00
|
|
|
its_iCode(iCode), itsVariables(variables),
|
|
|
|
itsParameterCount(maxParameter), itsMaxRegister(maxRegister),
|
2000-05-27 02:33:05 +04:00
|
|
|
mID(++sMaxID), mInstructionMap(instructionMap) { }
|
2000-05-09 22:59:12 +04:00
|
|
|
~ICodeModule()
|
|
|
|
{
|
|
|
|
delete its_iCode;
|
|
|
|
delete itsVariables;
|
2000-05-27 02:33:05 +04:00
|
|
|
delete mInstructionMap;
|
2000-05-09 22:59:12 +04:00
|
|
|
}
|
2000-05-05 02:42:49 +04:00
|
|
|
|
|
|
|
Formatter& print(Formatter& f);
|
2000-04-07 06:41:21 +04:00
|
|
|
|
2000-04-19 01:51:45 +04:00
|
|
|
InstructionStream *its_iCode;
|
2000-05-03 01:36:28 +04:00
|
|
|
VariableList *itsVariables;
|
2000-05-05 02:42:49 +04:00
|
|
|
uint32 itsParameterCount;
|
|
|
|
uint32 itsMaxRegister;
|
|
|
|
uint32 mID;
|
2000-05-27 02:33:05 +04:00
|
|
|
InstructionMap *mInstructionMap;
|
|
|
|
|
2000-05-05 02:42:49 +04:00
|
|
|
static uint32 sMaxID;
|
|
|
|
|
2000-04-19 01:51:45 +04:00
|
|
|
};
|
2000-05-05 02:42:49 +04:00
|
|
|
|
2000-05-24 06:11:39 +04:00
|
|
|
typedef std::vector<const StringAtom *> LabelSet;
|
|
|
|
class LabelEntry {
|
|
|
|
public:
|
|
|
|
LabelEntry(LabelSet *labelSet, Label *breakLabel)
|
|
|
|
: labelSet(labelSet), breakLabel(breakLabel), continueLabel(NULL) { }
|
|
|
|
LabelEntry(LabelSet *labelSet, Label *breakLabel, Label *continueLabel)
|
|
|
|
: labelSet(labelSet), breakLabel(breakLabel), continueLabel(continueLabel) { }
|
|
|
|
|
|
|
|
bool containsLabel(const StringAtom *label);
|
|
|
|
|
|
|
|
LabelSet *labelSet;
|
|
|
|
Label *breakLabel;
|
|
|
|
Label *continueLabel;
|
|
|
|
};
|
|
|
|
typedef std::vector<LabelEntry *> LabelStack;
|
|
|
|
|
2000-05-05 02:42:49 +04:00
|
|
|
Formatter& operator<<(Formatter &f, ICodeModule &i);
|
|
|
|
|
2000-04-19 01:51:45 +04:00
|
|
|
/****************************************************************/
|
|
|
|
|
|
|
|
// 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.
|
2000-03-29 23:19:23 +04:00
|
|
|
|
2000-04-19 01:51:45 +04:00
|
|
|
class ICodeGenerator {
|
|
|
|
private:
|
|
|
|
InstructionStream *iCode;
|
2000-05-09 22:59:12 +04:00
|
|
|
bool iCodeOwner;
|
2000-04-19 01:51:45 +04:00
|
|
|
LabelList labels;
|
2000-05-03 01:36:28 +04:00
|
|
|
|
|
|
|
Register topRegister; // highest (currently) alloacated register
|
|
|
|
Register registerBase; // start of registers available for expression temps
|
|
|
|
uint32 maxRegister; // highest (ever) allocated register
|
|
|
|
uint32 parameterCount; // number of parameters declared for the function
|
|
|
|
// these must come before any variables declared.
|
2000-06-21 02:45:45 +04:00
|
|
|
TypedRegister exceptionRegister;// reserved to carry the exception object.
|
2000-05-03 01:36:28 +04:00
|
|
|
VariableList *variableList; // name|register pair for each variable
|
2000-05-18 04:03:23 +04:00
|
|
|
|
2000-06-21 02:45:45 +04:00
|
|
|
World *mWorld; // used to register strings
|
|
|
|
JSScope *mGlobal; // the scope for compiling within
|
2000-05-27 02:33:05 +04:00
|
|
|
LabelStack mLabelStack; // stack of LabelEntry objects, one per nested looping construct
|
|
|
|
InstructionMap *mInstructionMap;// maps source position to instruction index
|
|
|
|
|
|
|
|
bool mWithinWith; // state from genStmt that indicates generating code beneath a with statement
|
|
|
|
|
2000-04-19 01:51:45 +04:00
|
|
|
void markMaxRegister() \
|
|
|
|
{ if (topRegister > maxRegister) maxRegister = topRegister; }
|
|
|
|
|
|
|
|
Register getRegister() \
|
|
|
|
{ return topRegister++; }
|
2000-05-03 01:36:28 +04:00
|
|
|
|
2000-04-19 01:51:45 +04:00
|
|
|
void resetTopRegister() \
|
2000-05-03 01:36:28 +04:00
|
|
|
{ markMaxRegister(); topRegister = registerBase; }
|
2000-04-19 01:51:45 +04:00
|
|
|
|
2000-05-03 01:36:28 +04:00
|
|
|
|
2000-04-19 01:51:45 +04:00
|
|
|
void setLabel(Label *label);
|
|
|
|
|
2000-04-27 05:27:09 +04:00
|
|
|
void jsr(Label *label) { iCode->push_back(new Jsr(label)); }
|
|
|
|
void rts() { iCode->push_back(new Rts()); }
|
2000-04-19 01:51:45 +04:00
|
|
|
void branch(Label *label);
|
2000-06-21 02:45:45 +04:00
|
|
|
GenericBranch *branchTrue(Label *label, TypedRegister condition);
|
|
|
|
GenericBranch *branchFalse(Label *label, TypedRegister condition);
|
2000-04-19 01:51:45 +04:00
|
|
|
|
2000-04-27 05:27:09 +04:00
|
|
|
void beginTry(Label *catchLabel, Label *finallyLabel)
|
2000-05-06 01:38:16 +04:00
|
|
|
{ iCode->push_back(new Tryin(catchLabel, finallyLabel)); }
|
2000-04-27 05:27:09 +04:00
|
|
|
void endTry()
|
2000-05-06 01:38:16 +04:00
|
|
|
{ iCode->push_back(new Tryout()); }
|
2000-05-27 02:33:05 +04:00
|
|
|
|
2000-06-21 02:45:45 +04:00
|
|
|
void beginWith(TypedRegister obj)
|
2000-05-27 02:33:05 +04:00
|
|
|
{ iCode->push_back(new Within(obj)); }
|
|
|
|
void endWith()
|
|
|
|
{ iCode->push_back(new Without()); }
|
2000-05-03 01:36:28 +04:00
|
|
|
|
2000-06-15 03:26:15 +04:00
|
|
|
void defFunction(const StringAtom& name, ICodeModule *code)
|
|
|
|
{ iCode->push_back(new FunctionDef(name, code)); }
|
2000-05-27 02:33:05 +04:00
|
|
|
|
2000-05-24 06:11:39 +04:00
|
|
|
void resetStatement() { resetTopRegister(); }
|
2000-04-22 02:52:52 +04:00
|
|
|
|
2000-06-21 02:45:45 +04:00
|
|
|
void setRegisterForVariable(const StringAtom& name, TypedRegister r)
|
2000-05-27 02:33:05 +04:00
|
|
|
{ (*variableList)[name] = r; }
|
|
|
|
|
|
|
|
|
|
|
|
void startStatement(uint32 pos) { mInstructionMap->push_back(new InstructionMapping(pos, iCode->size())); }
|
|
|
|
|
2000-05-18 04:03:23 +04:00
|
|
|
ICodeOp mapExprNodeToICodeOp(ExprNode::Kind kind);
|
2000-05-27 02:33:05 +04:00
|
|
|
|
2000-06-21 02:45:45 +04:00
|
|
|
TypedRegister grabRegister(const StringAtom& name, const JSType *type)
|
2000-05-27 02:33:05 +04:00
|
|
|
{
|
2000-06-21 02:45:45 +04:00
|
|
|
TypedRegister result(getRegister(), type);
|
2000-05-27 02:33:05 +04:00
|
|
|
(*variableList)[name] = result;
|
|
|
|
registerBase = topRegister;
|
|
|
|
return result;
|
|
|
|
}
|
2000-06-21 02:45:45 +04:00
|
|
|
|
|
|
|
const JSType *findType(const StringAtom& typeName);
|
2000-05-18 04:03:23 +04:00
|
|
|
|
2000-04-19 01:51:45 +04:00
|
|
|
public:
|
2000-06-21 02:45:45 +04:00
|
|
|
ICodeGenerator(World *world, JSScope *global);
|
2000-04-19 01:51:45 +04:00
|
|
|
|
2000-05-09 22:59:12 +04:00
|
|
|
~ICodeGenerator()
|
|
|
|
{
|
2000-05-27 02:33:05 +04:00
|
|
|
if (iCodeOwner) {
|
2000-05-09 22:59:12 +04:00
|
|
|
delete iCode;
|
2000-05-27 02:33:05 +04:00
|
|
|
delete mInstructionMap;
|
|
|
|
}
|
2000-05-09 22:59:12 +04:00
|
|
|
}
|
2000-05-24 06:11:39 +04:00
|
|
|
|
2000-04-19 01:51:45 +04:00
|
|
|
ICodeModule *complete();
|
2000-04-19 06:09:06 +04:00
|
|
|
|
2000-06-21 02:45:45 +04:00
|
|
|
TypedRegister genExpr(ExprNode *p,
|
2000-05-27 02:33:05 +04:00
|
|
|
bool needBoolValueInBranch = false,
|
2000-05-18 04:03:23 +04:00
|
|
|
Label *trueBranch = NULL,
|
|
|
|
Label *falseBranch = NULL);
|
2000-06-15 03:26:15 +04:00
|
|
|
void preprocess(StmtNode *p);
|
2000-06-21 02:45:45 +04:00
|
|
|
TypedRegister genStmt(StmtNode *p, LabelSet *currentLabelSet = NULL);
|
2000-05-24 06:11:39 +04:00
|
|
|
|
2000-06-15 03:26:15 +04:00
|
|
|
void isScript() { mWithinWith = true; }
|
2000-05-24 06:11:39 +04:00
|
|
|
|
2000-06-21 02:45:45 +04:00
|
|
|
void returnStmt(TypedRegister r);
|
|
|
|
void throwStmt(TypedRegister r)
|
2000-05-27 02:33:05 +04:00
|
|
|
{ iCode->push_back(new Throw(r)); }
|
2000-05-03 01:36:28 +04:00
|
|
|
|
2000-06-21 02:45:45 +04:00
|
|
|
TypedRegister allocateVariable(const StringAtom& name);
|
|
|
|
TypedRegister allocateVariable(const StringAtom& name, const StringAtom& typeName);
|
2000-06-16 05:36:59 +04:00
|
|
|
|
2000-06-21 02:45:45 +04:00
|
|
|
TypedRegister findVariable(const StringAtom& name)
|
2000-05-06 01:38:16 +04:00
|
|
|
{ VariableList::iterator i = variableList->find(name);
|
2000-06-21 02:45:45 +04:00
|
|
|
return (i == variableList->end()) ? TypedRegister(NotARegister, &None_Type) : (*i).second; }
|
2000-05-06 01:38:16 +04:00
|
|
|
|
2000-06-21 02:45:45 +04:00
|
|
|
TypedRegister allocateParameter(const StringAtom& name)
|
|
|
|
{ parameterCount++; return grabRegister(name, &Any_Type); }
|
|
|
|
TypedRegister allocateParameter(const StringAtom& name, const StringAtom& typeName)
|
|
|
|
{ parameterCount++; return grabRegister(name, findType(typeName)); }
|
2000-04-19 01:51:45 +04:00
|
|
|
|
|
|
|
Formatter& print(Formatter& f);
|
|
|
|
|
2000-06-21 02:45:45 +04:00
|
|
|
TypedRegister op(ICodeOp op, TypedRegister source);
|
|
|
|
TypedRegister op(ICodeOp op, TypedRegister source1, TypedRegister source2);
|
2000-06-22 02:32:21 +04:00
|
|
|
TypedRegister call(TypedRegister target, const StringAtom &name, RegisterList args);
|
|
|
|
TypedRegister methodCall(TypedRegister targetBase, TypedRegister targetValue, RegisterList args);
|
2000-06-21 02:45:45 +04:00
|
|
|
|
|
|
|
void move(TypedRegister destination, TypedRegister source);
|
|
|
|
TypedRegister logicalNot(TypedRegister source);
|
|
|
|
TypedRegister test(TypedRegister source);
|
2000-04-19 01:51:45 +04:00
|
|
|
|
2000-06-21 02:45:45 +04:00
|
|
|
TypedRegister loadBoolean(bool value);
|
|
|
|
TypedRegister loadImmediate(double value);
|
|
|
|
TypedRegister loadString(String &value);
|
2000-06-22 02:32:21 +04:00
|
|
|
TypedRegister loadString(const StringAtom &name);
|
2000-04-20 02:45:57 +04:00
|
|
|
|
2000-06-21 02:45:45 +04:00
|
|
|
TypedRegister newObject();
|
|
|
|
TypedRegister newArray();
|
2000-06-22 02:32:21 +04:00
|
|
|
TypedRegister newFunction(ICodeModule *icm);
|
2000-04-19 01:51:45 +04:00
|
|
|
|
2000-06-21 02:45:45 +04:00
|
|
|
TypedRegister loadName(const StringAtom &name);
|
|
|
|
void saveName(const StringAtom &name, TypedRegister value);
|
|
|
|
TypedRegister nameInc(const StringAtom &name);
|
|
|
|
TypedRegister nameDec(const StringAtom &name);
|
2000-06-15 03:26:15 +04:00
|
|
|
|
2000-06-21 02:45:45 +04:00
|
|
|
TypedRegister getProperty(TypedRegister base, const StringAtom &name);
|
|
|
|
void setProperty(TypedRegister base, const StringAtom &name, TypedRegister value);
|
|
|
|
TypedRegister propertyInc(TypedRegister base, const StringAtom &name);
|
|
|
|
TypedRegister propertyDec(TypedRegister base, const StringAtom &name);
|
2000-04-19 01:51:45 +04:00
|
|
|
|
2000-06-21 02:45:45 +04:00
|
|
|
TypedRegister getElement(TypedRegister base, TypedRegister index);
|
|
|
|
void setElement(TypedRegister base, TypedRegister index, TypedRegister value);
|
|
|
|
TypedRegister elementInc(TypedRegister base, TypedRegister index);
|
|
|
|
TypedRegister elementDec(TypedRegister base, TypedRegister index);
|
2000-06-15 03:26:15 +04:00
|
|
|
|
2000-06-21 02:45:45 +04:00
|
|
|
TypedRegister varInc(TypedRegister var);
|
|
|
|
TypedRegister varDec(TypedRegister var);
|
2000-06-15 03:26:15 +04:00
|
|
|
|
2000-05-03 01:36:28 +04:00
|
|
|
Register getRegisterBase() { return topRegister; }
|
|
|
|
InstructionStream *get_iCode() { return iCode; }
|
2000-04-19 01:51:45 +04:00
|
|
|
|
|
|
|
Label *getLabel();
|
2000-04-22 02:52:52 +04:00
|
|
|
|
2000-04-19 01:51:45 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
Formatter& operator<<(Formatter &f, ICodeGenerator &i);
|
|
|
|
/*
|
|
|
|
std::ostream &operator<<(std::ostream &s, ICodeGenerator &i);
|
|
|
|
std::ostream &operator<<(std::ostream &s, StringAtom &str);
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2000-04-22 02:52:52 +04:00
|
|
|
|
2000-04-19 01:51:45 +04:00
|
|
|
} /* namespace IGC */
|
2000-04-18 11:14:49 +04:00
|
|
|
} /* namespace JavaScript */
|
2000-04-01 06:53:16 +04:00
|
|
|
|
2000-04-18 04:17:34 +04:00
|
|
|
#endif /* icodegenerator_h */
|