Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 1 | // Copyright 2010 the V8 project authors. All rights reserved. |
| 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | #include <stdlib.h> |
| 29 | #include <cstdarg> |
| 30 | #include "v8.h" |
| 31 | |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame^] | 32 | #if defined(V8_TARGET_ARCH_MIPS) |
| 33 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 34 | #include "disasm.h" |
| 35 | #include "assembler.h" |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 36 | #include "globals.h" // Need the BitCast |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 37 | #include "mips/constants-mips.h" |
| 38 | #include "mips/simulator-mips.h" |
| 39 | |
| 40 | namespace v8i = v8::internal; |
| 41 | |
| 42 | #if !defined(__mips) |
| 43 | |
| 44 | // Only build the simulator if not compiling for real MIPS hardware. |
| 45 | namespace assembler { |
| 46 | namespace mips { |
| 47 | |
| 48 | using ::v8::internal::Object; |
| 49 | using ::v8::internal::PrintF; |
| 50 | using ::v8::internal::OS; |
| 51 | using ::v8::internal::ReadLine; |
| 52 | using ::v8::internal::DeleteArray; |
| 53 | |
| 54 | // Utils functions |
| 55 | bool HaveSameSign(int32_t a, int32_t b) { |
| 56 | return ((a ^ b) > 0); |
| 57 | } |
| 58 | |
| 59 | |
| 60 | // This macro provides a platform independent use of sscanf. The reason for |
| 61 | // SScanF not being implemented in a platform independent was through |
| 62 | // ::v8::internal::OS in the same way as SNPrintF is that the Windows C Run-Time |
| 63 | // Library does not provide vsscanf. |
| 64 | #define SScanF sscanf // NOLINT |
| 65 | |
| 66 | // The Debugger class is used by the simulator while debugging simulated MIPS |
| 67 | // code. |
| 68 | class Debugger { |
| 69 | public: |
| 70 | explicit Debugger(Simulator* sim); |
| 71 | ~Debugger(); |
| 72 | |
| 73 | void Stop(Instruction* instr); |
| 74 | void Debug(); |
| 75 | |
| 76 | private: |
| 77 | // We set the breakpoint code to 0xfffff to easily recognize it. |
| 78 | static const Instr kBreakpointInstr = SPECIAL | BREAK | 0xfffff << 6; |
| 79 | static const Instr kNopInstr = 0x0; |
| 80 | |
| 81 | Simulator* sim_; |
| 82 | |
| 83 | int32_t GetRegisterValue(int regnum); |
| 84 | bool GetValue(const char* desc, int32_t* value); |
| 85 | |
| 86 | // Set or delete a breakpoint. Returns true if successful. |
| 87 | bool SetBreakpoint(Instruction* breakpc); |
| 88 | bool DeleteBreakpoint(Instruction* breakpc); |
| 89 | |
| 90 | // Undo and redo all breakpoints. This is needed to bracket disassembly and |
| 91 | // execution to skip past breakpoints when run from the debugger. |
| 92 | void UndoBreakpoints(); |
| 93 | void RedoBreakpoints(); |
| 94 | |
| 95 | // Print all registers with a nice formatting. |
| 96 | void PrintAllRegs(); |
| 97 | }; |
| 98 | |
| 99 | Debugger::Debugger(Simulator* sim) { |
| 100 | sim_ = sim; |
| 101 | } |
| 102 | |
| 103 | Debugger::~Debugger() { |
| 104 | } |
| 105 | |
| 106 | #ifdef GENERATED_CODE_COVERAGE |
| 107 | static FILE* coverage_log = NULL; |
| 108 | |
| 109 | |
| 110 | static void InitializeCoverage() { |
| 111 | char* file_name = getenv("V8_GENERATED_CODE_COVERAGE_LOG"); |
| 112 | if (file_name != NULL) { |
| 113 | coverage_log = fopen(file_name, "aw+"); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | |
| 118 | void Debugger::Stop(Instruction* instr) { |
| 119 | UNIMPLEMENTED_MIPS(); |
| 120 | char* str = reinterpret_cast<char*>(instr->InstructionBits()); |
| 121 | if (strlen(str) > 0) { |
| 122 | if (coverage_log != NULL) { |
| 123 | fprintf(coverage_log, "%s\n", str); |
| 124 | fflush(coverage_log); |
| 125 | } |
| 126 | instr->SetInstructionBits(0x0); // Overwrite with nop. |
| 127 | } |
| 128 | sim_->set_pc(sim_->get_pc() + Instruction::kInstructionSize); |
| 129 | } |
| 130 | |
| 131 | #else // ndef GENERATED_CODE_COVERAGE |
| 132 | |
| 133 | #define UNSUPPORTED() printf("Unsupported instruction.\n"); |
| 134 | |
| 135 | static void InitializeCoverage() {} |
| 136 | |
| 137 | |
| 138 | void Debugger::Stop(Instruction* instr) { |
| 139 | const char* str = reinterpret_cast<char*>(instr->InstructionBits()); |
| 140 | PrintF("Simulator hit %s\n", str); |
| 141 | sim_->set_pc(sim_->get_pc() + Instruction::kInstructionSize); |
| 142 | Debug(); |
| 143 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 144 | #endif // GENERATED_CODE_COVERAGE |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 145 | |
| 146 | |
| 147 | int32_t Debugger::GetRegisterValue(int regnum) { |
| 148 | if (regnum == kNumSimuRegisters) { |
| 149 | return sim_->get_pc(); |
| 150 | } else { |
| 151 | return sim_->get_register(regnum); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | |
| 156 | bool Debugger::GetValue(const char* desc, int32_t* value) { |
| 157 | int regnum = Registers::Number(desc); |
| 158 | if (regnum != kInvalidRegister) { |
| 159 | *value = GetRegisterValue(regnum); |
| 160 | return true; |
| 161 | } else { |
| 162 | return SScanF(desc, "%i", value) == 1; |
| 163 | } |
| 164 | return false; |
| 165 | } |
| 166 | |
| 167 | |
| 168 | bool Debugger::SetBreakpoint(Instruction* breakpc) { |
| 169 | // Check if a breakpoint can be set. If not return without any side-effects. |
| 170 | if (sim_->break_pc_ != NULL) { |
| 171 | return false; |
| 172 | } |
| 173 | |
| 174 | // Set the breakpoint. |
| 175 | sim_->break_pc_ = breakpc; |
| 176 | sim_->break_instr_ = breakpc->InstructionBits(); |
| 177 | // Not setting the breakpoint instruction in the code itself. It will be set |
| 178 | // when the debugger shell continues. |
| 179 | return true; |
| 180 | } |
| 181 | |
| 182 | |
| 183 | bool Debugger::DeleteBreakpoint(Instruction* breakpc) { |
| 184 | if (sim_->break_pc_ != NULL) { |
| 185 | sim_->break_pc_->SetInstructionBits(sim_->break_instr_); |
| 186 | } |
| 187 | |
| 188 | sim_->break_pc_ = NULL; |
| 189 | sim_->break_instr_ = 0; |
| 190 | return true; |
| 191 | } |
| 192 | |
| 193 | |
| 194 | void Debugger::UndoBreakpoints() { |
| 195 | if (sim_->break_pc_ != NULL) { |
| 196 | sim_->break_pc_->SetInstructionBits(sim_->break_instr_); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | |
| 201 | void Debugger::RedoBreakpoints() { |
| 202 | if (sim_->break_pc_ != NULL) { |
| 203 | sim_->break_pc_->SetInstructionBits(kBreakpointInstr); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | void Debugger::PrintAllRegs() { |
| 208 | #define REG_INFO(n) Registers::Name(n), GetRegisterValue(n), GetRegisterValue(n) |
| 209 | |
| 210 | PrintF("\n"); |
| 211 | // at, v0, a0 |
| 212 | PrintF("%3s: 0x%08x %10d\t%3s: 0x%08x %10d\t%3s: 0x%08x %10d\n", |
| 213 | REG_INFO(1), REG_INFO(2), REG_INFO(4)); |
| 214 | // v1, a1 |
| 215 | PrintF("%26s\t%3s: 0x%08x %10d\t%3s: 0x%08x %10d\n", |
| 216 | "", REG_INFO(3), REG_INFO(5)); |
| 217 | // a2 |
| 218 | PrintF("%26s\t%26s\t%3s: 0x%08x %10d\n", "", "", REG_INFO(6)); |
| 219 | // a3 |
| 220 | PrintF("%26s\t%26s\t%3s: 0x%08x %10d\n", "", "", REG_INFO(7)); |
| 221 | PrintF("\n"); |
| 222 | // t0-t7, s0-s7 |
| 223 | for (int i = 0; i < 8; i++) { |
| 224 | PrintF("%3s: 0x%08x %10d\t%3s: 0x%08x %10d\n", |
| 225 | REG_INFO(8+i), REG_INFO(16+i)); |
| 226 | } |
| 227 | PrintF("\n"); |
| 228 | // t8, k0, LO |
| 229 | PrintF("%3s: 0x%08x %10d\t%3s: 0x%08x %10d\t%3s: 0x%08x %10d\n", |
| 230 | REG_INFO(24), REG_INFO(26), REG_INFO(32)); |
| 231 | // t9, k1, HI |
| 232 | PrintF("%3s: 0x%08x %10d\t%3s: 0x%08x %10d\t%3s: 0x%08x %10d\n", |
| 233 | REG_INFO(25), REG_INFO(27), REG_INFO(33)); |
| 234 | // sp, fp, gp |
| 235 | PrintF("%3s: 0x%08x %10d\t%3s: 0x%08x %10d\t%3s: 0x%08x %10d\n", |
| 236 | REG_INFO(29), REG_INFO(30), REG_INFO(28)); |
| 237 | // pc |
| 238 | PrintF("%3s: 0x%08x %10d\t%3s: 0x%08x %10d\n", |
| 239 | REG_INFO(31), REG_INFO(34)); |
| 240 | #undef REG_INFO |
| 241 | } |
| 242 | |
| 243 | void Debugger::Debug() { |
| 244 | intptr_t last_pc = -1; |
| 245 | bool done = false; |
| 246 | |
| 247 | #define COMMAND_SIZE 63 |
| 248 | #define ARG_SIZE 255 |
| 249 | |
| 250 | #define STR(a) #a |
| 251 | #define XSTR(a) STR(a) |
| 252 | |
| 253 | char cmd[COMMAND_SIZE + 1]; |
| 254 | char arg1[ARG_SIZE + 1]; |
| 255 | char arg2[ARG_SIZE + 1]; |
| 256 | |
| 257 | // make sure to have a proper terminating character if reaching the limit |
| 258 | cmd[COMMAND_SIZE] = 0; |
| 259 | arg1[ARG_SIZE] = 0; |
| 260 | arg2[ARG_SIZE] = 0; |
| 261 | |
| 262 | // Undo all set breakpoints while running in the debugger shell. This will |
| 263 | // make them invisible to all commands. |
| 264 | UndoBreakpoints(); |
| 265 | |
| 266 | while (!done && (sim_->get_pc() != Simulator::end_sim_pc)) { |
| 267 | if (last_pc != sim_->get_pc()) { |
| 268 | disasm::NameConverter converter; |
| 269 | disasm::Disassembler dasm(converter); |
| 270 | // use a reasonably large buffer |
| 271 | v8::internal::EmbeddedVector<char, 256> buffer; |
| 272 | dasm.InstructionDecode(buffer, |
| 273 | reinterpret_cast<byte_*>(sim_->get_pc())); |
| 274 | PrintF(" 0x%08x %s\n", sim_->get_pc(), buffer.start()); |
| 275 | last_pc = sim_->get_pc(); |
| 276 | } |
| 277 | char* line = ReadLine("sim> "); |
| 278 | if (line == NULL) { |
| 279 | break; |
| 280 | } else { |
| 281 | // Use sscanf to parse the individual parts of the command line. At the |
| 282 | // moment no command expects more than two parameters. |
| 283 | int args = SScanF(line, |
| 284 | "%" XSTR(COMMAND_SIZE) "s " |
| 285 | "%" XSTR(ARG_SIZE) "s " |
| 286 | "%" XSTR(ARG_SIZE) "s", |
| 287 | cmd, arg1, arg2); |
| 288 | if ((strcmp(cmd, "si") == 0) || (strcmp(cmd, "stepi") == 0)) { |
| 289 | if (!(reinterpret_cast<Instruction*>(sim_->get_pc())->IsTrap())) { |
| 290 | sim_->InstructionDecode( |
| 291 | reinterpret_cast<Instruction*>(sim_->get_pc())); |
| 292 | } else { |
| 293 | // Allow si to jump over generated breakpoints. |
| 294 | PrintF("/!\\ Jumping over generated breakpoint.\n"); |
| 295 | sim_->set_pc(sim_->get_pc() + Instruction::kInstructionSize); |
| 296 | } |
| 297 | } else if ((strcmp(cmd, "c") == 0) || (strcmp(cmd, "cont") == 0)) { |
| 298 | // Execute the one instruction we broke at with breakpoints disabled. |
| 299 | sim_->InstructionDecode(reinterpret_cast<Instruction*>(sim_->get_pc())); |
| 300 | // Leave the debugger shell. |
| 301 | done = true; |
| 302 | } else if ((strcmp(cmd, "p") == 0) || (strcmp(cmd, "print") == 0)) { |
| 303 | if (args == 2) { |
| 304 | int32_t value; |
| 305 | if (strcmp(arg1, "all") == 0) { |
| 306 | PrintAllRegs(); |
| 307 | } else { |
| 308 | if (GetValue(arg1, &value)) { |
| 309 | PrintF("%s: 0x%08x %d \n", arg1, value, value); |
| 310 | } else { |
| 311 | PrintF("%s unrecognized\n", arg1); |
| 312 | } |
| 313 | } |
| 314 | } else { |
| 315 | PrintF("print <register>\n"); |
| 316 | } |
| 317 | } else if ((strcmp(cmd, "po") == 0) |
| 318 | || (strcmp(cmd, "printobject") == 0)) { |
| 319 | if (args == 2) { |
| 320 | int32_t value; |
| 321 | if (GetValue(arg1, &value)) { |
| 322 | Object* obj = reinterpret_cast<Object*>(value); |
| 323 | PrintF("%s: \n", arg1); |
| 324 | #ifdef DEBUG |
| 325 | obj->PrintLn(); |
| 326 | #else |
| 327 | obj->ShortPrint(); |
| 328 | PrintF("\n"); |
| 329 | #endif |
| 330 | } else { |
| 331 | PrintF("%s unrecognized\n", arg1); |
| 332 | } |
| 333 | } else { |
| 334 | PrintF("printobject <value>\n"); |
| 335 | } |
| 336 | } else if ((strcmp(cmd, "disasm") == 0) || (strcmp(cmd, "dpc") == 0)) { |
| 337 | disasm::NameConverter converter; |
| 338 | disasm::Disassembler dasm(converter); |
| 339 | // use a reasonably large buffer |
| 340 | v8::internal::EmbeddedVector<char, 256> buffer; |
| 341 | |
| 342 | byte_* cur = NULL; |
| 343 | byte_* end = NULL; |
| 344 | |
| 345 | if (args == 1) { |
| 346 | cur = reinterpret_cast<byte_*>(sim_->get_pc()); |
| 347 | end = cur + (10 * Instruction::kInstructionSize); |
| 348 | } else if (args == 2) { |
| 349 | int32_t value; |
| 350 | if (GetValue(arg1, &value)) { |
| 351 | cur = reinterpret_cast<byte_*>(value); |
| 352 | // no length parameter passed, assume 10 instructions |
| 353 | end = cur + (10 * Instruction::kInstructionSize); |
| 354 | } |
| 355 | } else { |
| 356 | int32_t value1; |
| 357 | int32_t value2; |
| 358 | if (GetValue(arg1, &value1) && GetValue(arg2, &value2)) { |
| 359 | cur = reinterpret_cast<byte_*>(value1); |
| 360 | end = cur + (value2 * Instruction::kInstructionSize); |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | while (cur < end) { |
| 365 | dasm.InstructionDecode(buffer, cur); |
| 366 | PrintF(" 0x%08x %s\n", cur, buffer.start()); |
| 367 | cur += Instruction::kInstructionSize; |
| 368 | } |
| 369 | } else if (strcmp(cmd, "gdb") == 0) { |
| 370 | PrintF("relinquishing control to gdb\n"); |
| 371 | v8::internal::OS::DebugBreak(); |
| 372 | PrintF("regaining control from gdb\n"); |
| 373 | } else if (strcmp(cmd, "break") == 0) { |
| 374 | if (args == 2) { |
| 375 | int32_t value; |
| 376 | if (GetValue(arg1, &value)) { |
| 377 | if (!SetBreakpoint(reinterpret_cast<Instruction*>(value))) { |
| 378 | PrintF("setting breakpoint failed\n"); |
| 379 | } |
| 380 | } else { |
| 381 | PrintF("%s unrecognized\n", arg1); |
| 382 | } |
| 383 | } else { |
| 384 | PrintF("break <address>\n"); |
| 385 | } |
| 386 | } else if (strcmp(cmd, "del") == 0) { |
| 387 | if (!DeleteBreakpoint(NULL)) { |
| 388 | PrintF("deleting breakpoint failed\n"); |
| 389 | } |
| 390 | } else if (strcmp(cmd, "flags") == 0) { |
| 391 | PrintF("No flags on MIPS !\n"); |
| 392 | } else if (strcmp(cmd, "unstop") == 0) { |
| 393 | PrintF("Unstop command not implemented on MIPS."); |
| 394 | } else if ((strcmp(cmd, "stat") == 0) || (strcmp(cmd, "st") == 0)) { |
| 395 | // Print registers and disassemble |
| 396 | PrintAllRegs(); |
| 397 | PrintF("\n"); |
| 398 | |
| 399 | disasm::NameConverter converter; |
| 400 | disasm::Disassembler dasm(converter); |
| 401 | // use a reasonably large buffer |
| 402 | v8::internal::EmbeddedVector<char, 256> buffer; |
| 403 | |
| 404 | byte_* cur = NULL; |
| 405 | byte_* end = NULL; |
| 406 | |
| 407 | if (args == 1) { |
| 408 | cur = reinterpret_cast<byte_*>(sim_->get_pc()); |
| 409 | end = cur + (10 * Instruction::kInstructionSize); |
| 410 | } else if (args == 2) { |
| 411 | int32_t value; |
| 412 | if (GetValue(arg1, &value)) { |
| 413 | cur = reinterpret_cast<byte_*>(value); |
| 414 | // no length parameter passed, assume 10 instructions |
| 415 | end = cur + (10 * Instruction::kInstructionSize); |
| 416 | } |
| 417 | } else { |
| 418 | int32_t value1; |
| 419 | int32_t value2; |
| 420 | if (GetValue(arg1, &value1) && GetValue(arg2, &value2)) { |
| 421 | cur = reinterpret_cast<byte_*>(value1); |
| 422 | end = cur + (value2 * Instruction::kInstructionSize); |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | while (cur < end) { |
| 427 | dasm.InstructionDecode(buffer, cur); |
| 428 | PrintF(" 0x%08x %s\n", cur, buffer.start()); |
| 429 | cur += Instruction::kInstructionSize; |
| 430 | } |
| 431 | } else if ((strcmp(cmd, "h") == 0) || (strcmp(cmd, "help") == 0)) { |
| 432 | PrintF("cont\n"); |
| 433 | PrintF(" continue execution (alias 'c')\n"); |
| 434 | PrintF("stepi\n"); |
| 435 | PrintF(" step one instruction (alias 'si')\n"); |
| 436 | PrintF("print <register>\n"); |
| 437 | PrintF(" print register content (alias 'p')\n"); |
| 438 | PrintF(" use register name 'all' to print all registers\n"); |
| 439 | PrintF("printobject <register>\n"); |
| 440 | PrintF(" print an object from a register (alias 'po')\n"); |
| 441 | PrintF("flags\n"); |
| 442 | PrintF(" print flags\n"); |
| 443 | PrintF("disasm [<instructions>]\n"); |
| 444 | PrintF("disasm [[<address>] <instructions>]\n"); |
| 445 | PrintF(" disassemble code, default is 10 instructions from pc\n"); |
| 446 | PrintF("gdb\n"); |
| 447 | PrintF(" enter gdb\n"); |
| 448 | PrintF("break <address>\n"); |
| 449 | PrintF(" set a break point on the address\n"); |
| 450 | PrintF("del\n"); |
| 451 | PrintF(" delete the breakpoint\n"); |
| 452 | PrintF("unstop\n"); |
| 453 | PrintF(" ignore the stop instruction at the current location"); |
| 454 | PrintF(" from now on\n"); |
| 455 | } else { |
| 456 | PrintF("Unknown command: %s\n", cmd); |
| 457 | } |
| 458 | } |
| 459 | DeleteArray(line); |
| 460 | } |
| 461 | |
| 462 | // Add all the breakpoints back to stop execution and enter the debugger |
| 463 | // shell when hit. |
| 464 | RedoBreakpoints(); |
| 465 | |
| 466 | #undef COMMAND_SIZE |
| 467 | #undef ARG_SIZE |
| 468 | |
| 469 | #undef STR |
| 470 | #undef XSTR |
| 471 | } |
| 472 | |
| 473 | |
| 474 | // Create one simulator per thread and keep it in thread local storage. |
| 475 | static v8::internal::Thread::LocalStorageKey simulator_key; |
| 476 | |
| 477 | |
| 478 | bool Simulator::initialized_ = false; |
| 479 | |
| 480 | |
| 481 | void Simulator::Initialize() { |
| 482 | if (initialized_) return; |
| 483 | simulator_key = v8::internal::Thread::CreateThreadLocalKey(); |
| 484 | initialized_ = true; |
| 485 | ::v8::internal::ExternalReference::set_redirector(&RedirectExternalReference); |
| 486 | } |
| 487 | |
| 488 | |
| 489 | Simulator::Simulator() { |
| 490 | Initialize(); |
| 491 | // Setup simulator support first. Some of this information is needed to |
| 492 | // setup the architecture state. |
| 493 | size_t stack_size = 1 * 1024*1024; // allocate 1MB for stack |
| 494 | stack_ = reinterpret_cast<char*>(malloc(stack_size)); |
| 495 | pc_modified_ = false; |
| 496 | icount_ = 0; |
| 497 | break_pc_ = NULL; |
| 498 | break_instr_ = 0; |
| 499 | |
| 500 | // Setup architecture state. |
| 501 | // All registers are initialized to zero to start with. |
| 502 | for (int i = 0; i < kNumSimuRegisters; i++) { |
| 503 | registers_[i] = 0; |
| 504 | } |
| 505 | |
| 506 | // The sp is initialized to point to the bottom (high address) of the |
| 507 | // allocated stack area. To be safe in potential stack underflows we leave |
| 508 | // some buffer below. |
| 509 | registers_[sp] = reinterpret_cast<int32_t>(stack_) + stack_size - 64; |
| 510 | // The ra and pc are initialized to a known bad value that will cause an |
| 511 | // access violation if the simulator ever tries to execute it. |
| 512 | registers_[pc] = bad_ra; |
| 513 | registers_[ra] = bad_ra; |
| 514 | InitializeCoverage(); |
| 515 | } |
| 516 | |
| 517 | |
| 518 | // When the generated code calls an external reference we need to catch that in |
| 519 | // the simulator. The external reference will be a function compiled for the |
| 520 | // host architecture. We need to call that function instead of trying to |
| 521 | // execute it with the simulator. We do that by redirecting the external |
| 522 | // reference to a swi (software-interrupt) instruction that is handled by |
| 523 | // the simulator. We write the original destination of the jump just at a known |
| 524 | // offset from the swi instruction so the simulator knows what to call. |
| 525 | class Redirection { |
| 526 | public: |
| 527 | Redirection(void* external_function, bool fp_return) |
| 528 | : external_function_(external_function), |
| 529 | swi_instruction_(rtCallRedirInstr), |
| 530 | fp_return_(fp_return), |
| 531 | next_(list_) { |
| 532 | list_ = this; |
| 533 | } |
| 534 | |
| 535 | void* address_of_swi_instruction() { |
| 536 | return reinterpret_cast<void*>(&swi_instruction_); |
| 537 | } |
| 538 | |
| 539 | void* external_function() { return external_function_; } |
| 540 | bool fp_return() { return fp_return_; } |
| 541 | |
| 542 | static Redirection* Get(void* external_function, bool fp_return) { |
| 543 | Redirection* current; |
| 544 | for (current = list_; current != NULL; current = current->next_) { |
| 545 | if (current->external_function_ == external_function) return current; |
| 546 | } |
| 547 | return new Redirection(external_function, fp_return); |
| 548 | } |
| 549 | |
| 550 | static Redirection* FromSwiInstruction(Instruction* swi_instruction) { |
| 551 | char* addr_of_swi = reinterpret_cast<char*>(swi_instruction); |
| 552 | char* addr_of_redirection = |
| 553 | addr_of_swi - OFFSET_OF(Redirection, swi_instruction_); |
| 554 | return reinterpret_cast<Redirection*>(addr_of_redirection); |
| 555 | } |
| 556 | |
| 557 | private: |
| 558 | void* external_function_; |
| 559 | uint32_t swi_instruction_; |
| 560 | bool fp_return_; |
| 561 | Redirection* next_; |
| 562 | static Redirection* list_; |
| 563 | }; |
| 564 | |
| 565 | |
| 566 | Redirection* Redirection::list_ = NULL; |
| 567 | |
| 568 | |
| 569 | void* Simulator::RedirectExternalReference(void* external_function, |
| 570 | bool fp_return) { |
| 571 | Redirection* redirection = Redirection::Get(external_function, fp_return); |
| 572 | return redirection->address_of_swi_instruction(); |
| 573 | } |
| 574 | |
| 575 | |
| 576 | // Get the active Simulator for the current thread. |
| 577 | Simulator* Simulator::current() { |
| 578 | Initialize(); |
| 579 | Simulator* sim = reinterpret_cast<Simulator*>( |
| 580 | v8::internal::Thread::GetThreadLocal(simulator_key)); |
| 581 | if (sim == NULL) { |
| 582 | // TODO(146): delete the simulator object when a thread goes away. |
| 583 | sim = new Simulator(); |
| 584 | v8::internal::Thread::SetThreadLocal(simulator_key, sim); |
| 585 | } |
| 586 | return sim; |
| 587 | } |
| 588 | |
| 589 | |
| 590 | // Sets the register in the architecture state. It will also deal with updating |
| 591 | // Simulator internal state for special registers such as PC. |
| 592 | void Simulator::set_register(int reg, int32_t value) { |
| 593 | ASSERT((reg >= 0) && (reg < kNumSimuRegisters)); |
| 594 | if (reg == pc) { |
| 595 | pc_modified_ = true; |
| 596 | } |
| 597 | |
| 598 | // zero register always hold 0. |
| 599 | registers_[reg] = (reg == 0) ? 0 : value; |
| 600 | } |
| 601 | |
| 602 | void Simulator::set_fpu_register(int fpureg, int32_t value) { |
| 603 | ASSERT((fpureg >= 0) && (fpureg < kNumFPURegisters)); |
| 604 | FPUregisters_[fpureg] = value; |
| 605 | } |
| 606 | |
| 607 | void Simulator::set_fpu_register_double(int fpureg, double value) { |
| 608 | ASSERT((fpureg >= 0) && (fpureg < kNumFPURegisters) && ((fpureg % 2) == 0)); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 609 | *v8i::BitCast<double*, int32_t*>(&FPUregisters_[fpureg]) = value; |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 610 | } |
| 611 | |
| 612 | |
| 613 | // Get the register from the architecture state. This function does handle |
| 614 | // the special case of accessing the PC register. |
| 615 | int32_t Simulator::get_register(int reg) const { |
| 616 | ASSERT((reg >= 0) && (reg < kNumSimuRegisters)); |
| 617 | if (reg == 0) |
| 618 | return 0; |
| 619 | else |
| 620 | return registers_[reg] + ((reg == pc) ? Instruction::kPCReadOffset : 0); |
| 621 | } |
| 622 | |
| 623 | int32_t Simulator::get_fpu_register(int fpureg) const { |
| 624 | ASSERT((fpureg >= 0) && (fpureg < kNumFPURegisters)); |
| 625 | return FPUregisters_[fpureg]; |
| 626 | } |
| 627 | |
| 628 | double Simulator::get_fpu_register_double(int fpureg) const { |
| 629 | ASSERT((fpureg >= 0) && (fpureg < kNumFPURegisters) && ((fpureg % 2) == 0)); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 630 | return *v8i::BitCast<double*, int32_t*>( |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 631 | const_cast<int32_t*>(&FPUregisters_[fpureg])); |
| 632 | } |
| 633 | |
| 634 | // Raw access to the PC register. |
| 635 | void Simulator::set_pc(int32_t value) { |
| 636 | pc_modified_ = true; |
| 637 | registers_[pc] = value; |
| 638 | } |
| 639 | |
| 640 | // Raw access to the PC register without the special adjustment when reading. |
| 641 | int32_t Simulator::get_pc() const { |
| 642 | return registers_[pc]; |
| 643 | } |
| 644 | |
| 645 | |
| 646 | // The MIPS cannot do unaligned reads and writes. On some MIPS platforms an |
| 647 | // interrupt is caused. On others it does a funky rotation thing. For now we |
| 648 | // simply disallow unaligned reads, but at some point we may want to move to |
| 649 | // emulating the rotate behaviour. Note that simulator runs have the runtime |
| 650 | // system running directly on the host system and only generated code is |
| 651 | // executed in the simulator. Since the host is typically IA32 we will not |
| 652 | // get the correct MIPS-like behaviour on unaligned accesses. |
| 653 | |
| 654 | int Simulator::ReadW(int32_t addr, Instruction* instr) { |
| 655 | if ((addr & v8i::kPointerAlignmentMask) == 0) { |
| 656 | intptr_t* ptr = reinterpret_cast<intptr_t*>(addr); |
| 657 | return *ptr; |
| 658 | } |
| 659 | PrintF("Unaligned read at 0x%08x, pc=%p\n", addr, instr); |
| 660 | OS::Abort(); |
| 661 | return 0; |
| 662 | } |
| 663 | |
| 664 | |
| 665 | void Simulator::WriteW(int32_t addr, int value, Instruction* instr) { |
| 666 | if ((addr & v8i::kPointerAlignmentMask) == 0) { |
| 667 | intptr_t* ptr = reinterpret_cast<intptr_t*>(addr); |
| 668 | *ptr = value; |
| 669 | return; |
| 670 | } |
| 671 | PrintF("Unaligned write at 0x%08x, pc=%p\n", addr, instr); |
| 672 | OS::Abort(); |
| 673 | } |
| 674 | |
| 675 | |
| 676 | double Simulator::ReadD(int32_t addr, Instruction* instr) { |
| 677 | if ((addr & kDoubleAlignmentMask) == 0) { |
| 678 | double* ptr = reinterpret_cast<double*>(addr); |
| 679 | return *ptr; |
| 680 | } |
| 681 | PrintF("Unaligned read at 0x%08x, pc=%p\n", addr, instr); |
| 682 | OS::Abort(); |
| 683 | return 0; |
| 684 | } |
| 685 | |
| 686 | |
| 687 | void Simulator::WriteD(int32_t addr, double value, Instruction* instr) { |
| 688 | if ((addr & kDoubleAlignmentMask) == 0) { |
| 689 | double* ptr = reinterpret_cast<double*>(addr); |
| 690 | *ptr = value; |
| 691 | return; |
| 692 | } |
| 693 | PrintF("Unaligned write at 0x%08x, pc=%p\n", addr, instr); |
| 694 | OS::Abort(); |
| 695 | } |
| 696 | |
| 697 | |
| 698 | uint16_t Simulator::ReadHU(int32_t addr, Instruction* instr) { |
| 699 | if ((addr & 1) == 0) { |
| 700 | uint16_t* ptr = reinterpret_cast<uint16_t*>(addr); |
| 701 | return *ptr; |
| 702 | } |
| 703 | PrintF("Unaligned unsigned halfword read at 0x%08x, pc=%p\n", addr, instr); |
| 704 | OS::Abort(); |
| 705 | return 0; |
| 706 | } |
| 707 | |
| 708 | |
| 709 | int16_t Simulator::ReadH(int32_t addr, Instruction* instr) { |
| 710 | if ((addr & 1) == 0) { |
| 711 | int16_t* ptr = reinterpret_cast<int16_t*>(addr); |
| 712 | return *ptr; |
| 713 | } |
| 714 | PrintF("Unaligned signed halfword read at 0x%08x, pc=%p\n", addr, instr); |
| 715 | OS::Abort(); |
| 716 | return 0; |
| 717 | } |
| 718 | |
| 719 | |
| 720 | void Simulator::WriteH(int32_t addr, uint16_t value, Instruction* instr) { |
| 721 | if ((addr & 1) == 0) { |
| 722 | uint16_t* ptr = reinterpret_cast<uint16_t*>(addr); |
| 723 | *ptr = value; |
| 724 | return; |
| 725 | } |
| 726 | PrintF("Unaligned unsigned halfword write at 0x%08x, pc=%p\n", addr, instr); |
| 727 | OS::Abort(); |
| 728 | } |
| 729 | |
| 730 | |
| 731 | void Simulator::WriteH(int32_t addr, int16_t value, Instruction* instr) { |
| 732 | if ((addr & 1) == 0) { |
| 733 | int16_t* ptr = reinterpret_cast<int16_t*>(addr); |
| 734 | *ptr = value; |
| 735 | return; |
| 736 | } |
| 737 | PrintF("Unaligned halfword write at 0x%08x, pc=%p\n", addr, instr); |
| 738 | OS::Abort(); |
| 739 | } |
| 740 | |
| 741 | |
| 742 | uint32_t Simulator::ReadBU(int32_t addr) { |
| 743 | uint8_t* ptr = reinterpret_cast<uint8_t*>(addr); |
| 744 | return *ptr & 0xff; |
| 745 | } |
| 746 | |
| 747 | |
| 748 | int32_t Simulator::ReadB(int32_t addr) { |
| 749 | int8_t* ptr = reinterpret_cast<int8_t*>(addr); |
| 750 | return ((*ptr << 24) >> 24) & 0xff; |
| 751 | } |
| 752 | |
| 753 | |
| 754 | void Simulator::WriteB(int32_t addr, uint8_t value) { |
| 755 | uint8_t* ptr = reinterpret_cast<uint8_t*>(addr); |
| 756 | *ptr = value; |
| 757 | } |
| 758 | |
| 759 | |
| 760 | void Simulator::WriteB(int32_t addr, int8_t value) { |
| 761 | int8_t* ptr = reinterpret_cast<int8_t*>(addr); |
| 762 | *ptr = value; |
| 763 | } |
| 764 | |
| 765 | |
| 766 | // Returns the limit of the stack area to enable checking for stack overflows. |
| 767 | uintptr_t Simulator::StackLimit() const { |
| 768 | // Leave a safety margin of 256 bytes to prevent overrunning the stack when |
| 769 | // pushing values. |
| 770 | return reinterpret_cast<uintptr_t>(stack_) + 256; |
| 771 | } |
| 772 | |
| 773 | |
| 774 | // Unsupported instructions use Format to print an error and stop execution. |
| 775 | void Simulator::Format(Instruction* instr, const char* format) { |
| 776 | PrintF("Simulator found unsupported instruction:\n 0x%08x: %s\n", |
| 777 | instr, format); |
| 778 | UNIMPLEMENTED_MIPS(); |
| 779 | } |
| 780 | |
| 781 | |
| 782 | // Calls into the V8 runtime are based on this very simple interface. |
| 783 | // Note: To be able to return two values from some calls the code in runtime.cc |
| 784 | // uses the ObjectPair which is essentially two 32-bit values stuffed into a |
| 785 | // 64-bit value. With the code below we assume that all runtime calls return |
| 786 | // 64 bits of result. If they don't, the r1 result register contains a bogus |
| 787 | // value, which is fine because it is caller-saved. |
| 788 | typedef int64_t (*SimulatorRuntimeCall)(int32_t arg0, |
| 789 | int32_t arg1, |
| 790 | int32_t arg2, |
| 791 | int32_t arg3); |
| 792 | typedef double (*SimulatorRuntimeFPCall)(double fparg0, |
| 793 | double fparg1); |
| 794 | |
| 795 | |
| 796 | // Software interrupt instructions are used by the simulator to call into the |
| 797 | // C-based V8 runtime. |
| 798 | void Simulator::SoftwareInterrupt(Instruction* instr) { |
| 799 | // We first check if we met a call_rt_redirected. |
| 800 | if (instr->InstructionBits() == rtCallRedirInstr) { |
| 801 | Redirection* redirection = Redirection::FromSwiInstruction(instr); |
| 802 | int32_t arg0 = get_register(a0); |
| 803 | int32_t arg1 = get_register(a1); |
| 804 | int32_t arg2 = get_register(a2); |
| 805 | int32_t arg3 = get_register(a3); |
| 806 | // fp args are (not always) in f12 and f14. |
| 807 | // See MIPS conventions for more details. |
| 808 | double fparg0 = get_fpu_register_double(f12); |
| 809 | double fparg1 = get_fpu_register_double(f14); |
| 810 | // This is dodgy but it works because the C entry stubs are never moved. |
| 811 | // See comment in codegen-arm.cc and bug 1242173. |
| 812 | int32_t saved_ra = get_register(ra); |
| 813 | if (redirection->fp_return()) { |
| 814 | intptr_t external = |
| 815 | reinterpret_cast<intptr_t>(redirection->external_function()); |
| 816 | SimulatorRuntimeFPCall target = |
| 817 | reinterpret_cast<SimulatorRuntimeFPCall>(external); |
| 818 | if (::v8::internal::FLAG_trace_sim) { |
| 819 | PrintF("Call to host function at %p with args %f, %f\n", |
| 820 | FUNCTION_ADDR(target), fparg0, fparg1); |
| 821 | } |
| 822 | double result = target(fparg0, fparg1); |
| 823 | set_fpu_register_double(f0, result); |
| 824 | } else { |
| 825 | intptr_t external = |
| 826 | reinterpret_cast<int32_t>(redirection->external_function()); |
| 827 | SimulatorRuntimeCall target = |
| 828 | reinterpret_cast<SimulatorRuntimeCall>(external); |
| 829 | if (::v8::internal::FLAG_trace_sim) { |
| 830 | PrintF( |
| 831 | "Call to host function at %p with args %08x, %08x, %08x, %08x\n", |
| 832 | FUNCTION_ADDR(target), |
| 833 | arg0, |
| 834 | arg1, |
| 835 | arg2, |
| 836 | arg3); |
| 837 | } |
| 838 | int64_t result = target(arg0, arg1, arg2, arg3); |
| 839 | int32_t lo_res = static_cast<int32_t>(result); |
| 840 | int32_t hi_res = static_cast<int32_t>(result >> 32); |
| 841 | if (::v8::internal::FLAG_trace_sim) { |
| 842 | PrintF("Returned %08x\n", lo_res); |
| 843 | } |
| 844 | set_register(v0, lo_res); |
| 845 | set_register(v1, hi_res); |
| 846 | } |
| 847 | set_register(ra, saved_ra); |
| 848 | set_pc(get_register(ra)); |
| 849 | } else { |
| 850 | Debugger dbg(this); |
| 851 | dbg.Debug(); |
| 852 | } |
| 853 | } |
| 854 | |
| 855 | void Simulator::SignalExceptions() { |
| 856 | for (int i = 1; i < kNumExceptions; i++) { |
| 857 | if (exceptions[i] != 0) { |
| 858 | V8_Fatal(__FILE__, __LINE__, "Error: Exception %i raised.", i); |
| 859 | } |
| 860 | } |
| 861 | } |
| 862 | |
| 863 | // Handle execution based on instruction types. |
| 864 | void Simulator::DecodeTypeRegister(Instruction* instr) { |
| 865 | // Instruction fields |
| 866 | Opcode op = instr->OpcodeFieldRaw(); |
| 867 | int32_t rs_reg = instr->RsField(); |
| 868 | int32_t rs = get_register(rs_reg); |
| 869 | uint32_t rs_u = static_cast<uint32_t>(rs); |
| 870 | int32_t rt_reg = instr->RtField(); |
| 871 | int32_t rt = get_register(rt_reg); |
| 872 | uint32_t rt_u = static_cast<uint32_t>(rt); |
| 873 | int32_t rd_reg = instr->RdField(); |
| 874 | uint32_t sa = instr->SaField(); |
| 875 | |
| 876 | int32_t fs_reg= instr->FsField(); |
| 877 | |
| 878 | // ALU output |
| 879 | // It should not be used as is. Instructions using it should always initialize |
| 880 | // it first. |
| 881 | int32_t alu_out = 0x12345678; |
| 882 | // Output or temporary for floating point. |
| 883 | double fp_out = 0.0; |
| 884 | |
| 885 | // For break and trap instructions. |
| 886 | bool do_interrupt = false; |
| 887 | |
| 888 | // For jr and jalr |
| 889 | // Get current pc. |
| 890 | int32_t current_pc = get_pc(); |
| 891 | // Next pc |
| 892 | int32_t next_pc = 0; |
| 893 | |
| 894 | // ---------- Configuration |
| 895 | switch (op) { |
| 896 | case COP1: // Coprocessor instructions |
| 897 | switch (instr->RsFieldRaw()) { |
| 898 | case BC1: // branch on coprocessor condition |
| 899 | UNREACHABLE(); |
| 900 | break; |
| 901 | case MFC1: |
| 902 | alu_out = get_fpu_register(fs_reg); |
| 903 | break; |
| 904 | case MFHC1: |
| 905 | fp_out = get_fpu_register_double(fs_reg); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 906 | alu_out = *v8i::BitCast<int32_t*, double*>(&fp_out); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 907 | break; |
| 908 | case MTC1: |
| 909 | case MTHC1: |
| 910 | // Do the store in the execution step. |
| 911 | break; |
| 912 | case S: |
| 913 | case D: |
| 914 | case W: |
| 915 | case L: |
| 916 | case PS: |
| 917 | // Do everything in the execution step. |
| 918 | break; |
| 919 | default: |
| 920 | UNIMPLEMENTED_MIPS(); |
| 921 | }; |
| 922 | break; |
| 923 | case SPECIAL: |
| 924 | switch (instr->FunctionFieldRaw()) { |
| 925 | case JR: |
| 926 | case JALR: |
| 927 | next_pc = get_register(instr->RsField()); |
| 928 | break; |
| 929 | case SLL: |
| 930 | alu_out = rt << sa; |
| 931 | break; |
| 932 | case SRL: |
| 933 | alu_out = rt_u >> sa; |
| 934 | break; |
| 935 | case SRA: |
| 936 | alu_out = rt >> sa; |
| 937 | break; |
| 938 | case SLLV: |
| 939 | alu_out = rt << rs; |
| 940 | break; |
| 941 | case SRLV: |
| 942 | alu_out = rt_u >> rs; |
| 943 | break; |
| 944 | case SRAV: |
| 945 | alu_out = rt >> rs; |
| 946 | break; |
| 947 | case MFHI: |
| 948 | alu_out = get_register(HI); |
| 949 | break; |
| 950 | case MFLO: |
| 951 | alu_out = get_register(LO); |
| 952 | break; |
| 953 | case MULT: |
| 954 | UNIMPLEMENTED_MIPS(); |
| 955 | break; |
| 956 | case MULTU: |
| 957 | UNIMPLEMENTED_MIPS(); |
| 958 | break; |
| 959 | case DIV: |
| 960 | case DIVU: |
| 961 | exceptions[kDivideByZero] = rt == 0; |
| 962 | break; |
| 963 | case ADD: |
| 964 | if (HaveSameSign(rs, rt)) { |
| 965 | if (rs > 0) { |
| 966 | exceptions[kIntegerOverflow] = rs > (Registers::kMaxValue - rt); |
| 967 | } else if (rs < 0) { |
| 968 | exceptions[kIntegerUnderflow] = rs < (Registers::kMinValue - rt); |
| 969 | } |
| 970 | } |
| 971 | alu_out = rs + rt; |
| 972 | break; |
| 973 | case ADDU: |
| 974 | alu_out = rs + rt; |
| 975 | break; |
| 976 | case SUB: |
| 977 | if (!HaveSameSign(rs, rt)) { |
| 978 | if (rs > 0) { |
| 979 | exceptions[kIntegerOverflow] = rs > (Registers::kMaxValue + rt); |
| 980 | } else if (rs < 0) { |
| 981 | exceptions[kIntegerUnderflow] = rs < (Registers::kMinValue + rt); |
| 982 | } |
| 983 | } |
| 984 | alu_out = rs - rt; |
| 985 | break; |
| 986 | case SUBU: |
| 987 | alu_out = rs - rt; |
| 988 | break; |
| 989 | case AND: |
| 990 | alu_out = rs & rt; |
| 991 | break; |
| 992 | case OR: |
| 993 | alu_out = rs | rt; |
| 994 | break; |
| 995 | case XOR: |
| 996 | alu_out = rs ^ rt; |
| 997 | break; |
| 998 | case NOR: |
| 999 | alu_out = ~(rs | rt); |
| 1000 | break; |
| 1001 | case SLT: |
| 1002 | alu_out = rs < rt ? 1 : 0; |
| 1003 | break; |
| 1004 | case SLTU: |
| 1005 | alu_out = rs_u < rt_u ? 1 : 0; |
| 1006 | break; |
| 1007 | // Break and trap instructions |
| 1008 | case BREAK: |
| 1009 | do_interrupt = true; |
| 1010 | break; |
| 1011 | case TGE: |
| 1012 | do_interrupt = rs >= rt; |
| 1013 | break; |
| 1014 | case TGEU: |
| 1015 | do_interrupt = rs_u >= rt_u; |
| 1016 | break; |
| 1017 | case TLT: |
| 1018 | do_interrupt = rs < rt; |
| 1019 | break; |
| 1020 | case TLTU: |
| 1021 | do_interrupt = rs_u < rt_u; |
| 1022 | break; |
| 1023 | case TEQ: |
| 1024 | do_interrupt = rs == rt; |
| 1025 | break; |
| 1026 | case TNE: |
| 1027 | do_interrupt = rs != rt; |
| 1028 | break; |
| 1029 | default: |
| 1030 | UNREACHABLE(); |
| 1031 | }; |
| 1032 | break; |
| 1033 | case SPECIAL2: |
| 1034 | switch (instr->FunctionFieldRaw()) { |
| 1035 | case MUL: |
| 1036 | alu_out = rs_u * rt_u; // Only the lower 32 bits are kept. |
| 1037 | break; |
| 1038 | default: |
| 1039 | UNREACHABLE(); |
| 1040 | } |
| 1041 | break; |
| 1042 | default: |
| 1043 | UNREACHABLE(); |
| 1044 | }; |
| 1045 | |
| 1046 | // ---------- Raise exceptions triggered. |
| 1047 | SignalExceptions(); |
| 1048 | |
| 1049 | // ---------- Execution |
| 1050 | switch (op) { |
| 1051 | case COP1: |
| 1052 | switch (instr->RsFieldRaw()) { |
| 1053 | case BC1: // branch on coprocessor condition |
| 1054 | UNREACHABLE(); |
| 1055 | break; |
| 1056 | case MFC1: |
| 1057 | case MFHC1: |
| 1058 | set_register(rt_reg, alu_out); |
| 1059 | break; |
| 1060 | case MTC1: |
| 1061 | // We don't need to set the higher bits to 0, because MIPS ISA says |
| 1062 | // they are in an unpredictable state after executing MTC1. |
| 1063 | FPUregisters_[fs_reg] = registers_[rt_reg]; |
| 1064 | FPUregisters_[fs_reg+1] = Unpredictable; |
| 1065 | break; |
| 1066 | case MTHC1: |
| 1067 | // Here we need to keep the lower bits unchanged. |
| 1068 | FPUregisters_[fs_reg+1] = registers_[rt_reg]; |
| 1069 | break; |
| 1070 | case S: |
| 1071 | switch (instr->FunctionFieldRaw()) { |
| 1072 | case CVT_D_S: |
| 1073 | case CVT_W_S: |
| 1074 | case CVT_L_S: |
| 1075 | case CVT_PS_S: |
| 1076 | UNIMPLEMENTED_MIPS(); |
| 1077 | break; |
| 1078 | default: |
| 1079 | UNREACHABLE(); |
| 1080 | } |
| 1081 | break; |
| 1082 | case D: |
| 1083 | switch (instr->FunctionFieldRaw()) { |
| 1084 | case CVT_S_D: |
| 1085 | case CVT_W_D: |
| 1086 | case CVT_L_D: |
| 1087 | UNIMPLEMENTED_MIPS(); |
| 1088 | break; |
| 1089 | default: |
| 1090 | UNREACHABLE(); |
| 1091 | } |
| 1092 | break; |
| 1093 | case W: |
| 1094 | switch (instr->FunctionFieldRaw()) { |
| 1095 | case CVT_S_W: |
| 1096 | UNIMPLEMENTED_MIPS(); |
| 1097 | break; |
| 1098 | case CVT_D_W: // Convert word to double. |
| 1099 | set_fpu_register(rd_reg, static_cast<double>(rs)); |
| 1100 | break; |
| 1101 | default: |
| 1102 | UNREACHABLE(); |
| 1103 | }; |
| 1104 | break; |
| 1105 | case L: |
| 1106 | switch (instr->FunctionFieldRaw()) { |
| 1107 | case CVT_S_L: |
| 1108 | case CVT_D_L: |
| 1109 | UNIMPLEMENTED_MIPS(); |
| 1110 | break; |
| 1111 | default: |
| 1112 | UNREACHABLE(); |
| 1113 | } |
| 1114 | break; |
| 1115 | case PS: |
| 1116 | break; |
| 1117 | default: |
| 1118 | UNREACHABLE(); |
| 1119 | }; |
| 1120 | break; |
| 1121 | case SPECIAL: |
| 1122 | switch (instr->FunctionFieldRaw()) { |
| 1123 | case JR: { |
| 1124 | Instruction* branch_delay_instr = reinterpret_cast<Instruction*>( |
| 1125 | current_pc+Instruction::kInstructionSize); |
| 1126 | BranchDelayInstructionDecode(branch_delay_instr); |
| 1127 | set_pc(next_pc); |
| 1128 | pc_modified_ = true; |
| 1129 | break; |
| 1130 | } |
| 1131 | case JALR: { |
| 1132 | Instruction* branch_delay_instr = reinterpret_cast<Instruction*>( |
| 1133 | current_pc+Instruction::kInstructionSize); |
| 1134 | BranchDelayInstructionDecode(branch_delay_instr); |
| 1135 | set_register(31, current_pc + 2* Instruction::kInstructionSize); |
| 1136 | set_pc(next_pc); |
| 1137 | pc_modified_ = true; |
| 1138 | break; |
| 1139 | } |
| 1140 | // Instructions using HI and LO registers. |
| 1141 | case MULT: |
| 1142 | case MULTU: |
| 1143 | break; |
| 1144 | case DIV: |
| 1145 | // Divide by zero was checked in the configuration step. |
| 1146 | set_register(LO, rs / rt); |
| 1147 | set_register(HI, rs % rt); |
| 1148 | break; |
| 1149 | case DIVU: |
| 1150 | set_register(LO, rs_u / rt_u); |
| 1151 | set_register(HI, rs_u % rt_u); |
| 1152 | break; |
| 1153 | // Break and trap instructions |
| 1154 | case BREAK: |
| 1155 | case TGE: |
| 1156 | case TGEU: |
| 1157 | case TLT: |
| 1158 | case TLTU: |
| 1159 | case TEQ: |
| 1160 | case TNE: |
| 1161 | if (do_interrupt) { |
| 1162 | SoftwareInterrupt(instr); |
| 1163 | } |
| 1164 | break; |
| 1165 | default: // For other special opcodes we do the default operation. |
| 1166 | set_register(rd_reg, alu_out); |
| 1167 | }; |
| 1168 | break; |
| 1169 | case SPECIAL2: |
| 1170 | switch (instr->FunctionFieldRaw()) { |
| 1171 | case MUL: |
| 1172 | set_register(rd_reg, alu_out); |
| 1173 | // HI and LO are UNPREDICTABLE after the operation. |
| 1174 | set_register(LO, Unpredictable); |
| 1175 | set_register(HI, Unpredictable); |
| 1176 | break; |
| 1177 | default: |
| 1178 | UNREACHABLE(); |
| 1179 | } |
| 1180 | break; |
| 1181 | // Unimplemented opcodes raised an error in the configuration step before, |
| 1182 | // so we can use the default here to set the destination register in common |
| 1183 | // cases. |
| 1184 | default: |
| 1185 | set_register(rd_reg, alu_out); |
| 1186 | }; |
| 1187 | } |
| 1188 | |
| 1189 | // Type 2: instructions using a 16 bytes immediate. (eg: addi, beq) |
| 1190 | void Simulator::DecodeTypeImmediate(Instruction* instr) { |
| 1191 | // Instruction fields |
| 1192 | Opcode op = instr->OpcodeFieldRaw(); |
| 1193 | int32_t rs = get_register(instr->RsField()); |
| 1194 | uint32_t rs_u = static_cast<uint32_t>(rs); |
| 1195 | int32_t rt_reg = instr->RtField(); // destination register |
| 1196 | int32_t rt = get_register(rt_reg); |
| 1197 | int16_t imm16 = instr->Imm16Field(); |
| 1198 | |
| 1199 | int32_t ft_reg = instr->FtField(); // destination register |
| 1200 | int32_t ft = get_register(ft_reg); |
| 1201 | |
| 1202 | // zero extended immediate |
| 1203 | uint32_t oe_imm16 = 0xffff & imm16; |
| 1204 | // sign extended immediate |
| 1205 | int32_t se_imm16 = imm16; |
| 1206 | |
| 1207 | // Get current pc. |
| 1208 | int32_t current_pc = get_pc(); |
| 1209 | // Next pc. |
| 1210 | int32_t next_pc = bad_ra; |
| 1211 | |
| 1212 | // Used for conditional branch instructions |
| 1213 | bool do_branch = false; |
| 1214 | bool execute_branch_delay_instruction = false; |
| 1215 | |
| 1216 | // Used for arithmetic instructions |
| 1217 | int32_t alu_out = 0; |
| 1218 | // Floating point |
| 1219 | double fp_out = 0.0; |
| 1220 | |
| 1221 | // Used for memory instructions |
| 1222 | int32_t addr = 0x0; |
| 1223 | |
| 1224 | // ---------- Configuration (and execution for REGIMM) |
| 1225 | switch (op) { |
| 1226 | // ------------- COP1. Coprocessor instructions |
| 1227 | case COP1: |
| 1228 | switch (instr->RsFieldRaw()) { |
| 1229 | case BC1: // branch on coprocessor condition |
| 1230 | UNIMPLEMENTED_MIPS(); |
| 1231 | break; |
| 1232 | default: |
| 1233 | UNREACHABLE(); |
| 1234 | }; |
| 1235 | break; |
| 1236 | // ------------- REGIMM class |
| 1237 | case REGIMM: |
| 1238 | switch (instr->RtFieldRaw()) { |
| 1239 | case BLTZ: |
| 1240 | do_branch = (rs < 0); |
| 1241 | break; |
| 1242 | case BLTZAL: |
| 1243 | do_branch = rs < 0; |
| 1244 | break; |
| 1245 | case BGEZ: |
| 1246 | do_branch = rs >= 0; |
| 1247 | break; |
| 1248 | case BGEZAL: |
| 1249 | do_branch = rs >= 0; |
| 1250 | break; |
| 1251 | default: |
| 1252 | UNREACHABLE(); |
| 1253 | }; |
| 1254 | switch (instr->RtFieldRaw()) { |
| 1255 | case BLTZ: |
| 1256 | case BLTZAL: |
| 1257 | case BGEZ: |
| 1258 | case BGEZAL: |
| 1259 | // Branch instructions common part. |
| 1260 | execute_branch_delay_instruction = true; |
| 1261 | // Set next_pc |
| 1262 | if (do_branch) { |
| 1263 | next_pc = current_pc + (imm16 << 2) + Instruction::kInstructionSize; |
| 1264 | if (instr->IsLinkingInstruction()) { |
| 1265 | set_register(31, current_pc + kBranchReturnOffset); |
| 1266 | } |
| 1267 | } else { |
| 1268 | next_pc = current_pc + kBranchReturnOffset; |
| 1269 | } |
| 1270 | default: |
| 1271 | break; |
| 1272 | }; |
| 1273 | break; // case REGIMM |
| 1274 | // ------------- Branch instructions |
| 1275 | // When comparing to zero, the encoding of rt field is always 0, so we don't |
| 1276 | // need to replace rt with zero. |
| 1277 | case BEQ: |
| 1278 | do_branch = (rs == rt); |
| 1279 | break; |
| 1280 | case BNE: |
| 1281 | do_branch = rs != rt; |
| 1282 | break; |
| 1283 | case BLEZ: |
| 1284 | do_branch = rs <= 0; |
| 1285 | break; |
| 1286 | case BGTZ: |
| 1287 | do_branch = rs > 0; |
| 1288 | break; |
| 1289 | // ------------- Arithmetic instructions |
| 1290 | case ADDI: |
| 1291 | if (HaveSameSign(rs, se_imm16)) { |
| 1292 | if (rs > 0) { |
| 1293 | exceptions[kIntegerOverflow] = rs > (Registers::kMaxValue - se_imm16); |
| 1294 | } else if (rs < 0) { |
| 1295 | exceptions[kIntegerUnderflow] = |
| 1296 | rs < (Registers::kMinValue - se_imm16); |
| 1297 | } |
| 1298 | } |
| 1299 | alu_out = rs + se_imm16; |
| 1300 | break; |
| 1301 | case ADDIU: |
| 1302 | alu_out = rs + se_imm16; |
| 1303 | break; |
| 1304 | case SLTI: |
| 1305 | alu_out = (rs < se_imm16) ? 1 : 0; |
| 1306 | break; |
| 1307 | case SLTIU: |
| 1308 | alu_out = (rs_u < static_cast<uint32_t>(se_imm16)) ? 1 : 0; |
| 1309 | break; |
| 1310 | case ANDI: |
| 1311 | alu_out = rs & oe_imm16; |
| 1312 | break; |
| 1313 | case ORI: |
| 1314 | alu_out = rs | oe_imm16; |
| 1315 | break; |
| 1316 | case XORI: |
| 1317 | alu_out = rs ^ oe_imm16; |
| 1318 | break; |
| 1319 | case LUI: |
| 1320 | alu_out = (oe_imm16 << 16); |
| 1321 | break; |
| 1322 | // ------------- Memory instructions |
| 1323 | case LB: |
| 1324 | addr = rs + se_imm16; |
| 1325 | alu_out = ReadB(addr); |
| 1326 | break; |
| 1327 | case LW: |
| 1328 | addr = rs + se_imm16; |
| 1329 | alu_out = ReadW(addr, instr); |
| 1330 | break; |
| 1331 | case LBU: |
| 1332 | addr = rs + se_imm16; |
| 1333 | alu_out = ReadBU(addr); |
| 1334 | break; |
| 1335 | case SB: |
| 1336 | addr = rs + se_imm16; |
| 1337 | break; |
| 1338 | case SW: |
| 1339 | addr = rs + se_imm16; |
| 1340 | break; |
| 1341 | case LWC1: |
| 1342 | addr = rs + se_imm16; |
| 1343 | alu_out = ReadW(addr, instr); |
| 1344 | break; |
| 1345 | case LDC1: |
| 1346 | addr = rs + se_imm16; |
| 1347 | fp_out = ReadD(addr, instr); |
| 1348 | break; |
| 1349 | case SWC1: |
| 1350 | case SDC1: |
| 1351 | addr = rs + se_imm16; |
| 1352 | break; |
| 1353 | default: |
| 1354 | UNREACHABLE(); |
| 1355 | }; |
| 1356 | |
| 1357 | // ---------- Raise exceptions triggered. |
| 1358 | SignalExceptions(); |
| 1359 | |
| 1360 | // ---------- Execution |
| 1361 | switch (op) { |
| 1362 | // ------------- Branch instructions |
| 1363 | case BEQ: |
| 1364 | case BNE: |
| 1365 | case BLEZ: |
| 1366 | case BGTZ: |
| 1367 | // Branch instructions common part. |
| 1368 | execute_branch_delay_instruction = true; |
| 1369 | // Set next_pc |
| 1370 | if (do_branch) { |
| 1371 | next_pc = current_pc + (imm16 << 2) + Instruction::kInstructionSize; |
| 1372 | if (instr->IsLinkingInstruction()) { |
| 1373 | set_register(31, current_pc + 2* Instruction::kInstructionSize); |
| 1374 | } |
| 1375 | } else { |
| 1376 | next_pc = current_pc + 2 * Instruction::kInstructionSize; |
| 1377 | } |
| 1378 | break; |
| 1379 | // ------------- Arithmetic instructions |
| 1380 | case ADDI: |
| 1381 | case ADDIU: |
| 1382 | case SLTI: |
| 1383 | case SLTIU: |
| 1384 | case ANDI: |
| 1385 | case ORI: |
| 1386 | case XORI: |
| 1387 | case LUI: |
| 1388 | set_register(rt_reg, alu_out); |
| 1389 | break; |
| 1390 | // ------------- Memory instructions |
| 1391 | case LB: |
| 1392 | case LW: |
| 1393 | case LBU: |
| 1394 | set_register(rt_reg, alu_out); |
| 1395 | break; |
| 1396 | case SB: |
| 1397 | WriteB(addr, static_cast<int8_t>(rt)); |
| 1398 | break; |
| 1399 | case SW: |
| 1400 | WriteW(addr, rt, instr); |
| 1401 | break; |
| 1402 | case LWC1: |
| 1403 | set_fpu_register(ft_reg, alu_out); |
| 1404 | break; |
| 1405 | case LDC1: |
| 1406 | set_fpu_register_double(ft_reg, fp_out); |
| 1407 | break; |
| 1408 | case SWC1: |
| 1409 | addr = rs + se_imm16; |
| 1410 | WriteW(addr, get_fpu_register(ft_reg), instr); |
| 1411 | break; |
| 1412 | case SDC1: |
| 1413 | addr = rs + se_imm16; |
| 1414 | WriteD(addr, ft, instr); |
| 1415 | break; |
| 1416 | default: |
| 1417 | break; |
| 1418 | }; |
| 1419 | |
| 1420 | |
| 1421 | if (execute_branch_delay_instruction) { |
| 1422 | // Execute branch delay slot |
| 1423 | // We don't check for end_sim_pc. First it should not be met as the current |
| 1424 | // pc is valid. Secondly a jump should always execute its branch delay slot. |
| 1425 | Instruction* branch_delay_instr = |
| 1426 | reinterpret_cast<Instruction*>(current_pc+Instruction::kInstructionSize); |
| 1427 | BranchDelayInstructionDecode(branch_delay_instr); |
| 1428 | } |
| 1429 | |
| 1430 | // If needed update pc after the branch delay execution. |
| 1431 | if (next_pc != bad_ra) { |
| 1432 | set_pc(next_pc); |
| 1433 | } |
| 1434 | } |
| 1435 | |
| 1436 | // Type 3: instructions using a 26 bytes immediate. (eg: j, jal) |
| 1437 | void Simulator::DecodeTypeJump(Instruction* instr) { |
| 1438 | // Get current pc. |
| 1439 | int32_t current_pc = get_pc(); |
| 1440 | // Get unchanged bits of pc. |
| 1441 | int32_t pc_high_bits = current_pc & 0xf0000000; |
| 1442 | // Next pc |
| 1443 | int32_t next_pc = pc_high_bits | (instr->Imm26Field() << 2); |
| 1444 | |
| 1445 | // Execute branch delay slot |
| 1446 | // We don't check for end_sim_pc. First it should not be met as the current pc |
| 1447 | // is valid. Secondly a jump should always execute its branch delay slot. |
| 1448 | Instruction* branch_delay_instr = |
| 1449 | reinterpret_cast<Instruction*>(current_pc+Instruction::kInstructionSize); |
| 1450 | BranchDelayInstructionDecode(branch_delay_instr); |
| 1451 | |
| 1452 | // Update pc and ra if necessary. |
| 1453 | // Do this after the branch delay execution. |
| 1454 | if (instr->IsLinkingInstruction()) { |
| 1455 | set_register(31, current_pc + 2* Instruction::kInstructionSize); |
| 1456 | } |
| 1457 | set_pc(next_pc); |
| 1458 | pc_modified_ = true; |
| 1459 | } |
| 1460 | |
| 1461 | // Executes the current instruction. |
| 1462 | void Simulator::InstructionDecode(Instruction* instr) { |
| 1463 | pc_modified_ = false; |
| 1464 | if (::v8::internal::FLAG_trace_sim) { |
| 1465 | disasm::NameConverter converter; |
| 1466 | disasm::Disassembler dasm(converter); |
| 1467 | // use a reasonably large buffer |
| 1468 | v8::internal::EmbeddedVector<char, 256> buffer; |
| 1469 | dasm.InstructionDecode(buffer, |
| 1470 | reinterpret_cast<byte_*>(instr)); |
| 1471 | PrintF(" 0x%08x %s\n", instr, buffer.start()); |
| 1472 | } |
| 1473 | |
| 1474 | switch (instr->InstructionType()) { |
| 1475 | case Instruction::kRegisterType: |
| 1476 | DecodeTypeRegister(instr); |
| 1477 | break; |
| 1478 | case Instruction::kImmediateType: |
| 1479 | DecodeTypeImmediate(instr); |
| 1480 | break; |
| 1481 | case Instruction::kJumpType: |
| 1482 | DecodeTypeJump(instr); |
| 1483 | break; |
| 1484 | default: |
| 1485 | UNSUPPORTED(); |
| 1486 | } |
| 1487 | if (!pc_modified_) { |
| 1488 | set_register(pc, reinterpret_cast<int32_t>(instr) + |
| 1489 | Instruction::kInstructionSize); |
| 1490 | } |
| 1491 | } |
| 1492 | |
| 1493 | |
| 1494 | |
| 1495 | void Simulator::Execute() { |
| 1496 | // Get the PC to simulate. Cannot use the accessor here as we need the |
| 1497 | // raw PC value and not the one used as input to arithmetic instructions. |
| 1498 | int program_counter = get_pc(); |
| 1499 | if (::v8::internal::FLAG_stop_sim_at == 0) { |
| 1500 | // Fast version of the dispatch loop without checking whether the simulator |
| 1501 | // should be stopping at a particular executed instruction. |
| 1502 | while (program_counter != end_sim_pc) { |
| 1503 | Instruction* instr = reinterpret_cast<Instruction*>(program_counter); |
| 1504 | icount_++; |
| 1505 | InstructionDecode(instr); |
| 1506 | program_counter = get_pc(); |
| 1507 | } |
| 1508 | } else { |
| 1509 | // FLAG_stop_sim_at is at the non-default value. Stop in the debugger when |
| 1510 | // we reach the particular instuction count. |
| 1511 | while (program_counter != end_sim_pc) { |
| 1512 | Instruction* instr = reinterpret_cast<Instruction*>(program_counter); |
| 1513 | icount_++; |
| 1514 | if (icount_ == ::v8::internal::FLAG_stop_sim_at) { |
| 1515 | Debugger dbg(this); |
| 1516 | dbg.Debug(); |
| 1517 | } else { |
| 1518 | InstructionDecode(instr); |
| 1519 | } |
| 1520 | program_counter = get_pc(); |
| 1521 | } |
| 1522 | } |
| 1523 | } |
| 1524 | |
| 1525 | |
| 1526 | int32_t Simulator::Call(byte_* entry, int argument_count, ...) { |
| 1527 | va_list parameters; |
| 1528 | va_start(parameters, argument_count); |
| 1529 | // Setup arguments |
| 1530 | |
| 1531 | // First four arguments passed in registers. |
| 1532 | ASSERT(argument_count >= 4); |
| 1533 | set_register(a0, va_arg(parameters, int32_t)); |
| 1534 | set_register(a1, va_arg(parameters, int32_t)); |
| 1535 | set_register(a2, va_arg(parameters, int32_t)); |
| 1536 | set_register(a3, va_arg(parameters, int32_t)); |
| 1537 | |
| 1538 | // Remaining arguments passed on stack. |
| 1539 | int original_stack = get_register(sp); |
| 1540 | // Compute position of stack on entry to generated code. |
| 1541 | int entry_stack = (original_stack - (argument_count - 4) * sizeof(int32_t) |
| 1542 | - kArgsSlotsSize); |
| 1543 | if (OS::ActivationFrameAlignment() != 0) { |
| 1544 | entry_stack &= -OS::ActivationFrameAlignment(); |
| 1545 | } |
| 1546 | // Store remaining arguments on stack, from low to high memory. |
| 1547 | intptr_t* stack_argument = reinterpret_cast<intptr_t*>(entry_stack); |
| 1548 | for (int i = 4; i < argument_count; i++) { |
| 1549 | stack_argument[i - 4 + kArgsSlotsNum] = va_arg(parameters, int32_t); |
| 1550 | } |
| 1551 | va_end(parameters); |
| 1552 | set_register(sp, entry_stack); |
| 1553 | |
| 1554 | // Prepare to execute the code at entry |
| 1555 | set_register(pc, reinterpret_cast<int32_t>(entry)); |
| 1556 | // Put down marker for end of simulation. The simulator will stop simulation |
| 1557 | // when the PC reaches this value. By saving the "end simulation" value into |
| 1558 | // the LR the simulation stops when returning to this call point. |
| 1559 | set_register(ra, end_sim_pc); |
| 1560 | |
| 1561 | // Remember the values of callee-saved registers. |
| 1562 | // The code below assumes that r9 is not used as sb (static base) in |
| 1563 | // simulator code and therefore is regarded as a callee-saved register. |
| 1564 | int32_t s0_val = get_register(s0); |
| 1565 | int32_t s1_val = get_register(s1); |
| 1566 | int32_t s2_val = get_register(s2); |
| 1567 | int32_t s3_val = get_register(s3); |
| 1568 | int32_t s4_val = get_register(s4); |
| 1569 | int32_t s5_val = get_register(s5); |
| 1570 | int32_t s6_val = get_register(s6); |
| 1571 | int32_t s7_val = get_register(s7); |
| 1572 | int32_t gp_val = get_register(gp); |
| 1573 | int32_t sp_val = get_register(sp); |
| 1574 | int32_t fp_val = get_register(fp); |
| 1575 | |
| 1576 | // Setup the callee-saved registers with a known value. To be able to check |
| 1577 | // that they are preserved properly across JS execution. |
| 1578 | int32_t callee_saved_value = icount_; |
| 1579 | set_register(s0, callee_saved_value); |
| 1580 | set_register(s1, callee_saved_value); |
| 1581 | set_register(s2, callee_saved_value); |
| 1582 | set_register(s3, callee_saved_value); |
| 1583 | set_register(s4, callee_saved_value); |
| 1584 | set_register(s5, callee_saved_value); |
| 1585 | set_register(s6, callee_saved_value); |
| 1586 | set_register(s7, callee_saved_value); |
| 1587 | set_register(gp, callee_saved_value); |
| 1588 | set_register(fp, callee_saved_value); |
| 1589 | |
| 1590 | // Start the simulation |
| 1591 | Execute(); |
| 1592 | |
| 1593 | // Check that the callee-saved registers have been preserved. |
| 1594 | CHECK_EQ(callee_saved_value, get_register(s0)); |
| 1595 | CHECK_EQ(callee_saved_value, get_register(s1)); |
| 1596 | CHECK_EQ(callee_saved_value, get_register(s2)); |
| 1597 | CHECK_EQ(callee_saved_value, get_register(s3)); |
| 1598 | CHECK_EQ(callee_saved_value, get_register(s4)); |
| 1599 | CHECK_EQ(callee_saved_value, get_register(s5)); |
| 1600 | CHECK_EQ(callee_saved_value, get_register(s6)); |
| 1601 | CHECK_EQ(callee_saved_value, get_register(s7)); |
| 1602 | CHECK_EQ(callee_saved_value, get_register(gp)); |
| 1603 | CHECK_EQ(callee_saved_value, get_register(fp)); |
| 1604 | |
| 1605 | // Restore callee-saved registers with the original value. |
| 1606 | set_register(s0, s0_val); |
| 1607 | set_register(s1, s1_val); |
| 1608 | set_register(s2, s2_val); |
| 1609 | set_register(s3, s3_val); |
| 1610 | set_register(s4, s4_val); |
| 1611 | set_register(s5, s5_val); |
| 1612 | set_register(s6, s6_val); |
| 1613 | set_register(s7, s7_val); |
| 1614 | set_register(gp, gp_val); |
| 1615 | set_register(sp, sp_val); |
| 1616 | set_register(fp, fp_val); |
| 1617 | |
| 1618 | // Pop stack passed arguments. |
| 1619 | CHECK_EQ(entry_stack, get_register(sp)); |
| 1620 | set_register(sp, original_stack); |
| 1621 | |
| 1622 | int32_t result = get_register(v0); |
| 1623 | return result; |
| 1624 | } |
| 1625 | |
| 1626 | |
| 1627 | uintptr_t Simulator::PushAddress(uintptr_t address) { |
| 1628 | int new_sp = get_register(sp) - sizeof(uintptr_t); |
| 1629 | uintptr_t* stack_slot = reinterpret_cast<uintptr_t*>(new_sp); |
| 1630 | *stack_slot = address; |
| 1631 | set_register(sp, new_sp); |
| 1632 | return new_sp; |
| 1633 | } |
| 1634 | |
| 1635 | |
| 1636 | uintptr_t Simulator::PopAddress() { |
| 1637 | int current_sp = get_register(sp); |
| 1638 | uintptr_t* stack_slot = reinterpret_cast<uintptr_t*>(current_sp); |
| 1639 | uintptr_t address = *stack_slot; |
| 1640 | set_register(sp, current_sp + sizeof(uintptr_t)); |
| 1641 | return address; |
| 1642 | } |
| 1643 | |
| 1644 | |
| 1645 | #undef UNSUPPORTED |
| 1646 | |
| 1647 | } } // namespace assembler::mips |
| 1648 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1649 | #endif // __mips |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 1650 | |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame^] | 1651 | #endif // V8_TARGET_ARCH_MIPS |