Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2010 the V8 project authors. All rights reserved. | 1 // Copyright 2010 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| 11 // with the distribution. | 11 // with the distribution. |
| 12 // * Neither the name of Google Inc. nor the names of its | 12 // * Neither the name of Google Inc. nor the names of its |
| 13 // contributors may be used to endorse or promote products derived | 13 // contributors may be used to endorse or promote products derived |
| 14 // from this software without specific prior written permission. | 14 // from this software without specific prior written permission. |
| 15 // | 15 // |
| 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | 27 |
| 28 #include <stdlib.h> | 28 #include <stdlib.h> |
| 29 #include <math.h> | |
| 30 #include <limits.h> | |
| 29 #include <cstdarg> | 31 #include <cstdarg> |
| 30 #include "v8.h" | 32 #include "v8.h" |
| 31 | 33 |
| 32 #if defined(V8_TARGET_ARCH_MIPS) | 34 #if defined(V8_TARGET_ARCH_MIPS) |
| 33 | 35 |
| 34 #include "disasm.h" | 36 #include "disasm.h" |
| 35 #include "assembler.h" | 37 #include "assembler.h" |
| 36 #include "globals.h" // Need the BitCast | 38 #include "globals.h" // Need the BitCast |
| 37 #include "mips/constants-mips.h" | 39 #include "mips/constants-mips.h" |
| 38 #include "mips/simulator-mips.h" | 40 #include "mips/simulator-mips.h" |
| 39 | 41 |
| 40 namespace v8i = v8::internal; | |
| 41 | |
| 42 #if !defined(__mips) || defined(USE_SIMULATOR) | |
| 43 | 42 |
| 44 // Only build the simulator if not compiling for real MIPS hardware. | 43 // Only build the simulator if not compiling for real MIPS hardware. |
| 45 namespace assembler { | 44 #if defined(USE_SIMULATOR) |
| 46 namespace mips { | |
| 47 | 45 |
| 48 using ::v8::internal::Object; | 46 namespace v8 { |
| 49 using ::v8::internal::PrintF; | 47 namespace internal { |
| 50 using ::v8::internal::OS; | |
| 51 using ::v8::internal::ReadLine; | |
| 52 using ::v8::internal::DeleteArray; | |
| 53 | 48 |
| 54 // Utils functions | 49 // Utils functions |
| 55 bool HaveSameSign(int32_t a, int32_t b) { | 50 bool HaveSameSign(int32_t a, int32_t b) { |
| 56 return ((a ^ b) > 0); | 51 return ((a ^ b) >= 0); |
| 57 } | 52 } |
| 58 | 53 |
| 59 | 54 |
| 55 uint32_t get_fcsr_condition_bit(uint32_t cc) { | |
| 56 if (cc == 0) { | |
| 57 return 23; | |
| 58 } else { | |
| 59 return 24 + cc; | |
| 60 } | |
| 61 } | |
| 62 | |
| 63 | |
| 60 // This macro provides a platform independent use of sscanf. The reason for | 64 // This macro provides a platform independent use of sscanf. The reason for |
| 61 // SScanF not being implemented in a platform independent was through | 65 // 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 | 66 // ::v8::internal::OS in the same way as SNPrintF is that the Windows C Run-Time |
| 63 // Library does not provide vsscanf. | 67 // Library does not provide vsscanf. |
| 64 #define SScanF sscanf // NOLINT | 68 #define SScanF sscanf // NOLINT |
| 65 | 69 |
| 66 // The Debugger class is used by the simulator while debugging simulated MIPS | 70 // The MipsDebugger class is used by the simulator while debugging simulated |
| 67 // code. | 71 // code. |
| 68 class Debugger { | 72 class MipsDebugger { |
| 69 public: | 73 public: |
| 70 explicit Debugger(Simulator* sim); | 74 explicit MipsDebugger(Simulator* sim); |
| 71 ~Debugger(); | 75 ~MipsDebugger(); |
| 72 | 76 |
| 73 void Stop(Instruction* instr); | 77 void Stop(Instruction* instr); |
| 74 void Debug(); | 78 void Debug(); |
| 79 // Print all registers with a nice formatting. | |
| 80 void PrintAllRegs(); | |
| 81 void PrintAllRegsIncludingFPU(); | |
| 75 | 82 |
| 76 private: | 83 private: |
| 77 // We set the breakpoint code to 0xfffff to easily recognize it. | 84 // We set the breakpoint code to 0xfffff to easily recognize it. |
| 78 static const Instr kBreakpointInstr = SPECIAL | BREAK | 0xfffff << 6; | 85 static const Instr kBreakpointInstr = SPECIAL | BREAK | 0xfffff << 6; |
| 79 static const Instr kNopInstr = 0x0; | 86 static const Instr kNopInstr = 0x0; |
| 80 | 87 |
| 81 Simulator* sim_; | 88 Simulator* sim_; |
| 82 | 89 |
| 83 int32_t GetRegisterValue(int regnum); | 90 int32_t GetRegisterValue(int regnum); |
| 91 int32_t GetFPURegisterValueInt(int regnum); | |
| 92 int64_t GetFPURegisterValueLong(int regnum); | |
| 93 float GetFPURegisterValueFloat(int regnum); | |
| 94 double GetFPURegisterValueDouble(int regnum); | |
| 84 bool GetValue(const char* desc, int32_t* value); | 95 bool GetValue(const char* desc, int32_t* value); |
| 85 | 96 |
| 86 // Set or delete a breakpoint. Returns true if successful. | 97 // Set or delete a breakpoint. Returns true if successful. |
| 87 bool SetBreakpoint(Instruction* breakpc); | 98 bool SetBreakpoint(Instruction* breakpc); |
| 88 bool DeleteBreakpoint(Instruction* breakpc); | 99 bool DeleteBreakpoint(Instruction* breakpc); |
| 89 | 100 |
| 90 // Undo and redo all breakpoints. This is needed to bracket disassembly and | 101 // Undo and redo all breakpoints. This is needed to bracket disassembly and |
| 91 // execution to skip past breakpoints when run from the debugger. | 102 // execution to skip past breakpoints when run from the debugger. |
| 92 void UndoBreakpoints(); | 103 void UndoBreakpoints(); |
| 93 void RedoBreakpoints(); | 104 void RedoBreakpoints(); |
| 94 | |
| 95 // Print all registers with a nice formatting. | |
| 96 void PrintAllRegs(); | |
| 97 }; | 105 }; |
| 98 | 106 |
| 99 Debugger::Debugger(Simulator* sim) { | 107 MipsDebugger::MipsDebugger(Simulator* sim) { |
| 100 sim_ = sim; | 108 sim_ = sim; |
| 101 } | 109 } |
| 102 | 110 |
| 103 Debugger::~Debugger() { | 111 |
| 112 MipsDebugger::~MipsDebugger() { | |
| 104 } | 113 } |
| 105 | 114 |
| 115 | |
| 106 #ifdef GENERATED_CODE_COVERAGE | 116 #ifdef GENERATED_CODE_COVERAGE |
| 107 static FILE* coverage_log = NULL; | 117 static FILE* coverage_log = NULL; |
| 108 | 118 |
| 109 | 119 |
| 110 static void InitializeCoverage() { | 120 static void InitializeCoverage() { |
| 111 char* file_name = getenv("V8_GENERATED_CODE_COVERAGE_LOG"); | 121 char* file_name = getenv("V8_GENERATED_CODE_COVERAGE_LOG"); |
| 112 if (file_name != NULL) { | 122 if (file_name != NULL) { |
| 113 coverage_log = fopen(file_name, "aw+"); | 123 coverage_log = fopen(file_name, "aw+"); |
| 114 } | 124 } |
| 115 } | 125 } |
| 116 | 126 |
| 117 | 127 |
| 118 void Debugger::Stop(Instruction* instr) { | 128 void MipsDebugger::Stop(Instruction* instr) { |
| 119 UNIMPLEMENTED_MIPS(); | 129 UNIMPLEMENTED_MIPS(); |
| 120 char* str = reinterpret_cast<char*>(instr->InstructionBits()); | 130 char* str = reinterpret_cast<char*>(instr->InstructionBits()); |
| 121 if (strlen(str) > 0) { | 131 if (strlen(str) > 0) { |
| 122 if (coverage_log != NULL) { | 132 if (coverage_log != NULL) { |
| 123 fprintf(coverage_log, "%s\n", str); | 133 fprintf(coverage_log, "%s\n", str); |
| 124 fflush(coverage_log); | 134 fflush(coverage_log); |
| 125 } | 135 } |
| 126 instr->SetInstructionBits(0x0); // Overwrite with nop. | 136 instr->SetInstructionBits(0x0); // Overwrite with nop. |
| 127 } | 137 } |
| 128 sim_->set_pc(sim_->get_pc() + Instruction::kInstructionSize); | 138 sim_->set_pc(sim_->get_pc() + Instruction::kInstrSize); |
| 129 } | 139 } |
| 130 | 140 |
| 141 | |
| 131 #else // ndef GENERATED_CODE_COVERAGE | 142 #else // ndef GENERATED_CODE_COVERAGE |
| 132 | 143 |
| 133 #define UNSUPPORTED() printf("Unsupported instruction.\n"); | 144 #define UNSUPPORTED() printf("Unsupported instruction.\n"); |
| 134 | 145 |
| 135 static void InitializeCoverage() {} | 146 static void InitializeCoverage() {} |
| 136 | 147 |
| 137 | 148 |
| 138 void Debugger::Stop(Instruction* instr) { | 149 void MipsDebugger::Stop(Instruction* instr) { |
| 139 const char* str = reinterpret_cast<char*>(instr->InstructionBits()); | 150 const char* str = reinterpret_cast<char*>(instr->InstructionBits()); |
| 140 PrintF("Simulator hit %s\n", str); | 151 PrintF("Simulator hit %s\n", str); |
| 141 sim_->set_pc(sim_->get_pc() + Instruction::kInstructionSize); | 152 sim_->set_pc(sim_->get_pc() + Instruction::kInstrSize); |
| 142 Debug(); | 153 Debug(); |
| 143 } | 154 } |
| 144 #endif // GENERATED_CODE_COVERAGE | 155 #endif // GENERATED_CODE_COVERAGE |
| 145 | 156 |
| 146 | 157 |
| 147 int32_t Debugger::GetRegisterValue(int regnum) { | 158 int32_t MipsDebugger::GetRegisterValue(int regnum) { |
| 148 if (regnum == kNumSimuRegisters) { | 159 if (regnum == kNumSimuRegisters) { |
| 149 return sim_->get_pc(); | 160 return sim_->get_pc(); |
| 150 } else { | 161 } else { |
| 151 return sim_->get_register(regnum); | 162 return sim_->get_register(regnum); |
| 152 } | 163 } |
| 153 } | 164 } |
| 154 | 165 |
| 155 | 166 |
| 156 bool Debugger::GetValue(const char* desc, int32_t* value) { | 167 int32_t MipsDebugger::GetFPURegisterValueInt(int regnum) { |
| 168 if (regnum == kNumFPURegisters) { | |
| 169 return sim_->get_pc(); | |
| 170 } else { | |
| 171 return sim_->get_fpu_register(regnum); | |
| 172 } | |
| 173 } | |
| 174 | |
| 175 | |
| 176 int64_t MipsDebugger::GetFPURegisterValueLong(int regnum) { | |
| 177 if (regnum == kNumFPURegisters) { | |
| 178 return sim_->get_pc(); | |
| 179 } else { | |
| 180 return sim_->get_fpu_register_long(regnum); | |
| 181 } | |
| 182 } | |
| 183 | |
| 184 | |
| 185 float MipsDebugger::GetFPURegisterValueFloat(int regnum) { | |
| 186 if (regnum == kNumFPURegisters) { | |
| 187 return sim_->get_pc(); | |
| 188 } else { | |
| 189 return sim_->get_fpu_register_float(regnum); | |
| 190 } | |
| 191 } | |
| 192 | |
| 193 | |
| 194 double MipsDebugger::GetFPURegisterValueDouble(int regnum) { | |
| 195 if (regnum == kNumFPURegisters) { | |
| 196 return sim_->get_pc(); | |
| 197 } else { | |
| 198 return sim_->get_fpu_register_double(regnum); | |
| 199 } | |
| 200 } | |
| 201 | |
| 202 | |
| 203 bool MipsDebugger::GetValue(const char* desc, int32_t* value) { | |
| 157 int regnum = Registers::Number(desc); | 204 int regnum = Registers::Number(desc); |
| 205 int fpuregnum = FPURegisters::Number(desc); | |
| 206 | |
| 158 if (regnum != kInvalidRegister) { | 207 if (regnum != kInvalidRegister) { |
| 159 *value = GetRegisterValue(regnum); | 208 *value = GetRegisterValue(regnum); |
| 160 return true; | 209 return true; |
| 210 } else if (fpuregnum != kInvalidFPURegister) { | |
| 211 *value = GetFPURegisterValueInt(fpuregnum); | |
| 212 return true; | |
| 213 } else if (strncmp(desc, "0x", 2) == 0) { | |
| 214 return SScanF(desc, "%x", reinterpret_cast<uint32_t*>(value)) == 1; | |
| 161 } else { | 215 } else { |
| 162 return SScanF(desc, "%i", value) == 1; | 216 return SScanF(desc, "%i", value) == 1; |
| 163 } | 217 } |
| 164 return false; | 218 return false; |
| 165 } | 219 } |
| 166 | 220 |
| 167 | 221 |
| 168 bool Debugger::SetBreakpoint(Instruction* breakpc) { | 222 bool MipsDebugger::SetBreakpoint(Instruction* breakpc) { |
| 169 // Check if a breakpoint can be set. If not return without any side-effects. | 223 // Check if a breakpoint can be set. If not return without any side-effects. |
| 170 if (sim_->break_pc_ != NULL) { | 224 if (sim_->break_pc_ != NULL) { |
| 171 return false; | 225 return false; |
| 172 } | 226 } |
| 173 | 227 |
| 174 // Set the breakpoint. | 228 // Set the breakpoint. |
| 175 sim_->break_pc_ = breakpc; | 229 sim_->break_pc_ = breakpc; |
| 176 sim_->break_instr_ = breakpc->InstructionBits(); | 230 sim_->break_instr_ = breakpc->InstructionBits(); |
| 177 // Not setting the breakpoint instruction in the code itself. It will be set | 231 // Not setting the breakpoint instruction in the code itself. It will be set |
| 178 // when the debugger shell continues. | 232 // when the debugger shell continues. |
| 179 return true; | 233 return true; |
| 180 } | 234 } |
| 181 | 235 |
| 182 | 236 |
| 183 bool Debugger::DeleteBreakpoint(Instruction* breakpc) { | 237 bool MipsDebugger::DeleteBreakpoint(Instruction* breakpc) { |
| 184 if (sim_->break_pc_ != NULL) { | 238 if (sim_->break_pc_ != NULL) { |
| 185 sim_->break_pc_->SetInstructionBits(sim_->break_instr_); | 239 sim_->break_pc_->SetInstructionBits(sim_->break_instr_); |
| 186 } | 240 } |
| 187 | 241 |
| 188 sim_->break_pc_ = NULL; | 242 sim_->break_pc_ = NULL; |
| 189 sim_->break_instr_ = 0; | 243 sim_->break_instr_ = 0; |
| 190 return true; | 244 return true; |
| 191 } | 245 } |
| 192 | 246 |
| 193 | 247 |
| 194 void Debugger::UndoBreakpoints() { | 248 void MipsDebugger::UndoBreakpoints() { |
| 195 if (sim_->break_pc_ != NULL) { | 249 if (sim_->break_pc_ != NULL) { |
| 196 sim_->break_pc_->SetInstructionBits(sim_->break_instr_); | 250 sim_->break_pc_->SetInstructionBits(sim_->break_instr_); |
| 197 } | 251 } |
| 198 } | 252 } |
| 199 | 253 |
| 200 | 254 |
| 201 void Debugger::RedoBreakpoints() { | 255 void MipsDebugger::RedoBreakpoints() { |
| 202 if (sim_->break_pc_ != NULL) { | 256 if (sim_->break_pc_ != NULL) { |
| 203 sim_->break_pc_->SetInstructionBits(kBreakpointInstr); | 257 sim_->break_pc_->SetInstructionBits(kBreakpointInstr); |
| 204 } | 258 } |
| 205 } | 259 } |
| 206 | 260 |
| 207 void Debugger::PrintAllRegs() { | 261 |
| 262 void MipsDebugger::PrintAllRegs() { | |
| 208 #define REG_INFO(n) Registers::Name(n), GetRegisterValue(n), GetRegisterValue(n) | 263 #define REG_INFO(n) Registers::Name(n), GetRegisterValue(n), GetRegisterValue(n) |
| 209 | 264 |
| 210 PrintF("\n"); | 265 PrintF("\n"); |
| 211 // at, v0, a0 | 266 // at, v0, a0 |
| 212 PrintF("%3s: 0x%08x %10d\t%3s: 0x%08x %10d\t%3s: 0x%08x %10d\n", | 267 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)); | 268 REG_INFO(1), REG_INFO(2), REG_INFO(4)); |
| 214 // v1, a1 | 269 // v1, a1 |
| 215 PrintF("%26s\t%3s: 0x%08x %10d\t%3s: 0x%08x %10d\n", | 270 PrintF("%26s\t%3s: 0x%08x %10d\t%3s: 0x%08x %10d\n", |
| 216 "", REG_INFO(3), REG_INFO(5)); | 271 "", REG_INFO(3), REG_INFO(5)); |
| 217 // a2 | 272 // a2 |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 230 REG_INFO(24), REG_INFO(26), REG_INFO(32)); | 285 REG_INFO(24), REG_INFO(26), REG_INFO(32)); |
| 231 // t9, k1, HI | 286 // t9, k1, HI |
| 232 PrintF("%3s: 0x%08x %10d\t%3s: 0x%08x %10d\t%3s: 0x%08x %10d\n", | 287 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)); | 288 REG_INFO(25), REG_INFO(27), REG_INFO(33)); |
| 234 // sp, fp, gp | 289 // sp, fp, gp |
| 235 PrintF("%3s: 0x%08x %10d\t%3s: 0x%08x %10d\t%3s: 0x%08x %10d\n", | 290 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)); | 291 REG_INFO(29), REG_INFO(30), REG_INFO(28)); |
| 237 // pc | 292 // pc |
| 238 PrintF("%3s: 0x%08x %10d\t%3s: 0x%08x %10d\n", | 293 PrintF("%3s: 0x%08x %10d\t%3s: 0x%08x %10d\n", |
| 239 REG_INFO(31), REG_INFO(34)); | 294 REG_INFO(31), REG_INFO(34)); |
| 295 | |
| 240 #undef REG_INFO | 296 #undef REG_INFO |
| 297 #undef FPU_REG_INFO | |
| 241 } | 298 } |
| 242 | 299 |
| 243 void Debugger::Debug() { | 300 |
| 301 void MipsDebugger::PrintAllRegsIncludingFPU() { | |
| 302 #define FPU_REG_INFO(n) FPURegisters::Name(n), FPURegisters::Name(n+1), \ | |
| 303 GetFPURegisterValueInt(n+1), \ | |
| 304 GetFPURegisterValueInt(n), \ | |
| 305 GetFPURegisterValueDouble(n) | |
| 306 | |
| 307 PrintAllRegs(); | |
| 308 | |
| 309 PrintF("\n\n"); | |
|
Søren Thygesen Gjesse
2011/03/24 22:11:19
Only 2 space indent.
Paul Lind
2011/03/26 18:39:17
Done.
| |
| 310 // f0, f1, f2, ... f31 | |
| 311 PrintF("%3s,%3s: 0x%08x%08x %16.4e\n", FPU_REG_INFO(0) ); | |
| 312 PrintF("%3s,%3s: 0x%08x%08x %16.4e\n", FPU_REG_INFO(2) ); | |
| 313 PrintF("%3s,%3s: 0x%08x%08x %16.4e\n", FPU_REG_INFO(4) ); | |
| 314 PrintF("%3s,%3s: 0x%08x%08x %16.4e\n", FPU_REG_INFO(6) ); | |
| 315 PrintF("%3s,%3s: 0x%08x%08x %16.4e\n", FPU_REG_INFO(8) ); | |
| 316 PrintF("%3s,%3s: 0x%08x%08x %16.4e\n", FPU_REG_INFO(10)); | |
| 317 PrintF("%3s,%3s: 0x%08x%08x %16.4e\n", FPU_REG_INFO(12)); | |
| 318 PrintF("%3s,%3s: 0x%08x%08x %16.4e\n", FPU_REG_INFO(14)); | |
| 319 PrintF("%3s,%3s: 0x%08x%08x %16.4e\n", FPU_REG_INFO(16)); | |
| 320 PrintF("%3s,%3s: 0x%08x%08x %16.4e\n", FPU_REG_INFO(18)); | |
| 321 PrintF("%3s,%3s: 0x%08x%08x %16.4e\n", FPU_REG_INFO(20)); | |
| 322 PrintF("%3s,%3s: 0x%08x%08x %16.4e\n", FPU_REG_INFO(22)); | |
| 323 PrintF("%3s,%3s: 0x%08x%08x %16.4e\n", FPU_REG_INFO(24)); | |
| 324 PrintF("%3s,%3s: 0x%08x%08x %16.4e\n", FPU_REG_INFO(26)); | |
| 325 PrintF("%3s,%3s: 0x%08x%08x %16.4e\n", FPU_REG_INFO(28)); | |
| 326 PrintF("%3s,%3s: 0x%08x%08x %16.4e\n", FPU_REG_INFO(30)); | |
| 327 | |
| 328 #undef REG_INFO | |
| 329 #undef FPU_REG_INFO | |
| 330 } | |
| 331 | |
| 332 | |
| 333 void MipsDebugger::Debug() { | |
| 244 intptr_t last_pc = -1; | 334 intptr_t last_pc = -1; |
| 245 bool done = false; | 335 bool done = false; |
| 246 | 336 |
| 247 #define COMMAND_SIZE 63 | 337 #define COMMAND_SIZE 63 |
| 248 #define ARG_SIZE 255 | 338 #define ARG_SIZE 255 |
| 249 | 339 |
| 250 #define STR(a) #a | 340 #define STR(a) #a |
| 251 #define XSTR(a) STR(a) | 341 #define XSTR(a) STR(a) |
| 252 | 342 |
| 253 char cmd[COMMAND_SIZE + 1]; | 343 char cmd[COMMAND_SIZE + 1]; |
| 254 char arg1[ARG_SIZE + 1]; | 344 char arg1[ARG_SIZE + 1]; |
| 255 char arg2[ARG_SIZE + 1]; | 345 char arg2[ARG_SIZE + 1]; |
| 346 char* argv[3] = { cmd, arg1, arg2 }; | |
| 256 | 347 |
| 257 // make sure to have a proper terminating character if reaching the limit | 348 // make sure to have a proper terminating character if reaching the limit |
| 258 cmd[COMMAND_SIZE] = 0; | 349 cmd[COMMAND_SIZE] = 0; |
| 259 arg1[ARG_SIZE] = 0; | 350 arg1[ARG_SIZE] = 0; |
| 260 arg2[ARG_SIZE] = 0; | 351 arg2[ARG_SIZE] = 0; |
| 261 | 352 |
| 262 // Undo all set breakpoints while running in the debugger shell. This will | 353 // Undo all set breakpoints while running in the debugger shell. This will |
| 263 // make them invisible to all commands. | 354 // make them invisible to all commands. |
| 264 UndoBreakpoints(); | 355 UndoBreakpoints(); |
| 265 | 356 |
| 266 while (!done && (sim_->get_pc() != Simulator::end_sim_pc)) { | 357 while (!done && (sim_->get_pc() != Simulator::end_sim_pc)) { |
| 267 if (last_pc != sim_->get_pc()) { | 358 if (last_pc != sim_->get_pc()) { |
| 268 disasm::NameConverter converter; | 359 disasm::NameConverter converter; |
| 269 disasm::Disassembler dasm(converter); | 360 disasm::Disassembler dasm(converter); |
| 270 // use a reasonably large buffer | 361 // use a reasonably large buffer |
| 271 v8::internal::EmbeddedVector<char, 256> buffer; | 362 v8::internal::EmbeddedVector<char, 256> buffer; |
| 272 dasm.InstructionDecode(buffer, | 363 dasm.InstructionDecode(buffer, |
| 273 reinterpret_cast<byte_*>(sim_->get_pc())); | 364 reinterpret_cast<byte_*>(sim_->get_pc())); |
| 274 PrintF(" 0x%08x %s\n", sim_->get_pc(), buffer.start()); | 365 PrintF(" 0x%08x %s\n", sim_->get_pc(), buffer.start()); |
| 275 last_pc = sim_->get_pc(); | 366 last_pc = sim_->get_pc(); |
| 276 } | 367 } |
| 277 char* line = ReadLine("sim> "); | 368 char* line = ReadLine("sim> "); |
| 278 if (line == NULL) { | 369 if (line == NULL) { |
| 279 break; | 370 break; |
| 280 } else { | 371 } else { |
| 281 // Use sscanf to parse the individual parts of the command line. At the | 372 // Use sscanf to parse the individual parts of the command line. At the |
| 282 // moment no command expects more than two parameters. | 373 // moment no command expects more than two parameters. |
| 283 int args = SScanF(line, | 374 int argc = SScanF(line, |
| 284 "%" XSTR(COMMAND_SIZE) "s " | 375 "%" XSTR(COMMAND_SIZE) "s " |
| 285 "%" XSTR(ARG_SIZE) "s " | 376 "%" XSTR(ARG_SIZE) "s " |
| 286 "%" XSTR(ARG_SIZE) "s", | 377 "%" XSTR(ARG_SIZE) "s", |
| 287 cmd, arg1, arg2); | 378 cmd, arg1, arg2); |
| 288 if ((strcmp(cmd, "si") == 0) || (strcmp(cmd, "stepi") == 0)) { | 379 if ((strcmp(cmd, "si") == 0) || (strcmp(cmd, "stepi") == 0)) { |
| 289 if (!(reinterpret_cast<Instruction*>(sim_->get_pc())->IsTrap())) { | 380 Instruction* instr = reinterpret_cast<Instruction*>(sim_->get_pc()); |
| 381 if (!(instr->IsTrap()) || | |
| 382 instr->InstructionBits() == rtCallRedirInstr) { | |
| 290 sim_->InstructionDecode( | 383 sim_->InstructionDecode( |
| 291 reinterpret_cast<Instruction*>(sim_->get_pc())); | 384 reinterpret_cast<Instruction*>(sim_->get_pc())); |
| 292 } else { | 385 } else { |
| 293 // Allow si to jump over generated breakpoints. | 386 // Allow si to jump over generated breakpoints. |
| 294 PrintF("/!\\ Jumping over generated breakpoint.\n"); | 387 PrintF("/!\\ Jumping over generated breakpoint.\n"); |
| 295 sim_->set_pc(sim_->get_pc() + Instruction::kInstructionSize); | 388 sim_->set_pc(sim_->get_pc() + Instruction::kInstrSize); |
| 296 } | 389 } |
| 297 } else if ((strcmp(cmd, "c") == 0) || (strcmp(cmd, "cont") == 0)) { | 390 } else if ((strcmp(cmd, "c") == 0) || (strcmp(cmd, "cont") == 0)) { |
| 298 // Execute the one instruction we broke at with breakpoints disabled. | 391 // Execute the one instruction we broke at with breakpoints disabled. |
| 299 sim_->InstructionDecode(reinterpret_cast<Instruction*>(sim_->get_pc())); | 392 sim_->InstructionDecode(reinterpret_cast<Instruction*>(sim_->get_pc())); |
| 300 // Leave the debugger shell. | 393 // Leave the debugger shell. |
| 301 done = true; | 394 done = true; |
| 302 } else if ((strcmp(cmd, "p") == 0) || (strcmp(cmd, "print") == 0)) { | 395 } else if ((strcmp(cmd, "p") == 0) || (strcmp(cmd, "print") == 0)) { |
| 303 if (args == 2) { | 396 if (argc == 2) { |
| 304 int32_t value; | 397 int32_t value; |
| 398 float fvalue; | |
| 305 if (strcmp(arg1, "all") == 0) { | 399 if (strcmp(arg1, "all") == 0) { |
| 306 PrintAllRegs(); | 400 PrintAllRegs(); |
| 401 } else if (strcmp(arg1, "allf") == 0) { | |
| 402 PrintAllRegsIncludingFPU(); | |
| 307 } else { | 403 } else { |
| 308 if (GetValue(arg1, &value)) { | 404 int regnum = Registers::Number(arg1); |
| 405 int fpuregnum = FPURegisters::Number(arg1); | |
| 406 | |
| 407 if (regnum != kInvalidRegister) { | |
| 408 value = GetRegisterValue(regnum); | |
| 309 PrintF("%s: 0x%08x %d \n", arg1, value, value); | 409 PrintF("%s: 0x%08x %d \n", arg1, value, value); |
| 410 } else if (fpuregnum != kInvalidFPURegister) { | |
| 411 if (fpuregnum % 2 == 1) { | |
| 412 value = GetFPURegisterValueInt(fpuregnum); | |
| 413 fvalue = GetFPURegisterValueFloat(fpuregnum); | |
| 414 PrintF("%s: 0x%08x %11.4e\n", arg1, value, fvalue); | |
| 415 } else { | |
| 416 double dfvalue; | |
| 417 int32_t lvalue1 = GetFPURegisterValueInt(fpuregnum); | |
| 418 int32_t lvalue2 = GetFPURegisterValueInt(fpuregnum + 1); | |
| 419 dfvalue = GetFPURegisterValueDouble(fpuregnum); | |
| 420 PrintF("%3s,%3s: 0x%08x%08x %16.4e\n", | |
| 421 FPURegisters::Name(fpuregnum+1), | |
| 422 FPURegisters::Name(fpuregnum), | |
| 423 lvalue1, | |
| 424 lvalue2, | |
| 425 dfvalue); | |
| 426 } | |
| 310 } else { | 427 } else { |
| 311 PrintF("%s unrecognized\n", arg1); | 428 PrintF("%s unrecognized\n", arg1); |
| 312 } | 429 } |
| 313 } | 430 } |
| 314 } else { | 431 } else { |
| 315 PrintF("print <register>\n"); | 432 if (argc == 3) { |
| 433 if (strcmp(arg2, "single") == 0) { | |
| 434 int32_t value; | |
| 435 float fvalue; | |
| 436 int fpuregnum = FPURegisters::Number(arg1); | |
| 437 | |
| 438 if (fpuregnum != kInvalidFPURegister) { | |
| 439 value = GetFPURegisterValueInt(fpuregnum); | |
| 440 fvalue = GetFPURegisterValueFloat(fpuregnum); | |
| 441 PrintF("%s: 0x%08x %11.4e\n", arg1, value, fvalue); | |
| 442 } else { | |
| 443 PrintF("%s unrecognized\n", arg1); | |
| 444 } | |
| 445 } else { | |
| 446 PrintF("print <fpu register> single\n"); | |
| 447 } | |
| 448 } else { | |
| 449 PrintF("print <register> or print <fpu register> single\n"); | |
| 450 } | |
| 316 } | 451 } |
| 317 } else if ((strcmp(cmd, "po") == 0) | 452 } else if ((strcmp(cmd, "po") == 0) |
| 318 || (strcmp(cmd, "printobject") == 0)) { | 453 || (strcmp(cmd, "printobject") == 0)) { |
| 319 if (args == 2) { | 454 if (argc == 2) { |
| 320 int32_t value; | 455 int32_t value; |
| 321 if (GetValue(arg1, &value)) { | 456 if (GetValue(arg1, &value)) { |
| 322 Object* obj = reinterpret_cast<Object*>(value); | 457 Object* obj = reinterpret_cast<Object*>(value); |
| 323 PrintF("%s: \n", arg1); | 458 PrintF("%s: \n", arg1); |
| 324 #ifdef DEBUG | 459 #ifdef DEBUG |
| 325 obj->PrintLn(); | 460 obj->PrintLn(); |
| 326 #else | 461 #else |
| 327 obj->ShortPrint(); | 462 obj->ShortPrint(); |
| 328 PrintF("\n"); | 463 PrintF("\n"); |
| 329 #endif | 464 #endif |
| 330 } else { | 465 } else { |
| 331 PrintF("%s unrecognized\n", arg1); | 466 PrintF("%s unrecognized\n", arg1); |
| 332 } | 467 } |
| 333 } else { | 468 } else { |
| 334 PrintF("printobject <value>\n"); | 469 PrintF("printobject <value>\n"); |
| 335 } | 470 } |
| 471 } else if (strcmp(cmd, "stack") == 0 || strcmp(cmd, "mem") == 0) { | |
| 472 int32_t* cur = NULL; | |
| 473 int32_t* end = NULL; | |
| 474 int next_arg = 1; | |
| 475 | |
| 476 if (strcmp(cmd, "stack") == 0) { | |
| 477 cur = reinterpret_cast<int32_t*>(sim_->get_register(Simulator::sp)); | |
| 478 } else { // "mem" | |
| 479 int32_t value; | |
| 480 if (!GetValue(arg1, &value)) { | |
| 481 PrintF("%s unrecognized\n", arg1); | |
| 482 continue; | |
| 483 } | |
| 484 cur = reinterpret_cast<int32_t*>(value); | |
| 485 next_arg++; | |
| 486 } | |
| 487 | |
| 488 int32_t words; | |
| 489 if (argc == next_arg) { | |
| 490 words = 10; | |
| 491 } else if (argc == next_arg + 1) { | |
| 492 if (!GetValue(argv[next_arg], &words)) { | |
| 493 words = 10; | |
| 494 } | |
| 495 } | |
| 496 end = cur + words; | |
| 497 | |
| 498 while (cur < end) { | |
| 499 PrintF(" 0x%08x: 0x%08x %10d\n", | |
| 500 reinterpret_cast<intptr_t>(cur), *cur, *cur); | |
| 501 cur++; | |
| 502 } | |
| 503 | |
| 336 } else if ((strcmp(cmd, "disasm") == 0) || (strcmp(cmd, "dpc") == 0)) { | 504 } else if ((strcmp(cmd, "disasm") == 0) || (strcmp(cmd, "dpc") == 0)) { |
| 337 disasm::NameConverter converter; | 505 disasm::NameConverter converter; |
| 338 disasm::Disassembler dasm(converter); | 506 disasm::Disassembler dasm(converter); |
| 339 // use a reasonably large buffer | 507 // use a reasonably large buffer |
| 340 v8::internal::EmbeddedVector<char, 256> buffer; | 508 v8::internal::EmbeddedVector<char, 256> buffer; |
| 341 | 509 |
| 342 byte_* cur = NULL; | 510 byte_* cur = NULL; |
| 343 byte_* end = NULL; | 511 byte_* end = NULL; |
| 344 | 512 |
| 345 if (args == 1) { | 513 if (argc == 1) { |
| 346 cur = reinterpret_cast<byte_*>(sim_->get_pc()); | 514 cur = reinterpret_cast<byte_*>(sim_->get_pc()); |
| 347 end = cur + (10 * Instruction::kInstructionSize); | 515 end = cur + (10 * Instruction::kInstrSize); |
| 348 } else if (args == 2) { | 516 } else if (argc == 2) { |
| 349 int32_t value; | 517 int32_t value; |
| 350 if (GetValue(arg1, &value)) { | 518 if (GetValue(arg1, &value)) { |
| 351 cur = reinterpret_cast<byte_*>(value); | 519 cur = reinterpret_cast<byte_*>(value); |
| 352 // no length parameter passed, assume 10 instructions | 520 // no length parameter passed, assume 10 instructions |
| 353 end = cur + (10 * Instruction::kInstructionSize); | 521 end = cur + (10 * Instruction::kInstrSize); |
| 354 } | 522 } |
| 355 } else { | 523 } else { |
| 356 int32_t value1; | 524 int32_t value1; |
| 357 int32_t value2; | 525 int32_t value2; |
| 358 if (GetValue(arg1, &value1) && GetValue(arg2, &value2)) { | 526 if (GetValue(arg1, &value1) && GetValue(arg2, &value2)) { |
| 359 cur = reinterpret_cast<byte_*>(value1); | 527 cur = reinterpret_cast<byte_*>(value1); |
| 360 end = cur + (value2 * Instruction::kInstructionSize); | 528 end = cur + (value2 * Instruction::kInstrSize); |
| 361 } | 529 } |
| 362 } | 530 } |
| 363 | 531 |
| 364 while (cur < end) { | 532 while (cur < end) { |
| 365 dasm.InstructionDecode(buffer, cur); | 533 dasm.InstructionDecode(buffer, cur); |
| 366 PrintF(" 0x%08x %s\n", cur, buffer.start()); | 534 PrintF(" 0x%08x %s\n", |
| 367 cur += Instruction::kInstructionSize; | 535 reinterpret_cast<intptr_t>(cur), buffer.start()); |
| 536 cur += Instruction::kInstrSize; | |
| 368 } | 537 } |
| 369 } else if (strcmp(cmd, "gdb") == 0) { | 538 } else if (strcmp(cmd, "gdb") == 0) { |
| 370 PrintF("relinquishing control to gdb\n"); | 539 PrintF("relinquishing control to gdb\n"); |
| 371 v8::internal::OS::DebugBreak(); | 540 v8::internal::OS::DebugBreak(); |
| 372 PrintF("regaining control from gdb\n"); | 541 PrintF("regaining control from gdb\n"); |
| 373 } else if (strcmp(cmd, "break") == 0) { | 542 } else if (strcmp(cmd, "break") == 0) { |
| 374 if (args == 2) { | 543 if (argc == 2) { |
| 375 int32_t value; | 544 int32_t value; |
| 376 if (GetValue(arg1, &value)) { | 545 if (GetValue(arg1, &value)) { |
| 377 if (!SetBreakpoint(reinterpret_cast<Instruction*>(value))) { | 546 if (!SetBreakpoint(reinterpret_cast<Instruction*>(value))) { |
| 378 PrintF("setting breakpoint failed\n"); | 547 PrintF("setting breakpoint failed\n"); |
| 379 } | 548 } |
| 380 } else { | 549 } else { |
| 381 PrintF("%s unrecognized\n", arg1); | 550 PrintF("%s unrecognized\n", arg1); |
| 382 } | 551 } |
| 383 } else { | 552 } else { |
| 384 PrintF("break <address>\n"); | 553 PrintF("break <address>\n"); |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 397 PrintF("\n"); | 566 PrintF("\n"); |
| 398 | 567 |
| 399 disasm::NameConverter converter; | 568 disasm::NameConverter converter; |
| 400 disasm::Disassembler dasm(converter); | 569 disasm::Disassembler dasm(converter); |
| 401 // use a reasonably large buffer | 570 // use a reasonably large buffer |
| 402 v8::internal::EmbeddedVector<char, 256> buffer; | 571 v8::internal::EmbeddedVector<char, 256> buffer; |
| 403 | 572 |
| 404 byte_* cur = NULL; | 573 byte_* cur = NULL; |
| 405 byte_* end = NULL; | 574 byte_* end = NULL; |
| 406 | 575 |
| 407 if (args == 1) { | 576 if (argc == 1) { |
| 408 cur = reinterpret_cast<byte_*>(sim_->get_pc()); | 577 cur = reinterpret_cast<byte_*>(sim_->get_pc()); |
| 409 end = cur + (10 * Instruction::kInstructionSize); | 578 end = cur + (10 * Instruction::kInstrSize); |
| 410 } else if (args == 2) { | 579 } else if (argc == 2) { |
| 411 int32_t value; | 580 int32_t value; |
| 412 if (GetValue(arg1, &value)) { | 581 if (GetValue(arg1, &value)) { |
| 413 cur = reinterpret_cast<byte_*>(value); | 582 cur = reinterpret_cast<byte_*>(value); |
| 414 // no length parameter passed, assume 10 instructions | 583 // no length parameter passed, assume 10 instructions |
| 415 end = cur + (10 * Instruction::kInstructionSize); | 584 end = cur + (10 * Instruction::kInstrSize); |
| 416 } | 585 } |
| 417 } else { | 586 } else { |
| 418 int32_t value1; | 587 int32_t value1; |
| 419 int32_t value2; | 588 int32_t value2; |
| 420 if (GetValue(arg1, &value1) && GetValue(arg2, &value2)) { | 589 if (GetValue(arg1, &value1) && GetValue(arg2, &value2)) { |
| 421 cur = reinterpret_cast<byte_*>(value1); | 590 cur = reinterpret_cast<byte_*>(value1); |
| 422 end = cur + (value2 * Instruction::kInstructionSize); | 591 end = cur + (value2 * Instruction::kInstrSize); |
| 423 } | 592 } |
| 424 } | 593 } |
| 425 | 594 |
| 426 while (cur < end) { | 595 while (cur < end) { |
| 427 dasm.InstructionDecode(buffer, cur); | 596 dasm.InstructionDecode(buffer, cur); |
| 428 PrintF(" 0x%08x %s\n", cur, buffer.start()); | 597 PrintF(" 0x%08x %s\n", |
| 429 cur += Instruction::kInstructionSize; | 598 reinterpret_cast<intptr_t>(cur), buffer.start()); |
| 599 cur += Instruction::kInstrSize; | |
| 430 } | 600 } |
| 431 } else if ((strcmp(cmd, "h") == 0) || (strcmp(cmd, "help") == 0)) { | 601 } else if ((strcmp(cmd, "h") == 0) || (strcmp(cmd, "help") == 0)) { |
| 432 PrintF("cont\n"); | 602 PrintF("cont\n"); |
| 433 PrintF(" continue execution (alias 'c')\n"); | 603 PrintF(" continue execution (alias 'c')\n"); |
| 434 PrintF("stepi\n"); | 604 PrintF("stepi\n"); |
| 435 PrintF(" step one instruction (alias 'si')\n"); | 605 PrintF(" step one instruction (alias 'si')\n"); |
| 436 PrintF("print <register>\n"); | 606 PrintF("print <register>\n"); |
| 437 PrintF(" print register content (alias 'p')\n"); | 607 PrintF(" print register content (alias 'p')\n"); |
| 438 PrintF(" use register name 'all' to print all registers\n"); | 608 PrintF(" use register name 'all' to print all registers\n"); |
| 439 PrintF("printobject <register>\n"); | 609 PrintF("printobject <register>\n"); |
| 440 PrintF(" print an object from a register (alias 'po')\n"); | 610 PrintF(" print an object from a register (alias 'po')\n"); |
| 611 PrintF("stack [<words>]\n"); | |
| 612 PrintF(" dump stack content, default dump 10 words)\n"); | |
| 613 PrintF("mem <address> [<words>]\n"); | |
| 614 PrintF(" dump memory content, default dump 10 words)\n"); | |
| 441 PrintF("flags\n"); | 615 PrintF("flags\n"); |
| 442 PrintF(" print flags\n"); | 616 PrintF(" print flags\n"); |
| 443 PrintF("disasm [<instructions>]\n"); | 617 PrintF("disasm [<instructions>]\n"); |
| 444 PrintF("disasm [[<address>] <instructions>]\n"); | 618 PrintF("disasm [[<address>] <instructions>]\n"); |
| 445 PrintF(" disassemble code, default is 10 instructions from pc\n"); | 619 PrintF(" disassemble code, default is 10 instructions from pc\n"); |
| 446 PrintF("gdb\n"); | 620 PrintF("gdb\n"); |
| 447 PrintF(" enter gdb\n"); | 621 PrintF(" enter gdb\n"); |
| 448 PrintF("break <address>\n"); | 622 PrintF("break <address>\n"); |
| 449 PrintF(" set a break point on the address\n"); | 623 PrintF(" set a break point on the address\n"); |
| 450 PrintF("del\n"); | 624 PrintF("del\n"); |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 464 RedoBreakpoints(); | 638 RedoBreakpoints(); |
| 465 | 639 |
| 466 #undef COMMAND_SIZE | 640 #undef COMMAND_SIZE |
| 467 #undef ARG_SIZE | 641 #undef ARG_SIZE |
| 468 | 642 |
| 469 #undef STR | 643 #undef STR |
| 470 #undef XSTR | 644 #undef XSTR |
| 471 } | 645 } |
| 472 | 646 |
| 473 | 647 |
| 474 // Create one simulator per thread and keep it in thread local storage. | 648 static bool ICacheMatch(void* one, void* two) { |
| 475 static v8::internal::Thread::LocalStorageKey simulator_key; | 649 ASSERT((reinterpret_cast<intptr_t>(one) & CachePage::kPageMask) == 0); |
| 650 ASSERT((reinterpret_cast<intptr_t>(two) & CachePage::kPageMask) == 0); | |
| 651 return one == two; | |
| 652 } | |
| 476 | 653 |
| 477 | 654 |
| 478 bool Simulator::initialized_ = false; | 655 static uint32_t ICacheHash(void* key) { |
| 656 return static_cast<uint32_t>(reinterpret_cast<uintptr_t>(key)) >> 2; | |
| 657 } | |
| 658 | |
| 659 | |
| 660 static bool AllOnOnePage(uintptr_t start, int size) { | |
| 661 intptr_t start_page = (start & ~CachePage::kPageMask); | |
| 662 intptr_t end_page = ((start + size) & ~CachePage::kPageMask); | |
| 663 return start_page == end_page; | |
| 664 } | |
| 665 | |
| 666 | |
| 667 void Simulator::FlushICache(v8::internal::HashMap* i_cache, | |
| 668 void* start_addr, | |
| 669 size_t size) { | |
| 670 intptr_t start = reinterpret_cast<intptr_t>(start_addr); | |
| 671 int intra_line = (start & CachePage::kLineMask); | |
| 672 start -= intra_line; | |
| 673 size += intra_line; | |
| 674 size = ((size - 1) | CachePage::kLineMask) + 1; | |
| 675 int offset = (start & CachePage::kPageMask); | |
| 676 while (!AllOnOnePage(start, size - 1)) { | |
| 677 int bytes_to_flush = CachePage::kPageSize - offset; | |
| 678 FlushOnePage(i_cache, start, bytes_to_flush); | |
| 679 start += bytes_to_flush; | |
| 680 size -= bytes_to_flush; | |
| 681 ASSERT_EQ(0, start & CachePage::kPageMask); | |
| 682 offset = 0; | |
| 683 } | |
| 684 if (size != 0) { | |
| 685 FlushOnePage(i_cache, start, size); | |
| 686 } | |
| 687 } | |
| 688 | |
| 689 | |
| 690 CachePage* Simulator::GetCachePage(v8::internal::HashMap* i_cache, void* page) { | |
| 691 v8::internal::HashMap::Entry* entry = i_cache->Lookup(page, | |
| 692 ICacheHash(page), | |
| 693 true); | |
| 694 if (entry->value == NULL) { | |
| 695 CachePage* new_page = new CachePage(); | |
| 696 entry->value = new_page; | |
| 697 } | |
| 698 return reinterpret_cast<CachePage*>(entry->value); | |
| 699 } | |
| 700 | |
| 701 | |
| 702 // Flush from start up to and not including start + size. | |
| 703 void Simulator::FlushOnePage(v8::internal::HashMap* i_cache, | |
| 704 intptr_t start, | |
| 705 int size) { | |
| 706 ASSERT(size <= CachePage::kPageSize); | |
| 707 ASSERT(AllOnOnePage(start, size - 1)); | |
| 708 ASSERT((start & CachePage::kLineMask) == 0); | |
| 709 ASSERT((size & CachePage::kLineMask) == 0); | |
| 710 void* page = reinterpret_cast<void*>(start & (~CachePage::kPageMask)); | |
| 711 int offset = (start & CachePage::kPageMask); | |
| 712 CachePage* cache_page = GetCachePage(i_cache, page); | |
| 713 char* valid_bytemap = cache_page->ValidityByte(offset); | |
| 714 memset(valid_bytemap, CachePage::LINE_INVALID, size >> CachePage::kLineShift); | |
| 715 } | |
| 716 | |
| 717 | |
| 718 void Simulator::CheckICache(v8::internal::HashMap* i_cache, | |
| 719 Instruction* instr) { | |
| 720 intptr_t address = reinterpret_cast<intptr_t>(instr); | |
| 721 void* page = reinterpret_cast<void*>(address & (~CachePage::kPageMask)); | |
| 722 void* line = reinterpret_cast<void*>(address & (~CachePage::kLineMask)); | |
| 723 int offset = (address & CachePage::kPageMask); | |
| 724 CachePage* cache_page = GetCachePage(i_cache, page); | |
| 725 char* cache_valid_byte = cache_page->ValidityByte(offset); | |
| 726 bool cache_hit = (*cache_valid_byte == CachePage::LINE_VALID); | |
| 727 char* cached_line = cache_page->CachedData(offset & ~CachePage::kLineMask); | |
| 728 if (cache_hit) { | |
| 729 // Check that the data in memory matches the contents of the I-cache. | |
| 730 CHECK(memcmp(reinterpret_cast<void*>(instr), | |
| 731 cache_page->CachedData(offset), | |
| 732 Instruction::kInstrSize) == 0); | |
| 733 } else { | |
| 734 // Cache miss. Load memory into the cache. | |
| 735 memcpy(cached_line, line, CachePage::kLineLength); | |
| 736 *cache_valid_byte = CachePage::LINE_VALID; | |
| 737 } | |
| 738 } | |
| 479 | 739 |
| 480 | 740 |
| 481 void Simulator::Initialize() { | 741 void Simulator::Initialize() { |
| 482 if (initialized_) return; | 742 if (Isolate::Current()->simulator_initialized()) return; |
| 483 simulator_key = v8::internal::Thread::CreateThreadLocalKey(); | 743 Isolate::Current()->set_simulator_initialized(true); |
| 484 initialized_ = true; | |
| 485 ::v8::internal::ExternalReference::set_redirector(&RedirectExternalReference); | 744 ::v8::internal::ExternalReference::set_redirector(&RedirectExternalReference); |
| 486 } | 745 } |
| 487 | 746 |
| 488 | 747 |
| 489 Simulator::Simulator() { | 748 Simulator::Simulator() : isolate_(Isolate::Current()) { |
| 749 i_cache_ = isolate_->simulator_i_cache(); | |
| 750 if (i_cache_ == NULL) { | |
| 751 i_cache_ = new v8::internal::HashMap(&ICacheMatch); | |
| 752 isolate_->set_simulator_i_cache(i_cache_); | |
| 753 } | |
| 490 Initialize(); | 754 Initialize(); |
| 491 // Setup simulator support first. Some of this information is needed to | 755 // Setup simulator support first. Some of this information is needed to |
| 492 // setup the architecture state. | 756 // setup the architecture state. |
| 493 size_t stack_size = 1 * 1024*1024; // allocate 1MB for stack | 757 stack_size_ = 1 * 1024*1024; // allocate 1MB for stack |
| 494 stack_ = reinterpret_cast<char*>(malloc(stack_size)); | 758 stack_ = reinterpret_cast<char*>(malloc(stack_size_)); |
| 495 pc_modified_ = false; | 759 pc_modified_ = false; |
| 496 icount_ = 0; | 760 icount_ = 0; |
| 761 break_count_ = 0; | |
| 497 break_pc_ = NULL; | 762 break_pc_ = NULL; |
| 498 break_instr_ = 0; | 763 break_instr_ = 0; |
| 499 | 764 |
| 500 // Setup architecture state. | 765 // Setup architecture state. |
| 501 // All registers are initialized to zero to start with. | 766 // All registers are initialized to zero to start with. |
| 502 for (int i = 0; i < kNumSimuRegisters; i++) { | 767 for (int i = 0; i < kNumSimuRegisters; i++) { |
| 503 registers_[i] = 0; | 768 registers_[i] = 0; |
| 504 } | 769 } |
| 770 for (int i = 0; i < kNumFPURegisters; i++) { | |
| 771 FPUregisters_[i] = 0; | |
| 772 } | |
| 773 FCSR_ = 0; | |
| 505 | 774 |
| 506 // The sp is initialized to point to the bottom (high address) of the | 775 // 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 | 776 // allocated stack area. To be safe in potential stack underflows we leave |
| 508 // some buffer below. | 777 // some buffer below. |
| 509 registers_[sp] = reinterpret_cast<int32_t>(stack_) + stack_size - 64; | 778 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 | 779 // 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. | 780 // access violation if the simulator ever tries to execute it. |
| 512 registers_[pc] = bad_ra; | 781 registers_[pc] = bad_ra; |
| 513 registers_[ra] = bad_ra; | 782 registers_[ra] = bad_ra; |
| 514 InitializeCoverage(); | 783 InitializeCoverage(); |
| 784 for (int i = 0; i < kNumExceptions; i++) { | |
| 785 exceptions[i] = 0; | |
| 786 } | |
| 515 } | 787 } |
| 516 | 788 |
| 517 | 789 |
| 518 // When the generated code calls an external reference we need to catch that in | 790 // 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 | 791 // 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 | 792 // 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 | 793 // execute it with the simulator. We do that by redirecting the external |
| 522 // reference to a swi (software-interrupt) instruction that is handled by | 794 // 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 | 795 // 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. | 796 // offset from the swi instruction so the simulator knows what to call. |
| 525 class Redirection { | 797 class Redirection { |
| 526 public: | 798 public: |
| 527 Redirection(void* external_function, bool fp_return) | 799 Redirection(void* external_function, ExternalReference::Type type) |
| 528 : external_function_(external_function), | 800 : external_function_(external_function), |
| 529 swi_instruction_(rtCallRedirInstr), | 801 swi_instruction_(rtCallRedirInstr), |
| 530 fp_return_(fp_return), | 802 type_(type), |
| 531 next_(list_) { | 803 next_(NULL) { |
| 532 list_ = this; | 804 Isolate* isolate = Isolate::Current(); |
| 805 next_ = isolate->simulator_redirection(); | |
| 806 Simulator::current(isolate)-> | |
| 807 FlushICache(isolate->simulator_i_cache(), | |
| 808 reinterpret_cast<void*>(&swi_instruction_), | |
| 809 Instruction::kInstrSize); | |
| 810 isolate->set_simulator_redirection(this); | |
| 533 } | 811 } |
| 534 | 812 |
| 535 void* address_of_swi_instruction() { | 813 void* address_of_swi_instruction() { |
| 536 return reinterpret_cast<void*>(&swi_instruction_); | 814 return reinterpret_cast<void*>(&swi_instruction_); |
| 537 } | 815 } |
| 538 | 816 |
| 539 void* external_function() { return external_function_; } | 817 void* external_function() { return external_function_; } |
| 540 bool fp_return() { return fp_return_; } | 818 ExternalReference::Type type() { return type_; } |
| 541 | 819 |
| 542 static Redirection* Get(void* external_function, bool fp_return) { | 820 static Redirection* Get(void* external_function, |
| 543 Redirection* current; | 821 ExternalReference::Type type) { |
| 544 for (current = list_; current != NULL; current = current->next_) { | 822 Isolate* isolate = Isolate::Current(); |
| 823 Redirection* current = isolate->simulator_redirection(); | |
| 824 for (; current != NULL; current = current->next_) { | |
| 545 if (current->external_function_ == external_function) return current; | 825 if (current->external_function_ == external_function) return current; |
| 546 } | 826 } |
| 547 return new Redirection(external_function, fp_return); | 827 return new Redirection(external_function, type); |
| 548 } | 828 } |
| 549 | 829 |
| 550 static Redirection* FromSwiInstruction(Instruction* swi_instruction) { | 830 static Redirection* FromSwiInstruction(Instruction* swi_instruction) { |
| 551 char* addr_of_swi = reinterpret_cast<char*>(swi_instruction); | 831 char* addr_of_swi = reinterpret_cast<char*>(swi_instruction); |
| 552 char* addr_of_redirection = | 832 char* addr_of_redirection = |
| 553 addr_of_swi - OFFSET_OF(Redirection, swi_instruction_); | 833 addr_of_swi - OFFSET_OF(Redirection, swi_instruction_); |
| 554 return reinterpret_cast<Redirection*>(addr_of_redirection); | 834 return reinterpret_cast<Redirection*>(addr_of_redirection); |
| 555 } | 835 } |
| 556 | 836 |
| 557 private: | 837 private: |
| 558 void* external_function_; | 838 void* external_function_; |
| 559 uint32_t swi_instruction_; | 839 uint32_t swi_instruction_; |
| 560 bool fp_return_; | 840 ExternalReference::Type type_; |
| 561 Redirection* next_; | 841 Redirection* next_; |
| 562 static Redirection* list_; | |
| 563 }; | 842 }; |
| 564 | 843 |
| 565 | 844 |
| 566 Redirection* Redirection::list_ = NULL; | |
| 567 | |
| 568 | |
| 569 void* Simulator::RedirectExternalReference(void* external_function, | 845 void* Simulator::RedirectExternalReference(void* external_function, |
| 570 bool fp_return) { | 846 ExternalReference::Type type) { |
| 571 Redirection* redirection = Redirection::Get(external_function, fp_return); | 847 Redirection* redirection = Redirection::Get(external_function, type); |
| 572 return redirection->address_of_swi_instruction(); | 848 return redirection->address_of_swi_instruction(); |
| 573 } | 849 } |
| 574 | 850 |
| 575 | 851 |
| 576 // Get the active Simulator for the current thread. | 852 // Get the active Simulator for the current thread. |
| 577 Simulator* Simulator::current() { | 853 Simulator* Simulator::current(Isolate* isolate) { |
| 578 Initialize(); | 854 v8::internal::Isolate::PerIsolateThreadData* isolate_data = |
| 579 Simulator* sim = reinterpret_cast<Simulator*>( | 855 Isolate::CurrentPerIsolateThreadData(); |
| 580 v8::internal::Thread::GetThreadLocal(simulator_key)); | 856 if (isolate_data == NULL) { |
| 857 Isolate::EnterDefaultIsolate(); | |
| 858 isolate_data = Isolate::CurrentPerIsolateThreadData(); | |
| 859 } | |
| 860 ASSERT(isolate_data != NULL); | |
| 861 | |
| 862 Simulator* sim = isolate_data->simulator(); | |
| 581 if (sim == NULL) { | 863 if (sim == NULL) { |
| 582 // TODO(146): delete the simulator object when a thread goes away. | 864 // TODO(146): delete the simulator object when a thread/isolate goes away. |
| 583 sim = new Simulator(); | 865 sim = new Simulator(); |
| 584 v8::internal::Thread::SetThreadLocal(simulator_key, sim); | 866 isolate_data->set_simulator(sim); |
| 585 } | 867 } |
| 586 return sim; | 868 return sim; |
| 587 } | 869 } |
| 588 | 870 |
| 589 | 871 |
| 590 // Sets the register in the architecture state. It will also deal with updating | 872 // Sets the register in the architecture state. It will also deal with updating |
| 591 // Simulator internal state for special registers such as PC. | 873 // Simulator internal state for special registers such as PC. |
| 592 void Simulator::set_register(int reg, int32_t value) { | 874 void Simulator::set_register(int reg, int32_t value) { |
| 593 ASSERT((reg >= 0) && (reg < kNumSimuRegisters)); | 875 ASSERT((reg >= 0) && (reg < kNumSimuRegisters)); |
| 594 if (reg == pc) { | 876 if (reg == pc) { |
| 595 pc_modified_ = true; | 877 pc_modified_ = true; |
| 596 } | 878 } |
| 597 | 879 |
| 598 // zero register always hold 0. | 880 // zero register always hold 0. |
| 599 registers_[reg] = (reg == 0) ? 0 : value; | 881 registers_[reg] = (reg == 0) ? 0 : value; |
| 600 } | 882 } |
| 601 | 883 |
| 884 | |
| 602 void Simulator::set_fpu_register(int fpureg, int32_t value) { | 885 void Simulator::set_fpu_register(int fpureg, int32_t value) { |
| 603 ASSERT((fpureg >= 0) && (fpureg < kNumFPURegisters)); | 886 ASSERT((fpureg >= 0) && (fpureg < kNumFPURegisters)); |
| 604 FPUregisters_[fpureg] = value; | 887 FPUregisters_[fpureg] = value; |
| 605 } | 888 } |
| 606 | 889 |
| 607 void Simulator::set_fpu_register_double(int fpureg, double value) { | 890 |
| 608 ASSERT((fpureg >= 0) && (fpureg < kNumFPURegisters) && ((fpureg % 2) == 0)); | 891 void Simulator::set_fpu_register_float(int fpureg, float value) { |
| 609 *v8i::BitCast<double*>(&FPUregisters_[fpureg]) = value; | 892 ASSERT((fpureg >= 0) && (fpureg < kNumFPURegisters)); |
| 893 *BitCast<float*>(&FPUregisters_[fpureg]) = value; | |
| 610 } | 894 } |
| 611 | 895 |
| 612 | 896 |
| 897 void Simulator::set_fpu_register_double(int fpureg, double value) { | |
| 898 ASSERT((fpureg >= 0) && (fpureg < kNumFPURegisters) && ((fpureg % 2) == 0)); | |
| 899 *BitCast<double*>(&FPUregisters_[fpureg]) = value; | |
| 900 } | |
| 901 | |
| 902 | |
| 613 // Get the register from the architecture state. This function does handle | 903 // Get the register from the architecture state. This function does handle |
| 614 // the special case of accessing the PC register. | 904 // the special case of accessing the PC register. |
| 615 int32_t Simulator::get_register(int reg) const { | 905 int32_t Simulator::get_register(int reg) const { |
| 616 ASSERT((reg >= 0) && (reg < kNumSimuRegisters)); | 906 ASSERT((reg >= 0) && (reg < kNumSimuRegisters)); |
| 617 if (reg == 0) | 907 if (reg == 0) |
| 618 return 0; | 908 return 0; |
| 619 else | 909 else |
| 620 return registers_[reg] + ((reg == pc) ? Instruction::kPCReadOffset : 0); | 910 return registers_[reg] + ((reg == pc) ? Instruction::kPCReadOffset : 0); |
| 621 } | 911 } |
| 622 | 912 |
| 913 | |
| 623 int32_t Simulator::get_fpu_register(int fpureg) const { | 914 int32_t Simulator::get_fpu_register(int fpureg) const { |
| 624 ASSERT((fpureg >= 0) && (fpureg < kNumFPURegisters)); | 915 ASSERT((fpureg >= 0) && (fpureg < kNumFPURegisters)); |
| 625 return FPUregisters_[fpureg]; | 916 return FPUregisters_[fpureg]; |
| 626 } | 917 } |
| 627 | 918 |
| 919 | |
| 920 int64_t Simulator::get_fpu_register_long(int fpureg) const { | |
| 921 ASSERT((fpureg >= 0) && (fpureg < kNumFPURegisters) && ((fpureg % 2) == 0)); | |
| 922 return *BitCast<int64_t*>( | |
| 923 const_cast<int32_t*>(&FPUregisters_[fpureg])); | |
| 924 } | |
| 925 | |
| 926 | |
| 927 float Simulator::get_fpu_register_float(int fpureg) const { | |
| 928 ASSERT((fpureg >= 0) && (fpureg < kNumFPURegisters)); | |
| 929 return *BitCast<float*>( | |
| 930 const_cast<int32_t*>(&FPUregisters_[fpureg])); | |
| 931 } | |
| 932 | |
| 933 | |
| 628 double Simulator::get_fpu_register_double(int fpureg) const { | 934 double Simulator::get_fpu_register_double(int fpureg) const { |
| 629 ASSERT((fpureg >= 0) && (fpureg < kNumFPURegisters) && ((fpureg % 2) == 0)); | 935 ASSERT((fpureg >= 0) && (fpureg < kNumFPURegisters) && ((fpureg % 2) == 0)); |
| 630 return *v8i::BitCast<double*>(const_cast<int32_t*>(&FPUregisters_[fpureg])); | 936 return *BitCast<double*>(const_cast<int32_t*>(&FPUregisters_[fpureg])); |
| 631 } | 937 } |
| 632 | 938 |
| 939 | |
| 940 // Helper functions for setting and testing the FCSR register's bits. | |
| 941 void Simulator::set_fcsr_bit(uint32_t cc, bool value) { | |
| 942 if (value) { | |
| 943 FCSR_ |= (1 << cc); | |
| 944 } else { | |
| 945 FCSR_ &= ~(1 << cc); | |
| 946 } | |
| 947 } | |
| 948 | |
| 949 | |
| 950 bool Simulator::test_fcsr_bit(uint32_t cc) { | |
| 951 return FCSR_ & (1 << cc); | |
| 952 } | |
| 953 | |
| 954 | |
| 955 // Sets the rounding error codes in FCSR based on the result of the rounding. | |
| 956 // Returns true if the operation was invalid. | |
| 957 bool Simulator::set_fcsr_round_error(double original, double rounded) { | |
| 958 if (!isfinite(original) || | |
| 959 rounded > LONG_MAX || | |
| 960 rounded < LONG_MIN) { | |
| 961 set_fcsr_bit(6, true); // Invalid operation. | |
| 962 return true; | |
| 963 } else if (original != static_cast<double>(rounded)) { | |
| 964 set_fcsr_bit(2, true); // Inexact. | |
| 965 } | |
| 966 return false; | |
| 967 } | |
| 968 | |
| 969 | |
| 633 // Raw access to the PC register. | 970 // Raw access to the PC register. |
| 634 void Simulator::set_pc(int32_t value) { | 971 void Simulator::set_pc(int32_t value) { |
| 635 pc_modified_ = true; | 972 pc_modified_ = true; |
| 636 registers_[pc] = value; | 973 registers_[pc] = value; |
| 637 } | 974 } |
| 638 | 975 |
| 976 | |
| 977 bool Simulator::has_bad_pc() const { | |
| 978 return ((registers_[pc] == bad_ra) || (registers_[pc] == end_sim_pc)); | |
| 979 } | |
| 980 | |
| 981 | |
| 639 // Raw access to the PC register without the special adjustment when reading. | 982 // Raw access to the PC register without the special adjustment when reading. |
| 640 int32_t Simulator::get_pc() const { | 983 int32_t Simulator::get_pc() const { |
| 641 return registers_[pc]; | 984 return registers_[pc]; |
| 642 } | 985 } |
| 643 | 986 |
| 644 | 987 |
| 645 // The MIPS cannot do unaligned reads and writes. On some MIPS platforms an | 988 // The MIPS cannot do unaligned reads and writes. On some MIPS platforms an |
| 646 // interrupt is caused. On others it does a funky rotation thing. For now we | 989 // interrupt is caused. On others it does a funky rotation thing. For now we |
| 647 // simply disallow unaligned reads, but at some point we may want to move to | 990 // simply disallow unaligned reads, but at some point we may want to move to |
| 648 // emulating the rotate behaviour. Note that simulator runs have the runtime | 991 // emulating the rotate behaviour. Note that simulator runs have the runtime |
| 649 // system running directly on the host system and only generated code is | 992 // system running directly on the host system and only generated code is |
| 650 // executed in the simulator. Since the host is typically IA32 we will not | 993 // executed in the simulator. Since the host is typically IA32 we will not |
| 651 // get the correct MIPS-like behaviour on unaligned accesses. | 994 // get the correct MIPS-like behaviour on unaligned accesses. |
| 652 | 995 |
| 653 int Simulator::ReadW(int32_t addr, Instruction* instr) { | 996 int Simulator::ReadW(int32_t addr, Instruction* instr) { |
| 654 if ((addr & v8i::kPointerAlignmentMask) == 0) { | 997 if (addr >=0 && addr < 0x400) { |
| 998 // this has to be a NULL-dereference | |
| 999 MipsDebugger dbg(this); | |
| 1000 dbg.Debug(); | |
| 1001 } | |
| 1002 if ((addr & kPointerAlignmentMask) == 0) { | |
| 655 intptr_t* ptr = reinterpret_cast<intptr_t*>(addr); | 1003 intptr_t* ptr = reinterpret_cast<intptr_t*>(addr); |
| 656 return *ptr; | 1004 return *ptr; |
| 657 } | 1005 } |
| 658 PrintF("Unaligned read at 0x%08x, pc=%p\n", addr, instr); | 1006 PrintF("Unaligned read at 0x%08x, pc=%p\n", addr, |
| 659 OS::Abort(); | 1007 reinterpret_cast<void*>(instr)); |
| 1008 MipsDebugger dbg(this); | |
| 1009 dbg.Debug(); | |
| 660 return 0; | 1010 return 0; |
| 661 } | 1011 } |
| 662 | 1012 |
| 663 | 1013 |
| 664 void Simulator::WriteW(int32_t addr, int value, Instruction* instr) { | 1014 void Simulator::WriteW(int32_t addr, int value, Instruction* instr) { |
| 665 if ((addr & v8i::kPointerAlignmentMask) == 0) { | 1015 if (addr >= 0 && addr < 0x400) { |
| 1016 // this has to be a NULL-dereference | |
| 1017 MipsDebugger dbg(this); | |
| 1018 dbg.Debug(); | |
| 1019 } | |
| 1020 if ((addr & kPointerAlignmentMask) == 0) { | |
| 666 intptr_t* ptr = reinterpret_cast<intptr_t*>(addr); | 1021 intptr_t* ptr = reinterpret_cast<intptr_t*>(addr); |
| 667 *ptr = value; | 1022 *ptr = value; |
| 668 return; | 1023 return; |
| 669 } | 1024 } |
| 670 PrintF("Unaligned write at 0x%08x, pc=%p\n", addr, instr); | 1025 PrintF("Unaligned write at 0x%08x, pc=%p\n", addr, |
| 671 OS::Abort(); | 1026 reinterpret_cast<void*>(instr)); |
| 1027 MipsDebugger dbg(this); | |
| 1028 dbg.Debug(); | |
| 672 } | 1029 } |
| 673 | 1030 |
| 674 | 1031 |
| 675 double Simulator::ReadD(int32_t addr, Instruction* instr) { | 1032 double Simulator::ReadD(int32_t addr, Instruction* instr) { |
| 676 if ((addr & kDoubleAlignmentMask) == 0) { | 1033 if ((addr & kDoubleAlignmentMask) == 0) { |
| 677 double* ptr = reinterpret_cast<double*>(addr); | 1034 double* ptr = reinterpret_cast<double*>(addr); |
| 678 return *ptr; | 1035 return *ptr; |
| 679 } | 1036 } |
| 680 PrintF("Unaligned read at 0x%08x, pc=%p\n", addr, instr); | 1037 PrintF("Unaligned (double) read at 0x%08x, pc=%p\n", addr, |
| 1038 reinterpret_cast<void*>(instr)); | |
| 681 OS::Abort(); | 1039 OS::Abort(); |
| 682 return 0; | 1040 return 0; |
| 683 } | 1041 } |
| 684 | 1042 |
| 685 | 1043 |
| 686 void Simulator::WriteD(int32_t addr, double value, Instruction* instr) { | 1044 void Simulator::WriteD(int32_t addr, double value, Instruction* instr) { |
| 687 if ((addr & kDoubleAlignmentMask) == 0) { | 1045 if ((addr & kDoubleAlignmentMask) == 0) { |
| 688 double* ptr = reinterpret_cast<double*>(addr); | 1046 double* ptr = reinterpret_cast<double*>(addr); |
| 689 *ptr = value; | 1047 *ptr = value; |
| 690 return; | 1048 return; |
| 691 } | 1049 } |
| 692 PrintF("Unaligned write at 0x%08x, pc=%p\n", addr, instr); | 1050 PrintF("Unaligned (double) write at 0x%08x, pc=%p\n", addr, |
| 1051 reinterpret_cast<void*>(instr)); | |
| 693 OS::Abort(); | 1052 OS::Abort(); |
| 694 } | 1053 } |
| 695 | 1054 |
| 696 | 1055 |
| 697 uint16_t Simulator::ReadHU(int32_t addr, Instruction* instr) { | 1056 uint16_t Simulator::ReadHU(int32_t addr, Instruction* instr) { |
| 698 if ((addr & 1) == 0) { | 1057 if ((addr & 1) == 0) { |
| 699 uint16_t* ptr = reinterpret_cast<uint16_t*>(addr); | 1058 uint16_t* ptr = reinterpret_cast<uint16_t*>(addr); |
| 700 return *ptr; | 1059 return *ptr; |
| 701 } | 1060 } |
| 702 PrintF("Unaligned unsigned halfword read at 0x%08x, pc=%p\n", addr, instr); | 1061 PrintF("Unaligned unsigned halfword read at 0x%08x, pc=%p\n", addr, |
| 1062 reinterpret_cast<void*>(instr)); | |
| 703 OS::Abort(); | 1063 OS::Abort(); |
| 704 return 0; | 1064 return 0; |
| 705 } | 1065 } |
| 706 | 1066 |
| 707 | 1067 |
| 708 int16_t Simulator::ReadH(int32_t addr, Instruction* instr) { | 1068 int16_t Simulator::ReadH(int32_t addr, Instruction* instr) { |
| 709 if ((addr & 1) == 0) { | 1069 if ((addr & 1) == 0) { |
| 710 int16_t* ptr = reinterpret_cast<int16_t*>(addr); | 1070 int16_t* ptr = reinterpret_cast<int16_t*>(addr); |
| 711 return *ptr; | 1071 return *ptr; |
| 712 } | 1072 } |
| 713 PrintF("Unaligned signed halfword read at 0x%08x, pc=%p\n", addr, instr); | 1073 PrintF("Unaligned signed halfword read at 0x%08x, pc=%p\n", addr, |
| 1074 reinterpret_cast<void*>(instr)); | |
| 714 OS::Abort(); | 1075 OS::Abort(); |
| 715 return 0; | 1076 return 0; |
| 716 } | 1077 } |
| 717 | 1078 |
| 718 | 1079 |
| 719 void Simulator::WriteH(int32_t addr, uint16_t value, Instruction* instr) { | 1080 void Simulator::WriteH(int32_t addr, uint16_t value, Instruction* instr) { |
| 720 if ((addr & 1) == 0) { | 1081 if ((addr & 1) == 0) { |
| 721 uint16_t* ptr = reinterpret_cast<uint16_t*>(addr); | 1082 uint16_t* ptr = reinterpret_cast<uint16_t*>(addr); |
| 722 *ptr = value; | 1083 *ptr = value; |
| 723 return; | 1084 return; |
| 724 } | 1085 } |
| 725 PrintF("Unaligned unsigned halfword write at 0x%08x, pc=%p\n", addr, instr); | 1086 PrintF("Unaligned unsigned halfword write at 0x%08x, pc=%p\n", addr, |
| 1087 reinterpret_cast<void*>(instr)); | |
| 726 OS::Abort(); | 1088 OS::Abort(); |
| 727 } | 1089 } |
| 728 | 1090 |
| 729 | 1091 |
| 730 void Simulator::WriteH(int32_t addr, int16_t value, Instruction* instr) { | 1092 void Simulator::WriteH(int32_t addr, int16_t value, Instruction* instr) { |
| 731 if ((addr & 1) == 0) { | 1093 if ((addr & 1) == 0) { |
| 732 int16_t* ptr = reinterpret_cast<int16_t*>(addr); | 1094 int16_t* ptr = reinterpret_cast<int16_t*>(addr); |
| 733 *ptr = value; | 1095 *ptr = value; |
| 734 return; | 1096 return; |
| 735 } | 1097 } |
| 736 PrintF("Unaligned halfword write at 0x%08x, pc=%p\n", addr, instr); | 1098 PrintF("Unaligned halfword write at 0x%08x, pc=%p\n", addr, |
| 1099 reinterpret_cast<void*>(instr)); | |
| 737 OS::Abort(); | 1100 OS::Abort(); |
| 738 } | 1101 } |
| 739 | 1102 |
| 740 | 1103 |
| 741 uint32_t Simulator::ReadBU(int32_t addr) { | 1104 uint32_t Simulator::ReadBU(int32_t addr) { |
| 742 uint8_t* ptr = reinterpret_cast<uint8_t*>(addr); | 1105 uint8_t* ptr = reinterpret_cast<uint8_t*>(addr); |
| 743 return *ptr & 0xff; | 1106 return *ptr & 0xff; |
| 744 } | 1107 } |
| 745 | 1108 |
| 746 | 1109 |
| 747 int32_t Simulator::ReadB(int32_t addr) { | 1110 int32_t Simulator::ReadB(int32_t addr) { |
| 748 int8_t* ptr = reinterpret_cast<int8_t*>(addr); | 1111 int8_t* ptr = reinterpret_cast<int8_t*>(addr); |
| 749 return ((*ptr << 24) >> 24) & 0xff; | 1112 return *ptr; |
| 750 } | 1113 } |
| 751 | 1114 |
| 752 | 1115 |
| 753 void Simulator::WriteB(int32_t addr, uint8_t value) { | 1116 void Simulator::WriteB(int32_t addr, uint8_t value) { |
| 754 uint8_t* ptr = reinterpret_cast<uint8_t*>(addr); | 1117 uint8_t* ptr = reinterpret_cast<uint8_t*>(addr); |
| 755 *ptr = value; | 1118 *ptr = value; |
| 756 } | 1119 } |
| 757 | 1120 |
| 758 | 1121 |
| 759 void Simulator::WriteB(int32_t addr, int8_t value) { | 1122 void Simulator::WriteB(int32_t addr, int8_t value) { |
| 760 int8_t* ptr = reinterpret_cast<int8_t*>(addr); | 1123 int8_t* ptr = reinterpret_cast<int8_t*>(addr); |
| 761 *ptr = value; | 1124 *ptr = value; |
| 762 } | 1125 } |
| 763 | 1126 |
| 764 | 1127 |
| 765 // Returns the limit of the stack area to enable checking for stack overflows. | 1128 // Returns the limit of the stack area to enable checking for stack overflows. |
| 766 uintptr_t Simulator::StackLimit() const { | 1129 uintptr_t Simulator::StackLimit() const { |
| 767 // Leave a safety margin of 256 bytes to prevent overrunning the stack when | 1130 // Leave a safety margin of 256 bytes to prevent overrunning the stack when |
| 768 // pushing values. | 1131 // pushing values. |
| 769 return reinterpret_cast<uintptr_t>(stack_) + 256; | 1132 return reinterpret_cast<uintptr_t>(stack_) + 256; |
| 770 } | 1133 } |
| 771 | 1134 |
| 772 | 1135 |
| 773 // Unsupported instructions use Format to print an error and stop execution. | 1136 // Unsupported instructions use Format to print an error and stop execution. |
| 774 void Simulator::Format(Instruction* instr, const char* format) { | 1137 void Simulator::Format(Instruction* instr, const char* format) { |
| 775 PrintF("Simulator found unsupported instruction:\n 0x%08x: %s\n", | 1138 PrintF("Simulator found unsupported instruction:\n 0x%08x: %s\n", |
| 776 instr, format); | 1139 reinterpret_cast<intptr_t>(instr), format); |
| 777 UNIMPLEMENTED_MIPS(); | 1140 UNIMPLEMENTED_MIPS(); |
| 778 } | 1141 } |
| 779 | 1142 |
| 780 | 1143 |
| 781 // Calls into the V8 runtime are based on this very simple interface. | 1144 // Calls into the V8 runtime are based on this very simple interface. |
| 782 // Note: To be able to return two values from some calls the code in runtime.cc | 1145 // Note: To be able to return two values from some calls the code in runtime.cc |
| 783 // uses the ObjectPair which is essentially two 32-bit values stuffed into a | 1146 // uses the ObjectPair which is essentially two 32-bit values stuffed into a |
| 784 // 64-bit value. With the code below we assume that all runtime calls return | 1147 // 64-bit value. With the code below we assume that all runtime calls return |
| 785 // 64 bits of result. If they don't, the r1 result register contains a bogus | 1148 // 64 bits of result. If they don't, the v1 result register contains a bogus |
| 786 // value, which is fine because it is caller-saved. | 1149 // value, which is fine because it is caller-saved. |
| 787 typedef int64_t (*SimulatorRuntimeCall)(int32_t arg0, | 1150 typedef int64_t (*SimulatorRuntimeCall)(int32_t arg0, |
| 788 int32_t arg1, | 1151 int32_t arg1, |
| 789 int32_t arg2, | 1152 int32_t arg2, |
| 790 int32_t arg3); | 1153 int32_t arg3, |
| 791 typedef double (*SimulatorRuntimeFPCall)(double fparg0, | 1154 int32_t arg4, |
| 792 double fparg1); | 1155 int32_t arg5); |
| 793 | 1156 typedef double (*SimulatorRuntimeFPCall)(int32_t arg0, |
| 1157 int32_t arg1, | |
| 1158 int32_t arg2, | |
| 1159 int32_t arg3); | |
| 794 | 1160 |
| 795 // Software interrupt instructions are used by the simulator to call into the | 1161 // Software interrupt instructions are used by the simulator to call into the |
| 796 // C-based V8 runtime. | 1162 // C-based V8 runtime. They are also used for debugging with simulator. |
| 797 void Simulator::SoftwareInterrupt(Instruction* instr) { | 1163 void Simulator::SoftwareInterrupt(Instruction* instr) { |
| 1164 // There are several instructions that could get us here, | |
| 1165 // the break_ instruction, or several variants of traps. All | |
| 1166 // Are "SPECIAL" class opcode, and are distinuished by function. | |
| 1167 int32_t func = instr->FunctionFieldRaw(); | |
| 1168 int32_t code = (func == BREAK) ? instr->Bits(25, 6) : -1; | |
| 1169 | |
| 798 // We first check if we met a call_rt_redirected. | 1170 // We first check if we met a call_rt_redirected. |
| 799 if (instr->InstructionBits() == rtCallRedirInstr) { | 1171 if (instr->InstructionBits() == rtCallRedirInstr) { |
| 1172 // Check if stack is aligned. Error if not aligned is reported below to | |
| 1173 // include information on the function called. | |
| 1174 bool stack_aligned = | |
| 1175 (get_register(sp) | |
| 1176 & (::v8::internal::FLAG_sim_stack_alignment - 1)) == 0; | |
| 800 Redirection* redirection = Redirection::FromSwiInstruction(instr); | 1177 Redirection* redirection = Redirection::FromSwiInstruction(instr); |
| 801 int32_t arg0 = get_register(a0); | 1178 int32_t arg0 = get_register(a0); |
| 802 int32_t arg1 = get_register(a1); | 1179 int32_t arg1 = get_register(a1); |
| 803 int32_t arg2 = get_register(a2); | 1180 int32_t arg2 = get_register(a2); |
| 804 int32_t arg3 = get_register(a3); | 1181 int32_t arg3 = get_register(a3); |
| 805 // fp args are (not always) in f12 and f14. | 1182 int32_t arg4 = 0; |
| 806 // See MIPS conventions for more details. | 1183 int32_t arg5 = 0; |
| 807 double fparg0 = get_fpu_register_double(f12); | 1184 |
| 808 double fparg1 = get_fpu_register_double(f14); | 1185 // Need to check if sp is valid before assigning arg4, arg5. |
| 1186 // This is a fix for cctest test-api/CatchStackOverflow which causes | |
| 1187 // the stack to overflow. For some reason arm doesn't need this | |
| 1188 // stack check here. | |
| 1189 int32_t* stack_pointer = reinterpret_cast<int32_t*>(get_register(sp)); | |
| 1190 int32_t* stack = reinterpret_cast<int32_t*>(stack_); | |
| 1191 if (stack_pointer >= stack && stack_pointer < stack + stack_size_) { | |
| 1192 arg4 = stack_pointer[0]; | |
| 1193 arg5 = stack_pointer[1]; | |
| 1194 } | |
| 809 // This is dodgy but it works because the C entry stubs are never moved. | 1195 // This is dodgy but it works because the C entry stubs are never moved. |
| 810 // See comment in codegen-arm.cc and bug 1242173. | 1196 // See comment in codegen-arm.cc and bug 1242173. |
| 811 int32_t saved_ra = get_register(ra); | 1197 int32_t saved_ra = get_register(ra); |
| 812 if (redirection->fp_return()) { | 1198 |
| 813 intptr_t external = | 1199 intptr_t external = |
| 814 reinterpret_cast<intptr_t>(redirection->external_function()); | 1200 reinterpret_cast<int32_t>(redirection->external_function()); |
| 1201 | |
| 1202 // Based on CpuFeatures::IsSupported(FPU), Mips will use either hardware | |
| 1203 // FPU, or gcc soft-float routines. Hardware FPU is simulated in this | |
| 1204 // simulator. Soft-float has additional abstraction of ExternalReference, | |
| 1205 // to support serialization. Finally, when simulated on x86 host, the | |
| 1206 // x86 softfloat routines are used, and this Redirection infrastructure | |
| 1207 // lets simulated-mips make calls into x86 C code. | |
| 1208 // When doing that, the 'double' return type must be handled differently | |
| 1209 // than the usual int64_t return. The data is returned in different | |
| 1210 // registers and cannot be cast from one type to the other. However, the | |
| 1211 // calling arguments are passed the same way in both cases. | |
| 1212 if (redirection->type() == ExternalReference::FP_RETURN_CALL) { | |
| 815 SimulatorRuntimeFPCall target = | 1213 SimulatorRuntimeFPCall target = |
| 816 reinterpret_cast<SimulatorRuntimeFPCall>(external); | 1214 reinterpret_cast<SimulatorRuntimeFPCall>(external); |
| 817 if (::v8::internal::FLAG_trace_sim) { | 1215 if (::v8::internal::FLAG_trace_sim || !stack_aligned) { |
| 818 PrintF("Call to host function at %p with args %f, %f\n", | 1216 PrintF("Call to host function at %p with args %08x:%08x %08x:%08x", |
| 819 FUNCTION_ADDR(target), fparg0, fparg1); | 1217 FUNCTION_ADDR(target), arg0, arg1, arg2, arg3); |
| 1218 if (!stack_aligned) { | |
| 1219 PrintF(" with unaligned stack %08x\n", get_register(sp)); | |
| 1220 } | |
| 1221 PrintF("\n"); | |
| 820 } | 1222 } |
| 821 double result = target(fparg0, fparg1); | 1223 double result = target(arg0, arg1, arg2, arg3); |
| 822 set_fpu_register_double(f0, result); | 1224 // fp result -> registers v0 and v1. |
| 1225 int32_t gpreg_pair[2]; | |
| 1226 memcpy(&gpreg_pair[0], &result, 2 * sizeof(int32_t)); | |
| 1227 set_register(v0, gpreg_pair[0]); | |
| 1228 set_register(v1, gpreg_pair[1]); | |
| 1229 } else if (redirection->type() == ExternalReference::DIRECT_API_CALL) { | |
| 1230 PrintF("Mips does not yet support ExternalReference::DIRECT_API_CALL\n"); | |
| 1231 ASSERT(redirection->type() != ExternalReference::DIRECT_API_CALL); | |
| 1232 } else if (redirection->type() == ExternalReference::DIRECT_GETTER_CALL) { | |
| 1233 PrintF("Mips does not support ExternalReference::DIRECT_GETTER_CALL\n"); | |
| 1234 ASSERT(redirection->type() != ExternalReference::DIRECT_GETTER_CALL); | |
| 823 } else { | 1235 } else { |
| 824 intptr_t external = | 1236 // Builtin call. |
| 825 reinterpret_cast<int32_t>(redirection->external_function()); | 1237 ASSERT(redirection->type() == ExternalReference::BUILTIN_CALL); |
| 826 SimulatorRuntimeCall target = | 1238 SimulatorRuntimeCall target = |
| 827 reinterpret_cast<SimulatorRuntimeCall>(external); | 1239 reinterpret_cast<SimulatorRuntimeCall>(external); |
| 828 if (::v8::internal::FLAG_trace_sim) { | 1240 if (::v8::internal::FLAG_trace_sim || !stack_aligned) { |
| 829 PrintF( | 1241 PrintF( |
| 830 "Call to host function at %p with args %08x, %08x, %08x, %08x\n", | 1242 "Call to host function at %p: %08x, %08x, %08x, %08x, %08x, %08x", |
| 831 FUNCTION_ADDR(target), | 1243 FUNCTION_ADDR(target), |
| 832 arg0, | 1244 arg0, |
| 833 arg1, | 1245 arg1, |
| 834 arg2, | 1246 arg2, |
| 835 arg3); | 1247 arg3, |
| 1248 arg4, | |
| 1249 arg5); | |
| 1250 if (!stack_aligned) { | |
| 1251 PrintF(" with unaligned stack %08x\n", get_register(sp)); | |
| 1252 } | |
| 1253 PrintF("\n"); | |
| 836 } | 1254 } |
| 837 int64_t result = target(arg0, arg1, arg2, arg3); | 1255 |
| 838 int32_t lo_res = static_cast<int32_t>(result); | 1256 int64_t result = target(arg0, arg1, arg2, arg3, arg4, arg5); |
| 839 int32_t hi_res = static_cast<int32_t>(result >> 32); | 1257 set_register(v0, static_cast<int32_t>(result)); |
| 840 if (::v8::internal::FLAG_trace_sim) { | 1258 set_register(v1, static_cast<int32_t>(result >> 32)); |
| 841 PrintF("Returned %08x\n", lo_res); | 1259 } |
| 842 } | 1260 if (::v8::internal::FLAG_trace_sim) { |
| 843 set_register(v0, lo_res); | 1261 PrintF("Returned %08x : %08x\n", get_register(v1), get_register(v0)); |
| 844 set_register(v1, hi_res); | |
| 845 } | 1262 } |
| 846 set_register(ra, saved_ra); | 1263 set_register(ra, saved_ra); |
| 847 set_pc(get_register(ra)); | 1264 set_pc(get_register(ra)); |
| 1265 | |
| 1266 } else if (func == BREAK && code >= 0 && code < 16) { | |
| 1267 // First 16 break_ codes interpreted as debug markers. | |
| 1268 MipsDebugger dbg(this); | |
| 1269 ++break_count_; | |
| 1270 PrintF("\n---- break %d marker: %3d (instr count: %8d) ----------" | |
| 1271 "----------------------------------", | |
| 1272 code, break_count_, icount_); | |
| 1273 dbg.PrintAllRegs(); // Print registers and continue running. | |
| 848 } else { | 1274 } else { |
| 849 Debugger dbg(this); | 1275 // All remaining break_ codes, and all traps are handled here. |
| 1276 MipsDebugger dbg(this); | |
| 850 dbg.Debug(); | 1277 dbg.Debug(); |
| 851 } | 1278 } |
| 852 } | 1279 } |
| 853 | 1280 |
| 1281 | |
| 854 void Simulator::SignalExceptions() { | 1282 void Simulator::SignalExceptions() { |
| 855 for (int i = 1; i < kNumExceptions; i++) { | 1283 for (int i = 1; i < kNumExceptions; i++) { |
| 856 if (exceptions[i] != 0) { | 1284 if (exceptions[i] != 0) { |
| 857 V8_Fatal(__FILE__, __LINE__, "Error: Exception %i raised.", i); | 1285 V8_Fatal(__FILE__, __LINE__, "Error: Exception %i raised.", i); |
| 858 } | 1286 } |
| 859 } | 1287 } |
| 860 } | 1288 } |
| 861 | 1289 |
| 1290 | |
| 862 // Handle execution based on instruction types. | 1291 // Handle execution based on instruction types. |
| 863 void Simulator::DecodeTypeRegister(Instruction* instr) { | |
| 864 // Instruction fields | |
| 865 Opcode op = instr->OpcodeFieldRaw(); | |
| 866 int32_t rs_reg = instr->RsField(); | |
| 867 int32_t rs = get_register(rs_reg); | |
| 868 uint32_t rs_u = static_cast<uint32_t>(rs); | |
| 869 int32_t rt_reg = instr->RtField(); | |
| 870 int32_t rt = get_register(rt_reg); | |
| 871 uint32_t rt_u = static_cast<uint32_t>(rt); | |
| 872 int32_t rd_reg = instr->RdField(); | |
| 873 uint32_t sa = instr->SaField(); | |
| 874 | 1292 |
| 875 int32_t fs_reg= instr->FsField(); | 1293 void Simulator::ConfigureTypeRegister(Instruction* instr, |
| 1294 int32_t& alu_out, | |
| 1295 int64_t& i64hilo, | |
| 1296 uint64_t& u64hilo, | |
| 1297 int32_t& next_pc, | |
| 1298 bool& do_interrupt) { | |
| 1299 // Every local variable declared here needs to be const. | |
| 1300 // This is to make sure that changed values are sent back to | |
| 1301 // DecodeTypeRegister correctly. | |
| 876 | 1302 |
| 877 // ALU output | 1303 // Instruction fields. |
| 878 // It should not be used as is. Instructions using it should always initialize | 1304 const Opcode op = instr->OpcodeFieldRaw(); |
| 879 // it first. | 1305 const int32_t rs_reg = instr->RsValue(); |
| 880 int32_t alu_out = 0x12345678; | 1306 const int32_t rs = get_register(rs_reg); |
| 881 // Output or temporary for floating point. | 1307 const uint32_t rs_u = static_cast<uint32_t>(rs); |
| 882 double fp_out = 0.0; | 1308 const int32_t rt_reg = instr->RtValue(); |
| 1309 const int32_t rt = get_register(rt_reg); | |
| 1310 const uint32_t rt_u = static_cast<uint32_t>(rt); | |
| 1311 const int32_t rd_reg = instr->RdValue(); | |
| 1312 const uint32_t sa = instr->SaValue(); | |
| 883 | 1313 |
| 884 // For break and trap instructions. | 1314 const int32_t fs_reg = instr->FsValue(); |
| 885 bool do_interrupt = false; | |
| 886 | 1315 |
| 887 // For jr and jalr | |
| 888 // Get current pc. | |
| 889 int32_t current_pc = get_pc(); | |
| 890 // Next pc | |
| 891 int32_t next_pc = 0; | |
| 892 | 1316 |
| 893 // ---------- Configuration | 1317 // ---------- Configuration |
| 894 switch (op) { | 1318 switch (op) { |
| 895 case COP1: // Coprocessor instructions | 1319 case COP1: // Coprocessor instructions |
| 896 switch (instr->RsFieldRaw()) { | 1320 switch (instr->RsFieldRaw()) { |
| 897 case BC1: // branch on coprocessor condition | 1321 case BC1: // Handled in DecodeTypeImmed, should never come here. |
| 898 UNREACHABLE(); | 1322 UNREACHABLE(); |
| 899 break; | 1323 break; |
| 1324 case CFC1: | |
| 1325 // At the moment only FCSR is supported. | |
| 1326 ASSERT(fs_reg == kFCSRRegister); | |
| 1327 alu_out = FCSR_; | |
| 1328 break; | |
| 900 case MFC1: | 1329 case MFC1: |
| 901 alu_out = get_fpu_register(fs_reg); | 1330 alu_out = get_fpu_register(fs_reg); |
| 902 break; | 1331 break; |
| 903 case MFHC1: | 1332 case MFHC1: |
| 904 fp_out = get_fpu_register_double(fs_reg); | 1333 UNIMPLEMENTED_MIPS(); |
| 905 alu_out = *v8i::BitCast<int32_t*>(&fp_out); | |
| 906 break; | 1334 break; |
| 1335 case CTC1: | |
| 907 case MTC1: | 1336 case MTC1: |
| 908 case MTHC1: | 1337 case MTHC1: |
| 909 // Do the store in the execution step. | 1338 // Do the store in the execution step. |
| 910 break; | 1339 break; |
| 911 case S: | 1340 case S: |
| 912 case D: | 1341 case D: |
| 913 case W: | 1342 case W: |
| 914 case L: | 1343 case L: |
| 915 case PS: | 1344 case PS: |
| 916 // Do everything in the execution step. | 1345 // Do everything in the execution step. |
| 917 break; | 1346 break; |
| 918 default: | 1347 default: |
| 919 UNIMPLEMENTED_MIPS(); | 1348 UNIMPLEMENTED_MIPS(); |
| 920 }; | 1349 }; |
| 921 break; | 1350 break; |
| 922 case SPECIAL: | 1351 case SPECIAL: |
| 923 switch (instr->FunctionFieldRaw()) { | 1352 switch (instr->FunctionFieldRaw()) { |
| 924 case JR: | 1353 case JR: |
| 925 case JALR: | 1354 case JALR: |
| 926 next_pc = get_register(instr->RsField()); | 1355 next_pc = get_register(instr->RsValue()); |
| 927 break; | 1356 break; |
| 928 case SLL: | 1357 case SLL: |
| 929 alu_out = rt << sa; | 1358 alu_out = rt << sa; |
| 930 break; | 1359 break; |
| 931 case SRL: | 1360 case SRL: |
| 932 alu_out = rt_u >> sa; | 1361 if (rs_reg == 0) { |
| 1362 // Regular logical right shift of a word by a fixed number of | |
| 1363 // bits instruction. RS field is always equal to 0. | |
| 1364 alu_out = rt_u >> sa; | |
| 1365 } else { | |
| 1366 // Logical right-rotate of a word by a fixed number of bits. This | |
| 1367 // is special case of SRL instruction, added in MIPS32 Release 2. | |
| 1368 // RS field is equal to 00001 | |
|
Søren Thygesen Gjesse
2011/03/24 22:11:19
Is there any reason to check for R2 in the simulat
Paul Lind
2011/03/26 18:39:17
I guess I could see this either way. But sure, it
| |
| 1369 if (mips32r2) { | |
| 1370 alu_out = (rt_u >> sa) | (rt_u << (32 - sa)); | |
| 1371 } else { | |
| 1372 Format(instr, "SRL"); | |
| 1373 } | |
| 1374 } | |
| 933 break; | 1375 break; |
| 934 case SRA: | 1376 case SRA: |
| 935 alu_out = rt >> sa; | 1377 alu_out = rt >> sa; |
| 936 break; | 1378 break; |
| 937 case SLLV: | 1379 case SLLV: |
| 938 alu_out = rt << rs; | 1380 alu_out = rt << rs; |
| 939 break; | 1381 break; |
| 940 case SRLV: | 1382 case SRLV: |
| 941 alu_out = rt_u >> rs; | 1383 if (sa == 0) { |
| 1384 // Regular logical right-shift of a word by a variable number of | |
| 1385 // bits instruction. SA field is always equal to 0. | |
| 1386 alu_out = rt_u >> rs; | |
| 1387 } else { | |
| 1388 // Logical right-rotate of a word by a variable number of bits. | |
| 1389 // This is special case od SRLV instruction, added in MIPS32 | |
| 1390 // Release 2. SA field is equal to 00001 | |
| 1391 if (mips32r2) { | |
| 1392 alu_out = (rt_u >> rs_u) | (rt_u << (32 - rs_u)); | |
| 1393 } else { | |
| 1394 Format(instr, "SRLV"); | |
| 1395 } | |
| 1396 } | |
| 942 break; | 1397 break; |
| 943 case SRAV: | 1398 case SRAV: |
| 944 alu_out = rt >> rs; | 1399 alu_out = rt >> rs; |
| 945 break; | 1400 break; |
| 946 case MFHI: | 1401 case MFHI: |
| 947 alu_out = get_register(HI); | 1402 alu_out = get_register(HI); |
| 948 break; | 1403 break; |
| 949 case MFLO: | 1404 case MFLO: |
| 950 alu_out = get_register(LO); | 1405 alu_out = get_register(LO); |
| 951 break; | 1406 break; |
| 952 case MULT: | 1407 case MULT: |
| 953 UNIMPLEMENTED_MIPS(); | 1408 i64hilo = static_cast<int64_t>(rs) * static_cast<int64_t>(rt); |
| 954 break; | 1409 break; |
| 955 case MULTU: | 1410 case MULTU: |
| 956 UNIMPLEMENTED_MIPS(); | 1411 u64hilo = static_cast<uint64_t>(rs_u) * static_cast<uint64_t>(rt_u); |
| 957 break; | 1412 break; |
| 958 case DIV: | 1413 case DIV: |
| 959 case DIVU: | 1414 case DIVU: |
| 960 exceptions[kDivideByZero] = rt == 0; | 1415 exceptions[kDivideByZero] = rt == 0; |
| 961 break; | 1416 break; |
| 962 case ADD: | 1417 case ADD: |
| 963 if (HaveSameSign(rs, rt)) { | 1418 if (HaveSameSign(rs, rt)) { |
| 964 if (rs > 0) { | 1419 if (rs > 0) { |
| 965 exceptions[kIntegerOverflow] = rs > (Registers::kMaxValue - rt); | 1420 exceptions[kIntegerOverflow] = rs > (Registers::kMaxValue - rt); |
| 966 } else if (rs < 0) { | 1421 } else if (rs < 0) { |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 998 alu_out = ~(rs | rt); | 1453 alu_out = ~(rs | rt); |
| 999 break; | 1454 break; |
| 1000 case SLT: | 1455 case SLT: |
| 1001 alu_out = rs < rt ? 1 : 0; | 1456 alu_out = rs < rt ? 1 : 0; |
| 1002 break; | 1457 break; |
| 1003 case SLTU: | 1458 case SLTU: |
| 1004 alu_out = rs_u < rt_u ? 1 : 0; | 1459 alu_out = rs_u < rt_u ? 1 : 0; |
| 1005 break; | 1460 break; |
| 1006 // Break and trap instructions | 1461 // Break and trap instructions |
| 1007 case BREAK: | 1462 case BREAK: |
| 1463 | |
| 1008 do_interrupt = true; | 1464 do_interrupt = true; |
| 1009 break; | 1465 break; |
| 1010 case TGE: | 1466 case TGE: |
| 1011 do_interrupt = rs >= rt; | 1467 do_interrupt = rs >= rt; |
| 1012 break; | 1468 break; |
| 1013 case TGEU: | 1469 case TGEU: |
| 1014 do_interrupt = rs_u >= rt_u; | 1470 do_interrupt = rs_u >= rt_u; |
| 1015 break; | 1471 break; |
| 1016 case TLT: | 1472 case TLT: |
| 1017 do_interrupt = rs < rt; | 1473 do_interrupt = rs < rt; |
| 1018 break; | 1474 break; |
| 1019 case TLTU: | 1475 case TLTU: |
| 1020 do_interrupt = rs_u < rt_u; | 1476 do_interrupt = rs_u < rt_u; |
| 1021 break; | 1477 break; |
| 1022 case TEQ: | 1478 case TEQ: |
| 1023 do_interrupt = rs == rt; | 1479 do_interrupt = rs == rt; |
| 1024 break; | 1480 break; |
| 1025 case TNE: | 1481 case TNE: |
| 1026 do_interrupt = rs != rt; | 1482 do_interrupt = rs != rt; |
| 1027 break; | 1483 break; |
| 1484 case MOVN: | |
| 1485 case MOVZ: | |
| 1486 case MOVCI: | |
| 1487 // No action taken on decode. | |
| 1488 break; | |
| 1028 default: | 1489 default: |
| 1029 UNREACHABLE(); | 1490 UNREACHABLE(); |
| 1030 }; | 1491 }; |
| 1031 break; | 1492 break; |
| 1032 case SPECIAL2: | 1493 case SPECIAL2: |
| 1033 switch (instr->FunctionFieldRaw()) { | 1494 switch (instr->FunctionFieldRaw()) { |
| 1034 case MUL: | 1495 case MUL: |
| 1035 alu_out = rs_u * rt_u; // Only the lower 32 bits are kept. | 1496 alu_out = rs_u * rt_u; // Only the lower 32 bits are kept. |
| 1036 break; | 1497 break; |
| 1498 case CLZ: | |
| 1499 alu_out = __builtin_clz(rs_u); | |
| 1500 break; | |
| 1037 default: | 1501 default: |
| 1038 UNREACHABLE(); | 1502 UNREACHABLE(); |
| 1039 } | 1503 }; |
| 1504 break; | |
| 1505 case SPECIAL3: | |
| 1506 switch (instr->FunctionFieldRaw()) { | |
| 1507 case INS: { // mips32r2 instruction. | |
| 1508 if (mips32r2) { | |
| 1509 // Interpret Rd field as 5-bit msb of insert. | |
| 1510 uint16_t msb = rd_reg; | |
| 1511 // Interpret sa field as 5-bit lsb of insert. | |
| 1512 uint16_t lsb = sa; | |
| 1513 uint16_t size = msb - lsb + 1; | |
| 1514 uint32_t mask = (1 << size) - 1; | |
| 1515 alu_out = (rt_u & ~(mask << lsb)) | ((rs_u & mask) << lsb); | |
| 1516 } else { | |
| 1517 Format(instr, "INS"); | |
| 1518 } | |
| 1519 } | |
| 1520 break; | |
| 1521 case EXT: { // mips32r2 instruction. | |
| 1522 if (mips32r2) { | |
| 1523 // Interpret Rd field as 5-bit msb of extract. | |
| 1524 uint16_t msb = rd_reg; | |
| 1525 // Interpret sa field as 5-bit lsb of extract. | |
| 1526 uint16_t lsb = sa; | |
| 1527 uint16_t size = msb + 1; | |
| 1528 uint32_t mask = (1 << size) - 1; | |
| 1529 alu_out = (rs_u & (mask << lsb)) >> lsb; | |
| 1530 } else { | |
| 1531 Format(instr, "EXT"); | |
| 1532 } | |
| 1533 } | |
| 1534 break; | |
| 1535 default: | |
| 1536 UNREACHABLE(); | |
| 1537 }; | |
| 1040 break; | 1538 break; |
| 1041 default: | 1539 default: |
| 1042 UNREACHABLE(); | 1540 UNREACHABLE(); |
| 1043 }; | 1541 }; |
| 1542 } | |
| 1543 | |
| 1544 | |
| 1545 void Simulator::DecodeTypeRegister(Instruction* instr) { | |
| 1546 // Instruction fields. | |
| 1547 const Opcode op = instr->OpcodeFieldRaw(); | |
| 1548 const int32_t rs_reg = instr->RsValue(); | |
| 1549 const int32_t rs = get_register(rs_reg); | |
| 1550 const uint32_t rs_u = static_cast<uint32_t>(rs); | |
| 1551 const int32_t rt_reg = instr->RtValue(); | |
| 1552 const int32_t rt = get_register(rt_reg); | |
| 1553 const uint32_t rt_u = static_cast<uint32_t>(rt); | |
| 1554 const int32_t rd_reg = instr->RdValue(); | |
| 1555 | |
| 1556 const int32_t fs_reg = instr->FsValue(); | |
| 1557 const int32_t ft_reg = instr->FtValue(); | |
| 1558 const int32_t fd_reg = instr->FdValue(); | |
| 1559 int64_t i64hilo = 0; | |
| 1560 uint64_t u64hilo = 0; | |
| 1561 | |
| 1562 // ALU output | |
| 1563 // It should not be used as is. Instructions using it should always | |
| 1564 // initialize it first. | |
| 1565 int32_t alu_out = 0x12345678; | |
| 1566 | |
| 1567 // For break and trap instructions. | |
| 1568 bool do_interrupt = false; | |
| 1569 | |
| 1570 // For jr and jalr | |
| 1571 // Get current pc. | |
| 1572 int32_t current_pc = get_pc(); | |
| 1573 // Next pc | |
| 1574 int32_t next_pc = 0; | |
| 1575 | |
| 1576 // Setup the variables if needed before executing the instruction. | |
| 1577 ConfigureTypeRegister(instr, | |
| 1578 alu_out, | |
| 1579 i64hilo, | |
| 1580 u64hilo, | |
| 1581 next_pc, | |
| 1582 do_interrupt); | |
| 1044 | 1583 |
| 1045 // ---------- Raise exceptions triggered. | 1584 // ---------- Raise exceptions triggered. |
| 1046 SignalExceptions(); | 1585 SignalExceptions(); |
| 1047 | 1586 |
| 1048 // ---------- Execution | 1587 // ---------- Execution |
| 1049 switch (op) { | 1588 switch (op) { |
| 1050 case COP1: | 1589 case COP1: |
| 1051 switch (instr->RsFieldRaw()) { | 1590 switch (instr->RsFieldRaw()) { |
| 1052 case BC1: // branch on coprocessor condition | 1591 case BC1: // branch on coprocessor condition |
| 1053 UNREACHABLE(); | 1592 UNREACHABLE(); |
| 1054 break; | 1593 break; |
| 1594 case CFC1: | |
| 1595 set_register(rt_reg, alu_out); | |
| 1055 case MFC1: | 1596 case MFC1: |
| 1056 case MFHC1: | |
| 1057 set_register(rt_reg, alu_out); | 1597 set_register(rt_reg, alu_out); |
| 1058 break; | 1598 break; |
| 1599 case MFHC1: | |
| 1600 UNIMPLEMENTED_MIPS(); | |
| 1601 break; | |
| 1602 case CTC1: | |
| 1603 // At the moment only FCSR is supported. | |
| 1604 ASSERT(fs_reg == kFCSRRegister); | |
| 1605 FCSR_ = registers_[rt_reg]; | |
| 1606 break; | |
| 1059 case MTC1: | 1607 case MTC1: |
| 1060 // We don't need to set the higher bits to 0, because MIPS ISA says | |
| 1061 // they are in an unpredictable state after executing MTC1. | |
| 1062 FPUregisters_[fs_reg] = registers_[rt_reg]; | 1608 FPUregisters_[fs_reg] = registers_[rt_reg]; |
| 1063 FPUregisters_[fs_reg+1] = Unpredictable; | |
| 1064 break; | 1609 break; |
| 1065 case MTHC1: | 1610 case MTHC1: |
| 1066 // Here we need to keep the lower bits unchanged. | 1611 UNIMPLEMENTED_MIPS(); |
| 1067 FPUregisters_[fs_reg+1] = registers_[rt_reg]; | |
| 1068 break; | 1612 break; |
| 1069 case S: | 1613 case S: |
| 1614 float f; | |
| 1070 switch (instr->FunctionFieldRaw()) { | 1615 switch (instr->FunctionFieldRaw()) { |
| 1071 case CVT_D_S: | 1616 case CVT_D_S: |
| 1617 f = get_fpu_register_float(fs_reg); | |
| 1618 set_fpu_register_double(fd_reg, static_cast<double>(f)); | |
| 1619 break; | |
| 1072 case CVT_W_S: | 1620 case CVT_W_S: |
| 1073 case CVT_L_S: | 1621 case CVT_L_S: |
| 1622 case TRUNC_W_S: | |
| 1623 case TRUNC_L_S: | |
| 1624 case ROUND_W_S: | |
| 1625 case ROUND_L_S: | |
| 1626 case FLOOR_W_S: | |
| 1627 case FLOOR_L_S: | |
| 1628 case CEIL_W_S: | |
| 1629 case CEIL_L_S: | |
| 1074 case CVT_PS_S: | 1630 case CVT_PS_S: |
| 1075 UNIMPLEMENTED_MIPS(); | 1631 UNIMPLEMENTED_MIPS(); |
| 1076 break; | 1632 break; |
| 1077 default: | 1633 default: |
| 1078 UNREACHABLE(); | 1634 UNREACHABLE(); |
| 1079 } | 1635 } |
| 1080 break; | 1636 break; |
| 1081 case D: | 1637 case D: |
| 1638 double ft, fs; | |
| 1639 uint32_t cc, fcsr_cc; | |
| 1640 int64_t i64; | |
| 1641 fs = get_fpu_register_double(fs_reg); | |
| 1642 ft = get_fpu_register_double(ft_reg); | |
| 1643 cc = instr->FCccValue(); | |
| 1644 fcsr_cc = get_fcsr_condition_bit(cc); | |
| 1082 switch (instr->FunctionFieldRaw()) { | 1645 switch (instr->FunctionFieldRaw()) { |
| 1083 case CVT_S_D: | 1646 case ADD_D: |
| 1084 case CVT_W_D: | 1647 set_fpu_register_double(fd_reg, fs + ft); |
| 1085 case CVT_L_D: | 1648 break; |
| 1649 case SUB_D: | |
| 1650 set_fpu_register_double(fd_reg, fs - ft); | |
| 1651 break; | |
| 1652 case MUL_D: | |
| 1653 set_fpu_register_double(fd_reg, fs * ft); | |
| 1654 break; | |
| 1655 case DIV_D: | |
| 1656 set_fpu_register_double(fd_reg, fs / ft); | |
| 1657 break; | |
| 1658 case ABS_D: | |
| 1659 set_fpu_register_double(fd_reg, fs < 0 ? -fs : fs); | |
| 1660 break; | |
| 1661 case MOV_D: | |
| 1662 set_fpu_register_double(fd_reg, fs); | |
| 1663 break; | |
| 1664 case NEG_D: | |
| 1665 set_fpu_register_double(fd_reg, -fs); | |
| 1666 break; | |
| 1667 case SQRT_D: | |
| 1668 set_fpu_register_double(fd_reg, sqrt(fs)); | |
| 1669 break; | |
| 1670 case C_UN_D: | |
| 1671 set_fcsr_bit(fcsr_cc, isnan(fs) || isnan(ft)); | |
| 1672 break; | |
| 1673 case C_EQ_D: | |
| 1674 set_fcsr_bit(fcsr_cc, (fs == ft)); | |
| 1675 break; | |
| 1676 case C_UEQ_D: | |
| 1677 set_fcsr_bit(fcsr_cc, (fs == ft) || (isnan(fs) || isnan(ft))); | |
| 1678 break; | |
| 1679 case C_OLT_D: | |
| 1680 set_fcsr_bit(fcsr_cc, (fs < ft)); | |
| 1681 break; | |
| 1682 case C_ULT_D: | |
| 1683 set_fcsr_bit(fcsr_cc, (fs < ft) || (isnan(fs) || isnan(ft))); | |
| 1684 break; | |
| 1685 case C_OLE_D: | |
| 1686 set_fcsr_bit(fcsr_cc, (fs <= ft)); | |
| 1687 break; | |
| 1688 case C_ULE_D: | |
| 1689 set_fcsr_bit(fcsr_cc, (fs <= ft) || (isnan(fs) || isnan(ft))); | |
| 1690 break; | |
| 1691 case CVT_W_D: // Convert double to word. | |
| 1692 // Rounding modes are not yet supported. | |
| 1693 ASSERT((FCSR_ & 3) == 0); | |
| 1694 // In rounding mode 0 it should behave like ROUND. | |
| 1695 case ROUND_W_D: // Round double to word. | |
| 1696 { | |
| 1697 double rounded = fs > 0 ? floor(fs + 0.5) : ceil(fs - 0.5); | |
| 1698 int32_t result = static_cast<int32_t>(rounded); | |
| 1699 set_fpu_register(fd_reg, result); | |
| 1700 if (set_fcsr_round_error(fs, rounded)) { | |
| 1701 set_fpu_register(fd_reg, kFPUInvalidResult); | |
| 1702 } | |
| 1703 } | |
| 1704 break; | |
| 1705 case TRUNC_W_D: // Truncate double to word (round towards 0). | |
| 1706 { | |
| 1707 int32_t result = static_cast<int32_t>(fs); | |
| 1708 set_fpu_register(fd_reg, result); | |
| 1709 if (set_fcsr_round_error(fs, static_cast<double>(result))) { | |
| 1710 set_fpu_register(fd_reg, kFPUInvalidResult); | |
| 1711 } | |
| 1712 } | |
| 1713 break; | |
| 1714 case FLOOR_W_D: // Round double to word towards negative infinity. | |
| 1715 { | |
| 1716 double rounded = floor(fs); | |
| 1717 int32_t result = static_cast<int32_t>(rounded); | |
| 1718 set_fpu_register(fd_reg, result); | |
| 1719 if (set_fcsr_round_error(fs, rounded)) { | |
| 1720 set_fpu_register(fd_reg, kFPUInvalidResult); | |
| 1721 } | |
| 1722 } | |
| 1723 break; | |
| 1724 case CEIL_W_D: // Round double to word towards positive infinity. | |
| 1725 { | |
| 1726 double rounded = ceil(fs); | |
| 1727 int32_t result = static_cast<int32_t>(rounded); | |
| 1728 set_fpu_register(fd_reg, result); | |
| 1729 if (set_fcsr_round_error(fs, rounded)) { | |
| 1730 set_fpu_register(fd_reg, kFPUInvalidResult); | |
| 1731 } | |
| 1732 } | |
| 1733 break; | |
| 1734 case CVT_S_D: // Convert double to float (single). | |
| 1735 set_fpu_register_float(fd_reg, static_cast<float>(fs)); | |
| 1736 break; | |
| 1737 case CVT_L_D: // Truncate double to 64-bit long-word. | |
| 1738 if (mips32r2) { | |
| 1739 i64 = static_cast<int64_t>(fs); | |
| 1740 set_fpu_register(fd_reg, i64 & 0xffffffff); | |
| 1741 set_fpu_register(fd_reg + 1, i64 >> 32); | |
| 1742 } else { | |
| 1743 // Not allowed on MIPS32 Release 1 | |
| 1744 UNIMPLEMENTED_MIPS(); | |
| 1745 } | |
| 1746 break; | |
| 1747 case TRUNC_L_D: | |
| 1748 if (mips32r2) { | |
| 1749 i64 = static_cast<int64_t>(fs); | |
| 1750 set_fpu_register(fd_reg, i64 & 0xffffffff); | |
| 1751 set_fpu_register(fd_reg + 1, i64 >> 32); | |
| 1752 } else { | |
| 1753 // Not allowed on MIPS32 Release 1 | |
| 1754 UNIMPLEMENTED_MIPS(); | |
| 1755 } | |
| 1756 break; | |
| 1757 case ROUND_L_D: | |
| 1758 if (mips32r2) { | |
| 1759 double rounded = fs > 0 ? floor(fs + 0.5) : ceil(fs - 0.5); | |
| 1760 i64 = static_cast<int64_t>(rounded); | |
| 1761 set_fpu_register(fd_reg, i64 & 0xffffffff); | |
| 1762 set_fpu_register(fd_reg + 1, i64 >> 32); | |
| 1763 } else { | |
| 1764 // Not allowed on MIPS32 Release 1 | |
| 1765 UNIMPLEMENTED_MIPS(); | |
| 1766 } | |
| 1767 break; | |
| 1768 case FLOOR_L_D: | |
| 1769 if (mips32r2) { | |
| 1770 i64 = static_cast<int64_t>(floor(fs)); | |
| 1771 set_fpu_register(fd_reg, i64 & 0xffffffff); | |
| 1772 set_fpu_register(fd_reg + 1, i64 >> 32); | |
| 1773 } else { | |
| 1774 // Not allowed on MIPS32 Release 1 | |
| 1775 UNIMPLEMENTED_MIPS(); | |
| 1776 } | |
| 1777 break; | |
| 1778 case CEIL_L_D: | |
| 1779 if (mips32r2) { | |
| 1780 i64 = static_cast<int64_t>(ceil(fs)); | |
| 1781 set_fpu_register(fd_reg, i64 & 0xffffffff); | |
| 1782 set_fpu_register(fd_reg + 1, i64 >> 32); | |
| 1783 } else { | |
| 1784 // Not allowed on MIPS32 Release 1 | |
| 1785 UNIMPLEMENTED_MIPS(); | |
| 1786 } | |
| 1787 break; | |
| 1788 case C_F_D: | |
| 1086 UNIMPLEMENTED_MIPS(); | 1789 UNIMPLEMENTED_MIPS(); |
| 1087 break; | 1790 break; |
| 1088 default: | 1791 default: |
| 1089 UNREACHABLE(); | 1792 UNREACHABLE(); |
| 1090 } | 1793 } |
| 1091 break; | 1794 break; |
| 1092 case W: | 1795 case W: |
| 1093 switch (instr->FunctionFieldRaw()) { | 1796 switch (instr->FunctionFieldRaw()) { |
| 1094 case CVT_S_W: | 1797 case CVT_S_W: // Convert word to float (single). |
| 1095 UNIMPLEMENTED_MIPS(); | 1798 alu_out = get_fpu_register(fs_reg); |
| 1799 set_fpu_register_float(fd_reg, static_cast<float>(alu_out)); | |
| 1096 break; | 1800 break; |
| 1097 case CVT_D_W: // Convert word to double. | 1801 case CVT_D_W: // Convert word to double. |
| 1098 set_fpu_register(rd_reg, static_cast<double>(rs)); | 1802 alu_out = get_fpu_register(fs_reg); |
| 1803 set_fpu_register_double(fd_reg, static_cast<double>(alu_out)); | |
| 1099 break; | 1804 break; |
| 1100 default: | 1805 default: |
| 1101 UNREACHABLE(); | 1806 UNREACHABLE(); |
| 1102 }; | 1807 }; |
| 1103 break; | 1808 break; |
| 1104 case L: | 1809 case L: |
| 1105 switch (instr->FunctionFieldRaw()) { | 1810 switch (instr->FunctionFieldRaw()) { |
| 1811 case CVT_D_L: | |
| 1812 if (mips32r2) { | |
| 1813 // Watch the signs here, we want 2 32-bit vals | |
| 1814 // to make a sign-64. | |
| 1815 i64 = (uint32_t) get_fpu_register(fs_reg); | |
| 1816 i64 |= ((int64_t) get_fpu_register(fs_reg + 1) << 32); | |
| 1817 set_fpu_register_double(fd_reg, static_cast<double>(i64)); | |
| 1818 } else { | |
| 1819 // Not allowed on MIPS32 Release 1 | |
| 1820 UNIMPLEMENTED_MIPS(); | |
| 1821 } | |
| 1822 break; | |
| 1106 case CVT_S_L: | 1823 case CVT_S_L: |
| 1107 case CVT_D_L: | |
| 1108 UNIMPLEMENTED_MIPS(); | 1824 UNIMPLEMENTED_MIPS(); |
| 1109 break; | 1825 break; |
| 1110 default: | 1826 default: |
| 1111 UNREACHABLE(); | 1827 UNREACHABLE(); |
| 1112 } | 1828 } |
| 1113 break; | 1829 break; |
| 1114 case PS: | 1830 case PS: |
| 1115 break; | 1831 break; |
| 1116 default: | 1832 default: |
| 1117 UNREACHABLE(); | 1833 UNREACHABLE(); |
| 1118 }; | 1834 }; |
| 1119 break; | 1835 break; |
| 1120 case SPECIAL: | 1836 case SPECIAL: |
| 1121 switch (instr->FunctionFieldRaw()) { | 1837 switch (instr->FunctionFieldRaw()) { |
| 1122 case JR: { | 1838 case JR: { |
| 1123 Instruction* branch_delay_instr = reinterpret_cast<Instruction*>( | 1839 Instruction* branch_delay_instr = reinterpret_cast<Instruction*>( |
| 1124 current_pc+Instruction::kInstructionSize); | 1840 current_pc+Instruction::kInstrSize); |
| 1125 BranchDelayInstructionDecode(branch_delay_instr); | 1841 BranchDelayInstructionDecode(branch_delay_instr); |
| 1126 set_pc(next_pc); | 1842 set_pc(next_pc); |
| 1127 pc_modified_ = true; | 1843 pc_modified_ = true; |
| 1128 break; | 1844 break; |
| 1129 } | 1845 } |
| 1130 case JALR: { | 1846 case JALR: { |
| 1131 Instruction* branch_delay_instr = reinterpret_cast<Instruction*>( | 1847 Instruction* branch_delay_instr = reinterpret_cast<Instruction*>( |
| 1132 current_pc+Instruction::kInstructionSize); | 1848 current_pc+Instruction::kInstrSize); |
| 1133 BranchDelayInstructionDecode(branch_delay_instr); | 1849 BranchDelayInstructionDecode(branch_delay_instr); |
| 1134 set_register(31, current_pc + 2* Instruction::kInstructionSize); | 1850 set_register(31, current_pc + 2* Instruction::kInstrSize); |
| 1135 set_pc(next_pc); | 1851 set_pc(next_pc); |
| 1136 pc_modified_ = true; | 1852 pc_modified_ = true; |
| 1137 break; | 1853 break; |
| 1138 } | 1854 } |
| 1139 // Instructions using HI and LO registers. | 1855 // Instructions using HI and LO registers. |
| 1140 case MULT: | 1856 case MULT: |
| 1857 set_register(LO, static_cast<int32_t>(i64hilo & 0xffffffff)); | |
| 1858 set_register(HI, static_cast<int32_t>(i64hilo >> 32)); | |
| 1859 break; | |
| 1141 case MULTU: | 1860 case MULTU: |
| 1861 set_register(LO, static_cast<int32_t>(u64hilo & 0xffffffff)); | |
| 1862 set_register(HI, static_cast<int32_t>(u64hilo >> 32)); | |
| 1142 break; | 1863 break; |
| 1143 case DIV: | 1864 case DIV: |
| 1144 // Divide by zero was checked in the configuration step. | 1865 // Divide by zero was checked in the configuration step. |
| 1145 set_register(LO, rs / rt); | 1866 set_register(LO, rs / rt); |
| 1146 set_register(HI, rs % rt); | 1867 set_register(HI, rs % rt); |
| 1147 break; | 1868 break; |
| 1148 case DIVU: | 1869 case DIVU: |
| 1149 set_register(LO, rs_u / rt_u); | 1870 set_register(LO, rs_u / rt_u); |
| 1150 set_register(HI, rs_u % rt_u); | 1871 set_register(HI, rs_u % rt_u); |
| 1151 break; | 1872 break; |
| 1152 // Break and trap instructions | 1873 // Break and trap instructions. |
| 1153 case BREAK: | 1874 case BREAK: |
| 1154 case TGE: | 1875 case TGE: |
| 1155 case TGEU: | 1876 case TGEU: |
| 1156 case TLT: | 1877 case TLT: |
| 1157 case TLTU: | 1878 case TLTU: |
| 1158 case TEQ: | 1879 case TEQ: |
| 1159 case TNE: | 1880 case TNE: |
| 1160 if (do_interrupt) { | 1881 if (do_interrupt) { |
| 1161 SoftwareInterrupt(instr); | 1882 SoftwareInterrupt(instr); |
| 1162 } | 1883 } |
| 1163 break; | 1884 break; |
| 1885 // Conditional moves. | |
| 1886 case MOVN: | |
| 1887 if (rt) set_register(rd_reg, rs); | |
| 1888 break; | |
| 1889 case MOVCI: { | |
| 1890 uint32_t cc = instr->FCccValue(); | |
| 1891 uint32_t fcsr_cc = get_fcsr_condition_bit(cc); | |
| 1892 if (instr->Bit(16)) { // Read Tf bit | |
| 1893 if (test_fcsr_bit(fcsr_cc)) set_register(rd_reg, rs); | |
| 1894 } else { | |
| 1895 if (!test_fcsr_bit(fcsr_cc)) set_register(rd_reg, rs); | |
| 1896 } | |
| 1897 break; | |
| 1898 } | |
| 1899 case MOVZ: | |
| 1900 if (!rt) set_register(rd_reg, rs); | |
| 1901 break; | |
| 1164 default: // For other special opcodes we do the default operation. | 1902 default: // For other special opcodes we do the default operation. |
| 1165 set_register(rd_reg, alu_out); | 1903 set_register(rd_reg, alu_out); |
| 1166 }; | 1904 }; |
| 1167 break; | 1905 break; |
| 1168 case SPECIAL2: | 1906 case SPECIAL2: |
| 1169 switch (instr->FunctionFieldRaw()) { | 1907 switch (instr->FunctionFieldRaw()) { |
| 1170 case MUL: | 1908 case MUL: |
| 1171 set_register(rd_reg, alu_out); | 1909 set_register(rd_reg, alu_out); |
| 1172 // HI and LO are UNPREDICTABLE after the operation. | 1910 // HI and LO are UNPREDICTABLE after the operation. |
| 1173 set_register(LO, Unpredictable); | 1911 set_register(LO, Unpredictable); |
| 1174 set_register(HI, Unpredictable); | 1912 set_register(HI, Unpredictable); |
| 1175 break; | 1913 break; |
| 1914 default: // For other special2 opcodes we do the default operation. | |
| 1915 set_register(rd_reg, alu_out); | |
| 1916 } | |
| 1917 break; | |
| 1918 case SPECIAL3: | |
| 1919 switch (instr->FunctionFieldRaw()) { | |
| 1920 case INS: | |
| 1921 // Ins instr leaves result in Rt, rather than Rd. | |
| 1922 set_register(rt_reg, alu_out); | |
| 1923 break; | |
| 1924 case EXT: | |
| 1925 // Ext instr leaves result in Rt, rather than Rd. | |
| 1926 set_register(rt_reg, alu_out); | |
| 1927 break; | |
| 1176 default: | 1928 default: |
| 1177 UNREACHABLE(); | 1929 UNREACHABLE(); |
| 1178 } | 1930 }; |
| 1179 break; | 1931 break; |
| 1180 // Unimplemented opcodes raised an error in the configuration step before, | 1932 // Unimplemented opcodes raised an error in the configuration step before, |
| 1181 // so we can use the default here to set the destination register in common | 1933 // so we can use the default here to set the destination register in common |
| 1182 // cases. | 1934 // cases. |
| 1183 default: | 1935 default: |
| 1184 set_register(rd_reg, alu_out); | 1936 set_register(rd_reg, alu_out); |
| 1185 }; | 1937 }; |
| 1186 } | 1938 } |
| 1187 | 1939 |
| 1940 | |
| 1188 // Type 2: instructions using a 16 bytes immediate. (eg: addi, beq) | 1941 // Type 2: instructions using a 16 bytes immediate. (eg: addi, beq) |
| 1189 void Simulator::DecodeTypeImmediate(Instruction* instr) { | 1942 void Simulator::DecodeTypeImmediate(Instruction* instr) { |
| 1190 // Instruction fields | 1943 // Instruction fields. |
| 1191 Opcode op = instr->OpcodeFieldRaw(); | 1944 Opcode op = instr->OpcodeFieldRaw(); |
| 1192 int32_t rs = get_register(instr->RsField()); | 1945 int32_t rs = get_register(instr->RsValue()); |
| 1193 uint32_t rs_u = static_cast<uint32_t>(rs); | 1946 uint32_t rs_u = static_cast<uint32_t>(rs); |
| 1194 int32_t rt_reg = instr->RtField(); // destination register | 1947 int32_t rt_reg = instr->RtValue(); // destination register |
| 1195 int32_t rt = get_register(rt_reg); | 1948 int32_t rt = get_register(rt_reg); |
| 1196 int16_t imm16 = instr->Imm16Field(); | 1949 int16_t imm16 = instr->Imm16Value(); |
| 1197 | 1950 |
| 1198 int32_t ft_reg = instr->FtField(); // destination register | 1951 int32_t ft_reg = instr->FtValue(); // destination register |
| 1199 int32_t ft = get_register(ft_reg); | |
| 1200 | 1952 |
| 1201 // zero extended immediate | 1953 // Zero extended immediate. |
| 1202 uint32_t oe_imm16 = 0xffff & imm16; | 1954 uint32_t oe_imm16 = 0xffff & imm16; |
| 1203 // sign extended immediate | 1955 // Sign extended immediate. |
| 1204 int32_t se_imm16 = imm16; | 1956 int32_t se_imm16 = imm16; |
| 1205 | 1957 |
| 1206 // Get current pc. | 1958 // Get current pc. |
| 1207 int32_t current_pc = get_pc(); | 1959 int32_t current_pc = get_pc(); |
| 1208 // Next pc. | 1960 // Next pc. |
| 1209 int32_t next_pc = bad_ra; | 1961 int32_t next_pc = bad_ra; |
| 1210 | 1962 |
| 1211 // Used for conditional branch instructions | 1963 // Used for conditional branch instructions. |
| 1212 bool do_branch = false; | 1964 bool do_branch = false; |
| 1213 bool execute_branch_delay_instruction = false; | 1965 bool execute_branch_delay_instruction = false; |
| 1214 | 1966 |
| 1215 // Used for arithmetic instructions | 1967 // Used for arithmetic instructions. |
| 1216 int32_t alu_out = 0; | 1968 int32_t alu_out = 0; |
| 1217 // Floating point | 1969 // Floating point. |
| 1218 double fp_out = 0.0; | 1970 double fp_out = 0.0; |
| 1971 uint32_t cc, cc_value, fcsr_cc; | |
| 1219 | 1972 |
| 1220 // Used for memory instructions | 1973 // Used for memory instructions. |
| 1221 int32_t addr = 0x0; | 1974 int32_t addr = 0x0; |
| 1975 // Value to be written in memory | |
| 1976 uint32_t mem_value = 0x0; | |
| 1222 | 1977 |
| 1223 // ---------- Configuration (and execution for REGIMM) | 1978 // ---------- Configuration (and execution for REGIMM) |
| 1224 switch (op) { | 1979 switch (op) { |
| 1225 // ------------- COP1. Coprocessor instructions | 1980 // ------------- COP1. Coprocessor instructions. |
| 1226 case COP1: | 1981 case COP1: |
| 1227 switch (instr->RsFieldRaw()) { | 1982 switch (instr->RsFieldRaw()) { |
| 1228 case BC1: // branch on coprocessor condition | 1983 case BC1: // Branch on coprocessor condition. |
| 1229 UNIMPLEMENTED_MIPS(); | 1984 cc = instr->FBccValue(); |
| 1985 fcsr_cc = get_fcsr_condition_bit(cc); | |
| 1986 cc_value = test_fcsr_bit(fcsr_cc); | |
| 1987 do_branch = (instr->FBtrueValue()) ? cc_value : !cc_value; | |
| 1988 execute_branch_delay_instruction = true; | |
| 1989 // Set next_pc | |
| 1990 if (do_branch) { | |
| 1991 next_pc = current_pc + (imm16 << 2) + Instruction::kInstrSize; | |
| 1992 } else { | |
| 1993 next_pc = current_pc + kBranchReturnOffset; | |
| 1994 } | |
| 1230 break; | 1995 break; |
| 1231 default: | 1996 default: |
| 1232 UNREACHABLE(); | 1997 UNREACHABLE(); |
| 1233 }; | 1998 }; |
| 1234 break; | 1999 break; |
| 1235 // ------------- REGIMM class | 2000 // ------------- REGIMM class |
| 1236 case REGIMM: | 2001 case REGIMM: |
| 1237 switch (instr->RtFieldRaw()) { | 2002 switch (instr->RtFieldRaw()) { |
| 1238 case BLTZ: | 2003 case BLTZ: |
| 1239 do_branch = (rs < 0); | 2004 do_branch = (rs < 0); |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 1252 }; | 2017 }; |
| 1253 switch (instr->RtFieldRaw()) { | 2018 switch (instr->RtFieldRaw()) { |
| 1254 case BLTZ: | 2019 case BLTZ: |
| 1255 case BLTZAL: | 2020 case BLTZAL: |
| 1256 case BGEZ: | 2021 case BGEZ: |
| 1257 case BGEZAL: | 2022 case BGEZAL: |
| 1258 // Branch instructions common part. | 2023 // Branch instructions common part. |
| 1259 execute_branch_delay_instruction = true; | 2024 execute_branch_delay_instruction = true; |
| 1260 // Set next_pc | 2025 // Set next_pc |
| 1261 if (do_branch) { | 2026 if (do_branch) { |
| 1262 next_pc = current_pc + (imm16 << 2) + Instruction::kInstructionSize; | 2027 next_pc = current_pc + (imm16 << 2) + Instruction::kInstrSize; |
| 1263 if (instr->IsLinkingInstruction()) { | 2028 if (instr->IsLinkingInstruction()) { |
| 1264 set_register(31, current_pc + kBranchReturnOffset); | 2029 set_register(31, current_pc + kBranchReturnOffset); |
| 1265 } | 2030 } |
| 1266 } else { | 2031 } else { |
| 1267 next_pc = current_pc + kBranchReturnOffset; | 2032 next_pc = current_pc + kBranchReturnOffset; |
| 1268 } | 2033 } |
| 1269 default: | 2034 default: |
| 1270 break; | 2035 break; |
| 1271 }; | 2036 }; |
| 1272 break; // case REGIMM | 2037 break; // case REGIMM |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1316 alu_out = rs ^ oe_imm16; | 2081 alu_out = rs ^ oe_imm16; |
| 1317 break; | 2082 break; |
| 1318 case LUI: | 2083 case LUI: |
| 1319 alu_out = (oe_imm16 << 16); | 2084 alu_out = (oe_imm16 << 16); |
| 1320 break; | 2085 break; |
| 1321 // ------------- Memory instructions | 2086 // ------------- Memory instructions |
| 1322 case LB: | 2087 case LB: |
| 1323 addr = rs + se_imm16; | 2088 addr = rs + se_imm16; |
| 1324 alu_out = ReadB(addr); | 2089 alu_out = ReadB(addr); |
| 1325 break; | 2090 break; |
| 2091 case LH: | |
| 2092 addr = rs + se_imm16; | |
| 2093 alu_out = ReadH(addr, instr); | |
| 2094 break; | |
| 2095 case LWL: { | |
| 2096 // al_offset is an offset of the effective address within an aligned word | |
| 2097 uint8_t al_offset = (rs + se_imm16) & kPointerAlignmentMask; | |
| 2098 uint8_t byte_shift = kPointerAlignmentMask - al_offset; | |
| 2099 uint32_t mask = (1 << byte_shift * 8) - 1; | |
| 2100 addr = rs + se_imm16 - al_offset; | |
| 2101 alu_out = ReadW(addr, instr); | |
| 2102 alu_out <<= byte_shift * 8; | |
| 2103 alu_out |= rt & mask; | |
|
Søren Thygesen Gjesse
2011/03/24 22:11:19
Format
}
break;
as
break;
}
More below.
Paul Lind
2011/03/26 18:39:17
Fixed, several spots.
| |
| 2104 } | |
| 2105 break; | |
| 1326 case LW: | 2106 case LW: |
| 1327 addr = rs + se_imm16; | 2107 addr = rs + se_imm16; |
| 1328 alu_out = ReadW(addr, instr); | 2108 alu_out = ReadW(addr, instr); |
| 1329 break; | 2109 break; |
| 1330 case LBU: | 2110 case LBU: |
| 1331 addr = rs + se_imm16; | 2111 addr = rs + se_imm16; |
| 1332 alu_out = ReadBU(addr); | 2112 alu_out = ReadBU(addr); |
| 1333 break; | 2113 break; |
| 2114 case LHU: | |
| 2115 addr = rs + se_imm16; | |
| 2116 alu_out = ReadHU(addr, instr); | |
| 2117 break; | |
| 2118 case LWR: { | |
| 2119 // al_offset is an offset of the effective address within an aligned word | |
| 2120 uint8_t al_offset = (rs + se_imm16) & kPointerAlignmentMask; | |
| 2121 uint8_t byte_shift = kPointerAlignmentMask - al_offset; | |
| 2122 uint32_t mask = al_offset ? (~0 << (byte_shift + 1) * 8) : 0; | |
| 2123 addr = rs + se_imm16 - al_offset; | |
| 2124 alu_out = ReadW(addr, instr); | |
| 2125 alu_out = static_cast<uint32_t> (alu_out) >> al_offset * 8; | |
| 2126 alu_out |= rt & mask; | |
| 2127 } | |
| 2128 break; | |
| 1334 case SB: | 2129 case SB: |
| 1335 addr = rs + se_imm16; | 2130 addr = rs + se_imm16; |
| 1336 break; | 2131 break; |
| 2132 case SH: | |
| 2133 addr = rs + se_imm16; | |
| 2134 break; | |
| 2135 case SWL: { | |
| 2136 uint8_t al_offset = (rs + se_imm16) & kPointerAlignmentMask; | |
| 2137 uint8_t byte_shift = kPointerAlignmentMask - al_offset; | |
| 2138 uint32_t mask = byte_shift ? (~0 << (al_offset + 1) * 8) : 0; | |
| 2139 addr = rs + se_imm16 - al_offset; | |
| 2140 mem_value = ReadW(addr, instr) & mask; | |
| 2141 mem_value |= static_cast<uint32_t>(rt) >> byte_shift * 8; | |
| 2142 } | |
| 2143 break; | |
| 1337 case SW: | 2144 case SW: |
| 1338 addr = rs + se_imm16; | 2145 addr = rs + se_imm16; |
| 1339 break; | 2146 break; |
| 2147 case SWR: { | |
| 2148 uint8_t al_offset = (rs + se_imm16) & kPointerAlignmentMask; | |
| 2149 uint32_t mask = (1 << al_offset * 8) - 1; | |
| 2150 addr = rs + se_imm16 - al_offset; | |
| 2151 mem_value = ReadW(addr, instr); | |
| 2152 mem_value = (rt << al_offset * 8) | (mem_value & mask); | |
| 2153 } | |
| 2154 break; | |
| 1340 case LWC1: | 2155 case LWC1: |
| 1341 addr = rs + se_imm16; | 2156 addr = rs + se_imm16; |
| 1342 alu_out = ReadW(addr, instr); | 2157 alu_out = ReadW(addr, instr); |
| 1343 break; | 2158 break; |
| 1344 case LDC1: | 2159 case LDC1: |
| 1345 addr = rs + se_imm16; | 2160 addr = rs + se_imm16; |
| 1346 fp_out = ReadD(addr, instr); | 2161 fp_out = ReadD(addr, instr); |
| 1347 break; | 2162 break; |
| 1348 case SWC1: | 2163 case SWC1: |
| 1349 case SDC1: | 2164 case SDC1: |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 1360 switch (op) { | 2175 switch (op) { |
| 1361 // ------------- Branch instructions | 2176 // ------------- Branch instructions |
| 1362 case BEQ: | 2177 case BEQ: |
| 1363 case BNE: | 2178 case BNE: |
| 1364 case BLEZ: | 2179 case BLEZ: |
| 1365 case BGTZ: | 2180 case BGTZ: |
| 1366 // Branch instructions common part. | 2181 // Branch instructions common part. |
| 1367 execute_branch_delay_instruction = true; | 2182 execute_branch_delay_instruction = true; |
| 1368 // Set next_pc | 2183 // Set next_pc |
| 1369 if (do_branch) { | 2184 if (do_branch) { |
| 1370 next_pc = current_pc + (imm16 << 2) + Instruction::kInstructionSize; | 2185 next_pc = current_pc + (imm16 << 2) + Instruction::kInstrSize; |
| 1371 if (instr->IsLinkingInstruction()) { | 2186 if (instr->IsLinkingInstruction()) { |
| 1372 set_register(31, current_pc + 2* Instruction::kInstructionSize); | 2187 set_register(31, current_pc + 2* Instruction::kInstrSize); |
| 1373 } | 2188 } |
| 1374 } else { | 2189 } else { |
| 1375 next_pc = current_pc + 2 * Instruction::kInstructionSize; | 2190 next_pc = current_pc + 2 * Instruction::kInstrSize; |
| 1376 } | 2191 } |
| 1377 break; | 2192 break; |
| 1378 // ------------- Arithmetic instructions | 2193 // ------------- Arithmetic instructions |
| 1379 case ADDI: | 2194 case ADDI: |
| 1380 case ADDIU: | 2195 case ADDIU: |
| 1381 case SLTI: | 2196 case SLTI: |
| 1382 case SLTIU: | 2197 case SLTIU: |
| 1383 case ANDI: | 2198 case ANDI: |
| 1384 case ORI: | 2199 case ORI: |
| 1385 case XORI: | 2200 case XORI: |
| 1386 case LUI: | 2201 case LUI: |
| 1387 set_register(rt_reg, alu_out); | 2202 set_register(rt_reg, alu_out); |
| 1388 break; | 2203 break; |
| 1389 // ------------- Memory instructions | 2204 // ------------- Memory instructions |
| 1390 case LB: | 2205 case LB: |
| 2206 case LH: | |
| 2207 case LWL: | |
| 1391 case LW: | 2208 case LW: |
| 1392 case LBU: | 2209 case LBU: |
| 2210 case LHU: | |
| 2211 case LWR: | |
| 1393 set_register(rt_reg, alu_out); | 2212 set_register(rt_reg, alu_out); |
| 1394 break; | 2213 break; |
| 1395 case SB: | 2214 case SB: |
| 1396 WriteB(addr, static_cast<int8_t>(rt)); | 2215 WriteB(addr, static_cast<int8_t>(rt)); |
| 1397 break; | 2216 break; |
| 2217 case SH: | |
| 2218 WriteH(addr, static_cast<uint16_t>(rt), instr); | |
| 2219 break; | |
| 2220 case SWL: | |
| 2221 WriteW(addr, mem_value, instr); | |
| 2222 break; | |
| 1398 case SW: | 2223 case SW: |
| 1399 WriteW(addr, rt, instr); | 2224 WriteW(addr, rt, instr); |
| 1400 break; | 2225 break; |
| 2226 case SWR: | |
| 2227 WriteW(addr, mem_value, instr); | |
| 2228 break; | |
| 1401 case LWC1: | 2229 case LWC1: |
| 1402 set_fpu_register(ft_reg, alu_out); | 2230 set_fpu_register(ft_reg, alu_out); |
| 1403 break; | 2231 break; |
| 1404 case LDC1: | 2232 case LDC1: |
| 1405 set_fpu_register_double(ft_reg, fp_out); | 2233 set_fpu_register_double(ft_reg, fp_out); |
| 1406 break; | 2234 break; |
| 1407 case SWC1: | 2235 case SWC1: |
| 1408 addr = rs + se_imm16; | 2236 addr = rs + se_imm16; |
| 1409 WriteW(addr, get_fpu_register(ft_reg), instr); | 2237 WriteW(addr, get_fpu_register(ft_reg), instr); |
| 1410 break; | 2238 break; |
| 1411 case SDC1: | 2239 case SDC1: |
| 1412 addr = rs + se_imm16; | 2240 addr = rs + se_imm16; |
| 1413 WriteD(addr, ft, instr); | 2241 WriteD(addr, get_fpu_register_double(ft_reg), instr); |
| 1414 break; | 2242 break; |
| 1415 default: | 2243 default: |
| 1416 break; | 2244 break; |
| 1417 }; | 2245 }; |
| 1418 | 2246 |
| 1419 | 2247 |
| 1420 if (execute_branch_delay_instruction) { | 2248 if (execute_branch_delay_instruction) { |
| 1421 // Execute branch delay slot | 2249 // Execute branch delay slot |
| 1422 // We don't check for end_sim_pc. First it should not be met as the current | 2250 // We don't check for end_sim_pc. First it should not be met as the current |
| 1423 // pc is valid. Secondly a jump should always execute its branch delay slot. | 2251 // pc is valid. Secondly a jump should always execute its branch delay slot. |
| 1424 Instruction* branch_delay_instr = | 2252 Instruction* branch_delay_instr = |
| 1425 reinterpret_cast<Instruction*>(current_pc+Instruction::kInstructionSize); | 2253 reinterpret_cast<Instruction*>(current_pc+Instruction::kInstrSize); |
| 1426 BranchDelayInstructionDecode(branch_delay_instr); | 2254 BranchDelayInstructionDecode(branch_delay_instr); |
| 1427 } | 2255 } |
| 1428 | 2256 |
| 1429 // If needed update pc after the branch delay execution. | 2257 // If needed update pc after the branch delay execution. |
| 1430 if (next_pc != bad_ra) { | 2258 if (next_pc != bad_ra) { |
| 1431 set_pc(next_pc); | 2259 set_pc(next_pc); |
| 1432 } | 2260 } |
| 1433 } | 2261 } |
| 1434 | 2262 |
| 2263 | |
| 1435 // Type 3: instructions using a 26 bytes immediate. (eg: j, jal) | 2264 // Type 3: instructions using a 26 bytes immediate. (eg: j, jal) |
| 1436 void Simulator::DecodeTypeJump(Instruction* instr) { | 2265 void Simulator::DecodeTypeJump(Instruction* instr) { |
| 1437 // Get current pc. | 2266 // Get current pc. |
| 1438 int32_t current_pc = get_pc(); | 2267 int32_t current_pc = get_pc(); |
| 1439 // Get unchanged bits of pc. | 2268 // Get unchanged bits of pc. |
| 1440 int32_t pc_high_bits = current_pc & 0xf0000000; | 2269 int32_t pc_high_bits = current_pc & 0xf0000000; |
| 1441 // Next pc | 2270 // Next pc |
| 1442 int32_t next_pc = pc_high_bits | (instr->Imm26Field() << 2); | 2271 int32_t next_pc = pc_high_bits | (instr->Imm26Value() << 2); |
| 1443 | 2272 |
| 1444 // Execute branch delay slot | 2273 // Execute branch delay slot |
| 1445 // We don't check for end_sim_pc. First it should not be met as the current pc | 2274 // We don't check for end_sim_pc. First it should not be met as the current pc |
| 1446 // is valid. Secondly a jump should always execute its branch delay slot. | 2275 // is valid. Secondly a jump should always execute its branch delay slot. |
| 1447 Instruction* branch_delay_instr = | 2276 Instruction* branch_delay_instr = |
| 1448 reinterpret_cast<Instruction*>(current_pc+Instruction::kInstructionSize); | 2277 reinterpret_cast<Instruction*>(current_pc+Instruction::kInstrSize); |
| 1449 BranchDelayInstructionDecode(branch_delay_instr); | 2278 BranchDelayInstructionDecode(branch_delay_instr); |
| 1450 | 2279 |
| 1451 // Update pc and ra if necessary. | 2280 // Update pc and ra if necessary. |
| 1452 // Do this after the branch delay execution. | 2281 // Do this after the branch delay execution. |
| 1453 if (instr->IsLinkingInstruction()) { | 2282 if (instr->IsLinkingInstruction()) { |
| 1454 set_register(31, current_pc + 2* Instruction::kInstructionSize); | 2283 set_register(31, current_pc + 2* Instruction::kInstrSize); |
| 1455 } | 2284 } |
| 1456 set_pc(next_pc); | 2285 set_pc(next_pc); |
| 1457 pc_modified_ = true; | 2286 pc_modified_ = true; |
| 1458 } | 2287 } |
| 1459 | 2288 |
| 2289 | |
| 1460 // Executes the current instruction. | 2290 // Executes the current instruction. |
| 1461 void Simulator::InstructionDecode(Instruction* instr) { | 2291 void Simulator::InstructionDecode(Instruction* instr) { |
| 2292 if (v8::internal::FLAG_check_icache) { | |
| 2293 CheckICache(isolate_->simulator_i_cache(), instr); | |
| 2294 } | |
| 1462 pc_modified_ = false; | 2295 pc_modified_ = false; |
| 1463 if (::v8::internal::FLAG_trace_sim) { | 2296 if (::v8::internal::FLAG_trace_sim) { |
| 1464 disasm::NameConverter converter; | 2297 disasm::NameConverter converter; |
| 1465 disasm::Disassembler dasm(converter); | 2298 disasm::Disassembler dasm(converter); |
| 1466 // use a reasonably large buffer | 2299 // use a reasonably large buffer |
| 1467 v8::internal::EmbeddedVector<char, 256> buffer; | 2300 v8::internal::EmbeddedVector<char, 256> buffer; |
| 1468 dasm.InstructionDecode(buffer, | 2301 dasm.InstructionDecode(buffer, |
| 1469 reinterpret_cast<byte_*>(instr)); | 2302 reinterpret_cast<byte_*>(instr)); |
| 1470 PrintF(" 0x%08x %s\n", instr, buffer.start()); | 2303 PrintF(" 0x%08x %s\n", reinterpret_cast<intptr_t>(instr), |
|
Søren Thygesen Gjesse
2011/03/24 22:11:19
Indentation.
Paul Lind
2011/03/26 18:39:17
Done.
| |
| 2304 buffer.start()); | |
| 1471 } | 2305 } |
| 1472 | 2306 |
| 1473 switch (instr->InstructionType()) { | 2307 switch (instr->InstructionType()) { |
| 1474 case Instruction::kRegisterType: | 2308 case Instruction::kRegisterType: |
| 1475 DecodeTypeRegister(instr); | 2309 DecodeTypeRegister(instr); |
| 1476 break; | 2310 break; |
| 1477 case Instruction::kImmediateType: | 2311 case Instruction::kImmediateType: |
| 1478 DecodeTypeImmediate(instr); | 2312 DecodeTypeImmediate(instr); |
| 1479 break; | 2313 break; |
| 1480 case Instruction::kJumpType: | 2314 case Instruction::kJumpType: |
| 1481 DecodeTypeJump(instr); | 2315 DecodeTypeJump(instr); |
| 1482 break; | 2316 break; |
| 1483 default: | 2317 default: |
| 1484 UNSUPPORTED(); | 2318 UNSUPPORTED(); |
| 1485 } | 2319 } |
| 1486 if (!pc_modified_) { | 2320 if (!pc_modified_) { |
| 1487 set_register(pc, reinterpret_cast<int32_t>(instr) + | 2321 set_register(pc, reinterpret_cast<int32_t>(instr) + |
| 1488 Instruction::kInstructionSize); | 2322 Instruction::kInstrSize); |
| 1489 } | 2323 } |
| 1490 } | 2324 } |
| 1491 | 2325 |
| 1492 | 2326 |
| 1493 | 2327 |
| 1494 void Simulator::Execute() { | 2328 void Simulator::Execute() { |
| 1495 // Get the PC to simulate. Cannot use the accessor here as we need the | 2329 // Get the PC to simulate. Cannot use the accessor here as we need the |
| 1496 // raw PC value and not the one used as input to arithmetic instructions. | 2330 // raw PC value and not the one used as input to arithmetic instructions. |
| 1497 int program_counter = get_pc(); | 2331 int program_counter = get_pc(); |
| 1498 if (::v8::internal::FLAG_stop_sim_at == 0) { | 2332 if (::v8::internal::FLAG_stop_sim_at == 0) { |
| 1499 // Fast version of the dispatch loop without checking whether the simulator | 2333 // Fast version of the dispatch loop without checking whether the simulator |
| 1500 // should be stopping at a particular executed instruction. | 2334 // should be stopping at a particular executed instruction. |
| 1501 while (program_counter != end_sim_pc) { | 2335 while (program_counter != end_sim_pc) { |
| 1502 Instruction* instr = reinterpret_cast<Instruction*>(program_counter); | 2336 Instruction* instr = reinterpret_cast<Instruction*>(program_counter); |
| 1503 icount_++; | 2337 icount_++; |
| 1504 InstructionDecode(instr); | 2338 InstructionDecode(instr); |
| 1505 program_counter = get_pc(); | 2339 program_counter = get_pc(); |
| 1506 } | 2340 } |
| 1507 } else { | 2341 } else { |
| 1508 // FLAG_stop_sim_at is at the non-default value. Stop in the debugger when | 2342 // FLAG_stop_sim_at is at the non-default value. Stop in the debugger when |
| 1509 // we reach the particular instuction count. | 2343 // we reach the particular instuction count. |
| 1510 while (program_counter != end_sim_pc) { | 2344 while (program_counter != end_sim_pc) { |
| 1511 Instruction* instr = reinterpret_cast<Instruction*>(program_counter); | 2345 Instruction* instr = reinterpret_cast<Instruction*>(program_counter); |
| 1512 icount_++; | 2346 icount_++; |
| 1513 if (icount_ == ::v8::internal::FLAG_stop_sim_at) { | 2347 if (icount_ == ::v8::internal::FLAG_stop_sim_at) { |
| 1514 Debugger dbg(this); | 2348 MipsDebugger dbg(this); |
| 1515 dbg.Debug(); | 2349 dbg.Debug(); |
| 1516 } else { | 2350 } else { |
| 1517 InstructionDecode(instr); | 2351 InstructionDecode(instr); |
| 1518 } | 2352 } |
| 1519 program_counter = get_pc(); | 2353 program_counter = get_pc(); |
| 1520 } | 2354 } |
| 1521 } | 2355 } |
| 1522 } | 2356 } |
| 1523 | 2357 |
| 1524 | 2358 |
| 1525 int32_t Simulator::Call(byte_* entry, int argument_count, ...) { | 2359 int32_t Simulator::Call(byte_* entry, int argument_count, ...) { |
| 1526 va_list parameters; | 2360 va_list parameters; |
| 1527 va_start(parameters, argument_count); | 2361 va_start(parameters, argument_count); |
| 1528 // Setup arguments | 2362 // Setup arguments |
| 1529 | 2363 |
| 1530 // First four arguments passed in registers. | 2364 // First four arguments passed in registers. |
| 1531 ASSERT(argument_count >= 4); | 2365 ASSERT(argument_count >= 4); |
| 1532 set_register(a0, va_arg(parameters, int32_t)); | 2366 set_register(a0, va_arg(parameters, int32_t)); |
| 1533 set_register(a1, va_arg(parameters, int32_t)); | 2367 set_register(a1, va_arg(parameters, int32_t)); |
| 1534 set_register(a2, va_arg(parameters, int32_t)); | 2368 set_register(a2, va_arg(parameters, int32_t)); |
| 1535 set_register(a3, va_arg(parameters, int32_t)); | 2369 set_register(a3, va_arg(parameters, int32_t)); |
| 1536 | 2370 |
| 1537 // Remaining arguments passed on stack. | 2371 // Remaining arguments passed on stack. |
| 1538 int original_stack = get_register(sp); | 2372 int original_stack = get_register(sp); |
| 1539 // Compute position of stack on entry to generated code. | 2373 // Compute position of stack on entry to generated code. |
| 1540 int entry_stack = (original_stack - (argument_count - 4) * sizeof(int32_t) | 2374 int entry_stack = (original_stack - (argument_count - 4) * sizeof(int32_t) |
| 1541 - kArgsSlotsSize); | 2375 - kCArgsSlotsSize); |
| 1542 if (OS::ActivationFrameAlignment() != 0) { | 2376 if (OS::ActivationFrameAlignment() != 0) { |
| 1543 entry_stack &= -OS::ActivationFrameAlignment(); | 2377 entry_stack &= -OS::ActivationFrameAlignment(); |
| 1544 } | 2378 } |
| 1545 // Store remaining arguments on stack, from low to high memory. | 2379 // Store remaining arguments on stack, from low to high memory. |
| 1546 intptr_t* stack_argument = reinterpret_cast<intptr_t*>(entry_stack); | 2380 intptr_t* stack_argument = reinterpret_cast<intptr_t*>(entry_stack); |
| 1547 for (int i = 4; i < argument_count; i++) { | 2381 for (int i = 4; i < argument_count; i++) { |
| 1548 stack_argument[i - 4 + kArgsSlotsNum] = va_arg(parameters, int32_t); | 2382 stack_argument[i - 4 + kArgsSlotsNum] = va_arg(parameters, int32_t); |
| 1549 } | 2383 } |
| 1550 va_end(parameters); | 2384 va_end(parameters); |
| 1551 set_register(sp, entry_stack); | 2385 set_register(sp, entry_stack); |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1636 int current_sp = get_register(sp); | 2470 int current_sp = get_register(sp); |
| 1637 uintptr_t* stack_slot = reinterpret_cast<uintptr_t*>(current_sp); | 2471 uintptr_t* stack_slot = reinterpret_cast<uintptr_t*>(current_sp); |
| 1638 uintptr_t address = *stack_slot; | 2472 uintptr_t address = *stack_slot; |
| 1639 set_register(sp, current_sp + sizeof(uintptr_t)); | 2473 set_register(sp, current_sp + sizeof(uintptr_t)); |
| 1640 return address; | 2474 return address; |
| 1641 } | 2475 } |
| 1642 | 2476 |
| 1643 | 2477 |
| 1644 #undef UNSUPPORTED | 2478 #undef UNSUPPORTED |
| 1645 | 2479 |
| 1646 } } // namespace assembler::mips | 2480 } } // namespace v8::internal |
| 1647 | 2481 |
| 1648 #endif // !__mips || USE_SIMULATOR | 2482 #endif // USE_SIMULATOR |
| 1649 | 2483 |
| 1650 #endif // V8_TARGET_ARCH_MIPS | 2484 #endif // V8_TARGET_ARCH_MIPS |
| OLD | NEW |