blob: 3f0642db2c02d691fcd5fcba8d8b96c50410e62e [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// Copyright 2012 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
5// A Disassembler object is used to disassemble a block of code instruction by
6// instruction. The default implementation of the NameConverter object can be
7// overriden to modify register names or to do symbol lookup on addresses.
8//
9// The example below will disassemble a block of code and print it to stdout.
10//
11// NameConverter converter;
12// Disassembler d(converter);
13// for (byte* pc = begin; pc < end;) {
14// v8::internal::EmbeddedVector<char, 256> buffer;
15// byte* prev_pc = pc;
16// pc += d.InstructionDecode(buffer, pc);
17// printf("%p %08x %s\n",
18// prev_pc, *reinterpret_cast<int32_t*>(prev_pc), buffer);
19// }
20//
21// The Disassembler class also has a convenience method to disassemble a block
22// of code into a FILE*, meaning that the above functionality could also be
23// achieved by just calling Disassembler::Disassemble(stdout, begin, end);
24
25
26#include <assert.h>
27#include <stdarg.h>
28#include <stdio.h>
29#include <string.h>
30
31#include "src/v8.h"
32
33#if V8_TARGET_ARCH_MIPS64
34
35#include "src/base/platform/platform.h"
36#include "src/disasm.h"
37#include "src/macro-assembler.h"
38#include "src/mips64/constants-mips64.h"
39
40namespace v8 {
41namespace internal {
42
43//------------------------------------------------------------------------------
44
45// Decoder decodes and disassembles instructions into an output buffer.
46// It uses the converter to convert register names and call destinations into
47// more informative description.
48class Decoder {
49 public:
50 Decoder(const disasm::NameConverter& converter,
51 v8::internal::Vector<char> out_buffer)
52 : converter_(converter),
53 out_buffer_(out_buffer),
54 out_buffer_pos_(0) {
55 out_buffer_[out_buffer_pos_] = '\0';
56 }
57
58 ~Decoder() {}
59
60 // Writes one disassembled instruction into 'buffer' (0-terminated).
61 // Returns the length of the disassembled machine instruction in bytes.
62 int InstructionDecode(byte* instruction);
63
64 private:
65 // Bottleneck functions to print into the out_buffer.
66 void PrintChar(const char ch);
67 void Print(const char* str);
68
69 // Printing of common values.
70 void PrintRegister(int reg);
71 void PrintFPURegister(int freg);
72 void PrintRs(Instruction* instr);
73 void PrintRt(Instruction* instr);
74 void PrintRd(Instruction* instr);
75 void PrintFs(Instruction* instr);
76 void PrintFt(Instruction* instr);
77 void PrintFd(Instruction* instr);
78 void PrintSa(Instruction* instr);
79 void PrintSd(Instruction* instr);
80 void PrintSs1(Instruction* instr);
81 void PrintSs2(Instruction* instr);
82 void PrintBc(Instruction* instr);
83 void PrintCc(Instruction* instr);
84 void PrintFunction(Instruction* instr);
85 void PrintSecondaryField(Instruction* instr);
86 void PrintUImm16(Instruction* instr);
87 void PrintSImm16(Instruction* instr);
88 void PrintXImm16(Instruction* instr);
89 void PrintXImm21(Instruction* instr);
90 void PrintXImm26(Instruction* instr);
91 void PrintCode(Instruction* instr); // For break and trap instructions.
92 // Printing of instruction name.
93 void PrintInstructionName(Instruction* instr);
94
95 // Handle formatting of instructions and their options.
96 int FormatRegister(Instruction* instr, const char* option);
97 int FormatFPURegister(Instruction* instr, const char* option);
98 int FormatOption(Instruction* instr, const char* option);
99 void Format(Instruction* instr, const char* format);
100 void Unknown(Instruction* instr);
101 int DecodeBreakInstr(Instruction* instr);
102
103 // Each of these functions decodes one particular instruction type.
104 int DecodeTypeRegister(Instruction* instr);
105 void DecodeTypeImmediate(Instruction* instr);
106 void DecodeTypeJump(Instruction* instr);
107
108 const disasm::NameConverter& converter_;
109 v8::internal::Vector<char> out_buffer_;
110 int out_buffer_pos_;
111
112 DISALLOW_COPY_AND_ASSIGN(Decoder);
113};
114
115
116// Support for assertions in the Decoder formatting functions.
117#define STRING_STARTS_WITH(string, compare_string) \
118 (strncmp(string, compare_string, strlen(compare_string)) == 0)
119
120
121// Append the ch to the output buffer.
122void Decoder::PrintChar(const char ch) {
123 out_buffer_[out_buffer_pos_++] = ch;
124}
125
126
127// Append the str to the output buffer.
128void Decoder::Print(const char* str) {
129 char cur = *str++;
130 while (cur != '\0' && (out_buffer_pos_ < (out_buffer_.length() - 1))) {
131 PrintChar(cur);
132 cur = *str++;
133 }
134 out_buffer_[out_buffer_pos_] = 0;
135}
136
137
138// Print the register name according to the active name converter.
139void Decoder::PrintRegister(int reg) {
140 Print(converter_.NameOfCPURegister(reg));
141}
142
143
144void Decoder::PrintRs(Instruction* instr) {
145 int reg = instr->RsValue();
146 PrintRegister(reg);
147}
148
149
150void Decoder::PrintRt(Instruction* instr) {
151 int reg = instr->RtValue();
152 PrintRegister(reg);
153}
154
155
156void Decoder::PrintRd(Instruction* instr) {
157 int reg = instr->RdValue();
158 PrintRegister(reg);
159}
160
161
162// Print the FPUregister name according to the active name converter.
163void Decoder::PrintFPURegister(int freg) {
164 Print(converter_.NameOfXMMRegister(freg));
165}
166
167
168void Decoder::PrintFs(Instruction* instr) {
169 int freg = instr->RsValue();
170 PrintFPURegister(freg);
171}
172
173
174void Decoder::PrintFt(Instruction* instr) {
175 int freg = instr->RtValue();
176 PrintFPURegister(freg);
177}
178
179
180void Decoder::PrintFd(Instruction* instr) {
181 int freg = instr->RdValue();
182 PrintFPURegister(freg);
183}
184
185
186// Print the integer value of the sa field.
187void Decoder::PrintSa(Instruction* instr) {
188 int sa = instr->SaValue();
189 out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_, "%d", sa);
190}
191
192
193// Print the integer value of the rd field, when it is not used as reg.
194void Decoder::PrintSd(Instruction* instr) {
195 int sd = instr->RdValue();
196 out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_, "%d", sd);
197}
198
199
200// Print the integer value of the rd field, when used as 'ext' size.
201void Decoder::PrintSs1(Instruction* instr) {
202 int ss = instr->RdValue();
203 out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_, "%d", ss + 1);
204}
205
206
207// Print the integer value of the rd field, when used as 'ins' size.
208void Decoder::PrintSs2(Instruction* instr) {
209 int ss = instr->RdValue();
210 int pos = instr->SaValue();
211 out_buffer_pos_ +=
212 SNPrintF(out_buffer_ + out_buffer_pos_, "%d", ss - pos + 1);
213}
214
215
216// Print the integer value of the cc field for the bc1t/f instructions.
217void Decoder::PrintBc(Instruction* instr) {
218 int cc = instr->FBccValue();
219 out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_, "%d", cc);
220}
221
222
223// Print the integer value of the cc field for the FP compare instructions.
224void Decoder::PrintCc(Instruction* instr) {
225 int cc = instr->FCccValue();
226 out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_, "cc(%d)", cc);
227}
228
229
230// Print 16-bit unsigned immediate value.
231void Decoder::PrintUImm16(Instruction* instr) {
232 int32_t imm = instr->Imm16Value();
233 out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_, "%u", imm);
234}
235
236
237// Print 16-bit signed immediate value.
238void Decoder::PrintSImm16(Instruction* instr) {
239 int32_t imm = ((instr->Imm16Value()) << 16) >> 16;
240 out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_, "%d", imm);
241}
242
243
244// Print 16-bit hexa immediate value.
245void Decoder::PrintXImm16(Instruction* instr) {
246 int32_t imm = instr->Imm16Value();
247 out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_, "0x%x", imm);
248}
249
250
251// Print 21-bit immediate value.
252void Decoder::PrintXImm21(Instruction* instr) {
253 uint32_t imm = instr->Imm21Value();
254 out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_, "0x%x", imm);
255}
256
257
258// Print 26-bit immediate value.
259void Decoder::PrintXImm26(Instruction* instr) {
260 uint32_t imm = instr->Imm26Value() << kImmFieldShift;
261 out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_, "0x%x", imm);
262}
263
264
265// Print 26-bit immediate value.
266void Decoder::PrintCode(Instruction* instr) {
267 if (instr->OpcodeFieldRaw() != SPECIAL)
268 return; // Not a break or trap instruction.
269 switch (instr->FunctionFieldRaw()) {
270 case BREAK: {
271 int32_t code = instr->Bits(25, 6);
272 out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
273 "0x%05x (%d)", code, code);
274 break;
275 }
276 case TGE:
277 case TGEU:
278 case TLT:
279 case TLTU:
280 case TEQ:
281 case TNE: {
282 int32_t code = instr->Bits(15, 6);
283 out_buffer_pos_ +=
284 SNPrintF(out_buffer_ + out_buffer_pos_, "0x%03x", code);
285 break;
286 }
287 default: // Not a break or trap instruction.
288 break;
289 }
290}
291
292
293// Printing of instruction name.
294void Decoder::PrintInstructionName(Instruction* instr) {
295}
296
297
298// Handle all register based formatting in this function to reduce the
299// complexity of FormatOption.
300int Decoder::FormatRegister(Instruction* instr, const char* format) {
301 DCHECK(format[0] == 'r');
302 if (format[1] == 's') { // 'rs: Rs register.
303 int reg = instr->RsValue();
304 PrintRegister(reg);
305 return 2;
306 } else if (format[1] == 't') { // 'rt: rt register.
307 int reg = instr->RtValue();
308 PrintRegister(reg);
309 return 2;
310 } else if (format[1] == 'd') { // 'rd: rd register.
311 int reg = instr->RdValue();
312 PrintRegister(reg);
313 return 2;
314 }
315 UNREACHABLE();
316 return -1;
317}
318
319
320// Handle all FPUregister based formatting in this function to reduce the
321// complexity of FormatOption.
322int Decoder::FormatFPURegister(Instruction* instr, const char* format) {
323 DCHECK(format[0] == 'f');
324 if (format[1] == 's') { // 'fs: fs register.
325 int reg = instr->FsValue();
326 PrintFPURegister(reg);
327 return 2;
328 } else if (format[1] == 't') { // 'ft: ft register.
329 int reg = instr->FtValue();
330 PrintFPURegister(reg);
331 return 2;
332 } else if (format[1] == 'd') { // 'fd: fd register.
333 int reg = instr->FdValue();
334 PrintFPURegister(reg);
335 return 2;
336 } else if (format[1] == 'r') { // 'fr: fr register.
337 int reg = instr->FrValue();
338 PrintFPURegister(reg);
339 return 2;
340 }
341 UNREACHABLE();
342 return -1;
343}
344
345
346// FormatOption takes a formatting string and interprets it based on
347// the current instructions. The format string points to the first
348// character of the option string (the option escape has already been
349// consumed by the caller.) FormatOption returns the number of
350// characters that were consumed from the formatting string.
351int Decoder::FormatOption(Instruction* instr, const char* format) {
352 switch (format[0]) {
353 case 'c': { // 'code for break or trap instructions.
354 DCHECK(STRING_STARTS_WITH(format, "code"));
355 PrintCode(instr);
356 return 4;
357 }
358 case 'i': { // 'imm16u or 'imm26.
359 if (format[3] == '1') {
360 DCHECK(STRING_STARTS_WITH(format, "imm16"));
361 if (format[5] == 's') {
362 DCHECK(STRING_STARTS_WITH(format, "imm16s"));
363 PrintSImm16(instr);
364 } else if (format[5] == 'u') {
365 DCHECK(STRING_STARTS_WITH(format, "imm16u"));
366 PrintSImm16(instr);
367 } else {
368 DCHECK(STRING_STARTS_WITH(format, "imm16x"));
369 PrintXImm16(instr);
370 }
371 return 6;
372 } else if (format[3] == '2' && format[4] == '1') {
373 DCHECK(STRING_STARTS_WITH(format, "imm21x"));
374 PrintXImm21(instr);
375 return 6;
376 } else if (format[3] == '2' && format[4] == '6') {
377 DCHECK(STRING_STARTS_WITH(format, "imm26x"));
378 PrintXImm26(instr);
379 return 6;
380 }
381 }
382 case 'r': { // 'r: registers.
383 return FormatRegister(instr, format);
384 }
385 case 'f': { // 'f: FPUregisters.
386 return FormatFPURegister(instr, format);
387 }
388 case 's': { // 'sa.
389 switch (format[1]) {
390 case 'a': {
391 DCHECK(STRING_STARTS_WITH(format, "sa"));
392 PrintSa(instr);
393 return 2;
394 }
395 case 'd': {
396 DCHECK(STRING_STARTS_WITH(format, "sd"));
397 PrintSd(instr);
398 return 2;
399 }
400 case 's': {
401 if (format[2] == '1') {
402 DCHECK(STRING_STARTS_WITH(format, "ss1")); /* ext size */
403 PrintSs1(instr);
404 return 3;
405 } else {
406 DCHECK(STRING_STARTS_WITH(format, "ss2")); /* ins size */
407 PrintSs2(instr);
408 return 3;
409 }
410 }
411 }
412 }
413 case 'b': { // 'bc - Special for bc1 cc field.
414 DCHECK(STRING_STARTS_WITH(format, "bc"));
415 PrintBc(instr);
416 return 2;
417 }
418 case 'C': { // 'Cc - Special for c.xx.d cc field.
419 DCHECK(STRING_STARTS_WITH(format, "Cc"));
420 PrintCc(instr);
421 return 2;
422 }
423 }
424 UNREACHABLE();
425 return -1;
426}
427
428
429// Format takes a formatting string for a whole instruction and prints it into
430// the output buffer. All escaped options are handed to FormatOption to be
431// parsed further.
432void Decoder::Format(Instruction* instr, const char* format) {
433 char cur = *format++;
434 while ((cur != 0) && (out_buffer_pos_ < (out_buffer_.length() - 1))) {
435 if (cur == '\'') { // Single quote is used as the formatting escape.
436 format += FormatOption(instr, format);
437 } else {
438 out_buffer_[out_buffer_pos_++] = cur;
439 }
440 cur = *format++;
441 }
442 out_buffer_[out_buffer_pos_] = '\0';
443}
444
445
446// For currently unimplemented decodings the disassembler calls Unknown(instr)
447// which will just print "unknown" of the instruction bits.
448void Decoder::Unknown(Instruction* instr) {
449 Format(instr, "unknown");
450}
451
452
453int Decoder::DecodeBreakInstr(Instruction* instr) {
454 // This is already known to be BREAK instr, just extract the code.
455 if (instr->Bits(25, 6) == static_cast<int>(kMaxStopCode)) {
456 // This is stop(msg).
457 Format(instr, "break, code: 'code");
458 out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
459 "\n%p %08lx stop msg: %s",
460 static_cast<void*>
461 (reinterpret_cast<int32_t*>(instr
462 + Instruction::kInstrSize)),
463 reinterpret_cast<uint64_t>
464 (*reinterpret_cast<char**>(instr
465 + Instruction::kInstrSize)),
466 *reinterpret_cast<char**>(instr
467 + Instruction::kInstrSize));
468 // Size 3: the break_ instr, plus embedded 64-bit char pointer.
469 return 3 * Instruction::kInstrSize;
470 } else {
471 Format(instr, "break, code: 'code");
472 return Instruction::kInstrSize;
473 }
474}
475
476
477int Decoder::DecodeTypeRegister(Instruction* instr) {
478 switch (instr->OpcodeFieldRaw()) {
479 case COP1: // Coprocessor instructions.
480 switch (instr->RsFieldRaw()) {
481 case MFC1:
482 Format(instr, "mfc1 'rt, 'fs");
483 break;
484 case DMFC1:
485 Format(instr, "dmfc1 'rt, 'fs");
486 break;
487 case MFHC1:
488 Format(instr, "mfhc1 'rt, 'fs");
489 break;
490 case MTC1:
491 Format(instr, "mtc1 'rt, 'fs");
492 break;
493 case DMTC1:
494 Format(instr, "dmtc1 'rt, 'fs");
495 break;
496 // These are called "fs" too, although they are not FPU registers.
497 case CTC1:
498 Format(instr, "ctc1 'rt, 'fs");
499 break;
500 case CFC1:
501 Format(instr, "cfc1 'rt, 'fs");
502 break;
503 case MTHC1:
504 Format(instr, "mthc1 'rt, 'fs");
505 break;
506 case D:
507 switch (instr->FunctionFieldRaw()) {
508 case ADD_D:
509 Format(instr, "add.d 'fd, 'fs, 'ft");
510 break;
511 case SUB_D:
512 Format(instr, "sub.d 'fd, 'fs, 'ft");
513 break;
514 case MUL_D:
515 Format(instr, "mul.d 'fd, 'fs, 'ft");
516 break;
517 case DIV_D:
518 Format(instr, "div.d 'fd, 'fs, 'ft");
519 break;
520 case ABS_D:
521 Format(instr, "abs.d 'fd, 'fs");
522 break;
523 case MOV_D:
524 Format(instr, "mov.d 'fd, 'fs");
525 break;
526 case NEG_D:
527 Format(instr, "neg.d 'fd, 'fs");
528 break;
529 case SQRT_D:
530 Format(instr, "sqrt.d 'fd, 'fs");
531 break;
532 case CVT_W_D:
533 Format(instr, "cvt.w.d 'fd, 'fs");
534 break;
535 case CVT_L_D:
536 Format(instr, "cvt.l.d 'fd, 'fs");
537 break;
538 case TRUNC_W_D:
539 Format(instr, "trunc.w.d 'fd, 'fs");
540 break;
541 case TRUNC_L_D:
542 Format(instr, "trunc.l.d 'fd, 'fs");
543 break;
544 case ROUND_W_D:
545 Format(instr, "round.w.d 'fd, 'fs");
546 break;
547 case ROUND_L_D:
548 Format(instr, "round.l.d 'fd, 'fs");
549 break;
550 case FLOOR_W_D:
551 Format(instr, "floor.w.d 'fd, 'fs");
552 break;
553 case FLOOR_L_D:
554 Format(instr, "floor.l.d 'fd, 'fs");
555 break;
556 case CEIL_W_D:
557 Format(instr, "ceil.w.d 'fd, 'fs");
558 break;
559 case CEIL_L_D:
560 Format(instr, "ceil.l.d 'fd, 'fs");
561 break;
562 case CVT_S_D:
563 Format(instr, "cvt.s.d 'fd, 'fs");
564 break;
565 case C_F_D:
566 Format(instr, "c.f.d 'fs, 'ft, 'Cc");
567 break;
568 case C_UN_D:
569 Format(instr, "c.un.d 'fs, 'ft, 'Cc");
570 break;
571 case C_EQ_D:
572 Format(instr, "c.eq.d 'fs, 'ft, 'Cc");
573 break;
574 case C_UEQ_D:
575 Format(instr, "c.ueq.d 'fs, 'ft, 'Cc");
576 break;
577 case C_OLT_D:
578 Format(instr, "c.olt.d 'fs, 'ft, 'Cc");
579 break;
580 case C_ULT_D:
581 Format(instr, "c.ult.d 'fs, 'ft, 'Cc");
582 break;
583 case C_OLE_D:
584 Format(instr, "c.ole.d 'fs, 'ft, 'Cc");
585 break;
586 case C_ULE_D:
587 Format(instr, "c.ule.d 'fs, 'ft, 'Cc");
588 break;
589 default:
590 Format(instr, "unknown.cop1.d");
591 break;
592 }
593 break;
594 case W:
595 switch (instr->FunctionFieldRaw()) {
596 case CVT_D_W: // Convert word to double.
597 Format(instr, "cvt.d.w 'fd, 'fs");
598 break;
599 default:
600 UNREACHABLE();
601 }
602 break;
603 case L:
604 switch (instr->FunctionFieldRaw()) {
605 case CVT_D_L:
606 Format(instr, "cvt.d.l 'fd, 'fs");
607 break;
608 case CVT_S_L:
609 Format(instr, "cvt.s.l 'fd, 'fs");
610 break;
611 case CMP_UN:
612 Format(instr, "cmp.un.d 'fd, 'fs, 'ft");
613 break;
614 case CMP_EQ:
615 Format(instr, "cmp.eq.d 'fd, 'fs, 'ft");
616 break;
617 case CMP_UEQ:
618 Format(instr, "cmp.ueq.d 'fd, 'fs, 'ft");
619 break;
620 case CMP_LT:
621 Format(instr, "cmp.lt.d 'fd, 'fs, 'ft");
622 break;
623 case CMP_ULT:
624 Format(instr, "cmp.ult.d 'fd, 'fs, 'ft");
625 break;
626 case CMP_LE:
627 Format(instr, "cmp.le.d 'fd, 'fs, 'ft");
628 break;
629 case CMP_ULE:
630 Format(instr, "cmp.ule.d 'fd, 'fs, 'ft");
631 break;
632 case CMP_OR:
633 Format(instr, "cmp.or.d 'fd, 'fs, 'ft");
634 break;
635 case CMP_UNE:
636 Format(instr, "cmp.une.d 'fd, 'fs, 'ft");
637 break;
638 case CMP_NE:
639 Format(instr, "cmp.ne.d 'fd, 'fs, 'ft");
640 break;
641 default:
642 UNREACHABLE();
643 }
644 break;
645 default:
646 UNREACHABLE();
647 }
648 break;
649 case COP1X:
650 switch (instr->FunctionFieldRaw()) {
651 case MADD_D:
652 Format(instr, "madd.d 'fd, 'fr, 'fs, 'ft");
653 break;
654 default:
655 UNREACHABLE();
656 }
657 break;
658 case SPECIAL:
659 switch (instr->FunctionFieldRaw()) {
660 case JR:
661 Format(instr, "jr 'rs");
662 break;
663 case JALR:
664 Format(instr, "jalr 'rs");
665 break;
666 case SLL:
667 if (0x0 == static_cast<int>(instr->InstructionBits()))
668 Format(instr, "nop");
669 else
670 Format(instr, "sll 'rd, 'rt, 'sa");
671 break;
672 case DSLL:
673 Format(instr, "dsll 'rd, 'rt, 'sa");
674 break;
675 case D_MUL_MUH: // Equals to DMUL.
676 if (kArchVariant != kMips64r6) {
677 Format(instr, "dmult 'rs, 'rt");
678 } else {
679 if (instr->SaValue() == MUL_OP) {
680 Format(instr, "dmul 'rd, 'rs, 'rt");
681 } else {
682 Format(instr, "dmuh 'rd, 'rs, 'rt");
683 }
684 }
685 break;
686 case DSLL32:
687 Format(instr, "dsll32 'rd, 'rt, 'sa");
688 break;
689 case SRL:
690 if (instr->RsValue() == 0) {
691 Format(instr, "srl 'rd, 'rt, 'sa");
692 } else {
693 if (kArchVariant == kMips64r2) {
694 Format(instr, "rotr 'rd, 'rt, 'sa");
695 } else {
696 Unknown(instr);
697 }
698 }
699 break;
700 case DSRL:
701 if (instr->RsValue() == 0) {
702 Format(instr, "dsrl 'rd, 'rt, 'sa");
703 } else {
704 if (kArchVariant == kMips64r2) {
705 Format(instr, "drotr 'rd, 'rt, 'sa");
706 } else {
707 Unknown(instr);
708 }
709 }
710 break;
711 case DSRL32:
712 Format(instr, "dsrl32 'rd, 'rt, 'sa");
713 break;
714 case SRA:
715 Format(instr, "sra 'rd, 'rt, 'sa");
716 break;
717 case DSRA:
718 Format(instr, "dsra 'rd, 'rt, 'sa");
719 break;
720 case DSRA32:
721 Format(instr, "dsra32 'rd, 'rt, 'sa");
722 break;
723 case SLLV:
724 Format(instr, "sllv 'rd, 'rt, 'rs");
725 break;
726 case DSLLV:
727 Format(instr, "dsllv 'rd, 'rt, 'rs");
728 break;
729 case SRLV:
730 if (instr->SaValue() == 0) {
731 Format(instr, "srlv 'rd, 'rt, 'rs");
732 } else {
733 if (kArchVariant == kMips64r2) {
734 Format(instr, "rotrv 'rd, 'rt, 'rs");
735 } else {
736 Unknown(instr);
737 }
738 }
739 break;
740 case DSRLV:
741 if (instr->SaValue() == 0) {
742 Format(instr, "dsrlv 'rd, 'rt, 'rs");
743 } else {
744 if (kArchVariant == kMips64r2) {
745 Format(instr, "drotrv 'rd, 'rt, 'rs");
746 } else {
747 Unknown(instr);
748 }
749 }
750 break;
751 case SRAV:
752 Format(instr, "srav 'rd, 'rt, 'rs");
753 break;
754 case DSRAV:
755 Format(instr, "dsrav 'rd, 'rt, 'rs");
756 break;
757 case MFHI:
758 if (instr->Bits(25, 16) == 0) {
759 Format(instr, "mfhi 'rd");
760 } else {
761 if ((instr->FunctionFieldRaw() == CLZ_R6)
762 && (instr->FdValue() == 1)) {
763 Format(instr, "clz 'rd, 'rs");
764 } else if ((instr->FunctionFieldRaw() == CLO_R6)
765 && (instr->FdValue() == 1)) {
766 Format(instr, "clo 'rd, 'rs");
767 }
768 }
769 break;
770 case MFLO:
771 Format(instr, "mflo 'rd");
772 break;
773 case D_MUL_MUH_U: // Equals to DMULTU.
774 if (kArchVariant != kMips64r6) {
775 Format(instr, "dmultu 'rs, 'rt");
776 } else {
777 if (instr->SaValue() == MUL_OP) {
778 Format(instr, "dmulu 'rd, 'rs, 'rt");
779 } else {
780 Format(instr, "dmuhu 'rd, 'rs, 'rt");
781 }
782 }
783 break;
784 case MULT: // @Mips64r6 == MUL_MUH.
785 if (kArchVariant != kMips64r6) {
786 Format(instr, "mult 'rs, 'rt");
787 } else {
788 if (instr->SaValue() == MUL_OP) {
789 Format(instr, "mul 'rd, 'rs, 'rt");
790 } else {
791 Format(instr, "muh 'rd, 'rs, 'rt");
792 }
793 }
794 break;
795 case MULTU: // @Mips64r6 == MUL_MUH_U.
796 if (kArchVariant != kMips64r6) {
797 Format(instr, "multu 'rs, 'rt");
798 } else {
799 if (instr->SaValue() == MUL_OP) {
800 Format(instr, "mulu 'rd, 'rs, 'rt");
801 } else {
802 Format(instr, "muhu 'rd, 'rs, 'rt");
803 }
804 }
805
806 break;
807 case DIV: // @Mips64r6 == DIV_MOD.
808 if (kArchVariant != kMips64r6) {
809 Format(instr, "div 'rs, 'rt");
810 } else {
811 if (instr->SaValue() == DIV_OP) {
812 Format(instr, "div 'rd, 'rs, 'rt");
813 } else {
814 Format(instr, "mod 'rd, 'rs, 'rt");
815 }
816 }
817 break;
818 case DDIV: // @Mips64r6 == D_DIV_MOD.
819 if (kArchVariant != kMips64r6) {
820 Format(instr, "ddiv 'rs, 'rt");
821 } else {
822 if (instr->SaValue() == DIV_OP) {
823 Format(instr, "ddiv 'rd, 'rs, 'rt");
824 } else {
825 Format(instr, "dmod 'rd, 'rs, 'rt");
826 }
827 }
828 break;
829 case DIVU: // @Mips64r6 == DIV_MOD_U.
830 if (kArchVariant != kMips64r6) {
831 Format(instr, "divu 'rs, 'rt");
832 } else {
833 if (instr->SaValue() == DIV_OP) {
834 Format(instr, "divu 'rd, 'rs, 'rt");
835 } else {
836 Format(instr, "modu 'rd, 'rs, 'rt");
837 }
838 }
839 break;
840 case DDIVU: // @Mips64r6 == D_DIV_MOD_U.
841 if (kArchVariant != kMips64r6) {
842 Format(instr, "ddivu 'rs, 'rt");
843 } else {
844 if (instr->SaValue() == DIV_OP) {
845 Format(instr, "ddivu 'rd, 'rs, 'rt");
846 } else {
847 Format(instr, "dmodu 'rd, 'rs, 'rt");
848 }
849 }
850 break;
851 case ADD:
852 Format(instr, "add 'rd, 'rs, 'rt");
853 break;
854 case DADD:
855 Format(instr, "dadd 'rd, 'rs, 'rt");
856 break;
857 case ADDU:
858 Format(instr, "addu 'rd, 'rs, 'rt");
859 break;
860 case DADDU:
861 Format(instr, "daddu 'rd, 'rs, 'rt");
862 break;
863 case SUB:
864 Format(instr, "sub 'rd, 'rs, 'rt");
865 break;
866 case DSUB:
867 Format(instr, "dsub 'rd, 'rs, 'rt");
868 break;
869 case SUBU:
870 Format(instr, "subu 'rd, 'rs, 'rt");
871 break;
872 case DSUBU:
873 Format(instr, "dsubu 'rd, 'rs, 'rt");
874 break;
875 case AND:
876 Format(instr, "and 'rd, 'rs, 'rt");
877 break;
878 case OR:
879 if (0 == instr->RsValue()) {
880 Format(instr, "mov 'rd, 'rt");
881 } else if (0 == instr->RtValue()) {
882 Format(instr, "mov 'rd, 'rs");
883 } else {
884 Format(instr, "or 'rd, 'rs, 'rt");
885 }
886 break;
887 case XOR:
888 Format(instr, "xor 'rd, 'rs, 'rt");
889 break;
890 case NOR:
891 Format(instr, "nor 'rd, 'rs, 'rt");
892 break;
893 case SLT:
894 Format(instr, "slt 'rd, 'rs, 'rt");
895 break;
896 case SLTU:
897 Format(instr, "sltu 'rd, 'rs, 'rt");
898 break;
899 case BREAK:
900 return DecodeBreakInstr(instr);
901 case TGE:
902 Format(instr, "tge 'rs, 'rt, code: 'code");
903 break;
904 case TGEU:
905 Format(instr, "tgeu 'rs, 'rt, code: 'code");
906 break;
907 case TLT:
908 Format(instr, "tlt 'rs, 'rt, code: 'code");
909 break;
910 case TLTU:
911 Format(instr, "tltu 'rs, 'rt, code: 'code");
912 break;
913 case TEQ:
914 Format(instr, "teq 'rs, 'rt, code: 'code");
915 break;
916 case TNE:
917 Format(instr, "tne 'rs, 'rt, code: 'code");
918 break;
919 case MOVZ:
920 Format(instr, "movz 'rd, 'rs, 'rt");
921 break;
922 case MOVN:
923 Format(instr, "movn 'rd, 'rs, 'rt");
924 break;
925 case MOVCI:
926 if (instr->Bit(16)) {
927 Format(instr, "movt 'rd, 'rs, 'bc");
928 } else {
929 Format(instr, "movf 'rd, 'rs, 'bc");
930 }
931 break;
932 case SELEQZ_S:
933 Format(instr, "seleqz 'rd, 'rs, 'rt");
934 break;
935 case SELNEZ_S:
936 Format(instr, "selnez 'rd, 'rs, 'rt");
937 break;
938 default:
939 UNREACHABLE();
940 }
941 break;
942 case SPECIAL2:
943 switch (instr->FunctionFieldRaw()) {
944 case MUL:
945 Format(instr, "mul 'rd, 'rs, 'rt");
946 break;
947 case CLZ:
948 if (kArchVariant != kMips64r6) {
949 Format(instr, "clz 'rd, 'rs");
950 }
951 break;
952 default:
953 UNREACHABLE();
954 }
955 break;
956 case SPECIAL3:
957 switch (instr->FunctionFieldRaw()) {
958 case INS: {
959 Format(instr, "ins 'rt, 'rs, 'sa, 'ss2");
960 break;
961 }
962 case EXT: {
963 Format(instr, "ext 'rt, 'rs, 'sa, 'ss1");
964 break;
965 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400966 case DEXT: {
967 Format(instr, "dext 'rt, 'rs, 'sa, 'ss1");
968 break;
969 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000970 default:
971 UNREACHABLE();
972 }
973 break;
974 default:
975 UNREACHABLE();
976 }
977 return Instruction::kInstrSize;
978}
979
980
981void Decoder::DecodeTypeImmediate(Instruction* instr) {
982 switch (instr->OpcodeFieldRaw()) {
983 case COP1:
984 switch (instr->RsFieldRaw()) {
985 case BC1:
986 if (instr->FBtrueValue()) {
987 Format(instr, "bc1t 'bc, 'imm16u");
988 } else {
989 Format(instr, "bc1f 'bc, 'imm16u");
990 }
991 break;
992 case BC1EQZ:
993 Format(instr, "bc1eqz 'ft, 'imm16u");
994 break;
995 case BC1NEZ:
996 Format(instr, "bc1nez 'ft, 'imm16u");
997 break;
998 case W: // CMP.S instruction.
999 switch (instr->FunctionValue()) {
1000 case CMP_AF:
1001 Format(instr, "cmp.af.S 'ft, 'fs, 'fd");
1002 break;
1003 case CMP_UN:
1004 Format(instr, "cmp.un.S 'ft, 'fs, 'fd");
1005 break;
1006 case CMP_EQ:
1007 Format(instr, "cmp.eq.S 'ft, 'fs, 'fd");
1008 break;
1009 case CMP_UEQ:
1010 Format(instr, "cmp.ueq.S 'ft, 'fs, 'fd");
1011 break;
1012 case CMP_LT:
1013 Format(instr, "cmp.lt.S 'ft, 'fs, 'fd");
1014 break;
1015 case CMP_ULT:
1016 Format(instr, "cmp.ult.S 'ft, 'fs, 'fd");
1017 break;
1018 case CMP_LE:
1019 Format(instr, "cmp.le.S 'ft, 'fs, 'fd");
1020 break;
1021 case CMP_ULE:
1022 Format(instr, "cmp.ule.S 'ft, 'fs, 'fd");
1023 break;
1024 case CMP_OR:
1025 Format(instr, "cmp.or.S 'ft, 'fs, 'fd");
1026 break;
1027 case CMP_UNE:
1028 Format(instr, "cmp.une.S 'ft, 'fs, 'fd");
1029 break;
1030 case CMP_NE:
1031 Format(instr, "cmp.ne.S 'ft, 'fs, 'fd");
1032 break;
1033 default:
1034 UNREACHABLE();
1035 }
1036 break;
1037 case L: // CMP.D instruction.
1038 switch (instr->FunctionValue()) {
1039 case CMP_AF:
1040 Format(instr, "cmp.af.D 'ft, 'fs, 'fd");
1041 break;
1042 case CMP_UN:
1043 Format(instr, "cmp.un.D 'ft, 'fs, 'fd");
1044 break;
1045 case CMP_EQ:
1046 Format(instr, "cmp.eq.D 'ft, 'fs, 'fd");
1047 break;
1048 case CMP_UEQ:
1049 Format(instr, "cmp.ueq.D 'ft, 'fs, 'fd");
1050 break;
1051 case CMP_LT:
1052 Format(instr, "cmp.lt.D 'ft, 'fs, 'fd");
1053 break;
1054 case CMP_ULT:
1055 Format(instr, "cmp.ult.D 'ft, 'fs, 'fd");
1056 break;
1057 case CMP_LE:
1058 Format(instr, "cmp.le.D 'ft, 'fs, 'fd");
1059 break;
1060 case CMP_ULE:
1061 Format(instr, "cmp.ule.D 'ft, 'fs, 'fd");
1062 break;
1063 case CMP_OR:
1064 Format(instr, "cmp.or.D 'ft, 'fs, 'fd");
1065 break;
1066 case CMP_UNE:
1067 Format(instr, "cmp.une.D 'ft, 'fs, 'fd");
1068 break;
1069 case CMP_NE:
1070 Format(instr, "cmp.ne.D 'ft, 'fs, 'fd");
1071 break;
1072 default:
1073 UNREACHABLE();
1074 }
1075 break;
1076 case S:
1077 switch (instr->FunctionValue()) {
1078 case SEL:
1079 Format(instr, "sel.S 'ft, 'fs, 'fd");
1080 break;
1081 case SELEQZ_C:
1082 Format(instr, "seleqz.S 'ft, 'fs, 'fd");
1083 break;
1084 case SELNEZ_C:
1085 Format(instr, "selnez.S 'ft, 'fs, 'fd");
1086 break;
1087 case MIN:
1088 Format(instr, "min.S 'ft, 'fs, 'fd");
1089 break;
1090 case MINA:
1091 Format(instr, "mina.S 'ft, 'fs, 'fd");
1092 break;
1093 case MAX:
1094 Format(instr, "max.S 'ft, 'fs, 'fd");
1095 break;
1096 case MAXA:
1097 Format(instr, "maxa.S 'ft, 'fs, 'fd");
1098 break;
1099 default:
1100 UNREACHABLE();
1101 }
1102 break;
1103 case D:
1104 switch (instr->FunctionValue()) {
1105 case SEL:
1106 Format(instr, "sel.D 'ft, 'fs, 'fd");
1107 break;
1108 case SELEQZ_C:
1109 Format(instr, "seleqz.D 'ft, 'fs, 'fd");
1110 break;
1111 case SELNEZ_C:
1112 Format(instr, "selnez.D 'ft, 'fs, 'fd");
1113 break;
1114 case MIN:
1115 Format(instr, "min.D 'ft, 'fs, 'fd");
1116 break;
1117 case MINA:
1118 Format(instr, "mina.D 'ft, 'fs, 'fd");
1119 break;
1120 case MAX:
1121 Format(instr, "max.D 'ft, 'fs, 'fd");
1122 break;
1123 case MAXA:
1124 Format(instr, "maxa.D 'ft, 'fs, 'fd");
1125 break;
1126 default:
1127 UNREACHABLE();
1128 }
1129 break;
1130 default:
1131 UNREACHABLE();
1132 }
1133
1134 break; // Case COP1.
1135 // ------------- REGIMM class.
1136 case REGIMM:
1137 switch (instr->RtFieldRaw()) {
1138 case BLTZ:
1139 Format(instr, "bltz 'rs, 'imm16u");
1140 break;
1141 case BLTZAL:
1142 Format(instr, "bltzal 'rs, 'imm16u");
1143 break;
1144 case BGEZ:
1145 Format(instr, "bgez 'rs, 'imm16u");
1146 break;
1147 case BGEZAL:
1148 Format(instr, "bgezal 'rs, 'imm16u");
1149 break;
1150 case BGEZALL:
1151 Format(instr, "bgezall 'rs, 'imm16u");
1152 break;
1153 case DAHI:
1154 Format(instr, "dahi 'rs, 'imm16u");
1155 break;
1156 case DATI:
1157 Format(instr, "dati 'rs, 'imm16u");
1158 break;
1159 default:
1160 UNREACHABLE();
1161 }
1162 break; // Case REGIMM.
1163 // ------------- Branch instructions.
1164 case BEQ:
1165 Format(instr, "beq 'rs, 'rt, 'imm16u");
1166 break;
1167 case BNE:
1168 Format(instr, "bne 'rs, 'rt, 'imm16u");
1169 break;
1170 case BLEZ:
1171 if ((instr->RtFieldRaw() == 0)
1172 && (instr->RsFieldRaw() != 0)) {
1173 Format(instr, "blez 'rs, 'imm16u");
1174 } else if ((instr->RtFieldRaw() != instr->RsFieldRaw())
1175 && (instr->RsFieldRaw() != 0) && (instr->RtFieldRaw() != 0)) {
1176 Format(instr, "bgeuc 'rs, 'rt, 'imm16u");
1177 } else if ((instr->RtFieldRaw() == instr->RsFieldRaw())
1178 && (instr->RtFieldRaw() != 0)) {
1179 Format(instr, "bgezalc 'rs, 'imm16u");
1180 } else if ((instr->RsFieldRaw() == 0)
1181 && (instr->RtFieldRaw() != 0)) {
1182 Format(instr, "blezalc 'rs, 'imm16u");
1183 } else {
1184 UNREACHABLE();
1185 }
1186 break;
1187 case BGTZ:
1188 if ((instr->RtFieldRaw() == 0)
1189 && (instr->RsFieldRaw() != 0)) {
1190 Format(instr, "bgtz 'rs, 'imm16u");
1191 } else if ((instr->RtFieldRaw() != instr->RsFieldRaw())
1192 && (instr->RsFieldRaw() != 0) && (instr->RtFieldRaw() != 0)) {
1193 Format(instr, "bltuc 'rs, 'rt, 'imm16u");
1194 } else if ((instr->RtFieldRaw() == instr->RsFieldRaw())
1195 && (instr->RtFieldRaw() != 0)) {
1196 Format(instr, "bltzalc 'rt, 'imm16u");
1197 } else if ((instr->RsFieldRaw() == 0)
1198 && (instr->RtFieldRaw() != 0)) {
1199 Format(instr, "bgtzalc 'rt, 'imm16u");
1200 } else {
1201 UNREACHABLE();
1202 }
1203 break;
1204 case BLEZL:
1205 if ((instr->RtFieldRaw() == instr->RsFieldRaw())
1206 && (instr->RtFieldRaw() != 0)) {
1207 Format(instr, "bgezc 'rt, 'imm16u");
1208 } else if ((instr->RtFieldRaw() != instr->RsFieldRaw())
1209 && (instr->RsFieldRaw() != 0) && (instr->RtFieldRaw() != 0)) {
1210 Format(instr, "bgec 'rs, 'rt, 'imm16u");
1211 } else if ((instr->RsFieldRaw() == 0)
1212 && (instr->RtFieldRaw() != 0)) {
1213 Format(instr, "blezc 'rt, 'imm16u");
1214 } else {
1215 UNREACHABLE();
1216 }
1217 break;
1218 case BGTZL:
1219 if ((instr->RtFieldRaw() == instr->RsFieldRaw())
1220 && (instr->RtFieldRaw() != 0)) {
1221 Format(instr, "bltzc 'rt, 'imm16u");
1222 } else if ((instr->RtFieldRaw() != instr->RsFieldRaw())
1223 && (instr->RsFieldRaw() != 0) && (instr->RtFieldRaw() != 0)) {
1224 Format(instr, "bltc 'rs, 'rt, 'imm16u");
1225 } else if ((instr->RsFieldRaw() == 0)
1226 && (instr->RtFieldRaw() != 0)) {
1227 Format(instr, "bgtzc 'rt, 'imm16u");
1228 } else {
1229 UNREACHABLE();
1230 }
1231 break;
1232 case BEQZC:
1233 if (instr->RsFieldRaw() != 0) {
1234 Format(instr, "beqzc 'rs, 'imm21x");
1235 }
1236 break;
1237 case BNEZC:
1238 if (instr->RsFieldRaw() != 0) {
1239 Format(instr, "bnezc 'rs, 'imm21x");
1240 }
1241 break;
1242 // ------------- Arithmetic instructions.
1243 case ADDI:
1244 if (kArchVariant != kMips64r6) {
1245 Format(instr, "addi 'rt, 'rs, 'imm16s");
1246 } else {
1247 // Check if BOVC or BEQC instruction.
1248 if (instr->RsFieldRaw() >= instr->RtFieldRaw()) {
1249 Format(instr, "bovc 'rs, 'rt, 'imm16s");
1250 } else if (instr->RsFieldRaw() < instr->RtFieldRaw()) {
1251 Format(instr, "beqc 'rs, 'rt, 'imm16s");
1252 } else {
1253 UNREACHABLE();
1254 }
1255 }
1256 break;
1257 case DADDI:
1258 if (kArchVariant != kMips64r6) {
1259 Format(instr, "daddi 'rt, 'rs, 'imm16s");
1260 } else {
1261 // Check if BNVC or BNEC instruction.
1262 if (instr->RsFieldRaw() >= instr->RtFieldRaw()) {
1263 Format(instr, "bnvc 'rs, 'rt, 'imm16s");
1264 } else if (instr->RsFieldRaw() < instr->RtFieldRaw()) {
1265 Format(instr, "bnec 'rs, 'rt, 'imm16s");
1266 } else {
1267 UNREACHABLE();
1268 }
1269 }
1270 break;
1271 case ADDIU:
1272 Format(instr, "addiu 'rt, 'rs, 'imm16s");
1273 break;
1274 case DADDIU:
1275 Format(instr, "daddiu 'rt, 'rs, 'imm16s");
1276 break;
1277 case SLTI:
1278 Format(instr, "slti 'rt, 'rs, 'imm16s");
1279 break;
1280 case SLTIU:
1281 Format(instr, "sltiu 'rt, 'rs, 'imm16u");
1282 break;
1283 case ANDI:
1284 Format(instr, "andi 'rt, 'rs, 'imm16x");
1285 break;
1286 case ORI:
1287 Format(instr, "ori 'rt, 'rs, 'imm16x");
1288 break;
1289 case XORI:
1290 Format(instr, "xori 'rt, 'rs, 'imm16x");
1291 break;
1292 case LUI:
1293 if (kArchVariant != kMips64r6) {
1294 Format(instr, "lui 'rt, 'imm16x");
1295 } else {
1296 if (instr->RsValue() != 0) {
1297 Format(instr, "aui 'rt, 'imm16x");
1298 } else {
1299 Format(instr, "lui 'rt, 'imm16x");
1300 }
1301 }
1302 break;
1303 case DAUI:
1304 Format(instr, "daui 'rt, 'imm16x");
1305 break;
1306 // ------------- Memory instructions.
1307 case LB:
1308 Format(instr, "lb 'rt, 'imm16s('rs)");
1309 break;
1310 case LH:
1311 Format(instr, "lh 'rt, 'imm16s('rs)");
1312 break;
1313 case LWL:
1314 Format(instr, "lwl 'rt, 'imm16s('rs)");
1315 break;
1316 case LDL:
1317 Format(instr, "ldl 'rt, 'imm16s('rs)");
1318 break;
1319 case LW:
1320 Format(instr, "lw 'rt, 'imm16s('rs)");
1321 break;
1322 case LWU:
1323 Format(instr, "lwu 'rt, 'imm16s('rs)");
1324 break;
1325 case LD:
1326 Format(instr, "ld 'rt, 'imm16s('rs)");
1327 break;
1328 case LBU:
1329 Format(instr, "lbu 'rt, 'imm16s('rs)");
1330 break;
1331 case LHU:
1332 Format(instr, "lhu 'rt, 'imm16s('rs)");
1333 break;
1334 case LWR:
1335 Format(instr, "lwr 'rt, 'imm16s('rs)");
1336 break;
1337 case LDR:
1338 Format(instr, "ldr 'rt, 'imm16s('rs)");
1339 break;
1340 case PREF:
1341 Format(instr, "pref 'rt, 'imm16s('rs)");
1342 break;
1343 case SB:
1344 Format(instr, "sb 'rt, 'imm16s('rs)");
1345 break;
1346 case SH:
1347 Format(instr, "sh 'rt, 'imm16s('rs)");
1348 break;
1349 case SWL:
1350 Format(instr, "swl 'rt, 'imm16s('rs)");
1351 break;
1352 case SW:
1353 Format(instr, "sw 'rt, 'imm16s('rs)");
1354 break;
1355 case SD:
1356 Format(instr, "sd 'rt, 'imm16s('rs)");
1357 break;
1358 case SWR:
1359 Format(instr, "swr 'rt, 'imm16s('rs)");
1360 break;
1361 case LWC1:
1362 Format(instr, "lwc1 'ft, 'imm16s('rs)");
1363 break;
1364 case LDC1:
1365 Format(instr, "ldc1 'ft, 'imm16s('rs)");
1366 break;
1367 case SWC1:
1368 Format(instr, "swc1 'ft, 'imm16s('rs)");
1369 break;
1370 case SDC1:
1371 Format(instr, "sdc1 'ft, 'imm16s('rs)");
1372 break;
1373 default:
1374 printf("a 0x%x \n", instr->OpcodeFieldRaw());
1375 UNREACHABLE();
1376 break;
1377 }
1378}
1379
1380
1381void Decoder::DecodeTypeJump(Instruction* instr) {
1382 switch (instr->OpcodeFieldRaw()) {
1383 case J:
1384 Format(instr, "j 'imm26x");
1385 break;
1386 case JAL:
1387 Format(instr, "jal 'imm26x");
1388 break;
1389 default:
1390 UNREACHABLE();
1391 }
1392}
1393
1394
1395// Disassemble the instruction at *instr_ptr into the output buffer.
1396// All instructions are one word long, except for the simulator
1397// psuedo-instruction stop(msg). For that one special case, we return
1398// size larger than one kInstrSize.
1399int Decoder::InstructionDecode(byte* instr_ptr) {
1400 Instruction* instr = Instruction::At(instr_ptr);
1401 // Print raw instruction bytes.
1402 out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
1403 "%08x ",
1404 instr->InstructionBits());
1405 switch (instr->InstructionType()) {
1406 case Instruction::kRegisterType: {
1407 return DecodeTypeRegister(instr);
1408 }
1409 case Instruction::kImmediateType: {
1410 DecodeTypeImmediate(instr);
1411 break;
1412 }
1413 case Instruction::kJumpType: {
1414 DecodeTypeJump(instr);
1415 break;
1416 }
1417 default: {
1418 Format(instr, "UNSUPPORTED");
1419 UNSUPPORTED_MIPS();
1420 }
1421 }
1422 return Instruction::kInstrSize;
1423}
1424
1425
1426} } // namespace v8::internal
1427
1428
1429
1430//------------------------------------------------------------------------------
1431
1432namespace disasm {
1433
1434const char* NameConverter::NameOfAddress(byte* addr) const {
1435 v8::internal::SNPrintF(tmp_buffer_, "%p", addr);
1436 return tmp_buffer_.start();
1437}
1438
1439
1440const char* NameConverter::NameOfConstant(byte* addr) const {
1441 return NameOfAddress(addr);
1442}
1443
1444
1445const char* NameConverter::NameOfCPURegister(int reg) const {
1446 return v8::internal::Registers::Name(reg);
1447}
1448
1449
1450const char* NameConverter::NameOfXMMRegister(int reg) const {
1451 return v8::internal::FPURegisters::Name(reg);
1452}
1453
1454
1455const char* NameConverter::NameOfByteCPURegister(int reg) const {
1456 UNREACHABLE(); // MIPS does not have the concept of a byte register.
1457 return "nobytereg";
1458}
1459
1460
1461const char* NameConverter::NameInCode(byte* addr) const {
1462 // The default name converter is called for unknown code. So we will not try
1463 // to access any memory.
1464 return "";
1465}
1466
1467
1468//------------------------------------------------------------------------------
1469
1470Disassembler::Disassembler(const NameConverter& converter)
1471 : converter_(converter) {}
1472
1473
1474Disassembler::~Disassembler() {}
1475
1476
1477int Disassembler::InstructionDecode(v8::internal::Vector<char> buffer,
1478 byte* instruction) {
1479 v8::internal::Decoder d(converter_, buffer);
1480 return d.InstructionDecode(instruction);
1481}
1482
1483
1484// The MIPS assembler does not currently use constant pools.
1485int Disassembler::ConstantPoolSizeAt(byte* instruction) {
1486 return -1;
1487}
1488
1489
1490void Disassembler::Disassemble(FILE* f, byte* begin, byte* end) {
1491 NameConverter converter;
1492 Disassembler d(converter);
1493 for (byte* pc = begin; pc < end;) {
1494 v8::internal::EmbeddedVector<char, 128> buffer;
1495 buffer[0] = '\0';
1496 byte* prev_pc = pc;
1497 pc += d.InstructionDecode(buffer, pc);
1498 v8::internal::PrintF(f, "%p %08x %s\n",
1499 prev_pc, *reinterpret_cast<int32_t*>(prev_pc), buffer.start());
1500 }
1501}
1502
1503
1504#undef UNSUPPORTED
1505
1506} // namespace disasm
1507
1508#endif // V8_TARGET_ARCH_MIPS64