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

Contents of /trunk/js/jsemit.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 332 - (show annotations)
Thu Oct 23 19:03:33 2008 UTC (13 years, 8 months ago) by siliconforks
File MIME type: text/plain
File size: 36390 byte(s)
Add SpiderMonkey from Firefox 3.1b1.

The following directories and files were removed:
correct/, correct.js
liveconnect/
nanojit/
t/
v8/
vprof/
xpconnect/
all JavaScript files (Y.js, call.js, if.js, math-partial-sums.js, md5.js, perfect.js, trace-test.js, trace.js)


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

  ViewVC Help
Powered by ViewVC 1.1.24