deps: update v8 to 4.3.61.21
[platform/upstream/nodejs.git] / deps / v8 / src / arm / disasm-arm.cc
1 // Copyright 2011 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_ARM
34
35 #include "src/arm/constants-arm.h"
36 #include "src/base/bits.h"
37 #include "src/base/platform/platform.h"
38 #include "src/disasm.h"
39 #include "src/macro-assembler.h"
40
41
42 namespace v8 {
43 namespace internal {
44
45
46 //------------------------------------------------------------------------------
47
48 // Decoder decodes and disassembles instructions into an output buffer.
49 // It uses the converter to convert register names and call destinations into
50 // more informative description.
51 class Decoder {
52  public:
53   Decoder(const disasm::NameConverter& converter,
54           Vector<char> out_buffer)
55     : converter_(converter),
56       out_buffer_(out_buffer),
57       out_buffer_pos_(0) {
58     out_buffer_[out_buffer_pos_] = '\0';
59   }
60
61   ~Decoder() {}
62
63   // Writes one disassembled instruction into 'buffer' (0-terminated).
64   // Returns the length of the disassembled machine instruction in bytes.
65   int InstructionDecode(byte* instruction);
66
67   static bool IsConstantPoolAt(byte* instr_ptr);
68   static int ConstantPoolSizeAt(byte* instr_ptr);
69
70  private:
71   // Bottleneck functions to print into the out_buffer.
72   void PrintChar(const char ch);
73   void Print(const char* str);
74
75   // Printing of common values.
76   void PrintRegister(int reg);
77   void PrintSRegister(int reg);
78   void PrintDRegister(int reg);
79   int FormatVFPRegister(Instruction* instr, const char* format);
80   void PrintMovwMovt(Instruction* instr);
81   int FormatVFPinstruction(Instruction* instr, const char* format);
82   void PrintCondition(Instruction* instr);
83   void PrintShiftRm(Instruction* instr);
84   void PrintShiftImm(Instruction* instr);
85   void PrintShiftSat(Instruction* instr);
86   void PrintPU(Instruction* instr);
87   void PrintSoftwareInterrupt(SoftwareInterruptCodes svc);
88
89   // Handle formatting of instructions and their options.
90   int FormatRegister(Instruction* instr, const char* option);
91   void FormatNeonList(int Vd, int type);
92   void FormatNeonMemory(int Rn, int align, int Rm);
93   int FormatOption(Instruction* instr, const char* option);
94   void Format(Instruction* instr, const char* format);
95   void Unknown(Instruction* instr);
96
97   // Each of these functions decodes one particular instruction type, a 3-bit
98   // field in the instruction encoding.
99   // Types 0 and 1 are combined as they are largely the same except for the way
100   // they interpret the shifter operand.
101   void DecodeType01(Instruction* instr);
102   void DecodeType2(Instruction* instr);
103   void DecodeType3(Instruction* instr);
104   void DecodeType4(Instruction* instr);
105   void DecodeType5(Instruction* instr);
106   void DecodeType6(Instruction* instr);
107   // Type 7 includes special Debugger instructions.
108   int DecodeType7(Instruction* instr);
109   // For VFP support.
110   void DecodeTypeVFP(Instruction* instr);
111   void DecodeType6CoprocessorIns(Instruction* instr);
112
113   void DecodeSpecialCondition(Instruction* instr);
114
115   void DecodeVMOVBetweenCoreAndSinglePrecisionRegisters(Instruction* instr);
116   void DecodeVCMP(Instruction* instr);
117   void DecodeVCVTBetweenDoubleAndSingle(Instruction* instr);
118   void DecodeVCVTBetweenFloatingPointAndInteger(Instruction* instr);
119
120   const disasm::NameConverter& converter_;
121   Vector<char> out_buffer_;
122   int out_buffer_pos_;
123
124   DISALLOW_COPY_AND_ASSIGN(Decoder);
125 };
126
127
128 // Support for assertions in the Decoder formatting functions.
129 #define STRING_STARTS_WITH(string, compare_string) \
130   (strncmp(string, compare_string, strlen(compare_string)) == 0)
131
132
133 // Append the ch to the output buffer.
134 void Decoder::PrintChar(const char ch) {
135   out_buffer_[out_buffer_pos_++] = ch;
136 }
137
138
139 // Append the str to the output buffer.
140 void Decoder::Print(const char* str) {
141   char cur = *str++;
142   while (cur != '\0' && (out_buffer_pos_ < (out_buffer_.length() - 1))) {
143     PrintChar(cur);
144     cur = *str++;
145   }
146   out_buffer_[out_buffer_pos_] = 0;
147 }
148
149
150 // These condition names are defined in a way to match the native disassembler
151 // formatting. See for example the command "objdump -d <binary file>".
152 static const char* const cond_names[kNumberOfConditions] = {
153   "eq", "ne", "cs" , "cc" , "mi" , "pl" , "vs" , "vc" ,
154   "hi", "ls", "ge", "lt", "gt", "le", "", "invalid",
155 };
156
157
158 // Print the condition guarding the instruction.
159 void Decoder::PrintCondition(Instruction* instr) {
160   Print(cond_names[instr->ConditionValue()]);
161 }
162
163
164 // Print the register name according to the active name converter.
165 void Decoder::PrintRegister(int reg) {
166   Print(converter_.NameOfCPURegister(reg));
167 }
168
169
170 // Print the VFP S register name according to the active name converter.
171 void Decoder::PrintSRegister(int reg) {
172   Print(VFPRegisters::Name(reg, false));
173 }
174
175
176 // Print the VFP D register name according to the active name converter.
177 void Decoder::PrintDRegister(int reg) {
178   Print(VFPRegisters::Name(reg, true));
179 }
180
181
182 // These shift names are defined in a way to match the native disassembler
183 // formatting. See for example the command "objdump -d <binary file>".
184 static const char* const shift_names[kNumberOfShifts] = {
185   "lsl", "lsr", "asr", "ror"
186 };
187
188
189 // Print the register shift operands for the instruction. Generally used for
190 // data processing instructions.
191 void Decoder::PrintShiftRm(Instruction* instr) {
192   ShiftOp shift = instr->ShiftField();
193   int shift_index = instr->ShiftValue();
194   int shift_amount = instr->ShiftAmountValue();
195   int rm = instr->RmValue();
196
197   PrintRegister(rm);
198
199   if ((instr->RegShiftValue() == 0) && (shift == LSL) && (shift_amount == 0)) {
200     // Special case for using rm only.
201     return;
202   }
203   if (instr->RegShiftValue() == 0) {
204     // by immediate
205     if ((shift == ROR) && (shift_amount == 0)) {
206       Print(", RRX");
207       return;
208     } else if (((shift == LSR) || (shift == ASR)) && (shift_amount == 0)) {
209       shift_amount = 32;
210     }
211     out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
212                                 ", %s #%d",
213                                 shift_names[shift_index],
214                                 shift_amount);
215   } else {
216     // by register
217     int rs = instr->RsValue();
218     out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
219                                 ", %s ", shift_names[shift_index]);
220     PrintRegister(rs);
221   }
222 }
223
224
225 // Print the immediate operand for the instruction. Generally used for data
226 // processing instructions.
227 void Decoder::PrintShiftImm(Instruction* instr) {
228   int rotate = instr->RotateValue() * 2;
229   int immed8 = instr->Immed8Value();
230   int imm = base::bits::RotateRight32(immed8, rotate);
231   out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_, "#%d", imm);
232 }
233
234
235 // Print the optional shift and immediate used by saturating instructions.
236 void Decoder::PrintShiftSat(Instruction* instr) {
237   int shift = instr->Bits(11, 7);
238   if (shift > 0) {
239     out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
240                                 ", %s #%d",
241                                 shift_names[instr->Bit(6) * 2],
242                                 instr->Bits(11, 7));
243   }
244 }
245
246
247 // Print PU formatting to reduce complexity of FormatOption.
248 void Decoder::PrintPU(Instruction* instr) {
249   switch (instr->PUField()) {
250     case da_x: {
251       Print("da");
252       break;
253     }
254     case ia_x: {
255       Print("ia");
256       break;
257     }
258     case db_x: {
259       Print("db");
260       break;
261     }
262     case ib_x: {
263       Print("ib");
264       break;
265     }
266     default: {
267       UNREACHABLE();
268       break;
269     }
270   }
271 }
272
273
274 // Print SoftwareInterrupt codes. Factoring this out reduces the complexity of
275 // the FormatOption method.
276 void Decoder::PrintSoftwareInterrupt(SoftwareInterruptCodes svc) {
277   switch (svc) {
278     case kCallRtRedirected:
279       Print("call rt redirected");
280       return;
281     case kBreakpoint:
282       Print("breakpoint");
283       return;
284     default:
285       if (svc >= kStopCode) {
286         out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
287                                     "%d - 0x%x",
288                                     svc & kStopCodeMask,
289                                     svc & kStopCodeMask);
290       } else {
291         out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
292                                     "%d",
293                                     svc);
294       }
295       return;
296   }
297 }
298
299
300 // Handle all register based formatting in this function to reduce the
301 // complexity of FormatOption.
302 int Decoder::FormatRegister(Instruction* instr, const char* format) {
303   DCHECK(format[0] == 'r');
304   if (format[1] == 'n') {  // 'rn: Rn register
305     int reg = instr->RnValue();
306     PrintRegister(reg);
307     return 2;
308   } else if (format[1] == 'd') {  // 'rd: Rd register
309     int reg = instr->RdValue();
310     PrintRegister(reg);
311     return 2;
312   } else if (format[1] == 's') {  // 'rs: Rs register
313     int reg = instr->RsValue();
314     PrintRegister(reg);
315     return 2;
316   } else if (format[1] == 'm') {  // 'rm: Rm register
317     int reg = instr->RmValue();
318     PrintRegister(reg);
319     return 2;
320   } else if (format[1] == 't') {  // 'rt: Rt register
321     int reg = instr->RtValue();
322     PrintRegister(reg);
323     return 2;
324   } else if (format[1] == 'l') {
325     // 'rlist: register list for load and store multiple instructions
326     DCHECK(STRING_STARTS_WITH(format, "rlist"));
327     int rlist = instr->RlistValue();
328     int reg = 0;
329     Print("{");
330     // Print register list in ascending order, by scanning the bit mask.
331     while (rlist != 0) {
332       if ((rlist & 1) != 0) {
333         PrintRegister(reg);
334         if ((rlist >> 1) != 0) {
335           Print(", ");
336         }
337       }
338       reg++;
339       rlist >>= 1;
340     }
341     Print("}");
342     return 5;
343   }
344   UNREACHABLE();
345   return -1;
346 }
347
348
349 // Handle all VFP register based formatting in this function to reduce the
350 // complexity of FormatOption.
351 int Decoder::FormatVFPRegister(Instruction* instr, const char* format) {
352   DCHECK((format[0] == 'S') || (format[0] == 'D'));
353
354   VFPRegPrecision precision =
355       format[0] == 'D' ? kDoublePrecision : kSinglePrecision;
356
357   int retval = 2;
358   int reg = -1;
359   if (format[1] == 'n') {
360     reg = instr->VFPNRegValue(precision);
361   } else if (format[1] == 'm') {
362     reg = instr->VFPMRegValue(precision);
363   } else if (format[1] == 'd') {
364     if ((instr->TypeValue() == 7) &&
365         (instr->Bit(24) == 0x0) &&
366         (instr->Bits(11, 9) == 0x5) &&
367         (instr->Bit(4) == 0x1)) {
368       // vmov.32 has Vd in a different place.
369       reg = instr->Bits(19, 16) | (instr->Bit(7) << 4);
370     } else {
371       reg = instr->VFPDRegValue(precision);
372     }
373
374     if (format[2] == '+') {
375       int immed8 = instr->Immed8Value();
376       if (format[0] == 'S') reg += immed8 - 1;
377       if (format[0] == 'D') reg += (immed8 / 2 - 1);
378     }
379     if (format[2] == '+') retval = 3;
380   } else {
381     UNREACHABLE();
382   }
383
384   if (precision == kSinglePrecision) {
385     PrintSRegister(reg);
386   } else {
387     PrintDRegister(reg);
388   }
389
390   return retval;
391 }
392
393
394 int Decoder::FormatVFPinstruction(Instruction* instr, const char* format) {
395     Print(format);
396     return 0;
397 }
398
399
400 void Decoder::FormatNeonList(int Vd, int type) {
401   if (type == nlt_1) {
402     out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
403                                 "{d%d}", Vd);
404   } else if (type == nlt_2) {
405     out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
406                                 "{d%d, d%d}", Vd, Vd + 1);
407   } else if (type == nlt_3) {
408     out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
409                                 "{d%d, d%d, d%d}", Vd, Vd + 1, Vd + 2);
410   } else if (type == nlt_4) {
411     out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
412                         "{d%d, d%d, d%d, d%d}", Vd, Vd + 1, Vd + 2, Vd + 3);
413   }
414 }
415
416
417 void Decoder::FormatNeonMemory(int Rn, int align, int Rm) {
418   out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
419                               "[r%d", Rn);
420   if (align != 0) {
421     out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
422                                 ":%d", (1 << align) << 6);
423   }
424   if (Rm == 15) {
425     Print("]");
426   } else if (Rm == 13) {
427     Print("]!");
428   } else {
429     out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
430                                 "], r%d", Rm);
431   }
432 }
433
434
435 // Print the movw or movt instruction.
436 void Decoder::PrintMovwMovt(Instruction* instr) {
437   int imm = instr->ImmedMovwMovtValue();
438   int rd = instr->RdValue();
439   PrintRegister(rd);
440   out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_, ", #%d", imm);
441 }
442
443
444 // FormatOption takes a formatting string and interprets it based on
445 // the current instructions. The format string points to the first
446 // character of the option string (the option escape has already been
447 // consumed by the caller.)  FormatOption returns the number of
448 // characters that were consumed from the formatting string.
449 int Decoder::FormatOption(Instruction* instr, const char* format) {
450   switch (format[0]) {
451     case 'a': {  // 'a: accumulate multiplies
452       if (instr->Bit(21) == 0) {
453         Print("ul");
454       } else {
455         Print("la");
456       }
457       return 1;
458     }
459     case 'b': {  // 'b: byte loads or stores
460       if (instr->HasB()) {
461         Print("b");
462       }
463       return 1;
464     }
465     case 'c': {  // 'cond: conditional execution
466       DCHECK(STRING_STARTS_WITH(format, "cond"));
467       PrintCondition(instr);
468       return 4;
469     }
470     case 'd': {  // 'd: vmov double immediate.
471       double d = instr->DoubleImmedVmov();
472       out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_, "#%g", d);
473       return 1;
474     }
475     case 'f': {  // 'f: bitfield instructions - v7 and above.
476       uint32_t lsbit = instr->Bits(11, 7);
477       uint32_t width = instr->Bits(20, 16) + 1;
478       if (instr->Bit(21) == 0) {
479         // BFC/BFI:
480         // Bits 20-16 represent most-significant bit. Covert to width.
481         width -= lsbit;
482         DCHECK(width > 0);
483       }
484       DCHECK((width + lsbit) <= 32);
485       out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
486                                   "#%d, #%d", lsbit, width);
487       return 1;
488     }
489     case 'h': {  // 'h: halfword operation for extra loads and stores
490       if (instr->HasH()) {
491         Print("h");
492       } else {
493         Print("b");
494       }
495       return 1;
496     }
497     case 'i': {  // 'i: immediate value from adjacent bits.
498       // Expects tokens in the form imm%02d@%02d, i.e. imm05@07, imm10@16
499       int width = (format[3] - '0') * 10 + (format[4] - '0');
500       int lsb   = (format[6] - '0') * 10 + (format[7] - '0');
501
502       DCHECK((width >= 1) && (width <= 32));
503       DCHECK((lsb >= 0) && (lsb <= 31));
504       DCHECK((width + lsb) <= 32);
505
506       out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
507                                   "%d",
508                                   instr->Bits(width + lsb - 1, lsb));
509       return 8;
510     }
511     case 'l': {  // 'l: branch and link
512       if (instr->HasLink()) {
513         Print("l");
514       }
515       return 1;
516     }
517     case 'm': {
518       if (format[1] == 'w') {
519         // 'mw: movt/movw instructions.
520         PrintMovwMovt(instr);
521         return 2;
522       }
523       if (format[1] == 'e') {  // 'memop: load/store instructions.
524         DCHECK(STRING_STARTS_WITH(format, "memop"));
525         if (instr->HasL()) {
526           Print("ldr");
527         } else {
528           if ((instr->Bits(27, 25) == 0) && (instr->Bit(20) == 0) &&
529               (instr->Bits(7, 6) == 3) && (instr->Bit(4) == 1)) {
530             if (instr->Bit(5) == 1) {
531               Print("strd");
532             } else {
533               Print("ldrd");
534             }
535             return 5;
536           }
537           Print("str");
538         }
539         return 5;
540       }
541       // 'msg: for simulator break instructions
542       DCHECK(STRING_STARTS_WITH(format, "msg"));
543       byte* str =
544           reinterpret_cast<byte*>(instr->InstructionBits() & 0x0fffffff);
545       out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
546                                   "%s", converter_.NameInCode(str));
547       return 3;
548     }
549     case 'o': {
550       if ((format[3] == '1') && (format[4] == '2')) {
551         // 'off12: 12-bit offset for load and store instructions
552         DCHECK(STRING_STARTS_WITH(format, "off12"));
553         out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
554                                     "%d", instr->Offset12Value());
555         return 5;
556       } else if (format[3] == '0') {
557         // 'off0to3and8to19 16-bit immediate encoded in bits 19-8 and 3-0.
558         DCHECK(STRING_STARTS_WITH(format, "off0to3and8to19"));
559         out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
560                                     "%d",
561                                     (instr->Bits(19, 8) << 4) +
562                                     instr->Bits(3, 0));
563         return 15;
564       }
565       // 'off8: 8-bit offset for extra load and store instructions
566       DCHECK(STRING_STARTS_WITH(format, "off8"));
567       int offs8 = (instr->ImmedHValue() << 4) | instr->ImmedLValue();
568       out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_, "%d", offs8);
569       return 4;
570     }
571     case 'p': {  // 'pu: P and U bits for load and store instructions
572       DCHECK(STRING_STARTS_WITH(format, "pu"));
573       PrintPU(instr);
574       return 2;
575     }
576     case 'r': {
577       return FormatRegister(instr, format);
578     }
579     case 's': {
580       if (format[1] == 'h') {  // 'shift_op or 'shift_rm or 'shift_sat.
581         if (format[6] == 'o') {  // 'shift_op
582           DCHECK(STRING_STARTS_WITH(format, "shift_op"));
583           if (instr->TypeValue() == 0) {
584             PrintShiftRm(instr);
585           } else {
586             DCHECK(instr->TypeValue() == 1);
587             PrintShiftImm(instr);
588           }
589           return 8;
590         } else if (format[6] == 's') {  // 'shift_sat.
591           DCHECK(STRING_STARTS_WITH(format, "shift_sat"));
592           PrintShiftSat(instr);
593           return 9;
594         } else {  // 'shift_rm
595           DCHECK(STRING_STARTS_WITH(format, "shift_rm"));
596           PrintShiftRm(instr);
597           return 8;
598         }
599       } else if (format[1] == 'v') {  // 'svc
600         DCHECK(STRING_STARTS_WITH(format, "svc"));
601         PrintSoftwareInterrupt(instr->SvcValue());
602         return 3;
603       } else if (format[1] == 'i') {  // 'sign: signed extra loads and stores
604         DCHECK(STRING_STARTS_WITH(format, "sign"));
605         if (instr->HasSign()) {
606           Print("s");
607         }
608         return 4;
609       }
610       // 's: S field of data processing instructions
611       if (instr->HasS()) {
612         Print("s");
613       }
614       return 1;
615     }
616     case 't': {  // 'target: target of branch instructions
617       DCHECK(STRING_STARTS_WITH(format, "target"));
618       int off = (instr->SImmed24Value() << 2) + 8;
619       out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
620                                   "%+d -> %s",
621                                   off,
622                                   converter_.NameOfAddress(
623                                     reinterpret_cast<byte*>(instr) + off));
624       return 6;
625     }
626     case 'u': {  // 'u: signed or unsigned multiplies
627       // The manual gets the meaning of bit 22 backwards in the multiply
628       // instruction overview on page A3.16.2.  The instructions that
629       // exist in u and s variants are the following:
630       // smull A4.1.87
631       // umull A4.1.129
632       // umlal A4.1.128
633       // smlal A4.1.76
634       // For these 0 means u and 1 means s.  As can be seen on their individual
635       // pages.  The other 18 mul instructions have the bit set or unset in
636       // arbitrary ways that are unrelated to the signedness of the instruction.
637       // None of these 18 instructions exist in both a 'u' and an 's' variant.
638
639       if (instr->Bit(22) == 0) {
640         Print("u");
641       } else {
642         Print("s");
643       }
644       return 1;
645     }
646     case 'v': {
647       return FormatVFPinstruction(instr, format);
648     }
649     case 'S':
650     case 'D': {
651       return FormatVFPRegister(instr, format);
652     }
653     case 'w': {  // 'w: W field of load and store instructions
654       if (instr->HasW()) {
655         Print("!");
656       }
657       return 1;
658     }
659     default: {
660       UNREACHABLE();
661       break;
662     }
663   }
664   UNREACHABLE();
665   return -1;
666 }
667
668
669 // Format takes a formatting string for a whole instruction and prints it into
670 // the output buffer. All escaped options are handed to FormatOption to be
671 // parsed further.
672 void Decoder::Format(Instruction* instr, const char* format) {
673   char cur = *format++;
674   while ((cur != 0) && (out_buffer_pos_ < (out_buffer_.length() - 1))) {
675     if (cur == '\'') {  // Single quote is used as the formatting escape.
676       format += FormatOption(instr, format);
677     } else {
678       out_buffer_[out_buffer_pos_++] = cur;
679     }
680     cur = *format++;
681   }
682   out_buffer_[out_buffer_pos_]  = '\0';
683 }
684
685
686 // The disassembler may end up decoding data inlined in the code. We do not want
687 // it to crash if the data does not ressemble any known instruction.
688 #define VERIFY(condition) \
689 if(!(condition)) {        \
690   Unknown(instr);         \
691   return;                 \
692 }
693
694
695 // For currently unimplemented decodings the disassembler calls Unknown(instr)
696 // which will just print "unknown" of the instruction bits.
697 void Decoder::Unknown(Instruction* instr) {
698   Format(instr, "unknown");
699 }
700
701
702 void Decoder::DecodeType01(Instruction* instr) {
703   int type = instr->TypeValue();
704   if ((type == 0) && instr->IsSpecialType0()) {
705     // multiply instruction or extra loads and stores
706     if (instr->Bits(7, 4) == 9) {
707       if (instr->Bit(24) == 0) {
708         // multiply instructions
709         if (instr->Bit(23) == 0) {
710           if (instr->Bit(21) == 0) {
711             // The MUL instruction description (A 4.1.33) refers to Rd as being
712             // the destination for the operation, but it confusingly uses the
713             // Rn field to encode it.
714             Format(instr, "mul'cond's 'rn, 'rm, 'rs");
715           } else {
716             if (instr->Bit(22) == 0) {
717               // The MLA instruction description (A 4.1.28) refers to the order
718               // of registers as "Rd, Rm, Rs, Rn". But confusingly it uses the
719               // Rn field to encode the Rd register and the Rd field to encode
720               // the Rn register.
721               Format(instr, "mla'cond's 'rn, 'rm, 'rs, 'rd");
722             } else {
723               // The MLS instruction description (A 4.1.29) refers to the order
724               // of registers as "Rd, Rm, Rs, Rn". But confusingly it uses the
725               // Rn field to encode the Rd register and the Rd field to encode
726               // the Rn register.
727               Format(instr, "mls'cond's 'rn, 'rm, 'rs, 'rd");
728             }
729           }
730         } else {
731           // The signed/long multiply instructions use the terms RdHi and RdLo
732           // when referring to the target registers. They are mapped to the Rn
733           // and Rd fields as follows:
734           // RdLo == Rd field
735           // RdHi == Rn field
736           // The order of registers is: <RdLo>, <RdHi>, <Rm>, <Rs>
737           Format(instr, "'um'al'cond's 'rd, 'rn, 'rm, 'rs");
738         }
739       } else {
740         Unknown(instr);  // not used by V8
741       }
742     } else if ((instr->Bit(20) == 0) && ((instr->Bits(7, 4) & 0xd) == 0xd)) {
743       // ldrd, strd
744       switch (instr->PUField()) {
745         case da_x: {
746           if (instr->Bit(22) == 0) {
747             Format(instr, "'memop'cond's 'rd, ['rn], -'rm");
748           } else {
749             Format(instr, "'memop'cond's 'rd, ['rn], #-'off8");
750           }
751           break;
752         }
753         case ia_x: {
754           if (instr->Bit(22) == 0) {
755             Format(instr, "'memop'cond's 'rd, ['rn], +'rm");
756           } else {
757             Format(instr, "'memop'cond's 'rd, ['rn], #+'off8");
758           }
759           break;
760         }
761         case db_x: {
762           if (instr->Bit(22) == 0) {
763             Format(instr, "'memop'cond's 'rd, ['rn, -'rm]'w");
764           } else {
765             Format(instr, "'memop'cond's 'rd, ['rn, #-'off8]'w");
766           }
767           break;
768         }
769         case ib_x: {
770           if (instr->Bit(22) == 0) {
771             Format(instr, "'memop'cond's 'rd, ['rn, +'rm]'w");
772           } else {
773             Format(instr, "'memop'cond's 'rd, ['rn, #+'off8]'w");
774           }
775           break;
776         }
777         default: {
778           // The PU field is a 2-bit field.
779           UNREACHABLE();
780           break;
781         }
782       }
783     } else {
784       // extra load/store instructions
785       switch (instr->PUField()) {
786         case da_x: {
787           if (instr->Bit(22) == 0) {
788             Format(instr, "'memop'cond'sign'h 'rd, ['rn], -'rm");
789           } else {
790             Format(instr, "'memop'cond'sign'h 'rd, ['rn], #-'off8");
791           }
792           break;
793         }
794         case ia_x: {
795           if (instr->Bit(22) == 0) {
796             Format(instr, "'memop'cond'sign'h 'rd, ['rn], +'rm");
797           } else {
798             Format(instr, "'memop'cond'sign'h 'rd, ['rn], #+'off8");
799           }
800           break;
801         }
802         case db_x: {
803           if (instr->Bit(22) == 0) {
804             Format(instr, "'memop'cond'sign'h 'rd, ['rn, -'rm]'w");
805           } else {
806             Format(instr, "'memop'cond'sign'h 'rd, ['rn, #-'off8]'w");
807           }
808           break;
809         }
810         case ib_x: {
811           if (instr->Bit(22) == 0) {
812             Format(instr, "'memop'cond'sign'h 'rd, ['rn, +'rm]'w");
813           } else {
814             Format(instr, "'memop'cond'sign'h 'rd, ['rn, #+'off8]'w");
815           }
816           break;
817         }
818         default: {
819           // The PU field is a 2-bit field.
820           UNREACHABLE();
821           break;
822         }
823       }
824       return;
825     }
826   } else if ((type == 0) && instr->IsMiscType0()) {
827     if (instr->Bits(22, 21) == 1) {
828       switch (instr->BitField(7, 4)) {
829         case BX:
830           Format(instr, "bx'cond 'rm");
831           break;
832         case BLX:
833           Format(instr, "blx'cond 'rm");
834           break;
835         case BKPT:
836           Format(instr, "bkpt 'off0to3and8to19");
837           break;
838         default:
839           Unknown(instr);  // not used by V8
840           break;
841       }
842     } else if (instr->Bits(22, 21) == 3) {
843       switch (instr->BitField(7, 4)) {
844         case CLZ:
845           Format(instr, "clz'cond 'rd, 'rm");
846           break;
847         default:
848           Unknown(instr);  // not used by V8
849           break;
850       }
851     } else {
852       Unknown(instr);  // not used by V8
853     }
854   } else if ((type == 1) && instr->IsNopType1()) {
855     Format(instr, "nop'cond");
856   } else {
857     switch (instr->OpcodeField()) {
858       case AND: {
859         Format(instr, "and'cond's 'rd, 'rn, 'shift_op");
860         break;
861       }
862       case EOR: {
863         Format(instr, "eor'cond's 'rd, 'rn, 'shift_op");
864         break;
865       }
866       case SUB: {
867         Format(instr, "sub'cond's 'rd, 'rn, 'shift_op");
868         break;
869       }
870       case RSB: {
871         Format(instr, "rsb'cond's 'rd, 'rn, 'shift_op");
872         break;
873       }
874       case ADD: {
875         Format(instr, "add'cond's 'rd, 'rn, 'shift_op");
876         break;
877       }
878       case ADC: {
879         Format(instr, "adc'cond's 'rd, 'rn, 'shift_op");
880         break;
881       }
882       case SBC: {
883         Format(instr, "sbc'cond's 'rd, 'rn, 'shift_op");
884         break;
885       }
886       case RSC: {
887         Format(instr, "rsc'cond's 'rd, 'rn, 'shift_op");
888         break;
889       }
890       case TST: {
891         if (instr->HasS()) {
892           Format(instr, "tst'cond 'rn, 'shift_op");
893         } else {
894           Format(instr, "movw'cond 'mw");
895         }
896         break;
897       }
898       case TEQ: {
899         if (instr->HasS()) {
900           Format(instr, "teq'cond 'rn, 'shift_op");
901         } else {
902           // Other instructions matching this pattern are handled in the
903           // miscellaneous instructions part above.
904           UNREACHABLE();
905         }
906         break;
907       }
908       case CMP: {
909         if (instr->HasS()) {
910           Format(instr, "cmp'cond 'rn, 'shift_op");
911         } else {
912           Format(instr, "movt'cond 'mw");
913         }
914         break;
915       }
916       case CMN: {
917         if (instr->HasS()) {
918           Format(instr, "cmn'cond 'rn, 'shift_op");
919         } else {
920           // Other instructions matching this pattern are handled in the
921           // miscellaneous instructions part above.
922           UNREACHABLE();
923         }
924         break;
925       }
926       case ORR: {
927         Format(instr, "orr'cond's 'rd, 'rn, 'shift_op");
928         break;
929       }
930       case MOV: {
931         Format(instr, "mov'cond's 'rd, 'shift_op");
932         break;
933       }
934       case BIC: {
935         Format(instr, "bic'cond's 'rd, 'rn, 'shift_op");
936         break;
937       }
938       case MVN: {
939         Format(instr, "mvn'cond's 'rd, 'shift_op");
940         break;
941       }
942       default: {
943         // The Opcode field is a 4-bit field.
944         UNREACHABLE();
945         break;
946       }
947     }
948   }
949 }
950
951
952 void Decoder::DecodeType2(Instruction* instr) {
953   switch (instr->PUField()) {
954     case da_x: {
955       if (instr->HasW()) {
956         Unknown(instr);  // not used in V8
957         return;
958       }
959       Format(instr, "'memop'cond'b 'rd, ['rn], #-'off12");
960       break;
961     }
962     case ia_x: {
963       if (instr->HasW()) {
964         Unknown(instr);  // not used in V8
965         return;
966       }
967       Format(instr, "'memop'cond'b 'rd, ['rn], #+'off12");
968       break;
969     }
970     case db_x: {
971       Format(instr, "'memop'cond'b 'rd, ['rn, #-'off12]'w");
972       break;
973     }
974     case ib_x: {
975       Format(instr, "'memop'cond'b 'rd, ['rn, #+'off12]'w");
976       break;
977     }
978     default: {
979       // The PU field is a 2-bit field.
980       UNREACHABLE();
981       break;
982     }
983   }
984 }
985
986
987 void Decoder::DecodeType3(Instruction* instr) {
988   switch (instr->PUField()) {
989     case da_x: {
990       VERIFY(!instr->HasW());
991       Format(instr, "'memop'cond'b 'rd, ['rn], -'shift_rm");
992       break;
993     }
994     case ia_x: {
995       if (instr->Bit(4) == 0) {
996         Format(instr, "'memop'cond'b 'rd, ['rn], +'shift_rm");
997       } else {
998         if (instr->Bit(5) == 0) {
999           switch (instr->Bits(22, 21)) {
1000             case 0:
1001               if (instr->Bit(20) == 0) {
1002                 if (instr->Bit(6) == 0) {
1003                   Format(instr, "pkhbt'cond 'rd, 'rn, 'rm, lsl #'imm05@07");
1004                 } else {
1005                   if (instr->Bits(11, 7) == 0) {
1006                     Format(instr, "pkhtb'cond 'rd, 'rn, 'rm, asr #32");
1007                   } else {
1008                     Format(instr, "pkhtb'cond 'rd, 'rn, 'rm, asr #'imm05@07");
1009                   }
1010                 }
1011               } else {
1012                 UNREACHABLE();
1013               }
1014               break;
1015             case 1:
1016               UNREACHABLE();
1017               break;
1018             case 2:
1019               UNREACHABLE();
1020               break;
1021             case 3:
1022               Format(instr, "usat 'rd, #'imm05@16, 'rm'shift_sat");
1023               break;
1024           }
1025         } else {
1026           switch (instr->Bits(22, 21)) {
1027             case 0:
1028               UNREACHABLE();
1029               break;
1030             case 1:
1031               if (instr->Bits(9, 6) == 1) {
1032                 if (instr->Bit(20) == 0) {
1033                   if (instr->Bits(19, 16) == 0xF) {
1034                     switch (instr->Bits(11, 10)) {
1035                       case 0:
1036                         Format(instr, "sxtb'cond 'rd, 'rm");
1037                         break;
1038                       case 1:
1039                         Format(instr, "sxtb'cond 'rd, 'rm, ror #8");
1040                         break;
1041                       case 2:
1042                         Format(instr, "sxtb'cond 'rd, 'rm, ror #16");
1043                         break;
1044                       case 3:
1045                         Format(instr, "sxtb'cond 'rd, 'rm, ror #24");
1046                         break;
1047                     }
1048                   } else {
1049                     switch (instr->Bits(11, 10)) {
1050                       case 0:
1051                         Format(instr, "sxtab'cond 'rd, 'rn, 'rm");
1052                         break;
1053                       case 1:
1054                         Format(instr, "sxtab'cond 'rd, 'rn, 'rm, ror #8");
1055                         break;
1056                       case 2:
1057                         Format(instr, "sxtab'cond 'rd, 'rn, 'rm, ror #16");
1058                         break;
1059                       case 3:
1060                         Format(instr, "sxtab'cond 'rd, 'rn, 'rm, ror #24");
1061                         break;
1062                     }
1063                   }
1064                 } else {
1065                   if (instr->Bits(19, 16) == 0xF) {
1066                     switch (instr->Bits(11, 10)) {
1067                       case 0:
1068                         Format(instr, "sxth'cond 'rd, 'rm");
1069                         break;
1070                       case 1:
1071                         Format(instr, "sxth'cond 'rd, 'rm, ror #8");
1072                         break;
1073                       case 2:
1074                         Format(instr, "sxth'cond 'rd, 'rm, ror #16");
1075                         break;
1076                       case 3:
1077                         Format(instr, "sxth'cond 'rd, 'rm, ror #24");
1078                         break;
1079                     }
1080                   } else {
1081                     switch (instr->Bits(11, 10)) {
1082                       case 0:
1083                         Format(instr, "sxtah'cond 'rd, 'rn, 'rm");
1084                         break;
1085                       case 1:
1086                         Format(instr, "sxtah'cond 'rd, 'rn, 'rm, ror #8");
1087                         break;
1088                       case 2:
1089                         Format(instr, "sxtah'cond 'rd, 'rn, 'rm, ror #16");
1090                         break;
1091                       case 3:
1092                         Format(instr, "sxtah'cond 'rd, 'rn, 'rm, ror #24");
1093                         break;
1094                     }
1095                   }
1096                 }
1097               } else {
1098                 UNREACHABLE();
1099               }
1100               break;
1101             case 2:
1102               if ((instr->Bit(20) == 0) && (instr->Bits(9, 6) == 1)) {
1103                 if (instr->Bits(19, 16) == 0xF) {
1104                   switch (instr->Bits(11, 10)) {
1105                     case 0:
1106                       Format(instr, "uxtb16'cond 'rd, 'rm");
1107                       break;
1108                     case 1:
1109                       Format(instr, "uxtb16'cond 'rd, 'rm, ror #8");
1110                       break;
1111                     case 2:
1112                       Format(instr, "uxtb16'cond 'rd, 'rm, ror #16");
1113                       break;
1114                     case 3:
1115                       Format(instr, "uxtb16'cond 'rd, 'rm, ror #24");
1116                       break;
1117                   }
1118                 } else {
1119                   UNREACHABLE();
1120                 }
1121               } else {
1122                 UNREACHABLE();
1123               }
1124               break;
1125             case 3:
1126               if ((instr->Bits(9, 6) == 1)) {
1127                 if ((instr->Bit(20) == 0)) {
1128                   if (instr->Bits(19, 16) == 0xF) {
1129                     switch (instr->Bits(11, 10)) {
1130                       case 0:
1131                         Format(instr, "uxtb'cond 'rd, 'rm");
1132                         break;
1133                       case 1:
1134                         Format(instr, "uxtb'cond 'rd, 'rm, ror #8");
1135                         break;
1136                       case 2:
1137                         Format(instr, "uxtb'cond 'rd, 'rm, ror #16");
1138                         break;
1139                       case 3:
1140                         Format(instr, "uxtb'cond 'rd, 'rm, ror #24");
1141                         break;
1142                     }
1143                   } else {
1144                     switch (instr->Bits(11, 10)) {
1145                       case 0:
1146                         Format(instr, "uxtab'cond 'rd, 'rn, 'rm");
1147                         break;
1148                       case 1:
1149                         Format(instr, "uxtab'cond 'rd, 'rn, 'rm, ror #8");
1150                         break;
1151                       case 2:
1152                         Format(instr, "uxtab'cond 'rd, 'rn, 'rm, ror #16");
1153                         break;
1154                       case 3:
1155                         Format(instr, "uxtab'cond 'rd, 'rn, 'rm, ror #24");
1156                         break;
1157                     }
1158                   }
1159                 } else {
1160                   if (instr->Bits(19, 16) == 0xF) {
1161                     switch (instr->Bits(11, 10)) {
1162                       case 0:
1163                         Format(instr, "uxth'cond 'rd, 'rm");
1164                         break;
1165                       case 1:
1166                         Format(instr, "uxth'cond 'rd, 'rm, ror #8");
1167                         break;
1168                       case 2:
1169                         Format(instr, "uxth'cond 'rd, 'rm, ror #16");
1170                         break;
1171                       case 3:
1172                         Format(instr, "uxth'cond 'rd, 'rm, ror #24");
1173                         break;
1174                     }
1175                   } else {
1176                     switch (instr->Bits(11, 10)) {
1177                       case 0:
1178                         Format(instr, "uxtah'cond 'rd, 'rn, 'rm");
1179                         break;
1180                       case 1:
1181                         Format(instr, "uxtah'cond 'rd, 'rn, 'rm, ror #8");
1182                         break;
1183                       case 2:
1184                         Format(instr, "uxtah'cond 'rd, 'rn, 'rm, ror #16");
1185                         break;
1186                       case 3:
1187                         Format(instr, "uxtah'cond 'rd, 'rn, 'rm, ror #24");
1188                         break;
1189                     }
1190                   }
1191                 }
1192               } else {
1193                 UNREACHABLE();
1194               }
1195               break;
1196           }
1197         }
1198       }
1199       break;
1200     }
1201     case db_x: {
1202       if (instr->Bits(22, 20) == 0x5) {
1203         if (instr->Bits(7, 4) == 0x1) {
1204           if (instr->Bits(15, 12) == 0xF) {
1205             Format(instr, "smmul'cond 'rn, 'rm, 'rs");
1206           } else {
1207             // SMMLA (in V8 notation matching ARM ISA format)
1208             Format(instr, "smmla'cond 'rn, 'rm, 'rs, 'rd");
1209           }
1210           break;
1211         }
1212       }
1213       if (FLAG_enable_sudiv) {
1214         if (instr->Bits(5, 4) == 0x1) {
1215           if ((instr->Bit(22) == 0x0) && (instr->Bit(20) == 0x1)) {
1216             if (instr->Bit(21) == 0x1) {
1217               // UDIV (in V8 notation matching ARM ISA format) rn = rm/rs
1218               Format(instr, "udiv'cond'b 'rn, 'rm, 'rs");
1219             } else {
1220               // SDIV (in V8 notation matching ARM ISA format) rn = rm/rs
1221               Format(instr, "sdiv'cond'b 'rn, 'rm, 'rs");
1222             }
1223             break;
1224           }
1225         }
1226       }
1227       Format(instr, "'memop'cond'b 'rd, ['rn, -'shift_rm]'w");
1228       break;
1229     }
1230     case ib_x: {
1231       if (instr->HasW() && (instr->Bits(6, 4) == 0x5)) {
1232         uint32_t widthminus1 = static_cast<uint32_t>(instr->Bits(20, 16));
1233         uint32_t lsbit = static_cast<uint32_t>(instr->Bits(11, 7));
1234         uint32_t msbit = widthminus1 + lsbit;
1235         if (msbit <= 31) {
1236           if (instr->Bit(22)) {
1237             Format(instr, "ubfx'cond 'rd, 'rm, 'f");
1238           } else {
1239             Format(instr, "sbfx'cond 'rd, 'rm, 'f");
1240           }
1241         } else {
1242           UNREACHABLE();
1243         }
1244       } else if (!instr->HasW() && (instr->Bits(6, 4) == 0x1)) {
1245         uint32_t lsbit = static_cast<uint32_t>(instr->Bits(11, 7));
1246         uint32_t msbit = static_cast<uint32_t>(instr->Bits(20, 16));
1247         if (msbit >= lsbit) {
1248           if (instr->RmValue() == 15) {
1249             Format(instr, "bfc'cond 'rd, 'f");
1250           } else {
1251             Format(instr, "bfi'cond 'rd, 'rm, 'f");
1252           }
1253         } else {
1254           UNREACHABLE();
1255         }
1256       } else {
1257         Format(instr, "'memop'cond'b 'rd, ['rn, +'shift_rm]'w");
1258       }
1259       break;
1260     }
1261     default: {
1262       // The PU field is a 2-bit field.
1263       UNREACHABLE();
1264       break;
1265     }
1266   }
1267 }
1268
1269
1270 void Decoder::DecodeType4(Instruction* instr) {
1271   if (instr->Bit(22) != 0) {
1272     // Privileged mode currently not supported.
1273     Unknown(instr);
1274   } else {
1275     if (instr->HasL()) {
1276       Format(instr, "ldm'cond'pu 'rn'w, 'rlist");
1277     } else {
1278       Format(instr, "stm'cond'pu 'rn'w, 'rlist");
1279     }
1280   }
1281 }
1282
1283
1284 void Decoder::DecodeType5(Instruction* instr) {
1285   Format(instr, "b'l'cond 'target");
1286 }
1287
1288
1289 void Decoder::DecodeType6(Instruction* instr) {
1290   DecodeType6CoprocessorIns(instr);
1291 }
1292
1293
1294 int Decoder::DecodeType7(Instruction* instr) {
1295   if (instr->Bit(24) == 1) {
1296     if (instr->SvcValue() >= kStopCode) {
1297       Format(instr, "stop'cond 'svc");
1298       // Also print the stop message. Its address is encoded
1299       // in the following 4 bytes.
1300       out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
1301                                   "\n  %p  %08x       stop message: %s",
1302                                   reinterpret_cast<void*>(instr
1303                                                  + Instruction::kInstrSize),
1304                                   *reinterpret_cast<uint32_t*>(instr
1305                                                 + Instruction::kInstrSize),
1306                                   *reinterpret_cast<char**>(instr
1307                                                 + Instruction::kInstrSize));
1308       // We have decoded 2 * Instruction::kInstrSize bytes.
1309       return 2 * Instruction::kInstrSize;
1310     } else {
1311       Format(instr, "svc'cond 'svc");
1312     }
1313   } else {
1314     DecodeTypeVFP(instr);
1315   }
1316   return Instruction::kInstrSize;
1317 }
1318
1319
1320 // void Decoder::DecodeTypeVFP(Instruction* instr)
1321 // vmov: Sn = Rt
1322 // vmov: Rt = Sn
1323 // vcvt: Dd = Sm
1324 // vcvt: Sd = Dm
1325 // vcvt.f64.s32 Dd, Dd, #<fbits>
1326 // Dd = vabs(Dm)
1327 // Dd = vneg(Dm)
1328 // Dd = vadd(Dn, Dm)
1329 // Dd = vsub(Dn, Dm)
1330 // Dd = vmul(Dn, Dm)
1331 // Dd = vmla(Dn, Dm)
1332 // Dd = vmls(Dn, Dm)
1333 // Dd = vdiv(Dn, Dm)
1334 // vcmp(Dd, Dm)
1335 // vmrs
1336 // vmsr
1337 // Dd = vsqrt(Dm)
1338 void Decoder::DecodeTypeVFP(Instruction* instr) {
1339   VERIFY((instr->TypeValue() == 7) && (instr->Bit(24) == 0x0) );
1340   VERIFY(instr->Bits(11, 9) == 0x5);
1341
1342   if (instr->Bit(4) == 0) {
1343     if (instr->Opc1Value() == 0x7) {
1344       // Other data processing instructions
1345       if ((instr->Opc2Value() == 0x0) && (instr->Opc3Value() == 0x1)) {
1346         // vmov register to register.
1347         if (instr->SzValue() == 0x1) {
1348           Format(instr, "vmov'cond.f64 'Dd, 'Dm");
1349         } else {
1350           Format(instr, "vmov'cond.f32 'Sd, 'Sm");
1351         }
1352       } else if ((instr->Opc2Value() == 0x0) && (instr->Opc3Value() == 0x3)) {
1353         // vabs
1354         Format(instr, "vabs'cond.f64 'Dd, 'Dm");
1355       } else if ((instr->Opc2Value() == 0x1) && (instr->Opc3Value() == 0x1)) {
1356         // vneg
1357         Format(instr, "vneg'cond.f64 'Dd, 'Dm");
1358       } else if ((instr->Opc2Value() == 0x7) && (instr->Opc3Value() == 0x3)) {
1359         DecodeVCVTBetweenDoubleAndSingle(instr);
1360       } else if ((instr->Opc2Value() == 0x8) && (instr->Opc3Value() & 0x1)) {
1361         DecodeVCVTBetweenFloatingPointAndInteger(instr);
1362       } else if ((instr->Opc2Value() == 0xA) && (instr->Opc3Value() == 0x3) &&
1363                  (instr->Bit(8) == 1)) {
1364         // vcvt.f64.s32 Dd, Dd, #<fbits>
1365         int fraction_bits = 32 - ((instr->Bits(3, 0) << 1) | instr->Bit(5));
1366         Format(instr, "vcvt'cond.f64.s32 'Dd, 'Dd");
1367         out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
1368                                     ", #%d", fraction_bits);
1369       } else if (((instr->Opc2Value() >> 1) == 0x6) &&
1370                  (instr->Opc3Value() & 0x1)) {
1371         DecodeVCVTBetweenFloatingPointAndInteger(instr);
1372       } else if (((instr->Opc2Value() == 0x4) || (instr->Opc2Value() == 0x5)) &&
1373                  (instr->Opc3Value() & 0x1)) {
1374         DecodeVCMP(instr);
1375       } else if (((instr->Opc2Value() == 0x1)) && (instr->Opc3Value() == 0x3)) {
1376         Format(instr, "vsqrt'cond.f64 'Dd, 'Dm");
1377       } else if (instr->Opc3Value() == 0x0) {
1378         if (instr->SzValue() == 0x1) {
1379           Format(instr, "vmov'cond.f64 'Dd, 'd");
1380         } else {
1381           Unknown(instr);  // Not used by V8.
1382         }
1383       } else if (((instr->Opc2Value() == 0x6)) && instr->Opc3Value() == 0x3) {
1384         bool dp_operation = (instr->SzValue() == 1);
1385         // vrintz - round towards zero (truncate)
1386         if (dp_operation) {
1387           Format(instr, "vrintz'cond.f64.f64 'Dd, 'Dm");
1388         } else {
1389           Unknown(instr);  // Not used by V8.
1390         }
1391       } else {
1392         Unknown(instr);  // Not used by V8.
1393       }
1394     } else if (instr->Opc1Value() == 0x3) {
1395       if (instr->SzValue() == 0x1) {
1396         if (instr->Opc3Value() & 0x1) {
1397           Format(instr, "vsub'cond.f64 'Dd, 'Dn, 'Dm");
1398         } else {
1399           Format(instr, "vadd'cond.f64 'Dd, 'Dn, 'Dm");
1400         }
1401       } else {
1402         Unknown(instr);  // Not used by V8.
1403       }
1404     } else if ((instr->Opc1Value() == 0x2) && !(instr->Opc3Value() & 0x1)) {
1405       if (instr->SzValue() == 0x1) {
1406         Format(instr, "vmul'cond.f64 'Dd, 'Dn, 'Dm");
1407       } else {
1408         Unknown(instr);  // Not used by V8.
1409       }
1410     } else if ((instr->Opc1Value() == 0x0) && !(instr->Opc3Value() & 0x1)) {
1411       if (instr->SzValue() == 0x1) {
1412         Format(instr, "vmla'cond.f64 'Dd, 'Dn, 'Dm");
1413       } else {
1414         Unknown(instr);  // Not used by V8.
1415       }
1416     } else if ((instr->Opc1Value() == 0x0) && (instr->Opc3Value() & 0x1)) {
1417       if (instr->SzValue() == 0x1) {
1418         Format(instr, "vmls'cond.f64 'Dd, 'Dn, 'Dm");
1419       } else {
1420         Unknown(instr);  // Not used by V8.
1421       }
1422     } else if ((instr->Opc1Value() == 0x4) && !(instr->Opc3Value() & 0x1)) {
1423       if (instr->SzValue() == 0x1) {
1424         Format(instr, "vdiv'cond.f64 'Dd, 'Dn, 'Dm");
1425       } else {
1426         Unknown(instr);  // Not used by V8.
1427       }
1428     } else {
1429       Unknown(instr);  // Not used by V8.
1430     }
1431   } else {
1432     if ((instr->VCValue() == 0x0) &&
1433         (instr->VAValue() == 0x0)) {
1434       DecodeVMOVBetweenCoreAndSinglePrecisionRegisters(instr);
1435     } else if ((instr->VLValue() == 0x0) &&
1436                (instr->VCValue() == 0x1) &&
1437                (instr->Bit(23) == 0x0)) {
1438       if (instr->Bit(21) == 0x0) {
1439         Format(instr, "vmov'cond.32 'Dd[0], 'rt");
1440       } else {
1441         Format(instr, "vmov'cond.32 'Dd[1], 'rt");
1442       }
1443     } else if ((instr->VLValue() == 0x1) &&
1444                (instr->VCValue() == 0x1) &&
1445                (instr->Bit(23) == 0x0)) {
1446       if (instr->Bit(21) == 0x0) {
1447         Format(instr, "vmov'cond.32 'rt, 'Dd[0]");
1448       } else {
1449         Format(instr, "vmov'cond.32 'rt, 'Dd[1]");
1450       }
1451     } else if ((instr->VCValue() == 0x0) &&
1452                (instr->VAValue() == 0x7) &&
1453                (instr->Bits(19, 16) == 0x1)) {
1454       if (instr->VLValue() == 0) {
1455         if (instr->Bits(15, 12) == 0xF) {
1456           Format(instr, "vmsr'cond FPSCR, APSR");
1457         } else {
1458           Format(instr, "vmsr'cond FPSCR, 'rt");
1459         }
1460       } else {
1461         if (instr->Bits(15, 12) == 0xF) {
1462           Format(instr, "vmrs'cond APSR, FPSCR");
1463         } else {
1464           Format(instr, "vmrs'cond 'rt, FPSCR");
1465         }
1466       }
1467     }
1468   }
1469 }
1470
1471
1472 void Decoder::DecodeVMOVBetweenCoreAndSinglePrecisionRegisters(
1473     Instruction* instr) {
1474   VERIFY((instr->Bit(4) == 1) && (instr->VCValue() == 0x0) &&
1475          (instr->VAValue() == 0x0));
1476
1477   bool to_arm_register = (instr->VLValue() == 0x1);
1478
1479   if (to_arm_register) {
1480     Format(instr, "vmov'cond 'rt, 'Sn");
1481   } else {
1482     Format(instr, "vmov'cond 'Sn, 'rt");
1483   }
1484 }
1485
1486
1487 void Decoder::DecodeVCMP(Instruction* instr) {
1488   VERIFY((instr->Bit(4) == 0) && (instr->Opc1Value() == 0x7));
1489   VERIFY(((instr->Opc2Value() == 0x4) || (instr->Opc2Value() == 0x5)) &&
1490          (instr->Opc3Value() & 0x1));
1491
1492   // Comparison.
1493   bool dp_operation = (instr->SzValue() == 1);
1494   bool raise_exception_for_qnan = (instr->Bit(7) == 0x1);
1495
1496   if (dp_operation && !raise_exception_for_qnan) {
1497     if (instr->Opc2Value() == 0x4) {
1498       Format(instr, "vcmp'cond.f64 'Dd, 'Dm");
1499     } else if (instr->Opc2Value() == 0x5) {
1500       Format(instr, "vcmp'cond.f64 'Dd, #0.0");
1501     } else {
1502       Unknown(instr);  // invalid
1503     }
1504   } else {
1505     Unknown(instr);  // Not used by V8.
1506   }
1507 }
1508
1509
1510 void Decoder::DecodeVCVTBetweenDoubleAndSingle(Instruction* instr) {
1511   VERIFY((instr->Bit(4) == 0) && (instr->Opc1Value() == 0x7));
1512   VERIFY((instr->Opc2Value() == 0x7) && (instr->Opc3Value() == 0x3));
1513
1514   bool double_to_single = (instr->SzValue() == 1);
1515
1516   if (double_to_single) {
1517     Format(instr, "vcvt'cond.f32.f64 'Sd, 'Dm");
1518   } else {
1519     Format(instr, "vcvt'cond.f64.f32 'Dd, 'Sm");
1520   }
1521 }
1522
1523
1524 void Decoder::DecodeVCVTBetweenFloatingPointAndInteger(Instruction* instr) {
1525   VERIFY((instr->Bit(4) == 0) && (instr->Opc1Value() == 0x7));
1526   VERIFY(((instr->Opc2Value() == 0x8) && (instr->Opc3Value() & 0x1)) ||
1527          (((instr->Opc2Value() >> 1) == 0x6) && (instr->Opc3Value() & 0x1)));
1528
1529   bool to_integer = (instr->Bit(18) == 1);
1530   bool dp_operation = (instr->SzValue() == 1);
1531   if (to_integer) {
1532     bool unsigned_integer = (instr->Bit(16) == 0);
1533
1534     if (dp_operation) {
1535       if (unsigned_integer) {
1536         Format(instr, "vcvt'cond.u32.f64 'Sd, 'Dm");
1537       } else {
1538         Format(instr, "vcvt'cond.s32.f64 'Sd, 'Dm");
1539       }
1540     } else {
1541       if (unsigned_integer) {
1542         Format(instr, "vcvt'cond.u32.f32 'Sd, 'Sm");
1543       } else {
1544         Format(instr, "vcvt'cond.s32.f32 'Sd, 'Sm");
1545       }
1546     }
1547   } else {
1548     bool unsigned_integer = (instr->Bit(7) == 0);
1549
1550     if (dp_operation) {
1551       if (unsigned_integer) {
1552         Format(instr, "vcvt'cond.f64.u32 'Dd, 'Sm");
1553       } else {
1554         Format(instr, "vcvt'cond.f64.s32 'Dd, 'Sm");
1555       }
1556     } else {
1557       if (unsigned_integer) {
1558         Format(instr, "vcvt'cond.f32.u32 'Sd, 'Sm");
1559       } else {
1560         Format(instr, "vcvt'cond.f32.s32 'Sd, 'Sm");
1561       }
1562     }
1563   }
1564 }
1565
1566
1567 // Decode Type 6 coprocessor instructions.
1568 // Dm = vmov(Rt, Rt2)
1569 // <Rt, Rt2> = vmov(Dm)
1570 // Ddst = MEM(Rbase + 4*offset).
1571 // MEM(Rbase + 4*offset) = Dsrc.
1572 void Decoder::DecodeType6CoprocessorIns(Instruction* instr) {
1573   VERIFY(instr->TypeValue() == 6);
1574
1575   if (instr->CoprocessorValue() == 0xA) {
1576     switch (instr->OpcodeValue()) {
1577       case 0x8:
1578       case 0xA:
1579         if (instr->HasL()) {
1580           Format(instr, "vldr'cond 'Sd, ['rn - 4*'imm08@00]");
1581         } else {
1582           Format(instr, "vstr'cond 'Sd, ['rn - 4*'imm08@00]");
1583         }
1584         break;
1585       case 0xC:
1586       case 0xE:
1587         if (instr->HasL()) {
1588           Format(instr, "vldr'cond 'Sd, ['rn + 4*'imm08@00]");
1589         } else {
1590           Format(instr, "vstr'cond 'Sd, ['rn + 4*'imm08@00]");
1591         }
1592         break;
1593       case 0x4:
1594       case 0x5:
1595       case 0x6:
1596       case 0x7:
1597       case 0x9:
1598       case 0xB: {
1599         bool to_vfp_register = (instr->VLValue() == 0x1);
1600         if (to_vfp_register) {
1601           Format(instr, "vldm'cond'pu 'rn'w, {'Sd-'Sd+}");
1602         } else {
1603           Format(instr, "vstm'cond'pu 'rn'w, {'Sd-'Sd+}");
1604         }
1605         break;
1606       }
1607       default:
1608         Unknown(instr);  // Not used by V8.
1609     }
1610   } else if (instr->CoprocessorValue() == 0xB) {
1611     switch (instr->OpcodeValue()) {
1612       case 0x2:
1613         // Load and store double to two GP registers
1614         if (instr->Bits(7, 6) != 0 || instr->Bit(4) != 1) {
1615           Unknown(instr);  // Not used by V8.
1616         } else if (instr->HasL()) {
1617           Format(instr, "vmov'cond 'rt, 'rn, 'Dm");
1618         } else {
1619           Format(instr, "vmov'cond 'Dm, 'rt, 'rn");
1620         }
1621         break;
1622       case 0x8:
1623       case 0xA:
1624         if (instr->HasL()) {
1625           Format(instr, "vldr'cond 'Dd, ['rn - 4*'imm08@00]");
1626         } else {
1627           Format(instr, "vstr'cond 'Dd, ['rn - 4*'imm08@00]");
1628         }
1629         break;
1630       case 0xC:
1631       case 0xE:
1632         if (instr->HasL()) {
1633           Format(instr, "vldr'cond 'Dd, ['rn + 4*'imm08@00]");
1634         } else {
1635           Format(instr, "vstr'cond 'Dd, ['rn + 4*'imm08@00]");
1636         }
1637         break;
1638       case 0x4:
1639       case 0x5:
1640       case 0x6:
1641       case 0x7:
1642       case 0x9:
1643       case 0xB: {
1644         bool to_vfp_register = (instr->VLValue() == 0x1);
1645         if (to_vfp_register) {
1646           Format(instr, "vldm'cond'pu 'rn'w, {'Dd-'Dd+}");
1647         } else {
1648           Format(instr, "vstm'cond'pu 'rn'w, {'Dd-'Dd+}");
1649         }
1650         break;
1651       }
1652       default:
1653         Unknown(instr);  // Not used by V8.
1654     }
1655   } else {
1656     Unknown(instr);  // Not used by V8.
1657   }
1658 }
1659
1660
1661 void Decoder::DecodeSpecialCondition(Instruction* instr) {
1662   switch (instr->SpecialValue()) {
1663     case 5:
1664       if ((instr->Bits(18, 16) == 0) && (instr->Bits(11, 6) == 0x28) &&
1665           (instr->Bit(4) == 1)) {
1666         // vmovl signed
1667         if ((instr->VdValue() & 1) != 0) Unknown(instr);
1668         int Vd = (instr->Bit(22) << 3) | (instr->VdValue() >> 1);
1669         int Vm = (instr->Bit(5) << 4) | instr->VmValue();
1670         int imm3 = instr->Bits(21, 19);
1671         out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
1672                                     "vmovl.s%d q%d, d%d", imm3*8, Vd, Vm);
1673       } else {
1674         Unknown(instr);
1675       }
1676       break;
1677     case 7:
1678       if ((instr->Bits(18, 16) == 0) && (instr->Bits(11, 6) == 0x28) &&
1679           (instr->Bit(4) == 1)) {
1680         // vmovl unsigned
1681         if ((instr->VdValue() & 1) != 0) Unknown(instr);
1682         int Vd = (instr->Bit(22) << 3) | (instr->VdValue() >> 1);
1683         int Vm = (instr->Bit(5) << 4) | instr->VmValue();
1684         int imm3 = instr->Bits(21, 19);
1685         out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
1686                                     "vmovl.u%d q%d, d%d", imm3*8, Vd, Vm);
1687       } else {
1688         Unknown(instr);
1689       }
1690       break;
1691     case 8:
1692       if (instr->Bits(21, 20) == 0) {
1693         // vst1
1694         int Vd = (instr->Bit(22) << 4) | instr->VdValue();
1695         int Rn = instr->VnValue();
1696         int type = instr->Bits(11, 8);
1697         int size = instr->Bits(7, 6);
1698         int align = instr->Bits(5, 4);
1699         int Rm = instr->VmValue();
1700         out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
1701                                     "vst1.%d ", (1 << size) << 3);
1702         FormatNeonList(Vd, type);
1703         Print(", ");
1704         FormatNeonMemory(Rn, align, Rm);
1705       } else if (instr->Bits(21, 20) == 2) {
1706         // vld1
1707         int Vd = (instr->Bit(22) << 4) | instr->VdValue();
1708         int Rn = instr->VnValue();
1709         int type = instr->Bits(11, 8);
1710         int size = instr->Bits(7, 6);
1711         int align = instr->Bits(5, 4);
1712         int Rm = instr->VmValue();
1713         out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
1714                                     "vld1.%d ", (1 << size) << 3);
1715         FormatNeonList(Vd, type);
1716         Print(", ");
1717         FormatNeonMemory(Rn, align, Rm);
1718       } else {
1719         Unknown(instr);
1720       }
1721       break;
1722     case 0xA:
1723     case 0xB:
1724       if ((instr->Bits(22, 20) == 5) && (instr->Bits(15, 12) == 0xf)) {
1725         int Rn = instr->Bits(19, 16);
1726         int offset = instr->Bits(11, 0);
1727         if (offset == 0) {
1728           out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
1729                                       "pld [r%d]", Rn);
1730         } else if (instr->Bit(23) == 0) {
1731           out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
1732                                       "pld [r%d, #-%d]", Rn, offset);
1733         } else {
1734           out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
1735                                       "pld [r%d, #+%d]", Rn, offset);
1736         }
1737       } else {
1738         Unknown(instr);
1739       }
1740       break;
1741     case 0x1D:
1742       if (instr->Opc1Value() == 0x7 && instr->Bits(19, 18) == 0x2 &&
1743           instr->Bits(11, 9) == 0x5 && instr->Bits(7, 6) == 0x1 &&
1744           instr->Bit(4) == 0x0) {
1745         // VRINTA, VRINTN, VRINTP, VRINTM (floating-point)
1746         bool dp_operation = (instr->SzValue() == 1);
1747         int rounding_mode = instr->Bits(17, 16);
1748         switch (rounding_mode) {
1749           case 0x0:
1750             if (dp_operation) {
1751               Format(instr, "vrinta.f64.f64 'Dd, 'Dm");
1752             } else {
1753               Unknown(instr);
1754             }
1755             break;
1756           case 0x1:
1757             if (dp_operation) {
1758               Format(instr, "vrintn.f64.f64 'Dd, 'Dm");
1759             } else {
1760               Unknown(instr);
1761             }
1762             break;
1763           case 0x2:
1764             if (dp_operation) {
1765               Format(instr, "vrintp.f64.f64 'Dd, 'Dm");
1766             } else {
1767               Unknown(instr);
1768             }
1769             break;
1770           case 0x3:
1771             if (dp_operation) {
1772               Format(instr, "vrintm.f64.f64 'Dd, 'Dm");
1773             } else {
1774               Unknown(instr);
1775             }
1776             break;
1777           default:
1778             UNREACHABLE();  // Case analysis is exhaustive.
1779             break;
1780         }
1781       } else {
1782         Unknown(instr);
1783       }
1784       break;
1785     default:
1786       Unknown(instr);
1787       break;
1788   }
1789 }
1790
1791 #undef VERIFIY
1792
1793 bool Decoder::IsConstantPoolAt(byte* instr_ptr) {
1794   int instruction_bits = *(reinterpret_cast<int*>(instr_ptr));
1795   return (instruction_bits & kConstantPoolMarkerMask) == kConstantPoolMarker;
1796 }
1797
1798
1799 int Decoder::ConstantPoolSizeAt(byte* instr_ptr) {
1800   if (IsConstantPoolAt(instr_ptr)) {
1801     int instruction_bits = *(reinterpret_cast<int*>(instr_ptr));
1802     return DecodeConstantPoolLength(instruction_bits);
1803   } else {
1804     return -1;
1805   }
1806 }
1807
1808
1809 // Disassemble the instruction at *instr_ptr into the output buffer.
1810 int Decoder::InstructionDecode(byte* instr_ptr) {
1811   Instruction* instr = Instruction::At(instr_ptr);
1812   // Print raw instruction bytes.
1813   out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
1814                               "%08x       ",
1815                               instr->InstructionBits());
1816   if (instr->ConditionField() == kSpecialCondition) {
1817     DecodeSpecialCondition(instr);
1818     return Instruction::kInstrSize;
1819   }
1820   int instruction_bits = *(reinterpret_cast<int*>(instr_ptr));
1821   if ((instruction_bits & kConstantPoolMarkerMask) == kConstantPoolMarker) {
1822     out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
1823                                 "constant pool begin (length %d)",
1824                                 DecodeConstantPoolLength(instruction_bits));
1825     return Instruction::kInstrSize;
1826   } else if (instruction_bits == kCodeAgeJumpInstruction) {
1827     // The code age prologue has a constant immediatly following the jump
1828     // instruction.
1829     Instruction* target = Instruction::At(instr_ptr + Instruction::kInstrSize);
1830     DecodeType2(instr);
1831     SNPrintF(out_buffer_ + out_buffer_pos_,
1832              " (0x%08x)", target->InstructionBits());
1833     return 2 * Instruction::kInstrSize;
1834   }
1835   switch (instr->TypeValue()) {
1836     case 0:
1837     case 1: {
1838       DecodeType01(instr);
1839       break;
1840     }
1841     case 2: {
1842       DecodeType2(instr);
1843       break;
1844     }
1845     case 3: {
1846       DecodeType3(instr);
1847       break;
1848     }
1849     case 4: {
1850       DecodeType4(instr);
1851       break;
1852     }
1853     case 5: {
1854       DecodeType5(instr);
1855       break;
1856     }
1857     case 6: {
1858       DecodeType6(instr);
1859       break;
1860     }
1861     case 7: {
1862       return DecodeType7(instr);
1863     }
1864     default: {
1865       // The type field is 3-bits in the ARM encoding.
1866       UNREACHABLE();
1867       break;
1868     }
1869   }
1870   return Instruction::kInstrSize;
1871 }
1872
1873
1874 } }  // namespace v8::internal
1875
1876
1877
1878 //------------------------------------------------------------------------------
1879
1880 namespace disasm {
1881
1882
1883 const char* NameConverter::NameOfAddress(byte* addr) const {
1884   v8::internal::SNPrintF(tmp_buffer_, "%p", addr);
1885   return tmp_buffer_.start();
1886 }
1887
1888
1889 const char* NameConverter::NameOfConstant(byte* addr) const {
1890   return NameOfAddress(addr);
1891 }
1892
1893
1894 const char* NameConverter::NameOfCPURegister(int reg) const {
1895   return v8::internal::Registers::Name(reg);
1896 }
1897
1898
1899 const char* NameConverter::NameOfByteCPURegister(int reg) const {
1900   UNREACHABLE();  // ARM does not have the concept of a byte register
1901   return "nobytereg";
1902 }
1903
1904
1905 const char* NameConverter::NameOfXMMRegister(int reg) const {
1906   UNREACHABLE();  // ARM does not have any XMM registers
1907   return "noxmmreg";
1908 }
1909
1910
1911 const char* NameConverter::NameInCode(byte* addr) const {
1912   // The default name converter is called for unknown code. So we will not try
1913   // to access any memory.
1914   return "";
1915 }
1916
1917
1918 //------------------------------------------------------------------------------
1919
1920 Disassembler::Disassembler(const NameConverter& converter)
1921     : converter_(converter) {}
1922
1923
1924 Disassembler::~Disassembler() {}
1925
1926
1927 int Disassembler::InstructionDecode(v8::internal::Vector<char> buffer,
1928                                     byte* instruction) {
1929   v8::internal::Decoder d(converter_, buffer);
1930   return d.InstructionDecode(instruction);
1931 }
1932
1933
1934 int Disassembler::ConstantPoolSizeAt(byte* instruction) {
1935   return v8::internal::Decoder::ConstantPoolSizeAt(instruction);
1936 }
1937
1938
1939 void Disassembler::Disassemble(FILE* f, byte* begin, byte* end) {
1940   NameConverter converter;
1941   Disassembler d(converter);
1942   for (byte* pc = begin; pc < end;) {
1943     v8::internal::EmbeddedVector<char, 128> buffer;
1944     buffer[0] = '\0';
1945     byte* prev_pc = pc;
1946     pc += d.InstructionDecode(buffer, pc);
1947     v8::internal::PrintF(
1948         f, "%p    %08x      %s\n",
1949         prev_pc, *reinterpret_cast<int32_t*>(prev_pc), buffer.start());
1950   }
1951 }
1952
1953
1954 }  // namespace disasm
1955
1956 #endif  // V8_TARGET_ARCH_ARM