Debian Codegraph @

js/src/jit/arm/Assembler-arm.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_arm_Assembler_arm_h
8 #define jit_arm_Assembler_arm_h
9
10 #include "mozilla/Attributes.h"
11 #include "mozilla/MathAlgorithms.h"
12
13 #include <algorithm>
14 #include <iterator>
15 #include <type_traits>
16
17 #include "jit/arm/Architecture-arm.h"
18 #include "jit/arm/disasm/Disasm-arm.h"
19 #include "jit/CompactBuffer.h"
20 #include "jit/JitCode.h"
21 #include "jit/shared/Assembler-shared.h"
22 #include "jit/shared/Disassembler-shared.h"
23 #include "jit/shared/IonAssemblerBufferWithConstantPools.h"
24 #include "wasm/WasmTypeDecls.h"
25
26 union PoolHintPun;
27
28 namespace js {
29 namespace jit {
30
31 using LiteralDoc = DisassemblerSpew::LiteralDoc;
32 using LabelDoc = DisassemblerSpew::LabelDoc;
33
34 // NOTE: there are duplicates in this list! Sometimes we want to specifically
35 // refer to the link register as a link register (bl lr is much clearer than bl
36 // r14). HOWEVER, this register can easily be a gpr when it is not busy holding
37 // the return address.
38 static constexpr Register r0{Registers::r0};
39 static constexpr Register r1{Registers::r1};
40 static constexpr Register r2{Registers::r2};
41 static constexpr Register r3{Registers::r3};
42 static constexpr Register r4{Registers::r4};
43 static constexpr Register r5{Registers::r5};
44 static constexpr Register r6{Registers::r6};
45 static constexpr Register r7{Registers::r7};
46 static constexpr Register r8{Registers::r8};
47 static constexpr Register r9{Registers::r9};
48 static constexpr Register r10{Registers::r10};
49 static constexpr Register r11{Registers::r11};
50 static constexpr Register r12{Registers::ip};
51 static constexpr Register ip{Registers::ip};
52 static constexpr Register sp{Registers::sp};
53 static constexpr Register r14{Registers::lr};
54 static constexpr Register lr{Registers::lr};
55 static constexpr Register pc{Registers::pc};
56
57 static constexpr Register ScratchRegister{Registers::ip};
58
59 // Helper class for ScratchRegister usage. Asserts that only one piece
60 // of code thinks it has exclusive ownership of the scratch register.
61 struct ScratchRegisterScope : public AutoRegisterScope {
62 explicit ScratchRegisterScope(MacroAssembler& masm)
63 : AutoRegisterScope(masm, ScratchRegister) {}
64 };
65
66 struct SecondScratchRegisterScope : public AutoRegisterScope {
67 explicit SecondScratchRegisterScope(MacroAssembler& masm);
68 };
69
70 class MOZ_RAII AutoNonDefaultSecondScratchRegister {
71 public:
72 explicit AutoNonDefaultSecondScratchRegister(MacroAssembler& masm,
73 Register reg);
74 ~AutoNonDefaultSecondScratchRegister();
75
76 private:
77 Register prevSecondScratch_;
78 MacroAssembler& masm_;
79 };
80
81 static constexpr Register OsrFrameReg = r3;
82 static constexpr Register CallTempReg0 = r5;
83 static constexpr Register CallTempReg1 = r6;
84 static constexpr Register CallTempReg2 = r7;
85 static constexpr Register CallTempReg3 = r8;
86 static constexpr Register CallTempReg4 = r0;
87 static constexpr Register CallTempReg5 = r1;
88
89 static constexpr Register IntArgReg0 = r0;
90 static constexpr Register IntArgReg1 = r1;
91 static constexpr Register IntArgReg2 = r2;
92 static constexpr Register IntArgReg3 = r3;
93 static constexpr Register HeapReg = r10;
94 static constexpr Register CallTempNonArgRegs[] = {r5, r6, r7, r8};
95 static const uint32_t NumCallTempNonArgRegs = std::size(CallTempNonArgRegs);
96
97 // These register assignments for the 64-bit atomic ops are frequently too
98 // constraining, but we have no way of expressing looser constraints to the
99 // register allocator.
100
101 // CompareExchange: Any two odd/even pairs would do for `new` and `out`, and any
102 // pair would do for `old`, so long as none of them overlap.
103
104 static constexpr Register CmpXchgOldLo = r4;
105 static constexpr Register CmpXchgOldHi = r5;
106 static constexpr Register64 CmpXchgOld64 =
107 Register64(CmpXchgOldHi, CmpXchgOldLo);
108 static constexpr Register CmpXchgNewLo = IntArgReg2;
109 static constexpr Register CmpXchgNewHi = IntArgReg3;
110 static constexpr Register64 CmpXchgNew64 =
111 Register64(CmpXchgNewHi, CmpXchgNewLo);
112 static constexpr Register CmpXchgOutLo = IntArgReg0;
113 static constexpr Register CmpXchgOutHi = IntArgReg1;
114 static constexpr Register64 CmpXchgOut64 =
115 Register64(CmpXchgOutHi, CmpXchgOutLo);
116
117 // Exchange: Any two non-equal odd/even pairs would do for `new` and `out`.
118
119 static constexpr Register XchgNewLo = IntArgReg2;
120 static constexpr Register XchgNewHi = IntArgReg3;
121 static constexpr Register64 XchgNew64 = Register64(XchgNewHi, XchgNewLo);
122 static constexpr Register XchgOutLo = IntArgReg0;
123 static constexpr Register XchgOutHi = IntArgReg1;
124
125 // Atomic rmw operations: Any two odd/even pairs would do for `tmp` and `out`,
126 // and any pair would do for `val`, so long as none of them overlap.
127
128 static constexpr Register FetchOpValLo = r4;
129 static constexpr Register FetchOpValHi = r5;
130 static constexpr Register64 FetchOpVal64 =
131 Register64(FetchOpValHi, FetchOpValLo);
132 static constexpr Register FetchOpTmpLo = IntArgReg2;
133 static constexpr Register FetchOpTmpHi = IntArgReg3;
134 static constexpr Register64 FetchOpTmp64 =
135 Register64(FetchOpTmpHi, FetchOpTmpLo);
136 static constexpr Register FetchOpOutLo = IntArgReg0;
137 static constexpr Register FetchOpOutHi = IntArgReg1;
138 static constexpr Register64 FetchOpOut64 =
139 Register64(FetchOpOutHi, FetchOpOutLo);
140
141 class ABIArgGenerator {
142 unsigned intRegIndex_;
143 unsigned floatRegIndex_;
144 uint32_t stackOffset_;
145 ABIArg current_;
146
147 // ARM can either use HardFp (use float registers for float arguments), or
148 // SoftFp (use general registers for float arguments) ABI. We keep this
149 // switch as a runtime switch because wasm always use the HardFp back-end
150 // while the calls to native functions have to use the one provided by the
151 // system.
152 bool useHardFp_;
153
154 ABIArg softNext(MIRType argType);
155 ABIArg hardNext(MIRType argType);
156
157 public:
158 ABIArgGenerator();
159
160 void setUseHardFp(bool useHardFp) {
161 MOZ_ASSERT(intRegIndex_ == 0 && floatRegIndex_ == 0);
162 useHardFp_ = useHardFp;
163 }
164 ABIArg next(MIRType argType);
165 ABIArg& current() { return current_; }
166 uint32_t stackBytesConsumedSoFar() const { return stackOffset_; }
167 void increaseStackOffset(uint32_t bytes) { stackOffset_ += bytes; }
168 };
169
170 bool IsUnaligned(const wasm::MemoryAccessDesc& access);
171
172 // These registers may be volatile or nonvolatile.
173 static constexpr Register ABINonArgReg0 = r4;
174 static constexpr Register ABINonArgReg1 = r5;
175 static constexpr Register ABINonArgReg2 = r6;
176 static constexpr Register ABINonArgReg3 = r7;
177
178 // This register may be volatile or nonvolatile. Avoid d15 which is the
179 // ScratchDoubleReg_.
180 static constexpr FloatRegister ABINonArgDoubleReg{FloatRegisters::d8,
181 VFPRegister::Double};
182
183 // These registers may be volatile or nonvolatile.
184 // Note: these three registers are all guaranteed to be different
185 static constexpr Register ABINonArgReturnReg0 = r4;
186 static constexpr Register ABINonArgReturnReg1 = r5;
187 static constexpr Register ABINonVolatileReg = r6;
188
189 // This register is guaranteed to be clobberable during the prologue and
190 // epilogue of an ABI call which must preserve both ABI argument, return
191 // and non-volatile registers.
192 static constexpr Register ABINonArgReturnVolatileReg = lr;
193
194 // Instance pointer argument register for WebAssembly functions. This must not
195 // alias any other register used for passing function arguments or return
196 // values. Preserved by WebAssembly functions.
197 static constexpr Register InstanceReg = r9;
198
199 // Registers used for wasm table calls. These registers must be disjoint
200 // from the ABI argument registers, InstanceReg and each other.
201 static constexpr Register WasmTableCallScratchReg0 = ABINonArgReg0;
202 static constexpr Register WasmTableCallScratchReg1 = ABINonArgReg1;
203 static constexpr Register WasmTableCallSigReg = ABINonArgReg2;
204 static constexpr Register WasmTableCallIndexReg = ABINonArgReg3;
205
206 // Registers used for ref calls.
207 static constexpr Register WasmCallRefCallScratchReg0 = ABINonArgReg0;
208 static constexpr Register WasmCallRefCallScratchReg1 = ABINonArgReg1;
209 static constexpr Register WasmCallRefCallScratchReg2 = ABINonArgReg2;
210 static constexpr Register WasmCallRefReg = ABINonArgReg3;
211
212 // Registers used for wasm tail calls operations.
213 static constexpr Register WasmTailCallInstanceScratchReg = ABINonArgReg1;
214 static constexpr Register WasmTailCallRAScratchReg = lr;
215 static constexpr Register WasmTailCallFPScratchReg = ABINonArgReg3;
216
217 // Register used as a scratch along the return path in the fast js -> wasm stub
218 // code. This must not overlap ReturnReg, JSReturnOperand, or InstanceReg.
219 // It must be a volatile register.
220 static constexpr Register WasmJitEntryReturnScratch = r5;
221
222 static constexpr Register PreBarrierReg = r1;
223
224 static constexpr Register InterpreterPCReg = r9;
225
226 static constexpr Register InvalidReg{Registers::invalid_reg};
227 static constexpr FloatRegister InvalidFloatReg;
228
229 static constexpr Register JSReturnReg_Type = r3;
230 static constexpr Register JSReturnReg_Data = r2;
231 static constexpr Register StackPointer = sp;
232 static constexpr Register FramePointer = r11;
233 static constexpr Register ReturnReg = r0;
234 static constexpr Register64 ReturnReg64(r1, r0);
235
236 // The attribute '__value_in_regs' alters the calling convention of a function
237 // so that a structure of up to four elements can be returned via the argument
238 // registers rather than being written to memory.
239 static constexpr Register ReturnRegVal0 = IntArgReg0;
240 static constexpr Register ReturnRegVal1 = IntArgReg1;
241 static constexpr Register ReturnRegVal2 = IntArgReg2;
242 static constexpr Register ReturnRegVal3 = IntArgReg3;
243
244 static constexpr FloatRegister ReturnFloat32Reg = {FloatRegisters::d0,
245 VFPRegister::Single};
246 static constexpr FloatRegister ReturnDoubleReg = {FloatRegisters::d0,
247 VFPRegister::Double};
248 static constexpr FloatRegister ReturnSimd128Reg = InvalidFloatReg;
249 static constexpr FloatRegister ScratchFloat32Reg_ = {FloatRegisters::s30,
250 VFPRegister::Single};
251 static constexpr FloatRegister ScratchDoubleReg_ = {FloatRegisters::d15,
252 VFPRegister::Double};
253 static constexpr FloatRegister ScratchSimd128Reg = InvalidFloatReg;
254 static constexpr FloatRegister ScratchUIntReg = {FloatRegisters::d15,
255 VFPRegister::UInt};
256 static constexpr FloatRegister ScratchIntReg = {FloatRegisters::d15,
257 VFPRegister::Int};
258
259 // Do not reference ScratchFloat32Reg_ directly, use ScratchFloat32Scope
260 // instead.
261 struct ScratchFloat32Scope : public AutoFloatRegisterScope {
262 explicit ScratchFloat32Scope(MacroAssembler& masm)
263 : AutoFloatRegisterScope(masm, ScratchFloat32Reg_) {}
264 };
265
266 // Do not reference ScratchDoubleReg_ directly, use ScratchDoubleScope instead.
267 struct ScratchDoubleScope : public AutoFloatRegisterScope {
268 explicit ScratchDoubleScope(MacroAssembler& masm)
269 : AutoFloatRegisterScope(masm, ScratchDoubleReg_) {}
270 };
271
272 // Registers used by RegExpMatcher and RegExpExecMatch stubs (do not use
273 // JSReturnOperand).
274 static constexpr Register RegExpMatcherRegExpReg = CallTempReg0;
275 static constexpr Register RegExpMatcherStringReg = CallTempReg1;
276 static constexpr Register RegExpMatcherLastIndexReg = CallTempReg2;
277
278 // Registers used by RegExpExecTest stub (do not use ReturnReg).
279 static constexpr Register RegExpExecTestRegExpReg = CallTempReg0;
280 static constexpr Register RegExpExecTestStringReg = CallTempReg1;
281
282 // Registers used by RegExpSearcher stub (do not use ReturnReg).
283 static constexpr Register RegExpSearcherRegExpReg = CallTempReg0;
284 static constexpr Register RegExpSearcherStringReg = CallTempReg1;
285 static constexpr Register RegExpSearcherLastIndexReg = CallTempReg2;
286
287 static constexpr FloatRegister d0 = {FloatRegisters::d0, VFPRegister::Double};
288 static constexpr FloatRegister d1 = {FloatRegisters::d1, VFPRegister::Double};
289 static constexpr FloatRegister d2 = {FloatRegisters::d2, VFPRegister::Double};
290 static constexpr FloatRegister d3 = {FloatRegisters::d3, VFPRegister::Double};
291 static constexpr FloatRegister d4 = {FloatRegisters::d4, VFPRegister::Double};
292 static constexpr FloatRegister d5 = {FloatRegisters::d5, VFPRegister::Double};
293 static constexpr FloatRegister d6 = {FloatRegisters::d6, VFPRegister::Double};
294 static constexpr FloatRegister d7 = {FloatRegisters::d7, VFPRegister::Double};
295 static constexpr FloatRegister d8 = {FloatRegisters::d8, VFPRegister::Double};
296 static constexpr FloatRegister d9 = {FloatRegisters::d9, VFPRegister::Double};
297 static constexpr FloatRegister d10 = {FloatRegisters::d10, VFPRegister::Double};
298 static constexpr FloatRegister d11 = {FloatRegisters::d11, VFPRegister::Double};
299 static constexpr FloatRegister d12 = {FloatRegisters::d12, VFPRegister::Double};
300 static constexpr FloatRegister d13 = {FloatRegisters::d13, VFPRegister::Double};
301 static constexpr FloatRegister d14 = {FloatRegisters::d14, VFPRegister::Double};
302 static constexpr FloatRegister d15 = {FloatRegisters::d15, VFPRegister::Double};
303
304 // For maximal awesomeness, 8 should be sufficent. ldrd/strd (dual-register
305 // load/store) operate in a single cycle when the address they are dealing with
306 // is 8 byte aligned. Also, the ARM abi wants the stack to be 8 byte aligned at
307 // function boundaries. I'm trying to make sure this is always true.
308 static constexpr uint32_t ABIStackAlignment = 8;
309 static constexpr uint32_t CodeAlignment = 8;
310 static constexpr uint32_t JitStackAlignment = 8;
311
312 static constexpr uint32_t JitStackValueAlignment =
313 JitStackAlignment / sizeof(Value);
314 static_assert(JitStackAlignment % sizeof(Value) == 0 &&
315 JitStackValueAlignment >= 1,
316 "Stack alignment should be a non-zero multiple of sizeof(Value)");
317
318 static constexpr uint32_t SimdMemoryAlignment = 8;
319
320 static_assert(CodeAlignment % SimdMemoryAlignment == 0,
321 "Code alignment should be larger than any of the alignments "
322 "which are used for "
323 "the constant sections of the code buffer. Thus it should be "
324 "larger than the "
325 "alignment for SIMD constants.");
326
327 static_assert(JitStackAlignment % SimdMemoryAlignment == 0,
328 "Stack alignment should be larger than any of the alignments "
329 "which are used for "
330 "spilled values. Thus it should be larger than the alignment "
331 "for SIMD accesses.");
332
333 static const uint32_t WasmStackAlignment = SimdMemoryAlignment;
334 static const uint32_t WasmTrapInstructionLength = 4;
335
336 // See comments in wasm::GenerateFunctionPrologue. The difference between these
337 // is the size of the largest callable prologue on the platform.
338 static constexpr uint32_t WasmCheckedCallEntryOffset = 0u;
339
340 static const Scale ScalePointer = TimesFour;
341
342 class Instruction;
343 class InstBranchImm;
344 uint32_t RM(Register r);
345 uint32_t RS(Register r);
346 uint32_t RD(Register r);
347 uint32_t RT(Register r);
348 uint32_t RN(Register r);
349
350 uint32_t maybeRD(Register r);
351 uint32_t maybeRT(Register r);
352 uint32_t maybeRN(Register r);
353
354 Register toRN(Instruction i);
355 Register toRM(Instruction i);
356 Register toRD(Instruction i);
357 Register toR(Instruction i);
358
359 class VFPRegister;
360 uint32_t VD(VFPRegister vr);
361 uint32_t VN(VFPRegister vr);
362 uint32_t VM(VFPRegister vr);
363
364 // For being passed into the generic vfp instruction generator when there is an
365 // instruction that only takes two registers.
366 static constexpr VFPRegister NoVFPRegister(VFPRegister::Double, 0, false, true);
367
368 struct ImmTag : public Imm32 {
369 explicit ImmTag(JSValueTag mask) : Imm32(int32_t(mask)) {}
370 };
371
372 struct ImmType : public ImmTag {
373 explicit ImmType(JSValueType type) : ImmTag(JSVAL_TYPE_TO_TAG(type)) {}
374 };
375
376 enum Index {
377 Offset = 0 << 21 | 1 << 24,
378 PreIndex = 1 << 21 | 1 << 24,
379 PostIndex = 0 << 21 | 0 << 24
380 // The docs were rather unclear on this. It sounds like
381 // 1 << 21 | 0 << 24 encodes dtrt.
382 };
383
384 enum IsImmOp2_ { IsImmOp2 = 1 << 25, IsNotImmOp2 = 0 << 25 };
385 enum IsImmDTR_ { IsImmDTR = 0 << 25, IsNotImmDTR = 1 << 25 };
386 // For the extra memory operations, ldrd, ldrsb, ldrh.
387 enum IsImmEDTR_ { IsImmEDTR = 1 << 22, IsNotImmEDTR = 0 << 22 };
388
389 enum ShiftType {
390 LSL = 0, // << 5
391 LSR = 1, // << 5
392 ASR = 2, // << 5
393 ROR = 3, // << 5
394 RRX = ROR // RRX is encoded as ROR with a 0 offset.
395 };
396
397 // Modes for STM/LDM. Names are the suffixes applied to the instruction.
398 enum DTMMode {
399 A = 0 << 24, // empty / after
400 B = 1 << 24, // full / before
401 D = 0 << 23, // decrement
402 I = 1 << 23, // increment
403 DA = D | A,
404 DB = D | B,
405 IA = I | A,
406 IB = I | B
407 };
408
409 enum DTMWriteBack { WriteBack = 1 << 21, NoWriteBack = 0 << 21 };
410
411 // Condition code updating mode.
412 enum SBit {
413 SetCC = 1 << 20, // Set condition code.
414 LeaveCC = 0 << 20 // Leave condition code unchanged.
415 };
416
417 enum LoadStore { IsLoad = 1 << 20, IsStore = 0 << 20 };
418
419 // You almost never want to use this directly. Instead, you wantto pass in a
420 // signed constant, and let this bit be implicitly set for you. This is however,
421 // necessary if we want a negative index.
422 enum IsUp_ { IsUp = 1 << 23, IsDown = 0 << 23 };
423 enum ALUOp {
424 OpMov = 0xd << 21,
425 OpMvn = 0xf << 21,
426 OpAnd = 0x0 << 21,
427 OpBic = 0xe << 21,
428 OpEor = 0x1 << 21,
429 OpOrr = 0xc << 21,
430 OpAdc = 0x5 << 21,
431 OpAdd = 0x4 << 21,
432 OpSbc = 0x6 << 21,
433 OpSub = 0x2 << 21,
434 OpRsb = 0x3 << 21,
435 OpRsc = 0x7 << 21,
436 OpCmn = 0xb << 21,
437 OpCmp = 0xa << 21,
438 OpTeq = 0x9 << 21,
439 OpTst = 0x8 << 21,
440 OpInvalid = -1
441 };
442
443 enum MULOp {
444 OpmMul = 0 << 21,
445 OpmMla = 1 << 21,
446 OpmUmaal = 2 << 21,
447 OpmMls = 3 << 21,
448 OpmUmull = 4 << 21,
449 OpmUmlal = 5 << 21,
450 OpmSmull = 6 << 21,
451 OpmSmlal = 7 << 21
452 };
453 enum BranchTag {
454 OpB = 0x0a000000,
455 OpBMask = 0x0f000000,
456 OpBDestMask = 0x00ffffff,
457 OpBl = 0x0b000000,
458 OpBlx = 0x012fff30,
459 OpBx = 0x012fff10
460 };
461
462 // Just like ALUOp, but for the vfp instruction set.
463 enum VFPOp {
464 OpvMul = 0x2 << 20,
465 OpvAdd = 0x3 << 20,
466 OpvSub = 0x3 << 20 | 0x1 << 6,
467 OpvDiv = 0x8 << 20,
468 OpvMov = 0xB << 20 | 0x1 << 6,
469 OpvAbs = 0xB << 20 | 0x3 << 6,
470 OpvNeg = 0xB << 20 | 0x1 << 6 | 0x1 << 16,
471 OpvSqrt = 0xB << 20 | 0x3 << 6 | 0x1 << 16,
472 OpvCmp = 0xB << 20 | 0x1 << 6 | 0x4 << 16,
473 OpvCmpz = 0xB << 20 | 0x1 << 6 | 0x5 << 16
474 };
475
476 // Negate the operation, AND negate the immediate that we were passed in.
477 ALUOp ALUNeg(ALUOp op, Register dest, Register scratch, Imm32* imm,
478 Register* negDest);
479 bool can_dbl(ALUOp op);
480 bool condsAreSafe(ALUOp op);
481
482 // If there is a variant of op that has a dest (think cmp/sub) return that
483 // variant of it.
484 ALUOp getDestVariant(ALUOp op);
485
486 static constexpr ValueOperand JSReturnOperand{JSReturnReg_Type,
487 JSReturnReg_Data};
488 static const ValueOperand softfpReturnOperand = ValueOperand(r1, r0);
489
490 // All of these classes exist solely to shuffle data into the various operands.
491 // For example Operand2 can be an imm8, a register-shifted-by-a-constant or a
492 // register-shifted-by-a-register. We represent this in C++ by having a base
493 // class Operand2, which just stores the 32 bits of data as they will be encoded
494 // in the instruction. You cannot directly create an Operand2 since it is
495 // tricky, and not entirely sane to do so. Instead, you create one of its child
496 // classes, e.g. Imm8. Imm8's constructor takes a single integer argument. Imm8
497 // will verify that its argument can be encoded as an ARM 12 bit imm8, encode it
498 // using an Imm8data, and finally call its parent's (Operand2) constructor with
499 // the Imm8data. The Operand2 constructor will then call the Imm8data's encode()
500 // function to extract the raw bits from it.
501 //
502 // In the future, we should be able to extract data from the Operand2 by asking
503 // it for its component Imm8data structures. The reason this is so horribly
504 // round-about is we wanted to have Imm8 and RegisterShiftedRegister inherit
505 // directly from Operand2 but have all of them take up only a single word of
506 // storage. We also wanted to avoid passing around raw integers at all since
507 // they are error prone.
508 class Op2Reg;
509 class O2RegImmShift;
510 class O2RegRegShift;
511
512 namespace datastore {
513
514 class Reg {
515 // The "second register".
516 uint32_t rm_ : 4;
517 // Do we get another register for shifting.
518 uint32_t rrs_ : 1;
519 uint32_t type_ : 2;
520 // We'd like this to be a more sensible encoding, but that would need to be
521 // a struct and that would not pack :(
522 uint32_t shiftAmount_ : 5;
523
524 protected:
525 // Mark as a protected field to avoid unused private field warnings.
526 uint32_t pad_ : 20;
527
528 public:
529 Reg(uint32_t rm, ShiftType type, uint32_t rsr, uint32_t shiftAmount)
530 : rm_(rm), rrs_(rsr), type_(type), shiftAmount_(shiftAmount), pad_(0) {}
531 explicit Reg(const Op2Reg& op) { memcpy(this, &op, sizeof(*this)); }
532
533 uint32_t shiftAmount() const { return shiftAmount_; }
534
535 uint32_t encode() const {
536 return rm_ | (rrs_ << 4) | (type_ << 5) | (shiftAmount_ << 7);
537 }
538 };
539
540 // Op2 has a mode labelled "<imm8m>", which is arm's magical immediate encoding.
541 // Some instructions actually get 8 bits of data, which is called Imm8Data
542 // below. These should have edit distance > 1, but this is how it is for now.
543 class Imm8mData {
544 uint32_t data_ : 8;
545 uint32_t rot_ : 4;
546
547 protected:
548 // Mark as a protected field to avoid unused private field warnings.
549 uint32_t buff_ : 19;
550
551 private:
552 // Throw in an extra bit that will be 1 if we can't encode this properly.
553 // if we can encode it properly, a simple "|" will still suffice to meld it
554 // into the instruction.
555 uint32_t invalid_ : 1;
556
557 public:
558 // Default constructor makes an invalid immediate.
559 Imm8mData() : data_(0xff), rot_(0xf), buff_(0), invalid_(true) {}
560
561 Imm8mData(uint32_t data, uint32_t rot)
562 : data_(data), rot_(rot), buff_(0), invalid_(false) {
563 MOZ_ASSERT(data == data_);
564 MOZ_ASSERT(rot == rot_);
565 }
566
567 bool invalid() const { return invalid_; }
568
569 uint32_t encode() const {
570 MOZ_ASSERT(!invalid_);
571 return data_ | (rot_ << 8);
572 };
573 };
574
575 class Imm8Data {
576 uint32_t imm4L_ : 4;
577
578 protected:
579 // Mark as a protected field to avoid unused private field warnings.
580 uint32_t pad_ : 4;
581
582 private:
583 uint32_t imm4H_ : 4;
584
585 public:
586 explicit Imm8Data(uint32_t imm) : imm4L_(imm & 0xf), imm4H_(imm >> 4) {
587 MOZ_ASSERT(imm <= 0xff);
588 }
589
590 uint32_t encode() const { return imm4L_ | (imm4H_ << 8); };
591 };
592
593 // VLDR/VSTR take an 8 bit offset, which is implicitly left shifted by 2.
594 class Imm8VFPOffData {
595 uint32_t data_;
596
597 public:
598 explicit Imm8VFPOffData(uint32_t imm) : data_(imm) {
599 MOZ_ASSERT((imm & ~(0xff)) == 0);
600 }
601 uint32_t encode() const { return data_; };
602 };
603
604 // ARM can magically encode 256 very special immediates to be moved into a
605 // register.
606 struct Imm8VFPImmData {
607 // This structure's members are public and it has no constructor to
608 // initialize them, for a very special reason. Were this structure to
609 // have a constructor, the initialization for DoubleEncoder's internal
610 // table (see below) would require a rather large static constructor on
611 // some of our supported compilers. The known solution to this is to mark
612 // the constructor constexpr, but, again, some of our supported
613 // compilers don't support constexpr! So we are reduced to public
614 // members and eschewing a constructor in hopes that the initialization
615 // of DoubleEncoder's table is correct.
616 uint32_t imm4L : 4;
617 uint32_t imm4H : 4;
618 int32_t isInvalid : 24;
619
620 uint32_t encode() const {
621 // This assert is an attempting at ensuring that we don't create random
622 // instances of this structure and then asking to encode() it.
623 MOZ_ASSERT(isInvalid == 0);
624 return imm4L | (imm4H << 16);
625 };
626 };
627
628 class Imm12Data {
629 uint32_t data_ : 12;
630
631 public:
632 explicit Imm12Data(uint32_t imm) : data_(imm) { MOZ_ASSERT(data_ == imm); }
633
634 uint32_t encode() const { return data_; }
635 };
636
637 class RIS {
638 uint32_t shiftAmount_ : 5;
639
640 public:
641 explicit RIS(uint32_t imm) : shiftAmount_(imm) {
642 MOZ_ASSERT(shiftAmount_ == imm);
643 }
644
645 explicit RIS(Reg r) : shiftAmount_(r.shiftAmount()) {}
646
647 uint32_t encode() const { return shiftAmount_; }
648 };
649
650 class RRS {
651 protected:
652 // Mark as a protected field to avoid unused private field warnings.
653 uint32_t mustZero_ : 1;
654
655 private:
656 // The register that holds the shift amount.
657 uint32_t rs_ : 4;
658
659 public:
660 explicit RRS(uint32_t rs) : rs_(rs) { MOZ_ASSERT(rs_ == rs); }
661
662 uint32_t encode() const { return rs_ << 1; }
663 };
664
665 } // namespace datastore
666
667 class MacroAssemblerARM;
668 class Operand;
669
670 class Operand2 {
671 friend class Operand;
672 friend class MacroAssemblerARM;
673 friend class InstALU;
674
675 uint32_t oper_ : 31;
676 uint32_t invalid_ : 1;
677
678 protected:
679 explicit Operand2(datastore::Imm8mData base)
680 : oper_(base.invalid() ? -1 : (base.encode() | uint32_t(IsImmOp2))),
681 invalid_(base.invalid()) {}
682
683 explicit Operand2(datastore::Reg base)
684 : oper_(base.encode() | uint32_t(IsNotImmOp2)), invalid_(false) {}
685
686 private:
687 explicit Operand2(uint32_t blob) : oper_(blob), invalid_(false) {}
688
689 public:
690 bool isO2Reg() const { return !(oper_ & IsImmOp2); }
691
692 Op2Reg toOp2Reg() const;
693
694 bool isImm8() const { return oper_ & IsImmOp2; }
695
696 bool invalid() const { return invalid_; }
697
698 uint32_t encode() const { return oper_; }
699 };
700
701 class Imm8 : public Operand2 {
702 public:
703 explicit Imm8(uint32_t imm) : Operand2(EncodeImm(imm)) {}
704
705 static datastore::Imm8mData EncodeImm(uint32_t imm) {
706 // RotateLeft below may not be called with a shift of zero.
707 if (imm <= 0xFF) {
708 return datastore::Imm8mData(imm, 0);
709 }
710
711 // An encodable integer has a maximum of 8 contiguous set bits,
712 // with an optional wrapped left rotation to even bit positions.
713 for (int rot = 1; rot < 16; rot++) {
714 uint32_t rotimm = mozilla::RotateLeft(imm, rot * 2);
715 if (rotimm <= 0xFF) {
716 return datastore::Imm8mData(rotimm, rot);
717 }
718 }
719 return datastore::Imm8mData();
720 }
721
722 // Pair template?
723 struct TwoImm8mData {
724 datastore::Imm8mData fst_, snd_;
725
726 TwoImm8mData() = default;
727
728 TwoImm8mData(datastore::Imm8mData fst, datastore::Imm8mData snd)
729 : fst_(fst), snd_(snd) {}
730
731 datastore::Imm8mData fst() const { return fst_; }
732 datastore::Imm8mData snd() const { return snd_; }
733 };
734
735 static TwoImm8mData EncodeTwoImms(uint32_t);
736 };
737
738 class Op2Reg : public Operand2 {
739 public:
740 explicit Op2Reg(Register rm, ShiftType type, datastore::RIS shiftImm)
741 : Operand2(datastore::Reg(rm.code(), type, 0, shiftImm.encode())) {}
742
743 explicit Op2Reg(Register rm, ShiftType type, datastore::RRS shiftReg)
744 : Operand2(datastore::Reg(rm.code(), type, 1, shiftReg.encode())) {}
745 };
746
747 static_assert(sizeof(Op2Reg) == sizeof(datastore::Reg),
748 "datastore::Reg(const Op2Reg&) constructor relies on Reg/Op2Reg "
749 "having same size");
750
751 class O2RegImmShift : public Op2Reg {
752 public:
753 explicit O2RegImmShift(Register rn, ShiftType type, uint32_t shift)
754 : Op2Reg(rn, type, datastore::RIS(shift)) {}
755 };
756
757 class O2RegRegShift : public Op2Reg {
758 public:
759 explicit O2RegRegShift(Register rn, ShiftType type, Register rs)
760 : Op2Reg(rn, type, datastore::RRS(rs.code())) {}
761 };
762
763 O2RegImmShift O2Reg(Register r);
764 O2RegImmShift lsl(Register r, int amt);
765 O2RegImmShift lsr(Register r, int amt);
766 O2RegImmShift asr(Register r, int amt);
767 O2RegImmShift rol(Register r, int amt);
768 O2RegImmShift ror(Register r, int amt);
769
770 O2RegRegShift lsl(Register r, Register amt);
771 O2RegRegShift lsr(Register r, Register amt);
772 O2RegRegShift asr(Register r, Register amt);
773 O2RegRegShift ror(Register r, Register amt);
774
775 // An offset from a register to be used for ldr/str. This should include the
776 // sign bit, since ARM has "signed-magnitude" offsets. That is it encodes an
777 // unsigned offset, then the instruction specifies if the offset is positive or
778 // negative. The +/- bit is necessary if the instruction set wants to be able to
779 // have a negative register offset e.g. ldr pc, [r1,-r2];
780 class DtrOff {
781 uint32_t data_;
782
783 protected:
784 explicit DtrOff(datastore::Imm12Data immdata, IsUp_ iu)
785 : data_(immdata.encode() | uint32_t(IsImmDTR) | uint32_t(iu)) {}
786
787 explicit DtrOff(datastore::Reg reg, IsUp_ iu = IsUp)
788 : data_(reg.encode() | uint32_t(IsNotImmDTR) | iu) {}
789
790 public:
791 uint32_t encode() const { return data_; }
792 };
793
794 class DtrOffImm : public DtrOff {
795 public:
796 explicit DtrOffImm(int32_t imm)
797 : DtrOff(datastore::Imm12Data(mozilla::Abs(imm)),
798 imm >= 0 ? IsUp : IsDown) {
799 MOZ_ASSERT(mozilla::Abs(imm) < 4096);
800 }
801 };
802
803 class DtrOffReg : public DtrOff {
804 // These are designed to be called by a constructor of a subclass.
805 // Constructing the necessary RIS/RRS structures is annoying.
806
807 protected:
808 explicit DtrOffReg(Register rn, ShiftType type, datastore::RIS shiftImm,
809 IsUp_ iu = IsUp)
810 : DtrOff(datastore::Reg(rn.code(), type, 0, shiftImm.encode()), iu) {}
811
812 explicit DtrOffReg(Register rn, ShiftType type, datastore::RRS shiftReg,
813 IsUp_ iu = IsUp)
814 : DtrOff(datastore::Reg(rn.code(), type, 1, shiftReg.encode()), iu) {}
815 };
816
817 class DtrRegImmShift : public DtrOffReg {
818 public:
819 explicit DtrRegImmShift(Register rn, ShiftType type, uint32_t shift,
820 IsUp_ iu = IsUp)
821 : DtrOffReg(rn, type, datastore::RIS(shift), iu) {}
822 };
823
824 class DtrRegRegShift : public DtrOffReg {
825 public:
826 explicit DtrRegRegShift(Register rn, ShiftType type, Register rs,
827 IsUp_ iu = IsUp)
828 : DtrOffReg(rn, type, datastore::RRS(rs.code()), iu) {}
829 };
830
831 // We will frequently want to bundle a register with its offset so that we have
832 // an "operand" to a load instruction.
833 class DTRAddr {
834 friend class Operand;
835
836 uint32_t data_;
837
838 public:
839 explicit DTRAddr(Register reg, DtrOff dtr)
840 : data_(dtr.encode() | (reg.code() << 16)) {}
841
842 uint32_t encode() const { return data_; }
843
844 Register getBase() const { return Register::FromCode((data_ >> 16) & 0xf); }
845 };
846
847 // Offsets for the extended data transfer instructions:
848 // ldrsh, ldrd, ldrsb, etc.
849 class EDtrOff {
850 uint32_t data_;
851
852 protected:
853 explicit EDtrOff(datastore::Imm8Data imm8, IsUp_ iu = IsUp)
854 : data_(imm8.encode() | IsImmEDTR | uint32_t(iu)) {}
855
856 explicit EDtrOff(Register rm, IsUp_ iu = IsUp)
857 : data_(rm.code() | IsNotImmEDTR | iu) {}
858
859 public:
860 uint32_t encode() const { return data_; }
861 };
862
863 class EDtrOffImm : public EDtrOff {
864 public:
865 explicit EDtrOffImm(int32_t imm)
866 : EDtrOff(datastore::Imm8Data(mozilla::Abs(imm)),
867 (imm >= 0) ? IsUp : IsDown) {
868 MOZ_ASSERT(mozilla::Abs(imm) < 256);
869 }
870 };
871
872 // This is the most-derived class, since the extended data transfer instructions
873 // don't support any sort of modifying the "index" operand.
874 class EDtrOffReg : public EDtrOff {
875 public:
876 explicit EDtrOffReg(Register rm) : EDtrOff(rm) {}
877 };
878
879 class EDtrAddr {
880 uint32_t data_;
881
882 public:
883 explicit EDtrAddr(Register r, EDtrOff off) : data_(RN(r) | off.encode()) {}
884
885 uint32_t encode() const { return data_; }
886 #ifdef DEBUG
887 Register maybeOffsetRegister() const {
888 if (data_ & IsImmEDTR) {
889 return InvalidReg;
890 }
891 return Register::FromCode(data_ & 0xf);
892 }
893 #endif
894 };
895
896 class VFPOff {
897 uint32_t data_;
898
899 protected:
900 explicit VFPOff(datastore::Imm8VFPOffData imm, IsUp_ isup)
901 : data_(imm.encode() | uint32_t(isup)) {}
902
903 public:
904 uint32_t encode() const { return data_; }
905 };
906
907 class VFPOffImm : public VFPOff {
908 public:
909 explicit VFPOffImm(int32_t imm)
910 : VFPOff(datastore::Imm8VFPOffData(mozilla::Abs(imm) / 4),
911 imm < 0 ? IsDown : IsUp) {
912 MOZ_ASSERT(mozilla::Abs(imm) <= 255 * 4);
913 }
914 };
915
916 class VFPAddr {
917 friend class Operand;
918
919 uint32_t data_;
920
921 public:
922 explicit VFPAddr(Register base, VFPOff off)
923 : data_(RN(base) | off.encode()) {}
924
925 uint32_t encode() const { return data_; }
926 };
927
928 class VFPImm {
929 uint32_t data_;
930
931 public:
932 explicit VFPImm(uint32_t topWordOfDouble);
933
934 static const VFPImm One;
935
936 uint32_t encode() const { return data_; }
937 bool isValid() const { return data_ != (~0U); }
938 };
939
940 // A BOffImm is an immediate that is used for branches. Namely, it is the offset
941 // that will be encoded in the branch instruction. This is the only sane way of
942 // constructing a branch.
943 class BOffImm {
944 friend class InstBranchImm;
945
946 uint32_t data_;
947
948 public:
949 explicit BOffImm(int offset) : data_((offset - 8) >> 2 & 0x00ffffff) {
950 MOZ_ASSERT((offset & 0x3) == 0);
951 if (!IsInRange(offset)) {
952 MOZ_CRASH("BOffImm offset out of range");
953 }
954 }
955
956 explicit BOffImm() : data_(INVALID) {}
957
958 private:
959 explicit BOffImm(const Instruction& inst);
960
961 public:
962 static const uint32_t INVALID = 0x00800000;
963
964 uint32_t encode() const { return data_; }
965 int32_t decode() const { return ((int32_t(data_) << 8) >> 6) + 8; }
966
967 static bool IsInRange(int offset) {
968 if ((offset - 8) < -33554432) {
969 return false;
970 }
971 if ((offset - 8) > 33554428) {
972 return false;
973 }
974 return true;
975 }
976
977 bool isInvalid() const { return data_ == INVALID; }
978 Instruction* getDest(Instruction* src) const;
979 };
980
981 class Imm16 {
982 uint32_t lower_ : 12;
983
984 protected:
985 // Mark as a protected field to avoid unused private field warnings.
986 uint32_t pad_ : 4;
987
988 private:
989 uint32_t upper_ : 4;
990 uint32_t invalid_ : 12;
991
992 public:
993 explicit Imm16();
994 explicit Imm16(uint32_t imm);
995 explicit Imm16(Instruction& inst);
996
997 uint32_t encode() const { return lower_ | (upper_ << 16); }
998 uint32_t decode() const { return lower_ | (upper_ << 12); }
999
1000 bool isInvalid() const { return invalid_; }
1001 };
1002
1003 // I would preffer that these do not exist, since there are essentially no
1004 // instructions that would ever take more than one of these, however, the MIR
1005 // wants to only have one type of arguments to functions, so bugger.
1006 class Operand {
1007 // The encoding of registers is the same for OP2, DTR and EDTR yet the type
1008 // system doesn't let us express this, so choices must be made.
1009 public:
1010 enum class Tag : uint8_t { OP2, MEM, FOP };
1011
1012 private:
1013 uint32_t tag_ : 8;
1014 uint32_t reg_ : 5;
1015 int32_t offset_;
1016
1017 protected:
1018 Operand(Tag tag, uint32_t regCode, int32_t offset)
1019 : tag_(static_cast<uint32_t>(tag)), reg_(regCode), offset_(offset) {}
1020
1021 public:
1022 explicit Operand(Register reg) : Operand(Tag::OP2, reg.code(), 0) {}
1023
1024 explicit Operand(FloatRegister freg) : Operand(Tag::FOP, freg.code(), 0) {}
1025
1026 explicit Operand(Register base, Imm32 off)
1027 : Operand(Tag::MEM, base.code(), off.value) {}
1028
1029 explicit Operand(Register base, int32_t off)
1030 : Operand(Tag::MEM, base.code(), off) {}
1031
1032 explicit Operand(const Address& addr)
1033 : Operand(Tag::MEM, addr.base.code(), addr.offset) {}
1034
1035 public:
1036 Tag tag() const { return static_cast<Tag>(tag_); }
1037
1038 Operand2 toOp2() const {
1039 MOZ_ASSERT(tag() == Tag::OP2);
1040 return O2Reg(Register::FromCode(reg_));
1041 }
1042
1043 Register toReg() const {
1044 MOZ_ASSERT(tag() == Tag::OP2);
1045 return Register::FromCode(reg_);
1046 }
1047
1048 Address toAddress() const {
1049 MOZ_ASSERT(tag() == Tag::MEM);
1050 return Address(Register::FromCode(reg_), offset_);
1051 }
1052 int32_t disp() const {
1053 MOZ_ASSERT(tag() == Tag::MEM);
1054 return offset_;
1055 }
1056
1057 int32_t base() const {
1058 MOZ_ASSERT(tag() == Tag::MEM);
1059 return reg_;
1060 }
1061 Register baseReg() const {
1062 MOZ_ASSERT(tag() == Tag::MEM);
1063 return Register::FromCode(reg_);
1064 }
1065 DTRAddr toDTRAddr() const {
1066 MOZ_ASSERT(tag() == Tag::MEM);
1067 return DTRAddr(baseReg(), DtrOffImm(offset_));
1068 }
1069 VFPAddr toVFPAddr() const {
1070 MOZ_ASSERT(tag() == Tag::MEM);
1071 return VFPAddr(baseReg(), VFPOffImm(offset_));
1072 }
1073 };
1074
1075 class InstructionIterator {
1076 private:
1077 Instruction* inst_;
1078
1079 public:
1080 explicit InstructionIterator(Instruction* inst) : inst_(inst) {
1081 maybeSkipAutomaticInstructions();
1082 }
1083
1084 // Advances to the next intentionally-inserted instruction.
1085 Instruction* next();
1086
1087 // Advances past any automatically-inserted instructions.
1088 Instruction* maybeSkipAutomaticInstructions();
1089
1090 Instruction* cur() const { return inst_; }
1091
1092 protected:
1093 // Advances past the given number of instruction-length bytes.
1094 inline void advanceRaw(ptrdiff_t instructions = 1);
1095 };
1096
1097 class Assembler;
1098 using ARMBuffer =
1099 js::jit::AssemblerBufferWithConstantPools<1024, 4, Instruction, Assembler>;
1100
1101 class Assembler : public AssemblerShared {
1102 public:
1103 // ARM conditional constants:
1104 enum ARMCondition : uint32_t {
1105 EQ = 0x00000000, // Zero
1106 NE = 0x10000000, // Non-zero
1107 CS = 0x20000000,
1108 CC = 0x30000000,
1109 MI = 0x40000000,
1110 PL = 0x50000000,
1111 VS = 0x60000000,
1112 VC = 0x70000000,
1113 HI = 0x80000000,
1114 LS = 0x90000000,
1115 GE = 0xa0000000,
1116 LT = 0xb0000000,
1117 GT = 0xc0000000,
1118 LE = 0xd0000000,
1119 AL = 0xe0000000
1120 };
1121
1122 enum Condition : uint32_t {
1123 Equal = EQ,
1124 NotEqual = NE,
1125 Above = HI,
1126 AboveOrEqual = CS,
1127 Below = CC,
1128 BelowOrEqual = LS,
1129 GreaterThan = GT,
1130 GreaterThanOrEqual = GE,
1131 LessThan = LT,
1132 LessThanOrEqual = LE,
1133 Overflow = VS,
1134 CarrySet = CS,
1135 CarryClear = CC,
1136 Signed = MI,
1137 NotSigned = PL,
1138 Zero = EQ,
1139 NonZero = NE,
1140 Always = AL,
1141
1142 VFP_NotEqualOrUnordered = NE,
1143 VFP_Equal = EQ,
1144 VFP_Unordered = VS,
1145 VFP_NotUnordered = VC,
1146 VFP_GreaterThanOrEqualOrUnordered = CS,
1147 VFP_GreaterThanOrEqual = GE,
1148 VFP_GreaterThanOrUnordered = HI,
1149 VFP_GreaterThan = GT,
1150 VFP_LessThanOrEqualOrUnordered = LE,
1151 VFP_LessThanOrEqual = LS,
1152 VFP_LessThanOrUnordered = LT,
1153 VFP_LessThan = CC // MI is valid too.
1154 };
1155
1156 // Bit set when a DoubleCondition does not map to a single ARM condition.
1157 // The macro assembler has to special-case these conditions, or else
1158 // ConditionFromDoubleCondition will complain.
1159 static const int DoubleConditionBitSpecial = 0x1;
1160
1161 enum DoubleCondition : uint32_t {
1162 // These conditions will only evaluate to true if the comparison is
1163 // ordered - i.e. neither operand is NaN.
1164 DoubleOrdered = VFP_NotUnordered,
1165 DoubleEqual = VFP_Equal,
1166 DoubleNotEqual = VFP_NotEqualOrUnordered | DoubleConditionBitSpecial,
1167 DoubleGreaterThan = VFP_GreaterThan,
1168 DoubleGreaterThanOrEqual = VFP_GreaterThanOrEqual,
1169 DoubleLessThan = VFP_LessThan,
1170 DoubleLessThanOrEqual = VFP_LessThanOrEqual,
1171 // If either operand is NaN, these conditions always evaluate to true.
1172 DoubleUnordered = VFP_Unordered,
1173 DoubleEqualOrUnordered = VFP_Equal | DoubleConditionBitSpecial,
1174 DoubleNotEqualOrUnordered = VFP_NotEqualOrUnordered,
1175 DoubleGreaterThanOrUnordered = VFP_GreaterThanOrUnordered,
1176 DoubleGreaterThanOrEqualOrUnordered = VFP_GreaterThanOrEqualOrUnordered,
1177 DoubleLessThanOrUnordered = VFP_LessThanOrUnordered,
1178 DoubleLessThanOrEqualOrUnordered = VFP_LessThanOrEqualOrUnordered
1179 };
1180
1181 Condition getCondition(uint32_t inst) {
1182 return (Condition)(0xf0000000 & inst);
1183 }
1184 static inline Condition ConditionFromDoubleCondition(DoubleCondition cond) {
1185 MOZ_ASSERT(!(cond & DoubleConditionBitSpecial));
1186 return static_cast<Condition>(cond);
1187 }
1188
1189 enum BarrierOption {
1190 BarrierSY = 15, // Full system barrier
1191 BarrierST = 14 // StoreStore barrier
1192 };
1193
1194 // This should be protected, but since CodeGenerator wants to use it, it
1195 // needs to go out here :(
1196
1197 BufferOffset nextOffset() { return m_buffer.nextOffset(); }
1198
1199 protected:
1200 // Shim around AssemblerBufferWithConstantPools::allocEntry.
1201 BufferOffset allocLiteralLoadEntry(size_t numInst, unsigned numPoolEntries,
1202 PoolHintPun& php, uint8_t* data,
1203 const LiteralDoc& doc = LiteralDoc(),
1204 ARMBuffer::PoolEntry* pe = nullptr,
1205 bool loadToPC = false);
1206
1207 Instruction* editSrc(BufferOffset bo) { return m_buffer.getInst(bo); }
1208
1209 #ifdef JS_DISASM_ARM
1210 using DisasmBuffer =
1211 disasm::EmbeddedVector<char, disasm::ReasonableBufferSize>;
1212
1213 static void disassembleInstruction(const Instruction* i,
1214 DisasmBuffer& buffer);
1215
1216 void initDisassembler();
1217 void finishDisassembler();
1218 void spew(Instruction* i);
1219 void spewBranch(Instruction* i, const LabelDoc& target);
1220 void spewLiteralLoad(PoolHintPun& php, bool loadToPC, const Instruction* offs,
1221 const LiteralDoc& doc);
1222 #endif
1223
1224 public:
1225 void resetCounter();
1226 static uint32_t NopFill;
1227 static uint32_t GetNopFill();
1228 static uint32_t AsmPoolMaxOffset;
1229 static uint32_t GetPoolMaxOffset();
1230
1231 protected:
1232 // Structure for fixing up pc-relative loads/jumps when a the machine code
1233 // gets moved (executable copy, gc, etc.).
1234 class RelativePatch {
1235 void* target_;
1236 RelocationKind kind_;
1237
1238 public:
1239 RelativePatch(void* target, RelocationKind kind)
1240 : target_(target), kind_(kind) {}
1241 void* target() const { return target_; }
1242 RelocationKind kind() const { return kind_; }
1243 };
1244
1245 // TODO: this should actually be a pool-like object. It is currently a big
1246 // hack, and probably shouldn't exist.
1247 js::Vector<RelativePatch, 8, SystemAllocPolicy> jumps_;
1248
1249 CompactBufferWriter jumpRelocations_;
1250 CompactBufferWriter dataRelocations_;
1251
1252 ARMBuffer m_buffer;
1253
1254 #ifdef JS_DISASM_ARM
1255 DisassemblerSpew spew_;
1256 #endif
1257
1258 public:
1259 // For the alignment fill use NOP: 0x0320f000 or (Always | InstNOP::NopInst).
1260 // For the nopFill use a branch to the next instruction: 0xeaffffff.
1261 Assembler()
1262 : m_buffer(1, 1, 8, GetPoolMaxOffset(), 8, 0xe320f000, 0xeaffffff,
1263 GetNopFill()),
1264 isFinished(false),
1265 dtmActive(false),
1266 dtmCond(Always) {
1267 #ifdef JS_DISASM_ARM
1268 initDisassembler();
1269 #endif
1270 }
1271
1272 ~Assembler() {
1273 #ifdef JS_DISASM_ARM
1274 finishDisassembler();
1275 #endif
1276 }
1277
1278 void setUnlimitedBuffer() { m_buffer.setUnlimited(); }
1279
1280 static Condition InvertCondition(Condition cond);
1281 static Condition UnsignedCondition(Condition cond);
1282 static Condition ConditionWithoutEqual(Condition cond);
1283
1284 static DoubleCondition InvertCondition(DoubleCondition cond);
1285
1286 void writeDataRelocation(BufferOffset offset, ImmGCPtr ptr) {
1287 // Raw GC pointer relocations and Value relocations both end up in
1288 // Assembler::TraceDataRelocations.
1289 if (ptr.value) {
1290 if (gc::IsInsideNursery(ptr.value)) {
1291 embedsNurseryPointers_ = true;
1292 }
1293 dataRelocations_.writeUnsigned(offset.getOffset());
1294 }
1295 }
1296
1297 enum RelocBranchStyle { B_MOVWT, B_LDR_BX, B_LDR, B_MOVW_ADD };
1298
1299 enum RelocStyle { L_MOVWT, L_LDR };
1300
1301 public:
1302 // Given the start of a Control Flow sequence, grab the value that is
1303 // finally branched to given the start of a function that loads an address
1304 // into a register get the address that ends up in the register.
1305 template <class Iter>
1306 static const uint32_t* GetCF32Target(Iter* iter);
1307
1308 static uintptr_t GetPointer(uint8_t*);
1309 template <class Iter>
1310 static const uint32_t* GetPtr32Target(Iter iter, Register* dest = nullptr,
1311 RelocStyle* rs = nullptr);
1312
1313 bool oom() const;
1314
1315 void setPrinter(Sprinter* sp) {
1316 #ifdef JS_DISASM_ARM
1317 spew_.setPrinter(sp);
1318 #endif
1319 }
1320
1321 Register getStackPointer() const { return StackPointer; }
1322
1323 private:
1324 bool isFinished;
1325
1326 protected:
1327 LabelDoc refLabel(const Label* label) {
1328 #ifdef JS_DISASM_ARM
1329 return spew_.refLabel(label);
1330 #else
1331 return LabelDoc();
1332 #endif
1333 }
1334
1335 public:
1336 void finish();
1337 bool appendRawCode(const uint8_t* code, size_t numBytes);
1338 bool reserve(size_t size);
1339 bool swapBuffer(wasm::Bytes& bytes);
1340 void copyJumpRelocationTable(uint8_t* dest);
1341 void copyDataRelocationTable(uint8_t* dest);
1342
1343 // Size of the instruction stream, in bytes, after pools are flushed.
1344 size_t size() const;
1345 // Size of the jump relocation table, in bytes.
1346 size_t jumpRelocationTableBytes() const;
1347 size_t dataRelocationTableBytes() const;
1348
1349 // Size of the data table, in bytes.
1350 size_t bytesNeeded() const;
1351
1352 // Write a single instruction into the instruction stream. Very hot,
1353 // inlined for performance
1354 MOZ_ALWAYS_INLINE BufferOffset writeInst(uint32_t x) {
1355 MOZ_ASSERT(hasCreator());
1356 BufferOffset offs = m_buffer.putInt(x);
1357 #ifdef JS_DISASM_ARM
1358 spew(m_buffer.getInstOrNull(offs));
1359 #endif
1360 return offs;
1361 }
1362
1363 // As above, but also mark the instruction as a branch. Very hot, inlined
1364 // for performance
1365 MOZ_ALWAYS_INLINE BufferOffset
1366 writeBranchInst(uint32_t x, const LabelDoc& documentation) {
1367 BufferOffset offs = m_buffer.putInt(x);
1368 #ifdef JS_DISASM_ARM
1369 spewBranch(m_buffer.getInstOrNull(offs), documentation);
1370 #endif
1371 return offs;
1372 }
1373
1374 // Write a placeholder NOP for a branch into the instruction stream
1375 // (in order to adjust assembler addresses and mark it as a branch), it will
1376 // be overwritten subsequently.
1377 BufferOffset allocBranchInst();
1378
1379 // A static variant for the cases where we don't want to have an assembler
1380 // object.
1381 static void WriteInstStatic(uint32_t x, uint32_t* dest);
1382
1383 public:
1384 void writeCodePointer(CodeLabel* label);
1385
1386 void haltingAlign(int alignment);
1387 void nopAlign(int alignment);
1388 BufferOffset as_nop();
1389 BufferOffset as_alu(Register dest, Register src1, Operand2 op2, ALUOp op,
1390 SBit s = LeaveCC, Condition c = Always);
1391 BufferOffset as_mov(Register dest, Operand2 op2, SBit s = LeaveCC,
1392 Condition c = Always);
1393 BufferOffset as_mvn(Register dest, Operand2 op2, SBit s = LeaveCC,
1394 Condition c = Always);
1395
1396 static void as_alu_patch(Register dest, Register src1, Operand2 op2, ALUOp op,
1397 SBit s, Condition c, uint32_t* pos);
1398 static void as_mov_patch(Register dest, Operand2 op2, SBit s, Condition c,
1399 uint32_t* pos);
1400
1401 // Logical operations:
1402 BufferOffset as_and(Register dest, Register src1, Operand2 op2,
1403 SBit s = LeaveCC, Condition c = Always);
1404 BufferOffset as_bic(Register dest, Register src1, Operand2 op2,
1405 SBit s = LeaveCC, Condition c = Always);
1406 BufferOffset as_eor(Register dest, Register src1, Operand2 op2,
1407 SBit s = LeaveCC, Condition c = Always);
1408 BufferOffset as_orr(Register dest, Register src1, Operand2 op2,
1409 SBit s = LeaveCC, Condition c = Always);
1410 // Reverse byte operations:
1411 BufferOffset as_rev(Register dest, Register src, Condition c = Always);
1412 BufferOffset as_rev16(Register dest, Register src, Condition c = Always);
1413 BufferOffset as_revsh(Register dest, Register src, Condition c = Always);
1414 // Mathematical operations:
1415 BufferOffset as_adc(Register dest, Register src1, Operand2 op2,
1416 SBit s = LeaveCC, Condition c = Always);
1417 BufferOffset as_add(Register dest, Register src1, Operand2 op2,
1418 SBit s = LeaveCC, Condition c = Always);
1419 BufferOffset as_sbc(Register dest, Register src1, Operand2 op2,
1420 SBit s = LeaveCC, Condition c = Always);
1421 BufferOffset as_sub(Register dest, Register src1, Operand2 op2,
1422 SBit s = LeaveCC, Condition c = Always);
1423 BufferOffset as_rsb(Register dest, Register src1, Operand2 op2,
1424 SBit s = LeaveCC, Condition c = Always);
1425 BufferOffset as_rsc(Register dest, Register src1, Operand2 op2,
1426 SBit s = LeaveCC, Condition c = Always);
1427 // Test operations:
1428 BufferOffset as_cmn(Register src1, Operand2 op2, Condition c = Always);
1429 BufferOffset as_cmp(Register src1, Operand2 op2, Condition c = Always);
1430 BufferOffset as_teq(Register src1, Operand2 op2, Condition c = Always);
1431 BufferOffset as_tst(Register src1, Operand2 op2, Condition c = Always);
1432
1433 // Sign extension operations:
1434 BufferOffset as_sxtb(Register dest, Register src, int rotate,
1435 Condition c = Always);
1436 BufferOffset as_sxth(Register dest, Register src, int rotate,
1437 Condition c = Always);
1438 BufferOffset as_uxtb(Register dest, Register src, int rotate,
1439 Condition c = Always);
1440 BufferOffset as_uxth(Register dest, Register src, int rotate,
1441 Condition c = Always);
1442
1443 // Not quite ALU worthy, but useful none the less: These also have the issue
1444 // of these being formatted completly differently from the standard ALU
1445 // operations.
1446 BufferOffset as_movw(Register dest, Imm16 imm, Condition c = Always);
1447 BufferOffset as_movt(Register dest, Imm16 imm, Condition c = Always);
1448
1449 static void as_movw_patch(Register dest, Imm16 imm, Condition c,
1450 Instruction* pos);
1451 static void as_movt_patch(Register dest, Imm16 imm, Condition c,
1452 Instruction* pos);
1453
1454 BufferOffset as_genmul(Register d1, Register d2, Register rm, Register rn,
1455 MULOp op, SBit s, Condition c = Always);
1456 BufferOffset as_mul(Register dest, Register src1, Register src2,
1457 SBit s = LeaveCC, Condition c = Always);
1458 BufferOffset as_mla(Register dest, Register acc, Register src1, Register src2,
1459 SBit s = LeaveCC, Condition c = Always);
1460 BufferOffset as_umaal(Register dest1, Register dest2, Register src1,
1461 Register src2, Condition c = Always);
1462 BufferOffset as_mls(Register dest, Register acc, Register src1, Register src2,
1463 Condition c = Always);
1464 BufferOffset as_umull(Register dest1, Register dest2, Register src1,
1465 Register src2, SBit s = LeaveCC, Condition c = Always);
1466 BufferOffset as_umlal(Register dest1, Register dest2, Register src1,
1467 Register src2, SBit s = LeaveCC, Condition c = Always);
1468 BufferOffset as_smull(Register dest1, Register dest2, Register src1,
1469 Register src2, SBit s = LeaveCC, Condition c = Always);
1470 BufferOffset as_smlal(Register dest1, Register dest2, Register src1,
1471 Register src2, SBit s = LeaveCC, Condition c = Always);
1472
1473 BufferOffset as_sdiv(Register dest, Register num, Register div,
1474 Condition c = Always);
1475 BufferOffset as_udiv(Register dest, Register num, Register div,
1476 Condition c = Always);
1477 BufferOffset as_clz(Register dest, Register src, Condition c = Always);
1478
1479 // Data transfer instructions: ldr, str, ldrb, strb.
1480 // Using an int to differentiate between 8 bits and 32 bits is overkill.
1481 BufferOffset as_dtr(LoadStore ls, int size, Index mode, Register rt,
1482 DTRAddr addr, Condition c = Always);
1483
1484 static void as_dtr_patch(LoadStore ls, int size, Index mode, Register rt,
1485 DTRAddr addr, Condition c, uint32_t* dest);
1486
1487 // Handles all of the other integral data transferring functions:
1488 // ldrsb, ldrsh, ldrd, etc. The size is given in bits.
1489 BufferOffset as_extdtr(LoadStore ls, int size, bool IsSigned, Index mode,
1490 Register rt, EDtrAddr addr, Condition c = Always);
1491
1492 BufferOffset as_dtm(LoadStore ls, Register rn, uint32_t mask, DTMMode mode,
1493 DTMWriteBack wb, Condition c = Always);
1494
1495 // Overwrite a pool entry with new data.
1496 static void WritePoolEntry(Instruction* addr, Condition c, uint32_t data);
1497
1498 // Load a 32 bit immediate from a pool into a register.
1499 BufferOffset as_Imm32Pool(Register dest, uint32_t value,
1500 Condition c = Always);
1501
1502 // Load a 64 bit floating point immediate from a pool into a register.
1503 BufferOffset as_FImm64Pool(VFPRegister dest, double value,
1504 Condition c = Always);
1505 // Load a 32 bit floating point immediate from a pool into a register.
1506 BufferOffset as_FImm32Pool(VFPRegister dest, float value,
1507 Condition c = Always);
1508
1509 // Atomic instructions: ldrexd, ldrex, ldrexh, ldrexb, strexd, strex, strexh,
1510 // strexb.
1511 //
1512 // The doubleword, halfword, and byte versions are available from ARMv6K
1513 // forward.
1514 //
1515 // The word versions are available from ARMv6 forward and can be used to
1516 // implement the halfword and byte versions on older systems.
1517
1518 // LDREXD rt, rt2, [rn]. Constraint: rt even register, rt2=rt+1.
1519 BufferOffset as_ldrexd(Register rt, Register rt2, Register rn,
1520 Condition c = Always);
1521
1522 // LDREX rt, [rn]
1523 BufferOffset as_ldrex(Register rt, Register rn, Condition c = Always);
1524 BufferOffset as_ldrexh(Register rt, Register rn, Condition c = Always);
1525 BufferOffset as_ldrexb(Register rt, Register rn, Condition c = Always);
1526
1527 // STREXD rd, rt, rt2, [rn]. Constraint: rt even register, rt2=rt+1.
1528 BufferOffset as_strexd(Register rd, Register rt, Register rt2, Register rn,
1529 Condition c = Always);
1530
1531 // STREX rd, rt, [rn]. Constraint: rd != rn, rd != rt.
1532 BufferOffset as_strex(Register rd, Register rt, Register rn,
1533 Condition c = Always);
1534 BufferOffset as_strexh(Register rd, Register rt, Register rn,
1535 Condition c = Always);
1536 BufferOffset as_strexb(Register rd, Register rt, Register rn,
1537 Condition c = Always);
1538
1539 // CLREX
1540 BufferOffset as_clrex();
1541
1542 // Memory synchronization.
1543 // These are available from ARMv7 forward.
1544 BufferOffset as_dmb(BarrierOption option = BarrierSY);
1545 BufferOffset as_dsb(BarrierOption option = BarrierSY);
1546 BufferOffset as_isb();
1547
1548 // Memory synchronization for architectures before ARMv7.
1549 BufferOffset as_dsb_trap();
1550 BufferOffset as_dmb_trap();
1551 BufferOffset as_isb_trap();
1552
1553 // Speculation barrier
1554 BufferOffset as_csdb();
1555
1556 // Move Special Register and Hints:
1557
1558 // yield hint instruction.
1559 BufferOffset as_yield();
1560
1561 // Control flow stuff:
1562
1563 // bx can *only* branch to a register never to an immediate.
1564 BufferOffset as_bx(Register r, Condition c = Always);
1565
1566 // Branch can branch to an immediate *or* to a register. Branches to
1567 // immediates are pc relative, branches to registers are absolute.
1568 BufferOffset as_b(BOffImm off, Condition c, Label* documentation = nullptr);
1569
1570 BufferOffset as_b(Label* l, Condition c = Always);
1571 BufferOffset as_b(BOffImm off, Condition c, BufferOffset inst);
1572
1573 // blx can go to either an immediate or a register. When blx'ing to a
1574 // register, we change processor mode depending on the low bit of the
1575 // register when blx'ing to an immediate, we *always* change processor
1576 // state.
1577 BufferOffset as_blx(Label* l);
1578
1579 BufferOffset as_blx(Register r, Condition c = Always);
1580 BufferOffset as_bl(BOffImm off, Condition c, Label* documentation = nullptr);
1581 // bl can only branch+link to an immediate, never to a register it never
1582 // changes processor state.
1583 BufferOffset as_bl();
1584 // bl #imm can have a condition code, blx #imm cannot.
1585 // blx reg can be conditional.
1586 BufferOffset as_bl(Label* l, Condition c);
1587 BufferOffset as_bl(BOffImm off, Condition c, BufferOffset inst);
1588
1589 BufferOffset as_mrs(Register r, Condition c = Always);
1590 BufferOffset as_msr(Register r, Condition c = Always);
1591
1592 // VFP instructions!
1593 private:
1594 enum vfp_size { IsDouble = 1 << 8, IsSingle = 0 << 8 };
1595
1596 BufferOffset writeVFPInst(vfp_size sz, uint32_t blob);
1597
1598 static void WriteVFPInstStatic(vfp_size sz, uint32_t blob, uint32_t* dest);
1599
1600 // Unityped variants: all registers hold the same (ieee754 single/double)
1601 // notably not included are vcvt; vmov vd, #imm; vmov rt, vn.
1602 BufferOffset as_vfp_float(VFPRegister vd, VFPRegister vn, VFPRegister vm,
1603 VFPOp op, Condition c = Always);
1604
1605 public:
1606 BufferOffset as_vadd(VFPRegister vd, VFPRegister vn, VFPRegister vm,
1607 Condition c = Always);
1608 BufferOffset as_vdiv(VFPRegister vd, VFPRegister vn, VFPRegister vm,
1609 Condition c = Always);
1610 BufferOffset as_vmul(VFPRegister vd, VFPRegister vn, VFPRegister vm,
1611 Condition c = Always);
1612 BufferOffset as_vnmul(VFPRegister vd, VFPRegister vn, VFPRegister vm,
1613 Condition c = Always);
1614 BufferOffset as_vnmla(VFPRegister vd, VFPRegister vn, VFPRegister vm,
1615 Condition c = Always);
1616 BufferOffset as_vnmls(VFPRegister vd, VFPRegister vn, VFPRegister vm,
1617 Condition c = Always);
1618 BufferOffset as_vneg(VFPRegister vd, VFPRegister vm, Condition c = Always);
1619 BufferOffset as_vsqrt(VFPRegister vd, VFPRegister vm, Condition c = Always);
1620 BufferOffset as_vabs(VFPRegister vd, VFPRegister vm, Condition c = Always);
1621 BufferOffset as_vsub(VFPRegister vd, VFPRegister vn, VFPRegister vm,
1622 Condition c = Always);
1623 BufferOffset as_vcmp(VFPRegister vd, VFPRegister vm, Condition c = Always);
1624 BufferOffset as_vcmpz(VFPRegister vd, Condition c = Always);
1625
1626 // Specifically, a move between two same sized-registers.
1627 BufferOffset as_vmov(VFPRegister vd, VFPRegister vsrc, Condition c = Always);
1628
1629 // Transfer between Core and VFP.
1630 enum FloatToCore_ { FloatToCore = 1 << 20, CoreToFloat = 0 << 20 };
1631
1632 private:
1633 enum VFPXferSize { WordTransfer = 0x02000010, DoubleTransfer = 0x00400010 };
1634
1635 public:
1636 // Unlike the next function, moving between the core registers and vfp
1637 // registers can't be *that* properly typed. Namely, since I don't want to
1638 // munge the type VFPRegister to also include core registers. Thus, the core
1639 // and vfp registers are passed in based on their type, and src/dest is
1640 // determined by the float2core.
1641
1642 BufferOffset as_vxfer(Register vt1, Register vt2, VFPRegister vm,
1643 FloatToCore_ f2c, Condition c = Always, int idx = 0);
1644
1645 // Our encoding actually allows just the src and the dest (and their types)
1646 // to uniquely specify the encoding that we are going to use.
1647 BufferOffset as_vcvt(VFPRegister vd, VFPRegister vm, bool useFPSCR = false,
1648 Condition c = Always);
1649
1650 // Hard coded to a 32 bit fixed width result for now.
1651 BufferOffset as_vcvtFixed(VFPRegister vd, bool isSigned, uint32_t fixedPoint,
1652 bool toFixed, Condition c = Always);
1653
1654 // Convert between single- and half-precision. Both registers are single
1655 // precision.
1656 BufferOffset as_vcvtb_s2h(VFPRegister vd, VFPRegister vm,
1657 Condition c = Always);
1658 BufferOffset as_vcvtb_h2s(VFPRegister vd, VFPRegister vm,
1659 Condition c = Always);
1660
1661 // Transfer between VFP and memory.
1662 BufferOffset as_vdtr(LoadStore ls, VFPRegister vd, VFPAddr addr,
1663 Condition c = Always /* vfp doesn't have a wb option*/);
1664
1665 static void as_vdtr_patch(LoadStore ls, VFPRegister vd, VFPAddr addr,
1666 Condition c /* vfp doesn't have a wb option */,
1667 uint32_t* dest);
1668
1669 // VFP's ldm/stm work differently from the standard arm ones. You can only
1670 // transfer a range.
1671
1672 BufferOffset as_vdtm(LoadStore st, Register rn, VFPRegister vd, int length,
1673 /* also has update conditions */ Condition c = Always);
1674
1675 // vldr/vstr variants that handle unaligned accesses. These encode as NEON
1676 // single-element instructions and can only be used if NEON is available.
1677 // Here, vd must be tagged as a float or double register.
1678 BufferOffset as_vldr_unaligned(VFPRegister vd, Register rn);
1679 BufferOffset as_vstr_unaligned(VFPRegister vd, Register rn);
1680
1681 BufferOffset as_vimm(VFPRegister vd, VFPImm imm, Condition c = Always);
1682
1683 BufferOffset as_vmrs(Register r, Condition c = Always);
1684 BufferOffset as_vmsr(Register r, Condition c = Always);
1685
1686 // Label operations.
1687 bool nextLink(BufferOffset b, BufferOffset* next);
1688 void bind(Label* label, BufferOffset boff = BufferOffset());
1689 void bind(CodeLabel* label) { label->target()->bind(currentOffset()); }
1690 uint32_t currentOffset() { return nextOffset().getOffset(); }
1691 void retarget(Label* label, Label* target);
1692 // I'm going to pretend this doesn't exist for now.
1693 void retarget(Label* label, void* target, RelocationKind reloc);
1694
1695 static void Bind(uint8_t* rawCode, const CodeLabel& label);
1696 static void PatchMovwt(Instruction* addr, uint32_t imm);
1697
1698 void as_bkpt();
1699 BufferOffset as_illegal_trap();
1700
1701 public:
1702 static void TraceJumpRelocations(JSTracer* trc, JitCode* code,
1703 CompactBufferReader& reader);
1704 static void TraceDataRelocations(JSTracer* trc, JitCode* code,
1705 CompactBufferReader& reader);
1706
1707 void assertNoGCThings() const {
1708 #ifdef DEBUG
1709 MOZ_ASSERT(dataRelocations_.length() == 0);
1710 for (auto& j : jumps_) {
1711 MOZ_ASSERT(j.kind() == RelocationKind::HARDCODED);
1712 }
1713 #endif
1714 }
1715
1716 static bool SupportsFloatingPoint() { return ARMFlags::HasVFP(); }
1717 static bool SupportsUnalignedAccesses() { return ARMFlags::HasARMv7(); }
1718 // Note, returning false here is technically wrong, but one has to go via the
1719 // as_vldr_unaligned and as_vstr_unaligned instructions to get proper behavior
1720 // and those are NEON-specific and have to be asked for specifically.
1721 static bool SupportsFastUnalignedFPAccesses() { return false; }
1722 static bool SupportsFloat64To16() { return false; }
1723 static bool SupportsFloat32To16() { return ARMFlags::HasFPHalfPrecision(); }
1724
1725 static bool HasRoundInstruction(RoundingMode mode) { return false; }
1726
1727 protected:
1728 void addPendingJump(BufferOffset src, ImmPtr target, RelocationKind kind) {
1729 enoughMemory_ &= jumps_.append(RelativePatch(target.value, kind));
1730 if (kind == RelocationKind::JITCODE) {
1731 jumpRelocations_.writeUnsigned(src.getOffset());
1732 }
1733 }
1734
1735 public:
1736 // The buffer is about to be linked, make sure any constant pools or excess
1737 // bookkeeping has been flushed to the instruction stream.
1738 void flush() {
1739 MOZ_ASSERT(!isFinished);
1740 m_buffer.flushPool();
1741 return;
1742 }
1743
1744 void comment(const char* msg) {
1745 #ifdef JS_DISASM_ARM
1746 spew_.spew("; %s", msg);
1747 #endif
1748 }
1749
1750 // Copy the assembly code to the given buffer, and perform any pending
1751 // relocations relying on the target address.
1752 void executableCopy(uint8_t* buffer);
1753
1754 // Actual assembly emitting functions.
1755
1756 // Since I can't think of a reasonable default for the mode, I'm going to
1757 // leave it as a required argument.
1758 void startDataTransferM(LoadStore ls, Register rm, DTMMode mode,
1759 DTMWriteBack update = NoWriteBack,
1760 Condition c = Always) {
1761 MOZ_ASSERT(!dtmActive);
1762 dtmUpdate = update;
1763 dtmBase = rm;
1764 dtmLoadStore = ls;
1765 dtmLastReg = -1;
1766 dtmRegBitField = 0;
1767 dtmActive = 1;
1768 dtmCond = c;
1769 dtmMode = mode;
1770 }
1771
1772 void transferReg(Register rn) {
1773 MOZ_ASSERT(dtmActive);
1774 MOZ_ASSERT(rn.code() > dtmLastReg);
1775 dtmRegBitField |= 1 << rn.code();
1776 if (dtmLoadStore == IsLoad && rn.code() == 13 && dtmBase.code() == 13) {
1777 MOZ_CRASH("ARM Spec says this is invalid");
1778 }
1779 }
1780 void finishDataTransfer() {
1781 dtmActive = false;
1782 as_dtm(dtmLoadStore, dtmBase, dtmRegBitField, dtmMode, dtmUpdate, dtmCond);
1783 }
1784
1785 void startFloatTransferM(LoadStore ls, Register rm, DTMMode mode,
1786 DTMWriteBack update = NoWriteBack,
1787 Condition c = Always) {
1788 MOZ_ASSERT(!dtmActive);
1789 dtmActive = true;
1790 dtmUpdate = update;
1791 dtmLoadStore = ls;
1792 dtmBase = rm;
1793 dtmCond = c;
1794 dtmLastReg = -1;
1795 dtmMode = mode;
1796 dtmDelta = 0;
1797 }
1798 void transferFloatReg(VFPRegister rn) {
1799 if (dtmLastReg == -1) {
1800 vdtmFirstReg = rn.code();
1801 } else {
1802 if (dtmDelta == 0) {
1803 dtmDelta = rn.code() - dtmLastReg;
1804 MOZ_ASSERT(dtmDelta == 1 || dtmDelta == -1);
1805 }
1806 MOZ_ASSERT(dtmLastReg >= 0);
1807 MOZ_ASSERT(rn.code() == unsigned(dtmLastReg) + dtmDelta);
1808 }
1809
1810 dtmLastReg = rn.code();
1811 }
1812 void finishFloatTransfer() {
1813 MOZ_ASSERT(dtmActive);
1814 dtmActive = false;
1815 MOZ_ASSERT(dtmLastReg != -1);
1816 dtmDelta = dtmDelta ? dtmDelta : 1;
1817 // The operand for the vstr/vldr instruction is the lowest register in the
1818 // range.
1819 int low = std::min(dtmLastReg, vdtmFirstReg);
1820 int high = std::max(dtmLastReg, vdtmFirstReg);
1821 // Fencepost problem.
1822 int len = high - low + 1;
1823 // vdtm can only transfer 16 registers at once. If we need to transfer
1824 // more, then either hoops are necessary, or we need to be updating the
1825 // register.
1826 MOZ_ASSERT_IF(len > 16, dtmUpdate == WriteBack);
1827
1828 int adjustLow = dtmLoadStore == IsStore ? 0 : 1;
1829 int adjustHigh = dtmLoadStore == IsStore ? -1 : 0;
1830 while (len > 0) {
1831 // Limit the instruction to 16 registers.
1832 int curLen = std::min(len, 16);
1833 // If it is a store, we want to start at the high end and move down
1834 // (e.g. vpush d16-d31; vpush d0-d15).
1835 int curStart = (dtmLoadStore == IsStore) ? high - curLen + 1 : low;
1836 as_vdtm(dtmLoadStore, dtmBase,
1837 VFPRegister(FloatRegister::FromCode(curStart)), curLen, dtmCond);
1838 // Update the bounds.
1839 low += adjustLow * curLen;
1840 high += adjustHigh * curLen;
1841 // Update the length parameter.
1842 len -= curLen;
1843 }
1844 }
1845
1846 private:
1847 int dtmRegBitField;
1848 int vdtmFirstReg;
1849 int dtmLastReg;
1850 int dtmDelta;
1851 Register dtmBase;
1852 DTMWriteBack dtmUpdate;
1853 DTMMode dtmMode;
1854 LoadStore dtmLoadStore;
1855 bool dtmActive;
1856 Condition dtmCond;
1857
1858 public:
1859 enum {
1860 PadForAlign8 = (int)0x00,
1861 PadForAlign16 = (int)0x0000,
1862 PadForAlign32 = (int)0xe12fff7f // 'bkpt 0xffff'
1863 };
1864
1865 // API for speaking with the IonAssemblerBufferWithConstantPools generate an
1866 // initial placeholder instruction that we want to later fix up.
1867 static void InsertIndexIntoTag(uint8_t* load, uint32_t index);
1868
1869 // Take the stub value that was written in before, and write in an actual
1870 // load using the index we'd computed previously as well as the address of
1871 // the pool start.
1872 static void PatchConstantPoolLoad(void* loadAddr, void* constPoolAddr);
1873
1874 // We're not tracking short-range branches for ARM for now.
1875 static void PatchShortRangeBranchToVeneer(ARMBuffer*, unsigned rangeIdx,
1876 BufferOffset deadline,
1877 BufferOffset veneer) {
1878 MOZ_CRASH();
1879 }
1880 // END API
1881
1882 // Move our entire pool into the instruction stream. This is to force an
1883 // opportunistic dump of the pool, prefferably when it is more convenient to
1884 // do a dump.
1885 void flushBuffer();
1886 void enterNoPool(size_t maxInst);
1887 void leaveNoPool();
1888 void enterNoNops();
1889 void leaveNoNops();
1890
1891 static void WritePoolHeader(uint8_t* start, Pool* p, bool isNatural);
1892 static void WritePoolGuard(BufferOffset branch, Instruction* inst,
1893 BufferOffset dest);
1894
1895 static uint32_t PatchWrite_NearCallSize();
1896 static uint32_t NopSize() { return 4; }
1897 static void PatchWrite_NearCall(CodeLocationLabel start,
1898 CodeLocationLabel toCall);
1899 static void PatchDataWithValueCheck(CodeLocationLabel label,
1900 PatchedImmPtr newValue,
1901 PatchedImmPtr expectedValue);
1902 static void PatchDataWithValueCheck(CodeLocationLabel label, ImmPtr newValue,
1903 ImmPtr expectedValue);
1904 static void PatchWrite_Imm32(CodeLocationLabel label, Imm32 imm);
1905
1906 static uint32_t AlignDoubleArg(uint32_t offset) { return (offset + 1) & ~1; }
1907 static uint8_t* NextInstruction(uint8_t* instruction,
1908 uint32_t* count = nullptr);
1909
1910 // Toggle a jmp or cmp emitted by toggledJump().
1911 static void ToggleToJmp(CodeLocationLabel inst_);
1912 static void ToggleToCmp(CodeLocationLabel inst_);
1913
1914 static size_t ToggledCallSize(uint8_t* code);
1915 static void ToggleCall(CodeLocationLabel inst_, bool enabled);
1916
1917 void processCodeLabels(uint8_t* rawCode);
1918
1919 void verifyHeapAccessDisassembly(uint32_t begin, uint32_t end,
1920 const Disassembler::HeapAccess& heapAccess) {
1921 // Implement this if we implement a disassembler.
1922 }
1923 }; // Assembler
1924
1925 // An Instruction is a structure for both encoding and decoding any and all ARM
1926 // instructions. Many classes have not been implemented thus far.
1927 class Instruction {
1928 uint32_t data;
1929
1930 protected:
1931 // This is not for defaulting to always, this is for instructions that
1932 // cannot be made conditional, and have the usually invalid 4b1111 cond
1933 // field.
1934 explicit Instruction(uint32_t data_, bool fake = false)
1935 : data(data_ | 0xf0000000) {
1936 MOZ_ASSERT(fake || ((data_ & 0xf0000000) == 0));
1937 }
1938 // Standard constructor.
1939 Instruction(uint32_t data_, Assembler::Condition c)
1940 : data(data_ | (uint32_t)c) {
1941 MOZ_ASSERT((data_ & 0xf0000000) == 0);
1942 }
1943 // You should never create an instruction directly. You should create a more
1944 // specific instruction which will eventually call one of these constructors
1945 // for you.
1946 public:
1947 uint32_t encode() const { return data; }
1948 // Check if this instruction is really a particular case.
1949 template <class C>
1950 bool is() const {
1951 return C::IsTHIS(*this);
1952 }
1953
1954 // Safely get a more specific variant of this pointer.
1955 template <class C>
1956 C* as() const {
1957 return C::AsTHIS(*this);
1958 }
1959
1960 const Instruction& operator=(Instruction src) {
1961 data = src.data;
1962 return *this;
1963 }
1964 // Since almost all instructions have condition codes, the condition code
1965 // extractor resides in the base class.
1966 Assembler::Condition extractCond() const {
1967 MOZ_ASSERT(data >> 28 != 0xf,
1968 "The instruction does not have condition code");
1969 return (Assembler::Condition)(data & 0xf0000000);
1970 }
1971
1972 // Sometimes, an api wants a uint32_t (or a pointer to it) rather than an
1973 // instruction. raw() just coerces this into a pointer to a uint32_t.
1974 const uint32_t* raw() const { return &data; }
1975 uint32_t size() const { return 4; }
1976 }; // Instruction
1977
1978 // Make sure that it is the right size.
1979 static_assert(sizeof(Instruction) == 4);
1980
1981 inline void InstructionIterator::advanceRaw(ptrdiff_t instructions) {
1982 inst_ = inst_ + instructions;
1983 }
1984
1985 // Data Transfer Instructions.
1986 class InstDTR : public Instruction {
1987 public:
1988 enum IsByte_ { IsByte = 0x00400000, IsWord = 0x00000000 };
1989 static const int IsDTR = 0x04000000;
1990 static const int IsDTRMask = 0x0c000000;
1991
1992 // TODO: Replace the initialization with something that is safer.
1993 InstDTR(LoadStore ls, IsByte_ ib, Index mode, Register rt, DTRAddr addr,
1994 Assembler::Condition c)
1995 : Instruction(std::underlying_type_t<LoadStore>(ls) |
1996 std::underlying_type_t<IsByte_>(ib) |
1997 std::underlying_type_t<Index>(mode) | RT(rt) |
1998 addr.encode() | IsDTR,
1999 c) {}
2000
2001 static bool IsTHIS(const Instruction& i);
2002 static InstDTR* AsTHIS(const Instruction& i);
2003 };
2004 static_assert(sizeof(InstDTR) == sizeof(Instruction));
2005
2006 class InstLDR : public InstDTR {
2007 public:
2008 InstLDR(Index mode, Register rt, DTRAddr addr, Assembler::Condition c)
2009 : InstDTR(IsLoad, IsWord, mode, rt, addr, c) {}
2010
2011 static bool IsTHIS(const Instruction& i);
2012 static InstLDR* AsTHIS(const Instruction& i);
2013
2014 int32_t signedOffset() const {
2015 int32_t offset = encode() & 0xfff;
2016 if (IsUp_(encode() & IsUp) != IsUp) {
2017 return -offset;
2018 }
2019 return offset;
2020 }
2021 uint32_t* dest() const {
2022 int32_t offset = signedOffset();
2023 // When patching the load in PatchConstantPoolLoad, we ensure that the
2024 // offset is a multiple of 4, offset by 8 bytes from the actual
2025 // location. Indeed, when the base register is PC, ARM's 3 stages
2026 // pipeline design makes it that PC is off by 8 bytes (= 2 *
2027 // sizeof(uint32*)) when we actually executed it.
2028 MOZ_ASSERT(offset % 4 == 0);
2029 offset >>= 2;
2030 return (uint32_t*)raw() + offset + 2;
2031 }
2032 };
2033 static_assert(sizeof(InstDTR) == sizeof(InstLDR));
2034
2035 class InstNOP : public Instruction {
2036 public:
2037 static const uint32_t NopInst = 0x0320f000;
2038
2039 InstNOP() : Instruction(NopInst, Assembler::Always) {}
2040
2041 static bool IsTHIS(const Instruction& i);
2042 static InstNOP* AsTHIS(Instruction& i);
2043 };
2044
2045 // Branching to a register, or calling a register
2046 class InstBranchReg : public Instruction {
2047 protected:
2048 // Don't use BranchTag yourself, use a derived instruction.
2049 enum BranchTag { IsBX = 0x012fff10, IsBLX = 0x012fff30 };
2050
2051 static const uint32_t IsBRegMask = 0x0ffffff0;
2052
2053 InstBranchReg(BranchTag tag, Register rm, Assembler::Condition c)
2054 : Instruction(tag | rm.code(), c) {}
2055
2056 public:
2057 static bool IsTHIS(const Instruction& i);
2058 static InstBranchReg* AsTHIS(const Instruction& i);
2059
2060 // Get the register that is being branched to
2061 void extractDest(Register* dest);
2062 // Make sure we are branching to a pre-known register
2063 bool checkDest(Register dest);
2064 };
2065 static_assert(sizeof(InstBranchReg) == sizeof(Instruction));
2066
2067 // Branching to an immediate offset, or calling an immediate offset
2068 class InstBranchImm : public Instruction {
2069 protected:
2070 enum BranchTag { IsB = 0x0a000000, IsBL = 0x0b000000 };
2071
2072 static const uint32_t IsBImmMask = 0x0f000000;
2073
2074 InstBranchImm(BranchTag tag, BOffImm off, Assembler::Condition c)
2075 : Instruction(tag | off.encode(), c) {}
2076
2077 public:
2078 static bool IsTHIS(const Instruction& i);
2079 static InstBranchImm* AsTHIS(const Instruction& i);
2080
2081 void extractImm(BOffImm* dest);
2082 };
2083 static_assert(sizeof(InstBranchImm) == sizeof(Instruction));
2084
2085 // Very specific branching instructions.
2086 class InstBXReg : public InstBranchReg {
2087 public:
2088 static bool IsTHIS(const Instruction& i);
2089 static InstBXReg* AsTHIS(const Instruction& i);
2090 };
2091
2092 class InstBLXReg : public InstBranchReg {
2093 public:
2094 InstBLXReg(Register reg, Assembler::Condition c)
2095 : InstBranchReg(IsBLX, reg, c) {}
2096
2097 static bool IsTHIS(const Instruction& i);
2098 static InstBLXReg* AsTHIS(const Instruction& i);
2099 };
2100
2101 class InstBImm : public InstBranchImm {
2102 public:
2103 InstBImm(BOffImm off, Assembler::Condition c) : InstBranchImm(IsB, off, c) {}
2104
2105 static bool IsTHIS(const Instruction& i);
2106 static InstBImm* AsTHIS(const Instruction& i);
2107 };
2108
2109 class InstBLImm : public InstBranchImm {
2110 public:
2111 InstBLImm(BOffImm off, Assembler::Condition c)
2112 : InstBranchImm(IsBL, off, c) {}
2113
2114 static bool IsTHIS(const Instruction& i);
2115 static InstBLImm* AsTHIS(const Instruction& i);
2116 };
2117
2118 // Both movw and movt. The layout of both the immediate and the destination
2119 // register is the same so the code is being shared.
2120 class InstMovWT : public Instruction {
2121 protected:
2122 enum WT { IsW = 0x03000000, IsT = 0x03400000 };
2123 static const uint32_t IsWTMask = 0x0ff00000;
2124
2125 InstMovWT(Register rd, Imm16 imm, WT wt, Assembler::Condition c)
2126 : Instruction(RD(rd) | imm.encode() | wt, c) {}
2127
2128 public:
2129 void extractImm(Imm16* dest);
2130 void extractDest(Register* dest);
2131 bool checkImm(Imm16 dest);
2132 bool checkDest(Register dest);
2133
2134 static bool IsTHIS(Instruction& i);
2135 static InstMovWT* AsTHIS(Instruction& i);
2136 };
2137 static_assert(sizeof(InstMovWT) == sizeof(Instruction));
2138
2139 class InstMovW : public InstMovWT {
2140 public:
2141 InstMovW(Register rd, Imm16 imm, Assembler::Condition c)
2142 : InstMovWT(rd, imm, IsW, c) {}
2143
2144 static bool IsTHIS(const Instruction& i);
2145 static InstMovW* AsTHIS(const Instruction& i);
2146 };
2147
2148 class InstMovT : public InstMovWT {
2149 public:
2150 InstMovT(Register rd, Imm16 imm, Assembler::Condition c)
2151 : InstMovWT(rd, imm, IsT, c) {}
2152
2153 static bool IsTHIS(const Instruction& i);
2154 static InstMovT* AsTHIS(const Instruction& i);
2155 };
2156
2157 class InstALU : public Instruction {
2158 static const int32_t ALUMask = 0xc << 24;
2159
2160 public:
2161 InstALU(Register rd, Register rn, Operand2 op2, ALUOp op, SBit s,
2162 Assembler::Condition c)
2163 : Instruction(maybeRD(rd) | maybeRN(rn) | op2.encode() | op | s, c) {}
2164
2165 static bool IsTHIS(const Instruction& i);
2166 static InstALU* AsTHIS(const Instruction& i);
2167
2168 void extractOp(ALUOp* ret);
2169 bool checkOp(ALUOp op);
2170 void extractDest(Register* ret);
2171 bool checkDest(Register rd);
2172 void extractOp1(Register* ret);
2173 bool checkOp1(Register rn);
2174 Operand2 extractOp2();
2175 };
2176
2177 class InstCMP : public InstALU {
2178 public:
2179 static bool IsTHIS(const Instruction& i);
2180 static InstCMP* AsTHIS(const Instruction& i);
2181 };
2182
2183 class InstMOV : public InstALU {
2184 public:
2185 static bool IsTHIS(const Instruction& i);
2186 static InstMOV* AsTHIS(const Instruction& i);
2187 };
2188
2189 // Compile-time iterator over instructions, with a safe interface that
2190 // references not-necessarily-linear Instructions by linear BufferOffset.
2191 class BufferInstructionIterator
2192 : public ARMBuffer::AssemblerBufferInstIterator {
2193 public:
2194 BufferInstructionIterator(BufferOffset bo, ARMBuffer* buffer)
2195 : ARMBuffer::AssemblerBufferInstIterator(bo, buffer) {}
2196
2197 // Advances the buffer to the next intentionally-inserted instruction.
2198 Instruction* next() {
2199 advance(cur()->size());
2200 maybeSkipAutomaticInstructions();
2201 return cur();
2202 }
2203
2204 // Advances the BufferOffset past any automatically-inserted instructions.
2205 Instruction* maybeSkipAutomaticInstructions();
2206 };
2207
2208 static const uint32_t NumIntArgRegs = 4;
2209
2210 // There are 16 *float* registers available for arguments
2211 // If doubles are used, only half the number of registers are available.
2212 static const uint32_t NumFloatArgRegs = 16;
2213
2214 static inline bool GetIntArgReg(uint32_t usedIntArgs, uint32_t usedFloatArgs,
2215 Register* out) {
2216 if (usedIntArgs >= NumIntArgRegs) {
2217 return false;
2218 }
2219
2220 *out = Register::FromCode(usedIntArgs);
2221 return true;
2222 }
2223
2224 // Get a register in which we plan to put a quantity that will be used as an
2225 // integer argument. This differs from GetIntArgReg in that if we have no more
2226 // actual argument registers to use we will fall back on using whatever
2227 // CallTempReg* don't overlap the argument registers, and only fail once those
2228 // run out too.
2229 static inline bool GetTempRegForIntArg(uint32_t usedIntArgs,
2230 uint32_t usedFloatArgs, Register* out) {
2231 if (GetIntArgReg(usedIntArgs, usedFloatArgs, out)) {
2232 return true;
2233 }
2234
2235 // Unfortunately, we have to assume things about the point at which
2236 // GetIntArgReg returns false, because we need to know how many registers it
2237 // can allocate.
2238 usedIntArgs -= NumIntArgRegs;
2239 if (usedIntArgs >= NumCallTempNonArgRegs) {
2240 return false;
2241 }
2242
2243 *out = CallTempNonArgRegs[usedIntArgs];
2244 return true;
2245 }
2246
2247 class DoubleEncoder {
2248 struct DoubleEntry {
2249 uint32_t dblTop;
2250 datastore::Imm8VFPImmData data;
2251 };
2252
2253 static const DoubleEntry table[256];
2254
2255 public:
2256 bool lookup(uint32_t top, datastore::Imm8VFPImmData* ret) const {
2257 for (int i = 0; i < 256; i++) {
2258 if (table[i].dblTop == top) {
2259 *ret = table[i].data;
2260 return true;
2261 }
2262 }
2263 return false;
2264 }
2265 };
2266
2267 // Forbids nop filling for testing purposes. Not nestable.
2268 class AutoForbidNops {
2269 protected:
2270 Assembler* masm_;
2271
2272 public:
2273 explicit AutoForbidNops(Assembler* masm) : masm_(masm) {
2274 masm_->enterNoNops();
2275 }
2276 ~AutoForbidNops() { masm_->leaveNoNops(); }
2277 };
2278
2279 class AutoForbidPoolsAndNops : public AutoForbidNops {
2280 public:
2281 // The maxInst argument is the maximum number of word sized instructions
2282 // that will be allocated within this context. It is used to determine if
2283 // the pool needs to be dumped before entering this content. The debug code
2284 // checks that no more than maxInst instructions are actually allocated.
2285 //
2286 // Allocation of pool entries is not supported within this content so the
2287 // code can not use large integers or float constants etc.
2288 AutoForbidPoolsAndNops(Assembler* masm, size_t maxInst)
2289 : AutoForbidNops(masm) {
2290 masm_->enterNoPool(maxInst);
2291 }
2292
2293 ~AutoForbidPoolsAndNops() { masm_->leaveNoPool(); }
2294 };
2295
2296 } // namespace jit
2297 } // namespace js
2298
2299 #endif /* jit_arm_Assembler_arm_h */