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