// -*- 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 #include #include namespace JavaScript { typedef uint32 Register; enum { NotARegister = 0xFFFFFFFF }; typedef std::vector RegisterList; enum ICodeOp { // Operand1 Operand2 Operand3 NOP, MOVE_TO, // Destination Register Source Register LOAD_VAR, // Destination Register index of frame slot SAVE_VAR, // index of frame slot Source Register LOAD_IMMEDIATE, // Destination Register immediate (double) LOAD_NAME, // Destination Register StringAtom & SAVE_NAME, // StringAtom & Source Register NEW_OBJECT, // Destination Register NEW_ARRAY, // Destination Array GET_PROP, // Destination Register Object Register StringAtom* (name) SET_PROP, // Object Register StringAtom* (name) Source Register GET_ELEMENT, // Destination Register Array Register Index Register SET_ELEMENT, // Base Register Index Register Source Register ADD, // Destination Register Source Register 1 Source Register 2 SUBTRACT, MULTIPLY, DIVIDE, // maintain contiguity COMPARE_LT, // Destination Register Source Register 1 Source Register 2 COMPARE_LE, COMPARE_EQ, COMPARE_NE, COMPARE_GE, COMPARE_GT, NOT, // Destination Register Source Register BRANCH, // Target label BRANCH_LT, // Target label Condition Register BRANCH_LE, BRANCH_EQ, BRANCH_NE, BRANCH_GE, BRANCH_GT, RETURN, // Source Register CALL // Destination Register Target Register Arguments }; class Instruction { public: Instruction(ICodeOp op) : itsOp(op) { } #ifdef DEBUG // provide a virtual destructor, so we can see dynamic type in debugger. virtual ~Instruction() { } #endif ICodeOp itsOp; ICodeOp getBranchOp() { return ((itsOp >= COMPARE_LT) && (itsOp <= COMPARE_GT)) ? (ICodeOp)(BRANCH_LT + (itsOp - COMPARE_LT)) : NOP; } ICodeOp opcode() { return itsOp; } }; typedef std::vector InstructionStream; typedef InstructionStream::iterator InstructionIterator; /****************************************************************/ enum { NotALabel = 0xFFFFFFFF }; class Label { public: Label(InstructionStream *base) : itsBase(base), itsOffset(NotALabel) { } InstructionStream *itsBase; uint32 itsOffset; }; typedef std::vector