Home | History | Annotate | Download | only in ppc
      1 // Copyright 2014 the V8 project authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include <stdarg.h>
      6 #include <stdlib.h>
      7 #include <cmath>
      8 
      9 #if V8_TARGET_ARCH_PPC
     10 
     11 #include "src/assembler.h"
     12 #include "src/base/bits.h"
     13 #include "src/codegen.h"
     14 #include "src/disasm.h"
     15 #include "src/ppc/constants-ppc.h"
     16 #include "src/ppc/frames-ppc.h"
     17 #include "src/ppc/simulator-ppc.h"
     18 
     19 #if defined(USE_SIMULATOR)
     20 
     21 // Only build the simulator if not compiling for real PPC hardware.
     22 namespace v8 {
     23 namespace internal {
     24 
     25 // This macro provides a platform independent use of sscanf. The reason for
     26 // SScanF not being implemented in a platform independent way through
     27 // ::v8::internal::OS in the same way as SNPrintF is that the
     28 // Windows C Run-Time Library does not provide vsscanf.
     29 #define SScanF sscanf  // NOLINT
     30 
     31 // The PPCDebugger class is used by the simulator while debugging simulated
     32 // PowerPC code.
     33 class PPCDebugger {
     34  public:
     35   explicit PPCDebugger(Simulator* sim) : sim_(sim) {}
     36   ~PPCDebugger();
     37 
     38   void Stop(Instruction* instr);
     39   void Debug();
     40 
     41  private:
     42   static const Instr kBreakpointInstr = (TWI | 0x1f * B21);
     43   static const Instr kNopInstr = (ORI);  // ori, 0,0,0
     44 
     45   Simulator* sim_;
     46 
     47   intptr_t GetRegisterValue(int regnum);
     48   double GetRegisterPairDoubleValue(int regnum);
     49   double GetFPDoubleRegisterValue(int regnum);
     50   bool GetValue(const char* desc, intptr_t* value);
     51   bool GetFPDoubleValue(const char* desc, double* value);
     52 
     53   // Set or delete a breakpoint. Returns true if successful.
     54   bool SetBreakpoint(Instruction* break_pc);
     55   bool DeleteBreakpoint(Instruction* break_pc);
     56 
     57   // Undo and redo all breakpoints. This is needed to bracket disassembly and
     58   // execution to skip past breakpoints when run from the debugger.
     59   void UndoBreakpoints();
     60   void RedoBreakpoints();
     61 };
     62 
     63 
     64 PPCDebugger::~PPCDebugger() {}
     65 
     66 
     67 #ifdef GENERATED_CODE_COVERAGE
     68 static FILE* coverage_log = NULL;
     69 
     70 
     71 static void InitializeCoverage() {
     72   char* file_name = getenv("V8_GENERATED_CODE_COVERAGE_LOG");
     73   if (file_name != NULL) {
     74     coverage_log = fopen(file_name, "aw+");
     75   }
     76 }
     77 
     78 
     79 void PPCDebugger::Stop(Instruction* instr) {
     80   // Get the stop code.
     81   uint32_t code = instr->SvcValue() & kStopCodeMask;
     82   // Retrieve the encoded address, which comes just after this stop.
     83   char** msg_address =
     84       reinterpret_cast<char**>(sim_->get_pc() + Instruction::kInstrSize);
     85   char* msg = *msg_address;
     86   DCHECK(msg != NULL);
     87 
     88   // Update this stop description.
     89   if (isWatchedStop(code) && !watched_stops_[code].desc) {
     90     watched_stops_[code].desc = msg;
     91   }
     92 
     93   if (strlen(msg) > 0) {
     94     if (coverage_log != NULL) {
     95       fprintf(coverage_log, "%s\n", msg);
     96       fflush(coverage_log);
     97     }
     98     // Overwrite the instruction and address with nops.
     99     instr->SetInstructionBits(kNopInstr);
    100     reinterpret_cast<Instruction*>(msg_address)->SetInstructionBits(kNopInstr);
    101   }
    102   sim_->set_pc(sim_->get_pc() + Instruction::kInstrSize + kPointerSize);
    103 }
    104 
    105 #else  // ndef GENERATED_CODE_COVERAGE
    106 
    107 static void InitializeCoverage() {}
    108 
    109 
    110 void PPCDebugger::Stop(Instruction* instr) {
    111   // Get the stop code.
    112   // use of kStopCodeMask not right on PowerPC
    113   uint32_t code = instr->SvcValue() & kStopCodeMask;
    114   // Retrieve the encoded address, which comes just after this stop.
    115   char* msg =
    116       *reinterpret_cast<char**>(sim_->get_pc() + Instruction::kInstrSize);
    117   // Update this stop description.
    118   if (sim_->isWatchedStop(code) && !sim_->watched_stops_[code].desc) {
    119     sim_->watched_stops_[code].desc = msg;
    120   }
    121   // Print the stop message and code if it is not the default code.
    122   if (code != kMaxStopCode) {
    123     PrintF("Simulator hit stop %u: %s\n", code, msg);
    124   } else {
    125     PrintF("Simulator hit %s\n", msg);
    126   }
    127   sim_->set_pc(sim_->get_pc() + Instruction::kInstrSize + kPointerSize);
    128   Debug();
    129 }
    130 #endif
    131 
    132 
    133 intptr_t PPCDebugger::GetRegisterValue(int regnum) {
    134   return sim_->get_register(regnum);
    135 }
    136 
    137 
    138 double PPCDebugger::GetRegisterPairDoubleValue(int regnum) {
    139   return sim_->get_double_from_register_pair(regnum);
    140 }
    141 
    142 
    143 double PPCDebugger::GetFPDoubleRegisterValue(int regnum) {
    144   return sim_->get_double_from_d_register(regnum);
    145 }
    146 
    147 
    148 bool PPCDebugger::GetValue(const char* desc, intptr_t* value) {
    149   int regnum = Registers::Number(desc);
    150   if (regnum != kNoRegister) {
    151     *value = GetRegisterValue(regnum);
    152     return true;
    153   } else {
    154     if (strncmp(desc, "0x", 2) == 0) {
    155       return SScanF(desc + 2, "%" V8PRIxPTR,
    156                     reinterpret_cast<uintptr_t*>(value)) == 1;
    157     } else {
    158       return SScanF(desc, "%" V8PRIuPTR, reinterpret_cast<uintptr_t*>(value)) ==
    159              1;
    160     }
    161   }
    162   return false;
    163 }
    164 
    165 
    166 bool PPCDebugger::GetFPDoubleValue(const char* desc, double* value) {
    167   int regnum = DoubleRegisters::Number(desc);
    168   if (regnum != kNoRegister) {
    169     *value = sim_->get_double_from_d_register(regnum);
    170     return true;
    171   }
    172   return false;
    173 }
    174 
    175 
    176 bool PPCDebugger::SetBreakpoint(Instruction* break_pc) {
    177   // Check if a breakpoint can be set. If not return without any side-effects.
    178   if (sim_->break_pc_ != NULL) {
    179     return false;
    180   }
    181 
    182   // Set the breakpoint.
    183   sim_->break_pc_ = break_pc;
    184   sim_->break_instr_ = break_pc->InstructionBits();
    185   // Not setting the breakpoint instruction in the code itself. It will be set
    186   // when the debugger shell continues.
    187   return true;
    188 }
    189 
    190 
    191 bool PPCDebugger::DeleteBreakpoint(Instruction* break_pc) {
    192   if (sim_->break_pc_ != NULL) {
    193     sim_->break_pc_->SetInstructionBits(sim_->break_instr_);
    194   }
    195 
    196   sim_->break_pc_ = NULL;
    197   sim_->break_instr_ = 0;
    198   return true;
    199 }
    200 
    201 
    202 void PPCDebugger::UndoBreakpoints() {
    203   if (sim_->break_pc_ != NULL) {
    204     sim_->break_pc_->SetInstructionBits(sim_->break_instr_);
    205   }
    206 }
    207 
    208 
    209 void PPCDebugger::RedoBreakpoints() {
    210   if (sim_->break_pc_ != NULL) {
    211     sim_->break_pc_->SetInstructionBits(kBreakpointInstr);
    212   }
    213 }
    214 
    215 
    216 void PPCDebugger::Debug() {
    217   intptr_t last_pc = -1;
    218   bool done = false;
    219 
    220 #define COMMAND_SIZE 63
    221 #define ARG_SIZE 255
    222 
    223 #define STR(a) #a
    224 #define XSTR(a) STR(a)
    225 
    226   char cmd[COMMAND_SIZE + 1];
    227   char arg1[ARG_SIZE + 1];
    228   char arg2[ARG_SIZE + 1];
    229   char* argv[3] = {cmd, arg1, arg2};
    230 
    231   // make sure to have a proper terminating character if reaching the limit
    232   cmd[COMMAND_SIZE] = 0;
    233   arg1[ARG_SIZE] = 0;
    234   arg2[ARG_SIZE] = 0;
    235 
    236   // Undo all set breakpoints while running in the debugger shell. This will
    237   // make them invisible to all commands.
    238   UndoBreakpoints();
    239   // Disable tracing while simulating
    240   bool trace = ::v8::internal::FLAG_trace_sim;
    241   ::v8::internal::FLAG_trace_sim = false;
    242 
    243   while (!done && !sim_->has_bad_pc()) {
    244     if (last_pc != sim_->get_pc()) {
    245       disasm::NameConverter converter;
    246       disasm::Disassembler dasm(converter);
    247       // use a reasonably large buffer
    248       v8::internal::EmbeddedVector<char, 256> buffer;
    249       dasm.InstructionDecode(buffer, reinterpret_cast<byte*>(sim_->get_pc()));
    250       PrintF("  0x%08" V8PRIxPTR "  %s\n", sim_->get_pc(), buffer.start());
    251       last_pc = sim_->get_pc();
    252     }
    253     char* line = ReadLine("sim> ");
    254     if (line == NULL) {
    255       break;
    256     } else {
    257       char* last_input = sim_->last_debugger_input();
    258       if (strcmp(line, "\n") == 0 && last_input != NULL) {
    259         line = last_input;
    260       } else {
    261         // Ownership is transferred to sim_;
    262         sim_->set_last_debugger_input(line);
    263       }
    264       // Use sscanf to parse the individual parts of the command line. At the
    265       // moment no command expects more than two parameters.
    266       int argc = SScanF(line,
    267                         "%" XSTR(COMMAND_SIZE) "s "
    268                         "%" XSTR(ARG_SIZE) "s "
    269                         "%" XSTR(ARG_SIZE) "s",
    270                         cmd, arg1, arg2);
    271       if ((strcmp(cmd, "si") == 0) || (strcmp(cmd, "stepi") == 0)) {
    272         intptr_t value;
    273 
    274         // If at a breakpoint, proceed past it.
    275         if ((reinterpret_cast<Instruction*>(sim_->get_pc()))
    276                 ->InstructionBits() == 0x7d821008) {
    277           sim_->set_pc(sim_->get_pc() + Instruction::kInstrSize);
    278         } else {
    279           sim_->ExecuteInstruction(
    280               reinterpret_cast<Instruction*>(sim_->get_pc()));
    281         }
    282 
    283         if (argc == 2 && last_pc != sim_->get_pc() && GetValue(arg1, &value)) {
    284           for (int i = 1; i < value; i++) {
    285             disasm::NameConverter converter;
    286             disasm::Disassembler dasm(converter);
    287             // use a reasonably large buffer
    288             v8::internal::EmbeddedVector<char, 256> buffer;
    289             dasm.InstructionDecode(buffer,
    290                                    reinterpret_cast<byte*>(sim_->get_pc()));
    291             PrintF("  0x%08" V8PRIxPTR "  %s\n", sim_->get_pc(),
    292                    buffer.start());
    293             sim_->ExecuteInstruction(
    294                 reinterpret_cast<Instruction*>(sim_->get_pc()));
    295           }
    296         }
    297       } else if ((strcmp(cmd, "c") == 0) || (strcmp(cmd, "cont") == 0)) {
    298         // If at a breakpoint, proceed past it.
    299         if ((reinterpret_cast<Instruction*>(sim_->get_pc()))
    300                 ->InstructionBits() == 0x7d821008) {
    301           sim_->set_pc(sim_->get_pc() + Instruction::kInstrSize);
    302         } else {
    303           // Execute the one instruction we broke at with breakpoints disabled.
    304           sim_->ExecuteInstruction(
    305               reinterpret_cast<Instruction*>(sim_->get_pc()));
    306         }
    307         // Leave the debugger shell.
    308         done = true;
    309       } else if ((strcmp(cmd, "p") == 0) || (strcmp(cmd, "print") == 0)) {
    310         if (argc == 2 || (argc == 3 && strcmp(arg2, "fp") == 0)) {
    311           intptr_t value;
    312           double dvalue;
    313           if (strcmp(arg1, "all") == 0) {
    314             for (int i = 0; i < kNumRegisters; i++) {
    315               value = GetRegisterValue(i);
    316               PrintF("    %3s: %08" V8PRIxPTR,
    317                      Register::from_code(i).ToString(), value);
    318               if ((argc == 3 && strcmp(arg2, "fp") == 0) && i < 8 &&
    319                   (i % 2) == 0) {
    320                 dvalue = GetRegisterPairDoubleValue(i);
    321                 PrintF(" (%f)\n", dvalue);
    322               } else if (i != 0 && !((i + 1) & 3)) {
    323                 PrintF("\n");
    324               }
    325             }
    326             PrintF("  pc: %08" V8PRIxPTR "  lr: %08" V8PRIxPTR
    327                    "  "
    328                    "ctr: %08" V8PRIxPTR "  xer: %08x  cr: %08x\n",
    329                    sim_->special_reg_pc_, sim_->special_reg_lr_,
    330                    sim_->special_reg_ctr_, sim_->special_reg_xer_,
    331                    sim_->condition_reg_);
    332           } else if (strcmp(arg1, "alld") == 0) {
    333             for (int i = 0; i < kNumRegisters; i++) {
    334               value = GetRegisterValue(i);
    335               PrintF("     %3s: %08" V8PRIxPTR " %11" V8PRIdPTR,
    336                      Register::from_code(i).ToString(), value, value);
    337               if ((argc == 3 && strcmp(arg2, "fp") == 0) && i < 8 &&
    338                   (i % 2) == 0) {
    339                 dvalue = GetRegisterPairDoubleValue(i);
    340                 PrintF(" (%f)\n", dvalue);
    341               } else if (!((i + 1) % 2)) {
    342                 PrintF("\n");
    343               }
    344             }
    345             PrintF("   pc: %08" V8PRIxPTR "  lr: %08" V8PRIxPTR
    346                    "  "
    347                    "ctr: %08" V8PRIxPTR "  xer: %08x  cr: %08x\n",
    348                    sim_->special_reg_pc_, sim_->special_reg_lr_,
    349                    sim_->special_reg_ctr_, sim_->special_reg_xer_,
    350                    sim_->condition_reg_);
    351           } else if (strcmp(arg1, "allf") == 0) {
    352             for (int i = 0; i < DoubleRegister::kNumRegisters; i++) {
    353               dvalue = GetFPDoubleRegisterValue(i);
    354               uint64_t as_words = bit_cast<uint64_t>(dvalue);
    355               PrintF("%3s: %f 0x%08x %08x\n",
    356                      DoubleRegister::from_code(i).ToString(), dvalue,
    357                      static_cast<uint32_t>(as_words >> 32),
    358                      static_cast<uint32_t>(as_words & 0xffffffff));
    359             }
    360           } else if (arg1[0] == 'r' &&
    361                      (arg1[1] >= '0' && arg1[1] <= '9' &&
    362                       (arg1[2] == '\0' || (arg1[2] >= '0' && arg1[2] <= '9' &&
    363                                            arg1[3] == '\0')))) {
    364             int regnum = strtoul(&arg1[1], 0, 10);
    365             if (regnum != kNoRegister) {
    366               value = GetRegisterValue(regnum);
    367               PrintF("%s: 0x%08" V8PRIxPTR " %" V8PRIdPTR "\n", arg1, value,
    368                      value);
    369             } else {
    370               PrintF("%s unrecognized\n", arg1);
    371             }
    372           } else {
    373             if (GetValue(arg1, &value)) {
    374               PrintF("%s: 0x%08" V8PRIxPTR " %" V8PRIdPTR "\n", arg1, value,
    375                      value);
    376             } else if (GetFPDoubleValue(arg1, &dvalue)) {
    377               uint64_t as_words = bit_cast<uint64_t>(dvalue);
    378               PrintF("%s: %f 0x%08x %08x\n", arg1, dvalue,
    379                      static_cast<uint32_t>(as_words >> 32),
    380                      static_cast<uint32_t>(as_words & 0xffffffff));
    381             } else {
    382               PrintF("%s unrecognized\n", arg1);
    383             }
    384           }
    385         } else {
    386           PrintF("print <register>\n");
    387         }
    388       } else if ((strcmp(cmd, "po") == 0) ||
    389                  (strcmp(cmd, "printobject") == 0)) {
    390         if (argc == 2) {
    391           intptr_t value;
    392           OFStream os(stdout);
    393           if (GetValue(arg1, &value)) {
    394             Object* obj = reinterpret_cast<Object*>(value);
    395             os << arg1 << ": \n";
    396 #ifdef DEBUG
    397             obj->Print(os);
    398             os << "\n";
    399 #else
    400             os << Brief(obj) << "\n";
    401 #endif
    402           } else {
    403             os << arg1 << " unrecognized\n";
    404           }
    405         } else {
    406           PrintF("printobject <value>\n");
    407         }
    408       } else if (strcmp(cmd, "setpc") == 0) {
    409         intptr_t value;
    410 
    411         if (!GetValue(arg1, &value)) {
    412           PrintF("%s unrecognized\n", arg1);
    413           continue;
    414         }
    415         sim_->set_pc(value);
    416       } else if (strcmp(cmd, "stack") == 0 || strcmp(cmd, "mem") == 0) {
    417         intptr_t* cur = NULL;
    418         intptr_t* end = NULL;
    419         int next_arg = 1;
    420 
    421         if (strcmp(cmd, "stack") == 0) {
    422           cur = reinterpret_cast<intptr_t*>(sim_->get_register(Simulator::sp));
    423         } else {  // "mem"
    424           intptr_t value;
    425           if (!GetValue(arg1, &value)) {
    426             PrintF("%s unrecognized\n", arg1);
    427             continue;
    428           }
    429           cur = reinterpret_cast<intptr_t*>(value);
    430           next_arg++;
    431         }
    432 
    433         intptr_t words;  // likely inaccurate variable name for 64bit
    434         if (argc == next_arg) {
    435           words = 10;
    436         } else {
    437           if (!GetValue(argv[next_arg], &words)) {
    438             words = 10;
    439           }
    440         }
    441         end = cur + words;
    442 
    443         while (cur < end) {
    444           PrintF("  0x%08" V8PRIxPTR ":  0x%08" V8PRIxPTR " %10" V8PRIdPTR,
    445                  reinterpret_cast<intptr_t>(cur), *cur, *cur);
    446           HeapObject* obj = reinterpret_cast<HeapObject*>(*cur);
    447           intptr_t value = *cur;
    448           Heap* current_heap = sim_->isolate_->heap();
    449           if (((value & 1) == 0) || current_heap->Contains(obj)) {
    450             PrintF(" (");
    451             if ((value & 1) == 0) {
    452               PrintF("smi %d", PlatformSmiTagging::SmiToInt(obj));
    453             } else {
    454               obj->ShortPrint();
    455             }
    456             PrintF(")");
    457           }
    458           PrintF("\n");
    459           cur++;
    460         }
    461       } else if (strcmp(cmd, "disasm") == 0 || strcmp(cmd, "di") == 0) {
    462         disasm::NameConverter converter;
    463         disasm::Disassembler dasm(converter);
    464         // use a reasonably large buffer
    465         v8::internal::EmbeddedVector<char, 256> buffer;
    466 
    467         byte* prev = NULL;
    468         byte* cur = NULL;
    469         byte* end = NULL;
    470 
    471         if (argc == 1) {
    472           cur = reinterpret_cast<byte*>(sim_->get_pc());
    473           end = cur + (10 * Instruction::kInstrSize);
    474         } else if (argc == 2) {
    475           int regnum = Registers::Number(arg1);
    476           if (regnum != kNoRegister || strncmp(arg1, "0x", 2) == 0) {
    477             // The argument is an address or a register name.
    478             intptr_t value;
    479             if (GetValue(arg1, &value)) {
    480               cur = reinterpret_cast<byte*>(value);
    481               // Disassemble 10 instructions at <arg1>.
    482               end = cur + (10 * Instruction::kInstrSize);
    483             }
    484           } else {
    485             // The argument is the number of instructions.
    486             intptr_t value;
    487             if (GetValue(arg1, &value)) {
    488               cur = reinterpret_cast<byte*>(sim_->get_pc());
    489               // Disassemble <arg1> instructions.
    490               end = cur + (value * Instruction::kInstrSize);
    491             }
    492           }
    493         } else {
    494           intptr_t value1;
    495           intptr_t value2;
    496           if (GetValue(arg1, &value1) && GetValue(arg2, &value2)) {
    497             cur = reinterpret_cast<byte*>(value1);
    498             end = cur + (value2 * Instruction::kInstrSize);
    499           }
    500         }
    501 
    502         while (cur < end) {
    503           prev = cur;
    504           cur += dasm.InstructionDecode(buffer, cur);
    505           PrintF("  0x%08" V8PRIxPTR "  %s\n", reinterpret_cast<intptr_t>(prev),
    506                  buffer.start());
    507         }
    508       } else if (strcmp(cmd, "gdb") == 0) {
    509         PrintF("relinquishing control to gdb\n");
    510         v8::base::OS::DebugBreak();
    511         PrintF("regaining control from gdb\n");
    512       } else if (strcmp(cmd, "break") == 0) {
    513         if (argc == 2) {
    514           intptr_t value;
    515           if (GetValue(arg1, &value)) {
    516             if (!SetBreakpoint(reinterpret_cast<Instruction*>(value))) {
    517               PrintF("setting breakpoint failed\n");
    518             }
    519           } else {
    520             PrintF("%s unrecognized\n", arg1);
    521           }
    522         } else {
    523           PrintF("break <address>\n");
    524         }
    525       } else if (strcmp(cmd, "del") == 0) {
    526         if (!DeleteBreakpoint(NULL)) {
    527           PrintF("deleting breakpoint failed\n");
    528         }
    529       } else if (strcmp(cmd, "cr") == 0) {
    530         PrintF("Condition reg: %08x\n", sim_->condition_reg_);
    531       } else if (strcmp(cmd, "lr") == 0) {
    532         PrintF("Link reg: %08" V8PRIxPTR "\n", sim_->special_reg_lr_);
    533       } else if (strcmp(cmd, "ctr") == 0) {
    534         PrintF("Ctr reg: %08" V8PRIxPTR "\n", sim_->special_reg_ctr_);
    535       } else if (strcmp(cmd, "xer") == 0) {
    536         PrintF("XER: %08x\n", sim_->special_reg_xer_);
    537       } else if (strcmp(cmd, "fpscr") == 0) {
    538         PrintF("FPSCR: %08x\n", sim_->fp_condition_reg_);
    539       } else if (strcmp(cmd, "stop") == 0) {
    540         intptr_t value;
    541         intptr_t stop_pc =
    542             sim_->get_pc() - (Instruction::kInstrSize + kPointerSize);
    543         Instruction* stop_instr = reinterpret_cast<Instruction*>(stop_pc);
    544         Instruction* msg_address =
    545             reinterpret_cast<Instruction*>(stop_pc + Instruction::kInstrSize);
    546         if ((argc == 2) && (strcmp(arg1, "unstop") == 0)) {
    547           // Remove the current stop.
    548           if (sim_->isStopInstruction(stop_instr)) {
    549             stop_instr->SetInstructionBits(kNopInstr);
    550             msg_address->SetInstructionBits(kNopInstr);
    551           } else {
    552             PrintF("Not at debugger stop.\n");
    553           }
    554         } else if (argc == 3) {
    555           // Print information about all/the specified breakpoint(s).
    556           if (strcmp(arg1, "info") == 0) {
    557             if (strcmp(arg2, "all") == 0) {
    558               PrintF("Stop information:\n");
    559               for (uint32_t i = 0; i < sim_->kNumOfWatchedStops; i++) {
    560                 sim_->PrintStopInfo(i);
    561               }
    562             } else if (GetValue(arg2, &value)) {
    563               sim_->PrintStopInfo(value);
    564             } else {
    565               PrintF("Unrecognized argument.\n");
    566             }
    567           } else if (strcmp(arg1, "enable") == 0) {
    568             // Enable all/the specified breakpoint(s).
    569             if (strcmp(arg2, "all") == 0) {
    570               for (uint32_t i = 0; i < sim_->kNumOfWatchedStops; i++) {
    571                 sim_->EnableStop(i);
    572               }
    573             } else if (GetValue(arg2, &value)) {
    574               sim_->EnableStop(value);
    575             } else {
    576               PrintF("Unrecognized argument.\n");
    577             }
    578           } else if (strcmp(arg1, "disable") == 0) {
    579             // Disable all/the specified breakpoint(s).
    580             if (strcmp(arg2, "all") == 0) {
    581               for (uint32_t i = 0; i < sim_->kNumOfWatchedStops; i++) {
    582                 sim_->DisableStop(i);
    583               }
    584             } else if (GetValue(arg2, &value)) {
    585               sim_->DisableStop(value);
    586             } else {
    587               PrintF("Unrecognized argument.\n");
    588             }
    589           }
    590         } else {
    591           PrintF("Wrong usage. Use help command for more information.\n");
    592         }
    593       } else if ((strcmp(cmd, "t") == 0) || strcmp(cmd, "trace") == 0) {
    594         ::v8::internal::FLAG_trace_sim = !::v8::internal::FLAG_trace_sim;
    595         PrintF("Trace of executed instructions is %s\n",
    596                ::v8::internal::FLAG_trace_sim ? "on" : "off");
    597       } else if ((strcmp(cmd, "h") == 0) || (strcmp(cmd, "help") == 0)) {
    598         PrintF("cont\n");
    599         PrintF("  continue execution (alias 'c')\n");
    600         PrintF("stepi [num instructions]\n");
    601         PrintF("  step one/num instruction(s) (alias 'si')\n");
    602         PrintF("print <register>\n");
    603         PrintF("  print register content (alias 'p')\n");
    604         PrintF("  use register name 'all' to display all integer registers\n");
    605         PrintF(
    606             "  use register name 'alld' to display integer registers "
    607             "with decimal values\n");
    608         PrintF("  use register name 'rN' to display register number 'N'\n");
    609         PrintF("  add argument 'fp' to print register pair double values\n");
    610         PrintF(
    611             "  use register name 'allf' to display floating-point "
    612             "registers\n");
    613         PrintF("printobject <register>\n");
    614         PrintF("  print an object from a register (alias 'po')\n");
    615         PrintF("cr\n");
    616         PrintF("  print condition register\n");
    617         PrintF("lr\n");
    618         PrintF("  print link register\n");
    619         PrintF("ctr\n");
    620         PrintF("  print ctr register\n");
    621         PrintF("xer\n");
    622         PrintF("  print XER\n");
    623         PrintF("fpscr\n");
    624         PrintF("  print FPSCR\n");
    625         PrintF("stack [<num words>]\n");
    626         PrintF("  dump stack content, default dump 10 words)\n");
    627         PrintF("mem <address> [<num words>]\n");
    628         PrintF("  dump memory content, default dump 10 words)\n");
    629         PrintF("disasm [<instructions>]\n");
    630         PrintF("disasm [<address/register>]\n");
    631         PrintF("disasm [[<address/register>] <instructions>]\n");
    632         PrintF("  disassemble code, default is 10 instructions\n");
    633         PrintF("  from pc (alias 'di')\n");
    634         PrintF("gdb\n");
    635         PrintF("  enter gdb\n");
    636         PrintF("break <address>\n");
    637         PrintF("  set a break point on the address\n");
    638         PrintF("del\n");
    639         PrintF("  delete the breakpoint\n");
    640         PrintF("trace (alias 't')\n");
    641         PrintF("  toogle the tracing of all executed statements\n");
    642         PrintF("stop feature:\n");
    643         PrintF("  Description:\n");
    644         PrintF("    Stops are debug instructions inserted by\n");
    645         PrintF("    the Assembler::stop() function.\n");
    646         PrintF("    When hitting a stop, the Simulator will\n");
    647         PrintF("    stop and and give control to the PPCDebugger.\n");
    648         PrintF("    The first %d stop codes are watched:\n",
    649                Simulator::kNumOfWatchedStops);
    650         PrintF("    - They can be enabled / disabled: the Simulator\n");
    651         PrintF("      will / won't stop when hitting them.\n");
    652         PrintF("    - The Simulator keeps track of how many times they \n");
    653         PrintF("      are met. (See the info command.) Going over a\n");
    654         PrintF("      disabled stop still increases its counter. \n");
    655         PrintF("  Commands:\n");
    656         PrintF("    stop info all/<code> : print infos about number <code>\n");
    657         PrintF("      or all stop(s).\n");
    658         PrintF("    stop enable/disable all/<code> : enables / disables\n");
    659         PrintF("      all or number <code> stop(s)\n");
    660         PrintF("    stop unstop\n");
    661         PrintF("      ignore the stop instruction at the current location\n");
    662         PrintF("      from now on\n");
    663       } else {
    664         PrintF("Unknown command: %s\n", cmd);
    665       }
    666     }
    667   }
    668 
    669   // Add all the breakpoints back to stop execution and enter the debugger
    670   // shell when hit.
    671   RedoBreakpoints();
    672   // Restore tracing
    673   ::v8::internal::FLAG_trace_sim = trace;
    674 
    675 #undef COMMAND_SIZE
    676 #undef ARG_SIZE
    677 
    678 #undef STR
    679 #undef XSTR
    680 }
    681 
    682 
    683 static bool ICacheMatch(void* one, void* two) {
    684   DCHECK((reinterpret_cast<intptr_t>(one) & CachePage::kPageMask) == 0);
    685   DCHECK((reinterpret_cast<intptr_t>(two) & CachePage::kPageMask) == 0);
    686   return one == two;
    687 }
    688 
    689 
    690 static uint32_t ICacheHash(void* key) {
    691   return static_cast<uint32_t>(reinterpret_cast<uintptr_t>(key)) >> 2;
    692 }
    693 
    694 
    695 static bool AllOnOnePage(uintptr_t start, int size) {
    696   intptr_t start_page = (start & ~CachePage::kPageMask);
    697   intptr_t end_page = ((start + size) & ~CachePage::kPageMask);
    698   return start_page == end_page;
    699 }
    700 
    701 
    702 void Simulator::set_last_debugger_input(char* input) {
    703   DeleteArray(last_debugger_input_);
    704   last_debugger_input_ = input;
    705 }
    706 
    707 
    708 void Simulator::FlushICache(v8::internal::HashMap* i_cache, void* start_addr,
    709                             size_t size) {
    710   intptr_t start = reinterpret_cast<intptr_t>(start_addr);
    711   int intra_line = (start & CachePage::kLineMask);
    712   start -= intra_line;
    713   size += intra_line;
    714   size = ((size - 1) | CachePage::kLineMask) + 1;
    715   int offset = (start & CachePage::kPageMask);
    716   while (!AllOnOnePage(start, size - 1)) {
    717     int bytes_to_flush = CachePage::kPageSize - offset;
    718     FlushOnePage(i_cache, start, bytes_to_flush);
    719     start += bytes_to_flush;
    720     size -= bytes_to_flush;
    721     DCHECK_EQ(0, static_cast<int>(start & CachePage::kPageMask));
    722     offset = 0;
    723   }
    724   if (size != 0) {
    725     FlushOnePage(i_cache, start, size);
    726   }
    727 }
    728 
    729 
    730 CachePage* Simulator::GetCachePage(v8::internal::HashMap* i_cache, void* page) {
    731   v8::internal::HashMap::Entry* entry =
    732       i_cache->LookupOrInsert(page, ICacheHash(page));
    733   if (entry->value == NULL) {
    734     CachePage* new_page = new CachePage();
    735     entry->value = new_page;
    736   }
    737   return reinterpret_cast<CachePage*>(entry->value);
    738 }
    739 
    740 
    741 // Flush from start up to and not including start + size.
    742 void Simulator::FlushOnePage(v8::internal::HashMap* i_cache, intptr_t start,
    743                              int size) {
    744   DCHECK(size <= CachePage::kPageSize);
    745   DCHECK(AllOnOnePage(start, size - 1));
    746   DCHECK((start & CachePage::kLineMask) == 0);
    747   DCHECK((size & CachePage::kLineMask) == 0);
    748   void* page = reinterpret_cast<void*>(start & (~CachePage::kPageMask));
    749   int offset = (start & CachePage::kPageMask);
    750   CachePage* cache_page = GetCachePage(i_cache, page);
    751   char* valid_bytemap = cache_page->ValidityByte(offset);
    752   memset(valid_bytemap, CachePage::LINE_INVALID, size >> CachePage::kLineShift);
    753 }
    754 
    755 
    756 void Simulator::CheckICache(v8::internal::HashMap* i_cache,
    757                             Instruction* instr) {
    758   intptr_t address = reinterpret_cast<intptr_t>(instr);
    759   void* page = reinterpret_cast<void*>(address & (~CachePage::kPageMask));
    760   void* line = reinterpret_cast<void*>(address & (~CachePage::kLineMask));
    761   int offset = (address & CachePage::kPageMask);
    762   CachePage* cache_page = GetCachePage(i_cache, page);
    763   char* cache_valid_byte = cache_page->ValidityByte(offset);
    764   bool cache_hit = (*cache_valid_byte == CachePage::LINE_VALID);
    765   char* cached_line = cache_page->CachedData(offset & ~CachePage::kLineMask);
    766   if (cache_hit) {
    767     // Check that the data in memory matches the contents of the I-cache.
    768     CHECK_EQ(0,
    769              memcmp(reinterpret_cast<void*>(instr),
    770                     cache_page->CachedData(offset), Instruction::kInstrSize));
    771   } else {
    772     // Cache miss.  Load memory into the cache.
    773     memcpy(cached_line, line, CachePage::kLineLength);
    774     *cache_valid_byte = CachePage::LINE_VALID;
    775   }
    776 }
    777 
    778 
    779 void Simulator::Initialize(Isolate* isolate) {
    780   if (isolate->simulator_initialized()) return;
    781   isolate->set_simulator_initialized(true);
    782   ::v8::internal::ExternalReference::set_redirector(isolate,
    783                                                     &RedirectExternalReference);
    784 }
    785 
    786 
    787 Simulator::Simulator(Isolate* isolate) : isolate_(isolate) {
    788   i_cache_ = isolate_->simulator_i_cache();
    789   if (i_cache_ == NULL) {
    790     i_cache_ = new v8::internal::HashMap(&ICacheMatch);
    791     isolate_->set_simulator_i_cache(i_cache_);
    792   }
    793   Initialize(isolate);
    794 // Set up simulator support first. Some of this information is needed to
    795 // setup the architecture state.
    796 #if V8_TARGET_ARCH_PPC64
    797   size_t stack_size = FLAG_sim_stack_size * KB;
    798 #else
    799   size_t stack_size = MB;  // allocate 1MB for stack
    800 #endif
    801   stack_size += 2 * stack_protection_size_;
    802   stack_ = reinterpret_cast<char*>(malloc(stack_size));
    803   pc_modified_ = false;
    804   icount_ = 0;
    805   break_pc_ = NULL;
    806   break_instr_ = 0;
    807 
    808   // Set up architecture state.
    809   // All registers are initialized to zero to start with.
    810   for (int i = 0; i < kNumGPRs; i++) {
    811     registers_[i] = 0;
    812   }
    813   condition_reg_ = 0;
    814   fp_condition_reg_ = 0;
    815   special_reg_pc_ = 0;
    816   special_reg_lr_ = 0;
    817   special_reg_ctr_ = 0;
    818 
    819   // Initializing FP registers.
    820   for (int i = 0; i < kNumFPRs; i++) {
    821     fp_registers_[i] = 0.0;
    822   }
    823 
    824   // The sp is initialized to point to the bottom (high address) of the
    825   // allocated stack area. To be safe in potential stack underflows we leave
    826   // some buffer below.
    827   registers_[sp] =
    828       reinterpret_cast<intptr_t>(stack_) + stack_size - stack_protection_size_;
    829   InitializeCoverage();
    830 
    831   last_debugger_input_ = NULL;
    832 }
    833 
    834 
    835 Simulator::~Simulator() { free(stack_); }
    836 
    837 
    838 // When the generated code calls an external reference we need to catch that in
    839 // the simulator.  The external reference will be a function compiled for the
    840 // host architecture.  We need to call that function instead of trying to
    841 // execute it with the simulator.  We do that by redirecting the external
    842 // reference to a svc (Supervisor Call) instruction that is handled by
    843 // the simulator.  We write the original destination of the jump just at a known
    844 // offset from the svc instruction so the simulator knows what to call.
    845 class Redirection {
    846  public:
    847   Redirection(Isolate* isolate, void* external_function,
    848               ExternalReference::Type type)
    849       : external_function_(external_function),
    850         swi_instruction_(rtCallRedirInstr | kCallRtRedirected),
    851         type_(type),
    852         next_(NULL) {
    853     next_ = isolate->simulator_redirection();
    854     Simulator::current(isolate)->FlushICache(
    855         isolate->simulator_i_cache(),
    856         reinterpret_cast<void*>(&swi_instruction_), Instruction::kInstrSize);
    857     isolate->set_simulator_redirection(this);
    858   }
    859 
    860   void* address_of_swi_instruction() {
    861     return reinterpret_cast<void*>(&swi_instruction_);
    862   }
    863 
    864   void* external_function() { return external_function_; }
    865   ExternalReference::Type type() { return type_; }
    866 
    867   static Redirection* Get(Isolate* isolate, void* external_function,
    868                           ExternalReference::Type type) {
    869     Redirection* current = isolate->simulator_redirection();
    870     for (; current != NULL; current = current->next_) {
    871       if (current->external_function_ == external_function) {
    872         DCHECK_EQ(current->type(), type);
    873         return current;
    874       }
    875     }
    876     return new Redirection(isolate, external_function, type);
    877   }
    878 
    879   static Redirection* FromSwiInstruction(Instruction* swi_instruction) {
    880     char* addr_of_swi = reinterpret_cast<char*>(swi_instruction);
    881     char* addr_of_redirection =
    882         addr_of_swi - offsetof(Redirection, swi_instruction_);
    883     return reinterpret_cast<Redirection*>(addr_of_redirection);
    884   }
    885 
    886   static void* ReverseRedirection(intptr_t reg) {
    887     Redirection* redirection = FromSwiInstruction(
    888         reinterpret_cast<Instruction*>(reinterpret_cast<void*>(reg)));
    889     return redirection->external_function();
    890   }
    891 
    892   static void DeleteChain(Redirection* redirection) {
    893     while (redirection != nullptr) {
    894       Redirection* next = redirection->next_;
    895       delete redirection;
    896       redirection = next;
    897     }
    898   }
    899 
    900  private:
    901   void* external_function_;
    902   uint32_t swi_instruction_;
    903   ExternalReference::Type type_;
    904   Redirection* next_;
    905 };
    906 
    907 
    908 // static
    909 void Simulator::TearDown(HashMap* i_cache, Redirection* first) {
    910   Redirection::DeleteChain(first);
    911   if (i_cache != nullptr) {
    912     for (HashMap::Entry* entry = i_cache->Start(); entry != nullptr;
    913          entry = i_cache->Next(entry)) {
    914       delete static_cast<CachePage*>(entry->value);
    915     }
    916     delete i_cache;
    917   }
    918 }
    919 
    920 
    921 void* Simulator::RedirectExternalReference(Isolate* isolate,
    922                                            void* external_function,
    923                                            ExternalReference::Type type) {
    924   Redirection* redirection = Redirection::Get(isolate, external_function, type);
    925   return redirection->address_of_swi_instruction();
    926 }
    927 
    928 
    929 // Get the active Simulator for the current thread.
    930 Simulator* Simulator::current(Isolate* isolate) {
    931   v8::internal::Isolate::PerIsolateThreadData* isolate_data =
    932       isolate->FindOrAllocatePerThreadDataForThisThread();
    933   DCHECK(isolate_data != NULL);
    934 
    935   Simulator* sim = isolate_data->simulator();
    936   if (sim == NULL) {
    937     // TODO(146): delete the simulator object when a thread/isolate goes away.
    938     sim = new Simulator(isolate);
    939     isolate_data->set_simulator(sim);
    940   }
    941   return sim;
    942 }
    943 
    944 
    945 // Sets the register in the architecture state.
    946 void Simulator::set_register(int reg, intptr_t value) {
    947   DCHECK((reg >= 0) && (reg < kNumGPRs));
    948   registers_[reg] = value;
    949 }
    950 
    951 
    952 // Get the register from the architecture state.
    953 intptr_t Simulator::get_register(int reg) const {
    954   DCHECK((reg >= 0) && (reg < kNumGPRs));
    955   // Stupid code added to avoid bug in GCC.
    956   // See: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43949
    957   if (reg >= kNumGPRs) return 0;
    958   // End stupid code.
    959   return registers_[reg];
    960 }
    961 
    962 
    963 double Simulator::get_double_from_register_pair(int reg) {
    964   DCHECK((reg >= 0) && (reg < kNumGPRs) && ((reg % 2) == 0));
    965 
    966   double dm_val = 0.0;
    967 #if !V8_TARGET_ARCH_PPC64  // doesn't make sense in 64bit mode
    968   // Read the bits from the unsigned integer register_[] array
    969   // into the double precision floating point value and return it.
    970   char buffer[sizeof(fp_registers_[0])];
    971   memcpy(buffer, &registers_[reg], 2 * sizeof(registers_[0]));
    972   memcpy(&dm_val, buffer, 2 * sizeof(registers_[0]));
    973 #endif
    974   return (dm_val);
    975 }
    976 
    977 
    978 // Raw access to the PC register.
    979 void Simulator::set_pc(intptr_t value) {
    980   pc_modified_ = true;
    981   special_reg_pc_ = value;
    982 }
    983 
    984 
    985 bool Simulator::has_bad_pc() const {
    986   return ((special_reg_pc_ == bad_lr) || (special_reg_pc_ == end_sim_pc));
    987 }
    988 
    989 
    990 // Raw access to the PC register without the special adjustment when reading.
    991 intptr_t Simulator::get_pc() const { return special_reg_pc_; }
    992 
    993 
    994 // Runtime FP routines take:
    995 // - two double arguments
    996 // - one double argument and zero or one integer arguments.
    997 // All are consructed here from d1, d2 and r3.
    998 void Simulator::GetFpArgs(double* x, double* y, intptr_t* z) {
    999   *x = get_double_from_d_register(1);
   1000   *y = get_double_from_d_register(2);
   1001   *z = get_register(3);
   1002 }
   1003 
   1004 
   1005 // The return value is in d1.
   1006 void Simulator::SetFpResult(const double& result) {
   1007   set_d_register_from_double(1, result);
   1008 }
   1009 
   1010 
   1011 void Simulator::TrashCallerSaveRegisters() {
   1012 // We don't trash the registers with the return value.
   1013 #if 0  // A good idea to trash volatile registers, needs to be done
   1014   registers_[2] = 0x50Bad4U;
   1015   registers_[3] = 0x50Bad4U;
   1016   registers_[12] = 0x50Bad4U;
   1017 #endif
   1018 }
   1019 
   1020 
   1021 uint32_t Simulator::ReadWU(intptr_t addr, Instruction* instr) {
   1022   uint32_t* ptr = reinterpret_cast<uint32_t*>(addr);
   1023   return *ptr;
   1024 }
   1025 
   1026 
   1027 int32_t Simulator::ReadW(intptr_t addr, Instruction* instr) {
   1028   int32_t* ptr = reinterpret_cast<int32_t*>(addr);
   1029   return *ptr;
   1030 }
   1031 
   1032 
   1033 void Simulator::WriteW(intptr_t addr, uint32_t value, Instruction* instr) {
   1034   uint32_t* ptr = reinterpret_cast<uint32_t*>(addr);
   1035   *ptr = value;
   1036   return;
   1037 }
   1038 
   1039 
   1040 void Simulator::WriteW(intptr_t addr, int32_t value, Instruction* instr) {
   1041   int32_t* ptr = reinterpret_cast<int32_t*>(addr);
   1042   *ptr = value;
   1043   return;
   1044 }
   1045 
   1046 
   1047 uint16_t Simulator::ReadHU(intptr_t addr, Instruction* instr) {
   1048   uint16_t* ptr = reinterpret_cast<uint16_t*>(addr);
   1049   return *ptr;
   1050 }
   1051 
   1052 
   1053 int16_t Simulator::ReadH(intptr_t addr, Instruction* instr) {
   1054   int16_t* ptr = reinterpret_cast<int16_t*>(addr);
   1055   return *ptr;
   1056 }
   1057 
   1058 
   1059 void Simulator::WriteH(intptr_t addr, uint16_t value, Instruction* instr) {
   1060   uint16_t* ptr = reinterpret_cast<uint16_t*>(addr);
   1061   *ptr = value;
   1062   return;
   1063 }
   1064 
   1065 
   1066 void Simulator::WriteH(intptr_t addr, int16_t value, Instruction* instr) {
   1067   int16_t* ptr = reinterpret_cast<int16_t*>(addr);
   1068   *ptr = value;
   1069   return;
   1070 }
   1071 
   1072 
   1073 uint8_t Simulator::ReadBU(intptr_t addr) {
   1074   uint8_t* ptr = reinterpret_cast<uint8_t*>(addr);
   1075   return *ptr;
   1076 }
   1077 
   1078 
   1079 int8_t Simulator::ReadB(intptr_t addr) {
   1080   int8_t* ptr = reinterpret_cast<int8_t*>(addr);
   1081   return *ptr;
   1082 }
   1083 
   1084 
   1085 void Simulator::WriteB(intptr_t addr, uint8_t value) {
   1086   uint8_t* ptr = reinterpret_cast<uint8_t*>(addr);
   1087   *ptr = value;
   1088 }
   1089 
   1090 
   1091 void Simulator::WriteB(intptr_t addr, int8_t value) {
   1092   int8_t* ptr = reinterpret_cast<int8_t*>(addr);
   1093   *ptr = value;
   1094 }
   1095 
   1096 
   1097 intptr_t* Simulator::ReadDW(intptr_t addr) {
   1098   intptr_t* ptr = reinterpret_cast<intptr_t*>(addr);
   1099   return ptr;
   1100 }
   1101 
   1102 
   1103 void Simulator::WriteDW(intptr_t addr, int64_t value) {
   1104   int64_t* ptr = reinterpret_cast<int64_t*>(addr);
   1105   *ptr = value;
   1106   return;
   1107 }
   1108 
   1109 
   1110 // Returns the limit of the stack area to enable checking for stack overflows.
   1111 uintptr_t Simulator::StackLimit(uintptr_t c_limit) const {
   1112   // The simulator uses a separate JS stack. If we have exhausted the C stack,
   1113   // we also drop down the JS limit to reflect the exhaustion on the JS stack.
   1114   if (GetCurrentStackPosition() < c_limit) {
   1115     return reinterpret_cast<uintptr_t>(get_sp());
   1116   }
   1117 
   1118   // Otherwise the limit is the JS stack. Leave a safety margin to prevent
   1119   // overrunning the stack when pushing values.
   1120   return reinterpret_cast<uintptr_t>(stack_) + stack_protection_size_;
   1121 }
   1122 
   1123 
   1124 // Unsupported instructions use Format to print an error and stop execution.
   1125 void Simulator::Format(Instruction* instr, const char* format) {
   1126   PrintF("Simulator found unsupported instruction:\n 0x%08" V8PRIxPTR ": %s\n",
   1127          reinterpret_cast<intptr_t>(instr), format);
   1128   UNIMPLEMENTED();
   1129 }
   1130 
   1131 
   1132 // Calculate C flag value for additions.
   1133 bool Simulator::CarryFrom(int32_t left, int32_t right, int32_t carry) {
   1134   uint32_t uleft = static_cast<uint32_t>(left);
   1135   uint32_t uright = static_cast<uint32_t>(right);
   1136   uint32_t urest = 0xffffffffU - uleft;
   1137 
   1138   return (uright > urest) ||
   1139          (carry && (((uright + 1) > urest) || (uright > (urest - 1))));
   1140 }
   1141 
   1142 
   1143 // Calculate C flag value for subtractions.
   1144 bool Simulator::BorrowFrom(int32_t left, int32_t right) {
   1145   uint32_t uleft = static_cast<uint32_t>(left);
   1146   uint32_t uright = static_cast<uint32_t>(right);
   1147 
   1148   return (uright > uleft);
   1149 }
   1150 
   1151 
   1152 // Calculate V flag value for additions and subtractions.
   1153 bool Simulator::OverflowFrom(int32_t alu_out, int32_t left, int32_t right,
   1154                              bool addition) {
   1155   bool overflow;
   1156   if (addition) {
   1157     // operands have the same sign
   1158     overflow = ((left >= 0 && right >= 0) || (left < 0 && right < 0))
   1159                // and operands and result have different sign
   1160                &&
   1161                ((left < 0 && alu_out >= 0) || (left >= 0 && alu_out < 0));
   1162   } else {
   1163     // operands have different signs
   1164     overflow = ((left < 0 && right >= 0) || (left >= 0 && right < 0))
   1165                // and first operand and result have different signs
   1166                &&
   1167                ((left < 0 && alu_out >= 0) || (left >= 0 && alu_out < 0));
   1168   }
   1169   return overflow;
   1170 }
   1171 
   1172 
   1173 #if V8_TARGET_ARCH_PPC64
   1174 struct ObjectPair {
   1175   intptr_t x;
   1176   intptr_t y;
   1177 };
   1178 
   1179 
   1180 static void decodeObjectPair(ObjectPair* pair, intptr_t* x, intptr_t* y) {
   1181   *x = pair->x;
   1182   *y = pair->y;
   1183 }
   1184 #else
   1185 typedef uint64_t ObjectPair;
   1186 
   1187 
   1188 static void decodeObjectPair(ObjectPair* pair, intptr_t* x, intptr_t* y) {
   1189 #if V8_TARGET_BIG_ENDIAN
   1190   *x = static_cast<int32_t>(*pair >> 32);
   1191   *y = static_cast<int32_t>(*pair);
   1192 #else
   1193   *x = static_cast<int32_t>(*pair);
   1194   *y = static_cast<int32_t>(*pair >> 32);
   1195 #endif
   1196 }
   1197 #endif
   1198 
   1199 // Calls into the V8 runtime are based on this very simple interface.
   1200 // Note: To be able to return two values from some calls the code in
   1201 // runtime.cc uses the ObjectPair which is essentially two pointer
   1202 // values stuffed into a structure. With the code below we assume that
   1203 // all runtime calls return this pair. If they don't, the r4 result
   1204 // register contains a bogus value, which is fine because it is
   1205 // caller-saved.
   1206 typedef ObjectPair (*SimulatorRuntimeCall)(intptr_t arg0, intptr_t arg1,
   1207                                            intptr_t arg2, intptr_t arg3,
   1208                                            intptr_t arg4, intptr_t arg5);
   1209 
   1210 // These prototypes handle the four types of FP calls.
   1211 typedef int (*SimulatorRuntimeCompareCall)(double darg0, double darg1);
   1212 typedef double (*SimulatorRuntimeFPFPCall)(double darg0, double darg1);
   1213 typedef double (*SimulatorRuntimeFPCall)(double darg0);
   1214 typedef double (*SimulatorRuntimeFPIntCall)(double darg0, intptr_t arg0);
   1215 
   1216 // This signature supports direct call in to API function native callback
   1217 // (refer to InvocationCallback in v8.h).
   1218 typedef void (*SimulatorRuntimeDirectApiCall)(intptr_t arg0);
   1219 typedef void (*SimulatorRuntimeProfilingApiCall)(intptr_t arg0, void* arg1);
   1220 
   1221 // This signature supports direct call to accessor getter callback.
   1222 typedef void (*SimulatorRuntimeDirectGetterCall)(intptr_t arg0, intptr_t arg1);
   1223 typedef void (*SimulatorRuntimeProfilingGetterCall)(intptr_t arg0,
   1224                                                     intptr_t arg1, void* arg2);
   1225 
   1226 // Software interrupt instructions are used by the simulator to call into the
   1227 // C-based V8 runtime.
   1228 void Simulator::SoftwareInterrupt(Instruction* instr) {
   1229   int svc = instr->SvcValue();
   1230   switch (svc) {
   1231     case kCallRtRedirected: {
   1232       // Check if stack is aligned. Error if not aligned is reported below to
   1233       // include information on the function called.
   1234       bool stack_aligned =
   1235           (get_register(sp) & (::v8::internal::FLAG_sim_stack_alignment - 1)) ==
   1236           0;
   1237       Redirection* redirection = Redirection::FromSwiInstruction(instr);
   1238       const int kArgCount = 6;
   1239       int arg0_regnum = 3;
   1240 #if !ABI_RETURNS_OBJECT_PAIRS_IN_REGS
   1241       intptr_t result_buffer = 0;
   1242       if (redirection->type() == ExternalReference::BUILTIN_OBJECTPAIR_CALL) {
   1243         result_buffer = get_register(r3);
   1244         arg0_regnum++;
   1245       }
   1246 #endif
   1247       intptr_t arg[kArgCount];
   1248       for (int i = 0; i < kArgCount; i++) {
   1249         arg[i] = get_register(arg0_regnum + i);
   1250       }
   1251       bool fp_call =
   1252           (redirection->type() == ExternalReference::BUILTIN_FP_FP_CALL) ||
   1253           (redirection->type() == ExternalReference::BUILTIN_COMPARE_CALL) ||
   1254           (redirection->type() == ExternalReference::BUILTIN_FP_CALL) ||
   1255           (redirection->type() == ExternalReference::BUILTIN_FP_INT_CALL);
   1256       // This is dodgy but it works because the C entry stubs are never moved.
   1257       // See comment in codegen-arm.cc and bug 1242173.
   1258       intptr_t saved_lr = special_reg_lr_;
   1259       intptr_t external =
   1260           reinterpret_cast<intptr_t>(redirection->external_function());
   1261       if (fp_call) {
   1262         double dval0, dval1;  // one or two double parameters
   1263         intptr_t ival;        // zero or one integer parameters
   1264         int iresult = 0;      // integer return value
   1265         double dresult = 0;   // double return value
   1266         GetFpArgs(&dval0, &dval1, &ival);
   1267         if (::v8::internal::FLAG_trace_sim || !stack_aligned) {
   1268           SimulatorRuntimeCall generic_target =
   1269               reinterpret_cast<SimulatorRuntimeCall>(external);
   1270           switch (redirection->type()) {
   1271             case ExternalReference::BUILTIN_FP_FP_CALL:
   1272             case ExternalReference::BUILTIN_COMPARE_CALL:
   1273               PrintF("Call to host function at %p with args %f, %f",
   1274                      FUNCTION_ADDR(generic_target), dval0, dval1);
   1275               break;
   1276             case ExternalReference::BUILTIN_FP_CALL:
   1277               PrintF("Call to host function at %p with arg %f",
   1278                      FUNCTION_ADDR(generic_target), dval0);
   1279               break;
   1280             case ExternalReference::BUILTIN_FP_INT_CALL:
   1281               PrintF("Call to host function at %p with args %f, %" V8PRIdPTR,
   1282                      FUNCTION_ADDR(generic_target), dval0, ival);
   1283               break;
   1284             default:
   1285               UNREACHABLE();
   1286               break;
   1287           }
   1288           if (!stack_aligned) {
   1289             PrintF(" with unaligned stack %08" V8PRIxPTR "\n",
   1290                    get_register(sp));
   1291           }
   1292           PrintF("\n");
   1293         }
   1294         CHECK(stack_aligned);
   1295         switch (redirection->type()) {
   1296           case ExternalReference::BUILTIN_COMPARE_CALL: {
   1297             SimulatorRuntimeCompareCall target =
   1298                 reinterpret_cast<SimulatorRuntimeCompareCall>(external);
   1299             iresult = target(dval0, dval1);
   1300             set_register(r3, iresult);
   1301             break;
   1302           }
   1303           case ExternalReference::BUILTIN_FP_FP_CALL: {
   1304             SimulatorRuntimeFPFPCall target =
   1305                 reinterpret_cast<SimulatorRuntimeFPFPCall>(external);
   1306             dresult = target(dval0, dval1);
   1307             SetFpResult(dresult);
   1308             break;
   1309           }
   1310           case ExternalReference::BUILTIN_FP_CALL: {
   1311             SimulatorRuntimeFPCall target =
   1312                 reinterpret_cast<SimulatorRuntimeFPCall>(external);
   1313             dresult = target(dval0);
   1314             SetFpResult(dresult);
   1315             break;
   1316           }
   1317           case ExternalReference::BUILTIN_FP_INT_CALL: {
   1318             SimulatorRuntimeFPIntCall target =
   1319                 reinterpret_cast<SimulatorRuntimeFPIntCall>(external);
   1320             dresult = target(dval0, ival);
   1321             SetFpResult(dresult);
   1322             break;
   1323           }
   1324           default:
   1325             UNREACHABLE();
   1326             break;
   1327         }
   1328         if (::v8::internal::FLAG_trace_sim || !stack_aligned) {
   1329           switch (redirection->type()) {
   1330             case ExternalReference::BUILTIN_COMPARE_CALL:
   1331               PrintF("Returned %08x\n", iresult);
   1332               break;
   1333             case ExternalReference::BUILTIN_FP_FP_CALL:
   1334             case ExternalReference::BUILTIN_FP_CALL:
   1335             case ExternalReference::BUILTIN_FP_INT_CALL:
   1336               PrintF("Returned %f\n", dresult);
   1337               break;
   1338             default:
   1339               UNREACHABLE();
   1340               break;
   1341           }
   1342         }
   1343       } else if (redirection->type() == ExternalReference::DIRECT_API_CALL) {
   1344         // See callers of MacroAssembler::CallApiFunctionAndReturn for
   1345         // explanation of register usage.
   1346         if (::v8::internal::FLAG_trace_sim || !stack_aligned) {
   1347           PrintF("Call to host function at %p args %08" V8PRIxPTR,
   1348                  reinterpret_cast<void*>(external), arg[0]);
   1349           if (!stack_aligned) {
   1350             PrintF(" with unaligned stack %08" V8PRIxPTR "\n",
   1351                    get_register(sp));
   1352           }
   1353           PrintF("\n");
   1354         }
   1355         CHECK(stack_aligned);
   1356         SimulatorRuntimeDirectApiCall target =
   1357             reinterpret_cast<SimulatorRuntimeDirectApiCall>(external);
   1358         target(arg[0]);
   1359       } else if (redirection->type() == ExternalReference::PROFILING_API_CALL) {
   1360         // See callers of MacroAssembler::CallApiFunctionAndReturn for
   1361         // explanation of register usage.
   1362         if (::v8::internal::FLAG_trace_sim || !stack_aligned) {
   1363           PrintF("Call to host function at %p args %08" V8PRIxPTR
   1364                  " %08" V8PRIxPTR,
   1365                  reinterpret_cast<void*>(external), arg[0], arg[1]);
   1366           if (!stack_aligned) {
   1367             PrintF(" with unaligned stack %08" V8PRIxPTR "\n",
   1368                    get_register(sp));
   1369           }
   1370           PrintF("\n");
   1371         }
   1372         CHECK(stack_aligned);
   1373         SimulatorRuntimeProfilingApiCall target =
   1374             reinterpret_cast<SimulatorRuntimeProfilingApiCall>(external);
   1375         target(arg[0], Redirection::ReverseRedirection(arg[1]));
   1376       } else if (redirection->type() == ExternalReference::DIRECT_GETTER_CALL) {
   1377         // See callers of MacroAssembler::CallApiFunctionAndReturn for
   1378         // explanation of register usage.
   1379         if (::v8::internal::FLAG_trace_sim || !stack_aligned) {
   1380           PrintF("Call to host function at %p args %08" V8PRIxPTR
   1381                  " %08" V8PRIxPTR,
   1382                  reinterpret_cast<void*>(external), arg[0], arg[1]);
   1383           if (!stack_aligned) {
   1384             PrintF(" with unaligned stack %08" V8PRIxPTR "\n",
   1385                    get_register(sp));
   1386           }
   1387           PrintF("\n");
   1388         }
   1389         CHECK(stack_aligned);
   1390         SimulatorRuntimeDirectGetterCall target =
   1391             reinterpret_cast<SimulatorRuntimeDirectGetterCall>(external);
   1392 #if !ABI_PASSES_HANDLES_IN_REGS
   1393         arg[0] = *(reinterpret_cast<intptr_t*>(arg[0]));
   1394 #endif
   1395         target(arg[0], arg[1]);
   1396       } else if (redirection->type() ==
   1397                  ExternalReference::PROFILING_GETTER_CALL) {
   1398         if (::v8::internal::FLAG_trace_sim || !stack_aligned) {
   1399           PrintF("Call to host function at %p args %08" V8PRIxPTR
   1400                  " %08" V8PRIxPTR " %08" V8PRIxPTR,
   1401                  reinterpret_cast<void*>(external), arg[0], arg[1], arg[2]);
   1402           if (!stack_aligned) {
   1403             PrintF(" with unaligned stack %08" V8PRIxPTR "\n",
   1404                    get_register(sp));
   1405           }
   1406           PrintF("\n");
   1407         }
   1408         CHECK(stack_aligned);
   1409         SimulatorRuntimeProfilingGetterCall target =
   1410             reinterpret_cast<SimulatorRuntimeProfilingGetterCall>(external);
   1411 #if !ABI_PASSES_HANDLES_IN_REGS
   1412         arg[0] = *(reinterpret_cast<intptr_t*>(arg[0]));
   1413 #endif
   1414         target(arg[0], arg[1], Redirection::ReverseRedirection(arg[2]));
   1415       } else {
   1416         // builtin call.
   1417         if (::v8::internal::FLAG_trace_sim || !stack_aligned) {
   1418           SimulatorRuntimeCall target =
   1419               reinterpret_cast<SimulatorRuntimeCall>(external);
   1420           PrintF(
   1421               "Call to host function at %p,\n"
   1422               "\t\t\t\targs %08" V8PRIxPTR ", %08" V8PRIxPTR ", %08" V8PRIxPTR
   1423               ", %08" V8PRIxPTR ", %08" V8PRIxPTR ", %08" V8PRIxPTR,
   1424               FUNCTION_ADDR(target), arg[0], arg[1], arg[2], arg[3], arg[4],
   1425               arg[5]);
   1426           if (!stack_aligned) {
   1427             PrintF(" with unaligned stack %08" V8PRIxPTR "\n",
   1428                    get_register(sp));
   1429           }
   1430           PrintF("\n");
   1431         }
   1432         CHECK(stack_aligned);
   1433         DCHECK(redirection->type() == ExternalReference::BUILTIN_CALL);
   1434         SimulatorRuntimeCall target =
   1435             reinterpret_cast<SimulatorRuntimeCall>(external);
   1436         ObjectPair result =
   1437             target(arg[0], arg[1], arg[2], arg[3], arg[4], arg[5]);
   1438         intptr_t x;
   1439         intptr_t y;
   1440         decodeObjectPair(&result, &x, &y);
   1441         if (::v8::internal::FLAG_trace_sim) {
   1442           PrintF("Returned {%08" V8PRIxPTR ", %08" V8PRIxPTR "}\n", x, y);
   1443         }
   1444         set_register(r3, x);
   1445         set_register(r4, y);
   1446       }
   1447       set_pc(saved_lr);
   1448       break;
   1449     }
   1450     case kBreakpoint: {
   1451       PPCDebugger dbg(this);
   1452       dbg.Debug();
   1453       break;
   1454     }
   1455     // stop uses all codes greater than 1 << 23.
   1456     default: {
   1457       if (svc >= (1 << 23)) {
   1458         uint32_t code = svc & kStopCodeMask;
   1459         if (isWatchedStop(code)) {
   1460           IncreaseStopCounter(code);
   1461         }
   1462         // Stop if it is enabled, otherwise go on jumping over the stop
   1463         // and the message address.
   1464         if (isEnabledStop(code)) {
   1465           PPCDebugger dbg(this);
   1466           dbg.Stop(instr);
   1467         } else {
   1468           set_pc(get_pc() + Instruction::kInstrSize + kPointerSize);
   1469         }
   1470       } else {
   1471         // This is not a valid svc code.
   1472         UNREACHABLE();
   1473         break;
   1474       }
   1475     }
   1476   }
   1477 }
   1478 
   1479 
   1480 // Stop helper functions.
   1481 bool Simulator::isStopInstruction(Instruction* instr) {
   1482   return (instr->Bits(27, 24) == 0xF) && (instr->SvcValue() >= kStopCode);
   1483 }
   1484 
   1485 
   1486 bool Simulator::isWatchedStop(uint32_t code) {
   1487   DCHECK(code <= kMaxStopCode);
   1488   return code < kNumOfWatchedStops;
   1489 }
   1490 
   1491 
   1492 bool Simulator::isEnabledStop(uint32_t code) {
   1493   DCHECK(code <= kMaxStopCode);
   1494   // Unwatched stops are always enabled.
   1495   return !isWatchedStop(code) ||
   1496          !(watched_stops_[code].count & kStopDisabledBit);
   1497 }
   1498 
   1499 
   1500 void Simulator::EnableStop(uint32_t code) {
   1501   DCHECK(isWatchedStop(code));
   1502   if (!isEnabledStop(code)) {
   1503     watched_stops_[code].count &= ~kStopDisabledBit;
   1504   }
   1505 }
   1506 
   1507 
   1508 void Simulator::DisableStop(uint32_t code) {
   1509   DCHECK(isWatchedStop(code));
   1510   if (isEnabledStop(code)) {
   1511     watched_stops_[code].count |= kStopDisabledBit;
   1512   }
   1513 }
   1514 
   1515 
   1516 void Simulator::IncreaseStopCounter(uint32_t code) {
   1517   DCHECK(code <= kMaxStopCode);
   1518   DCHECK(isWatchedStop(code));
   1519   if ((watched_stops_[code].count & ~(1 << 31)) == 0x7fffffff) {
   1520     PrintF(
   1521         "Stop counter for code %i has overflowed.\n"
   1522         "Enabling this code and reseting the counter to 0.\n",
   1523         code);
   1524     watched_stops_[code].count = 0;
   1525     EnableStop(code);
   1526   } else {
   1527     watched_stops_[code].count++;
   1528   }
   1529 }
   1530 
   1531 
   1532 // Print a stop status.
   1533 void Simulator::PrintStopInfo(uint32_t code) {
   1534   DCHECK(code <= kMaxStopCode);
   1535   if (!isWatchedStop(code)) {
   1536     PrintF("Stop not watched.");
   1537   } else {
   1538     const char* state = isEnabledStop(code) ? "Enabled" : "Disabled";
   1539     int32_t count = watched_stops_[code].count & ~kStopDisabledBit;
   1540     // Don't print the state of unused breakpoints.
   1541     if (count != 0) {
   1542       if (watched_stops_[code].desc) {
   1543         PrintF("stop %i - 0x%x: \t%s, \tcounter = %i, \t%s\n", code, code,
   1544                state, count, watched_stops_[code].desc);
   1545       } else {
   1546         PrintF("stop %i - 0x%x: \t%s, \tcounter = %i\n", code, code, state,
   1547                count);
   1548       }
   1549     }
   1550   }
   1551 }
   1552 
   1553 
   1554 void Simulator::SetCR0(intptr_t result, bool setSO) {
   1555   int bf = 0;
   1556   if (result < 0) {
   1557     bf |= 0x80000000;
   1558   }
   1559   if (result > 0) {
   1560     bf |= 0x40000000;
   1561   }
   1562   if (result == 0) {
   1563     bf |= 0x20000000;
   1564   }
   1565   if (setSO) {
   1566     bf |= 0x10000000;
   1567   }
   1568   condition_reg_ = (condition_reg_ & ~0xF0000000) | bf;
   1569 }
   1570 
   1571 
   1572 void Simulator::ExecuteBranchConditional(Instruction* instr, BCType type) {
   1573   int bo = instr->Bits(25, 21) << 21;
   1574   int condition_bit = instr->Bits(20, 16);
   1575   int condition_mask = 0x80000000 >> condition_bit;
   1576   switch (bo) {
   1577     case DCBNZF:  // Decrement CTR; branch if CTR != 0 and condition false
   1578     case DCBEZF:  // Decrement CTR; branch if CTR == 0 and condition false
   1579       UNIMPLEMENTED();
   1580     case BF: {  // Branch if condition false
   1581       if (condition_reg_ & condition_mask) return;
   1582       break;
   1583     }
   1584     case DCBNZT:  // Decrement CTR; branch if CTR != 0 and condition true
   1585     case DCBEZT:  // Decrement CTR; branch if CTR == 0 and condition true
   1586       UNIMPLEMENTED();
   1587     case BT: {  // Branch if condition true
   1588       if (!(condition_reg_ & condition_mask)) return;
   1589       break;
   1590     }
   1591     case DCBNZ:  // Decrement CTR; branch if CTR != 0
   1592     case DCBEZ:  // Decrement CTR; branch if CTR == 0
   1593       special_reg_ctr_ -= 1;
   1594       if ((special_reg_ctr_ == 0) != (bo == DCBEZ)) return;
   1595       break;
   1596     case BA: {                   // Branch always
   1597       break;
   1598     }
   1599     default:
   1600       UNIMPLEMENTED();  // Invalid encoding
   1601   }
   1602 
   1603   intptr_t old_pc = get_pc();
   1604 
   1605   switch (type) {
   1606     case BC_OFFSET: {
   1607       int offset = (instr->Bits(15, 2) << 18) >> 16;
   1608       set_pc(old_pc + offset);
   1609       break;
   1610     }
   1611     case BC_LINK_REG:
   1612       set_pc(special_reg_lr_);
   1613       break;
   1614     case BC_CTR_REG:
   1615       set_pc(special_reg_ctr_);
   1616       break;
   1617   }
   1618 
   1619   if (instr->Bit(0) == 1) {  // LK flag set
   1620     special_reg_lr_ = old_pc + 4;
   1621   }
   1622 }
   1623 
   1624 
   1625 // Handle execution based on instruction types.
   1626 void Simulator::ExecuteExt1(Instruction* instr) {
   1627   switch (instr->Bits(10, 1) << 1) {
   1628     case MCRF:
   1629       UNIMPLEMENTED();  // Not used by V8.
   1630     case BCLRX:
   1631       ExecuteBranchConditional(instr, BC_LINK_REG);
   1632       break;
   1633     case BCCTRX:
   1634       ExecuteBranchConditional(instr, BC_CTR_REG);
   1635       break;
   1636     case CRNOR:
   1637     case RFI:
   1638     case CRANDC:
   1639       UNIMPLEMENTED();
   1640     case ISYNC: {
   1641       // todo - simulate isync
   1642       break;
   1643     }
   1644     case CRXOR: {
   1645       int bt = instr->Bits(25, 21);
   1646       int ba = instr->Bits(20, 16);
   1647       int bb = instr->Bits(15, 11);
   1648       int ba_val = ((0x80000000 >> ba) & condition_reg_) == 0 ? 0 : 1;
   1649       int bb_val = ((0x80000000 >> bb) & condition_reg_) == 0 ? 0 : 1;
   1650       int bt_val = ba_val ^ bb_val;
   1651       bt_val = bt_val << (31 - bt);  // shift bit to correct destination
   1652       condition_reg_ &= ~(0x80000000 >> bt);
   1653       condition_reg_ |= bt_val;
   1654       break;
   1655     }
   1656     case CREQV: {
   1657       int bt = instr->Bits(25, 21);
   1658       int ba = instr->Bits(20, 16);
   1659       int bb = instr->Bits(15, 11);
   1660       int ba_val = ((0x80000000 >> ba) & condition_reg_) == 0 ? 0 : 1;
   1661       int bb_val = ((0x80000000 >> bb) & condition_reg_) == 0 ? 0 : 1;
   1662       int bt_val = 1 - (ba_val ^ bb_val);
   1663       bt_val = bt_val << (31 - bt);  // shift bit to correct destination
   1664       condition_reg_ &= ~(0x80000000 >> bt);
   1665       condition_reg_ |= bt_val;
   1666       break;
   1667     }
   1668     case CRNAND:
   1669     case CRAND:
   1670     case CRORC:
   1671     case CROR:
   1672     default: {
   1673       UNIMPLEMENTED();  // Not used by V8.
   1674     }
   1675   }
   1676 }
   1677 
   1678 
   1679 bool Simulator::ExecuteExt2_10bit(Instruction* instr) {
   1680   bool found = true;
   1681 
   1682   int opcode = instr->Bits(10, 1) << 1;
   1683   switch (opcode) {
   1684     case SRWX: {
   1685       int rs = instr->RSValue();
   1686       int ra = instr->RAValue();
   1687       int rb = instr->RBValue();
   1688       uint32_t rs_val = get_register(rs);
   1689       uintptr_t rb_val = get_register(rb);
   1690       intptr_t result = rs_val >> (rb_val & 0x3f);
   1691       set_register(ra, result);
   1692       if (instr->Bit(0)) {  // RC bit set
   1693         SetCR0(result);
   1694       }
   1695       break;
   1696     }
   1697 #if V8_TARGET_ARCH_PPC64
   1698     case SRDX: {
   1699       int rs = instr->RSValue();
   1700       int ra = instr->RAValue();
   1701       int rb = instr->RBValue();
   1702       uintptr_t rs_val = get_register(rs);
   1703       uintptr_t rb_val = get_register(rb);
   1704       intptr_t result = rs_val >> (rb_val & 0x7f);
   1705       set_register(ra, result);
   1706       if (instr->Bit(0)) {  // RC bit set
   1707         SetCR0(result);
   1708       }
   1709       break;
   1710     }
   1711 #endif
   1712     case SRAW: {
   1713       int rs = instr->RSValue();
   1714       int ra = instr->RAValue();
   1715       int rb = instr->RBValue();
   1716       int32_t rs_val = get_register(rs);
   1717       intptr_t rb_val = get_register(rb);
   1718       intptr_t result = rs_val >> (rb_val & 0x3f);
   1719       set_register(ra, result);
   1720       if (instr->Bit(0)) {  // RC bit set
   1721         SetCR0(result);
   1722       }
   1723       break;
   1724     }
   1725 #if V8_TARGET_ARCH_PPC64
   1726     case SRAD: {
   1727       int rs = instr->RSValue();
   1728       int ra = instr->RAValue();
   1729       int rb = instr->RBValue();
   1730       intptr_t rs_val = get_register(rs);
   1731       intptr_t rb_val = get_register(rb);
   1732       intptr_t result = rs_val >> (rb_val & 0x7f);
   1733       set_register(ra, result);
   1734       if (instr->Bit(0)) {  // RC bit set
   1735         SetCR0(result);
   1736       }
   1737       break;
   1738     }
   1739 #endif
   1740     case SRAWIX: {
   1741       int ra = instr->RAValue();
   1742       int rs = instr->RSValue();
   1743       int sh = instr->Bits(15, 11);
   1744       int32_t rs_val = get_register(rs);
   1745       intptr_t result = rs_val >> sh;
   1746       set_register(ra, result);
   1747       if (instr->Bit(0)) {  // RC bit set
   1748         SetCR0(result);
   1749       }
   1750       break;
   1751     }
   1752 #if V8_TARGET_ARCH_PPC64
   1753     case EXTSW: {
   1754       const int shift = kBitsPerPointer - 32;
   1755       int ra = instr->RAValue();
   1756       int rs = instr->RSValue();
   1757       intptr_t rs_val = get_register(rs);
   1758       intptr_t ra_val = (rs_val << shift) >> shift;
   1759       set_register(ra, ra_val);
   1760       if (instr->Bit(0)) {  // RC bit set
   1761         SetCR0(ra_val);
   1762       }
   1763       break;
   1764     }
   1765 #endif
   1766     case EXTSH: {
   1767       const int shift = kBitsPerPointer - 16;
   1768       int ra = instr->RAValue();
   1769       int rs = instr->RSValue();
   1770       intptr_t rs_val = get_register(rs);
   1771       intptr_t ra_val = (rs_val << shift) >> shift;
   1772       set_register(ra, ra_val);
   1773       if (instr->Bit(0)) {  // RC bit set
   1774         SetCR0(ra_val);
   1775       }
   1776       break;
   1777     }
   1778     case EXTSB: {
   1779       const int shift = kBitsPerPointer - 8;
   1780       int ra = instr->RAValue();
   1781       int rs = instr->RSValue();
   1782       intptr_t rs_val = get_register(rs);
   1783       intptr_t ra_val = (rs_val << shift) >> shift;
   1784       set_register(ra, ra_val);
   1785       if (instr->Bit(0)) {  // RC bit set
   1786         SetCR0(ra_val);
   1787       }
   1788       break;
   1789     }
   1790     case LFSUX:
   1791     case LFSX: {
   1792       int frt = instr->RTValue();
   1793       int ra = instr->RAValue();
   1794       int rb = instr->RBValue();
   1795       intptr_t ra_val = ra == 0 ? 0 : get_register(ra);
   1796       intptr_t rb_val = get_register(rb);
   1797       int32_t val = ReadW(ra_val + rb_val, instr);
   1798       float* fptr = reinterpret_cast<float*>(&val);
   1799       set_d_register_from_double(frt, static_cast<double>(*fptr));
   1800       if (opcode == LFSUX) {
   1801         DCHECK(ra != 0);
   1802         set_register(ra, ra_val + rb_val);
   1803       }
   1804       break;
   1805     }
   1806     case LFDUX:
   1807     case LFDX: {
   1808       int frt = instr->RTValue();
   1809       int ra = instr->RAValue();
   1810       int rb = instr->RBValue();
   1811       intptr_t ra_val = ra == 0 ? 0 : get_register(ra);
   1812       intptr_t rb_val = get_register(rb);
   1813       int64_t* dptr = reinterpret_cast<int64_t*>(ReadDW(ra_val + rb_val));
   1814       set_d_register(frt, *dptr);
   1815       if (opcode == LFDUX) {
   1816         DCHECK(ra != 0);
   1817         set_register(ra, ra_val + rb_val);
   1818       }
   1819       break;
   1820     }
   1821     case STFSUX: {
   1822       case STFSX:
   1823         int frs = instr->RSValue();
   1824         int ra = instr->RAValue();
   1825         int rb = instr->RBValue();
   1826         intptr_t ra_val = ra == 0 ? 0 : get_register(ra);
   1827         intptr_t rb_val = get_register(rb);
   1828         float frs_val = static_cast<float>(get_double_from_d_register(frs));
   1829         int32_t* p = reinterpret_cast<int32_t*>(&frs_val);
   1830         WriteW(ra_val + rb_val, *p, instr);
   1831         if (opcode == STFSUX) {
   1832           DCHECK(ra != 0);
   1833           set_register(ra, ra_val + rb_val);
   1834         }
   1835         break;
   1836     }
   1837     case STFDUX: {
   1838       case STFDX:
   1839         int frs = instr->RSValue();
   1840         int ra = instr->RAValue();
   1841         int rb = instr->RBValue();
   1842         intptr_t ra_val = ra == 0 ? 0 : get_register(ra);
   1843         intptr_t rb_val = get_register(rb);
   1844         int64_t frs_val = get_d_register(frs);
   1845         WriteDW(ra_val + rb_val, frs_val);
   1846         if (opcode == STFDUX) {
   1847           DCHECK(ra != 0);
   1848           set_register(ra, ra_val + rb_val);
   1849         }
   1850         break;
   1851     }
   1852     case POPCNTW: {
   1853       int rs = instr->RSValue();
   1854       int ra = instr->RAValue();
   1855       uintptr_t rs_val = get_register(rs);
   1856       uintptr_t count = 0;
   1857       int n = 0;
   1858       uintptr_t bit = 0x80000000;
   1859       for (; n < 32; n++) {
   1860         if (bit & rs_val) count++;
   1861         bit >>= 1;
   1862       }
   1863       set_register(ra, count);
   1864       break;
   1865     }
   1866 #if V8_TARGET_ARCH_PPC64
   1867     case POPCNTD: {
   1868       int rs = instr->RSValue();
   1869       int ra = instr->RAValue();
   1870       uintptr_t rs_val = get_register(rs);
   1871       uintptr_t count = 0;
   1872       int n = 0;
   1873       uintptr_t bit = 0x8000000000000000UL;
   1874       for (; n < 64; n++) {
   1875         if (bit & rs_val) count++;
   1876         bit >>= 1;
   1877       }
   1878       set_register(ra, count);
   1879       break;
   1880     }
   1881 #endif
   1882     case SYNC: {
   1883       // todo - simulate sync
   1884       break;
   1885     }
   1886     case ICBI: {
   1887       // todo - simulate icbi
   1888       break;
   1889     }
   1890     default: {
   1891       found = false;
   1892       break;
   1893     }
   1894   }
   1895 
   1896   if (found) return found;
   1897 
   1898   found = true;
   1899   opcode = instr->Bits(10, 2) << 2;
   1900   switch (opcode) {
   1901     case SRADIX: {
   1902       int ra = instr->RAValue();
   1903       int rs = instr->RSValue();
   1904       int sh = (instr->Bits(15, 11) | (instr->Bit(1) << 5));
   1905       intptr_t rs_val = get_register(rs);
   1906       intptr_t result = rs_val >> sh;
   1907       set_register(ra, result);
   1908       if (instr->Bit(0)) {  // RC bit set
   1909         SetCR0(result);
   1910       }
   1911       break;
   1912     }
   1913     default: {
   1914       found = false;
   1915       break;
   1916     }
   1917   }
   1918 
   1919   return found;
   1920 }
   1921 
   1922 
   1923 bool Simulator::ExecuteExt2_9bit_part1(Instruction* instr) {
   1924   bool found = true;
   1925 
   1926   int opcode = instr->Bits(9, 1) << 1;
   1927   switch (opcode) {
   1928     case TW: {
   1929       // used for call redirection in simulation mode
   1930       SoftwareInterrupt(instr);
   1931       break;
   1932     }
   1933     case CMP: {
   1934       int ra = instr->RAValue();
   1935       int rb = instr->RBValue();
   1936       int cr = instr->Bits(25, 23);
   1937       uint32_t bf = 0;
   1938 #if V8_TARGET_ARCH_PPC64
   1939       int L = instr->Bit(21);
   1940       if (L) {
   1941 #endif
   1942         intptr_t ra_val = get_register(ra);
   1943         intptr_t rb_val = get_register(rb);
   1944         if (ra_val < rb_val) {
   1945           bf |= 0x80000000;
   1946         }
   1947         if (ra_val > rb_val) {
   1948           bf |= 0x40000000;
   1949         }
   1950         if (ra_val == rb_val) {
   1951           bf |= 0x20000000;
   1952         }
   1953 #if V8_TARGET_ARCH_PPC64
   1954       } else {
   1955         int32_t ra_val = get_register(ra);
   1956         int32_t rb_val = get_register(rb);
   1957         if (ra_val < rb_val) {
   1958           bf |= 0x80000000;
   1959         }
   1960         if (ra_val > rb_val) {
   1961           bf |= 0x40000000;
   1962         }
   1963         if (ra_val == rb_val) {
   1964           bf |= 0x20000000;
   1965         }
   1966       }
   1967 #endif
   1968       uint32_t condition_mask = 0xF0000000U >> (cr * 4);
   1969       uint32_t condition = bf >> (cr * 4);
   1970       condition_reg_ = (condition_reg_ & ~condition_mask) | condition;
   1971       break;
   1972     }
   1973     case SUBFCX: {
   1974       int rt = instr->RTValue();
   1975       int ra = instr->RAValue();
   1976       int rb = instr->RBValue();
   1977       // int oe = instr->Bit(10);
   1978       uintptr_t ra_val = get_register(ra);
   1979       uintptr_t rb_val = get_register(rb);
   1980       uintptr_t alu_out = ~ra_val + rb_val + 1;
   1981       set_register(rt, alu_out);
   1982       // If the sign of rb and alu_out don't match, carry = 0
   1983       if ((alu_out ^ rb_val) & 0x80000000) {
   1984         special_reg_xer_ &= ~0xF0000000;
   1985       } else {
   1986         special_reg_xer_ = (special_reg_xer_ & ~0xF0000000) | 0x20000000;
   1987       }
   1988       if (instr->Bit(0)) {  // RC bit set
   1989         SetCR0(alu_out);
   1990       }
   1991       // todo - handle OE bit
   1992       break;
   1993     }
   1994     case ADDCX: {
   1995       int rt = instr->RTValue();
   1996       int ra = instr->RAValue();
   1997       int rb = instr->RBValue();
   1998       // int oe = instr->Bit(10);
   1999       uintptr_t ra_val = get_register(ra);
   2000       uintptr_t rb_val = get_register(rb);
   2001       uintptr_t alu_out = ra_val + rb_val;
   2002       // Check overflow
   2003       if (~ra_val < rb_val) {
   2004         special_reg_xer_ = (special_reg_xer_ & ~0xF0000000) | 0x20000000;
   2005       } else {
   2006         special_reg_xer_ &= ~0xF0000000;
   2007       }
   2008       set_register(rt, alu_out);
   2009       if (instr->Bit(0)) {  // RC bit set
   2010         SetCR0(static_cast<intptr_t>(alu_out));
   2011       }
   2012       // todo - handle OE bit
   2013       break;
   2014     }
   2015     case MULHWX: {
   2016       int rt = instr->RTValue();
   2017       int ra = instr->RAValue();
   2018       int rb = instr->RBValue();
   2019       int32_t ra_val = (get_register(ra) & 0xFFFFFFFF);
   2020       int32_t rb_val = (get_register(rb) & 0xFFFFFFFF);
   2021       int64_t alu_out = (int64_t)ra_val * (int64_t)rb_val;
   2022       alu_out >>= 32;
   2023       set_register(rt, alu_out);
   2024       if (instr->Bit(0)) {  // RC bit set
   2025         SetCR0(static_cast<intptr_t>(alu_out));
   2026       }
   2027       break;
   2028     }
   2029     case MULHWUX: {
   2030       int rt = instr->RTValue();
   2031       int ra = instr->RAValue();
   2032       int rb = instr->RBValue();
   2033       uint32_t ra_val = (get_register(ra) & 0xFFFFFFFF);
   2034       uint32_t rb_val = (get_register(rb) & 0xFFFFFFFF);
   2035       uint64_t alu_out = (uint64_t)ra_val * (uint64_t)rb_val;
   2036       alu_out >>= 32;
   2037       set_register(rt, alu_out);
   2038       if (instr->Bit(0)) {  // RC bit set
   2039         SetCR0(static_cast<intptr_t>(alu_out));
   2040       }
   2041       break;
   2042     }
   2043     case NEGX: {
   2044       int rt = instr->RTValue();
   2045       int ra = instr->RAValue();
   2046       intptr_t ra_val = get_register(ra);
   2047       intptr_t alu_out = 1 + ~ra_val;
   2048 #if V8_TARGET_ARCH_PPC64
   2049       intptr_t one = 1;  // work-around gcc
   2050       intptr_t kOverflowVal = (one << 63);
   2051 #else
   2052       intptr_t kOverflowVal = kMinInt;
   2053 #endif
   2054       set_register(rt, alu_out);
   2055       if (instr->Bit(10)) {  // OE bit set
   2056         if (ra_val == kOverflowVal) {
   2057           special_reg_xer_ |= 0xC0000000;  // set SO,OV
   2058         } else {
   2059           special_reg_xer_ &= ~0x40000000;  // clear OV
   2060         }
   2061       }
   2062       if (instr->Bit(0)) {  // RC bit set
   2063         bool setSO = (special_reg_xer_ & 0x80000000);
   2064         SetCR0(alu_out, setSO);
   2065       }
   2066       break;
   2067     }
   2068     case SLWX: {
   2069       int rs = instr->RSValue();
   2070       int ra = instr->RAValue();
   2071       int rb = instr->RBValue();
   2072       uint32_t rs_val = get_register(rs);
   2073       uintptr_t rb_val = get_register(rb);
   2074       uint32_t result = rs_val << (rb_val & 0x3f);
   2075       set_register(ra, result);
   2076       if (instr->Bit(0)) {  // RC bit set
   2077         SetCR0(result);
   2078       }
   2079       break;
   2080     }
   2081 #if V8_TARGET_ARCH_PPC64
   2082     case SLDX: {
   2083       int rs = instr->RSValue();
   2084       int ra = instr->RAValue();
   2085       int rb = instr->RBValue();
   2086       uintptr_t rs_val = get_register(rs);
   2087       uintptr_t rb_val = get_register(rb);
   2088       uintptr_t result = rs_val << (rb_val & 0x7f);
   2089       set_register(ra, result);
   2090       if (instr->Bit(0)) {  // RC bit set
   2091         SetCR0(result);
   2092       }
   2093       break;
   2094     }
   2095     case MFVSRD: {
   2096       DCHECK(!instr->Bit(0));
   2097       int frt = instr->RTValue();
   2098       int ra = instr->RAValue();
   2099       int64_t frt_val = get_d_register(frt);
   2100       set_register(ra, frt_val);
   2101       break;
   2102     }
   2103     case MFVSRWZ: {
   2104       DCHECK(!instr->Bit(0));
   2105       int frt = instr->RTValue();
   2106       int ra = instr->RAValue();
   2107       int64_t frt_val = get_d_register(frt);
   2108       set_register(ra, static_cast<uint32_t>(frt_val));
   2109       break;
   2110     }
   2111     case MTVSRD: {
   2112       DCHECK(!instr->Bit(0));
   2113       int frt = instr->RTValue();
   2114       int ra = instr->RAValue();
   2115       int64_t ra_val = get_register(ra);
   2116       set_d_register(frt, ra_val);
   2117       break;
   2118     }
   2119     case MTVSRWA: {
   2120       DCHECK(!instr->Bit(0));
   2121       int frt = instr->RTValue();
   2122       int ra = instr->RAValue();
   2123       int64_t ra_val = static_cast<int32_t>(get_register(ra));
   2124       set_d_register(frt, ra_val);
   2125       break;
   2126     }
   2127     case MTVSRWZ: {
   2128       DCHECK(!instr->Bit(0));
   2129       int frt = instr->RTValue();
   2130       int ra = instr->RAValue();
   2131       uint64_t ra_val = static_cast<uint32_t>(get_register(ra));
   2132       set_d_register(frt, ra_val);
   2133       break;
   2134     }
   2135 #endif
   2136     default: {
   2137       found = false;
   2138       break;
   2139     }
   2140   }
   2141 
   2142   return found;
   2143 }
   2144 
   2145 
   2146 bool Simulator::ExecuteExt2_9bit_part2(Instruction* instr) {
   2147   bool found = true;
   2148   int opcode = instr->Bits(9, 1) << 1;
   2149   switch (opcode) {
   2150     case CNTLZWX: {
   2151       int rs = instr->RSValue();
   2152       int ra = instr->RAValue();
   2153       uintptr_t rs_val = get_register(rs);
   2154       uintptr_t count = 0;
   2155       int n = 0;
   2156       uintptr_t bit = 0x80000000;
   2157       for (; n < 32; n++) {
   2158         if (bit & rs_val) break;
   2159         count++;
   2160         bit >>= 1;
   2161       }
   2162       set_register(ra, count);
   2163       if (instr->Bit(0)) {  // RC Bit set
   2164         int bf = 0;
   2165         if (count > 0) {
   2166           bf |= 0x40000000;
   2167         }
   2168         if (count == 0) {
   2169           bf |= 0x20000000;
   2170         }
   2171         condition_reg_ = (condition_reg_ & ~0xF0000000) | bf;
   2172       }
   2173       break;
   2174     }
   2175 #if V8_TARGET_ARCH_PPC64
   2176     case CNTLZDX: {
   2177       int rs = instr->RSValue();
   2178       int ra = instr->RAValue();
   2179       uintptr_t rs_val = get_register(rs);
   2180       uintptr_t count = 0;
   2181       int n = 0;
   2182       uintptr_t bit = 0x8000000000000000UL;
   2183       for (; n < 64; n++) {
   2184         if (bit & rs_val) break;
   2185         count++;
   2186         bit >>= 1;
   2187       }
   2188       set_register(ra, count);
   2189       if (instr->Bit(0)) {  // RC Bit set
   2190         int bf = 0;
   2191         if (count > 0) {
   2192           bf |= 0x40000000;
   2193         }
   2194         if (count == 0) {
   2195           bf |= 0x20000000;
   2196         }
   2197         condition_reg_ = (condition_reg_ & ~0xF0000000) | bf;
   2198       }
   2199       break;
   2200     }
   2201 #endif
   2202     case ANDX: {
   2203       int rs = instr->RSValue();
   2204       int ra = instr->RAValue();
   2205       int rb = instr->RBValue();
   2206       intptr_t rs_val = get_register(rs);
   2207       intptr_t rb_val = get_register(rb);
   2208       intptr_t alu_out = rs_val & rb_val;
   2209       set_register(ra, alu_out);
   2210       if (instr->Bit(0)) {  // RC Bit set
   2211         SetCR0(alu_out);
   2212       }
   2213       break;
   2214     }
   2215     case ANDCX: {
   2216       int rs = instr->RSValue();
   2217       int ra = instr->RAValue();
   2218       int rb = instr->RBValue();
   2219       intptr_t rs_val = get_register(rs);
   2220       intptr_t rb_val = get_register(rb);
   2221       intptr_t alu_out = rs_val & ~rb_val;
   2222       set_register(ra, alu_out);
   2223       if (instr->Bit(0)) {  // RC Bit set
   2224         SetCR0(alu_out);
   2225       }
   2226       break;
   2227     }
   2228     case CMPL: {
   2229       int ra = instr->RAValue();
   2230       int rb = instr->RBValue();
   2231       int cr = instr->Bits(25, 23);
   2232       uint32_t bf = 0;
   2233 #if V8_TARGET_ARCH_PPC64
   2234       int L = instr->Bit(21);
   2235       if (L) {
   2236 #endif
   2237         uintptr_t ra_val = get_register(ra);
   2238         uintptr_t rb_val = get_register(rb);
   2239         if (ra_val < rb_val) {
   2240           bf |= 0x80000000;
   2241         }
   2242         if (ra_val > rb_val) {
   2243           bf |= 0x40000000;
   2244         }
   2245         if (ra_val == rb_val) {
   2246           bf |= 0x20000000;
   2247         }
   2248 #if V8_TARGET_ARCH_PPC64
   2249       } else {
   2250         uint32_t ra_val = get_register(ra);
   2251         uint32_t rb_val = get_register(rb);
   2252         if (ra_val < rb_val) {
   2253           bf |= 0x80000000;
   2254         }
   2255         if (ra_val > rb_val) {
   2256           bf |= 0x40000000;
   2257         }
   2258         if (ra_val == rb_val) {
   2259           bf |= 0x20000000;
   2260         }
   2261       }
   2262 #endif
   2263       uint32_t condition_mask = 0xF0000000U >> (cr * 4);
   2264       uint32_t condition = bf >> (cr * 4);
   2265       condition_reg_ = (condition_reg_ & ~condition_mask) | condition;
   2266       break;
   2267     }
   2268     case SUBFX: {
   2269       int rt = instr->RTValue();
   2270       int ra = instr->RAValue();
   2271       int rb = instr->RBValue();
   2272       // int oe = instr->Bit(10);
   2273       intptr_t ra_val = get_register(ra);
   2274       intptr_t rb_val = get_register(rb);
   2275       intptr_t alu_out = rb_val - ra_val;
   2276       // todo - figure out underflow
   2277       set_register(rt, alu_out);
   2278       if (instr->Bit(0)) {  // RC Bit set
   2279         SetCR0(alu_out);
   2280       }
   2281       // todo - handle OE bit
   2282       break;
   2283     }
   2284     case ADDZEX: {
   2285       int rt = instr->RTValue();
   2286       int ra = instr->RAValue();
   2287       intptr_t ra_val = get_register(ra);
   2288       if (special_reg_xer_ & 0x20000000) {
   2289         ra_val += 1;
   2290       }
   2291       set_register(rt, ra_val);
   2292       if (instr->Bit(0)) {  // RC bit set
   2293         SetCR0(ra_val);
   2294       }
   2295       // todo - handle OE bit
   2296       break;
   2297     }
   2298     case NORX: {
   2299       int rs = instr->RSValue();
   2300       int ra = instr->RAValue();
   2301       int rb = instr->RBValue();
   2302       intptr_t rs_val = get_register(rs);
   2303       intptr_t rb_val = get_register(rb);
   2304       intptr_t alu_out = ~(rs_val | rb_val);
   2305       set_register(ra, alu_out);
   2306       if (instr->Bit(0)) {  // RC bit set
   2307         SetCR0(alu_out);
   2308       }
   2309       break;
   2310     }
   2311     case MULLW: {
   2312       int rt = instr->RTValue();
   2313       int ra = instr->RAValue();
   2314       int rb = instr->RBValue();
   2315       int32_t ra_val = (get_register(ra) & 0xFFFFFFFF);
   2316       int32_t rb_val = (get_register(rb) & 0xFFFFFFFF);
   2317       int32_t alu_out = ra_val * rb_val;
   2318       set_register(rt, alu_out);
   2319       if (instr->Bit(0)) {  // RC bit set
   2320         SetCR0(alu_out);
   2321       }
   2322       // todo - handle OE bit
   2323       break;
   2324     }
   2325 #if V8_TARGET_ARCH_PPC64
   2326     case MULLD: {
   2327       int rt = instr->RTValue();
   2328       int ra = instr->RAValue();
   2329       int rb = instr->RBValue();
   2330       int64_t ra_val = get_register(ra);
   2331       int64_t rb_val = get_register(rb);
   2332       int64_t alu_out = ra_val * rb_val;
   2333       set_register(rt, alu_out);
   2334       if (instr->Bit(0)) {  // RC bit set
   2335         SetCR0(alu_out);
   2336       }
   2337       // todo - handle OE bit
   2338       break;
   2339     }
   2340 #endif
   2341     case DIVW: {
   2342       int rt = instr->RTValue();
   2343       int ra = instr->RAValue();
   2344       int rb = instr->RBValue();
   2345       int32_t ra_val = get_register(ra);
   2346       int32_t rb_val = get_register(rb);
   2347       bool overflow = (ra_val == kMinInt && rb_val == -1);
   2348       // result is undefined if divisor is zero or if operation
   2349       // is 0x80000000 / -1.
   2350       int32_t alu_out = (rb_val == 0 || overflow) ? -1 : ra_val / rb_val;
   2351       set_register(rt, alu_out);
   2352       if (instr->Bit(10)) {  // OE bit set
   2353         if (overflow) {
   2354           special_reg_xer_ |= 0xC0000000;  // set SO,OV
   2355         } else {
   2356           special_reg_xer_ &= ~0x40000000;  // clear OV
   2357         }
   2358       }
   2359       if (instr->Bit(0)) {  // RC bit set
   2360         bool setSO = (special_reg_xer_ & 0x80000000);
   2361         SetCR0(alu_out, setSO);
   2362       }
   2363       break;
   2364     }
   2365     case DIVWU: {
   2366       int rt = instr->RTValue();
   2367       int ra = instr->RAValue();
   2368       int rb = instr->RBValue();
   2369       uint32_t ra_val = get_register(ra);
   2370       uint32_t rb_val = get_register(rb);
   2371       bool overflow = (rb_val == 0);
   2372       // result is undefined if divisor is zero
   2373       uint32_t alu_out = (overflow) ? -1 : ra_val / rb_val;
   2374       set_register(rt, alu_out);
   2375       if (instr->Bit(10)) {  // OE bit set
   2376         if (overflow) {
   2377           special_reg_xer_ |= 0xC0000000;  // set SO,OV
   2378         } else {
   2379           special_reg_xer_ &= ~0x40000000;  // clear OV
   2380         }
   2381       }
   2382       if (instr->Bit(0)) {  // RC bit set
   2383         bool setSO = (special_reg_xer_ & 0x80000000);
   2384         SetCR0(alu_out, setSO);
   2385       }
   2386       break;
   2387     }
   2388 #if V8_TARGET_ARCH_PPC64
   2389     case DIVD: {
   2390       int rt = instr->RTValue();
   2391       int ra = instr->RAValue();
   2392       int rb = instr->RBValue();
   2393       int64_t ra_val = get_register(ra);
   2394       int64_t rb_val = get_register(rb);
   2395       int64_t one = 1;  // work-around gcc
   2396       int64_t kMinLongLong = (one << 63);
   2397       // result is undefined if divisor is zero or if operation
   2398       // is 0x80000000_00000000 / -1.
   2399       int64_t alu_out =
   2400           (rb_val == 0 || (ra_val == kMinLongLong && rb_val == -1))
   2401               ? -1
   2402               : ra_val / rb_val;
   2403       set_register(rt, alu_out);
   2404       if (instr->Bit(0)) {  // RC bit set
   2405         SetCR0(alu_out);
   2406       }
   2407       // todo - handle OE bit
   2408       break;
   2409     }
   2410     case DIVDU: {
   2411       int rt = instr->RTValue();
   2412       int ra = instr->RAValue();
   2413       int rb = instr->RBValue();
   2414       uint64_t ra_val = get_register(ra);
   2415       uint64_t rb_val = get_register(rb);
   2416       // result is undefined if divisor is zero
   2417       uint64_t alu_out = (rb_val == 0) ? -1 : ra_val / rb_val;
   2418       set_register(rt, alu_out);
   2419       if (instr->Bit(0)) {  // RC bit set
   2420         SetCR0(alu_out);
   2421       }
   2422       // todo - handle OE bit
   2423       break;
   2424     }
   2425 #endif
   2426     case ADDX: {
   2427       int rt = instr->RTValue();
   2428       int ra = instr->RAValue();
   2429       int rb = instr->RBValue();
   2430       // int oe = instr->Bit(10);
   2431       intptr_t ra_val = get_register(ra);
   2432       intptr_t rb_val = get_register(rb);
   2433       intptr_t alu_out = ra_val + rb_val;
   2434       set_register(rt, alu_out);
   2435       if (instr->Bit(0)) {  // RC bit set
   2436         SetCR0(alu_out);
   2437       }
   2438       // todo - handle OE bit
   2439       break;
   2440     }
   2441     case XORX: {
   2442       int rs = instr->RSValue();
   2443       int ra = instr->RAValue();
   2444       int rb = instr->RBValue();
   2445       intptr_t rs_val = get_register(rs);
   2446       intptr_t rb_val = get_register(rb);
   2447       intptr_t alu_out = rs_val ^ rb_val;
   2448       set_register(ra, alu_out);
   2449       if (instr->Bit(0)) {  // RC bit set
   2450         SetCR0(alu_out);
   2451       }
   2452       break;
   2453     }
   2454     case ORX: {
   2455       int rs = instr->RSValue();
   2456       int ra = instr->RAValue();
   2457       int rb = instr->RBValue();
   2458       intptr_t rs_val = get_register(rs);
   2459       intptr_t rb_val = get_register(rb);
   2460       intptr_t alu_out = rs_val | rb_val;
   2461       set_register(ra, alu_out);
   2462       if (instr->Bit(0)) {  // RC bit set
   2463         SetCR0(alu_out);
   2464       }
   2465       break;
   2466     }
   2467     case ORC: {
   2468       int rs = instr->RSValue();
   2469       int ra = instr->RAValue();
   2470       int rb = instr->RBValue();
   2471       intptr_t rs_val = get_register(rs);
   2472       intptr_t rb_val = get_register(rb);
   2473       intptr_t alu_out = rs_val | ~rb_val;
   2474       set_register(ra, alu_out);
   2475       if (instr->Bit(0)) {  // RC bit set
   2476         SetCR0(alu_out);
   2477       }
   2478       break;
   2479     }
   2480     case MFSPR: {
   2481       int rt = instr->RTValue();
   2482       int spr = instr->Bits(20, 11);
   2483       if (spr != 256) {
   2484         UNIMPLEMENTED();  // Only LRLR supported
   2485       }
   2486       set_register(rt, special_reg_lr_);
   2487       break;
   2488     }
   2489     case MTSPR: {
   2490       int rt = instr->RTValue();
   2491       intptr_t rt_val = get_register(rt);
   2492       int spr = instr->Bits(20, 11);
   2493       if (spr == 256) {
   2494         special_reg_lr_ = rt_val;
   2495       } else if (spr == 288) {
   2496         special_reg_ctr_ = rt_val;
   2497       } else if (spr == 32) {
   2498         special_reg_xer_ = rt_val;
   2499       } else {
   2500         UNIMPLEMENTED();  // Only LR supported
   2501       }
   2502       break;
   2503     }
   2504     case MFCR: {
   2505       int rt = instr->RTValue();
   2506       set_register(rt, condition_reg_);
   2507       break;
   2508     }
   2509     case STWUX:
   2510     case STWX: {
   2511       int rs = instr->RSValue();
   2512       int ra = instr->RAValue();
   2513       int rb = instr->RBValue();
   2514       intptr_t ra_val = ra == 0 ? 0 : get_register(ra);
   2515       int32_t rs_val = get_register(rs);
   2516       intptr_t rb_val = get_register(rb);
   2517       WriteW(ra_val + rb_val, rs_val, instr);
   2518       if (opcode == STWUX) {
   2519         DCHECK(ra != 0);
   2520         set_register(ra, ra_val + rb_val);
   2521       }
   2522       break;
   2523     }
   2524     case STBUX:
   2525     case STBX: {
   2526       int rs = instr->RSValue();
   2527       int ra = instr->RAValue();
   2528       int rb = instr->RBValue();
   2529       intptr_t ra_val = ra == 0 ? 0 : get_register(ra);
   2530       int8_t rs_val = get_register(rs);
   2531       intptr_t rb_val = get_register(rb);
   2532       WriteB(ra_val + rb_val, rs_val);
   2533       if (opcode == STBUX) {
   2534         DCHECK(ra != 0);
   2535         set_register(ra, ra_val + rb_val);
   2536       }
   2537       break;
   2538     }
   2539     case STHUX:
   2540     case STHX: {
   2541       int rs = instr->RSValue();
   2542       int ra = instr->RAValue();
   2543       int rb = instr->RBValue();
   2544       intptr_t ra_val = ra == 0 ? 0 : get_register(ra);
   2545       int16_t rs_val = get_register(rs);
   2546       intptr_t rb_val = get_register(rb);
   2547       WriteH(ra_val + rb_val, rs_val, instr);
   2548       if (opcode == STHUX) {
   2549         DCHECK(ra != 0);
   2550         set_register(ra, ra_val + rb_val);
   2551       }
   2552       break;
   2553     }
   2554     case LWZX:
   2555     case LWZUX: {
   2556       int rt = instr->RTValue();
   2557       int ra = instr->RAValue();
   2558       int rb = instr->RBValue();
   2559       intptr_t ra_val = ra == 0 ? 0 : get_register(ra);
   2560       intptr_t rb_val = get_register(rb);
   2561       set_register(rt, ReadWU(ra_val + rb_val, instr));
   2562       if (opcode == LWZUX) {
   2563         DCHECK(ra != 0 && ra != rt);
   2564         set_register(ra, ra_val + rb_val);
   2565       }
   2566       break;
   2567     }
   2568 #if V8_TARGET_ARCH_PPC64
   2569     case LWAX: {
   2570       int rt = instr->RTValue();
   2571       int ra = instr->RAValue();
   2572       int rb = instr->RBValue();
   2573       intptr_t ra_val = ra == 0 ? 0 : get_register(ra);
   2574       intptr_t rb_val = get_register(rb);
   2575       set_register(rt, ReadW(ra_val + rb_val, instr));
   2576       break;
   2577     }
   2578     case LDX:
   2579     case LDUX: {
   2580       int rt = instr->RTValue();
   2581       int ra = instr->RAValue();
   2582       int rb = instr->RBValue();
   2583       intptr_t ra_val = ra == 0 ? 0 : get_register(ra);
   2584       intptr_t rb_val = get_register(rb);
   2585       intptr_t* result = ReadDW(ra_val + rb_val);
   2586       set_register(rt, *result);
   2587       if (opcode == LDUX) {
   2588         DCHECK(ra != 0 && ra != rt);
   2589         set_register(ra, ra_val + rb_val);
   2590       }
   2591       break;
   2592     }
   2593     case STDX:
   2594     case STDUX: {
   2595       int rs = instr->RSValue();
   2596       int ra = instr->RAValue();
   2597       int rb = instr->RBValue();
   2598       intptr_t ra_val = ra == 0 ? 0 : get_register(ra);
   2599       intptr_t rs_val = get_register(rs);
   2600       intptr_t rb_val = get_register(rb);
   2601       WriteDW(ra_val + rb_val, rs_val);
   2602       if (opcode == STDUX) {
   2603         DCHECK(ra != 0);
   2604         set_register(ra, ra_val + rb_val);
   2605       }
   2606       break;
   2607     }
   2608 #endif
   2609     case LBZX:
   2610     case LBZUX: {
   2611       int rt = instr->RTValue();
   2612       int ra = instr->RAValue();
   2613       int rb = instr->RBValue();
   2614       intptr_t ra_val = ra == 0 ? 0 : get_register(ra);
   2615       intptr_t rb_val = get_register(rb);
   2616       set_register(rt, ReadBU(ra_val + rb_val) & 0xFF);
   2617       if (opcode == LBZUX) {
   2618         DCHECK(ra != 0 && ra != rt);
   2619         set_register(ra, ra_val + rb_val);
   2620       }
   2621       break;
   2622     }
   2623     case LHZX:
   2624     case LHZUX: {
   2625       int rt = instr->RTValue();
   2626       int ra = instr->RAValue();
   2627       int rb = instr->RBValue();
   2628       intptr_t ra_val = ra == 0 ? 0 : get_register(ra);
   2629       intptr_t rb_val = get_register(rb);
   2630       set_register(rt, ReadHU(ra_val + rb_val, instr) & 0xFFFF);
   2631       if (opcode == LHZUX) {
   2632         DCHECK(ra != 0 && ra != rt);
   2633         set_register(ra, ra_val + rb_val);
   2634       }
   2635       break;
   2636     }
   2637     case LHAX:
   2638     case LHAUX: {
   2639       int rt = instr->RTValue();
   2640       int ra = instr->RAValue();
   2641       int rb = instr->RBValue();
   2642       intptr_t ra_val = ra == 0 ? 0 : get_register(ra);
   2643       intptr_t rb_val = get_register(rb);
   2644       set_register(rt, ReadH(ra_val + rb_val, instr));
   2645       if (opcode == LHAUX) {
   2646         DCHECK(ra != 0 && ra != rt);
   2647         set_register(ra, ra_val + rb_val);
   2648       }
   2649       break;
   2650     }
   2651     case DCBF: {
   2652       // todo - simulate dcbf
   2653       break;
   2654     }
   2655     default: {
   2656       found = false;
   2657       break;
   2658     }
   2659   }
   2660 
   2661   return found;
   2662 }
   2663 
   2664 
   2665 void Simulator::ExecuteExt2_5bit(Instruction* instr) {
   2666   int opcode = instr->Bits(5, 1) << 1;
   2667   switch (opcode) {
   2668     case ISEL: {
   2669       int rt = instr->RTValue();
   2670       int ra = instr->RAValue();
   2671       int rb = instr->RBValue();
   2672       int condition_bit = instr->RCValue();
   2673       int condition_mask = 0x80000000 >> condition_bit;
   2674       intptr_t ra_val = (ra == 0) ? 0 : get_register(ra);
   2675       intptr_t rb_val = get_register(rb);
   2676       intptr_t value = (condition_reg_ & condition_mask) ? ra_val : rb_val;
   2677       set_register(rt, value);
   2678       break;
   2679     }
   2680     default: {
   2681       PrintF("Unimplemented: %08x\n", instr->InstructionBits());
   2682       UNIMPLEMENTED();  // Not used by V8.
   2683     }
   2684   }
   2685 }
   2686 
   2687 
   2688 void Simulator::ExecuteExt2(Instruction* instr) {
   2689   // Check first the 10-1 bit versions
   2690   if (ExecuteExt2_10bit(instr)) return;
   2691   // Now look at the lesser encodings
   2692   if (ExecuteExt2_9bit_part1(instr)) return;
   2693   if (ExecuteExt2_9bit_part2(instr)) return;
   2694   ExecuteExt2_5bit(instr);
   2695 }
   2696 
   2697 
   2698 void Simulator::ExecuteExt3(Instruction* instr) {
   2699   int opcode = instr->Bits(10, 1) << 1;
   2700   switch (opcode) {
   2701     case FCFID: {
   2702       // fcfids
   2703       int frt = instr->RTValue();
   2704       int frb = instr->RBValue();
   2705       int64_t frb_val = get_d_register(frb);
   2706       double frt_val = static_cast<float>(frb_val);
   2707       set_d_register_from_double(frt, frt_val);
   2708       return;
   2709     }
   2710     case FCFIDU: {
   2711       // fcfidus
   2712       int frt = instr->RTValue();
   2713       int frb = instr->RBValue();
   2714       uint64_t frb_val = get_d_register(frb);
   2715       double frt_val = static_cast<float>(frb_val);
   2716       set_d_register_from_double(frt, frt_val);
   2717       return;
   2718     }
   2719   }
   2720   UNIMPLEMENTED();  // Not used by V8.
   2721 }
   2722 
   2723 
   2724 void Simulator::ExecuteExt4(Instruction* instr) {
   2725   switch (instr->Bits(5, 1) << 1) {
   2726     case FDIV: {
   2727       int frt = instr->RTValue();
   2728       int fra = instr->RAValue();
   2729       int frb = instr->RBValue();
   2730       double fra_val = get_double_from_d_register(fra);
   2731       double frb_val = get_double_from_d_register(frb);
   2732       double frt_val = fra_val / frb_val;
   2733       set_d_register_from_double(frt, frt_val);
   2734       return;
   2735     }
   2736     case FSUB: {
   2737       int frt = instr->RTValue();
   2738       int fra = instr->RAValue();
   2739       int frb = instr->RBValue();
   2740       double fra_val = get_double_from_d_register(fra);
   2741       double frb_val = get_double_from_d_register(frb);
   2742       double frt_val = fra_val - frb_val;
   2743       set_d_register_from_double(frt, frt_val);
   2744       return;
   2745     }
   2746     case FADD: {
   2747       int frt = instr->RTValue();
   2748       int fra = instr->RAValue();
   2749       int frb = instr->RBValue();
   2750       double fra_val = get_double_from_d_register(fra);
   2751       double frb_val = get_double_from_d_register(frb);
   2752       double frt_val = fra_val + frb_val;
   2753       set_d_register_from_double(frt, frt_val);
   2754       return;
   2755     }
   2756     case FSQRT: {
   2757       lazily_initialize_fast_sqrt(isolate_);
   2758       int frt = instr->RTValue();
   2759       int frb = instr->RBValue();
   2760       double frb_val = get_double_from_d_register(frb);
   2761       double frt_val = fast_sqrt(frb_val, isolate_);
   2762       set_d_register_from_double(frt, frt_val);
   2763       return;
   2764     }
   2765     case FSEL: {
   2766       int frt = instr->RTValue();
   2767       int fra = instr->RAValue();
   2768       int frb = instr->RBValue();
   2769       int frc = instr->RCValue();
   2770       double fra_val = get_double_from_d_register(fra);
   2771       double frb_val = get_double_from_d_register(frb);
   2772       double frc_val = get_double_from_d_register(frc);
   2773       double frt_val = ((fra_val >= 0.0) ? frc_val : frb_val);
   2774       set_d_register_from_double(frt, frt_val);
   2775       return;
   2776     }
   2777     case FMUL: {
   2778       int frt = instr->RTValue();
   2779       int fra = instr->RAValue();
   2780       int frc = instr->RCValue();
   2781       double fra_val = get_double_from_d_register(fra);
   2782       double frc_val = get_double_from_d_register(frc);
   2783       double frt_val = fra_val * frc_val;
   2784       set_d_register_from_double(frt, frt_val);
   2785       return;
   2786     }
   2787     case FMSUB: {
   2788       int frt = instr->RTValue();
   2789       int fra = instr->RAValue();
   2790       int frb = instr->RBValue();
   2791       int frc = instr->RCValue();
   2792       double fra_val = get_double_from_d_register(fra);
   2793       double frb_val = get_double_from_d_register(frb);
   2794       double frc_val = get_double_from_d_register(frc);
   2795       double frt_val = (fra_val * frc_val) - frb_val;
   2796       set_d_register_from_double(frt, frt_val);
   2797       return;
   2798     }
   2799     case FMADD: {
   2800       int frt = instr->RTValue();
   2801       int fra = instr->RAValue();
   2802       int frb = instr->RBValue();
   2803       int frc = instr->RCValue();
   2804       double fra_val = get_double_from_d_register(fra);
   2805       double frb_val = get_double_from_d_register(frb);
   2806       double frc_val = get_double_from_d_register(frc);
   2807       double frt_val = (fra_val * frc_val) + frb_val;
   2808       set_d_register_from_double(frt, frt_val);
   2809       return;
   2810     }
   2811   }
   2812   int opcode = instr->Bits(10, 1) << 1;
   2813   switch (opcode) {
   2814     case FCMPU: {
   2815       int fra = instr->RAValue();
   2816       int frb = instr->RBValue();
   2817       double fra_val = get_double_from_d_register(fra);
   2818       double frb_val = get_double_from_d_register(frb);
   2819       int cr = instr->Bits(25, 23);
   2820       int bf = 0;
   2821       if (fra_val < frb_val) {
   2822         bf |= 0x80000000;
   2823       }
   2824       if (fra_val > frb_val) {
   2825         bf |= 0x40000000;
   2826       }
   2827       if (fra_val == frb_val) {
   2828         bf |= 0x20000000;
   2829       }
   2830       if (std::isunordered(fra_val, frb_val)) {
   2831         bf |= 0x10000000;
   2832       }
   2833       int condition_mask = 0xF0000000 >> (cr * 4);
   2834       int condition = bf >> (cr * 4);
   2835       condition_reg_ = (condition_reg_ & ~condition_mask) | condition;
   2836       return;
   2837     }
   2838     case FRIN: {
   2839       int frt = instr->RTValue();
   2840       int frb = instr->RBValue();
   2841       double frb_val = get_double_from_d_register(frb);
   2842       double frt_val = std::round(frb_val);
   2843       set_d_register_from_double(frt, frt_val);
   2844       if (instr->Bit(0)) {  // RC bit set
   2845                             //  UNIMPLEMENTED();
   2846       }
   2847       return;
   2848     }
   2849     case FRIZ: {
   2850       int frt = instr->RTValue();
   2851       int frb = instr->RBValue();
   2852       double frb_val = get_double_from_d_register(frb);
   2853       double frt_val = std::trunc(frb_val);
   2854       set_d_register_from_double(frt, frt_val);
   2855       if (instr->Bit(0)) {  // RC bit set
   2856                             //  UNIMPLEMENTED();
   2857       }
   2858       return;
   2859     }
   2860     case FRIP: {
   2861       int frt = instr->RTValue();
   2862       int frb = instr->RBValue();
   2863       double frb_val = get_double_from_d_register(frb);
   2864       double frt_val = std::ceil(frb_val);
   2865       set_d_register_from_double(frt, frt_val);
   2866       if (instr->Bit(0)) {  // RC bit set
   2867                             //  UNIMPLEMENTED();
   2868       }
   2869       return;
   2870     }
   2871     case FRIM: {
   2872       int frt = instr->RTValue();
   2873       int frb = instr->RBValue();
   2874       double frb_val = get_double_from_d_register(frb);
   2875       double frt_val = std::floor(frb_val);
   2876       set_d_register_from_double(frt, frt_val);
   2877       if (instr->Bit(0)) {  // RC bit set
   2878                             //  UNIMPLEMENTED();
   2879       }
   2880       return;
   2881     }
   2882     case FRSP: {
   2883       int frt = instr->RTValue();
   2884       int frb = instr->RBValue();
   2885       // frsp round 8-byte double-precision value to
   2886       // single-precision value
   2887       double frb_val = get_double_from_d_register(frb);
   2888       double frt_val = static_cast<float>(frb_val);
   2889       set_d_register_from_double(frt, frt_val);
   2890       if (instr->Bit(0)) {  // RC bit set
   2891                             //  UNIMPLEMENTED();
   2892       }
   2893       return;
   2894     }
   2895     case FCFID: {
   2896       int frt = instr->RTValue();
   2897       int frb = instr->RBValue();
   2898       int64_t frb_val = get_d_register(frb);
   2899       double frt_val = static_cast<double>(frb_val);
   2900       set_d_register_from_double(frt, frt_val);
   2901       return;
   2902     }
   2903     case FCFIDU: {
   2904       int frt = instr->RTValue();
   2905       int frb = instr->RBValue();
   2906       uint64_t frb_val = get_d_register(frb);
   2907       double frt_val = static_cast<double>(frb_val);
   2908       set_d_register_from_double(frt, frt_val);
   2909       return;
   2910     }
   2911     case FCTID:
   2912     case FCTIDZ: {
   2913       int frt = instr->RTValue();
   2914       int frb = instr->RBValue();
   2915       double frb_val = get_double_from_d_register(frb);
   2916       int mode = (opcode == FCTIDZ) ? kRoundToZero
   2917                                     : (fp_condition_reg_ & kFPRoundingModeMask);
   2918       int64_t frt_val;
   2919       int64_t one = 1;  // work-around gcc
   2920       int64_t kMinVal = (one << 63);
   2921       int64_t kMaxVal = kMinVal - 1;
   2922       bool invalid_convert = false;
   2923 
   2924       if (std::isnan(frb_val)) {
   2925         frt_val = kMinVal;
   2926         invalid_convert = true;
   2927       } else {
   2928         switch (mode) {
   2929           case kRoundToZero:
   2930             frb_val = std::trunc(frb_val);
   2931             break;
   2932           case kRoundToPlusInf:
   2933             frb_val = std::ceil(frb_val);
   2934             break;
   2935           case kRoundToMinusInf:
   2936             frb_val = std::floor(frb_val);
   2937             break;
   2938           default:
   2939             UNIMPLEMENTED();  // Not used by V8.
   2940             break;
   2941         }
   2942         if (frb_val < static_cast<double>(kMinVal)) {
   2943           frt_val = kMinVal;
   2944           invalid_convert = true;
   2945         } else if (frb_val >= static_cast<double>(kMaxVal)) {
   2946           frt_val = kMaxVal;
   2947           invalid_convert = true;
   2948         } else {
   2949           frt_val = (int64_t)frb_val;
   2950         }
   2951       }
   2952       set_d_register(frt, frt_val);
   2953       if (invalid_convert) SetFPSCR(VXCVI);
   2954       return;
   2955     }
   2956     case FCTIDU:
   2957     case FCTIDUZ: {
   2958       int frt = instr->RTValue();
   2959       int frb = instr->RBValue();
   2960       double frb_val = get_double_from_d_register(frb);
   2961       int mode = (opcode == FCTIDUZ)
   2962                      ? kRoundToZero
   2963                      : (fp_condition_reg_ & kFPRoundingModeMask);
   2964       uint64_t frt_val;
   2965       uint64_t kMinVal = 0;
   2966       uint64_t kMaxVal = kMinVal - 1;
   2967       bool invalid_convert = false;
   2968 
   2969       if (std::isnan(frb_val)) {
   2970         frt_val = kMinVal;
   2971         invalid_convert = true;
   2972       } else {
   2973         switch (mode) {
   2974           case kRoundToZero:
   2975             frb_val = std::trunc(frb_val);
   2976             break;
   2977           case kRoundToPlusInf:
   2978             frb_val = std::ceil(frb_val);
   2979             break;
   2980           case kRoundToMinusInf:
   2981             frb_val = std::floor(frb_val);
   2982             break;
   2983           default:
   2984             UNIMPLEMENTED();  // Not used by V8.
   2985             break;
   2986         }
   2987         if (frb_val < static_cast<double>(kMinVal)) {
   2988           frt_val = kMinVal;
   2989           invalid_convert = true;
   2990         } else if (frb_val >= static_cast<double>(kMaxVal)) {
   2991           frt_val = kMaxVal;
   2992           invalid_convert = true;
   2993         } else {
   2994           frt_val = (uint64_t)frb_val;
   2995         }
   2996       }
   2997       set_d_register(frt, frt_val);
   2998       if (invalid_convert) SetFPSCR(VXCVI);
   2999       return;
   3000     }
   3001     case FCTIW:
   3002     case FCTIWZ: {
   3003       int frt = instr->RTValue();
   3004       int frb = instr->RBValue();
   3005       double frb_val = get_double_from_d_register(frb);
   3006       int mode = (opcode == FCTIWZ) ? kRoundToZero
   3007                                     : (fp_condition_reg_ & kFPRoundingModeMask);
   3008       int64_t frt_val;
   3009       int64_t kMinVal = kMinInt;
   3010       int64_t kMaxVal = kMaxInt;
   3011 
   3012       if (std::isnan(frb_val)) {
   3013         frt_val = kMinVal;
   3014       } else {
   3015         switch (mode) {
   3016           case kRoundToZero:
   3017             frb_val = std::trunc(frb_val);
   3018             break;
   3019           case kRoundToPlusInf:
   3020             frb_val = std::ceil(frb_val);
   3021             break;
   3022           case kRoundToMinusInf:
   3023             frb_val = std::floor(frb_val);
   3024             break;
   3025           case kRoundToNearest: {
   3026             double orig = frb_val;
   3027             frb_val = lround(frb_val);
   3028             // Round to even if exactly halfway.  (lround rounds up)
   3029             if (std::fabs(frb_val - orig) == 0.5 && ((int64_t)frb_val % 2)) {
   3030               frb_val += ((frb_val > 0) ? -1.0 : 1.0);
   3031             }
   3032             break;
   3033           }
   3034           default:
   3035             UNIMPLEMENTED();  // Not used by V8.
   3036             break;
   3037         }
   3038         if (frb_val < kMinVal) {
   3039           frt_val = kMinVal;
   3040         } else if (frb_val > kMaxVal) {
   3041           frt_val = kMaxVal;
   3042         } else {
   3043           frt_val = (int64_t)frb_val;
   3044         }
   3045       }
   3046       set_d_register(frt, frt_val);
   3047       return;
   3048     }
   3049     case FNEG: {
   3050       int frt = instr->RTValue();
   3051       int frb = instr->RBValue();
   3052       double frb_val = get_double_from_d_register(frb);
   3053       double frt_val = -frb_val;
   3054       set_d_register_from_double(frt, frt_val);
   3055       return;
   3056     }
   3057     case FMR: {
   3058       int frt = instr->RTValue();
   3059       int frb = instr->RBValue();
   3060       int64_t frb_val = get_d_register(frb);
   3061       set_d_register(frt, frb_val);
   3062       return;
   3063     }
   3064     case MTFSFI: {
   3065       int bf = instr->Bits(25, 23);
   3066       int imm = instr->Bits(15, 12);
   3067       int fp_condition_mask = 0xF0000000 >> (bf * 4);
   3068       fp_condition_reg_ &= ~fp_condition_mask;
   3069       fp_condition_reg_ |= (imm << (28 - (bf * 4)));
   3070       if (instr->Bit(0)) {  // RC bit set
   3071         condition_reg_ &= 0xF0FFFFFF;
   3072         condition_reg_ |= (imm << 23);
   3073       }
   3074       return;
   3075     }
   3076     case MTFSF: {
   3077       int frb = instr->RBValue();
   3078       int64_t frb_dval = get_d_register(frb);
   3079       int32_t frb_ival = static_cast<int32_t>((frb_dval)&0xffffffff);
   3080       int l = instr->Bits(25, 25);
   3081       if (l == 1) {
   3082         fp_condition_reg_ = frb_ival;
   3083       } else {
   3084         UNIMPLEMENTED();
   3085       }
   3086       if (instr->Bit(0)) {  // RC bit set
   3087         UNIMPLEMENTED();
   3088         // int w = instr->Bits(16, 16);
   3089         // int flm = instr->Bits(24, 17);
   3090       }
   3091       return;
   3092     }
   3093     case MFFS: {
   3094       int frt = instr->RTValue();
   3095       int64_t lval = static_cast<int64_t>(fp_condition_reg_);
   3096       set_d_register(frt, lval);
   3097       return;
   3098     }
   3099     case MCRFS: {
   3100       int bf = instr->Bits(25, 23);
   3101       int bfa = instr->Bits(20, 18);
   3102       int cr_shift = (7 - bf) * CRWIDTH;
   3103       int fp_shift = (7 - bfa) * CRWIDTH;
   3104       int field_val = (fp_condition_reg_ >> fp_shift) & 0xf;
   3105       condition_reg_ &= ~(0x0f << cr_shift);
   3106       condition_reg_ |= (field_val << cr_shift);
   3107       // Clear copied exception bits
   3108       switch (bfa) {
   3109         case 5:
   3110           ClearFPSCR(VXSOFT);
   3111           ClearFPSCR(VXSQRT);
   3112           ClearFPSCR(VXCVI);
   3113           break;
   3114         default:
   3115           UNIMPLEMENTED();
   3116           break;
   3117       }
   3118       return;
   3119     }
   3120     case MTFSB0: {
   3121       int bt = instr->Bits(25, 21);
   3122       ClearFPSCR(bt);
   3123       if (instr->Bit(0)) {  // RC bit set
   3124         UNIMPLEMENTED();
   3125       }
   3126       return;
   3127     }
   3128     case MTFSB1: {
   3129       int bt = instr->Bits(25, 21);
   3130       SetFPSCR(bt);
   3131       if (instr->Bit(0)) {  // RC bit set
   3132         UNIMPLEMENTED();
   3133       }
   3134       return;
   3135     }
   3136     case FABS: {
   3137       int frt = instr->RTValue();
   3138       int frb = instr->RBValue();
   3139       double frb_val = get_double_from_d_register(frb);
   3140       double frt_val = std::fabs(frb_val);
   3141       set_d_register_from_double(frt, frt_val);
   3142       return;
   3143     }
   3144   }
   3145   UNIMPLEMENTED();  // Not used by V8.
   3146 }
   3147 
   3148 #if V8_TARGET_ARCH_PPC64
   3149 void Simulator::ExecuteExt5(Instruction* instr) {
   3150   switch (instr->Bits(4, 2) << 2) {
   3151     case RLDICL: {
   3152       int ra = instr->RAValue();
   3153       int rs = instr->RSValue();
   3154       uintptr_t rs_val = get_register(rs);
   3155       int sh = (instr->Bits(15, 11) | (instr->Bit(1) << 5));
   3156       int mb = (instr->Bits(10, 6) | (instr->Bit(5) << 5));
   3157       DCHECK(sh >= 0 && sh <= 63);
   3158       DCHECK(mb >= 0 && mb <= 63);
   3159       uintptr_t result = base::bits::RotateLeft64(rs_val, sh);
   3160       uintptr_t mask = 0xffffffffffffffff >> mb;
   3161       result &= mask;
   3162       set_register(ra, result);
   3163       if (instr->Bit(0)) {  // RC bit set
   3164         SetCR0(result);
   3165       }
   3166       return;
   3167     }
   3168     case RLDICR: {
   3169       int ra = instr->RAValue();
   3170       int rs = instr->RSValue();
   3171       uintptr_t rs_val = get_register(rs);
   3172       int sh = (instr->Bits(15, 11) | (instr->Bit(1) << 5));
   3173       int me = (instr->Bits(10, 6) | (instr->Bit(5) << 5));
   3174       DCHECK(sh >= 0 && sh <= 63);
   3175       DCHECK(me >= 0 && me <= 63);
   3176       uintptr_t result = base::bits::RotateLeft64(rs_val, sh);
   3177       uintptr_t mask = 0xffffffffffffffff << (63 - me);
   3178       result &= mask;
   3179       set_register(ra, result);
   3180       if (instr->Bit(0)) {  // RC bit set
   3181         SetCR0(result);
   3182       }
   3183       return;
   3184     }
   3185     case RLDIC: {
   3186       int ra = instr->RAValue();
   3187       int rs = instr->RSValue();
   3188       uintptr_t rs_val = get_register(rs);
   3189       int sh = (instr->Bits(15, 11) | (instr->Bit(1) << 5));
   3190       int mb = (instr->Bits(10, 6) | (instr->Bit(5) << 5));
   3191       DCHECK(sh >= 0 && sh <= 63);
   3192       DCHECK(mb >= 0 && mb <= 63);
   3193       uintptr_t result = base::bits::RotateLeft64(rs_val, sh);
   3194       uintptr_t mask = (0xffffffffffffffff >> mb) & (0xffffffffffffffff << sh);
   3195       result &= mask;
   3196       set_register(ra, result);
   3197       if (instr->Bit(0)) {  // RC bit set
   3198         SetCR0(result);
   3199       }
   3200       return;
   3201     }
   3202     case RLDIMI: {
   3203       int ra = instr->RAValue();
   3204       int rs = instr->RSValue();
   3205       uintptr_t rs_val = get_register(rs);
   3206       intptr_t ra_val = get_register(ra);
   3207       int sh = (instr->Bits(15, 11) | (instr->Bit(1) << 5));
   3208       int mb = (instr->Bits(10, 6) | (instr->Bit(5) << 5));
   3209       int me = 63 - sh;
   3210       uintptr_t result = base::bits::RotateLeft64(rs_val, sh);
   3211       uintptr_t mask = 0;
   3212       if (mb < me + 1) {
   3213         uintptr_t bit = 0x8000000000000000 >> mb;
   3214         for (; mb <= me; mb++) {
   3215           mask |= bit;
   3216           bit >>= 1;
   3217         }
   3218       } else if (mb == me + 1) {
   3219         mask = 0xffffffffffffffff;
   3220       } else {                                           // mb > me+1
   3221         uintptr_t bit = 0x8000000000000000 >> (me + 1);  // needs to be tested
   3222         mask = 0xffffffffffffffff;
   3223         for (; me < mb; me++) {
   3224           mask ^= bit;
   3225           bit >>= 1;
   3226         }
   3227       }
   3228       result &= mask;
   3229       ra_val &= ~mask;
   3230       result |= ra_val;
   3231       set_register(ra, result);
   3232       if (instr->Bit(0)) {  // RC bit set
   3233         SetCR0(result);
   3234       }
   3235       return;
   3236     }
   3237   }
   3238   switch (instr->Bits(4, 1) << 1) {
   3239     case RLDCL: {
   3240       int ra = instr->RAValue();
   3241       int rs = instr->RSValue();
   3242       int rb = instr->RBValue();
   3243       uintptr_t rs_val = get_register(rs);
   3244       uintptr_t rb_val = get_register(rb);
   3245       int sh = (rb_val & 0x3f);
   3246       int mb = (instr->Bits(10, 6) | (instr->Bit(5) << 5));
   3247       DCHECK(sh >= 0 && sh <= 63);
   3248       DCHECK(mb >= 0 && mb <= 63);
   3249       uintptr_t result = base::bits::RotateLeft64(rs_val, sh);
   3250       uintptr_t mask = 0xffffffffffffffff >> mb;
   3251       result &= mask;
   3252       set_register(ra, result);
   3253       if (instr->Bit(0)) {  // RC bit set
   3254         SetCR0(result);
   3255       }
   3256       return;
   3257     }
   3258   }
   3259   UNIMPLEMENTED();  // Not used by V8.
   3260 }
   3261 #endif
   3262 
   3263 
   3264 void Simulator::ExecuteGeneric(Instruction* instr) {
   3265   int opcode = instr->OpcodeValue() << 26;
   3266   switch (opcode) {
   3267     case SUBFIC: {
   3268       int rt = instr->RTValue();
   3269       int ra = instr->RAValue();
   3270       intptr_t ra_val = get_register(ra);
   3271       int32_t im_val = instr->Bits(15, 0);
   3272       im_val = SIGN_EXT_IMM16(im_val);
   3273       intptr_t alu_out = im_val - ra_val;
   3274       set_register(rt, alu_out);
   3275       // todo - handle RC bit
   3276       break;
   3277     }
   3278     case CMPLI: {
   3279       int ra = instr->RAValue();
   3280       uint32_t im_val = instr->Bits(15, 0);
   3281       int cr = instr->Bits(25, 23);
   3282       uint32_t bf = 0;
   3283 #if V8_TARGET_ARCH_PPC64
   3284       int L = instr->Bit(21);
   3285       if (L) {
   3286 #endif
   3287         uintptr_t ra_val = get_register(ra);
   3288         if (ra_val < im_val) {
   3289           bf |= 0x80000000;
   3290         }
   3291         if (ra_val > im_val) {
   3292           bf |= 0x40000000;
   3293         }
   3294         if (ra_val == im_val) {
   3295           bf |= 0x20000000;
   3296         }
   3297 #if V8_TARGET_ARCH_PPC64
   3298       } else {
   3299         uint32_t ra_val = get_register(ra);
   3300         if (ra_val < im_val) {
   3301           bf |= 0x80000000;
   3302         }
   3303         if (ra_val > im_val) {
   3304           bf |= 0x40000000;
   3305         }
   3306         if (ra_val == im_val) {
   3307           bf |= 0x20000000;
   3308         }
   3309       }
   3310 #endif
   3311       uint32_t condition_mask = 0xF0000000U >> (cr * 4);
   3312       uint32_t condition = bf >> (cr * 4);
   3313       condition_reg_ = (condition_reg_ & ~condition_mask) | condition;
   3314       break;
   3315     }
   3316     case CMPI: {
   3317       int ra = instr->RAValue();
   3318       int32_t im_val = instr->Bits(15, 0);
   3319       im_val = SIGN_EXT_IMM16(im_val);
   3320       int cr = instr->Bits(25, 23);
   3321       uint32_t bf = 0;
   3322 #if V8_TARGET_ARCH_PPC64
   3323       int L = instr->Bit(21);
   3324       if (L) {
   3325 #endif
   3326         intptr_t ra_val = get_register(ra);
   3327         if (ra_val < im_val) {
   3328           bf |= 0x80000000;
   3329         }
   3330         if (ra_val > im_val) {
   3331           bf |= 0x40000000;
   3332         }
   3333         if (ra_val == im_val) {
   3334           bf |= 0x20000000;
   3335         }
   3336 #if V8_TARGET_ARCH_PPC64
   3337       } else {
   3338         int32_t ra_val = get_register(ra);
   3339         if (ra_val < im_val) {
   3340           bf |= 0x80000000;
   3341         }
   3342         if (ra_val > im_val) {
   3343           bf |= 0x40000000;
   3344         }
   3345         if (ra_val == im_val) {
   3346           bf |= 0x20000000;
   3347         }
   3348       }
   3349 #endif
   3350       uint32_t condition_mask = 0xF0000000U >> (cr * 4);
   3351       uint32_t condition = bf >> (cr * 4);
   3352       condition_reg_ = (condition_reg_ & ~condition_mask) | condition;
   3353       break;
   3354     }
   3355     case ADDIC: {
   3356       int rt = instr->RTValue();
   3357       int ra = instr->RAValue();
   3358       uintptr_t ra_val = get_register(ra);
   3359       uintptr_t im_val = SIGN_EXT_IMM16(instr->Bits(15, 0));
   3360       uintptr_t alu_out = ra_val + im_val;
   3361       // Check overflow
   3362       if (~ra_val < im_val) {
   3363         special_reg_xer_ = (special_reg_xer_ & ~0xF0000000) | 0x20000000;
   3364       } else {
   3365         special_reg_xer_ &= ~0xF0000000;
   3366       }
   3367       set_register(rt, alu_out);
   3368       break;
   3369     }
   3370     case ADDI: {
   3371       int rt = instr->RTValue();
   3372       int ra = instr->RAValue();
   3373       int32_t im_val = SIGN_EXT_IMM16(instr->Bits(15, 0));
   3374       intptr_t alu_out;
   3375       if (ra == 0) {
   3376         alu_out = im_val;
   3377       } else {
   3378         intptr_t ra_val = get_register(ra);
   3379         alu_out = ra_val + im_val;
   3380       }
   3381       set_register(rt, alu_out);
   3382       // todo - handle RC bit
   3383       break;
   3384     }
   3385     case ADDIS: {
   3386       int rt = instr->RTValue();
   3387       int ra = instr->RAValue();
   3388       int32_t im_val = (instr->Bits(15, 0) << 16);
   3389       intptr_t alu_out;
   3390       if (ra == 0) {  // treat r0 as zero
   3391         alu_out = im_val;
   3392       } else {
   3393         intptr_t ra_val = get_register(ra);
   3394         alu_out = ra_val + im_val;
   3395       }
   3396       set_register(rt, alu_out);
   3397       break;
   3398     }
   3399     case BCX: {
   3400       ExecuteBranchConditional(instr, BC_OFFSET);
   3401       break;
   3402     }
   3403     case BX: {
   3404       int offset = (instr->Bits(25, 2) << 8) >> 6;
   3405       if (instr->Bit(0) == 1) {  // LK flag set
   3406         special_reg_lr_ = get_pc() + 4;
   3407       }
   3408       set_pc(get_pc() + offset);
   3409       // todo - AA flag
   3410       break;
   3411     }
   3412     case EXT1: {
   3413       ExecuteExt1(instr);
   3414       break;
   3415     }
   3416     case RLWIMIX: {
   3417       int ra = instr->RAValue();
   3418       int rs = instr->RSValue();
   3419       uint32_t rs_val = get_register(rs);
   3420       int32_t ra_val = get_register(ra);
   3421       int sh = instr->Bits(15, 11);
   3422       int mb = instr->Bits(10, 6);
   3423       int me = instr->Bits(5, 1);
   3424       uint32_t result = base::bits::RotateLeft32(rs_val, sh);
   3425       int mask = 0;
   3426       if (mb < me + 1) {
   3427         int bit = 0x80000000 >> mb;
   3428         for (; mb <= me; mb++) {
   3429           mask |= bit;
   3430           bit >>= 1;
   3431         }
   3432       } else if (mb == me + 1) {
   3433         mask = 0xffffffff;
   3434       } else {                             // mb > me+1
   3435         int bit = 0x80000000 >> (me + 1);  // needs to be tested
   3436         mask = 0xffffffff;
   3437         for (; me < mb; me++) {
   3438           mask ^= bit;
   3439           bit >>= 1;
   3440         }
   3441       }
   3442       result &= mask;
   3443       ra_val &= ~mask;
   3444       result |= ra_val;
   3445       set_register(ra, result);
   3446       if (instr->Bit(0)) {  // RC bit set
   3447         SetCR0(result);
   3448       }
   3449       break;
   3450     }
   3451     case RLWINMX:
   3452     case RLWNMX: {
   3453       int ra = instr->RAValue();
   3454       int rs = instr->RSValue();
   3455       uint32_t rs_val = get_register(rs);
   3456       int sh = 0;
   3457       if (opcode == RLWINMX) {
   3458         sh = instr->Bits(15, 11);
   3459       } else {
   3460         int rb = instr->RBValue();
   3461         uint32_t rb_val = get_register(rb);
   3462         sh = (rb_val & 0x1f);
   3463       }
   3464       int mb = instr->Bits(10, 6);
   3465       int me = instr->Bits(5, 1);
   3466       uint32_t result = base::bits::RotateLeft32(rs_val, sh);
   3467       int mask = 0;
   3468       if (mb < me + 1) {
   3469         int bit = 0x80000000 >> mb;
   3470         for (; mb <= me; mb++) {
   3471           mask |= bit;
   3472           bit >>= 1;
   3473         }
   3474       } else if (mb == me + 1) {
   3475         mask = 0xffffffff;
   3476       } else {                             // mb > me+1
   3477         int bit = 0x80000000 >> (me + 1);  // needs to be tested
   3478         mask = 0xffffffff;
   3479         for (; me < mb; me++) {
   3480           mask ^= bit;
   3481           bit >>= 1;
   3482         }
   3483       }
   3484       result &= mask;
   3485       set_register(ra, result);
   3486       if (instr->Bit(0)) {  // RC bit set
   3487         SetCR0(result);
   3488       }
   3489       break;
   3490     }
   3491     case ORI: {
   3492       int rs = instr->RSValue();
   3493       int ra = instr->RAValue();
   3494       intptr_t rs_val = get_register(rs);
   3495       uint32_t im_val = instr->Bits(15, 0);
   3496       intptr_t alu_out = rs_val | im_val;
   3497       set_register(ra, alu_out);
   3498       break;
   3499     }
   3500     case ORIS: {
   3501       int rs = instr->RSValue();
   3502       int ra = instr->RAValue();
   3503       intptr_t rs_val = get_register(rs);
   3504       uint32_t im_val = instr->Bits(15, 0);
   3505       intptr_t alu_out = rs_val | (im_val << 16);
   3506       set_register(ra, alu_out);
   3507       break;
   3508     }
   3509     case XORI: {
   3510       int rs = instr->RSValue();
   3511       int ra = instr->RAValue();
   3512       intptr_t rs_val = get_register(rs);
   3513       uint32_t im_val = instr->Bits(15, 0);
   3514       intptr_t alu_out = rs_val ^ im_val;
   3515       set_register(ra, alu_out);
   3516       // todo - set condition based SO bit
   3517       break;
   3518     }
   3519     case XORIS: {
   3520       int rs = instr->RSValue();
   3521       int ra = instr->RAValue();
   3522       intptr_t rs_val = get_register(rs);
   3523       uint32_t im_val = instr->Bits(15, 0);
   3524       intptr_t alu_out = rs_val ^ (im_val << 16);
   3525       set_register(ra, alu_out);
   3526       break;
   3527     }
   3528     case ANDIx: {
   3529       int rs = instr->RSValue();
   3530       int ra = instr->RAValue();
   3531       intptr_t rs_val = get_register(rs);
   3532       uint32_t im_val = instr->Bits(15, 0);
   3533       intptr_t alu_out = rs_val & im_val;
   3534       set_register(ra, alu_out);
   3535       SetCR0(alu_out);
   3536       break;
   3537     }
   3538     case ANDISx: {
   3539       int rs = instr->RSValue();
   3540       int ra = instr->RAValue();
   3541       intptr_t rs_val = get_register(rs);
   3542       uint32_t im_val = instr->Bits(15, 0);
   3543       intptr_t alu_out = rs_val & (im_val << 16);
   3544       set_register(ra, alu_out);
   3545       SetCR0(alu_out);
   3546       break;
   3547     }
   3548     case EXT2: {
   3549       ExecuteExt2(instr);
   3550       break;
   3551     }
   3552 
   3553     case LWZU:
   3554     case LWZ: {
   3555       int ra = instr->RAValue();
   3556       int rt = instr->RTValue();
   3557       intptr_t ra_val = ra == 0 ? 0 : get_register(ra);
   3558       int offset = SIGN_EXT_IMM16(instr->Bits(15, 0));
   3559       set_register(rt, ReadWU(ra_val + offset, instr));
   3560       if (opcode == LWZU) {
   3561         DCHECK(ra != 0);
   3562         set_register(ra, ra_val + offset);
   3563       }
   3564       break;
   3565     }
   3566 
   3567     case LBZU:
   3568     case LBZ: {
   3569       int ra = instr->RAValue();
   3570       int rt = instr->RTValue();
   3571       intptr_t ra_val = ra == 0 ? 0 : get_register(ra);
   3572       int offset = SIGN_EXT_IMM16(instr->Bits(15, 0));
   3573       set_register(rt, ReadB(ra_val + offset) & 0xFF);
   3574       if (opcode == LBZU) {
   3575         DCHECK(ra != 0);
   3576         set_register(ra, ra_val + offset);
   3577       }
   3578       break;
   3579     }
   3580 
   3581     case STWU:
   3582     case STW: {
   3583       int ra = instr->RAValue();
   3584       int rs = instr->RSValue();
   3585       intptr_t ra_val = ra == 0 ? 0 : get_register(ra);
   3586       int32_t rs_val = get_register(rs);
   3587       int offset = SIGN_EXT_IMM16(instr->Bits(15, 0));
   3588       WriteW(ra_val + offset, rs_val, instr);
   3589       if (opcode == STWU) {
   3590         DCHECK(ra != 0);
   3591         set_register(ra, ra_val + offset);
   3592       }
   3593       // printf("r%d %08x -> %08x\n", rs, rs_val, offset); // 0xdead
   3594       break;
   3595     }
   3596 
   3597     case STBU:
   3598     case STB: {
   3599       int ra = instr->RAValue();
   3600       int rs = instr->RSValue();
   3601       intptr_t ra_val = ra == 0 ? 0 : get_register(ra);
   3602       int8_t rs_val = get_register(rs);
   3603       int offset = SIGN_EXT_IMM16(instr->Bits(15, 0));
   3604       WriteB(ra_val + offset, rs_val);
   3605       if (opcode == STBU) {
   3606         DCHECK(ra != 0);
   3607         set_register(ra, ra_val + offset);
   3608       }
   3609       break;
   3610     }
   3611 
   3612     case LHZU:
   3613     case LHZ: {
   3614       int ra = instr->RAValue();
   3615       int rt = instr->RTValue();
   3616       intptr_t ra_val = ra == 0 ? 0 : get_register(ra);
   3617       int offset = SIGN_EXT_IMM16(instr->Bits(15, 0));
   3618       uintptr_t result = ReadHU(ra_val + offset, instr) & 0xffff;
   3619       set_register(rt, result);
   3620       if (opcode == LHZU) {
   3621         set_register(ra, ra_val + offset);
   3622       }
   3623       break;
   3624     }
   3625 
   3626     case LHA:
   3627     case LHAU: {
   3628       int ra = instr->RAValue();
   3629       int rt = instr->RTValue();
   3630       intptr_t ra_val = ra == 0 ? 0 : get_register(ra);
   3631       int offset = SIGN_EXT_IMM16(instr->Bits(15, 0));
   3632       intptr_t result = ReadH(ra_val + offset, instr);
   3633       set_register(rt, result);
   3634       if (opcode == LHAU) {
   3635         set_register(ra, ra_val + offset);
   3636       }
   3637       break;
   3638     }
   3639 
   3640     case STHU:
   3641     case STH: {
   3642       int ra = instr->RAValue();
   3643       int rs = instr->RSValue();
   3644       intptr_t ra_val = ra == 0 ? 0 : get_register(ra);
   3645       int16_t rs_val = get_register(rs);
   3646       int offset = SIGN_EXT_IMM16(instr->Bits(15, 0));
   3647       WriteH(ra_val + offset, rs_val, instr);
   3648       if (opcode == STHU) {
   3649         DCHECK(ra != 0);
   3650         set_register(ra, ra_val + offset);
   3651       }
   3652       break;
   3653     }
   3654 
   3655     case LMW:
   3656     case STMW: {
   3657       UNIMPLEMENTED();
   3658       break;
   3659     }
   3660 
   3661     case LFSU:
   3662     case LFS: {
   3663       int frt = instr->RTValue();
   3664       int ra = instr->RAValue();
   3665       int32_t offset = SIGN_EXT_IMM16(instr->Bits(15, 0));
   3666       intptr_t ra_val = ra == 0 ? 0 : get_register(ra);
   3667       int32_t val = ReadW(ra_val + offset, instr);
   3668       float* fptr = reinterpret_cast<float*>(&val);
   3669       set_d_register_from_double(frt, static_cast<double>(*fptr));
   3670       if (opcode == LFSU) {
   3671         DCHECK(ra != 0);
   3672         set_register(ra, ra_val + offset);
   3673       }
   3674       break;
   3675     }
   3676 
   3677     case LFDU:
   3678     case LFD: {
   3679       int frt = instr->RTValue();
   3680       int ra = instr->RAValue();
   3681       int32_t offset = SIGN_EXT_IMM16(instr->Bits(15, 0));
   3682       intptr_t ra_val = ra == 0 ? 0 : get_register(ra);
   3683       int64_t* dptr = reinterpret_cast<int64_t*>(ReadDW(ra_val + offset));
   3684       set_d_register(frt, *dptr);
   3685       if (opcode == LFDU) {
   3686         DCHECK(ra != 0);
   3687         set_register(ra, ra_val + offset);
   3688       }
   3689       break;
   3690     }
   3691 
   3692     case STFSU: {
   3693       case STFS:
   3694         int frs = instr->RSValue();
   3695         int ra = instr->RAValue();
   3696         int32_t offset = SIGN_EXT_IMM16(instr->Bits(15, 0));
   3697         intptr_t ra_val = ra == 0 ? 0 : get_register(ra);
   3698         float frs_val = static_cast<float>(get_double_from_d_register(frs));
   3699         int32_t* p = reinterpret_cast<int32_t*>(&frs_val);
   3700         WriteW(ra_val + offset, *p, instr);
   3701         if (opcode == STFSU) {
   3702           DCHECK(ra != 0);
   3703           set_register(ra, ra_val + offset);
   3704         }
   3705         break;
   3706     }
   3707 
   3708     case STFDU:
   3709     case STFD: {
   3710       int frs = instr->RSValue();
   3711       int ra = instr->RAValue();
   3712       int32_t offset = SIGN_EXT_IMM16(instr->Bits(15, 0));
   3713       intptr_t ra_val = ra == 0 ? 0 : get_register(ra);
   3714       int64_t frs_val = get_d_register(frs);
   3715       WriteDW(ra_val + offset, frs_val);
   3716       if (opcode == STFDU) {
   3717         DCHECK(ra != 0);
   3718         set_register(ra, ra_val + offset);
   3719       }
   3720       break;
   3721     }
   3722 
   3723     case EXT3: {
   3724       ExecuteExt3(instr);
   3725       break;
   3726     }
   3727     case EXT4: {
   3728       ExecuteExt4(instr);
   3729       break;
   3730     }
   3731 
   3732 #if V8_TARGET_ARCH_PPC64
   3733     case EXT5: {
   3734       ExecuteExt5(instr);
   3735       break;
   3736     }
   3737     case LD: {
   3738       int ra = instr->RAValue();
   3739       int rt = instr->RTValue();
   3740       int64_t ra_val = ra == 0 ? 0 : get_register(ra);
   3741       int offset = SIGN_EXT_IMM16(instr->Bits(15, 0) & ~3);
   3742       switch (instr->Bits(1, 0)) {
   3743         case 0: {  // ld
   3744           intptr_t* result = ReadDW(ra_val + offset);
   3745           set_register(rt, *result);
   3746           break;
   3747         }
   3748         case 1: {  // ldu
   3749           intptr_t* result = ReadDW(ra_val + offset);
   3750           set_register(rt, *result);
   3751           DCHECK(ra != 0);
   3752           set_register(ra, ra_val + offset);
   3753           break;
   3754         }
   3755         case 2: {  // lwa
   3756           intptr_t result = ReadW(ra_val + offset, instr);
   3757           set_register(rt, result);
   3758           break;
   3759         }
   3760       }
   3761       break;
   3762     }
   3763 
   3764     case STD: {
   3765       int ra = instr->RAValue();
   3766       int rs = instr->RSValue();
   3767       int64_t ra_val = ra == 0 ? 0 : get_register(ra);
   3768       int64_t rs_val = get_register(rs);
   3769       int offset = SIGN_EXT_IMM16(instr->Bits(15, 0) & ~3);
   3770       WriteDW(ra_val + offset, rs_val);
   3771       if (instr->Bit(0) == 1) {  // This is the STDU form
   3772         DCHECK(ra != 0);
   3773         set_register(ra, ra_val + offset);
   3774       }
   3775       break;
   3776     }
   3777 #endif
   3778 
   3779     default: {
   3780       UNIMPLEMENTED();
   3781       break;
   3782     }
   3783   }
   3784 }  // NOLINT
   3785 
   3786 
   3787 void Simulator::Trace(Instruction* instr) {
   3788   disasm::NameConverter converter;
   3789   disasm::Disassembler dasm(converter);
   3790   // use a reasonably large buffer
   3791   v8::internal::EmbeddedVector<char, 256> buffer;
   3792   dasm.InstructionDecode(buffer, reinterpret_cast<byte*>(instr));
   3793   PrintF("%05d  %08" V8PRIxPTR "  %s\n", icount_,
   3794          reinterpret_cast<intptr_t>(instr), buffer.start());
   3795 }
   3796 
   3797 
   3798 // Executes the current instruction.
   3799 void Simulator::ExecuteInstruction(Instruction* instr) {
   3800   if (v8::internal::FLAG_check_icache) {
   3801     CheckICache(isolate_->simulator_i_cache(), instr);
   3802   }
   3803   pc_modified_ = false;
   3804   if (::v8::internal::FLAG_trace_sim) {
   3805     Trace(instr);
   3806   }
   3807   int opcode = instr->OpcodeValue() << 26;
   3808   if (opcode == TWI) {
   3809     SoftwareInterrupt(instr);
   3810   } else {
   3811     ExecuteGeneric(instr);
   3812   }
   3813   if (!pc_modified_) {
   3814     set_pc(reinterpret_cast<intptr_t>(instr) + Instruction::kInstrSize);
   3815   }
   3816 }
   3817 
   3818 
   3819 void Simulator::Execute() {
   3820   // Get the PC to simulate. Cannot use the accessor here as we need the
   3821   // raw PC value and not the one used as input to arithmetic instructions.
   3822   intptr_t program_counter = get_pc();
   3823 
   3824   if (::v8::internal::FLAG_stop_sim_at == 0) {
   3825     // Fast version of the dispatch loop without checking whether the simulator
   3826     // should be stopping at a particular executed instruction.
   3827     while (program_counter != end_sim_pc) {
   3828       Instruction* instr = reinterpret_cast<Instruction*>(program_counter);
   3829       icount_++;
   3830       ExecuteInstruction(instr);
   3831       program_counter = get_pc();
   3832     }
   3833   } else {
   3834     // FLAG_stop_sim_at is at the non-default value. Stop in the debugger when
   3835     // we reach the particular instuction count.
   3836     while (program_counter != end_sim_pc) {
   3837       Instruction* instr = reinterpret_cast<Instruction*>(program_counter);
   3838       icount_++;
   3839       if (icount_ == ::v8::internal::FLAG_stop_sim_at) {
   3840         PPCDebugger dbg(this);
   3841         dbg.Debug();
   3842       } else {
   3843         ExecuteInstruction(instr);
   3844       }
   3845       program_counter = get_pc();
   3846     }
   3847   }
   3848 }
   3849 
   3850 
   3851 void Simulator::CallInternal(byte* entry) {
   3852   // Adjust JS-based stack limit to C-based stack limit.
   3853   isolate_->stack_guard()->AdjustStackLimitForSimulator();
   3854 
   3855 // Prepare to execute the code at entry
   3856 #if ABI_USES_FUNCTION_DESCRIPTORS
   3857   // entry is the function descriptor
   3858   set_pc(*(reinterpret_cast<intptr_t*>(entry)));
   3859 #else
   3860   // entry is the instruction address
   3861   set_pc(reinterpret_cast<intptr_t>(entry));
   3862 #endif
   3863 
   3864   // Put target address in ip (for JS prologue).
   3865   set_register(r12, get_pc());
   3866 
   3867   // Put down marker for end of simulation. The simulator will stop simulation
   3868   // when the PC reaches this value. By saving the "end simulation" value into
   3869   // the LR the simulation stops when returning to this call point.
   3870   special_reg_lr_ = end_sim_pc;
   3871 
   3872   // Remember the values of non-volatile registers.
   3873   intptr_t r2_val = get_register(r2);
   3874   intptr_t r13_val = get_register(r13);
   3875   intptr_t r14_val = get_register(r14);
   3876   intptr_t r15_val = get_register(r15);
   3877   intptr_t r16_val = get_register(r16);
   3878   intptr_t r17_val = get_register(r17);
   3879   intptr_t r18_val = get_register(r18);
   3880   intptr_t r19_val = get_register(r19);
   3881   intptr_t r20_val = get_register(r20);
   3882   intptr_t r21_val = get_register(r21);
   3883   intptr_t r22_val = get_register(r22);
   3884   intptr_t r23_val = get_register(r23);
   3885   intptr_t r24_val = get_register(r24);
   3886   intptr_t r25_val = get_register(r25);
   3887   intptr_t r26_val = get_register(r26);
   3888   intptr_t r27_val = get_register(r27);
   3889   intptr_t r28_val = get_register(r28);
   3890   intptr_t r29_val = get_register(r29);
   3891   intptr_t r30_val = get_register(r30);
   3892   intptr_t r31_val = get_register(fp);
   3893 
   3894   // Set up the non-volatile registers with a known value. To be able to check
   3895   // that they are preserved properly across JS execution.
   3896   intptr_t callee_saved_value = icount_;
   3897   set_register(r2, callee_saved_value);
   3898   set_register(r13, callee_saved_value);
   3899   set_register(r14, callee_saved_value);
   3900   set_register(r15, callee_saved_value);
   3901   set_register(r16, callee_saved_value);
   3902   set_register(r17, callee_saved_value);
   3903   set_register(r18, callee_saved_value);
   3904   set_register(r19, callee_saved_value);
   3905   set_register(r20, callee_saved_value);
   3906   set_register(r21, callee_saved_value);
   3907   set_register(r22, callee_saved_value);
   3908   set_register(r23, callee_saved_value);
   3909   set_register(r24, callee_saved_value);
   3910   set_register(r25, callee_saved_value);
   3911   set_register(r26, callee_saved_value);
   3912   set_register(r27, callee_saved_value);
   3913   set_register(r28, callee_saved_value);
   3914   set_register(r29, callee_saved_value);
   3915   set_register(r30, callee_saved_value);
   3916   set_register(fp, callee_saved_value);
   3917 
   3918   // Start the simulation
   3919   Execute();
   3920 
   3921   // Check that the non-volatile registers have been preserved.
   3922   CHECK_EQ(callee_saved_value, get_register(r2));
   3923   CHECK_EQ(callee_saved_value, get_register(r13));
   3924   CHECK_EQ(callee_saved_value, get_register(r14));
   3925   CHECK_EQ(callee_saved_value, get_register(r15));
   3926   CHECK_EQ(callee_saved_value, get_register(r16));
   3927   CHECK_EQ(callee_saved_value, get_register(r17));
   3928   CHECK_EQ(callee_saved_value, get_register(r18));
   3929   CHECK_EQ(callee_saved_value, get_register(r19));
   3930   CHECK_EQ(callee_saved_value, get_register(r20));
   3931   CHECK_EQ(callee_saved_value, get_register(r21));
   3932   CHECK_EQ(callee_saved_value, get_register(r22));
   3933   CHECK_EQ(callee_saved_value, get_register(r23));
   3934   CHECK_EQ(callee_saved_value, get_register(r24));
   3935   CHECK_EQ(callee_saved_value, get_register(r25));
   3936   CHECK_EQ(callee_saved_value, get_register(r26));
   3937   CHECK_EQ(callee_saved_value, get_register(r27));
   3938   CHECK_EQ(callee_saved_value, get_register(r28));
   3939   CHECK_EQ(callee_saved_value, get_register(r29));
   3940   CHECK_EQ(callee_saved_value, get_register(r30));
   3941   CHECK_EQ(callee_saved_value, get_register(fp));
   3942 
   3943   // Restore non-volatile registers with the original value.
   3944   set_register(r2, r2_val);
   3945   set_register(r13, r13_val);
   3946   set_register(r14, r14_val);
   3947   set_register(r15, r15_val);
   3948   set_register(r16, r16_val);
   3949   set_register(r17, r17_val);
   3950   set_register(r18, r18_val);
   3951   set_register(r19, r19_val);
   3952   set_register(r20, r20_val);
   3953   set_register(r21, r21_val);
   3954   set_register(r22, r22_val);
   3955   set_register(r23, r23_val);
   3956   set_register(r24, r24_val);
   3957   set_register(r25, r25_val);
   3958   set_register(r26, r26_val);
   3959   set_register(r27, r27_val);
   3960   set_register(r28, r28_val);
   3961   set_register(r29, r29_val);
   3962   set_register(r30, r30_val);
   3963   set_register(fp, r31_val);
   3964 }
   3965 
   3966 
   3967 intptr_t Simulator::Call(byte* entry, int argument_count, ...) {
   3968   va_list parameters;
   3969   va_start(parameters, argument_count);
   3970   // Set up arguments
   3971 
   3972   // First eight arguments passed in registers r3-r10.
   3973   int reg_arg_count = (argument_count > 8) ? 8 : argument_count;
   3974   int stack_arg_count = argument_count - reg_arg_count;
   3975   for (int i = 0; i < reg_arg_count; i++) {
   3976     set_register(i + 3, va_arg(parameters, intptr_t));
   3977   }
   3978 
   3979   // Remaining arguments passed on stack.
   3980   intptr_t original_stack = get_register(sp);
   3981   // Compute position of stack on entry to generated code.
   3982   intptr_t entry_stack =
   3983       (original_stack -
   3984        (kNumRequiredStackFrameSlots + stack_arg_count) * sizeof(intptr_t));
   3985   if (base::OS::ActivationFrameAlignment() != 0) {
   3986     entry_stack &= -base::OS::ActivationFrameAlignment();
   3987   }
   3988   // Store remaining arguments on stack, from low to high memory.
   3989   // +2 is a hack for the LR slot + old SP on PPC
   3990   intptr_t* stack_argument =
   3991       reinterpret_cast<intptr_t*>(entry_stack) + kStackFrameExtraParamSlot;
   3992   for (int i = 0; i < stack_arg_count; i++) {
   3993     stack_argument[i] = va_arg(parameters, intptr_t);
   3994   }
   3995   va_end(parameters);
   3996   set_register(sp, entry_stack);
   3997 
   3998   CallInternal(entry);
   3999 
   4000   // Pop stack passed arguments.
   4001   CHECK_EQ(entry_stack, get_register(sp));
   4002   set_register(sp, original_stack);
   4003 
   4004   intptr_t result = get_register(r3);
   4005   return result;
   4006 }
   4007 
   4008 
   4009 void Simulator::CallFP(byte* entry, double d0, double d1) {
   4010   set_d_register_from_double(1, d0);
   4011   set_d_register_from_double(2, d1);
   4012   CallInternal(entry);
   4013 }
   4014 
   4015 
   4016 int32_t Simulator::CallFPReturnsInt(byte* entry, double d0, double d1) {
   4017   CallFP(entry, d0, d1);
   4018   int32_t result = get_register(r3);
   4019   return result;
   4020 }
   4021 
   4022 
   4023 double Simulator::CallFPReturnsDouble(byte* entry, double d0, double d1) {
   4024   CallFP(entry, d0, d1);
   4025   return get_double_from_d_register(1);
   4026 }
   4027 
   4028 
   4029 uintptr_t Simulator::PushAddress(uintptr_t address) {
   4030   uintptr_t new_sp = get_register(sp) - sizeof(uintptr_t);
   4031   uintptr_t* stack_slot = reinterpret_cast<uintptr_t*>(new_sp);
   4032   *stack_slot = address;
   4033   set_register(sp, new_sp);
   4034   return new_sp;
   4035 }
   4036 
   4037 
   4038 uintptr_t Simulator::PopAddress() {
   4039   uintptr_t current_sp = get_register(sp);
   4040   uintptr_t* stack_slot = reinterpret_cast<uintptr_t*>(current_sp);
   4041   uintptr_t address = *stack_slot;
   4042   set_register(sp, current_sp + sizeof(uintptr_t));
   4043   return address;
   4044 }
   4045 }  // namespace internal
   4046 }  // namespace v8
   4047 
   4048 #endif  // USE_SIMULATOR
   4049 #endif  // V8_TARGET_ARCH_PPC
   4050