Debian Codegraph @

js/src/jit/mips64/Assembler-mips64.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 jit_mips64_Assembler_mips64_h
8 #define jit_mips64_Assembler_mips64_h
9
10 #include <iterator>
11
12 #include "jit/mips-shared/Assembler-mips-shared.h"
13
14 #include "jit/mips64/Architecture-mips64.h"
15
16 namespace js {
17 namespace jit {
18
19 static constexpr Register CallTempReg4 = a4;
20 static constexpr Register CallTempReg5 = a5;
21
22 static constexpr Register CallTempNonArgRegs[] = {t0, t1, t2, t3};
23 static const uint32_t NumCallTempNonArgRegs = std::size(CallTempNonArgRegs);
24
25 class ABIArgGenerator {
26 unsigned regIndex_;
27 uint32_t stackOffset_;
28 ABIArg current_;
29
30 public:
31 ABIArgGenerator();
32 ABIArg next(MIRType argType);
33 ABIArg& current() { return current_; }
34
35 uint32_t stackBytesConsumedSoFar() const { return stackOffset_; }
36 void increaseStackOffset(uint32_t bytes) { stackOffset_ += bytes; }
37 };
38
39 // These registers may be volatile or nonvolatile.
40 static constexpr Register ABINonArgReg0 = t0;
41 static constexpr Register ABINonArgReg1 = t1;
42 static constexpr Register ABINonArgReg2 = t2;
43 static constexpr Register ABINonArgReg3 = t3;
44
45 // This register may be volatile or nonvolatile. Avoid f23 which is the
46 // ScratchDoubleReg.
47 static constexpr FloatRegister ABINonArgDoubleReg{FloatRegisters::f21,
48 FloatRegisters::Double};
49
50 // These registers may be volatile or nonvolatile.
51 // Note: these three registers are all guaranteed to be different
52 static constexpr Register ABINonArgReturnReg0 = t0;
53 static constexpr Register ABINonArgReturnReg1 = t1;
54 static constexpr Register ABINonVolatileReg = s0;
55
56 // This register is guaranteed to be clobberable during the prologue and
57 // epilogue of an ABI call which must preserve both ABI argument, return
58 // and non-volatile registers.
59 static constexpr Register ABINonArgReturnVolatileReg = t0;
60
61 // TLS pointer argument register for WebAssembly functions. This must not alias
62 // any other register used for passing function arguments or return values.
63 // Preserved by WebAssembly functions.
64 static constexpr Register InstanceReg = s5;
65
66 // Registers used for wasm table calls. These registers must be disjoint
67 // from the ABI argument registers, InstanceReg and each other.
68 static constexpr Register WasmTableCallScratchReg0 = ABINonArgReg0;
69 static constexpr Register WasmTableCallScratchReg1 = ABINonArgReg1;
70 static constexpr Register WasmTableCallSigReg = ABINonArgReg2;
71 static constexpr Register WasmTableCallIndexReg = ABINonArgReg3;
72
73 // Registers used for ref calls.
74 static constexpr Register WasmCallRefCallScratchReg0 = ABINonArgReg0;
75 static constexpr Register WasmCallRefCallScratchReg1 = ABINonArgReg1;
76 static constexpr Register WasmCallRefCallScratchReg2 = ABINonArgReg2;
77 static constexpr Register WasmCallRefReg = ABINonArgReg3;
78
79 // Registers used for wasm tail calls operations.
80 static constexpr Register WasmTailCallInstanceScratchReg = ABINonArgReg1;
81 static constexpr Register WasmTailCallRAScratchReg = ra;
82 static constexpr Register WasmTailCallFPScratchReg = ABINonArgReg3;
83
84 // Register used as a scratch along the return path in the fast js -> wasm stub
85 // code. This must not overlap ReturnReg, JSReturnOperand, or InstanceReg.
86 // It must be a volatile register.
87 static constexpr Register WasmJitEntryReturnScratch = t1;
88
89 static constexpr Register InterpreterPCReg = t5;
90
91 static constexpr Register JSReturnReg = v1;
92 static constexpr Register JSReturnReg_Type = JSReturnReg;
93 static constexpr Register JSReturnReg_Data = JSReturnReg;
94 static constexpr Register64 ReturnReg64(ReturnReg);
95 static constexpr FloatRegister ReturnFloat32Reg = {FloatRegisters::f0,
96 FloatRegisters::Single};
97 static constexpr FloatRegister ReturnDoubleReg = {FloatRegisters::f0,
98 FloatRegisters::Double};
99 static constexpr FloatRegister ScratchFloat32Reg = {FloatRegisters::f23,
100 FloatRegisters::Single};
101 static constexpr FloatRegister ScratchDoubleReg = {FloatRegisters::f23,
102 FloatRegisters::Double};
103
104 struct ScratchFloat32Scope : public AutoFloatRegisterScope {
105 explicit ScratchFloat32Scope(MacroAssembler& masm)
106 : AutoFloatRegisterScope(masm, ScratchFloat32Reg) {}
107 };
108
109 struct ScratchDoubleScope : public AutoFloatRegisterScope {
110 explicit ScratchDoubleScope(MacroAssembler& masm)
111 : AutoFloatRegisterScope(masm, ScratchDoubleReg) {}
112 };
113
114 static constexpr FloatRegister f0 = {FloatRegisters::f0,
115 FloatRegisters::Double};
116 static constexpr FloatRegister f1 = {FloatRegisters::f1,
117 FloatRegisters::Double};
118 static constexpr FloatRegister f2 = {FloatRegisters::f2,
119 FloatRegisters::Double};
120 static constexpr FloatRegister f3 = {FloatRegisters::f3,
121 FloatRegisters::Double};
122 static constexpr FloatRegister f4 = {FloatRegisters::f4,
123 FloatRegisters::Double};
124 static constexpr FloatRegister f5 = {FloatRegisters::f5,
125 FloatRegisters::Double};
126 static constexpr FloatRegister f6 = {FloatRegisters::f6,
127 FloatRegisters::Double};
128 static constexpr FloatRegister f7 = {FloatRegisters::f7,
129 FloatRegisters::Double};
130 static constexpr FloatRegister f8 = {FloatRegisters::f8,
131 FloatRegisters::Double};
132 static constexpr FloatRegister f9 = {FloatRegisters::f9,
133 FloatRegisters::Double};
134 static constexpr FloatRegister f10 = {FloatRegisters::f10,
135 FloatRegisters::Double};
136 static constexpr FloatRegister f11 = {FloatRegisters::f11,
137 FloatRegisters::Double};
138 static constexpr FloatRegister f12 = {FloatRegisters::f12,
139 FloatRegisters::Double};
140 static constexpr FloatRegister f13 = {FloatRegisters::f13,
141 FloatRegisters::Double};
142 static constexpr FloatRegister f14 = {FloatRegisters::f14,
143 FloatRegisters::Double};
144 static constexpr FloatRegister f15 = {FloatRegisters::f15,
145 FloatRegisters::Double};
146 static constexpr FloatRegister f16 = {FloatRegisters::f16,
147 FloatRegisters::Double};
148 static constexpr FloatRegister f17 = {FloatRegisters::f17,
149 FloatRegisters::Double};
150 static constexpr FloatRegister f18 = {FloatRegisters::f18,
151 FloatRegisters::Double};
152 static constexpr FloatRegister f19 = {FloatRegisters::f19,
153 FloatRegisters::Double};
154 static constexpr FloatRegister f20 = {FloatRegisters::f20,
155 FloatRegisters::Double};
156 static constexpr FloatRegister f21 = {FloatRegisters::f21,
157 FloatRegisters::Double};
158 static constexpr FloatRegister f22 = {FloatRegisters::f22,
159 FloatRegisters::Double};
160 static constexpr FloatRegister f23 = {FloatRegisters::f23,
161 FloatRegisters::Double};
162 static constexpr FloatRegister f24 = {FloatRegisters::f24,
163 FloatRegisters::Double};
164 static constexpr FloatRegister f25 = {FloatRegisters::f25,
165 FloatRegisters::Double};
166 static constexpr FloatRegister f26 = {FloatRegisters::f26,
167 FloatRegisters::Double};
168 static constexpr FloatRegister f27 = {FloatRegisters::f27,
169 FloatRegisters::Double};
170 static constexpr FloatRegister f28 = {FloatRegisters::f28,
171 FloatRegisters::Double};
172 static constexpr FloatRegister f29 = {FloatRegisters::f29,
173 FloatRegisters::Double};
174 static constexpr FloatRegister f30 = {FloatRegisters::f30,
175 FloatRegisters::Double};
176 static constexpr FloatRegister f31 = {FloatRegisters::f31,
177 FloatRegisters::Double};
178
179 // MIPS64 CPUs can only load multibyte data that is "naturally"
180 // eight-byte-aligned, sp register should be sixteen-byte-aligned.
181 static constexpr uint32_t ABIStackAlignment = 16;
182 static constexpr uint32_t JitStackAlignment = 16;
183
184 static constexpr uint32_t JitStackValueAlignment =
185 JitStackAlignment / sizeof(Value);
186 static_assert(JitStackAlignment % sizeof(Value) == 0 &&
187 JitStackValueAlignment >= 1,
188 "Stack alignment should be a non-zero multiple of sizeof(Value)");
189
190 // TODO this is just a filler to prevent a build failure. The MIPS SIMD
191 // alignment requirements still need to be explored.
192 // TODO Copy the static_asserts from x64/x86 assembler files.
193 static constexpr uint32_t SimdMemoryAlignment = 16;
194
195 static constexpr uint32_t WasmStackAlignment = SimdMemoryAlignment;
196 static const uint32_t WasmTrapInstructionLength = 4;
197
198 // See comments in wasm::GenerateFunctionPrologue. The difference between these
199 // is the size of the largest callable prologue on the platform.
200 static constexpr uint32_t WasmCheckedCallEntryOffset = 0u;
201
202 static constexpr Scale ScalePointer = TimesEight;
203
204 class Assembler : public AssemblerMIPSShared {
205 public:
206 Assembler() : AssemblerMIPSShared() {}
207
208 static uintptr_t GetPointer(uint8_t*);
209
210 using AssemblerMIPSShared::bind;
211
212 static void Bind(uint8_t* rawCode, const CodeLabel& label);
213
214 void processCodeLabels(uint8_t* rawCode);
215
216 static void TraceJumpRelocations(JSTracer* trc, JitCode* code,
217 CompactBufferReader& reader);
218 static void TraceDataRelocations(JSTracer* trc, JitCode* code,
219 CompactBufferReader& reader);
220
221 void bind(InstImm* inst, uintptr_t branch, uintptr_t target);
222
223 // Copy the assembly code to the given buffer, and perform any pending
224 // relocations relying on the target address.
225 void executableCopy(uint8_t* buffer);
226
227 static uint32_t PatchWrite_NearCallSize();
228
229 static uint64_t ExtractLoad64Value(Instruction* inst0);
230 static void UpdateLoad64Value(Instruction* inst0, uint64_t value);
231 static void WriteLoad64Instructions(Instruction* inst0, Register reg,
232 uint64_t value);
233
234 static void PatchWrite_NearCall(CodeLocationLabel start,
235 CodeLocationLabel toCall);
236 static void PatchDataWithValueCheck(CodeLocationLabel label, ImmPtr newValue,
237 ImmPtr expectedValue);
238 static void PatchDataWithValueCheck(CodeLocationLabel label,
239 PatchedImmPtr newValue,
240 PatchedImmPtr expectedValue);
241
242 static uint64_t ExtractInstructionImmediate(uint8_t* code);
243
244 static void ToggleCall(CodeLocationLabel inst_, bool enabled);
245 }; // Assembler
246
247 static const uint32_t NumIntArgRegs = 8;
248 static const uint32_t NumFloatArgRegs = NumIntArgRegs;
249
250 static inline bool GetIntArgReg(uint32_t usedArgSlots, Register* out) {
251 if (usedArgSlots < NumIntArgRegs) {
252 *out = Register::FromCode(a0.code() + usedArgSlots);
253 return true;
254 }
255 return false;
256 }
257
258 static inline bool GetFloatArgReg(uint32_t usedArgSlots, FloatRegister* out) {
259 if (usedArgSlots < NumFloatArgRegs) {
260 *out = FloatRegister::FromCode(f12.code() + usedArgSlots);
261 return true;
262 }
263 return false;
264 }
265
266 // Get a register in which we plan to put a quantity that will be used as an
267 // integer argument. This differs from GetIntArgReg in that if we have no more
268 // actual argument registers to use we will fall back on using whatever
269 // CallTempReg* don't overlap the argument registers, and only fail once those
270 // run out too.
271 static inline bool GetTempRegForIntArg(uint32_t usedIntArgs,
272 uint32_t usedFloatArgs, Register* out) {
273 // NOTE: We can't properly determine which regs are used if there are
274 // float arguments. If this is needed, we will have to guess.
275 MOZ_ASSERT(usedFloatArgs == 0);
276
277 if (GetIntArgReg(usedIntArgs, out)) {
278 return true;
279 }
280 // Unfortunately, we have to assume things about the point at which
281 // GetIntArgReg returns false, because we need to know how many registers it
282 // can allocate.
283 usedIntArgs -= NumIntArgRegs;
284 if (usedIntArgs >= NumCallTempNonArgRegs) {
285 return false;
286 }
287 *out = CallTempNonArgRegs[usedIntArgs];
288 return true;
289 }
290
291 } // namespace jit
292 } // namespace js
293
294 #endif /* jit_mips64_Assembler_mips64_h */