Debian Codegraph @

js/src/vm/BytecodeUtil-inl.h

raw
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80:
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 vm_BytecodeUtil_inl_h
8 #define vm_BytecodeUtil_inl_h
9
10 #include "vm/BytecodeUtil.h"
11
12 #include "frontend/SourceNotes.h" // SrcNote, SrcNoteType, SrcNoteIterator
13 #include "js/ColumnNumber.h" // JS::LimitedColumnNumberOneOrigin, JS::ColumnNumberOffset
14 #include "vm/JSScript.h"
15
16 namespace js {
17
18 static inline unsigned GetDefCount(jsbytecode* pc) {
19 /*
20 * Add an extra pushed value for Or/And opcodes, so that they are included
21 * in the pushed array of stack values for type inference.
22 */
23 JSOp op = JSOp(*pc);
24 switch (op) {
25 case JSOp::Or:
26 case JSOp::And:
27 case JSOp::Coalesce:
28 return 1;
29 case JSOp::Pick:
30 case JSOp::Unpick:
31 /*
32 * Pick pops and pushes how deep it looks in the stack + 1
33 * items. i.e. if the stack were |a b[2] c[1] d[0]|, pick 2
34 * would pop b, c, and d to rearrange the stack to |a c[0]
35 * d[1] b[2]|.
36 */
37 return pc[1] + 1;
38 default:
39 return StackDefs(op);
40 }
41 }
42
43 static inline unsigned GetUseCount(jsbytecode* pc) {
44 JSOp op = JSOp(*pc);
45 if (op == JSOp::Pick || op == JSOp::Unpick) {
46 return pc[1] + 1;
47 }
48
49 return StackUses(op, pc);
50 }
51
52 static inline JSOp ReverseCompareOp(JSOp op) {
53 switch (op) {
54 case JSOp::Gt:
55 return JSOp::Lt;
56 case JSOp::Ge:
57 return JSOp::Le;
58 case JSOp::Lt:
59 return JSOp::Gt;
60 case JSOp::Le:
61 return JSOp::Ge;
62 case JSOp::Eq:
63 case JSOp::Ne:
64 case JSOp::StrictEq:
65 case JSOp::StrictNe:
66 return op;
67 default:
68 MOZ_CRASH("unrecognized op");
69 }
70 }
71
72 static inline JSOp NegateCompareOp(JSOp op) {
73 switch (op) {
74 case JSOp::Gt:
75 return JSOp::Le;
76 case JSOp::Ge:
77 return JSOp::Lt;
78 case JSOp::Lt:
79 return JSOp::Ge;
80 case JSOp::Le:
81 return JSOp::Gt;
82 case JSOp::Eq:
83 return JSOp::Ne;
84 case JSOp::Ne:
85 return JSOp::Eq;
86 case JSOp::StrictNe:
87 return JSOp::StrictEq;
88 case JSOp::StrictEq:
89 return JSOp::StrictNe;
90 default:
91 MOZ_CRASH("unrecognized op");
92 }
93 }
94
95 class BytecodeRange {
96 public:
97 BytecodeRange(JSContext* cx, JSScript* script)
98 : script(cx, script), pc(script->code()), end(pc + script->length()) {}
99 bool empty() const { return pc == end; }
100 jsbytecode* frontPC() const { return pc; }
101 JSOp frontOpcode() const { return JSOp(*pc); }
102 size_t frontOffset() const { return script->pcToOffset(pc); }
103 void popFront() { pc += GetBytecodeLength(pc); }
104
105 private:
106 RootedScript script;
107 jsbytecode* pc;
108 jsbytecode* end;
109 };
110
111 class BytecodeRangeWithPosition : private BytecodeRange {
112 public:
113 using BytecodeRange::empty;
114 using BytecodeRange::frontOffset;
115 using BytecodeRange::frontOpcode;
116 using BytecodeRange::frontPC;
117
118 BytecodeRangeWithPosition(JSContext* cx, JSScript* script)
119 : BytecodeRange(cx, script),
120 initialLine(script->lineno()),
121 lineno(script->lineno()),
122 column(script->column()),
123 sn(script->notes()),
124 snEnd(script->notesEnd()),
125 snpc(script->code()),
126 isEntryPoint(false),
127 isBreakpoint(false),
128 seenStepSeparator(false),
129 wasArtifactEntryPoint(false) {
130 if (sn < snEnd) {
131 snpc += sn->delta();
132 }
133 updatePosition();
134 while (frontPC() != script->main()) {
135 popFront();
136 }
137
138 if (frontOpcode() != JSOp::JumpTarget) {
139 isEntryPoint = true;
140 } else {
141 wasArtifactEntryPoint = true;
142 }
143 }
144
145 void popFront() {
146 BytecodeRange::popFront();
147 if (empty()) {
148 isEntryPoint = false;
149 } else {
150 updatePosition();
151 }
152
153 // The following conditions are handling artifacts introduced by the
154 // bytecode emitter, such that we do not add breakpoints on empty
155 // statements of the source code of the user.
156 if (wasArtifactEntryPoint) {
157 wasArtifactEntryPoint = false;
158 isEntryPoint = true;
159 }
160
161 if (isEntryPoint && frontOpcode() == JSOp::JumpTarget) {
162 wasArtifactEntryPoint = isEntryPoint;
163 isEntryPoint = false;
164 }
165 }
166
167 uint32_t frontLineNumber() const { return lineno; }
168 JS::LimitedColumnNumberOneOrigin frontColumnNumber() const { return column; }
169
170 // Entry points are restricted to bytecode offsets that have an
171 // explicit mention in the line table. This restriction avoids a
172 // number of failing cases caused by some instructions not having
173 // sensible (to the user) line numbers, and it is one way to
174 // implement the idea that the bytecode emitter should tell the
175 // debugger exactly which offsets represent "interesting" (to the
176 // user) places to stop.
177 bool frontIsEntryPoint() const { return isEntryPoint; }
178
179 // Breakable points are explicitly marked by the emitter as locations where
180 // the debugger may want to allow users to pause.
181 bool frontIsBreakablePoint() const { return isBreakpoint; }
182
183 // Breakable step points are the first breakable point after a
184 // SrcNote::StepSep note has been encountered.
185 bool frontIsBreakableStepPoint() const {
186 return isBreakpoint && seenStepSeparator;
187 }
188
189 private:
190 void updatePosition() {
191 if (isBreakpoint) {
192 isBreakpoint = false;
193 seenStepSeparator = false;
194 }
195
196 // Determine the current line number by reading all source notes up to
197 // and including the current offset.
198 jsbytecode* lastLinePC = nullptr;
199 SrcNoteIterator iter(sn, snEnd);
200 while (!iter.atEnd() && snpc <= frontPC()) {
201 auto sn = *iter;
202
203 SrcNoteType type = sn->type();
204 if (type == SrcNoteType::ColSpan) {
205 column += SrcNote::ColSpan::getSpan(sn);
206 } else if (type == SrcNoteType::SetLine) {
207 lineno = SrcNote::SetLine::getLine(sn, initialLine);
208 column = JS::LimitedColumnNumberOneOrigin();
209 } else if (type == SrcNoteType::SetLineColumn) {
210 lineno = SrcNote::SetLineColumn::getLine(sn, initialLine);
211 column = SrcNote::SetLineColumn::getColumn(sn);
212 } else if (type == SrcNoteType::NewLine) {
213 lineno++;
214 column = JS::LimitedColumnNumberOneOrigin();
215 } else if (type == SrcNoteType::NewLineColumn) {
216 lineno++;
217 column = SrcNote::NewLineColumn::getColumn(sn);
218 } else if (type == SrcNoteType::Breakpoint) {
219 isBreakpoint = true;
220 } else if (type == SrcNoteType::BreakpointStepSep) {
221 isBreakpoint = true;
222 seenStepSeparator = true;
223 }
224 lastLinePC = snpc;
225 ++iter;
226 if (!iter.atEnd()) {
227 snpc += (*iter)->delta();
228 }
229 }
230
231 sn = *iter;
232 isEntryPoint = lastLinePC == frontPC();
233 }
234
235 uint32_t initialLine;
236
237 // Line number (1-origin).
238 uint32_t lineno;
239
240 // Column number in UTF-16 code units.
241 JS::LimitedColumnNumberOneOrigin column;
242
243 const SrcNote* sn;
244 const SrcNote* snEnd;
245 jsbytecode* snpc;
246 bool isEntryPoint;
247 bool isBreakpoint;
248 bool seenStepSeparator;
249 bool wasArtifactEntryPoint;
250 };
251
252 } // namespace js
253
254 #endif /* vm_BytecodeUtil_inl_h */