Debian Codegraph @

js/src/jit/riscv64/extension/base-riscv-i.cc

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 #include "jit/riscv64/extension/base-riscv-i.h"
5 #include "jit/riscv64/constant/Constant-riscv64.h"
6 #include "jit/riscv64/Assembler-riscv64.h"
7 #include "jit/riscv64/Architecture-riscv64.h"
8 namespace js {
9 namespace jit {
10
11 void AssemblerRISCVI::lui(Register rd, int32_t imm20) {
12 GenInstrU(LUI, rd, imm20);
13 }
14
15 void AssemblerRISCVI::auipc(Register rd, int32_t imm20) {
16 GenInstrU(AUIPC, rd, imm20);
17 }
18
19 // Jumps
20
21 void AssemblerRISCVI::jal(Register rd, int32_t imm21) {
22 GenInstrJ(JAL, rd, imm21);
23 }
24
25 void AssemblerRISCVI::jalr(Register rd, Register rs1, int16_t imm12) {
26 GenInstrI(0b000, JALR, rd, rs1, imm12);
27 }
28
29 // Branches
30
31 void AssemblerRISCVI::beq(Register rs1, Register rs2, int16_t imm13) {
32 GenInstrBranchCC_rri(0b000, rs1, rs2, imm13);
33 }
34
35 void AssemblerRISCVI::bne(Register rs1, Register rs2, int16_t imm13) {
36 GenInstrBranchCC_rri(0b001, rs1, rs2, imm13);
37 }
38
39 void AssemblerRISCVI::blt(Register rs1, Register rs2, int16_t imm13) {
40 GenInstrBranchCC_rri(0b100, rs1, rs2, imm13);
41 }
42
43 void AssemblerRISCVI::bge(Register rs1, Register rs2, int16_t imm13) {
44 GenInstrBranchCC_rri(0b101, rs1, rs2, imm13);
45 }
46
47 void AssemblerRISCVI::bltu(Register rs1, Register rs2, int16_t imm13) {
48 GenInstrBranchCC_rri(0b110, rs1, rs2, imm13);
49 }
50
51 void AssemblerRISCVI::bgeu(Register rs1, Register rs2, int16_t imm13) {
52 GenInstrBranchCC_rri(0b111, rs1, rs2, imm13);
53 }
54
55 // Loads
56
57 void AssemblerRISCVI::lb(Register rd, Register rs1, int16_t imm12) {
58 GenInstrLoad_ri(0b000, rd, rs1, imm12);
59 }
60
61 void AssemblerRISCVI::lh(Register rd, Register rs1, int16_t imm12) {
62 GenInstrLoad_ri(0b001, rd, rs1, imm12);
63 }
64
65 void AssemblerRISCVI::lw(Register rd, Register rs1, int16_t imm12) {
66 GenInstrLoad_ri(0b010, rd, rs1, imm12);
67 }
68
69 void AssemblerRISCVI::lbu(Register rd, Register rs1, int16_t imm12) {
70 GenInstrLoad_ri(0b100, rd, rs1, imm12);
71 }
72
73 void AssemblerRISCVI::lhu(Register rd, Register rs1, int16_t imm12) {
74 GenInstrLoad_ri(0b101, rd, rs1, imm12);
75 }
76
77 // Stores
78
79 void AssemblerRISCVI::sb(Register source, Register base, int16_t imm12) {
80 GenInstrStore_rri(0b000, base, source, imm12);
81 }
82
83 void AssemblerRISCVI::sh(Register source, Register base, int16_t imm12) {
84 GenInstrStore_rri(0b001, base, source, imm12);
85 }
86
87 void AssemblerRISCVI::sw(Register source, Register base, int16_t imm12) {
88 GenInstrStore_rri(0b010, base, source, imm12);
89 }
90
91 // Arithmetic with immediate
92
93 void AssemblerRISCVI::addi(Register rd, Register rs1, int16_t imm12) {
94 GenInstrALU_ri(0b000, rd, rs1, imm12);
95 }
96
97 void AssemblerRISCVI::slti(Register rd, Register rs1, int16_t imm12) {
98 GenInstrALU_ri(0b010, rd, rs1, imm12);
99 }
100
101 void AssemblerRISCVI::sltiu(Register rd, Register rs1, int16_t imm12) {
102 GenInstrALU_ri(0b011, rd, rs1, imm12);
103 }
104
105 void AssemblerRISCVI::xori(Register rd, Register rs1, int16_t imm12) {
106 GenInstrALU_ri(0b100, rd, rs1, imm12);
107 }
108
109 void AssemblerRISCVI::ori(Register rd, Register rs1, int16_t imm12) {
110 GenInstrALU_ri(0b110, rd, rs1, imm12);
111 }
112
113 void AssemblerRISCVI::andi(Register rd, Register rs1, int16_t imm12) {
114 GenInstrALU_ri(0b111, rd, rs1, imm12);
115 }
116
117 void AssemblerRISCVI::slli(Register rd, Register rs1, uint8_t shamt) {
118 GenInstrShift_ri(0, 0b001, rd, rs1, shamt & 0x3f);
119 }
120
121 void AssemblerRISCVI::srli(Register rd, Register rs1, uint8_t shamt) {
122 GenInstrShift_ri(0, 0b101, rd, rs1, shamt & 0x3f);
123 }
124
125 void AssemblerRISCVI::srai(Register rd, Register rs1, uint8_t shamt) {
126 GenInstrShift_ri(1, 0b101, rd, rs1, shamt & 0x3f);
127 }
128
129 // Arithmetic
130
131 void AssemblerRISCVI::add(Register rd, Register rs1, Register rs2) {
132 GenInstrALU_rr(0b0000000, 0b000, rd, rs1, rs2);
133 }
134
135 void AssemblerRISCVI::sub(Register rd, Register rs1, Register rs2) {
136 GenInstrALU_rr(0b0100000, 0b000, rd, rs1, rs2);
137 }
138
139 void AssemblerRISCVI::sll(Register rd, Register rs1, Register rs2) {
140 GenInstrALU_rr(0b0000000, 0b001, rd, rs1, rs2);
141 }
142
143 void AssemblerRISCVI::slt(Register rd, Register rs1, Register rs2) {
144 GenInstrALU_rr(0b0000000, 0b010, rd, rs1, rs2);
145 }
146
147 void AssemblerRISCVI::sltu(Register rd, Register rs1, Register rs2) {
148 GenInstrALU_rr(0b0000000, 0b011, rd, rs1, rs2);
149 }
150
151 void AssemblerRISCVI::xor_(Register rd, Register rs1, Register rs2) {
152 GenInstrALU_rr(0b0000000, 0b100, rd, rs1, rs2);
153 }
154
155 void AssemblerRISCVI::srl(Register rd, Register rs1, Register rs2) {
156 GenInstrALU_rr(0b0000000, 0b101, rd, rs1, rs2);
157 }
158
159 void AssemblerRISCVI::sra(Register rd, Register rs1, Register rs2) {
160 GenInstrALU_rr(0b0100000, 0b101, rd, rs1, rs2);
161 }
162
163 void AssemblerRISCVI::or_(Register rd, Register rs1, Register rs2) {
164 GenInstrALU_rr(0b0000000, 0b110, rd, rs1, rs2);
165 }
166
167 void AssemblerRISCVI::and_(Register rd, Register rs1, Register rs2) {
168 GenInstrALU_rr(0b0000000, 0b111, rd, rs1, rs2);
169 }
170
171 // Memory fences
172
173 void AssemblerRISCVI::fence(uint8_t pred, uint8_t succ) {
174 MOZ_ASSERT(is_uint4(pred) && is_uint4(succ));
175 uint16_t imm12 = succ | (pred << 4) | (0b0000 << 8);
176 GenInstrI(0b000, MISC_MEM, ToRegister(0UL), ToRegister(0UL), imm12);
177 }
178
179 void AssemblerRISCVI::fence_tso() {
180 uint16_t imm12 = (0b0011) | (0b0011 << 4) | (0b1000 << 8);
181 GenInstrI(0b000, MISC_MEM, ToRegister(0UL), ToRegister(0UL), imm12);
182 }
183
184 // Environment call / break
185
186 void AssemblerRISCVI::ecall() {
187 GenInstrI(0b000, SYSTEM, ToRegister(0UL), ToRegister(0UL), 0);
188 }
189
190 void AssemblerRISCVI::ebreak() {
191 GenInstrI(0b000, SYSTEM, ToRegister(0UL), ToRegister(0UL), 1);
192 }
193
194 // This is a de facto standard (as set by GNU binutils) 32-bit unimplemented
195 // instruction (i.e., it should always trap, if your implementation has invalid
196 // instruction traps).
197 void AssemblerRISCVI::unimp() {
198 GenInstrI(0b001, SYSTEM, ToRegister(0), ToRegister(0), 0b110000000000);
199 }
200
201 bool AssemblerRISCVI::IsBranch(Instr instr) {
202 return (instr & kBaseOpcodeMask) == BRANCH;
203 }
204
205 bool AssemblerRISCVI::IsJump(Instr instr) {
206 int Op = instr & kBaseOpcodeMask;
207 return Op == JAL || Op == JALR;
208 }
209
210 bool AssemblerRISCVI::IsNop(Instr instr) { return instr == kNopByte; }
211
212 bool AssemblerRISCVI::IsJal(Instr instr) {
213 return (instr & kBaseOpcodeMask) == JAL;
214 }
215
216 bool AssemblerRISCVI::IsJalr(Instr instr) {
217 return (instr & kBaseOpcodeMask) == JALR;
218 }
219
220 bool AssemblerRISCVI::IsLui(Instr instr) {
221 return (instr & kBaseOpcodeMask) == LUI;
222 }
223 bool AssemblerRISCVI::IsAuipc(Instr instr) {
224 return (instr & kBaseOpcodeMask) == AUIPC;
225 }
226 bool AssemblerRISCVI::IsAddi(Instr instr) {
227 return (instr & (kBaseOpcodeMask | kFunct3Mask)) == RO_ADDI;
228 }
229 bool AssemblerRISCVI::IsOri(Instr instr) {
230 return (instr & (kBaseOpcodeMask | kFunct3Mask)) == RO_ORI;
231 }
232 bool AssemblerRISCVI::IsSlli(Instr instr) {
233 return (instr & (kBaseOpcodeMask | kFunct3Mask)) == RO_SLLI;
234 }
235
236 int AssemblerRISCVI::JumpOffset(Instr instr) {
237 int32_t imm21 = ((instr & 0x7fe00000) >> 20) | ((instr & 0x100000) >> 9) |
238 (instr & 0xff000) | ((instr & 0x80000000) >> 11);
239 imm21 = imm21 << 11 >> 11;
240 return imm21;
241 }
242
243 int AssemblerRISCVI::JalrOffset(Instr instr) {
244 MOZ_ASSERT(IsJalr(instr));
245 int32_t imm12 = static_cast<int32_t>(instr & kImm12Mask) >> 20;
246 return imm12;
247 }
248
249 int AssemblerRISCVI::AuipcOffset(Instr instr) {
250 MOZ_ASSERT(IsAuipc(instr));
251 int32_t imm20 = static_cast<int32_t>(instr & kImm20Mask);
252 return imm20;
253 }
254
255 bool AssemblerRISCVI::IsLw(Instr instr) {
256 return (instr & (kBaseOpcodeMask | kFunct3Mask)) == RO_LW;
257 }
258
259 int AssemblerRISCVI::LoadOffset(Instr instr) {
260 #if JS_CODEGEN_RISCV64
261 MOZ_ASSERT(IsLd(instr));
262 #elif V8_TARGET_ARCH_RISCV32
263 MOZ_ASSERT(IsLw(instr));
264 #endif
265 int32_t imm12 = static_cast<int32_t>(instr & kImm12Mask) >> 20;
266 return imm12;
267 }
268
269 #ifdef JS_CODEGEN_RISCV64
270
271 bool AssemblerRISCVI::IsAddiw(Instr instr) {
272 return (instr & (kBaseOpcodeMask | kFunct3Mask)) == RO_ADDIW;
273 }
274
275 bool AssemblerRISCVI::IsLd(Instr instr) {
276 return (instr & (kBaseOpcodeMask | kFunct3Mask)) == RO_LD;
277 }
278
279 void AssemblerRISCVI::lwu(Register rd, Register rs1, int16_t imm12) {
280 GenInstrLoad_ri(0b110, rd, rs1, imm12);
281 }
282
283 void AssemblerRISCVI::ld(Register rd, Register rs1, int16_t imm12) {
284 GenInstrLoad_ri(0b011, rd, rs1, imm12);
285 }
286
287 void AssemblerRISCVI::sd(Register source, Register base, int16_t imm12) {
288 GenInstrStore_rri(0b011, base, source, imm12);
289 }
290
291 void AssemblerRISCVI::addiw(Register rd, Register rs1, int16_t imm12) {
292 GenInstrI(0b000, OP_IMM_32, rd, rs1, imm12);
293 }
294
295 void AssemblerRISCVI::slliw(Register rd, Register rs1, uint8_t shamt) {
296 GenInstrShiftW_ri(0, 0b001, rd, rs1, shamt & 0x1f);
297 }
298
299 void AssemblerRISCVI::srliw(Register rd, Register rs1, uint8_t shamt) {
300 GenInstrShiftW_ri(0, 0b101, rd, rs1, shamt & 0x1f);
301 }
302
303 void AssemblerRISCVI::sraiw(Register rd, Register rs1, uint8_t shamt) {
304 GenInstrShiftW_ri(1, 0b101, rd, rs1, shamt & 0x1f);
305 }
306
307 void AssemblerRISCVI::addw(Register rd, Register rs1, Register rs2) {
308 GenInstrALUW_rr(0b0000000, 0b000, rd, rs1, rs2);
309 }
310
311 void AssemblerRISCVI::subw(Register rd, Register rs1, Register rs2) {
312 GenInstrALUW_rr(0b0100000, 0b000, rd, rs1, rs2);
313 }
314
315 void AssemblerRISCVI::sllw(Register rd, Register rs1, Register rs2) {
316 GenInstrALUW_rr(0b0000000, 0b001, rd, rs1, rs2);
317 }
318
319 void AssemblerRISCVI::srlw(Register rd, Register rs1, Register rs2) {
320 GenInstrALUW_rr(0b0000000, 0b101, rd, rs1, rs2);
321 }
322
323 void AssemblerRISCVI::sraw(Register rd, Register rs1, Register rs2) {
324 GenInstrALUW_rr(0b0100000, 0b101, rd, rs1, rs2);
325 }
326
327 #endif
328
329 int AssemblerRISCVI::BranchOffset(Instr instr) {
330 // | imm[12] | imm[10:5] | rs2 | rs1 | funct3 | imm[4:1|11] | opcode |
331 // 31 25 11 7
332 int32_t imm13 = ((instr & 0xf00) >> 7) | ((instr & 0x7e000000) >> 20) |
333 ((instr & 0x80) << 4) | ((instr & 0x80000000) >> 19);
334 imm13 = imm13 << 19 >> 19;
335 return imm13;
336 }
337
338 int AssemblerRISCVI::BrachlongOffset(Instr auipc, Instr instr_I) {
339 MOZ_ASSERT(reinterpret_cast<Instruction*>(&instr_I)->InstructionType() ==
340 InstructionBase::kIType);
341 MOZ_ASSERT(IsAuipc(auipc));
342 MOZ_ASSERT(((auipc & kRdFieldMask) >> kRdShift) ==
343 ((instr_I & kRs1FieldMask) >> kRs1Shift));
344 int32_t imm_auipc = AuipcOffset(auipc);
345 int32_t imm12 = static_cast<int32_t>(instr_I & kImm12Mask) >> 20;
346 int32_t offset = imm12 + imm_auipc;
347 return offset;
348 }
349
350 } // namespace jit
351 } // namespace js