2000-10-11 06:44:14 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2000-10-12 05:32:01 +04:00
|
|
|
#include "icodeasm.h"
|
2000-10-11 06:44:14 +04:00
|
|
|
#include "icodemap.h"
|
2000-11-14 06:11:54 +03:00
|
|
|
#include "utilities.h"
|
|
|
|
#include "lexutils.h"
|
|
|
|
#include "exception.h"
|
2000-10-11 06:44:14 +04:00
|
|
|
|
|
|
|
namespace JavaScript {
|
|
|
|
namespace ICodeASM {
|
2000-11-14 06:11:54 +03:00
|
|
|
using namespace LexUtils;
|
|
|
|
|
2000-10-17 04:41:54 +04:00
|
|
|
static char *keyword_offset = "offset";
|
2001-01-11 03:03:05 +03:00
|
|
|
static char *keyword_exprNodeKinds[] = {"add", "subtract", "multiply", "divide",
|
2000-10-18 05:02:13 +04:00
|
|
|
"remainder", "leftshift", "rightshift",
|
2000-10-20 14:33:57 +04:00
|
|
|
"logicalrightshift", "bitwiseor",
|
|
|
|
"bitwisexor", "bitwiseand", "less",
|
2000-10-18 05:02:13 +04:00
|
|
|
"lessorequal", "equal", "identical", 0};
|
2001-01-11 03:03:05 +03:00
|
|
|
static ExprNode::Kind exprNodeOps[] =
|
|
|
|
{ ExprNode::add, ExprNode::subtract, ExprNode::multiply, ExprNode::divide,
|
|
|
|
ExprNode::modulo, ExprNode::leftShift, ExprNode::rightShift,
|
|
|
|
ExprNode::logicalRightShift, ExprNode::bitwiseOr,
|
|
|
|
ExprNode::bitwiseXor, ExprNode::bitwiseAnd, ExprNode::lessThan,
|
|
|
|
ExprNode::lessThanOrEqual, ExprNode::equal, ExprNode::identical };
|
2000-10-19 00:44:14 +04:00
|
|
|
|
2000-10-11 06:44:14 +04:00
|
|
|
void
|
2000-11-14 06:11:54 +03:00
|
|
|
ICodeParser::parseSourceFromString (const string8 &source)
|
2000-10-11 06:44:14 +04:00
|
|
|
{
|
2000-10-12 05:32:01 +04:00
|
|
|
uint statementNo = 0;
|
2000-11-14 06:11:54 +03:00
|
|
|
string8_citer begin = source.begin();
|
|
|
|
string8_citer end = source.end();
|
2000-10-21 02:18:52 +04:00
|
|
|
|
|
|
|
mInstructions = new VM::InstructionStream();
|
2000-10-20 07:33:37 +04:00
|
|
|
mMaxRegister = 0;
|
2000-10-20 14:33:57 +04:00
|
|
|
mInstructionCount = 0;
|
2000-10-21 02:18:52 +04:00
|
|
|
mLabels.clear();
|
|
|
|
mNamedLabels.clear();
|
2000-10-20 07:33:37 +04:00
|
|
|
|
2000-10-19 00:44:14 +04:00
|
|
|
while (begin < end)
|
2000-10-11 06:44:14 +04:00
|
|
|
{
|
2000-10-18 07:12:42 +04:00
|
|
|
try
|
|
|
|
{
|
|
|
|
++statementNo;
|
2000-11-14 06:11:54 +03:00
|
|
|
begin = parseNextStatement (begin, end);
|
2000-10-18 07:12:42 +04:00
|
|
|
}
|
2000-11-14 06:11:54 +03:00
|
|
|
catch (JSException &e)
|
2000-10-18 07:12:42 +04:00
|
|
|
{
|
2000-11-14 06:11:54 +03:00
|
|
|
string8 etext;
|
|
|
|
e.toString8(etext);
|
|
|
|
|
|
|
|
fprintf (stderr, "%s at statement %u\n",
|
|
|
|
etext.c_str(), statementNo);
|
2000-10-18 07:12:42 +04:00
|
|
|
return;
|
|
|
|
}
|
2000-10-11 06:44:14 +04:00
|
|
|
}
|
2000-10-19 00:44:14 +04:00
|
|
|
|
|
|
|
if (begin > end)
|
|
|
|
NOT_REACHED ("Overran source buffer!");
|
2000-10-11 06:44:14 +04:00
|
|
|
}
|
|
|
|
|
2000-10-18 05:02:13 +04:00
|
|
|
/**********************************************************************
|
|
|
|
* operand parse functions (see comment in the .h file) ...
|
|
|
|
*/
|
2000-10-13 03:58:51 +04:00
|
|
|
|
2000-11-14 06:11:54 +03:00
|
|
|
string8_citer
|
|
|
|
ICodeParser::parseArgumentListOperand (string8_citer begin,
|
|
|
|
string8_citer end,
|
2000-10-18 07:12:42 +04:00
|
|
|
VM::ArgumentList **rval)
|
2000-10-12 11:55:13 +04:00
|
|
|
{
|
2000-10-19 02:01:17 +04:00
|
|
|
/* parse argument list on the format "(['argname': ]register[, ...])" */
|
2000-11-14 06:11:54 +03:00
|
|
|
TokenLocation tl = seekTokenStart (begin, end);
|
2000-10-18 07:12:42 +04:00
|
|
|
VM::ArgumentList *al = new VM::ArgumentList();
|
|
|
|
|
|
|
|
if (tl.estimate != teOpenParen)
|
2000-11-14 06:11:54 +03:00
|
|
|
throw JSParseException (eidExpectArgList);
|
2000-10-18 07:12:42 +04:00
|
|
|
|
2000-11-14 06:11:54 +03:00
|
|
|
tl = seekTokenStart (tl.begin + 1, end);
|
2001-01-20 02:56:37 +03:00
|
|
|
StringFormatter s_fmt;
|
2000-10-19 02:01:17 +04:00
|
|
|
while (tl.estimate == teString || tl.estimate == teAlpha) {
|
|
|
|
string *argName = 0;
|
2000-10-18 07:12:42 +04:00
|
|
|
|
2000-10-19 02:01:17 +04:00
|
|
|
if (tl.estimate == teString) {
|
|
|
|
/* look for the argname in quotes */
|
2000-11-14 06:11:54 +03:00
|
|
|
begin = lexString8 (tl.begin, end, &argName);
|
2000-10-19 02:01:17 +04:00
|
|
|
|
|
|
|
/* look for the : */
|
2000-11-14 06:11:54 +03:00
|
|
|
tl = seekTokenStart (begin, end);
|
2000-10-19 02:01:17 +04:00
|
|
|
if (tl.estimate != teColon)
|
2000-11-14 06:11:54 +03:00
|
|
|
throw JSParseException (eidExpectColon);
|
2000-10-19 02:01:17 +04:00
|
|
|
|
|
|
|
/* and now the register */
|
2000-11-14 06:11:54 +03:00
|
|
|
tl = seekTokenStart (tl.begin + 1, end);
|
2000-10-19 02:01:17 +04:00
|
|
|
}
|
2000-10-18 07:12:42 +04:00
|
|
|
|
|
|
|
if (tl.estimate != teAlpha)
|
2000-11-14 06:11:54 +03:00
|
|
|
throw JSParseException (eidExpectRegister);
|
2000-10-18 07:12:42 +04:00
|
|
|
|
|
|
|
JSTypes::Register r;
|
2000-11-14 06:11:54 +03:00
|
|
|
begin = lexRegister (tl.begin, end, &r);
|
|
|
|
if (r != VM::NotARegister && r > mMaxRegister)
|
|
|
|
mMaxRegister = r;
|
|
|
|
|
2000-10-18 07:12:42 +04:00
|
|
|
/* pass 0 (null) as the "type" because it is
|
|
|
|
* not actually used by the interpreter, only in (the current)
|
|
|
|
* codegen (acording to rogerl.)
|
|
|
|
*/
|
|
|
|
VM::TypedRegister tr = VM::TypedRegister(r, 0);
|
2000-10-19 04:03:12 +04:00
|
|
|
|
|
|
|
StringAtom *sap = 0;
|
|
|
|
if (argName) {
|
|
|
|
sap = &(mCx->getWorld().identifiers[argName->c_str()]);
|
|
|
|
delete argName;
|
|
|
|
}
|
2001-01-20 02:56:37 +03:00
|
|
|
else {
|
|
|
|
/* if an argument name was not specified, use the position
|
|
|
|
* to build a default name.
|
|
|
|
*/
|
|
|
|
s_fmt << (uint32)al->size();
|
|
|
|
sap = &(mCx->getWorld().identifiers[s_fmt.getString()]);
|
|
|
|
s_fmt.clear();
|
|
|
|
|
|
|
|
}
|
2000-10-19 04:03:12 +04:00
|
|
|
VM::Argument arg = VM::Argument (tr, sap);
|
2000-10-18 07:12:42 +04:00
|
|
|
|
|
|
|
al->push_back(arg);
|
|
|
|
|
2000-11-14 06:11:54 +03:00
|
|
|
tl = seekTokenStart (begin, end);
|
2000-10-18 07:12:42 +04:00
|
|
|
/* if the next token is a comma,
|
|
|
|
* seek to the next one and go again */
|
|
|
|
if (tl.estimate == teComma) {
|
2000-11-14 06:11:54 +03:00
|
|
|
tl = seekTokenStart (tl.begin + 1, end);
|
2000-10-18 07:12:42 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tl.estimate != teCloseParen)
|
2000-11-14 06:11:54 +03:00
|
|
|
throw JSParseException (eidExpectCloseParen);
|
2000-10-18 07:12:42 +04:00
|
|
|
|
|
|
|
*rval = al;
|
|
|
|
|
|
|
|
return tl.begin + 1;
|
2000-10-12 11:55:13 +04:00
|
|
|
}
|
|
|
|
|
2000-11-14 06:11:54 +03:00
|
|
|
string8_citer
|
2001-01-11 03:03:05 +03:00
|
|
|
ICodeParser::parseExprNodeKindOperand (string8_citer begin, string8_citer end,
|
|
|
|
ExprNode::Kind *rval)
|
2000-10-12 11:55:13 +04:00
|
|
|
{
|
2000-11-14 06:11:54 +03:00
|
|
|
TokenLocation tl = seekTokenStart (begin, end);
|
2000-10-12 11:55:13 +04:00
|
|
|
|
2000-10-18 05:02:13 +04:00
|
|
|
if (tl.estimate != teAlpha)
|
2000-11-14 06:11:54 +03:00
|
|
|
throw JSParseException (eidExpectBinaryOp);
|
|
|
|
string8 *str;
|
|
|
|
end = lexAlpha (tl.begin, end, &str);
|
2000-10-18 05:02:13 +04:00
|
|
|
|
2001-01-11 03:03:05 +03:00
|
|
|
for (int i = 0; keyword_exprNodeKinds[i] != 0; ++i)
|
|
|
|
if (cmp_nocase (*str, keyword_exprNodeKinds[i], keyword_exprNodeKinds[i] +
|
2001-01-20 02:56:37 +03:00
|
|
|
strlen (keyword_exprNodeKinds[i])) == 0) {
|
2001-01-11 03:03:05 +03:00
|
|
|
*rval = exprNodeOps[i];
|
2000-10-18 05:02:13 +04:00
|
|
|
delete str;
|
|
|
|
return end;
|
|
|
|
}
|
|
|
|
|
|
|
|
delete str;
|
2000-11-14 06:11:54 +03:00
|
|
|
throw JSParseException (eidUnknownBinaryOp);
|
2000-10-12 05:32:01 +04:00
|
|
|
}
|
2000-10-13 03:58:51 +04:00
|
|
|
|
2000-11-14 06:11:54 +03:00
|
|
|
string8_citer
|
|
|
|
ICodeParser::parseBoolOperand (string8_citer begin, string8_citer end,
|
|
|
|
bool *rval)
|
2000-10-13 03:58:51 +04:00
|
|
|
{
|
2000-11-14 06:11:54 +03:00
|
|
|
TokenLocation tl = seekTokenStart (begin, end);
|
2000-10-13 03:58:51 +04:00
|
|
|
|
|
|
|
if (tl.estimate != teAlpha)
|
2000-11-14 06:11:54 +03:00
|
|
|
throw JSParseException (eidExpectBool);
|
2000-10-13 03:58:51 +04:00
|
|
|
|
2000-11-14 06:11:54 +03:00
|
|
|
return lexBool (tl.begin, end, rval);
|
2000-10-13 03:58:51 +04:00
|
|
|
}
|
|
|
|
|
2000-11-14 06:11:54 +03:00
|
|
|
string8_citer
|
|
|
|
ICodeParser::parseDoubleOperand (string8_citer begin, string8_citer end,
|
|
|
|
double *rval)
|
2000-10-14 04:06:42 +04:00
|
|
|
{
|
2000-11-14 06:11:54 +03:00
|
|
|
TokenLocation tl = seekTokenStart (begin, end);
|
2000-10-17 04:41:54 +04:00
|
|
|
|
|
|
|
if ((tl.estimate != teNumeric) && (tl.estimate != teMinus) &&
|
|
|
|
(tl.estimate != tePlus))
|
2000-11-14 06:11:54 +03:00
|
|
|
throw JSParseException (eidExpectDouble);
|
2000-10-17 04:41:54 +04:00
|
|
|
|
2000-11-14 06:11:54 +03:00
|
|
|
return lexDouble (tl.begin, end, rval);
|
2000-10-14 04:06:42 +04:00
|
|
|
}
|
|
|
|
|
2000-11-14 06:11:54 +03:00
|
|
|
string8_citer
|
2001-01-26 02:34:33 +03:00
|
|
|
ICodeParser::parseICodeModuleOperand (string8_citer begin,
|
|
|
|
string8_citer end,
|
|
|
|
VM::ICodeModule **rval)
|
2000-10-13 03:58:51 +04:00
|
|
|
{
|
2001-01-26 02:34:33 +03:00
|
|
|
TokenLocation tl = seekTokenStart (begin, end);
|
|
|
|
|
|
|
|
if (tl.estimate != teString)
|
|
|
|
throw JSParseException (eidExpectString);
|
|
|
|
|
|
|
|
string8 *str;
|
|
|
|
end = lexString8 (tl.begin, end, &str);
|
|
|
|
StringAtom &typename_atom = mCx->getWorld().identifiers[str->c_str()];
|
|
|
|
delete str;
|
|
|
|
JSTypes::JSValue jsv =
|
|
|
|
mCx->getGlobalObject()->getVariable(typename_atom);
|
|
|
|
if (!jsv.isFunction()) {
|
|
|
|
ASSERT(false);
|
|
|
|
}
|
|
|
|
*rval = jsv.function->getICode();
|
|
|
|
|
2000-10-19 04:03:12 +04:00
|
|
|
return end;
|
2001-01-26 02:34:33 +03:00
|
|
|
// NOT_REACHED ("ICode modules are hard, lets go shopping.");
|
|
|
|
// return end;
|
2000-10-13 03:58:51 +04:00
|
|
|
}
|
|
|
|
|
2000-11-14 06:11:54 +03:00
|
|
|
string8_citer
|
2001-01-20 02:56:37 +03:00
|
|
|
ICodeParser::parseJSClassOperand (string8_citer begin,
|
|
|
|
string8_citer end, JSTypes::JSType **rval)
|
2000-10-14 04:06:42 +04:00
|
|
|
{
|
2001-01-20 02:56:37 +03:00
|
|
|
TokenLocation tl = seekTokenStart (begin, end);
|
|
|
|
|
|
|
|
if (tl.estimate != teString)
|
|
|
|
throw JSParseException (eidExpectString);
|
|
|
|
|
|
|
|
string8 *str;
|
|
|
|
end = lexString8 (tl.begin, end, &str);
|
|
|
|
StringAtom &typename_atom = mCx->getWorld().identifiers[str->c_str()];
|
|
|
|
delete str;
|
|
|
|
JSTypes::JSValue jsv =
|
|
|
|
mCx->getGlobalObject()->getVariable(typename_atom);
|
|
|
|
if (jsv.isType())
|
|
|
|
*rval = jsv.type;
|
|
|
|
else
|
|
|
|
*rval = &(JSTypes::Any_Type);
|
|
|
|
|
2000-10-19 04:03:12 +04:00
|
|
|
return end;
|
2001-01-20 02:56:37 +03:00
|
|
|
// NOT_REACHED ("JSClasses are hard, lets go shopping.");
|
|
|
|
// return end;
|
2000-10-14 04:06:42 +04:00
|
|
|
}
|
|
|
|
|
2000-11-14 06:11:54 +03:00
|
|
|
string8_citer
|
|
|
|
ICodeParser::parseJSStringOperand (string8_citer begin, string8_citer end,
|
2000-10-19 04:03:12 +04:00
|
|
|
JSTypes::JSString **rval)
|
2000-10-14 04:06:42 +04:00
|
|
|
{
|
2000-11-14 06:11:54 +03:00
|
|
|
TokenLocation tl = seekTokenStart (begin, end);
|
2000-10-17 04:41:54 +04:00
|
|
|
|
|
|
|
if (tl.estimate != teString)
|
2000-11-14 06:11:54 +03:00
|
|
|
throw JSParseException (eidExpectString);
|
|
|
|
string8 *str;
|
|
|
|
end = lexString8 (tl.begin, end, &str);
|
2000-10-19 04:03:12 +04:00
|
|
|
*rval = new JSTypes::JSString (str->c_str());
|
|
|
|
delete str;
|
|
|
|
return end;
|
2000-10-14 04:06:42 +04:00
|
|
|
}
|
2000-10-12 11:55:13 +04:00
|
|
|
|
2000-11-14 06:11:54 +03:00
|
|
|
string8_citer
|
|
|
|
ICodeParser::parseJSFunctionOperand (string8_citer /*begin*/,
|
|
|
|
string8_citer end,
|
2000-10-21 02:18:52 +04:00
|
|
|
string ** /*rval*/)
|
2000-10-14 04:06:42 +04:00
|
|
|
{
|
2000-10-19 04:03:12 +04:00
|
|
|
NOT_REACHED ("JSFunctions are hard, lets go shopping.");
|
|
|
|
return end;
|
2000-10-14 04:06:42 +04:00
|
|
|
}
|
|
|
|
|
2000-11-14 06:11:54 +03:00
|
|
|
string8_citer
|
|
|
|
ICodeParser::parseJSTypeOperand (string8_citer begin, string8_citer end,
|
2000-10-19 04:03:12 +04:00
|
|
|
JSTypes::JSType **rval)
|
2000-10-14 04:06:42 +04:00
|
|
|
{
|
2000-11-14 06:11:54 +03:00
|
|
|
TokenLocation tl = seekTokenStart (begin, end);
|
2000-10-17 04:41:54 +04:00
|
|
|
|
|
|
|
if (tl.estimate != teString)
|
2000-11-14 06:11:54 +03:00
|
|
|
throw JSParseException (eidExpectString);
|
2000-10-19 04:03:12 +04:00
|
|
|
|
2000-11-14 06:11:54 +03:00
|
|
|
string8 *str;
|
|
|
|
end = lexString8 (tl.begin, end, &str);
|
2000-10-19 04:03:12 +04:00
|
|
|
StringAtom &typename_atom = mCx->getWorld().identifiers[str->c_str()];
|
|
|
|
delete str;
|
|
|
|
JSTypes::JSValue jsv =
|
|
|
|
mCx->getGlobalObject()->getVariable(typename_atom);
|
|
|
|
if (jsv.isType())
|
|
|
|
*rval = jsv.type;
|
|
|
|
else
|
|
|
|
*rval = &(JSTypes::Any_Type);
|
|
|
|
|
|
|
|
return end;
|
2000-10-14 04:06:42 +04:00
|
|
|
}
|
|
|
|
|
2000-11-14 06:11:54 +03:00
|
|
|
string8_citer
|
|
|
|
ICodeParser::parseLabelOperand (string8_citer begin, string8_citer end,
|
|
|
|
VM::Label **rval)
|
2000-10-14 04:06:42 +04:00
|
|
|
{
|
2000-11-14 06:11:54 +03:00
|
|
|
TokenLocation tl = seekTokenStart (begin, end);
|
2000-10-17 04:41:54 +04:00
|
|
|
|
|
|
|
if (tl.estimate != teAlpha)
|
2000-11-14 06:11:54 +03:00
|
|
|
throw JSParseException (eidExpectLabel);
|
2000-10-17 04:41:54 +04:00
|
|
|
|
2000-11-14 06:11:54 +03:00
|
|
|
string8 *str;
|
|
|
|
begin = lexAlpha (tl.begin, end, &str);
|
2000-10-17 12:02:16 +04:00
|
|
|
|
2000-10-18 05:02:13 +04:00
|
|
|
if (cmp_nocase(*str, keyword_offset, keyword_offset +
|
2001-01-20 02:56:37 +03:00
|
|
|
strlen(keyword_offset)) == 0) {
|
2000-10-20 14:33:57 +04:00
|
|
|
delete str;
|
2000-10-17 12:02:16 +04:00
|
|
|
/* got the "Offset" keyword, treat next thing as a jump offset
|
|
|
|
* expressed as "Offset +/-N" */
|
2000-11-14 06:11:54 +03:00
|
|
|
tl = seekTokenStart (begin, end);
|
2000-10-17 04:41:54 +04:00
|
|
|
|
2000-10-20 14:33:57 +04:00
|
|
|
if (tl.estimate != teNumeric)
|
2000-11-14 06:11:54 +03:00
|
|
|
throw JSParseException (eidExpectUInt32);
|
2000-10-20 14:33:57 +04:00
|
|
|
|
|
|
|
uint32 ofs;
|
2000-11-14 06:11:54 +03:00
|
|
|
begin = lexUInt32 (tl.begin, end, &ofs);
|
2000-10-20 07:33:37 +04:00
|
|
|
VM::Label *new_label = new VM::Label(mInstructions);
|
2000-10-20 14:33:57 +04:00
|
|
|
new_label->mOffset = ofs;
|
2000-10-21 02:18:52 +04:00
|
|
|
mLabels.push_back (new_label);
|
2000-10-18 05:02:13 +04:00
|
|
|
*rval = new_label;
|
2000-10-17 04:41:54 +04:00
|
|
|
} else {
|
2000-10-17 12:02:16 +04:00
|
|
|
/* label expressed as "label_name", look for it in the
|
|
|
|
* namedlabels map */
|
2000-10-18 05:02:13 +04:00
|
|
|
LabelMap::const_iterator l = mNamedLabels.find(str->c_str());
|
|
|
|
if (l != mNamedLabels.end()) {
|
2000-10-17 12:02:16 +04:00
|
|
|
/* found the label, use it */
|
2000-10-18 05:02:13 +04:00
|
|
|
*rval = (*l).second;
|
2000-10-17 12:02:16 +04:00
|
|
|
} else {
|
|
|
|
/* havn't seen the label definition yet, put a placeholder
|
|
|
|
* in the namedlabels map */
|
2000-10-20 07:33:37 +04:00
|
|
|
VM::Label *new_label = new VM::Label(mInstructions);
|
2000-10-17 12:02:16 +04:00
|
|
|
new_label->mOffset = VM::NotALabel;
|
2000-10-18 05:02:13 +04:00
|
|
|
*rval = new_label;
|
|
|
|
mNamedLabels[str->c_str()] = new_label;
|
2000-10-21 02:18:52 +04:00
|
|
|
mLabels.push_back (new_label);
|
2000-10-17 12:02:16 +04:00
|
|
|
}
|
2000-10-20 14:33:57 +04:00
|
|
|
delete str;
|
2000-10-17 04:41:54 +04:00
|
|
|
}
|
2000-10-17 12:02:16 +04:00
|
|
|
return begin;
|
2000-10-14 04:06:42 +04:00
|
|
|
}
|
|
|
|
|
2000-11-14 06:11:54 +03:00
|
|
|
string8_citer
|
|
|
|
ICodeParser::parseUInt32Operand (string8_citer begin,
|
|
|
|
string8_citer end, uint32 *rval)
|
2000-10-14 04:06:42 +04:00
|
|
|
{
|
2000-11-14 06:11:54 +03:00
|
|
|
TokenLocation tl = seekTokenStart (begin, end);
|
2000-10-18 05:02:13 +04:00
|
|
|
|
|
|
|
if (tl.estimate != teNumeric)
|
2000-11-14 06:11:54 +03:00
|
|
|
throw JSParseException (eidExpectUInt32);
|
2000-10-18 05:02:13 +04:00
|
|
|
|
2000-11-14 06:11:54 +03:00
|
|
|
return lexUInt32 (tl.begin, end, rval);
|
2000-10-14 04:06:42 +04:00
|
|
|
}
|
|
|
|
|
2000-11-14 06:11:54 +03:00
|
|
|
string8_citer
|
|
|
|
ICodeParser::parseRegisterOperand (string8_citer begin, string8_citer end,
|
2000-10-18 05:02:13 +04:00
|
|
|
JSTypes::Register *rval)
|
2000-10-14 04:06:42 +04:00
|
|
|
{
|
2000-11-14 06:11:54 +03:00
|
|
|
TokenLocation tl = seekTokenStart (begin, end);
|
|
|
|
|
|
|
|
end = lexRegister (tl.begin, end, rval);
|
|
|
|
if (*rval != VM::NotARegister && *rval > mMaxRegister)
|
|
|
|
mMaxRegister = *rval;
|
2000-10-18 05:02:13 +04:00
|
|
|
|
2000-11-14 06:11:54 +03:00
|
|
|
return end;
|
2000-10-14 04:06:42 +04:00
|
|
|
}
|
|
|
|
|
2000-11-14 06:11:54 +03:00
|
|
|
string8_citer
|
|
|
|
ICodeParser::parseStringAtomOperand (string8_citer begin, string8_citer end,
|
|
|
|
StringAtom **rval)
|
2000-10-14 04:06:42 +04:00
|
|
|
{
|
2000-11-14 06:11:54 +03:00
|
|
|
TokenLocation tl = seekTokenStart (begin, end);
|
2000-10-18 05:02:13 +04:00
|
|
|
|
|
|
|
if (tl.estimate != teString)
|
2000-11-14 06:11:54 +03:00
|
|
|
throw JSParseException (eidExpectString);
|
|
|
|
string8 *str;
|
|
|
|
end = lexString8 (tl.begin, end, &str);
|
2000-10-19 04:03:12 +04:00
|
|
|
*rval = &(mCx->getWorld().identifiers[str->c_str()]);
|
|
|
|
delete str;
|
|
|
|
return end;
|
2000-10-18 05:02:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* "High Level" parse functions ... */
|
2000-11-14 06:11:54 +03:00
|
|
|
string8_citer
|
|
|
|
ICodeParser::parseInstruction (uint icodeID, string8_citer begin,
|
|
|
|
string8_citer end)
|
2000-10-18 05:02:13 +04:00
|
|
|
{
|
2000-11-14 06:11:54 +03:00
|
|
|
string8_citer curpos = begin;
|
2000-10-20 07:33:37 +04:00
|
|
|
StatementNode node;
|
|
|
|
node.icodeID = icodeID;
|
2000-10-18 05:02:13 +04:00
|
|
|
|
|
|
|
# define CASE_TYPE(T, C, CTYPE) \
|
|
|
|
case ot##T: \
|
|
|
|
{ \
|
|
|
|
C rval; \
|
2000-10-20 07:33:37 +04:00
|
|
|
node.operand[i].type = ot##T; \
|
2000-11-14 06:11:54 +03:00
|
|
|
curpos = parse##T##Operand (curpos, end, &rval); \
|
2000-10-20 07:33:37 +04:00
|
|
|
node.operand[i].data = CTYPE<int64>(rval); \
|
2000-10-18 05:02:13 +04:00
|
|
|
break; \
|
|
|
|
}
|
|
|
|
|
|
|
|
for (uint i = 0; i < 4; ++i)
|
|
|
|
{
|
|
|
|
switch (icodemap[icodeID].otype[i])
|
|
|
|
{
|
|
|
|
CASE_TYPE(ArgumentList, VM::ArgumentList *, reinterpret_cast);
|
2001-01-11 03:03:05 +03:00
|
|
|
CASE_TYPE(ExprNodeKind, ExprNode::Kind, static_cast);
|
2000-10-18 05:02:13 +04:00
|
|
|
CASE_TYPE(Bool, bool, static_cast);
|
|
|
|
CASE_TYPE(Double, double, static_cast);
|
2001-01-26 02:34:33 +03:00
|
|
|
CASE_TYPE(ICodeModule, VM::ICodeModule *, reinterpret_cast);
|
2001-01-20 02:56:37 +03:00
|
|
|
CASE_TYPE(JSClass, JSTypes::JSType *, reinterpret_cast);
|
2000-10-19 04:03:12 +04:00
|
|
|
CASE_TYPE(JSString, JSTypes::JSString *, reinterpret_cast);
|
2000-10-18 05:02:13 +04:00
|
|
|
CASE_TYPE(JSFunction, string *, reinterpret_cast);
|
2000-10-19 04:03:12 +04:00
|
|
|
CASE_TYPE(JSType, JSTypes::JSType *, reinterpret_cast);
|
2000-10-18 05:02:13 +04:00
|
|
|
CASE_TYPE(Label, VM::Label *, reinterpret_cast);
|
|
|
|
CASE_TYPE(UInt32, uint32, static_cast);
|
|
|
|
CASE_TYPE(Register, JSTypes::Register, static_cast);
|
2000-10-19 04:03:12 +04:00
|
|
|
CASE_TYPE(StringAtom, StringAtom *, reinterpret_cast);
|
2000-10-18 05:02:13 +04:00
|
|
|
default:
|
2000-10-20 07:33:37 +04:00
|
|
|
node.operand[i].type = otNone;
|
2000-10-18 05:02:13 +04:00
|
|
|
break;
|
|
|
|
}
|
2000-10-18 07:12:42 +04:00
|
|
|
if (i != 3 && icodemap[icodeID].otype[i + 1] != otNone) {
|
2000-10-19 00:44:14 +04:00
|
|
|
/* if the instruction has more arguments, eat a comma and
|
|
|
|
* locate the next token */
|
2000-11-14 06:11:54 +03:00
|
|
|
TokenLocation tl = seekTokenStart (curpos, end);
|
2000-10-18 07:12:42 +04:00
|
|
|
if (tl.estimate != teComma)
|
2000-11-14 06:11:54 +03:00
|
|
|
throw JSParseException (eidExpectComma);
|
|
|
|
tl = seekTokenStart (tl.begin + 1, end);
|
2000-10-18 07:12:42 +04:00
|
|
|
curpos = tl.begin;
|
|
|
|
}
|
2000-10-18 05:02:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
# undef CASE_TYPE
|
|
|
|
|
2000-10-21 02:18:52 +04:00
|
|
|
mInstructions->push_back (InstructionFromNode(&node));
|
2000-10-20 07:33:37 +04:00
|
|
|
++mInstructionCount;
|
|
|
|
|
2000-11-14 06:11:54 +03:00
|
|
|
TokenLocation tl = seekTokenStart (curpos, end);
|
2000-10-19 00:44:14 +04:00
|
|
|
if (tl.estimate != teNewline && tl.estimate != teEOF)
|
2000-11-14 06:11:54 +03:00
|
|
|
throw JSParseException (eidExpectNewline);
|
2000-10-19 00:44:14 +04:00
|
|
|
|
|
|
|
if (tl.estimate == teEOF)
|
|
|
|
return tl.begin;
|
|
|
|
else
|
|
|
|
return tl.begin + 1;
|
2000-10-18 05:02:13 +04:00
|
|
|
}
|
|
|
|
|
2000-11-14 06:11:54 +03:00
|
|
|
string8_citer
|
|
|
|
ICodeParser::parseNextStatement (string8_citer begin, string8_citer end)
|
2000-10-18 05:02:13 +04:00
|
|
|
{
|
|
|
|
bool isLabel = false;
|
2000-11-14 06:11:54 +03:00
|
|
|
string8_citer firstTokenEnd = end;
|
|
|
|
TokenLocation tl = seekTokenStart (begin, end);
|
2000-10-18 05:02:13 +04:00
|
|
|
|
2000-10-19 00:44:14 +04:00
|
|
|
if (tl.estimate == teNewline) {
|
|
|
|
/* empty statement, do nothing */
|
|
|
|
return tl.begin + 1;
|
|
|
|
}
|
|
|
|
|
2000-10-18 05:02:13 +04:00
|
|
|
if (tl.estimate != teAlpha)
|
2000-11-14 06:11:54 +03:00
|
|
|
throw JSParseException (eidExpectIdentifier);
|
2000-10-18 05:02:13 +04:00
|
|
|
|
2000-11-14 06:11:54 +03:00
|
|
|
for (string8_citer curpos = tl.begin; curpos < end; ++curpos) {
|
2000-10-18 05:02:13 +04:00
|
|
|
switch (*curpos)
|
|
|
|
{
|
|
|
|
case ':':
|
|
|
|
isLabel = true;
|
|
|
|
firstTokenEnd = ++curpos;
|
|
|
|
goto scan_done;
|
|
|
|
|
|
|
|
default:
|
2000-10-19 00:44:14 +04:00
|
|
|
if (!IS_ALPHA(*curpos)) {
|
|
|
|
firstTokenEnd = curpos;
|
|
|
|
goto scan_done;
|
|
|
|
}
|
2000-10-18 05:02:13 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
scan_done:
|
|
|
|
|
|
|
|
if (isLabel) {
|
|
|
|
/* the thing we scanned was a label...
|
|
|
|
* ignore the trailing : */
|
2000-11-14 06:11:54 +03:00
|
|
|
string8 label_str(tl.begin, firstTokenEnd - 1);
|
2000-10-18 05:02:13 +04:00
|
|
|
/* check to see if it was already referenced... */
|
|
|
|
LabelMap::const_iterator l = mNamedLabels.find(label_str.c_str());
|
|
|
|
if (l == mNamedLabels.end()) {
|
|
|
|
/* if it wasn't already referenced, add it */
|
2000-10-20 07:33:37 +04:00
|
|
|
VM::Label *new_label = new VM::Label (mInstructions);
|
|
|
|
new_label->mOffset = mInstructionCount;
|
2000-10-18 05:02:13 +04:00
|
|
|
mNamedLabels[label_str.c_str()] = new_label;
|
2000-10-21 02:18:52 +04:00
|
|
|
mLabels.push_back(new_label);
|
2000-10-18 05:02:13 +04:00
|
|
|
} else {
|
|
|
|
/* if it was already referenced, check to see if the offset
|
|
|
|
* was already set */
|
|
|
|
if ((*l).second->mOffset == VM::NotALabel) {
|
|
|
|
/* offset not set yet, set it and move along */
|
2000-10-20 07:33:37 +04:00
|
|
|
(*l).second->mOffset = mInstructionCount;
|
2000-10-18 05:02:13 +04:00
|
|
|
} else {
|
|
|
|
/* offset was already set, this must be a dupe! */
|
2000-11-14 06:11:54 +03:00
|
|
|
throw JSParseException (eidDuplicateLabel);
|
2000-10-18 05:02:13 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* the thing we scanned was an instruction, search the icode map
|
|
|
|
* for a matching instruction */
|
2000-11-14 06:11:54 +03:00
|
|
|
string8 icode_str(tl.begin, firstTokenEnd);
|
2000-10-18 05:02:13 +04:00
|
|
|
for (uint i = 0; i < icodemap_size; ++i)
|
|
|
|
if (cmp_nocase(icode_str, &icodemap[i].name[0],
|
|
|
|
&icodemap[i].name[0] +
|
2001-01-20 02:56:37 +03:00
|
|
|
strlen(icodemap[i].name)) == 0)
|
2000-10-18 05:02:13 +04:00
|
|
|
/* if match found, parse it's operands */
|
2000-11-14 06:11:54 +03:00
|
|
|
return parseInstruction (i, firstTokenEnd, end);
|
2000-10-18 05:02:13 +04:00
|
|
|
/* otherwise, choke on it */
|
2000-11-14 06:11:54 +03:00
|
|
|
throw JSParseException (eidUnknownICode);
|
2000-10-18 05:02:13 +04:00
|
|
|
}
|
2000-12-16 09:56:37 +03:00
|
|
|
return firstTokenEnd;
|
2000-10-14 04:06:42 +04:00
|
|
|
}
|
2000-10-19 04:03:12 +04:00
|
|
|
|
2000-10-11 06:44:14 +04:00
|
|
|
}
|
|
|
|
}
|