1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 *
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
10 *
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
15 *
16 * The Original Code is Mozilla Communicator client code, released
17 * March 31, 1998.
18 *
19 * The Initial Developer of the Original Code is
20 * Netscape Communications Corporation.
21 * Portions created by the Initial Developer are Copyright (C) 1998
22 * the Initial Developer. All Rights Reserved.
23 *
24 * Contributor(s):
25 *
26 * Alternatively, the contents of this file may be used under the terms of
27 * either of the GNU General Public License Version 2 or later (the "GPL"),
28 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
37 *
38 * ***** END LICENSE BLOCK ***** */
40 #ifndef jsemit_h___
41 #define jsemit_h___
42 /*
43 * JS bytecode generation.
44 */
46 #include "jsstddef.h"
47 #include "jstypes.h"
48 #include "jsatom.h"
49 #include "jsopcode.h"
50 #include "jsprvtd.h"
51 #include "jspubtd.h"
53 JS_BEGIN_EXTERN_C
55 /*
56 * NB: If you add non-loop STMT_* enumerators, do so before STMT_DO_LOOP or
57 * you will break the STMT_IS_LOOP macro, just below this enum.
58 */
59 typedef enum JSStmtType {
60 STMT_BLOCK = 0, /* compound statement: { s1[;... sN] } */
61 STMT_LABEL = 1, /* labeled statement: L: s */
62 STMT_IF = 2, /* if (then) statement */
63 STMT_ELSE = 3, /* else clause of if statement */
64 STMT_SWITCH = 4, /* switch statement */
65 STMT_WITH = 5, /* with statement */
66 STMT_TRY = 6, /* try statement */
67 STMT_CATCH = 7, /* catch block */
68 STMT_FINALLY = 8, /* finally statement */
69 STMT_SUBROUTINE = 9, /* gosub-target subroutine body */
70 STMT_DO_LOOP = 10, /* do/while loop statement */
71 STMT_FOR_LOOP = 11, /* for loop statement */
72 STMT_FOR_IN_LOOP = 12, /* for/in loop statement */
73 STMT_WHILE_LOOP = 13 /* while loop statement */
74 } JSStmtType;
76 #define STMT_IS_LOOP(stmt) ((stmt)->type >= STMT_DO_LOOP)
78 typedef struct JSStmtInfo JSStmtInfo;
80 struct JSStmtInfo {
81 JSStmtType type; /* statement type */
82 ptrdiff_t update; /* loop update offset (top if none) */
83 ptrdiff_t breaks; /* offset of last break in loop */
84 ptrdiff_t continues; /* offset of last continue in loop */
85 ptrdiff_t gosub; /* offset of last GOSUB for this finally */
86 ptrdiff_t catchJump; /* offset of last end-of-catch jump */
87 JSAtom *label; /* name of LABEL or CATCH var */
88 JSStmtInfo *down; /* info for enclosing statement */
89 };
91 #define SET_STATEMENT_TOP(stmt, top) \
92 ((stmt)->update = (top), (stmt)->breaks = \
93 (stmt)->continues = (stmt)->catchJump = (stmt)->gosub = (-1))
95 struct JSTreeContext { /* tree context for semantic checks */
96 uint32 flags; /* statement state flags, see below */
97 uint32 tryCount; /* total count of try statements parsed */
98 JSStmtInfo *topStmt; /* top of statement info stack */
99 JSAtomList decls; /* function, const, and var declarations */
100 JSParseNode *nodeList; /* list of recyclable parse-node structs */
101 };
103 #define TCF_COMPILING 0x01 /* generating bytecode; this tc is a cg */
104 #define TCF_IN_FUNCTION 0x02 /* parsing inside function body */
105 #define TCF_RETURN_EXPR 0x04 /* function has 'return expr;' */
106 #define TCF_RETURN_VOID 0x08 /* function has 'return;' */
107 #define TCF_IN_FOR_INIT 0x10 /* parsing init expr of for; exclude 'in' */
108 #define TCF_FUN_CLOSURE_VS_VAR 0x20 /* function and var with same name */
109 #define TCF_FUN_USES_NONLOCALS 0x40 /* function refers to non-local names */
110 #define TCF_FUN_HEAVYWEIGHT 0x80 /* function needs Call object per call */
111 #define TCF_FUN_FLAGS 0xE0 /* flags to propagate from FunctionBody */
113 #define TREE_CONTEXT_INIT(tc) \
114 ((tc)->flags = 0, (tc)->tryCount = 0, (tc)->topStmt = NULL, \
115 ATOM_LIST_INIT(&(tc)->decls), (tc)->nodeList = NULL)
117 #define TREE_CONTEXT_FINISH(tc) \
118 ((void)0)
120 /*
121 * Span-dependent instructions are jumps whose span (from the jump bytecode to
122 * the jump target) may require 2 or 4 bytes of immediate operand.
123 */
124 typedef struct JSSpanDep JSSpanDep;
125 typedef struct JSJumpTarget JSJumpTarget;
127 struct JSSpanDep {
128 ptrdiff_t top; /* offset of first bytecode in an opcode */
129 ptrdiff_t offset; /* offset - 1 within opcode of jump operand */
130 ptrdiff_t before; /* original offset - 1 of jump operand */
131 JSJumpTarget *target; /* tagged target pointer or backpatch delta */
132 };
134 /*
135 * Jump targets are stored in an AVL tree, for O(log(n)) lookup with targets
136 * sorted by offset from left to right, so that targets after a span-dependent
137 * instruction whose jump offset operand must be extended can be found quickly
138 * and adjusted upward (toward higher offsets).
139 */
140 struct JSJumpTarget {
141 ptrdiff_t offset; /* offset of span-dependent jump target */
142 int balance; /* AVL tree balance number */
143 JSJumpTarget *kids[2]; /* left and right AVL tree child pointers */
144 };
146 #define JT_LEFT 0
147 #define JT_RIGHT 1
148 #define JT_OTHER_DIR(dir) (1 - (dir))
149 #define JT_IMBALANCE(dir) (((dir) << 1) - 1)
150 #define JT_DIR(imbalance) (((imbalance) + 1) >> 1)
152 /*
153 * Backpatch deltas are encoded in JSSpanDep.target if JT_TAG_BIT is clear,
154 * so we can maintain backpatch chains when using span dependency records to
155 * hold jump offsets that overflow 16 bits.
156 */
157 #define JT_TAG_BIT ((jsword) 1)
158 #define JT_UNTAG_SHIFT 1
159 #define JT_SET_TAG(jt) ((JSJumpTarget *)((jsword)(jt) | JT_TAG_BIT))
160 #define JT_CLR_TAG(jt) ((JSJumpTarget *)((jsword)(jt) & ~JT_TAG_BIT))
161 #define JT_HAS_TAG(jt) ((jsword)(jt) & JT_TAG_BIT)
163 #define BITS_PER_PTRDIFF (sizeof(ptrdiff_t) * JS_BITS_PER_BYTE)
164 #define BITS_PER_BPDELTA (BITS_PER_PTRDIFF - 1 - JT_UNTAG_SHIFT)
165 #define BPDELTA_MAX (((ptrdiff_t)1 << BITS_PER_BPDELTA) - 1)
166 #define BPDELTA_TO_JT(bp) ((JSJumpTarget *)((bp) << JT_UNTAG_SHIFT))
167 #define JT_TO_BPDELTA(jt) ((ptrdiff_t)((jsword)(jt) >> JT_UNTAG_SHIFT))
169 #define SD_SET_TARGET(sd,jt) ((sd)->target = JT_SET_TAG(jt))
170 #define SD_SET_BPDELTA(sd,bp) ((sd)->target = BPDELTA_TO_JT(bp))
171 #define SD_GET_BPDELTA(sd) (JS_ASSERT(!JT_HAS_TAG((sd)->target)), \
172 JT_TO_BPDELTA((sd)->target))
173 #define SD_TARGET_OFFSET(sd) (JS_ASSERT(JT_HAS_TAG((sd)->target)), \
174 JT_CLR_TAG((sd)->target)->offset)
176 struct JSCodeGenerator {
177 JSTreeContext treeContext; /* base state: statement info stack, etc. */
178 JSArenaPool *codePool; /* pointer to thread code arena pool */
179 JSArenaPool *notePool; /* pointer to thread srcnote arena pool */
180 void *codeMark; /* low watermark in cg->codePool */
181 void *noteMark; /* low watermark in cg->notePool */
182 void *tempMark; /* low watermark in cx->tempPool */
183 struct {
184 jsbytecode *base; /* base of JS bytecode vector */
185 jsbytecode *limit; /* one byte beyond end of bytecode */
186 jsbytecode *next; /* pointer to next free bytecode */
187 jssrcnote *notes; /* source notes, see below */
188 uintN noteCount; /* number of source notes so far */
189 uintN noteMask; /* growth increment for notes */
190 ptrdiff_t lastNoteOffset; /* code offset for last source note */
191 uintN currentLine; /* line number for tree-based srcnote gen */
192 } prolog, main, *current;
193 const char *filename; /* null or weak link to source filename */
194 uintN firstLine; /* first line, for js_NewScriptFromCG */
195 JSPrincipals *principals; /* principals for constant folding eval */
196 JSAtomList atomList; /* literals indexed for mapping */
197 intN stackDepth; /* current stack depth in script frame */
198 uintN maxStackDepth; /* maximum stack depth so far */
199 JSTryNote *tryBase; /* first exception handling note */
200 JSTryNote *tryNext; /* next available note */
201 size_t tryNoteSpace; /* # of bytes allocated at tryBase */
202 JSSpanDep *spanDeps; /* span dependent instruction records */
203 JSJumpTarget *jumpTargets; /* AVL tree of jump target offsets */
204 JSJumpTarget *jtFreeList; /* JT_LEFT-linked list of free structs */
205 uintN numSpanDeps; /* number of span dependencies */
206 uintN numJumpTargets; /* number of jump targets */
207 uintN emitLevel; /* js_EmitTree recursion level */
208 };
210 #define CG_BASE(cg) ((cg)->current->base)
211 #define CG_LIMIT(cg) ((cg)->current->limit)
212 #define CG_NEXT(cg) ((cg)->current->next)
213 #define CG_CODE(cg,offset) (CG_BASE(cg) + (offset))
214 #define CG_OFFSET(cg) PTRDIFF(CG_NEXT(cg), CG_BASE(cg), jsbytecode)
216 #define CG_NOTES(cg) ((cg)->current->notes)
217 #define CG_NOTE_COUNT(cg) ((cg)->current->noteCount)
218 #define CG_NOTE_MASK(cg) ((cg)->current->noteMask)
219 #define CG_LAST_NOTE_OFFSET(cg) ((cg)->current->lastNoteOffset)
220 #define CG_CURRENT_LINE(cg) ((cg)->current->currentLine)
222 #define CG_PROLOG_BASE(cg) ((cg)->prolog.base)
223 #define CG_PROLOG_LIMIT(cg) ((cg)->prolog.limit)
224 #define CG_PROLOG_NEXT(cg) ((cg)->prolog.next)
225 #define CG_PROLOG_CODE(cg,poff) (CG_PROLOG_BASE(cg) + (poff))
226 #define CG_PROLOG_OFFSET(cg) PTRDIFF(CG_PROLOG_NEXT(cg), CG_PROLOG_BASE(cg),\
227 jsbytecode)
229 #define CG_SWITCH_TO_MAIN(cg) ((cg)->current = &(cg)->main)
230 #define CG_SWITCH_TO_PROLOG(cg) ((cg)->current = &(cg)->prolog)
232 /*
233 * Initialize cg to allocate bytecode space from codePool, source note space
234 * from notePool, and all other arena-allocated temporaries from cx->tempPool.
235 * Return true on success. Report an error and return false if the initial
236 * code segment can't be allocated.
237 */
238 extern JS_FRIEND_API(JSBool)
239 js_InitCodeGenerator(JSContext *cx, JSCodeGenerator *cg,
240 JSArenaPool *codePool, JSArenaPool *notePool,
241 const char *filename, uintN lineno,
242 JSPrincipals *principals);
244 /*
245 * Release cg->codePool, cg->notePool, and cx->tempPool to marks set by
246 * js_InitCodeGenerator. Note that cgs are magic: they own the arena pool
247 * "tops-of-stack" space above their codeMark, noteMark, and tempMark points.
248 * This means you cannot alloc from tempPool and save the pointer beyond the
249 * next JS_FinishCodeGenerator.
250 */
251 extern JS_FRIEND_API(void)
252 js_FinishCodeGenerator(JSContext *cx, JSCodeGenerator *cg);
254 /*
255 * Emit one bytecode.
256 */
257 extern ptrdiff_t
258 js_Emit1(JSContext *cx, JSCodeGenerator *cg, JSOp op);
260 /*
261 * Emit two bytecodes, an opcode (op) with a byte of immediate operand (op1).
262 */
263 extern ptrdiff_t
264 js_Emit2(JSContext *cx, JSCodeGenerator *cg, JSOp op, jsbytecode op1);
266 /*
267 * Emit three bytecodes, an opcode with two bytes of immediate operands.
268 */
269 extern ptrdiff_t
270 js_Emit3(JSContext *cx, JSCodeGenerator *cg, JSOp op, jsbytecode op1,
271 jsbytecode op2);
273 /*
274 * Emit (1 + extra) bytecodes, for N bytes of op and its immediate operand.
275 */
276 extern ptrdiff_t
277 js_EmitN(JSContext *cx, JSCodeGenerator *cg, JSOp op, size_t extra);
279 /*
280 * Unsafe macro to call js_SetJumpOffset and return false if it does.
281 */
282 #define CHECK_AND_SET_JUMP_OFFSET(cx,cg,pc,off) \
283 JS_BEGIN_MACRO \
284 if (!js_SetJumpOffset(cx, cg, pc, off)) \
285 return JS_FALSE; \
286 JS_END_MACRO
288 #define CHECK_AND_SET_JUMP_OFFSET_AT(cx,cg,off) \
289 CHECK_AND_SET_JUMP_OFFSET(cx, cg, CG_CODE(cg,off), CG_OFFSET(cg) - (off))
291 extern JSBool
292 js_SetJumpOffset(JSContext *cx, JSCodeGenerator *cg, jsbytecode *pc,
293 ptrdiff_t off);
295 /* Test whether we're in a with statement. */
296 extern JSBool
297 js_InWithStatement(JSTreeContext *tc);
299 /* Test whether we're in a catch block with exception named by atom. */
300 extern JSBool
301 js_InCatchBlock(JSTreeContext *tc, JSAtom *atom);
303 /*
304 * Push the C-stack-allocated struct at stmt onto the stmtInfo stack.
305 */
306 extern void
307 js_PushStatement(JSTreeContext *tc, JSStmtInfo *stmt, JSStmtType type,
308 ptrdiff_t top);
310 /*
311 * Pop tc->topStmt. If the top JSStmtInfo struct is not stack-allocated, it
312 * is up to the caller to free it.
313 */
314 extern void
315 js_PopStatement(JSTreeContext *tc);
317 /*
318 * Like js_PopStatement(&cg->treeContext), also patch breaks and continues.
319 * May fail if a jump offset overflows.
320 */
321 extern JSBool
322 js_PopStatementCG(JSContext *cx, JSCodeGenerator *cg);
324 /*
325 * Emit code into cg for the tree rooted at pn.
326 */
327 extern JSBool
328 js_EmitTree(JSContext *cx, JSCodeGenerator *cg, JSParseNode *pn);
330 /*
331 * Emit code into cg for the tree rooted at body, then create a persistent
332 * script for fun from cg.
333 */
334 extern JSBool
335 js_EmitFunctionBody(JSContext *cx, JSCodeGenerator *cg, JSParseNode *body,
336 JSFunction *fun);
338 /*
339 * Source notes generated along with bytecode for decompiling and debugging.
340 * A source note is a uint8 with 5 bits of type and 3 of offset from the pc of
341 * the previous note. If 3 bits of offset aren't enough, extended delta notes
342 * (SRC_XDELTA) consisting of 2 set high order bits followed by 6 offset bits
343 * are emitted before the next note. Some notes have operand offsets encoded
344 * immediately after them, in note bytes or byte-triples.
345 *
346 * Source Note Extended Delta
347 * +7-6-5-4-3+2-1-0+ +7-6-5+4-3-2-1-0+
348 * |note-type|delta| |1 1| ext-delta |
349 * +---------+-----+ +---+-----------+
350 *
351 * At most one "gettable" note (i.e., a note of type other than SRC_NEWLINE,
352 * SRC_SETLINE, and SRC_XDELTA) applies to a given bytecode.
353 *
354 * NB: the js_SrcNoteSpec array in jsemit.c is indexed by this enum, so its
355 * initializers need to match the order here.
356 */
357 typedef enum JSSrcNoteType {
358 SRC_NULL = 0, /* terminates a note vector */
359 SRC_IF = 1, /* JSOP_IFEQ bytecode is from an if-then */
360 SRC_IF_ELSE = 2, /* JSOP_IFEQ bytecode is from an if-then-else */
361 SRC_WHILE = 3, /* JSOP_IFEQ is from a while loop */
362 SRC_FOR = 4, /* JSOP_NOP or JSOP_POP in for loop head */
363 SRC_CONTINUE = 5, /* JSOP_GOTO is a continue, not a break;
364 also used on JSOP_ENDINIT if extra comma
365 at end of array literal: [1,2,,] */
366 SRC_VAR = 6, /* JSOP_NAME/SETNAME/FORNAME in a var decl */
367 SRC_PCDELTA = 7, /* offset from comma-operator to next POP,
368 or from CONDSWITCH to first CASE opcode */
369 SRC_ASSIGNOP = 8, /* += or another assign-op follows */
370 SRC_COND = 9, /* JSOP_IFEQ is from conditional ?: operator */
371 SRC_RESERVED0 = 10, /* reserved for future use */
372 SRC_HIDDEN = 11, /* opcode shouldn't be decompiled */
373 SRC_PCBASE = 12, /* offset of first obj.prop.subprop bytecode */
374 SRC_LABEL = 13, /* JSOP_NOP for label: with atomid immediate */
375 SRC_LABELBRACE = 14, /* JSOP_NOP for label: {...} begin brace */
376 SRC_ENDBRACE = 15, /* JSOP_NOP for label: {...} end brace */
377 SRC_BREAK2LABEL = 16, /* JSOP_GOTO for 'break label' with atomid */
378 SRC_CONT2LABEL = 17, /* JSOP_GOTO for 'continue label' with atomid */
379 SRC_SWITCH = 18, /* JSOP_*SWITCH with offset to end of switch,
380 2nd off to first JSOP_CASE if condswitch */
381 SRC_FUNCDEF = 19, /* JSOP_NOP for function f() with atomid */
382 SRC_CATCH = 20, /* catch block has guard */
383 SRC_CONST = 21, /* JSOP_SETCONST in a const decl */
384 SRC_NEWLINE = 22, /* bytecode follows a source newline */
385 SRC_SETLINE = 23, /* a file-absolute source line number note */
386 SRC_XDELTA = 24 /* 24-31 are for extended delta notes */
387 } JSSrcNoteType;
389 #define SN_TYPE_BITS 5
390 #define SN_DELTA_BITS 3
391 #define SN_XDELTA_BITS 6
392 #define SN_TYPE_MASK (JS_BITMASK(SN_TYPE_BITS) << SN_DELTA_BITS)
393 #define SN_DELTA_MASK ((ptrdiff_t)JS_BITMASK(SN_DELTA_BITS))
394 #define SN_XDELTA_MASK ((ptrdiff_t)JS_BITMASK(SN_XDELTA_BITS))
396 #define SN_MAKE_NOTE(sn,t,d) (*(sn) = (jssrcnote) \
397 (((t) << SN_DELTA_BITS) \
398 | ((d) & SN_DELTA_MASK)))
399 #define SN_MAKE_XDELTA(sn,d) (*(sn) = (jssrcnote) \
400 ((SRC_XDELTA << SN_DELTA_BITS) \
401 | ((d) & SN_XDELTA_MASK)))
403 #define SN_IS_XDELTA(sn) ((*(sn) >> SN_DELTA_BITS) >= SRC_XDELTA)
404 #define SN_TYPE(sn) (SN_IS_XDELTA(sn) ? SRC_XDELTA \
405 : *(sn) >> SN_DELTA_BITS)
406 #define SN_SET_TYPE(sn,type) SN_MAKE_NOTE(sn, type, SN_DELTA(sn))
407 #define SN_IS_GETTABLE(sn) (SN_TYPE(sn) < SRC_NEWLINE)
409 #define SN_DELTA(sn) ((ptrdiff_t)(SN_IS_XDELTA(sn) \
410 ? *(sn) & SN_XDELTA_MASK \
411 : *(sn) & SN_DELTA_MASK))
412 #define SN_SET_DELTA(sn,delta) (SN_IS_XDELTA(sn) \
413 ? SN_MAKE_XDELTA(sn, delta) \
414 : SN_MAKE_NOTE(sn, SN_TYPE(sn), delta))
416 #define SN_DELTA_LIMIT ((ptrdiff_t)JS_BIT(SN_DELTA_BITS))
417 #define SN_XDELTA_LIMIT ((ptrdiff_t)JS_BIT(SN_XDELTA_BITS))
419 /*
420 * Offset fields follow certain notes and are frequency-encoded: an offset in
421 * [0,0x7f] consumes one byte, an offset in [0x80,0x7fffff] takes three, and
422 * the high bit of the first byte is set.
423 */
424 #define SN_3BYTE_OFFSET_FLAG 0x80
425 #define SN_3BYTE_OFFSET_MASK 0x7f
427 typedef struct JSSrcNoteSpec {
428 const char *name; /* name for disassembly/debugging output */
429 uint8 arity; /* number of offset operands */
430 uint8 offsetBias; /* bias of offset(s) from annotated pc */
431 int8 isSpanDep; /* 1 or -1 if offsets could span extended ops,
432 0 otherwise; sign tells span direction */
433 } JSSrcNoteSpec;
435 extern JS_FRIEND_DATA(JSSrcNoteSpec) js_SrcNoteSpec[];
436 extern JS_FRIEND_API(uintN) js_SrcNoteLength(jssrcnote *sn);
438 #define SN_LENGTH(sn) ((js_SrcNoteSpec[SN_TYPE(sn)].arity == 0) ? 1 \
439 : js_SrcNoteLength(sn))
440 #define SN_NEXT(sn) ((sn) + SN_LENGTH(sn))
442 /* A source note array is terminated by an all-zero element. */
443 #define SN_MAKE_TERMINATOR(sn) (*(sn) = SRC_NULL)
444 #define SN_IS_TERMINATOR(sn) (*(sn) == SRC_NULL)
446 /*
447 * Append a new source note of the given type (and therefore size) to cg's
448 * notes dynamic array, updating cg->noteCount. Return the new note's index
449 * within the array pointed at by cg->current->notes. Return -1 if out of
450 * memory.
451 */
452 extern intN
453 js_NewSrcNote(JSContext *cx, JSCodeGenerator *cg, JSSrcNoteType type);
455 extern intN
456 js_NewSrcNote2(JSContext *cx, JSCodeGenerator *cg, JSSrcNoteType type,
457 ptrdiff_t offset);
459 extern intN
460 js_NewSrcNote3(JSContext *cx, JSCodeGenerator *cg, JSSrcNoteType type,
461 ptrdiff_t offset1, ptrdiff_t offset2);
463 /*
464 * NB: this function can add at most one extra extended delta note.
465 */
466 extern jssrcnote *
467 js_AddToSrcNoteDelta(JSContext *cx, JSCodeGenerator *cg, jssrcnote *sn,
468 ptrdiff_t delta);
470 /*
471 * Get and set the offset operand identified by which (0 for the first, etc.).
472 */
473 extern JS_FRIEND_API(ptrdiff_t)
474 js_GetSrcNoteOffset(jssrcnote *sn, uintN which);
476 extern JSBool
477 js_SetSrcNoteOffset(JSContext *cx, JSCodeGenerator *cg, uintN index,
478 uintN which, ptrdiff_t offset);
480 /*
481 * Finish taking source notes in cx's notePool, copying final notes to the new
482 * stable store allocated by the caller and passed in via notes. Return false
483 * on malloc failure, which means this function reported an error.
484 *
485 * To compute the number of jssrcnotes to allocate and pass in via notes, use
486 * the CG_COUNT_FINAL_SRCNOTES macro. This macro knows a lot about details of
487 * js_FinishTakingSrcNotes, SO DON'T CHANGE jsemit.c's js_FinishTakingSrcNotes
488 * FUNCTION WITHOUT CHECKING WHETHER THIS MACRO NEEDS CORRESPONDING CHANGES!
489 */
490 #define CG_COUNT_FINAL_SRCNOTES(cg, cnt) \
491 JS_BEGIN_MACRO \
492 ptrdiff_t diff_ = CG_PROLOG_OFFSET(cg) - (cg)->prolog.lastNoteOffset; \
493 cnt = (cg)->prolog.noteCount + (cg)->main.noteCount + 1; \
494 if ((cg)->prolog.noteCount && \
495 (cg)->prolog.currentLine != (cg)->firstLine) { \
496 if (diff_ > SN_DELTA_MASK) \
497 cnt += JS_HOWMANY(diff_ - SN_DELTA_MASK, SN_XDELTA_MASK); \
498 cnt += 2 + (((cg)->firstLine > SN_3BYTE_OFFSET_MASK) << 1); \
499 } else if (diff_ > 0) { \
500 if (cg->main.noteCount) { \
501 jssrcnote *sn_ = (cg)->main.notes; \
502 diff_ -= SN_IS_XDELTA(sn_) \
503 ? SN_XDELTA_MASK - (*sn_ & SN_XDELTA_MASK) \
504 : SN_DELTA_MASK - (*sn_ & SN_DELTA_MASK); \
505 } \
506 if (diff_ > 0) \
507 cnt += JS_HOWMANY(diff_, SN_XDELTA_MASK); \
508 } \
509 JS_END_MACRO
511 extern JSBool
512 js_FinishTakingSrcNotes(JSContext *cx, JSCodeGenerator *cg, jssrcnote *notes);
514 /*
515 * Allocate cg->treeContext.tryCount notes (plus one for the end sentinel)
516 * from cx->tempPool and set up cg->tryBase/tryNext for exactly tryCount
517 * js_NewTryNote calls. The storage is freed by js_FinishCodeGenerator.
518 */
519 extern JSBool
520 js_AllocTryNotes(JSContext *cx, JSCodeGenerator *cg);
522 /*
523 * Grab the next trynote slot in cg, filling it in appropriately.
524 */
525 extern JSTryNote *
526 js_NewTryNote(JSContext *cx, JSCodeGenerator *cg, ptrdiff_t start,
527 ptrdiff_t end, ptrdiff_t catchStart);
529 /*
530 * Finish generating exception information into the space at notes. As with
531 * js_FinishTakingSrcNotes, the caller must use CG_COUNT_FINAL_TRYNOTES(cg) to
532 * preallocate enough space in a JSTryNote[] to pass as the notes parameter of
533 * js_FinishTakingTryNotes.
534 */
535 #define CG_COUNT_FINAL_TRYNOTES(cg, cnt) \
536 JS_BEGIN_MACRO \
537 cnt = ((cg)->tryNext > (cg)->tryBase) \
538 ? PTRDIFF(cg->tryNext, cg->tryBase, JSTryNote) + 1 \
539 : 0; \
540 JS_END_MACRO
542 extern void
543 js_FinishTakingTryNotes(JSContext *cx, JSCodeGenerator *cg, JSTryNote *notes);
545 JS_END_EXTERN_C
547 #endif /* jsemit_h___ */