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