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 ***** */ 39 40 #ifndef jsemit_h___ 41 #define jsemit_h___ 42 /* 43 * JS bytecode generation. 44 */ 45 46 #include "jsstddef.h" 47 #include "jstypes.h" 48 #include "jsatom.h" 49 #include "jsopcode.h" 50 #include "jsprvtd.h" 51 #include "jspubtd.h" 52 53 JS_BEGIN_EXTERN_C 54 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; 75 76 #define STMT_IS_LOOP(stmt) ((stmt)->type >= STMT_DO_LOOP) 77 78 typedef struct JSStmtInfo JSStmtInfo; 79 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 }; 90 91 #define SET_STATEMENT_TOP(stmt, top) \ 92 ((stmt)->update = (top), (stmt)->breaks = \ 93 (stmt)->continues = (stmt)->catchJump = (stmt)->gosub = (-1)) 94 95 struct JSTreeContext { /* tree context for semantic checks */ 96 uint16 flags; /* statement state flags, see below */ 97 uint16 numGlobalVars; /* max. no. of global variables/regexps */ 98 uint32 tryCount; /* total count of try statements parsed */ 99 uint32 globalUses; /* optimizable global var uses in total */ 100 uint32 loopyGlobalUses;/* optimizable global var uses in loops */ 101 JSStmtInfo *topStmt; /* top of statement info stack */ 102 JSAtomList decls; /* function, const, and var declarations */ 103 JSParseNode *nodeList; /* list of recyclable parse-node structs */ 104 JSTreeContext *down; /* info for enclosing tree context */ 105 }; 106 107 #define TCF_COMPILING 0x01 /* generating bytecode; this tc is a cg */ 108 #define TCF_IN_FUNCTION 0x02 /* parsing inside function body */ 109 #define TCF_RETURN_EXPR 0x04 /* function has 'return expr;' */ 110 #define TCF_RETURN_VOID 0x08 /* function has 'return;' */ 111 #define TCF_IN_FOR_INIT 0x10 /* parsing init expr of for; exclude 'in' */ 112 #define TCF_FUN_CLOSURE_VS_VAR 0x20 /* function and var with same name */ 113 #define TCF_FUN_USES_NONLOCALS 0x40 /* function refers to non-local names */ 114 #define TCF_FUN_HEAVYWEIGHT 0x80 /* function needs Call object per call */ 115 #define TCF_FUN_FLAGS 0xE0 /* flags to propagate from FunctionBody */ 116 #define TCF_IN_FOR_POST 0x100 /* parsing post expr of for (lint) */ 117 118 #define TREE_CONTEXT_INIT(tc) \ 119 ((tc)->flags = (tc)->numGlobalVars = 0, \ 120 (tc)->tryCount = (tc)->globalUses = (tc)->loopyGlobalUses = 0, \ 121 (tc)->topStmt = NULL, ATOM_LIST_INIT(&(tc)->decls), \ 122 (tc)->nodeList = NULL, (tc)->down = NULL) 123 124 #define TREE_CONTEXT_FINISH(tc) \ 125 ((void)0) 126 127 /* 128 * Span-dependent instructions are jumps whose span (from the jump bytecode to 129 * the jump target) may require 2 or 4 bytes of immediate operand. 130 */ 131 typedef struct JSSpanDep JSSpanDep; 132 typedef struct JSJumpTarget JSJumpTarget; 133 134 struct JSSpanDep { 135 ptrdiff_t top; /* offset of first bytecode in an opcode */ 136 ptrdiff_t offset; /* offset - 1 within opcode of jump operand */ 137 ptrdiff_t before; /* original offset - 1 of jump operand */ 138 JSJumpTarget *target; /* tagged target pointer or backpatch delta */ 139 }; 140 141 /* 142 * Jump targets are stored in an AVL tree, for O(log(n)) lookup with targets 143 * sorted by offset from left to right, so that targets after a span-dependent 144 * instruction whose jump offset operand must be extended can be found quickly 145 * and adjusted upward (toward higher offsets). 146 */ 147 struct JSJumpTarget { 148 ptrdiff_t offset; /* offset of span-dependent jump target */ 149 int balance; /* AVL tree balance number */ 150 JSJumpTarget *kids[2]; /* left and right AVL tree child pointers */ 151 }; 152 153 #define JT_LEFT 0 154 #define JT_RIGHT 1 155 #define JT_OTHER_DIR(dir) (1 - (dir)) 156 #define JT_IMBALANCE(dir) (((dir) << 1) - 1) 157 #define JT_DIR(imbalance) (((imbalance) + 1) >> 1) 158 159 /* 160 * Backpatch deltas are encoded in JSSpanDep.target if JT_TAG_BIT is clear, 161 * so we can maintain backpatch chains when using span dependency records to 162 * hold jump offsets that overflow 16 bits. 163 */ 164 #define JT_TAG_BIT ((jsword) 1) 165 #define JT_UNTAG_SHIFT 1 166 #define JT_SET_TAG(jt) ((JSJumpTarget *)((jsword)(jt) | JT_TAG_BIT)) 167 #define JT_CLR_TAG(jt) ((JSJumpTarget *)((jsword)(jt) & ~JT_TAG_BIT)) 168 #define JT_HAS_TAG(jt) ((jsword)(jt) & JT_TAG_BIT) 169 170 #define BITS_PER_PTRDIFF (sizeof(ptrdiff_t) * JS_BITS_PER_BYTE) 171 #define BITS_PER_BPDELTA (BITS_PER_PTRDIFF - 1 - JT_UNTAG_SHIFT) 172 #define BPDELTA_MAX (((ptrdiff_t)1 << BITS_PER_BPDELTA) - 1) 173 #define BPDELTA_TO_JT(bp) ((JSJumpTarget *)((bp) << JT_UNTAG_SHIFT)) 174 #define JT_TO_BPDELTA(jt) ((ptrdiff_t)((jsword)(jt) >> JT_UNTAG_SHIFT)) 175 176 #define SD_SET_TARGET(sd,jt) ((sd)->target = JT_SET_TAG(jt)) 177 #define SD_SET_BPDELTA(sd,bp) ((sd)->target = BPDELTA_TO_JT(bp)) 178 #define SD_GET_BPDELTA(sd) (JS_ASSERT(!JT_HAS_TAG((sd)->target)), \ 179 JT_TO_BPDELTA((sd)->target)) 180 #define SD_TARGET_OFFSET(sd) (JS_ASSERT(JT_HAS_TAG((sd)->target)), \ 181 JT_CLR_TAG((sd)->target)->offset) 182 183 struct JSCodeGenerator { 184 JSTreeContext treeContext; /* base state: statement info stack, etc. */ 185 JSArenaPool *codePool; /* pointer to thread code arena pool */ 186 JSArenaPool *notePool; /* pointer to thread srcnote arena pool */ 187 void *codeMark; /* low watermark in cg->codePool */ 188 void *noteMark; /* low watermark in cg->notePool */ 189 void *tempMark; /* low watermark in cx->tempPool */ 190 struct { 191 jsbytecode *base; /* base of JS bytecode vector */ 192 jsbytecode *limit; /* one byte beyond end of bytecode */ 193 jsbytecode *next; /* pointer to next free bytecode */ 194 jssrcnote *notes; /* source notes, see below */ 195 uintN noteCount; /* number of source notes so far */ 196 uintN noteMask; /* growth increment for notes */ 197 ptrdiff_t lastNoteOffset; /* code offset for last source note */ 198 uintN currentLine; /* line number for tree-based srcnote gen */ 199 } prolog, main, *current; 200 const char *filename; /* null or weak link to source filename */ 201 uintN firstLine; /* first line, for js_NewScriptFromCG */ 202 JSPrincipals *principals; /* principals for constant folding eval */ 203 JSAtomList atomList; /* literals indexed for mapping */ 204 intN stackDepth; /* current stack depth in script frame */ 205 uintN maxStackDepth; /* maximum stack depth so far */ 206 JSTryNote *tryBase; /* first exception handling note */ 207 JSTryNote *tryNext; /* next available note */ 208 size_t tryNoteSpace; /* # of bytes allocated at tryBase */ 209 JSSpanDep *spanDeps; /* span dependent instruction records */ 210 JSJumpTarget *jumpTargets; /* AVL tree of jump target offsets */ 211 JSJumpTarget *jtFreeList; /* JT_LEFT-linked list of free structs */ 212 uintN numSpanDeps; /* number of span dependencies */ 213 uintN numJumpTargets; /* number of jump targets */ 214 uintN emitLevel; /* js_EmitTree recursion level */ 215 JSAtomList constList; /* compile time constants */ 216 JSCodeGenerator *parent; /* Enclosing function or global context */ 217 }; 218 219 #define CG_BASE(cg) ((cg)->current->base) 220 #define CG_LIMIT(cg) ((cg)->current->limit) 221 #define CG_NEXT(cg) ((cg)->current->next) 222 #define CG_CODE(cg,offset) (CG_BASE(cg) + (offset)) 223 #define CG_OFFSET(cg) PTRDIFF(CG_NEXT(cg), CG_BASE(cg), jsbytecode) 224 225 #define CG_NOTES(cg) ((cg)->current->notes) 226 #define CG_NOTE_COUNT(cg) ((cg)->current->noteCount) 227 #define CG_NOTE_MASK(cg) ((cg)->current->noteMask) 228 #define CG_LAST_NOTE_OFFSET(cg) ((cg)->current->lastNoteOffset) 229 #define CG_CURRENT_LINE(cg) ((cg)->current->currentLine) 230 231 #define CG_PROLOG_BASE(cg) ((cg)->prolog.base) 232 #define CG_PROLOG_LIMIT(cg) ((cg)->prolog.limit) 233 #define CG_PROLOG_NEXT(cg) ((cg)->prolog.next) 234 #define CG_PROLOG_CODE(cg,poff) (CG_PROLOG_BASE(cg) + (poff)) 235 #define CG_PROLOG_OFFSET(cg) PTRDIFF(CG_PROLOG_NEXT(cg), CG_PROLOG_BASE(cg),\ 236 jsbytecode) 237 238 #define CG_SWITCH_TO_MAIN(cg) ((cg)->current = &(cg)->main) 239 #define CG_SWITCH_TO_PROLOG(cg) ((cg)->current = &(cg)->prolog) 240 241 /* 242 * Initialize cg to allocate bytecode space from codePool, source note space 243 * from notePool, and all other arena-allocated temporaries from cx->tempPool. 244 * Return true on success. Report an error and return false if the initial 245 * code segment can't be allocated. 246 */ 247 extern JS_FRIEND_API(JSBool) 248 js_InitCodeGenerator(JSContext *cx, JSCodeGenerator *cg, 249 JSArenaPool *codePool, JSArenaPool *notePool, 250 const char *filename, uintN lineno, 251 JSPrincipals *principals); 252 253 /* 254 * Release cg->codePool, cg->notePool, and cx->tempPool to marks set by 255 * js_InitCodeGenerator. Note that cgs are magic: they own the arena pool 256 * "tops-of-stack" space above their codeMark, noteMark, and tempMark points. 257 * This means you cannot alloc from tempPool and save the pointer beyond the 258 * next JS_FinishCodeGenerator. 259 */ 260 extern JS_FRIEND_API(void) 261 js_FinishCodeGenerator(JSContext *cx, JSCodeGenerator *cg); 262 263 /* 264 * Emit one bytecode. 265 */ 266 extern ptrdiff_t 267 js_Emit1(JSContext *cx, JSCodeGenerator *cg, JSOp op); 268 269 /* 270 * Emit two bytecodes, an opcode (op) with a byte of immediate operand (op1). 271 */ 272 extern ptrdiff_t 273 js_Emit2(JSContext *cx, JSCodeGenerator *cg, JSOp op, jsbytecode op1); 274 275 /* 276 * Emit three bytecodes, an opcode with two bytes of immediate operands. 277 */ 278 extern ptrdiff_t 279 js_Emit3(JSContext *cx, JSCodeGenerator *cg, JSOp op, jsbytecode op1, 280 jsbytecode op2); 281 282 /* 283 * Emit (1 + extra) bytecodes, for N bytes of op and its immediate operand. 284 */ 285 extern ptrdiff_t 286 js_EmitN(JSContext *cx, JSCodeGenerator *cg, JSOp op, size_t extra); 287 288 /* 289 * Unsafe macro to call js_SetJumpOffset and return false if it does. 290 */ 291 #define CHECK_AND_SET_JUMP_OFFSET(cx,cg,pc,off) \ 292 JS_BEGIN_MACRO \ 293 if (!js_SetJumpOffset(cx, cg, pc, off)) \ 294 return JS_FALSE; \ 295 JS_END_MACRO 296 297 #define CHECK_AND_SET_JUMP_OFFSET_AT(cx,cg,off) \ 298 CHECK_AND_SET_JUMP_OFFSET(cx, cg, CG_CODE(cg,off), CG_OFFSET(cg) - (off)) 299 300 extern JSBool 301 js_SetJumpOffset(JSContext *cx, JSCodeGenerator *cg, jsbytecode *pc, 302 ptrdiff_t off); 303 304 /* Test whether we're in a with statement. */ 305 extern JSBool 306 js_InWithStatement(JSTreeContext *tc); 307 308 /* Test whether we're in a catch block with exception named by atom. */ 309 extern JSBool 310 js_InCatchBlock(JSTreeContext *tc, JSAtom *atom); 311 312 /* 313 * Push the C-stack-allocated struct at stmt onto the stmtInfo stack. 314 */ 315 extern void 316 js_PushStatement(JSTreeContext *tc, JSStmtInfo *stmt, JSStmtType type, 317 ptrdiff_t top); 318 319 /* 320 * Pop tc->topStmt. If the top JSStmtInfo struct is not stack-allocated, it 321 * is up to the caller to free it. 322 */ 323 extern void 324 js_PopStatement(JSTreeContext *tc); 325 326 /* 327 * Like js_PopStatement(&cg->treeContext), also patch breaks and continues. 328 * May fail if a jump offset overflows. 329 */ 330 extern JSBool 331 js_PopStatementCG(JSContext *cx, JSCodeGenerator *cg); 332 333 /* 334 * Define and lookup a primitive jsval associated with the const named by atom. 335 * js_DefineCompileTimeConstant analyzes the constant-folded initializer at pn 336 * and saves the const's value in cg->constList, if it can be used at compile 337 * time. It returns true unless an error occurred. 338 * 339 * If the initializer's value could not be saved, js_LookupCompileTimeConstant 340 * calls will return the undefined value. js_LookupCompileTimeConstant tries 341 * to find a const value memorized for atom, returning true with *vp set to a 342 * value other than undefined if the constant was found, true with *vp set to 343 * JSVAL_VOID if not found, and false on error. 344 */ 345 extern JSBool 346 js_DefineCompileTimeConstant(JSContext *cx, JSCodeGenerator *cg, JSAtom *atom, 347 JSParseNode *pn); 348 349 extern JSBool 350 js_LookupCompileTimeConstant(JSContext *cx, JSCodeGenerator *cg, JSAtom *atom, 351 jsval *vp); 352 353 /* 354 * Emit code into cg for the tree rooted at pn. 355 */ 356 extern JSBool 357 js_EmitTree(JSContext *cx, JSCodeGenerator *cg, JSParseNode *pn); 358 359 /* 360 * Emit code into cg for the tree rooted at body, then create a persistent 361 * script for fun from cg. 362 */ 363 extern JSBool 364 js_EmitFunctionBody(JSContext *cx, JSCodeGenerator *cg, JSParseNode *body, 365 JSFunction *fun); 366 367 /* 368 * Source notes generated along with bytecode for decompiling and debugging. 369 * A source note is a uint8 with 5 bits of type and 3 of offset from the pc of 370 * the previous note. If 3 bits of offset aren't enough, extended delta notes 371 * (SRC_XDELTA) consisting of 2 set high order bits followed by 6 offset bits 372 * are emitted before the next note. Some notes have operand offsets encoded 373 * immediately after them, in note bytes or byte-triples. 374 * 375 * Source Note Extended Delta 376 * +7-6-5-4-3+2-1-0+ +7-6-5+4-3-2-1-0+ 377 * |note-type|delta| |1 1| ext-delta | 378 * +---------+-----+ +---+-----------+ 379 * 380 * At most one "gettable" note (i.e., a note of type other than SRC_NEWLINE, 381 * SRC_SETLINE, and SRC_XDELTA) applies to a given bytecode. 382 * 383 * NB: the js_SrcNoteSpec array in jsemit.c is indexed by this enum, so its 384 * initializers need to match the order here. 385 */ 386 typedef enum JSSrcNoteType { 387 SRC_NULL = 0, /* terminates a note vector */ 388 SRC_IF = 1, /* JSOP_IFEQ bytecode is from an if-then */ 389 SRC_IF_ELSE = 2, /* JSOP_IFEQ bytecode is from an if-then-else */ 390 SRC_WHILE = 3, /* JSOP_IFEQ is from a while loop */ 391 SRC_FOR = 4, /* JSOP_NOP or JSOP_POP in for loop head */ 392 SRC_CONTINUE = 5, /* JSOP_GOTO is a continue, not a break; 393 also used on JSOP_ENDINIT if extra comma 394 at end of array literal: [1,2,,] */ 395 SRC_VAR = 6, /* JSOP_NAME/SETNAME/FORNAME in a var decl */ 396 SRC_PCDELTA = 7, /* offset from comma-operator to next POP, 397 or from CONDSWITCH to first CASE opcode */ 398 SRC_ASSIGNOP = 8, /* += or another assign-op follows */ 399 SRC_COND = 9, /* JSOP_IFEQ is from conditional ?: operator */ 400 SRC_RESERVED0 = 10, /* reserved for future use */ 401 SRC_HIDDEN = 11, /* opcode shouldn't be decompiled */ 402 SRC_PCBASE = 12, /* offset of first obj.prop.subprop bytecode */ 403 SRC_LABEL = 13, /* JSOP_NOP for label: with atomid immediate */ 404 SRC_LABELBRACE = 14, /* JSOP_NOP for label: {...} begin brace */ 405 SRC_ENDBRACE = 15, /* JSOP_NOP for label: {...} end brace */ 406 SRC_BREAK2LABEL = 16, /* JSOP_GOTO for 'break label' with atomid */ 407 SRC_CONT2LABEL = 17, /* JSOP_GOTO for 'continue label' with atomid */ 408 SRC_SWITCH = 18, /* JSOP_*SWITCH with offset to end of switch, 409 2nd off to first JSOP_CASE if condswitch */ 410 SRC_FUNCDEF = 19, /* JSOP_NOP for function f() with atomid */ 411 SRC_CATCH = 20, /* catch block has guard */ 412 SRC_CONST = 21, /* JSOP_SETCONST in a const decl */ 413 SRC_NEWLINE = 22, /* bytecode follows a source newline */ 414 SRC_SETLINE = 23, /* a file-absolute source line number note */ 415 SRC_XDELTA = 24 /* 24-31 are for extended delta notes */ 416 } JSSrcNoteType; 417 418 #define SN_TYPE_BITS 5 419 #define SN_DELTA_BITS 3 420 #define SN_XDELTA_BITS 6 421 #define SN_TYPE_MASK (JS_BITMASK(SN_TYPE_BITS) << SN_DELTA_BITS) 422 #define SN_DELTA_MASK ((ptrdiff_t)JS_BITMASK(SN_DELTA_BITS)) 423 #define SN_XDELTA_MASK ((ptrdiff_t)JS_BITMASK(SN_XDELTA_BITS)) 424 425 #define SN_MAKE_NOTE(sn,t,d) (*(sn) = (jssrcnote) \ 426 (((t) << SN_DELTA_BITS) \ 427 | ((d) & SN_DELTA_MASK))) 428 #define SN_MAKE_XDELTA(sn,d) (*(sn) = (jssrcnote) \ 429 ((SRC_XDELTA << SN_DELTA_BITS) \ 430 | ((d) & SN_XDELTA_MASK))) 431 432 #define SN_IS_XDELTA(sn) ((*(sn) >> SN_DELTA_BITS) >= SRC_XDELTA) 433 #define SN_TYPE(sn) (SN_IS_XDELTA(sn) ? SRC_XDELTA \ 434 : *(sn) >> SN_DELTA_BITS) 435 #define SN_SET_TYPE(sn,type) SN_MAKE_NOTE(sn, type, SN_DELTA(sn)) 436 #define SN_IS_GETTABLE(sn) (SN_TYPE(sn) < SRC_NEWLINE) 437 438 #define SN_DELTA(sn) ((ptrdiff_t)(SN_IS_XDELTA(sn) \ 439 ? *(sn) & SN_XDELTA_MASK \ 440 : *(sn) & SN_DELTA_MASK)) 441 #define SN_SET_DELTA(sn,delta) (SN_IS_XDELTA(sn) \ 442 ? SN_MAKE_XDELTA(sn, delta) \ 443 : SN_MAKE_NOTE(sn, SN_TYPE(sn), delta)) 444 445 #define SN_DELTA_LIMIT ((ptrdiff_t)JS_BIT(SN_DELTA_BITS)) 446 #define SN_XDELTA_LIMIT ((ptrdiff_t)JS_BIT(SN_XDELTA_BITS)) 447 448 /* 449 * Offset fields follow certain notes and are frequency-encoded: an offset in 450 * [0,0x7f] consumes one byte, an offset in [0x80,0x7fffff] takes three, and 451 * the high bit of the first byte is set. 452 */ 453 #define SN_3BYTE_OFFSET_FLAG 0x80 454 #define SN_3BYTE_OFFSET_MASK 0x7f 455 456 typedef struct JSSrcNoteSpec { 457 const char *name; /* name for disassembly/debugging output */ 458 uint8 arity; /* number of offset operands */ 459 uint8 offsetBias; /* bias of offset(s) from annotated pc */ 460 int8 isSpanDep; /* 1 or -1 if offsets could span extended ops, 461 0 otherwise; sign tells span direction */ 462 } JSSrcNoteSpec; 463 464 extern JS_FRIEND_DATA(JSSrcNoteSpec) js_SrcNoteSpec[]; 465 extern JS_FRIEND_API(uintN) js_SrcNoteLength(jssrcnote *sn); 466 467 #define SN_LENGTH(sn) ((js_SrcNoteSpec[SN_TYPE(sn)].arity == 0) ? 1 \ 468 : js_SrcNoteLength(sn)) 469 #define SN_NEXT(sn) ((sn) + SN_LENGTH(sn)) 470 471 /* A source note array is terminated by an all-zero element. */ 472 #define SN_MAKE_TERMINATOR(sn) (*(sn) = SRC_NULL) 473 #define SN_IS_TERMINATOR(sn) (*(sn) == SRC_NULL) 474 475 /* 476 * Append a new source note of the given type (and therefore size) to cg's 477 * notes dynamic array, updating cg->noteCount. Return the new note's index 478 * within the array pointed at by cg->current->notes. Return -1 if out of 479 * memory. 480 */ 481 extern intN 482 js_NewSrcNote(JSContext *cx, JSCodeGenerator *cg, JSSrcNoteType type); 483 484 extern intN 485 js_NewSrcNote2(JSContext *cx, JSCodeGenerator *cg, JSSrcNoteType type, 486 ptrdiff_t offset); 487 488 extern intN 489 js_NewSrcNote3(JSContext *cx, JSCodeGenerator *cg, JSSrcNoteType type, 490 ptrdiff_t offset1, ptrdiff_t offset2); 491 492 /* 493 * NB: this function can add at most one extra extended delta note. 494 */ 495 extern jssrcnote * 496 js_AddToSrcNoteDelta(JSContext *cx, JSCodeGenerator *cg, jssrcnote *sn, 497 ptrdiff_t delta); 498 499 /* 500 * Get and set the offset operand identified by which (0 for the first, etc.). 501 */ 502 extern JS_FRIEND_API(ptrdiff_t) 503 js_GetSrcNoteOffset(jssrcnote *sn, uintN which); 504 505 extern JSBool 506 js_SetSrcNoteOffset(JSContext *cx, JSCodeGenerator *cg, uintN index, 507 uintN which, ptrdiff_t offset); 508 509 /* 510 * Finish taking source notes in cx's notePool, copying final notes to the new 511 * stable store allocated by the caller and passed in via notes. Return false 512 * on malloc failure, which means this function reported an error. 513 * 514 * To compute the number of jssrcnotes to allocate and pass in via notes, use 515 * the CG_COUNT_FINAL_SRCNOTES macro. This macro knows a lot about details of 516 * js_FinishTakingSrcNotes, SO DON'T CHANGE jsemit.c's js_FinishTakingSrcNotes 517 * FUNCTION WITHOUT CHECKING WHETHER THIS MACRO NEEDS CORRESPONDING CHANGES! 518 */ 519 #define CG_COUNT_FINAL_SRCNOTES(cg, cnt) \ 520 JS_BEGIN_MACRO \ 521 ptrdiff_t diff_ = CG_PROLOG_OFFSET(cg) - (cg)->prolog.lastNoteOffset; \ 522 cnt = (cg)->prolog.noteCount + (cg)->main.noteCount + 1; \ 523 if ((cg)->prolog.noteCount && \ 524 (cg)->prolog.currentLine != (cg)->firstLine) { \ 525 if (diff_ > SN_DELTA_MASK) \ 526 cnt += JS_HOWMANY(diff_ - SN_DELTA_MASK, SN_XDELTA_MASK); \ 527 cnt += 2 + (((cg)->firstLine > SN_3BYTE_OFFSET_MASK) << 1); \ 528 } else if (diff_ > 0) { \ 529 if (cg->main.noteCount) { \ 530 jssrcnote *sn_ = (cg)->main.notes; \ 531 diff_ -= SN_IS_XDELTA(sn_) \ 532 ? SN_XDELTA_MASK - (*sn_ & SN_XDELTA_MASK) \ 533 : SN_DELTA_MASK - (*sn_ & SN_DELTA_MASK); \ 534 } \ 535 if (diff_ > 0) \ 536 cnt += JS_HOWMANY(diff_, SN_XDELTA_MASK); \ 537 } \ 538 JS_END_MACRO 539 540 extern JSBool 541 js_FinishTakingSrcNotes(JSContext *cx, JSCodeGenerator *cg, jssrcnote *notes); 542 543 /* 544 * Allocate cg->treeContext.tryCount notes (plus one for the end sentinel) 545 * from cx->tempPool and set up cg->tryBase/tryNext for exactly tryCount 546 * js_NewTryNote calls. The storage is freed by js_FinishCodeGenerator. 547 */ 548 extern JSBool 549 js_AllocTryNotes(JSContext *cx, JSCodeGenerator *cg); 550 551 /* 552 * Grab the next trynote slot in cg, filling it in appropriately. 553 */ 554 extern JSTryNote * 555 js_NewTryNote(JSContext *cx, JSCodeGenerator *cg, ptrdiff_t start, 556 ptrdiff_t end, ptrdiff_t catchStart); 557 558 /* 559 * Finish generating exception information into the space at notes. As with 560 * js_FinishTakingSrcNotes, the caller must use CG_COUNT_FINAL_TRYNOTES(cg) to 561 * preallocate enough space in a JSTryNote[] to pass as the notes parameter of 562 * js_FinishTakingTryNotes. 563 */ 564 #define CG_COUNT_FINAL_TRYNOTES(cg, cnt) \ 565 JS_BEGIN_MACRO \ 566 cnt = ((cg)->tryNext > (cg)->tryBase) \ 567 ? PTRDIFF(cg->tryNext, cg->tryBase, JSTryNote) + 1 \ 568 : 0; \ 569 JS_END_MACRO 570 571 extern void 572 js_FinishTakingTryNotes(JSContext *cx, JSCodeGenerator *cg, JSTryNote *notes); 573 574 JS_END_EXTERN_C 575 576 #endif /* jsemit_h___ */ 577