Debian Codegraph @

js/src/jit/riscv64/Architecture-riscv64.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_riscv64_Architecture_riscv64_h
8 #define jit_riscv64_Architecture_riscv64_h
9
10 // JitSpewer.h is included through MacroAssembler implementations for other
11 // platforms, so include it here to avoid inadvertent build bustage.
12 #include "mozilla/MathAlgorithms.h"
13
14 #include <algorithm>
15 #include <iterator>
16
17 #include "jit/JitSpewer.h"
18 #include "jit/shared/Architecture-shared.h"
19 #include "js/Utility.h"
20
21 namespace js {
22 namespace jit {
23
24 static const uint32_t SimdMemoryAlignment =
25 16; // Make it 4 to avoid a bunch of div-by-zero warnings
26
27 // RISCV64 has 32 64-bit integer registers, x0 though x31.
28 // The program counter is not accessible as a register.
29
30 // RISCV INT Register Convention:
31 // Name Alias Usage
32 // x0 zero hardwired to 0, ignores writes
33 // x1 ra return address for calls
34 // x2 sp stack pointer
35 // x3 gp global pointer
36 // x4 tp thread pointer
37 // x5-x7 t0-t2 temporary register 0
38 // x8 fp/s0 Callee-saved register 0 or frame pointer
39 // x9 s1 Callee-saved register 1
40 // x10-x11 a0-a1 return value or function argument
41 // x12-x17 a2-a7 function argument 2
42 // x18-x27 s2-s11 Callee-saved register
43 // x28-x31 t3-t6 temporary register 3
44
45 // RISCV-64 FP Register Convention:
46 // Name Alias Usage
47 // $f0-$f7 $ft0-$ft7 Temporary registers
48 // $f8-$f9 $fs0-$fs1 Callee-saved registers
49 // $f10-$f11 $fa0-$fa1 Return values
50 // $f12-$f17 $fa2-$fa7 Args values
51 // $f18-$f27 $fs2-$fs11 Callee-saved registers
52 // $f28-$f31 $ft8-$ft11 Temporary registers
53 class Registers {
54 public:
55 enum RegisterID {
56 x0 = 0,
57 x1,
58 x2,
59 x3,
60 x4,
61 x5,
62 x6,
63 x7,
64 x8,
65 x9,
66 x10,
67 x11,
68 x12,
69 x13,
70 x14,
71 x15,
72 x16,
73 x17,
74 x18,
75 x19,
76 x20,
77 x21,
78 x22,
79 x23,
80 x24,
81 x25,
82 x26,
83 x27,
84 x28,
85 x29,
86 x30,
87 x31,
88 zero = x0,
89 ra = x1,
90 sp = x2,
91 gp = x3,
92 tp = x4,
93 t0 = x5,
94 t1 = x6,
95 t2 = x7,
96 fp = x8,
97 s1 = x9,
98 a0 = x10,
99 a1 = x11,
100 a2 = x12,
101 a3 = x13,
102 a4 = x14,
103 a5 = x15,
104 a6 = x16,
105 a7 = x17,
106 s2 = x18,
107 s3 = x19,
108 s4 = x20,
109 s5 = x21,
110 s6 = x22,
111 s7 = x23,
112 s8 = x24,
113 s9 = x25,
114 s10 = x26,
115 s11 = x27,
116 t3 = x28,
117 t4 = x29,
118 t5 = x30,
119 t6 = x31,
120 invalid_reg,
121 };
122 typedef uint8_t Code;
123 typedef RegisterID Encoding;
124 union RegisterContent {
125 uintptr_t r;
126 };
127
128 typedef uint32_t SetType;
129
130 static uint32_t SetSize(SetType x) {
131 static_assert(sizeof(SetType) == 4, "SetType must be 32 bits");
132 return mozilla::CountPopulation32(x);
133 }
134 static uint32_t FirstBit(SetType x) {
135 return mozilla::CountTrailingZeroes32(x);
136 }
137 static uint32_t LastBit(SetType x) {
138 return 31 - mozilla::CountLeadingZeroes32(x);
139 }
140 static const char* GetName(uint32_t code) {
141 static const char* const Names[] = {
142 "zero", "ra", "sp", "gp", "tp", "t0", "t1", "t2", "fp", "s1", "a0",
143 "a1", "a2", "a3", "a4", "a5", "a6", "a7", "s2", "s3", "s4", "s5",
144 "s6", "s7", "s8", "s9", "s10", "s11", "t3", "t4", "t5", "t6"};
145 static_assert(Total == std::size(Names), "Table is the correct size");
146 if (code >= Total) {
147 return "invalid";
148 }
149 return Names[code];
150 }
151
152 static Code FromName(const char*);
153
154 static const Encoding StackPointer = sp;
155 static const Encoding Invalid = invalid_reg;
156 static const uint32_t Total = 32;
157 static const uint32_t TotalPhys = 32;
158 static const uint32_t Allocatable = 24;
159 static const SetType NoneMask = 0x0;
160 static const SetType AllMask = 0xFFFFFFFF;
161 static const SetType ArgRegMask =
162 (1 << Registers::a0) | (1 << Registers::a1) | (1 << Registers::a2) |
163 (1 << Registers::a3) | (1 << Registers::a4) | (1 << Registers::a5) |
164 (1 << Registers::a6) | (1 << Registers::a7);
165
166 static const SetType VolatileMask =
167 ArgRegMask | (1 << Registers::t0) | (1 << Registers::t1) |
168 (1 << Registers::t2) | (1 << Registers::t3) | (1 << Registers::t4) |
169 (1 << Registers::t5) | (1 << Registers::t6);
170
171 // We use this constant to save registers when entering functions. This
172 // is why $ra is added here even though it is not "Non Volatile".
173 static const SetType NonVolatileMask =
174 (1 << Registers::ra) | (1 << Registers::fp) | (1 << Registers::s1) |
175 (1 << Registers::s2) | (1 << Registers::s3) | (1 << Registers::s4) |
176 (1 << Registers::s5) | (1 << Registers::s6) | (1 << Registers::s7) |
177 (1 << Registers::s8) | (1 << Registers::s9) | (1 << Registers::s10) |
178 (1 << Registers::s11);
179
180 static const SetType NonAllocatableMask =
181 (1 << Registers::zero) | // Always be zero.
182 (1 << Registers::t4) | // Scratch reg
183 (1 << Registers::t5) | // Scratch reg
184 (1 << Registers::t6) | // Scratch reg or call reg
185 (1 << Registers::s11) | // Scratch reg
186 (1 << Registers::ra) | (1 << Registers::tp) | (1 << Registers::sp) |
187 (1 << Registers::fp) | (1 << Registers::gp);
188
189 static const SetType AllocatableMask = AllMask & ~NonAllocatableMask;
190
191 // Registers returned from a JS -> JS call.
192 static const SetType JSCallMask = (1 << Registers::a2);
193
194 // Registers returned from a JS -> C call.
195 static const SetType CallMask = (1 << Registers::a0);
196
197 static const SetType WrapperMask = VolatileMask;
198 };
199
200 // Smallest integer type that can hold a register bitmask.
201 typedef uint32_t PackedRegisterMask;
202
203 class FloatRegisters {
204 public:
205 enum FPRegisterID {
206 f0 = 0,
207 f1,
208 f2,
209 f3,
210 f4,
211 f5,
212 f6,
213 f7,
214 f8,
215 f9,
216 f10,
217 f11,
218 f12,
219 f13,
220 f14,
221 f15,
222 f16,
223 f17,
224 f18,
225 f19,
226 f20,
227 f21,
228 f22,
229 f23,
230 f24,
231 f25,
232 f26,
233 f27,
234 f28,
235 f29,
236 f30,
237 f31,
238 invalid_reg,
239 ft0 = f0,
240 ft1 = f1,
241 ft2 = f2,
242 ft3 = f3,
243 ft4 = f4,
244 ft5 = f5,
245 ft6 = f6,
246 ft7 = f7,
247 fs0 = f8,
248 fs1 = f9,
249 fa0 = f10,
250 fa1 = f11,
251 fa2 = f12,
252 fa3 = f13,
253 fa4 = f14,
254 fa5 = f15,
255 fa6 = f16,
256 fa7 = f17,
257 fs2 = f18,
258 fs3 = f19,
259 fs4 = f20,
260 fs5 = f21,
261 fs6 = f22,
262 fs7 = f23,
263 fs8 = f24,
264 fs9 = f25,
265 fs10 = f26,
266 fs11 = f27, // Scratch register
267 ft8 = f28,
268 ft9 = f29,
269 ft10 = f30, // Scratch register
270 ft11 = f31
271 };
272
273 enum Kind : uint8_t { Double, NumTypes, Single };
274
275 typedef FPRegisterID Code;
276 typedef FPRegisterID Encoding;
277 union RegisterContent {
278 float s;
279 double d;
280 };
281
282 static const char* GetName(uint32_t code) {
283 static const char* const Names[] = {
284 "ft0", "ft1", "ft2", "ft3", "ft4", "ft5", "ft6", "ft7",
285 "fs0", "fs2", "fa0", "fa1", "fa2", "fa3", "fa4", "fa5",
286 "fa6", "fa7", "fs2", "fs3", "fs4", "fs5", "fs6", "fs7",
287 "fs8", "fs9", "fs10", "fs11", "ft8", "ft9", "ft10", "ft11"};
288 static_assert(TotalPhys == std::size(Names), "Table is the correct size");
289 if (code >= Total) {
290 return "invalid";
291 }
292 return Names[code];
293 }
294
295 static Code FromName(const char* name);
296
297 typedef uint32_t SetType;
298
299 static const Code Invalid = invalid_reg;
300 static const uint32_t Total = 32;
301 static const uint32_t TotalPhys = 32;
302 static const uint32_t Allocatable = 23;
303 static const SetType AllPhysMask = 0xFFFFFFFF;
304 static const SetType AllMask = 0xFFFFFFFF;
305 static const SetType AllDoubleMask = AllMask;
306 // Single values are stored as 64 bits values (NaN-boxed) when pushing them to
307 // the stack, we do not require making distinctions between the 2 types, and
308 // therefore the masks are overlapping.See The RISC-V Instruction Set Manual
309 // for 14.2 NaN Boxing of Narrower Values.
310 static const SetType AllSingleMask = AllMask;
311 static const SetType NonVolatileMask =
312 SetType((1 << FloatRegisters::fs0) | (1 << FloatRegisters::fs1) |
313 (1 << FloatRegisters::fs2) | (1 << FloatRegisters::fs3) |
314 (1 << FloatRegisters::fs4) | (1 << FloatRegisters::fs5) |
315 (1 << FloatRegisters::fs6) | (1 << FloatRegisters::fs7) |
316 (1 << FloatRegisters::fs8) | (1 << FloatRegisters::fs9) |
317 (1 << FloatRegisters::fs10) | (1 << FloatRegisters::fs11));
318 static const SetType VolatileMask = AllMask & ~NonVolatileMask;
319
320 // fs11/ft10 is the scratch register.
321 static const SetType NonAllocatableMask =
322 SetType((1 << FloatRegisters::fs11) | (1 << FloatRegisters::ft10));
323
324 static const SetType AllocatableMask = AllMask & ~NonAllocatableMask;
325 };
326
327 template <typename T>
328 class TypedRegisterSet;
329
330 struct FloatRegister {
331 public:
332 typedef FloatRegisters Codes;
333 typedef Codes::Code Code;
334 typedef Codes::Encoding Encoding;
335 typedef Codes::SetType SetType;
336
337 static uint32_t SetSize(SetType x) {
338 static_assert(sizeof(SetType) == 4, "SetType must be 32 bits");
339 x &= FloatRegisters::AllPhysMask;
340 return mozilla::CountPopulation32(x);
341 }
342
343 static uint32_t FirstBit(SetType x) {
344 static_assert(sizeof(SetType) == 4, "SetType");
345 return mozilla::CountTrailingZeroes64(x);
346 }
347 static uint32_t LastBit(SetType x) {
348 static_assert(sizeof(SetType) == 4, "SetType");
349 return 31 - mozilla::CountLeadingZeroes64(x);
350 }
351
352 static FloatRegister FromCode(uint32_t i) {
353 uint32_t code = i & 0x1f;
354 return FloatRegister(Code(code));
355 }
356 bool isSimd128() const { return false; }
357 bool isInvalid() const { return invalid_; }
358 FloatRegister asSingle() const {
359 MOZ_ASSERT(!invalid_);
360 return FloatRegister(Encoding(encoding_), FloatRegisters::Single);
361 }
362 FloatRegister asDouble() const {
363 MOZ_ASSERT(!invalid_);
364 return FloatRegister(Encoding(encoding_), FloatRegisters::Double);
365 }
366 FloatRegister asSimd128() const { MOZ_CRASH(); }
367 constexpr Code code() const {
368 MOZ_ASSERT(!invalid_);
369 return encoding_;
370 }
371 Encoding encoding() const { return encoding_; }
372 const char* name() const { return FloatRegisters::GetName(code()); }
373 bool volatile_() const {
374 MOZ_ASSERT(!invalid_);
375 return !!((SetType(1) << code()) & FloatRegisters::VolatileMask);
376 }
377 bool operator!=(FloatRegister other) const { return code() != other.code(); }
378 bool operator==(FloatRegister other) const { return code() == other.code(); }
379 bool aliases(FloatRegister other) const {
380 return other.encoding_ == encoding_;
381 }
382 uint32_t numAliased() const { return 1; }
383 FloatRegister aliased(uint32_t aliasIdx) const {
384 MOZ_ASSERT(aliasIdx == 0);
385 return *this;
386 }
387 // Ensure that two floating point registers' types are equivalent.
388 bool equiv(FloatRegister other) const {
389 MOZ_ASSERT(!invalid_);
390 return kind_ == other.kind_;
391 }
392 constexpr uint32_t size() const {
393 MOZ_ASSERT(!invalid_);
394 if (kind_ == FloatRegisters::Double) {
395 return sizeof(double);
396 }
397 MOZ_ASSERT(kind_ == FloatRegisters::Single);
398 return sizeof(float);
399 }
400 uint32_t numAlignedAliased() { return numAliased(); }
401 FloatRegister alignedAliased(uint32_t aliasIdx) {
402 MOZ_ASSERT(aliasIdx < numAliased());
403 return aliased(aliasIdx);
404 }
405 SetType alignedOrDominatedAliasedSet() const { return SetType(1) << code(); }
406 static constexpr RegTypeName DefaultType = RegTypeName::Float64;
407
408 template <RegTypeName Name = DefaultType>
409 static SetType LiveAsIndexableSet(SetType s) {
410 return SetType(0);
411 }
412
413 template <RegTypeName Name = DefaultType>
414 static SetType AllocatableAsIndexableSet(SetType s) {
415 static_assert(Name != RegTypeName::Any, "Allocatable set are not iterable");
416 return LiveAsIndexableSet<Name>(s);
417 }
418
419 FloatRegister singleOverlay() const;
420 FloatRegister doubleOverlay() const;
421
422 static TypedRegisterSet<FloatRegister> ReduceSetForPush(
423 const TypedRegisterSet<FloatRegister>& s);
424
425 uint32_t getRegisterDumpOffsetInBytes() {
426 #ifdef ENABLE_WASM_SIMD
427 # error "Needs more careful logic if SIMD is enabled"
428 #endif
429
430 return code() * sizeof(double);
431 }
432 static Code FromName(const char* name);
433
434 // This is used in static initializers, so produce a bogus value instead of
435 // crashing.
436 static uint32_t GetPushSizeInBytes(const TypedRegisterSet<FloatRegister>& s);
437
438 private:
439 typedef Codes::Kind Kind;
440 // These fields only hold valid values: an invalid register is always
441 // represented as a valid encoding and kind with the invalid_ bit set.
442 Encoding encoding_; // 32 encodings
443 Kind kind_; // Double, Single; more later
444 bool invalid_;
445
446 public:
447 constexpr FloatRegister(Encoding encoding, Kind kind)
448 : encoding_(encoding), kind_(kind), invalid_(false) {
449 MOZ_ASSERT(uint32_t(encoding) < Codes::Total);
450 }
451
452 constexpr FloatRegister(Encoding encoding)
453 : encoding_(encoding), kind_(FloatRegisters::Double), invalid_(false) {
454 MOZ_ASSERT(uint32_t(encoding) < Codes::Total);
455 }
456
457 constexpr FloatRegister()
458 : encoding_(FloatRegisters::invalid_reg),
459 kind_(FloatRegisters::Double),
460 invalid_(true) {}
461
462 bool isSingle() const {
463 MOZ_ASSERT(!invalid_);
464 // On riscv64 arch, float register and double register using the same
465 // register file.
466 return kind_ == FloatRegisters::Single || kind_ == FloatRegisters::Double;
467 }
468 bool isDouble() const {
469 MOZ_ASSERT(!invalid_);
470 return kind_ == FloatRegisters::Double;
471 }
472
473 Encoding code() { return encoding_; }
474 };
475
476 template <>
477 inline FloatRegister::SetType
478 FloatRegister::LiveAsIndexableSet<RegTypeName::Float32>(SetType set) {
479 return set & FloatRegisters::AllSingleMask;
480 }
481
482 template <>
483 inline FloatRegister::SetType
484 FloatRegister::LiveAsIndexableSet<RegTypeName::Float64>(SetType set) {
485 return set & FloatRegisters::AllDoubleMask;
486 }
487
488 template <>
489 inline FloatRegister::SetType
490 FloatRegister::LiveAsIndexableSet<RegTypeName::Any>(SetType set) {
491 return set;
492 }
493
494 inline bool hasUnaliasedDouble() { return false; }
495 inline bool hasMultiAlias() { return false; }
496
497 static constexpr uint32_t ShadowStackSpace = 0;
498 static const uint32_t JumpImmediateRange = INT32_MAX;
499
500 #ifdef JS_NUNBOX32
501 static const int32_t NUNBOX32_TYPE_OFFSET = 4;
502 static const int32_t NUNBOX32_PAYLOAD_OFFSET = 0;
503 #endif
504
505 static const uint32_t SpillSlotSize =
506 std::max(sizeof(Registers::RegisterContent),
507 sizeof(FloatRegisters::RegisterContent));
508
509 inline uint32_t GetRISCV64Flags() { return 0; }
510
511 } // namespace jit
512 } // namespace js
513
514 #endif /* jit_riscv64_Architecture_riscv64_h */