Debian Codegraph @

js/src/jit/riscv64/disasm/Disasm-riscv64.cpp

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 */
4 // Copyright 2011 the V8 project authors. All rights reserved.
5 // Use of this source code is governed by a BSD-style license that can be
6 // found in the LICENSE file.
7
8 // A Disassembler object is used to disassemble a block of code instruction by
9 // instruction. The default implementation of the NameConverter object can be
10 // overriden to modify register names or to do symbol lookup on addresses.
11 //
12 // The example below will disassemble a block of code and print it to stdout.
13 //
14 // disasm::NameConverter converter;
15 // disasm::Disassembler d(converter);
16 // for (uint8_t* pc = begin; pc < end;) {
17 // disasm::EmbeddedVector<char, disasm::ReasonableBufferSize> buffer;
18 // uint8_t* prev_pc = pc;
19 // pc += d.InstructionDecode(buffer, pc);
20 // printf("%p %08x %s\n",
21 // prev_pc, *reinterpret_cast<int32_t*>(prev_pc), buffer);
22 // }
23 //
24 // The Disassembler class also has a convenience method to disassemble a block
25 // of code into a FILE*, meaning that the above functionality could also be
26 // achieved by just calling Disassembler::Disassemble(stdout, begin, end);
27
28 #include "jit/riscv64/disasm/Disasm-riscv64.h"
29
30 #include <stdarg.h>
31 #include <stdio.h>
32 #include <string.h>
33
34 #include "jit/riscv64/Assembler-riscv64.h"
35
36 namespace js {
37 namespace jit {
38 namespace disasm {
39
40 #define UNSUPPORTED_RISCV() printf("Unsupported instruction %d.\n", __LINE__)
41 //------------------------------------------------------------------------------
42
43 // Decoder decodes and disassembles instructions into an output buffer.
44 // It uses the converter to convert register names and call destinations into
45 // more informative description.
46 class Decoder {
47 public:
48 Decoder(const disasm::NameConverter& converter, V8Vector<char> out_buffer)
49 : converter_(converter), out_buffer_(out_buffer), out_buffer_pos_(0) {
50 out_buffer_[out_buffer_pos_] = '\0';
51 }
52
53 ~Decoder() {}
54
55 // Writes one disassembled instruction into 'buffer' (0-terminated).
56 // Returns the length of the disassembled machine instruction in bytes.
57 int InstructionDecode(uint8_t* instruction);
58
59 static bool IsConstantPoolAt(uint8_t* instr_ptr);
60 static int ConstantPoolSizeAt(uint8_t* instr_ptr);
61
62 private:
63 // Bottleneck functions to print into the out_buffer.
64 void PrintChar(const char ch);
65 void Print(const char* str);
66
67 // Printing of common values.
68 void PrintRegister(int reg);
69 void PrintFPURegister(int freg);
70 void PrintVRegister(int reg);
71 void PrintFPUStatusRegister(int freg);
72 void PrintRs1(Instruction* instr);
73 void PrintRs2(Instruction* instr);
74 void PrintRd(Instruction* instr);
75 void PrintUimm(Instruction* instr);
76 void PrintVs1(Instruction* instr);
77 void PrintVs2(Instruction* instr);
78 void PrintVd(Instruction* instr);
79 void PrintFRs1(Instruction* instr);
80 void PrintFRs2(Instruction* instr);
81 void PrintFRs3(Instruction* instr);
82 void PrintFRd(Instruction* instr);
83 void PrintImm12(Instruction* instr);
84 void PrintImm12X(Instruction* instr);
85 void PrintImm20U(Instruction* instr);
86 void PrintImm20J(Instruction* instr);
87 void PrintShamt(Instruction* instr);
88 void PrintShamt32(Instruction* instr);
89 void PrintRvcImm6(Instruction* instr);
90 void PrintRvcImm6U(Instruction* instr);
91 void PrintRvcImm6Addi16sp(Instruction* instr);
92 void PrintRvcShamt(Instruction* instr);
93 void PrintRvcImm6Ldsp(Instruction* instr);
94 void PrintRvcImm6Lwsp(Instruction* instr);
95 void PrintRvcImm6Sdsp(Instruction* instr);
96 void PrintRvcImm6Swsp(Instruction* instr);
97 void PrintRvcImm5W(Instruction* instr);
98 void PrintRvcImm5D(Instruction* instr);
99 void PrintRvcImm8Addi4spn(Instruction* instr);
100 void PrintRvcImm11CJ(Instruction* instr);
101 void PrintRvcImm8B(Instruction* instr);
102 void PrintRvvVm(Instruction* instr);
103 void PrintAcquireRelease(Instruction* instr);
104 void PrintBranchOffset(Instruction* instr);
105 void PrintStoreOffset(Instruction* instr);
106 void PrintCSRReg(Instruction* instr);
107 void PrintRvvSEW(Instruction* instr);
108 void PrintRvvLMUL(Instruction* instr);
109 void PrintRvvSimm5(Instruction* instr);
110 void PrintRvvUimm5(Instruction* instr);
111 void PrintRoundingMode(Instruction* instr);
112 void PrintMemoryOrder(Instruction* instr, bool is_pred);
113
114 // Each of these functions decodes one particular instruction type.
115 void DecodeRType(Instruction* instr);
116 void DecodeR4Type(Instruction* instr);
117 void DecodeRAType(Instruction* instr);
118 void DecodeRFPType(Instruction* instr);
119 void DecodeIType(Instruction* instr);
120 void DecodeSType(Instruction* instr);
121 void DecodeBType(Instruction* instr);
122 void DecodeUType(Instruction* instr);
123 void DecodeJType(Instruction* instr);
124 void DecodeCRType(Instruction* instr);
125 void DecodeCAType(Instruction* instr);
126 void DecodeCIType(Instruction* instr);
127 void DecodeCIWType(Instruction* instr);
128 void DecodeCSSType(Instruction* instr);
129 void DecodeCLType(Instruction* instr);
130 void DecodeCSType(Instruction* instr);
131 void DecodeCJType(Instruction* instr);
132 void DecodeCBType(Instruction* instr);
133
134 // Printing of instruction name.
135 void PrintInstructionName(Instruction* instr);
136 void PrintTarget(Instruction* instr);
137
138 // Handle formatting of instructions and their options.
139 int FormatRegister(Instruction* instr, const char* option);
140 int FormatFPURegisterOrRoundMode(Instruction* instr, const char* option);
141 int FormatRvcRegister(Instruction* instr, const char* option);
142 int FormatRvcImm(Instruction* instr, const char* option);
143 int FormatOption(Instruction* instr, const char* option);
144 void Format(Instruction* instr, const char* format);
145 void Unknown(Instruction* instr);
146
147 int switch_sew(Instruction* instr);
148 int switch_nf(Instruction* instr);
149
150 const disasm::NameConverter& converter_;
151 V8Vector<char> out_buffer_;
152 int out_buffer_pos_;
153
154 // Disallow copy and assign.
155 Decoder(const Decoder&) = delete;
156 void operator=(const Decoder&) = delete;
157 };
158
159 // Support for assertions in the Decoder formatting functions.
160 #define STRING_STARTS_WITH(string, compare_string) \
161 (strncmp(string, compare_string, strlen(compare_string)) == 0)
162
163 // Append the ch to the output buffer.
164 void Decoder::PrintChar(const char ch) { out_buffer_[out_buffer_pos_++] = ch; }
165
166 // Append the str to the output buffer.
167 void Decoder::Print(const char* str) {
168 char cur = *str++;
169 while (cur != '\0' && (out_buffer_pos_ < int(out_buffer_.length() - 1))) {
170 PrintChar(cur);
171 cur = *str++;
172 }
173 out_buffer_[out_buffer_pos_] = 0;
174 }
175
176 int Decoder::switch_nf(Instruction* instr) {
177 int nf = 0;
178 switch (instr->InstructionBits() & kRvvNfMask) {
179 case 0x20000000:
180 nf = 2;
181 break;
182 case 0x40000000:
183 nf = 3;
184 break;
185 case 0x60000000:
186 nf = 4;
187 break;
188 case 0x80000000:
189 nf = 5;
190 break;
191 case 0xa0000000:
192 nf = 6;
193 break;
194 case 0xc0000000:
195 nf = 7;
196 break;
197 case 0xe0000000:
198 nf = 8;
199 break;
200 }
201 return nf;
202 }
203
204 int Decoder::switch_sew(Instruction* instr) {
205 int width = 0;
206 if ((instr->InstructionBits() & kBaseOpcodeMask) != LOAD_FP &&
207 (instr->InstructionBits() & kBaseOpcodeMask) != STORE_FP)
208 return -1;
209 switch (instr->InstructionBits() & (kRvvWidthMask | kRvvMewMask)) {
210 case 0x0:
211 width = 8;
212 break;
213 case 0x00005000:
214 width = 16;
215 break;
216 case 0x00006000:
217 width = 32;
218 break;
219 case 0x00007000:
220 width = 64;
221 break;
222 case 0x10000000:
223 width = 128;
224 break;
225 case 0x10005000:
226 width = 256;
227 break;
228 case 0x10006000:
229 width = 512;
230 break;
231 case 0x10007000:
232 width = 1024;
233 break;
234 default:
235 width = -1;
236 break;
237 }
238 return width;
239 }
240
241 // Handle all register based formatting in this function to reduce the
242 // complexity of FormatOption.
243 int Decoder::FormatRegister(Instruction* instr, const char* format) {
244 MOZ_ASSERT(format[0] == 'r');
245 if (format[1] == 's') { // 'rs[12]: Rs register.
246 if (format[2] == '1') {
247 int reg = instr->Rs1Value();
248 PrintRegister(reg);
249 return 3;
250 } else if (format[2] == '2') {
251 int reg = instr->Rs2Value();
252 PrintRegister(reg);
253 return 3;
254 }
255 MOZ_CRASH();
256 } else if (format[1] == 'd') { // 'rd: rd register.
257 int reg = instr->RdValue();
258 PrintRegister(reg);
259 return 2;
260 }
261 MOZ_CRASH();
262 }
263
264 // Handle all FPUregister based formatting in this function to reduce the
265 // complexity of FormatOption.
266 int Decoder::FormatFPURegisterOrRoundMode(Instruction* instr,
267 const char* format) {
268 MOZ_ASSERT(format[0] == 'f');
269 if (format[1] == 's') { // 'fs[1-3]: Rs register.
270 if (format[2] == '1') {
271 int reg = instr->Rs1Value();
272 PrintFPURegister(reg);
273 return 3;
274 } else if (format[2] == '2') {
275 int reg = instr->Rs2Value();
276 PrintFPURegister(reg);
277 return 3;
278 } else if (format[2] == '3') {
279 int reg = instr->Rs3Value();
280 PrintFPURegister(reg);
281 return 3;
282 }
283 MOZ_CRASH();
284 } else if (format[1] == 'd') { // 'fd: fd register.
285 int reg = instr->RdValue();
286 PrintFPURegister(reg);
287 return 2;
288 } else if (format[1] == 'r') { // 'frm
289 MOZ_ASSERT(STRING_STARTS_WITH(format, "frm"));
290 PrintRoundingMode(instr);
291 return 3;
292 }
293 MOZ_CRASH();
294 }
295
296 // Handle all C extension register based formatting in this function to reduce
297 // the complexity of FormatOption.
298 int Decoder::FormatRvcRegister(Instruction* instr, const char* format) {
299 MOZ_ASSERT(format[0] == 'C');
300 MOZ_ASSERT(format[1] == 'r' || format[1] == 'f');
301 if (format[2] == 's') { // 'Crs[12]: Rs register.
302 if (format[3] == '1') {
303 if (format[4] == 's') { // 'Crs1s: 3-bits register
304 int reg = instr->RvcRs1sValue();
305 if (format[1] == 'r') {
306 PrintRegister(reg);
307 } else if (format[1] == 'f') {
308 PrintFPURegister(reg);
309 }
310 return 5;
311 }
312 int reg = instr->RvcRs1Value();
313 if (format[1] == 'r') {
314 PrintRegister(reg);
315 } else if (format[1] == 'f') {
316 PrintFPURegister(reg);
317 }
318 return 4;
319 } else if (format[3] == '2') {
320 if (format[4] == 's') { // 'Crs2s: 3-bits register
321 int reg = instr->RvcRs2sValue();
322 if (format[1] == 'r') {
323 PrintRegister(reg);
324 } else if (format[1] == 'f') {
325 PrintFPURegister(reg);
326 }
327 return 5;
328 }
329 int reg = instr->RvcRs2Value();
330 if (format[1] == 'r') {
331 PrintRegister(reg);
332 } else if (format[1] == 'f') {
333 PrintFPURegister(reg);
334 }
335 return 4;
336 }
337 MOZ_CRASH();
338 } else if (format[2] == 'd') { // 'Crd: rd register.
339 int reg = instr->RvcRdValue();
340 if (format[1] == 'r') {
341 PrintRegister(reg);
342 } else if (format[1] == 'f') {
343 PrintFPURegister(reg);
344 }
345 return 3;
346 }
347 MOZ_CRASH();
348 }
349
350 // Handle all C extension immediates based formatting in this function to reduce
351 // the complexity of FormatOption.
352 int Decoder::FormatRvcImm(Instruction* instr, const char* format) {
353 // TODO(riscv): add other rvc imm format
354 MOZ_ASSERT(STRING_STARTS_WITH(format, "Cimm"));
355 if (format[4] == '6') {
356 if (format[5] == 'U') {
357 MOZ_ASSERT(STRING_STARTS_WITH(format, "Cimm6U"));
358 PrintRvcImm6U(instr);
359 return 6;
360 } else if (format[5] == 'A') {
361 if (format[9] == '1' && format[10] == '6') {
362 MOZ_ASSERT(STRING_STARTS_WITH(format, "Cimm6Addi16sp"));
363 PrintRvcImm6Addi16sp(instr);
364 return 13;
365 }
366 MOZ_CRASH();
367 } else if (format[5] == 'L') {
368 if (format[6] == 'd') {
369 if (format[7] == 's') {
370 MOZ_ASSERT(STRING_STARTS_WITH(format, "Cimm6Ldsp"));
371 PrintRvcImm6Ldsp(instr);
372 return 9;
373 }
374 } else if (format[6] == 'w') {
375 if (format[7] == 's') {
376 MOZ_ASSERT(STRING_STARTS_WITH(format, "Cimm6Lwsp"));
377 PrintRvcImm6Lwsp(instr);
378 return 9;
379 }
380 }
381 MOZ_CRASH();
382 } else if (format[5] == 'S') {
383 if (format[6] == 'w') {
384 MOZ_ASSERT(STRING_STARTS_WITH(format, "Cimm6Swsp"));
385 PrintRvcImm6Swsp(instr);
386 return 9;
387 } else if (format[6] == 'd') {
388 MOZ_ASSERT(STRING_STARTS_WITH(format, "Cimm6Sdsp"));
389 PrintRvcImm6Sdsp(instr);
390 return 9;
391 }
392 MOZ_CRASH();
393 }
394 PrintRvcImm6(instr);
395 return 5;
396 } else if (format[4] == '5') {
397 MOZ_ASSERT(STRING_STARTS_WITH(format, "Cimm5"));
398 if (format[5] == 'W') {
399 MOZ_ASSERT(STRING_STARTS_WITH(format, "Cimm5W"));
400 PrintRvcImm5W(instr);
401 return 6;
402 } else if (format[5] == 'D') {
403 MOZ_ASSERT(STRING_STARTS_WITH(format, "Cimm5D"));
404 PrintRvcImm5D(instr);
405 return 6;
406 }
407 MOZ_CRASH();
408 } else if (format[4] == '8') {
409 MOZ_ASSERT(STRING_STARTS_WITH(format, "Cimm8"));
410 if (format[5] == 'A') {
411 MOZ_ASSERT(STRING_STARTS_WITH(format, "Cimm8Addi4spn"));
412 PrintRvcImm8Addi4spn(instr);
413 return 13;
414 } else if (format[5] == 'B') {
415 MOZ_ASSERT(STRING_STARTS_WITH(format, "Cimm8B"));
416 PrintRvcImm8B(instr);
417 return 6;
418 }
419 MOZ_CRASH();
420 } else if (format[4] == '1') {
421 MOZ_ASSERT(STRING_STARTS_WITH(format, "Cimm1"));
422 if (format[5] == '1') {
423 MOZ_ASSERT(STRING_STARTS_WITH(format, "Cimm11CJ"));
424 PrintRvcImm11CJ(instr);
425 return 8;
426 }
427 MOZ_CRASH();
428 }
429 MOZ_CRASH();
430 }
431
432 // FormatOption takes a formatting string and interprets it based on
433 // the current instructions. The format string points to the first
434 // character of the option string (the option escape has already been
435 // consumed by the caller.) FormatOption returns the number of
436 // characters that were consumed from the formatting string.
437 int Decoder::FormatOption(Instruction* instr, const char* format) {
438 switch (format[0]) {
439 case 'C': { // `C extension
440 if (format[1] == 'r' || format[1] == 'f') {
441 return FormatRvcRegister(instr, format);
442 } else if (format[1] == 'i') {
443 return FormatRvcImm(instr, format);
444 } else if (format[1] == 's') {
445 MOZ_ASSERT(STRING_STARTS_WITH(format, "Cshamt"));
446 PrintRvcShamt(instr);
447 return 6;
448 }
449 MOZ_CRASH();
450 }
451 case 'c': { // `csr: CSR registers
452 if (format[1] == 's') {
453 if (format[2] == 'r') {
454 PrintCSRReg(instr);
455 return 3;
456 }
457 }
458 MOZ_CRASH();
459 }
460 case 'i': { // 'imm12, 'imm12x, 'imm20U, or 'imm20J: Immediates.
461 if (format[3] == '1') {
462 if (format[4] == '2') {
463 MOZ_ASSERT(STRING_STARTS_WITH(format, "imm12"));
464 if (format[5] == 'x') {
465 PrintImm12X(instr);
466 return 6;
467 }
468 PrintImm12(instr);
469 return 5;
470 }
471 } else if (format[3] == '2' && format[4] == '0') {
472 MOZ_ASSERT(STRING_STARTS_WITH(format, "imm20"));
473 switch (format[5]) {
474 case 'U':
475 MOZ_ASSERT(STRING_STARTS_WITH(format, "imm20U"));
476 PrintImm20U(instr);
477 break;
478 case 'J':
479 MOZ_ASSERT(STRING_STARTS_WITH(format, "imm20J"));
480 PrintImm20J(instr);
481 break;
482 }
483 return 6;
484 }
485 MOZ_CRASH();
486 }
487 case 'o': { // 'offB or 'offS: Offsets.
488 if (format[3] == 'B') {
489 MOZ_ASSERT(STRING_STARTS_WITH(format, "offB"));
490 PrintBranchOffset(instr);
491 return 4;
492 } else if (format[3] == 'S') {
493 MOZ_ASSERT(STRING_STARTS_WITH(format, "offS"));
494 PrintStoreOffset(instr);
495 return 4;
496 }
497 MOZ_CRASH();
498 }
499 case 'r': { // 'r: registers.
500 return FormatRegister(instr, format);
501 }
502 case 'f': { // 'f: FPUregisters or `frm
503 return FormatFPURegisterOrRoundMode(instr, format);
504 }
505 case 'a': { // 'a: Atomic acquire and release.
506 PrintAcquireRelease(instr);
507 return 1;
508 }
509 case 'p': { // `pre
510 MOZ_ASSERT(STRING_STARTS_WITH(format, "pre"));
511 PrintMemoryOrder(instr, true);
512 return 3;
513 }
514 case 's': { // 's32 or 's64: Shift amount.
515 if (format[1] == '3') {
516 MOZ_ASSERT(STRING_STARTS_WITH(format, "s32"));
517 PrintShamt32(instr);
518 return 3;
519 } else if (format[1] == '6') {
520 MOZ_ASSERT(STRING_STARTS_WITH(format, "s64"));
521 PrintShamt(instr);
522 return 3;
523 } else if (format[1] == 'u') {
524 MOZ_ASSERT(STRING_STARTS_WITH(format, "suc"));
525 PrintMemoryOrder(instr, false);
526 return 3;
527 } else if (format[1] == 'e') {
528 MOZ_ASSERT(STRING_STARTS_WITH(format, "sew"));
529 PrintRvvSEW(instr);
530 return 3;
531 } else if (format[1] == 'i') {
532 MOZ_ASSERT(STRING_STARTS_WITH(format, "simm5"));
533 PrintRvvSimm5(instr);
534 return 5;
535 }
536 MOZ_CRASH();
537 }
538 case 'v': {
539 if (format[1] == 'd') {
540 MOZ_ASSERT(STRING_STARTS_WITH(format, "vd"));
541 PrintVd(instr);
542 return 2;
543 } else if (format[2] == '1') {
544 MOZ_ASSERT(STRING_STARTS_WITH(format, "vs1"));
545 PrintVs1(instr);
546 return 3;
547 } else if (format[2] == '2') {
548 MOZ_ASSERT(STRING_STARTS_WITH(format, "vs2"));
549 PrintVs2(instr);
550 return 3;
551 } else {
552 MOZ_ASSERT(STRING_STARTS_WITH(format, "vm"));
553 PrintRvvVm(instr);
554 return 2;
555 }
556 }
557 case 'l': {
558 MOZ_ASSERT(STRING_STARTS_WITH(format, "lmul"));
559 PrintRvvLMUL(instr);
560 return 4;
561 }
562 case 'u': {
563 if (STRING_STARTS_WITH(format, "uimm5")) {
564 PrintRvvUimm5(instr);
565 return 5;
566 } else {
567 MOZ_ASSERT(STRING_STARTS_WITH(format, "uimm"));
568 PrintUimm(instr);
569 return 4;
570 }
571 }
572 case 't': { // 'target: target of branch instructions'
573 MOZ_ASSERT(STRING_STARTS_WITH(format, "target"));
574 PrintTarget(instr);
575 return 6;
576 }
577 }
578 MOZ_CRASH();
579 }
580
581 // Format takes a formatting string for a whole instruction and prints it into
582 // the output buffer. All escaped options are handed to FormatOption to be
583 // parsed further.
584 void Decoder::Format(Instruction* instr, const char* format) {
585 char cur = *format++;
586 while ((cur != 0) && (out_buffer_pos_ < (out_buffer_.length() - 1))) {
587 if (cur == '\'') { // Single quote is used as the formatting escape.
588 format += FormatOption(instr, format);
589 } else {
590 out_buffer_[out_buffer_pos_++] = cur;
591 }
592 cur = *format++;
593 }
594 out_buffer_[out_buffer_pos_] = '\0';
595 }
596
597 // The disassembler may end up decoding data inlined in the code. We do not want
598 // it to crash if the data does not ressemble any known instruction.
599 #define VERIFY(condition) \
600 if (!(condition)) { \
601 Unknown(instr); \
602 return; \
603 }
604
605 // For currently unimplemented decodings the disassembler calls Unknown(instr)
606 // which will just print "unknown" of the instruction bits.
607 void Decoder::Unknown(Instruction* instr) { Format(instr, "unknown"); }
608
609 // Print the register name according to the active name converter.
610 void Decoder::PrintRegister(int reg) {
611 Print(converter_.NameOfCPURegister(reg));
612 }
613
614 void Decoder::PrintVRegister(int reg) { UNSUPPORTED_RISCV(); }
615
616 void Decoder::PrintRs1(Instruction* instr) {
617 int reg = instr->Rs1Value();
618 PrintRegister(reg);
619 }
620
621 void Decoder::PrintRs2(Instruction* instr) {
622 int reg = instr->Rs2Value();
623 PrintRegister(reg);
624 }
625
626 void Decoder::PrintRd(Instruction* instr) {
627 int reg = instr->RdValue();
628 PrintRegister(reg);
629 }
630
631 void Decoder::PrintUimm(Instruction* instr) {
632 int val = instr->Rs1Value();
633 out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_, "0x%x", val);
634 }
635
636 void Decoder::PrintVs1(Instruction* instr) {
637 int reg = instr->Vs1Value();
638 PrintVRegister(reg);
639 }
640
641 void Decoder::PrintVs2(Instruction* instr) {
642 int reg = instr->Vs2Value();
643 PrintVRegister(reg);
644 }
645
646 void Decoder::PrintVd(Instruction* instr) {
647 int reg = instr->VdValue();
648 PrintVRegister(reg);
649 }
650
651 // Print the FPUregister name according to the active name converter.
652 void Decoder::PrintFPURegister(int freg) {
653 Print(converter_.NameOfXMMRegister(freg));
654 }
655
656 void Decoder::PrintFRs1(Instruction* instr) {
657 int reg = instr->Rs1Value();
658 PrintFPURegister(reg);
659 }
660
661 void Decoder::PrintFRs2(Instruction* instr) {
662 int reg = instr->Rs2Value();
663 PrintFPURegister(reg);
664 }
665
666 void Decoder::PrintFRs3(Instruction* instr) {
667 int reg = instr->Rs3Value();
668 PrintFPURegister(reg);
669 }
670
671 void Decoder::PrintFRd(Instruction* instr) {
672 int reg = instr->RdValue();
673 PrintFPURegister(reg);
674 }
675
676 void Decoder::PrintImm12X(Instruction* instr) {
677 int32_t imm = instr->Imm12Value();
678 out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_, "0x%x", imm);
679 }
680
681 void Decoder::PrintImm12(Instruction* instr) {
682 int32_t imm = instr->Imm12Value();
683 out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_, "%d", imm);
684 }
685
686 void Decoder::PrintTarget(Instruction* instr) {
687 // if (Assembler::IsJalr(instr->InstructionBits())) {
688 // if (Assembler::IsAuipc((instr - 4)->InstructionBits()) &&
689 // (instr - 4)->RdValue() == instr->Rs1Value()) {
690 // int32_t imm = Assembler::BrachlongOffset((instr -
691 // 4)->InstructionBits(),
692 // instr->InstructionBits());
693 // const char* target =
694 // converter_.NameOfAddress(reinterpret_cast<byte*>(instr - 4) + imm);
695 // out_buffer_pos_ +=
696 // SNPrintF(out_buffer_ + out_buffer_pos_, " -> %s", target);
697 // return;
698 // }
699 // }
700 }
701
702 void Decoder::PrintBranchOffset(Instruction* instr) {
703 int32_t imm = instr->BranchOffset();
704 const char* target =
705 converter_.NameOfAddress(reinterpret_cast<byte*>(instr) + imm);
706 out_buffer_pos_ +=
707 SNPrintF(out_buffer_ + out_buffer_pos_, "%d -> %s", imm, target);
708 }
709
710 void Decoder::PrintStoreOffset(Instruction* instr) {
711 int32_t imm = instr->StoreOffset();
712 out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_, "%d", imm);
713 }
714
715 void Decoder::PrintRvvSEW(Instruction* instr) {
716 const char* sew = instr->RvvSEW();
717 out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_, "%s", sew);
718 }
719
720 void Decoder::PrintRvvLMUL(Instruction* instr) {
721 const char* lmul = instr->RvvLMUL();
722 out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_, "%s", lmul);
723 }
724
725 void Decoder::PrintRvvSimm5(Instruction* instr) {
726 const int simm5 = instr->RvvSimm5();
727 out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_, "%d", simm5);
728 }
729
730 void Decoder::PrintRvvUimm5(Instruction* instr) {
731 const uint32_t uimm5 = instr->RvvUimm5();
732 out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_, "%u", uimm5);
733 }
734
735 void Decoder::PrintImm20U(Instruction* instr) {
736 int32_t imm = instr->Imm20UValue();
737 out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_, "0x%x", imm);
738 }
739
740 void Decoder::PrintImm20J(Instruction* instr) {
741 int32_t imm = instr->Imm20JValue();
742 const char* target =
743 converter_.NameOfAddress(reinterpret_cast<byte*>(instr) + imm);
744 out_buffer_pos_ +=
745 SNPrintF(out_buffer_ + out_buffer_pos_, "%d -> %s", imm, target);
746 }
747
748 void Decoder::PrintShamt(Instruction* instr) {
749 int32_t imm = instr->Shamt();
750 out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_, "%d", imm);
751 }
752
753 void Decoder::PrintShamt32(Instruction* instr) {
754 int32_t imm = instr->Shamt32();
755 out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_, "%d", imm);
756 }
757
758 void Decoder::PrintRvcImm6(Instruction* instr) {
759 int32_t imm = instr->RvcImm6Value();
760 out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_, "%d", imm);
761 }
762
763 void Decoder::PrintRvcImm6U(Instruction* instr) {
764 int32_t imm = instr->RvcImm6Value() & 0xFFFFF;
765 out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_, "0x%x", imm);
766 }
767
768 void Decoder::PrintRvcImm6Addi16sp(Instruction* instr) {
769 int32_t imm = instr->RvcImm6Addi16spValue();
770 out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_, "%d", imm);
771 }
772
773 void Decoder::PrintRvcShamt(Instruction* instr) {
774 int32_t imm = instr->RvcShamt6();
775 out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_, "%d", imm);
776 }
777
778 void Decoder::PrintRvcImm6Ldsp(Instruction* instr) {
779 int32_t imm = instr->RvcImm6LdspValue();
780 out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_, "%d", imm);
781 }
782
783 void Decoder::PrintRvcImm6Lwsp(Instruction* instr) {
784 int32_t imm = instr->RvcImm6LwspValue();
785 out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_, "%d", imm);
786 }
787
788 void Decoder::PrintRvcImm6Swsp(Instruction* instr) {
789 int32_t imm = instr->RvcImm6SwspValue();
790 out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_, "%d", imm);
791 }
792
793 void Decoder::PrintRvcImm6Sdsp(Instruction* instr) {
794 int32_t imm = instr->RvcImm6SdspValue();
795 out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_, "%d", imm);
796 }
797
798 void Decoder::PrintRvcImm5W(Instruction* instr) {
799 int32_t imm = instr->RvcImm5WValue();
800 out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_, "%d", imm);
801 }
802
803 void Decoder::PrintRvcImm5D(Instruction* instr) {
804 int32_t imm = instr->RvcImm5DValue();
805 out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_, "%d", imm);
806 }
807
808 void Decoder::PrintRvcImm8Addi4spn(Instruction* instr) {
809 int32_t imm = instr->RvcImm8Addi4spnValue();
810 out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_, "%d", imm);
811 }
812
813 void Decoder::PrintRvcImm11CJ(Instruction* instr) {
814 int32_t imm = instr->RvcImm11CJValue();
815 out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_, "%d", imm);
816 }
817
818 void Decoder::PrintRvcImm8B(Instruction* instr) {
819 int32_t imm = instr->RvcImm8BValue();
820 out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_, "%d", imm);
821 }
822
823 void Decoder::PrintRvvVm(Instruction* instr) {
824 uint8_t imm = instr->RvvVM();
825 if (imm == 0) {
826 out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_, " v0.t");
827 }
828 }
829
830 void Decoder::PrintAcquireRelease(Instruction* instr) {
831 bool aq = instr->AqValue();
832 bool rl = instr->RlValue();
833 if (aq || rl) {
834 out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_, ".");
835 }
836 if (aq) {
837 out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_, "aq");
838 }
839 if (rl) {
840 out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_, "rl");
841 }
842 }
843
844 void Decoder::PrintCSRReg(Instruction* instr) {
845 int32_t csr_reg = instr->CsrValue();
846 std::string s;
847 switch (csr_reg) {
848 case csr_fflags: // Floating-Point Accrued Exceptions (RW)
849 s = "csr_fflags";
850 break;
851 case csr_frm: // Floating-Point Dynamic Rounding Mode (RW)
852 s = "csr_frm";
853 break;
854 case csr_fcsr: // Floating-Point Control and Status Register (RW)
855 s = "csr_fcsr";
856 break;
857 case csr_cycle:
858 s = "csr_cycle";
859 break;
860 case csr_time:
861 s = "csr_time";
862 break;
863 case csr_instret:
864 s = "csr_instret";
865 break;
866 case csr_cycleh:
867 s = "csr_cycleh";
868 break;
869 case csr_timeh:
870 s = "csr_timeh";
871 break;
872 case csr_instreth:
873 s = "csr_instreth";
874 break;
875 default:
876 MOZ_CRASH();
877 }
878 out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_, "%s", s.c_str());
879 }
880
881 void Decoder::PrintRoundingMode(Instruction* instr) {
882 int frm = instr->RoundMode();
883 std::string s;
884 switch (frm) {
885 case RNE:
886 s = "RNE";
887 break;
888 case RTZ:
889 s = "RTZ";
890 break;
891 case RDN:
892 s = "RDN";
893 break;
894 case RUP:
895 s = "RUP";
896 break;
897 case RMM:
898 s = "RMM";
899 break;
900 case DYN:
901 s = "DYN";
902 break;
903 default:
904 MOZ_CRASH();
905 }
906 out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_, "%s", s.c_str());
907 }
908
909 void Decoder::PrintMemoryOrder(Instruction* instr, bool is_pred) {
910 int memOrder = instr->MemoryOrder(is_pred);
911 std::string s;
912 if ((memOrder & PSI) == PSI) {
913 s += "i";
914 }
915 if ((memOrder & PSO) == PSO) {
916 s += "o";
917 }
918 if ((memOrder & PSR) == PSR) {
919 s += "r";
920 }
921 if ((memOrder & PSW) == PSW) {
922 s += "w";
923 }
924 out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_, "%s", s.c_str());
925 }
926
927 // Printing of instruction name.
928 void Decoder::PrintInstructionName(Instruction* instr) {}
929
930 // RISCV Instruction Decode Routine
931 void Decoder::DecodeRType(Instruction* instr) {
932 switch (instr->InstructionBits() & kRTypeMask) {
933 case RO_ADD:
934 Format(instr, "add 'rd, 'rs1, 'rs2");
935 break;
936 case RO_SUB:
937 if (instr->Rs1Value() == zero.code())
938 Format(instr, "neg 'rd, 'rs2");
939 else
940 Format(instr, "sub 'rd, 'rs1, 'rs2");
941 break;
942 case RO_SLL:
943 Format(instr, "sll 'rd, 'rs1, 'rs2");
944 break;
945 case RO_SLT:
946 if (instr->Rs2Value() == zero.code())
947 Format(instr, "sltz 'rd, 'rs1");
948 else if (instr->Rs1Value() == zero.code())
949 Format(instr, "sgtz 'rd, 'rs2");
950 else
951 Format(instr, "slt 'rd, 'rs1, 'rs2");
952 break;
953 case RO_SLTU:
954 if (instr->Rs1Value() == zero.code())
955 Format(instr, "snez 'rd, 'rs2");
956 else
957 Format(instr, "sltu 'rd, 'rs1, 'rs2");
958 break;
959 case RO_XOR:
960 Format(instr, "xor 'rd, 'rs1, 'rs2");
961 break;
962 case RO_SRL:
963 Format(instr, "srl 'rd, 'rs1, 'rs2");
964 break;
965 case RO_SRA:
966 Format(instr, "sra 'rd, 'rs1, 'rs2");
967 break;
968 case RO_OR:
969 Format(instr, "or 'rd, 'rs1, 'rs2");
970 break;
971 case RO_AND:
972 Format(instr, "and 'rd, 'rs1, 'rs2");
973 break;
974 #ifdef JS_CODEGEN_RISCV64
975 case RO_ADDW:
976 Format(instr, "addw 'rd, 'rs1, 'rs2");
977 break;
978 case RO_SUBW:
979 if (instr->Rs1Value() == zero.code())
980 Format(instr, "negw 'rd, 'rs2");
981 else
982 Format(instr, "subw 'rd, 'rs1, 'rs2");
983 break;
984 case RO_SLLW:
985 Format(instr, "sllw 'rd, 'rs1, 'rs2");
986 break;
987 case RO_SRLW:
988 Format(instr, "srlw 'rd, 'rs1, 'rs2");
989 break;
990 case RO_SRAW:
991 Format(instr, "sraw 'rd, 'rs1, 'rs2");
992 break;
993 #endif /* JS_CODEGEN_RISCV64 */
994 // TODO(riscv): Add RISCV M extension macro
995 case RO_MUL:
996 Format(instr, "mul 'rd, 'rs1, 'rs2");
997 break;
998 case RO_MULH:
999 Format(instr, "mulh 'rd, 'rs1, 'rs2");
1000 break;
1001 case RO_MULHSU:
1002 Format(instr, "mulhsu 'rd, 'rs1, 'rs2");
1003 break;
1004 case RO_MULHU:
1005 Format(instr, "mulhu 'rd, 'rs1, 'rs2");
1006 break;
1007 case RO_DIV:
1008 Format(instr, "div 'rd, 'rs1, 'rs2");
1009 break;
1010 case RO_DIVU:
1011 Format(instr, "divu 'rd, 'rs1, 'rs2");
1012 break;
1013 case RO_REM:
1014 Format(instr, "rem 'rd, 'rs1, 'rs2");
1015 break;
1016 case RO_REMU:
1017 Format(instr, "remu 'rd, 'rs1, 'rs2");
1018 break;
1019 #ifdef JS_CODEGEN_RISCV64
1020 case RO_MULW:
1021 Format(instr, "mulw 'rd, 'rs1, 'rs2");
1022 break;
1023 case RO_DIVW:
1024 Format(instr, "divw 'rd, 'rs1, 'rs2");
1025 break;
1026 case RO_DIVUW:
1027 Format(instr, "divuw 'rd, 'rs1, 'rs2");
1028 break;
1029 case RO_REMW:
1030 Format(instr, "remw 'rd, 'rs1, 'rs2");
1031 break;
1032 case RO_REMUW:
1033 Format(instr, "remuw 'rd, 'rs1, 'rs2");
1034 break;
1035 #endif /*JS_CODEGEN_RISCV64*/
1036 // TODO(riscv): End Add RISCV M extension macro
1037 default: {
1038 switch (instr->BaseOpcode()) {
1039 case AMO:
1040 DecodeRAType(instr);
1041 break;
1042 case OP_FP:
1043 DecodeRFPType(instr);
1044 break;
1045 default:
1046 UNSUPPORTED_RISCV();
1047 }
1048 }
1049 }
1050 }
1051
1052 void Decoder::DecodeRAType(Instruction* instr) {
1053 // TODO(riscv): Add macro for RISCV A extension
1054 // Special handling for A extension instructions because it uses func5
1055 // For all A extension instruction, V8 simulator is pure sequential. No
1056 // Memory address lock or other synchronizaiton behaviors.
1057 switch (instr->InstructionBits() & kRATypeMask) {
1058 case RO_LR_W:
1059 Format(instr, "lr.w'a 'rd, ('rs1)");
1060 break;
1061 case RO_SC_W:
1062 Format(instr, "sc.w'a 'rd, 'rs2, ('rs1)");
1063 break;
1064 case RO_AMOSWAP_W:
1065 Format(instr, "amoswap.w'a 'rd, 'rs2, ('rs1)");
1066 break;
1067 case RO_AMOADD_W:
1068 Format(instr, "amoadd.w'a 'rd, 'rs2, ('rs1)");
1069 break;
1070 case RO_AMOXOR_W:
1071 Format(instr, "amoxor.w'a 'rd, 'rs2, ('rs1)");
1072 break;
1073 case RO_AMOAND_W:
1074 Format(instr, "amoand.w'a 'rd, 'rs2, ('rs1)");
1075 break;
1076 case RO_AMOOR_W:
1077 Format(instr, "amoor.w'a 'rd, 'rs2, ('rs1)");
1078 break;
1079 case RO_AMOMIN_W:
1080 Format(instr, "amomin.w'a 'rd, 'rs2, ('rs1)");
1081 break;
1082 case RO_AMOMAX_W:
1083 Format(instr, "amomax.w'a 'rd, 'rs2, ('rs1)");
1084 break;
1085 case RO_AMOMINU_W:
1086 Format(instr, "amominu.w'a 'rd, 'rs2, ('rs1)");
1087 break;
1088 case RO_AMOMAXU_W:
1089 Format(instr, "amomaxu.w'a 'rd, 'rs2, ('rs1)");
1090 break;
1091 #ifdef JS_CODEGEN_RISCV64
1092 case RO_LR_D:
1093 Format(instr, "lr.d'a 'rd, ('rs1)");
1094 break;
1095 case RO_SC_D:
1096 Format(instr, "sc.d'a 'rd, 'rs2, ('rs1)");
1097 break;
1098 case RO_AMOSWAP_D:
1099 Format(instr, "amoswap.d'a 'rd, 'rs2, ('rs1)");
1100 break;
1101 case RO_AMOADD_D:
1102 Format(instr, "amoadd.d'a 'rd, 'rs2, ('rs1)");
1103 break;
1104 case RO_AMOXOR_D:
1105 Format(instr, "amoxor.d'a 'rd, 'rs2, ('rs1)");
1106 break;
1107 case RO_AMOAND_D:
1108 Format(instr, "amoand.d'a 'rd, 'rs2, ('rs1)");
1109 break;
1110 case RO_AMOOR_D:
1111 Format(instr, "amoor.d'a 'rd, 'rs2, ('rs1)");
1112 break;
1113 case RO_AMOMIN_D:
1114 Format(instr, "amomin.d'a 'rd, 'rs2, ('rs1)");
1115 break;
1116 case RO_AMOMAX_D:
1117 Format(instr, "amoswap.d'a 'rd, 'rs2, ('rs1)");
1118 break;
1119 case RO_AMOMINU_D:
1120 Format(instr, "amominu.d'a 'rd, 'rs2, ('rs1)");
1121 break;
1122 case RO_AMOMAXU_D:
1123 Format(instr, "amomaxu.d'a 'rd, 'rs2, ('rs1)");
1124 break;
1125 #endif /*JS_CODEGEN_RISCV64*/
1126 // TODO(riscv): End Add macro for RISCV A extension
1127 default: {
1128 UNSUPPORTED_RISCV();
1129 }
1130 }
1131 }
1132
1133 void Decoder::DecodeRFPType(Instruction* instr) {
1134 // OP_FP instructions (F/D) uses func7 first. Some further uses fun3 and rs2()
1135
1136 // kRATypeMask is only for func7
1137 switch (instr->InstructionBits() & kRFPTypeMask) {
1138 // TODO(riscv): Add macro for RISCV F extension
1139 case RO_FADD_S:
1140 Format(instr, "fadd.s 'fd, 'fs1, 'fs2");
1141 break;
1142 case RO_FSUB_S:
1143 Format(instr, "fsub.s 'fd, 'fs1, 'fs2");
1144 break;
1145 case RO_FMUL_S:
1146 Format(instr, "fmul.s 'fd, 'fs1, 'fs2");
1147 break;
1148 case RO_FDIV_S:
1149 Format(instr, "fdiv.s 'fd, 'fs1, 'fs2");
1150 break;
1151 case RO_FSQRT_S:
1152 Format(instr, "fsqrt.s 'fd, 'fs1");
1153 break;
1154 case RO_FSGNJ_S: { // RO_FSGNJN_S RO_FSGNJX_S
1155 switch (instr->Funct3Value()) {
1156 case 0b000: // RO_FSGNJ_S
1157 if (instr->Rs1Value() == instr->Rs2Value())
1158 Format(instr, "fmv.s 'fd, 'fs1");
1159 else
1160 Format(instr, "fsgnj.s 'fd, 'fs1, 'fs2");
1161 break;
1162 case 0b001: // RO_FSGNJN_S
1163 if (instr->Rs1Value() == instr->Rs2Value())
1164 Format(instr, "fneg.s 'fd, 'fs1");
1165 else
1166 Format(instr, "fsgnjn.s 'fd, 'fs1, 'fs2");
1167 break;
1168 case 0b010: // RO_FSGNJX_S
1169 if (instr->Rs1Value() == instr->Rs2Value())
1170 Format(instr, "fabs.s 'fd, 'fs1");
1171 else
1172 Format(instr, "fsgnjx.s 'fd, 'fs1, 'fs2");
1173 break;
1174 default:
1175 UNSUPPORTED_RISCV();
1176 }
1177 break;
1178 }
1179 case RO_FMIN_S: { // RO_FMAX_S
1180 switch (instr->Funct3Value()) {
1181 case 0b000: // RO_FMIN_S
1182 Format(instr, "fmin.s 'fd, 'fs1, 'fs2");
1183 break;
1184 case 0b001: // RO_FMAX_S
1185 Format(instr, "fmax.s 'fd, 'fs1, 'fs2");
1186 break;
1187 default:
1188 UNSUPPORTED_RISCV();
1189 }
1190 break;
1191 }
1192 case RO_FCVT_W_S: { // RO_FCVT_WU_S , 64F RO_FCVT_L_S RO_FCVT_LU_S
1193 switch (instr->Rs2Value()) {
1194 case 0b00000: // RO_FCVT_W_S
1195 Format(instr, "fcvt.w.s ['frm] 'rd, 'fs1");
1196 break;
1197 case 0b00001: // RO_FCVT_WU_S
1198 Format(instr, "fcvt.wu.s ['frm] 'rd, 'fs1");
1199 break;
1200 #ifdef JS_CODEGEN_RISCV64
1201 case 0b00010: // RO_FCVT_L_S
1202 Format(instr, "fcvt.l.s ['frm] 'rd, 'fs1");
1203 break;
1204 case 0b00011: // RO_FCVT_LU_S
1205 Format(instr, "fcvt.lu.s ['frm] 'rd, 'fs1");
1206 break;
1207 #endif /* JS_CODEGEN_RISCV64 */
1208 default:
1209 UNSUPPORTED_RISCV();
1210 }
1211 break;
1212 }
1213 case RO_FMV: { // RO_FCLASS_S
1214 if (instr->Rs2Value() != 0b00000) {
1215 UNSUPPORTED_RISCV();
1216 }
1217 switch (instr->Funct3Value()) {
1218 case 0b000: // RO_FMV_X_W
1219 Format(instr, "fmv.x.w 'rd, 'fs1");
1220 break;
1221 case 0b001: // RO_FCLASS_S
1222 Format(instr, "fclass.s 'rd, 'fs1");
1223 break;
1224 default:
1225 UNSUPPORTED_RISCV();
1226 }
1227 break;
1228 }
1229 case RO_FLE_S: { // RO_FEQ_S RO_FLT_S RO_FLE_S
1230 switch (instr->Funct3Value()) {
1231 case 0b010: // RO_FEQ_S
1232 Format(instr, "feq.s 'rd, 'fs1, 'fs2");
1233 break;
1234 case 0b001: // RO_FLT_S
1235 Format(instr, "flt.s 'rd, 'fs1, 'fs2");
1236 break;
1237 case 0b000: // RO_FLE_S
1238 Format(instr, "fle.s 'rd, 'fs1, 'fs2");
1239 break;
1240 default:
1241 UNSUPPORTED_RISCV();
1242 }
1243 break;
1244 }
1245 case RO_FCVT_S_W: { // RO_FCVT_S_WU , 64F RO_FCVT_S_L RO_FCVT_S_LU
1246 switch (instr->Rs2Value()) {
1247 case 0b00000: // RO_FCVT_S_W
1248 Format(instr, "fcvt.s.w 'fd, 'rs1");
1249 break;
1250 case 0b00001: // RO_FCVT_S_WU
1251 Format(instr, "fcvt.s.wu 'fd, 'rs1");
1252 break;
1253 #ifdef JS_CODEGEN_RISCV64
1254 case 0b00010: // RO_FCVT_S_L
1255 Format(instr, "fcvt.s.l 'fd, 'rs1");
1256 break;
1257 case 0b00011: // RO_FCVT_S_LU
1258 Format(instr, "fcvt.s.lu 'fd, 'rs1");
1259 break;
1260 #endif /* JS_CODEGEN_RISCV64 */
1261 default: {
1262 UNSUPPORTED_RISCV();
1263 }
1264 }
1265 break;
1266 }
1267 case RO_FMV_W_X: {
1268 if (instr->Funct3Value() == 0b000) {
1269 Format(instr, "fmv.w.x 'fd, 'rs1");
1270 } else {
1271 UNSUPPORTED_RISCV();
1272 }
1273 break;
1274 }
1275 // TODO(riscv): Add macro for RISCV D extension
1276 case RO_FADD_D:
1277 Format(instr, "fadd.d 'fd, 'fs1, 'fs2");
1278 break;
1279 case RO_FSUB_D:
1280 Format(instr, "fsub.d 'fd, 'fs1, 'fs2");
1281 break;
1282 case RO_FMUL_D:
1283 Format(instr, "fmul.d 'fd, 'fs1, 'fs2");
1284 break;
1285 case RO_FDIV_D:
1286 Format(instr, "fdiv.d 'fd, 'fs1, 'fs2");
1287 break;
1288 case RO_FSQRT_D: {
1289 if (instr->Rs2Value() == 0b00000) {
1290 Format(instr, "fsqrt.d 'fd, 'fs1");
1291 } else {
1292 UNSUPPORTED_RISCV();
1293 }
1294 break;
1295 }
1296 case RO_FSGNJ_D: { // RO_FSGNJN_D RO_FSGNJX_D
1297 switch (instr->Funct3Value()) {
1298 case 0b000: // RO_FSGNJ_D
1299 if (instr->Rs1Value() == instr->Rs2Value())
1300 Format(instr, "fmv.d 'fd, 'fs1");
1301 else
1302 Format(instr, "fsgnj.d 'fd, 'fs1, 'fs2");
1303 break;
1304 case 0b001: // RO_FSGNJN_D
1305 if (instr->Rs1Value() == instr->Rs2Value())
1306 Format(instr, "fneg.d 'fd, 'fs1");
1307 else
1308 Format(instr, "fsgnjn.d 'fd, 'fs1, 'fs2");
1309 break;
1310 case 0b010: // RO_FSGNJX_D
1311 if (instr->Rs1Value() == instr->Rs2Value())
1312 Format(instr, "fabs.d 'fd, 'fs1");
1313 else
1314 Format(instr, "fsgnjx.d 'fd, 'fs1, 'fs2");
1315 break;
1316 default:
1317 UNSUPPORTED_RISCV();
1318 }
1319 break;
1320 }
1321 case RO_FMIN_D: { // RO_FMAX_D
1322 switch (instr->Funct3Value()) {
1323 case 0b000: // RO_FMIN_D
1324 Format(instr, "fmin.d 'fd, 'fs1, 'fs2");
1325 break;
1326 case 0b001: // RO_FMAX_D
1327 Format(instr, "fmax.d 'fd, 'fs1, 'fs2");
1328 break;
1329 default:
1330 UNSUPPORTED_RISCV();
1331 }
1332 break;
1333 }
1334 case (RO_FCVT_S_D & kRFPTypeMask): {
1335 if (instr->Rs2Value() == 0b00001) {
1336 Format(instr, "fcvt.s.d ['frm] 'fd, 'fs1");
1337 } else {
1338 UNSUPPORTED_RISCV();
1339 }
1340 break;
1341 }
1342 case RO_FCVT_D_S: {
1343 if (instr->Rs2Value() == 0b00000) {
1344 Format(instr, "fcvt.d.s 'fd, 'fs1");
1345 } else {
1346 UNSUPPORTED_RISCV();
1347 }
1348 break;
1349 }
1350 case RO_FLE_D: { // RO_FEQ_D RO_FLT_D RO_FLE_D
1351 switch (instr->Funct3Value()) {
1352 case 0b010: // RO_FEQ_S
1353 Format(instr, "feq.d 'rd, 'fs1, 'fs2");
1354 break;
1355 case 0b001: // RO_FLT_D
1356 Format(instr, "flt.d 'rd, 'fs1, 'fs2");
1357 break;
1358 case 0b000: // RO_FLE_D
1359 Format(instr, "fle.d 'rd, 'fs1, 'fs2");
1360 break;
1361 default:
1362 UNSUPPORTED_RISCV();
1363 }
1364 break;
1365 }
1366 case (RO_FCLASS_D & kRFPTypeMask): { // RO_FCLASS_D , 64D RO_FMV_X_D
1367 if (instr->Rs2Value() != 0b00000) {
1368 UNSUPPORTED_RISCV();
1369 break;
1370 }
1371 switch (instr->Funct3Value()) {
1372 case 0b001: // RO_FCLASS_D
1373 Format(instr, "fclass.d 'rd, 'fs1");
1374 break;
1375 #ifdef JS_CODEGEN_RISCV64
1376 case 0b000: // RO_FMV_X_D
1377 Format(instr, "fmv.x.d 'rd, 'fs1");
1378 break;
1379 #endif /* JS_CODEGEN_RISCV64 */
1380 default:
1381 UNSUPPORTED_RISCV();
1382 }
1383 break;
1384 }
1385 case RO_FCVT_W_D: { // RO_FCVT_WU_D , 64F RO_FCVT_L_D RO_FCVT_LU_D
1386 switch (instr->Rs2Value()) {
1387 case 0b00000: // RO_FCVT_W_D
1388 Format(instr, "fcvt.w.d ['frm] 'rd, 'fs1");
1389 break;
1390 case 0b00001: // RO_FCVT_WU_D
1391 Format(instr, "fcvt.wu.d ['frm] 'rd, 'fs1");
1392 break;
1393 #ifdef JS_CODEGEN_RISCV64
1394 case 0b00010: // RO_FCVT_L_D
1395 Format(instr, "fcvt.l.d ['frm] 'rd, 'fs1");
1396 break;
1397 case 0b00011: // RO_FCVT_LU_D
1398 Format(instr, "fcvt.lu.d ['frm] 'rd, 'fs1");
1399 break;
1400 #endif /* JS_CODEGEN_RISCV64 */
1401 default:
1402 UNSUPPORTED_RISCV();
1403 }
1404 break;
1405 }
1406 case RO_FCVT_D_W: { // RO_FCVT_D_WU , 64F RO_FCVT_D_L RO_FCVT_D_LU
1407 switch (instr->Rs2Value()) {
1408 case 0b00000: // RO_FCVT_D_W
1409 Format(instr, "fcvt.d.w 'fd, 'rs1");
1410 break;
1411 case 0b00001: // RO_FCVT_D_WU
1412 Format(instr, "fcvt.d.wu 'fd, 'rs1");
1413 break;
1414 #ifdef JS_CODEGEN_RISCV64
1415 case 0b00010: // RO_FCVT_D_L
1416 Format(instr, "fcvt.d.l 'fd, 'rs1");
1417 break;
1418 case 0b00011: // RO_FCVT_D_LU
1419 Format(instr, "fcvt.d.lu 'fd, 'rs1");
1420 break;
1421 #endif /* JS_CODEGEN_RISCV64 */
1422 default:
1423 UNSUPPORTED_RISCV();
1424 }
1425 break;
1426 }
1427 #ifdef JS_CODEGEN_RISCV64
1428 case RO_FMV_D_X: {
1429 if (instr->Funct3Value() == 0b000 && instr->Rs2Value() == 0b00000) {
1430 Format(instr, "fmv.d.x 'fd, 'rs1");
1431 } else {
1432 UNSUPPORTED_RISCV();
1433 }
1434 break;
1435 }
1436 #endif /* JS_CODEGEN_RISCV64 */
1437 default: {
1438 UNSUPPORTED_RISCV();
1439 }
1440 }
1441 }
1442
1443 void Decoder::DecodeR4Type(Instruction* instr) {
1444 switch (instr->InstructionBits() & kR4TypeMask) {
1445 // TODO(riscv): use F Extension macro block
1446 case RO_FMADD_S:
1447 Format(instr, "fmadd.s 'fd, 'fs1, 'fs2, 'fs3");
1448 break;
1449 case RO_FMSUB_S:
1450 Format(instr, "fmsub.s 'fd, 'fs1, 'fs2, 'fs3");
1451 break;
1452 case RO_FNMSUB_S:
1453 Format(instr, "fnmsub.s 'fd, 'fs1, 'fs2, 'fs3");
1454 break;
1455 case RO_FNMADD_S:
1456 Format(instr, "fnmadd.s 'fd, 'fs1, 'fs2, 'fs3");
1457 break;
1458 // TODO(riscv): use F Extension macro block
1459 case RO_FMADD_D:
1460 Format(instr, "fmadd.d 'fd, 'fs1, 'fs2, 'fs3");
1461 break;
1462 case RO_FMSUB_D:
1463 Format(instr, "fmsub.d 'fd, 'fs1, 'fs2, 'fs3");
1464 break;
1465 case RO_FNMSUB_D:
1466 Format(instr, "fnmsub.d 'fd, 'fs1, 'fs2, 'fs3");
1467 break;
1468 case RO_FNMADD_D:
1469 Format(instr, "fnmadd.d 'fd, 'fs1, 'fs2, 'fs3");
1470 break;
1471 default:
1472 UNSUPPORTED_RISCV();
1473 }
1474 }
1475
1476 void Decoder::DecodeIType(Instruction* instr) {
1477 switch (instr->InstructionBits() & kITypeMask) {
1478 case RO_JALR:
1479 if (instr->RdValue() == zero.code() && instr->Rs1Value() == ra.code() &&
1480 instr->Imm12Value() == 0)
1481 Format(instr, "ret");
1482 else if (instr->RdValue() == zero.code() && instr->Imm12Value() == 0)
1483 Format(instr, "jr 'rs1");
1484 else if (instr->RdValue() == ra.code() && instr->Imm12Value() == 0)
1485 Format(instr, "jalr 'rs1");
1486 else
1487 Format(instr, "jalr 'rd, 'imm12('rs1)");
1488 break;
1489 case RO_LB:
1490 Format(instr, "lb 'rd, 'imm12('rs1)");
1491 break;
1492 case RO_LH:
1493 Format(instr, "lh 'rd, 'imm12('rs1)");
1494 break;
1495 case RO_LW:
1496 Format(instr, "lw 'rd, 'imm12('rs1)");
1497 break;
1498 case RO_LBU:
1499 Format(instr, "lbu 'rd, 'imm12('rs1)");
1500 break;
1501 case RO_LHU:
1502 Format(instr, "lhu 'rd, 'imm12('rs1)");
1503 break;
1504 #ifdef JS_CODEGEN_RISCV64
1505 case RO_LWU:
1506 Format(instr, "lwu 'rd, 'imm12('rs1)");
1507 break;
1508 case RO_LD:
1509 Format(instr, "ld 'rd, 'imm12('rs1)");
1510 break;
1511 #endif /*JS_CODEGEN_RISCV64*/
1512 case RO_ADDI:
1513 if (instr->Imm12Value() == 0) {
1514 if (instr->RdValue() == zero.code() && instr->Rs1Value() == zero.code())
1515 Format(instr, "nop");
1516 else
1517 Format(instr, "mv 'rd, 'rs1");
1518 } else if (instr->Rs1Value() == zero.code()) {
1519 Format(instr, "li 'rd, 'imm12");
1520 } else {
1521 Format(instr, "addi 'rd, 'rs1, 'imm12");
1522 }
1523 break;
1524 case RO_SLTI:
1525 Format(instr, "slti 'rd, 'rs1, 'imm12");
1526 break;
1527 case RO_SLTIU:
1528 if (instr->Imm12Value() == 1)
1529 Format(instr, "seqz 'rd, 'rs1");
1530 else
1531 Format(instr, "sltiu 'rd, 'rs1, 'imm12");
1532 break;
1533 case RO_XORI:
1534 if (instr->Imm12Value() == -1)
1535 Format(instr, "not 'rd, 'rs1");
1536 else
1537 Format(instr, "xori 'rd, 'rs1, 'imm12x");
1538 break;
1539 case RO_ORI:
1540 Format(instr, "ori 'rd, 'rs1, 'imm12x");
1541 break;
1542 case RO_ANDI:
1543 Format(instr, "andi 'rd, 'rs1, 'imm12x");
1544 break;
1545 case RO_SLLI:
1546 Format(instr, "slli 'rd, 'rs1, 's64");
1547 break;
1548 case RO_SRLI: { // RO_SRAI
1549 if (!instr->IsArithShift()) {
1550 Format(instr, "srli 'rd, 'rs1, 's64");
1551 } else {
1552 Format(instr, "srai 'rd, 'rs1, 's64");
1553 }
1554 break;
1555 }
1556 #ifdef JS_CODEGEN_RISCV64
1557 case RO_ADDIW:
1558 if (instr->Imm12Value() == 0)
1559 Format(instr, "sext.w 'rd, 'rs1");
1560 else
1561 Format(instr, "addiw 'rd, 'rs1, 'imm12");
1562 break;
1563 case RO_SLLIW:
1564 Format(instr, "slliw 'rd, 'rs1, 's32");
1565 break;
1566 case RO_SRLIW: { // RO_SRAIW
1567 if (!instr->IsArithShift()) {
1568 Format(instr, "srliw 'rd, 'rs1, 's32");
1569 } else {
1570 Format(instr, "sraiw 'rd, 'rs1, 's32");
1571 }
1572 break;
1573 }
1574 #endif /*JS_CODEGEN_RISCV64*/
1575 case RO_FENCE:
1576 if (instr->MemoryOrder(true) == PSIORW &&
1577 instr->MemoryOrder(false) == PSIORW)
1578 Format(instr, "fence");
1579 else
1580 Format(instr, "fence 'pre, 'suc");
1581 break;
1582 case RO_ECALL: { // RO_EBREAK
1583 if (instr->Imm12Value() == 0) { // ECALL
1584 Format(instr, "ecall");
1585 } else if (instr->Imm12Value() == 1) { // EBREAK
1586 Format(instr, "ebreak");
1587 } else {
1588 UNSUPPORTED_RISCV();
1589 }
1590 break;
1591 }
1592 // TODO(riscv): use Zifencei Standard Extension macro block
1593 case RO_FENCE_I:
1594 Format(instr, "fence.i");
1595 break;
1596 // TODO(riscv): use Zicsr Standard Extension macro block
1597 // FIXME(RISC-V): Add special formatting for CSR registers
1598 case RO_CSRRW:
1599 if (instr->CsrValue() == csr_fcsr) {
1600 if (instr->RdValue() == zero.code())
1601 Format(instr, "fscsr 'rs1");
1602 else
1603 Format(instr, "fscsr 'rd, 'rs1");
1604 } else if (instr->CsrValue() == csr_frm) {
1605 if (instr->RdValue() == zero.code())
1606 Format(instr, "fsrm 'rs1");
1607 else
1608 Format(instr, "fsrm 'rd, 'rs1");
1609 } else if (instr->CsrValue() == csr_fflags) {
1610 if (instr->RdValue() == zero.code())
1611 Format(instr, "fsflags 'rs1");
1612 else
1613 Format(instr, "fsflags 'rd, 'rs1");
1614 } else if (instr->RdValue() == zero.code()) {
1615 Format(instr, "csrw 'csr, 'rs1");
1616 } else {
1617 Format(instr, "csrrw 'rd, 'csr, 'rs1");
1618 }
1619 break;
1620 case RO_CSRRS:
1621 if (instr->Rs1Value() == zero.code()) {
1622 switch (instr->CsrValue()) {
1623 case csr_instret:
1624 Format(instr, "rdinstret 'rd");
1625 break;
1626 case csr_instreth:
1627 Format(instr, "rdinstreth 'rd");
1628 break;
1629 case csr_time:
1630 Format(instr, "rdtime 'rd");
1631 break;
1632 case csr_timeh:
1633 Format(instr, "rdtimeh 'rd");
1634 break;
1635 case csr_cycle:
1636 Format(instr, "rdcycle 'rd");
1637 break;
1638 case csr_cycleh:
1639 Format(instr, "rdcycleh 'rd");
1640 break;
1641 case csr_fflags:
1642 Format(instr, "frflags 'rd");
1643 break;
1644 case csr_frm:
1645 Format(instr, "frrm 'rd");
1646 break;
1647 case csr_fcsr:
1648 Format(instr, "frcsr 'rd");
1649 break;
1650 default:
1651 MOZ_CRASH();
1652 }
1653 } else if (instr->Rs1Value() == zero.code()) {
1654 Format(instr, "csrr 'rd, 'csr");
1655 } else if (instr->RdValue() == zero.code()) {
1656 Format(instr, "csrs 'csr, 'rs1");
1657 } else {
1658 Format(instr, "csrrs 'rd, 'csr, 'rs1");
1659 }
1660 break;
1661 case RO_CSRRC:
1662 if (instr->RdValue() == zero.code())
1663 Format(instr, "csrc 'csr, 'rs1");
1664 else
1665 Format(instr, "csrrc 'rd, 'csr, 'rs1");
1666 break;
1667 case RO_CSRRWI:
1668 if (instr->RdValue() == zero.code())
1669 Format(instr, "csrwi 'csr, 'uimm");
1670 else
1671 Format(instr, "csrrwi 'rd, 'csr, 'uimm");
1672 break;
1673 case RO_CSRRSI:
1674 if (instr->RdValue() == zero.code())
1675 Format(instr, "csrsi 'csr, 'uimm");
1676 else
1677 Format(instr, "csrrsi 'rd, 'csr, 'uimm");
1678 break;
1679 case RO_CSRRCI:
1680 if (instr->RdValue() == zero.code())
1681 Format(instr, "csrci 'csr, 'uimm");
1682 else
1683 Format(instr, "csrrci 'rd, 'csr, 'uimm");
1684 break;
1685 // TODO(riscv): use F Extension macro block
1686 case RO_FLW:
1687 Format(instr, "flw 'fd, 'imm12('rs1)");
1688 break;
1689 // TODO(riscv): use D Extension macro block
1690 case RO_FLD:
1691 Format(instr, "fld 'fd, 'imm12('rs1)");
1692 break;
1693 default:
1694 #ifdef CAN_USE_RVV_INSTRUCTIONS
1695 if (instr->vl_vs_width() != -1) {
1696 DecodeRvvVL(instr);
1697 } else {
1698 UNSUPPORTED_RISCV();
1699 }
1700 break;
1701 #else
1702 UNSUPPORTED_RISCV();
1703 #endif
1704 }
1705 }
1706
1707 void Decoder::DecodeSType(Instruction* instr) {
1708 switch (instr->InstructionBits() & kSTypeMask) {
1709 case RO_SB:
1710 Format(instr, "sb 'rs2, 'offS('rs1)");
1711 break;
1712 case RO_SH:
1713 Format(instr, "sh 'rs2, 'offS('rs1)");
1714 break;
1715 case RO_SW:
1716 Format(instr, "sw 'rs2, 'offS('rs1)");
1717 break;
1718 #ifdef JS_CODEGEN_RISCV64
1719 case RO_SD:
1720 Format(instr, "sd 'rs2, 'offS('rs1)");
1721 break;
1722 #endif /*JS_CODEGEN_RISCV64*/
1723 // TODO(riscv): use F Extension macro block
1724 case RO_FSW:
1725 Format(instr, "fsw 'fs2, 'offS('rs1)");
1726 break;
1727 // TODO(riscv): use D Extension macro block
1728 case RO_FSD:
1729 Format(instr, "fsd 'fs2, 'offS('rs1)");
1730 break;
1731 default:
1732 #ifdef CAN_USE_RVV_INSTRUCTIONS
1733 if (instr->vl_vs_width() != -1) {
1734 DecodeRvvVS(instr);
1735 } else {
1736 UNSUPPORTED_RISCV();
1737 }
1738 break;
1739 #else
1740 UNSUPPORTED_RISCV();
1741 #endif
1742 }
1743 }
1744
1745 void Decoder::DecodeBType(Instruction* instr) {
1746 switch (instr->InstructionBits() & kBTypeMask) {
1747 case RO_BEQ:
1748 Format(instr, "beq 'rs1, 'rs2, 'offB");
1749 break;
1750 case RO_BNE:
1751 Format(instr, "bne 'rs1, 'rs2, 'offB");
1752 break;
1753 case RO_BLT:
1754 Format(instr, "blt 'rs1, 'rs2, 'offB");
1755 break;
1756 case RO_BGE:
1757 Format(instr, "bge 'rs1, 'rs2, 'offB");
1758 break;
1759 case RO_BLTU:
1760 Format(instr, "bltu 'rs1, 'rs2, 'offB");
1761 break;
1762 case RO_BGEU:
1763 Format(instr, "bgeu 'rs1, 'rs2, 'offB");
1764 break;
1765 default:
1766 UNSUPPORTED_RISCV();
1767 }
1768 }
1769 void Decoder::DecodeUType(Instruction* instr) {
1770 // U Type doesn't have additional mask
1771 switch (instr->BaseOpcodeFieldRaw()) {
1772 case LUI:
1773 Format(instr, "lui 'rd, 'imm20U");
1774 break;
1775 case AUIPC:
1776 Format(instr, "auipc 'rd, 'imm20U");
1777 break;
1778 default:
1779 UNSUPPORTED_RISCV();
1780 }
1781 }
1782 // namespace jit
1783 void Decoder::DecodeJType(Instruction* instr) {
1784 // J Type doesn't have additional mask
1785 switch (instr->BaseOpcodeValue()) {
1786 case JAL:
1787 if (instr->RdValue() == zero.code())
1788 Format(instr, "j 'imm20J");
1789 else if (instr->RdValue() == ra.code())
1790 Format(instr, "jal 'imm20J");
1791 else
1792 Format(instr, "jal 'rd, 'imm20J");
1793 break;
1794 default:
1795 UNSUPPORTED_RISCV();
1796 }
1797 }
1798
1799 void Decoder::DecodeCRType(Instruction* instr) {
1800 switch (instr->RvcFunct4Value()) {
1801 case 0b1000:
1802 if (instr->RvcRs1Value() != 0 && instr->RvcRs2Value() == 0)
1803 Format(instr, "jr 'Crs1");
1804 else if (instr->RvcRdValue() != 0 && instr->RvcRs2Value() != 0)
1805 Format(instr, "mv 'Crd, 'Crs2");
1806 else
1807 UNSUPPORTED_RISCV();
1808 break;
1809 case 0b1001:
1810 if (instr->RvcRs1Value() == 0 && instr->RvcRs2Value() == 0)
1811 Format(instr, "ebreak");
1812 else if (instr->RvcRdValue() != 0 && instr->RvcRs2Value() == 0)
1813 Format(instr, "jalr 'Crs1");
1814 else if (instr->RvcRdValue() != 0 && instr->RvcRs2Value() != 0)
1815 Format(instr, "add 'Crd, 'Crd, 'Crs2");
1816 else
1817 UNSUPPORTED_RISCV();
1818 break;
1819 default:
1820 UNSUPPORTED_RISCV();
1821 }
1822 }
1823
1824 void Decoder::DecodeCAType(Instruction* instr) {
1825 switch (instr->InstructionBits() & kCATypeMask) {
1826 case RO_C_SUB:
1827 Format(instr, "sub 'Crs1s, 'Crs1s, 'Crs2s");
1828 break;
1829 case RO_C_XOR:
1830 Format(instr, "xor 'Crs1s, 'Crs1s, 'Crs2s");
1831 break;
1832 case RO_C_OR:
1833 Format(instr, "or 'Crs1s, 'Crs1s, 'Crs2s");
1834 break;
1835 case RO_C_AND:
1836 Format(instr, "and 'Crs1s, 'Crs1s, 'Crs2s");
1837 break;
1838 #ifdef JS_CODEGEN_RISCV64
1839 case RO_C_SUBW:
1840 Format(instr, "subw 'Crs1s, 'Crs1s, 'Crs2s");
1841 break;
1842 case RO_C_ADDW:
1843 Format(instr, "addw 'Crs1s, 'Crs1s, 'Crs2s");
1844 break;
1845 #endif
1846 default:
1847 UNSUPPORTED_RISCV();
1848 }
1849 }
1850
1851 void Decoder::DecodeCIType(Instruction* instr) {
1852 switch (instr->RvcOpcode()) {
1853 case RO_C_NOP_ADDI:
1854 if (instr->RvcRdValue() == 0)
1855 Format(instr, "nop");
1856 else
1857 Format(instr, "addi 'Crd, 'Crd, 'Cimm6");
1858 break;
1859 #ifdef JS_CODEGEN_RISCV64
1860 case RO_C_ADDIW:
1861 Format(instr, "addiw 'Crd, 'Crd, 'Cimm6");
1862 break;
1863 #endif
1864 case RO_C_LI:
1865 Format(instr, "li 'Crd, 'Cimm6");
1866 break;
1867 case RO_C_LUI_ADD:
1868 if (instr->RvcRdValue() == 2)
1869 Format(instr, "addi sp, sp, 'Cimm6Addi16sp");
1870 else if (instr->RvcRdValue() != 0 && instr->RvcRdValue() != 2)
1871 Format(instr, "lui 'Crd, 'Cimm6U");
1872 else
1873 UNSUPPORTED_RISCV();
1874 break;
1875 case RO_C_SLLI:
1876 Format(instr, "slli 'Crd, 'Crd, 'Cshamt");
1877 break;
1878 case RO_C_FLDSP:
1879 Format(instr, "fld 'Cfd, 'Cimm6Ldsp(sp)");
1880 break;
1881 case RO_C_LWSP:
1882 Format(instr, "lw 'Crd, 'Cimm6Lwsp(sp)");
1883 break;
1884 #ifdef JS_CODEGEN_RISCV64
1885 case RO_C_LDSP:
1886 Format(instr, "ld 'Crd, 'Cimm6Ldsp(sp)");
1887 break;
1888 #elif defined(JS_CODEGEN_RISCV32)
1889 case RO_C_FLWSP:
1890 Format(instr, "flw 'Cfd, 'Cimm6Ldsp(sp)");
1891 break;
1892 #endif
1893 default:
1894 UNSUPPORTED_RISCV();
1895 }
1896 }
1897
1898 void Decoder::DecodeCIWType(Instruction* instr) {
1899 switch (instr->RvcOpcode()) {
1900 case RO_C_ADDI4SPN:
1901 Format(instr, "addi 'Crs2s, sp, 'Cimm8Addi4spn");
1902 break;
1903 default:
1904 UNSUPPORTED_RISCV();
1905 }
1906 }
1907
1908 void Decoder::DecodeCSSType(Instruction* instr) {
1909 switch (instr->RvcOpcode()) {
1910 case RO_C_SWSP:
1911 Format(instr, "sw 'Crs2, 'Cimm6Swsp(sp)");
1912 break;
1913 #ifdef JS_CODEGEN_RISCV64
1914 case RO_C_SDSP:
1915 Format(instr, "sd 'Crs2, 'Cimm6Sdsp(sp)");
1916 break;
1917 #elif defined(JS_CODEGEN_RISCV32)
1918 case RO_C_FSWSP:
1919 Format(instr, "fsw 'Cfs2, 'Cimm6Sdsp(sp)");
1920 break;
1921 #endif
1922 case RO_C_FSDSP:
1923 Format(instr, "fsd 'Cfs2, 'Cimm6Sdsp(sp)");
1924 break;
1925 default:
1926 UNSUPPORTED_RISCV();
1927 }
1928 }
1929
1930 void Decoder::DecodeCLType(Instruction* instr) {
1931 switch (instr->RvcOpcode()) {
1932 case RO_C_FLD:
1933 Format(instr, "fld 'Cfs2s, 'Cimm5D('Crs1s)");
1934 break;
1935 case RO_C_LW:
1936 Format(instr, "lw 'Crs2s, 'Cimm5W('Crs1s)");
1937 break;
1938 #ifdef JS_CODEGEN_RISCV64
1939 case RO_C_LD:
1940 Format(instr, "ld 'Crs2s, 'Cimm5D('Crs1s)");
1941 break;
1942 #elif defined(JS_CODEGEN_RISCV32)
1943 case RO_C_FLW:
1944 Format(instr, "fld 'Cfs2s, 'Cimm5D('Crs1s)");
1945 break;
1946 #endif
1947
1948 default:
1949 UNSUPPORTED_RISCV();
1950 }
1951 }
1952
1953 void Decoder::DecodeCSType(Instruction* instr) {
1954 switch (instr->RvcOpcode()) {
1955 case RO_C_FSD:
1956 Format(instr, "fsd 'Cfs2s, 'Cimm5D('Crs1s)");
1957 break;
1958 case RO_C_SW:
1959 Format(instr, "sw 'Crs2s, 'Cimm5W('Crs1s)");
1960 break;
1961 #ifdef JS_CODEGEN_RISCV64
1962 case RO_C_SD:
1963 Format(instr, "sd 'Crs2s, 'Cimm5D('Crs1s)");
1964 break;
1965 #elif defined(JS_CODEGEN_RISCV32)
1966 case RO_C_FSW:
1967 Format(instr, "fsw 'Cfs2s, 'Cimm5D('Crs1s)");
1968 break;
1969 #endif
1970 default:
1971 UNSUPPORTED_RISCV();
1972 }
1973 }
1974
1975 void Decoder::DecodeCJType(Instruction* instr) {
1976 switch (instr->RvcOpcode()) {
1977 case RO_C_J:
1978 Format(instr, "j 'Cimm11CJ");
1979 break;
1980 default:
1981 UNSUPPORTED_RISCV();
1982 }
1983 }
1984
1985 void Decoder::DecodeCBType(Instruction* instr) {
1986 switch (instr->RvcOpcode()) {
1987 case RO_C_BNEZ:
1988 Format(instr, "bnez 'Crs1s, x0, 'Cimm8B");
1989 break;
1990 case RO_C_BEQZ:
1991 Format(instr, "beqz 'Crs1s, x0, 'Cimm8B");
1992 break;
1993 case RO_C_MISC_ALU:
1994 if (instr->RvcFunct2BValue() == 0b00)
1995 Format(instr, "srli 'Crs1s, 'Crs1s, 'Cshamt");
1996 else if (instr->RvcFunct2BValue() == 0b01)
1997 Format(instr, "srai 'Crs1s, 'Crs1s, 'Cshamt");
1998 else if (instr->RvcFunct2BValue() == 0b10)
1999 Format(instr, "andi 'Crs1s, 'Crs1s, 'Cimm6");
2000 else
2001 UNSUPPORTED_RISCV();
2002 break;
2003 default:
2004 UNSUPPORTED_RISCV();
2005 }
2006 }
2007
2008 #undef VERIFIY
2009
2010 bool Decoder::IsConstantPoolAt(uint8_t* instr_ptr) {
2011 UNSUPPORTED_RISCV();
2012 MOZ_CRASH();
2013 }
2014
2015 int Decoder::ConstantPoolSizeAt(uint8_t* instr_ptr) {
2016 UNSUPPORTED_RISCV();
2017 MOZ_CRASH();
2018 }
2019
2020 // Disassemble the instruction at *instr_ptr into the output buffer.
2021 int Decoder::InstructionDecode(byte* instr_ptr) {
2022 Instruction* instr = Instruction::At(instr_ptr);
2023 // Print raw instruction bytes.
2024 out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_, "%08x ",
2025 instr->InstructionBits());
2026 switch (instr->InstructionType()) {
2027 case Instruction::kRType:
2028 DecodeRType(instr);
2029 break;
2030 case Instruction::kR4Type:
2031 DecodeR4Type(instr);
2032 break;
2033 case Instruction::kIType:
2034 DecodeIType(instr);
2035 break;
2036 case Instruction::kSType:
2037 DecodeSType(instr);
2038 break;
2039 case Instruction::kBType:
2040 DecodeBType(instr);
2041 break;
2042 case Instruction::kUType:
2043 DecodeUType(instr);
2044 break;
2045 case Instruction::kJType:
2046 DecodeJType(instr);
2047 break;
2048 case Instruction::kCRType:
2049 DecodeCRType(instr);
2050 break;
2051 case Instruction::kCAType:
2052 DecodeCAType(instr);
2053 break;
2054 case Instruction::kCJType:
2055 DecodeCJType(instr);
2056 break;
2057 case Instruction::kCIType:
2058 DecodeCIType(instr);
2059 break;
2060 case Instruction::kCIWType:
2061 DecodeCIWType(instr);
2062 break;
2063 case Instruction::kCSSType:
2064 DecodeCSSType(instr);
2065 break;
2066 case Instruction::kCLType:
2067 DecodeCLType(instr);
2068 break;
2069 case Instruction::kCSType:
2070 DecodeCSType(instr);
2071 break;
2072 case Instruction::kCBType:
2073 DecodeCBType(instr);
2074 break;
2075 #ifdef CAN_USE_RVV_INSTRUCTIONS
2076 case Instruction::kVType:
2077 DecodeVType(instr);
2078 break;
2079 #endif
2080 default:
2081 Format(instr, "UNSUPPORTED");
2082 UNSUPPORTED_RISCV();
2083 }
2084 return instr->InstructionSize();
2085 }
2086
2087 } // namespace disasm
2088
2089 #undef STRING_STARTS_WITH
2090 #undef VERIFY
2091
2092 //------------------------------------------------------------------------------
2093
2094 namespace disasm {
2095
2096 const char* NameConverter::NameOfAddress(uint8_t* addr) const {
2097 SNPrintF(tmp_buffer_, "%p", addr);
2098 return tmp_buffer_.start();
2099 }
2100
2101 const char* NameConverter::NameOfConstant(uint8_t* addr) const {
2102 return NameOfAddress(addr);
2103 }
2104
2105 const char* NameConverter::NameOfCPURegister(int reg) const {
2106 return Registers::GetName(reg);
2107 }
2108
2109 const char* NameConverter::NameOfByteCPURegister(int reg) const {
2110 MOZ_CRASH(" RISC-V does not have the concept of a byte register.");
2111 }
2112
2113 const char* NameConverter::NameOfXMMRegister(int reg) const {
2114 return FloatRegisters::GetName(reg);
2115 }
2116
2117 const char* NameConverter::NameInCode(uint8_t* addr) const {
2118 // The default name converter is called for unknown code. So we will not try
2119 // to access any memory.
2120 return "";
2121 }
2122
2123 //------------------------------------------------------------------------------
2124
2125 Disassembler::Disassembler(const NameConverter& converter)
2126 : converter_(converter) {}
2127
2128 Disassembler::~Disassembler() {}
2129
2130 int Disassembler::InstructionDecode(V8Vector<char> buffer,
2131 uint8_t* instruction) {
2132 Decoder d(converter_, buffer);
2133 return d.InstructionDecode(instruction);
2134 }
2135
2136 int Disassembler::ConstantPoolSizeAt(uint8_t* instruction) {
2137 return Decoder::ConstantPoolSizeAt(instruction);
2138 }
2139
2140 void Disassembler::Disassemble(FILE* f, uint8_t* begin, uint8_t* end) {
2141 NameConverter converter;
2142 Disassembler d(converter);
2143 for (uint8_t* pc = begin; pc < end;) {
2144 EmbeddedVector<char, ReasonableBufferSize> buffer;
2145 buffer[0] = '\0';
2146 uint8_t* prev_pc = pc;
2147 pc += d.InstructionDecode(buffer, pc);
2148 fprintf(f, "%p %08x %s\n", prev_pc,
2149 *reinterpret_cast<int32_t*>(prev_pc), buffer.start());
2150 }
2151 }
2152
2153 } // namespace disasm
2154 } // namespace jit
2155 } // namespace js