disasm-arm.cc 89.6 KB
Newer Older
1
// Copyright 2011 the V8 project authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4

5 6 7 8 9 10 11 12 13
// A Disassembler object is used to disassemble a block of code instruction by
// instruction. The default implementation of the NameConverter object can be
// overriden to modify register names or to do symbol lookup on addresses.
//
// The example below will disassemble a block of code and print it to stdout.
//
//   NameConverter converter;
//   Disassembler d(converter);
//   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 18 19 20 21 22 23 24
//     printf("%p    %08x      %s\n",
//            prev_pc, *reinterpret_cast<int32_t*>(prev_pc), buffer);
//   }
//
// The Disassembler class also has a convenience method to disassemble a block
// of code into a FILE*, meaning that the above functionality could also be
// achieved by just calling Disassembler::Disassemble(stdout, begin, end);

25 26 27 28 29
#include <cassert>
#include <cinttypes>
#include <cstdarg>
#include <cstdio>
#include <cstring>
30

31
#if V8_TARGET_ARCH_ARM
32

33
#include "src/arm/assembler-arm.h"
34
#include "src/arm/constants-arm.h"
35
#include "src/base/bits.h"
36
#include "src/base/platform/platform.h"
37
#include "src/diagnostics/disasm.h"
38
#include "src/vector.h"
39

40 41
namespace v8 {
namespace internal {
42 43 44 45 46 47 48 49 50

//------------------------------------------------------------------------------

// Decoder decodes and disassembles instructions into an output buffer.
// It uses the converter to convert register names and call destinations into
// more informative description.
class Decoder {
 public:
  Decoder(const disasm::NameConverter& converter,
51
          Vector<char> out_buffer)
52 53 54 55 56 57 58 59 60 61 62 63
    : converter_(converter),
      out_buffer_(out_buffer),
      out_buffer_pos_(0) {
    out_buffer_[out_buffer_pos_] = '\0';
  }

  ~Decoder() {}

  // Writes one disassembled instruction into 'buffer' (0-terminated).
  // Returns the length of the disassembled machine instruction in bytes.
  int InstructionDecode(byte* instruction);

64 65 66
  static bool IsConstantPoolAt(byte* instr_ptr);
  static int ConstantPoolSizeAt(byte* instr_ptr);

67
 private:
68
  // Bottleneck functions to print into the out_buffer.
69 70 71
  void PrintChar(const char ch);
  void Print(const char* str);

72
  // Printing of common values.
73
  void PrintRegister(int reg);
74 75
  void PrintSRegister(int reg);
  void PrintDRegister(int reg);
76 77 78 79 80 81 82 83
  int FormatVFPRegister(Instruction* instr, const char* format);
  void PrintMovwMovt(Instruction* instr);
  int FormatVFPinstruction(Instruction* instr, const char* format);
  void PrintCondition(Instruction* instr);
  void PrintShiftRm(Instruction* instr);
  void PrintShiftImm(Instruction* instr);
  void PrintShiftSat(Instruction* instr);
  void PrintPU(Instruction* instr);
84
  void PrintSoftwareInterrupt(SoftwareInterruptCodes svc);
85

86
  // Handle formatting of instructions and their options.
87
  int FormatRegister(Instruction* instr, const char* option);
88 89
  void FormatNeonList(int Vd, int type);
  void FormatNeonMemory(int Rn, int align, int Rm);
90 91 92
  int FormatOption(Instruction* instr, const char* option);
  void Format(Instruction* instr, const char* format);
  void Unknown(Instruction* instr);
93

94 95 96 97
  // Each of these functions decodes one particular instruction type, a 3-bit
  // field in the instruction encoding.
  // Types 0 and 1 are combined as they are largely the same except for the way
  // they interpret the shifter operand.
98 99 100 101 102 103
  void DecodeType01(Instruction* instr);
  void DecodeType2(Instruction* instr);
  void DecodeType3(Instruction* instr);
  void DecodeType4(Instruction* instr);
  void DecodeType5(Instruction* instr);
  void DecodeType6(Instruction* instr);
104
  // Type 7 includes special Debugger instructions.
105
  int DecodeType7(Instruction* instr);
106 107
  // CP15 coprocessor instructions.
  void DecodeTypeCP15(Instruction* instr);
108
  // For VFP support.
109 110
  void DecodeTypeVFP(Instruction* instr);
  void DecodeType6CoprocessorIns(Instruction* instr);
111

112 113
  void DecodeSpecialCondition(Instruction* instr);

114 115 116 117
  void DecodeVMOVBetweenCoreAndSinglePrecisionRegisters(Instruction* instr);
  void DecodeVCMP(Instruction* instr);
  void DecodeVCVTBetweenDoubleAndSingle(Instruction* instr);
  void DecodeVCVTBetweenFloatingPointAndInteger(Instruction* instr);
118 119

  const disasm::NameConverter& converter_;
120
  Vector<char> out_buffer_;
121 122 123
  int out_buffer_pos_;

  DISALLOW_COPY_AND_ASSIGN(Decoder);
124 125 126
};


127 128 129 130 131
// Support for assertions in the Decoder formatting functions.
#define STRING_STARTS_WITH(string, compare_string) \
  (strncmp(string, compare_string, strlen(compare_string)) == 0)


132 133 134 135 136 137 138 139 140
// Append the ch to the output buffer.
void Decoder::PrintChar(const char ch) {
  out_buffer_[out_buffer_pos_++] = ch;
}


// Append the str to the output buffer.
void Decoder::Print(const char* str) {
  char cur = *str++;
141
  while (cur != '\0' && (out_buffer_pos_ < (out_buffer_.length() - 1))) {
142 143 144 145 146 147 148
    PrintChar(cur);
    cur = *str++;
  }
  out_buffer_[out_buffer_pos_] = 0;
}


149 150
// These condition names are defined in a way to match the native disassembler
// formatting. See for example the command "objdump -d <binary file>".
151
static const char* const cond_names[kNumberOfConditions] = {
152 153
  "eq", "ne", "cs" , "cc" , "mi" , "pl" , "vs" , "vc" ,
  "hi", "ls", "ge", "lt", "gt", "le", "", "invalid",
154 155 156 157
};


// Print the condition guarding the instruction.
158 159
void Decoder::PrintCondition(Instruction* instr) {
  Print(cond_names[instr->ConditionValue()]);
160 161 162 163 164 165 166 167
}


// Print the register name according to the active name converter.
void Decoder::PrintRegister(int reg) {
  Print(converter_.NameOfCPURegister(reg));
}

168

169 170
// Print the VFP S register name according to the active name converter.
void Decoder::PrintSRegister(int reg) {
171
  Print(VFPRegisters::Name(reg, false));
172 173
}

174

175
// Print the VFP D register name according to the active name converter.
176
void Decoder::PrintDRegister(int reg) {
177
  Print(VFPRegisters::Name(reg, true));
178 179
}

180

181 182
// These shift names are defined in a way to match the native disassembler
// formatting. See for example the command "objdump -d <binary file>".
183
static const char* const shift_names[kNumberOfShifts] = {
184 185 186 187 188 189
  "lsl", "lsr", "asr", "ror"
};


// Print the register shift operands for the instruction. Generally used for
// data processing instructions.
190 191 192 193 194
void Decoder::PrintShiftRm(Instruction* instr) {
  ShiftOp shift = instr->ShiftField();
  int shift_index = instr->ShiftValue();
  int shift_amount = instr->ShiftAmountValue();
  int rm = instr->RmValue();
195 196

  PrintRegister(rm);
197

198
  if ((instr->RegShiftValue() == 0) && (shift == LSL) && (shift_amount == 0)) {
199 200 201
    // Special case for using rm only.
    return;
  }
202
  if (instr->RegShiftValue() == 0) {
203 204 205 206 207 208
    // by immediate
    if ((shift == ROR) && (shift_amount == 0)) {
      Print(", RRX");
      return;
    } else if (((shift == LSR) || (shift == ASR)) && (shift_amount == 0)) {
      shift_amount = 32;
209
    }
210 211 212 213
    out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
                                ", %s #%d",
                                shift_names[shift_index],
                                shift_amount);
214 215
  } else {
    // by register
216
    int rs = instr->RsValue();
217 218
    out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
                                ", %s ", shift_names[shift_index]);
219
    PrintRegister(rs);
220 221 222 223 224 225
  }
}


// Print the immediate operand for the instruction. Generally used for data
// processing instructions.
226 227 228
void Decoder::PrintShiftImm(Instruction* instr) {
  int rotate = instr->RotateValue() * 2;
  int immed8 = instr->Immed8Value();
229
  int imm = base::bits::RotateRight32(immed8, rotate);
230
  out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_, "#%d", imm);
231 232 233
}


234
// Print the optional shift and immediate used by saturating instructions.
235
void Decoder::PrintShiftSat(Instruction* instr) {
236 237
  int shift = instr->Bits(11, 7);
  if (shift > 0) {
238 239 240 241
    out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
                                ", %s #%d",
                                shift_names[instr->Bit(6) * 2],
                                instr->Bits(11, 7));
242 243 244 245
  }
}


246
// Print PU formatting to reduce complexity of FormatOption.
247
void Decoder::PrintPU(Instruction* instr) {
248
  switch (instr->PUField()) {
249
    case da_x: {
250 251 252
      Print("da");
      break;
    }
253
    case ia_x: {
254 255 256
      Print("ia");
      break;
    }
257
    case db_x: {
258 259 260
      Print("db");
      break;
    }
261
    case ib_x: {
262 263 264 265 266 267 268 269 270 271 272 273
      Print("ib");
      break;
    }
    default: {
      UNREACHABLE();
    }
  }
}


// Print SoftwareInterrupt codes. Factoring this out reduces the complexity of
// the FormatOption method.
274 275
void Decoder::PrintSoftwareInterrupt(SoftwareInterruptCodes svc) {
  switch (svc) {
276 277
    case kCallRtRedirected:
      Print("call rt redirected");
278
      return;
279 280
    case kBreakpoint:
      Print("breakpoint");
281 282
      return;
    default:
283
      if (svc >= kStopCode) {
284 285 286 287
        out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
                                    "%d - 0x%x",
                                    svc & kStopCodeMask,
                                    svc & kStopCodeMask);
288
      } else {
289 290 291
        out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
                                    "%d",
                                    svc);
292
      }
293 294 295 296 297 298 299
      return;
  }
}


// Handle all register based formatting in this function to reduce the
// complexity of FormatOption.
300
int Decoder::FormatRegister(Instruction* instr, const char* format) {
301
  DCHECK_EQ(format[0], 'r');
302
  if (format[1] == 'n') {  // 'rn: Rn register
303
    int reg = instr->RnValue();
304 305 306
    PrintRegister(reg);
    return 2;
  } else if (format[1] == 'd') {  // 'rd: Rd register
307
    int reg = instr->RdValue();
308 309 310
    PrintRegister(reg);
    return 2;
  } else if (format[1] == 's') {  // 'rs: Rs register
311
    int reg = instr->RsValue();
312 313 314
    PrintRegister(reg);
    return 2;
  } else if (format[1] == 'm') {  // 'rm: Rm register
315
    int reg = instr->RmValue();
316 317
    PrintRegister(reg);
    return 2;
318
  } else if (format[1] == 't') {  // 'rt: Rt register
319
    int reg = instr->RtValue();
320 321
    PrintRegister(reg);
    return 2;
322 323
  } else if (format[1] == 'l') {
    // 'rlist: register list for load and store multiple instructions
324
    DCHECK(STRING_STARTS_WITH(format, "rlist"));
325
    int rlist = instr->RlistValue();
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
    int reg = 0;
    Print("{");
    // Print register list in ascending order, by scanning the bit mask.
    while (rlist != 0) {
      if ((rlist & 1) != 0) {
        PrintRegister(reg);
        if ((rlist >> 1) != 0) {
          Print(", ");
        }
      }
      reg++;
      rlist >>= 1;
    }
    Print("}");
    return 5;
  }
  UNREACHABLE();
}

345

346 347
// Handle all VFP register based formatting in this function to reduce the
// complexity of FormatOption.
348
int Decoder::FormatVFPRegister(Instruction* instr, const char* format) {
349
  DCHECK((format[0] == 'S') || (format[0] == 'D'));
350

351 352 353 354 355
  VFPRegPrecision precision =
      format[0] == 'D' ? kDoublePrecision : kSinglePrecision;

  int retval = 2;
  int reg = -1;
356
  if (format[1] == 'n') {
357
    reg = instr->VFPNRegValue(precision);
358
  } else if (format[1] == 'm') {
359
    reg = instr->VFPMRegValue(precision);
360
  } else if (format[1] == 'd') {
361 362 363 364 365 366 367 368 369 370
    if ((instr->TypeValue() == 7) &&
        (instr->Bit(24) == 0x0) &&
        (instr->Bits(11, 9) == 0x5) &&
        (instr->Bit(4) == 0x1)) {
      // vmov.32 has Vd in a different place.
      reg = instr->Bits(19, 16) | (instr->Bit(7) << 4);
    } else {
      reg = instr->VFPDRegValue(precision);
    }

371 372 373 374 375 376 377 378
    if (format[2] == '+') {
      int immed8 = instr->Immed8Value();
      if (format[0] == 'S') reg += immed8 - 1;
      if (format[0] == 'D') reg += (immed8 / 2 - 1);
    }
    if (format[2] == '+') retval = 3;
  } else {
    UNREACHABLE();
379 380
  }

381 382 383 384 385 386 387
  if (precision == kSinglePrecision) {
    PrintSRegister(reg);
  } else {
    PrintDRegister(reg);
  }

  return retval;
388 389
}

390

391
int Decoder::FormatVFPinstruction(Instruction* instr, const char* format) {
392 393 394 395 396
    Print(format);
    return 0;
}


397 398
void Decoder::FormatNeonList(int Vd, int type) {
  if (type == nlt_1) {
399 400
    out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
                                "{d%d}", Vd);
401
  } else if (type == nlt_2) {
402 403
    out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
                                "{d%d, d%d}", Vd, Vd + 1);
404
  } else if (type == nlt_3) {
405 406
    out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
                                "{d%d, d%d, d%d}", Vd, Vd + 1, Vd + 2);
407
  } else if (type == nlt_4) {
408 409
    out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
                        "{d%d, d%d, d%d, d%d}", Vd, Vd + 1, Vd + 2, Vd + 3);
410 411 412 413 414
  }
}


void Decoder::FormatNeonMemory(int Rn, int align, int Rm) {
415 416
  out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_, "[%s",
                              converter_.NameOfCPURegister(Rn));
417
  if (align != 0) {
418 419
    out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
                                ":%d", (1 << align) << 6);
420 421 422 423 424 425
  }
  if (Rm == 15) {
    Print("]");
  } else if (Rm == 13) {
    Print("]!");
  } else {
426 427
    out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_, "], %s",
                                converter_.NameOfCPURegister(Rm));
428 429 430 431
  }
}


432
// Print the movw or movt instruction.
433 434 435
void Decoder::PrintMovwMovt(Instruction* instr) {
  int imm = instr->ImmedMovwMovtValue();
  int rd = instr->RdValue();
436
  PrintRegister(rd);
437
  out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_, ", #%d", imm);
438 439 440
}


441 442 443 444 445
// FormatOption takes a formatting string and interprets it based on
// the current instructions. The format string points to the first
// character of the option string (the option escape has already been
// consumed by the caller.)  FormatOption returns the number of
// characters that were consumed from the formatting string.
446
int Decoder::FormatOption(Instruction* instr, const char* format) {
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
  switch (format[0]) {
    case 'a': {  // 'a: accumulate multiplies
      if (instr->Bit(21) == 0) {
        Print("ul");
      } else {
        Print("la");
      }
      return 1;
    }
    case 'b': {  // 'b: byte loads or stores
      if (instr->HasB()) {
        Print("b");
      }
      return 1;
    }
    case 'c': {  // 'cond: conditional execution
463
      DCHECK(STRING_STARTS_WITH(format, "cond"));
464 465 466
      PrintCondition(instr);
      return 4;
    }
467
    case 'd': {  // 'd: vmov double immediate.
468
      double d = instr->DoubleImmedVmov().get_scalar();
469
      out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_, "#%g", d);
470 471
      return 1;
    }
472 473 474 475 476 477 478
    case 'f': {  // 'f: bitfield instructions - v7 and above.
      uint32_t lsbit = instr->Bits(11, 7);
      uint32_t width = instr->Bits(20, 16) + 1;
      if (instr->Bit(21) == 0) {
        // BFC/BFI:
        // Bits 20-16 represent most-significant bit. Covert to width.
        width -= lsbit;
479
        DCHECK_GT(width, 0);
480
      }
481
      DCHECK_LE(width + lsbit, 32);
482 483
      out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
                                  "#%d, #%d", lsbit, width);
484 485
      return 1;
    }
486 487 488 489 490 491 492 493
    case 'h': {  // 'h: halfword operation for extra loads and stores
      if (instr->HasH()) {
        Print("h");
      } else {
        Print("b");
      }
      return 1;
    }
494
    case 'i': {  // 'i: immediate value from adjacent bits.
495
      // Expects tokens in the form imm%02d@%02d, i.e. imm05@07, imm10@16
496 497 498
      int width = (format[3] - '0') * 10 + (format[4] - '0');
      int lsb   = (format[6] - '0') * 10 + (format[7] - '0');

499 500
      DCHECK((width >= 1) && (width <= 32));
      DCHECK((lsb >= 0) && (lsb <= 31));
501
      DCHECK_LE(width + lsb, 32);
502

503 504 505
      out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
                                  "%d",
                                  instr->Bits(width + lsb - 1, lsb));
506 507
      return 8;
    }
508 509 510 511 512 513
    case 'l': {  // 'l: branch and link
      if (instr->HasLink()) {
        Print("l");
      }
      return 1;
    }
514
    case 'm': {
515 516 517 518 519 520
      if (format[1] == 'w') {
        // 'mw: movt/movw instructions.
        PrintMovwMovt(instr);
        return 2;
      }
      if (format[1] == 'e') {  // 'memop: load/store instructions.
521
        DCHECK(STRING_STARTS_WITH(format, "memop"));
522 523 524
        if (instr->HasL()) {
          Print("ldr");
        } else {
525 526 527 528 529 530 531 532 533
          if ((instr->Bits(27, 25) == 0) && (instr->Bit(20) == 0) &&
              (instr->Bits(7, 6) == 3) && (instr->Bit(4) == 1)) {
            if (instr->Bit(5) == 1) {
              Print("strd");
            } else {
              Print("ldrd");
            }
            return 5;
          }
534 535 536 537
          Print("str");
        }
        return 5;
      }
538
      // 'msg: for simulator break instructions
539
      DCHECK(STRING_STARTS_WITH(format, "msg"));
540
      byte* str =
541
          reinterpret_cast<byte*>(instr->InstructionBits() & 0x0FFFFFFF);
542 543
      out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
                                  "%s", converter_.NameInCode(str));
544
      return 3;
545 546
    }
    case 'o': {
547
      if ((format[3] == '1') && (format[4] == '2')) {
548
        // 'off12: 12-bit offset for load and store instructions
549
        DCHECK(STRING_STARTS_WITH(format, "off12"));
550 551
        out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
                                    "%d", instr->Offset12Value());
552
        return 5;
553 554
      } else if (format[3] == '0') {
        // 'off0to3and8to19 16-bit immediate encoded in bits 19-8 and 3-0.
555
        DCHECK(STRING_STARTS_WITH(format, "off0to3and8to19"));
556 557 558 559
        out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
                                    "%d",
                                    (instr->Bits(19, 8) << 4) +
                                    instr->Bits(3, 0));
560
        return 15;
561
      }
562
      // 'off8: 8-bit offset for extra load and store instructions
563
      DCHECK(STRING_STARTS_WITH(format, "off8"));
564
      int offs8 = (instr->ImmedHValue() << 4) | instr->ImmedLValue();
565
      out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_, "%d", offs8);
566
      return 4;
567
    }
568
    case 'p': {  // 'pu: P and U bits for load and store instructions
569
      DCHECK(STRING_STARTS_WITH(format, "pu"));
570
      PrintPU(instr);
571 572 573
      return 2;
    }
    case 'r': {
574
      return FormatRegister(instr, format);
575 576
    }
    case 's': {
577
      if (format[1] == 'h') {  // 'shift_op or 'shift_rm or 'shift_sat.
578
        if (format[6] == 'o') {  // 'shift_op
579
          DCHECK(STRING_STARTS_WITH(format, "shift_op"));
580
          if (instr->TypeValue() == 0) {
581 582
            PrintShiftRm(instr);
          } else {
583
            DCHECK_EQ(instr->TypeValue(), 1);
584 585 586
            PrintShiftImm(instr);
          }
          return 8;
587
        } else if (format[6] == 's') {  // 'shift_sat.
588
          DCHECK(STRING_STARTS_WITH(format, "shift_sat"));
589 590
          PrintShiftSat(instr);
          return 9;
591
        } else {  // 'shift_rm
592
          DCHECK(STRING_STARTS_WITH(format, "shift_rm"));
593 594
          PrintShiftRm(instr);
          return 8;
595
        }
596
      } else if (format[1] == 'v') {  // 'svc
597
        DCHECK(STRING_STARTS_WITH(format, "svc"));
598
        PrintSoftwareInterrupt(instr->SvcValue());
599 600
        return 3;
      } else if (format[1] == 'i') {  // 'sign: signed extra loads and stores
601
        DCHECK(STRING_STARTS_WITH(format, "sign"));
602 603 604 605
        if (instr->HasSign()) {
          Print("s");
        }
        return 4;
606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625
      } else if (format[1] == 'p') {
        if (format[8] == '_') {  // 'spec_reg_fields
          DCHECK(STRING_STARTS_WITH(format, "spec_reg_fields"));
          Print("_");
          int mask = instr->Bits(19, 16);
          if (mask == 0) Print("(none)");
          if ((mask & 0x8) != 0) Print("f");
          if ((mask & 0x4) != 0) Print("s");
          if ((mask & 0x2) != 0) Print("x");
          if ((mask & 0x1) != 0) Print("c");
          return 15;
        } else {  // 'spec_reg
          DCHECK(STRING_STARTS_WITH(format, "spec_reg"));
          if (instr->Bit(22) == 0) {
            Print("CPSR");
          } else {
            Print("SPSR");
          }
          return 8;
        }
626
      }
627 628 629 630 631
      // 's: S field of data processing instructions
      if (instr->HasS()) {
        Print("s");
      }
      return 1;
632 633
    }
    case 't': {  // 'target: target of branch instructions
634
      DCHECK(STRING_STARTS_WITH(format, "target"));
635
      int off = (instr->SImmed24Value() << 2) + 8;
636 637 638 639 640
      out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
                                  "%+d -> %s",
                                  off,
                                  converter_.NameOfAddress(
                                    reinterpret_cast<byte*>(instr) + off));
641 642 643
      return 6;
    }
    case 'u': {  // 'u: signed or unsigned multiplies
644 645 646 647 648 649 650 651 652 653 654 655 656
      // The manual gets the meaning of bit 22 backwards in the multiply
      // instruction overview on page A3.16.2.  The instructions that
      // exist in u and s variants are the following:
      // smull A4.1.87
      // umull A4.1.129
      // umlal A4.1.128
      // smlal A4.1.76
      // For these 0 means u and 1 means s.  As can be seen on their individual
      // pages.  The other 18 mul instructions have the bit set or unset in
      // arbitrary ways that are unrelated to the signedness of the instruction.
      // None of these 18 instructions exist in both a 'u' and an 's' variant.

      if (instr->Bit(22) == 0) {
657 658 659 660 661 662
        Print("u");
      } else {
        Print("s");
      }
      return 1;
    }
663 664 665
    case 'v': {
      return FormatVFPinstruction(instr, format);
    }
666 667 668
    case 'A': {
      // Print pc-relative address.
      int offset = instr->Offset12Value();
669
      byte* pc = reinterpret_cast<byte*>(instr) + Instruction::kPcLoadDelta;
670 671 672 673 674 675 676 677 678 679 680 681 682 683 684
      byte* addr;
      switch (instr->PUField()) {
        case db_x: {
          addr = pc - offset;
          break;
        }
        case ib_x: {
          addr = pc + offset;
          break;
        }
        default: {
          UNREACHABLE();
          return -1;
        }
      }
685 686 687
      out_buffer_pos_ +=
          SNPrintF(out_buffer_ + out_buffer_pos_, "0x%08" PRIxPTR,
                   reinterpret_cast<uintptr_t>(addr));
688 689
      return 1;
    }
690 691 692 693
    case 'S':
    case 'D': {
      return FormatVFPRegister(instr, format);
    }
694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710
    case 'w': {  // 'w: W field of load and store instructions
      if (instr->HasW()) {
        Print("!");
      }
      return 1;
    }
    default: {
      UNREACHABLE();
    }
  }
  UNREACHABLE();
}


// Format takes a formatting string for a whole instruction and prints it into
// the output buffer. All escaped options are handed to FormatOption to be
// parsed further.
711
void Decoder::Format(Instruction* instr, const char* format) {
712
  char cur = *format++;
713
  while ((cur != 0) && (out_buffer_pos_ < (out_buffer_.length() - 1))) {
714 715 716 717 718 719 720 721 722 723 724
    if (cur == '\'') {  // Single quote is used as the formatting escape.
      format += FormatOption(instr, format);
    } else {
      out_buffer_[out_buffer_pos_++] = cur;
    }
    cur = *format++;
  }
  out_buffer_[out_buffer_pos_]  = '\0';
}


725
// The disassembler may end up decoding data inlined in the code. We do not want
726
// it to crash if the data does not resemble any known instruction.
727 728 729 730 731 732 733
#define VERIFY(condition) \
if(!(condition)) {        \
  Unknown(instr);         \
  return;                 \
}


734 735
// For currently unimplemented decodings the disassembler calls Unknown(instr)
// which will just print "unknown" of the instruction bits.
736
void Decoder::Unknown(Instruction* instr) {
737 738 739 740
  Format(instr, "unknown");
}


741 742
void Decoder::DecodeType01(Instruction* instr) {
  int type = instr->TypeValue();
743
  if ((type == 0) && instr->IsSpecialType0()) {
744 745 746 747 748 749
    // multiply instruction or extra loads and stores
    if (instr->Bits(7, 4) == 9) {
      if (instr->Bit(24) == 0) {
        // multiply instructions
        if (instr->Bit(23) == 0) {
          if (instr->Bit(21) == 0) {
750 751 752
            // The MUL instruction description (A 4.1.33) refers to Rd as being
            // the destination for the operation, but it confusingly uses the
            // Rn field to encode it.
753
            Format(instr, "mul'cond's 'rn, 'rm, 'rs");
754
          } else {
755 756 757 758 759 760 761 762 763 764 765 766 767
            if (instr->Bit(22) == 0) {
              // The MLA instruction description (A 4.1.28) refers to the order
              // of registers as "Rd, Rm, Rs, Rn". But confusingly it uses the
              // Rn field to encode the Rd register and the Rd field to encode
              // the Rn register.
              Format(instr, "mla'cond's 'rn, 'rm, 'rs, 'rd");
            } else {
              // The MLS instruction description (A 4.1.29) refers to the order
              // of registers as "Rd, Rm, Rs, Rn". But confusingly it uses the
              // Rn field to encode the Rd register and the Rd field to encode
              // the Rn register.
              Format(instr, "mls'cond's 'rn, 'rm, 'rs, 'rd");
            }
768 769
          }
        } else {
770 771 772 773 774 775 776
          // The signed/long multiply instructions use the terms RdHi and RdLo
          // when referring to the target registers. They are mapped to the Rn
          // and Rd fields as follows:
          // RdLo == Rd field
          // RdHi == Rn field
          // The order of registers is: <RdLo>, <RdHi>, <Rm>, <Rs>
          Format(instr, "'um'al'cond's 'rd, 'rn, 'rm, 'rs");
777 778
        }
      } else {
779 780 781 782 783 784 785
        if (instr->Bits(24, 23) == 3) {
          if (instr->Bit(20) == 1) {
            // ldrex
            switch (instr->Bits(22, 21)) {
              case 0:
                Format(instr, "ldrex'cond 'rt, ['rn]");
                break;
786 787 788
              case 1:
                Format(instr, "ldrexd'cond 'rt, ['rn]");
                break;
789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806
              case 2:
                Format(instr, "ldrexb'cond 'rt, ['rn]");
                break;
              case 3:
                Format(instr, "ldrexh'cond 'rt, ['rn]");
                break;
              default:
                UNREACHABLE();
                break;
            }
          } else {
            // strex
            // The instruction is documented as strex rd, rt, [rn], but the
            // "rt" register is using the rm bits.
            switch (instr->Bits(22, 21)) {
              case 0:
                Format(instr, "strex'cond 'rd, 'rm, ['rn]");
                break;
807 808 809
              case 1:
                Format(instr, "strexd'cond 'rd, 'rm, ['rn]");
                break;
810 811 812 813 814 815 816 817 818 819 820 821 822 823
              case 2:
                Format(instr, "strexb'cond 'rd, 'rm, ['rn]");
                break;
              case 3:
                Format(instr, "strexh'cond 'rd, 'rm, ['rn]");
                break;
              default:
                UNREACHABLE();
                break;
            }
          }
        } else {
          Unknown(instr);  // not used by V8
        }
824
      }
825
    } else if ((instr->Bit(20) == 0) && ((instr->Bits(7, 4) & 0xD) == 0xD)) {
826 827
      // ldrd, strd
      switch (instr->PUField()) {
828
        case da_x: {
829 830 831 832 833 834 835
          if (instr->Bit(22) == 0) {
            Format(instr, "'memop'cond's 'rd, ['rn], -'rm");
          } else {
            Format(instr, "'memop'cond's 'rd, ['rn], #-'off8");
          }
          break;
        }
836
        case ia_x: {
837 838 839 840 841 842 843
          if (instr->Bit(22) == 0) {
            Format(instr, "'memop'cond's 'rd, ['rn], +'rm");
          } else {
            Format(instr, "'memop'cond's 'rd, ['rn], #+'off8");
          }
          break;
        }
844
        case db_x: {
845 846 847 848 849 850 851
          if (instr->Bit(22) == 0) {
            Format(instr, "'memop'cond's 'rd, ['rn, -'rm]'w");
          } else {
            Format(instr, "'memop'cond's 'rd, ['rn, #-'off8]'w");
          }
          break;
        }
852
        case ib_x: {
853 854 855 856 857 858 859 860 861 862 863 864 865
          if (instr->Bit(22) == 0) {
            Format(instr, "'memop'cond's 'rd, ['rn, +'rm]'w");
          } else {
            Format(instr, "'memop'cond's 'rd, ['rn, #+'off8]'w");
          }
          break;
        }
        default: {
          // The PU field is a 2-bit field.
          UNREACHABLE();
          break;
        }
      }
866 867 868
    } else {
      // extra load/store instructions
      switch (instr->PUField()) {
869
        case da_x: {
870 871 872 873 874 875 876
          if (instr->Bit(22) == 0) {
            Format(instr, "'memop'cond'sign'h 'rd, ['rn], -'rm");
          } else {
            Format(instr, "'memop'cond'sign'h 'rd, ['rn], #-'off8");
          }
          break;
        }
877
        case ia_x: {
878 879 880 881 882 883 884
          if (instr->Bit(22) == 0) {
            Format(instr, "'memop'cond'sign'h 'rd, ['rn], +'rm");
          } else {
            Format(instr, "'memop'cond'sign'h 'rd, ['rn], #+'off8");
          }
          break;
        }
885
        case db_x: {
886 887 888 889 890 891 892
          if (instr->Bit(22) == 0) {
            Format(instr, "'memop'cond'sign'h 'rd, ['rn, -'rm]'w");
          } else {
            Format(instr, "'memop'cond'sign'h 'rd, ['rn, #-'off8]'w");
          }
          break;
        }
893
        case ib_x: {
894 895 896 897 898 899 900 901 902 903 904 905 906 907 908
          if (instr->Bit(22) == 0) {
            Format(instr, "'memop'cond'sign'h 'rd, ['rn, +'rm]'w");
          } else {
            Format(instr, "'memop'cond'sign'h 'rd, ['rn, #+'off8]'w");
          }
          break;
        }
        default: {
          // The PU field is a 2-bit field.
          UNREACHABLE();
          break;
        }
      }
      return;
    }
909
  } else if ((type == 0) && instr->IsMiscType0()) {
910
    if ((instr->Bits(27, 23) == 2) && (instr->Bits(21, 20) == 2) &&
911
        (instr->Bits(15, 4) == 0xF00)) {
912 913 914 915 916
      Format(instr, "msr'cond 'spec_reg'spec_reg_fields, 'rm");
    } else if ((instr->Bits(27, 23) == 2) && (instr->Bits(21, 20) == 0) &&
               (instr->Bits(11, 0) == 0)) {
      Format(instr, "mrs'cond 'rd, 'spec_reg");
    } else if (instr->Bits(22, 21) == 1) {
917
      switch (instr->BitField(7, 4)) {
918 919 920 921 922 923 924 925 926 927 928 929 930 931
        case BX:
          Format(instr, "bx'cond 'rm");
          break;
        case BLX:
          Format(instr, "blx'cond 'rm");
          break;
        case BKPT:
          Format(instr, "bkpt 'off0to3and8to19");
          break;
        default:
          Unknown(instr);  // not used by V8
          break;
      }
    } else if (instr->Bits(22, 21) == 3) {
932
      switch (instr->BitField(7, 4)) {
933 934 935 936 937 938 939 940 941 942
        case CLZ:
          Format(instr, "clz'cond 'rd, 'rm");
          break;
        default:
          Unknown(instr);  // not used by V8
          break;
      }
    } else {
      Unknown(instr);  // not used by V8
    }
943 944 945 946 947 948 949 950
  } else if ((type == 1) && instr->IsNopLikeType1()) {
    if (instr->BitField(7, 0) == 0) {
      Format(instr, "nop'cond");
    } else if (instr->BitField(7, 0) == 20) {
      Format(instr, "csdb");
    } else {
      Unknown(instr);  // Not used in V8.
    }
951 952 953
  } else {
    switch (instr->OpcodeField()) {
      case AND: {
954
        Format(instr, "and'cond's 'rd, 'rn, 'shift_op");
955 956 957
        break;
      }
      case EOR: {
958
        Format(instr, "eor'cond's 'rd, 'rn, 'shift_op");
959 960 961
        break;
      }
      case SUB: {
962
        Format(instr, "sub'cond's 'rd, 'rn, 'shift_op");
963 964 965
        break;
      }
      case RSB: {
966
        Format(instr, "rsb'cond's 'rd, 'rn, 'shift_op");
967 968 969
        break;
      }
      case ADD: {
970
        Format(instr, "add'cond's 'rd, 'rn, 'shift_op");
971 972 973
        break;
      }
      case ADC: {
974
        Format(instr, "adc'cond's 'rd, 'rn, 'shift_op");
975 976 977
        break;
      }
      case SBC: {
978
        Format(instr, "sbc'cond's 'rd, 'rn, 'shift_op");
979 980 981
        break;
      }
      case RSC: {
982
        Format(instr, "rsc'cond's 'rd, 'rn, 'shift_op");
983 984 985 986
        break;
      }
      case TST: {
        if (instr->HasS()) {
987
          Format(instr, "tst'cond 'rn, 'shift_op");
988
        } else {
989
          Format(instr, "movw'cond 'mw");
990 991 992 993 994
        }
        break;
      }
      case TEQ: {
        if (instr->HasS()) {
995
          Format(instr, "teq'cond 'rn, 'shift_op");
996
        } else {
997 998 999
          // Other instructions matching this pattern are handled in the
          // miscellaneous instructions part above.
          UNREACHABLE();
1000 1001 1002 1003 1004
        }
        break;
      }
      case CMP: {
        if (instr->HasS()) {
1005
          Format(instr, "cmp'cond 'rn, 'shift_op");
1006
        } else {
1007
          Format(instr, "movt'cond 'mw");
1008 1009 1010 1011 1012
        }
        break;
      }
      case CMN: {
        if (instr->HasS()) {
1013
          Format(instr, "cmn'cond 'rn, 'shift_op");
1014
        } else {
1015 1016 1017
          // Other instructions matching this pattern are handled in the
          // miscellaneous instructions part above.
          UNREACHABLE();
1018 1019 1020 1021
        }
        break;
      }
      case ORR: {
1022
        Format(instr, "orr'cond's 'rd, 'rn, 'shift_op");
1023 1024 1025
        break;
      }
      case MOV: {
1026
        Format(instr, "mov'cond's 'rd, 'shift_op");
1027 1028 1029
        break;
      }
      case BIC: {
1030
        Format(instr, "bic'cond's 'rd, 'rn, 'shift_op");
1031 1032 1033
        break;
      }
      case MVN: {
1034
        Format(instr, "mvn'cond's 'rd, 'shift_op");
1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046
        break;
      }
      default: {
        // The Opcode field is a 4-bit field.
        UNREACHABLE();
        break;
      }
    }
  }
}


1047
void Decoder::DecodeType2(Instruction* instr) {
1048
  switch (instr->PUField()) {
1049
    case da_x: {
1050 1051
      if (instr->HasW()) {
        Unknown(instr);  // not used in V8
1052
        return;
1053 1054 1055 1056
      }
      Format(instr, "'memop'cond'b 'rd, ['rn], #-'off12");
      break;
    }
1057
    case ia_x: {
1058 1059
      if (instr->HasW()) {
        Unknown(instr);  // not used in V8
1060
        return;
1061 1062 1063 1064
      }
      Format(instr, "'memop'cond'b 'rd, ['rn], #+'off12");
      break;
    }
1065
    case db_x: {
1066 1067 1068 1069 1070
      if (instr->HasL() && (instr->RnValue() == kPCRegister)) {
        Format(instr, "'memop'cond'b 'rd, [pc, #-'off12]'w (addr 'A)");
      } else {
        Format(instr, "'memop'cond'b 'rd, ['rn, #-'off12]'w");
      }
1071 1072
      break;
    }
1073
    case ib_x: {
1074 1075 1076 1077 1078
      if (instr->HasL() && (instr->RnValue() == kPCRegister)) {
        Format(instr, "'memop'cond'b 'rd, [pc, #+'off12]'w (addr 'A)");
      } else {
        Format(instr, "'memop'cond'b 'rd, ['rn, #+'off12]'w");
      }
1079 1080 1081 1082 1083 1084 1085 1086 1087 1088
      break;
    }
    default: {
      // The PU field is a 2-bit field.
      UNREACHABLE();
    }
  }
}


1089
void Decoder::DecodeType3(Instruction* instr) {
1090
  switch (instr->PUField()) {
1091
    case da_x: {
1092
      VERIFY(!instr->HasW());
1093 1094 1095
      Format(instr, "'memop'cond'b 'rd, ['rn], -'shift_rm");
      break;
    }
1096
    case ia_x: {
1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126
      if (instr->Bit(4) == 0) {
        Format(instr, "'memop'cond'b 'rd, ['rn], +'shift_rm");
      } else {
        if (instr->Bit(5) == 0) {
          switch (instr->Bits(22, 21)) {
            case 0:
              if (instr->Bit(20) == 0) {
                if (instr->Bit(6) == 0) {
                  Format(instr, "pkhbt'cond 'rd, 'rn, 'rm, lsl #'imm05@07");
                } else {
                  if (instr->Bits(11, 7) == 0) {
                    Format(instr, "pkhtb'cond 'rd, 'rn, 'rm, asr #32");
                  } else {
                    Format(instr, "pkhtb'cond 'rd, 'rn, 'rm, asr #'imm05@07");
                  }
                }
              } else {
                UNREACHABLE();
              }
              break;
            case 1:
              UNREACHABLE();
              break;
            case 2:
              UNREACHABLE();
              break;
            case 3:
              Format(instr, "usat 'rd, #'imm05@16, 'rm'shift_sat");
              break;
          }
1127
        } else {
1128 1129 1130 1131 1132
          switch (instr->Bits(22, 21)) {
            case 0:
              UNREACHABLE();
              break;
            case 1:
1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198
              if (instr->Bits(9, 6) == 1) {
                if (instr->Bit(20) == 0) {
                  if (instr->Bits(19, 16) == 0xF) {
                    switch (instr->Bits(11, 10)) {
                      case 0:
                        Format(instr, "sxtb'cond 'rd, 'rm");
                        break;
                      case 1:
                        Format(instr, "sxtb'cond 'rd, 'rm, ror #8");
                        break;
                      case 2:
                        Format(instr, "sxtb'cond 'rd, 'rm, ror #16");
                        break;
                      case 3:
                        Format(instr, "sxtb'cond 'rd, 'rm, ror #24");
                        break;
                    }
                  } else {
                    switch (instr->Bits(11, 10)) {
                      case 0:
                        Format(instr, "sxtab'cond 'rd, 'rn, 'rm");
                        break;
                      case 1:
                        Format(instr, "sxtab'cond 'rd, 'rn, 'rm, ror #8");
                        break;
                      case 2:
                        Format(instr, "sxtab'cond 'rd, 'rn, 'rm, ror #16");
                        break;
                      case 3:
                        Format(instr, "sxtab'cond 'rd, 'rn, 'rm, ror #24");
                        break;
                    }
                  }
                } else {
                  if (instr->Bits(19, 16) == 0xF) {
                    switch (instr->Bits(11, 10)) {
                      case 0:
                        Format(instr, "sxth'cond 'rd, 'rm");
                        break;
                      case 1:
                        Format(instr, "sxth'cond 'rd, 'rm, ror #8");
                        break;
                      case 2:
                        Format(instr, "sxth'cond 'rd, 'rm, ror #16");
                        break;
                      case 3:
                        Format(instr, "sxth'cond 'rd, 'rm, ror #24");
                        break;
                    }
                  } else {
                    switch (instr->Bits(11, 10)) {
                      case 0:
                        Format(instr, "sxtah'cond 'rd, 'rn, 'rm");
                        break;
                      case 1:
                        Format(instr, "sxtah'cond 'rd, 'rn, 'rm, ror #8");
                        break;
                      case 2:
                        Format(instr, "sxtah'cond 'rd, 'rn, 'rm, ror #16");
                        break;
                      case 3:
                        Format(instr, "sxtah'cond 'rd, 'rn, 'rm, ror #24");
                        break;
                    }
                  }
                }
1199 1200 1201
              } else if (instr->Bits(27, 16) == 0x6BF &&
                         instr->Bits(11, 4) == 0xF3) {
                Format(instr, "rev'cond 'rd, 'rm");
1202 1203 1204
              } else {
                UNREACHABLE();
              }
1205 1206 1207 1208 1209 1210
              break;
            case 2:
              if ((instr->Bit(20) == 0) && (instr->Bits(9, 6) == 1)) {
                if (instr->Bits(19, 16) == 0xF) {
                  switch (instr->Bits(11, 10)) {
                    case 0:
1211
                      Format(instr, "uxtb16'cond 'rd, 'rm");
1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230
                      break;
                    case 1:
                      Format(instr, "uxtb16'cond 'rd, 'rm, ror #8");
                      break;
                    case 2:
                      Format(instr, "uxtb16'cond 'rd, 'rm, ror #16");
                      break;
                    case 3:
                      Format(instr, "uxtb16'cond 'rd, 'rm, ror #24");
                      break;
                  }
                } else {
                  UNREACHABLE();
                }
              } else {
                UNREACHABLE();
              }
              break;
            case 3:
1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262
              if ((instr->Bits(9, 6) == 1)) {
                if ((instr->Bit(20) == 0)) {
                  if (instr->Bits(19, 16) == 0xF) {
                    switch (instr->Bits(11, 10)) {
                      case 0:
                        Format(instr, "uxtb'cond 'rd, 'rm");
                        break;
                      case 1:
                        Format(instr, "uxtb'cond 'rd, 'rm, ror #8");
                        break;
                      case 2:
                        Format(instr, "uxtb'cond 'rd, 'rm, ror #16");
                        break;
                      case 3:
                        Format(instr, "uxtb'cond 'rd, 'rm, ror #24");
                        break;
                    }
                  } else {
                    switch (instr->Bits(11, 10)) {
                      case 0:
                        Format(instr, "uxtab'cond 'rd, 'rn, 'rm");
                        break;
                      case 1:
                        Format(instr, "uxtab'cond 'rd, 'rn, 'rm, ror #8");
                        break;
                      case 2:
                        Format(instr, "uxtab'cond 'rd, 'rn, 'rm, ror #16");
                        break;
                      case 3:
                        Format(instr, "uxtab'cond 'rd, 'rn, 'rm, ror #24");
                        break;
                    }
1263 1264
                  }
                } else {
1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294
                  if (instr->Bits(19, 16) == 0xF) {
                    switch (instr->Bits(11, 10)) {
                      case 0:
                        Format(instr, "uxth'cond 'rd, 'rm");
                        break;
                      case 1:
                        Format(instr, "uxth'cond 'rd, 'rm, ror #8");
                        break;
                      case 2:
                        Format(instr, "uxth'cond 'rd, 'rm, ror #16");
                        break;
                      case 3:
                        Format(instr, "uxth'cond 'rd, 'rm, ror #24");
                        break;
                    }
                  } else {
                    switch (instr->Bits(11, 10)) {
                      case 0:
                        Format(instr, "uxtah'cond 'rd, 'rn, 'rm");
                        break;
                      case 1:
                        Format(instr, "uxtah'cond 'rd, 'rn, 'rm, ror #8");
                        break;
                      case 2:
                        Format(instr, "uxtah'cond 'rd, 'rn, 'rm, ror #16");
                        break;
                      case 3:
                        Format(instr, "uxtah'cond 'rd, 'rn, 'rm, ror #24");
                        break;
                    }
1295 1296 1297
                  }
                }
              } else {
1298
                // PU == 0b01, BW == 0b11, Bits(9, 6) != 0b0001
1299 1300
                if ((instr->Bits(20, 16) == 0x1F) &&
                    (instr->Bits(11, 4) == 0xF3)) {
1301 1302 1303 1304
                  Format(instr, "rbit'cond 'rd, 'rm");
                } else {
                  UNREACHABLE();
                }
1305 1306 1307
              }
              break;
          }
1308 1309
        }
      }
1310 1311
      break;
    }
1312
    case db_x: {
1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323
      if (instr->Bits(22, 20) == 0x5) {
        if (instr->Bits(7, 4) == 0x1) {
          if (instr->Bits(15, 12) == 0xF) {
            Format(instr, "smmul'cond 'rn, 'rm, 'rs");
          } else {
            // SMMLA (in V8 notation matching ARM ISA format)
            Format(instr, "smmla'cond 'rn, 'rm, 'rs, 'rd");
          }
          break;
        }
      }
1324 1325 1326 1327 1328 1329 1330 1331
      if (instr->Bits(5, 4) == 0x1) {
        if ((instr->Bit(22) == 0x0) && (instr->Bit(20) == 0x1)) {
          if (instr->Bit(21) == 0x1) {
            // UDIV (in V8 notation matching ARM ISA format) rn = rm/rs
            Format(instr, "udiv'cond'b 'rn, 'rm, 'rs");
          } else {
            // SDIV (in V8 notation matching ARM ISA format) rn = rm/rs
            Format(instr, "sdiv'cond'b 'rn, 'rm, 'rs");
1332
          }
1333
          break;
1334 1335
        }
      }
1336 1337 1338
      Format(instr, "'memop'cond'b 'rd, ['rn, -'shift_rm]'w");
      break;
    }
1339
    case ib_x: {
1340 1341
      if (instr->HasW() && (instr->Bits(6, 4) == 0x5)) {
        uint32_t widthminus1 = static_cast<uint32_t>(instr->Bits(20, 16));
1342
        uint32_t lsbit = static_cast<uint32_t>(instr->Bits(11, 7));
1343 1344
        uint32_t msbit = widthminus1 + lsbit;
        if (msbit <= 31) {
1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356
          if (instr->Bit(22)) {
            Format(instr, "ubfx'cond 'rd, 'rm, 'f");
          } else {
            Format(instr, "sbfx'cond 'rd, 'rm, 'f");
          }
        } else {
          UNREACHABLE();
        }
      } else if (!instr->HasW() && (instr->Bits(6, 4) == 0x1)) {
        uint32_t lsbit = static_cast<uint32_t>(instr->Bits(11, 7));
        uint32_t msbit = static_cast<uint32_t>(instr->Bits(20, 16));
        if (msbit >= lsbit) {
1357
          if (instr->RmValue() == 15) {
1358 1359 1360 1361
            Format(instr, "bfc'cond 'rd, 'f");
          } else {
            Format(instr, "bfi'cond 'rd, 'rm, 'f");
          }
1362 1363 1364 1365 1366 1367
        } else {
          UNREACHABLE();
        }
      } else {
        Format(instr, "'memop'cond'b 'rd, ['rn, +'shift_rm]'w");
      }
1368 1369 1370 1371 1372 1373 1374 1375 1376 1377
      break;
    }
    default: {
      // The PU field is a 2-bit field.
      UNREACHABLE();
    }
  }
}


1378
void Decoder::DecodeType4(Instruction* instr) {
1379 1380 1381
  if (instr->Bit(22) != 0) {
    // Privileged mode currently not supported.
    Unknown(instr);
1382
  } else {
1383 1384 1385 1386 1387
    if (instr->HasL()) {
      Format(instr, "ldm'cond'pu 'rn'w, 'rlist");
    } else {
      Format(instr, "stm'cond'pu 'rn'w, 'rlist");
    }
1388 1389 1390 1391
  }
}


1392
void Decoder::DecodeType5(Instruction* instr) {
1393 1394 1395 1396
  Format(instr, "b'l'cond 'target");
}


1397
void Decoder::DecodeType6(Instruction* instr) {
1398
  DecodeType6CoprocessorIns(instr);
1399 1400 1401
}


1402
int Decoder::DecodeType7(Instruction* instr) {
1403
  if (instr->Bit(24) == 1) {
1404
    if (instr->SvcValue() >= kStopCode) {
1405 1406 1407 1408
      Format(instr, "stop'cond 'svc");
    } else {
      Format(instr, "svc'cond 'svc");
    }
1409
  } else {
1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421
    switch (instr->CoprocessorValue()) {
      case 10:  // Fall through.
      case 11:
        DecodeTypeVFP(instr);
        break;
      case 15:
        DecodeTypeCP15(instr);
        break;
      default:
        Unknown(instr);
        break;
    }
1422
  }
1423
  return kInstrSize;
lrn@chromium.org's avatar
lrn@chromium.org committed
1424 1425 1426
}


1427
// void Decoder::DecodeTypeVFP(Instruction* instr)
1428 1429 1430 1431
// vmov: Sn = Rt
// vmov: Rt = Sn
// vcvt: Dd = Sm
// vcvt: Sd = Dm
1432
// vcvt.f64.s32 Dd, Dd, #<fbits>
1433
// Dd = vabs(Dm)
1434
// Sd = vabs(Sm)
1435
// Dd = vneg(Dm)
1436
// Sd = vneg(Sm)
1437
// Dd = vadd(Dn, Dm)
1438
// Sd = vadd(Sn, Sm)
1439
// Dd = vsub(Dn, Dm)
1440
// Sd = vsub(Sn, Sm)
1441
// Dd = vmul(Dn, Dm)
1442
// Sd = vmul(Sn, Sm)
1443
// Dd = vmla(Dn, Dm)
1444
// Sd = vmla(Sn, Sm)
1445
// Dd = vmls(Dn, Dm)
1446
// Sd = vmls(Sn, Sm)
1447
// Dd = vdiv(Dn, Dm)
1448
// Sd = vdiv(Sn, Sm)
1449
// vcmp(Dd, Dm)
1450 1451 1452
// vcmp(Sd, Sm)
// Dd = vsqrt(Dm)
// Sd = vsqrt(Sm)
1453
// vmrs
1454
// vmsr
1455 1456 1457
// Qd = vdup.size(Qd, Rt)
// vmov.size: Dd[i] = Rt
// vmov.sign.size: Rt = Dn[i]
1458
void Decoder::DecodeTypeVFP(Instruction* instr) {
1459 1460
  VERIFY((instr->TypeValue() == 7) && (instr->Bit(24) == 0x0) );
  VERIFY(instr->Bits(11, 9) == 0x5);
1461 1462

  if (instr->Bit(4) == 0) {
1463
    if (instr->Opc1Value() == 0x7) {
1464
      // Other data processing instructions
1465
      if ((instr->Opc2Value() == 0x0) && (instr->Opc3Value() == 0x1)) {
1466
        // vmov register to register.
1467
        if (instr->SzValue() == 0x1) {
1468
          Format(instr, "vmov'cond.f64 'Dd, 'Dm");
1469
        } else {
1470
          Format(instr, "vmov'cond.f32 'Sd, 'Sm");
1471
        }
1472 1473
      } else if ((instr->Opc2Value() == 0x0) && (instr->Opc3Value() == 0x3)) {
        // vabs
1474 1475 1476 1477 1478
        if (instr->SzValue() == 0x1) {
          Format(instr, "vabs'cond.f64 'Dd, 'Dm");
        } else {
          Format(instr, "vabs'cond.f32 'Sd, 'Sm");
        }
1479 1480
      } else if ((instr->Opc2Value() == 0x1) && (instr->Opc3Value() == 0x1)) {
        // vneg
1481 1482 1483 1484 1485
        if (instr->SzValue() == 0x1) {
          Format(instr, "vneg'cond.f64 'Dd, 'Dm");
        } else {
          Format(instr, "vneg'cond.f32 'Sd, 'Sm");
        }
1486
      } else if ((instr->Opc2Value() == 0x7) && (instr->Opc3Value() == 0x3)) {
1487
        DecodeVCVTBetweenDoubleAndSingle(instr);
1488
      } else if ((instr->Opc2Value() == 0x8) && (instr->Opc3Value() & 0x1)) {
1489
        DecodeVCVTBetweenFloatingPointAndInteger(instr);
1490 1491 1492
      } else if ((instr->Opc2Value() == 0xA) && (instr->Opc3Value() == 0x3) &&
                 (instr->Bit(8) == 1)) {
        // vcvt.f64.s32 Dd, Dd, #<fbits>
1493
        int fraction_bits = 32 - ((instr->Bits(3, 0) << 1) | instr->Bit(5));
1494
        Format(instr, "vcvt'cond.f64.s32 'Dd, 'Dd");
1495 1496
        out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
                                    ", #%d", fraction_bits);
1497 1498
      } else if (((instr->Opc2Value() >> 1) == 0x6) &&
                 (instr->Opc3Value() & 0x1)) {
1499
        DecodeVCVTBetweenFloatingPointAndInteger(instr);
1500 1501
      } else if (((instr->Opc2Value() == 0x4) || (instr->Opc2Value() == 0x5)) &&
                 (instr->Opc3Value() & 0x1)) {
1502
        DecodeVCMP(instr);
1503
      } else if (((instr->Opc2Value() == 0x1)) && (instr->Opc3Value() == 0x3)) {
1504 1505 1506 1507 1508
        if (instr->SzValue() == 0x1) {
          Format(instr, "vsqrt'cond.f64 'Dd, 'Dm");
        } else {
          Format(instr, "vsqrt'cond.f32 'Sd, 'Sm");
        }
1509 1510
      } else if (instr->Opc3Value() == 0x0) {
        if (instr->SzValue() == 0x1) {
1511
          Format(instr, "vmov'cond.f64 'Dd, 'd");
1512
        } else {
1513
          Format(instr, "vmov'cond.f32 'Sd, 'd");
1514
        }
1515 1516
      } else if (((instr->Opc2Value() == 0x6)) && instr->Opc3Value() == 0x3) {
        // vrintz - round towards zero (truncate)
1517
        if (instr->SzValue() == 0x1) {
1518 1519
          Format(instr, "vrintz'cond.f64.f64 'Dd, 'Dm");
        } else {
1520
          Format(instr, "vrintz'cond.f32.f32 'Sd, 'Sm");
1521
        }
1522 1523 1524
      } else {
        Unknown(instr);  // Not used by V8.
      }
1525 1526 1527
    } else if (instr->Opc1Value() == 0x3) {
      if (instr->SzValue() == 0x1) {
        if (instr->Opc3Value() & 0x1) {
1528
          Format(instr, "vsub'cond.f64 'Dd, 'Dn, 'Dm");
1529
        } else {
1530
          Format(instr, "vadd'cond.f64 'Dd, 'Dn, 'Dm");
1531 1532
        }
      } else {
1533 1534 1535 1536 1537
        if (instr->Opc3Value() & 0x1) {
          Format(instr, "vsub'cond.f32 'Sd, 'Sn, 'Sm");
        } else {
          Format(instr, "vadd'cond.f32 'Sd, 'Sn, 'Sm");
        }
1538
      }
1539 1540
    } else if ((instr->Opc1Value() == 0x2) && !(instr->Opc3Value() & 0x1)) {
      if (instr->SzValue() == 0x1) {
1541
        Format(instr, "vmul'cond.f64 'Dd, 'Dn, 'Dm");
1542
      } else {
1543
        Format(instr, "vmul'cond.f32 'Sd, 'Sn, 'Sm");
1544
      }
1545 1546
    } else if ((instr->Opc1Value() == 0x0) && !(instr->Opc3Value() & 0x1)) {
      if (instr->SzValue() == 0x1) {
1547
        Format(instr, "vmla'cond.f64 'Dd, 'Dn, 'Dm");
1548
      } else {
1549
        Format(instr, "vmla'cond.f32 'Sd, 'Sn, 'Sm");
1550
      }
1551 1552
    } else if ((instr->Opc1Value() == 0x0) && (instr->Opc3Value() & 0x1)) {
      if (instr->SzValue() == 0x1) {
1553
        Format(instr, "vmls'cond.f64 'Dd, 'Dn, 'Dm");
1554
      } else {
1555
        Format(instr, "vmls'cond.f32 'Sd, 'Sn, 'Sm");
1556
      }
1557 1558
    } else if ((instr->Opc1Value() == 0x4) && !(instr->Opc3Value() & 0x1)) {
      if (instr->SzValue() == 0x1) {
1559
        Format(instr, "vdiv'cond.f64 'Dd, 'Dn, 'Dm");
1560
      } else {
1561
        Format(instr, "vdiv'cond.f32 'Sd, 'Sn, 'Sm");
1562
      }
1563
    } else {
1564
      Unknown(instr);  // Not used by V8.
1565
    }
1566
  } else {
1567 1568
    if ((instr->VCValue() == 0x0) &&
        (instr->VAValue() == 0x0)) {
1569
      DecodeVMOVBetweenCoreAndSinglePrecisionRegisters(instr);
1570
    } else if ((instr->VLValue() == 0x0) && (instr->VCValue() == 0x1)) {
1571
      const char* rt_name = converter_.NameOfCPURegister(instr->RtValue());
1572 1573
      if (instr->Bit(23) == 0) {
        int opc1_opc2 = (instr->Bits(22, 21) << 2) | instr->Bits(6, 5);
1574
        if ((opc1_opc2 & 0xB) == 0) {
1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586
          // NeonS32/NeonU32
          if (instr->Bit(21) == 0x0) {
            Format(instr, "vmov'cond.32 'Dd[0], 'rt");
          } else {
            Format(instr, "vmov'cond.32 'Dd[1], 'rt");
          }
        } else {
          int vd = instr->VFPNRegValue(kDoublePrecision);
          if ((opc1_opc2 & 0x8) != 0) {
            // NeonS8 / NeonU8
            int i = opc1_opc2 & 0x7;
            out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
1587
                                        "vmov.8 d%d[%d], %s", vd, i, rt_name);
1588 1589 1590 1591
          } else if ((opc1_opc2 & 0x1) != 0) {
            // NeonS16 / NeonU16
            int i = (opc1_opc2 >> 1) & 0x3;
            out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
1592
                                        "vmov.16 d%d[%d], %s", vd, i, rt_name);
1593 1594 1595 1596
          } else {
            Unknown(instr);
          }
        }
1597
      } else {
1598
        int size = 32;
1599
        if (instr->Bit(5) != 0) {
1600
          size = 16;
1601
        } else if (instr->Bit(22) != 0) {
1602
          size = 8;
1603
        }
1604 1605
        int Vd = instr->VFPNRegValue(kSimd128Precision);
        out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
1606
                                    "vdup.%i q%d, %s", size, Vd, rt_name);
1607
      }
1608 1609
    } else if ((instr->VLValue() == 0x1) && (instr->VCValue() == 0x1)) {
      int opc1_opc2 = (instr->Bits(22, 21) << 2) | instr->Bits(6, 5);
1610
      if ((opc1_opc2 & 0xB) == 0) {
1611 1612 1613 1614 1615 1616
        // NeonS32 / NeonU32
        if (instr->Bit(21) == 0x0) {
          Format(instr, "vmov'cond.32 'rt, 'Dd[0]");
        } else {
          Format(instr, "vmov'cond.32 'rt, 'Dd[1]");
        }
1617
      } else {
1618
        char sign = instr->Bit(23) != 0 ? 'u' : 's';
1619
        const char* rt_name = converter_.NameOfCPURegister(instr->RtValue());
1620 1621 1622 1623
        int vn = instr->VFPNRegValue(kDoublePrecision);
        if ((opc1_opc2 & 0x8) != 0) {
          // NeonS8 / NeonU8
          int i = opc1_opc2 & 0x7;
1624 1625 1626
          out_buffer_pos_ +=
              SNPrintF(out_buffer_ + out_buffer_pos_, "vmov.%c8 %s, d%d[%d]",
                       sign, rt_name, vn, i);
1627 1628 1629 1630
        } else if ((opc1_opc2 & 0x1) != 0) {
          // NeonS16 / NeonU16
          int i = (opc1_opc2 >> 1) & 0x3;
          out_buffer_pos_ +=
1631 1632
              SNPrintF(out_buffer_ + out_buffer_pos_, "vmov.%c16 %s, d%d[%d]",
                       sign, rt_name, vn, i);
1633 1634 1635
        } else {
          Unknown(instr);
        }
1636
      }
1637 1638
    } else if ((instr->VCValue() == 0x0) &&
               (instr->VAValue() == 0x7) &&
1639
               (instr->Bits(19, 16) == 0x1)) {
1640
      if (instr->VLValue() == 0) {
1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652
        if (instr->Bits(15, 12) == 0xF) {
          Format(instr, "vmsr'cond FPSCR, APSR");
        } else {
          Format(instr, "vmsr'cond FPSCR, 'rt");
        }
      } else {
        if (instr->Bits(15, 12) == 0xF) {
          Format(instr, "vmrs'cond APSR, FPSCR");
        } else {
          Format(instr, "vmrs'cond 'rt, FPSCR");
        }
      }
1653 1654
    } else {
      Unknown(instr);  // Not used by V8.
1655
    }
1656 1657 1658
  }
}

1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686
void Decoder::DecodeTypeCP15(Instruction* instr) {
  VERIFY((instr->TypeValue() == 7) && (instr->Bit(24) == 0x0));
  VERIFY(instr->CoprocessorValue() == 15);

  if (instr->Bit(4) == 1) {
    int crn = instr->Bits(19, 16);
    int crm = instr->Bits(3, 0);
    int opc1 = instr->Bits(23, 21);
    int opc2 = instr->Bits(7, 5);
    if ((opc1 == 0) && (crn == 7)) {
      // ARMv6 memory barrier operations.
      // Details available in ARM DDI 0406C.b, B3-1750.
      if ((crm == 10) && (opc2 == 5)) {
        Format(instr, "mcr'cond (CP15DMB)");
      } else if ((crm == 10) && (opc2 == 4)) {
        Format(instr, "mcr'cond (CP15DSB)");
      } else if ((crm == 5) && (opc2 == 4)) {
        Format(instr, "mcr'cond (CP15ISB)");
      } else {
        Unknown(instr);
      }
    } else {
      Unknown(instr);
    }
  } else {
    Unknown(instr);
  }
}
1687

1688 1689
void Decoder::DecodeVMOVBetweenCoreAndSinglePrecisionRegisters(
    Instruction* instr) {
1690
  VERIFY((instr->Bit(4) == 1) && (instr->VCValue() == 0x0) &&
1691
         (instr->VAValue() == 0x0));
1692

1693
  bool to_arm_register = (instr->VLValue() == 0x1);
1694 1695 1696 1697 1698 1699 1700 1701 1702

  if (to_arm_register) {
    Format(instr, "vmov'cond 'rt, 'Sn");
  } else {
    Format(instr, "vmov'cond 'Sn, 'rt");
  }
}


1703
void Decoder::DecodeVCMP(Instruction* instr) {
1704 1705
  VERIFY((instr->Bit(4) == 0) && (instr->Opc1Value() == 0x7));
  VERIFY(((instr->Opc2Value() == 0x4) || (instr->Opc2Value() == 0x5)) &&
1706
         (instr->Opc3Value() & 0x1));
1707 1708

  // Comparison.
1709
  bool dp_operation = (instr->SzValue() == 1);
1710 1711 1712
  bool raise_exception_for_qnan = (instr->Bit(7) == 0x1);

  if (dp_operation && !raise_exception_for_qnan) {
1713
    if (instr->Opc2Value() == 0x4) {
1714
      Format(instr, "vcmp'cond.f64 'Dd, 'Dm");
1715
    } else if (instr->Opc2Value() == 0x5) {
1716
      Format(instr, "vcmp'cond.f64 'Dd, #0.0");
1717 1718 1719
    } else {
      Unknown(instr);  // invalid
    }
1720 1721 1722 1723 1724 1725 1726 1727
  } else if (!raise_exception_for_qnan) {
    if (instr->Opc2Value() == 0x4) {
      Format(instr, "vcmp'cond.f32 'Sd, 'Sm");
    } else if (instr->Opc2Value() == 0x5) {
      Format(instr, "vcmp'cond.f32 'Sd, #0.0");
    } else {
      Unknown(instr);  // invalid
    }
1728 1729 1730 1731 1732 1733
  } else {
    Unknown(instr);  // Not used by V8.
  }
}


1734
void Decoder::DecodeVCVTBetweenDoubleAndSingle(Instruction* instr) {
1735 1736
  VERIFY((instr->Bit(4) == 0) && (instr->Opc1Value() == 0x7));
  VERIFY((instr->Opc2Value() == 0x7) && (instr->Opc3Value() == 0x3));
1737

1738
  bool double_to_single = (instr->SzValue() == 1);
1739 1740

  if (double_to_single) {
1741
    Format(instr, "vcvt'cond.f32.f64 'Sd, 'Dm");
1742
  } else {
1743
    Format(instr, "vcvt'cond.f64.f32 'Dd, 'Sm");
1744 1745 1746 1747
  }
}


1748
void Decoder::DecodeVCVTBetweenFloatingPointAndInteger(Instruction* instr) {
1749 1750
  VERIFY((instr->Bit(4) == 0) && (instr->Opc1Value() == 0x7));
  VERIFY(((instr->Opc2Value() == 0x8) && (instr->Opc3Value() & 0x1)) ||
1751
         (((instr->Opc2Value() >> 1) == 0x6) && (instr->Opc3Value() & 0x1)));
1752 1753

  bool to_integer = (instr->Bit(18) == 1);
1754
  bool dp_operation = (instr->SzValue() == 1);
1755 1756 1757 1758 1759
  if (to_integer) {
    bool unsigned_integer = (instr->Bit(16) == 0);

    if (dp_operation) {
      if (unsigned_integer) {
1760
        Format(instr, "vcvt'cond.u32.f64 'Sd, 'Dm");
1761
      } else {
1762
        Format(instr, "vcvt'cond.s32.f64 'Sd, 'Dm");
1763 1764 1765
      }
    } else {
      if (unsigned_integer) {
1766
        Format(instr, "vcvt'cond.u32.f32 'Sd, 'Sm");
1767
      } else {
1768
        Format(instr, "vcvt'cond.s32.f32 'Sd, 'Sm");
1769 1770
      }
    }
1771
  } else {
1772 1773 1774 1775
    bool unsigned_integer = (instr->Bit(7) == 0);

    if (dp_operation) {
      if (unsigned_integer) {
1776
        Format(instr, "vcvt'cond.f64.u32 'Dd, 'Sm");
1777
      } else {
1778
        Format(instr, "vcvt'cond.f64.s32 'Dd, 'Sm");
1779
      }
1780
    } else {
1781
      if (unsigned_integer) {
1782
        Format(instr, "vcvt'cond.f32.u32 'Sd, 'Sm");
1783
      } else {
1784
        Format(instr, "vcvt'cond.f32.s32 'Sd, 'Sm");
1785
      }
1786 1787 1788 1789 1790
    }
  }
}


1791
// Decode Type 6 coprocessor instructions.
1792 1793
// Dm = vmov(Rt, Rt2)
// <Rt, Rt2> = vmov(Dm)
1794 1795
// Ddst = MEM(Rbase + 4*offset).
// MEM(Rbase + 4*offset) = Dsrc.
1796
void Decoder::DecodeType6CoprocessorIns(Instruction* instr) {
1797
  VERIFY(instr->TypeValue() == 6);
1798

1799 1800
  if (instr->CoprocessorValue() == 0xA) {
    switch (instr->OpcodeValue()) {
1801
      case 0x8:
1802
      case 0xA:
1803
        if (instr->HasL()) {
1804
          Format(instr, "vldr'cond 'Sd, ['rn - 4*'imm08@00]");
1805
        } else {
1806
          Format(instr, "vstr'cond 'Sd, ['rn - 4*'imm08@00]");
1807 1808 1809
        }
        break;
      case 0xC:
1810
      case 0xE:
1811
        if (instr->HasL()) {
1812
          Format(instr, "vldr'cond 'Sd, ['rn + 4*'imm08@00]");
1813
        } else {
1814
          Format(instr, "vstr'cond 'Sd, ['rn + 4*'imm08@00]");
1815 1816
        }
        break;
1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830
      case 0x4:
      case 0x5:
      case 0x6:
      case 0x7:
      case 0x9:
      case 0xB: {
        bool to_vfp_register = (instr->VLValue() == 0x1);
        if (to_vfp_register) {
          Format(instr, "vldm'cond'pu 'rn'w, {'Sd-'Sd+}");
        } else {
          Format(instr, "vstm'cond'pu 'rn'w, {'Sd-'Sd+}");
        }
        break;
      }
1831 1832 1833
      default:
        Unknown(instr);  // Not used by V8.
    }
1834 1835
  } else if (instr->CoprocessorValue() == 0xB) {
    switch (instr->OpcodeValue()) {
1836 1837
      case 0x2:
        // Load and store double to two GP registers
1838
        if (instr->Bits(7, 6) != 0 || instr->Bit(4) != 1) {
1839 1840 1841 1842 1843 1844 1845 1846
          Unknown(instr);  // Not used by V8.
        } else if (instr->HasL()) {
          Format(instr, "vmov'cond 'rt, 'rn, 'Dm");
        } else {
          Format(instr, "vmov'cond 'Dm, 'rt, 'rn");
        }
        break;
      case 0x8:
1847
      case 0xA:
1848
        if (instr->HasL()) {
1849
          Format(instr, "vldr'cond 'Dd, ['rn - 4*'imm08@00]");
1850
        } else {
1851
          Format(instr, "vstr'cond 'Dd, ['rn - 4*'imm08@00]");
1852 1853 1854
        }
        break;
      case 0xC:
1855
      case 0xE:
1856
        if (instr->HasL()) {
1857
          Format(instr, "vldr'cond 'Dd, ['rn + 4*'imm08@00]");
1858
        } else {
1859
          Format(instr, "vstr'cond 'Dd, ['rn + 4*'imm08@00]");
1860 1861
        }
        break;
1862 1863
      case 0x4:
      case 0x5:
1864 1865 1866 1867
      case 0x6:
      case 0x7:
      case 0x9:
      case 0xB: {
1868 1869 1870 1871 1872 1873 1874 1875
        bool to_vfp_register = (instr->VLValue() == 0x1);
        if (to_vfp_register) {
          Format(instr, "vldm'cond'pu 'rn'w, {'Dd-'Dd+}");
        } else {
          Format(instr, "vstm'cond'pu 'rn'w, {'Dd-'Dd+}");
        }
        break;
      }
1876 1877 1878
      default:
        Unknown(instr);  // Not used by V8.
    }
1879
  } else {
1880 1881 1882 1883
    Unknown(instr);  // Not used by V8.
  }
}

1884

1885 1886 1887 1888 1889 1890
static const char* const barrier_option_names[] = {
    "invalid", "oshld", "oshst", "osh", "invalid", "nshld", "nshst", "nsh",
    "invalid", "ishld", "ishst", "ish", "invalid", "ld",    "st",    "sy",
};


1891 1892
void Decoder::DecodeSpecialCondition(Instruction* instr) {
  switch (instr->SpecialValue()) {
1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903
    case 4: {
      int Vd, Vm, Vn;
      if (instr->Bit(6) == 0) {
        Vd = instr->VFPDRegValue(kDoublePrecision);
        Vm = instr->VFPMRegValue(kDoublePrecision);
        Vn = instr->VFPNRegValue(kDoublePrecision);
      } else {
        Vd = instr->VFPDRegValue(kSimd128Precision);
        Vm = instr->VFPMRegValue(kSimd128Precision);
        Vn = instr->VFPNRegValue(kSimd128Precision);
      }
1904
      int size = kBitsPerByte * (1 << instr->Bits(21, 20));
1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952
      switch (instr->Bits(11, 8)) {
        case 0x0: {
          if (instr->Bit(4) == 1) {
            // vqadd.s<size> Qd, Qm, Qn.
            out_buffer_pos_ +=
                SNPrintF(out_buffer_ + out_buffer_pos_,
                         "vqadd.s%d q%d, q%d, q%d", size, Vd, Vn, Vm);
          } else {
            Unknown(instr);
          }
          break;
        }
        case 0x1: {
          if (instr->Bits(21, 20) == 2 && instr->Bit(6) == 1 &&
              instr->Bit(4) == 1) {
            if (Vm == Vn) {
              // vmov Qd, Qm
              out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
                                          "vmov q%d, q%d", Vd, Vm);
            } else {
              // vorr Qd, Qm, Qn.
              out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
                                          "vorr q%d, q%d, q%d", Vd, Vn, Vm);
            }
          } else if (instr->Bits(21, 20) == 0 && instr->Bit(6) == 1 &&
                     instr->Bit(4) == 1) {
            // vand Qd, Qm, Qn.
            out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
                                        "vand q%d, q%d, q%d", Vd, Vn, Vm);
          } else {
            Unknown(instr);
          }
          break;
        }
        case 0x2: {
          if (instr->Bit(4) == 1) {
            // vqsub.s<size> Qd, Qm, Qn.
            out_buffer_pos_ +=
                SNPrintF(out_buffer_ + out_buffer_pos_,
                         "vqsub.s%d q%d, q%d, q%d", size, Vd, Vn, Vm);
          } else {
            Unknown(instr);
          }
          break;
        }
        case 0x3: {
          const char* op = (instr->Bit(4) == 1) ? "vcge" : "vcgt";
          // vcge/vcgt.s<size> Qd, Qm, Qn.
1953
          out_buffer_pos_ +=
1954 1955 1956
              SNPrintF(out_buffer_ + out_buffer_pos_, "%s.s%d q%d, q%d, q%d",
                       op, size, Vd, Vn, Vm);
          break;
1957
        }
1958 1959 1960 1961 1962 1963 1964
        case 0x6: {
          // vmin/vmax.s<size> Qd, Qm, Qn.
          const char* op = instr->Bit(4) == 1 ? "vmin" : "vmax";
          out_buffer_pos_ +=
              SNPrintF(out_buffer_ + out_buffer_pos_, "%s.s%d q%d, q%d, q%d",
                       op, size, Vd, Vn, Vm);
          break;
1965
        }
1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984
        case 0x8: {
          const char* op = (instr->Bit(4) == 0) ? "vadd" : "vtst";
          // vadd/vtst.i<size> Qd, Qm, Qn.
          out_buffer_pos_ +=
              SNPrintF(out_buffer_ + out_buffer_pos_, "%s.i%d q%d, q%d, q%d",
                       op, size, Vd, Vn, Vm);
          break;
        }
        case 0x9: {
          if (instr->Bit(6) == 1 && instr->Bit(4) == 1) {
            // vmul.i<size> Qd, Qm, Qn.
            out_buffer_pos_ +=
                SNPrintF(out_buffer_ + out_buffer_pos_,
                         "vmul.i%d q%d, q%d, q%d", size, Vd, Vn, Vm);
          } else {
            Unknown(instr);
          }
          break;
        }
1985
        case 0xA: {
1986 1987 1988 1989 1990 1991 1992
          // vpmin/vpmax.s<size> Dd, Dm, Dn.
          const char* op = instr->Bit(4) == 1 ? "vpmin" : "vpmax";
          out_buffer_pos_ +=
              SNPrintF(out_buffer_ + out_buffer_pos_, "%s.s%d d%d, d%d, d%d",
                       op, size, Vd, Vn, Vm);
          break;
        }
1993
        case 0xB: {
1994 1995 1996 1997 1998 1999
          // vpadd.i<size> Dd, Dm, Dn.
          out_buffer_pos_ +=
              SNPrintF(out_buffer_ + out_buffer_pos_, "vpadd.i%d d%d, d%d, d%d",
                       size, Vd, Vn, Vm);
          break;
        }
2000
        case 0xD: {
2001 2002 2003 2004 2005 2006 2007 2008 2009 2010
          if (instr->Bit(4) == 0) {
            const char* op = (instr->Bits(21, 20) == 0) ? "vadd" : "vsub";
            // vadd/vsub.f32 Qd, Qm, Qn.
            out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
                                        "%s.f32 q%d, q%d, q%d", op, Vd, Vn, Vm);
          } else {
            Unknown(instr);
          }
          break;
        }
2011
        case 0xE: {
2012 2013 2014 2015 2016 2017 2018 2019 2020
          if (instr->Bits(21, 20) == 0 && instr->Bit(4) == 0) {
            // vceq.f32 Qd, Qm, Qn.
            out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
                                        "vceq.f32 q%d, q%d, q%d", Vd, Vn, Vm);
          } else {
            Unknown(instr);
          }
          break;
        }
2021
        case 0xF: {
2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043
          if (instr->Bit(20) == 0 && instr->Bit(6) == 1) {
            if (instr->Bit(4) == 1) {
              // vrecps/vrsqrts.f32 Qd, Qm, Qn.
              const char* op = instr->Bit(21) == 0 ? "vrecps" : "vrsqrts";
              out_buffer_pos_ +=
                  SNPrintF(out_buffer_ + out_buffer_pos_,
                           "%s.f32 q%d, q%d, q%d", op, Vd, Vn, Vm);
            } else {
              // vmin/max.f32 Qd, Qm, Qn.
              const char* op = instr->Bit(21) == 1 ? "vmin" : "vmax";
              out_buffer_pos_ +=
                  SNPrintF(out_buffer_ + out_buffer_pos_,
                           "%s.f32 q%d, q%d, q%d", op, Vd, Vn, Vm);
            }
          } else {
            Unknown(instr);
          }
          break;
        }
        default:
          Unknown(instr);
          break;
2044 2045
      }
      break;
2046
    }
2047 2048 2049 2050
    case 5:
      if ((instr->Bits(18, 16) == 0) && (instr->Bits(11, 6) == 0x28) &&
          (instr->Bit(4) == 1)) {
        // vmovl signed
2051 2052
        if ((instr->VdValue() & 1) != 0) Unknown(instr);
        int Vd = (instr->Bit(22) << 3) | (instr->VdValue() >> 1);
2053 2054
        int Vm = (instr->Bit(5) << 4) | instr->VmValue();
        int imm3 = instr->Bits(21, 19);
2055
        out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
2056
                                    "vmovl.s%d q%d, d%d", imm3 * 8, Vd, Vm);
2057 2058 2059 2060 2061 2062 2063 2064 2065
      } else if (instr->Bits(21, 20) == 3 && instr->Bit(4) == 0) {
        // vext.8 Qd, Qm, Qn, imm4
        int imm4 = instr->Bits(11, 8);
        int Vd = instr->VFPDRegValue(kSimd128Precision);
        int Vm = instr->VFPMRegValue(kSimd128Precision);
        int Vn = instr->VFPNRegValue(kSimd128Precision);
        out_buffer_pos_ +=
            SNPrintF(out_buffer_ + out_buffer_pos_, "vext.8 q%d, q%d, q%d, #%d",
                     Vd, Vn, Vm, imm4);
2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083
      } else if (instr->Bits(11, 7) == 0xA && instr->Bit(4) == 1) {
        // vshl.i<size> Qd, Qm, shift
        int size = base::bits::RoundDownToPowerOfTwo32(instr->Bits(21, 16));
        int shift = instr->Bits(21, 16) - size;
        int Vd = instr->VFPDRegValue(kSimd128Precision);
        int Vm = instr->VFPMRegValue(kSimd128Precision);
        out_buffer_pos_ +=
            SNPrintF(out_buffer_ + out_buffer_pos_, "vshl.i%d q%d, q%d, #%d",
                     size, Vd, Vm, shift);
      } else if (instr->Bits(11, 7) == 0 && instr->Bit(4) == 1) {
        // vshr.s<size> Qd, Qm, shift
        int size = base::bits::RoundDownToPowerOfTwo32(instr->Bits(21, 16));
        int shift = 2 * size - instr->Bits(21, 16);
        int Vd = instr->VFPDRegValue(kSimd128Precision);
        int Vm = instr->VFPMRegValue(kSimd128Precision);
        out_buffer_pos_ +=
            SNPrintF(out_buffer_ + out_buffer_pos_, "vshr.s%d q%d, q%d, #%d",
                     size, Vd, Vm, shift);
2084 2085 2086 2087
      } else {
        Unknown(instr);
      }
      break;
2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098
    case 6: {
      int Vd, Vm, Vn;
      if (instr->Bit(6) == 0) {
        Vd = instr->VFPDRegValue(kDoublePrecision);
        Vm = instr->VFPMRegValue(kDoublePrecision);
        Vn = instr->VFPNRegValue(kDoublePrecision);
      } else {
        Vd = instr->VFPDRegValue(kSimd128Precision);
        Vm = instr->VFPMRegValue(kSimd128Precision);
        Vn = instr->VFPNRegValue(kSimd128Precision);
      }
2099
      int size = kBitsPerByte * (1 << instr->Bits(21, 20));
2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145
      switch (instr->Bits(11, 8)) {
        case 0x0: {
          if (instr->Bit(4) == 1) {
            // vqadd.u<size> Qd, Qm, Qn.
            out_buffer_pos_ +=
                SNPrintF(out_buffer_ + out_buffer_pos_,
                         "vqadd.u%d q%d, q%d, q%d", size, Vd, Vn, Vm);
          } else {
            Unknown(instr);
          }
          break;
        }
        case 0x1: {
          if (instr->Bits(21, 20) == 1 && instr->Bit(4) == 1) {
            out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
                                        "vbsl q%d, q%d, q%d", Vd, Vn, Vm);
          } else if (instr->Bits(21, 20) == 0 && instr->Bit(4) == 1) {
            if (instr->Bit(6) == 0) {
              // veor Dd, Dn, Dm
              out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
                                          "veor d%d, d%d, d%d", Vd, Vn, Vm);

            } else {
              // veor Qd, Qn, Qm
              out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
                                          "veor q%d, q%d, q%d", Vd, Vn, Vm);
            }
          } else {
            Unknown(instr);
          }
          break;
        }
        case 0x2: {
          if (instr->Bit(4) == 1) {
            // vqsub.u<size> Qd, Qm, Qn.
            out_buffer_pos_ +=
                SNPrintF(out_buffer_ + out_buffer_pos_,
                         "vqsub.u%d q%d, q%d, q%d", size, Vd, Vn, Vm);
          } else {
            Unknown(instr);
          }
          break;
        }
        case 0x3: {
          const char* op = (instr->Bit(4) == 1) ? "vcge" : "vcgt";
          // vcge/vcgt.u<size> Qd, Qm, Qn.
2146
          out_buffer_pos_ +=
2147 2148 2149 2150 2151 2152 2153
              SNPrintF(out_buffer_ + out_buffer_pos_, "%s.u%d q%d, q%d, q%d",
                       op, size, Vd, Vn, Vm);
          break;
        }
        case 0x6: {
          // vmin/vmax.u<size> Qd, Qm, Qn.
          const char* op = instr->Bit(4) == 1 ? "vmin" : "vmax";
2154
          out_buffer_pos_ +=
2155 2156 2157
              SNPrintF(out_buffer_ + out_buffer_pos_, "%s.u%d q%d, q%d, q%d",
                       op, size, Vd, Vn, Vm);
          break;
2158
        }
2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169
        case 0x8: {
          if (instr->Bit(4) == 0) {
            out_buffer_pos_ +=
                SNPrintF(out_buffer_ + out_buffer_pos_,
                         "vsub.i%d q%d, q%d, q%d", size, Vd, Vn, Vm);
          } else {
            out_buffer_pos_ +=
                SNPrintF(out_buffer_ + out_buffer_pos_,
                         "vceq.i%d q%d, q%d, q%d", size, Vd, Vn, Vm);
          }
          break;
2170
        }
2171
        case 0xA: {
2172 2173 2174 2175 2176 2177 2178
          // vpmin/vpmax.u<size> Dd, Dm, Dn.
          const char* op = instr->Bit(4) == 1 ? "vpmin" : "vpmax";
          out_buffer_pos_ +=
              SNPrintF(out_buffer_ + out_buffer_pos_, "%s.u%d d%d, d%d, d%d",
                       op, size, Vd, Vn, Vm);
          break;
        }
2179
        case 0xD: {
2180 2181 2182
          if (instr->Bits(21, 20) == 0 && instr->Bit(6) == 1 &&
              instr->Bit(4) == 1) {
            // vmul.f32 Qd, Qm, Qn
2183 2184
            out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
                                        "vmul.f32 q%d, q%d, q%d", Vd, Vn, Vm);
2185 2186 2187 2188 2189
          } else if (instr->Bits(21, 20) == 0 && instr->Bit(6) == 0 &&
                     instr->Bit(4) == 0) {
            // vpadd.f32 Dd, Dm, Dn.
            out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
                                        "vpadd.f32 d%d, d%d, d%d", Vd, Vn, Vm);
2190 2191 2192 2193 2194
          } else {
            Unknown(instr);
          }
          break;
        }
2195
        case 0xE: {
2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208
          if (instr->Bit(20) == 0 && instr->Bit(4) == 0) {
            const char* op = (instr->Bit(21) == 0) ? "vcge" : "vcgt";
            // vcge/vcgt.f32 Qd, Qm, Qn.
            out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
                                        "%s.f32 q%d, q%d, q%d", op, Vd, Vn, Vm);
          } else {
            Unknown(instr);
          }
          break;
        }
        default:
          Unknown(instr);
          break;
2209 2210
      }
      break;
2211
    }
2212 2213 2214 2215
    case 7:
      if ((instr->Bits(18, 16) == 0) && (instr->Bits(11, 6) == 0x28) &&
          (instr->Bit(4) == 1)) {
        // vmovl unsigned
2216 2217
        if ((instr->VdValue() & 1) != 0) Unknown(instr);
        int Vd = (instr->Bit(22) << 3) | (instr->VdValue() >> 1);
2218 2219
        int Vm = (instr->Bit(5) << 4) | instr->VmValue();
        int imm3 = instr->Bits(21, 19);
2220
        out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
2221
                                    "vmovl.u%d q%d, d%d", imm3 * 8, Vd, Vm);
2222
      } else if (instr->Opc1Value() == 7 && instr->Bit(4) == 0) {
2223
        if (instr->Bits(11, 7) == 0x18) {
2224
          int Vm = instr->VFPMRegValue(kDoublePrecision);
2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247
          int imm4 = instr->Bits(19, 16);
          int size = 0, index = 0;
          if ((imm4 & 0x1) != 0) {
            size = 8;
            index = imm4 >> 1;
          } else if ((imm4 & 0x2) != 0) {
            size = 16;
            index = imm4 >> 2;
          } else {
            size = 32;
            index = imm4 >> 3;
          }
          if (instr->Bit(6) == 0) {
            int Vd = instr->VFPDRegValue(kDoublePrecision);
            out_buffer_pos_ +=
                SNPrintF(out_buffer_ + out_buffer_pos_, "vdup.%i d%d, d%d[%d]",
                         size, Vd, Vm, index);
          } else {
            int Vd = instr->VFPDRegValue(kSimd128Precision);
            out_buffer_pos_ +=
                SNPrintF(out_buffer_ + out_buffer_pos_, "vdup.%i q%d, d%d[%d]",
                         size, Vd, Vm, index);
          }
2248 2249 2250 2251 2252 2253
        } else if (instr->Bits(11, 10) == 0x2) {
          int Vd = instr->VFPDRegValue(kDoublePrecision);
          int Vn = instr->VFPNRegValue(kDoublePrecision);
          int Vm = instr->VFPMRegValue(kDoublePrecision);
          int len = instr->Bits(9, 8);
          NeonListOperand list(DwVfpRegister::from_code(Vn), len + 1);
2254
          out_buffer_pos_ +=
2255 2256 2257 2258 2259
              SNPrintF(out_buffer_ + out_buffer_pos_, "%s d%d, ",
                       instr->Bit(6) == 0 ? "vtbl.8" : "vtbx.8", Vd);
          FormatNeonList(Vn, list.type());
          Print(", ");
          PrintDRegister(Vm);
2260 2261 2262 2263 2264 2265 2266 2267 2268 2269
        } else if (instr->Bits(17, 16) == 0x2 && instr->Bits(11, 8) == 0x2 &&
                   instr->Bits(7, 6) != 0) {
          // vqmovn.<type><size> Dd, Qm.
          int Vd = instr->VFPDRegValue(kDoublePrecision);
          int Vm = instr->VFPMRegValue(kSimd128Precision);
          char type = instr->Bit(6) != 0 ? 'u' : 's';
          int size = 2 * kBitsPerByte * (1 << instr->Bits(19, 18));
          out_buffer_pos_ +=
              SNPrintF(out_buffer_ + out_buffer_pos_, "vqmovn.%c%i d%d, q%d",
                       type, size, Vd, Vm);
2270
        } else {
2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344
          int Vd, Vm;
          if (instr->Bit(6) == 0) {
            Vd = instr->VFPDRegValue(kDoublePrecision);
            Vm = instr->VFPMRegValue(kDoublePrecision);
          } else {
            Vd = instr->VFPDRegValue(kSimd128Precision);
            Vm = instr->VFPMRegValue(kSimd128Precision);
          }
          if (instr->Bits(17, 16) == 0x2 && instr->Bits(11, 7) == 0) {
            if (instr->Bit(6) == 0) {
              out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
                                          "vswp d%d, d%d", Vd, Vm);
            } else {
              out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
                                          "vswp q%d, q%d", Vd, Vm);
            }
          } else if (instr->Bits(19, 16) == 0 && instr->Bits(11, 6) == 0x17) {
            out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
                                        "vmvn q%d, q%d", Vd, Vm);
          } else if (instr->Bits(19, 16) == 0xB && instr->Bits(11, 9) == 0x3 &&
                     instr->Bit(6) == 1) {
            const char* suffix = nullptr;
            int op = instr->Bits(8, 7);
            switch (op) {
              case 0:
                suffix = "f32.s32";
                break;
              case 1:
                suffix = "f32.u32";
                break;
              case 2:
                suffix = "s32.f32";
                break;
              case 3:
                suffix = "u32.f32";
                break;
            }
            out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
                                        "vcvt.%s q%d, q%d", suffix, Vd, Vm);
          } else if (instr->Bits(17, 16) == 0x2 && instr->Bits(11, 8) == 0x1) {
            int size = kBitsPerByte * (1 << instr->Bits(19, 18));
            const char* op = instr->Bit(7) != 0 ? "vzip" : "vuzp";
            if (instr->Bit(6) == 0) {
              // vzip/vuzp.<size> Dd, Dm.
              out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
                                          "%s.%d d%d, d%d", op, size, Vd, Vm);
            } else {
              // vzip/vuzp.<size> Qd, Qm.
              out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
                                          "%s.%d q%d, q%d", op, size, Vd, Vm);
            }
          } else if (instr->Bits(17, 16) == 0 && instr->Bits(11, 9) == 0 &&
                     instr->Bit(6) == 1) {
            int size = kBitsPerByte * (1 << instr->Bits(19, 18));
            int op = kBitsPerByte
                     << (static_cast<int>(Neon64) - instr->Bits(8, 7));
            // vrev<op>.<size> Qd, Qm.
            out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
                                        "vrev%d.%d q%d, q%d", op, size, Vd, Vm);
          } else if (instr->Bits(17, 16) == 0x2 && instr->Bits(11, 7) == 0x1) {
            int size = kBitsPerByte * (1 << instr->Bits(19, 18));
            if (instr->Bit(6) == 0) {
              // vtrn.<size> Dd, Dm.
              out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
                                          "vtrn.%d d%d, d%d", size, Vd, Vm);
            } else {
              // vtrn.<size> Qd, Qm.
              out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
                                          "vtrn.%d q%d, q%d", size, Vd, Vm);
            }
          } else if (instr->Bits(17, 16) == 0x1 && instr->Bit(11) == 0 &&
                     instr->Bit(6) == 1) {
            int size = kBitsPerByte * (1 << instr->Bits(19, 18));
            char type = instr->Bit(10) != 0 ? 'f' : 's';
2345
            if (instr->Bits(9, 6) == 0xD) {
2346 2347 2348 2349
              // vabs<type>.<size> Qd, Qm.
              out_buffer_pos_ +=
                  SNPrintF(out_buffer_ + out_buffer_pos_, "vabs.%c%d q%d, q%d",
                           type, size, Vd, Vm);
2350
            } else if (instr->Bits(9, 6) == 0xF) {
2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366
              // vneg<type>.<size> Qd, Qm.
              out_buffer_pos_ +=
                  SNPrintF(out_buffer_ + out_buffer_pos_, "vneg.%c%d q%d, q%d",
                           type, size, Vd, Vm);
            } else {
              Unknown(instr);
            }
          } else if (instr->Bits(19, 18) == 0x2 && instr->Bits(11, 8) == 0x5 &&
                     instr->Bit(6) == 1) {
            // vrecpe/vrsqrte.f32 Qd, Qm.
            const char* op = instr->Bit(7) == 0 ? "vrecpe" : "vrsqrte";
            out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
                                        "%s.f32 q%d, q%d", op, Vd, Vm);
          } else {
            Unknown(instr);
          }
2367
        }
2368 2369
      } else if (instr->Bits(11, 7) == 0 && instr->Bit(4) == 1 &&
                 instr->Bit(6) == 1) {
2370 2371 2372 2373 2374 2375 2376 2377
        // vshr.u<size> Qd, Qm, shift
        int size = base::bits::RoundDownToPowerOfTwo32(instr->Bits(21, 16));
        int shift = 2 * size - instr->Bits(21, 16);
        int Vd = instr->VFPDRegValue(kSimd128Precision);
        int Vm = instr->VFPMRegValue(kSimd128Precision);
        out_buffer_pos_ +=
            SNPrintF(out_buffer_ + out_buffer_pos_, "vshr.u%d q%d, q%d, #%d",
                     size, Vd, Vm, shift);
2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398
      } else if (instr->Bit(10) == 1 && instr->Bit(6) == 0 &&
                 instr->Bit(4) == 1) {
        // vsli.<size> Dd, Dm, shift
        // vsri.<size> Dd, Dm, shift
        int imm7 = instr->Bits(21, 16);
        if (instr->Bit(7) != 0) imm7 += 64;
        int size = base::bits::RoundDownToPowerOfTwo32(imm7);
        int shift;
        char direction;
        if (instr->Bit(8) == 1) {
          shift = imm7 - size;
          direction = 'l';  // vsli
        } else {
          shift = 2 * size - imm7;
          direction = 'r';  // vsri
        }
        int Vd = instr->VFPDRegValue(kDoublePrecision);
        int Vm = instr->VFPMRegValue(kDoublePrecision);
        out_buffer_pos_ +=
            SNPrintF(out_buffer_ + out_buffer_pos_, "vs%ci.%d d%d, d%d, #%d",
                     direction, size, Vd, Vm, shift);
2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411
      } else {
        Unknown(instr);
      }
      break;
    case 8:
      if (instr->Bits(21, 20) == 0) {
        // vst1
        int Vd = (instr->Bit(22) << 4) | instr->VdValue();
        int Rn = instr->VnValue();
        int type = instr->Bits(11, 8);
        int size = instr->Bits(7, 6);
        int align = instr->Bits(5, 4);
        int Rm = instr->VmValue();
2412 2413
        out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_, "vst1.%d ",
                                    (1 << size) << 3);
2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424
        FormatNeonList(Vd, type);
        Print(", ");
        FormatNeonMemory(Rn, align, Rm);
      } else if (instr->Bits(21, 20) == 2) {
        // vld1
        int Vd = (instr->Bit(22) << 4) | instr->VdValue();
        int Rn = instr->VnValue();
        int type = instr->Bits(11, 8);
        int size = instr->Bits(7, 6);
        int align = instr->Bits(5, 4);
        int Rm = instr->VmValue();
2425 2426
        out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_, "vld1.%d ",
                                    (1 << size) << 3);
2427 2428 2429 2430 2431 2432 2433 2434 2435
        FormatNeonList(Vd, type);
        Print(", ");
        FormatNeonMemory(Rn, align, Rm);
      } else {
        Unknown(instr);
      }
      break;
    case 0xA:
    case 0xB:
2436
      if ((instr->Bits(22, 20) == 5) && (instr->Bits(15, 12) == 0xF)) {
2437
        const char* rn_name = converter_.NameOfCPURegister(instr->Bits(19, 16));
2438 2439
        int offset = instr->Bits(11, 0);
        if (offset == 0) {
2440
          out_buffer_pos_ +=
2441
              SNPrintF(out_buffer_ + out_buffer_pos_, "pld [%s]", rn_name);
2442
        } else if (instr->Bit(23) == 0) {
2443
          out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
2444
                                      "pld [%s, #-%d]", rn_name, offset);
2445
        } else {
2446
          out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
2447
                                      "pld [%s, #+%d]", rn_name, offset);
2448
        }
2449 2450 2451 2452
      } else if (instr->SpecialValue() == 0xA && instr->Bits(22, 20) == 7) {
        int option = instr->Bits(3, 0);
        switch (instr->Bits(7, 4)) {
          case 4:
2453 2454
            out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_, "dsb %s",
                                        barrier_option_names[option]);
2455 2456
            break;
          case 5:
2457 2458
            out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_, "dmb %s",
                                        barrier_option_names[option]);
2459 2460
            break;
          case 6:
2461 2462
            out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_, "isb %s",
                                        barrier_option_names[option]);
2463 2464 2465 2466
            break;
          default:
            Unknown(instr);
        }
2467 2468 2469 2470
      } else {
        Unknown(instr);
      }
      break;
2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482
    case 0x1D:
      if (instr->Opc1Value() == 0x7 && instr->Bits(19, 18) == 0x2 &&
          instr->Bits(11, 9) == 0x5 && instr->Bits(7, 6) == 0x1 &&
          instr->Bit(4) == 0x0) {
        // VRINTA, VRINTN, VRINTP, VRINTM (floating-point)
        bool dp_operation = (instr->SzValue() == 1);
        int rounding_mode = instr->Bits(17, 16);
        switch (rounding_mode) {
          case 0x0:
            if (dp_operation) {
              Format(instr, "vrinta.f64.f64 'Dd, 'Dm");
            } else {
2483
              Format(instr, "vrinta.f32.f32 'Sd, 'Sm");
2484 2485 2486 2487 2488 2489
            }
            break;
          case 0x1:
            if (dp_operation) {
              Format(instr, "vrintn.f64.f64 'Dd, 'Dm");
            } else {
2490
              Format(instr, "vrintn.f32.f32 'Sd, 'Sm");
2491 2492 2493 2494 2495 2496
            }
            break;
          case 0x2:
            if (dp_operation) {
              Format(instr, "vrintp.f64.f64 'Dd, 'Dm");
            } else {
2497
              Format(instr, "vrintp.f32.f32 'Sd, 'Sm");
2498 2499 2500 2501 2502 2503
            }
            break;
          case 0x3:
            if (dp_operation) {
              Format(instr, "vrintm.f64.f64 'Dd, 'Dm");
            } else {
2504
              Format(instr, "vrintm.f32.f32 'Sd, 'Sm");
2505 2506 2507 2508 2509 2510
            }
            break;
          default:
            UNREACHABLE();  // Case analysis is exhaustive.
            break;
        }
2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526
      } else if ((instr->Opc1Value() == 0x4) && (instr->Bits(11, 9) == 0x5) &&
                 (instr->Bit(4) == 0x0)) {
        // VMAXNM, VMINNM (floating-point)
        if (instr->SzValue() == 0x1) {
          if (instr->Bit(6) == 0x1) {
            Format(instr, "vminnm.f64 'Dd, 'Dn, 'Dm");
          } else {
            Format(instr, "vmaxnm.f64 'Dd, 'Dn, 'Dm");
          }
        } else {
          if (instr->Bit(6) == 0x1) {
            Format(instr, "vminnm.f32 'Sd, 'Sn, 'Sm");
          } else {
            Format(instr, "vmaxnm.f32 'Sd, 'Sn, 'Sm");
          }
        }
2527 2528 2529 2530
      } else {
        Unknown(instr);
      }
      break;
2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572
    case 0x1C:
      if ((instr->Bits(11, 9) == 0x5) && (instr->Bit(6) == 0) &&
          (instr->Bit(4) == 0)) {
        // VSEL* (floating-point)
        bool dp_operation = (instr->SzValue() == 1);
        switch (instr->Bits(21, 20)) {
          case 0x0:
            if (dp_operation) {
              Format(instr, "vseleq.f64 'Dd, 'Dn, 'Dm");
            } else {
              Format(instr, "vseleq.f32 'Sd, 'Sn, 'Sm");
            }
            break;
          case 0x1:
            if (dp_operation) {
              Format(instr, "vselvs.f64 'Dd, 'Dn, 'Dm");
            } else {
              Format(instr, "vselvs.f32 'Sd, 'Sn, 'Sm");
            }
            break;
          case 0x2:
            if (dp_operation) {
              Format(instr, "vselge.f64 'Dd, 'Dn, 'Dm");
            } else {
              Format(instr, "vselge.f32 'Sd, 'Sn, 'Sm");
            }
            break;
          case 0x3:
            if (dp_operation) {
              Format(instr, "vselgt.f64 'Dd, 'Dn, 'Dm");
            } else {
              Format(instr, "vselgt.f32 'Sd, 'Sn, 'Sm");
            }
            break;
          default:
            UNREACHABLE();  // Case analysis is exhaustive.
            break;
        }
      } else {
        Unknown(instr);
      }
      break;
2573 2574 2575 2576 2577 2578
    default:
      Unknown(instr);
      break;
  }
}

2579
#undef VERIFIY
2580 2581 2582 2583 2584 2585 2586 2587 2588 2589

bool Decoder::IsConstantPoolAt(byte* instr_ptr) {
  int instruction_bits = *(reinterpret_cast<int*>(instr_ptr));
  return (instruction_bits & kConstantPoolMarkerMask) == kConstantPoolMarker;
}


int Decoder::ConstantPoolSizeAt(byte* instr_ptr) {
  if (IsConstantPoolAt(instr_ptr)) {
    int instruction_bits = *(reinterpret_cast<int*>(instr_ptr));
2590
    return DecodeConstantPoolLength(instruction_bits);
2591 2592
  } else {
    return -1;
2593 2594 2595 2596
  }
}


2597 2598
// Disassemble the instruction at *instr_ptr into the output buffer.
int Decoder::InstructionDecode(byte* instr_ptr) {
2599
  Instruction* instr = Instruction::At(reinterpret_cast<Address>(instr_ptr));
2600
  // Print raw instruction bytes.
2601 2602 2603
  out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
                              "%08x       ",
                              instr->InstructionBits());
2604
  if (instr->ConditionField() == kSpecialCondition) {
2605
    DecodeSpecialCondition(instr);
2606
    return kInstrSize;
2607 2608 2609
  }
  int instruction_bits = *(reinterpret_cast<int*>(instr_ptr));
  if ((instruction_bits & kConstantPoolMarkerMask) == kConstantPoolMarker) {
2610 2611 2612
    out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
                                "constant pool begin (length %d)",
                                DecodeConstantPoolLength(instruction_bits));
2613
    return kInstrSize;
2614
  }
2615
  switch (instr->TypeValue()) {
2616
    case 0:
2617
    case 1: {
2618
      DecodeType01(instr);
2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641
      break;
    }
    case 2: {
      DecodeType2(instr);
      break;
    }
    case 3: {
      DecodeType3(instr);
      break;
    }
    case 4: {
      DecodeType4(instr);
      break;
    }
    case 5: {
      DecodeType5(instr);
      break;
    }
    case 6: {
      DecodeType6(instr);
      break;
    }
    case 7: {
2642
      return DecodeType7(instr);
2643 2644 2645 2646 2647 2648
    }
    default: {
      // The type field is 3-bits in the ARM encoding.
      UNREACHABLE();
    }
  }
2649
  return kInstrSize;
2650 2651 2652
}


2653 2654
}  // namespace internal
}  // namespace v8
2655 2656 2657 2658 2659 2660


//------------------------------------------------------------------------------

namespace disasm {

2661

2662
const char* NameConverter::NameOfAddress(byte* addr) const {
2663
  v8::internal::SNPrintF(tmp_buffer_, "%p", static_cast<void*>(addr));
2664
  return tmp_buffer_.begin();
2665 2666 2667 2668 2669 2670 2671 2672 2673
}


const char* NameConverter::NameOfConstant(byte* addr) const {
  return NameOfAddress(addr);
}


const char* NameConverter::NameOfCPURegister(int reg) const {
2674
  return RegisterName(i::Register::from_code(reg));
2675 2676 2677
}


2678 2679 2680 2681 2682 2683
const char* NameConverter::NameOfByteCPURegister(int reg) const {
  UNREACHABLE();  // ARM does not have the concept of a byte register
  return "nobytereg";
}


2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698
const char* NameConverter::NameOfXMMRegister(int reg) const {
  UNREACHABLE();  // ARM does not have any XMM registers
  return "noxmmreg";
}


const char* NameConverter::NameInCode(byte* addr) const {
  // The default name converter is called for unknown code. So we will not try
  // to access any memory.
  return "";
}


//------------------------------------------------------------------------------

2699
int Disassembler::InstructionDecode(v8::internal::Vector<char> buffer,
2700
                                    byte* instruction) {
2701
  v8::internal::Decoder d(converter_, buffer);
2702 2703 2704 2705
  return d.InstructionDecode(instruction);
}


2706
int Disassembler::ConstantPoolSizeAt(byte* instruction) {
2707
  return v8::internal::Decoder::ConstantPoolSizeAt(instruction);
2708 2709
}

2710 2711
void Disassembler::Disassemble(FILE* f, byte* begin, byte* end,
                               UnimplementedOpcodeAction unimplemented_action) {
2712
  NameConverter converter;
2713
  Disassembler d(converter, unimplemented_action);
2714
  for (byte* pc = begin; pc < end;) {
2715
    v8::internal::EmbeddedVector<char, 128> buffer;
2716 2717
    buffer[0] = '\0';
    byte* prev_pc = pc;
2718
    pc += d.InstructionDecode(buffer, pc);
2719
    v8::internal::PrintF(f, "%p    %08x      %s\n", static_cast<void*>(prev_pc),
2720
                         *reinterpret_cast<int32_t*>(prev_pc), buffer.begin());
2721 2722 2723 2724
  }
}

}  // namespace disasm
2725 2726

#endif  // V8_TARGET_ARCH_ARM