Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #include "vm/globals.h" | 5 #include "vm/globals.h" |
| 6 #if defined(TARGET_ARCH_MIPS) | 6 #if defined(TARGET_ARCH_MIPS) |
| 7 | 7 |
| 8 // Only build the simulator if not compiling for real MIPS hardware. | 8 // Only build the simulator if not compiling for real MIPS hardware. |
| 9 #if !defined(HOST_ARCH_MIPS) | 9 #if !defined(HOST_ARCH_MIPS) |
| 10 | 10 |
| 11 #include "vm/simulator.h" | 11 #include "vm/simulator.h" |
| 12 | 12 |
| 13 #include "vm/assembler.h" | 13 #include "vm/assembler.h" |
| 14 #include "vm/constants_mips.h" | 14 #include "vm/constants_mips.h" |
| 15 #include "vm/disassembler.h" | 15 #include "vm/disassembler.h" |
| 16 | 16 |
| 17 namespace dart { | 17 namespace dart { |
| 18 | 18 |
| 19 DEFINE_FLAG(int, stop_sim_at, 0, "Address to stop simulator at."); | |
| 20 | |
| 21 void Simulator::InitOnce() { | |
| 22 } | |
| 23 | |
| 24 | |
| 19 Simulator::Simulator() { | 25 Simulator::Simulator() { |
| 20 // Setup simulator support first. Some of this information is needed to | 26 // Setup simulator support first. Some of this information is needed to |
| 21 // setup the architecture state. | 27 // setup the architecture state. |
| 22 // We allocate the stack here, the size is computed as the sum of | 28 // We allocate the stack here, the size is computed as the sum of |
| 23 // the size specified by the user and the buffer space needed for | 29 // the size specified by the user and the buffer space needed for |
| 24 // handling stack overflow exceptions. To be safe in potential | 30 // handling stack overflow exceptions. To be safe in potential |
| 25 // stack underflows we also add some underflow buffer space. | 31 // stack underflows we also add some underflow buffer space. |
| 26 stack_ = new char[(Isolate::GetSpecifiedStackSize() + | 32 stack_ = new char[(Isolate::GetSpecifiedStackSize() + |
| 27 Isolate::kStackSizeBuffer + | 33 Isolate::kStackSizeBuffer + |
| 28 kSimulatorStackUnderflowSize)]; | 34 kSimulatorStackUnderflowSize)]; |
| 35 icount_ = 0; | |
| 36 delay_slot_ = false; | |
| 37 | |
| 38 // Setup architecture state. | |
| 39 // All registers are initialized to zero to start with. | |
| 40 for (int i = 0; i < kNumberOfCpuRegisters; i++) { | |
| 41 registers_[i] = 0; | |
| 42 } | |
| 43 pc_ = 0; | |
| 44 // The sp is initialized to point to the bottom (high address) of the | |
| 45 // allocated stack area. | |
| 46 registers_[SP] = StackTop(); | |
| 29 } | 47 } |
| 30 | 48 |
| 31 | 49 |
| 32 Simulator::~Simulator() { | 50 Simulator::~Simulator() { |
| 33 Isolate* isolate = Isolate::Current(); | 51 Isolate* isolate = Isolate::Current(); |
| 34 if (isolate != NULL) { | 52 if (isolate != NULL) { |
| 35 isolate->set_simulator(NULL); | 53 isolate->set_simulator(NULL); |
| 36 } | 54 } |
| 37 } | 55 } |
| 38 | 56 |
| 39 | 57 |
| 40 // Get the active Simulator for the current isolate. | 58 // Get the active Simulator for the current isolate. |
| 41 Simulator* Simulator::Current() { | 59 Simulator* Simulator::Current() { |
| 42 Simulator* simulator = Isolate::Current()->simulator(); | 60 Simulator* simulator = Isolate::Current()->simulator(); |
| 43 if (simulator == NULL) { | 61 if (simulator == NULL) { |
| 44 simulator = new Simulator(); | 62 simulator = new Simulator(); |
| 45 Isolate::Current()->set_simulator(simulator); | 63 Isolate::Current()->set_simulator(simulator); |
| 46 } | 64 } |
| 47 return simulator; | 65 return simulator; |
| 48 } | 66 } |
| 49 | 67 |
| 50 | 68 |
| 51 // Sets the register in the architecture state. It will also deal with updating | 69 // Sets the register in the architecture state. It will also deal with updating |
| 52 // Simulator internal state for special registers such as PC. | 70 // Simulator internal state for special registers such as PC. |
| 53 void Simulator::set_register(Register reg, int32_t value) { | 71 void Simulator::set_register(Register reg, int32_t value) { |
| 54 UNIMPLEMENTED(); | 72 if (reg != R0) { |
| 73 registers_[reg] = value; | |
| 74 } | |
| 55 } | 75 } |
| 56 | 76 |
| 57 | 77 |
| 58 // Get the register from the architecture state. This function does handle | 78 // Get the register from the architecture state. This function does handle |
| 59 // the special case of accessing the PC register. | 79 // the special case of accessing the PC register. |
| 60 int32_t Simulator::get_register(Register reg) const { | 80 int32_t Simulator::get_register(Register reg) const { |
| 61 UNIMPLEMENTED(); | 81 if (reg == R0) { |
| 62 return 0; | 82 return 0; |
| 83 } | |
| 84 return registers_[reg]; | |
| 63 } | 85 } |
| 64 | 86 |
| 65 | 87 |
| 66 // Returns the top of the stack area to enable checking for stack pointer | 88 // Returns the top of the stack area to enable checking for stack pointer |
| 67 // validity. | 89 // validity. |
| 68 uword Simulator::StackTop() const { | 90 uword Simulator::StackTop() const { |
| 69 // To be safe in potential stack underflows we leave some buffer above and | 91 // To be safe in potential stack underflows we leave some buffer above and |
| 70 // set the stack top. | 92 // set the stack top. |
| 71 return reinterpret_cast<uword>(stack_) + | 93 return reinterpret_cast<uword>(stack_) + |
| 72 (Isolate::GetSpecifiedStackSize() + Isolate::kStackSizeBuffer); | 94 (Isolate::GetSpecifiedStackSize() + Isolate::kStackSizeBuffer); |
| 73 } | 95 } |
| 74 | 96 |
| 75 | 97 |
| 76 void Simulator::InitOnce() { | 98 void Simulator::Format(Instr* instr, const char* format) { |
| 99 OS::PrintErr("Simulator - unknown instruction: %s\n", format); | |
| 100 UNIMPLEMENTED(); | |
| 101 } | |
| 102 | |
| 103 void Simulator::DecodeSpecial(Instr* instr) { | |
| 104 switch (instr->FunctionField()) { | |
| 105 case SLL: { | |
| 106 if ((instr->RdField() == R0) && | |
| 107 (instr->RtField() == R0) && | |
| 108 (instr->SaField() == 0)) { | |
| 109 // Format(instr, "nop"); | |
| 110 // Nothing to be done for NOP. | |
| 111 } else { | |
| 112 Format(instr, "sll 'rd, 'rt, 'sa"); | |
| 113 } | |
| 114 break; | |
| 115 } | |
| 116 case JR: { | |
| 117 ASSERT(instr->RtField() == R0); | |
| 118 ASSERT(instr->RdField() == R0); | |
| 119 ASSERT(!delay_slot_); | |
| 120 // Format(instr, "jr'hint 'rs"); | |
| 121 uword next_pc = get_register(instr->RsField()); | |
| 122 ExecuteDelaySlot(); | |
| 123 pc_ = next_pc - Instr::kInstrSize; // Account for regular PC increment. | |
| 124 break; | |
| 125 } | |
| 126 default: { | |
| 127 OS::PrintErr("DecodeSpecial: 0x%x\n", instr->InstructionBits()); | |
| 128 UNREACHABLE(); | |
| 129 break; | |
| 130 } | |
| 131 } | |
| 132 } | |
| 133 | |
| 134 | |
| 135 void Simulator::InstructionDecode(Instr* instr) { | |
| 136 switch (instr->OpcodeField()) { | |
| 137 case SPECIAL: { | |
| 138 DecodeSpecial(instr); | |
| 139 break; | |
| 140 } | |
| 141 case ORI: { | |
| 142 // Format(instr, "ori 'rt, 'rs, 'imm"); | |
| 143 int32_t rs_val = get_register(instr->RsField()); | |
| 144 set_register(instr->RtField(), rs_val | instr->ImmField()); | |
|
regis
2013/03/06 21:18:42
The immediate field should be zero extended, i.e.
Ivan Posva
2013/03/07 11:03:22
The result of ImmField() is already unsigned.
| |
| 145 break; | |
| 146 } | |
| 147 default: { | |
| 148 OS::PrintErr("Undecoded instruction: 0x%x\n", instr->InstructionBits()); | |
| 149 UNREACHABLE(); | |
| 150 break; | |
| 151 } | |
| 152 } | |
| 153 pc_ += Instr::kInstrSize; | |
| 154 } | |
| 155 | |
| 156 | |
| 157 void Simulator::ExecuteDelaySlot() { | |
| 158 ASSERT(pc_ != kEndSimulatingPC); | |
| 159 delay_slot_ = true; | |
| 160 icount_++; | |
| 161 if (icount_ == FLAG_stop_sim_at) { | |
| 162 UNIMPLEMENTED(); | |
| 163 } | |
| 164 Instr* instr = Instr::At(pc_ + Instr::kInstrSize); | |
| 165 InstructionDecode(instr); | |
| 166 delay_slot_ = false; | |
| 167 } | |
| 168 | |
| 169 | |
| 170 void Simulator::Execute() { | |
| 171 if (FLAG_stop_sim_at == 0) { | |
| 172 // Fast version of the dispatch loop without checking whether the simulator | |
| 173 // should be stopping at a particular executed instruction. | |
| 174 while (pc_ != kEndSimulatingPC) { | |
| 175 icount_++; | |
| 176 Instr* instr = Instr::At(pc_); | |
| 177 InstructionDecode(instr); | |
| 178 } | |
| 179 } else { | |
| 180 // FLAG_stop_sim_at is at the non-default value. Stop in the debugger when | |
| 181 // we reach the particular instruction count. | |
| 182 while (pc_ != kEndSimulatingPC) { | |
| 183 icount_++; | |
| 184 if (icount_ == FLAG_stop_sim_at) { | |
| 185 UNIMPLEMENTED(); | |
| 186 } else { | |
| 187 Instr* instr = Instr::At(pc_); | |
| 188 InstructionDecode(instr); | |
| 189 } | |
| 190 } | |
| 191 } | |
| 77 } | 192 } |
| 78 | 193 |
| 79 | 194 |
| 80 int64_t Simulator::Call(int32_t entry, | 195 int64_t Simulator::Call(int32_t entry, |
| 81 int32_t parameter0, | 196 int32_t parameter0, |
| 82 int32_t parameter1, | 197 int32_t parameter1, |
| 83 int32_t parameter2, | 198 int32_t parameter2, |
| 84 int32_t parameter3, | 199 int32_t parameter3) { |
| 85 int32_t parameter4) { | 200 // Save the SP register before the call so we can restore it. |
| 86 UNIMPLEMENTED(); | 201 int32_t sp_before_call = get_register(SP); |
| 87 return 0LL; | 202 |
| 203 // Setup parameters. | |
| 204 set_register(A0, parameter0); | |
| 205 set_register(A1, parameter1); | |
| 206 set_register(A2, parameter2); | |
| 207 set_register(A3, parameter3); | |
| 208 | |
| 209 // Make sure the activation frames are properly aligned. | |
| 210 int32_t stack_pointer = sp_before_call; | |
| 211 static const int kFrameAlignment = OS::ActivationFrameAlignment(); | |
| 212 if (kFrameAlignment > 0) { | |
| 213 stack_pointer = Utils::RoundDown(stack_pointer, kFrameAlignment); | |
| 214 } | |
| 215 set_register(SP, stack_pointer); | |
| 216 | |
| 217 // Prepare to execute the code at entry. | |
| 218 set_pc(entry); | |
| 219 // Put down marker for end of simulation. The simulator will stop simulation | |
| 220 // when the PC reaches this value. By saving the "end simulation" value into | |
| 221 // RA the simulation stops when returning to this call point. | |
| 222 set_register(RA, kEndSimulatingPC); | |
| 223 | |
| 224 // Remember the values of callee-saved registers. | |
| 225 // The code below assumes that r9 is not used as sb (static base) in | |
| 226 // simulator code and therefore is regarded as a callee-saved register. | |
| 227 int32_t r16_val = get_register(R16); | |
| 228 int32_t r17_val = get_register(R17); | |
| 229 int32_t r18_val = get_register(R18); | |
| 230 int32_t r19_val = get_register(R19); | |
| 231 int32_t r20_val = get_register(R20); | |
| 232 int32_t r21_val = get_register(R21); | |
| 233 int32_t r22_val = get_register(R22); | |
| 234 int32_t r23_val = get_register(R23); | |
| 235 | |
| 236 // Setup the callee-saved registers with a known value. To be able to check | |
| 237 // that they are preserved properly across dart execution. | |
| 238 int32_t callee_saved_value = icount_; | |
| 239 set_register(R16, callee_saved_value); | |
| 240 set_register(R17, callee_saved_value); | |
| 241 set_register(R18, callee_saved_value); | |
| 242 set_register(R19, callee_saved_value); | |
| 243 set_register(R20, callee_saved_value); | |
| 244 set_register(R21, callee_saved_value); | |
| 245 set_register(R22, callee_saved_value); | |
| 246 set_register(R23, callee_saved_value); | |
| 247 | |
| 248 // Start the simulation | |
| 249 Execute(); | |
| 250 | |
| 251 // Check that the callee-saved registers have been preserved. | |
| 252 ASSERT(callee_saved_value == get_register(R16)); | |
| 253 ASSERT(callee_saved_value == get_register(R17)); | |
| 254 ASSERT(callee_saved_value == get_register(R18)); | |
| 255 ASSERT(callee_saved_value == get_register(R19)); | |
| 256 ASSERT(callee_saved_value == get_register(R20)); | |
| 257 ASSERT(callee_saved_value == get_register(R21)); | |
| 258 ASSERT(callee_saved_value == get_register(R22)); | |
| 259 ASSERT(callee_saved_value == get_register(R23)); | |
| 260 | |
| 261 // Restore callee-saved registers with the original value. | |
| 262 set_register(R16, r16_val); | |
| 263 set_register(R17, r17_val); | |
| 264 set_register(R18, r18_val); | |
| 265 set_register(R19, r19_val); | |
| 266 set_register(R20, r20_val); | |
| 267 set_register(R21, r21_val); | |
| 268 set_register(R22, r22_val); | |
| 269 set_register(R23, r23_val); | |
| 270 | |
| 271 // Restore the SP register and return R1:R0. | |
| 272 set_register(SP, sp_before_call); | |
| 273 return Utils::LowHighTo64Bits(get_register(V0), get_register(V1)); | |
| 88 } | 274 } |
| 89 | 275 |
| 90 } // namespace dart | 276 } // namespace dart |
| 91 | 277 |
| 92 #endif // !defined(HOST_ARCH_MIPS) | 278 #endif // !defined(HOST_ARCH_MIPS) |
| 93 | 279 |
| 94 #endif // defined TARGET_ARCH_MIPS | 280 #endif // defined TARGET_ARCH_MIPS |
| OLD | NEW |