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