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