disasm-ppc.cc 36.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
// Copyright 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// 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;) {
//     v8::internal::EmbeddedVector<char, 256> buffer;
//     byte* prev_pc = pc;
//     pc += d.InstructionDecode(buffer, pc);
//     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);

#include <assert.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>

#if V8_TARGET_ARCH_PPC

#include "src/base/platform/platform.h"
33
#include "src/codegen/macro-assembler.h"
34
#include "src/codegen/ppc/constants-ppc.h"
35
#include "src/codegen/register-configuration.h"
36
#include "src/diagnostics/disasm.h"
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78

namespace v8 {
namespace internal {

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

// 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, Vector<char> out_buffer)
      : 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);

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

  // Printing of common values.
  void PrintRegister(int reg);
  void PrintDRegister(int reg);
  int FormatFPRegister(Instruction* instr, const char* format);
  void PrintSoftwareInterrupt(SoftwareInterruptCodes svc);

  // Handle formatting of instructions and their options.
  int FormatRegister(Instruction* instr, const char* option);
  int FormatOption(Instruction* instr, const char* option);
  void Format(Instruction* instr, const char* format);
  void Unknown(Instruction* instr);
  void UnknownFormat(Instruction* instr, const char* opcname);

  void DecodeExt1(Instruction* instr);
  void DecodeExt2(Instruction* instr);
79
  void DecodeExt3(Instruction* instr);
80 81
  void DecodeExt4(Instruction* instr);
  void DecodeExt5(Instruction* instr);
82
  void DecodeExt6(Instruction* instr);
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113

  const disasm::NameConverter& converter_;
  Vector<char> out_buffer_;
  int out_buffer_pos_;

  DISALLOW_COPY_AND_ASSIGN(Decoder);
};

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

// 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++;
  while (cur != '\0' && (out_buffer_pos_ < (out_buffer_.length() - 1))) {
    PrintChar(cur);
    cur = *str++;
  }
  out_buffer_[out_buffer_pos_] = 0;
}

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

// Print the double FP register name according to the active name converter.
114
void Decoder::PrintDRegister(int reg) {
115
  Print(RegisterName(DoubleRegister::from_code(reg)));
116
}
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141

// Print SoftwareInterrupt codes. Factoring this out reduces the complexity of
// the FormatOption method.
void Decoder::PrintSoftwareInterrupt(SoftwareInterruptCodes svc) {
  switch (svc) {
    case kCallRtRedirected:
      Print("call rt redirected");
      return;
    case kBreakpoint:
      Print("breakpoint");
      return;
    default:
      if (svc >= kStopCode) {
        out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_, "%d - 0x%x",
                                    svc & kStopCodeMask, svc & kStopCodeMask);
      } else {
        out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_, "%d", svc);
      }
      return;
  }
}

// Handle all register based formatting in this function to reduce the
// complexity of FormatOption.
int Decoder::FormatRegister(Instruction* instr, const char* format) {
142
  DCHECK_EQ(format[0], 'r');
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163

  if ((format[1] == 't') || (format[1] == 's')) {  // 'rt & 'rs register
    int reg = instr->RTValue();
    PrintRegister(reg);
    return 2;
  } else if (format[1] == 'a') {  // 'ra: RA register
    int reg = instr->RAValue();
    PrintRegister(reg);
    return 2;
  } else if (format[1] == 'b') {  // 'rb: RB register
    int reg = instr->RBValue();
    PrintRegister(reg);
    return 2;
  }

  UNREACHABLE();
}

// Handle all FP register based formatting in this function to reduce the
// complexity of FormatOption.
int Decoder::FormatFPRegister(Instruction* instr, const char* format) {
164
  DCHECK_EQ(format[0], 'D');
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235

  int retval = 2;
  int reg = -1;
  if (format[1] == 't') {
    reg = instr->RTValue();
  } else if (format[1] == 'a') {
    reg = instr->RAValue();
  } else if (format[1] == 'b') {
    reg = instr->RBValue();
  } else if (format[1] == 'c') {
    reg = instr->RCValue();
  } else {
    UNREACHABLE();
  }

  PrintDRegister(reg);

  return retval;
}

// 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.
int Decoder::FormatOption(Instruction* instr, const char* format) {
  switch (format[0]) {
    case 'o': {
      if (instr->Bit(10) == 1) {
        Print("o");
      }
      return 1;
    }
    case '.': {
      if (instr->Bit(0) == 1) {
        Print(".");
      } else {
        Print(" ");  // ensure consistent spacing
      }
      return 1;
    }
    case 'r': {
      return FormatRegister(instr, format);
    }
    case 'D': {
      return FormatFPRegister(instr, format);
    }
    case 'i': {  // int16
      int32_t value = (instr->Bits(15, 0) << 16) >> 16;
      out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_, "%d", value);
      return 5;
    }
    case 'u': {  // uint16
      int32_t value = instr->Bits(15, 0);
      out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_, "%d", value);
      return 6;
    }
    case 'l': {
      // Link (LK) Bit 0
      if (instr->Bit(0) == 1) {
        Print("l");
      }
      return 1;
    }
    case 'a': {
      // Absolute Address Bit 1
      if (instr->Bit(1) == 1) {
        Print("a");
      }
      return 1;
    }
236 237 238 239 240 241 242 243
    case 'c': {  // 'cr: condition register of branch instruction
      int code = instr->Bits(20, 18);
      if (code != 7) {
        out_buffer_pos_ +=
            SNPrintF(out_buffer_ + out_buffer_pos_, " cr%d", code);
      }
      return 2;
    }
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
    case 't': {  // 'target: target of branch instructions
      // target26 or target16
      DCHECK(STRING_STARTS_WITH(format, "target"));
      if ((format[6] == '2') && (format[7] == '6')) {
        int off = ((instr->Bits(25, 2)) << 8) >> 6;
        out_buffer_pos_ += SNPrintF(
            out_buffer_ + out_buffer_pos_, "%+d -> %s", off,
            converter_.NameOfAddress(reinterpret_cast<byte*>(instr) + off));
        return 8;
      } else if ((format[6] == '1') && (format[7] == '6')) {
        int off = ((instr->Bits(15, 2)) << 18) >> 16;
        out_buffer_pos_ += SNPrintF(
            out_buffer_ + out_buffer_pos_, "%+d -> %s", off,
            converter_.NameOfAddress(reinterpret_cast<byte*>(instr) + off));
        return 8;
      }
260
      break;
261
      case 's': {
262
        DCHECK_EQ(format[1], 'h');
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
        int32_t value = 0;
        int32_t opcode = instr->OpcodeValue() << 26;
        int32_t sh = instr->Bits(15, 11);
        if (opcode == EXT5 ||
            (opcode == EXT2 && instr->Bits(10, 2) << 2 == SRADIX)) {
          // SH Bits 1 and 15-11 (split field)
          value = (sh | (instr->Bit(1) << 5));
        } else {
          // SH Bits 15-11
          value = (sh << 26) >> 26;
        }
        out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_, "%d", value);
        return 2;
      }
      case 'm': {
        int32_t value = 0;
        if (format[1] == 'e') {
          if (instr->OpcodeValue() << 26 != EXT5) {
            // ME Bits 10-6
            value = (instr->Bits(10, 6) << 26) >> 26;
          } else {
            // ME Bits 5 and 10-6 (split field)
            value = (instr->Bits(10, 6) | (instr->Bit(5) << 5));
          }
        } else if (format[1] == 'b') {
          if (instr->OpcodeValue() << 26 != EXT5) {
            // MB Bits 5-1
            value = (instr->Bits(5, 1) << 26) >> 26;
          } else {
            // MB Bits 5 and 10-6 (split field)
            value = (instr->Bits(10, 6) | (instr->Bit(5) << 5));
          }
        } else {
          UNREACHABLE();  // bad format
        }
        out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_, "%d", value);
        return 2;
      }
    }
#if V8_TARGET_ARCH_PPC64
    case 'd': {  // ds value for offset
      int32_t value = SIGN_EXT_IMM16(instr->Bits(15, 0) & ~3);
      out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_, "%d", value);
      return 1;
    }
#endif
    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.
void Decoder::Format(Instruction* instr, const char* format) {
  char cur = *format++;
  while ((cur != 0) && (out_buffer_pos_ < (out_buffer_.length() - 1))) {
    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';
}

// The disassembler may end up decoding data inlined in the code. We do not want
334
// it to crash if the data does not resemble any known instruction.
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
#define VERIFY(condition) \
  if (!(condition)) {     \
    Unknown(instr);       \
    return;               \
  }

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

// For currently unimplemented decodings the disassembler calls
// UnknownFormat(instr) which will just print opcode name of the
// instruction bits.
void Decoder::UnknownFormat(Instruction* instr, const char* name) {
  char buffer[100];
  snprintf(buffer, sizeof(buffer), "%s (unknown-format)", name);
  Format(instr, buffer);
}

void Decoder::DecodeExt1(Instruction* instr) {
sampsong's avatar
sampsong committed
355
  switch (EXT1 | (instr->BitField(10, 1))) {
356 357 358 359 360
    case MCRF: {
      UnknownFormat(instr, "mcrf");  // not used by V8
      break;
    }
    case BCLRX: {
sampsong's avatar
sampsong committed
361
      int bo = instr->BitField(25, 21);
362 363 364
      int bi = instr->Bits(20, 16);
      CRBit cond = static_cast<CRBit>(bi & (CRWIDTH - 1));
      switch (bo) {
365 366 367 368 369 370 371 372 373
        case DCBNZF: {
          UnknownFormat(instr, "bclrx-dcbnzf");
          break;
        }
        case DCBEZF: {
          UnknownFormat(instr, "bclrx-dcbezf");
          break;
        }
        case BF: {
374 375 376 377 378 379 380 381 382 383 384 385 386 387
          switch (cond) {
            case CR_EQ:
              Format(instr, "bnelr'l'cr");
              break;
            case CR_GT:
              Format(instr, "blelr'l'cr");
              break;
            case CR_LT:
              Format(instr, "bgelr'l'cr");
              break;
            case CR_SO:
              Format(instr, "bnsolr'l'cr");
              break;
          }
388 389 390 391 392 393 394 395 396 397 398
          break;
        }
        case DCBNZT: {
          UnknownFormat(instr, "bclrx-dcbbzt");
          break;
        }
        case DCBEZT: {
          UnknownFormat(instr, "bclrx-dcbnezt");
          break;
        }
        case BT: {
399 400 401 402 403 404 405 406 407 408 409 410 411 412
          switch (cond) {
            case CR_EQ:
              Format(instr, "beqlr'l'cr");
              break;
            case CR_GT:
              Format(instr, "bgtlr'l'cr");
              break;
            case CR_LT:
              Format(instr, "bltlr'l'cr");
              break;
            case CR_SO:
              Format(instr, "bsolr'l'cr");
              break;
          }
413 414 415 416 417 418 419 420 421 422 423
          break;
        }
        case DCBNZ: {
          UnknownFormat(instr, "bclrx-dcbnz");
          break;
        }
        case DCBEZ: {
          UnknownFormat(instr, "bclrx-dcbez");  // not used by V8
          break;
        }
        case BA: {
424
          Format(instr, "blr'l");
425 426 427 428 429 430
          break;
        }
      }
      break;
    }
    case BCCTRX: {
sampsong's avatar
sampsong committed
431
      switch (instr->BitField(25, 21)) {
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
        case DCBNZF: {
          UnknownFormat(instr, "bcctrx-dcbnzf");
          break;
        }
        case DCBEZF: {
          UnknownFormat(instr, "bcctrx-dcbezf");
          break;
        }
        case BF: {
          UnknownFormat(instr, "bcctrx-bf");
          break;
        }
        case DCBNZT: {
          UnknownFormat(instr, "bcctrx-dcbnzt");
          break;
        }
        case DCBEZT: {
          UnknownFormat(instr, "bcctrx-dcbezf");
          break;
        }
        case BT: {
          UnknownFormat(instr, "bcctrx-bt");
          break;
        }
        case DCBNZ: {
          UnknownFormat(instr, "bcctrx-dcbnz");
          break;
        }
        case DCBEZ: {
          UnknownFormat(instr, "bcctrx-dcbez");
          break;
        }
        case BA: {
          if (instr->Bit(0) == 1) {
            Format(instr, "bctrl");
          } else {
            Format(instr, "bctr");
          }
          break;
        }
472 473 474
        default: {
          UNREACHABLE();
        }
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
      }
      break;
    }
    case CRNOR: {
      Format(instr, "crnor (stuff)");
      break;
    }
    case RFI: {
      Format(instr, "rfi (stuff)");
      break;
    }
    case CRANDC: {
      Format(instr, "crandc (stuff)");
      break;
    }
    case ISYNC: {
      Format(instr, "isync (stuff)");
      break;
    }
    case CRXOR: {
      Format(instr, "crxor (stuff)");
      break;
    }
    case CRNAND: {
      UnknownFormat(instr, "crnand");
      break;
    }
    case CRAND: {
      UnknownFormat(instr, "crand");
      break;
    }
    case CREQV: {
      UnknownFormat(instr, "creqv");
      break;
    }
    case CRORC: {
      UnknownFormat(instr, "crorc");
      break;
    }
    case CROR: {
      UnknownFormat(instr, "cror");
      break;
    }
    default: {
      Unknown(instr);  // not used by V8
    }
  }
}

void Decoder::DecodeExt2(Instruction* instr) {
  // Some encodings are 10-1 bits, handle those first
sampsong's avatar
sampsong committed
526
  switch (EXT2 | (instr->BitField(10, 1))) {
527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545
    case SRWX: {
      Format(instr, "srw'.    'ra, 'rs, 'rb");
      return;
    }
#if V8_TARGET_ARCH_PPC64
    case SRDX: {
      Format(instr, "srd'.    'ra, 'rs, 'rb");
      return;
    }
#endif
    case SRAW: {
      Format(instr, "sraw'.   'ra, 'rs, 'rb");
      return;
    }
#if V8_TARGET_ARCH_PPC64
    case SRAD: {
      Format(instr, "srad'.   'ra, 'rs, 'rb");
      return;
    }
546
#endif
547 548 549 550
    case SYNC: {
      Format(instr, "sync");
      return;
    }
551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567
    case MODSW: {
      Format(instr, "modsw  'rt, 'ra, 'rb");
      return;
    }
    case MODUW: {
      Format(instr, "moduw  'rt, 'ra, 'rb");
      return;
    }
#if V8_TARGET_ARCH_PPC64
    case MODSD: {
      Format(instr, "modsd  'rt, 'ra, 'rb");
      return;
    }
    case MODUD: {
      Format(instr, "modud  'rt, 'ra, 'rb");
      return;
    }
568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587
#endif
    case SRAWIX: {
      Format(instr, "srawi'.  'ra,'rs,'sh");
      return;
    }
    case EXTSH: {
      Format(instr, "extsh'.  'ra, 'rs");
      return;
    }
#if V8_TARGET_ARCH_PPC64
    case EXTSW: {
      Format(instr, "extsw'.  'ra, 'rs");
      return;
    }
#endif
    case EXTSB: {
      Format(instr, "extsb'.  'ra, 'rs");
      return;
    }
    case LFSX: {
588
      Format(instr, "lfsx    'Dt, 'ra, 'rb");
589 590 591
      return;
    }
    case LFSUX: {
592
      Format(instr, "lfsux   'Dt, 'ra, 'rb");
593 594 595
      return;
    }
    case LFDX: {
596
      Format(instr, "lfdx    'Dt, 'ra, 'rb");
597 598 599
      return;
    }
    case LFDUX: {
600
      Format(instr, "lfdux   'Dt, 'ra, 'rb");
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
      return;
    }
    case STFSX: {
      Format(instr, "stfsx    'rs, 'ra, 'rb");
      return;
    }
    case STFSUX: {
      Format(instr, "stfsux   'rs, 'ra, 'rb");
      return;
    }
    case STFDX: {
      Format(instr, "stfdx    'rs, 'ra, 'rb");
      return;
    }
    case STFDUX: {
      Format(instr, "stfdux   'rs, 'ra, 'rb");
      return;
    }
619 620 621 622
    case POPCNTW: {
      Format(instr, "popcntw  'ra, 'rs");
      return;
    }
623 624 625 626 627 628
#if V8_TARGET_ARCH_PPC64
    case POPCNTD: {
      Format(instr, "popcntd  'ra, 'rs");
      return;
    }
#endif
629 630
  }

sampsong's avatar
sampsong committed
631
  switch (EXT2 | (instr->BitField(10, 2))) {
632 633 634 635 636 637
    case SRADIX: {
      Format(instr, "sradi'.  'ra,'rs,'sh");
      return;
    }
  }

638 639 640 641 642 643 644 645 646 647 648 649 650
  switch (EXT2 | (instr->BitField(10, 0))) {
    case STBCX: {
      Format(instr, "stbcx   'rs, 'ra, 'rb");
      return;
    }
    case STHCX: {
      Format(instr, "sthcx   'rs, 'ra, 'rb");
      return;
    }
    case STWCX: {
      Format(instr, "stwcx   'rs, 'ra, 'rb");
      return;
    }
651 652 653 654
    case STDCX: {
      Format(instr, "stdcx   'rs, 'ra, 'rb");
      return;
    }
655 656
  }

657
  // ?? are all of these xo_form?
sampsong's avatar
sampsong committed
658
  switch (EXT2 | (instr->BitField(9, 1))) {
659 660 661 662 663 664 665 666 667 668
    case CMP: {
#if V8_TARGET_ARCH_PPC64
      if (instr->Bit(21)) {
#endif
        Format(instr, "cmp     'ra, 'rb");
#if V8_TARGET_ARCH_PPC64
      } else {
        Format(instr, "cmpw    'ra, 'rb");
      }
#endif
669
      return;
670 671 672
    }
    case SLWX: {
      Format(instr, "slw'.   'ra, 'rs, 'rb");
673
      return;
674 675 676 677
    }
#if V8_TARGET_ARCH_PPC64
    case SLDX: {
      Format(instr, "sld'.   'ra, 'rs, 'rb");
678
      return;
679 680 681 682
    }
#endif
    case SUBFCX: {
      Format(instr, "subfc'. 'rt, 'ra, 'rb");
683
      return;
684
    }
685 686 687 688
    case SUBFEX: {
      Format(instr, "subfe'. 'rt, 'ra, 'rb");
      return;
    }
689 690
    case ADDCX: {
      Format(instr, "addc'.   'rt, 'ra, 'rb");
691
      return;
692
    }
693 694 695 696
    case ADDEX: {
      Format(instr, "adde'.   'rt, 'ra, 'rb");
      return;
    }
697 698
    case CNTLZWX: {
      Format(instr, "cntlzw'. 'ra, 'rs");
699
      return;
700 701 702 703
    }
#if V8_TARGET_ARCH_PPC64
    case CNTLZDX: {
      Format(instr, "cntlzd'. 'ra, 'rs");
704
      return;
705 706 707 708
    }
#endif
    case ANDX: {
      Format(instr, "and'.    'ra, 'rs, 'rb");
709
      return;
710 711 712
    }
    case ANDCX: {
      Format(instr, "andc'.   'ra, 'rs, 'rb");
713
      return;
714 715 716 717 718 719 720 721 722 723 724
    }
    case CMPL: {
#if V8_TARGET_ARCH_PPC64
      if (instr->Bit(21)) {
#endif
        Format(instr, "cmpl    'ra, 'rb");
#if V8_TARGET_ARCH_PPC64
      } else {
        Format(instr, "cmplw   'ra, 'rb");
      }
#endif
725
      return;
726 727 728
    }
    case NEGX: {
      Format(instr, "neg'.    'rt, 'ra");
729
      return;
730 731 732
    }
    case NORX: {
      Format(instr, "nor'.    'rt, 'ra, 'rb");
733
      return;
734 735 736
    }
    case SUBFX: {
      Format(instr, "subf'.   'rt, 'ra, 'rb");
737
      return;
738 739 740
    }
    case MULHWX: {
      Format(instr, "mulhw'o'.  'rt, 'ra, 'rb");
741
      return;
742 743 744
    }
    case ADDZEX: {
      Format(instr, "addze'.   'rt, 'ra");
745
      return;
746 747 748
    }
    case MULLW: {
      Format(instr, "mullw'o'.  'rt, 'ra, 'rb");
749
      return;
750 751 752 753
    }
#if V8_TARGET_ARCH_PPC64
    case MULLD: {
      Format(instr, "mulld'o'.  'rt, 'ra, 'rb");
754
      return;
755 756 757 758
    }
#endif
    case DIVW: {
      Format(instr, "divw'o'.   'rt, 'ra, 'rb");
759 760 761 762 763
      return;
    }
    case DIVWU: {
      Format(instr, "divwu'o'.  'rt, 'ra, 'rb");
      return;
764 765 766 767
    }
#if V8_TARGET_ARCH_PPC64
    case DIVD: {
      Format(instr, "divd'o'.   'rt, 'ra, 'rb");
768
      return;
769 770 771 772
    }
#endif
    case ADDX: {
      Format(instr, "add'o     'rt, 'ra, 'rb");
773
      return;
774 775 776
    }
    case XORX: {
      Format(instr, "xor'.    'ra, 'rs, 'rb");
777
      return;
778 779 780 781 782 783 784
    }
    case ORX: {
      if (instr->RTValue() == instr->RBValue()) {
        Format(instr, "mr      'ra, 'rb");
      } else {
        Format(instr, "or      'ra, 'rs, 'rb");
      }
785
      return;
786 787 788 789 790 791 792 793
    }
    case MFSPR: {
      int spr = instr->Bits(20, 11);
      if (256 == spr) {
        Format(instr, "mflr    'rt");
      } else {
        Format(instr, "mfspr   'rt ??");
      }
794
      return;
795 796 797 798 799 800 801 802 803 804
    }
    case MTSPR: {
      int spr = instr->Bits(20, 11);
      if (256 == spr) {
        Format(instr, "mtlr    'rt");
      } else if (288 == spr) {
        Format(instr, "mtctr   'rt");
      } else {
        Format(instr, "mtspr   'rt ??");
      }
805
      return;
806 807 808
    }
    case MFCR: {
      Format(instr, "mfcr    'rt");
809
      return;
810 811 812
    }
    case STWX: {
      Format(instr, "stwx    'rs, 'ra, 'rb");
813
      return;
814 815 816
    }
    case STWUX: {
      Format(instr, "stwux   'rs, 'ra, 'rb");
817
      return;
818 819 820
    }
    case STBX: {
      Format(instr, "stbx    'rs, 'ra, 'rb");
821
      return;
822 823 824
    }
    case STBUX: {
      Format(instr, "stbux   'rs, 'ra, 'rb");
825
      return;
826 827 828
    }
    case STHX: {
      Format(instr, "sthx    'rs, 'ra, 'rb");
829
      return;
830 831 832
    }
    case STHUX: {
      Format(instr, "sthux   'rs, 'ra, 'rb");
833
      return;
834 835 836
    }
    case LWZX: {
      Format(instr, "lwzx    'rt, 'ra, 'rb");
837
      return;
838 839 840
    }
    case LWZUX: {
      Format(instr, "lwzux   'rt, 'ra, 'rb");
841 842 843 844 845
      return;
    }
    case LWAX: {
      Format(instr, "lwax    'rt, 'ra, 'rb");
      return;
846 847 848
    }
    case LBZX: {
      Format(instr, "lbzx    'rt, 'ra, 'rb");
849
      return;
850 851 852
    }
    case LBZUX: {
      Format(instr, "lbzux   'rt, 'ra, 'rb");
853
      return;
854 855 856
    }
    case LHZX: {
      Format(instr, "lhzx    'rt, 'ra, 'rb");
857
      return;
858 859 860
    }
    case LHZUX: {
      Format(instr, "lhzux   'rt, 'ra, 'rb");
861 862 863 864 865
      return;
    }
    case LHAX: {
      Format(instr, "lhax    'rt, 'ra, 'rb");
      return;
866
    }
867 868 869 870 871 872 873 874 875 876 877 878
    case LBARX: {
      Format(instr, "lbarx   'rt, 'ra, 'rb");
      return;
    }
    case LHARX: {
      Format(instr, "lharx   'rt, 'ra, 'rb");
      return;
    }
    case LWARX: {
      Format(instr, "lwarx   'rt, 'ra, 'rb");
      return;
    }
879 880 881
#if V8_TARGET_ARCH_PPC64
    case LDX: {
      Format(instr, "ldx     'rt, 'ra, 'rb");
882
      return;
883 884 885
    }
    case LDUX: {
      Format(instr, "ldux    'rt, 'ra, 'rb");
886
      return;
887
    }
888 889 890 891
    case LDARX: {
      Format(instr, "ldarx   'rt, 'ra, 'rb");
      return;
    }
892 893
    case STDX: {
      Format(instr, "stdx    'rt, 'ra, 'rb");
894
      return;
895 896 897
    }
    case STDUX: {
      Format(instr, "stdux   'rt, 'ra, 'rb");
898
      return;
899 900 901
    }
    case MFVSRD: {
      Format(instr, "mffprd  'ra, 'Dt");
902
      return;
903 904 905
    }
    case MFVSRWZ: {
      Format(instr, "mffprwz 'ra, 'Dt");
906
      return;
907 908 909
    }
    case MTVSRD: {
      Format(instr, "mtfprd  'Dt, 'ra");
910
      return;
911 912 913
    }
    case MTVSRWA: {
      Format(instr, "mtfprwa 'Dt, 'ra");
914
      return;
915 916 917
    }
    case MTVSRWZ: {
      Format(instr, "mtfprwz 'Dt, 'ra");
918
      return;
919 920
    }
#endif
921 922
  }

sampsong's avatar
sampsong committed
923
  switch (EXT2 | (instr->BitField(5, 1))) {
924 925 926 927
    case ISEL: {
      Format(instr, "isel    'rt, 'ra, 'rb");
      return;
    }
928 929 930 931 932 933
    default: {
      Unknown(instr);  // not used by V8
    }
  }
}

934
void Decoder::DecodeExt3(Instruction* instr) {
sampsong's avatar
sampsong committed
935
  switch (EXT3 | (instr->BitField(10, 1))) {
936 937 938 939
    case FCFID: {
      Format(instr, "fcfids'. 'Dt, 'Db");
      break;
    }
940 941 942 943
    case FCFIDU: {
      Format(instr, "fcfidus'.'Dt, 'Db");
      break;
    }
944 945 946 947 948 949
    default: {
      Unknown(instr);  // not used by V8
    }
  }
}

950
void Decoder::DecodeExt4(Instruction* instr) {
sampsong's avatar
sampsong committed
951
  switch (EXT4 | (instr->BitField(5, 1))) {
952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985
    case FDIV: {
      Format(instr, "fdiv'.   'Dt, 'Da, 'Db");
      return;
    }
    case FSUB: {
      Format(instr, "fsub'.   'Dt, 'Da, 'Db");
      return;
    }
    case FADD: {
      Format(instr, "fadd'.   'Dt, 'Da, 'Db");
      return;
    }
    case FSQRT: {
      Format(instr, "fsqrt'.  'Dt, 'Db");
      return;
    }
    case FSEL: {
      Format(instr, "fsel'.   'Dt, 'Da, 'Dc, 'Db");
      return;
    }
    case FMUL: {
      Format(instr, "fmul'.   'Dt, 'Da, 'Dc");
      return;
    }
    case FMSUB: {
      Format(instr, "fmsub'.  'Dt, 'Da, 'Dc, 'Db");
      return;
    }
    case FMADD: {
      Format(instr, "fmadd'.  'Dt, 'Da, 'Dc, 'Db");
      return;
    }
  }

sampsong's avatar
sampsong committed
986
  switch (EXT4 | (instr->BitField(10, 1))) {
987 988 989 990 991 992 993 994 995 996 997 998
    case FCMPU: {
      Format(instr, "fcmpu   'Da, 'Db");
      break;
    }
    case FRSP: {
      Format(instr, "frsp'.   'Dt, 'Db");
      break;
    }
    case FCFID: {
      Format(instr, "fcfid'.  'Dt, 'Db");
      break;
    }
999 1000 1001 1002
    case FCFIDU: {
      Format(instr, "fcfidu'. 'Dt, 'Db");
      break;
    }
1003 1004 1005 1006 1007 1008 1009 1010
    case FCTID: {
      Format(instr, "fctid   'Dt, 'Db");
      break;
    }
    case FCTIDZ: {
      Format(instr, "fctidz  'Dt, 'Db");
      break;
    }
1011 1012 1013 1014 1015 1016 1017 1018
    case FCTIDU: {
      Format(instr, "fctidu  'Dt, 'Db");
      break;
    }
    case FCTIDUZ: {
      Format(instr, "fctiduz 'Dt, 'Db");
      break;
    }
1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046
    case FCTIW: {
      Format(instr, "fctiw'. 'Dt, 'Db");
      break;
    }
    case FCTIWZ: {
      Format(instr, "fctiwz'. 'Dt, 'Db");
      break;
    }
    case FMR: {
      Format(instr, "fmr'.    'Dt, 'Db");
      break;
    }
    case MTFSFI: {
      Format(instr, "mtfsfi'.  ?,?");
      break;
    }
    case MFFS: {
      Format(instr, "mffs'.   'Dt");
      break;
    }
    case MTFSF: {
      Format(instr, "mtfsf'.  'Db ?,?,?");
      break;
    }
    case FABS: {
      Format(instr, "fabs'.   'Dt, 'Db");
      break;
    }
1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058
    case FRIN: {
      Format(instr, "frin.   'Dt, 'Db");
      break;
    }
    case FRIZ: {
      Format(instr, "friz.   'Dt, 'Db");
      break;
    }
    case FRIP: {
      Format(instr, "frip.   'Dt, 'Db");
      break;
    }
1059
    case FRIM: {
1060
      Format(instr, "frim.   'Dt, 'Db");
1061 1062 1063 1064 1065 1066
      break;
    }
    case FNEG: {
      Format(instr, "fneg'.   'Dt, 'Db");
      break;
    }
1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078
    case MCRFS: {
      Format(instr, "mcrfs   ?,?");
      break;
    }
    case MTFSB0: {
      Format(instr, "mtfsb0'. ?");
      break;
    }
    case MTFSB1: {
      Format(instr, "mtfsb1'. ?");
      break;
    }
1079 1080 1081 1082 1083 1084 1085
    default: {
      Unknown(instr);  // not used by V8
    }
  }
}

void Decoder::DecodeExt5(Instruction* instr) {
sampsong's avatar
sampsong committed
1086
  switch (EXT5 | (instr->BitField(4, 2))) {
1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103
    case RLDICL: {
      Format(instr, "rldicl'. 'ra, 'rs, 'sh, 'mb");
      return;
    }
    case RLDICR: {
      Format(instr, "rldicr'. 'ra, 'rs, 'sh, 'me");
      return;
    }
    case RLDIC: {
      Format(instr, "rldic'.  'ra, 'rs, 'sh, 'mb");
      return;
    }
    case RLDIMI: {
      Format(instr, "rldimi'. 'ra, 'rs, 'sh, 'mb");
      return;
    }
  }
sampsong's avatar
sampsong committed
1104
  switch (EXT5 | (instr->BitField(4, 1))) {
1105 1106 1107 1108 1109 1110 1111 1112
    case RLDCL: {
      Format(instr, "rldcl'.  'ra, 'rs, 'sb, 'mb");
      return;
    }
  }
  Unknown(instr);  // not used by V8
}

1113
void Decoder::DecodeExt6(Instruction* instr) {
sampsong's avatar
sampsong committed
1114
  switch (EXT6 | (instr->BitField(10, 3))) {
1115 1116
#define DECODE_XX3_INSTRUCTIONS(name, opcode_name, opcode_value) \
  case opcode_name: {                                            \
1117
    Format(instr, #name " 'Dt, 'Da, 'Db");                       \
1118 1119
    return;                                                      \
  }
sampsong's avatar
sampsong committed
1120
    PPC_XX3_OPCODE_LIST(DECODE_XX3_INSTRUCTIONS)
1121 1122
#undef DECODE_XX3_INSTRUCTIONS
  }
sampsong's avatar
sampsong committed
1123
  switch (EXT6 | (instr->BitField(10, 2))) {
1124 1125
#define DECODE_XX2_INSTRUCTIONS(name, opcode_name, opcode_value) \
  case opcode_name: {                                            \
1126
    Format(instr, #name " 'Dt, 'Db");                            \
1127 1128
    return;                                                      \
  }
sampsong's avatar
sampsong committed
1129
    PPC_XX2_OPCODE_LIST(DECODE_XX2_INSTRUCTIONS)
1130
  }
1131
#undef DECODE_XX2_INSTRUCTIONS
1132 1133 1134
  Unknown(instr);  // not used by V8
}

1135
#undef VERIFY
1136 1137 1138 1139 1140 1141 1142 1143

// Disassemble the instruction at *instr_ptr into the output buffer.
int Decoder::InstructionDecode(byte* instr_ptr) {
  Instruction* instr = Instruction::At(instr_ptr);
  // Print raw instruction bytes.
  out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_, "%08x       ",
                              instr->InstructionBits());

1144 1145 1146
  if (ABI_USES_FUNCTION_DESCRIPTORS && instr->InstructionBits() == 0) {
    // The first field will be identified as a jump table entry.  We
    // emit the rest of the structure as zero, so just skip past them.
1147
    Format(instr, "constant");
1148
    return kInstrSize;
1149 1150
  }

sampsong's avatar
sampsong committed
1151 1152
  uint32_t opcode = instr->OpcodeValue() << 26;
  switch (opcode) {
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 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216
    case TWI: {
      PrintSoftwareInterrupt(instr->SvcValue());
      break;
    }
    case MULLI: {
      UnknownFormat(instr, "mulli");
      break;
    }
    case SUBFIC: {
      Format(instr, "subfic  'rt, 'ra, 'int16");
      break;
    }
    case CMPLI: {
#if V8_TARGET_ARCH_PPC64
      if (instr->Bit(21)) {
#endif
        Format(instr, "cmpli   'ra, 'uint16");
#if V8_TARGET_ARCH_PPC64
      } else {
        Format(instr, "cmplwi  'ra, 'uint16");
      }
#endif
      break;
    }
    case CMPI: {
#if V8_TARGET_ARCH_PPC64
      if (instr->Bit(21)) {
#endif
        Format(instr, "cmpi    'ra, 'int16");
#if V8_TARGET_ARCH_PPC64
      } else {
        Format(instr, "cmpwi   'ra, 'int16");
      }
#endif
      break;
    }
    case ADDIC: {
      Format(instr, "addic   'rt, 'ra, 'int16");
      break;
    }
    case ADDICx: {
      UnknownFormat(instr, "addicx");
      break;
    }
    case ADDI: {
      if (instr->RAValue() == 0) {
        // this is load immediate
        Format(instr, "li      'rt, 'int16");
      } else {
        Format(instr, "addi    'rt, 'ra, 'int16");
      }
      break;
    }
    case ADDIS: {
      if (instr->RAValue() == 0) {
        Format(instr, "lis     'rt, 'int16");
      } else {
        Format(instr, "addis   'rt, 'ra, 'int16");
      }
      break;
    }
    case BCX: {
      int bo = instr->Bits(25, 21) << 21;
      int bi = instr->Bits(20, 16);
1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232
      CRBit cond = static_cast<CRBit>(bi & (CRWIDTH - 1));
      switch (bo) {
        case BT: {  // Branch if condition true
          switch (cond) {
            case CR_EQ:
              Format(instr, "beq'l'a'cr 'target16");
              break;
            case CR_GT:
              Format(instr, "bgt'l'a'cr 'target16");
              break;
            case CR_LT:
              Format(instr, "blt'l'a'cr 'target16");
              break;
            case CR_SO:
              Format(instr, "bso'l'a'cr 'target16");
              break;
1233 1234
          }
          break;
1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249
        }
        case BF: {  // Branch if condition false
          switch (cond) {
            case CR_EQ:
              Format(instr, "bne'l'a'cr 'target16");
              break;
            case CR_GT:
              Format(instr, "ble'l'a'cr 'target16");
              break;
            case CR_LT:
              Format(instr, "bge'l'a'cr 'target16");
              break;
            case CR_SO:
              Format(instr, "bnso'l'a'cr 'target16");
              break;
1250 1251
          }
          break;
1252 1253 1254
        }
        case DCBNZ: {  // Decrement CTR; branch if CTR != 0
          Format(instr, "bdnz'l'a 'target16");
1255
          break;
1256
        }
1257
        default:
1258
          Format(instr, "bc'l'a'cr 'target16");
1259 1260 1261 1262 1263 1264 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 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410
          break;
      }
      break;
    }
    case SC: {
      UnknownFormat(instr, "sc");
      break;
    }
    case BX: {
      Format(instr, "b'l'a 'target26");
      break;
    }
    case EXT1: {
      DecodeExt1(instr);
      break;
    }
    case RLWIMIX: {
      Format(instr, "rlwimi'. 'ra, 'rs, 'sh, 'me, 'mb");
      break;
    }
    case RLWINMX: {
      Format(instr, "rlwinm'. 'ra, 'rs, 'sh, 'me, 'mb");
      break;
    }
    case RLWNMX: {
      Format(instr, "rlwnm'.  'ra, 'rs, 'rb, 'me, 'mb");
      break;
    }
    case ORI: {
      Format(instr, "ori     'ra, 'rs, 'uint16");
      break;
    }
    case ORIS: {
      Format(instr, "oris    'ra, 'rs, 'uint16");
      break;
    }
    case XORI: {
      Format(instr, "xori    'ra, 'rs, 'uint16");
      break;
    }
    case XORIS: {
      Format(instr, "xoris   'ra, 'rs, 'uint16");
      break;
    }
    case ANDIx: {
      Format(instr, "andi.   'ra, 'rs, 'uint16");
      break;
    }
    case ANDISx: {
      Format(instr, "andis.  'ra, 'rs, 'uint16");
      break;
    }
    case EXT2: {
      DecodeExt2(instr);
      break;
    }
    case LWZ: {
      Format(instr, "lwz     'rt, 'int16('ra)");
      break;
    }
    case LWZU: {
      Format(instr, "lwzu    'rt, 'int16('ra)");
      break;
    }
    case LBZ: {
      Format(instr, "lbz     'rt, 'int16('ra)");
      break;
    }
    case LBZU: {
      Format(instr, "lbzu    'rt, 'int16('ra)");
      break;
    }
    case STW: {
      Format(instr, "stw     'rs, 'int16('ra)");
      break;
    }
    case STWU: {
      Format(instr, "stwu    'rs, 'int16('ra)");
      break;
    }
    case STB: {
      Format(instr, "stb     'rs, 'int16('ra)");
      break;
    }
    case STBU: {
      Format(instr, "stbu    'rs, 'int16('ra)");
      break;
    }
    case LHZ: {
      Format(instr, "lhz     'rt, 'int16('ra)");
      break;
    }
    case LHZU: {
      Format(instr, "lhzu    'rt, 'int16('ra)");
      break;
    }
    case LHA: {
      Format(instr, "lha     'rt, 'int16('ra)");
      break;
    }
    case LHAU: {
      Format(instr, "lhau    'rt, 'int16('ra)");
      break;
    }
    case STH: {
      Format(instr, "sth 'rs, 'int16('ra)");
      break;
    }
    case STHU: {
      Format(instr, "sthu 'rs, 'int16('ra)");
      break;
    }
    case LMW: {
      UnknownFormat(instr, "lmw");
      break;
    }
    case STMW: {
      UnknownFormat(instr, "stmw");
      break;
    }
    case LFS: {
      Format(instr, "lfs     'Dt, 'int16('ra)");
      break;
    }
    case LFSU: {
      Format(instr, "lfsu    'Dt, 'int16('ra)");
      break;
    }
    case LFD: {
      Format(instr, "lfd     'Dt, 'int16('ra)");
      break;
    }
    case LFDU: {
      Format(instr, "lfdu    'Dt, 'int16('ra)");
      break;
    }
    case STFS: {
      Format(instr, "stfs    'Dt, 'int16('ra)");
      break;
    }
    case STFSU: {
      Format(instr, "stfsu   'Dt, 'int16('ra)");
      break;
    }
    case STFD: {
      Format(instr, "stfd    'Dt, 'int16('ra)");
      break;
    }
    case STFDU: {
      Format(instr, "stfdu   'Dt, 'int16('ra)");
      break;
    }
1411 1412 1413 1414
    case EXT3: {
      DecodeExt3(instr);
      break;
    }
1415 1416 1417 1418 1419 1420 1421 1422
    case EXT4: {
      DecodeExt4(instr);
      break;
    }
    case EXT5: {
      DecodeExt5(instr);
      break;
    }
1423 1424 1425 1426
    case EXT6: {
      DecodeExt6(instr);
      break;
    }
1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456
#if V8_TARGET_ARCH_PPC64
    case LD: {
      switch (instr->Bits(1, 0)) {
        case 0:
          Format(instr, "ld      'rt, 'd('ra)");
          break;
        case 1:
          Format(instr, "ldu     'rt, 'd('ra)");
          break;
        case 2:
          Format(instr, "lwa     'rt, 'd('ra)");
          break;
      }
      break;
    }
    case STD: {  // could be STD or STDU
      if (instr->Bit(0) == 0) {
        Format(instr, "std     'rs, 'd('ra)");
      } else {
        Format(instr, "stdu    'rs, 'd('ra)");
      }
      break;
    }
#endif
    default: {
      Unknown(instr);
      break;
    }
  }

1457
  return kInstrSize;
1458
}
1459 1460
}  // namespace internal
}  // namespace v8
1461 1462 1463 1464 1465 1466

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

namespace disasm {

const char* NameConverter::NameOfAddress(byte* addr) const {
1467
  v8::internal::SNPrintF(tmp_buffer_, "%p", static_cast<void*>(addr));
1468
  return tmp_buffer_.begin();
1469 1470 1471 1472 1473 1474 1475
}

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

const char* NameConverter::NameOfCPURegister(int reg) const {
1476
  return RegisterName(i::Register::from_code(reg));
1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503
}

const char* NameConverter::NameOfByteCPURegister(int reg) const {
  UNREACHABLE();  // PPC does not have the concept of a byte register
}

const char* NameConverter::NameOfXMMRegister(int reg) const {
  UNREACHABLE();  // PPC does not have any XMM registers
}

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 "";
}

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

int Disassembler::InstructionDecode(v8::internal::Vector<char> buffer,
                                    byte* instruction) {
  v8::internal::Decoder d(converter_, buffer);
  return d.InstructionDecode(instruction);
}

// The PPC assembler does not currently use constant pools.
int Disassembler::ConstantPoolSizeAt(byte* instruction) { return -1; }

1504 1505
void Disassembler::Disassemble(FILE* f, byte* begin, byte* end,
                               UnimplementedOpcodeAction unimplemented_action) {
1506
  NameConverter converter;
1507
  Disassembler d(converter, unimplemented_action);
1508 1509 1510 1511 1512
  for (byte* pc = begin; pc < end;) {
    v8::internal::EmbeddedVector<char, 128> buffer;
    buffer[0] = '\0';
    byte* prev_pc = pc;
    pc += d.InstructionDecode(buffer, pc);
1513
    v8::internal::PrintF(f, "%p    %08x      %s\n", static_cast<void*>(prev_pc),
1514
                         *reinterpret_cast<int32_t*>(prev_pc), buffer.begin());
1515 1516 1517
  }
}

1518
#undef STRING_STARTS_WITH
1519 1520 1521 1522

}  // namespace disasm

#endif  // V8_TARGET_ARCH_PPC