Annotation of sbbs/include/mozilla/js/jsemit.h, revision 1.1.1.2

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

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.