v8  3.14.5(node0.10.28)
V8 is Google's open source JavaScript engine
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
disasm-arm.cc
Go to the documentation of this file.
1 // Copyright 2011 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
4 // met:
5 //
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 // A Disassembler object is used to disassemble a block of code instruction by
29 // instruction. The default implementation of the NameConverter object can be
30 // overriden to modify register names or to do symbol lookup on addresses.
31 //
32 // The example below will disassemble a block of code and print it to stdout.
33 //
34 // NameConverter converter;
35 // Disassembler d(converter);
36 // for (byte* pc = begin; pc < end;) {
37 // v8::internal::EmbeddedVector<char, 256> buffer;
38 // byte* prev_pc = pc;
39 // pc += d.InstructionDecode(buffer, pc);
40 // printf("%p %08x %s\n",
41 // prev_pc, *reinterpret_cast<int32_t*>(prev_pc), buffer);
42 // }
43 //
44 // The Disassembler class also has a convenience method to disassemble a block
45 // of code into a FILE*, meaning that the above functionality could also be
46 // achieved by just calling Disassembler::Disassemble(stdout, begin, end);
47 
48 
49 #include <assert.h>
50 #include <stdio.h>
51 #include <stdarg.h>
52 #include <string.h>
53 #ifndef WIN32
54 #include <stdint.h>
55 #endif
56 
57 #include "v8.h"
58 
59 #if defined(V8_TARGET_ARCH_ARM)
60 
61 #include "constants-arm.h"
62 #include "disasm.h"
63 #include "macro-assembler.h"
64 #include "platform.h"
65 
66 
67 namespace v8 {
68 namespace internal {
69 
70 
71 //------------------------------------------------------------------------------
72 
73 // Decoder decodes and disassembles instructions into an output buffer.
74 // It uses the converter to convert register names and call destinations into
75 // more informative description.
76 class Decoder {
77  public:
78  Decoder(const disasm::NameConverter& converter,
79  Vector<char> out_buffer)
80  : converter_(converter),
81  out_buffer_(out_buffer),
82  out_buffer_pos_(0) {
83  out_buffer_[out_buffer_pos_] = '\0';
84  }
85 
86  ~Decoder() {}
87 
88  // Writes one disassembled instruction into 'buffer' (0-terminated).
89  // Returns the length of the disassembled machine instruction in bytes.
90  int InstructionDecode(byte* instruction);
91 
92  static bool IsConstantPoolAt(byte* instr_ptr);
93  static int ConstantPoolSizeAt(byte* instr_ptr);
94 
95  private:
96  // Bottleneck functions to print into the out_buffer.
97  void PrintChar(const char ch);
98  void Print(const char* str);
99 
100  // Printing of common values.
101  void PrintRegister(int reg);
102  void PrintSRegister(int reg);
103  void PrintDRegister(int reg);
104  int FormatVFPRegister(Instruction* instr, const char* format);
105  void PrintMovwMovt(Instruction* instr);
106  int FormatVFPinstruction(Instruction* instr, const char* format);
107  void PrintCondition(Instruction* instr);
108  void PrintShiftRm(Instruction* instr);
109  void PrintShiftImm(Instruction* instr);
110  void PrintShiftSat(Instruction* instr);
111  void PrintPU(Instruction* instr);
112  void PrintSoftwareInterrupt(SoftwareInterruptCodes svc);
113 
114  // Handle formatting of instructions and their options.
115  int FormatRegister(Instruction* instr, const char* option);
116  int FormatOption(Instruction* instr, const char* option);
117  void Format(Instruction* instr, const char* format);
118  void Unknown(Instruction* instr);
119 
120  // Each of these functions decodes one particular instruction type, a 3-bit
121  // field in the instruction encoding.
122  // Types 0 and 1 are combined as they are largely the same except for the way
123  // they interpret the shifter operand.
124  void DecodeType01(Instruction* instr);
125  void DecodeType2(Instruction* instr);
126  void DecodeType3(Instruction* instr);
127  void DecodeType4(Instruction* instr);
128  void DecodeType5(Instruction* instr);
129  void DecodeType6(Instruction* instr);
130  // Type 7 includes special Debugger instructions.
131  int DecodeType7(Instruction* instr);
132  // For VFP support.
133  void DecodeTypeVFP(Instruction* instr);
134  void DecodeType6CoprocessorIns(Instruction* instr);
135 
136  void DecodeVMOVBetweenCoreAndSinglePrecisionRegisters(Instruction* instr);
137  void DecodeVCMP(Instruction* instr);
138  void DecodeVCVTBetweenDoubleAndSingle(Instruction* instr);
139  void DecodeVCVTBetweenFloatingPointAndInteger(Instruction* instr);
140 
141  const disasm::NameConverter& converter_;
142  Vector<char> out_buffer_;
143  int out_buffer_pos_;
144 
145  DISALLOW_COPY_AND_ASSIGN(Decoder);
146 };
147 
148 
149 // Support for assertions in the Decoder formatting functions.
150 #define STRING_STARTS_WITH(string, compare_string) \
151  (strncmp(string, compare_string, strlen(compare_string)) == 0)
152 
153 
154 // Append the ch to the output buffer.
155 void Decoder::PrintChar(const char ch) {
156  out_buffer_[out_buffer_pos_++] = ch;
157 }
158 
159 
160 // Append the str to the output buffer.
161 void Decoder::Print(const char* str) {
162  char cur = *str++;
163  while (cur != '\0' && (out_buffer_pos_ < (out_buffer_.length() - 1))) {
164  PrintChar(cur);
165  cur = *str++;
166  }
167  out_buffer_[out_buffer_pos_] = 0;
168 }
169 
170 
171 // These condition names are defined in a way to match the native disassembler
172 // formatting. See for example the command "objdump -d <binary file>".
173 static const char* cond_names[kNumberOfConditions] = {
174  "eq", "ne", "cs" , "cc" , "mi" , "pl" , "vs" , "vc" ,
175  "hi", "ls", "ge", "lt", "gt", "le", "", "invalid",
176 };
177 
178 
179 // Print the condition guarding the instruction.
180 void Decoder::PrintCondition(Instruction* instr) {
181  Print(cond_names[instr->ConditionValue()]);
182 }
183 
184 
185 // Print the register name according to the active name converter.
186 void Decoder::PrintRegister(int reg) {
187  Print(converter_.NameOfCPURegister(reg));
188 }
189 
190 // Print the VFP S register name according to the active name converter.
191 void Decoder::PrintSRegister(int reg) {
192  Print(VFPRegisters::Name(reg, false));
193 }
194 
195 // Print the VFP D register name according to the active name converter.
196 void Decoder::PrintDRegister(int reg) {
197  Print(VFPRegisters::Name(reg, true));
198 }
199 
200 
201 // These shift names are defined in a way to match the native disassembler
202 // formatting. See for example the command "objdump -d <binary file>".
203 static const char* const shift_names[kNumberOfShifts] = {
204  "lsl", "lsr", "asr", "ror"
205 };
206 
207 
208 // Print the register shift operands for the instruction. Generally used for
209 // data processing instructions.
210 void Decoder::PrintShiftRm(Instruction* instr) {
211  ShiftOp shift = instr->ShiftField();
212  int shift_index = instr->ShiftValue();
213  int shift_amount = instr->ShiftAmountValue();
214  int rm = instr->RmValue();
215 
216  PrintRegister(rm);
217 
218  if ((instr->RegShiftValue() == 0) && (shift == LSL) && (shift_amount == 0)) {
219  // Special case for using rm only.
220  return;
221  }
222  if (instr->RegShiftValue() == 0) {
223  // by immediate
224  if ((shift == ROR) && (shift_amount == 0)) {
225  Print(", RRX");
226  return;
227  } else if (((shift == LSR) || (shift == ASR)) && (shift_amount == 0)) {
228  shift_amount = 32;
229  }
230  out_buffer_pos_ += OS::SNPrintF(out_buffer_ + out_buffer_pos_,
231  ", %s #%d",
232  shift_names[shift_index],
233  shift_amount);
234  } else {
235  // by register
236  int rs = instr->RsValue();
237  out_buffer_pos_ += OS::SNPrintF(out_buffer_ + out_buffer_pos_,
238  ", %s ", shift_names[shift_index]);
239  PrintRegister(rs);
240  }
241 }
242 
243 
244 // Print the immediate operand for the instruction. Generally used for data
245 // processing instructions.
246 void Decoder::PrintShiftImm(Instruction* instr) {
247  int rotate = instr->RotateValue() * 2;
248  int immed8 = instr->Immed8Value();
249  int imm = (immed8 >> rotate) | (immed8 << (32 - rotate));
250  out_buffer_pos_ += OS::SNPrintF(out_buffer_ + out_buffer_pos_,
251  "#%d", imm);
252 }
253 
254 
255 // Print the optional shift and immediate used by saturating instructions.
256 void Decoder::PrintShiftSat(Instruction* instr) {
257  int shift = instr->Bits(11, 7);
258  if (shift > 0) {
259  out_buffer_pos_ += OS::SNPrintF(out_buffer_ + out_buffer_pos_,
260  ", %s #%d",
261  shift_names[instr->Bit(6) * 2],
262  instr->Bits(11, 7));
263  }
264 }
265 
266 
267 // Print PU formatting to reduce complexity of FormatOption.
268 void Decoder::PrintPU(Instruction* instr) {
269  switch (instr->PUField()) {
270  case da_x: {
271  Print("da");
272  break;
273  }
274  case ia_x: {
275  Print("ia");
276  break;
277  }
278  case db_x: {
279  Print("db");
280  break;
281  }
282  case ib_x: {
283  Print("ib");
284  break;
285  }
286  default: {
287  UNREACHABLE();
288  break;
289  }
290  }
291 }
292 
293 
294 // Print SoftwareInterrupt codes. Factoring this out reduces the complexity of
295 // the FormatOption method.
296 void Decoder::PrintSoftwareInterrupt(SoftwareInterruptCodes svc) {
297  switch (svc) {
298  case kCallRtRedirected:
299  Print("call rt redirected");
300  return;
301  case kBreakpoint:
302  Print("breakpoint");
303  return;
304  default:
305  if (svc >= kStopCode) {
306  out_buffer_pos_ += OS::SNPrintF(out_buffer_ + out_buffer_pos_,
307  "%d - 0x%x",
308  svc & kStopCodeMask,
309  svc & kStopCodeMask);
310  } else {
311  out_buffer_pos_ += OS::SNPrintF(out_buffer_ + out_buffer_pos_,
312  "%d",
313  svc);
314  }
315  return;
316  }
317 }
318 
319 
320 // Handle all register based formatting in this function to reduce the
321 // complexity of FormatOption.
322 int Decoder::FormatRegister(Instruction* instr, const char* format) {
323  ASSERT(format[0] == 'r');
324  if (format[1] == 'n') { // 'rn: Rn register
325  int reg = instr->RnValue();
326  PrintRegister(reg);
327  return 2;
328  } else if (format[1] == 'd') { // 'rd: Rd register
329  int reg = instr->RdValue();
330  PrintRegister(reg);
331  return 2;
332  } else if (format[1] == 's') { // 'rs: Rs register
333  int reg = instr->RsValue();
334  PrintRegister(reg);
335  return 2;
336  } else if (format[1] == 'm') { // 'rm: Rm register
337  int reg = instr->RmValue();
338  PrintRegister(reg);
339  return 2;
340  } else if (format[1] == 't') { // 'rt: Rt register
341  int reg = instr->RtValue();
342  PrintRegister(reg);
343  return 2;
344  } else if (format[1] == 'l') {
345  // 'rlist: register list for load and store multiple instructions
346  ASSERT(STRING_STARTS_WITH(format, "rlist"));
347  int rlist = instr->RlistValue();
348  int reg = 0;
349  Print("{");
350  // Print register list in ascending order, by scanning the bit mask.
351  while (rlist != 0) {
352  if ((rlist & 1) != 0) {
353  PrintRegister(reg);
354  if ((rlist >> 1) != 0) {
355  Print(", ");
356  }
357  }
358  reg++;
359  rlist >>= 1;
360  }
361  Print("}");
362  return 5;
363  }
364  UNREACHABLE();
365  return -1;
366 }
367 
368 
369 // Handle all VFP register based formatting in this function to reduce the
370 // complexity of FormatOption.
371 int Decoder::FormatVFPRegister(Instruction* instr, const char* format) {
372  ASSERT((format[0] == 'S') || (format[0] == 'D'));
373 
374  VFPRegPrecision precision =
375  format[0] == 'D' ? kDoublePrecision : kSinglePrecision;
376 
377  int retval = 2;
378  int reg = -1;
379  if (format[1] == 'n') {
380  reg = instr->VFPNRegValue(precision);
381  } else if (format[1] == 'm') {
382  reg = instr->VFPMRegValue(precision);
383  } else if (format[1] == 'd') {
384  reg = instr->VFPDRegValue(precision);
385  if (format[2] == '+') {
386  int immed8 = instr->Immed8Value();
387  if (format[0] == 'S') reg += immed8 - 1;
388  if (format[0] == 'D') reg += (immed8 / 2 - 1);
389  }
390  if (format[2] == '+') retval = 3;
391  } else {
392  UNREACHABLE();
393  }
394 
395  if (precision == kSinglePrecision) {
396  PrintSRegister(reg);
397  } else {
398  PrintDRegister(reg);
399  }
400 
401  return retval;
402 }
403 
404 
405 int Decoder::FormatVFPinstruction(Instruction* instr, const char* format) {
406  Print(format);
407  return 0;
408 }
409 
410 
411 // Print the movw or movt instruction.
412 void Decoder::PrintMovwMovt(Instruction* instr) {
413  int imm = instr->ImmedMovwMovtValue();
414  int rd = instr->RdValue();
415  PrintRegister(rd);
416  out_buffer_pos_ += OS::SNPrintF(out_buffer_ + out_buffer_pos_,
417  ", #%d", imm);
418 }
419 
420 
421 // FormatOption takes a formatting string and interprets it based on
422 // the current instructions. The format string points to the first
423 // character of the option string (the option escape has already been
424 // consumed by the caller.) FormatOption returns the number of
425 // characters that were consumed from the formatting string.
426 int Decoder::FormatOption(Instruction* instr, const char* format) {
427  switch (format[0]) {
428  case 'a': { // 'a: accumulate multiplies
429  if (instr->Bit(21) == 0) {
430  Print("ul");
431  } else {
432  Print("la");
433  }
434  return 1;
435  }
436  case 'b': { // 'b: byte loads or stores
437  if (instr->HasB()) {
438  Print("b");
439  }
440  return 1;
441  }
442  case 'c': { // 'cond: conditional execution
443  ASSERT(STRING_STARTS_WITH(format, "cond"));
444  PrintCondition(instr);
445  return 4;
446  }
447  case 'd': { // 'd: vmov double immediate.
448  double d = instr->DoubleImmedVmov();
449  out_buffer_pos_ += OS::SNPrintF(out_buffer_ + out_buffer_pos_,
450  "#%g", d);
451  return 1;
452  }
453  case 'f': { // 'f: bitfield instructions - v7 and above.
454  uint32_t lsbit = instr->Bits(11, 7);
455  uint32_t width = instr->Bits(20, 16) + 1;
456  if (instr->Bit(21) == 0) {
457  // BFC/BFI:
458  // Bits 20-16 represent most-significant bit. Covert to width.
459  width -= lsbit;
460  ASSERT(width > 0);
461  }
462  ASSERT((width + lsbit) <= 32);
463  out_buffer_pos_ += OS::SNPrintF(out_buffer_ + out_buffer_pos_,
464  "#%d, #%d", lsbit, width);
465  return 1;
466  }
467  case 'h': { // 'h: halfword operation for extra loads and stores
468  if (instr->HasH()) {
469  Print("h");
470  } else {
471  Print("b");
472  }
473  return 1;
474  }
475  case 'i': { // 'i: immediate value from adjacent bits.
476  // Expects tokens in the form imm%02d@%02d, i.e. imm05@07, imm10@16
477  int width = (format[3] - '0') * 10 + (format[4] - '0');
478  int lsb = (format[6] - '0') * 10 + (format[7] - '0');
479 
480  ASSERT((width >= 1) && (width <= 32));
481  ASSERT((lsb >= 0) && (lsb <= 31));
482  ASSERT((width + lsb) <= 32);
483 
484  out_buffer_pos_ += OS::SNPrintF(out_buffer_ + out_buffer_pos_,
485  "%d",
486  instr->Bits(width + lsb - 1, lsb));
487  return 8;
488  }
489  case 'l': { // 'l: branch and link
490  if (instr->HasLink()) {
491  Print("l");
492  }
493  return 1;
494  }
495  case 'm': {
496  if (format[1] == 'w') {
497  // 'mw: movt/movw instructions.
498  PrintMovwMovt(instr);
499  return 2;
500  }
501  if (format[1] == 'e') { // 'memop: load/store instructions.
502  ASSERT(STRING_STARTS_WITH(format, "memop"));
503  if (instr->HasL()) {
504  Print("ldr");
505  } else {
506  if ((instr->Bits(27, 25) == 0) && (instr->Bit(20) == 0) &&
507  (instr->Bits(7, 6) == 3) && (instr->Bit(4) == 1)) {
508  if (instr->Bit(5) == 1) {
509  Print("strd");
510  } else {
511  Print("ldrd");
512  }
513  return 5;
514  }
515  Print("str");
516  }
517  return 5;
518  }
519  // 'msg: for simulator break instructions
520  ASSERT(STRING_STARTS_WITH(format, "msg"));
521  byte* str =
522  reinterpret_cast<byte*>(instr->InstructionBits() & 0x0fffffff);
523  out_buffer_pos_ += OS::SNPrintF(out_buffer_ + out_buffer_pos_,
524  "%s", converter_.NameInCode(str));
525  return 3;
526  }
527  case 'o': {
528  if ((format[3] == '1') && (format[4] == '2')) {
529  // 'off12: 12-bit offset for load and store instructions
530  ASSERT(STRING_STARTS_WITH(format, "off12"));
531  out_buffer_pos_ += OS::SNPrintF(out_buffer_ + out_buffer_pos_,
532  "%d", instr->Offset12Value());
533  return 5;
534  } else if (format[3] == '0') {
535  // 'off0to3and8to19 16-bit immediate encoded in bits 19-8 and 3-0.
536  ASSERT(STRING_STARTS_WITH(format, "off0to3and8to19"));
537  out_buffer_pos_ += OS::SNPrintF(out_buffer_ + out_buffer_pos_,
538  "%d",
539  (instr->Bits(19, 8) << 4) +
540  instr->Bits(3, 0));
541  return 15;
542  }
543  // 'off8: 8-bit offset for extra load and store instructions
544  ASSERT(STRING_STARTS_WITH(format, "off8"));
545  int offs8 = (instr->ImmedHValue() << 4) | instr->ImmedLValue();
546  out_buffer_pos_ += OS::SNPrintF(out_buffer_ + out_buffer_pos_,
547  "%d", offs8);
548  return 4;
549  }
550  case 'p': { // 'pu: P and U bits for load and store instructions
551  ASSERT(STRING_STARTS_WITH(format, "pu"));
552  PrintPU(instr);
553  return 2;
554  }
555  case 'r': {
556  return FormatRegister(instr, format);
557  }
558  case 's': {
559  if (format[1] == 'h') { // 'shift_op or 'shift_rm or 'shift_sat.
560  if (format[6] == 'o') { // 'shift_op
561  ASSERT(STRING_STARTS_WITH(format, "shift_op"));
562  if (instr->TypeValue() == 0) {
563  PrintShiftRm(instr);
564  } else {
565  ASSERT(instr->TypeValue() == 1);
566  PrintShiftImm(instr);
567  }
568  return 8;
569  } else if (format[6] == 's') { // 'shift_sat.
570  ASSERT(STRING_STARTS_WITH(format, "shift_sat"));
571  PrintShiftSat(instr);
572  return 9;
573  } else { // 'shift_rm
574  ASSERT(STRING_STARTS_WITH(format, "shift_rm"));
575  PrintShiftRm(instr);
576  return 8;
577  }
578  } else if (format[1] == 'v') { // 'svc
579  ASSERT(STRING_STARTS_WITH(format, "svc"));
580  PrintSoftwareInterrupt(instr->SvcValue());
581  return 3;
582  } else if (format[1] == 'i') { // 'sign: signed extra loads and stores
583  ASSERT(STRING_STARTS_WITH(format, "sign"));
584  if (instr->HasSign()) {
585  Print("s");
586  }
587  return 4;
588  }
589  // 's: S field of data processing instructions
590  if (instr->HasS()) {
591  Print("s");
592  }
593  return 1;
594  }
595  case 't': { // 'target: target of branch instructions
596  ASSERT(STRING_STARTS_WITH(format, "target"));
597  int off = (instr->SImmed24Value() << 2) + 8;
598  out_buffer_pos_ += OS::SNPrintF(out_buffer_ + out_buffer_pos_,
599  "%+d -> %s",
600  off,
601  converter_.NameOfAddress(
602  reinterpret_cast<byte*>(instr) + off));
603  return 6;
604  }
605  case 'u': { // 'u: signed or unsigned multiplies
606  // The manual gets the meaning of bit 22 backwards in the multiply
607  // instruction overview on page A3.16.2. The instructions that
608  // exist in u and s variants are the following:
609  // smull A4.1.87
610  // umull A4.1.129
611  // umlal A4.1.128
612  // smlal A4.1.76
613  // For these 0 means u and 1 means s. As can be seen on their individual
614  // pages. The other 18 mul instructions have the bit set or unset in
615  // arbitrary ways that are unrelated to the signedness of the instruction.
616  // None of these 18 instructions exist in both a 'u' and an 's' variant.
617 
618  if (instr->Bit(22) == 0) {
619  Print("u");
620  } else {
621  Print("s");
622  }
623  return 1;
624  }
625  case 'v': {
626  return FormatVFPinstruction(instr, format);
627  }
628  case 'S':
629  case 'D': {
630  return FormatVFPRegister(instr, format);
631  }
632  case 'w': { // 'w: W field of load and store instructions
633  if (instr->HasW()) {
634  Print("!");
635  }
636  return 1;
637  }
638  default: {
639  UNREACHABLE();
640  break;
641  }
642  }
643  UNREACHABLE();
644  return -1;
645 }
646 
647 
648 // Format takes a formatting string for a whole instruction and prints it into
649 // the output buffer. All escaped options are handed to FormatOption to be
650 // parsed further.
651 void Decoder::Format(Instruction* instr, const char* format) {
652  char cur = *format++;
653  while ((cur != 0) && (out_buffer_pos_ < (out_buffer_.length() - 1))) {
654  if (cur == '\'') { // Single quote is used as the formatting escape.
655  format += FormatOption(instr, format);
656  } else {
657  out_buffer_[out_buffer_pos_++] = cur;
658  }
659  cur = *format++;
660  }
661  out_buffer_[out_buffer_pos_] = '\0';
662 }
663 
664 
665 // The disassembler may end up decoding data inlined in the code. We do not want
666 // it to crash if the data does not ressemble any known instruction.
667 #define VERIFY(condition) \
668 if(!(condition)) { \
669  Unknown(instr); \
670  return; \
671 }
672 
673 
674 // For currently unimplemented decodings the disassembler calls Unknown(instr)
675 // which will just print "unknown" of the instruction bits.
676 void Decoder::Unknown(Instruction* instr) {
677  Format(instr, "unknown");
678 }
679 
680 
681 void Decoder::DecodeType01(Instruction* instr) {
682  int type = instr->TypeValue();
683  if ((type == 0) && instr->IsSpecialType0()) {
684  // multiply instruction or extra loads and stores
685  if (instr->Bits(7, 4) == 9) {
686  if (instr->Bit(24) == 0) {
687  // multiply instructions
688  if (instr->Bit(23) == 0) {
689  if (instr->Bit(21) == 0) {
690  // The MUL instruction description (A 4.1.33) refers to Rd as being
691  // the destination for the operation, but it confusingly uses the
692  // Rn field to encode it.
693  Format(instr, "mul'cond's 'rn, 'rm, 'rs");
694  } else {
695  if (instr->Bit(22) == 0) {
696  // The MLA instruction description (A 4.1.28) refers to the order
697  // of registers as "Rd, Rm, Rs, Rn". But confusingly it uses the
698  // Rn field to encode the Rd register and the Rd field to encode
699  // the Rn register.
700  Format(instr, "mla'cond's 'rn, 'rm, 'rs, 'rd");
701  } else {
702  // The MLS instruction description (A 4.1.29) refers to the order
703  // of registers as "Rd, Rm, Rs, Rn". But confusingly it uses the
704  // Rn field to encode the Rd register and the Rd field to encode
705  // the Rn register.
706  Format(instr, "mls'cond's 'rn, 'rm, 'rs, 'rd");
707  }
708  }
709  } else {
710  // The signed/long multiply instructions use the terms RdHi and RdLo
711  // when referring to the target registers. They are mapped to the Rn
712  // and Rd fields as follows:
713  // RdLo == Rd field
714  // RdHi == Rn field
715  // The order of registers is: <RdLo>, <RdHi>, <Rm>, <Rs>
716  Format(instr, "'um'al'cond's 'rd, 'rn, 'rm, 'rs");
717  }
718  } else {
719  Unknown(instr); // not used by V8
720  }
721  } else if ((instr->Bit(20) == 0) && ((instr->Bits(7, 4) & 0xd) == 0xd)) {
722  // ldrd, strd
723  switch (instr->PUField()) {
724  case da_x: {
725  if (instr->Bit(22) == 0) {
726  Format(instr, "'memop'cond's 'rd, ['rn], -'rm");
727  } else {
728  Format(instr, "'memop'cond's 'rd, ['rn], #-'off8");
729  }
730  break;
731  }
732  case ia_x: {
733  if (instr->Bit(22) == 0) {
734  Format(instr, "'memop'cond's 'rd, ['rn], +'rm");
735  } else {
736  Format(instr, "'memop'cond's 'rd, ['rn], #+'off8");
737  }
738  break;
739  }
740  case db_x: {
741  if (instr->Bit(22) == 0) {
742  Format(instr, "'memop'cond's 'rd, ['rn, -'rm]'w");
743  } else {
744  Format(instr, "'memop'cond's 'rd, ['rn, #-'off8]'w");
745  }
746  break;
747  }
748  case ib_x: {
749  if (instr->Bit(22) == 0) {
750  Format(instr, "'memop'cond's 'rd, ['rn, +'rm]'w");
751  } else {
752  Format(instr, "'memop'cond's 'rd, ['rn, #+'off8]'w");
753  }
754  break;
755  }
756  default: {
757  // The PU field is a 2-bit field.
758  UNREACHABLE();
759  break;
760  }
761  }
762  } else {
763  // extra load/store instructions
764  switch (instr->PUField()) {
765  case da_x: {
766  if (instr->Bit(22) == 0) {
767  Format(instr, "'memop'cond'sign'h 'rd, ['rn], -'rm");
768  } else {
769  Format(instr, "'memop'cond'sign'h 'rd, ['rn], #-'off8");
770  }
771  break;
772  }
773  case ia_x: {
774  if (instr->Bit(22) == 0) {
775  Format(instr, "'memop'cond'sign'h 'rd, ['rn], +'rm");
776  } else {
777  Format(instr, "'memop'cond'sign'h 'rd, ['rn], #+'off8");
778  }
779  break;
780  }
781  case db_x: {
782  if (instr->Bit(22) == 0) {
783  Format(instr, "'memop'cond'sign'h 'rd, ['rn, -'rm]'w");
784  } else {
785  Format(instr, "'memop'cond'sign'h 'rd, ['rn, #-'off8]'w");
786  }
787  break;
788  }
789  case ib_x: {
790  if (instr->Bit(22) == 0) {
791  Format(instr, "'memop'cond'sign'h 'rd, ['rn, +'rm]'w");
792  } else {
793  Format(instr, "'memop'cond'sign'h 'rd, ['rn, #+'off8]'w");
794  }
795  break;
796  }
797  default: {
798  // The PU field is a 2-bit field.
799  UNREACHABLE();
800  break;
801  }
802  }
803  return;
804  }
805  } else if ((type == 0) && instr->IsMiscType0()) {
806  if (instr->Bits(22, 21) == 1) {
807  switch (instr->BitField(7, 4)) {
808  case BX:
809  Format(instr, "bx'cond 'rm");
810  break;
811  case BLX:
812  Format(instr, "blx'cond 'rm");
813  break;
814  case BKPT:
815  Format(instr, "bkpt 'off0to3and8to19");
816  break;
817  default:
818  Unknown(instr); // not used by V8
819  break;
820  }
821  } else if (instr->Bits(22, 21) == 3) {
822  switch (instr->BitField(7, 4)) {
823  case CLZ:
824  Format(instr, "clz'cond 'rd, 'rm");
825  break;
826  default:
827  Unknown(instr); // not used by V8
828  break;
829  }
830  } else {
831  Unknown(instr); // not used by V8
832  }
833  } else if ((type == 1) && instr->IsNopType1()) {
834  Format(instr, "nop'cond");
835  } else {
836  switch (instr->OpcodeField()) {
837  case AND: {
838  Format(instr, "and'cond's 'rd, 'rn, 'shift_op");
839  break;
840  }
841  case EOR: {
842  Format(instr, "eor'cond's 'rd, 'rn, 'shift_op");
843  break;
844  }
845  case SUB: {
846  Format(instr, "sub'cond's 'rd, 'rn, 'shift_op");
847  break;
848  }
849  case RSB: {
850  Format(instr, "rsb'cond's 'rd, 'rn, 'shift_op");
851  break;
852  }
853  case ADD: {
854  Format(instr, "add'cond's 'rd, 'rn, 'shift_op");
855  break;
856  }
857  case ADC: {
858  Format(instr, "adc'cond's 'rd, 'rn, 'shift_op");
859  break;
860  }
861  case SBC: {
862  Format(instr, "sbc'cond's 'rd, 'rn, 'shift_op");
863  break;
864  }
865  case RSC: {
866  Format(instr, "rsc'cond's 'rd, 'rn, 'shift_op");
867  break;
868  }
869  case TST: {
870  if (instr->HasS()) {
871  Format(instr, "tst'cond 'rn, 'shift_op");
872  } else {
873  Format(instr, "movw'cond 'mw");
874  }
875  break;
876  }
877  case TEQ: {
878  if (instr->HasS()) {
879  Format(instr, "teq'cond 'rn, 'shift_op");
880  } else {
881  // Other instructions matching this pattern are handled in the
882  // miscellaneous instructions part above.
883  UNREACHABLE();
884  }
885  break;
886  }
887  case CMP: {
888  if (instr->HasS()) {
889  Format(instr, "cmp'cond 'rn, 'shift_op");
890  } else {
891  Format(instr, "movt'cond 'mw");
892  }
893  break;
894  }
895  case CMN: {
896  if (instr->HasS()) {
897  Format(instr, "cmn'cond 'rn, 'shift_op");
898  } else {
899  // Other instructions matching this pattern are handled in the
900  // miscellaneous instructions part above.
901  UNREACHABLE();
902  }
903  break;
904  }
905  case ORR: {
906  Format(instr, "orr'cond's 'rd, 'rn, 'shift_op");
907  break;
908  }
909  case MOV: {
910  Format(instr, "mov'cond's 'rd, 'shift_op");
911  break;
912  }
913  case BIC: {
914  Format(instr, "bic'cond's 'rd, 'rn, 'shift_op");
915  break;
916  }
917  case MVN: {
918  Format(instr, "mvn'cond's 'rd, 'shift_op");
919  break;
920  }
921  default: {
922  // The Opcode field is a 4-bit field.
923  UNREACHABLE();
924  break;
925  }
926  }
927  }
928 }
929 
930 
931 void Decoder::DecodeType2(Instruction* instr) {
932  switch (instr->PUField()) {
933  case da_x: {
934  if (instr->HasW()) {
935  Unknown(instr); // not used in V8
936  return;
937  }
938  Format(instr, "'memop'cond'b 'rd, ['rn], #-'off12");
939  break;
940  }
941  case ia_x: {
942  if (instr->HasW()) {
943  Unknown(instr); // not used in V8
944  return;
945  }
946  Format(instr, "'memop'cond'b 'rd, ['rn], #+'off12");
947  break;
948  }
949  case db_x: {
950  Format(instr, "'memop'cond'b 'rd, ['rn, #-'off12]'w");
951  break;
952  }
953  case ib_x: {
954  Format(instr, "'memop'cond'b 'rd, ['rn, #+'off12]'w");
955  break;
956  }
957  default: {
958  // The PU field is a 2-bit field.
959  UNREACHABLE();
960  break;
961  }
962  }
963 }
964 
965 
966 void Decoder::DecodeType3(Instruction* instr) {
967  switch (instr->PUField()) {
968  case da_x: {
969  VERIFY(!instr->HasW());
970  Format(instr, "'memop'cond'b 'rd, ['rn], -'shift_rm");
971  break;
972  }
973  case ia_x: {
974  if (instr->HasW()) {
975  VERIFY(instr->Bits(5, 4) == 0x1);
976  if (instr->Bit(22) == 0x1) {
977  Format(instr, "usat 'rd, #'imm05@16, 'rm'shift_sat");
978  } else {
979  UNREACHABLE(); // SSAT.
980  }
981  } else {
982  Format(instr, "'memop'cond'b 'rd, ['rn], +'shift_rm");
983  }
984  break;
985  }
986  case db_x: {
987  if (FLAG_enable_sudiv) {
988  if (!instr->HasW()) {
989  if (instr->Bits(5, 4) == 0x1) {
990  if ((instr->Bit(22) == 0x0) && (instr->Bit(20) == 0x1)) {
991  // SDIV (in V8 notation matching ARM ISA format) rn = rm/rs
992  Format(instr, "sdiv'cond'b 'rn, 'rm, 'rs");
993  break;
994  }
995  }
996  }
997  }
998  Format(instr, "'memop'cond'b 'rd, ['rn, -'shift_rm]'w");
999  break;
1000  }
1001  case ib_x: {
1002  if (instr->HasW() && (instr->Bits(6, 4) == 0x5)) {
1003  uint32_t widthminus1 = static_cast<uint32_t>(instr->Bits(20, 16));
1004  uint32_t lsbit = static_cast<uint32_t>(instr->Bits(11, 7));
1005  uint32_t msbit = widthminus1 + lsbit;
1006  if (msbit <= 31) {
1007  if (instr->Bit(22)) {
1008  Format(instr, "ubfx'cond 'rd, 'rm, 'f");
1009  } else {
1010  Format(instr, "sbfx'cond 'rd, 'rm, 'f");
1011  }
1012  } else {
1013  UNREACHABLE();
1014  }
1015  } else if (!instr->HasW() && (instr->Bits(6, 4) == 0x1)) {
1016  uint32_t lsbit = static_cast<uint32_t>(instr->Bits(11, 7));
1017  uint32_t msbit = static_cast<uint32_t>(instr->Bits(20, 16));
1018  if (msbit >= lsbit) {
1019  if (instr->RmValue() == 15) {
1020  Format(instr, "bfc'cond 'rd, 'f");
1021  } else {
1022  Format(instr, "bfi'cond 'rd, 'rm, 'f");
1023  }
1024  } else {
1025  UNREACHABLE();
1026  }
1027  } else {
1028  Format(instr, "'memop'cond'b 'rd, ['rn, +'shift_rm]'w");
1029  }
1030  break;
1031  }
1032  default: {
1033  // The PU field is a 2-bit field.
1034  UNREACHABLE();
1035  break;
1036  }
1037  }
1038 }
1039 
1040 
1041 void Decoder::DecodeType4(Instruction* instr) {
1042  if (instr->Bit(22) != 0) {
1043  // Privileged mode currently not supported.
1044  Unknown(instr);
1045  } else {
1046  if (instr->HasL()) {
1047  Format(instr, "ldm'cond'pu 'rn'w, 'rlist");
1048  } else {
1049  Format(instr, "stm'cond'pu 'rn'w, 'rlist");
1050  }
1051  }
1052 }
1053 
1054 
1055 void Decoder::DecodeType5(Instruction* instr) {
1056  Format(instr, "b'l'cond 'target");
1057 }
1058 
1059 
1060 void Decoder::DecodeType6(Instruction* instr) {
1061  DecodeType6CoprocessorIns(instr);
1062 }
1063 
1064 
1065 int Decoder::DecodeType7(Instruction* instr) {
1066  if (instr->Bit(24) == 1) {
1067  if (instr->SvcValue() >= kStopCode) {
1068  Format(instr, "stop'cond 'svc");
1069  // Also print the stop message. Its address is encoded
1070  // in the following 4 bytes.
1071  out_buffer_pos_ += OS::SNPrintF(out_buffer_ + out_buffer_pos_,
1072  "\n %p %08x stop message: %s",
1073  reinterpret_cast<int32_t*>(instr
1075  *reinterpret_cast<char**>(instr
1077  *reinterpret_cast<char**>(instr
1079  // We have decoded 2 * Instruction::kInstrSize bytes.
1080  return 2 * Instruction::kInstrSize;
1081  } else {
1082  Format(instr, "svc'cond 'svc");
1083  }
1084  } else {
1085  DecodeTypeVFP(instr);
1086  }
1087  return Instruction::kInstrSize;
1088 }
1089 
1090 
1091 // void Decoder::DecodeTypeVFP(Instruction* instr)
1092 // vmov: Sn = Rt
1093 // vmov: Rt = Sn
1094 // vcvt: Dd = Sm
1095 // vcvt: Sd = Dm
1096 // Dd = vabs(Dm)
1097 // Dd = vneg(Dm)
1098 // Dd = vadd(Dn, Dm)
1099 // Dd = vsub(Dn, Dm)
1100 // Dd = vmul(Dn, Dm)
1101 // Dd = vdiv(Dn, Dm)
1102 // vcmp(Dd, Dm)
1103 // vmrs
1104 // vmsr
1105 // Dd = vsqrt(Dm)
1106 void Decoder::DecodeTypeVFP(Instruction* instr) {
1107  VERIFY((instr->TypeValue() == 7) && (instr->Bit(24) == 0x0) );
1108  VERIFY(instr->Bits(11, 9) == 0x5);
1109 
1110  if (instr->Bit(4) == 0) {
1111  if (instr->Opc1Value() == 0x7) {
1112  // Other data processing instructions
1113  if ((instr->Opc2Value() == 0x0) && (instr->Opc3Value() == 0x1)) {
1114  // vmov register to register.
1115  if (instr->SzValue() == 0x1) {
1116  Format(instr, "vmov.f64'cond 'Dd, 'Dm");
1117  } else {
1118  Format(instr, "vmov.f32'cond 'Sd, 'Sm");
1119  }
1120  } else if ((instr->Opc2Value() == 0x0) && (instr->Opc3Value() == 0x3)) {
1121  // vabs
1122  Format(instr, "vabs.f64'cond 'Dd, 'Dm");
1123  } else if ((instr->Opc2Value() == 0x1) && (instr->Opc3Value() == 0x1)) {
1124  // vneg
1125  Format(instr, "vneg.f64'cond 'Dd, 'Dm");
1126  } else if ((instr->Opc2Value() == 0x7) && (instr->Opc3Value() == 0x3)) {
1127  DecodeVCVTBetweenDoubleAndSingle(instr);
1128  } else if ((instr->Opc2Value() == 0x8) && (instr->Opc3Value() & 0x1)) {
1129  DecodeVCVTBetweenFloatingPointAndInteger(instr);
1130  } else if (((instr->Opc2Value() >> 1) == 0x6) &&
1131  (instr->Opc3Value() & 0x1)) {
1132  DecodeVCVTBetweenFloatingPointAndInteger(instr);
1133  } else if (((instr->Opc2Value() == 0x4) || (instr->Opc2Value() == 0x5)) &&
1134  (instr->Opc3Value() & 0x1)) {
1135  DecodeVCMP(instr);
1136  } else if (((instr->Opc2Value() == 0x1)) && (instr->Opc3Value() == 0x3)) {
1137  Format(instr, "vsqrt.f64'cond 'Dd, 'Dm");
1138  } else if (instr->Opc3Value() == 0x0) {
1139  if (instr->SzValue() == 0x1) {
1140  Format(instr, "vmov.f64'cond 'Dd, 'd");
1141  } else {
1142  Unknown(instr); // Not used by V8.
1143  }
1144  } else {
1145  Unknown(instr); // Not used by V8.
1146  }
1147  } else if (instr->Opc1Value() == 0x3) {
1148  if (instr->SzValue() == 0x1) {
1149  if (instr->Opc3Value() & 0x1) {
1150  Format(instr, "vsub.f64'cond 'Dd, 'Dn, 'Dm");
1151  } else {
1152  Format(instr, "vadd.f64'cond 'Dd, 'Dn, 'Dm");
1153  }
1154  } else {
1155  Unknown(instr); // Not used by V8.
1156  }
1157  } else if ((instr->Opc1Value() == 0x2) && !(instr->Opc3Value() & 0x1)) {
1158  if (instr->SzValue() == 0x1) {
1159  Format(instr, "vmul.f64'cond 'Dd, 'Dn, 'Dm");
1160  } else {
1161  Unknown(instr); // Not used by V8.
1162  }
1163  } else if ((instr->Opc1Value() == 0x4) && !(instr->Opc3Value() & 0x1)) {
1164  if (instr->SzValue() == 0x1) {
1165  Format(instr, "vdiv.f64'cond 'Dd, 'Dn, 'Dm");
1166  } else {
1167  Unknown(instr); // Not used by V8.
1168  }
1169  } else {
1170  Unknown(instr); // Not used by V8.
1171  }
1172  } else {
1173  if ((instr->VCValue() == 0x0) &&
1174  (instr->VAValue() == 0x0)) {
1175  DecodeVMOVBetweenCoreAndSinglePrecisionRegisters(instr);
1176  } else if ((instr->VCValue() == 0x0) &&
1177  (instr->VAValue() == 0x7) &&
1178  (instr->Bits(19, 16) == 0x1)) {
1179  if (instr->VLValue() == 0) {
1180  if (instr->Bits(15, 12) == 0xF) {
1181  Format(instr, "vmsr'cond FPSCR, APSR");
1182  } else {
1183  Format(instr, "vmsr'cond FPSCR, 'rt");
1184  }
1185  } else {
1186  if (instr->Bits(15, 12) == 0xF) {
1187  Format(instr, "vmrs'cond APSR, FPSCR");
1188  } else {
1189  Format(instr, "vmrs'cond 'rt, FPSCR");
1190  }
1191  }
1192  }
1193  }
1194 }
1195 
1196 
1197 void Decoder::DecodeVMOVBetweenCoreAndSinglePrecisionRegisters(
1198  Instruction* instr) {
1199  VERIFY((instr->Bit(4) == 1) && (instr->VCValue() == 0x0) &&
1200  (instr->VAValue() == 0x0));
1201 
1202  bool to_arm_register = (instr->VLValue() == 0x1);
1203 
1204  if (to_arm_register) {
1205  Format(instr, "vmov'cond 'rt, 'Sn");
1206  } else {
1207  Format(instr, "vmov'cond 'Sn, 'rt");
1208  }
1209 }
1210 
1211 
1212 void Decoder::DecodeVCMP(Instruction* instr) {
1213  VERIFY((instr->Bit(4) == 0) && (instr->Opc1Value() == 0x7));
1214  VERIFY(((instr->Opc2Value() == 0x4) || (instr->Opc2Value() == 0x5)) &&
1215  (instr->Opc3Value() & 0x1));
1216 
1217  // Comparison.
1218  bool dp_operation = (instr->SzValue() == 1);
1219  bool raise_exception_for_qnan = (instr->Bit(7) == 0x1);
1220 
1221  if (dp_operation && !raise_exception_for_qnan) {
1222  if (instr->Opc2Value() == 0x4) {
1223  Format(instr, "vcmp.f64'cond 'Dd, 'Dm");
1224  } else if (instr->Opc2Value() == 0x5) {
1225  Format(instr, "vcmp.f64'cond 'Dd, #0.0");
1226  } else {
1227  Unknown(instr); // invalid
1228  }
1229  } else {
1230  Unknown(instr); // Not used by V8.
1231  }
1232 }
1233 
1234 
1235 void Decoder::DecodeVCVTBetweenDoubleAndSingle(Instruction* instr) {
1236  VERIFY((instr->Bit(4) == 0) && (instr->Opc1Value() == 0x7));
1237  VERIFY((instr->Opc2Value() == 0x7) && (instr->Opc3Value() == 0x3));
1238 
1239  bool double_to_single = (instr->SzValue() == 1);
1240 
1241  if (double_to_single) {
1242  Format(instr, "vcvt.f32.f64'cond 'Sd, 'Dm");
1243  } else {
1244  Format(instr, "vcvt.f64.f32'cond 'Dd, 'Sm");
1245  }
1246 }
1247 
1248 
1249 void Decoder::DecodeVCVTBetweenFloatingPointAndInteger(Instruction* instr) {
1250  VERIFY((instr->Bit(4) == 0) && (instr->Opc1Value() == 0x7));
1251  VERIFY(((instr->Opc2Value() == 0x8) && (instr->Opc3Value() & 0x1)) ||
1252  (((instr->Opc2Value() >> 1) == 0x6) && (instr->Opc3Value() & 0x1)));
1253 
1254  bool to_integer = (instr->Bit(18) == 1);
1255  bool dp_operation = (instr->SzValue() == 1);
1256  if (to_integer) {
1257  bool unsigned_integer = (instr->Bit(16) == 0);
1258 
1259  if (dp_operation) {
1260  if (unsigned_integer) {
1261  Format(instr, "vcvt.u32.f64'cond 'Sd, 'Dm");
1262  } else {
1263  Format(instr, "vcvt.s32.f64'cond 'Sd, 'Dm");
1264  }
1265  } else {
1266  if (unsigned_integer) {
1267  Format(instr, "vcvt.u32.f32'cond 'Sd, 'Sm");
1268  } else {
1269  Format(instr, "vcvt.s32.f32'cond 'Sd, 'Sm");
1270  }
1271  }
1272  } else {
1273  bool unsigned_integer = (instr->Bit(7) == 0);
1274 
1275  if (dp_operation) {
1276  if (unsigned_integer) {
1277  Format(instr, "vcvt.f64.u32'cond 'Dd, 'Sm");
1278  } else {
1279  Format(instr, "vcvt.f64.s32'cond 'Dd, 'Sm");
1280  }
1281  } else {
1282  if (unsigned_integer) {
1283  Format(instr, "vcvt.f32.u32'cond 'Sd, 'Sm");
1284  } else {
1285  Format(instr, "vcvt.f32.s32'cond 'Sd, 'Sm");
1286  }
1287  }
1288  }
1289 }
1290 
1291 
1292 // Decode Type 6 coprocessor instructions.
1293 // Dm = vmov(Rt, Rt2)
1294 // <Rt, Rt2> = vmov(Dm)
1295 // Ddst = MEM(Rbase + 4*offset).
1296 // MEM(Rbase + 4*offset) = Dsrc.
1297 void Decoder::DecodeType6CoprocessorIns(Instruction* instr) {
1298  VERIFY(instr->TypeValue() == 6);
1299 
1300  if (instr->CoprocessorValue() == 0xA) {
1301  switch (instr->OpcodeValue()) {
1302  case 0x8:
1303  case 0xA:
1304  if (instr->HasL()) {
1305  Format(instr, "vldr'cond 'Sd, ['rn - 4*'imm08@00]");
1306  } else {
1307  Format(instr, "vstr'cond 'Sd, ['rn - 4*'imm08@00]");
1308  }
1309  break;
1310  case 0xC:
1311  case 0xE:
1312  if (instr->HasL()) {
1313  Format(instr, "vldr'cond 'Sd, ['rn + 4*'imm08@00]");
1314  } else {
1315  Format(instr, "vstr'cond 'Sd, ['rn + 4*'imm08@00]");
1316  }
1317  break;
1318  case 0x4:
1319  case 0x5:
1320  case 0x6:
1321  case 0x7:
1322  case 0x9:
1323  case 0xB: {
1324  bool to_vfp_register = (instr->VLValue() == 0x1);
1325  if (to_vfp_register) {
1326  Format(instr, "vldm'cond'pu 'rn'w, {'Sd-'Sd+}");
1327  } else {
1328  Format(instr, "vstm'cond'pu 'rn'w, {'Sd-'Sd+}");
1329  }
1330  break;
1331  }
1332  default:
1333  Unknown(instr); // Not used by V8.
1334  }
1335  } else if (instr->CoprocessorValue() == 0xB) {
1336  switch (instr->OpcodeValue()) {
1337  case 0x2:
1338  // Load and store double to two GP registers
1339  if (instr->Bits(7, 4) != 0x1) {
1340  Unknown(instr); // Not used by V8.
1341  } else if (instr->HasL()) {
1342  Format(instr, "vmov'cond 'rt, 'rn, 'Dm");
1343  } else {
1344  Format(instr, "vmov'cond 'Dm, 'rt, 'rn");
1345  }
1346  break;
1347  case 0x8:
1348  if (instr->HasL()) {
1349  Format(instr, "vldr'cond 'Dd, ['rn - 4*'imm08@00]");
1350  } else {
1351  Format(instr, "vstr'cond 'Dd, ['rn - 4*'imm08@00]");
1352  }
1353  break;
1354  case 0xC:
1355  if (instr->HasL()) {
1356  Format(instr, "vldr'cond 'Dd, ['rn + 4*'imm08@00]");
1357  } else {
1358  Format(instr, "vstr'cond 'Dd, ['rn + 4*'imm08@00]");
1359  }
1360  break;
1361  case 0x4:
1362  case 0x5:
1363  case 0x9: {
1364  bool to_vfp_register = (instr->VLValue() == 0x1);
1365  if (to_vfp_register) {
1366  Format(instr, "vldm'cond'pu 'rn'w, {'Dd-'Dd+}");
1367  } else {
1368  Format(instr, "vstm'cond'pu 'rn'w, {'Dd-'Dd+}");
1369  }
1370  break;
1371  }
1372  default:
1373  Unknown(instr); // Not used by V8.
1374  }
1375  } else {
1376  Unknown(instr); // Not used by V8.
1377  }
1378 }
1379 
1380 #undef VERIFIY
1381 
1382 bool Decoder::IsConstantPoolAt(byte* instr_ptr) {
1383  int instruction_bits = *(reinterpret_cast<int*>(instr_ptr));
1384  return (instruction_bits & kConstantPoolMarkerMask) == kConstantPoolMarker;
1385 }
1386 
1387 
1388 int Decoder::ConstantPoolSizeAt(byte* instr_ptr) {
1389  if (IsConstantPoolAt(instr_ptr)) {
1390  int instruction_bits = *(reinterpret_cast<int*>(instr_ptr));
1391  return instruction_bits & kConstantPoolLengthMask;
1392  } else {
1393  return -1;
1394  }
1395 }
1396 
1397 
1398 // Disassemble the instruction at *instr_ptr into the output buffer.
1399 int Decoder::InstructionDecode(byte* instr_ptr) {
1400  Instruction* instr = Instruction::At(instr_ptr);
1401  // Print raw instruction bytes.
1402  out_buffer_pos_ += OS::SNPrintF(out_buffer_ + out_buffer_pos_,
1403  "%08x ",
1404  instr->InstructionBits());
1405  if (instr->ConditionField() == kSpecialCondition) {
1406  Unknown(instr);
1407  return Instruction::kInstrSize;
1408  }
1409  int instruction_bits = *(reinterpret_cast<int*>(instr_ptr));
1410  if ((instruction_bits & kConstantPoolMarkerMask) == kConstantPoolMarker) {
1411  out_buffer_pos_ += OS::SNPrintF(out_buffer_ + out_buffer_pos_,
1412  "constant pool begin (length %d)",
1413  instruction_bits &
1415  return Instruction::kInstrSize;
1416  }
1417  switch (instr->TypeValue()) {
1418  case 0:
1419  case 1: {
1420  DecodeType01(instr);
1421  break;
1422  }
1423  case 2: {
1424  DecodeType2(instr);
1425  break;
1426  }
1427  case 3: {
1428  DecodeType3(instr);
1429  break;
1430  }
1431  case 4: {
1432  DecodeType4(instr);
1433  break;
1434  }
1435  case 5: {
1436  DecodeType5(instr);
1437  break;
1438  }
1439  case 6: {
1440  DecodeType6(instr);
1441  break;
1442  }
1443  case 7: {
1444  return DecodeType7(instr);
1445  }
1446  default: {
1447  // The type field is 3-bits in the ARM encoding.
1448  UNREACHABLE();
1449  break;
1450  }
1451  }
1452  return Instruction::kInstrSize;
1453 }
1454 
1455 
1456 } } // namespace v8::internal
1457 
1458 
1459 
1460 //------------------------------------------------------------------------------
1461 
1462 namespace disasm {
1463 
1464 
1465 const char* NameConverter::NameOfAddress(byte* addr) const {
1467  return tmp_buffer_.start();
1468 }
1469 
1470 
1471 const char* NameConverter::NameOfConstant(byte* addr) const {
1472  return NameOfAddress(addr);
1473 }
1474 
1475 
1476 const char* NameConverter::NameOfCPURegister(int reg) const {
1477  return v8::internal::Registers::Name(reg);
1478 }
1479 
1480 
1481 const char* NameConverter::NameOfByteCPURegister(int reg) const {
1482  UNREACHABLE(); // ARM does not have the concept of a byte register
1483  return "nobytereg";
1484 }
1485 
1486 
1487 const char* NameConverter::NameOfXMMRegister(int reg) const {
1488  UNREACHABLE(); // ARM does not have any XMM registers
1489  return "noxmmreg";
1490 }
1491 
1492 
1493 const char* NameConverter::NameInCode(byte* addr) const {
1494  // The default name converter is called for unknown code. So we will not try
1495  // to access any memory.
1496  return "";
1497 }
1498 
1499 
1500 //------------------------------------------------------------------------------
1501 
1502 Disassembler::Disassembler(const NameConverter& converter)
1503  : converter_(converter) {}
1504 
1505 
1506 Disassembler::~Disassembler() {}
1507 
1508 
1509 int Disassembler::InstructionDecode(v8::internal::Vector<char> buffer,
1510  byte* instruction) {
1511  v8::internal::Decoder d(converter_, buffer);
1512  return d.InstructionDecode(instruction);
1513 }
1514 
1515 
1516 int Disassembler::ConstantPoolSizeAt(byte* instruction) {
1517  return v8::internal::Decoder::ConstantPoolSizeAt(instruction);
1518 }
1519 
1520 
1521 void Disassembler::Disassemble(FILE* f, byte* begin, byte* end) {
1522  NameConverter converter;
1523  Disassembler d(converter);
1524  for (byte* pc = begin; pc < end;) {
1526  buffer[0] = '\0';
1527  byte* prev_pc = pc;
1528  pc += d.InstructionDecode(buffer, pc);
1529  fprintf(f, "%p %08x %s\n",
1530  prev_pc, *reinterpret_cast<int32_t*>(prev_pc), buffer.start());
1531  }
1532 }
1533 
1534 
1535 } // namespace disasm
1536 
1537 #endif // V8_TARGET_ARCH_ARM
unsigned char byte
Definition: disasm.h:33
Disassembler(const NameConverter &converter)
virtual const char * NameOfXMMRegister(int reg) const
v8::internal::EmbeddedVector< char, 128 > tmp_buffer_
Definition: disasm.h:49
virtual const char * NameOfConstant(byte *addr) const
static const char * Name(int reg, bool is_double)
#define ASSERT(condition)
Definition: checks.h:270
v8::Handle< v8::Value > Print(const v8::Arguments &args)
const uint32_t kStopCodeMask
virtual const char * NameInCode(byte *addr) const
static const char * Name(int reg)
virtual const char * NameOfByteCPURegister(int reg) const
uint8_t byte
Definition: globals.h:156
static Instruction * At(byte *pc)
#define UNREACHABLE()
Definition: checks.h:50
T * start() const
Definition: utils.h:390
virtual const char * NameOfCPURegister(int reg) const
#define DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition: globals.h:307
const Register pc
virtual const char * NameOfAddress(byte *addr) const
static int SNPrintF(Vector< char > str, const char *format,...)
const int kConstantPoolMarker
Definition: constants-arm.h:88
const int kConstantPoolMarkerMask
Definition: constants-arm.h:87
const int kConstantPoolLengthMask
Definition: constants-arm.h:89