| 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_BytecodeLocation_h |
| 8 |
#define vm_BytecodeLocation_h |
| 9 |
|
| 10 |
#include "frontend/NameAnalysisTypes.h" |
| 11 |
#include "js/TypeDecls.h" |
| 12 |
#include "vm/BuiltinObjectKind.h" |
| 13 |
#include "vm/BytecodeUtil.h" |
| 14 |
#include "vm/CheckIsObjectKind.h" // CheckIsObjectKind |
| 15 |
#include "vm/CompletionKind.h" // CompletionKind |
| 16 |
#include "vm/ConstantCompareOperand.h" // ConstantCompareOperand |
| 17 |
#include "vm/FunctionPrefixKind.h" // FunctionPrefixKind |
| 18 |
#include "vm/GeneratorResumeKind.h" |
| 19 |
#include "vm/TypeofEqOperand.h" // TypeofEqOperand |
| 20 |
#ifdef ENABLE_EXPLICIT_RESOURCE_MANAGEMENT |
| 21 |
# include "vm/UsingHint.h" |
| 22 |
#endif |
| 23 |
|
| 24 |
namespace js { |
| 25 |
|
| 26 |
using RawBytecodeLocationOffset = uint32_t; |
| 27 |
|
| 28 |
class PropertyName; |
| 29 |
class RegExpObject; |
| 30 |
|
| 31 |
class BytecodeLocationOffset { |
| 32 |
RawBytecodeLocationOffset rawOffset_; |
| 33 |
|
| 34 |
public: |
| 35 |
explicit BytecodeLocationOffset(RawBytecodeLocationOffset offset) |
| 36 |
: rawOffset_(offset) {} |
| 37 |
|
| 38 |
RawBytecodeLocationOffset rawOffset() const { return rawOffset_; } |
| 39 |
}; |
| 40 |
|
| 41 |
using RawBytecode = jsbytecode*; |
| 42 |
|
| 43 |
// A immutable representation of a program location |
| 44 |
// |
| 45 |
class BytecodeLocation { |
| 46 |
RawBytecode rawBytecode_; |
| 47 |
#ifdef DEBUG |
| 48 |
const JSScript* debugOnlyScript_; |
| 49 |
#endif |
| 50 |
|
| 51 |
// Construct a new BytecodeLocation, while borrowing scriptIdentity |
| 52 |
// from some other BytecodeLocation. |
| 53 |
BytecodeLocation(const BytecodeLocation& loc, RawBytecode pc) |
| 54 |
: rawBytecode_(pc) |
| 55 |
#ifdef DEBUG |
| 56 |
, |
| 57 |
debugOnlyScript_(loc.debugOnlyScript_) |
| 58 |
#endif |
| 59 |
{ |
| 60 |
MOZ_ASSERT(isValid()); |
| 61 |
} |
| 62 |
|
| 63 |
public: |
| 64 |
// Disallow the creation of an uninitialized location. |
| 65 |
BytecodeLocation() = delete; |
| 66 |
|
| 67 |
BytecodeLocation(const JSScript* script, RawBytecode pc) |
| 68 |
: rawBytecode_(pc) |
| 69 |
#ifdef DEBUG |
| 70 |
, |
| 71 |
debugOnlyScript_(script) |
| 72 |
#endif |
| 73 |
{ |
| 74 |
MOZ_ASSERT(isValid()); |
| 75 |
} |
| 76 |
|
| 77 |
RawBytecode toRawBytecode() const { return rawBytecode_; } |
| 78 |
|
| 79 |
#ifdef DEBUG |
| 80 |
// Return true if this bytecode location is valid for the given script. |
| 81 |
// This includes the location 1-past the end of the bytecode. |
| 82 |
bool isValid(const JSScript* script) const; |
| 83 |
|
| 84 |
// Return true if this bytecode location is within the bounds of the |
| 85 |
// bytecode for a given script. |
| 86 |
bool isInBounds(const JSScript* script) const; |
| 87 |
|
| 88 |
const JSScript* getDebugOnlyScript() const; |
| 89 |
#endif |
| 90 |
|
| 91 |
inline uint32_t bytecodeToOffset(const JSScript* script) const; |
| 92 |
|
| 93 |
inline uint32_t tableSwitchCaseOffset(const JSScript* script, |
| 94 |
uint32_t caseIndex) const; |
| 95 |
|
| 96 |
inline uint32_t getJumpTargetOffset(const JSScript* script) const; |
| 97 |
|
| 98 |
inline uint32_t getTableSwitchDefaultOffset(const JSScript* script) const; |
| 99 |
|
| 100 |
inline BytecodeLocation getTableSwitchDefaultTarget() const; |
| 101 |
inline BytecodeLocation getTableSwitchCaseTarget(const JSScript* script, |
| 102 |
uint32_t caseIndex) const; |
| 103 |
|
| 104 |
inline uint32_t useCount() const; |
| 105 |
inline uint32_t defCount() const; |
| 106 |
|
| 107 |
int32_t jumpOffset() const { return GET_JUMP_OFFSET(rawBytecode_); } |
| 108 |
|
| 109 |
inline JSAtom* getAtom(const JSScript* script) const; |
| 110 |
inline JSString* getString(const JSScript* script) const; |
| 111 |
inline bool atomizeString(JSContext* cx, JSScript* script); |
| 112 |
inline PropertyName* getPropertyName(const JSScript* script) const; |
| 113 |
inline JS::BigInt* getBigInt(const JSScript* script) const; |
| 114 |
inline JSObject* getObject(const JSScript* script) const; |
| 115 |
inline JSFunction* getFunction(const JSScript* script) const; |
| 116 |
inline js::RegExpObject* getRegExp(const JSScript* script) const; |
| 117 |
inline js::Scope* getScope(const JSScript* script) const; |
| 118 |
|
| 119 |
uint32_t getSymbolIndex() const { |
| 120 |
MOZ_ASSERT(is(JSOp::Symbol)); |
| 121 |
return GET_UINT8(rawBytecode_); |
| 122 |
} |
| 123 |
|
| 124 |
inline Scope* innermostScope(const JSScript* script) const; |
| 125 |
|
| 126 |
#ifdef DEBUG |
| 127 |
bool hasSameScript(const BytecodeLocation& other) const { |
| 128 |
return debugOnlyScript_ == other.debugOnlyScript_; |
| 129 |
} |
| 130 |
#endif |
| 131 |
|
| 132 |
// Overloaded operators |
| 133 |
|
| 134 |
bool operator==(const BytecodeLocation& other) const { |
| 135 |
MOZ_ASSERT(this->debugOnlyScript_ == other.debugOnlyScript_); |
| 136 |
return rawBytecode_ == other.rawBytecode_; |
| 137 |
} |
| 138 |
|
| 139 |
bool operator!=(const BytecodeLocation& other) const { |
| 140 |
return !(other == *this); |
| 141 |
} |
| 142 |
|
| 143 |
bool operator<(const BytecodeLocation& other) const { |
| 144 |
MOZ_ASSERT(this->debugOnlyScript_ == other.debugOnlyScript_); |
| 145 |
return rawBytecode_ < other.rawBytecode_; |
| 146 |
} |
| 147 |
|
| 148 |
// It is traditional to represent the rest of the relational operators |
| 149 |
// using operator<, so we don't need to assert for these. |
| 150 |
bool operator>(const BytecodeLocation& other) const { return other < *this; } |
| 151 |
|
| 152 |
bool operator<=(const BytecodeLocation& other) const { |
| 153 |
return !(other < *this); |
| 154 |
} |
| 155 |
|
| 156 |
bool operator>=(const BytecodeLocation& other) const { |
| 157 |
return !(*this < other); |
| 158 |
} |
| 159 |
|
| 160 |
// Return the next bytecode |
| 161 |
BytecodeLocation next() const { |
| 162 |
return BytecodeLocation(*this, |
| 163 |
rawBytecode_ + GetBytecodeLength(rawBytecode_)); |
| 164 |
} |
| 165 |
|
| 166 |
// Add an offset. |
| 167 |
BytecodeLocation operator+(const BytecodeLocationOffset& offset) const { |
| 168 |
return BytecodeLocation(*this, rawBytecode_ + offset.rawOffset()); |
| 169 |
} |
| 170 |
|
| 171 |
// Identity Checks |
| 172 |
bool is(JSOp op) const { |
| 173 |
MOZ_ASSERT(isInBounds()); |
| 174 |
return getOp() == op; |
| 175 |
} |
| 176 |
|
| 177 |
// Accessors: |
| 178 |
|
| 179 |
uint32_t length() const { return GetBytecodeLength(rawBytecode_); } |
| 180 |
|
| 181 |
bool isJumpTarget() const { return BytecodeIsJumpTarget(getOp()); } |
| 182 |
|
| 183 |
bool isJump() const { return IsJumpOpcode(getOp()); } |
| 184 |
|
| 185 |
bool isBackedge() const { return IsBackedgePC(rawBytecode_); } |
| 186 |
|
| 187 |
bool isBackedgeForLoophead(BytecodeLocation loopHead) const { |
| 188 |
return IsBackedgeForLoopHead(rawBytecode_, loopHead.rawBytecode_); |
| 189 |
} |
| 190 |
|
| 191 |
bool opHasIC() const { return BytecodeOpHasIC(getOp()); } |
| 192 |
|
| 193 |
bool fallsThrough() const { return BytecodeFallsThrough(getOp()); } |
| 194 |
|
| 195 |
uint32_t icIndex() const { return GET_ICINDEX(rawBytecode_); } |
| 196 |
|
| 197 |
uint32_t local() const { return GET_LOCALNO(rawBytecode_); } |
| 198 |
|
| 199 |
uint16_t arg() const { return GET_ARGNO(rawBytecode_); } |
| 200 |
|
| 201 |
bool isEqualityOp() const { return IsEqualityOp(getOp()); } |
| 202 |
|
| 203 |
bool isStrictEqualityOp() const { return IsStrictEqualityOp(getOp()); } |
| 204 |
|
| 205 |
bool isStrictSetOp() const { return IsStrictSetPC(rawBytecode_); } |
| 206 |
|
| 207 |
bool isSpreadOp() const { return IsSpreadOp(getOp()); } |
| 208 |
|
| 209 |
bool isInvokeOp() const { return IsInvokeOp(getOp()); } |
| 210 |
|
| 211 |
bool isGetPropOp() const { return IsGetPropOp(getOp()); } |
| 212 |
bool isGetElemOp() const { return IsGetElemOp(getOp()); } |
| 213 |
|
| 214 |
bool isSetPropOp() const { return IsSetPropOp(getOp()); } |
| 215 |
bool isSetElemOp() const { return IsSetElemOp(getOp()); } |
| 216 |
|
| 217 |
bool resultIsPopped() const { |
| 218 |
MOZ_ASSERT(StackDefs(getOp()) == 1); |
| 219 |
return BytecodeIsPopped(rawBytecode_); |
| 220 |
} |
| 221 |
|
| 222 |
// Accessors: |
| 223 |
JSOp getOp() const { return JSOp(*rawBytecode_); } |
| 224 |
|
| 225 |
BytecodeLocation getJumpTarget() const { |
| 226 |
MOZ_ASSERT(isJump()); |
| 227 |
return BytecodeLocation(*this, |
| 228 |
rawBytecode_ + GET_JUMP_OFFSET(rawBytecode_)); |
| 229 |
} |
| 230 |
|
| 231 |
// Return the 'low' parameter to the tableswitch opcode |
| 232 |
int32_t getTableSwitchLow() const { |
| 233 |
MOZ_ASSERT(is(JSOp::TableSwitch)); |
| 234 |
return GET_JUMP_OFFSET(rawBytecode_ + JUMP_OFFSET_LEN); |
| 235 |
} |
| 236 |
|
| 237 |
// Return the 'high' parameter to the tableswitch opcode |
| 238 |
int32_t getTableSwitchHigh() const { |
| 239 |
MOZ_ASSERT(is(JSOp::TableSwitch)); |
| 240 |
return GET_JUMP_OFFSET(rawBytecode_ + (2 * JUMP_OFFSET_LEN)); |
| 241 |
} |
| 242 |
|
| 243 |
uint32_t getPopCount() const { |
| 244 |
MOZ_ASSERT(is(JSOp::PopN)); |
| 245 |
return GET_UINT16(rawBytecode_); |
| 246 |
} |
| 247 |
|
| 248 |
uint32_t getDupAtIndex() const { |
| 249 |
MOZ_ASSERT(is(JSOp::DupAt)); |
| 250 |
return GET_UINT24(rawBytecode_); |
| 251 |
} |
| 252 |
|
| 253 |
uint8_t getPickDepth() const { |
| 254 |
MOZ_ASSERT(is(JSOp::Pick)); |
| 255 |
return GET_UINT8(rawBytecode_); |
| 256 |
} |
| 257 |
uint8_t getUnpickDepth() const { |
| 258 |
MOZ_ASSERT(is(JSOp::Unpick)); |
| 259 |
return GET_UINT8(rawBytecode_); |
| 260 |
} |
| 261 |
|
| 262 |
uint32_t getEnvCalleeNumHops() const { |
| 263 |
MOZ_ASSERT(is(JSOp::EnvCallee)); |
| 264 |
return GET_UINT8(rawBytecode_); |
| 265 |
} |
| 266 |
|
| 267 |
EnvironmentCoordinate getEnvironmentCoordinate() const { |
| 268 |
MOZ_ASSERT(JOF_OPTYPE(getOp()) == JOF_ENVCOORD); |
| 269 |
return EnvironmentCoordinate(rawBytecode_); |
| 270 |
} |
| 271 |
|
| 272 |
uint32_t getCallArgc() const { |
| 273 |
MOZ_ASSERT(JOF_OPTYPE(getOp()) == JOF_ARGC); |
| 274 |
return GET_ARGC(rawBytecode_); |
| 275 |
} |
| 276 |
|
| 277 |
uint32_t getInitElemArrayIndex() const { |
| 278 |
MOZ_ASSERT(is(JSOp::InitElemArray)); |
| 279 |
uint32_t index = GET_UINT32(rawBytecode_); |
| 280 |
MOZ_ASSERT(index <= INT32_MAX, |
| 281 |
"the bytecode emitter must never generate JSOp::InitElemArray " |
| 282 |
"with an index exceeding int32_t range"); |
| 283 |
return index; |
| 284 |
} |
| 285 |
|
| 286 |
TypeofEqOperand getTypeofEqOperand() const { |
| 287 |
MOZ_ASSERT(is(JSOp::TypeofEq)); |
| 288 |
return TypeofEqOperand::fromRawValue(GET_UINT8(rawBytecode_)); |
| 289 |
} |
| 290 |
|
| 291 |
ConstantCompareOperand getConstantCompareOperand() const { |
| 292 |
MOZ_ASSERT(is(JSOp::StrictConstantEq) || is(JSOp::StrictConstantNe)); |
| 293 |
return ConstantCompareOperand::fromRawValue(GET_UINT16(rawBytecode_)); |
| 294 |
} |
| 295 |
|
| 296 |
FunctionPrefixKind getFunctionPrefixKind() const { |
| 297 |
MOZ_ASSERT(is(JSOp::SetFunName)); |
| 298 |
return FunctionPrefixKind(GET_UINT8(rawBytecode_)); |
| 299 |
} |
| 300 |
|
| 301 |
CheckIsObjectKind getCheckIsObjectKind() const { |
| 302 |
MOZ_ASSERT(is(JSOp::CheckIsObj)); |
| 303 |
return CheckIsObjectKind(GET_UINT8(rawBytecode_)); |
| 304 |
} |
| 305 |
|
| 306 |
BuiltinObjectKind getBuiltinObjectKind() const { |
| 307 |
MOZ_ASSERT(is(JSOp::BuiltinObject)); |
| 308 |
return BuiltinObjectKind(GET_UINT8(rawBytecode_)); |
| 309 |
} |
| 310 |
|
| 311 |
CompletionKind getCompletionKind() const { |
| 312 |
MOZ_ASSERT(is(JSOp::CloseIter)); |
| 313 |
return CompletionKind(GET_UINT8(rawBytecode_)); |
| 314 |
} |
| 315 |
|
| 316 |
#ifdef ENABLE_EXPLICIT_RESOURCE_MANAGEMENT |
| 317 |
UsingHint getUsingHint() const { |
| 318 |
MOZ_ASSERT(is(JSOp::AddDisposable)); |
| 319 |
return UsingHint(GET_UINT8(rawBytecode_)); |
| 320 |
} |
| 321 |
#endif |
| 322 |
|
| 323 |
uint32_t getNewArrayLength() const { |
| 324 |
MOZ_ASSERT(is(JSOp::NewArray)); |
| 325 |
return GET_UINT32(rawBytecode_); |
| 326 |
} |
| 327 |
|
| 328 |
int8_t getInt8() const { |
| 329 |
MOZ_ASSERT(is(JSOp::Int8)); |
| 330 |
return GET_INT8(rawBytecode_); |
| 331 |
} |
| 332 |
uint16_t getUint16() const { |
| 333 |
MOZ_ASSERT(is(JSOp::Uint16)); |
| 334 |
return GET_UINT16(rawBytecode_); |
| 335 |
} |
| 336 |
uint32_t getUint24() const { |
| 337 |
MOZ_ASSERT(is(JSOp::Uint24)); |
| 338 |
return GET_UINT24(rawBytecode_); |
| 339 |
} |
| 340 |
int32_t getInt32() const { |
| 341 |
MOZ_ASSERT(is(JSOp::Int32)); |
| 342 |
return GET_INT32(rawBytecode_); |
| 343 |
} |
| 344 |
uint32_t getResumeIndex() const { |
| 345 |
MOZ_ASSERT(is(JSOp::InitialYield) || is(JSOp::Yield) || is(JSOp::Await)); |
| 346 |
return GET_RESUMEINDEX(rawBytecode_); |
| 347 |
} |
| 348 |
Value getInlineValue() const { |
| 349 |
MOZ_ASSERT(is(JSOp::Double)); |
| 350 |
return GET_INLINE_VALUE(rawBytecode_); |
| 351 |
} |
| 352 |
|
| 353 |
GeneratorResumeKind resumeKind() { return ResumeKindFromPC(rawBytecode_); } |
| 354 |
|
| 355 |
ThrowMsgKind throwMsgKind() { |
| 356 |
MOZ_ASSERT(is(JSOp::ThrowMsg)); |
| 357 |
return static_cast<ThrowMsgKind>(GET_UINT8(rawBytecode_)); |
| 358 |
} |
| 359 |
|
| 360 |
#ifdef DEBUG |
| 361 |
// To ease writing assertions |
| 362 |
bool isValid() const { return isValid(debugOnlyScript_); } |
| 363 |
|
| 364 |
bool isInBounds() const { return isInBounds(debugOnlyScript_); } |
| 365 |
#endif |
| 366 |
}; |
| 367 |
|
| 368 |
} // namespace js |
| 369 |
|
| 370 |
#endif |