summaryrefslogtreecommitdiffstats
path: root/js/src/vm/BytecodeLocation.h
blob: e5876ed9d2811a139250d28c52ee04d4e1cb3931 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 * vim: set ts=8 sts=2 et sw=2 tw=80:
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef vm_BytecodeLocation_h
#define vm_BytecodeLocation_h

#include "frontend/NameAnalysisTypes.h"
#include "js/TypeDecls.h"
#include "vm/AsyncFunctionResolveKind.h"
#include "vm/BuiltinObjectKind.h"
#include "vm/BytecodeUtil.h"
#include "vm/CheckIsObjectKind.h"   // CheckIsObjectKind
#include "vm/CompletionKind.h"      // CompletionKind
#include "vm/FunctionPrefixKind.h"  // FunctionPrefixKind
#include "vm/GeneratorResumeKind.h"

namespace js {

using RawBytecodeLocationOffset = uint32_t;

class PropertyName;
class RegExpObject;

class BytecodeLocationOffset {
  RawBytecodeLocationOffset rawOffset_;

 public:
  explicit BytecodeLocationOffset(RawBytecodeLocationOffset offset)
      : rawOffset_(offset) {}

  RawBytecodeLocationOffset rawOffset() const { return rawOffset_; }
};

using RawBytecode = jsbytecode*;

// A immutable representation of a program location
//
class BytecodeLocation {
  RawBytecode rawBytecode_;
#ifdef DEBUG
  const JSScript* debugOnlyScript_;
#endif

  // Construct a new BytecodeLocation, while borrowing scriptIdentity
  // from some other BytecodeLocation.
  BytecodeLocation(const BytecodeLocation& loc, RawBytecode pc)
      : rawBytecode_(pc)
#ifdef DEBUG
        ,
        debugOnlyScript_(loc.debugOnlyScript_)
#endif
  {
    MOZ_ASSERT(isValid());
  }

 public:
  // Disallow the creation of an uninitialized location.
  BytecodeLocation() = delete;

  BytecodeLocation(const JSScript* script, RawBytecode pc)
      : rawBytecode_(pc)
#ifdef DEBUG
        ,
        debugOnlyScript_(script)
#endif
  {
    MOZ_ASSERT(isValid());
  }

  RawBytecode toRawBytecode() const { return rawBytecode_; }

#ifdef DEBUG
  // Return true if this bytecode location is valid for the given script.
  // This includes the location 1-past the end of the bytecode.
  bool isValid(const JSScript* script) const;

  // Return true if this bytecode location is within the bounds of the
  // bytecode for a given script.
  bool isInBounds(const JSScript* script) const;

  const JSScript* getDebugOnlyScript() const;
#endif

  inline uint32_t bytecodeToOffset(const JSScript* script) const;

  inline uint32_t tableSwitchCaseOffset(const JSScript* script,
                                        uint32_t caseIndex) const;

  inline uint32_t getJumpTargetOffset(const JSScript* script) const;

  inline uint32_t getTableSwitchDefaultOffset(const JSScript* script) const;

  inline BytecodeLocation getTableSwitchDefaultTarget() const;
  inline BytecodeLocation getTableSwitchCaseTarget(const JSScript* script,
                                                   uint32_t caseIndex) const;

  inline uint32_t useCount() const;
  inline uint32_t defCount() const;

  int32_t jumpOffset() const { return GET_JUMP_OFFSET(rawBytecode_); }

  inline JSAtom* getAtom(const JSScript* script) const;
  inline JSString* getString(const JSScript* script) const;
  inline PropertyName* getPropertyName(const JSScript* script) const;
  inline JS::BigInt* getBigInt(const JSScript* script) const;
  inline JSObject* getObject(const JSScript* script) const;
  inline JSFunction* getFunction(const JSScript* script) const;
  inline js::RegExpObject* getRegExp(const JSScript* script) const;
  inline js::Scope* getScope(const JSScript* script) const;

  uint32_t getSymbolIndex() const {
    MOZ_ASSERT(is(JSOp::Symbol));
    return GET_UINT8(rawBytecode_);
  }

  inline Scope* innermostScope(const JSScript* script) const;

#ifdef DEBUG
  bool hasSameScript(const BytecodeLocation& other) const {
    return debugOnlyScript_ == other.debugOnlyScript_;
  }
#endif

  // Overloaded operators

  bool operator==(const BytecodeLocation& other) const {
    MOZ_ASSERT(this->debugOnlyScript_ == other.debugOnlyScript_);
    return rawBytecode_ == other.rawBytecode_;
  }

  bool operator!=(const BytecodeLocation& other) const {
    return !(other == *this);
  }

  bool operator<(const BytecodeLocation& other) const {
    MOZ_ASSERT(this->debugOnlyScript_ == other.debugOnlyScript_);
    return rawBytecode_ < other.rawBytecode_;
  }

  // It is traditional to represent the rest of the relational operators
  // using operator<, so we don't need to assert for these.
  bool operator>(const BytecodeLocation& other) const { return other < *this; }

  bool operator<=(const BytecodeLocation& other) const {
    return !(other < *this);
  }

  bool operator>=(const BytecodeLocation& other) const {
    return !(*this < other);
  }

  // Return the next bytecode
  BytecodeLocation next() const {
    return BytecodeLocation(*this,
                            rawBytecode_ + GetBytecodeLength(rawBytecode_));
  }

  // Add an offset.
  BytecodeLocation operator+(const BytecodeLocationOffset& offset) const {
    return BytecodeLocation(*this, rawBytecode_ + offset.rawOffset());
  }

  // Identity Checks
  bool is(JSOp op) const {
    MOZ_ASSERT(isInBounds());
    return getOp() == op;
  }

  // Accessors:

  uint32_t length() const { return GetBytecodeLength(rawBytecode_); }

  bool isJumpTarget() const { return BytecodeIsJumpTarget(getOp()); }

  bool isJump() const { return IsJumpOpcode(getOp()); }

  bool isBackedge() const { return IsBackedgePC(rawBytecode_); }

  bool isBackedgeForLoophead(BytecodeLocation loopHead) const {
    return IsBackedgeForLoopHead(rawBytecode_, loopHead.rawBytecode_);
  }

  bool opHasIC() const { return BytecodeOpHasIC(getOp()); }

  bool fallsThrough() const { return BytecodeFallsThrough(getOp()); }

  uint32_t icIndex() const { return GET_ICINDEX(rawBytecode_); }

  uint32_t local() const { return GET_LOCALNO(rawBytecode_); }

  uint16_t arg() const { return GET_ARGNO(rawBytecode_); }

  bool isEqualityOp() const { return IsEqualityOp(getOp()); }

  bool isStrictEqualityOp() const { return IsStrictEqualityOp(getOp()); }

  bool isStrictSetOp() const { return IsStrictSetPC(rawBytecode_); }

  bool isNameOp() const { return IsNameOp(getOp()); }

  bool isSpreadOp() const { return IsSpreadOp(getOp()); }

  bool isInvokeOp() const { return IsInvokeOp(getOp()); }

  bool isGetPropOp() const { return IsGetPropOp(getOp()); }
  bool isGetElemOp() const { return IsGetElemOp(getOp()); }

  bool isSetPropOp() const { return IsSetPropOp(getOp()); }
  bool isSetElemOp() const { return IsSetElemOp(getOp()); }

  AsyncFunctionResolveKind getAsyncFunctionResolveKind() {
    return AsyncFunctionResolveKind(GET_UINT8(rawBytecode_));
  }

  bool resultIsPopped() const {
    MOZ_ASSERT(StackDefs(getOp()) == 1);
    return BytecodeIsPopped(rawBytecode_);
  }

  // Accessors:
  JSOp getOp() const { return JSOp(*rawBytecode_); }

  BytecodeLocation getJumpTarget() const {
    MOZ_ASSERT(isJump());
    return BytecodeLocation(*this,
                            rawBytecode_ + GET_JUMP_OFFSET(rawBytecode_));
  }

  // Return the 'low' parameter to the tableswitch opcode
  int32_t getTableSwitchLow() const {
    MOZ_ASSERT(is(JSOp::TableSwitch));
    return GET_JUMP_OFFSET(rawBytecode_ + JUMP_OFFSET_LEN);
  }

  // Return the 'high' parameter to the tableswitch opcode
  int32_t getTableSwitchHigh() const {
    MOZ_ASSERT(is(JSOp::TableSwitch));
    return GET_JUMP_OFFSET(rawBytecode_ + (2 * JUMP_OFFSET_LEN));
  }

  uint32_t getPopCount() const {
    MOZ_ASSERT(is(JSOp::PopN));
    return GET_UINT16(rawBytecode_);
  }

  uint32_t getDupAtIndex() const {
    MOZ_ASSERT(is(JSOp::DupAt));
    return GET_UINT24(rawBytecode_);
  }

  uint8_t getPickDepth() const {
    MOZ_ASSERT(is(JSOp::Pick));
    return GET_UINT8(rawBytecode_);
  }
  uint8_t getUnpickDepth() const {
    MOZ_ASSERT(is(JSOp::Unpick));
    return GET_UINT8(rawBytecode_);
  }

  uint32_t getEnvCalleeNumHops() const {
    MOZ_ASSERT(is(JSOp::EnvCallee));
    return GET_UINT8(rawBytecode_);
  }

  EnvironmentCoordinate getEnvironmentCoordinate() const {
    MOZ_ASSERT(JOF_OPTYPE(getOp()) == JOF_ENVCOORD);
    return EnvironmentCoordinate(rawBytecode_);
  }

  uint32_t getCallArgc() const {
    MOZ_ASSERT(JOF_OPTYPE(getOp()) == JOF_ARGC);
    return GET_ARGC(rawBytecode_);
  }

  uint32_t getInitElemArrayIndex() const {
    MOZ_ASSERT(is(JSOp::InitElemArray));
    uint32_t index = GET_UINT32(rawBytecode_);
    MOZ_ASSERT(index <= INT32_MAX,
               "the bytecode emitter must never generate JSOp::InitElemArray "
               "with an index exceeding int32_t range");
    return index;
  }

  FunctionPrefixKind getFunctionPrefixKind() const {
    MOZ_ASSERT(is(JSOp::SetFunName));
    return FunctionPrefixKind(GET_UINT8(rawBytecode_));
  }

  CheckIsObjectKind getCheckIsObjectKind() const {
    MOZ_ASSERT(is(JSOp::CheckIsObj));
    return CheckIsObjectKind(GET_UINT8(rawBytecode_));
  }

  BuiltinObjectKind getBuiltinObjectKind() const {
    MOZ_ASSERT(is(JSOp::BuiltinObject));
    return BuiltinObjectKind(GET_UINT8(rawBytecode_));
  }

  CompletionKind getCompletionKind() const {
    MOZ_ASSERT(is(JSOp::CloseIter));
    return CompletionKind(GET_UINT8(rawBytecode_));
  }

  uint32_t getNewArrayLength() const {
    MOZ_ASSERT(is(JSOp::NewArray));
    return GET_UINT32(rawBytecode_);
  }

  int8_t getInt8() const {
    MOZ_ASSERT(is(JSOp::Int8));
    return GET_INT8(rawBytecode_);
  }
  uint16_t getUint16() const {
    MOZ_ASSERT(is(JSOp::Uint16));
    return GET_UINT16(rawBytecode_);
  }
  uint32_t getUint24() const {
    MOZ_ASSERT(is(JSOp::Uint24));
    return GET_UINT24(rawBytecode_);
  }
  int32_t getInt32() const {
    MOZ_ASSERT(is(JSOp::Int32));
    return GET_INT32(rawBytecode_);
  }
  uint32_t getResumeIndex() const {
    MOZ_ASSERT(is(JSOp::InitialYield) || is(JSOp::Yield) || is(JSOp::Await));
    return GET_RESUMEINDEX(rawBytecode_);
  }
  Value getInlineValue() const {
    MOZ_ASSERT(is(JSOp::Double));
    return GET_INLINE_VALUE(rawBytecode_);
  }

  GeneratorResumeKind resumeKind() { return ResumeKindFromPC(rawBytecode_); }

  ThrowMsgKind throwMsgKind() {
    MOZ_ASSERT(is(JSOp::ThrowMsg));
    return static_cast<ThrowMsgKind>(GET_UINT8(rawBytecode_));
  }

#ifdef DEBUG
  // To ease writing assertions
  bool isValid() const { return isValid(debugOnlyScript_); }

  bool isInBounds() const { return isInBounds(debugOnlyScript_); }
#endif
};

}  // namespace js

#endif