Bug 1922904 - Fix bug 1780701 in a different approach. r=botond
[gecko.git] / js / src / jit / CacheIRReader.h
bloba27fd3bb9a0bd78ef038a244e4f0ffee05a0a272
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/. */
7 #ifndef jit_CacheIRReader_h
8 #define jit_CacheIRReader_h
10 #include "mozilla/Assertions.h"
11 #include "mozilla/Attributes.h"
13 #include <stdint.h>
14 #include "NamespaceImports.h"
16 #include "jit/CacheIR.h"
17 #include "jit/CacheIRWriter.h"
18 #include "jit/CompactBuffer.h"
19 #include "js/ScalarType.h"
20 #include "js/Value.h"
21 #include "vm/TypeofEqOperand.h" // TypeofEqOperand
22 #include "wasm/WasmValType.h"
24 enum class JSOp : uint8_t;
26 namespace js {
28 enum class UnaryMathFunction : uint8_t;
30 namespace gc {
31 enum class AllocKind : uint8_t;
34 namespace jit {
36 class CacheIRStubInfo;
38 // Helper class for reading CacheIR bytecode.
39 class MOZ_RAII CacheIRReader {
40 CompactBufferReader buffer_;
42 CacheIRReader(const CacheIRReader&) = delete;
43 CacheIRReader& operator=(const CacheIRReader&) = delete;
45 public:
46 CacheIRReader(const uint8_t* start, const uint8_t* end)
47 : buffer_(start, end) {}
48 explicit CacheIRReader(const CacheIRWriter& writer)
49 : CacheIRReader(writer.codeStart(), writer.codeEnd()) {}
50 explicit CacheIRReader(const CacheIRStubInfo* stubInfo);
52 bool more() const { return buffer_.more(); }
54 CacheOp readOp() { return CacheOp(buffer_.readFixedUint16_t()); }
55 CacheOp peekOp() { return CacheOp(buffer_.peekFixedUint16_t()); }
57 // Skip data not currently used.
58 void skip() { buffer_.readByte(); }
59 void skip(uint32_t skipLength) {
60 if (skipLength > 0) {
61 buffer_.seek(buffer_.currentPosition(), skipLength);
65 ValOperandId valOperandId() { return ValOperandId(buffer_.readByte()); }
66 ValueTagOperandId valueTagOperandId() {
67 return ValueTagOperandId(buffer_.readByte());
70 IntPtrOperandId intPtrOperandId() {
71 return IntPtrOperandId(buffer_.readByte());
74 ObjOperandId objOperandId() { return ObjOperandId(buffer_.readByte()); }
75 NumberOperandId numberOperandId() {
76 return NumberOperandId(buffer_.readByte());
78 StringOperandId stringOperandId() {
79 return StringOperandId(buffer_.readByte());
82 SymbolOperandId symbolOperandId() {
83 return SymbolOperandId(buffer_.readByte());
86 BigIntOperandId bigIntOperandId() {
87 return BigIntOperandId(buffer_.readByte());
90 BooleanOperandId booleanOperandId() {
91 return BooleanOperandId(buffer_.readByte());
94 Int32OperandId int32OperandId() { return Int32OperandId(buffer_.readByte()); }
96 uint32_t rawOperandId() { return buffer_.readByte(); }
98 uint32_t stubOffset() { return buffer_.readByte() * sizeof(uintptr_t); }
99 GuardClassKind guardClassKind() { return GuardClassKind(buffer_.readByte()); }
100 ArrayBufferViewKind arrayBufferViewKind() {
101 return ArrayBufferViewKind(buffer_.readByte());
103 ValueType valueType() { return ValueType(buffer_.readByte()); }
104 wasm::ValType::Kind wasmValType() {
105 return wasm::ValType::Kind(buffer_.readByte());
107 gc::AllocKind allocKind() { return gc::AllocKind(buffer_.readByte()); }
108 CompletionKind completionKind() { return CompletionKind(buffer_.readByte()); }
109 RealmFuses::FuseIndex realmFuseIndex() {
110 return RealmFuses::FuseIndex(buffer_.readByte());
113 Scalar::Type scalarType() { return Scalar::Type(buffer_.readByte()); }
114 JSWhyMagic whyMagic() { return JSWhyMagic(buffer_.readByte()); }
115 JSOp jsop() { return JSOp(buffer_.readByte()); }
116 TypeofEqOperand typeofEqOperand() {
117 return TypeofEqOperand::fromRawValue(buffer_.readByte());
119 int32_t int32Immediate() { return int32_t(buffer_.readFixedUint32_t()); }
120 uint32_t uint32Immediate() { return buffer_.readFixedUint32_t(); }
121 void* pointer() { return buffer_.readRawPointer(); }
123 UnaryMathFunction unaryMathFunction() {
124 return UnaryMathFunction(buffer_.readByte());
127 CallFlags callFlags() {
128 // See CacheIRWriter::writeCallFlagsImm()
129 uint8_t encoded = buffer_.readByte();
130 CallFlags::ArgFormat format =
131 CallFlags::ArgFormat(encoded & CallFlags::ArgFormatMask);
132 bool isConstructing = encoded & CallFlags::IsConstructing;
133 bool isSameRealm = encoded & CallFlags::IsSameRealm;
134 bool needsUninitializedThis = encoded & CallFlags::NeedsUninitializedThis;
135 MOZ_ASSERT_IF(needsUninitializedThis, isConstructing);
137 // FunCall and FunApply can't be constructors.
138 MOZ_ASSERT_IF(format == CallFlags::FunCall, !isConstructing);
139 MOZ_ASSERT_IF(format == CallFlags::FunApplyArgsObj, !isConstructing);
140 MOZ_ASSERT_IF(format == CallFlags::FunApplyArray, !isConstructing);
141 MOZ_ASSERT_IF(format == CallFlags::FunApplyNullUndefined, !isConstructing);
143 return CallFlags(format, isConstructing, isSameRealm,
144 needsUninitializedThis);
147 uint8_t readByte() { return buffer_.readByte(); }
148 bool readBool() {
149 uint8_t b = buffer_.readByte();
150 MOZ_ASSERT(b <= 1);
151 return bool(b);
154 const uint8_t* currentPosition() const { return buffer_.currentPosition(); }
157 } // namespace jit
158 } // namespace js
160 #endif /* jit_CacheIRReader_h */