Line data Source code
1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 : * vim: set ts=8 sts=4 et sw=4 tw=99:
3 : * This Source Code Form is subject to the terms of the Mozilla Public
4 : * License, v. 2.0. If a copy of the MPL was not distributed with this
5 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 :
7 : #ifndef frontend_SourceNotes_h
8 : #define frontend_SourceNotes_h
9 :
10 : #include <stdint.h>
11 :
12 : #include "jstypes.h"
13 :
14 : typedef uint8_t jssrcnote;
15 :
16 : namespace js {
17 :
18 : /*
19 : * Source notes generated along with bytecode for decompiling and debugging.
20 : * A source note is a uint8_t with 5 bits of type and 3 of offset from the pc
21 : * of the previous note. If 3 bits of offset aren't enough, extended delta
22 : * notes (SRC_XDELTA) consisting of 2 set high order bits followed by 6 offset
23 : * bits are emitted before the next note. Some notes have operand offsets
24 : * encoded immediately after them, in note bytes or byte-triples.
25 : *
26 : * Source Note Extended Delta
27 : * +7-6-5-4-3+2-1-0+ +7-6-5+4-3-2-1-0+
28 : * |note-type|delta| |1 1| ext-delta |
29 : * +---------+-----+ +---+-----------+
30 : *
31 : * At most one "gettable" note (i.e., a note of type other than SRC_NEWLINE,
32 : * SRC_COLSPAN, SRC_SETLINE, and SRC_XDELTA) applies to a given bytecode.
33 : *
34 : * NB: the js_SrcNoteSpec array in BytecodeEmitter.cpp is indexed by this
35 : * enum, so its initializers need to match the order here.
36 : */
37 : #define FOR_EACH_SRC_NOTE_TYPE(M) \
38 : M(SRC_NULL, "null", 0) /* Terminates a note vector. */ \
39 : M(SRC_IF, "if", 0) /* JSOP_IFEQ bytecode is from an if-then. */ \
40 : M(SRC_IF_ELSE, "if-else", 1) /* JSOP_IFEQ bytecode is from an if-then-else. */ \
41 : M(SRC_COND, "cond", 1) /* JSOP_IFEQ is from conditional ?: operator. */ \
42 : M(SRC_FOR, "for", 3) /* JSOP_NOP or JSOP_POP in for(;;) loop head. */ \
43 : M(SRC_WHILE, "while", 1) /* JSOP_GOTO to for or while loop condition from before \
44 : loop, else JSOP_NOP at top of do-while loop. */ \
45 : M(SRC_FOR_IN, "for-in", 1) /* JSOP_GOTO to for-in loop condition from before \
46 : loop. */ \
47 : M(SRC_FOR_OF, "for-of", 1) /* JSOP_GOTO to for-of loop condition from before \
48 : loop. */ \
49 : M(SRC_CONTINUE, "continue", 0) /* JSOP_GOTO is a continue. */ \
50 : M(SRC_BREAK, "break", 0) /* JSOP_GOTO is a break. */ \
51 : M(SRC_BREAK2LABEL, "break2label", 0) /* JSOP_GOTO for 'break label'. */ \
52 : M(SRC_SWITCHBREAK, "switchbreak", 0) /* JSOP_GOTO is a break in a switch. */ \
53 : M(SRC_TABLESWITCH, "tableswitch", 1) /* JSOP_TABLESWITCH; offset points to end of switch. */ \
54 : M(SRC_CONDSWITCH, "condswitch", 2) /* JSOP_CONDSWITCH; 1st offset points to end of switch, \
55 : 2nd points to first JSOP_CASE. */ \
56 : M(SRC_NEXTCASE, "nextcase", 1) /* Distance forward from one CASE in a CONDSWITCH to \
57 : the next. */ \
58 : M(SRC_ASSIGNOP, "assignop", 0) /* += or another assign-op follows. */ \
59 : M(SRC_CLASS_SPAN, "class", 2) /* The starting and ending offsets for the class, used \
60 : for toString correctness for default ctors. */ \
61 : M(SRC_TRY, "try", 1) /* JSOP_TRY, offset points to goto at the end of the \
62 : try block. */ \
63 : /* All notes above here are "gettable". See SN_IS_GETTABLE below. */ \
64 : M(SRC_COLSPAN, "colspan", 1) /* Number of columns this opcode spans. */ \
65 : M(SRC_NEWLINE, "newline", 0) /* Bytecode follows a source newline. */ \
66 : M(SRC_SETLINE, "setline", 1) /* A file-absolute source line number note. */ \
67 : M(SRC_UNUSED21, "unused21", 0) /* Unused. */ \
68 : M(SRC_UNUSED22, "unused22", 0) /* Unused. */ \
69 : M(SRC_UNUSED23, "unused23", 0) /* Unused. */ \
70 : M(SRC_XDELTA, "xdelta", 0) /* 24-31 are for extended delta notes. */
71 :
72 : enum SrcNoteType {
73 : #define DEFINE_SRC_NOTE_TYPE(sym, name, arity) sym,
74 : FOR_EACH_SRC_NOTE_TYPE(DEFINE_SRC_NOTE_TYPE)
75 : #undef DEFINE_SRC_NOTE_TYPE
76 :
77 : SRC_LAST,
78 : SRC_LAST_GETTABLE = SRC_TRY
79 : };
80 :
81 : static_assert(SRC_XDELTA == 24, "SRC_XDELTA should be 24");
82 :
83 : /* A source note array is terminated by an all-zero element. */
84 : inline void
85 5674 : SN_MAKE_TERMINATOR(jssrcnote* sn)
86 : {
87 5674 : *sn = SRC_NULL;
88 5674 : }
89 :
90 : inline bool
91 252212 : SN_IS_TERMINATOR(jssrcnote* sn)
92 : {
93 252212 : return *sn == SRC_NULL;
94 : }
95 :
96 : } // namespace js
97 :
98 : #define SN_TYPE_BITS 5
99 : #define SN_DELTA_BITS 3
100 : #define SN_XDELTA_BITS 6
101 : #define SN_TYPE_MASK (JS_BITMASK(SN_TYPE_BITS) << SN_DELTA_BITS)
102 : #define SN_DELTA_MASK ((ptrdiff_t)JS_BITMASK(SN_DELTA_BITS))
103 : #define SN_XDELTA_MASK ((ptrdiff_t)JS_BITMASK(SN_XDELTA_BITS))
104 :
105 : #define SN_MAKE_NOTE(sn,t,d) (*(sn) = (jssrcnote) \
106 : (((t) << SN_DELTA_BITS) \
107 : | ((d) & SN_DELTA_MASK)))
108 : #define SN_MAKE_XDELTA(sn,d) (*(sn) = (jssrcnote) \
109 : ((SRC_XDELTA << SN_DELTA_BITS) \
110 : | ((d) & SN_XDELTA_MASK)))
111 :
112 : #define SN_IS_XDELTA(sn) ((*(sn) >> SN_DELTA_BITS) >= SRC_XDELTA)
113 : #define SN_TYPE(sn) ((js::SrcNoteType)(SN_IS_XDELTA(sn) \
114 : ? SRC_XDELTA \
115 : : *(sn) >> SN_DELTA_BITS))
116 : #define SN_SET_TYPE(sn,type) SN_MAKE_NOTE(sn, type, SN_DELTA(sn))
117 : #define SN_IS_GETTABLE(sn) (SN_TYPE(sn) <= SRC_LAST_GETTABLE)
118 :
119 : #define SN_DELTA(sn) ((ptrdiff_t)(SN_IS_XDELTA(sn) \
120 : ? *(sn) & SN_XDELTA_MASK \
121 : : *(sn) & SN_DELTA_MASK))
122 : #define SN_SET_DELTA(sn,delta) (SN_IS_XDELTA(sn) \
123 : ? SN_MAKE_XDELTA(sn, delta) \
124 : : SN_MAKE_NOTE(sn, SN_TYPE(sn), delta))
125 :
126 : #define SN_DELTA_LIMIT ((ptrdiff_t)JS_BIT(SN_DELTA_BITS))
127 : #define SN_XDELTA_LIMIT ((ptrdiff_t)JS_BIT(SN_XDELTA_BITS))
128 :
129 : /*
130 : * Offset fields follow certain notes and are frequency-encoded: an offset in
131 : * [0,0x7f] consumes one byte, an offset in [0x80,0x7fffffff] takes four, and
132 : * the high bit of the first byte is set.
133 : */
134 : #define SN_4BYTE_OFFSET_FLAG 0x80
135 : #define SN_4BYTE_OFFSET_MASK 0x7f
136 :
137 : #define SN_OFFSET_BITS 31
138 : #define SN_MAX_OFFSET (((size_t) 1 << SN_OFFSET_BITS) - 1)
139 :
140 : inline bool
141 91343 : SN_REPRESENTABLE_OFFSET(ptrdiff_t offset)
142 : {
143 91343 : return 0 <= offset && size_t(offset) <= SN_MAX_OFFSET;
144 : }
145 :
146 : /*
147 : * SRC_COLSPAN values represent changes to the column number. Colspans are
148 : * signed: negative changes arise in describing constructs like for(;;) loops,
149 : * that generate code in non-source order. (Negative colspans also have a
150 : * history of indicating bugs in updating ParseNodes' source locations.)
151 : *
152 : * We store colspans using the same variable-length encoding as offsets,
153 : * described above. However, unlike offsets, colspans are signed, so we truncate
154 : * colspans (SN_COLSPAN_TO_OFFSET) for storage as offsets, and sign-extend
155 : * offsets into colspans when we read them (SN_OFFSET_TO_COLSPAN).
156 : */
157 : #define SN_COLSPAN_SIGN_BIT (1 << (SN_OFFSET_BITS - 1))
158 : #define SN_MIN_COLSPAN (-SN_COLSPAN_SIGN_BIT)
159 : #define SN_MAX_COLSPAN (SN_COLSPAN_SIGN_BIT - 1)
160 :
161 : inline bool
162 63975 : SN_REPRESENTABLE_COLSPAN(ptrdiff_t colspan)
163 : {
164 63975 : return SN_MIN_COLSPAN <= colspan && colspan <= SN_MAX_COLSPAN;
165 : }
166 :
167 : inline ptrdiff_t
168 88974 : SN_OFFSET_TO_COLSPAN(ptrdiff_t offset) {
169 : // There should be no bits set outside the field we're going to sign-extend.
170 88974 : MOZ_ASSERT(!(offset & ~((1U << SN_OFFSET_BITS) - 1)));
171 : // Sign-extend the least significant SN_OFFSET_BITS bits.
172 88974 : return (offset ^ SN_COLSPAN_SIGN_BIT) - SN_COLSPAN_SIGN_BIT;
173 : }
174 :
175 : inline ptrdiff_t
176 63975 : SN_COLSPAN_TO_OFFSET(ptrdiff_t colspan) {
177 : // Truncate the two's complement colspan, for storage as an offset.
178 63975 : ptrdiff_t offset = colspan & ((1U << SN_OFFSET_BITS) - 1);
179 : // When we read this back, we'd better get the value we stored.
180 63975 : MOZ_ASSERT(SN_OFFSET_TO_COLSPAN(offset) == colspan);
181 63975 : return offset;
182 : }
183 :
184 : #define SN_LENGTH(sn) ((js_SrcNoteSpec[SN_TYPE(sn)].arity == 0) ? 1 \
185 : : js::SrcNoteLength(sn))
186 : #define SN_NEXT(sn) ((sn) + SN_LENGTH(sn))
187 :
188 : struct JSSrcNoteSpec {
189 : const char* name; /* name for disassembly/debugging output */
190 : int8_t arity; /* number of offset operands */
191 : };
192 :
193 : extern JS_FRIEND_DATA(const JSSrcNoteSpec) js_SrcNoteSpec[];
194 :
195 : namespace js {
196 :
197 : extern JS_FRIEND_API(unsigned)
198 : SrcNoteLength(jssrcnote* sn);
199 :
200 : /*
201 : * Get and set the offset operand identified by which (0 for the first, etc.).
202 : */
203 : extern JS_FRIEND_API(ptrdiff_t)
204 : GetSrcNoteOffset(jssrcnote* sn, unsigned which);
205 :
206 : } // namespace js
207 :
208 : #endif /* frontend_SourceNotes_h */
|