blob: 79dc8252b7379c16b245fe7ffb99040d746712fb [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);
Ben Murdochda12d292016-06-02 14:46:10 +01001736 uintptr_t rb_val = get_register(rb) & 0x3f;
1737 intptr_t result = (rb_val > 31) ? 0 : rs_val >> rb_val;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001738 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);
Ben Murdochda12d292016-06-02 14:46:10 +01001750 uintptr_t rb_val = get_register(rb) & 0x7f;
1751 intptr_t result = (rb_val > 63) ? 0 : rs_val >> rb_val;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001752 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);
Ben Murdochda12d292016-06-02 14:46:10 +01001764 intptr_t rb_val = get_register(rb) & 0x3f;
1765 intptr_t result = (rb_val > 31) ? rs_val >> 31 : rs_val >> rb_val;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001766 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);
Ben Murdochda12d292016-06-02 14:46:10 +01001778 intptr_t rb_val = get_register(rb) & 0x7f;
1779 intptr_t result = (rb_val > 63) ? rs_val >> 63 : rs_val >> rb_val;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001780 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;
Ben Murdochda12d292016-06-02 14:46:10 +01002028 // Set carry
2029 if (ra_val <= rb_val) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002030 special_reg_xer_ = (special_reg_xer_ & ~0xF0000000) | 0x20000000;
Ben Murdochda12d292016-06-02 14:46:10 +01002031 } else {
2032 special_reg_xer_ &= ~0xF0000000;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002033 }
Ben Murdochda12d292016-06-02 14:46:10 +01002034 set_register(rt, alu_out);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002035 if (instr->Bit(0)) { // RC bit set
2036 SetCR0(alu_out);
2037 }
2038 // todo - handle OE bit
2039 break;
2040 }
Ben Murdochda12d292016-06-02 14:46:10 +01002041 case SUBFEX: {
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 if (special_reg_xer_ & 0x20000000) {
2050 alu_out += 1;
2051 }
2052 set_register(rt, alu_out);
2053 if (instr->Bit(0)) { // RC bit set
2054 SetCR0(static_cast<intptr_t>(alu_out));
2055 }
2056 // todo - handle OE bit
2057 break;
2058 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002059 case ADDCX: {
2060 int rt = instr->RTValue();
2061 int ra = instr->RAValue();
2062 int rb = instr->RBValue();
2063 // int oe = instr->Bit(10);
2064 uintptr_t ra_val = get_register(ra);
2065 uintptr_t rb_val = get_register(rb);
2066 uintptr_t alu_out = ra_val + rb_val;
Ben Murdochda12d292016-06-02 14:46:10 +01002067 // Set carry
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002068 if (~ra_val < rb_val) {
2069 special_reg_xer_ = (special_reg_xer_ & ~0xF0000000) | 0x20000000;
2070 } else {
2071 special_reg_xer_ &= ~0xF0000000;
2072 }
2073 set_register(rt, alu_out);
2074 if (instr->Bit(0)) { // RC bit set
2075 SetCR0(static_cast<intptr_t>(alu_out));
2076 }
2077 // todo - handle OE bit
2078 break;
2079 }
Ben Murdochda12d292016-06-02 14:46:10 +01002080 case ADDEX: {
2081 int rt = instr->RTValue();
2082 int ra = instr->RAValue();
2083 int rb = instr->RBValue();
2084 // int oe = instr->Bit(10);
2085 uintptr_t ra_val = get_register(ra);
2086 uintptr_t rb_val = get_register(rb);
2087 uintptr_t alu_out = ra_val + rb_val;
2088 if (special_reg_xer_ & 0x20000000) {
2089 alu_out += 1;
2090 }
2091 set_register(rt, alu_out);
2092 if (instr->Bit(0)) { // RC bit set
2093 SetCR0(static_cast<intptr_t>(alu_out));
2094 }
2095 // todo - handle OE bit
2096 break;
2097 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002098 case MULHWX: {
2099 int rt = instr->RTValue();
2100 int ra = instr->RAValue();
2101 int rb = instr->RBValue();
2102 int32_t ra_val = (get_register(ra) & 0xFFFFFFFF);
2103 int32_t rb_val = (get_register(rb) & 0xFFFFFFFF);
2104 int64_t alu_out = (int64_t)ra_val * (int64_t)rb_val;
2105 alu_out >>= 32;
2106 set_register(rt, alu_out);
2107 if (instr->Bit(0)) { // RC bit set
2108 SetCR0(static_cast<intptr_t>(alu_out));
2109 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002110 break;
2111 }
2112 case MULHWUX: {
2113 int rt = instr->RTValue();
2114 int ra = instr->RAValue();
2115 int rb = instr->RBValue();
2116 uint32_t ra_val = (get_register(ra) & 0xFFFFFFFF);
2117 uint32_t rb_val = (get_register(rb) & 0xFFFFFFFF);
2118 uint64_t alu_out = (uint64_t)ra_val * (uint64_t)rb_val;
2119 alu_out >>= 32;
2120 set_register(rt, alu_out);
2121 if (instr->Bit(0)) { // RC bit set
2122 SetCR0(static_cast<intptr_t>(alu_out));
2123 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002124 break;
2125 }
2126 case NEGX: {
2127 int rt = instr->RTValue();
2128 int ra = instr->RAValue();
2129 intptr_t ra_val = get_register(ra);
2130 intptr_t alu_out = 1 + ~ra_val;
2131#if V8_TARGET_ARCH_PPC64
2132 intptr_t one = 1; // work-around gcc
2133 intptr_t kOverflowVal = (one << 63);
2134#else
2135 intptr_t kOverflowVal = kMinInt;
2136#endif
2137 set_register(rt, alu_out);
2138 if (instr->Bit(10)) { // OE bit set
2139 if (ra_val == kOverflowVal) {
2140 special_reg_xer_ |= 0xC0000000; // set SO,OV
2141 } else {
2142 special_reg_xer_ &= ~0x40000000; // clear OV
2143 }
2144 }
2145 if (instr->Bit(0)) { // RC bit set
2146 bool setSO = (special_reg_xer_ & 0x80000000);
2147 SetCR0(alu_out, setSO);
2148 }
2149 break;
2150 }
2151 case SLWX: {
2152 int rs = instr->RSValue();
2153 int ra = instr->RAValue();
2154 int rb = instr->RBValue();
2155 uint32_t rs_val = get_register(rs);
Ben Murdochda12d292016-06-02 14:46:10 +01002156 uintptr_t rb_val = get_register(rb) & 0x3f;
2157 uint32_t result = (rb_val > 31) ? 0 : rs_val << rb_val;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002158 set_register(ra, result);
2159 if (instr->Bit(0)) { // RC bit set
2160 SetCR0(result);
2161 }
2162 break;
2163 }
2164#if V8_TARGET_ARCH_PPC64
2165 case SLDX: {
2166 int rs = instr->RSValue();
2167 int ra = instr->RAValue();
2168 int rb = instr->RBValue();
2169 uintptr_t rs_val = get_register(rs);
Ben Murdochda12d292016-06-02 14:46:10 +01002170 uintptr_t rb_val = get_register(rb) & 0x7f;
2171 uintptr_t result = (rb_val > 63) ? 0 : rs_val << rb_val;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002172 set_register(ra, result);
2173 if (instr->Bit(0)) { // RC bit set
2174 SetCR0(result);
2175 }
2176 break;
2177 }
2178 case MFVSRD: {
2179 DCHECK(!instr->Bit(0));
2180 int frt = instr->RTValue();
2181 int ra = instr->RAValue();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002182 int64_t frt_val = get_d_register(frt);
2183 set_register(ra, frt_val);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002184 break;
2185 }
2186 case MFVSRWZ: {
2187 DCHECK(!instr->Bit(0));
2188 int frt = instr->RTValue();
2189 int ra = instr->RAValue();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002190 int64_t frt_val = get_d_register(frt);
2191 set_register(ra, static_cast<uint32_t>(frt_val));
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002192 break;
2193 }
2194 case MTVSRD: {
2195 DCHECK(!instr->Bit(0));
2196 int frt = instr->RTValue();
2197 int ra = instr->RAValue();
2198 int64_t ra_val = get_register(ra);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002199 set_d_register(frt, ra_val);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002200 break;
2201 }
2202 case MTVSRWA: {
2203 DCHECK(!instr->Bit(0));
2204 int frt = instr->RTValue();
2205 int ra = instr->RAValue();
2206 int64_t ra_val = static_cast<int32_t>(get_register(ra));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002207 set_d_register(frt, ra_val);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002208 break;
2209 }
2210 case MTVSRWZ: {
2211 DCHECK(!instr->Bit(0));
2212 int frt = instr->RTValue();
2213 int ra = instr->RAValue();
2214 uint64_t ra_val = static_cast<uint32_t>(get_register(ra));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002215 set_d_register(frt, ra_val);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002216 break;
2217 }
2218#endif
2219 default: {
2220 found = false;
2221 break;
2222 }
2223 }
2224
2225 return found;
2226}
2227
2228
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002229bool Simulator::ExecuteExt2_9bit_part2(Instruction* instr) {
2230 bool found = true;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002231 int opcode = instr->Bits(9, 1) << 1;
2232 switch (opcode) {
2233 case CNTLZWX: {
2234 int rs = instr->RSValue();
2235 int ra = instr->RAValue();
2236 uintptr_t rs_val = get_register(rs);
2237 uintptr_t count = 0;
2238 int n = 0;
2239 uintptr_t bit = 0x80000000;
2240 for (; n < 32; n++) {
2241 if (bit & rs_val) break;
2242 count++;
2243 bit >>= 1;
2244 }
2245 set_register(ra, count);
2246 if (instr->Bit(0)) { // RC Bit set
2247 int bf = 0;
2248 if (count > 0) {
2249 bf |= 0x40000000;
2250 }
2251 if (count == 0) {
2252 bf |= 0x20000000;
2253 }
2254 condition_reg_ = (condition_reg_ & ~0xF0000000) | bf;
2255 }
2256 break;
2257 }
2258#if V8_TARGET_ARCH_PPC64
2259 case CNTLZDX: {
2260 int rs = instr->RSValue();
2261 int ra = instr->RAValue();
2262 uintptr_t rs_val = get_register(rs);
2263 uintptr_t count = 0;
2264 int n = 0;
2265 uintptr_t bit = 0x8000000000000000UL;
2266 for (; n < 64; n++) {
2267 if (bit & rs_val) break;
2268 count++;
2269 bit >>= 1;
2270 }
2271 set_register(ra, count);
2272 if (instr->Bit(0)) { // RC Bit set
2273 int bf = 0;
2274 if (count > 0) {
2275 bf |= 0x40000000;
2276 }
2277 if (count == 0) {
2278 bf |= 0x20000000;
2279 }
2280 condition_reg_ = (condition_reg_ & ~0xF0000000) | bf;
2281 }
2282 break;
2283 }
2284#endif
2285 case ANDX: {
2286 int rs = instr->RSValue();
2287 int ra = instr->RAValue();
2288 int rb = instr->RBValue();
2289 intptr_t rs_val = get_register(rs);
2290 intptr_t rb_val = get_register(rb);
2291 intptr_t alu_out = rs_val & rb_val;
2292 set_register(ra, alu_out);
2293 if (instr->Bit(0)) { // RC Bit set
2294 SetCR0(alu_out);
2295 }
2296 break;
2297 }
2298 case ANDCX: {
2299 int rs = instr->RSValue();
2300 int ra = instr->RAValue();
2301 int rb = instr->RBValue();
2302 intptr_t rs_val = get_register(rs);
2303 intptr_t rb_val = get_register(rb);
2304 intptr_t alu_out = rs_val & ~rb_val;
2305 set_register(ra, alu_out);
2306 if (instr->Bit(0)) { // RC Bit set
2307 SetCR0(alu_out);
2308 }
2309 break;
2310 }
2311 case CMPL: {
2312 int ra = instr->RAValue();
2313 int rb = instr->RBValue();
2314 int cr = instr->Bits(25, 23);
2315 uint32_t bf = 0;
2316#if V8_TARGET_ARCH_PPC64
2317 int L = instr->Bit(21);
2318 if (L) {
2319#endif
2320 uintptr_t ra_val = get_register(ra);
2321 uintptr_t rb_val = get_register(rb);
2322 if (ra_val < rb_val) {
2323 bf |= 0x80000000;
2324 }
2325 if (ra_val > rb_val) {
2326 bf |= 0x40000000;
2327 }
2328 if (ra_val == rb_val) {
2329 bf |= 0x20000000;
2330 }
2331#if V8_TARGET_ARCH_PPC64
2332 } else {
2333 uint32_t ra_val = get_register(ra);
2334 uint32_t rb_val = get_register(rb);
2335 if (ra_val < rb_val) {
2336 bf |= 0x80000000;
2337 }
2338 if (ra_val > rb_val) {
2339 bf |= 0x40000000;
2340 }
2341 if (ra_val == rb_val) {
2342 bf |= 0x20000000;
2343 }
2344 }
2345#endif
2346 uint32_t condition_mask = 0xF0000000U >> (cr * 4);
2347 uint32_t condition = bf >> (cr * 4);
2348 condition_reg_ = (condition_reg_ & ~condition_mask) | condition;
2349 break;
2350 }
2351 case SUBFX: {
2352 int rt = instr->RTValue();
2353 int ra = instr->RAValue();
2354 int rb = instr->RBValue();
2355 // int oe = instr->Bit(10);
2356 intptr_t ra_val = get_register(ra);
2357 intptr_t rb_val = get_register(rb);
2358 intptr_t alu_out = rb_val - ra_val;
2359 // todo - figure out underflow
2360 set_register(rt, alu_out);
2361 if (instr->Bit(0)) { // RC Bit set
2362 SetCR0(alu_out);
2363 }
2364 // todo - handle OE bit
2365 break;
2366 }
2367 case ADDZEX: {
2368 int rt = instr->RTValue();
2369 int ra = instr->RAValue();
2370 intptr_t ra_val = get_register(ra);
2371 if (special_reg_xer_ & 0x20000000) {
2372 ra_val += 1;
2373 }
2374 set_register(rt, ra_val);
2375 if (instr->Bit(0)) { // RC bit set
2376 SetCR0(ra_val);
2377 }
2378 // todo - handle OE bit
2379 break;
2380 }
2381 case NORX: {
2382 int rs = instr->RSValue();
2383 int ra = instr->RAValue();
2384 int rb = instr->RBValue();
2385 intptr_t rs_val = get_register(rs);
2386 intptr_t rb_val = get_register(rb);
2387 intptr_t alu_out = ~(rs_val | rb_val);
2388 set_register(ra, alu_out);
2389 if (instr->Bit(0)) { // RC bit set
2390 SetCR0(alu_out);
2391 }
2392 break;
2393 }
2394 case MULLW: {
2395 int rt = instr->RTValue();
2396 int ra = instr->RAValue();
2397 int rb = instr->RBValue();
2398 int32_t ra_val = (get_register(ra) & 0xFFFFFFFF);
2399 int32_t rb_val = (get_register(rb) & 0xFFFFFFFF);
2400 int32_t alu_out = ra_val * rb_val;
2401 set_register(rt, alu_out);
2402 if (instr->Bit(0)) { // RC bit set
2403 SetCR0(alu_out);
2404 }
2405 // todo - handle OE bit
2406 break;
2407 }
2408#if V8_TARGET_ARCH_PPC64
2409 case MULLD: {
2410 int rt = instr->RTValue();
2411 int ra = instr->RAValue();
2412 int rb = instr->RBValue();
2413 int64_t ra_val = get_register(ra);
2414 int64_t rb_val = get_register(rb);
2415 int64_t alu_out = ra_val * rb_val;
2416 set_register(rt, alu_out);
2417 if (instr->Bit(0)) { // RC bit set
2418 SetCR0(alu_out);
2419 }
2420 // todo - handle OE bit
2421 break;
2422 }
2423#endif
2424 case DIVW: {
2425 int rt = instr->RTValue();
2426 int ra = instr->RAValue();
2427 int rb = instr->RBValue();
2428 int32_t ra_val = get_register(ra);
2429 int32_t rb_val = get_register(rb);
2430 bool overflow = (ra_val == kMinInt && rb_val == -1);
2431 // result is undefined if divisor is zero or if operation
2432 // is 0x80000000 / -1.
2433 int32_t alu_out = (rb_val == 0 || overflow) ? -1 : ra_val / rb_val;
2434 set_register(rt, alu_out);
2435 if (instr->Bit(10)) { // OE bit set
2436 if (overflow) {
2437 special_reg_xer_ |= 0xC0000000; // set SO,OV
2438 } else {
2439 special_reg_xer_ &= ~0x40000000; // clear OV
2440 }
2441 }
2442 if (instr->Bit(0)) { // RC bit set
2443 bool setSO = (special_reg_xer_ & 0x80000000);
2444 SetCR0(alu_out, setSO);
2445 }
2446 break;
2447 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002448 case DIVWU: {
2449 int rt = instr->RTValue();
2450 int ra = instr->RAValue();
2451 int rb = instr->RBValue();
2452 uint32_t ra_val = get_register(ra);
2453 uint32_t rb_val = get_register(rb);
2454 bool overflow = (rb_val == 0);
2455 // result is undefined if divisor is zero
2456 uint32_t alu_out = (overflow) ? -1 : ra_val / rb_val;
2457 set_register(rt, alu_out);
2458 if (instr->Bit(10)) { // OE bit set
2459 if (overflow) {
2460 special_reg_xer_ |= 0xC0000000; // set SO,OV
2461 } else {
2462 special_reg_xer_ &= ~0x40000000; // clear OV
2463 }
2464 }
2465 if (instr->Bit(0)) { // RC bit set
2466 bool setSO = (special_reg_xer_ & 0x80000000);
2467 SetCR0(alu_out, setSO);
2468 }
2469 break;
2470 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002471#if V8_TARGET_ARCH_PPC64
2472 case DIVD: {
2473 int rt = instr->RTValue();
2474 int ra = instr->RAValue();
2475 int rb = instr->RBValue();
2476 int64_t ra_val = get_register(ra);
2477 int64_t rb_val = get_register(rb);
2478 int64_t one = 1; // work-around gcc
2479 int64_t kMinLongLong = (one << 63);
2480 // result is undefined if divisor is zero or if operation
2481 // is 0x80000000_00000000 / -1.
2482 int64_t alu_out =
2483 (rb_val == 0 || (ra_val == kMinLongLong && rb_val == -1))
2484 ? -1
2485 : ra_val / rb_val;
2486 set_register(rt, alu_out);
2487 if (instr->Bit(0)) { // RC bit set
2488 SetCR0(alu_out);
2489 }
2490 // todo - handle OE bit
2491 break;
2492 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002493 case DIVDU: {
2494 int rt = instr->RTValue();
2495 int ra = instr->RAValue();
2496 int rb = instr->RBValue();
2497 uint64_t ra_val = get_register(ra);
2498 uint64_t rb_val = get_register(rb);
2499 // result is undefined if divisor is zero
2500 uint64_t alu_out = (rb_val == 0) ? -1 : ra_val / rb_val;
2501 set_register(rt, alu_out);
2502 if (instr->Bit(0)) { // RC bit set
2503 SetCR0(alu_out);
2504 }
2505 // todo - handle OE bit
2506 break;
2507 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002508#endif
2509 case ADDX: {
2510 int rt = instr->RTValue();
2511 int ra = instr->RAValue();
2512 int rb = instr->RBValue();
2513 // int oe = instr->Bit(10);
2514 intptr_t ra_val = get_register(ra);
2515 intptr_t rb_val = get_register(rb);
2516 intptr_t alu_out = ra_val + rb_val;
2517 set_register(rt, alu_out);
2518 if (instr->Bit(0)) { // RC bit set
2519 SetCR0(alu_out);
2520 }
2521 // todo - handle OE bit
2522 break;
2523 }
2524 case XORX: {
2525 int rs = instr->RSValue();
2526 int ra = instr->RAValue();
2527 int rb = instr->RBValue();
2528 intptr_t rs_val = get_register(rs);
2529 intptr_t rb_val = get_register(rb);
2530 intptr_t alu_out = rs_val ^ rb_val;
2531 set_register(ra, alu_out);
2532 if (instr->Bit(0)) { // RC bit set
2533 SetCR0(alu_out);
2534 }
2535 break;
2536 }
2537 case ORX: {
2538 int rs = instr->RSValue();
2539 int ra = instr->RAValue();
2540 int rb = instr->RBValue();
2541 intptr_t rs_val = get_register(rs);
2542 intptr_t rb_val = get_register(rb);
2543 intptr_t alu_out = rs_val | rb_val;
2544 set_register(ra, alu_out);
2545 if (instr->Bit(0)) { // RC bit set
2546 SetCR0(alu_out);
2547 }
2548 break;
2549 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002550 case ORC: {
2551 int rs = instr->RSValue();
2552 int ra = instr->RAValue();
2553 int rb = instr->RBValue();
2554 intptr_t rs_val = get_register(rs);
2555 intptr_t rb_val = get_register(rb);
2556 intptr_t alu_out = rs_val | ~rb_val;
2557 set_register(ra, alu_out);
2558 if (instr->Bit(0)) { // RC bit set
2559 SetCR0(alu_out);
2560 }
2561 break;
2562 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002563 case MFSPR: {
2564 int rt = instr->RTValue();
2565 int spr = instr->Bits(20, 11);
2566 if (spr != 256) {
2567 UNIMPLEMENTED(); // Only LRLR supported
2568 }
2569 set_register(rt, special_reg_lr_);
2570 break;
2571 }
2572 case MTSPR: {
2573 int rt = instr->RTValue();
2574 intptr_t rt_val = get_register(rt);
2575 int spr = instr->Bits(20, 11);
2576 if (spr == 256) {
2577 special_reg_lr_ = rt_val;
2578 } else if (spr == 288) {
2579 special_reg_ctr_ = rt_val;
2580 } else if (spr == 32) {
2581 special_reg_xer_ = rt_val;
2582 } else {
2583 UNIMPLEMENTED(); // Only LR supported
2584 }
2585 break;
2586 }
2587 case MFCR: {
2588 int rt = instr->RTValue();
2589 set_register(rt, condition_reg_);
2590 break;
2591 }
2592 case STWUX:
2593 case STWX: {
2594 int rs = instr->RSValue();
2595 int ra = instr->RAValue();
2596 int rb = instr->RBValue();
2597 intptr_t ra_val = ra == 0 ? 0 : get_register(ra);
2598 int32_t rs_val = get_register(rs);
2599 intptr_t rb_val = get_register(rb);
2600 WriteW(ra_val + rb_val, rs_val, instr);
2601 if (opcode == STWUX) {
2602 DCHECK(ra != 0);
2603 set_register(ra, ra_val + rb_val);
2604 }
2605 break;
2606 }
2607 case STBUX:
2608 case STBX: {
2609 int rs = instr->RSValue();
2610 int ra = instr->RAValue();
2611 int rb = instr->RBValue();
2612 intptr_t ra_val = ra == 0 ? 0 : get_register(ra);
2613 int8_t rs_val = get_register(rs);
2614 intptr_t rb_val = get_register(rb);
2615 WriteB(ra_val + rb_val, rs_val);
2616 if (opcode == STBUX) {
2617 DCHECK(ra != 0);
2618 set_register(ra, ra_val + rb_val);
2619 }
2620 break;
2621 }
2622 case STHUX:
2623 case STHX: {
2624 int rs = instr->RSValue();
2625 int ra = instr->RAValue();
2626 int rb = instr->RBValue();
2627 intptr_t ra_val = ra == 0 ? 0 : get_register(ra);
2628 int16_t rs_val = get_register(rs);
2629 intptr_t rb_val = get_register(rb);
2630 WriteH(ra_val + rb_val, rs_val, instr);
2631 if (opcode == STHUX) {
2632 DCHECK(ra != 0);
2633 set_register(ra, ra_val + rb_val);
2634 }
2635 break;
2636 }
2637 case LWZX:
2638 case LWZUX: {
2639 int rt = instr->RTValue();
2640 int ra = instr->RAValue();
2641 int rb = instr->RBValue();
2642 intptr_t ra_val = ra == 0 ? 0 : get_register(ra);
2643 intptr_t rb_val = get_register(rb);
2644 set_register(rt, ReadWU(ra_val + rb_val, instr));
2645 if (opcode == LWZUX) {
2646 DCHECK(ra != 0 && ra != rt);
2647 set_register(ra, ra_val + rb_val);
2648 }
2649 break;
2650 }
2651#if V8_TARGET_ARCH_PPC64
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002652 case LWAX: {
2653 int rt = instr->RTValue();
2654 int ra = instr->RAValue();
2655 int rb = instr->RBValue();
2656 intptr_t ra_val = ra == 0 ? 0 : get_register(ra);
2657 intptr_t rb_val = get_register(rb);
2658 set_register(rt, ReadW(ra_val + rb_val, instr));
2659 break;
2660 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002661 case LDX:
2662 case LDUX: {
2663 int rt = instr->RTValue();
2664 int ra = instr->RAValue();
2665 int rb = instr->RBValue();
2666 intptr_t ra_val = ra == 0 ? 0 : get_register(ra);
2667 intptr_t rb_val = get_register(rb);
2668 intptr_t* result = ReadDW(ra_val + rb_val);
2669 set_register(rt, *result);
2670 if (opcode == LDUX) {
2671 DCHECK(ra != 0 && ra != rt);
2672 set_register(ra, ra_val + rb_val);
2673 }
2674 break;
2675 }
2676 case STDX:
2677 case STDUX: {
2678 int rs = instr->RSValue();
2679 int ra = instr->RAValue();
2680 int rb = instr->RBValue();
2681 intptr_t ra_val = ra == 0 ? 0 : get_register(ra);
2682 intptr_t rs_val = get_register(rs);
2683 intptr_t rb_val = get_register(rb);
2684 WriteDW(ra_val + rb_val, rs_val);
2685 if (opcode == STDUX) {
2686 DCHECK(ra != 0);
2687 set_register(ra, ra_val + rb_val);
2688 }
2689 break;
2690 }
2691#endif
2692 case LBZX:
2693 case LBZUX: {
2694 int rt = instr->RTValue();
2695 int ra = instr->RAValue();
2696 int rb = instr->RBValue();
2697 intptr_t ra_val = ra == 0 ? 0 : get_register(ra);
2698 intptr_t rb_val = get_register(rb);
2699 set_register(rt, ReadBU(ra_val + rb_val) & 0xFF);
2700 if (opcode == LBZUX) {
2701 DCHECK(ra != 0 && ra != rt);
2702 set_register(ra, ra_val + rb_val);
2703 }
2704 break;
2705 }
2706 case LHZX:
2707 case LHZUX: {
2708 int rt = instr->RTValue();
2709 int ra = instr->RAValue();
2710 int rb = instr->RBValue();
2711 intptr_t ra_val = ra == 0 ? 0 : get_register(ra);
2712 intptr_t rb_val = get_register(rb);
2713 set_register(rt, ReadHU(ra_val + rb_val, instr) & 0xFFFF);
2714 if (opcode == LHZUX) {
2715 DCHECK(ra != 0 && ra != rt);
2716 set_register(ra, ra_val + rb_val);
2717 }
2718 break;
2719 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002720 case LHAX:
2721 case LHAUX: {
2722 int rt = instr->RTValue();
2723 int ra = instr->RAValue();
2724 int rb = instr->RBValue();
2725 intptr_t ra_val = ra == 0 ? 0 : get_register(ra);
2726 intptr_t rb_val = get_register(rb);
2727 set_register(rt, ReadH(ra_val + rb_val, instr));
2728 if (opcode == LHAUX) {
2729 DCHECK(ra != 0 && ra != rt);
2730 set_register(ra, ra_val + rb_val);
2731 }
2732 break;
2733 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002734 case DCBF: {
2735 // todo - simulate dcbf
2736 break;
2737 }
2738 default: {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002739 found = false;
2740 break;
2741 }
2742 }
2743
2744 return found;
2745}
2746
2747
2748void Simulator::ExecuteExt2_5bit(Instruction* instr) {
2749 int opcode = instr->Bits(5, 1) << 1;
2750 switch (opcode) {
2751 case ISEL: {
2752 int rt = instr->RTValue();
2753 int ra = instr->RAValue();
2754 int rb = instr->RBValue();
2755 int condition_bit = instr->RCValue();
2756 int condition_mask = 0x80000000 >> condition_bit;
2757 intptr_t ra_val = (ra == 0) ? 0 : get_register(ra);
2758 intptr_t rb_val = get_register(rb);
2759 intptr_t value = (condition_reg_ & condition_mask) ? ra_val : rb_val;
2760 set_register(rt, value);
2761 break;
2762 }
2763 default: {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002764 PrintF("Unimplemented: %08x\n", instr->InstructionBits());
2765 UNIMPLEMENTED(); // Not used by V8.
2766 }
2767 }
2768}
2769
2770
2771void Simulator::ExecuteExt2(Instruction* instr) {
2772 // Check first the 10-1 bit versions
2773 if (ExecuteExt2_10bit(instr)) return;
2774 // Now look at the lesser encodings
2775 if (ExecuteExt2_9bit_part1(instr)) return;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002776 if (ExecuteExt2_9bit_part2(instr)) return;
2777 ExecuteExt2_5bit(instr);
2778}
2779
2780
2781void Simulator::ExecuteExt3(Instruction* instr) {
2782 int opcode = instr->Bits(10, 1) << 1;
2783 switch (opcode) {
2784 case FCFID: {
2785 // fcfids
2786 int frt = instr->RTValue();
2787 int frb = instr->RBValue();
2788 int64_t frb_val = get_d_register(frb);
2789 double frt_val = static_cast<float>(frb_val);
2790 set_d_register_from_double(frt, frt_val);
2791 return;
2792 }
2793 case FCFIDU: {
2794 // fcfidus
2795 int frt = instr->RTValue();
2796 int frb = instr->RBValue();
2797 uint64_t frb_val = get_d_register(frb);
2798 double frt_val = static_cast<float>(frb_val);
2799 set_d_register_from_double(frt, frt_val);
2800 return;
2801 }
2802 }
2803 UNIMPLEMENTED(); // Not used by V8.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002804}
2805
2806
2807void Simulator::ExecuteExt4(Instruction* instr) {
2808 switch (instr->Bits(5, 1) << 1) {
2809 case FDIV: {
2810 int frt = instr->RTValue();
2811 int fra = instr->RAValue();
2812 int frb = instr->RBValue();
2813 double fra_val = get_double_from_d_register(fra);
2814 double frb_val = get_double_from_d_register(frb);
2815 double frt_val = fra_val / frb_val;
2816 set_d_register_from_double(frt, frt_val);
2817 return;
2818 }
2819 case FSUB: {
2820 int frt = instr->RTValue();
2821 int fra = instr->RAValue();
2822 int frb = instr->RBValue();
2823 double fra_val = get_double_from_d_register(fra);
2824 double frb_val = get_double_from_d_register(frb);
2825 double frt_val = fra_val - frb_val;
2826 set_d_register_from_double(frt, frt_val);
2827 return;
2828 }
2829 case FADD: {
2830 int frt = instr->RTValue();
2831 int fra = instr->RAValue();
2832 int frb = instr->RBValue();
2833 double fra_val = get_double_from_d_register(fra);
2834 double frb_val = get_double_from_d_register(frb);
2835 double frt_val = fra_val + frb_val;
2836 set_d_register_from_double(frt, frt_val);
2837 return;
2838 }
2839 case FSQRT: {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002840 lazily_initialize_fast_sqrt(isolate_);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002841 int frt = instr->RTValue();
2842 int frb = instr->RBValue();
2843 double frb_val = get_double_from_d_register(frb);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002844 double frt_val = fast_sqrt(frb_val, isolate_);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002845 set_d_register_from_double(frt, frt_val);
2846 return;
2847 }
2848 case FSEL: {
2849 int frt = instr->RTValue();
2850 int fra = instr->RAValue();
2851 int frb = instr->RBValue();
2852 int frc = instr->RCValue();
2853 double fra_val = get_double_from_d_register(fra);
2854 double frb_val = get_double_from_d_register(frb);
2855 double frc_val = get_double_from_d_register(frc);
2856 double frt_val = ((fra_val >= 0.0) ? frc_val : frb_val);
2857 set_d_register_from_double(frt, frt_val);
2858 return;
2859 }
2860 case FMUL: {
2861 int frt = instr->RTValue();
2862 int fra = instr->RAValue();
2863 int frc = instr->RCValue();
2864 double fra_val = get_double_from_d_register(fra);
2865 double frc_val = get_double_from_d_register(frc);
2866 double frt_val = fra_val * frc_val;
2867 set_d_register_from_double(frt, frt_val);
2868 return;
2869 }
2870 case FMSUB: {
2871 int frt = instr->RTValue();
2872 int fra = instr->RAValue();
2873 int frb = instr->RBValue();
2874 int frc = instr->RCValue();
2875 double fra_val = get_double_from_d_register(fra);
2876 double frb_val = get_double_from_d_register(frb);
2877 double frc_val = get_double_from_d_register(frc);
2878 double frt_val = (fra_val * frc_val) - frb_val;
2879 set_d_register_from_double(frt, frt_val);
2880 return;
2881 }
2882 case FMADD: {
2883 int frt = instr->RTValue();
2884 int fra = instr->RAValue();
2885 int frb = instr->RBValue();
2886 int frc = instr->RCValue();
2887 double fra_val = get_double_from_d_register(fra);
2888 double frb_val = get_double_from_d_register(frb);
2889 double frc_val = get_double_from_d_register(frc);
2890 double frt_val = (fra_val * frc_val) + frb_val;
2891 set_d_register_from_double(frt, frt_val);
2892 return;
2893 }
2894 }
2895 int opcode = instr->Bits(10, 1) << 1;
2896 switch (opcode) {
2897 case FCMPU: {
2898 int fra = instr->RAValue();
2899 int frb = instr->RBValue();
2900 double fra_val = get_double_from_d_register(fra);
2901 double frb_val = get_double_from_d_register(frb);
2902 int cr = instr->Bits(25, 23);
2903 int bf = 0;
2904 if (fra_val < frb_val) {
2905 bf |= 0x80000000;
2906 }
2907 if (fra_val > frb_val) {
2908 bf |= 0x40000000;
2909 }
2910 if (fra_val == frb_val) {
2911 bf |= 0x20000000;
2912 }
2913 if (std::isunordered(fra_val, frb_val)) {
2914 bf |= 0x10000000;
2915 }
2916 int condition_mask = 0xF0000000 >> (cr * 4);
2917 int condition = bf >> (cr * 4);
2918 condition_reg_ = (condition_reg_ & ~condition_mask) | condition;
2919 return;
2920 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002921 case FRIN: {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002922 int frt = instr->RTValue();
2923 int frb = instr->RBValue();
2924 double frb_val = get_double_from_d_register(frb);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002925 double frt_val = std::round(frb_val);
2926 set_d_register_from_double(frt, frt_val);
2927 if (instr->Bit(0)) { // RC bit set
2928 // UNIMPLEMENTED();
2929 }
2930 return;
2931 }
2932 case FRIZ: {
2933 int frt = instr->RTValue();
2934 int frb = instr->RBValue();
2935 double frb_val = get_double_from_d_register(frb);
2936 double frt_val = std::trunc(frb_val);
2937 set_d_register_from_double(frt, frt_val);
2938 if (instr->Bit(0)) { // RC bit set
2939 // UNIMPLEMENTED();
2940 }
2941 return;
2942 }
2943 case FRIP: {
2944 int frt = instr->RTValue();
2945 int frb = instr->RBValue();
2946 double frb_val = get_double_from_d_register(frb);
2947 double frt_val = std::ceil(frb_val);
2948 set_d_register_from_double(frt, frt_val);
2949 if (instr->Bit(0)) { // RC bit set
2950 // UNIMPLEMENTED();
2951 }
2952 return;
2953 }
2954 case FRIM: {
2955 int frt = instr->RTValue();
2956 int frb = instr->RBValue();
2957 double frb_val = get_double_from_d_register(frb);
2958 double frt_val = std::floor(frb_val);
2959 set_d_register_from_double(frt, frt_val);
2960 if (instr->Bit(0)) { // RC bit set
2961 // UNIMPLEMENTED();
2962 }
2963 return;
2964 }
2965 case FRSP: {
2966 int frt = instr->RTValue();
2967 int frb = instr->RBValue();
2968 // frsp round 8-byte double-precision value to
2969 // single-precision value
2970 double frb_val = get_double_from_d_register(frb);
2971 double frt_val = static_cast<float>(frb_val);
2972 set_d_register_from_double(frt, frt_val);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002973 if (instr->Bit(0)) { // RC bit set
2974 // UNIMPLEMENTED();
2975 }
2976 return;
2977 }
2978 case FCFID: {
2979 int frt = instr->RTValue();
2980 int frb = instr->RBValue();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002981 int64_t frb_val = get_d_register(frb);
2982 double frt_val = static_cast<double>(frb_val);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002983 set_d_register_from_double(frt, frt_val);
2984 return;
2985 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002986 case FCFIDU: {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002987 int frt = instr->RTValue();
2988 int frb = instr->RBValue();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002989 uint64_t frb_val = get_d_register(frb);
2990 double frt_val = static_cast<double>(frb_val);
2991 set_d_register_from_double(frt, frt_val);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002992 return;
2993 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002994 case FCTID:
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002995 case FCTIDZ: {
2996 int frt = instr->RTValue();
2997 int frb = instr->RBValue();
2998 double frb_val = get_double_from_d_register(frb);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002999 int mode = (opcode == FCTIDZ) ? kRoundToZero
3000 : (fp_condition_reg_ & kFPRoundingModeMask);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003001 int64_t frt_val;
3002 int64_t one = 1; // work-around gcc
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003003 int64_t kMinVal = (one << 63);
3004 int64_t kMaxVal = kMinVal - 1;
3005 bool invalid_convert = false;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003006
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003007 if (std::isnan(frb_val)) {
3008 frt_val = kMinVal;
3009 invalid_convert = true;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003010 } else {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003011 switch (mode) {
3012 case kRoundToZero:
3013 frb_val = std::trunc(frb_val);
3014 break;
3015 case kRoundToPlusInf:
3016 frb_val = std::ceil(frb_val);
3017 break;
3018 case kRoundToMinusInf:
3019 frb_val = std::floor(frb_val);
3020 break;
3021 default:
3022 UNIMPLEMENTED(); // Not used by V8.
3023 break;
3024 }
3025 if (frb_val < static_cast<double>(kMinVal)) {
3026 frt_val = kMinVal;
3027 invalid_convert = true;
3028 } else if (frb_val >= static_cast<double>(kMaxVal)) {
3029 frt_val = kMaxVal;
3030 invalid_convert = true;
3031 } else {
3032 frt_val = (int64_t)frb_val;
3033 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003034 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003035 set_d_register(frt, frt_val);
3036 if (invalid_convert) SetFPSCR(VXCVI);
3037 return;
3038 }
3039 case FCTIDU:
3040 case FCTIDUZ: {
3041 int frt = instr->RTValue();
3042 int frb = instr->RBValue();
3043 double frb_val = get_double_from_d_register(frb);
3044 int mode = (opcode == FCTIDUZ)
3045 ? kRoundToZero
3046 : (fp_condition_reg_ & kFPRoundingModeMask);
3047 uint64_t frt_val;
3048 uint64_t kMinVal = 0;
3049 uint64_t kMaxVal = kMinVal - 1;
3050 bool invalid_convert = false;
3051
3052 if (std::isnan(frb_val)) {
3053 frt_val = kMinVal;
3054 invalid_convert = true;
3055 } else {
3056 switch (mode) {
3057 case kRoundToZero:
3058 frb_val = std::trunc(frb_val);
3059 break;
3060 case kRoundToPlusInf:
3061 frb_val = std::ceil(frb_val);
3062 break;
3063 case kRoundToMinusInf:
3064 frb_val = std::floor(frb_val);
3065 break;
3066 default:
3067 UNIMPLEMENTED(); // Not used by V8.
3068 break;
3069 }
3070 if (frb_val < static_cast<double>(kMinVal)) {
3071 frt_val = kMinVal;
3072 invalid_convert = true;
3073 } else if (frb_val >= static_cast<double>(kMaxVal)) {
3074 frt_val = kMaxVal;
3075 invalid_convert = true;
3076 } else {
3077 frt_val = (uint64_t)frb_val;
3078 }
3079 }
3080 set_d_register(frt, frt_val);
3081 if (invalid_convert) SetFPSCR(VXCVI);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003082 return;
3083 }
3084 case FCTIW:
3085 case FCTIWZ: {
3086 int frt = instr->RTValue();
3087 int frb = instr->RBValue();
3088 double frb_val = get_double_from_d_register(frb);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003089 int mode = (opcode == FCTIWZ) ? kRoundToZero
3090 : (fp_condition_reg_ & kFPRoundingModeMask);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003091 int64_t frt_val;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003092 int64_t kMinVal = kMinInt;
3093 int64_t kMaxVal = kMaxInt;
3094
3095 if (std::isnan(frb_val)) {
3096 frt_val = kMinVal;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003097 } else {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003098 switch (mode) {
3099 case kRoundToZero:
3100 frb_val = std::trunc(frb_val);
3101 break;
3102 case kRoundToPlusInf:
3103 frb_val = std::ceil(frb_val);
3104 break;
3105 case kRoundToMinusInf:
3106 frb_val = std::floor(frb_val);
3107 break;
3108 case kRoundToNearest: {
3109 double orig = frb_val;
3110 frb_val = lround(frb_val);
3111 // Round to even if exactly halfway. (lround rounds up)
3112 if (std::fabs(frb_val - orig) == 0.5 && ((int64_t)frb_val % 2)) {
3113 frb_val += ((frb_val > 0) ? -1.0 : 1.0);
3114 }
3115 break;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003116 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003117 default:
3118 UNIMPLEMENTED(); // Not used by V8.
3119 break;
3120 }
3121 if (frb_val < kMinVal) {
3122 frt_val = kMinVal;
3123 } else if (frb_val > kMaxVal) {
3124 frt_val = kMaxVal;
3125 } else {
3126 frt_val = (int64_t)frb_val;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003127 }
3128 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003129 set_d_register(frt, frt_val);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003130 return;
3131 }
3132 case FNEG: {
3133 int frt = instr->RTValue();
3134 int frb = instr->RBValue();
3135 double frb_val = get_double_from_d_register(frb);
3136 double frt_val = -frb_val;
3137 set_d_register_from_double(frt, frt_val);
3138 return;
3139 }
3140 case FMR: {
3141 int frt = instr->RTValue();
3142 int frb = instr->RBValue();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003143 int64_t frb_val = get_d_register(frb);
3144 set_d_register(frt, frb_val);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003145 return;
3146 }
3147 case MTFSFI: {
3148 int bf = instr->Bits(25, 23);
3149 int imm = instr->Bits(15, 12);
3150 int fp_condition_mask = 0xF0000000 >> (bf * 4);
3151 fp_condition_reg_ &= ~fp_condition_mask;
3152 fp_condition_reg_ |= (imm << (28 - (bf * 4)));
3153 if (instr->Bit(0)) { // RC bit set
3154 condition_reg_ &= 0xF0FFFFFF;
3155 condition_reg_ |= (imm << 23);
3156 }
3157 return;
3158 }
3159 case MTFSF: {
3160 int frb = instr->RBValue();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003161 int64_t frb_dval = get_d_register(frb);
3162 int32_t frb_ival = static_cast<int32_t>((frb_dval)&0xffffffff);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003163 int l = instr->Bits(25, 25);
3164 if (l == 1) {
3165 fp_condition_reg_ = frb_ival;
3166 } else {
3167 UNIMPLEMENTED();
3168 }
3169 if (instr->Bit(0)) { // RC bit set
3170 UNIMPLEMENTED();
3171 // int w = instr->Bits(16, 16);
3172 // int flm = instr->Bits(24, 17);
3173 }
3174 return;
3175 }
3176 case MFFS: {
3177 int frt = instr->RTValue();
3178 int64_t lval = static_cast<int64_t>(fp_condition_reg_);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003179 set_d_register(frt, lval);
3180 return;
3181 }
3182 case MCRFS: {
3183 int bf = instr->Bits(25, 23);
3184 int bfa = instr->Bits(20, 18);
3185 int cr_shift = (7 - bf) * CRWIDTH;
3186 int fp_shift = (7 - bfa) * CRWIDTH;
3187 int field_val = (fp_condition_reg_ >> fp_shift) & 0xf;
3188 condition_reg_ &= ~(0x0f << cr_shift);
3189 condition_reg_ |= (field_val << cr_shift);
3190 // Clear copied exception bits
3191 switch (bfa) {
3192 case 5:
3193 ClearFPSCR(VXSOFT);
3194 ClearFPSCR(VXSQRT);
3195 ClearFPSCR(VXCVI);
3196 break;
3197 default:
3198 UNIMPLEMENTED();
3199 break;
3200 }
3201 return;
3202 }
3203 case MTFSB0: {
3204 int bt = instr->Bits(25, 21);
3205 ClearFPSCR(bt);
3206 if (instr->Bit(0)) { // RC bit set
3207 UNIMPLEMENTED();
3208 }
3209 return;
3210 }
3211 case MTFSB1: {
3212 int bt = instr->Bits(25, 21);
3213 SetFPSCR(bt);
3214 if (instr->Bit(0)) { // RC bit set
3215 UNIMPLEMENTED();
3216 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003217 return;
3218 }
3219 case FABS: {
3220 int frt = instr->RTValue();
3221 int frb = instr->RBValue();
3222 double frb_val = get_double_from_d_register(frb);
3223 double frt_val = std::fabs(frb_val);
3224 set_d_register_from_double(frt, frt_val);
3225 return;
3226 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003227 }
3228 UNIMPLEMENTED(); // Not used by V8.
3229}
3230
3231#if V8_TARGET_ARCH_PPC64
3232void Simulator::ExecuteExt5(Instruction* instr) {
3233 switch (instr->Bits(4, 2) << 2) {
3234 case RLDICL: {
3235 int ra = instr->RAValue();
3236 int rs = instr->RSValue();
3237 uintptr_t rs_val = get_register(rs);
3238 int sh = (instr->Bits(15, 11) | (instr->Bit(1) << 5));
3239 int mb = (instr->Bits(10, 6) | (instr->Bit(5) << 5));
3240 DCHECK(sh >= 0 && sh <= 63);
3241 DCHECK(mb >= 0 && mb <= 63);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003242 uintptr_t result = base::bits::RotateLeft64(rs_val, sh);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003243 uintptr_t mask = 0xffffffffffffffff >> mb;
3244 result &= mask;
3245 set_register(ra, result);
3246 if (instr->Bit(0)) { // RC bit set
3247 SetCR0(result);
3248 }
3249 return;
3250 }
3251 case RLDICR: {
3252 int ra = instr->RAValue();
3253 int rs = instr->RSValue();
3254 uintptr_t rs_val = get_register(rs);
3255 int sh = (instr->Bits(15, 11) | (instr->Bit(1) << 5));
3256 int me = (instr->Bits(10, 6) | (instr->Bit(5) << 5));
3257 DCHECK(sh >= 0 && sh <= 63);
3258 DCHECK(me >= 0 && me <= 63);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003259 uintptr_t result = base::bits::RotateLeft64(rs_val, sh);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003260 uintptr_t mask = 0xffffffffffffffff << (63 - me);
3261 result &= mask;
3262 set_register(ra, result);
3263 if (instr->Bit(0)) { // RC bit set
3264 SetCR0(result);
3265 }
3266 return;
3267 }
3268 case RLDIC: {
3269 int ra = instr->RAValue();
3270 int rs = instr->RSValue();
3271 uintptr_t rs_val = get_register(rs);
3272 int sh = (instr->Bits(15, 11) | (instr->Bit(1) << 5));
3273 int mb = (instr->Bits(10, 6) | (instr->Bit(5) << 5));
3274 DCHECK(sh >= 0 && sh <= 63);
3275 DCHECK(mb >= 0 && mb <= 63);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003276 uintptr_t result = base::bits::RotateLeft64(rs_val, sh);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003277 uintptr_t mask = (0xffffffffffffffff >> mb) & (0xffffffffffffffff << sh);
3278 result &= mask;
3279 set_register(ra, result);
3280 if (instr->Bit(0)) { // RC bit set
3281 SetCR0(result);
3282 }
3283 return;
3284 }
3285 case RLDIMI: {
3286 int ra = instr->RAValue();
3287 int rs = instr->RSValue();
3288 uintptr_t rs_val = get_register(rs);
3289 intptr_t ra_val = get_register(ra);
3290 int sh = (instr->Bits(15, 11) | (instr->Bit(1) << 5));
3291 int mb = (instr->Bits(10, 6) | (instr->Bit(5) << 5));
3292 int me = 63 - sh;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003293 uintptr_t result = base::bits::RotateLeft64(rs_val, sh);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003294 uintptr_t mask = 0;
3295 if (mb < me + 1) {
3296 uintptr_t bit = 0x8000000000000000 >> mb;
3297 for (; mb <= me; mb++) {
3298 mask |= bit;
3299 bit >>= 1;
3300 }
3301 } else if (mb == me + 1) {
3302 mask = 0xffffffffffffffff;
3303 } else { // mb > me+1
3304 uintptr_t bit = 0x8000000000000000 >> (me + 1); // needs to be tested
3305 mask = 0xffffffffffffffff;
3306 for (; me < mb; me++) {
3307 mask ^= bit;
3308 bit >>= 1;
3309 }
3310 }
3311 result &= mask;
3312 ra_val &= ~mask;
3313 result |= ra_val;
3314 set_register(ra, result);
3315 if (instr->Bit(0)) { // RC bit set
3316 SetCR0(result);
3317 }
3318 return;
3319 }
3320 }
3321 switch (instr->Bits(4, 1) << 1) {
3322 case RLDCL: {
3323 int ra = instr->RAValue();
3324 int rs = instr->RSValue();
3325 int rb = instr->RBValue();
3326 uintptr_t rs_val = get_register(rs);
3327 uintptr_t rb_val = get_register(rb);
3328 int sh = (rb_val & 0x3f);
3329 int mb = (instr->Bits(10, 6) | (instr->Bit(5) << 5));
3330 DCHECK(sh >= 0 && sh <= 63);
3331 DCHECK(mb >= 0 && mb <= 63);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003332 uintptr_t result = base::bits::RotateLeft64(rs_val, sh);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003333 uintptr_t mask = 0xffffffffffffffff >> mb;
3334 result &= mask;
3335 set_register(ra, result);
3336 if (instr->Bit(0)) { // RC bit set
3337 SetCR0(result);
3338 }
3339 return;
3340 }
3341 }
3342 UNIMPLEMENTED(); // Not used by V8.
3343}
3344#endif
3345
3346
3347void Simulator::ExecuteGeneric(Instruction* instr) {
3348 int opcode = instr->OpcodeValue() << 26;
3349 switch (opcode) {
3350 case SUBFIC: {
3351 int rt = instr->RTValue();
3352 int ra = instr->RAValue();
3353 intptr_t ra_val = get_register(ra);
3354 int32_t im_val = instr->Bits(15, 0);
3355 im_val = SIGN_EXT_IMM16(im_val);
3356 intptr_t alu_out = im_val - ra_val;
3357 set_register(rt, alu_out);
3358 // todo - handle RC bit
3359 break;
3360 }
3361 case CMPLI: {
3362 int ra = instr->RAValue();
3363 uint32_t im_val = instr->Bits(15, 0);
3364 int cr = instr->Bits(25, 23);
3365 uint32_t bf = 0;
3366#if V8_TARGET_ARCH_PPC64
3367 int L = instr->Bit(21);
3368 if (L) {
3369#endif
3370 uintptr_t ra_val = get_register(ra);
3371 if (ra_val < im_val) {
3372 bf |= 0x80000000;
3373 }
3374 if (ra_val > im_val) {
3375 bf |= 0x40000000;
3376 }
3377 if (ra_val == im_val) {
3378 bf |= 0x20000000;
3379 }
3380#if V8_TARGET_ARCH_PPC64
3381 } else {
3382 uint32_t ra_val = get_register(ra);
3383 if (ra_val < im_val) {
3384 bf |= 0x80000000;
3385 }
3386 if (ra_val > im_val) {
3387 bf |= 0x40000000;
3388 }
3389 if (ra_val == im_val) {
3390 bf |= 0x20000000;
3391 }
3392 }
3393#endif
3394 uint32_t condition_mask = 0xF0000000U >> (cr * 4);
3395 uint32_t condition = bf >> (cr * 4);
3396 condition_reg_ = (condition_reg_ & ~condition_mask) | condition;
3397 break;
3398 }
3399 case CMPI: {
3400 int ra = instr->RAValue();
3401 int32_t im_val = instr->Bits(15, 0);
3402 im_val = SIGN_EXT_IMM16(im_val);
3403 int cr = instr->Bits(25, 23);
3404 uint32_t bf = 0;
3405#if V8_TARGET_ARCH_PPC64
3406 int L = instr->Bit(21);
3407 if (L) {
3408#endif
3409 intptr_t ra_val = get_register(ra);
3410 if (ra_val < im_val) {
3411 bf |= 0x80000000;
3412 }
3413 if (ra_val > im_val) {
3414 bf |= 0x40000000;
3415 }
3416 if (ra_val == im_val) {
3417 bf |= 0x20000000;
3418 }
3419#if V8_TARGET_ARCH_PPC64
3420 } else {
3421 int32_t ra_val = get_register(ra);
3422 if (ra_val < im_val) {
3423 bf |= 0x80000000;
3424 }
3425 if (ra_val > im_val) {
3426 bf |= 0x40000000;
3427 }
3428 if (ra_val == im_val) {
3429 bf |= 0x20000000;
3430 }
3431 }
3432#endif
3433 uint32_t condition_mask = 0xF0000000U >> (cr * 4);
3434 uint32_t condition = bf >> (cr * 4);
3435 condition_reg_ = (condition_reg_ & ~condition_mask) | condition;
3436 break;
3437 }
3438 case ADDIC: {
3439 int rt = instr->RTValue();
3440 int ra = instr->RAValue();
3441 uintptr_t ra_val = get_register(ra);
3442 uintptr_t im_val = SIGN_EXT_IMM16(instr->Bits(15, 0));
3443 uintptr_t alu_out = ra_val + im_val;
3444 // Check overflow
3445 if (~ra_val < im_val) {
3446 special_reg_xer_ = (special_reg_xer_ & ~0xF0000000) | 0x20000000;
3447 } else {
3448 special_reg_xer_ &= ~0xF0000000;
3449 }
3450 set_register(rt, alu_out);
3451 break;
3452 }
3453 case ADDI: {
3454 int rt = instr->RTValue();
3455 int ra = instr->RAValue();
3456 int32_t im_val = SIGN_EXT_IMM16(instr->Bits(15, 0));
3457 intptr_t alu_out;
3458 if (ra == 0) {
3459 alu_out = im_val;
3460 } else {
3461 intptr_t ra_val = get_register(ra);
3462 alu_out = ra_val + im_val;
3463 }
3464 set_register(rt, alu_out);
3465 // todo - handle RC bit
3466 break;
3467 }
3468 case ADDIS: {
3469 int rt = instr->RTValue();
3470 int ra = instr->RAValue();
3471 int32_t im_val = (instr->Bits(15, 0) << 16);
3472 intptr_t alu_out;
3473 if (ra == 0) { // treat r0 as zero
3474 alu_out = im_val;
3475 } else {
3476 intptr_t ra_val = get_register(ra);
3477 alu_out = ra_val + im_val;
3478 }
3479 set_register(rt, alu_out);
3480 break;
3481 }
3482 case BCX: {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003483 ExecuteBranchConditional(instr, BC_OFFSET);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003484 break;
3485 }
3486 case BX: {
3487 int offset = (instr->Bits(25, 2) << 8) >> 6;
3488 if (instr->Bit(0) == 1) { // LK flag set
3489 special_reg_lr_ = get_pc() + 4;
3490 }
3491 set_pc(get_pc() + offset);
3492 // todo - AA flag
3493 break;
3494 }
3495 case EXT1: {
3496 ExecuteExt1(instr);
3497 break;
3498 }
3499 case RLWIMIX: {
3500 int ra = instr->RAValue();
3501 int rs = instr->RSValue();
3502 uint32_t rs_val = get_register(rs);
3503 int32_t ra_val = get_register(ra);
3504 int sh = instr->Bits(15, 11);
3505 int mb = instr->Bits(10, 6);
3506 int me = instr->Bits(5, 1);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003507 uint32_t result = base::bits::RotateLeft32(rs_val, sh);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003508 int mask = 0;
3509 if (mb < me + 1) {
3510 int bit = 0x80000000 >> mb;
3511 for (; mb <= me; mb++) {
3512 mask |= bit;
3513 bit >>= 1;
3514 }
3515 } else if (mb == me + 1) {
3516 mask = 0xffffffff;
3517 } else { // mb > me+1
3518 int bit = 0x80000000 >> (me + 1); // needs to be tested
3519 mask = 0xffffffff;
3520 for (; me < mb; me++) {
3521 mask ^= bit;
3522 bit >>= 1;
3523 }
3524 }
3525 result &= mask;
3526 ra_val &= ~mask;
3527 result |= ra_val;
3528 set_register(ra, result);
3529 if (instr->Bit(0)) { // RC bit set
3530 SetCR0(result);
3531 }
3532 break;
3533 }
3534 case RLWINMX:
3535 case RLWNMX: {
3536 int ra = instr->RAValue();
3537 int rs = instr->RSValue();
3538 uint32_t rs_val = get_register(rs);
3539 int sh = 0;
3540 if (opcode == RLWINMX) {
3541 sh = instr->Bits(15, 11);
3542 } else {
3543 int rb = instr->RBValue();
3544 uint32_t rb_val = get_register(rb);
3545 sh = (rb_val & 0x1f);
3546 }
3547 int mb = instr->Bits(10, 6);
3548 int me = instr->Bits(5, 1);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003549 uint32_t result = base::bits::RotateLeft32(rs_val, sh);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003550 int mask = 0;
3551 if (mb < me + 1) {
3552 int bit = 0x80000000 >> mb;
3553 for (; mb <= me; mb++) {
3554 mask |= bit;
3555 bit >>= 1;
3556 }
3557 } else if (mb == me + 1) {
3558 mask = 0xffffffff;
3559 } else { // mb > me+1
3560 int bit = 0x80000000 >> (me + 1); // needs to be tested
3561 mask = 0xffffffff;
3562 for (; me < mb; me++) {
3563 mask ^= bit;
3564 bit >>= 1;
3565 }
3566 }
3567 result &= mask;
3568 set_register(ra, result);
3569 if (instr->Bit(0)) { // RC bit set
3570 SetCR0(result);
3571 }
3572 break;
3573 }
3574 case ORI: {
3575 int rs = instr->RSValue();
3576 int ra = instr->RAValue();
3577 intptr_t rs_val = get_register(rs);
3578 uint32_t im_val = instr->Bits(15, 0);
3579 intptr_t alu_out = rs_val | im_val;
3580 set_register(ra, alu_out);
3581 break;
3582 }
3583 case ORIS: {
3584 int rs = instr->RSValue();
3585 int ra = instr->RAValue();
3586 intptr_t rs_val = get_register(rs);
3587 uint32_t im_val = instr->Bits(15, 0);
3588 intptr_t alu_out = rs_val | (im_val << 16);
3589 set_register(ra, alu_out);
3590 break;
3591 }
3592 case XORI: {
3593 int rs = instr->RSValue();
3594 int ra = instr->RAValue();
3595 intptr_t rs_val = get_register(rs);
3596 uint32_t im_val = instr->Bits(15, 0);
3597 intptr_t alu_out = rs_val ^ im_val;
3598 set_register(ra, alu_out);
3599 // todo - set condition based SO bit
3600 break;
3601 }
3602 case XORIS: {
3603 int rs = instr->RSValue();
3604 int ra = instr->RAValue();
3605 intptr_t rs_val = get_register(rs);
3606 uint32_t im_val = instr->Bits(15, 0);
3607 intptr_t alu_out = rs_val ^ (im_val << 16);
3608 set_register(ra, alu_out);
3609 break;
3610 }
3611 case ANDIx: {
3612 int rs = instr->RSValue();
3613 int ra = instr->RAValue();
3614 intptr_t rs_val = get_register(rs);
3615 uint32_t im_val = instr->Bits(15, 0);
3616 intptr_t alu_out = rs_val & im_val;
3617 set_register(ra, alu_out);
3618 SetCR0(alu_out);
3619 break;
3620 }
3621 case ANDISx: {
3622 int rs = instr->RSValue();
3623 int ra = instr->RAValue();
3624 intptr_t rs_val = get_register(rs);
3625 uint32_t im_val = instr->Bits(15, 0);
3626 intptr_t alu_out = rs_val & (im_val << 16);
3627 set_register(ra, alu_out);
3628 SetCR0(alu_out);
3629 break;
3630 }
3631 case EXT2: {
3632 ExecuteExt2(instr);
3633 break;
3634 }
3635
3636 case LWZU:
3637 case LWZ: {
3638 int ra = instr->RAValue();
3639 int rt = instr->RTValue();
3640 intptr_t ra_val = ra == 0 ? 0 : get_register(ra);
3641 int offset = SIGN_EXT_IMM16(instr->Bits(15, 0));
3642 set_register(rt, ReadWU(ra_val + offset, instr));
3643 if (opcode == LWZU) {
3644 DCHECK(ra != 0);
3645 set_register(ra, ra_val + offset);
3646 }
3647 break;
3648 }
3649
3650 case LBZU:
3651 case LBZ: {
3652 int ra = instr->RAValue();
3653 int rt = instr->RTValue();
3654 intptr_t ra_val = ra == 0 ? 0 : get_register(ra);
3655 int offset = SIGN_EXT_IMM16(instr->Bits(15, 0));
3656 set_register(rt, ReadB(ra_val + offset) & 0xFF);
3657 if (opcode == LBZU) {
3658 DCHECK(ra != 0);
3659 set_register(ra, ra_val + offset);
3660 }
3661 break;
3662 }
3663
3664 case STWU:
3665 case STW: {
3666 int ra = instr->RAValue();
3667 int rs = instr->RSValue();
3668 intptr_t ra_val = ra == 0 ? 0 : get_register(ra);
3669 int32_t rs_val = get_register(rs);
3670 int offset = SIGN_EXT_IMM16(instr->Bits(15, 0));
3671 WriteW(ra_val + offset, rs_val, instr);
3672 if (opcode == STWU) {
3673 DCHECK(ra != 0);
3674 set_register(ra, ra_val + offset);
3675 }
3676 // printf("r%d %08x -> %08x\n", rs, rs_val, offset); // 0xdead
3677 break;
3678 }
3679
3680 case STBU:
3681 case STB: {
3682 int ra = instr->RAValue();
3683 int rs = instr->RSValue();
3684 intptr_t ra_val = ra == 0 ? 0 : get_register(ra);
3685 int8_t rs_val = get_register(rs);
3686 int offset = SIGN_EXT_IMM16(instr->Bits(15, 0));
3687 WriteB(ra_val + offset, rs_val);
3688 if (opcode == STBU) {
3689 DCHECK(ra != 0);
3690 set_register(ra, ra_val + offset);
3691 }
3692 break;
3693 }
3694
3695 case LHZU:
3696 case LHZ: {
3697 int ra = instr->RAValue();
3698 int rt = instr->RTValue();
3699 intptr_t ra_val = ra == 0 ? 0 : get_register(ra);
3700 int offset = SIGN_EXT_IMM16(instr->Bits(15, 0));
3701 uintptr_t result = ReadHU(ra_val + offset, instr) & 0xffff;
3702 set_register(rt, result);
3703 if (opcode == LHZU) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003704 set_register(ra, ra_val + offset);
3705 }
3706 break;
3707 }
3708
3709 case LHA:
3710 case LHAU: {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003711 int ra = instr->RAValue();
3712 int rt = instr->RTValue();
3713 intptr_t ra_val = ra == 0 ? 0 : get_register(ra);
3714 int offset = SIGN_EXT_IMM16(instr->Bits(15, 0));
3715 intptr_t result = ReadH(ra_val + offset, instr);
3716 set_register(rt, result);
3717 if (opcode == LHAU) {
3718 set_register(ra, ra_val + offset);
3719 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003720 break;
3721 }
3722
3723 case STHU:
3724 case STH: {
3725 int ra = instr->RAValue();
3726 int rs = instr->RSValue();
3727 intptr_t ra_val = ra == 0 ? 0 : get_register(ra);
3728 int16_t rs_val = get_register(rs);
3729 int offset = SIGN_EXT_IMM16(instr->Bits(15, 0));
3730 WriteH(ra_val + offset, rs_val, instr);
3731 if (opcode == STHU) {
3732 DCHECK(ra != 0);
3733 set_register(ra, ra_val + offset);
3734 }
3735 break;
3736 }
3737
3738 case LMW:
3739 case STMW: {
3740 UNIMPLEMENTED();
3741 break;
3742 }
3743
3744 case LFSU:
3745 case LFS: {
3746 int frt = instr->RTValue();
3747 int ra = instr->RAValue();
3748 int32_t offset = SIGN_EXT_IMM16(instr->Bits(15, 0));
3749 intptr_t ra_val = ra == 0 ? 0 : get_register(ra);
3750 int32_t val = ReadW(ra_val + offset, instr);
3751 float* fptr = reinterpret_cast<float*>(&val);
3752 set_d_register_from_double(frt, static_cast<double>(*fptr));
3753 if (opcode == LFSU) {
3754 DCHECK(ra != 0);
3755 set_register(ra, ra_val + offset);
3756 }
3757 break;
3758 }
3759
3760 case LFDU:
3761 case LFD: {
3762 int frt = instr->RTValue();
3763 int ra = instr->RAValue();
3764 int32_t offset = SIGN_EXT_IMM16(instr->Bits(15, 0));
3765 intptr_t ra_val = ra == 0 ? 0 : get_register(ra);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003766 int64_t* dptr = reinterpret_cast<int64_t*>(ReadDW(ra_val + offset));
3767 set_d_register(frt, *dptr);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003768 if (opcode == LFDU) {
3769 DCHECK(ra != 0);
3770 set_register(ra, ra_val + offset);
3771 }
3772 break;
3773 }
3774
3775 case STFSU: {
3776 case STFS:
3777 int frs = instr->RSValue();
3778 int ra = instr->RAValue();
3779 int32_t offset = SIGN_EXT_IMM16(instr->Bits(15, 0));
3780 intptr_t ra_val = ra == 0 ? 0 : get_register(ra);
3781 float frs_val = static_cast<float>(get_double_from_d_register(frs));
3782 int32_t* p = reinterpret_cast<int32_t*>(&frs_val);
3783 WriteW(ra_val + offset, *p, instr);
3784 if (opcode == STFSU) {
3785 DCHECK(ra != 0);
3786 set_register(ra, ra_val + offset);
3787 }
3788 break;
3789 }
3790
3791 case STFDU:
3792 case STFD: {
3793 int frs = instr->RSValue();
3794 int ra = instr->RAValue();
3795 int32_t offset = SIGN_EXT_IMM16(instr->Bits(15, 0));
3796 intptr_t ra_val = ra == 0 ? 0 : get_register(ra);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003797 int64_t frs_val = get_d_register(frs);
3798 WriteDW(ra_val + offset, frs_val);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003799 if (opcode == STFDU) {
3800 DCHECK(ra != 0);
3801 set_register(ra, ra_val + offset);
3802 }
3803 break;
3804 }
3805
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003806 case EXT3: {
3807 ExecuteExt3(instr);
3808 break;
3809 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003810 case EXT4: {
3811 ExecuteExt4(instr);
3812 break;
3813 }
3814
3815#if V8_TARGET_ARCH_PPC64
3816 case EXT5: {
3817 ExecuteExt5(instr);
3818 break;
3819 }
3820 case LD: {
3821 int ra = instr->RAValue();
3822 int rt = instr->RTValue();
3823 int64_t ra_val = ra == 0 ? 0 : get_register(ra);
3824 int offset = SIGN_EXT_IMM16(instr->Bits(15, 0) & ~3);
3825 switch (instr->Bits(1, 0)) {
3826 case 0: { // ld
3827 intptr_t* result = ReadDW(ra_val + offset);
3828 set_register(rt, *result);
3829 break;
3830 }
3831 case 1: { // ldu
3832 intptr_t* result = ReadDW(ra_val + offset);
3833 set_register(rt, *result);
3834 DCHECK(ra != 0);
3835 set_register(ra, ra_val + offset);
3836 break;
3837 }
3838 case 2: { // lwa
3839 intptr_t result = ReadW(ra_val + offset, instr);
3840 set_register(rt, result);
3841 break;
3842 }
3843 }
3844 break;
3845 }
3846
3847 case STD: {
3848 int ra = instr->RAValue();
3849 int rs = instr->RSValue();
3850 int64_t ra_val = ra == 0 ? 0 : get_register(ra);
3851 int64_t rs_val = get_register(rs);
3852 int offset = SIGN_EXT_IMM16(instr->Bits(15, 0) & ~3);
3853 WriteDW(ra_val + offset, rs_val);
3854 if (instr->Bit(0) == 1) { // This is the STDU form
3855 DCHECK(ra != 0);
3856 set_register(ra, ra_val + offset);
3857 }
3858 break;
3859 }
3860#endif
3861
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003862 default: {
3863 UNIMPLEMENTED();
3864 break;
3865 }
3866 }
3867} // NOLINT
3868
3869
3870void Simulator::Trace(Instruction* instr) {
3871 disasm::NameConverter converter;
3872 disasm::Disassembler dasm(converter);
3873 // use a reasonably large buffer
3874 v8::internal::EmbeddedVector<char, 256> buffer;
3875 dasm.InstructionDecode(buffer, reinterpret_cast<byte*>(instr));
3876 PrintF("%05d %08" V8PRIxPTR " %s\n", icount_,
3877 reinterpret_cast<intptr_t>(instr), buffer.start());
3878}
3879
3880
3881// Executes the current instruction.
3882void Simulator::ExecuteInstruction(Instruction* instr) {
3883 if (v8::internal::FLAG_check_icache) {
3884 CheckICache(isolate_->simulator_i_cache(), instr);
3885 }
3886 pc_modified_ = false;
3887 if (::v8::internal::FLAG_trace_sim) {
3888 Trace(instr);
3889 }
3890 int opcode = instr->OpcodeValue() << 26;
3891 if (opcode == TWI) {
3892 SoftwareInterrupt(instr);
3893 } else {
3894 ExecuteGeneric(instr);
3895 }
3896 if (!pc_modified_) {
3897 set_pc(reinterpret_cast<intptr_t>(instr) + Instruction::kInstrSize);
3898 }
3899}
3900
3901
3902void Simulator::Execute() {
3903 // Get the PC to simulate. Cannot use the accessor here as we need the
3904 // raw PC value and not the one used as input to arithmetic instructions.
3905 intptr_t program_counter = get_pc();
3906
3907 if (::v8::internal::FLAG_stop_sim_at == 0) {
3908 // Fast version of the dispatch loop without checking whether the simulator
3909 // should be stopping at a particular executed instruction.
3910 while (program_counter != end_sim_pc) {
3911 Instruction* instr = reinterpret_cast<Instruction*>(program_counter);
3912 icount_++;
3913 ExecuteInstruction(instr);
3914 program_counter = get_pc();
3915 }
3916 } else {
3917 // FLAG_stop_sim_at is at the non-default value. Stop in the debugger when
3918 // we reach the particular instuction count.
3919 while (program_counter != end_sim_pc) {
3920 Instruction* instr = reinterpret_cast<Instruction*>(program_counter);
3921 icount_++;
3922 if (icount_ == ::v8::internal::FLAG_stop_sim_at) {
3923 PPCDebugger dbg(this);
3924 dbg.Debug();
3925 } else {
3926 ExecuteInstruction(instr);
3927 }
3928 program_counter = get_pc();
3929 }
3930 }
3931}
3932
3933
3934void Simulator::CallInternal(byte* entry) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003935 // Adjust JS-based stack limit to C-based stack limit.
3936 isolate_->stack_guard()->AdjustStackLimitForSimulator();
3937
Ben Murdoch097c5b22016-05-18 11:27:45 +01003938 // Prepare to execute the code at entry
3939 if (ABI_USES_FUNCTION_DESCRIPTORS) {
3940 // entry is the function descriptor
3941 set_pc(*(reinterpret_cast<intptr_t*>(entry)));
3942 } else {
3943 // entry is the instruction address
3944 set_pc(reinterpret_cast<intptr_t>(entry));
3945 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003946
Ben Murdoch097c5b22016-05-18 11:27:45 +01003947 if (ABI_CALL_VIA_IP) {
3948 // Put target address in ip (for JS prologue).
3949 set_register(r12, get_pc());
3950 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003951
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003952 // Put down marker for end of simulation. The simulator will stop simulation
3953 // when the PC reaches this value. By saving the "end simulation" value into
3954 // the LR the simulation stops when returning to this call point.
3955 special_reg_lr_ = end_sim_pc;
3956
3957 // Remember the values of non-volatile registers.
3958 intptr_t r2_val = get_register(r2);
3959 intptr_t r13_val = get_register(r13);
3960 intptr_t r14_val = get_register(r14);
3961 intptr_t r15_val = get_register(r15);
3962 intptr_t r16_val = get_register(r16);
3963 intptr_t r17_val = get_register(r17);
3964 intptr_t r18_val = get_register(r18);
3965 intptr_t r19_val = get_register(r19);
3966 intptr_t r20_val = get_register(r20);
3967 intptr_t r21_val = get_register(r21);
3968 intptr_t r22_val = get_register(r22);
3969 intptr_t r23_val = get_register(r23);
3970 intptr_t r24_val = get_register(r24);
3971 intptr_t r25_val = get_register(r25);
3972 intptr_t r26_val = get_register(r26);
3973 intptr_t r27_val = get_register(r27);
3974 intptr_t r28_val = get_register(r28);
3975 intptr_t r29_val = get_register(r29);
3976 intptr_t r30_val = get_register(r30);
3977 intptr_t r31_val = get_register(fp);
3978
3979 // Set up the non-volatile registers with a known value. To be able to check
3980 // that they are preserved properly across JS execution.
3981 intptr_t callee_saved_value = icount_;
3982 set_register(r2, callee_saved_value);
3983 set_register(r13, callee_saved_value);
3984 set_register(r14, callee_saved_value);
3985 set_register(r15, callee_saved_value);
3986 set_register(r16, callee_saved_value);
3987 set_register(r17, callee_saved_value);
3988 set_register(r18, callee_saved_value);
3989 set_register(r19, callee_saved_value);
3990 set_register(r20, callee_saved_value);
3991 set_register(r21, callee_saved_value);
3992 set_register(r22, callee_saved_value);
3993 set_register(r23, callee_saved_value);
3994 set_register(r24, callee_saved_value);
3995 set_register(r25, callee_saved_value);
3996 set_register(r26, callee_saved_value);
3997 set_register(r27, callee_saved_value);
3998 set_register(r28, callee_saved_value);
3999 set_register(r29, callee_saved_value);
4000 set_register(r30, callee_saved_value);
4001 set_register(fp, callee_saved_value);
4002
4003 // Start the simulation
4004 Execute();
4005
4006 // Check that the non-volatile registers have been preserved.
Ben Murdoch097c5b22016-05-18 11:27:45 +01004007 if (ABI_TOC_REGISTER != 2) {
4008 CHECK_EQ(callee_saved_value, get_register(r2));
4009 }
4010 if (ABI_TOC_REGISTER != 13) {
4011 CHECK_EQ(callee_saved_value, get_register(r13));
4012 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004013 CHECK_EQ(callee_saved_value, get_register(r14));
4014 CHECK_EQ(callee_saved_value, get_register(r15));
4015 CHECK_EQ(callee_saved_value, get_register(r16));
4016 CHECK_EQ(callee_saved_value, get_register(r17));
4017 CHECK_EQ(callee_saved_value, get_register(r18));
4018 CHECK_EQ(callee_saved_value, get_register(r19));
4019 CHECK_EQ(callee_saved_value, get_register(r20));
4020 CHECK_EQ(callee_saved_value, get_register(r21));
4021 CHECK_EQ(callee_saved_value, get_register(r22));
4022 CHECK_EQ(callee_saved_value, get_register(r23));
4023 CHECK_EQ(callee_saved_value, get_register(r24));
4024 CHECK_EQ(callee_saved_value, get_register(r25));
4025 CHECK_EQ(callee_saved_value, get_register(r26));
4026 CHECK_EQ(callee_saved_value, get_register(r27));
4027 CHECK_EQ(callee_saved_value, get_register(r28));
4028 CHECK_EQ(callee_saved_value, get_register(r29));
4029 CHECK_EQ(callee_saved_value, get_register(r30));
4030 CHECK_EQ(callee_saved_value, get_register(fp));
4031
4032 // Restore non-volatile registers with the original value.
4033 set_register(r2, r2_val);
4034 set_register(r13, r13_val);
4035 set_register(r14, r14_val);
4036 set_register(r15, r15_val);
4037 set_register(r16, r16_val);
4038 set_register(r17, r17_val);
4039 set_register(r18, r18_val);
4040 set_register(r19, r19_val);
4041 set_register(r20, r20_val);
4042 set_register(r21, r21_val);
4043 set_register(r22, r22_val);
4044 set_register(r23, r23_val);
4045 set_register(r24, r24_val);
4046 set_register(r25, r25_val);
4047 set_register(r26, r26_val);
4048 set_register(r27, r27_val);
4049 set_register(r28, r28_val);
4050 set_register(r29, r29_val);
4051 set_register(r30, r30_val);
4052 set_register(fp, r31_val);
4053}
4054
4055
4056intptr_t Simulator::Call(byte* entry, int argument_count, ...) {
4057 va_list parameters;
4058 va_start(parameters, argument_count);
4059 // Set up arguments
4060
4061 // First eight arguments passed in registers r3-r10.
4062 int reg_arg_count = (argument_count > 8) ? 8 : argument_count;
4063 int stack_arg_count = argument_count - reg_arg_count;
4064 for (int i = 0; i < reg_arg_count; i++) {
4065 set_register(i + 3, va_arg(parameters, intptr_t));
4066 }
4067
4068 // Remaining arguments passed on stack.
4069 intptr_t original_stack = get_register(sp);
4070 // Compute position of stack on entry to generated code.
4071 intptr_t entry_stack =
4072 (original_stack -
4073 (kNumRequiredStackFrameSlots + stack_arg_count) * sizeof(intptr_t));
4074 if (base::OS::ActivationFrameAlignment() != 0) {
4075 entry_stack &= -base::OS::ActivationFrameAlignment();
4076 }
4077 // Store remaining arguments on stack, from low to high memory.
4078 // +2 is a hack for the LR slot + old SP on PPC
4079 intptr_t* stack_argument =
4080 reinterpret_cast<intptr_t*>(entry_stack) + kStackFrameExtraParamSlot;
4081 for (int i = 0; i < stack_arg_count; i++) {
4082 stack_argument[i] = va_arg(parameters, intptr_t);
4083 }
4084 va_end(parameters);
4085 set_register(sp, entry_stack);
4086
4087 CallInternal(entry);
4088
4089 // Pop stack passed arguments.
4090 CHECK_EQ(entry_stack, get_register(sp));
4091 set_register(sp, original_stack);
4092
4093 intptr_t result = get_register(r3);
4094 return result;
4095}
4096
4097
4098void Simulator::CallFP(byte* entry, double d0, double d1) {
4099 set_d_register_from_double(1, d0);
4100 set_d_register_from_double(2, d1);
4101 CallInternal(entry);
4102}
4103
4104
4105int32_t Simulator::CallFPReturnsInt(byte* entry, double d0, double d1) {
4106 CallFP(entry, d0, d1);
4107 int32_t result = get_register(r3);
4108 return result;
4109}
4110
4111
4112double Simulator::CallFPReturnsDouble(byte* entry, double d0, double d1) {
4113 CallFP(entry, d0, d1);
4114 return get_double_from_d_register(1);
4115}
4116
4117
4118uintptr_t Simulator::PushAddress(uintptr_t address) {
4119 uintptr_t new_sp = get_register(sp) - sizeof(uintptr_t);
4120 uintptr_t* stack_slot = reinterpret_cast<uintptr_t*>(new_sp);
4121 *stack_slot = address;
4122 set_register(sp, new_sp);
4123 return new_sp;
4124}
4125
4126
4127uintptr_t Simulator::PopAddress() {
4128 uintptr_t current_sp = get_register(sp);
4129 uintptr_t* stack_slot = reinterpret_cast<uintptr_t*>(current_sp);
4130 uintptr_t address = *stack_slot;
4131 set_register(sp, current_sp + sizeof(uintptr_t));
4132 return address;
4133}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004134} // namespace internal
4135} // namespace v8
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004136
4137#endif // USE_SIMULATOR
4138#endif // V8_TARGET_ARCH_PPC