Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
michael@0 | 2 | * vim: set ts=8 sts=4 et sw=4 tw=99: |
michael@0 | 3 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #ifndef frontend_SourceNotes_h |
michael@0 | 8 | #define frontend_SourceNotes_h |
michael@0 | 9 | |
michael@0 | 10 | #include <stdint.h> |
michael@0 | 11 | |
michael@0 | 12 | #include "jstypes.h" |
michael@0 | 13 | |
michael@0 | 14 | typedef uint8_t jssrcnote; |
michael@0 | 15 | |
michael@0 | 16 | namespace js { |
michael@0 | 17 | |
michael@0 | 18 | /* |
michael@0 | 19 | * Source notes generated along with bytecode for decompiling and debugging. |
michael@0 | 20 | * A source note is a uint8_t with 5 bits of type and 3 of offset from the pc |
michael@0 | 21 | * of the previous note. If 3 bits of offset aren't enough, extended delta |
michael@0 | 22 | * notes (SRC_XDELTA) consisting of 2 set high order bits followed by 6 offset |
michael@0 | 23 | * bits are emitted before the next note. Some notes have operand offsets |
michael@0 | 24 | * encoded immediately after them, in note bytes or byte-triples. |
michael@0 | 25 | * |
michael@0 | 26 | * Source Note Extended Delta |
michael@0 | 27 | * +7-6-5-4-3+2-1-0+ +7-6-5+4-3-2-1-0+ |
michael@0 | 28 | * |note-type|delta| |1 1| ext-delta | |
michael@0 | 29 | * +---------+-----+ +---+-----------+ |
michael@0 | 30 | * |
michael@0 | 31 | * At most one "gettable" note (i.e., a note of type other than SRC_NEWLINE, |
michael@0 | 32 | * SRC_COLSPAN, SRC_SETLINE, and SRC_XDELTA) applies to a given bytecode. |
michael@0 | 33 | * |
michael@0 | 34 | * NB: the js_SrcNoteSpec array in BytecodeEmitter.cpp is indexed by this |
michael@0 | 35 | * enum, so its initializers need to match the order here. |
michael@0 | 36 | * |
michael@0 | 37 | * Don't forget to update XDR_BYTECODE_VERSION in vm/Xdr.h for all such |
michael@0 | 38 | * incompatible source note or other bytecode changes. |
michael@0 | 39 | */ |
michael@0 | 40 | #define FOR_EACH_SRC_NOTE_TYPE(M) \ |
michael@0 | 41 | M(SRC_NULL, "null", 0) /* Terminates a note vector. */ \ |
michael@0 | 42 | M(SRC_IF, "if", 0) /* JSOP_IFEQ bytecode is from an if-then. */ \ |
michael@0 | 43 | M(SRC_IF_ELSE, "if-else", 1) /* JSOP_IFEQ bytecode is from an if-then-else. */ \ |
michael@0 | 44 | M(SRC_COND, "cond", 1) /* JSOP_IFEQ is from conditional ?: operator. */ \ |
michael@0 | 45 | M(SRC_FOR, "for", 3) /* JSOP_NOP or JSOP_POP in for(;;) loop head. */ \ |
michael@0 | 46 | M(SRC_WHILE, "while", 1) /* JSOP_GOTO to for or while loop condition from before \ |
michael@0 | 47 | loop, else JSOP_NOP at top of do-while loop. */ \ |
michael@0 | 48 | M(SRC_FOR_IN, "for-in", 1) /* JSOP_GOTO to for-in loop condition from before \ |
michael@0 | 49 | loop. */ \ |
michael@0 | 50 | M(SRC_FOR_OF, "for-of", 1) /* JSOP_GOTO to for-of loop condition from before \ |
michael@0 | 51 | loop. */ \ |
michael@0 | 52 | M(SRC_CONTINUE, "continue", 0) /* JSOP_GOTO is a continue. */ \ |
michael@0 | 53 | M(SRC_BREAK, "break", 0) /* JSOP_GOTO is a break. */ \ |
michael@0 | 54 | M(SRC_BREAK2LABEL, "break2label", 0) /* JSOP_GOTO for 'break label'. */ \ |
michael@0 | 55 | M(SRC_SWITCHBREAK, "switchbreak", 0) /* JSOP_GOTO is a break in a switch. */ \ |
michael@0 | 56 | M(SRC_TABLESWITCH, "tableswitch", 1) /* JSOP_TABLESWITCH; offset points to end of switch. */ \ |
michael@0 | 57 | M(SRC_CONDSWITCH, "condswitch", 2) /* JSOP_CONDSWITCH; 1st offset points to end of switch, \ |
michael@0 | 58 | 2nd points to first JSOP_CASE. */ \ |
michael@0 | 59 | M(SRC_NEXTCASE, "nextcase", 1) /* Distance forward from one CASE in a CONDSWITCH to \ |
michael@0 | 60 | the next. */ \ |
michael@0 | 61 | M(SRC_ASSIGNOP, "assignop", 0) /* += or another assign-op follows. */ \ |
michael@0 | 62 | M(SRC_TRY, "try", 1) /* JSOP_TRY, offset points to goto at the end of the \ |
michael@0 | 63 | try block. */ \ |
michael@0 | 64 | /* All notes below here are "gettable". See SN_IS_GETTABLE below. */ \ |
michael@0 | 65 | M(SRC_COLSPAN, "colspan", 1) /* Number of columns this opcode spans. */ \ |
michael@0 | 66 | M(SRC_NEWLINE, "newline", 0) /* Bytecode follows a source newline. */ \ |
michael@0 | 67 | M(SRC_SETLINE, "setline", 1) /* A file-absolute source line number note. */ \ |
michael@0 | 68 | M(SRC_UNUSED20, "unused20", 0) /* Unused. */ \ |
michael@0 | 69 | M(SRC_UNUSED21, "unused21", 0) /* Unused. */ \ |
michael@0 | 70 | M(SRC_UNUSED22, "unused22", 0) /* Unused. */ \ |
michael@0 | 71 | M(SRC_UNUSED23, "unused23", 0) /* Unused. */ \ |
michael@0 | 72 | M(SRC_XDELTA, "xdelta", 0) /* 24-31 are for extended delta notes. */ |
michael@0 | 73 | |
michael@0 | 74 | enum SrcNoteType { |
michael@0 | 75 | #define DEFINE_SRC_NOTE_TYPE(sym, name, arity) sym, |
michael@0 | 76 | FOR_EACH_SRC_NOTE_TYPE(DEFINE_SRC_NOTE_TYPE) |
michael@0 | 77 | #undef DEFINE_SRC_NOTE_TYPE |
michael@0 | 78 | |
michael@0 | 79 | SRC_LAST, |
michael@0 | 80 | SRC_LAST_GETTABLE = SRC_TRY |
michael@0 | 81 | }; |
michael@0 | 82 | |
michael@0 | 83 | static_assert(SRC_XDELTA == 24, "SRC_XDELTA should be 24"); |
michael@0 | 84 | |
michael@0 | 85 | /* A source note array is terminated by an all-zero element. */ |
michael@0 | 86 | inline void |
michael@0 | 87 | SN_MAKE_TERMINATOR(jssrcnote *sn) |
michael@0 | 88 | { |
michael@0 | 89 | *sn = SRC_NULL; |
michael@0 | 90 | } |
michael@0 | 91 | |
michael@0 | 92 | inline bool |
michael@0 | 93 | SN_IS_TERMINATOR(jssrcnote *sn) |
michael@0 | 94 | { |
michael@0 | 95 | return *sn == SRC_NULL; |
michael@0 | 96 | } |
michael@0 | 97 | |
michael@0 | 98 | } // namespace js |
michael@0 | 99 | |
michael@0 | 100 | #define SN_TYPE_BITS 5 |
michael@0 | 101 | #define SN_DELTA_BITS 3 |
michael@0 | 102 | #define SN_XDELTA_BITS 6 |
michael@0 | 103 | #define SN_TYPE_MASK (JS_BITMASK(SN_TYPE_BITS) << SN_DELTA_BITS) |
michael@0 | 104 | #define SN_DELTA_MASK ((ptrdiff_t)JS_BITMASK(SN_DELTA_BITS)) |
michael@0 | 105 | #define SN_XDELTA_MASK ((ptrdiff_t)JS_BITMASK(SN_XDELTA_BITS)) |
michael@0 | 106 | |
michael@0 | 107 | #define SN_MAKE_NOTE(sn,t,d) (*(sn) = (jssrcnote) \ |
michael@0 | 108 | (((t) << SN_DELTA_BITS) \ |
michael@0 | 109 | | ((d) & SN_DELTA_MASK))) |
michael@0 | 110 | #define SN_MAKE_XDELTA(sn,d) (*(sn) = (jssrcnote) \ |
michael@0 | 111 | ((SRC_XDELTA << SN_DELTA_BITS) \ |
michael@0 | 112 | | ((d) & SN_XDELTA_MASK))) |
michael@0 | 113 | |
michael@0 | 114 | #define SN_IS_XDELTA(sn) ((*(sn) >> SN_DELTA_BITS) >= SRC_XDELTA) |
michael@0 | 115 | #define SN_TYPE(sn) ((js::SrcNoteType)(SN_IS_XDELTA(sn) \ |
michael@0 | 116 | ? SRC_XDELTA \ |
michael@0 | 117 | : *(sn) >> SN_DELTA_BITS)) |
michael@0 | 118 | #define SN_SET_TYPE(sn,type) SN_MAKE_NOTE(sn, type, SN_DELTA(sn)) |
michael@0 | 119 | #define SN_IS_GETTABLE(sn) (SN_TYPE(sn) <= SRC_LAST_GETTABLE) |
michael@0 | 120 | |
michael@0 | 121 | #define SN_DELTA(sn) ((ptrdiff_t)(SN_IS_XDELTA(sn) \ |
michael@0 | 122 | ? *(sn) & SN_XDELTA_MASK \ |
michael@0 | 123 | : *(sn) & SN_DELTA_MASK)) |
michael@0 | 124 | #define SN_SET_DELTA(sn,delta) (SN_IS_XDELTA(sn) \ |
michael@0 | 125 | ? SN_MAKE_XDELTA(sn, delta) \ |
michael@0 | 126 | : SN_MAKE_NOTE(sn, SN_TYPE(sn), delta)) |
michael@0 | 127 | |
michael@0 | 128 | #define SN_DELTA_LIMIT ((ptrdiff_t)JS_BIT(SN_DELTA_BITS)) |
michael@0 | 129 | #define SN_XDELTA_LIMIT ((ptrdiff_t)JS_BIT(SN_XDELTA_BITS)) |
michael@0 | 130 | |
michael@0 | 131 | /* |
michael@0 | 132 | * Offset fields follow certain notes and are frequency-encoded: an offset in |
michael@0 | 133 | * [0,0x7f] consumes one byte, an offset in [0x80,0x7fffffff] takes four, and |
michael@0 | 134 | * the high bit of the first byte is set. |
michael@0 | 135 | */ |
michael@0 | 136 | #define SN_4BYTE_OFFSET_FLAG 0x80 |
michael@0 | 137 | #define SN_4BYTE_OFFSET_MASK 0x7f |
michael@0 | 138 | |
michael@0 | 139 | /* |
michael@0 | 140 | * Negative SRC_COLSPAN offsets are rare, but can arise with for(;;) loops and |
michael@0 | 141 | * other constructs that generate code in non-source order. They can also arise |
michael@0 | 142 | * due to failure to update pn->pn_pos.end to be the last child's end -- such |
michael@0 | 143 | * failures are bugs to fix. |
michael@0 | 144 | * |
michael@0 | 145 | * Source note offsets in general must be non-negative and less than 0x800000, |
michael@0 | 146 | * per the above SN_4BYTE_* definitions. To encode negative colspans, we bias |
michael@0 | 147 | * them by the offset domain size and restrict non-negative colspans to less |
michael@0 | 148 | * than half this domain. |
michael@0 | 149 | */ |
michael@0 | 150 | #define SN_COLSPAN_DOMAIN ptrdiff_t(1 << 23) |
michael@0 | 151 | |
michael@0 | 152 | #define SN_MAX_OFFSET ((size_t)((ptrdiff_t)SN_4BYTE_OFFSET_FLAG << 24) - 1) |
michael@0 | 153 | |
michael@0 | 154 | #define SN_LENGTH(sn) ((js_SrcNoteSpec[SN_TYPE(sn)].arity == 0) ? 1 \ |
michael@0 | 155 | : js_SrcNoteLength(sn)) |
michael@0 | 156 | #define SN_NEXT(sn) ((sn) + SN_LENGTH(sn)) |
michael@0 | 157 | |
michael@0 | 158 | struct JSSrcNoteSpec { |
michael@0 | 159 | const char *name; /* name for disassembly/debugging output */ |
michael@0 | 160 | int8_t arity; /* number of offset operands */ |
michael@0 | 161 | }; |
michael@0 | 162 | |
michael@0 | 163 | extern JS_FRIEND_DATA(const JSSrcNoteSpec) js_SrcNoteSpec[]; |
michael@0 | 164 | extern JS_FRIEND_API(unsigned) js_SrcNoteLength(jssrcnote *sn); |
michael@0 | 165 | |
michael@0 | 166 | /* |
michael@0 | 167 | * Get and set the offset operand identified by which (0 for the first, etc.). |
michael@0 | 168 | */ |
michael@0 | 169 | extern JS_FRIEND_API(ptrdiff_t) |
michael@0 | 170 | js_GetSrcNoteOffset(jssrcnote *sn, unsigned which); |
michael@0 | 171 | |
michael@0 | 172 | #endif /* frontend_SourceNotes_h */ |