2009-06-10 18:29:44 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
2008-08-21 03:47:33 -07:00
|
|
|
* vim: set ts=8 sw=4 et tw=79:
|
2007-03-22 10:30:00 -07:00
|
|
|
*
|
|
|
|
* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla 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/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is Mozilla Communicator client code, released
|
|
|
|
* March 31, 1998.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either of the GNU General Public License Version 2 or later (the "GPL"),
|
|
|
|
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
#ifndef jsemit_h___
|
|
|
|
#define jsemit_h___
|
|
|
|
/*
|
|
|
|
* JS bytecode generation.
|
|
|
|
*/
|
|
|
|
#include "jstypes.h"
|
|
|
|
#include "jsatom.h"
|
|
|
|
#include "jsopcode.h"
|
2009-04-05 21:17:22 -07:00
|
|
|
#include "jsparse.h"
|
2007-05-24 00:51:46 -07:00
|
|
|
#include "jsscript.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "jsprvtd.h"
|
|
|
|
#include "jspubtd.h"
|
|
|
|
|
|
|
|
JS_BEGIN_EXTERN_C
|
|
|
|
|
|
|
|
/*
|
|
|
|
* NB: If you add enumerators for scope statements, add them between STMT_WITH
|
2008-01-04 17:34:11 -08:00
|
|
|
* and STMT_CATCH, or you will break the STMT_TYPE_IS_SCOPE macro. If you add
|
2007-03-22 10:30:00 -07:00
|
|
|
* non-looping statement enumerators, add them before STMT_DO_LOOP or you will
|
|
|
|
* break the STMT_TYPE_IS_LOOP macro.
|
|
|
|
*
|
|
|
|
* Also remember to keep the statementName array in jsemit.c in sync.
|
|
|
|
*/
|
|
|
|
typedef enum JSStmtType {
|
|
|
|
STMT_LABEL, /* labeled statement: L: s */
|
|
|
|
STMT_IF, /* if (then) statement */
|
|
|
|
STMT_ELSE, /* else clause of if statement */
|
2008-10-08 18:43:28 -07:00
|
|
|
STMT_SEQ, /* synthetic sequence of statements */
|
2007-03-22 10:30:00 -07:00
|
|
|
STMT_BLOCK, /* compound statement: { s1[;... sN] } */
|
2008-01-08 16:49:35 -08:00
|
|
|
STMT_SWITCH, /* switch statement */
|
2007-03-22 10:30:00 -07:00
|
|
|
STMT_WITH, /* with statement */
|
|
|
|
STMT_CATCH, /* catch block */
|
|
|
|
STMT_TRY, /* try block */
|
|
|
|
STMT_FINALLY, /* finally block */
|
|
|
|
STMT_SUBROUTINE, /* gosub-target subroutine body */
|
|
|
|
STMT_DO_LOOP, /* do/while loop statement */
|
|
|
|
STMT_FOR_LOOP, /* for loop statement */
|
|
|
|
STMT_FOR_IN_LOOP, /* for/in loop statement */
|
2008-02-13 06:32:31 -08:00
|
|
|
STMT_WHILE_LOOP, /* while loop statement */
|
|
|
|
STMT_LIMIT
|
2007-03-22 10:30:00 -07:00
|
|
|
} JSStmtType;
|
|
|
|
|
|
|
|
#define STMT_TYPE_IN_RANGE(t,b,e) ((uint)((t) - (b)) <= (uintN)((e) - (b)))
|
|
|
|
|
|
|
|
/*
|
|
|
|
* A comment on the encoding of the JSStmtType enum and type-testing macros:
|
|
|
|
*
|
|
|
|
* STMT_TYPE_MAYBE_SCOPE tells whether a statement type is always, or may
|
2008-01-08 16:49:35 -08:00
|
|
|
* become, a lexical scope. It therefore includes block and switch (the two
|
|
|
|
* low-numbered "maybe" scope types) and excludes with (with has dynamic scope
|
|
|
|
* pending the "reformed with" in ES4/JS2). It includes all try-catch-finally
|
|
|
|
* types, which are high-numbered maybe-scope types.
|
2007-03-22 10:30:00 -07:00
|
|
|
*
|
|
|
|
* STMT_TYPE_LINKS_SCOPE tells whether a JSStmtInfo of the given type eagerly
|
2008-01-08 16:49:35 -08:00
|
|
|
* links to other scoping statement info records. It excludes the two early
|
|
|
|
* "maybe" types, block and switch, as well as the try and both finally types,
|
|
|
|
* since try and the other trailing maybe-scope types don't need block scope
|
|
|
|
* unless they contain let declarations.
|
2007-03-22 10:30:00 -07:00
|
|
|
*
|
2008-01-04 17:34:11 -08:00
|
|
|
* We treat WITH as a static scope because it prevents lexical binding from
|
2008-08-11 23:45:40 -07:00
|
|
|
* continuing further up the static scope chain. With the lost "reformed with"
|
|
|
|
* proposal for ES4, we would be able to model it statically, too.
|
2007-03-22 10:30:00 -07:00
|
|
|
*/
|
|
|
|
#define STMT_TYPE_MAYBE_SCOPE(type) \
|
|
|
|
(type != STMT_WITH && \
|
|
|
|
STMT_TYPE_IN_RANGE(type, STMT_BLOCK, STMT_SUBROUTINE))
|
|
|
|
|
|
|
|
#define STMT_TYPE_LINKS_SCOPE(type) \
|
|
|
|
STMT_TYPE_IN_RANGE(type, STMT_WITH, STMT_CATCH)
|
|
|
|
|
|
|
|
#define STMT_TYPE_IS_TRYING(type) \
|
|
|
|
STMT_TYPE_IN_RANGE(type, STMT_TRY, STMT_SUBROUTINE)
|
|
|
|
|
|
|
|
#define STMT_TYPE_IS_LOOP(type) ((type) >= STMT_DO_LOOP)
|
|
|
|
|
|
|
|
#define STMT_MAYBE_SCOPE(stmt) STMT_TYPE_MAYBE_SCOPE((stmt)->type)
|
|
|
|
#define STMT_LINKS_SCOPE(stmt) (STMT_TYPE_LINKS_SCOPE((stmt)->type) || \
|
|
|
|
((stmt)->flags & SIF_SCOPE))
|
|
|
|
#define STMT_IS_TRYING(stmt) STMT_TYPE_IS_TRYING((stmt)->type)
|
|
|
|
#define STMT_IS_LOOP(stmt) STMT_TYPE_IS_LOOP((stmt)->type)
|
|
|
|
|
|
|
|
typedef struct JSStmtInfo JSStmtInfo;
|
|
|
|
|
|
|
|
struct JSStmtInfo {
|
|
|
|
uint16 type; /* statement type */
|
|
|
|
uint16 flags; /* flags, see below */
|
2009-04-05 21:17:22 -07:00
|
|
|
uint32 blockid; /* for simplified dominance computation */
|
2007-03-22 10:30:00 -07:00
|
|
|
ptrdiff_t update; /* loop update offset (top if none) */
|
|
|
|
ptrdiff_t breaks; /* offset of last break in loop */
|
|
|
|
ptrdiff_t continues; /* offset of last continue in loop */
|
2007-07-08 02:03:34 -07:00
|
|
|
union {
|
|
|
|
JSAtom *label; /* name of LABEL */
|
|
|
|
JSObject *blockObj; /* block scope object */
|
2009-04-05 21:17:22 -07:00
|
|
|
};
|
2007-03-22 10:30:00 -07:00
|
|
|
JSStmtInfo *down; /* info for enclosing statement */
|
|
|
|
JSStmtInfo *downScope; /* next enclosing lexical scope */
|
|
|
|
};
|
|
|
|
|
|
|
|
#define SIF_SCOPE 0x0001 /* statement has its own lexical scope */
|
|
|
|
#define SIF_BODY_BLOCK 0x0002 /* STMT_BLOCK type is a function body */
|
2008-01-07 01:41:28 -08:00
|
|
|
#define SIF_FOR_BLOCK 0x0004 /* for (let ...) induced block scope */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* To reuse space in JSStmtInfo, rename breaks and continues for use during
|
2008-01-04 17:34:11 -08:00
|
|
|
* try/catch/finally code generation and backpatching. To match most common
|
|
|
|
* use cases, the macro argument is a struct, not a struct pointer. Only a
|
2007-03-22 10:30:00 -07:00
|
|
|
* loop, switch, or label statement info record can have breaks and continues,
|
|
|
|
* and only a for loop has an update backpatch chain, so it's safe to overlay
|
|
|
|
* these for the "trying" JSStmtTypes.
|
|
|
|
*/
|
|
|
|
#define CATCHNOTE(stmt) ((stmt).update)
|
|
|
|
#define GOSUBS(stmt) ((stmt).breaks)
|
|
|
|
#define GUARDJUMP(stmt) ((stmt).continues)
|
|
|
|
|
|
|
|
#define SET_STATEMENT_TOP(stmt, top) \
|
|
|
|
((stmt)->update = (top), (stmt)->breaks = (stmt)->continues = (-1))
|
|
|
|
|
2009-04-05 21:17:22 -07:00
|
|
|
#ifdef JS_SCOPE_DEPTH_METER
|
|
|
|
# define JS_SCOPE_DEPTH_METERING(code) ((void) (code))
|
2009-12-14 18:36:58 -08:00
|
|
|
# define JS_SCOPE_DEPTH_METERING_IF(cond, code) ((cond) ? (void) (code) : (void) 0)
|
2009-04-05 21:17:22 -07:00
|
|
|
#else
|
|
|
|
# define JS_SCOPE_DEPTH_METERING(code) ((void) 0)
|
2009-12-14 18:36:58 -08:00
|
|
|
# define JS_SCOPE_DEPTH_METERING_IF(code, x) ((void) 0)
|
2009-04-05 21:17:22 -07:00
|
|
|
#endif
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
struct JSTreeContext { /* tree context for semantic checks */
|
2009-10-08 10:29:03 -07:00
|
|
|
uint32 flags; /* statement state flags, see below */
|
2007-07-08 02:03:34 -07:00
|
|
|
uint16 ngvars; /* max. no. of global variables/regexps */
|
2009-04-05 21:17:22 -07:00
|
|
|
uint32 bodyid; /* block number of program/function body */
|
|
|
|
uint32 blockidGen; /* preincremented block number generator */
|
2007-03-22 10:30:00 -07:00
|
|
|
JSStmtInfo *topStmt; /* top of statement info stack */
|
|
|
|
JSStmtInfo *topScopeStmt; /* top lexical scope statement */
|
|
|
|
JSObject *blockChain; /* compile time block scope chain (NB: one
|
|
|
|
deeper than the topScopeStmt/downScope
|
|
|
|
chain when in head of let block/expr) */
|
2009-04-05 21:17:22 -07:00
|
|
|
JSParseNode *blockNode; /* parse node for a block with let declarations
|
|
|
|
(block with its own lexical scope) */
|
2007-03-22 10:30:00 -07:00
|
|
|
JSAtomList decls; /* function, const, and var declarations */
|
2010-04-10 21:15:35 -07:00
|
|
|
js::Parser *parser; /* ptr to common parsing and lexing data */
|
2008-09-08 02:47:12 -07:00
|
|
|
|
|
|
|
union {
|
|
|
|
JSFunction *fun; /* function to store argument and variable
|
2007-11-29 06:49:42 -08:00
|
|
|
names when flags & TCF_IN_FUNCTION */
|
2008-09-08 02:47:12 -07:00
|
|
|
JSObject *scopeChain; /* scope chain object for the script */
|
2009-04-05 21:17:22 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
JSAtomList lexdeps; /* unresolved lexical name dependencies */
|
|
|
|
JSTreeContext *parent; /* enclosing function or global context */
|
|
|
|
uintN staticLevel; /* static compilation unit nesting level */
|
|
|
|
|
|
|
|
JSFunctionBox *funbox; /* null or box for function we're compiling
|
|
|
|
if (flags & TCF_IN_FUNCTION) and not in
|
2010-04-10 21:15:35 -07:00
|
|
|
Compiler::compileFunctionBody */
|
2009-04-05 21:17:22 -07:00
|
|
|
JSFunctionBox *functionList;
|
2008-09-08 02:47:12 -07:00
|
|
|
|
2008-07-20 13:13:17 -07:00
|
|
|
#ifdef JS_SCOPE_DEPTH_METER
|
|
|
|
uint16 scopeDepth; /* current lexical scope chain depth */
|
|
|
|
uint16 maxScopeDepth; /* maximum lexical scope chain depth */
|
|
|
|
#endif
|
2009-04-03 00:53:02 -07:00
|
|
|
|
2010-04-10 21:15:35 -07:00
|
|
|
JSTreeContext(js::Parser *prs)
|
2009-04-05 21:17:22 -07:00
|
|
|
: flags(0), ngvars(0), bodyid(0), blockidGen(0),
|
|
|
|
topStmt(NULL), topScopeStmt(NULL), blockChain(NULL), blockNode(NULL),
|
2010-04-10 21:15:35 -07:00
|
|
|
parser(prs), scopeChain(NULL), parent(prs->tc), staticLevel(0),
|
2009-09-09 20:21:15 -07:00
|
|
|
funbox(NULL), functionList(NULL), sharpSlotBase(-1)
|
2009-04-05 21:17:22 -07:00
|
|
|
{
|
2010-04-10 21:15:35 -07:00
|
|
|
prs->tc = this;
|
2009-04-05 21:17:22 -07:00
|
|
|
JS_SCOPE_DEPTH_METERING(scopeDepth = maxScopeDepth = 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* For functions the tree context is constructed and destructed a second
|
|
|
|
* time during code generation. To avoid a redundant stats update in such
|
2009-12-14 18:36:58 -08:00
|
|
|
* cases, we store uint16(-1) in maxScopeDepth.
|
2009-04-05 21:17:22 -07:00
|
|
|
*/
|
|
|
|
~JSTreeContext() {
|
2010-04-10 21:15:35 -07:00
|
|
|
parser->tc = this->parent;
|
2009-12-14 18:36:58 -08:00
|
|
|
JS_SCOPE_DEPTH_METERING_IF((maxScopeDepth != uint16(-1)),
|
2010-04-10 21:15:35 -07:00
|
|
|
JS_BASIC_STATS_ACCUM(&parser
|
2009-12-14 18:36:58 -08:00
|
|
|
->context
|
|
|
|
->runtime
|
|
|
|
->lexicalScopeDepthStats,
|
|
|
|
maxScopeDepth));
|
2009-04-05 21:17:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
uintN blockid() { return topStmt ? topStmt->blockid : bodyid; }
|
|
|
|
|
|
|
|
bool atTopLevel() { return !topStmt || (topStmt->flags & SIF_BODY_BLOCK); }
|
|
|
|
|
|
|
|
/* Test whether we're in a statement of given type. */
|
|
|
|
bool inStatement(JSStmtType type);
|
2009-09-09 20:21:15 -07:00
|
|
|
|
2009-11-19 12:35:55 -08:00
|
|
|
inline bool needStrictChecks();
|
|
|
|
|
2009-09-09 20:21:15 -07:00
|
|
|
/*
|
|
|
|
* sharpSlotBase is -1 or first slot of pair for [sharpArray, sharpDepth].
|
|
|
|
* The parser calls ensureSharpSlots to allocate these two stack locals.
|
|
|
|
*/
|
|
|
|
int sharpSlotBase;
|
|
|
|
bool ensureSharpSlots();
|
2010-01-21 13:27:36 -08:00
|
|
|
|
|
|
|
// Return true there is a generator function within |skip| lexical scopes
|
|
|
|
// (going upward) from this context's lexical scope. Always return true if
|
|
|
|
// this context is itself a generator.
|
|
|
|
bool skipSpansGenerator(unsigned skip);
|
2009-04-05 21:17:22 -07:00
|
|
|
};
|
2009-04-04 10:14:52 -07:00
|
|
|
|
2009-04-05 21:17:22 -07:00
|
|
|
#define TCF_COMPILING 0x01 /* JSTreeContext is JSCodeGenerator */
|
|
|
|
#define TCF_IN_FUNCTION 0x02 /* parsing inside function body */
|
|
|
|
#define TCF_RETURN_EXPR 0x04 /* function has 'return expr;' */
|
|
|
|
#define TCF_RETURN_VOID 0x08 /* function has 'return;' */
|
|
|
|
#define TCF_IN_FOR_INIT 0x10 /* parsing init expr of for; exclude 'in' */
|
|
|
|
#define TCF_FUN_SETS_OUTER_NAME 0x20 /* function set outer name (lexical or free) */
|
|
|
|
#define TCF_FUN_PARAM_ARGUMENTS 0x40 /* function has parameter named arguments */
|
|
|
|
#define TCF_FUN_USES_ARGUMENTS 0x80 /* function uses arguments except as a
|
|
|
|
parameter name */
|
|
|
|
#define TCF_FUN_HEAVYWEIGHT 0x100 /* function needs Call object per call */
|
|
|
|
#define TCF_FUN_IS_GENERATOR 0x200 /* parsed yield statement in function */
|
2009-09-03 14:41:19 -07:00
|
|
|
#define TCF_FUN_USES_OWN_NAME 0x400 /* named function expression that uses its
|
|
|
|
own name */
|
2009-04-05 21:17:22 -07:00
|
|
|
#define TCF_HAS_FUNCTION_STMT 0x800 /* block contains a function statement */
|
|
|
|
#define TCF_GENEXP_LAMBDA 0x1000 /* flag lambda from generator expression */
|
2010-04-10 21:15:35 -07:00
|
|
|
#define TCF_COMPILE_N_GO 0x2000 /* compile-and-go mode of script, can
|
2009-04-05 21:17:22 -07:00
|
|
|
optimize name references based on scope
|
|
|
|
chain */
|
|
|
|
#define TCF_NO_SCRIPT_RVAL 0x4000 /* API caller does not want result value
|
|
|
|
from global script */
|
|
|
|
#define TCF_HAS_SHARPS 0x8000 /* source contains sharp defs or uses */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-10-08 10:29:03 -07:00
|
|
|
/*
|
|
|
|
* Set when parsing a declaration-like destructuring pattern. This
|
|
|
|
* flag causes PrimaryExpr to create PN_NAME parse nodes for variable
|
|
|
|
* references which are not hooked into any definition's use chain,
|
|
|
|
* added to any tree context's AtomList, etc. etc. CheckDestructuring
|
|
|
|
* will do that work later.
|
|
|
|
*
|
|
|
|
* The comments atop CheckDestructuring explain the distinction
|
|
|
|
* between assignment-like and declaration-like destructuring
|
|
|
|
* patterns, and why they need to be treated differently.
|
|
|
|
*/
|
2009-10-18 17:41:24 -07:00
|
|
|
#define TCF_DECL_DESTRUCTURING 0x10000
|
|
|
|
|
|
|
|
/*
|
2010-04-10 21:15:35 -07:00
|
|
|
* A request flag passed to Compiler::compileScript and then down via
|
2009-10-18 17:41:24 -07:00
|
|
|
* JSCodeGenerator to js_NewScriptFromCG, from script_compile_sub and any
|
|
|
|
* kindred functions that need to make mutable scripts (even empty ones;
|
|
|
|
* i.e., they can't share the const JSScript::emptyScript() singleton).
|
|
|
|
*/
|
|
|
|
#define TCF_NEED_MUTABLE_SCRIPT 0x20000
|
2009-10-08 10:29:03 -07:00
|
|
|
|
2009-11-18 13:33:53 -08:00
|
|
|
/*
|
2010-01-14 09:33:14 -08:00
|
|
|
* This function/global/eval code body contained a Use Strict Directive. Treat
|
|
|
|
* certain strict warnings as errors, and forbid the use of 'with'. See also
|
|
|
|
* TSF_STRICT_MODE_CODE, JSScript::strictModeCode, and JSREPORT_STRICT_ERROR.
|
2009-11-18 13:33:53 -08:00
|
|
|
*/
|
2010-01-14 09:33:14 -08:00
|
|
|
#define TCF_STRICT_MODE_CODE 0x40000
|
2009-12-02 15:17:02 -08:00
|
|
|
|
|
|
|
/* Function has parameter named 'eval'. */
|
2010-01-14 09:33:14 -08:00
|
|
|
#define TCF_FUN_PARAM_EVAL 0x80000
|
2009-11-18 13:33:53 -08:00
|
|
|
|
|
|
|
/*
|
2010-01-14 09:33:14 -08:00
|
|
|
* Flag signifying that the current function seems to be a constructor that
|
|
|
|
* sets this.foo to define "methods", at least one of which can't be a null
|
|
|
|
* closure, so we should avoid over-specializing property cache entries and
|
|
|
|
* trace inlining guards to method function object identity, which will vary
|
|
|
|
* per instance.
|
|
|
|
*/
|
|
|
|
#define TCF_FUN_UNBRAND_THIS 0x100000
|
|
|
|
|
2010-02-10 15:17:52 -08:00
|
|
|
/*
|
|
|
|
* "Module pattern", i.e., a lambda that is immediately applied and the whole
|
|
|
|
* of an expression statement.
|
|
|
|
*/
|
|
|
|
#define TCF_FUN_MODULE_PATTERN 0x200000
|
|
|
|
|
2010-04-29 19:04:55 -07:00
|
|
|
/*
|
2010-04-30 08:50:03 -07:00
|
|
|
* Flag to prevent a non-escaping function from being optimized into a null
|
2010-04-29 19:04:55 -07:00
|
|
|
* closure (i.e., a closure that needs only its global object for free variable
|
2010-04-30 08:50:03 -07:00
|
|
|
* resolution, thanks to JSOP_{GET,CALL}UPVAR), because this function contains
|
|
|
|
* a closure that needs one or more scope objects surrounding it (i.e., Call
|
2010-04-29 19:04:55 -07:00
|
|
|
* object for a heavyweight outer function). See bug 560234.
|
|
|
|
*/
|
|
|
|
#define TCF_FUN_ENTRAINS_SCOPES 0x400000
|
|
|
|
|
2010-01-14 09:33:14 -08:00
|
|
|
/*
|
|
|
|
* Flags to check for return; vs. return expr; in a function.
|
2009-11-18 13:33:53 -08:00
|
|
|
*/
|
|
|
|
#define TCF_RETURN_FLAGS (TCF_RETURN_EXPR | TCF_RETURN_VOID)
|
|
|
|
|
2008-07-20 13:13:17 -07:00
|
|
|
/*
|
2009-04-11 01:07:42 -07:00
|
|
|
* Sticky deoptimization flags to propagate from FunctionBody.
|
2008-07-20 13:13:17 -07:00
|
|
|
*/
|
2009-04-05 21:17:22 -07:00
|
|
|
#define TCF_FUN_FLAGS (TCF_FUN_SETS_OUTER_NAME | \
|
|
|
|
TCF_FUN_USES_ARGUMENTS | \
|
|
|
|
TCF_FUN_PARAM_ARGUMENTS | \
|
|
|
|
TCF_FUN_HEAVYWEIGHT | \
|
|
|
|
TCF_FUN_IS_GENERATOR | \
|
2009-09-03 14:41:19 -07:00
|
|
|
TCF_FUN_USES_OWN_NAME | \
|
2009-11-18 13:33:53 -08:00
|
|
|
TCF_HAS_SHARPS | \
|
|
|
|
TCF_STRICT_MODE_CODE)
|
2009-04-04 10:14:52 -07:00
|
|
|
|
2009-11-19 12:35:55 -08:00
|
|
|
/*
|
|
|
|
* Return true if we need to check for conditions that elicit
|
|
|
|
* JSOPTION_STRICT warnings or strict mode errors.
|
|
|
|
*/
|
|
|
|
inline bool JSTreeContext::needStrictChecks() {
|
2010-04-10 21:15:35 -07:00
|
|
|
return JS_HAS_STRICT_OPTION(parser->context) ||
|
2009-11-19 12:35:55 -08:00
|
|
|
(flags & TCF_STRICT_MODE_CODE);
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/*
|
|
|
|
* Span-dependent instructions are jumps whose span (from the jump bytecode to
|
|
|
|
* the jump target) may require 2 or 4 bytes of immediate operand.
|
|
|
|
*/
|
|
|
|
typedef struct JSSpanDep JSSpanDep;
|
|
|
|
typedef struct JSJumpTarget JSJumpTarget;
|
|
|
|
|
|
|
|
struct JSSpanDep {
|
|
|
|
ptrdiff_t top; /* offset of first bytecode in an opcode */
|
|
|
|
ptrdiff_t offset; /* offset - 1 within opcode of jump operand */
|
|
|
|
ptrdiff_t before; /* original offset - 1 of jump operand */
|
|
|
|
JSJumpTarget *target; /* tagged target pointer or backpatch delta */
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Jump targets are stored in an AVL tree, for O(log(n)) lookup with targets
|
|
|
|
* sorted by offset from left to right, so that targets after a span-dependent
|
|
|
|
* instruction whose jump offset operand must be extended can be found quickly
|
|
|
|
* and adjusted upward (toward higher offsets).
|
|
|
|
*/
|
|
|
|
struct JSJumpTarget {
|
|
|
|
ptrdiff_t offset; /* offset of span-dependent jump target */
|
|
|
|
int balance; /* AVL tree balance number */
|
|
|
|
JSJumpTarget *kids[2]; /* left and right AVL tree child pointers */
|
|
|
|
};
|
|
|
|
|
|
|
|
#define JT_LEFT 0
|
|
|
|
#define JT_RIGHT 1
|
|
|
|
#define JT_OTHER_DIR(dir) (1 - (dir))
|
|
|
|
#define JT_IMBALANCE(dir) (((dir) << 1) - 1)
|
|
|
|
#define JT_DIR(imbalance) (((imbalance) + 1) >> 1)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Backpatch deltas are encoded in JSSpanDep.target if JT_TAG_BIT is clear,
|
|
|
|
* so we can maintain backpatch chains when using span dependency records to
|
|
|
|
* hold jump offsets that overflow 16 bits.
|
|
|
|
*/
|
|
|
|
#define JT_TAG_BIT ((jsword) 1)
|
|
|
|
#define JT_UNTAG_SHIFT 1
|
|
|
|
#define JT_SET_TAG(jt) ((JSJumpTarget *)((jsword)(jt) | JT_TAG_BIT))
|
|
|
|
#define JT_CLR_TAG(jt) ((JSJumpTarget *)((jsword)(jt) & ~JT_TAG_BIT))
|
|
|
|
#define JT_HAS_TAG(jt) ((jsword)(jt) & JT_TAG_BIT)
|
|
|
|
|
|
|
|
#define BITS_PER_PTRDIFF (sizeof(ptrdiff_t) * JS_BITS_PER_BYTE)
|
|
|
|
#define BITS_PER_BPDELTA (BITS_PER_PTRDIFF - 1 - JT_UNTAG_SHIFT)
|
|
|
|
#define BPDELTA_MAX (((ptrdiff_t)1 << BITS_PER_BPDELTA) - 1)
|
|
|
|
#define BPDELTA_TO_JT(bp) ((JSJumpTarget *)((bp) << JT_UNTAG_SHIFT))
|
|
|
|
#define JT_TO_BPDELTA(jt) ((ptrdiff_t)((jsword)(jt) >> JT_UNTAG_SHIFT))
|
|
|
|
|
|
|
|
#define SD_SET_TARGET(sd,jt) ((sd)->target = JT_SET_TAG(jt))
|
|
|
|
#define SD_GET_TARGET(sd) (JS_ASSERT(JT_HAS_TAG((sd)->target)), \
|
|
|
|
JT_CLR_TAG((sd)->target))
|
|
|
|
#define SD_SET_BPDELTA(sd,bp) ((sd)->target = BPDELTA_TO_JT(bp))
|
|
|
|
#define SD_GET_BPDELTA(sd) (JS_ASSERT(!JT_HAS_TAG((sd)->target)), \
|
|
|
|
JT_TO_BPDELTA((sd)->target))
|
|
|
|
|
|
|
|
/* Avoid asserting twice by expanding SD_GET_TARGET in the "then" clause. */
|
|
|
|
#define SD_SPAN(sd,pivot) (SD_GET_TARGET(sd) \
|
|
|
|
? JT_CLR_TAG((sd)->target)->offset - (pivot) \
|
|
|
|
: 0)
|
|
|
|
|
2007-07-02 05:13:23 -07:00
|
|
|
typedef struct JSTryNode JSTryNode;
|
|
|
|
|
|
|
|
struct JSTryNode {
|
|
|
|
JSTryNote note;
|
|
|
|
JSTryNode *prev;
|
|
|
|
};
|
|
|
|
|
2009-04-05 21:17:22 -07:00
|
|
|
struct JSCGObjectList {
|
2007-07-08 02:03:34 -07:00
|
|
|
uint32 length; /* number of emitted so far objects */
|
2009-04-05 21:17:22 -07:00
|
|
|
JSObjectBox *lastbox; /* last emitted object */
|
2007-07-08 02:03:34 -07:00
|
|
|
|
2009-04-05 21:17:22 -07:00
|
|
|
JSCGObjectList() : length(0), lastbox(NULL) {}
|
2007-07-08 02:03:34 -07:00
|
|
|
|
2009-04-05 21:17:22 -07:00
|
|
|
uintN index(JSObjectBox *objbox);
|
|
|
|
void finish(JSObjectArray *array);
|
|
|
|
};
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-04-05 21:17:22 -07:00
|
|
|
struct JSCodeGenerator : public JSTreeContext
|
|
|
|
{
|
2007-03-22 10:30:00 -07:00
|
|
|
JSArenaPool *codePool; /* pointer to thread code arena pool */
|
|
|
|
JSArenaPool *notePool; /* pointer to thread srcnote arena pool */
|
|
|
|
void *codeMark; /* low watermark in cg->codePool */
|
|
|
|
void *noteMark; /* low watermark in cg->notePool */
|
|
|
|
|
|
|
|
struct {
|
|
|
|
jsbytecode *base; /* base of JS bytecode vector */
|
|
|
|
jsbytecode *limit; /* one byte beyond end of bytecode */
|
|
|
|
jsbytecode *next; /* pointer to next free bytecode */
|
|
|
|
jssrcnote *notes; /* source notes, see below */
|
|
|
|
uintN noteCount; /* number of source notes so far */
|
|
|
|
uintN noteMask; /* growth increment for notes */
|
|
|
|
ptrdiff_t lastNoteOffset; /* code offset for last source note */
|
|
|
|
uintN currentLine; /* line number for tree-based srcnote gen */
|
|
|
|
} prolog, main, *current;
|
|
|
|
|
|
|
|
JSAtomList atomList; /* literals indexed for mapping */
|
2008-05-28 19:07:32 -07:00
|
|
|
uintN firstLine; /* first line, for js_NewScriptFromCG */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
intN stackDepth; /* current stack depth in script frame */
|
|
|
|
uintN maxStackDepth; /* maximum stack depth so far */
|
|
|
|
|
2007-07-02 05:13:23 -07:00
|
|
|
uintN ntrynotes; /* number of allocated so far try notes */
|
|
|
|
JSTryNode *lastTryNode; /* the last allocated try node */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
JSSpanDep *spanDeps; /* span dependent instruction records */
|
|
|
|
JSJumpTarget *jumpTargets; /* AVL tree of jump target offsets */
|
|
|
|
JSJumpTarget *jtFreeList; /* JT_LEFT-linked list of free structs */
|
|
|
|
uintN numSpanDeps; /* number of span dependencies */
|
|
|
|
uintN numJumpTargets; /* number of jump targets */
|
|
|
|
ptrdiff_t spanDepTodo; /* offset from main.base of potentially
|
|
|
|
unoptimized spandeps */
|
|
|
|
|
2008-07-20 13:13:17 -07:00
|
|
|
uintN arrayCompDepth; /* stack depth of array in comprehension */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
uintN emitLevel; /* js_EmitTree recursion level */
|
|
|
|
JSAtomList constList; /* compile time constants */
|
2007-07-08 02:03:34 -07:00
|
|
|
|
2009-04-05 21:17:22 -07:00
|
|
|
JSCGObjectList objectList; /* list of emitted objects */
|
|
|
|
JSCGObjectList regexpList; /* list of emitted regexp that will be
|
|
|
|
cloned during execution */
|
2007-07-08 02:03:34 -07:00
|
|
|
|
2008-08-21 03:47:33 -07:00
|
|
|
JSAtomList upvarList; /* map of atoms to upvar indexes */
|
|
|
|
JSUpvarArray upvarMap; /* indexed upvar pairs (JS_realloc'ed) */
|
2009-04-05 21:17:22 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize cg to allocate bytecode space from codePool, source note
|
|
|
|
* space from notePool, and all other arena-allocated temporaries from
|
2010-04-10 21:15:35 -07:00
|
|
|
* parser->context->tempPool.
|
2009-04-05 21:17:22 -07:00
|
|
|
*/
|
2010-04-10 21:15:35 -07:00
|
|
|
JSCodeGenerator(js::Parser *parser,
|
2009-04-05 21:17:22 -07:00
|
|
|
JSArenaPool *codePool, JSArenaPool *notePool,
|
|
|
|
uintN lineno);
|
|
|
|
|
|
|
|
/*
|
2010-04-10 21:15:35 -07:00
|
|
|
* Release cg->codePool, cg->notePool, and parser->context->tempPool to
|
2009-04-05 21:17:22 -07:00
|
|
|
* marks set by JSCodeGenerator's ctor. Note that cgs are magic: they own
|
|
|
|
* the arena pool "tops-of-stack" space above their codeMark, noteMark, and
|
|
|
|
* tempMark points. This means you cannot alloc from tempPool and save the
|
|
|
|
* pointer beyond the next JSCodeGenerator destructor call.
|
|
|
|
*/
|
|
|
|
~JSCodeGenerator();
|
2009-09-09 20:21:15 -07:00
|
|
|
|
|
|
|
bool hasSharps() {
|
2009-09-23 11:16:30 -07:00
|
|
|
bool rv = !!(flags & TCF_HAS_SHARPS);
|
2009-09-09 20:21:15 -07:00
|
|
|
JS_ASSERT((sharpSlotBase >= 0) == rv);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
uintN sharpSlots() {
|
2009-09-14 07:29:56 -07:00
|
|
|
return hasSharps() ? SHARP_NSLOTS : 0;
|
2009-09-09 20:21:15 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
2010-04-10 21:15:35 -07:00
|
|
|
#define CG_TS(cg) TS((cg)->parser)
|
2008-08-21 03:47:33 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
#define CG_BASE(cg) ((cg)->current->base)
|
|
|
|
#define CG_LIMIT(cg) ((cg)->current->limit)
|
|
|
|
#define CG_NEXT(cg) ((cg)->current->next)
|
|
|
|
#define CG_CODE(cg,offset) (CG_BASE(cg) + (offset))
|
2009-02-18 09:14:51 -08:00
|
|
|
#define CG_OFFSET(cg) (CG_NEXT(cg) - CG_BASE(cg))
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#define CG_NOTES(cg) ((cg)->current->notes)
|
|
|
|
#define CG_NOTE_COUNT(cg) ((cg)->current->noteCount)
|
|
|
|
#define CG_NOTE_MASK(cg) ((cg)->current->noteMask)
|
|
|
|
#define CG_LAST_NOTE_OFFSET(cg) ((cg)->current->lastNoteOffset)
|
|
|
|
#define CG_CURRENT_LINE(cg) ((cg)->current->currentLine)
|
|
|
|
|
|
|
|
#define CG_PROLOG_BASE(cg) ((cg)->prolog.base)
|
|
|
|
#define CG_PROLOG_LIMIT(cg) ((cg)->prolog.limit)
|
|
|
|
#define CG_PROLOG_NEXT(cg) ((cg)->prolog.next)
|
|
|
|
#define CG_PROLOG_CODE(cg,poff) (CG_PROLOG_BASE(cg) + (poff))
|
2009-02-18 09:14:51 -08:00
|
|
|
#define CG_PROLOG_OFFSET(cg) (CG_PROLOG_NEXT(cg) - CG_PROLOG_BASE(cg))
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#define CG_SWITCH_TO_MAIN(cg) ((cg)->current = &(cg)->main)
|
|
|
|
#define CG_SWITCH_TO_PROLOG(cg) ((cg)->current = &(cg)->prolog)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Emit one bytecode.
|
|
|
|
*/
|
|
|
|
extern ptrdiff_t
|
|
|
|
js_Emit1(JSContext *cx, JSCodeGenerator *cg, JSOp op);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Emit two bytecodes, an opcode (op) with a byte of immediate operand (op1).
|
|
|
|
*/
|
|
|
|
extern ptrdiff_t
|
|
|
|
js_Emit2(JSContext *cx, JSCodeGenerator *cg, JSOp op, jsbytecode op1);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Emit three bytecodes, an opcode with two bytes of immediate operands.
|
|
|
|
*/
|
|
|
|
extern ptrdiff_t
|
|
|
|
js_Emit3(JSContext *cx, JSCodeGenerator *cg, JSOp op, jsbytecode op1,
|
|
|
|
jsbytecode op2);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Emit (1 + extra) bytecodes, for N bytes of op and its immediate operand.
|
|
|
|
*/
|
|
|
|
extern ptrdiff_t
|
|
|
|
js_EmitN(JSContext *cx, JSCodeGenerator *cg, JSOp op, size_t extra);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Unsafe macro to call js_SetJumpOffset and return false if it does.
|
|
|
|
*/
|
2008-09-02 20:50:19 -07:00
|
|
|
#define CHECK_AND_SET_JUMP_OFFSET_CUSTOM(cx,cg,pc,off,BAD_EXIT) \
|
2007-03-22 10:30:00 -07:00
|
|
|
JS_BEGIN_MACRO \
|
2008-09-02 20:50:19 -07:00
|
|
|
if (!js_SetJumpOffset(cx, cg, pc, off)) { \
|
|
|
|
BAD_EXIT; \
|
|
|
|
} \
|
2007-03-22 10:30:00 -07:00
|
|
|
JS_END_MACRO
|
|
|
|
|
2008-09-02 20:50:19 -07:00
|
|
|
#define CHECK_AND_SET_JUMP_OFFSET(cx,cg,pc,off) \
|
|
|
|
CHECK_AND_SET_JUMP_OFFSET_CUSTOM(cx,cg,pc,off,return JS_FALSE)
|
|
|
|
|
|
|
|
#define CHECK_AND_SET_JUMP_OFFSET_AT_CUSTOM(cx,cg,off,BAD_EXIT) \
|
|
|
|
CHECK_AND_SET_JUMP_OFFSET_CUSTOM(cx, cg, CG_CODE(cg,off), \
|
|
|
|
CG_OFFSET(cg) - (off), BAD_EXIT)
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
#define CHECK_AND_SET_JUMP_OFFSET_AT(cx,cg,off) \
|
2008-09-02 20:50:19 -07:00
|
|
|
CHECK_AND_SET_JUMP_OFFSET_AT_CUSTOM(cx, cg, off, return JS_FALSE)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
extern JSBool
|
|
|
|
js_SetJumpOffset(JSContext *cx, JSCodeGenerator *cg, jsbytecode *pc,
|
|
|
|
ptrdiff_t off);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Push the C-stack-allocated struct at stmt onto the stmtInfo stack.
|
|
|
|
*/
|
|
|
|
extern void
|
|
|
|
js_PushStatement(JSTreeContext *tc, JSStmtInfo *stmt, JSStmtType type,
|
|
|
|
ptrdiff_t top);
|
|
|
|
|
|
|
|
/*
|
2008-01-04 17:34:11 -08:00
|
|
|
* Push a block scope statement and link blockObj into tc->blockChain. To pop
|
2007-07-08 02:03:34 -07:00
|
|
|
* this statement info record, use js_PopStatement as usual, or if appropriate
|
|
|
|
* (if generating code), js_PopStatementCG.
|
2007-03-22 10:30:00 -07:00
|
|
|
*/
|
|
|
|
extern void
|
2007-07-08 02:03:34 -07:00
|
|
|
js_PushBlockScope(JSTreeContext *tc, JSStmtInfo *stmt, JSObject *blockObj,
|
2007-03-22 10:30:00 -07:00
|
|
|
ptrdiff_t top);
|
|
|
|
|
|
|
|
/*
|
2008-01-04 17:34:11 -08:00
|
|
|
* Pop tc->topStmt. If the top JSStmtInfo struct is not stack-allocated, it
|
2007-03-22 10:30:00 -07:00
|
|
|
* is up to the caller to free it.
|
|
|
|
*/
|
|
|
|
extern void
|
|
|
|
js_PopStatement(JSTreeContext *tc);
|
|
|
|
|
|
|
|
/*
|
2009-04-05 21:17:22 -07:00
|
|
|
* Like js_PopStatement(cg), also patch breaks and continues unless the top
|
|
|
|
* statement info record represents a try-catch-finally suite. May fail if a
|
|
|
|
* jump offset overflows.
|
2007-03-22 10:30:00 -07:00
|
|
|
*/
|
|
|
|
extern JSBool
|
|
|
|
js_PopStatementCG(JSContext *cx, JSCodeGenerator *cg);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Define and lookup a primitive jsval associated with the const named by atom.
|
|
|
|
* js_DefineCompileTimeConstant analyzes the constant-folded initializer at pn
|
|
|
|
* and saves the const's value in cg->constList, if it can be used at compile
|
2008-01-04 17:34:11 -08:00
|
|
|
* time. It returns true unless an error occurred.
|
2007-03-22 10:30:00 -07:00
|
|
|
*
|
2007-12-07 13:36:58 -08:00
|
|
|
* If the initializer's value could not be saved, js_DefineCompileTimeConstant
|
2008-01-04 17:34:11 -08:00
|
|
|
* calls will return the undefined value. js_DefineCompileTimeConstant tries
|
2007-03-22 10:30:00 -07:00
|
|
|
* to find a const value memorized for atom, returning true with *vp set to a
|
|
|
|
* value other than undefined if the constant was found, true with *vp set to
|
|
|
|
* JSVAL_VOID if not found, and false on error.
|
|
|
|
*/
|
|
|
|
extern JSBool
|
|
|
|
js_DefineCompileTimeConstant(JSContext *cx, JSCodeGenerator *cg, JSAtom *atom,
|
|
|
|
JSParseNode *pn);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find a lexically scoped variable (one declared by let, catch, or an array
|
|
|
|
* comprehension) named by atom, looking in tc's compile-time scopes.
|
|
|
|
*
|
|
|
|
* If a WITH statement is reached along the scope stack, return its statement
|
2008-01-04 17:34:11 -08:00
|
|
|
* info record, so callers can tell that atom is ambiguous. If slotp is not
|
2007-03-22 10:30:00 -07:00
|
|
|
* null, then if atom is found, set *slotp to its stack slot, otherwise to -1.
|
|
|
|
* This means that if slotp is not null, all the block objects on the lexical
|
|
|
|
* scope chain must have had their depth slots computed by the code generator,
|
|
|
|
* so the caller must be under js_EmitTree.
|
|
|
|
*
|
|
|
|
* In any event, directly return the statement info record in which atom was
|
2008-01-04 17:34:11 -08:00
|
|
|
* found. Otherwise return null.
|
2007-03-22 10:30:00 -07:00
|
|
|
*/
|
|
|
|
extern JSStmtInfo *
|
2009-04-08 18:42:20 -07:00
|
|
|
js_LexicalLookup(JSTreeContext *tc, JSAtom *atom, jsint *slotp,
|
|
|
|
JSStmtInfo *stmt = NULL);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Emit code into cg for the tree rooted at pn.
|
|
|
|
*/
|
|
|
|
extern JSBool
|
|
|
|
js_EmitTree(JSContext *cx, JSCodeGenerator *cg, JSParseNode *pn);
|
|
|
|
|
|
|
|
/*
|
2008-01-03 10:13:42 -08:00
|
|
|
* Emit function code using cg for the tree rooted at body.
|
2007-03-22 10:30:00 -07:00
|
|
|
*/
|
|
|
|
extern JSBool
|
2008-01-03 10:13:42 -08:00
|
|
|
js_EmitFunctionScript(JSContext *cx, JSCodeGenerator *cg, JSParseNode *body);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Source notes generated along with bytecode for decompiling and debugging.
|
|
|
|
* A source note is a uint8 with 5 bits of type and 3 of offset from the pc of
|
2008-01-04 17:34:11 -08:00
|
|
|
* the previous note. If 3 bits of offset aren't enough, extended delta notes
|
2007-03-22 10:30:00 -07:00
|
|
|
* (SRC_XDELTA) consisting of 2 set high order bits followed by 6 offset bits
|
2008-01-04 17:34:11 -08:00
|
|
|
* are emitted before the next note. Some notes have operand offsets encoded
|
2007-03-22 10:30:00 -07:00
|
|
|
* immediately after them, in note bytes or byte-triples.
|
|
|
|
*
|
|
|
|
* Source Note Extended Delta
|
|
|
|
* +7-6-5-4-3+2-1-0+ +7-6-5+4-3-2-1-0+
|
|
|
|
* |note-type|delta| |1 1| ext-delta |
|
|
|
|
* +---------+-----+ +---+-----------+
|
|
|
|
*
|
|
|
|
* At most one "gettable" note (i.e., a note of type other than SRC_NEWLINE,
|
|
|
|
* SRC_SETLINE, and SRC_XDELTA) applies to a given bytecode.
|
|
|
|
*
|
|
|
|
* NB: the js_SrcNoteSpec array in jsemit.c is indexed by this enum, so its
|
|
|
|
* initializers need to match the order here.
|
|
|
|
*
|
|
|
|
* Note on adding new source notes: every pair of bytecodes (A, B) where A and
|
|
|
|
* B have disjoint sets of source notes that could apply to each bytecode may
|
|
|
|
* reuse the same note type value for two notes (snA, snB) that have the same
|
2008-01-04 17:34:11 -08:00
|
|
|
* arity, offsetBias, and isSpanDep initializers in js_SrcNoteSpec. This is
|
|
|
|
* why SRC_IF and SRC_INITPROP have the same value below. For bad historical
|
2007-03-22 10:30:00 -07:00
|
|
|
* reasons, some bytecodes below that could be overlayed have not been, but
|
|
|
|
* before using SRC_EXTENDED, consider compressing the existing note types.
|
|
|
|
*
|
|
|
|
* Don't forget to update JSXDR_BYTECODE_VERSION in jsxdrapi.h for all such
|
|
|
|
* incompatible source note or other bytecode changes.
|
|
|
|
*/
|
|
|
|
typedef enum JSSrcNoteType {
|
|
|
|
SRC_NULL = 0, /* terminates a note vector */
|
|
|
|
SRC_IF = 1, /* JSOP_IFEQ bytecode is from an if-then */
|
2008-08-25 14:34:26 -07:00
|
|
|
SRC_BREAK = 1, /* JSOP_GOTO is a break */
|
2007-03-22 10:30:00 -07:00
|
|
|
SRC_INITPROP = 1, /* disjoint meaning applied to JSOP_INITELEM or
|
|
|
|
to an index label in a regular (structuring)
|
|
|
|
or a destructuring object initialiser */
|
2009-04-05 21:17:22 -07:00
|
|
|
SRC_GENEXP = 1, /* JSOP_LAMBDA from generator expression */
|
2007-03-22 10:30:00 -07:00
|
|
|
SRC_IF_ELSE = 2, /* JSOP_IFEQ bytecode is from an if-then-else */
|
2008-10-22 12:47:51 -07:00
|
|
|
SRC_FOR_IN = 2, /* JSOP_GOTO to for-in loop condition from
|
|
|
|
before loop (same arity as SRC_IF_ELSE) */
|
|
|
|
SRC_FOR = 3, /* JSOP_NOP or JSOP_POP in for(;;) loop head */
|
|
|
|
SRC_WHILE = 4, /* JSOP_GOTO to for or while loop condition
|
|
|
|
from before loop, else JSOP_NOP at top of
|
|
|
|
do-while loop */
|
2007-03-22 10:30:00 -07:00
|
|
|
SRC_CONTINUE = 5, /* JSOP_GOTO is a continue, not a break;
|
|
|
|
also used on JSOP_ENDINIT if extra comma
|
2009-04-02 07:31:54 -07:00
|
|
|
at end of array literal: [1,2,,];
|
|
|
|
JSOP_DUP continuing destructuring pattern */
|
2007-03-22 10:30:00 -07:00
|
|
|
SRC_DECL = 6, /* type of a declaration (var, const, let*) */
|
|
|
|
SRC_DESTRUCT = 6, /* JSOP_DUP starting a destructuring assignment
|
|
|
|
operation, with SRC_DECL_* offset operand */
|
|
|
|
SRC_PCDELTA = 7, /* distance forward from comma-operator to
|
|
|
|
next POP, or from CONDSWITCH to first CASE
|
|
|
|
opcode, etc. -- always a forward delta */
|
|
|
|
SRC_GROUPASSIGN = 7, /* SRC_DESTRUCT variant for [a, b] = [c, d] */
|
|
|
|
SRC_ASSIGNOP = 8, /* += or another assign-op follows */
|
|
|
|
SRC_COND = 9, /* JSOP_IFEQ is from conditional ?: operator */
|
|
|
|
SRC_BRACE = 10, /* mandatory brace, for scope or to avoid
|
|
|
|
dangling else */
|
|
|
|
SRC_HIDDEN = 11, /* opcode shouldn't be decompiled */
|
|
|
|
SRC_PCBASE = 12, /* distance back from annotated getprop or
|
|
|
|
setprop op to left-most obj.prop.subprop
|
|
|
|
bytecode -- always a backward delta */
|
|
|
|
SRC_LABEL = 13, /* JSOP_NOP for label: with atomid immediate */
|
|
|
|
SRC_LABELBRACE = 14, /* JSOP_NOP for label: {...} begin brace */
|
|
|
|
SRC_ENDBRACE = 15, /* JSOP_NOP for label: {...} end brace */
|
|
|
|
SRC_BREAK2LABEL = 16, /* JSOP_GOTO for 'break label' with atomid */
|
|
|
|
SRC_CONT2LABEL = 17, /* JSOP_GOTO for 'continue label' with atomid */
|
|
|
|
SRC_SWITCH = 18, /* JSOP_*SWITCH with offset to end of switch,
|
|
|
|
2nd off to first JSOP_CASE if condswitch */
|
|
|
|
SRC_FUNCDEF = 19, /* JSOP_NOP for function f() with atomid */
|
|
|
|
SRC_CATCH = 20, /* catch block has guard */
|
|
|
|
SRC_EXTENDED = 21, /* extended source note, 32-159, in next byte */
|
|
|
|
SRC_NEWLINE = 22, /* bytecode follows a source newline */
|
|
|
|
SRC_SETLINE = 23, /* a file-absolute source line number note */
|
|
|
|
SRC_XDELTA = 24 /* 24-31 are for extended delta notes */
|
|
|
|
} JSSrcNoteType;
|
|
|
|
|
|
|
|
/*
|
2008-01-04 17:34:11 -08:00
|
|
|
* Constants for the SRC_DECL source note. Note that span-dependent bytecode
|
2007-03-22 10:30:00 -07:00
|
|
|
* selection means that any SRC_DECL offset greater than SRC_DECL_LET may need
|
|
|
|
* to be adjusted, but these "offsets" are too small to span a span-dependent
|
|
|
|
* instruction, so can be used to denote distinct declaration syntaxes to the
|
|
|
|
* decompiler.
|
|
|
|
*
|
|
|
|
* NB: the var_prefix array in jsopcode.c depends on these dense indexes from
|
|
|
|
* SRC_DECL_VAR through SRC_DECL_LET.
|
|
|
|
*/
|
|
|
|
#define SRC_DECL_VAR 0
|
|
|
|
#define SRC_DECL_CONST 1
|
|
|
|
#define SRC_DECL_LET 2
|
|
|
|
#define SRC_DECL_NONE 3
|
|
|
|
|
|
|
|
#define SN_TYPE_BITS 5
|
|
|
|
#define SN_DELTA_BITS 3
|
|
|
|
#define SN_XDELTA_BITS 6
|
|
|
|
#define SN_TYPE_MASK (JS_BITMASK(SN_TYPE_BITS) << SN_DELTA_BITS)
|
|
|
|
#define SN_DELTA_MASK ((ptrdiff_t)JS_BITMASK(SN_DELTA_BITS))
|
|
|
|
#define SN_XDELTA_MASK ((ptrdiff_t)JS_BITMASK(SN_XDELTA_BITS))
|
|
|
|
|
|
|
|
#define SN_MAKE_NOTE(sn,t,d) (*(sn) = (jssrcnote) \
|
|
|
|
(((t) << SN_DELTA_BITS) \
|
|
|
|
| ((d) & SN_DELTA_MASK)))
|
|
|
|
#define SN_MAKE_XDELTA(sn,d) (*(sn) = (jssrcnote) \
|
|
|
|
((SRC_XDELTA << SN_DELTA_BITS) \
|
|
|
|
| ((d) & SN_XDELTA_MASK)))
|
|
|
|
|
|
|
|
#define SN_IS_XDELTA(sn) ((*(sn) >> SN_DELTA_BITS) >= SRC_XDELTA)
|
2007-07-05 13:37:47 -07:00
|
|
|
#define SN_TYPE(sn) ((JSSrcNoteType)(SN_IS_XDELTA(sn) \
|
|
|
|
? SRC_XDELTA \
|
|
|
|
: *(sn) >> SN_DELTA_BITS))
|
2007-03-22 10:30:00 -07:00
|
|
|
#define SN_SET_TYPE(sn,type) SN_MAKE_NOTE(sn, type, SN_DELTA(sn))
|
|
|
|
#define SN_IS_GETTABLE(sn) (SN_TYPE(sn) < SRC_NEWLINE)
|
|
|
|
|
|
|
|
#define SN_DELTA(sn) ((ptrdiff_t)(SN_IS_XDELTA(sn) \
|
|
|
|
? *(sn) & SN_XDELTA_MASK \
|
|
|
|
: *(sn) & SN_DELTA_MASK))
|
|
|
|
#define SN_SET_DELTA(sn,delta) (SN_IS_XDELTA(sn) \
|
|
|
|
? SN_MAKE_XDELTA(sn, delta) \
|
|
|
|
: SN_MAKE_NOTE(sn, SN_TYPE(sn), delta))
|
|
|
|
|
|
|
|
#define SN_DELTA_LIMIT ((ptrdiff_t)JS_BIT(SN_DELTA_BITS))
|
|
|
|
#define SN_XDELTA_LIMIT ((ptrdiff_t)JS_BIT(SN_XDELTA_BITS))
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Offset fields follow certain notes and are frequency-encoded: an offset in
|
|
|
|
* [0,0x7f] consumes one byte, an offset in [0x80,0x7fffff] takes three, and
|
|
|
|
* the high bit of the first byte is set.
|
|
|
|
*/
|
|
|
|
#define SN_3BYTE_OFFSET_FLAG 0x80
|
|
|
|
#define SN_3BYTE_OFFSET_MASK 0x7f
|
|
|
|
|
|
|
|
typedef struct JSSrcNoteSpec {
|
|
|
|
const char *name; /* name for disassembly/debugging output */
|
2008-05-28 19:07:32 -07:00
|
|
|
int8 arity; /* number of offset operands */
|
2007-03-22 10:30:00 -07:00
|
|
|
uint8 offsetBias; /* bias of offset(s) from annotated pc */
|
|
|
|
int8 isSpanDep; /* 1 or -1 if offsets could span extended ops,
|
|
|
|
0 otherwise; sign tells span direction */
|
|
|
|
} JSSrcNoteSpec;
|
|
|
|
|
|
|
|
extern JS_FRIEND_DATA(JSSrcNoteSpec) js_SrcNoteSpec[];
|
|
|
|
extern JS_FRIEND_API(uintN) js_SrcNoteLength(jssrcnote *sn);
|
|
|
|
|
|
|
|
#define SN_LENGTH(sn) ((js_SrcNoteSpec[SN_TYPE(sn)].arity == 0) ? 1 \
|
|
|
|
: js_SrcNoteLength(sn))
|
|
|
|
#define SN_NEXT(sn) ((sn) + SN_LENGTH(sn))
|
|
|
|
|
|
|
|
/* A source note array is terminated by an all-zero element. */
|
|
|
|
#define SN_MAKE_TERMINATOR(sn) (*(sn) = SRC_NULL)
|
|
|
|
#define SN_IS_TERMINATOR(sn) (*(sn) == SRC_NULL)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Append a new source note of the given type (and therefore size) to cg's
|
2008-01-04 17:34:11 -08:00
|
|
|
* notes dynamic array, updating cg->noteCount. Return the new note's index
|
|
|
|
* within the array pointed at by cg->current->notes. Return -1 if out of
|
2007-03-22 10:30:00 -07:00
|
|
|
* memory.
|
|
|
|
*/
|
|
|
|
extern intN
|
|
|
|
js_NewSrcNote(JSContext *cx, JSCodeGenerator *cg, JSSrcNoteType type);
|
|
|
|
|
|
|
|
extern intN
|
|
|
|
js_NewSrcNote2(JSContext *cx, JSCodeGenerator *cg, JSSrcNoteType type,
|
|
|
|
ptrdiff_t offset);
|
|
|
|
|
|
|
|
extern intN
|
|
|
|
js_NewSrcNote3(JSContext *cx, JSCodeGenerator *cg, JSSrcNoteType type,
|
|
|
|
ptrdiff_t offset1, ptrdiff_t offset2);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* NB: this function can add at most one extra extended delta note.
|
|
|
|
*/
|
|
|
|
extern jssrcnote *
|
|
|
|
js_AddToSrcNoteDelta(JSContext *cx, JSCodeGenerator *cg, jssrcnote *sn,
|
|
|
|
ptrdiff_t delta);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get and set the offset operand identified by which (0 for the first, etc.).
|
|
|
|
*/
|
|
|
|
extern JS_FRIEND_API(ptrdiff_t)
|
|
|
|
js_GetSrcNoteOffset(jssrcnote *sn, uintN which);
|
|
|
|
|
|
|
|
extern JSBool
|
|
|
|
js_SetSrcNoteOffset(JSContext *cx, JSCodeGenerator *cg, uintN index,
|
|
|
|
uintN which, ptrdiff_t offset);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Finish taking source notes in cx's notePool, copying final notes to the new
|
2008-01-04 17:34:11 -08:00
|
|
|
* stable store allocated by the caller and passed in via notes. Return false
|
2007-03-22 10:30:00 -07:00
|
|
|
* on malloc failure, which means this function reported an error.
|
|
|
|
*
|
|
|
|
* To compute the number of jssrcnotes to allocate and pass in via notes, use
|
2008-01-04 17:34:11 -08:00
|
|
|
* the CG_COUNT_FINAL_SRCNOTES macro. This macro knows a lot about details of
|
2007-03-22 10:30:00 -07:00
|
|
|
* js_FinishTakingSrcNotes, SO DON'T CHANGE jsemit.c's js_FinishTakingSrcNotes
|
|
|
|
* FUNCTION WITHOUT CHECKING WHETHER THIS MACRO NEEDS CORRESPONDING CHANGES!
|
|
|
|
*/
|
|
|
|
#define CG_COUNT_FINAL_SRCNOTES(cg, cnt) \
|
|
|
|
JS_BEGIN_MACRO \
|
|
|
|
ptrdiff_t diff_ = CG_PROLOG_OFFSET(cg) - (cg)->prolog.lastNoteOffset; \
|
|
|
|
cnt = (cg)->prolog.noteCount + (cg)->main.noteCount + 1; \
|
|
|
|
if ((cg)->prolog.noteCount && \
|
|
|
|
(cg)->prolog.currentLine != (cg)->firstLine) { \
|
|
|
|
if (diff_ > SN_DELTA_MASK) \
|
|
|
|
cnt += JS_HOWMANY(diff_ - SN_DELTA_MASK, SN_XDELTA_MASK); \
|
|
|
|
cnt += 2 + (((cg)->firstLine > SN_3BYTE_OFFSET_MASK) << 1); \
|
|
|
|
} else if (diff_ > 0) { \
|
|
|
|
if (cg->main.noteCount) { \
|
|
|
|
jssrcnote *sn_ = (cg)->main.notes; \
|
|
|
|
diff_ -= SN_IS_XDELTA(sn_) \
|
|
|
|
? SN_XDELTA_MASK - (*sn_ & SN_XDELTA_MASK) \
|
|
|
|
: SN_DELTA_MASK - (*sn_ & SN_DELTA_MASK); \
|
|
|
|
} \
|
|
|
|
if (diff_ > 0) \
|
|
|
|
cnt += JS_HOWMANY(diff_, SN_XDELTA_MASK); \
|
|
|
|
} \
|
|
|
|
JS_END_MACRO
|
|
|
|
|
|
|
|
extern JSBool
|
|
|
|
js_FinishTakingSrcNotes(JSContext *cx, JSCodeGenerator *cg, jssrcnote *notes);
|
|
|
|
|
|
|
|
extern void
|
2007-07-08 02:03:34 -07:00
|
|
|
js_FinishTakingTryNotes(JSCodeGenerator *cg, JSTryNoteArray *array);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
JS_END_EXTERN_C
|
|
|
|
|
|
|
|
#endif /* jsemit_h___ */
|