2000-10-12 01:32:01 +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.
|
|
|
|
*
|
|
|
|
* 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-10-18 03:12:42 +00:00
|
|
|
#ifndef __icodeasm_h
|
|
|
|
|
|
|
|
#define __icodeasm_h
|
|
|
|
|
2000-10-12 01:32:01 +00:00
|
|
|
#include <string>
|
|
|
|
#include <iterator>
|
|
|
|
|
2000-10-12 23:58:51 +00:00
|
|
|
#include "vmtypes.h"
|
2000-10-18 01:02:13 +00:00
|
|
|
#include "jstypes.h"
|
2000-10-19 00:03:12 +00:00
|
|
|
#include "interpreter.h"
|
2000-10-12 23:58:51 +00:00
|
|
|
|
2000-10-12 01:32:01 +00:00
|
|
|
#define iter string::const_iterator
|
|
|
|
|
|
|
|
namespace JavaScript {
|
|
|
|
namespace ICodeASM {
|
|
|
|
enum TokenEstimation {
|
|
|
|
/* guess at tokentype, based on first character of token */
|
2000-10-18 03:12:42 +00:00
|
|
|
teAlpha,
|
|
|
|
teCloseParen,
|
|
|
|
teComma,
|
|
|
|
teColon,
|
2000-10-12 01:32:01 +00:00
|
|
|
teEOF,
|
|
|
|
teIllegal,
|
2000-10-17 00:41:54 +00:00
|
|
|
teMinus,
|
2000-10-18 20:44:14 +00:00
|
|
|
teNewline,
|
2000-10-18 03:12:42 +00:00
|
|
|
teNotARegister,
|
2000-10-12 01:32:01 +00:00
|
|
|
teNumeric,
|
2000-10-18 03:12:42 +00:00
|
|
|
teOpenParen,
|
2000-10-17 00:41:54 +00:00
|
|
|
tePlus,
|
2000-10-12 01:32:01 +00:00
|
|
|
teString,
|
2000-10-18 03:12:42 +00:00
|
|
|
teUnknown,
|
2000-10-12 01:32:01 +00:00
|
|
|
};
|
|
|
|
|
2000-10-18 01:02:13 +00:00
|
|
|
/*
|
2000-10-12 01:32:01 +00:00
|
|
|
enum TokenType {
|
2000-10-18 01:02:13 +00:00
|
|
|
// verified token type
|
2000-10-12 01:32:01 +00:00
|
|
|
ttUndetermined,
|
|
|
|
ttLabel,
|
|
|
|
ttInstruction,
|
|
|
|
ttRegister,
|
|
|
|
ttRegisterList,
|
|
|
|
ttNotARegister,
|
|
|
|
ttString,
|
|
|
|
ttNumber,
|
|
|
|
ttOffsetKeyword
|
|
|
|
};
|
2000-10-18 01:02:13 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
enum OperandType {
|
|
|
|
otNone = 0,
|
|
|
|
otArgumentList,
|
|
|
|
otBinaryOp,
|
|
|
|
otBool,
|
|
|
|
otDouble,
|
|
|
|
otICodeModule,
|
|
|
|
otJSClass,
|
|
|
|
otJSString,
|
|
|
|
otJSFunction,
|
|
|
|
otJSType,
|
|
|
|
otLabel,
|
|
|
|
otUInt32,
|
|
|
|
otRegister,
|
|
|
|
otStringAtom
|
|
|
|
};
|
2000-10-12 01:32:01 +00:00
|
|
|
|
|
|
|
struct ICodeParseException {
|
2000-10-12 23:58:51 +00:00
|
|
|
ICodeParseException (string aMsg, iter aPos = 0)
|
|
|
|
: msg(aMsg), pos(aPos) {}
|
2000-10-12 01:32:01 +00:00
|
|
|
string msg;
|
2000-10-12 23:58:51 +00:00
|
|
|
iter pos;
|
2000-10-12 01:32:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct TokenLocation {
|
2000-10-18 01:02:13 +00:00
|
|
|
TokenLocation () : begin(0), estimate(teIllegal) /*,
|
|
|
|
type(ttUndetermined) */ {}
|
|
|
|
iter begin;
|
2000-10-12 01:32:01 +00:00
|
|
|
TokenEstimation estimate;
|
2000-10-18 01:02:13 +00:00
|
|
|
// TokenType type;
|
2000-10-12 01:32:01 +00:00
|
|
|
};
|
|
|
|
|
2000-10-18 01:02:13 +00:00
|
|
|
struct AnyOperand {
|
|
|
|
OperandType type;
|
|
|
|
int64 data;
|
|
|
|
/*void *data;*/
|
2000-10-12 01:32:01 +00:00
|
|
|
};
|
2000-10-17 08:02:16 +00:00
|
|
|
|
2000-10-12 01:32:01 +00:00
|
|
|
struct StatementNode {
|
2000-10-18 01:02:13 +00:00
|
|
|
iter begin;
|
2000-10-12 01:32:01 +00:00
|
|
|
uint icodeID;
|
2000-10-19 00:03:12 +00:00
|
|
|
AnyOperand operand[4];
|
2000-10-12 23:58:51 +00:00
|
|
|
};
|
2000-10-12 01:32:01 +00:00
|
|
|
|
|
|
|
class ICodeParser
|
|
|
|
{
|
|
|
|
private:
|
2000-10-19 03:32:23 +00:00
|
|
|
ICodeParser(const ICodeParser&); /* No copy constructor */
|
2000-10-19 00:03:12 +00:00
|
|
|
|
|
|
|
Interpreter::Context *mCx;
|
2000-10-12 01:32:01 +00:00
|
|
|
uint mMaxRegister;
|
|
|
|
std::vector<StatementNode *> mStatementNodes;
|
2000-10-17 08:02:16 +00:00
|
|
|
VM::LabelList mUnnamedLabels;
|
|
|
|
typedef std::map<const char *, VM::Label*> LabelMap;
|
|
|
|
LabelMap mNamedLabels;
|
2000-10-12 01:32:01 +00:00
|
|
|
|
|
|
|
public:
|
2000-10-19 03:32:23 +00:00
|
|
|
ICodeParser (Interpreter::Context *aCx = 0) : mCx(aCx) {}
|
2000-10-18 01:02:13 +00:00
|
|
|
void ParseSourceFromString (const string &source);
|
2000-10-12 23:59:25 +00:00
|
|
|
|
2000-10-17 08:02:16 +00:00
|
|
|
/* locate the beginning of the next token and take a guess at what it
|
|
|
|
* might be */
|
2000-10-12 01:32:01 +00:00
|
|
|
TokenLocation SeekTokenStart (iter begin, iter end);
|
2000-10-12 23:59:25 +00:00
|
|
|
|
2000-10-17 00:41:54 +00:00
|
|
|
/* general purpose parse functions; |begin| is expected to point
|
|
|
|
* at the start of the token to be processed (eg, these routines
|
|
|
|
* don't call |SeekTokenStart|, and (currently, this might change) no
|
|
|
|
* initial check is done to ensure that |begin| != |end|.
|
|
|
|
*/
|
|
|
|
iter ParseAlpha (iter begin, iter end, string **rval);
|
2000-10-12 23:58:51 +00:00
|
|
|
iter ParseBool (iter begin, iter end, bool *rval);
|
2000-10-17 00:41:54 +00:00
|
|
|
iter ParseDouble (iter begin, iter end, double *rval);
|
2000-10-18 01:02:13 +00:00
|
|
|
iter ParseInt32 (iter begin, iter end, int32 *rval);
|
|
|
|
iter ParseRegister (iter begin, iter end, JSTypes::Register *rval);
|
2000-10-17 00:41:54 +00:00
|
|
|
iter ParseString (iter begin, iter end, string **rval);
|
|
|
|
iter ParseUInt32 (iter begin, iter end, uint32 *rval);
|
2000-10-12 23:58:51 +00:00
|
|
|
|
2000-10-18 01:02:13 +00:00
|
|
|
/* operand parse functions; These functions take care of finding
|
|
|
|
* the start of the token with |SeekTokenStart|, and checking the
|
|
|
|
* "estimation" (explicit checking takes care of |begin| == |end|,
|
|
|
|
* aka EOF, because EOF is a token estimate.) Once the start of the
|
|
|
|
* token is found, and it is the expected type, the actual parsing is
|
|
|
|
* carried out by one of the general purpose parse functions.
|
|
|
|
*/
|
|
|
|
iter ParseArgumentListOperand (iter begin, iter end,
|
|
|
|
VM::ArgumentList **rval);
|
|
|
|
iter ParseBinaryOpOperand (iter begin, iter end,
|
|
|
|
VM::BinaryOperator::BinaryOp *rval);
|
|
|
|
iter ParseBoolOperand (iter begin, iter end, bool *rval);
|
|
|
|
iter ParseDoubleOperand (iter begin, iter end, double *rval);
|
|
|
|
iter ParseICodeModuleOperand (iter begin, iter end, string **rval);
|
|
|
|
iter ParseJSClassOperand (iter begin, iter end, string **rval);
|
2000-10-19 00:03:12 +00:00
|
|
|
iter ParseJSStringOperand (iter begin, iter end,
|
|
|
|
JSTypes::JSString **rval);
|
2000-10-18 01:02:13 +00:00
|
|
|
iter ParseJSFunctionOperand (iter begin, iter end, string **rval);
|
2000-10-19 00:03:12 +00:00
|
|
|
iter ParseJSTypeOperand (iter begin, iter end, JSTypes::JSType **rval);
|
2000-10-18 01:02:13 +00:00
|
|
|
iter ParseLabelOperand (iter begin, iter end, VM::Label **rval);
|
|
|
|
iter ParseUInt32Operand (iter begin, iter end, uint32 *rval);
|
|
|
|
iter ParseRegisterOperand (iter begin, iter end,
|
|
|
|
JSTypes::Register *rval);
|
2000-10-19 00:03:12 +00:00
|
|
|
iter ParseStringAtomOperand (iter begin, iter end, StringAtom **rval);
|
2000-10-18 01:02:13 +00:00
|
|
|
|
2000-10-12 23:59:25 +00:00
|
|
|
/* "high level" parse functions */
|
2000-10-12 01:32:01 +00:00
|
|
|
iter ParseInstruction (uint icodeID, iter start, iter end);
|
2000-10-18 20:44:14 +00:00
|
|
|
iter ParseNextStatement (iter begin, iter end);
|
2000-10-12 01:32:01 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2000-10-18 03:12:42 +00:00
|
|
|
|
|
|
|
#endif /* #ifndef __icodeasm_h */
|