/[jscoverage]/trunk/js/jsemit.h
ViewVC logotype

Annotation of /trunk/js/jsemit.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 507 - (hide annotations)
Sun Jan 10 07:23:34 2010 UTC (12 years, 5 months ago) by siliconforks
File MIME type: text/plain
File size: 37681 byte(s)
Update SpiderMonkey from Firefox 3.6rc1.

1 siliconforks 507 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 siliconforks 332 * vim: set ts=8 sw=4 et tw=79:
3     *
4     * ***** BEGIN LICENSE BLOCK *****
5     * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6     *
7     * The contents of this file are subject to the Mozilla Public License Version
8     * 1.1 (the "License"); you may not use this file except in compliance with
9     * the License. You may obtain a copy of the License at
10     * http://www.mozilla.org/MPL/
11     *
12     * Software distributed under the License is distributed on an "AS IS" basis,
13     * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14     * for the specific language governing rights and limitations under the
15     * License.
16     *
17     * The Original Code is Mozilla Communicator client code, released
18     * March 31, 1998.
19     *
20     * The Initial Developer of the Original Code is
21     * Netscape Communications Corporation.
22     * Portions created by the Initial Developer are Copyright (C) 1998
23     * the Initial Developer. All Rights Reserved.
24     *
25     * Contributor(s):
26     *
27     * Alternatively, the contents of this file may be used under the terms of
28     * either of the GNU General Public License Version 2 or later (the "GPL"),
29     * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30     * in which case the provisions of the GPL or the LGPL are applicable instead
31     * of those above. If you wish to allow use of your version of this file only
32     * under the terms of either the GPL or the LGPL, and not to allow others to
33     * use your version of this file under the terms of the MPL, indicate your
34     * decision by deleting the provisions above and replace them with the notice
35     * and other provisions required by the GPL or the LGPL. If you do not delete
36     * the provisions above, a recipient may use your version of this file under
37     * the terms of any one of the MPL, the GPL or the LGPL.
38     *
39     * ***** END LICENSE BLOCK ***** */
40    
41     #ifndef jsemit_h___
42     #define jsemit_h___
43     /*
44     * JS bytecode generation.
45     */
46     #include "jstypes.h"
47     #include "jsatom.h"
48     #include "jsopcode.h"
49 siliconforks 460 #include "jsparse.h"
50 siliconforks 332 #include "jsscript.h"
51     #include "jsprvtd.h"
52     #include "jspubtd.h"
53    
54     JS_BEGIN_EXTERN_C
55    
56     /*
57     * NB: If you add enumerators for scope statements, add them between STMT_WITH
58     * and STMT_CATCH, or you will break the STMT_TYPE_IS_SCOPE macro. If you add
59     * non-looping statement enumerators, add them before STMT_DO_LOOP or you will
60     * break the STMT_TYPE_IS_LOOP macro.
61     *
62     * Also remember to keep the statementName array in jsemit.c in sync.
63     */
64     typedef enum JSStmtType {
65     STMT_LABEL, /* labeled statement: L: s */
66     STMT_IF, /* if (then) statement */
67     STMT_ELSE, /* else clause of if statement */
68 siliconforks 399 STMT_SEQ, /* synthetic sequence of statements */
69 siliconforks 332 STMT_BLOCK, /* compound statement: { s1[;... sN] } */
70     STMT_SWITCH, /* switch statement */
71     STMT_WITH, /* with statement */
72     STMT_CATCH, /* catch block */
73     STMT_TRY, /* try block */
74     STMT_FINALLY, /* finally block */
75     STMT_SUBROUTINE, /* gosub-target subroutine body */
76     STMT_DO_LOOP, /* do/while loop statement */
77     STMT_FOR_LOOP, /* for loop statement */
78     STMT_FOR_IN_LOOP, /* for/in loop statement */
79     STMT_WHILE_LOOP, /* while loop statement */
80     STMT_LIMIT
81     } JSStmtType;
82    
83     #define STMT_TYPE_IN_RANGE(t,b,e) ((uint)((t) - (b)) <= (uintN)((e) - (b)))
84    
85     /*
86     * A comment on the encoding of the JSStmtType enum and type-testing macros:
87     *
88     * STMT_TYPE_MAYBE_SCOPE tells whether a statement type is always, or may
89     * become, a lexical scope. It therefore includes block and switch (the two
90     * low-numbered "maybe" scope types) and excludes with (with has dynamic scope
91     * pending the "reformed with" in ES4/JS2). It includes all try-catch-finally
92     * types, which are high-numbered maybe-scope types.
93     *
94     * STMT_TYPE_LINKS_SCOPE tells whether a JSStmtInfo of the given type eagerly
95     * links to other scoping statement info records. It excludes the two early
96     * "maybe" types, block and switch, as well as the try and both finally types,
97     * since try and the other trailing maybe-scope types don't need block scope
98     * unless they contain let declarations.
99     *
100     * We treat WITH as a static scope because it prevents lexical binding from
101     * continuing further up the static scope chain. With the lost "reformed with"
102     * proposal for ES4, we would be able to model it statically, too.
103     */
104     #define STMT_TYPE_MAYBE_SCOPE(type) \
105     (type != STMT_WITH && \
106     STMT_TYPE_IN_RANGE(type, STMT_BLOCK, STMT_SUBROUTINE))
107    
108     #define STMT_TYPE_LINKS_SCOPE(type) \
109     STMT_TYPE_IN_RANGE(type, STMT_WITH, STMT_CATCH)
110    
111     #define STMT_TYPE_IS_TRYING(type) \
112     STMT_TYPE_IN_RANGE(type, STMT_TRY, STMT_SUBROUTINE)
113    
114     #define STMT_TYPE_IS_LOOP(type) ((type) >= STMT_DO_LOOP)
115    
116     #define STMT_MAYBE_SCOPE(stmt) STMT_TYPE_MAYBE_SCOPE((stmt)->type)
117     #define STMT_LINKS_SCOPE(stmt) (STMT_TYPE_LINKS_SCOPE((stmt)->type) || \
118     ((stmt)->flags & SIF_SCOPE))
119     #define STMT_IS_TRYING(stmt) STMT_TYPE_IS_TRYING((stmt)->type)
120     #define STMT_IS_LOOP(stmt) STMT_TYPE_IS_LOOP((stmt)->type)
121    
122     typedef struct JSStmtInfo JSStmtInfo;
123    
124     struct JSStmtInfo {
125     uint16 type; /* statement type */
126     uint16 flags; /* flags, see below */
127 siliconforks 460 uint32 blockid; /* for simplified dominance computation */
128 siliconforks 332 ptrdiff_t update; /* loop update offset (top if none) */
129     ptrdiff_t breaks; /* offset of last break in loop */
130     ptrdiff_t continues; /* offset of last continue in loop */
131     union {
132     JSAtom *label; /* name of LABEL */
133     JSObject *blockObj; /* block scope object */
134 siliconforks 460 };
135 siliconforks 332 JSStmtInfo *down; /* info for enclosing statement */
136     JSStmtInfo *downScope; /* next enclosing lexical scope */
137     };
138    
139     #define SIF_SCOPE 0x0001 /* statement has its own lexical scope */
140     #define SIF_BODY_BLOCK 0x0002 /* STMT_BLOCK type is a function body */
141     #define SIF_FOR_BLOCK 0x0004 /* for (let ...) induced block scope */
142    
143     /*
144     * To reuse space in JSStmtInfo, rename breaks and continues for use during
145     * try/catch/finally code generation and backpatching. To match most common
146     * use cases, the macro argument is a struct, not a struct pointer. Only a
147     * loop, switch, or label statement info record can have breaks and continues,
148     * and only a for loop has an update backpatch chain, so it's safe to overlay
149     * these for the "trying" JSStmtTypes.
150     */
151     #define CATCHNOTE(stmt) ((stmt).update)
152     #define GOSUBS(stmt) ((stmt).breaks)
153     #define GUARDJUMP(stmt) ((stmt).continues)
154    
155     #define SET_STATEMENT_TOP(stmt, top) \
156     ((stmt)->update = (top), (stmt)->breaks = (stmt)->continues = (-1))
157    
158 siliconforks 460 #ifdef JS_SCOPE_DEPTH_METER
159     # define JS_SCOPE_DEPTH_METERING(code) ((void) (code))
160     #else
161     # define JS_SCOPE_DEPTH_METERING(code) ((void) 0)
162     #endif
163    
164 siliconforks 332 struct JSTreeContext { /* tree context for semantic checks */
165     uint16 flags; /* statement state flags, see below */
166     uint16 ngvars; /* max. no. of global variables/regexps */
167 siliconforks 460 uint32 bodyid; /* block number of program/function body */
168     uint32 blockidGen; /* preincremented block number generator */
169 siliconforks 332 JSStmtInfo *topStmt; /* top of statement info stack */
170     JSStmtInfo *topScopeStmt; /* top lexical scope statement */
171     JSObject *blockChain; /* compile time block scope chain (NB: one
172     deeper than the topScopeStmt/downScope
173     chain when in head of let block/expr) */
174 siliconforks 460 JSParseNode *blockNode; /* parse node for a block with let declarations
175     (block with its own lexical scope) */
176 siliconforks 332 JSAtomList decls; /* function, const, and var declarations */
177 siliconforks 460 JSCompiler *compiler; /* ptr to common parsing and lexing data */
178 siliconforks 332
179     union {
180     JSFunction *fun; /* function to store argument and variable
181     names when flags & TCF_IN_FUNCTION */
182     JSObject *scopeChain; /* scope chain object for the script */
183 siliconforks 460 };
184 siliconforks 332
185 siliconforks 460 JSAtomList lexdeps; /* unresolved lexical name dependencies */
186     JSTreeContext *parent; /* enclosing function or global context */
187     uintN staticLevel; /* static compilation unit nesting level */
188    
189     JSFunctionBox *funbox; /* null or box for function we're compiling
190     if (flags & TCF_IN_FUNCTION) and not in
191     JSCompiler::compileFunctionBody */
192     JSFunctionBox *functionList;
193    
194 siliconforks 332 #ifdef JS_SCOPE_DEPTH_METER
195     uint16 scopeDepth; /* current lexical scope chain depth */
196     uint16 maxScopeDepth; /* maximum lexical scope chain depth */
197     #endif
198 siliconforks 460
199     JSTreeContext(JSCompiler *jsc)
200     : flags(0), ngvars(0), bodyid(0), blockidGen(0),
201     topStmt(NULL), topScopeStmt(NULL), blockChain(NULL), blockNode(NULL),
202     compiler(jsc), scopeChain(NULL), parent(NULL), staticLevel(0),
203     funbox(NULL), functionList(NULL)
204     {
205     JS_SCOPE_DEPTH_METERING(scopeDepth = maxScopeDepth = 0);
206     }
207    
208     /*
209     * For functions the tree context is constructed and destructed a second
210     * time during code generation. To avoid a redundant stats update in such
211     * cases, we store (uintN) -1 in maxScopeDepth.
212     */
213     ~JSTreeContext() {
214     JS_SCOPE_DEPTH_METERING(maxScopeDepth == (uintN) -1 ||
215     JS_BASIC_STATS_ACCUM(&compiler
216     ->context
217     ->runtime
218     ->lexicalScopeDepthStats,
219     maxScopeDepth));
220     }
221    
222     uintN blockid() { return topStmt ? topStmt->blockid : bodyid; }
223    
224     bool atTopLevel() { return !topStmt || (topStmt->flags & SIF_BODY_BLOCK); }
225    
226     /* Test whether we're in a statement of given type. */
227     bool inStatement(JSStmtType type);
228 siliconforks 332 };
229    
230     /*
231     * Flags to propagate out of the blocks.
232     */
233     #define TCF_RETURN_FLAGS (TCF_RETURN_EXPR | TCF_RETURN_VOID)
234    
235     /*
236 siliconforks 460 * TreeContext flags must fit in 16 bits, and all bits are in use now. Widening
237     * requires changing JSFunctionBox.tcflags too and repacking. Alternative fix
238     * gets rid of flags, probably starting with TCF_HAS_FUNCTION_STMT.
239 siliconforks 332 */
240 siliconforks 460 #define TCF_COMPILING 0x01 /* JSTreeContext is JSCodeGenerator */
241     #define TCF_IN_FUNCTION 0x02 /* parsing inside function body */
242     #define TCF_RETURN_EXPR 0x04 /* function has 'return expr;' */
243     #define TCF_RETURN_VOID 0x08 /* function has 'return;' */
244     #define TCF_IN_FOR_INIT 0x10 /* parsing init expr of for; exclude 'in' */
245     #define TCF_FUN_SETS_OUTER_NAME 0x20 /* function set outer name (lexical or free) */
246     #define TCF_FUN_PARAM_ARGUMENTS 0x40 /* function has parameter named arguments */
247     #define TCF_FUN_USES_ARGUMENTS 0x80 /* function uses arguments except as a
248     parameter name */
249     #define TCF_FUN_HEAVYWEIGHT 0x100 /* function needs Call object per call */
250     #define TCF_FUN_IS_GENERATOR 0x200 /* parsed yield statement in function */
251     #define TCF_FUN_IS_FUNARG 0x400 /* function escapes as an argument, return
252     value, or via the heap */
253     #define TCF_HAS_FUNCTION_STMT 0x800 /* block contains a function statement */
254     #define TCF_GENEXP_LAMBDA 0x1000 /* flag lambda from generator expression */
255     #define TCF_COMPILE_N_GO 0x2000 /* compiler-and-go mode of script, can
256     optimize name references based on scope
257     chain */
258     #define TCF_NO_SCRIPT_RVAL 0x4000 /* API caller does not want result value
259     from global script */
260     #define TCF_HAS_SHARPS 0x8000 /* source contains sharp defs or uses */
261 siliconforks 332
262     /*
263 siliconforks 460 * Sticky deoptimization flags to propagate from FunctionBody.
264 siliconforks 332 */
265 siliconforks 460 #define TCF_FUN_FLAGS (TCF_FUN_SETS_OUTER_NAME | \
266     TCF_FUN_USES_ARGUMENTS | \
267     TCF_FUN_PARAM_ARGUMENTS | \
268     TCF_FUN_HEAVYWEIGHT | \
269     TCF_FUN_IS_GENERATOR | \
270     TCF_FUN_IS_FUNARG | \
271     TCF_HAS_SHARPS)
272 siliconforks 332
273     /*
274 siliconforks 460 * Flags field, not stored in JSTreeContext.flags, for passing a static level
275     * into js_CompileScript.
276 siliconforks 332 */
277 siliconforks 460 #define TCF_STATIC_LEVEL_MASK 0xffff0000
278     #define TCF_GET_STATIC_LEVEL(f) ((uint32)(f) >> 16)
279     #define TCF_PUT_STATIC_LEVEL(d) ((uint16)(d) << 16)
280 siliconforks 332
281     /*
282     * Span-dependent instructions are jumps whose span (from the jump bytecode to
283     * the jump target) may require 2 or 4 bytes of immediate operand.
284     */
285     typedef struct JSSpanDep JSSpanDep;
286     typedef struct JSJumpTarget JSJumpTarget;
287    
288     struct JSSpanDep {
289     ptrdiff_t top; /* offset of first bytecode in an opcode */
290     ptrdiff_t offset; /* offset - 1 within opcode of jump operand */
291     ptrdiff_t before; /* original offset - 1 of jump operand */
292     JSJumpTarget *target; /* tagged target pointer or backpatch delta */
293     };
294    
295     /*
296     * Jump targets are stored in an AVL tree, for O(log(n)) lookup with targets
297     * sorted by offset from left to right, so that targets after a span-dependent
298     * instruction whose jump offset operand must be extended can be found quickly
299     * and adjusted upward (toward higher offsets).
300     */
301     struct JSJumpTarget {
302     ptrdiff_t offset; /* offset of span-dependent jump target */
303     int balance; /* AVL tree balance number */
304     JSJumpTarget *kids[2]; /* left and right AVL tree child pointers */
305     };
306    
307     #define JT_LEFT 0
308     #define JT_RIGHT 1
309     #define JT_OTHER_DIR(dir) (1 - (dir))
310     #define JT_IMBALANCE(dir) (((dir) << 1) - 1)
311     #define JT_DIR(imbalance) (((imbalance) + 1) >> 1)
312    
313     /*
314     * Backpatch deltas are encoded in JSSpanDep.target if JT_TAG_BIT is clear,
315     * so we can maintain backpatch chains when using span dependency records to
316     * hold jump offsets that overflow 16 bits.
317     */
318     #define JT_TAG_BIT ((jsword) 1)
319     #define JT_UNTAG_SHIFT 1
320     #define JT_SET_TAG(jt) ((JSJumpTarget *)((jsword)(jt) | JT_TAG_BIT))
321     #define JT_CLR_TAG(jt) ((JSJumpTarget *)((jsword)(jt) & ~JT_TAG_BIT))
322     #define JT_HAS_TAG(jt) ((jsword)(jt) & JT_TAG_BIT)
323    
324     #define BITS_PER_PTRDIFF (sizeof(ptrdiff_t) * JS_BITS_PER_BYTE)
325     #define BITS_PER_BPDELTA (BITS_PER_PTRDIFF - 1 - JT_UNTAG_SHIFT)
326     #define BPDELTA_MAX (((ptrdiff_t)1 << BITS_PER_BPDELTA) - 1)
327     #define BPDELTA_TO_JT(bp) ((JSJumpTarget *)((bp) << JT_UNTAG_SHIFT))
328     #define JT_TO_BPDELTA(jt) ((ptrdiff_t)((jsword)(jt) >> JT_UNTAG_SHIFT))
329    
330     #define SD_SET_TARGET(sd,jt) ((sd)->target = JT_SET_TAG(jt))
331     #define SD_GET_TARGET(sd) (JS_ASSERT(JT_HAS_TAG((sd)->target)), \
332     JT_CLR_TAG((sd)->target))
333     #define SD_SET_BPDELTA(sd,bp) ((sd)->target = BPDELTA_TO_JT(bp))
334     #define SD_GET_BPDELTA(sd) (JS_ASSERT(!JT_HAS_TAG((sd)->target)), \
335     JT_TO_BPDELTA((sd)->target))
336    
337     /* Avoid asserting twice by expanding SD_GET_TARGET in the "then" clause. */
338     #define SD_SPAN(sd,pivot) (SD_GET_TARGET(sd) \
339     ? JT_CLR_TAG((sd)->target)->offset - (pivot) \
340     : 0)
341    
342     typedef struct JSTryNode JSTryNode;
343    
344     struct JSTryNode {
345     JSTryNote note;
346     JSTryNode *prev;
347     };
348    
349 siliconforks 460 struct JSCGObjectList {
350 siliconforks 332 uint32 length; /* number of emitted so far objects */
351 siliconforks 460 JSObjectBox *lastbox; /* last emitted object */
352 siliconforks 332
353 siliconforks 460 JSCGObjectList() : length(0), lastbox(NULL) {}
354 siliconforks 332
355 siliconforks 460 uintN index(JSObjectBox *objbox);
356     void finish(JSObjectArray *array);
357     };
358 siliconforks 332
359 siliconforks 460 struct JSCodeGenerator : public JSTreeContext
360     {
361 siliconforks 332 JSArenaPool *codePool; /* pointer to thread code arena pool */
362     JSArenaPool *notePool; /* pointer to thread srcnote arena pool */
363     void *codeMark; /* low watermark in cg->codePool */
364     void *noteMark; /* low watermark in cg->notePool */
365    
366     struct {
367     jsbytecode *base; /* base of JS bytecode vector */
368     jsbytecode *limit; /* one byte beyond end of bytecode */
369     jsbytecode *next; /* pointer to next free bytecode */
370     jssrcnote *notes; /* source notes, see below */
371     uintN noteCount; /* number of source notes so far */
372     uintN noteMask; /* growth increment for notes */
373     ptrdiff_t lastNoteOffset; /* code offset for last source note */
374     uintN currentLine; /* line number for tree-based srcnote gen */
375     } prolog, main, *current;
376    
377     JSAtomList atomList; /* literals indexed for mapping */
378     uintN firstLine; /* first line, for js_NewScriptFromCG */
379    
380     intN stackDepth; /* current stack depth in script frame */
381     uintN maxStackDepth; /* maximum stack depth so far */
382    
383     uintN ntrynotes; /* number of allocated so far try notes */
384     JSTryNode *lastTryNode; /* the last allocated try node */
385    
386     JSSpanDep *spanDeps; /* span dependent instruction records */
387     JSJumpTarget *jumpTargets; /* AVL tree of jump target offsets */
388     JSJumpTarget *jtFreeList; /* JT_LEFT-linked list of free structs */
389     uintN numSpanDeps; /* number of span dependencies */
390     uintN numJumpTargets; /* number of jump targets */
391     ptrdiff_t spanDepTodo; /* offset from main.base of potentially
392     unoptimized spandeps */
393    
394     uintN arrayCompDepth; /* stack depth of array in comprehension */
395    
396     uintN emitLevel; /* js_EmitTree recursion level */
397     JSAtomList constList; /* compile time constants */
398    
399 siliconforks 460 JSCGObjectList objectList; /* list of emitted objects */
400     JSCGObjectList regexpList; /* list of emitted regexp that will be
401     cloned during execution */
402 siliconforks 332
403     JSAtomList upvarList; /* map of atoms to upvar indexes */
404     JSUpvarArray upvarMap; /* indexed upvar pairs (JS_realloc'ed) */
405 siliconforks 460
406     /*
407     * Initialize cg to allocate bytecode space from codePool, source note
408     * space from notePool, and all other arena-allocated temporaries from
409     * jsc->context->tempPool.
410     */
411     JSCodeGenerator(JSCompiler *jsc,
412     JSArenaPool *codePool, JSArenaPool *notePool,
413     uintN lineno);
414    
415     /*
416     * Release cg->codePool, cg->notePool, and compiler->context->tempPool to
417     * marks set by JSCodeGenerator's ctor. Note that cgs are magic: they own
418     * the arena pool "tops-of-stack" space above their codeMark, noteMark, and
419     * tempMark points. This means you cannot alloc from tempPool and save the
420     * pointer beyond the next JSCodeGenerator destructor call.
421     */
422     ~JSCodeGenerator();
423 siliconforks 332 };
424    
425 siliconforks 460 #define CG_TS(cg) TS((cg)->compiler)
426 siliconforks 332
427     #define CG_BASE(cg) ((cg)->current->base)
428     #define CG_LIMIT(cg) ((cg)->current->limit)
429     #define CG_NEXT(cg) ((cg)->current->next)
430     #define CG_CODE(cg,offset) (CG_BASE(cg) + (offset))
431 siliconforks 507 #define CG_OFFSET(cg) (CG_NEXT(cg) - CG_BASE(cg))
432 siliconforks 332
433     #define CG_NOTES(cg) ((cg)->current->notes)
434     #define CG_NOTE_COUNT(cg) ((cg)->current->noteCount)
435     #define CG_NOTE_MASK(cg) ((cg)->current->noteMask)
436     #define CG_LAST_NOTE_OFFSET(cg) ((cg)->current->lastNoteOffset)
437     #define CG_CURRENT_LINE(cg) ((cg)->current->currentLine)
438    
439     #define CG_PROLOG_BASE(cg) ((cg)->prolog.base)
440     #define CG_PROLOG_LIMIT(cg) ((cg)->prolog.limit)
441     #define CG_PROLOG_NEXT(cg) ((cg)->prolog.next)
442     #define CG_PROLOG_CODE(cg,poff) (CG_PROLOG_BASE(cg) + (poff))
443 siliconforks 507 #define CG_PROLOG_OFFSET(cg) (CG_PROLOG_NEXT(cg) - CG_PROLOG_BASE(cg))
444 siliconforks 332
445     #define CG_SWITCH_TO_MAIN(cg) ((cg)->current = &(cg)->main)
446     #define CG_SWITCH_TO_PROLOG(cg) ((cg)->current = &(cg)->prolog)
447    
448     /*
449     * Emit one bytecode.
450     */
451     extern ptrdiff_t
452     js_Emit1(JSContext *cx, JSCodeGenerator *cg, JSOp op);
453    
454     /*
455     * Emit two bytecodes, an opcode (op) with a byte of immediate operand (op1).
456     */
457     extern ptrdiff_t
458     js_Emit2(JSContext *cx, JSCodeGenerator *cg, JSOp op, jsbytecode op1);
459    
460     /*
461     * Emit three bytecodes, an opcode with two bytes of immediate operands.
462     */
463     extern ptrdiff_t
464     js_Emit3(JSContext *cx, JSCodeGenerator *cg, JSOp op, jsbytecode op1,
465     jsbytecode op2);
466    
467     /*
468     * Emit (1 + extra) bytecodes, for N bytes of op and its immediate operand.
469     */
470     extern ptrdiff_t
471     js_EmitN(JSContext *cx, JSCodeGenerator *cg, JSOp op, size_t extra);
472    
473     /*
474     * Unsafe macro to call js_SetJumpOffset and return false if it does.
475     */
476     #define CHECK_AND_SET_JUMP_OFFSET_CUSTOM(cx,cg,pc,off,BAD_EXIT) \
477     JS_BEGIN_MACRO \
478     if (!js_SetJumpOffset(cx, cg, pc, off)) { \
479     BAD_EXIT; \
480     } \
481     JS_END_MACRO
482    
483     #define CHECK_AND_SET_JUMP_OFFSET(cx,cg,pc,off) \
484     CHECK_AND_SET_JUMP_OFFSET_CUSTOM(cx,cg,pc,off,return JS_FALSE)
485    
486     #define CHECK_AND_SET_JUMP_OFFSET_AT_CUSTOM(cx,cg,off,BAD_EXIT) \
487     CHECK_AND_SET_JUMP_OFFSET_CUSTOM(cx, cg, CG_CODE(cg,off), \
488     CG_OFFSET(cg) - (off), BAD_EXIT)
489    
490     #define CHECK_AND_SET_JUMP_OFFSET_AT(cx,cg,off) \
491     CHECK_AND_SET_JUMP_OFFSET_AT_CUSTOM(cx, cg, off, return JS_FALSE)
492    
493     extern JSBool
494     js_SetJumpOffset(JSContext *cx, JSCodeGenerator *cg, jsbytecode *pc,
495     ptrdiff_t off);
496    
497     /*
498     * Push the C-stack-allocated struct at stmt onto the stmtInfo stack.
499     */
500     extern void
501     js_PushStatement(JSTreeContext *tc, JSStmtInfo *stmt, JSStmtType type,
502     ptrdiff_t top);
503    
504     /*
505     * Push a block scope statement and link blockObj into tc->blockChain. To pop
506     * this statement info record, use js_PopStatement as usual, or if appropriate
507     * (if generating code), js_PopStatementCG.
508     */
509     extern void
510     js_PushBlockScope(JSTreeContext *tc, JSStmtInfo *stmt, JSObject *blockObj,
511     ptrdiff_t top);
512    
513     /*
514     * Pop tc->topStmt. If the top JSStmtInfo struct is not stack-allocated, it
515     * is up to the caller to free it.
516     */
517     extern void
518     js_PopStatement(JSTreeContext *tc);
519    
520     /*
521 siliconforks 460 * Like js_PopStatement(cg), also patch breaks and continues unless the top
522     * statement info record represents a try-catch-finally suite. May fail if a
523     * jump offset overflows.
524 siliconforks 332 */
525     extern JSBool
526     js_PopStatementCG(JSContext *cx, JSCodeGenerator *cg);
527    
528     /*
529     * Define and lookup a primitive jsval associated with the const named by atom.
530     * js_DefineCompileTimeConstant analyzes the constant-folded initializer at pn
531     * and saves the const's value in cg->constList, if it can be used at compile
532     * time. It returns true unless an error occurred.
533     *
534     * If the initializer's value could not be saved, js_DefineCompileTimeConstant
535     * calls will return the undefined value. js_DefineCompileTimeConstant tries
536     * to find a const value memorized for atom, returning true with *vp set to a
537     * value other than undefined if the constant was found, true with *vp set to
538     * JSVAL_VOID if not found, and false on error.
539     */
540     extern JSBool
541     js_DefineCompileTimeConstant(JSContext *cx, JSCodeGenerator *cg, JSAtom *atom,
542     JSParseNode *pn);
543    
544     /*
545     * Find a lexically scoped variable (one declared by let, catch, or an array
546     * comprehension) named by atom, looking in tc's compile-time scopes.
547     *
548     * If a WITH statement is reached along the scope stack, return its statement
549     * info record, so callers can tell that atom is ambiguous. If slotp is not
550     * null, then if atom is found, set *slotp to its stack slot, otherwise to -1.
551     * This means that if slotp is not null, all the block objects on the lexical
552     * scope chain must have had their depth slots computed by the code generator,
553     * so the caller must be under js_EmitTree.
554     *
555     * In any event, directly return the statement info record in which atom was
556     * found. Otherwise return null.
557     */
558     extern JSStmtInfo *
559 siliconforks 460 js_LexicalLookup(JSTreeContext *tc, JSAtom *atom, jsint *slotp,
560     JSStmtInfo *stmt = NULL);
561 siliconforks 332
562     /*
563     * Emit code into cg for the tree rooted at pn.
564     */
565     extern JSBool
566     js_EmitTree(JSContext *cx, JSCodeGenerator *cg, JSParseNode *pn);
567    
568     /*
569     * Emit function code using cg for the tree rooted at body.
570     */
571     extern JSBool
572     js_EmitFunctionScript(JSContext *cx, JSCodeGenerator *cg, JSParseNode *body);
573    
574     /*
575     * Source notes generated along with bytecode for decompiling and debugging.
576     * A source note is a uint8 with 5 bits of type and 3 of offset from the pc of
577     * the previous note. If 3 bits of offset aren't enough, extended delta notes
578     * (SRC_XDELTA) consisting of 2 set high order bits followed by 6 offset bits
579     * are emitted before the next note. Some notes have operand offsets encoded
580     * immediately after them, in note bytes or byte-triples.
581     *
582     * Source Note Extended Delta
583     * +7-6-5-4-3+2-1-0+ +7-6-5+4-3-2-1-0+
584     * |note-type|delta| |1 1| ext-delta |
585     * +---------+-----+ +---+-----------+
586     *
587     * At most one "gettable" note (i.e., a note of type other than SRC_NEWLINE,
588     * SRC_SETLINE, and SRC_XDELTA) applies to a given bytecode.
589     *
590     * NB: the js_SrcNoteSpec array in jsemit.c is indexed by this enum, so its
591     * initializers need to match the order here.
592     *
593     * Note on adding new source notes: every pair of bytecodes (A, B) where A and
594     * B have disjoint sets of source notes that could apply to each bytecode may
595     * reuse the same note type value for two notes (snA, snB) that have the same
596     * arity, offsetBias, and isSpanDep initializers in js_SrcNoteSpec. This is
597     * why SRC_IF and SRC_INITPROP have the same value below. For bad historical
598     * reasons, some bytecodes below that could be overlayed have not been, but
599     * before using SRC_EXTENDED, consider compressing the existing note types.
600     *
601     * Don't forget to update JSXDR_BYTECODE_VERSION in jsxdrapi.h for all such
602     * incompatible source note or other bytecode changes.
603     */
604     typedef enum JSSrcNoteType {
605     SRC_NULL = 0, /* terminates a note vector */
606     SRC_IF = 1, /* JSOP_IFEQ bytecode is from an if-then */
607     SRC_BREAK = 1, /* JSOP_GOTO is a break */
608     SRC_INITPROP = 1, /* disjoint meaning applied to JSOP_INITELEM or
609     to an index label in a regular (structuring)
610     or a destructuring object initialiser */
611 siliconforks 460 SRC_GENEXP = 1, /* JSOP_LAMBDA from generator expression */
612 siliconforks 332 SRC_IF_ELSE = 2, /* JSOP_IFEQ bytecode is from an if-then-else */
613 siliconforks 399 SRC_FOR_IN = 2, /* JSOP_GOTO to for-in loop condition from
614     before loop (same arity as SRC_IF_ELSE) */
615     SRC_FOR = 3, /* JSOP_NOP or JSOP_POP in for(;;) loop head */
616     SRC_WHILE = 4, /* JSOP_GOTO to for or while loop condition
617     from before loop, else JSOP_NOP at top of
618     do-while loop */
619 siliconforks 332 SRC_CONTINUE = 5, /* JSOP_GOTO is a continue, not a break;
620     also used on JSOP_ENDINIT if extra comma
621 siliconforks 460 at end of array literal: [1,2,,];
622     JSOP_DUP continuing destructuring pattern */
623 siliconforks 332 SRC_DECL = 6, /* type of a declaration (var, const, let*) */
624     SRC_DESTRUCT = 6, /* JSOP_DUP starting a destructuring assignment
625     operation, with SRC_DECL_* offset operand */
626     SRC_PCDELTA = 7, /* distance forward from comma-operator to
627     next POP, or from CONDSWITCH to first CASE
628     opcode, etc. -- always a forward delta */
629     SRC_GROUPASSIGN = 7, /* SRC_DESTRUCT variant for [a, b] = [c, d] */
630     SRC_ASSIGNOP = 8, /* += or another assign-op follows */
631     SRC_COND = 9, /* JSOP_IFEQ is from conditional ?: operator */
632     SRC_BRACE = 10, /* mandatory brace, for scope or to avoid
633     dangling else */
634     SRC_HIDDEN = 11, /* opcode shouldn't be decompiled */
635     SRC_PCBASE = 12, /* distance back from annotated getprop or
636     setprop op to left-most obj.prop.subprop
637     bytecode -- always a backward delta */
638     SRC_LABEL = 13, /* JSOP_NOP for label: with atomid immediate */
639     SRC_LABELBRACE = 14, /* JSOP_NOP for label: {...} begin brace */
640     SRC_ENDBRACE = 15, /* JSOP_NOP for label: {...} end brace */
641     SRC_BREAK2LABEL = 16, /* JSOP_GOTO for 'break label' with atomid */
642     SRC_CONT2LABEL = 17, /* JSOP_GOTO for 'continue label' with atomid */
643     SRC_SWITCH = 18, /* JSOP_*SWITCH with offset to end of switch,
644     2nd off to first JSOP_CASE if condswitch */
645     SRC_FUNCDEF = 19, /* JSOP_NOP for function f() with atomid */
646     SRC_CATCH = 20, /* catch block has guard */
647     SRC_EXTENDED = 21, /* extended source note, 32-159, in next byte */
648     SRC_NEWLINE = 22, /* bytecode follows a source newline */
649     SRC_SETLINE = 23, /* a file-absolute source line number note */
650     SRC_XDELTA = 24 /* 24-31 are for extended delta notes */
651     } JSSrcNoteType;
652    
653     /*
654     * Constants for the SRC_DECL source note. Note that span-dependent bytecode
655     * selection means that any SRC_DECL offset greater than SRC_DECL_LET may need
656     * to be adjusted, but these "offsets" are too small to span a span-dependent
657     * instruction, so can be used to denote distinct declaration syntaxes to the
658     * decompiler.
659     *
660     * NB: the var_prefix array in jsopcode.c depends on these dense indexes from
661     * SRC_DECL_VAR through SRC_DECL_LET.
662     */
663     #define SRC_DECL_VAR 0
664     #define SRC_DECL_CONST 1
665     #define SRC_DECL_LET 2
666     #define SRC_DECL_NONE 3
667    
668     #define SN_TYPE_BITS 5
669     #define SN_DELTA_BITS 3
670     #define SN_XDELTA_BITS 6
671     #define SN_TYPE_MASK (JS_BITMASK(SN_TYPE_BITS) << SN_DELTA_BITS)
672     #define SN_DELTA_MASK ((ptrdiff_t)JS_BITMASK(SN_DELTA_BITS))
673     #define SN_XDELTA_MASK ((ptrdiff_t)JS_BITMASK(SN_XDELTA_BITS))
674    
675     #define SN_MAKE_NOTE(sn,t,d) (*(sn) = (jssrcnote) \
676     (((t) << SN_DELTA_BITS) \
677     | ((d) & SN_DELTA_MASK)))
678     #define SN_MAKE_XDELTA(sn,d) (*(sn) = (jssrcnote) \
679     ((SRC_XDELTA << SN_DELTA_BITS) \
680     | ((d) & SN_XDELTA_MASK)))
681    
682     #define SN_IS_XDELTA(sn) ((*(sn) >> SN_DELTA_BITS) >= SRC_XDELTA)
683     #define SN_TYPE(sn) ((JSSrcNoteType)(SN_IS_XDELTA(sn) \
684     ? SRC_XDELTA \
685     : *(sn) >> SN_DELTA_BITS))
686     #define SN_SET_TYPE(sn,type) SN_MAKE_NOTE(sn, type, SN_DELTA(sn))
687     #define SN_IS_GETTABLE(sn) (SN_TYPE(sn) < SRC_NEWLINE)
688    
689     #define SN_DELTA(sn) ((ptrdiff_t)(SN_IS_XDELTA(sn) \
690     ? *(sn) & SN_XDELTA_MASK \
691     : *(sn) & SN_DELTA_MASK))
692     #define SN_SET_DELTA(sn,delta) (SN_IS_XDELTA(sn) \
693     ? SN_MAKE_XDELTA(sn, delta) \
694     : SN_MAKE_NOTE(sn, SN_TYPE(sn), delta))
695    
696     #define SN_DELTA_LIMIT ((ptrdiff_t)JS_BIT(SN_DELTA_BITS))
697     #define SN_XDELTA_LIMIT ((ptrdiff_t)JS_BIT(SN_XDELTA_BITS))
698    
699     /*
700     * Offset fields follow certain notes and are frequency-encoded: an offset in
701     * [0,0x7f] consumes one byte, an offset in [0x80,0x7fffff] takes three, and
702     * the high bit of the first byte is set.
703     */
704     #define SN_3BYTE_OFFSET_FLAG 0x80
705     #define SN_3BYTE_OFFSET_MASK 0x7f
706    
707     typedef struct JSSrcNoteSpec {
708     const char *name; /* name for disassembly/debugging output */
709     int8 arity; /* number of offset operands */
710     uint8 offsetBias; /* bias of offset(s) from annotated pc */
711     int8 isSpanDep; /* 1 or -1 if offsets could span extended ops,
712     0 otherwise; sign tells span direction */
713     } JSSrcNoteSpec;
714    
715     extern JS_FRIEND_DATA(JSSrcNoteSpec) js_SrcNoteSpec[];
716     extern JS_FRIEND_API(uintN) js_SrcNoteLength(jssrcnote *sn);
717    
718     #define SN_LENGTH(sn) ((js_SrcNoteSpec[SN_TYPE(sn)].arity == 0) ? 1 \
719     : js_SrcNoteLength(sn))
720     #define SN_NEXT(sn) ((sn) + SN_LENGTH(sn))
721    
722     /* A source note array is terminated by an all-zero element. */
723     #define SN_MAKE_TERMINATOR(sn) (*(sn) = SRC_NULL)
724     #define SN_IS_TERMINATOR(sn) (*(sn) == SRC_NULL)
725    
726     /*
727     * Append a new source note of the given type (and therefore size) to cg's
728     * notes dynamic array, updating cg->noteCount. Return the new note's index
729     * within the array pointed at by cg->current->notes. Return -1 if out of
730     * memory.
731     */
732     extern intN
733     js_NewSrcNote(JSContext *cx, JSCodeGenerator *cg, JSSrcNoteType type);
734    
735     extern intN
736     js_NewSrcNote2(JSContext *cx, JSCodeGenerator *cg, JSSrcNoteType type,
737     ptrdiff_t offset);
738    
739     extern intN
740     js_NewSrcNote3(JSContext *cx, JSCodeGenerator *cg, JSSrcNoteType type,
741     ptrdiff_t offset1, ptrdiff_t offset2);
742    
743     /*
744     * NB: this function can add at most one extra extended delta note.
745     */
746     extern jssrcnote *
747     js_AddToSrcNoteDelta(JSContext *cx, JSCodeGenerator *cg, jssrcnote *sn,
748     ptrdiff_t delta);
749    
750     /*
751     * Get and set the offset operand identified by which (0 for the first, etc.).
752     */
753     extern JS_FRIEND_API(ptrdiff_t)
754     js_GetSrcNoteOffset(jssrcnote *sn, uintN which);
755    
756     extern JSBool
757     js_SetSrcNoteOffset(JSContext *cx, JSCodeGenerator *cg, uintN index,
758     uintN which, ptrdiff_t offset);
759    
760     /*
761     * Finish taking source notes in cx's notePool, copying final notes to the new
762     * stable store allocated by the caller and passed in via notes. Return false
763     * on malloc failure, which means this function reported an error.
764     *
765     * To compute the number of jssrcnotes to allocate and pass in via notes, use
766     * the CG_COUNT_FINAL_SRCNOTES macro. This macro knows a lot about details of
767     * js_FinishTakingSrcNotes, SO DON'T CHANGE jsemit.c's js_FinishTakingSrcNotes
768     * FUNCTION WITHOUT CHECKING WHETHER THIS MACRO NEEDS CORRESPONDING CHANGES!
769     */
770     #define CG_COUNT_FINAL_SRCNOTES(cg, cnt) \
771     JS_BEGIN_MACRO \
772     ptrdiff_t diff_ = CG_PROLOG_OFFSET(cg) - (cg)->prolog.lastNoteOffset; \
773     cnt = (cg)->prolog.noteCount + (cg)->main.noteCount + 1; \
774     if ((cg)->prolog.noteCount && \
775     (cg)->prolog.currentLine != (cg)->firstLine) { \
776     if (diff_ > SN_DELTA_MASK) \
777     cnt += JS_HOWMANY(diff_ - SN_DELTA_MASK, SN_XDELTA_MASK); \
778     cnt += 2 + (((cg)->firstLine > SN_3BYTE_OFFSET_MASK) << 1); \
779     } else if (diff_ > 0) { \
780     if (cg->main.noteCount) { \
781     jssrcnote *sn_ = (cg)->main.notes; \
782     diff_ -= SN_IS_XDELTA(sn_) \
783     ? SN_XDELTA_MASK - (*sn_ & SN_XDELTA_MASK) \
784     : SN_DELTA_MASK - (*sn_ & SN_DELTA_MASK); \
785     } \
786     if (diff_ > 0) \
787     cnt += JS_HOWMANY(diff_, SN_XDELTA_MASK); \
788     } \
789     JS_END_MACRO
790    
791     extern JSBool
792     js_FinishTakingSrcNotes(JSContext *cx, JSCodeGenerator *cg, jssrcnote *notes);
793    
794     extern void
795     js_FinishTakingTryNotes(JSCodeGenerator *cg, JSTryNoteArray *array);
796    
797     JS_END_EXTERN_C
798    
799     #endif /* jsemit_h___ */

  ViewVC Help
Powered by ViewVC 1.1.24