Debian Codegraph @

js/src/jit/riscv64/constant/Base-constant-riscv.h

raw
1 // Copyright 2022 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 #ifndef jit_riscv64_constant_Base_constant_riscv__h_
5 #define jit_riscv64_constant_Base_constant_riscv__h_
6 namespace js {
7 namespace jit {
8
9 // On RISC-V Simulator breakpoints can have different codes:
10 // - Breaks between 0 and kMaxWatchpointCode are treated as simple watchpoints,
11 // the simulator will run through them and print the registers.
12 // - Breaks between kMaxWatchpointCode and kMaxStopCode are treated as stop()
13 // instructions (see Assembler::stop()).
14 // - Breaks larger than kMaxStopCode are simple breaks, dropping you into the
15 // debugger.
16 const uint32_t kMaxTracepointCode = 63;
17 const uint32_t kMaxWatchpointCode = 31;
18 const uint32_t kMaxStopCode = 127;
19 const uint32_t kWasmTrapCode = 6;
20 static_assert(kMaxWatchpointCode < kMaxStopCode);
21 static_assert(kMaxTracepointCode < kMaxStopCode);
22
23 // Debug parameters.
24 //
25 // For example:
26 //
27 // __ Debug(TRACE_ENABLE | LOG_TRACE);
28 // starts tracing: set v8_flags.trace-sim is true.
29 // __ Debug(TRACE_ENABLE | LOG_REGS);
30 // PrintAllregs.
31 // __ Debug(TRACE_DISABLE | LOG_TRACE);
32 // stops tracing: set v8_flags.trace-sim is false.
33 const uint32_t kDebuggerTracingDirectivesMask = 0b111 << 3;
34 enum DebugParameters : uint32_t {
35 NO_PARAM = 1 << 5,
36 BREAK = 1 << 0,
37 LOG_TRACE = 1 << 1,
38 LOG_REGS = 1 << 2,
39 LOG_ALL = LOG_TRACE,
40 // Trace control.
41 TRACE_ENABLE = 1 << 3 | NO_PARAM,
42 TRACE_DISABLE = 1 << 4 | NO_PARAM,
43 };
44 // On RISCV all instructions are 32 bits, except for RVC.
45 using Instr = int32_t;
46 using ShortInstr = int16_t;
47 typedef unsigned char byte;
48 // ----- Fields offset and length.
49 // RISCV constants
50 const int kBaseOpcodeShift = 0;
51 const int kBaseOpcodeBits = 7;
52 const int kFunct7Shift = 25;
53 const int kFunct7Bits = 7;
54 const int kFunct5Shift = 27;
55 const int kFunct5Bits = 5;
56 const int kFunct3Shift = 12;
57 const int kFunct3Bits = 3;
58 const int kFunct2Shift = 25;
59 const int kFunct2Bits = 2;
60 const int kRs1Shift = 15;
61 const int kRs1Bits = 5;
62 const int kVs1Shift = 15;
63 const int kVs1Bits = 5;
64 const int kVs2Shift = 20;
65 const int kVs2Bits = 5;
66 const int kVdShift = 7;
67 const int kVdBits = 5;
68 const int kRs2Shift = 20;
69 const int kRs2Bits = 5;
70 const int kRs3Shift = 27;
71 const int kRs3Bits = 5;
72 const int kRdShift = 7;
73 const int kRdBits = 5;
74 const int kRlShift = 25;
75 const int kAqShift = 26;
76 const int kImm12Shift = 20;
77 const int kImm12Bits = 12;
78 const int kImm11Shift = 2;
79 const int kImm11Bits = 11;
80 const int kShamtShift = 20;
81 const int kShamtBits = 5;
82 const int kShamtWShift = 20;
83 // FIXME: remove this once we have a proper way to handle the wide shift amount
84 const int kShamtWBits = 6;
85 const int kArithShiftShift = 30;
86 const int kImm20Shift = 12;
87 const int kImm20Bits = 20;
88 const int kCsrShift = 20;
89 const int kCsrBits = 12;
90 const int kMemOrderBits = 4;
91 const int kPredOrderShift = 24;
92 const int kSuccOrderShift = 20;
93
94 // for C extension
95 const int kRvcFunct4Shift = 12;
96 const int kRvcFunct4Bits = 4;
97 const int kRvcFunct3Shift = 13;
98 const int kRvcFunct3Bits = 3;
99 const int kRvcRs1Shift = 7;
100 const int kRvcRs1Bits = 5;
101 const int kRvcRs2Shift = 2;
102 const int kRvcRs2Bits = 5;
103 const int kRvcRdShift = 7;
104 const int kRvcRdBits = 5;
105 const int kRvcRs1sShift = 7;
106 const int kRvcRs1sBits = 3;
107 const int kRvcRs2sShift = 2;
108 const int kRvcRs2sBits = 3;
109 const int kRvcFunct2Shift = 5;
110 const int kRvcFunct2BShift = 10;
111 const int kRvcFunct2Bits = 2;
112 const int kRvcFunct6Shift = 10;
113 const int kRvcFunct6Bits = 6;
114
115 const uint32_t kRvcOpcodeMask =
116 0b11 | (((1 << kRvcFunct3Bits) - 1) << kRvcFunct3Shift);
117 const uint32_t kRvcFunct3Mask =
118 (((1 << kRvcFunct3Bits) - 1) << kRvcFunct3Shift);
119 const uint32_t kRvcFunct4Mask =
120 (((1 << kRvcFunct4Bits) - 1) << kRvcFunct4Shift);
121 const uint32_t kRvcFunct6Mask =
122 (((1 << kRvcFunct6Bits) - 1) << kRvcFunct6Shift);
123 const uint32_t kRvcFunct2Mask =
124 (((1 << kRvcFunct2Bits) - 1) << kRvcFunct2Shift);
125 const uint32_t kRvcFunct2BMask =
126 (((1 << kRvcFunct2Bits) - 1) << kRvcFunct2BShift);
127 const uint32_t kCRTypeMask = kRvcOpcodeMask | kRvcFunct4Mask;
128 const uint32_t kCSTypeMask = kRvcOpcodeMask | kRvcFunct6Mask;
129 const uint32_t kCATypeMask = kRvcOpcodeMask | kRvcFunct6Mask | kRvcFunct2Mask;
130 const uint32_t kRvcBImm8Mask = (((1 << 5) - 1) << 2) | (((1 << 3) - 1) << 10);
131
132 // RISCV Instruction bit masks
133 const uint32_t kBaseOpcodeMask = ((1 << kBaseOpcodeBits) - 1)
134 << kBaseOpcodeShift;
135 const uint32_t kFunct3Mask = ((1 << kFunct3Bits) - 1) << kFunct3Shift;
136 const uint32_t kFunct5Mask = ((1 << kFunct5Bits) - 1) << kFunct5Shift;
137 const uint32_t kFunct7Mask = ((1 << kFunct7Bits) - 1) << kFunct7Shift;
138 const uint32_t kFunct2Mask = 0b11 << kFunct7Shift;
139 const uint32_t kRTypeMask = kBaseOpcodeMask | kFunct3Mask | kFunct7Mask;
140 const uint32_t kRATypeMask = kBaseOpcodeMask | kFunct3Mask | kFunct5Mask;
141 const uint32_t kRFPTypeMask = kBaseOpcodeMask | kFunct7Mask;
142 const uint32_t kR4TypeMask = kBaseOpcodeMask | kFunct3Mask | kFunct2Mask;
143 const uint32_t kITypeMask = kBaseOpcodeMask | kFunct3Mask;
144 const uint32_t kSTypeMask = kBaseOpcodeMask | kFunct3Mask;
145 const uint32_t kBTypeMask = kBaseOpcodeMask | kFunct3Mask;
146 const uint32_t kUTypeMask = kBaseOpcodeMask;
147 const uint32_t kJTypeMask = kBaseOpcodeMask;
148 const uint32_t kRs1FieldMask = ((1 << kRs1Bits) - 1) << kRs1Shift;
149 const uint32_t kRs2FieldMask = ((1 << kRs2Bits) - 1) << kRs2Shift;
150 const uint32_t kRs3FieldMask = ((1 << kRs3Bits) - 1) << kRs3Shift;
151 const uint32_t kRdFieldMask = ((1 << kRdBits) - 1) << kRdShift;
152 const uint32_t kBImm12Mask = kFunct7Mask | kRdFieldMask;
153 const uint32_t kImm20Mask = ((1 << kImm20Bits) - 1) << kImm20Shift;
154 const uint32_t kImm12Mask = ((1 << kImm12Bits) - 1) << kImm12Shift;
155 const uint32_t kImm11Mask = ((1 << kImm11Bits) - 1) << kImm11Shift;
156 const uint32_t kImm31_12Mask = ((1 << 20) - 1) << 12;
157 const uint32_t kImm19_0Mask = ((1 << 20) - 1);
158
159 // for RVV extension
160 #define RVV_LMUL(V) \
161 V(m1) \
162 V(m2) \
163 V(m4) \
164 V(m8) \
165 V(RESERVERD) \
166 V(mf8) \
167 V(mf4) \
168 V(mf2)
169
170 enum Vlmul {
171 #define DEFINE_FLAG(name) name,
172 RVV_LMUL(DEFINE_FLAG)
173 #undef DEFINE_FLAG
174 };
175
176 #define RVV_SEW(V) \
177 V(E8) \
178 V(E16) \
179 V(E32) \
180 V(E64)
181
182 #define DEFINE_FLAG(name) name,
183 enum VSew {
184 RVV_SEW(DEFINE_FLAG)
185 #undef DEFINE_FLAG
186 };
187
188 constexpr int kRvvELEN = 64;
189 constexpr int kRvvVLEN = 128;
190 constexpr int kRvvSLEN = kRvvVLEN;
191 const int kRvvFunct6Shift = 26;
192 const int kRvvFunct6Bits = 6;
193 const uint32_t kRvvFunct6Mask =
194 (((1 << kRvvFunct6Bits) - 1) << kRvvFunct6Shift);
195
196 const int kRvvVmBits = 1;
197 const int kRvvVmShift = 25;
198 const uint32_t kRvvVmMask = (((1 << kRvvVmBits) - 1) << kRvvVmShift);
199
200 const int kRvvVs2Bits = 5;
201 const int kRvvVs2Shift = 20;
202 const uint32_t kRvvVs2Mask = (((1 << kRvvVs2Bits) - 1) << kRvvVs2Shift);
203
204 const int kRvvVs1Bits = 5;
205 const int kRvvVs1Shift = 15;
206 const uint32_t kRvvVs1Mask = (((1 << kRvvVs1Bits) - 1) << kRvvVs1Shift);
207
208 const int kRvvRs1Bits = kRvvVs1Bits;
209 const int kRvvRs1Shift = kRvvVs1Shift;
210 const uint32_t kRvvRs1Mask = (((1 << kRvvRs1Bits) - 1) << kRvvRs1Shift);
211
212 const int kRvvRs2Bits = 5;
213 const int kRvvRs2Shift = 20;
214 const uint32_t kRvvRs2Mask = (((1 << kRvvRs2Bits) - 1) << kRvvRs2Shift);
215
216 const int kRvvImm5Bits = kRvvVs1Bits;
217 const int kRvvImm5Shift = kRvvVs1Shift;
218 const uint32_t kRvvImm5Mask = (((1 << kRvvImm5Bits) - 1) << kRvvImm5Shift);
219
220 const int kRvvVdBits = 5;
221 const int kRvvVdShift = 7;
222 const uint32_t kRvvVdMask = (((1 << kRvvVdBits) - 1) << kRvvVdShift);
223
224 const int kRvvRdBits = kRvvVdBits;
225 const int kRvvRdShift = kRvvVdShift;
226 const uint32_t kRvvRdMask = (((1 << kRvvRdBits) - 1) << kRvvRdShift);
227
228 const int kRvvZimmBits = 11;
229 const int kRvvZimmShift = 20;
230 const uint32_t kRvvZimmMask = (((1 << kRvvZimmBits) - 1) << kRvvZimmShift);
231
232 const int kRvvUimmShift = kRvvRs1Shift;
233 const int kRvvUimmBits = kRvvRs1Bits;
234 const uint32_t kRvvUimmMask = (((1 << kRvvUimmBits) - 1) << kRvvUimmShift);
235
236 const int kRvvWidthBits = 3;
237 const int kRvvWidthShift = 12;
238 const uint32_t kRvvWidthMask = (((1 << kRvvWidthBits) - 1) << kRvvWidthShift);
239
240 const int kRvvMopBits = 2;
241 const int kRvvMopShift = 26;
242 const uint32_t kRvvMopMask = (((1 << kRvvMopBits) - 1) << kRvvMopShift);
243
244 const int kRvvMewBits = 1;
245 const int kRvvMewShift = 28;
246 const uint32_t kRvvMewMask = (((1 << kRvvMewBits) - 1) << kRvvMewShift);
247
248 const int kRvvNfBits = 3;
249 const int kRvvNfShift = 29;
250 const uint32_t kRvvNfMask = (((1 << kRvvNfBits) - 1) << kRvvNfShift);
251
252 const int kNopByte = 0x00000013;
253
254 enum BaseOpcode : uint32_t {
255 LOAD = 0b0000011, // I form: LB LH LW LBU LHU
256 LOAD_FP = 0b0000111, // I form: FLW FLD FLQ
257 MISC_MEM = 0b0001111, // I special form: FENCE FENCE.I
258 OP_IMM = 0b0010011, // I form: ADDI SLTI SLTIU XORI ORI ANDI SLLI SRLI SRAI
259 // Note: SLLI/SRLI/SRAI I form first, then func3 001/101 => R type
260 AUIPC = 0b0010111, // U form: AUIPC
261 OP_IMM_32 = 0b0011011, // I form: ADDIW SLLIW SRLIW SRAIW
262 // Note: SRLIW SRAIW I form first, then func3 101 special shift encoding
263 STORE = 0b0100011, // S form: SB SH SW SD
264 STORE_FP = 0b0100111, // S form: FSW FSD FSQ
265 AMO = 0b0101111, // R form: All A instructions
266 OP = 0b0110011, // R: ADD SUB SLL SLT SLTU XOR SRL SRA OR AND and 32M set
267 LUI = 0b0110111, // U form: LUI
268 OP_32 = 0b0111011, // R: ADDW SUBW SLLW SRLW SRAW MULW DIVW DIVUW REMW REMUW
269 MADD = 0b1000011, // R4 type: FMADD.S FMADD.D FMADD.Q
270 MSUB = 0b1000111, // R4 type: FMSUB.S FMSUB.D FMSUB.Q
271 NMSUB = 0b1001011, // R4 type: FNMSUB.S FNMSUB.D FNMSUB.Q
272 NMADD = 0b1001111, // R4 type: FNMADD.S FNMADD.D FNMADD.Q
273 OP_FP = 0b1010011, // R type: Q ext
274 BRANCH = 0b1100011, // B form: BEQ BNE, BLT, BGE, BLTU BGEU
275 JALR = 0b1100111, // I form: JALR
276 JAL = 0b1101111, // J form: JAL
277 SYSTEM = 0b1110011, // I form: ECALL EBREAK Zicsr ext
278 OP_V = 0b1010111, // V form: RVV
279
280 // C extension
281 C0 = 0b00,
282 C1 = 0b01,
283 C2 = 0b10,
284 FUNCT2_0 = 0b00,
285 FUNCT2_1 = 0b01,
286 FUNCT2_2 = 0b10,
287 FUNCT2_3 = 0b11,
288 };
289
290 // ----- Emulated conditions.
291 // On RISC-V we use this enum to abstract from conditional branch instructions.
292 // The 'U' prefix is used to specify unsigned comparisons.
293 // Opposite conditions must be paired as odd/even numbers
294 // because 'NegateCondition' function flips LSB to negate condition.
295 enum RiscvCondition { // Any value < 0 is considered no_condition.
296 overflow = 0,
297 no_overflow = 1,
298 Uless = 2,
299 Ugreater_equal = 3,
300 Uless_equal = 4,
301 Ugreater = 5,
302 equal = 6,
303 not_equal = 7, // Unordered or Not Equal.
304 less = 8,
305 greater_equal = 9,
306 less_equal = 10,
307 greater = 11,
308 cc_always = 12,
309
310 // Aliases.
311 eq = equal,
312 ne = not_equal,
313 ge = greater_equal,
314 lt = less,
315 gt = greater,
316 le = less_equal,
317 al = cc_always,
318 ult = Uless,
319 uge = Ugreater_equal,
320 ule = Uless_equal,
321 ugt = Ugreater,
322 };
323
324 // ----- Coprocessor conditions.
325 enum FPUCondition {
326 kNoFPUCondition = -1,
327 EQ = 0x02, // Ordered and Equal
328 NE = 0x03, // Unordered or Not Equal
329 LT = 0x04, // Ordered and Less Than
330 GE = 0x05, // Ordered and Greater Than or Equal
331 LE = 0x06, // Ordered and Less Than or Equal
332 GT = 0x07, // Ordered and Greater Than
333 };
334
335 enum CheckForInexactConversion {
336 kCheckForInexactConversion,
337 kDontCheckForInexactConversion
338 };
339
340 enum class MaxMinKind : int { kMin = 0, kMax = 1 };
341
342 // ----------------------------------------------------------------------------
343 // RISCV flags
344
345 enum ControlStatusReg {
346 csr_fflags = 0x001, // Floating-Point Accrued Exceptions (RW)
347 csr_frm = 0x002, // Floating-Point Dynamic Rounding Mode (RW)
348 csr_fcsr = 0x003, // Floating-Point Control and Status Register (RW)
349 csr_cycle = 0xc00, // Cycle counter for RDCYCLE instruction (RO)
350 csr_time = 0xc01, // Timer for RDTIME instruction (RO)
351 csr_instret = 0xc02, // Insns-retired counter for RDINSTRET instruction (RO)
352 csr_cycleh = 0xc80, // Upper 32 bits of cycle, RV32I only (RO)
353 csr_timeh = 0xc81, // Upper 32 bits of time, RV32I only (RO)
354 csr_instreth = 0xc82 // Upper 32 bits of instret, RV32I only (RO)
355 };
356
357 enum FFlagsMask {
358 kInvalidOperation = 0b10000, // NV: Invalid
359 kDivideByZero = 0b1000, // DZ: Divide by Zero
360 kOverflow = 0b100, // OF: Overflow
361 kUnderflow = 0b10, // UF: Underflow
362 kInexact = 0b1 // NX: Inexact
363 };
364
365 enum FPURoundingMode {
366 RNE = 0b000, // Round to Nearest, ties to Even
367 RTZ = 0b001, // Round towards Zero
368 RDN = 0b010, // Round Down (towards -infinity)
369 RUP = 0b011, // Round Up (towards +infinity)
370 RMM = 0b100, // Round to Nearest, tiest to Max Magnitude
371 DYN = 0b111 // In instruction's rm field, selects dynamic rounding mode;
372 // In Rounding Mode register, Invalid
373 };
374
375 enum MemoryOdering {
376 PSI = 0b1000, // PI or SI
377 PSO = 0b0100, // PO or SO
378 PSR = 0b0010, // PR or SR
379 PSW = 0b0001, // PW or SW
380 PSIORW = PSI | PSO | PSR | PSW
381 };
382
383 const int kFloat32ExponentBias = 127;
384 const int kFloat32MantissaBits = 23;
385 const int kFloat32ExponentBits = 8;
386 const int kFloat64ExponentBias = 1023;
387 const int kFloat64MantissaBits = 52;
388 const int kFloat64ExponentBits = 11;
389
390 enum FClassFlag {
391 kNegativeInfinity = 1,
392 kNegativeNormalNumber = 1 << 1,
393 kNegativeSubnormalNumber = 1 << 2,
394 kNegativeZero = 1 << 3,
395 kPositiveZero = 1 << 4,
396 kPositiveSubnormalNumber = 1 << 5,
397 kPositiveNormalNumber = 1 << 6,
398 kPositiveInfinity = 1 << 7,
399 kSignalingNaN = 1 << 8,
400 kQuietNaN = 1 << 9
401 };
402
403 enum OffsetSize : uint32_t {
404 kOffset21 = 21, // RISCV jal
405 kOffset12 = 12, // RISCV imm12
406 kOffset20 = 20, // RISCV imm20
407 kOffset13 = 13, // RISCV branch
408 kOffset32 = 32, // RISCV auipc + instr_I
409 kOffset11 = 11, // RISCV C_J
410 kOffset9 = 9, // RISCV compressed branch
411 };
412
413 // The classes of immediate branch ranges, in order of increasing range.
414 // Note that CondBranchType and CompareBranchType have the same range.
415 enum ImmBranchRangeType {
416 CondBranchRangeType, //
417 UncondBranchRangeType, //
418 UnknownBranchRangeType,
419
420 // Number of 'short-range' branch range types.
421 // We don't consider unconditional branches 'short-range'.
422 NumShortBranchRangeTypes = UnknownBranchRangeType
423 };
424
425 inline ImmBranchRangeType OffsetSizeToImmBranchRangeType(OffsetSize bits) {
426 switch (bits) {
427 case kOffset21:
428 return UncondBranchRangeType;
429 case kOffset13:
430 return CondBranchRangeType;
431 default:
432 MOZ_CRASH("Unimplement");
433 }
434 }
435
436 inline OffsetSize ImmBranchRangeTypeToOffsetSize(ImmBranchRangeType type) {
437 switch (type) {
438 case CondBranchRangeType:
439 return kOffset13;
440 case UncondBranchRangeType:
441 return kOffset21;
442 default:
443 MOZ_CRASH("Unimplement");
444 }
445 }
446
447 int32_t ImmBranchMaxForwardOffset(OffsetSize bits);
448
449 inline int32_t ImmBranchMaxForwardOffset(ImmBranchRangeType type) {
450 return ImmBranchMaxForwardOffset(ImmBranchRangeTypeToOffsetSize(type));
451 }
452 // -----------------------------------------------------------------------------
453 // Specific instructions, constants, and masks.
454 // These constants are declared in assembler-riscv64.cc, as they use named
455 // registers and other constants.
456
457 // An Illegal instruction
458 const Instr kIllegalInstr = 0; // All other bits are 0s (i.e., ecall)
459 // An ECALL instruction, used for redirected real time call
460 const Instr rtCallRedirInstr = SYSTEM; // All other bits are 0s (i.e., ecall)
461 // An EBreak instruction, used for debugging and semi-hosting
462 const Instr kBreakInstr = SYSTEM | 1 << kImm12Shift; // ebreak
463
464 constexpr uint8_t kInstrSize = 4;
465 constexpr uint8_t kShortInstrSize = 2;
466 constexpr uint8_t kInstrSizeLog2 = 2;
467
468 class InstructionBase {
469 public:
470 enum {
471 // On RISC-V, PC cannot actually be directly accessed. We behave as if PC
472 // was always the value of the current instruction being executed.
473 kPCReadOffset = 0
474 };
475
476 // Instruction type.
477 enum Type {
478 kRType,
479 kR4Type, // Special R4 for Q extension
480 kIType,
481 kSType,
482 kBType,
483 kUType,
484 kJType,
485 // C extension
486 kCRType,
487 kCIType,
488 kCSSType,
489 kCIWType,
490 kCLType,
491 kCSType,
492 kCAType,
493 kCBType,
494 kCJType,
495 // V extension
496 kVType,
497 kVLType,
498 kVSType,
499 kVAMOType,
500 kVIVVType,
501 kVFVVType,
502 kVMVVType,
503 kVIVIType,
504 kVIVXType,
505 kVFVFType,
506 kVMVXType,
507 kVSETType,
508 kUnsupported = -1
509 };
510
511 inline bool IsIllegalInstruction() const {
512 uint16_t FirstHalfWord = *reinterpret_cast<const uint16_t*>(this);
513 return FirstHalfWord == 0;
514 }
515
516 bool IsShortInstruction() const;
517
518 inline uint8_t InstructionSize() const {
519 return (this->IsShortInstruction()) ? kShortInstrSize : kInstrSize;
520 }
521
522 // Get the raw instruction bits.
523 inline Instr InstructionBits() const {
524 if (this->IsShortInstruction()) {
525 return 0x0000FFFF & (*reinterpret_cast<const ShortInstr*>(this));
526 }
527 return *reinterpret_cast<const Instr*>(this);
528 }
529
530 // Set the raw instruction bits to value.
531 inline void SetInstructionBits(Instr value) {
532 *reinterpret_cast<Instr*>(this) = value;
533 }
534
535 // Read one particular bit out of the instruction bits.
536 inline int Bit(int nr) const { return (InstructionBits() >> nr) & 1; }
537
538 // Read a bit field out of the instruction bits.
539 inline int Bits(int hi, int lo) const {
540 return (InstructionBits() >> lo) & ((2U << (hi - lo)) - 1);
541 }
542
543 // Accessors for the different named fields used in the RISC-V encoding.
544 inline BaseOpcode BaseOpcodeValue() const {
545 return static_cast<BaseOpcode>(
546 Bits(kBaseOpcodeShift + kBaseOpcodeBits - 1, kBaseOpcodeShift));
547 }
548
549 // Return the fields at their original place in the instruction encoding.
550 inline BaseOpcode BaseOpcodeFieldRaw() const {
551 return static_cast<BaseOpcode>(InstructionBits() & kBaseOpcodeMask);
552 }
553
554 // Safe to call within R-type instructions
555 inline int Funct7FieldRaw() const { return InstructionBits() & kFunct7Mask; }
556
557 // Safe to call within R-, I-, S-, or B-type instructions
558 inline int Funct3FieldRaw() const { return InstructionBits() & kFunct3Mask; }
559
560 // Safe to call within R-, I-, S-, or B-type instructions
561 inline int Rs1FieldRawNoAssert() const {
562 return InstructionBits() & kRs1FieldMask;
563 }
564
565 // Safe to call within R-, S-, or B-type instructions
566 inline int Rs2FieldRawNoAssert() const {
567 return InstructionBits() & kRs2FieldMask;
568 }
569
570 // Safe to call within R4-type instructions
571 inline int Rs3FieldRawNoAssert() const {
572 return InstructionBits() & kRs3FieldMask;
573 }
574
575 inline int32_t ITypeBits() const { return InstructionBits() & kITypeMask; }
576
577 inline int32_t InstructionOpcodeType() const {
578 if (IsShortInstruction()) {
579 return InstructionBits() & kRvcOpcodeMask;
580 } else {
581 return InstructionBits() & kBaseOpcodeMask;
582 }
583 }
584
585 // Get the encoding type of the instruction.
586 Type InstructionType() const;
587 OffsetSize GetOffsetSize() const;
588 inline ImmBranchRangeType GetImmBranchRangeType() const {
589 return OffsetSizeToImmBranchRangeType(GetOffsetSize());
590 }
591
592 protected:
593 InstructionBase() {}
594 };
595
596 template <class T>
597 class InstructionGetters : public T {
598 public:
599 // Say if the instruction is a break or a trap.
600 bool IsTrap() const;
601
602 inline int BaseOpcode() const {
603 return this->InstructionBits() & kBaseOpcodeMask;
604 }
605
606 inline int RvcOpcode() const {
607 MOZ_ASSERT(this->IsShortInstruction());
608 return this->InstructionBits() & kRvcOpcodeMask;
609 }
610
611 inline int Rs1Value() const {
612 MOZ_ASSERT(this->InstructionType() == InstructionBase::kRType ||
613 this->InstructionType() == InstructionBase::kR4Type ||
614 this->InstructionType() == InstructionBase::kIType ||
615 this->InstructionType() == InstructionBase::kSType ||
616 this->InstructionType() == InstructionBase::kBType ||
617 this->InstructionType() == InstructionBase::kIType ||
618 this->InstructionType() == InstructionBase::kVType);
619 return this->Bits(kRs1Shift + kRs1Bits - 1, kRs1Shift);
620 }
621
622 inline int Rs2Value() const {
623 MOZ_ASSERT(this->InstructionType() == InstructionBase::kRType ||
624 this->InstructionType() == InstructionBase::kR4Type ||
625 this->InstructionType() == InstructionBase::kSType ||
626 this->InstructionType() == InstructionBase::kBType ||
627 this->InstructionType() == InstructionBase::kIType ||
628 this->InstructionType() == InstructionBase::kVType);
629 return this->Bits(kRs2Shift + kRs2Bits - 1, kRs2Shift);
630 }
631
632 inline int Rs3Value() const {
633 MOZ_ASSERT(this->InstructionType() == InstructionBase::kR4Type);
634 return this->Bits(kRs3Shift + kRs3Bits - 1, kRs3Shift);
635 }
636
637 inline int Vs1Value() const {
638 MOZ_ASSERT(this->InstructionType() == InstructionBase::kVType ||
639 this->InstructionType() == InstructionBase::kIType ||
640 this->InstructionType() == InstructionBase::kSType);
641 return this->Bits(kVs1Shift + kVs1Bits - 1, kVs1Shift);
642 }
643
644 inline int Vs2Value() const {
645 MOZ_ASSERT(this->InstructionType() == InstructionBase::kVType ||
646 this->InstructionType() == InstructionBase::kIType ||
647 this->InstructionType() == InstructionBase::kSType);
648 return this->Bits(kVs2Shift + kVs2Bits - 1, kVs2Shift);
649 }
650
651 inline int VdValue() const {
652 MOZ_ASSERT(this->InstructionType() == InstructionBase::kVType ||
653 this->InstructionType() == InstructionBase::kIType ||
654 this->InstructionType() == InstructionBase::kSType);
655 return this->Bits(kVdShift + kVdBits - 1, kVdShift);
656 }
657
658 inline int RdValue() const {
659 MOZ_ASSERT(this->InstructionType() == InstructionBase::kRType ||
660 this->InstructionType() == InstructionBase::kR4Type ||
661 this->InstructionType() == InstructionBase::kIType ||
662 this->InstructionType() == InstructionBase::kSType ||
663 this->InstructionType() == InstructionBase::kUType ||
664 this->InstructionType() == InstructionBase::kJType ||
665 this->InstructionType() == InstructionBase::kVType);
666 return this->Bits(kRdShift + kRdBits - 1, kRdShift);
667 }
668
669 inline int RvcRs1Value() const { return this->RvcRdValue(); }
670
671 int RvcRdValue() const;
672
673 int RvcRs2Value() const;
674
675 int RvcRs1sValue() const;
676
677 int RvcRs2sValue() const;
678
679 int Funct7Value() const;
680
681 inline int Funct3Value() const {
682 MOZ_ASSERT(this->InstructionType() == InstructionBase::kRType ||
683 this->InstructionType() == InstructionBase::kIType ||
684 this->InstructionType() == InstructionBase::kSType ||
685 this->InstructionType() == InstructionBase::kBType);
686 return this->Bits(kFunct3Shift + kFunct3Bits - 1, kFunct3Shift);
687 }
688
689 inline int Funct5Value() const {
690 MOZ_ASSERT(this->InstructionType() == InstructionBase::kRType &&
691 this->BaseOpcode() == OP_FP);
692 return this->Bits(kFunct5Shift + kFunct5Bits - 1, kFunct5Shift);
693 }
694
695 int RvcFunct6Value() const;
696
697 int RvcFunct4Value() const;
698
699 int RvcFunct3Value() const;
700
701 int RvcFunct2Value() const;
702
703 int RvcFunct2BValue() const;
704
705 inline int CsrValue() const {
706 MOZ_ASSERT(this->InstructionType() == InstructionBase::kIType &&
707 this->BaseOpcode() == SYSTEM);
708 return (this->Bits(kCsrShift + kCsrBits - 1, kCsrShift));
709 }
710
711 inline int RoundMode() const {
712 MOZ_ASSERT((this->InstructionType() == InstructionBase::kRType ||
713 this->InstructionType() == InstructionBase::kR4Type) &&
714 this->BaseOpcode() == OP_FP);
715 return this->Bits(kFunct3Shift + kFunct3Bits - 1, kFunct3Shift);
716 }
717
718 inline int MemoryOrder(bool is_pred) const {
719 MOZ_ASSERT((this->InstructionType() == InstructionBase::kIType &&
720 this->BaseOpcode() == MISC_MEM));
721 if (is_pred) {
722 return this->Bits(kPredOrderShift + kMemOrderBits - 1, kPredOrderShift);
723 } else {
724 return this->Bits(kSuccOrderShift + kMemOrderBits - 1, kSuccOrderShift);
725 }
726 }
727
728 inline int Imm12Value() const {
729 MOZ_ASSERT(this->InstructionType() == InstructionBase::kIType);
730 int Value = this->Bits(kImm12Shift + kImm12Bits - 1, kImm12Shift);
731 return Value << 20 >> 20;
732 }
733
734 inline int32_t Imm12SExtValue() const {
735 int32_t Value = this->Imm12Value() << 20 >> 20;
736 return Value;
737 }
738
739 inline int BranchOffset() const {
740 MOZ_ASSERT(this->InstructionType() == InstructionBase::kBType);
741 // | imm[12|10:5] | rs2 | rs1 | funct3 | imm[4:1|11] | opcode |
742 // 31 25 11 7
743 uint32_t Bits = this->InstructionBits();
744 int16_t imm13 = ((Bits & 0xf00) >> 7) | ((Bits & 0x7e000000) >> 20) |
745 ((Bits & 0x80) << 4) | ((Bits & 0x80000000) >> 19);
746 return imm13 << 19 >> 19;
747 }
748
749 inline int StoreOffset() const {
750 MOZ_ASSERT(this->InstructionType() == InstructionBase::kSType);
751 // | imm[11:5] | rs2 | rs1 | funct3 | imm[4:0] | opcode |
752 // 31 25 11 7
753 uint32_t Bits = this->InstructionBits();
754 int16_t imm12 = ((Bits & 0xf80) >> 7) | ((Bits & 0xfe000000) >> 20);
755 return imm12 << 20 >> 20;
756 }
757
758 inline int Imm20UValue() const {
759 MOZ_ASSERT(this->InstructionType() == InstructionBase::kUType);
760 // | imm[31:12] | rd | opcode |
761 // 31 12
762 int32_t Bits = this->InstructionBits();
763 return Bits >> 12;
764 }
765
766 inline int Imm20JValue() const {
767 MOZ_ASSERT(this->InstructionType() == InstructionBase::kJType);
768 // | imm[20|10:1|11|19:12] | rd | opcode |
769 // 31 12
770 uint32_t Bits = this->InstructionBits();
771 int32_t imm20 = ((Bits & 0x7fe00000) >> 20) | ((Bits & 0x100000) >> 9) |
772 (Bits & 0xff000) | ((Bits & 0x80000000) >> 11);
773 return imm20 << 11 >> 11;
774 }
775
776 inline bool IsArithShift() const {
777 // Valid only for right shift operations
778 MOZ_ASSERT((this->BaseOpcode() == OP || this->BaseOpcode() == OP_32 ||
779 this->BaseOpcode() == OP_IMM ||
780 this->BaseOpcode() == OP_IMM_32) &&
781 this->Funct3Value() == 0b101);
782 return this->InstructionBits() & 0x40000000;
783 }
784
785 inline int Shamt() const {
786 // Valid only for shift instructions (SLLI, SRLI, SRAI)
787 MOZ_ASSERT((this->InstructionBits() & kBaseOpcodeMask) == OP_IMM &&
788 (this->Funct3Value() == 0b001 || this->Funct3Value() == 0b101));
789 // | 0A0000 | shamt | rs1 | funct3 | rd | opcode |
790 // 31 25 20
791 return this->Bits(kImm12Shift + 5, kImm12Shift);
792 }
793
794 inline int Shamt32() const {
795 // Valid only for shift instructions (SLLIW, SRLIW, SRAIW)
796 MOZ_ASSERT((this->InstructionBits() & kBaseOpcodeMask) == OP_IMM_32 &&
797 (this->Funct3Value() == 0b001 || this->Funct3Value() == 0b101));
798 // | 0A00000 | shamt | rs1 | funct3 | rd | opcode |
799 // 31 24 20
800 return this->Bits(kImm12Shift + 4, kImm12Shift);
801 }
802
803 inline int RvcImm6Value() const {
804 MOZ_ASSERT(this->IsShortInstruction());
805 // | funct3 | imm[5] | rs1/rd | imm[4:0] | opcode |
806 // 15 12 6 2
807 uint32_t Bits = this->InstructionBits();
808 int32_t imm6 = ((Bits & 0x1000) >> 7) | ((Bits & 0x7c) >> 2);
809 return imm6 << 26 >> 26;
810 }
811
812 inline int RvcImm6Addi16spValue() const {
813 MOZ_ASSERT(this->IsShortInstruction());
814 // | funct3 | nzimm[9] | 2 | nzimm[4|6|8:7|5] | opcode |
815 // 15 12 6 2
816 uint32_t Bits = this->InstructionBits();
817 int32_t imm10 = ((Bits & 0x1000) >> 3) | ((Bits & 0x40) >> 2) |
818 ((Bits & 0x20) << 1) | ((Bits & 0x18) << 4) |
819 ((Bits & 0x4) << 3);
820 MOZ_ASSERT(imm10 != 0);
821 return imm10 << 22 >> 22;
822 }
823
824 inline int RvcImm8Addi4spnValue() const {
825 MOZ_ASSERT(this->IsShortInstruction());
826 // | funct3 | nzimm[11] | rd' | opcode |
827 // 15 13 5 2
828 uint32_t Bits = this->InstructionBits();
829 int32_t uimm10 = ((Bits & 0x20) >> 2) | ((Bits & 0x40) >> 4) |
830 ((Bits & 0x780) >> 1) | ((Bits & 0x1800) >> 7);
831 MOZ_ASSERT(uimm10 != 0);
832 return uimm10;
833 }
834
835 inline int RvcShamt6() const {
836 MOZ_ASSERT(this->IsShortInstruction());
837 // | funct3 | nzuimm[5] | rs1/rd | nzuimm[4:0] | opcode |
838 // 15 12 6 2
839 int32_t imm6 = this->RvcImm6Value();
840 return imm6 & 0x3f;
841 }
842
843 inline int RvcImm6LwspValue() const {
844 MOZ_ASSERT(this->IsShortInstruction());
845 // | funct3 | uimm[5] | rs1 | uimm[4:2|7:6] | opcode |
846 // 15 12 6 2
847 uint32_t Bits = this->InstructionBits();
848 int32_t imm8 =
849 ((Bits & 0x1000) >> 7) | ((Bits & 0x70) >> 2) | ((Bits & 0xc) << 4);
850 return imm8;
851 }
852
853 inline int RvcImm6LdspValue() const {
854 MOZ_ASSERT(this->IsShortInstruction());
855 // | funct3 | uimm[5] | rs1 | uimm[4:3|8:6] | opcode |
856 // 15 12 6 2
857 uint32_t Bits = this->InstructionBits();
858 int32_t imm9 =
859 ((Bits & 0x1000) >> 7) | ((Bits & 0x60) >> 2) | ((Bits & 0x1c) << 4);
860 return imm9;
861 }
862
863 inline int RvcImm6SwspValue() const {
864 MOZ_ASSERT(this->IsShortInstruction());
865 // | funct3 | uimm[5:2|7:6] | rs2 | opcode |
866 // 15 12 7
867 uint32_t Bits = this->InstructionBits();
868 int32_t imm8 = ((Bits & 0x1e00) >> 7) | ((Bits & 0x180) >> 1);
869 return imm8;
870 }
871
872 inline int RvcImm6SdspValue() const {
873 MOZ_ASSERT(this->IsShortInstruction());
874 // | funct3 | uimm[5:3|8:6] | rs2 | opcode |
875 // 15 12 7
876 uint32_t Bits = this->InstructionBits();
877 int32_t imm9 = ((Bits & 0x1c00) >> 7) | ((Bits & 0x380) >> 1);
878 return imm9;
879 }
880
881 inline int RvcImm5WValue() const {
882 MOZ_ASSERT(this->IsShortInstruction());
883 // | funct3 | imm[5:3] | rs1 | imm[2|6] | rd | opcode |
884 // 15 12 10 6 4 2
885 uint32_t Bits = this->InstructionBits();
886 int32_t imm7 =
887 ((Bits & 0x1c00) >> 7) | ((Bits & 0x40) >> 4) | ((Bits & 0x20) << 1);
888 return imm7;
889 }
890
891 inline int RvcImm5DValue() const {
892 MOZ_ASSERT(this->IsShortInstruction());
893 // | funct3 | imm[5:3] | rs1 | imm[7:6] | rd | opcode |
894 // 15 12 10 6 4 2
895 uint32_t Bits = this->InstructionBits();
896 int32_t imm8 = ((Bits & 0x1c00) >> 7) | ((Bits & 0x60) << 1);
897 return imm8;
898 }
899
900 inline int RvcImm11CJValue() const {
901 MOZ_ASSERT(this->IsShortInstruction());
902 // | funct3 | [11|4|9:8|10|6|7|3:1|5] | opcode |
903 // 15 12 2
904 uint32_t Bits = this->InstructionBits();
905 int32_t imm12 = ((Bits & 0x4) << 3) | ((Bits & 0x38) >> 2) |
906 ((Bits & 0x40) << 1) | ((Bits & 0x80) >> 1) |
907 ((Bits & 0x100) << 2) | ((Bits & 0x600) >> 1) |
908 ((Bits & 0x800) >> 7) | ((Bits & 0x1000) >> 1);
909 return imm12 << 20 >> 20;
910 }
911
912 inline int RvcImm8BValue() const {
913 MOZ_ASSERT(this->IsShortInstruction());
914 // | funct3 | imm[8|4:3] | rs1` | imm[7:6|2:1|5] | opcode |
915 // 15 12 10 7 2
916 uint32_t Bits = this->InstructionBits();
917 int32_t imm9 = ((Bits & 0x4) << 3) | ((Bits & 0x18) >> 2) |
918 ((Bits & 0x60) << 1) | ((Bits & 0xc00) >> 7) |
919 ((Bits & 0x1000) >> 4);
920 return imm9 << 23 >> 23;
921 }
922
923 inline int vl_vs_width() {
924 int width = 0;
925 if ((this->InstructionBits() & kBaseOpcodeMask) != LOAD_FP &&
926 (this->InstructionBits() & kBaseOpcodeMask) != STORE_FP)
927 return -1;
928 switch (this->InstructionBits() & (kRvvWidthMask | kRvvMewMask)) {
929 case 0x0:
930 width = 8;
931 break;
932 case 0x00005000:
933 width = 16;
934 break;
935 case 0x00006000:
936 width = 32;
937 break;
938 case 0x00007000:
939 width = 64;
940 break;
941 case 0x10000000:
942 width = 128;
943 break;
944 case 0x10005000:
945 width = 256;
946 break;
947 case 0x10006000:
948 width = 512;
949 break;
950 case 0x10007000:
951 width = 1024;
952 break;
953 default:
954 width = -1;
955 break;
956 }
957 return width;
958 }
959
960 uint32_t Rvvzimm() const;
961
962 uint32_t Rvvuimm() const;
963
964 inline uint32_t RvvVsew() const {
965 uint32_t zimm = this->Rvvzimm();
966 uint32_t vsew = (zimm >> 3) & 0x7;
967 return vsew;
968 }
969
970 inline uint32_t RvvVlmul() const {
971 uint32_t zimm = this->Rvvzimm();
972 uint32_t vlmul = zimm & 0x7;
973 return vlmul;
974 }
975
976 inline uint8_t RvvVM() const {
977 MOZ_ASSERT(this->InstructionType() == InstructionBase::kVType ||
978 this->InstructionType() == InstructionBase::kIType ||
979 this->InstructionType() == InstructionBase::kSType);
980 return this->Bits(kRvvVmShift + kRvvVmBits - 1, kRvvVmShift);
981 }
982
983 inline const char* RvvSEW() const {
984 uint32_t vsew = this->RvvVsew();
985 switch (vsew) {
986 #define CAST_VSEW(name) \
987 case name: \
988 return #name;
989 RVV_SEW(CAST_VSEW)
990 default:
991 return "unknown";
992 #undef CAST_VSEW
993 }
994 }
995
996 inline const char* RvvLMUL() const {
997 uint32_t vlmul = this->RvvVlmul();
998 switch (vlmul) {
999 #define CAST_VLMUL(name) \
1000 case name: \
1001 return #name;
1002 RVV_LMUL(CAST_VLMUL)
1003 default:
1004 return "unknown";
1005 #undef CAST_VLMUL
1006 }
1007 }
1008
1009 #define sext(x, len) (((int32_t)(x) << (32 - len)) >> (32 - len))
1010 #define zext(x, len) (((uint32_t)(x) << (32 - len)) >> (32 - len))
1011
1012 inline int32_t RvvSimm5() const {
1013 MOZ_ASSERT(this->InstructionType() == InstructionBase::kVType);
1014 return sext(this->Bits(kRvvImm5Shift + kRvvImm5Bits - 1, kRvvImm5Shift),
1015 kRvvImm5Bits);
1016 }
1017
1018 inline uint32_t RvvUimm5() const {
1019 MOZ_ASSERT(this->InstructionType() == InstructionBase::kVType);
1020 uint32_t imm = this->Bits(kRvvImm5Shift + kRvvImm5Bits - 1, kRvvImm5Shift);
1021 return zext(imm, kRvvImm5Bits);
1022 }
1023 #undef sext
1024 #undef zext
1025 inline bool AqValue() const { return this->Bits(kAqShift, kAqShift); }
1026
1027 inline bool RlValue() const { return this->Bits(kRlShift, kRlShift); }
1028 };
1029
1030 class Instruction : public InstructionGetters<InstructionBase> {
1031 public:
1032 // Instructions are read of out a code stream. The only way to get a
1033 // reference to an instruction is to convert a pointer. There is no way
1034 // to allocate or create instances of class Instruction.
1035 // Use the At(pc) function to create references to Instruction.
1036 static Instruction* At(byte* pc) {
1037 return reinterpret_cast<Instruction*>(pc);
1038 }
1039
1040 private:
1041 // We need to prevent the creation of instances of class Instruction.
1042 Instruction() = delete;
1043 Instruction(const Instruction&) = delete;
1044 Instruction& operator=(const Instruction&) = delete;
1045 };
1046
1047 // -----------------------------------------------------------------------------
1048 // Instructions.
1049
1050 template <class P>
1051 bool InstructionGetters<P>::IsTrap() const {
1052 return (this->InstructionBits() == kBreakInstr);
1053 }
1054
1055 } // namespace jit
1056 } // namespace js
1057 #endif // jit_riscv64_constant_Base_constant_riscv__h_