blob: e34c3116e6d0d41debcc4c6c8a8bb3e5dd3fa6cd [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// Copyright 2012 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Steve Blocka7e24c12009-10-30 11:49:00 +00004
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005#include <stdarg.h>
Steve Blocka7e24c12009-10-30 11:49:00 +00006#include <stdlib.h>
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007#include <cmath>
Steve Blocka7e24c12009-10-30 11:49:00 +00008
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009#include "src/v8.h"
Leon Clarkef7060e22010-06-03 12:02:55 +010010
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011#if V8_TARGET_ARCH_ARM
12
13#include "src/arm/constants-arm.h"
14#include "src/arm/simulator-arm.h"
15#include "src/assembler.h"
16#include "src/codegen.h"
17#include "src/disasm.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000018
John Reck59135872010-11-02 12:39:01 -070019#if defined(USE_SIMULATOR)
Steve Blocka7e24c12009-10-30 11:49:00 +000020
21// Only build the simulator if not compiling for real ARM hardware.
Steve Block1e0659c2011-05-24 12:43:12 +010022namespace v8 {
23namespace internal {
Steve Blocka7e24c12009-10-30 11:49:00 +000024
25// This macro provides a platform independent use of sscanf. The reason for
Leon Clarked91b9f72010-01-27 17:25:45 +000026// SScanF not being implemented in a platform independent way through
27// ::v8::internal::OS in the same way as SNPrintF is that the
28// Windows C Run-Time Library does not provide vsscanf.
Steve Blocka7e24c12009-10-30 11:49:00 +000029#define SScanF sscanf // NOLINT
30
Steve Block44f0eee2011-05-26 01:26:41 +010031// The ArmDebugger class is used by the simulator while debugging simulated ARM
Steve Blocka7e24c12009-10-30 11:49:00 +000032// code.
Steve Block44f0eee2011-05-26 01:26:41 +010033class ArmDebugger {
Steve Blocka7e24c12009-10-30 11:49:00 +000034 public:
Ben Murdoch3ef787d2012-04-12 10:51:47 +010035 explicit ArmDebugger(Simulator* sim) : sim_(sim) { }
Steve Block44f0eee2011-05-26 01:26:41 +010036 ~ArmDebugger();
Steve Blocka7e24c12009-10-30 11:49:00 +000037
Steve Block1e0659c2011-05-24 12:43:12 +010038 void Stop(Instruction* instr);
Steve Blocka7e24c12009-10-30 11:49:00 +000039 void Debug();
40
41 private:
Steve Block1e0659c2011-05-24 12:43:12 +010042 static const Instr kBreakpointInstr =
43 (al | (7*B25) | (1*B24) | kBreakpoint);
44 static const Instr kNopInstr = (al | (13*B21));
Steve Blocka7e24c12009-10-30 11:49:00 +000045
46 Simulator* sim_;
47
48 int32_t GetRegisterValue(int regnum);
Ben Murdoch8b112d22011-06-08 16:22:53 +010049 double GetRegisterPairDoubleValue(int regnum);
Ben Murdochb0fe1622011-05-05 13:52:32 +010050 double GetVFPDoubleRegisterValue(int regnum);
Steve Blocka7e24c12009-10-30 11:49:00 +000051 bool GetValue(const char* desc, int32_t* value);
Steve Block6ded16b2010-05-10 14:33:55 +010052 bool GetVFPSingleValue(const char* desc, float* value);
53 bool GetVFPDoubleValue(const char* desc, double* value);
Steve Blocka7e24c12009-10-30 11:49:00 +000054
55 // Set or delete a breakpoint. Returns true if successful.
Steve Block1e0659c2011-05-24 12:43:12 +010056 bool SetBreakpoint(Instruction* breakpc);
57 bool DeleteBreakpoint(Instruction* breakpc);
Steve Blocka7e24c12009-10-30 11:49:00 +000058
59 // Undo and redo all breakpoints. This is needed to bracket disassembly and
60 // execution to skip past breakpoints when run from the debugger.
61 void UndoBreakpoints();
62 void RedoBreakpoints();
63};
64
65
Steve Block44f0eee2011-05-26 01:26:41 +010066ArmDebugger::~ArmDebugger() {
Steve Blocka7e24c12009-10-30 11:49:00 +000067}
68
69
70
71#ifdef GENERATED_CODE_COVERAGE
72static FILE* coverage_log = NULL;
73
74
75static void InitializeCoverage() {
76 char* file_name = getenv("V8_GENERATED_CODE_COVERAGE_LOG");
77 if (file_name != NULL) {
78 coverage_log = fopen(file_name, "aw+");
79 }
80}
81
82
Steve Block44f0eee2011-05-26 01:26:41 +010083void ArmDebugger::Stop(Instruction* instr) {
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -080084 // Get the stop code.
Steve Block1e0659c2011-05-24 12:43:12 +010085 uint32_t code = instr->SvcValue() & kStopCodeMask;
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -080086 // Retrieve the encoded address, which comes just after this stop.
87 char** msg_address =
Steve Block1e0659c2011-05-24 12:43:12 +010088 reinterpret_cast<char**>(sim_->get_pc() + Instruction::kInstrSize);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -080089 char* msg = *msg_address;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000090 DCHECK(msg != NULL);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -080091
92 // Update this stop description.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000093 if (isWatchedStop(code) && !watched_stops_[code].desc) {
94 watched_stops_[code].desc = msg;
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -080095 }
96
97 if (strlen(msg) > 0) {
Steve Blocka7e24c12009-10-30 11:49:00 +000098 if (coverage_log != NULL) {
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -080099 fprintf(coverage_log, "%s\n", msg);
Steve Blocka7e24c12009-10-30 11:49:00 +0000100 fflush(coverage_log);
101 }
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800102 // Overwrite the instruction and address with nops.
103 instr->SetInstructionBits(kNopInstr);
Steve Block1e0659c2011-05-24 12:43:12 +0100104 reinterpret_cast<Instruction*>(msg_address)->SetInstructionBits(kNopInstr);
Steve Blocka7e24c12009-10-30 11:49:00 +0000105 }
Steve Block1e0659c2011-05-24 12:43:12 +0100106 sim_->set_pc(sim_->get_pc() + 2 * Instruction::kInstrSize);
Steve Blocka7e24c12009-10-30 11:49:00 +0000107}
108
109#else // ndef GENERATED_CODE_COVERAGE
110
111static void InitializeCoverage() {
112}
113
114
Steve Block44f0eee2011-05-26 01:26:41 +0100115void ArmDebugger::Stop(Instruction* instr) {
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800116 // Get the stop code.
Steve Block1e0659c2011-05-24 12:43:12 +0100117 uint32_t code = instr->SvcValue() & kStopCodeMask;
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800118 // Retrieve the encoded address, which comes just after this stop.
Steve Block1e0659c2011-05-24 12:43:12 +0100119 char* msg = *reinterpret_cast<char**>(sim_->get_pc()
120 + Instruction::kInstrSize);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800121 // Update this stop description.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000122 if (sim_->isWatchedStop(code) && !sim_->watched_stops_[code].desc) {
123 sim_->watched_stops_[code].desc = msg;
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800124 }
Steve Block1e0659c2011-05-24 12:43:12 +0100125 // Print the stop message and code if it is not the default code.
126 if (code != kMaxStopCode) {
127 PrintF("Simulator hit stop %u: %s\n", code, msg);
128 } else {
129 PrintF("Simulator hit %s\n", msg);
130 }
131 sim_->set_pc(sim_->get_pc() + 2 * Instruction::kInstrSize);
Steve Blocka7e24c12009-10-30 11:49:00 +0000132 Debug();
133}
134#endif
135
136
Steve Block44f0eee2011-05-26 01:26:41 +0100137int32_t ArmDebugger::GetRegisterValue(int regnum) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000138 if (regnum == kPCRegister) {
139 return sim_->get_pc();
140 } else {
141 return sim_->get_register(regnum);
142 }
143}
144
145
Ben Murdoch8b112d22011-06-08 16:22:53 +0100146double ArmDebugger::GetRegisterPairDoubleValue(int regnum) {
147 return sim_->get_double_from_register_pair(regnum);
148}
149
150
Steve Block44f0eee2011-05-26 01:26:41 +0100151double ArmDebugger::GetVFPDoubleRegisterValue(int regnum) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100152 return sim_->get_double_from_d_register(regnum);
153}
154
155
Steve Block44f0eee2011-05-26 01:26:41 +0100156bool ArmDebugger::GetValue(const char* desc, int32_t* value) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000157 int regnum = Registers::Number(desc);
158 if (regnum != kNoRegister) {
159 *value = GetRegisterValue(regnum);
160 return true;
161 } else {
Steve Block6ded16b2010-05-10 14:33:55 +0100162 if (strncmp(desc, "0x", 2) == 0) {
163 return SScanF(desc + 2, "%x", reinterpret_cast<uint32_t*>(value)) == 1;
164 } else {
165 return SScanF(desc, "%u", reinterpret_cast<uint32_t*>(value)) == 1;
166 }
167 }
168 return false;
169}
170
171
Steve Block44f0eee2011-05-26 01:26:41 +0100172bool ArmDebugger::GetVFPSingleValue(const char* desc, float* value) {
Steve Block6ded16b2010-05-10 14:33:55 +0100173 bool is_double;
174 int regnum = VFPRegisters::Number(desc, &is_double);
175 if (regnum != kNoRegister && !is_double) {
176 *value = sim_->get_float_from_s_register(regnum);
177 return true;
178 }
179 return false;
180}
181
182
Steve Block44f0eee2011-05-26 01:26:41 +0100183bool ArmDebugger::GetVFPDoubleValue(const char* desc, double* value) {
Steve Block6ded16b2010-05-10 14:33:55 +0100184 bool is_double;
185 int regnum = VFPRegisters::Number(desc, &is_double);
186 if (regnum != kNoRegister && is_double) {
187 *value = sim_->get_double_from_d_register(regnum);
188 return true;
Steve Blocka7e24c12009-10-30 11:49:00 +0000189 }
190 return false;
191}
192
193
Steve Block44f0eee2011-05-26 01:26:41 +0100194bool ArmDebugger::SetBreakpoint(Instruction* breakpc) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000195 // Check if a breakpoint can be set. If not return without any side-effects.
196 if (sim_->break_pc_ != NULL) {
197 return false;
198 }
199
200 // Set the breakpoint.
201 sim_->break_pc_ = breakpc;
202 sim_->break_instr_ = breakpc->InstructionBits();
203 // Not setting the breakpoint instruction in the code itself. It will be set
204 // when the debugger shell continues.
205 return true;
206}
207
208
Steve Block44f0eee2011-05-26 01:26:41 +0100209bool ArmDebugger::DeleteBreakpoint(Instruction* breakpc) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000210 if (sim_->break_pc_ != NULL) {
211 sim_->break_pc_->SetInstructionBits(sim_->break_instr_);
212 }
213
214 sim_->break_pc_ = NULL;
215 sim_->break_instr_ = 0;
216 return true;
217}
218
219
Steve Block44f0eee2011-05-26 01:26:41 +0100220void ArmDebugger::UndoBreakpoints() {
Steve Blocka7e24c12009-10-30 11:49:00 +0000221 if (sim_->break_pc_ != NULL) {
222 sim_->break_pc_->SetInstructionBits(sim_->break_instr_);
223 }
224}
225
226
Steve Block44f0eee2011-05-26 01:26:41 +0100227void ArmDebugger::RedoBreakpoints() {
Steve Blocka7e24c12009-10-30 11:49:00 +0000228 if (sim_->break_pc_ != NULL) {
229 sim_->break_pc_->SetInstructionBits(kBreakpointInstr);
230 }
231}
232
233
Steve Block44f0eee2011-05-26 01:26:41 +0100234void ArmDebugger::Debug() {
Steve Blocka7e24c12009-10-30 11:49:00 +0000235 intptr_t last_pc = -1;
236 bool done = false;
237
238#define COMMAND_SIZE 63
239#define ARG_SIZE 255
240
241#define STR(a) #a
242#define XSTR(a) STR(a)
243
244 char cmd[COMMAND_SIZE + 1];
245 char arg1[ARG_SIZE + 1];
246 char arg2[ARG_SIZE + 1];
Steve Block6ded16b2010-05-10 14:33:55 +0100247 char* argv[3] = { cmd, arg1, arg2 };
Steve Blocka7e24c12009-10-30 11:49:00 +0000248
249 // make sure to have a proper terminating character if reaching the limit
250 cmd[COMMAND_SIZE] = 0;
251 arg1[ARG_SIZE] = 0;
252 arg2[ARG_SIZE] = 0;
253
254 // Undo all set breakpoints while running in the debugger shell. This will
255 // make them invisible to all commands.
256 UndoBreakpoints();
257
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000258 while (!done && !sim_->has_bad_pc()) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000259 if (last_pc != sim_->get_pc()) {
260 disasm::NameConverter converter;
261 disasm::Disassembler dasm(converter);
262 // use a reasonably large buffer
263 v8::internal::EmbeddedVector<char, 256> buffer;
264 dasm.InstructionDecode(buffer,
265 reinterpret_cast<byte*>(sim_->get_pc()));
266 PrintF(" 0x%08x %s\n", sim_->get_pc(), buffer.start());
267 last_pc = sim_->get_pc();
268 }
269 char* line = ReadLine("sim> ");
270 if (line == NULL) {
271 break;
272 } else {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100273 char* last_input = sim_->last_debugger_input();
274 if (strcmp(line, "\n") == 0 && last_input != NULL) {
275 line = last_input;
276 } else {
277 // Ownership is transferred to sim_;
278 sim_->set_last_debugger_input(line);
279 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000280 // Use sscanf to parse the individual parts of the command line. At the
281 // moment no command expects more than two parameters.
Steve Block6ded16b2010-05-10 14:33:55 +0100282 int argc = SScanF(line,
Steve Blocka7e24c12009-10-30 11:49:00 +0000283 "%" XSTR(COMMAND_SIZE) "s "
284 "%" XSTR(ARG_SIZE) "s "
285 "%" XSTR(ARG_SIZE) "s",
286 cmd, arg1, arg2);
287 if ((strcmp(cmd, "si") == 0) || (strcmp(cmd, "stepi") == 0)) {
Steve Block1e0659c2011-05-24 12:43:12 +0100288 sim_->InstructionDecode(reinterpret_cast<Instruction*>(sim_->get_pc()));
Steve Blocka7e24c12009-10-30 11:49:00 +0000289 } else if ((strcmp(cmd, "c") == 0) || (strcmp(cmd, "cont") == 0)) {
290 // Execute the one instruction we broke at with breakpoints disabled.
Steve Block1e0659c2011-05-24 12:43:12 +0100291 sim_->InstructionDecode(reinterpret_cast<Instruction*>(sim_->get_pc()));
Steve Blocka7e24c12009-10-30 11:49:00 +0000292 // Leave the debugger shell.
293 done = true;
294 } else if ((strcmp(cmd, "p") == 0) || (strcmp(cmd, "print") == 0)) {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100295 if (argc == 2 || (argc == 3 && strcmp(arg2, "fp") == 0)) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000296 int32_t value;
Steve Block6ded16b2010-05-10 14:33:55 +0100297 float svalue;
298 double dvalue;
Steve Blocka7e24c12009-10-30 11:49:00 +0000299 if (strcmp(arg1, "all") == 0) {
300 for (int i = 0; i < kNumRegisters; i++) {
301 value = GetRegisterValue(i);
Ben Murdoch8b112d22011-06-08 16:22:53 +0100302 PrintF("%3s: 0x%08x %10d", Registers::Name(i), value, value);
303 if ((argc == 3 && strcmp(arg2, "fp") == 0) &&
304 i < 8 &&
305 (i % 2) == 0) {
306 dvalue = GetRegisterPairDoubleValue(i);
307 PrintF(" (%f)\n", dvalue);
308 } else {
309 PrintF("\n");
310 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000311 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000312 for (int i = 0; i < DwVfpRegister::NumRegisters(); i++) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100313 dvalue = GetVFPDoubleRegisterValue(i);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000314 uint64_t as_words = bit_cast<uint64_t>(dvalue);
Steve Block44f0eee2011-05-26 01:26:41 +0100315 PrintF("%3s: %f 0x%08x %08x\n",
316 VFPRegisters::Name(i, true),
317 dvalue,
318 static_cast<uint32_t>(as_words >> 32),
319 static_cast<uint32_t>(as_words & 0xffffffff));
Ben Murdochb0fe1622011-05-05 13:52:32 +0100320 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000321 } else {
322 if (GetValue(arg1, &value)) {
323 PrintF("%s: 0x%08x %d \n", arg1, value, value);
Steve Block6ded16b2010-05-10 14:33:55 +0100324 } else if (GetVFPSingleValue(arg1, &svalue)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000325 uint32_t as_word = bit_cast<uint32_t>(svalue);
Steve Block44f0eee2011-05-26 01:26:41 +0100326 PrintF("%s: %f 0x%08x\n", arg1, svalue, as_word);
Steve Block6ded16b2010-05-10 14:33:55 +0100327 } else if (GetVFPDoubleValue(arg1, &dvalue)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000328 uint64_t as_words = bit_cast<uint64_t>(dvalue);
Steve Block44f0eee2011-05-26 01:26:41 +0100329 PrintF("%s: %f 0x%08x %08x\n",
330 arg1,
331 dvalue,
332 static_cast<uint32_t>(as_words >> 32),
333 static_cast<uint32_t>(as_words & 0xffffffff));
Steve Blocka7e24c12009-10-30 11:49:00 +0000334 } else {
335 PrintF("%s unrecognized\n", arg1);
336 }
337 }
338 } else {
339 PrintF("print <register>\n");
340 }
341 } else if ((strcmp(cmd, "po") == 0)
342 || (strcmp(cmd, "printobject") == 0)) {
Steve Block6ded16b2010-05-10 14:33:55 +0100343 if (argc == 2) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000344 int32_t value;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000345 OFStream os(stdout);
Steve Blocka7e24c12009-10-30 11:49:00 +0000346 if (GetValue(arg1, &value)) {
347 Object* obj = reinterpret_cast<Object*>(value);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000348 os << arg1 << ": \n";
Steve Blocka7e24c12009-10-30 11:49:00 +0000349#ifdef DEBUG
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000350 obj->Print(os);
351 os << "\n";
Steve Blocka7e24c12009-10-30 11:49:00 +0000352#else
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000353 os << Brief(obj) << "\n";
Steve Blocka7e24c12009-10-30 11:49:00 +0000354#endif
355 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000356 os << arg1 << " unrecognized\n";
Steve Blocka7e24c12009-10-30 11:49:00 +0000357 }
358 } else {
359 PrintF("printobject <value>\n");
360 }
Steve Block6ded16b2010-05-10 14:33:55 +0100361 } else if (strcmp(cmd, "stack") == 0 || strcmp(cmd, "mem") == 0) {
362 int32_t* cur = NULL;
363 int32_t* end = NULL;
364 int next_arg = 1;
365
366 if (strcmp(cmd, "stack") == 0) {
367 cur = reinterpret_cast<int32_t*>(sim_->get_register(Simulator::sp));
368 } else { // "mem"
369 int32_t value;
370 if (!GetValue(arg1, &value)) {
371 PrintF("%s unrecognized\n", arg1);
372 continue;
373 }
374 cur = reinterpret_cast<int32_t*>(value);
375 next_arg++;
376 }
377
378 int32_t words;
379 if (argc == next_arg) {
380 words = 10;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000381 } else {
Steve Block6ded16b2010-05-10 14:33:55 +0100382 if (!GetValue(argv[next_arg], &words)) {
383 words = 10;
384 }
385 }
386 end = cur + words;
387
388 while (cur < end) {
Steve Block44f0eee2011-05-26 01:26:41 +0100389 PrintF(" 0x%08x: 0x%08x %10d",
Ben Murdochf87a2032010-10-22 12:50:53 +0100390 reinterpret_cast<intptr_t>(cur), *cur, *cur);
Steve Block44f0eee2011-05-26 01:26:41 +0100391 HeapObject* obj = reinterpret_cast<HeapObject*>(*cur);
392 int value = *cur;
393 Heap* current_heap = v8::internal::Isolate::Current()->heap();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000394 if (((value & 1) == 0) || current_heap->Contains(obj)) {
Steve Block44f0eee2011-05-26 01:26:41 +0100395 PrintF(" (");
396 if ((value & 1) == 0) {
397 PrintF("smi %d", value / 2);
398 } else {
399 obj->ShortPrint();
400 }
401 PrintF(")");
402 }
403 PrintF("\n");
Steve Block6ded16b2010-05-10 14:33:55 +0100404 cur++;
405 }
Steve Block44f0eee2011-05-26 01:26:41 +0100406 } else if (strcmp(cmd, "disasm") == 0 || strcmp(cmd, "di") == 0) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000407 disasm::NameConverter converter;
408 disasm::Disassembler dasm(converter);
409 // use a reasonably large buffer
410 v8::internal::EmbeddedVector<char, 256> buffer;
411
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800412 byte* prev = NULL;
Steve Blocka7e24c12009-10-30 11:49:00 +0000413 byte* cur = NULL;
414 byte* end = NULL;
415
Steve Block6ded16b2010-05-10 14:33:55 +0100416 if (argc == 1) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000417 cur = reinterpret_cast<byte*>(sim_->get_pc());
Steve Block1e0659c2011-05-24 12:43:12 +0100418 end = cur + (10 * Instruction::kInstrSize);
Steve Block6ded16b2010-05-10 14:33:55 +0100419 } else if (argc == 2) {
Steve Block44f0eee2011-05-26 01:26:41 +0100420 int regnum = Registers::Number(arg1);
421 if (regnum != kNoRegister || strncmp(arg1, "0x", 2) == 0) {
422 // The argument is an address or a register name.
423 int32_t value;
424 if (GetValue(arg1, &value)) {
425 cur = reinterpret_cast<byte*>(value);
426 // Disassemble 10 instructions at <arg1>.
427 end = cur + (10 * Instruction::kInstrSize);
428 }
429 } else {
430 // The argument is the number of instructions.
431 int32_t value;
432 if (GetValue(arg1, &value)) {
433 cur = reinterpret_cast<byte*>(sim_->get_pc());
434 // Disassemble <arg1> instructions.
435 end = cur + (value * Instruction::kInstrSize);
436 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000437 }
438 } else {
439 int32_t value1;
440 int32_t value2;
441 if (GetValue(arg1, &value1) && GetValue(arg2, &value2)) {
442 cur = reinterpret_cast<byte*>(value1);
Steve Block1e0659c2011-05-24 12:43:12 +0100443 end = cur + (value2 * Instruction::kInstrSize);
Steve Blocka7e24c12009-10-30 11:49:00 +0000444 }
445 }
446
447 while (cur < end) {
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800448 prev = cur;
449 cur += dasm.InstructionDecode(buffer, cur);
Ben Murdochf87a2032010-10-22 12:50:53 +0100450 PrintF(" 0x%08x %s\n",
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800451 reinterpret_cast<intptr_t>(prev), buffer.start());
Steve Blocka7e24c12009-10-30 11:49:00 +0000452 }
453 } else if (strcmp(cmd, "gdb") == 0) {
454 PrintF("relinquishing control to gdb\n");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000455 v8::base::OS::DebugBreak();
Steve Blocka7e24c12009-10-30 11:49:00 +0000456 PrintF("regaining control from gdb\n");
457 } else if (strcmp(cmd, "break") == 0) {
Steve Block6ded16b2010-05-10 14:33:55 +0100458 if (argc == 2) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000459 int32_t value;
460 if (GetValue(arg1, &value)) {
Steve Block1e0659c2011-05-24 12:43:12 +0100461 if (!SetBreakpoint(reinterpret_cast<Instruction*>(value))) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000462 PrintF("setting breakpoint failed\n");
463 }
464 } else {
465 PrintF("%s unrecognized\n", arg1);
466 }
467 } else {
468 PrintF("break <address>\n");
469 }
470 } else if (strcmp(cmd, "del") == 0) {
471 if (!DeleteBreakpoint(NULL)) {
472 PrintF("deleting breakpoint failed\n");
473 }
474 } else if (strcmp(cmd, "flags") == 0) {
475 PrintF("N flag: %d; ", sim_->n_flag_);
476 PrintF("Z flag: %d; ", sim_->z_flag_);
477 PrintF("C flag: %d; ", sim_->c_flag_);
478 PrintF("V flag: %d\n", sim_->v_flag_);
Steve Blockd0582a62009-12-15 09:54:21 +0000479 PrintF("INVALID OP flag: %d; ", sim_->inv_op_vfp_flag_);
480 PrintF("DIV BY ZERO flag: %d; ", sim_->div_zero_vfp_flag_);
481 PrintF("OVERFLOW flag: %d; ", sim_->overflow_vfp_flag_);
482 PrintF("UNDERFLOW flag: %d; ", sim_->underflow_vfp_flag_);
Steve Block1e0659c2011-05-24 12:43:12 +0100483 PrintF("INEXACT flag: %d;\n", sim_->inexact_vfp_flag_);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800484 } else if (strcmp(cmd, "stop") == 0) {
485 int32_t value;
Steve Block1e0659c2011-05-24 12:43:12 +0100486 intptr_t stop_pc = sim_->get_pc() - 2 * Instruction::kInstrSize;
487 Instruction* stop_instr = reinterpret_cast<Instruction*>(stop_pc);
488 Instruction* msg_address =
489 reinterpret_cast<Instruction*>(stop_pc + Instruction::kInstrSize);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800490 if ((argc == 2) && (strcmp(arg1, "unstop") == 0)) {
491 // Remove the current stop.
492 if (sim_->isStopInstruction(stop_instr)) {
493 stop_instr->SetInstructionBits(kNopInstr);
494 msg_address->SetInstructionBits(kNopInstr);
495 } else {
496 PrintF("Not at debugger stop.\n");
497 }
498 } else if (argc == 3) {
499 // Print information about all/the specified breakpoint(s).
500 if (strcmp(arg1, "info") == 0) {
501 if (strcmp(arg2, "all") == 0) {
502 PrintF("Stop information:\n");
503 for (uint32_t i = 0; i < sim_->kNumOfWatchedStops; i++) {
504 sim_->PrintStopInfo(i);
505 }
506 } else if (GetValue(arg2, &value)) {
507 sim_->PrintStopInfo(value);
508 } else {
509 PrintF("Unrecognized argument.\n");
510 }
511 } else if (strcmp(arg1, "enable") == 0) {
512 // Enable all/the specified breakpoint(s).
513 if (strcmp(arg2, "all") == 0) {
514 for (uint32_t i = 0; i < sim_->kNumOfWatchedStops; i++) {
515 sim_->EnableStop(i);
516 }
517 } else if (GetValue(arg2, &value)) {
518 sim_->EnableStop(value);
519 } else {
520 PrintF("Unrecognized argument.\n");
521 }
522 } else if (strcmp(arg1, "disable") == 0) {
523 // Disable all/the specified breakpoint(s).
524 if (strcmp(arg2, "all") == 0) {
525 for (uint32_t i = 0; i < sim_->kNumOfWatchedStops; i++) {
526 sim_->DisableStop(i);
527 }
528 } else if (GetValue(arg2, &value)) {
529 sim_->DisableStop(value);
530 } else {
531 PrintF("Unrecognized argument.\n");
532 }
533 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000534 } else {
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800535 PrintF("Wrong usage. Use help command for more information.\n");
Steve Blocka7e24c12009-10-30 11:49:00 +0000536 }
Leon Clarkee46be812010-01-19 14:06:41 +0000537 } else if ((strcmp(cmd, "t") == 0) || strcmp(cmd, "trace") == 0) {
538 ::v8::internal::FLAG_trace_sim = !::v8::internal::FLAG_trace_sim;
539 PrintF("Trace of executed instructions is %s\n",
540 ::v8::internal::FLAG_trace_sim ? "on" : "off");
Steve Blocka7e24c12009-10-30 11:49:00 +0000541 } else if ((strcmp(cmd, "h") == 0) || (strcmp(cmd, "help") == 0)) {
542 PrintF("cont\n");
543 PrintF(" continue execution (alias 'c')\n");
544 PrintF("stepi\n");
545 PrintF(" step one instruction (alias 'si')\n");
546 PrintF("print <register>\n");
547 PrintF(" print register content (alias 'p')\n");
548 PrintF(" use register name 'all' to print all registers\n");
Ben Murdoch8b112d22011-06-08 16:22:53 +0100549 PrintF(" add argument 'fp' to print register pair double values\n");
Steve Blocka7e24c12009-10-30 11:49:00 +0000550 PrintF("printobject <register>\n");
551 PrintF(" print an object from a register (alias 'po')\n");
552 PrintF("flags\n");
553 PrintF(" print flags\n");
Steve Block6ded16b2010-05-10 14:33:55 +0100554 PrintF("stack [<words>]\n");
555 PrintF(" dump stack content, default dump 10 words)\n");
556 PrintF("mem <address> [<words>]\n");
557 PrintF(" dump memory content, default dump 10 words)\n");
Steve Blocka7e24c12009-10-30 11:49:00 +0000558 PrintF("disasm [<instructions>]\n");
Steve Block44f0eee2011-05-26 01:26:41 +0100559 PrintF("disasm [<address/register>]\n");
560 PrintF("disasm [[<address/register>] <instructions>]\n");
561 PrintF(" disassemble code, default is 10 instructions\n");
562 PrintF(" from pc (alias 'di')\n");
Steve Blocka7e24c12009-10-30 11:49:00 +0000563 PrintF("gdb\n");
564 PrintF(" enter gdb\n");
565 PrintF("break <address>\n");
566 PrintF(" set a break point on the address\n");
567 PrintF("del\n");
568 PrintF(" delete the breakpoint\n");
Leon Clarkee46be812010-01-19 14:06:41 +0000569 PrintF("trace (alias 't')\n");
Steve Block6ded16b2010-05-10 14:33:55 +0100570 PrintF(" toogle the tracing of all executed statements\n");
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800571 PrintF("stop feature:\n");
572 PrintF(" Description:\n");
573 PrintF(" Stops are debug instructions inserted by\n");
574 PrintF(" the Assembler::stop() function.\n");
575 PrintF(" When hitting a stop, the Simulator will\n");
Steve Block44f0eee2011-05-26 01:26:41 +0100576 PrintF(" stop and and give control to the ArmDebugger.\n");
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800577 PrintF(" The first %d stop codes are watched:\n",
578 Simulator::kNumOfWatchedStops);
579 PrintF(" - They can be enabled / disabled: the Simulator\n");
Steve Block44f0eee2011-05-26 01:26:41 +0100580 PrintF(" will / won't stop when hitting them.\n");
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800581 PrintF(" - The Simulator keeps track of how many times they \n");
582 PrintF(" are met. (See the info command.) Going over a\n");
583 PrintF(" disabled stop still increases its counter. \n");
584 PrintF(" Commands:\n");
585 PrintF(" stop info all/<code> : print infos about number <code>\n");
586 PrintF(" or all stop(s).\n");
587 PrintF(" stop enable/disable all/<code> : enables / disables\n");
588 PrintF(" all or number <code> stop(s)\n");
589 PrintF(" stop unstop\n");
590 PrintF(" ignore the stop instruction at the current location\n");
591 PrintF(" from now on\n");
Steve Blocka7e24c12009-10-30 11:49:00 +0000592 } else {
593 PrintF("Unknown command: %s\n", cmd);
594 }
595 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000596 }
597
598 // Add all the breakpoints back to stop execution and enter the debugger
599 // shell when hit.
600 RedoBreakpoints();
601
602#undef COMMAND_SIZE
603#undef ARG_SIZE
604
605#undef STR
606#undef XSTR
607}
608
609
Steve Block6ded16b2010-05-10 14:33:55 +0100610static bool ICacheMatch(void* one, void* two) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000611 DCHECK((reinterpret_cast<intptr_t>(one) & CachePage::kPageMask) == 0);
612 DCHECK((reinterpret_cast<intptr_t>(two) & CachePage::kPageMask) == 0);
Steve Block6ded16b2010-05-10 14:33:55 +0100613 return one == two;
614}
615
616
617static uint32_t ICacheHash(void* key) {
618 return static_cast<uint32_t>(reinterpret_cast<uintptr_t>(key)) >> 2;
619}
620
621
622static bool AllOnOnePage(uintptr_t start, int size) {
623 intptr_t start_page = (start & ~CachePage::kPageMask);
624 intptr_t end_page = ((start + size) & ~CachePage::kPageMask);
625 return start_page == end_page;
626}
627
628
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100629void Simulator::set_last_debugger_input(char* input) {
630 DeleteArray(last_debugger_input_);
631 last_debugger_input_ = input;
632}
633
634
Steve Block44f0eee2011-05-26 01:26:41 +0100635void Simulator::FlushICache(v8::internal::HashMap* i_cache,
636 void* start_addr,
637 size_t size) {
Steve Block6ded16b2010-05-10 14:33:55 +0100638 intptr_t start = reinterpret_cast<intptr_t>(start_addr);
639 int intra_line = (start & CachePage::kLineMask);
640 start -= intra_line;
641 size += intra_line;
642 size = ((size - 1) | CachePage::kLineMask) + 1;
643 int offset = (start & CachePage::kPageMask);
644 while (!AllOnOnePage(start, size - 1)) {
645 int bytes_to_flush = CachePage::kPageSize - offset;
Steve Block44f0eee2011-05-26 01:26:41 +0100646 FlushOnePage(i_cache, start, bytes_to_flush);
Steve Block6ded16b2010-05-10 14:33:55 +0100647 start += bytes_to_flush;
648 size -= bytes_to_flush;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000649 DCHECK_EQ(0, start & CachePage::kPageMask);
Steve Block6ded16b2010-05-10 14:33:55 +0100650 offset = 0;
651 }
652 if (size != 0) {
Steve Block44f0eee2011-05-26 01:26:41 +0100653 FlushOnePage(i_cache, start, size);
Steve Block6ded16b2010-05-10 14:33:55 +0100654 }
655}
656
657
Steve Block44f0eee2011-05-26 01:26:41 +0100658CachePage* Simulator::GetCachePage(v8::internal::HashMap* i_cache, void* page) {
659 v8::internal::HashMap::Entry* entry = i_cache->Lookup(page,
660 ICacheHash(page),
661 true);
Steve Block6ded16b2010-05-10 14:33:55 +0100662 if (entry->value == NULL) {
663 CachePage* new_page = new CachePage();
664 entry->value = new_page;
665 }
666 return reinterpret_cast<CachePage*>(entry->value);
667}
668
669
670// Flush from start up to and not including start + size.
Steve Block44f0eee2011-05-26 01:26:41 +0100671void Simulator::FlushOnePage(v8::internal::HashMap* i_cache,
672 intptr_t start,
673 int size) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000674 DCHECK(size <= CachePage::kPageSize);
675 DCHECK(AllOnOnePage(start, size - 1));
676 DCHECK((start & CachePage::kLineMask) == 0);
677 DCHECK((size & CachePage::kLineMask) == 0);
Steve Block6ded16b2010-05-10 14:33:55 +0100678 void* page = reinterpret_cast<void*>(start & (~CachePage::kPageMask));
679 int offset = (start & CachePage::kPageMask);
Steve Block44f0eee2011-05-26 01:26:41 +0100680 CachePage* cache_page = GetCachePage(i_cache, page);
Steve Block6ded16b2010-05-10 14:33:55 +0100681 char* valid_bytemap = cache_page->ValidityByte(offset);
682 memset(valid_bytemap, CachePage::LINE_INVALID, size >> CachePage::kLineShift);
683}
684
685
Steve Block44f0eee2011-05-26 01:26:41 +0100686void Simulator::CheckICache(v8::internal::HashMap* i_cache,
687 Instruction* instr) {
Steve Block6ded16b2010-05-10 14:33:55 +0100688 intptr_t address = reinterpret_cast<intptr_t>(instr);
689 void* page = reinterpret_cast<void*>(address & (~CachePage::kPageMask));
690 void* line = reinterpret_cast<void*>(address & (~CachePage::kLineMask));
691 int offset = (address & CachePage::kPageMask);
Steve Block44f0eee2011-05-26 01:26:41 +0100692 CachePage* cache_page = GetCachePage(i_cache, page);
Steve Block6ded16b2010-05-10 14:33:55 +0100693 char* cache_valid_byte = cache_page->ValidityByte(offset);
694 bool cache_hit = (*cache_valid_byte == CachePage::LINE_VALID);
695 char* cached_line = cache_page->CachedData(offset & ~CachePage::kLineMask);
696 if (cache_hit) {
697 // Check that the data in memory matches the contents of the I-cache.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000698 CHECK_EQ(0,
699 memcmp(reinterpret_cast<void*>(instr),
700 cache_page->CachedData(offset), Instruction::kInstrSize));
Steve Block6ded16b2010-05-10 14:33:55 +0100701 } else {
702 // Cache miss. Load memory into the cache.
703 memcpy(cached_line, line, CachePage::kLineLength);
704 *cache_valid_byte = CachePage::LINE_VALID;
705 }
706}
707
708
Ben Murdoch257744e2011-11-30 15:57:28 +0000709void Simulator::Initialize(Isolate* isolate) {
710 if (isolate->simulator_initialized()) return;
711 isolate->set_simulator_initialized(true);
712 ::v8::internal::ExternalReference::set_redirector(isolate,
713 &RedirectExternalReference);
Steve Blocka7e24c12009-10-30 11:49:00 +0000714}
715
716
Ben Murdoch257744e2011-11-30 15:57:28 +0000717Simulator::Simulator(Isolate* isolate) : isolate_(isolate) {
Steve Block44f0eee2011-05-26 01:26:41 +0100718 i_cache_ = isolate_->simulator_i_cache();
Steve Block6ded16b2010-05-10 14:33:55 +0100719 if (i_cache_ == NULL) {
720 i_cache_ = new v8::internal::HashMap(&ICacheMatch);
Steve Block44f0eee2011-05-26 01:26:41 +0100721 isolate_->set_simulator_i_cache(i_cache_);
Steve Block6ded16b2010-05-10 14:33:55 +0100722 }
Ben Murdoch257744e2011-11-30 15:57:28 +0000723 Initialize(isolate);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100724 // Set up simulator support first. Some of this information is needed to
Steve Blocka7e24c12009-10-30 11:49:00 +0000725 // setup the architecture state.
726 size_t stack_size = 1 * 1024*1024; // allocate 1MB for stack
727 stack_ = reinterpret_cast<char*>(malloc(stack_size));
728 pc_modified_ = false;
729 icount_ = 0;
730 break_pc_ = NULL;
731 break_instr_ = 0;
732
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100733 // Set up architecture state.
Steve Blocka7e24c12009-10-30 11:49:00 +0000734 // All registers are initialized to zero to start with.
735 for (int i = 0; i < num_registers; i++) {
736 registers_[i] = 0;
737 }
738 n_flag_ = false;
739 z_flag_ = false;
740 c_flag_ = false;
741 v_flag_ = false;
742
Steve Blockd0582a62009-12-15 09:54:21 +0000743 // Initializing VFP registers.
744 // All registers are initialized to zero to start with
745 // even though s_registers_ & d_registers_ share the same
746 // physical registers in the target.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000747 for (int i = 0; i < num_d_registers * 2; i++) {
748 vfp_registers_[i] = 0;
Steve Blockd0582a62009-12-15 09:54:21 +0000749 }
750 n_flag_FPSCR_ = false;
751 z_flag_FPSCR_ = false;
752 c_flag_FPSCR_ = false;
753 v_flag_FPSCR_ = false;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000754 FPSCR_rounding_mode_ = RN;
755 FPSCR_default_NaN_mode_ = false;
Steve Blockd0582a62009-12-15 09:54:21 +0000756
757 inv_op_vfp_flag_ = false;
758 div_zero_vfp_flag_ = false;
759 overflow_vfp_flag_ = false;
760 underflow_vfp_flag_ = false;
761 inexact_vfp_flag_ = false;
762
Steve Blocka7e24c12009-10-30 11:49:00 +0000763 // The sp is initialized to point to the bottom (high address) of the
764 // allocated stack area. To be safe in potential stack underflows we leave
765 // some buffer below.
766 registers_[sp] = reinterpret_cast<int32_t>(stack_) + stack_size - 64;
767 // The lr and pc are initialized to a known bad value that will cause an
768 // access violation if the simulator ever tries to execute it.
769 registers_[pc] = bad_lr;
770 registers_[lr] = bad_lr;
771 InitializeCoverage();
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100772
773 last_debugger_input_ = NULL;
Steve Blocka7e24c12009-10-30 11:49:00 +0000774}
775
776
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000777Simulator::~Simulator() {
778}
779
780
Steve Blocka7e24c12009-10-30 11:49:00 +0000781// When the generated code calls an external reference we need to catch that in
782// the simulator. The external reference will be a function compiled for the
783// host architecture. We need to call that function instead of trying to
784// execute it with the simulator. We do that by redirecting the external
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800785// reference to a svc (Supervisor Call) instruction that is handled by
Steve Blocka7e24c12009-10-30 11:49:00 +0000786// the simulator. We write the original destination of the jump just at a known
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800787// offset from the svc instruction so the simulator knows what to call.
Steve Blocka7e24c12009-10-30 11:49:00 +0000788class Redirection {
789 public:
Steve Block1e0659c2011-05-24 12:43:12 +0100790 Redirection(void* external_function, ExternalReference::Type type)
Steve Blocka7e24c12009-10-30 11:49:00 +0000791 : external_function_(external_function),
Steve Block1e0659c2011-05-24 12:43:12 +0100792 swi_instruction_(al | (0xf*B24) | kCallRtRedirected),
793 type_(type),
Steve Block44f0eee2011-05-26 01:26:41 +0100794 next_(NULL) {
795 Isolate* isolate = Isolate::Current();
796 next_ = isolate->simulator_redirection();
797 Simulator::current(isolate)->
798 FlushICache(isolate->simulator_i_cache(),
799 reinterpret_cast<void*>(&swi_instruction_),
800 Instruction::kInstrSize);
801 isolate->set_simulator_redirection(this);
Steve Blocka7e24c12009-10-30 11:49:00 +0000802 }
803
804 void* address_of_swi_instruction() {
805 return reinterpret_cast<void*>(&swi_instruction_);
806 }
807
808 void* external_function() { return external_function_; }
Steve Block1e0659c2011-05-24 12:43:12 +0100809 ExternalReference::Type type() { return type_; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000810
Steve Block1e0659c2011-05-24 12:43:12 +0100811 static Redirection* Get(void* external_function,
812 ExternalReference::Type type) {
Steve Block44f0eee2011-05-26 01:26:41 +0100813 Isolate* isolate = Isolate::Current();
814 Redirection* current = isolate->simulator_redirection();
815 for (; current != NULL; current = current->next_) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000816 if (current->external_function_ == external_function) {
817 DCHECK_EQ(current->type(), type);
818 return current;
819 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000820 }
Steve Block1e0659c2011-05-24 12:43:12 +0100821 return new Redirection(external_function, type);
Steve Blocka7e24c12009-10-30 11:49:00 +0000822 }
823
Steve Block1e0659c2011-05-24 12:43:12 +0100824 static Redirection* FromSwiInstruction(Instruction* swi_instruction) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000825 char* addr_of_swi = reinterpret_cast<char*>(swi_instruction);
826 char* addr_of_redirection =
827 addr_of_swi - OFFSET_OF(Redirection, swi_instruction_);
828 return reinterpret_cast<Redirection*>(addr_of_redirection);
829 }
830
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000831 static void* ReverseRedirection(int32_t reg) {
832 Redirection* redirection = FromSwiInstruction(
833 reinterpret_cast<Instruction*>(reinterpret_cast<void*>(reg)));
834 return redirection->external_function();
835 }
836
Steve Blocka7e24c12009-10-30 11:49:00 +0000837 private:
838 void* external_function_;
839 uint32_t swi_instruction_;
Steve Block1e0659c2011-05-24 12:43:12 +0100840 ExternalReference::Type type_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000841 Redirection* next_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000842};
843
844
Steve Blocka7e24c12009-10-30 11:49:00 +0000845void* Simulator::RedirectExternalReference(void* external_function,
Steve Block1e0659c2011-05-24 12:43:12 +0100846 ExternalReference::Type type) {
847 Redirection* redirection = Redirection::Get(external_function, type);
Steve Blocka7e24c12009-10-30 11:49:00 +0000848 return redirection->address_of_swi_instruction();
849}
850
851
852// Get the active Simulator for the current thread.
Steve Block44f0eee2011-05-26 01:26:41 +0100853Simulator* Simulator::current(Isolate* isolate) {
854 v8::internal::Isolate::PerIsolateThreadData* isolate_data =
Ben Murdoch257744e2011-11-30 15:57:28 +0000855 isolate->FindOrAllocatePerThreadDataForThisThread();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000856 DCHECK(isolate_data != NULL);
Steve Block44f0eee2011-05-26 01:26:41 +0100857
858 Simulator* sim = isolate_data->simulator();
Steve Blocka7e24c12009-10-30 11:49:00 +0000859 if (sim == NULL) {
Steve Block44f0eee2011-05-26 01:26:41 +0100860 // TODO(146): delete the simulator object when a thread/isolate goes away.
Ben Murdoch257744e2011-11-30 15:57:28 +0000861 sim = new Simulator(isolate);
Steve Block44f0eee2011-05-26 01:26:41 +0100862 isolate_data->set_simulator(sim);
Steve Blocka7e24c12009-10-30 11:49:00 +0000863 }
864 return sim;
865}
866
867
868// Sets the register in the architecture state. It will also deal with updating
869// Simulator internal state for special registers such as PC.
870void Simulator::set_register(int reg, int32_t value) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000871 DCHECK((reg >= 0) && (reg < num_registers));
Steve Blocka7e24c12009-10-30 11:49:00 +0000872 if (reg == pc) {
873 pc_modified_ = true;
874 }
875 registers_[reg] = value;
876}
877
878
879// Get the register from the architecture state. This function does handle
880// the special case of accessing the PC register.
881int32_t Simulator::get_register(int reg) const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000882 DCHECK((reg >= 0) && (reg < num_registers));
Steve Block791712a2010-08-27 10:21:07 +0100883 // Stupid code added to avoid bug in GCC.
884 // See: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43949
885 if (reg >= num_registers) return 0;
886 // End stupid code.
Steve Block1e0659c2011-05-24 12:43:12 +0100887 return registers_[reg] + ((reg == pc) ? Instruction::kPCReadOffset : 0);
Steve Blocka7e24c12009-10-30 11:49:00 +0000888}
889
890
Ben Murdoch8b112d22011-06-08 16:22:53 +0100891double Simulator::get_double_from_register_pair(int reg) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000892 DCHECK((reg >= 0) && (reg < num_registers) && ((reg % 2) == 0));
Ben Murdoch8b112d22011-06-08 16:22:53 +0100893
894 double dm_val = 0.0;
895 // Read the bits from the unsigned integer register_[] array
896 // into the double precision floating point value and return it.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000897 char buffer[2 * sizeof(vfp_registers_[0])];
Ben Murdoch8b112d22011-06-08 16:22:53 +0100898 memcpy(buffer, &registers_[reg], 2 * sizeof(registers_[0]));
899 memcpy(&dm_val, buffer, 2 * sizeof(registers_[0]));
900 return(dm_val);
901}
902
903
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000904void Simulator::set_register_pair_from_double(int reg, double* value) {
905 DCHECK((reg >= 0) && (reg < num_registers) && ((reg % 2) == 0));
906 memcpy(registers_ + reg, value, sizeof(*value));
907}
908
909
Kristian Monsen25f61362010-05-21 11:50:48 +0100910void Simulator::set_dw_register(int dreg, const int* dbl) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000911 DCHECK((dreg >= 0) && (dreg < num_d_registers));
Kristian Monsen25f61362010-05-21 11:50:48 +0100912 registers_[dreg] = dbl[0];
913 registers_[dreg + 1] = dbl[1];
914}
915
916
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000917void Simulator::get_d_register(int dreg, uint64_t* value) {
918 DCHECK((dreg >= 0) && (dreg < DwVfpRegister::NumRegisters()));
919 memcpy(value, vfp_registers_ + dreg * 2, sizeof(*value));
920}
921
922
923void Simulator::set_d_register(int dreg, const uint64_t* value) {
924 DCHECK((dreg >= 0) && (dreg < DwVfpRegister::NumRegisters()));
925 memcpy(vfp_registers_ + dreg * 2, value, sizeof(*value));
926}
927
928
929void Simulator::get_d_register(int dreg, uint32_t* value) {
930 DCHECK((dreg >= 0) && (dreg < DwVfpRegister::NumRegisters()));
931 memcpy(value, vfp_registers_ + dreg * 2, sizeof(*value) * 2);
932}
933
934
935void Simulator::set_d_register(int dreg, const uint32_t* value) {
936 DCHECK((dreg >= 0) && (dreg < DwVfpRegister::NumRegisters()));
937 memcpy(vfp_registers_ + dreg * 2, value, sizeof(*value) * 2);
938}
939
940
941void Simulator::get_q_register(int qreg, uint64_t* value) {
942 DCHECK((qreg >= 0) && (qreg < num_q_registers));
943 memcpy(value, vfp_registers_ + qreg * 4, sizeof(*value) * 2);
944}
945
946
947void Simulator::set_q_register(int qreg, const uint64_t* value) {
948 DCHECK((qreg >= 0) && (qreg < num_q_registers));
949 memcpy(vfp_registers_ + qreg * 4, value, sizeof(*value) * 2);
950}
951
952
953void Simulator::get_q_register(int qreg, uint32_t* value) {
954 DCHECK((qreg >= 0) && (qreg < num_q_registers));
955 memcpy(value, vfp_registers_ + qreg * 4, sizeof(*value) * 4);
956}
957
958
959void Simulator::set_q_register(int qreg, const uint32_t* value) {
960 DCHECK((qreg >= 0) && (qreg < num_q_registers));
961 memcpy(vfp_registers_ + qreg * 4, value, sizeof(*value) * 4);
962}
963
964
Steve Blocka7e24c12009-10-30 11:49:00 +0000965// Raw access to the PC register.
966void Simulator::set_pc(int32_t value) {
967 pc_modified_ = true;
968 registers_[pc] = value;
969}
970
971
Ben Murdochb0fe1622011-05-05 13:52:32 +0100972bool Simulator::has_bad_pc() const {
973 return ((registers_[pc] == bad_lr) || (registers_[pc] == end_sim_pc));
974}
975
976
Steve Blocka7e24c12009-10-30 11:49:00 +0000977// Raw access to the PC register without the special adjustment when reading.
978int32_t Simulator::get_pc() const {
979 return registers_[pc];
980}
981
982
Steve Blockd0582a62009-12-15 09:54:21 +0000983// Getting from and setting into VFP registers.
984void Simulator::set_s_register(int sreg, unsigned int value) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000985 DCHECK((sreg >= 0) && (sreg < num_s_registers));
986 vfp_registers_[sreg] = value;
Steve Blockd0582a62009-12-15 09:54:21 +0000987}
988
989
990unsigned int Simulator::get_s_register(int sreg) const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000991 DCHECK((sreg >= 0) && (sreg < num_s_registers));
992 return vfp_registers_[sreg];
Steve Blockd0582a62009-12-15 09:54:21 +0000993}
994
995
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000996template<class InputType, int register_size>
997void Simulator::SetVFPRegister(int reg_index, const InputType& value) {
998 DCHECK(reg_index >= 0);
999 if (register_size == 1) DCHECK(reg_index < num_s_registers);
1000 if (register_size == 2) DCHECK(reg_index < DwVfpRegister::NumRegisters());
1001
1002 char buffer[register_size * sizeof(vfp_registers_[0])];
1003 memcpy(buffer, &value, register_size * sizeof(vfp_registers_[0]));
1004 memcpy(&vfp_registers_[reg_index * register_size], buffer,
1005 register_size * sizeof(vfp_registers_[0]));
Steve Blockd0582a62009-12-15 09:54:21 +00001006}
1007
1008
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001009template<class ReturnType, int register_size>
1010ReturnType Simulator::GetFromVFPRegister(int reg_index) {
1011 DCHECK(reg_index >= 0);
1012 if (register_size == 1) DCHECK(reg_index < num_s_registers);
1013 if (register_size == 2) DCHECK(reg_index < DwVfpRegister::NumRegisters());
1014
1015 ReturnType value = 0;
1016 char buffer[register_size * sizeof(vfp_registers_[0])];
1017 memcpy(buffer, &vfp_registers_[register_size * reg_index],
1018 register_size * sizeof(vfp_registers_[0]));
1019 memcpy(&value, buffer, register_size * sizeof(vfp_registers_[0]));
1020 return value;
Steve Blockd0582a62009-12-15 09:54:21 +00001021}
1022
1023
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001024// Runtime FP routines take:
1025// - two double arguments
1026// - one double argument and zero or one integer arguments.
1027// All are consructed here from r0-r3 or d0, d1 and r0.
1028void Simulator::GetFpArgs(double* x, double* y, int32_t* z) {
Ben Murdoch257744e2011-11-30 15:57:28 +00001029 if (use_eabi_hardfloat()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001030 *x = get_double_from_d_register(0);
1031 *y = get_double_from_d_register(1);
1032 *z = get_register(0);
Ben Murdoch257744e2011-11-30 15:57:28 +00001033 } else {
Ben Murdoch257744e2011-11-30 15:57:28 +00001034 // Registers 0 and 1 -> x.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001035 *x = get_double_from_register_pair(0);
1036 // Register 2 and 3 -> y.
1037 *y = get_double_from_register_pair(2);
1038 // Register 2 -> z
1039 *z = get_register(2);
Ben Murdoch257744e2011-11-30 15:57:28 +00001040 }
1041}
1042
1043
1044// The return value is either in r0/r1 or d0.
Steve Blocka7e24c12009-10-30 11:49:00 +00001045void Simulator::SetFpResult(const double& result) {
Ben Murdoch257744e2011-11-30 15:57:28 +00001046 if (use_eabi_hardfloat()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001047 char buffer[2 * sizeof(vfp_registers_[0])];
Ben Murdoch257744e2011-11-30 15:57:28 +00001048 memcpy(buffer, &result, sizeof(buffer));
1049 // Copy result to d0.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001050 memcpy(vfp_registers_, buffer, sizeof(buffer));
Ben Murdoch257744e2011-11-30 15:57:28 +00001051 } else {
1052 char buffer[2 * sizeof(registers_[0])];
1053 memcpy(buffer, &result, sizeof(buffer));
1054 // Copy result to r0 and r1.
1055 memcpy(registers_, buffer, sizeof(buffer));
1056 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001057}
1058
1059
1060void Simulator::TrashCallerSaveRegisters() {
1061 // We don't trash the registers with the return value.
1062 registers_[2] = 0x50Bad4U;
1063 registers_[3] = 0x50Bad4U;
1064 registers_[12] = 0x50Bad4U;
1065}
1066
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001067
Kristian Monsen25f61362010-05-21 11:50:48 +01001068// Some Operating Systems allow unaligned access on ARMv7 targets. We
1069// assume that unaligned accesses are not allowed unless the v8 build system
1070// defines the CAN_USE_UNALIGNED_ACCESSES macro to be non-zero.
1071// The following statements below describes the behavior of the ARM CPUs
1072// that don't support unaligned access.
1073// Some ARM platforms raise an interrupt on detecting unaligned access.
1074// On others it does a funky rotation thing. For now we
1075// simply disallow unaligned reads. Note that simulator runs have the runtime
Steve Blocka7e24c12009-10-30 11:49:00 +00001076// system running directly on the host system and only generated code is
1077// executed in the simulator. Since the host is typically IA32 we will not
Kristian Monsen25f61362010-05-21 11:50:48 +01001078// get the correct ARM-like behaviour on unaligned accesses for those ARM
1079// targets that don't support unaligned loads and stores.
1080
Steve Blocka7e24c12009-10-30 11:49:00 +00001081
Steve Block1e0659c2011-05-24 12:43:12 +01001082int Simulator::ReadW(int32_t addr, Instruction* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001083 if (FLAG_enable_unaligned_accesses || (addr & 3) == 0) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001084 intptr_t* ptr = reinterpret_cast<intptr_t*>(addr);
1085 return *ptr;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001086 } else {
1087 PrintF("Unaligned read at 0x%08x, pc=0x%08" V8PRIxPTR "\n",
1088 addr,
1089 reinterpret_cast<intptr_t>(instr));
1090 UNIMPLEMENTED();
1091 return 0;
Steve Blocka7e24c12009-10-30 11:49:00 +00001092 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001093}
1094
1095
Steve Block1e0659c2011-05-24 12:43:12 +01001096void Simulator::WriteW(int32_t addr, int value, Instruction* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001097 if (FLAG_enable_unaligned_accesses || (addr & 3) == 0) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001098 intptr_t* ptr = reinterpret_cast<intptr_t*>(addr);
1099 *ptr = value;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001100 } else {
1101 PrintF("Unaligned write at 0x%08x, pc=0x%08" V8PRIxPTR "\n",
1102 addr,
1103 reinterpret_cast<intptr_t>(instr));
1104 UNIMPLEMENTED();
Steve Blocka7e24c12009-10-30 11:49:00 +00001105 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001106}
1107
1108
Steve Block1e0659c2011-05-24 12:43:12 +01001109uint16_t Simulator::ReadHU(int32_t addr, Instruction* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001110 if (FLAG_enable_unaligned_accesses || (addr & 1) == 0) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001111 uint16_t* ptr = reinterpret_cast<uint16_t*>(addr);
1112 return *ptr;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001113 } else {
1114 PrintF("Unaligned unsigned halfword read at 0x%08x, pc=0x%08"
1115 V8PRIxPTR "\n",
1116 addr,
1117 reinterpret_cast<intptr_t>(instr));
1118 UNIMPLEMENTED();
1119 return 0;
Steve Blocka7e24c12009-10-30 11:49:00 +00001120 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001121}
1122
1123
Steve Block1e0659c2011-05-24 12:43:12 +01001124int16_t Simulator::ReadH(int32_t addr, Instruction* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001125 if (FLAG_enable_unaligned_accesses || (addr & 1) == 0) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001126 int16_t* ptr = reinterpret_cast<int16_t*>(addr);
1127 return *ptr;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001128 } else {
1129 PrintF("Unaligned signed halfword read at 0x%08x\n", addr);
1130 UNIMPLEMENTED();
1131 return 0;
Steve Blocka7e24c12009-10-30 11:49:00 +00001132 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001133}
1134
1135
Steve Block1e0659c2011-05-24 12:43:12 +01001136void Simulator::WriteH(int32_t addr, uint16_t value, Instruction* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001137 if (FLAG_enable_unaligned_accesses || (addr & 1) == 0) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001138 uint16_t* ptr = reinterpret_cast<uint16_t*>(addr);
1139 *ptr = value;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001140 } else {
1141 PrintF("Unaligned unsigned halfword write at 0x%08x, pc=0x%08"
1142 V8PRIxPTR "\n",
1143 addr,
1144 reinterpret_cast<intptr_t>(instr));
1145 UNIMPLEMENTED();
Steve Blocka7e24c12009-10-30 11:49:00 +00001146 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001147}
1148
1149
Steve Block1e0659c2011-05-24 12:43:12 +01001150void Simulator::WriteH(int32_t addr, int16_t value, Instruction* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001151 if (FLAG_enable_unaligned_accesses || (addr & 1) == 0) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001152 int16_t* ptr = reinterpret_cast<int16_t*>(addr);
1153 *ptr = value;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001154 } else {
1155 PrintF("Unaligned halfword write at 0x%08x, pc=0x%08" V8PRIxPTR "\n",
1156 addr,
1157 reinterpret_cast<intptr_t>(instr));
1158 UNIMPLEMENTED();
Steve Blocka7e24c12009-10-30 11:49:00 +00001159 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001160}
1161
1162
1163uint8_t Simulator::ReadBU(int32_t addr) {
1164 uint8_t* ptr = reinterpret_cast<uint8_t*>(addr);
1165 return *ptr;
1166}
1167
1168
1169int8_t Simulator::ReadB(int32_t addr) {
1170 int8_t* ptr = reinterpret_cast<int8_t*>(addr);
1171 return *ptr;
1172}
1173
1174
1175void Simulator::WriteB(int32_t addr, uint8_t value) {
1176 uint8_t* ptr = reinterpret_cast<uint8_t*>(addr);
1177 *ptr = value;
1178}
1179
1180
1181void Simulator::WriteB(int32_t addr, int8_t value) {
1182 int8_t* ptr = reinterpret_cast<int8_t*>(addr);
1183 *ptr = value;
1184}
1185
1186
Kristian Monsen25f61362010-05-21 11:50:48 +01001187int32_t* Simulator::ReadDW(int32_t addr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001188 if (FLAG_enable_unaligned_accesses || (addr & 3) == 0) {
Kristian Monsen25f61362010-05-21 11:50:48 +01001189 int32_t* ptr = reinterpret_cast<int32_t*>(addr);
1190 return ptr;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001191 } else {
1192 PrintF("Unaligned read at 0x%08x\n", addr);
1193 UNIMPLEMENTED();
1194 return 0;
Kristian Monsen25f61362010-05-21 11:50:48 +01001195 }
Kristian Monsen25f61362010-05-21 11:50:48 +01001196}
1197
1198
1199void Simulator::WriteDW(int32_t addr, int32_t value1, int32_t value2) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001200 if (FLAG_enable_unaligned_accesses || (addr & 3) == 0) {
Kristian Monsen25f61362010-05-21 11:50:48 +01001201 int32_t* ptr = reinterpret_cast<int32_t*>(addr);
1202 *ptr++ = value1;
1203 *ptr = value2;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001204 } else {
1205 PrintF("Unaligned write at 0x%08x\n", addr);
1206 UNIMPLEMENTED();
Kristian Monsen25f61362010-05-21 11:50:48 +01001207 }
Kristian Monsen25f61362010-05-21 11:50:48 +01001208}
1209
1210
Steve Blocka7e24c12009-10-30 11:49:00 +00001211// Returns the limit of the stack area to enable checking for stack overflows.
1212uintptr_t Simulator::StackLimit() const {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001213 // Leave a safety margin of 1024 bytes to prevent overrunning the stack when
Steve Blocka7e24c12009-10-30 11:49:00 +00001214 // pushing values.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001215 return reinterpret_cast<uintptr_t>(stack_) + 1024;
Steve Blocka7e24c12009-10-30 11:49:00 +00001216}
1217
1218
1219// Unsupported instructions use Format to print an error and stop execution.
Steve Block1e0659c2011-05-24 12:43:12 +01001220void Simulator::Format(Instruction* instr, const char* format) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001221 PrintF("Simulator found unsupported instruction:\n 0x%08x: %s\n",
Ben Murdochf87a2032010-10-22 12:50:53 +01001222 reinterpret_cast<intptr_t>(instr), format);
Steve Blocka7e24c12009-10-30 11:49:00 +00001223 UNIMPLEMENTED();
1224}
1225
1226
1227// Checks if the current instruction should be executed based on its
1228// condition bits.
Steve Block1e0659c2011-05-24 12:43:12 +01001229bool Simulator::ConditionallyExecute(Instruction* instr) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001230 switch (instr->ConditionField()) {
Steve Block1e0659c2011-05-24 12:43:12 +01001231 case eq: return z_flag_;
1232 case ne: return !z_flag_;
1233 case cs: return c_flag_;
1234 case cc: return !c_flag_;
1235 case mi: return n_flag_;
1236 case pl: return !n_flag_;
1237 case vs: return v_flag_;
1238 case vc: return !v_flag_;
1239 case hi: return c_flag_ && !z_flag_;
1240 case ls: return !c_flag_ || z_flag_;
1241 case ge: return n_flag_ == v_flag_;
1242 case lt: return n_flag_ != v_flag_;
1243 case gt: return !z_flag_ && (n_flag_ == v_flag_);
1244 case le: return z_flag_ || (n_flag_ != v_flag_);
1245 case al: return true;
Steve Blocka7e24c12009-10-30 11:49:00 +00001246 default: UNREACHABLE();
1247 }
1248 return false;
1249}
1250
1251
1252// Calculate and set the Negative and Zero flags.
1253void Simulator::SetNZFlags(int32_t val) {
1254 n_flag_ = (val < 0);
1255 z_flag_ = (val == 0);
1256}
1257
1258
1259// Set the Carry flag.
1260void Simulator::SetCFlag(bool val) {
1261 c_flag_ = val;
1262}
1263
1264
1265// Set the oVerflow flag.
1266void Simulator::SetVFlag(bool val) {
1267 v_flag_ = val;
1268}
1269
1270
1271// Calculate C flag value for additions.
Ben Murdoch257744e2011-11-30 15:57:28 +00001272bool Simulator::CarryFrom(int32_t left, int32_t right, int32_t carry) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001273 uint32_t uleft = static_cast<uint32_t>(left);
1274 uint32_t uright = static_cast<uint32_t>(right);
1275 uint32_t urest = 0xffffffffU - uleft;
1276
Ben Murdoch257744e2011-11-30 15:57:28 +00001277 return (uright > urest) ||
1278 (carry && (((uright + 1) > urest) || (uright > (urest - 1))));
Steve Blocka7e24c12009-10-30 11:49:00 +00001279}
1280
1281
1282// Calculate C flag value for subtractions.
1283bool Simulator::BorrowFrom(int32_t left, int32_t right) {
1284 uint32_t uleft = static_cast<uint32_t>(left);
1285 uint32_t uright = static_cast<uint32_t>(right);
1286
1287 return (uright > uleft);
1288}
1289
1290
1291// Calculate V flag value for additions and subtractions.
1292bool Simulator::OverflowFrom(int32_t alu_out,
1293 int32_t left, int32_t right, bool addition) {
1294 bool overflow;
1295 if (addition) {
1296 // operands have the same sign
1297 overflow = ((left >= 0 && right >= 0) || (left < 0 && right < 0))
1298 // and operands and result have different sign
1299 && ((left < 0 && alu_out >= 0) || (left >= 0 && alu_out < 0));
1300 } else {
1301 // operands have different signs
1302 overflow = ((left < 0 && right >= 0) || (left >= 0 && right < 0))
1303 // and first operand and result have different signs
1304 && ((left < 0 && alu_out >= 0) || (left >= 0 && alu_out < 0));
1305 }
1306 return overflow;
1307}
1308
1309
Steve Blockd0582a62009-12-15 09:54:21 +00001310// Support for VFP comparisons.
1311void Simulator::Compute_FPSCR_Flags(double val1, double val2) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001312 if (std::isnan(val1) || std::isnan(val2)) {
Leon Clarkee46be812010-01-19 14:06:41 +00001313 n_flag_FPSCR_ = false;
1314 z_flag_FPSCR_ = false;
1315 c_flag_FPSCR_ = true;
1316 v_flag_FPSCR_ = true;
Steve Blockd0582a62009-12-15 09:54:21 +00001317 // All non-NaN cases.
Leon Clarkee46be812010-01-19 14:06:41 +00001318 } else if (val1 == val2) {
Steve Blockd0582a62009-12-15 09:54:21 +00001319 n_flag_FPSCR_ = false;
1320 z_flag_FPSCR_ = true;
1321 c_flag_FPSCR_ = true;
1322 v_flag_FPSCR_ = false;
1323 } else if (val1 < val2) {
1324 n_flag_FPSCR_ = true;
1325 z_flag_FPSCR_ = false;
1326 c_flag_FPSCR_ = false;
1327 v_flag_FPSCR_ = false;
1328 } else {
1329 // Case when (val1 > val2).
1330 n_flag_FPSCR_ = false;
1331 z_flag_FPSCR_ = false;
1332 c_flag_FPSCR_ = true;
1333 v_flag_FPSCR_ = false;
1334 }
1335}
1336
1337
1338void Simulator::Copy_FPSCR_to_APSR() {
1339 n_flag_ = n_flag_FPSCR_;
1340 z_flag_ = z_flag_FPSCR_;
1341 c_flag_ = c_flag_FPSCR_;
1342 v_flag_ = v_flag_FPSCR_;
1343}
1344
1345
Steve Blocka7e24c12009-10-30 11:49:00 +00001346// Addressing Mode 1 - Data-processing operands:
1347// Get the value based on the shifter_operand with register.
Steve Block1e0659c2011-05-24 12:43:12 +01001348int32_t Simulator::GetShiftRm(Instruction* instr, bool* carry_out) {
1349 ShiftOp shift = instr->ShiftField();
1350 int shift_amount = instr->ShiftAmountValue();
1351 int32_t result = get_register(instr->RmValue());
Steve Blocka7e24c12009-10-30 11:49:00 +00001352 if (instr->Bit(4) == 0) {
1353 // by immediate
1354 if ((shift == ROR) && (shift_amount == 0)) {
1355 UNIMPLEMENTED();
1356 return result;
1357 } else if (((shift == LSR) || (shift == ASR)) && (shift_amount == 0)) {
1358 shift_amount = 32;
1359 }
1360 switch (shift) {
1361 case ASR: {
1362 if (shift_amount == 0) {
1363 if (result < 0) {
1364 result = 0xffffffff;
1365 *carry_out = true;
1366 } else {
1367 result = 0;
1368 *carry_out = false;
1369 }
1370 } else {
1371 result >>= (shift_amount - 1);
1372 *carry_out = (result & 1) == 1;
1373 result >>= 1;
1374 }
1375 break;
1376 }
1377
1378 case LSL: {
1379 if (shift_amount == 0) {
1380 *carry_out = c_flag_;
1381 } else {
1382 result <<= (shift_amount - 1);
1383 *carry_out = (result < 0);
1384 result <<= 1;
1385 }
1386 break;
1387 }
1388
1389 case LSR: {
1390 if (shift_amount == 0) {
1391 result = 0;
1392 *carry_out = c_flag_;
1393 } else {
1394 uint32_t uresult = static_cast<uint32_t>(result);
1395 uresult >>= (shift_amount - 1);
1396 *carry_out = (uresult & 1) == 1;
1397 uresult >>= 1;
1398 result = static_cast<int32_t>(uresult);
1399 }
1400 break;
1401 }
1402
1403 case ROR: {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001404 if (shift_amount == 0) {
1405 *carry_out = c_flag_;
1406 } else {
1407 uint32_t left = static_cast<uint32_t>(result) >> shift_amount;
1408 uint32_t right = static_cast<uint32_t>(result) << (32 - shift_amount);
1409 result = right | left;
1410 *carry_out = (static_cast<uint32_t>(result) >> 31) != 0;
1411 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001412 break;
1413 }
1414
1415 default: {
1416 UNREACHABLE();
1417 break;
1418 }
1419 }
1420 } else {
1421 // by register
Steve Block1e0659c2011-05-24 12:43:12 +01001422 int rs = instr->RsValue();
Steve Blocka7e24c12009-10-30 11:49:00 +00001423 shift_amount = get_register(rs) &0xff;
1424 switch (shift) {
1425 case ASR: {
1426 if (shift_amount == 0) {
1427 *carry_out = c_flag_;
1428 } else if (shift_amount < 32) {
1429 result >>= (shift_amount - 1);
1430 *carry_out = (result & 1) == 1;
1431 result >>= 1;
1432 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001433 DCHECK(shift_amount >= 32);
Steve Blocka7e24c12009-10-30 11:49:00 +00001434 if (result < 0) {
1435 *carry_out = true;
1436 result = 0xffffffff;
1437 } else {
1438 *carry_out = false;
1439 result = 0;
1440 }
1441 }
1442 break;
1443 }
1444
1445 case LSL: {
1446 if (shift_amount == 0) {
1447 *carry_out = c_flag_;
1448 } else if (shift_amount < 32) {
1449 result <<= (shift_amount - 1);
1450 *carry_out = (result < 0);
1451 result <<= 1;
1452 } else if (shift_amount == 32) {
1453 *carry_out = (result & 1) == 1;
1454 result = 0;
1455 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001456 DCHECK(shift_amount > 32);
Steve Blocka7e24c12009-10-30 11:49:00 +00001457 *carry_out = false;
1458 result = 0;
1459 }
1460 break;
1461 }
1462
1463 case LSR: {
1464 if (shift_amount == 0) {
1465 *carry_out = c_flag_;
1466 } else if (shift_amount < 32) {
1467 uint32_t uresult = static_cast<uint32_t>(result);
1468 uresult >>= (shift_amount - 1);
1469 *carry_out = (uresult & 1) == 1;
1470 uresult >>= 1;
1471 result = static_cast<int32_t>(uresult);
1472 } else if (shift_amount == 32) {
1473 *carry_out = (result < 0);
1474 result = 0;
1475 } else {
1476 *carry_out = false;
1477 result = 0;
1478 }
1479 break;
1480 }
1481
1482 case ROR: {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001483 if (shift_amount == 0) {
1484 *carry_out = c_flag_;
1485 } else {
1486 uint32_t left = static_cast<uint32_t>(result) >> shift_amount;
1487 uint32_t right = static_cast<uint32_t>(result) << (32 - shift_amount);
1488 result = right | left;
1489 *carry_out = (static_cast<uint32_t>(result) >> 31) != 0;
1490 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001491 break;
1492 }
1493
1494 default: {
1495 UNREACHABLE();
1496 break;
1497 }
1498 }
1499 }
1500 return result;
1501}
1502
1503
1504// Addressing Mode 1 - Data-processing operands:
1505// Get the value based on the shifter_operand with immediate.
Steve Block1e0659c2011-05-24 12:43:12 +01001506int32_t Simulator::GetImm(Instruction* instr, bool* carry_out) {
1507 int rotate = instr->RotateValue() * 2;
1508 int immed8 = instr->Immed8Value();
Steve Blocka7e24c12009-10-30 11:49:00 +00001509 int imm = (immed8 >> rotate) | (immed8 << (32 - rotate));
1510 *carry_out = (rotate == 0) ? c_flag_ : (imm < 0);
1511 return imm;
1512}
1513
1514
1515static int count_bits(int bit_vector) {
1516 int count = 0;
1517 while (bit_vector != 0) {
1518 if ((bit_vector & 1) != 0) {
1519 count++;
1520 }
1521 bit_vector >>= 1;
1522 }
1523 return count;
1524}
1525
1526
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001527int32_t Simulator::ProcessPU(Instruction* instr,
1528 int num_regs,
1529 int reg_size,
1530 intptr_t* start_address,
1531 intptr_t* end_address) {
Steve Block1e0659c2011-05-24 12:43:12 +01001532 int rn = instr->RnValue();
Steve Blocka7e24c12009-10-30 11:49:00 +00001533 int32_t rn_val = get_register(rn);
Steve Blocka7e24c12009-10-30 11:49:00 +00001534 switch (instr->PUField()) {
Steve Block1e0659c2011-05-24 12:43:12 +01001535 case da_x: {
Steve Blocka7e24c12009-10-30 11:49:00 +00001536 UNIMPLEMENTED();
1537 break;
1538 }
Steve Block1e0659c2011-05-24 12:43:12 +01001539 case ia_x: {
Ben Murdoch8b112d22011-06-08 16:22:53 +01001540 *start_address = rn_val;
1541 *end_address = rn_val + (num_regs * reg_size) - reg_size;
1542 rn_val = rn_val + (num_regs * reg_size);
Steve Blocka7e24c12009-10-30 11:49:00 +00001543 break;
1544 }
Steve Block1e0659c2011-05-24 12:43:12 +01001545 case db_x: {
Ben Murdoch8b112d22011-06-08 16:22:53 +01001546 *start_address = rn_val - (num_regs * reg_size);
1547 *end_address = rn_val - reg_size;
1548 rn_val = *start_address;
Steve Blocka7e24c12009-10-30 11:49:00 +00001549 break;
1550 }
Steve Block1e0659c2011-05-24 12:43:12 +01001551 case ib_x: {
Ben Murdoch8b112d22011-06-08 16:22:53 +01001552 *start_address = rn_val + reg_size;
1553 *end_address = rn_val + (num_regs * reg_size);
1554 rn_val = *end_address;
Steve Blocka7e24c12009-10-30 11:49:00 +00001555 break;
1556 }
1557 default: {
1558 UNREACHABLE();
1559 break;
1560 }
1561 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001562 return rn_val;
Ben Murdoch8b112d22011-06-08 16:22:53 +01001563}
1564
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001565
Ben Murdoch8b112d22011-06-08 16:22:53 +01001566// Addressing Mode 4 - Load and Store Multiple
1567void Simulator::HandleRList(Instruction* instr, bool load) {
1568 int rlist = instr->RlistValue();
1569 int num_regs = count_bits(rlist);
1570
1571 intptr_t start_address = 0;
1572 intptr_t end_address = 0;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001573 int32_t rn_val =
1574 ProcessPU(instr, num_regs, kPointerSize, &start_address, &end_address);
Ben Murdoch8b112d22011-06-08 16:22:53 +01001575
Steve Blocka7e24c12009-10-30 11:49:00 +00001576 intptr_t* address = reinterpret_cast<intptr_t*>(start_address);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001577 // Catch null pointers a little earlier.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001578 DCHECK(start_address > 8191 || start_address < 0);
Steve Blocka7e24c12009-10-30 11:49:00 +00001579 int reg = 0;
1580 while (rlist != 0) {
1581 if ((rlist & 1) != 0) {
1582 if (load) {
1583 set_register(reg, *address);
1584 } else {
1585 *address = get_register(reg);
1586 }
1587 address += 1;
1588 }
1589 reg++;
1590 rlist >>= 1;
1591 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001592 DCHECK(end_address == ((intptr_t)address) - 4);
1593 if (instr->HasW()) {
1594 set_register(instr->RnValue(), rn_val);
1595 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001596}
1597
1598
Ben Murdoch8b112d22011-06-08 16:22:53 +01001599// Addressing Mode 6 - Load and Store Multiple Coprocessor registers.
1600void Simulator::HandleVList(Instruction* instr) {
1601 VFPRegPrecision precision =
1602 (instr->SzValue() == 0) ? kSinglePrecision : kDoublePrecision;
1603 int operand_size = (precision == kSinglePrecision) ? 4 : 8;
1604
1605 bool load = (instr->VLValue() == 0x1);
1606
1607 int vd;
1608 int num_regs;
1609 vd = instr->VFPDRegValue(precision);
1610 if (precision == kSinglePrecision) {
1611 num_regs = instr->Immed8Value();
1612 } else {
1613 num_regs = instr->Immed8Value() / 2;
1614 }
1615
1616 intptr_t start_address = 0;
1617 intptr_t end_address = 0;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001618 int32_t rn_val =
1619 ProcessPU(instr, num_regs, operand_size, &start_address, &end_address);
Ben Murdoch8b112d22011-06-08 16:22:53 +01001620
1621 intptr_t* address = reinterpret_cast<intptr_t*>(start_address);
1622 for (int reg = vd; reg < vd + num_regs; reg++) {
1623 if (precision == kSinglePrecision) {
1624 if (load) {
1625 set_s_register_from_sinteger(
1626 reg, ReadW(reinterpret_cast<int32_t>(address), instr));
1627 } else {
1628 WriteW(reinterpret_cast<int32_t>(address),
1629 get_sinteger_from_s_register(reg), instr);
1630 }
1631 address += 1;
1632 } else {
1633 if (load) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001634 int32_t data[] = {
1635 ReadW(reinterpret_cast<int32_t>(address), instr),
1636 ReadW(reinterpret_cast<int32_t>(address + 1), instr)
1637 };
1638 double d;
1639 memcpy(&d, data, 8);
1640 set_d_register_from_double(reg, d);
Ben Murdoch8b112d22011-06-08 16:22:53 +01001641 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001642 int32_t data[2];
1643 double d = get_double_from_d_register(reg);
1644 memcpy(data, &d, 8);
1645 WriteW(reinterpret_cast<int32_t>(address), data[0], instr);
1646 WriteW(reinterpret_cast<int32_t>(address + 1), data[1], instr);
Ben Murdoch8b112d22011-06-08 16:22:53 +01001647 }
1648 address += 2;
1649 }
1650 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001651 DCHECK(reinterpret_cast<intptr_t>(address) - operand_size == end_address);
1652 if (instr->HasW()) {
1653 set_register(instr->RnValue(), rn_val);
1654 }
Ben Murdoch8b112d22011-06-08 16:22:53 +01001655}
1656
1657
Steve Blocka7e24c12009-10-30 11:49:00 +00001658// Calls into the V8 runtime are based on this very simple interface.
1659// Note: To be able to return two values from some calls the code in runtime.cc
1660// uses the ObjectPair which is essentially two 32-bit values stuffed into a
1661// 64-bit value. With the code below we assume that all runtime calls return
1662// 64 bits of result. If they don't, the r1 result register contains a bogus
1663// value, which is fine because it is caller-saved.
1664typedef int64_t (*SimulatorRuntimeCall)(int32_t arg0,
1665 int32_t arg1,
1666 int32_t arg2,
Ben Murdochb0fe1622011-05-05 13:52:32 +01001667 int32_t arg3,
Steve Block44f0eee2011-05-26 01:26:41 +01001668 int32_t arg4,
1669 int32_t arg5);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001670
1671// These prototypes handle the four types of FP calls.
1672typedef int64_t (*SimulatorRuntimeCompareCall)(double darg0, double darg1);
1673typedef double (*SimulatorRuntimeFPFPCall)(double darg0, double darg1);
1674typedef double (*SimulatorRuntimeFPCall)(double darg0);
1675typedef double (*SimulatorRuntimeFPIntCall)(double darg0, int32_t arg0);
Steve Blocka7e24c12009-10-30 11:49:00 +00001676
Steve Block1e0659c2011-05-24 12:43:12 +01001677// This signature supports direct call in to API function native callback
1678// (refer to InvocationCallback in v8.h).
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001679typedef void (*SimulatorRuntimeDirectApiCall)(int32_t arg0);
1680typedef void (*SimulatorRuntimeProfilingApiCall)(int32_t arg0, void* arg1);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001681
1682// This signature supports direct call to accessor getter callback.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001683typedef void (*SimulatorRuntimeDirectGetterCall)(int32_t arg0, int32_t arg1);
1684typedef void (*SimulatorRuntimeProfilingGetterCall)(
1685 int32_t arg0, int32_t arg1, void* arg2);
Steve Blocka7e24c12009-10-30 11:49:00 +00001686
1687// Software interrupt instructions are used by the simulator to call into the
1688// C-based V8 runtime.
Steve Block1e0659c2011-05-24 12:43:12 +01001689void Simulator::SoftwareInterrupt(Instruction* instr) {
1690 int svc = instr->SvcValue();
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001691 switch (svc) {
Steve Block1e0659c2011-05-24 12:43:12 +01001692 case kCallRtRedirected: {
Steve Block6ded16b2010-05-10 14:33:55 +01001693 // Check if stack is aligned. Error if not aligned is reported below to
1694 // include information on the function called.
1695 bool stack_aligned =
1696 (get_register(sp)
1697 & (::v8::internal::FLAG_sim_stack_alignment - 1)) == 0;
Steve Blocka7e24c12009-10-30 11:49:00 +00001698 Redirection* redirection = Redirection::FromSwiInstruction(instr);
1699 int32_t arg0 = get_register(r0);
1700 int32_t arg1 = get_register(r1);
1701 int32_t arg2 = get_register(r2);
1702 int32_t arg3 = get_register(r3);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001703 int32_t* stack_pointer = reinterpret_cast<int32_t*>(get_register(sp));
Steve Block44f0eee2011-05-26 01:26:41 +01001704 int32_t arg4 = stack_pointer[0];
1705 int32_t arg5 = stack_pointer[1];
Ben Murdoch257744e2011-11-30 15:57:28 +00001706 bool fp_call =
1707 (redirection->type() == ExternalReference::BUILTIN_FP_FP_CALL) ||
1708 (redirection->type() == ExternalReference::BUILTIN_COMPARE_CALL) ||
1709 (redirection->type() == ExternalReference::BUILTIN_FP_CALL) ||
1710 (redirection->type() == ExternalReference::BUILTIN_FP_INT_CALL);
Steve Blocka7e24c12009-10-30 11:49:00 +00001711 // This is dodgy but it works because the C entry stubs are never moved.
1712 // See comment in codegen-arm.cc and bug 1242173.
1713 int32_t saved_lr = get_register(lr);
Steve Block1e0659c2011-05-24 12:43:12 +01001714 intptr_t external =
1715 reinterpret_cast<intptr_t>(redirection->external_function());
Ben Murdoch257744e2011-11-30 15:57:28 +00001716 if (fp_call) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001717 double dval0, dval1; // one or two double parameters
1718 int32_t ival; // zero or one integer parameters
1719 int64_t iresult = 0; // integer return value
1720 double dresult = 0; // double return value
1721 GetFpArgs(&dval0, &dval1, &ival);
Steve Block6ded16b2010-05-10 14:33:55 +01001722 if (::v8::internal::FLAG_trace_sim || !stack_aligned) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001723 SimulatorRuntimeCall generic_target =
1724 reinterpret_cast<SimulatorRuntimeCall>(external);
Ben Murdoch257744e2011-11-30 15:57:28 +00001725 switch (redirection->type()) {
1726 case ExternalReference::BUILTIN_FP_FP_CALL:
1727 case ExternalReference::BUILTIN_COMPARE_CALL:
Ben Murdoch257744e2011-11-30 15:57:28 +00001728 PrintF("Call to host function at %p with args %f, %f",
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001729 FUNCTION_ADDR(generic_target), dval0, dval1);
Ben Murdoch257744e2011-11-30 15:57:28 +00001730 break;
1731 case ExternalReference::BUILTIN_FP_CALL:
Ben Murdoch257744e2011-11-30 15:57:28 +00001732 PrintF("Call to host function at %p with arg %f",
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001733 FUNCTION_ADDR(generic_target), dval0);
Ben Murdoch257744e2011-11-30 15:57:28 +00001734 break;
1735 case ExternalReference::BUILTIN_FP_INT_CALL:
Ben Murdoch257744e2011-11-30 15:57:28 +00001736 PrintF("Call to host function at %p with args %f, %d",
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001737 FUNCTION_ADDR(generic_target), dval0, ival);
Ben Murdoch257744e2011-11-30 15:57:28 +00001738 break;
1739 default:
1740 UNREACHABLE();
1741 break;
1742 }
Steve Block6ded16b2010-05-10 14:33:55 +01001743 if (!stack_aligned) {
1744 PrintF(" with unaligned stack %08x\n", get_register(sp));
1745 }
1746 PrintF("\n");
Steve Blocka7e24c12009-10-30 11:49:00 +00001747 }
Steve Block6ded16b2010-05-10 14:33:55 +01001748 CHECK(stack_aligned);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001749 switch (redirection->type()) {
1750 case ExternalReference::BUILTIN_COMPARE_CALL: {
1751 SimulatorRuntimeCompareCall target =
1752 reinterpret_cast<SimulatorRuntimeCompareCall>(external);
1753 iresult = target(dval0, dval1);
1754 set_register(r0, static_cast<int32_t>(iresult));
1755 set_register(r1, static_cast<int32_t>(iresult >> 32));
1756 break;
1757 }
1758 case ExternalReference::BUILTIN_FP_FP_CALL: {
1759 SimulatorRuntimeFPFPCall target =
1760 reinterpret_cast<SimulatorRuntimeFPFPCall>(external);
1761 dresult = target(dval0, dval1);
1762 SetFpResult(dresult);
1763 break;
1764 }
1765 case ExternalReference::BUILTIN_FP_CALL: {
Ben Murdoch257744e2011-11-30 15:57:28 +00001766 SimulatorRuntimeFPCall target =
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001767 reinterpret_cast<SimulatorRuntimeFPCall>(external);
1768 dresult = target(dval0);
1769 SetFpResult(dresult);
1770 break;
1771 }
1772 case ExternalReference::BUILTIN_FP_INT_CALL: {
1773 SimulatorRuntimeFPIntCall target =
1774 reinterpret_cast<SimulatorRuntimeFPIntCall>(external);
1775 dresult = target(dval0, ival);
1776 SetFpResult(dresult);
1777 break;
1778 }
1779 default:
1780 UNREACHABLE();
1781 break;
1782 }
1783 if (::v8::internal::FLAG_trace_sim || !stack_aligned) {
1784 switch (redirection->type()) {
1785 case ExternalReference::BUILTIN_COMPARE_CALL:
1786 PrintF("Returned %08x\n", static_cast<int32_t>(iresult));
1787 break;
1788 case ExternalReference::BUILTIN_FP_FP_CALL:
1789 case ExternalReference::BUILTIN_FP_CALL:
1790 case ExternalReference::BUILTIN_FP_INT_CALL:
1791 PrintF("Returned %f\n", dresult);
1792 break;
1793 default:
1794 UNREACHABLE();
1795 break;
Ben Murdoch257744e2011-11-30 15:57:28 +00001796 }
Ben Murdoch257744e2011-11-30 15:57:28 +00001797 }
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001798 } else if (redirection->type() == ExternalReference::DIRECT_API_CALL) {
Steve Block1e0659c2011-05-24 12:43:12 +01001799 if (::v8::internal::FLAG_trace_sim || !stack_aligned) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001800 PrintF("Call to host function at %p args %08x",
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001801 reinterpret_cast<void*>(external), arg0);
Steve Block1e0659c2011-05-24 12:43:12 +01001802 if (!stack_aligned) {
1803 PrintF(" with unaligned stack %08x\n", get_register(sp));
1804 }
1805 PrintF("\n");
1806 }
1807 CHECK(stack_aligned);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001808 SimulatorRuntimeDirectApiCall target =
1809 reinterpret_cast<SimulatorRuntimeDirectApiCall>(external);
1810 target(arg0);
1811 } else if (
1812 redirection->type() == ExternalReference::PROFILING_API_CALL) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001813 if (::v8::internal::FLAG_trace_sim || !stack_aligned) {
1814 PrintF("Call to host function at %p args %08x %08x",
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001815 reinterpret_cast<void*>(external), arg0, arg1);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001816 if (!stack_aligned) {
1817 PrintF(" with unaligned stack %08x\n", get_register(sp));
1818 }
1819 PrintF("\n");
1820 }
1821 CHECK(stack_aligned);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001822 SimulatorRuntimeProfilingApiCall target =
1823 reinterpret_cast<SimulatorRuntimeProfilingApiCall>(external);
1824 target(arg0, Redirection::ReverseRedirection(arg1));
1825 } else if (
1826 redirection->type() == ExternalReference::DIRECT_GETTER_CALL) {
1827 if (::v8::internal::FLAG_trace_sim || !stack_aligned) {
1828 PrintF("Call to host function at %p args %08x %08x",
1829 reinterpret_cast<void*>(external), arg0, arg1);
1830 if (!stack_aligned) {
1831 PrintF(" with unaligned stack %08x\n", get_register(sp));
1832 }
1833 PrintF("\n");
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001834 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001835 CHECK(stack_aligned);
1836 SimulatorRuntimeDirectGetterCall target =
1837 reinterpret_cast<SimulatorRuntimeDirectGetterCall>(external);
1838 target(arg0, arg1);
1839 } else if (
1840 redirection->type() == ExternalReference::PROFILING_GETTER_CALL) {
1841 if (::v8::internal::FLAG_trace_sim || !stack_aligned) {
1842 PrintF("Call to host function at %p args %08x %08x %08x",
1843 reinterpret_cast<void*>(external), arg0, arg1, arg2);
1844 if (!stack_aligned) {
1845 PrintF(" with unaligned stack %08x\n", get_register(sp));
1846 }
1847 PrintF("\n");
1848 }
1849 CHECK(stack_aligned);
1850 SimulatorRuntimeProfilingGetterCall target =
1851 reinterpret_cast<SimulatorRuntimeProfilingGetterCall>(
1852 external);
1853 target(arg0, arg1, Redirection::ReverseRedirection(arg2));
Steve Blocka7e24c12009-10-30 11:49:00 +00001854 } else {
Steve Block1e0659c2011-05-24 12:43:12 +01001855 // builtin call.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001856 DCHECK(redirection->type() == ExternalReference::BUILTIN_CALL);
Steve Blocka7e24c12009-10-30 11:49:00 +00001857 SimulatorRuntimeCall target =
1858 reinterpret_cast<SimulatorRuntimeCall>(external);
Steve Block6ded16b2010-05-10 14:33:55 +01001859 if (::v8::internal::FLAG_trace_sim || !stack_aligned) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001860 PrintF(
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001861 "Call to host function at %p "
Steve Block44f0eee2011-05-26 01:26:41 +01001862 "args %08x, %08x, %08x, %08x, %08x, %08x",
Steve Blocka7e24c12009-10-30 11:49:00 +00001863 FUNCTION_ADDR(target),
1864 arg0,
1865 arg1,
1866 arg2,
Ben Murdochb0fe1622011-05-05 13:52:32 +01001867 arg3,
Steve Block44f0eee2011-05-26 01:26:41 +01001868 arg4,
1869 arg5);
Steve Block6ded16b2010-05-10 14:33:55 +01001870 if (!stack_aligned) {
1871 PrintF(" with unaligned stack %08x\n", get_register(sp));
1872 }
1873 PrintF("\n");
Steve Blocka7e24c12009-10-30 11:49:00 +00001874 }
Steve Block6ded16b2010-05-10 14:33:55 +01001875 CHECK(stack_aligned);
Steve Block44f0eee2011-05-26 01:26:41 +01001876 int64_t result = target(arg0, arg1, arg2, arg3, arg4, arg5);
Steve Blocka7e24c12009-10-30 11:49:00 +00001877 int32_t lo_res = static_cast<int32_t>(result);
1878 int32_t hi_res = static_cast<int32_t>(result >> 32);
1879 if (::v8::internal::FLAG_trace_sim) {
1880 PrintF("Returned %08x\n", lo_res);
1881 }
1882 set_register(r0, lo_res);
1883 set_register(r1, hi_res);
1884 }
1885 set_register(lr, saved_lr);
1886 set_pc(get_register(lr));
1887 break;
1888 }
Steve Block1e0659c2011-05-24 12:43:12 +01001889 case kBreakpoint: {
Steve Block44f0eee2011-05-26 01:26:41 +01001890 ArmDebugger dbg(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001891 dbg.Debug();
1892 break;
1893 }
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001894 // stop uses all codes greater than 1 << 23.
Steve Blocka7e24c12009-10-30 11:49:00 +00001895 default: {
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001896 if (svc >= (1 << 23)) {
1897 uint32_t code = svc & kStopCodeMask;
1898 if (isWatchedStop(code)) {
1899 IncreaseStopCounter(code);
1900 }
1901 // Stop if it is enabled, otherwise go on jumping over the stop
1902 // and the message address.
1903 if (isEnabledStop(code)) {
Steve Block44f0eee2011-05-26 01:26:41 +01001904 ArmDebugger dbg(this);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001905 dbg.Stop(instr);
1906 } else {
Steve Block1e0659c2011-05-24 12:43:12 +01001907 set_pc(get_pc() + 2 * Instruction::kInstrSize);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001908 }
1909 } else {
1910 // This is not a valid svc code.
1911 UNREACHABLE();
1912 break;
1913 }
1914 }
1915 }
1916}
1917
1918
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001919double Simulator::canonicalizeNaN(double value) {
1920 return (FPSCR_default_NaN_mode_ && std::isnan(value)) ?
1921 FixedDoubleArray::canonical_not_the_hole_nan_as_double() : value;
1922}
1923
1924
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001925// Stop helper functions.
Steve Block1e0659c2011-05-24 12:43:12 +01001926bool Simulator::isStopInstruction(Instruction* instr) {
1927 return (instr->Bits(27, 24) == 0xF) && (instr->SvcValue() >= kStopCode);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001928}
1929
1930
1931bool Simulator::isWatchedStop(uint32_t code) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001932 DCHECK(code <= kMaxStopCode);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001933 return code < kNumOfWatchedStops;
1934}
1935
1936
1937bool Simulator::isEnabledStop(uint32_t code) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001938 DCHECK(code <= kMaxStopCode);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001939 // Unwatched stops are always enabled.
1940 return !isWatchedStop(code) ||
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001941 !(watched_stops_[code].count & kStopDisabledBit);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001942}
1943
1944
1945void Simulator::EnableStop(uint32_t code) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001946 DCHECK(isWatchedStop(code));
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001947 if (!isEnabledStop(code)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001948 watched_stops_[code].count &= ~kStopDisabledBit;
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001949 }
1950}
1951
1952
1953void Simulator::DisableStop(uint32_t code) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001954 DCHECK(isWatchedStop(code));
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001955 if (isEnabledStop(code)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001956 watched_stops_[code].count |= kStopDisabledBit;
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001957 }
1958}
1959
1960
1961void Simulator::IncreaseStopCounter(uint32_t code) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001962 DCHECK(code <= kMaxStopCode);
1963 DCHECK(isWatchedStop(code));
1964 if ((watched_stops_[code].count & ~(1 << 31)) == 0x7fffffff) {
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001965 PrintF("Stop counter for code %i has overflowed.\n"
1966 "Enabling this code and reseting the counter to 0.\n", code);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001967 watched_stops_[code].count = 0;
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001968 EnableStop(code);
1969 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001970 watched_stops_[code].count++;
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001971 }
1972}
1973
1974
1975// Print a stop status.
1976void Simulator::PrintStopInfo(uint32_t code) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001977 DCHECK(code <= kMaxStopCode);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001978 if (!isWatchedStop(code)) {
1979 PrintF("Stop not watched.");
1980 } else {
1981 const char* state = isEnabledStop(code) ? "Enabled" : "Disabled";
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001982 int32_t count = watched_stops_[code].count & ~kStopDisabledBit;
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001983 // Don't print the state of unused breakpoints.
1984 if (count != 0) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001985 if (watched_stops_[code].desc) {
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001986 PrintF("stop %i - 0x%x: \t%s, \tcounter = %i, \t%s\n",
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001987 code, code, state, count, watched_stops_[code].desc);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001988 } else {
1989 PrintF("stop %i - 0x%x: \t%s, \tcounter = %i\n",
1990 code, code, state, count);
1991 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001992 }
1993 }
1994}
1995
1996
1997// Handle execution based on instruction types.
1998
1999// Instruction types 0 and 1 are both rolled into one function because they
2000// only differ in the handling of the shifter_operand.
Steve Block1e0659c2011-05-24 12:43:12 +01002001void Simulator::DecodeType01(Instruction* instr) {
2002 int type = instr->TypeValue();
Steve Blocka7e24c12009-10-30 11:49:00 +00002003 if ((type == 0) && instr->IsSpecialType0()) {
2004 // multiply instruction or extra loads and stores
2005 if (instr->Bits(7, 4) == 9) {
2006 if (instr->Bit(24) == 0) {
2007 // Raw field decoding here. Multiply instructions have their Rd in
2008 // funny places.
Steve Block1e0659c2011-05-24 12:43:12 +01002009 int rn = instr->RnValue();
2010 int rm = instr->RmValue();
2011 int rs = instr->RsValue();
Steve Blocka7e24c12009-10-30 11:49:00 +00002012 int32_t rs_val = get_register(rs);
2013 int32_t rm_val = get_register(rm);
2014 if (instr->Bit(23) == 0) {
2015 if (instr->Bit(21) == 0) {
2016 // The MUL instruction description (A 4.1.33) refers to Rd as being
2017 // the destination for the operation, but it confusingly uses the
2018 // Rn field to encode it.
2019 // Format(instr, "mul'cond's 'rn, 'rm, 'rs");
2020 int rd = rn; // Remap the rn field to the Rd register.
2021 int32_t alu_out = rm_val * rs_val;
2022 set_register(rd, alu_out);
2023 if (instr->HasS()) {
2024 SetNZFlags(alu_out);
2025 }
2026 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002027 int rd = instr->RdValue();
2028 int32_t acc_value = get_register(rd);
2029 if (instr->Bit(22) == 0) {
2030 // The MLA instruction description (A 4.1.28) refers to the order
2031 // of registers as "Rd, Rm, Rs, Rn". But confusingly it uses the
2032 // Rn field to encode the Rd register and the Rd field to encode
2033 // the Rn register.
2034 // Format(instr, "mla'cond's 'rn, 'rm, 'rs, 'rd");
2035 int32_t mul_out = rm_val * rs_val;
2036 int32_t result = acc_value + mul_out;
2037 set_register(rn, result);
2038 } else {
2039 // Format(instr, "mls'cond's 'rn, 'rm, 'rs, 'rd");
2040 int32_t mul_out = rm_val * rs_val;
2041 int32_t result = acc_value - mul_out;
2042 set_register(rn, result);
2043 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002044 }
2045 } else {
2046 // The signed/long multiply instructions use the terms RdHi and RdLo
2047 // when referring to the target registers. They are mapped to the Rn
2048 // and Rd fields as follows:
2049 // RdLo == Rd
2050 // RdHi == Rn (This is confusingly stored in variable rd here
2051 // because the mul instruction from above uses the
2052 // Rn field to encode the Rd register. Good luck figuring
2053 // this out without reading the ARM instruction manual
2054 // at a very detailed level.)
2055 // Format(instr, "'um'al'cond's 'rd, 'rn, 'rs, 'rm");
2056 int rd_hi = rn; // Remap the rn field to the RdHi register.
Steve Block1e0659c2011-05-24 12:43:12 +01002057 int rd_lo = instr->RdValue();
Steve Blocka7e24c12009-10-30 11:49:00 +00002058 int32_t hi_res = 0;
2059 int32_t lo_res = 0;
2060 if (instr->Bit(22) == 1) {
2061 int64_t left_op = static_cast<int32_t>(rm_val);
2062 int64_t right_op = static_cast<int32_t>(rs_val);
2063 uint64_t result = left_op * right_op;
2064 hi_res = static_cast<int32_t>(result >> 32);
2065 lo_res = static_cast<int32_t>(result & 0xffffffff);
2066 } else {
2067 // unsigned multiply
2068 uint64_t left_op = static_cast<uint32_t>(rm_val);
2069 uint64_t right_op = static_cast<uint32_t>(rs_val);
2070 uint64_t result = left_op * right_op;
2071 hi_res = static_cast<int32_t>(result >> 32);
2072 lo_res = static_cast<int32_t>(result & 0xffffffff);
2073 }
2074 set_register(rd_lo, lo_res);
2075 set_register(rd_hi, hi_res);
2076 if (instr->HasS()) {
2077 UNIMPLEMENTED();
2078 }
2079 }
2080 } else {
Steve Blockd0582a62009-12-15 09:54:21 +00002081 UNIMPLEMENTED(); // Not used by V8.
Steve Blocka7e24c12009-10-30 11:49:00 +00002082 }
2083 } else {
2084 // extra load/store instructions
Steve Block1e0659c2011-05-24 12:43:12 +01002085 int rd = instr->RdValue();
2086 int rn = instr->RnValue();
Steve Blocka7e24c12009-10-30 11:49:00 +00002087 int32_t rn_val = get_register(rn);
2088 int32_t addr = 0;
2089 if (instr->Bit(22) == 0) {
Steve Block1e0659c2011-05-24 12:43:12 +01002090 int rm = instr->RmValue();
Steve Blocka7e24c12009-10-30 11:49:00 +00002091 int32_t rm_val = get_register(rm);
2092 switch (instr->PUField()) {
Steve Block1e0659c2011-05-24 12:43:12 +01002093 case da_x: {
Steve Blocka7e24c12009-10-30 11:49:00 +00002094 // Format(instr, "'memop'cond'sign'h 'rd, ['rn], -'rm");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002095 DCHECK(!instr->HasW());
Steve Blocka7e24c12009-10-30 11:49:00 +00002096 addr = rn_val;
2097 rn_val -= rm_val;
2098 set_register(rn, rn_val);
2099 break;
2100 }
Steve Block1e0659c2011-05-24 12:43:12 +01002101 case ia_x: {
Steve Blocka7e24c12009-10-30 11:49:00 +00002102 // Format(instr, "'memop'cond'sign'h 'rd, ['rn], +'rm");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002103 DCHECK(!instr->HasW());
Steve Blocka7e24c12009-10-30 11:49:00 +00002104 addr = rn_val;
2105 rn_val += rm_val;
2106 set_register(rn, rn_val);
2107 break;
2108 }
Steve Block1e0659c2011-05-24 12:43:12 +01002109 case db_x: {
Steve Blocka7e24c12009-10-30 11:49:00 +00002110 // Format(instr, "'memop'cond'sign'h 'rd, ['rn, -'rm]'w");
2111 rn_val -= rm_val;
2112 addr = rn_val;
2113 if (instr->HasW()) {
2114 set_register(rn, rn_val);
2115 }
2116 break;
2117 }
Steve Block1e0659c2011-05-24 12:43:12 +01002118 case ib_x: {
Steve Blocka7e24c12009-10-30 11:49:00 +00002119 // Format(instr, "'memop'cond'sign'h 'rd, ['rn, +'rm]'w");
2120 rn_val += rm_val;
2121 addr = rn_val;
2122 if (instr->HasW()) {
2123 set_register(rn, rn_val);
2124 }
2125 break;
2126 }
2127 default: {
2128 // The PU field is a 2-bit field.
2129 UNREACHABLE();
2130 break;
2131 }
2132 }
2133 } else {
Steve Block1e0659c2011-05-24 12:43:12 +01002134 int32_t imm_val = (instr->ImmedHValue() << 4) | instr->ImmedLValue();
Steve Blocka7e24c12009-10-30 11:49:00 +00002135 switch (instr->PUField()) {
Steve Block1e0659c2011-05-24 12:43:12 +01002136 case da_x: {
Steve Blocka7e24c12009-10-30 11:49:00 +00002137 // Format(instr, "'memop'cond'sign'h 'rd, ['rn], #-'off8");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002138 DCHECK(!instr->HasW());
Steve Blocka7e24c12009-10-30 11:49:00 +00002139 addr = rn_val;
2140 rn_val -= imm_val;
2141 set_register(rn, rn_val);
2142 break;
2143 }
Steve Block1e0659c2011-05-24 12:43:12 +01002144 case ia_x: {
Steve Blocka7e24c12009-10-30 11:49:00 +00002145 // Format(instr, "'memop'cond'sign'h 'rd, ['rn], #+'off8");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002146 DCHECK(!instr->HasW());
Steve Blocka7e24c12009-10-30 11:49:00 +00002147 addr = rn_val;
2148 rn_val += imm_val;
2149 set_register(rn, rn_val);
2150 break;
2151 }
Steve Block1e0659c2011-05-24 12:43:12 +01002152 case db_x: {
Steve Blocka7e24c12009-10-30 11:49:00 +00002153 // Format(instr, "'memop'cond'sign'h 'rd, ['rn, #-'off8]'w");
2154 rn_val -= imm_val;
2155 addr = rn_val;
2156 if (instr->HasW()) {
2157 set_register(rn, rn_val);
2158 }
2159 break;
2160 }
Steve Block1e0659c2011-05-24 12:43:12 +01002161 case ib_x: {
Steve Blocka7e24c12009-10-30 11:49:00 +00002162 // Format(instr, "'memop'cond'sign'h 'rd, ['rn, #+'off8]'w");
2163 rn_val += imm_val;
2164 addr = rn_val;
2165 if (instr->HasW()) {
2166 set_register(rn, rn_val);
2167 }
2168 break;
2169 }
2170 default: {
2171 // The PU field is a 2-bit field.
2172 UNREACHABLE();
2173 break;
2174 }
2175 }
2176 }
Kristian Monsen25f61362010-05-21 11:50:48 +01002177 if (((instr->Bits(7, 4) & 0xd) == 0xd) && (instr->Bit(20) == 0)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002178 DCHECK((rd % 2) == 0);
Kristian Monsen25f61362010-05-21 11:50:48 +01002179 if (instr->HasH()) {
2180 // The strd instruction.
2181 int32_t value1 = get_register(rd);
2182 int32_t value2 = get_register(rd+1);
2183 WriteDW(addr, value1, value2);
2184 } else {
2185 // The ldrd instruction.
2186 int* rn_data = ReadDW(addr);
2187 set_dw_register(rd, rn_data);
2188 }
2189 } else if (instr->HasH()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002190 if (instr->HasSign()) {
2191 if (instr->HasL()) {
2192 int16_t val = ReadH(addr, instr);
2193 set_register(rd, val);
2194 } else {
2195 int16_t val = get_register(rd);
2196 WriteH(addr, val, instr);
2197 }
2198 } else {
2199 if (instr->HasL()) {
2200 uint16_t val = ReadHU(addr, instr);
2201 set_register(rd, val);
2202 } else {
2203 uint16_t val = get_register(rd);
2204 WriteH(addr, val, instr);
2205 }
2206 }
2207 } else {
2208 // signed byte loads
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002209 DCHECK(instr->HasSign());
2210 DCHECK(instr->HasL());
Steve Blocka7e24c12009-10-30 11:49:00 +00002211 int8_t val = ReadB(addr);
2212 set_register(rd, val);
2213 }
2214 return;
2215 }
Steve Block6ded16b2010-05-10 14:33:55 +01002216 } else if ((type == 0) && instr->IsMiscType0()) {
2217 if (instr->Bits(22, 21) == 1) {
Steve Block1e0659c2011-05-24 12:43:12 +01002218 int rm = instr->RmValue();
2219 switch (instr->BitField(7, 4)) {
Steve Block6ded16b2010-05-10 14:33:55 +01002220 case BX:
2221 set_pc(get_register(rm));
2222 break;
2223 case BLX: {
2224 uint32_t old_pc = get_pc();
2225 set_pc(get_register(rm));
Steve Block1e0659c2011-05-24 12:43:12 +01002226 set_register(lr, old_pc + Instruction::kInstrSize);
Steve Block6ded16b2010-05-10 14:33:55 +01002227 break;
2228 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01002229 case BKPT: {
Steve Block44f0eee2011-05-26 01:26:41 +01002230 ArmDebugger dbg(this);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002231 PrintF("Simulator hit BKPT.\n");
2232 dbg.Debug();
Steve Block6ded16b2010-05-10 14:33:55 +01002233 break;
Ben Murdochb0fe1622011-05-05 13:52:32 +01002234 }
Steve Block6ded16b2010-05-10 14:33:55 +01002235 default:
2236 UNIMPLEMENTED();
2237 }
2238 } else if (instr->Bits(22, 21) == 3) {
Steve Block1e0659c2011-05-24 12:43:12 +01002239 int rm = instr->RmValue();
2240 int rd = instr->RdValue();
2241 switch (instr->BitField(7, 4)) {
Steve Block6ded16b2010-05-10 14:33:55 +01002242 case CLZ: {
2243 uint32_t bits = get_register(rm);
2244 int leading_zeros = 0;
2245 if (bits == 0) {
2246 leading_zeros = 32;
2247 } else {
2248 while ((bits & 0x80000000u) == 0) {
2249 bits <<= 1;
2250 leading_zeros++;
2251 }
2252 }
2253 set_register(rd, leading_zeros);
2254 break;
2255 }
2256 default:
2257 UNIMPLEMENTED();
2258 }
2259 } else {
2260 PrintF("%08x\n", instr->InstructionBits());
2261 UNIMPLEMENTED();
2262 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002263 } else if ((type == 1) && instr->IsNopType1()) {
2264 // NOP.
Steve Blocka7e24c12009-10-30 11:49:00 +00002265 } else {
Steve Block1e0659c2011-05-24 12:43:12 +01002266 int rd = instr->RdValue();
2267 int rn = instr->RnValue();
Steve Blocka7e24c12009-10-30 11:49:00 +00002268 int32_t rn_val = get_register(rn);
2269 int32_t shifter_operand = 0;
2270 bool shifter_carry_out = 0;
2271 if (type == 0) {
2272 shifter_operand = GetShiftRm(instr, &shifter_carry_out);
2273 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002274 DCHECK(instr->TypeValue() == 1);
Steve Blocka7e24c12009-10-30 11:49:00 +00002275 shifter_operand = GetImm(instr, &shifter_carry_out);
2276 }
2277 int32_t alu_out;
2278
2279 switch (instr->OpcodeField()) {
2280 case AND: {
2281 // Format(instr, "and'cond's 'rd, 'rn, 'shift_rm");
2282 // Format(instr, "and'cond's 'rd, 'rn, 'imm");
2283 alu_out = rn_val & shifter_operand;
2284 set_register(rd, alu_out);
2285 if (instr->HasS()) {
2286 SetNZFlags(alu_out);
2287 SetCFlag(shifter_carry_out);
2288 }
2289 break;
2290 }
2291
2292 case EOR: {
2293 // Format(instr, "eor'cond's 'rd, 'rn, 'shift_rm");
2294 // Format(instr, "eor'cond's 'rd, 'rn, 'imm");
2295 alu_out = rn_val ^ shifter_operand;
2296 set_register(rd, alu_out);
2297 if (instr->HasS()) {
2298 SetNZFlags(alu_out);
2299 SetCFlag(shifter_carry_out);
2300 }
2301 break;
2302 }
2303
2304 case SUB: {
2305 // Format(instr, "sub'cond's 'rd, 'rn, 'shift_rm");
2306 // Format(instr, "sub'cond's 'rd, 'rn, 'imm");
2307 alu_out = rn_val - shifter_operand;
2308 set_register(rd, alu_out);
2309 if (instr->HasS()) {
2310 SetNZFlags(alu_out);
2311 SetCFlag(!BorrowFrom(rn_val, shifter_operand));
2312 SetVFlag(OverflowFrom(alu_out, rn_val, shifter_operand, false));
2313 }
2314 break;
2315 }
2316
2317 case RSB: {
2318 // Format(instr, "rsb'cond's 'rd, 'rn, 'shift_rm");
2319 // Format(instr, "rsb'cond's 'rd, 'rn, 'imm");
2320 alu_out = shifter_operand - rn_val;
2321 set_register(rd, alu_out);
2322 if (instr->HasS()) {
2323 SetNZFlags(alu_out);
2324 SetCFlag(!BorrowFrom(shifter_operand, rn_val));
2325 SetVFlag(OverflowFrom(alu_out, shifter_operand, rn_val, false));
2326 }
2327 break;
2328 }
2329
2330 case ADD: {
2331 // Format(instr, "add'cond's 'rd, 'rn, 'shift_rm");
2332 // Format(instr, "add'cond's 'rd, 'rn, 'imm");
2333 alu_out = rn_val + shifter_operand;
2334 set_register(rd, alu_out);
2335 if (instr->HasS()) {
2336 SetNZFlags(alu_out);
2337 SetCFlag(CarryFrom(rn_val, shifter_operand));
2338 SetVFlag(OverflowFrom(alu_out, rn_val, shifter_operand, true));
2339 }
2340 break;
2341 }
2342
2343 case ADC: {
Ben Murdoch257744e2011-11-30 15:57:28 +00002344 // Format(instr, "adc'cond's 'rd, 'rn, 'shift_rm");
2345 // Format(instr, "adc'cond's 'rd, 'rn, 'imm");
2346 alu_out = rn_val + shifter_operand + GetCarry();
2347 set_register(rd, alu_out);
2348 if (instr->HasS()) {
2349 SetNZFlags(alu_out);
2350 SetCFlag(CarryFrom(rn_val, shifter_operand, GetCarry()));
2351 SetVFlag(OverflowFrom(alu_out, rn_val, shifter_operand, true));
2352 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002353 break;
2354 }
2355
2356 case SBC: {
2357 Format(instr, "sbc'cond's 'rd, 'rn, 'shift_rm");
2358 Format(instr, "sbc'cond's 'rd, 'rn, 'imm");
2359 break;
2360 }
2361
2362 case RSC: {
2363 Format(instr, "rsc'cond's 'rd, 'rn, 'shift_rm");
2364 Format(instr, "rsc'cond's 'rd, 'rn, 'imm");
2365 break;
2366 }
2367
2368 case TST: {
2369 if (instr->HasS()) {
2370 // Format(instr, "tst'cond 'rn, 'shift_rm");
2371 // Format(instr, "tst'cond 'rn, 'imm");
2372 alu_out = rn_val & shifter_operand;
2373 SetNZFlags(alu_out);
2374 SetCFlag(shifter_carry_out);
2375 } else {
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01002376 // Format(instr, "movw'cond 'rd, 'imm").
Steve Block1e0659c2011-05-24 12:43:12 +01002377 alu_out = instr->ImmedMovwMovtValue();
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01002378 set_register(rd, alu_out);
Steve Blocka7e24c12009-10-30 11:49:00 +00002379 }
2380 break;
2381 }
2382
2383 case TEQ: {
2384 if (instr->HasS()) {
2385 // Format(instr, "teq'cond 'rn, 'shift_rm");
2386 // Format(instr, "teq'cond 'rn, 'imm");
2387 alu_out = rn_val ^ shifter_operand;
2388 SetNZFlags(alu_out);
2389 SetCFlag(shifter_carry_out);
2390 } else {
Steve Block6ded16b2010-05-10 14:33:55 +01002391 // Other instructions matching this pattern are handled in the
2392 // miscellaneous instructions part above.
2393 UNREACHABLE();
Steve Blocka7e24c12009-10-30 11:49:00 +00002394 }
2395 break;
2396 }
2397
2398 case CMP: {
2399 if (instr->HasS()) {
2400 // Format(instr, "cmp'cond 'rn, 'shift_rm");
2401 // Format(instr, "cmp'cond 'rn, 'imm");
2402 alu_out = rn_val - shifter_operand;
2403 SetNZFlags(alu_out);
2404 SetCFlag(!BorrowFrom(rn_val, shifter_operand));
2405 SetVFlag(OverflowFrom(alu_out, rn_val, shifter_operand, false));
2406 } else {
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01002407 // Format(instr, "movt'cond 'rd, 'imm").
2408 alu_out = (get_register(rd) & 0xffff) |
Steve Block1e0659c2011-05-24 12:43:12 +01002409 (instr->ImmedMovwMovtValue() << 16);
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01002410 set_register(rd, alu_out);
Steve Blocka7e24c12009-10-30 11:49:00 +00002411 }
2412 break;
2413 }
2414
2415 case CMN: {
2416 if (instr->HasS()) {
2417 // Format(instr, "cmn'cond 'rn, 'shift_rm");
2418 // Format(instr, "cmn'cond 'rn, 'imm");
2419 alu_out = rn_val + shifter_operand;
2420 SetNZFlags(alu_out);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002421 SetCFlag(CarryFrom(rn_val, shifter_operand));
Steve Blocka7e24c12009-10-30 11:49:00 +00002422 SetVFlag(OverflowFrom(alu_out, rn_val, shifter_operand, true));
2423 } else {
Steve Block6ded16b2010-05-10 14:33:55 +01002424 // Other instructions matching this pattern are handled in the
2425 // miscellaneous instructions part above.
2426 UNREACHABLE();
Steve Blocka7e24c12009-10-30 11:49:00 +00002427 }
2428 break;
2429 }
2430
2431 case ORR: {
2432 // Format(instr, "orr'cond's 'rd, 'rn, 'shift_rm");
2433 // Format(instr, "orr'cond's 'rd, 'rn, 'imm");
2434 alu_out = rn_val | shifter_operand;
2435 set_register(rd, alu_out);
2436 if (instr->HasS()) {
2437 SetNZFlags(alu_out);
2438 SetCFlag(shifter_carry_out);
2439 }
2440 break;
2441 }
2442
2443 case MOV: {
2444 // Format(instr, "mov'cond's 'rd, 'shift_rm");
2445 // Format(instr, "mov'cond's 'rd, 'imm");
2446 alu_out = shifter_operand;
2447 set_register(rd, alu_out);
2448 if (instr->HasS()) {
2449 SetNZFlags(alu_out);
2450 SetCFlag(shifter_carry_out);
2451 }
2452 break;
2453 }
2454
2455 case BIC: {
2456 // Format(instr, "bic'cond's 'rd, 'rn, 'shift_rm");
2457 // Format(instr, "bic'cond's 'rd, 'rn, 'imm");
2458 alu_out = rn_val & ~shifter_operand;
2459 set_register(rd, alu_out);
2460 if (instr->HasS()) {
2461 SetNZFlags(alu_out);
2462 SetCFlag(shifter_carry_out);
2463 }
2464 break;
2465 }
2466
2467 case MVN: {
2468 // Format(instr, "mvn'cond's 'rd, 'shift_rm");
2469 // Format(instr, "mvn'cond's 'rd, 'imm");
2470 alu_out = ~shifter_operand;
2471 set_register(rd, alu_out);
2472 if (instr->HasS()) {
2473 SetNZFlags(alu_out);
2474 SetCFlag(shifter_carry_out);
2475 }
2476 break;
2477 }
2478
2479 default: {
2480 UNREACHABLE();
2481 break;
2482 }
2483 }
2484 }
2485}
2486
2487
Steve Block1e0659c2011-05-24 12:43:12 +01002488void Simulator::DecodeType2(Instruction* instr) {
2489 int rd = instr->RdValue();
2490 int rn = instr->RnValue();
Steve Blocka7e24c12009-10-30 11:49:00 +00002491 int32_t rn_val = get_register(rn);
Steve Block1e0659c2011-05-24 12:43:12 +01002492 int32_t im_val = instr->Offset12Value();
Steve Blocka7e24c12009-10-30 11:49:00 +00002493 int32_t addr = 0;
2494 switch (instr->PUField()) {
Steve Block1e0659c2011-05-24 12:43:12 +01002495 case da_x: {
Steve Blocka7e24c12009-10-30 11:49:00 +00002496 // Format(instr, "'memop'cond'b 'rd, ['rn], #-'off12");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002497 DCHECK(!instr->HasW());
Steve Blocka7e24c12009-10-30 11:49:00 +00002498 addr = rn_val;
2499 rn_val -= im_val;
2500 set_register(rn, rn_val);
2501 break;
2502 }
Steve Block1e0659c2011-05-24 12:43:12 +01002503 case ia_x: {
Steve Blocka7e24c12009-10-30 11:49:00 +00002504 // Format(instr, "'memop'cond'b 'rd, ['rn], #+'off12");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002505 DCHECK(!instr->HasW());
Steve Blocka7e24c12009-10-30 11:49:00 +00002506 addr = rn_val;
2507 rn_val += im_val;
2508 set_register(rn, rn_val);
2509 break;
2510 }
Steve Block1e0659c2011-05-24 12:43:12 +01002511 case db_x: {
Steve Blocka7e24c12009-10-30 11:49:00 +00002512 // Format(instr, "'memop'cond'b 'rd, ['rn, #-'off12]'w");
2513 rn_val -= im_val;
2514 addr = rn_val;
2515 if (instr->HasW()) {
2516 set_register(rn, rn_val);
2517 }
2518 break;
2519 }
Steve Block1e0659c2011-05-24 12:43:12 +01002520 case ib_x: {
Steve Blocka7e24c12009-10-30 11:49:00 +00002521 // Format(instr, "'memop'cond'b 'rd, ['rn, #+'off12]'w");
2522 rn_val += im_val;
2523 addr = rn_val;
2524 if (instr->HasW()) {
2525 set_register(rn, rn_val);
2526 }
2527 break;
2528 }
2529 default: {
2530 UNREACHABLE();
2531 break;
2532 }
2533 }
2534 if (instr->HasB()) {
2535 if (instr->HasL()) {
2536 byte val = ReadBU(addr);
2537 set_register(rd, val);
2538 } else {
2539 byte val = get_register(rd);
2540 WriteB(addr, val);
2541 }
2542 } else {
2543 if (instr->HasL()) {
2544 set_register(rd, ReadW(addr, instr));
2545 } else {
2546 WriteW(addr, get_register(rd), instr);
2547 }
2548 }
2549}
2550
2551
Steve Block1e0659c2011-05-24 12:43:12 +01002552void Simulator::DecodeType3(Instruction* instr) {
2553 int rd = instr->RdValue();
2554 int rn = instr->RnValue();
Steve Blocka7e24c12009-10-30 11:49:00 +00002555 int32_t rn_val = get_register(rn);
2556 bool shifter_carry_out = 0;
2557 int32_t shifter_operand = GetShiftRm(instr, &shifter_carry_out);
2558 int32_t addr = 0;
2559 switch (instr->PUField()) {
Steve Block1e0659c2011-05-24 12:43:12 +01002560 case da_x: {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002561 DCHECK(!instr->HasW());
Steve Blocka7e24c12009-10-30 11:49:00 +00002562 Format(instr, "'memop'cond'b 'rd, ['rn], -'shift_rm");
Kristian Monsen50ef84f2010-07-29 15:18:00 +01002563 UNIMPLEMENTED();
Steve Blocka7e24c12009-10-30 11:49:00 +00002564 break;
2565 }
Steve Block1e0659c2011-05-24 12:43:12 +01002566 case ia_x: {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002567 if (instr->Bit(4) == 0) {
2568 // Memop.
2569 } else {
2570 if (instr->Bit(5) == 0) {
2571 switch (instr->Bits(22, 21)) {
2572 case 0:
2573 if (instr->Bit(20) == 0) {
2574 if (instr->Bit(6) == 0) {
2575 // Pkhbt.
2576 uint32_t rn_val = get_register(rn);
2577 uint32_t rm_val = get_register(instr->RmValue());
2578 int32_t shift = instr->Bits(11, 7);
2579 rm_val <<= shift;
2580 set_register(rd, (rn_val & 0xFFFF) | (rm_val & 0xFFFF0000U));
2581 } else {
2582 // Pkhtb.
2583 uint32_t rn_val = get_register(rn);
2584 int32_t rm_val = get_register(instr->RmValue());
2585 int32_t shift = instr->Bits(11, 7);
2586 if (shift == 0) {
2587 shift = 32;
2588 }
2589 rm_val >>= shift;
2590 set_register(rd, (rn_val & 0xFFFF0000U) | (rm_val & 0xFFFF));
2591 }
2592 } else {
2593 UNIMPLEMENTED();
2594 }
2595 break;
2596 case 1:
2597 UNIMPLEMENTED();
2598 break;
2599 case 2:
2600 UNIMPLEMENTED();
2601 break;
2602 case 3: {
2603 // Usat.
2604 int32_t sat_pos = instr->Bits(20, 16);
2605 int32_t sat_val = (1 << sat_pos) - 1;
2606 int32_t shift = instr->Bits(11, 7);
2607 int32_t shift_type = instr->Bit(6);
2608 int32_t rm_val = get_register(instr->RmValue());
2609 if (shift_type == 0) { // LSL
2610 rm_val <<= shift;
2611 } else { // ASR
2612 rm_val >>= shift;
2613 }
2614 // If saturation occurs, the Q flag should be set in the CPSR.
2615 // There is no Q flag yet, and no instruction (MRS) to read the
2616 // CPSR directly.
2617 if (rm_val > sat_val) {
2618 rm_val = sat_val;
2619 } else if (rm_val < 0) {
2620 rm_val = 0;
2621 }
2622 set_register(rd, rm_val);
2623 break;
2624 }
Kristian Monsen50ef84f2010-07-29 15:18:00 +01002625 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002626 } else {
2627 switch (instr->Bits(22, 21)) {
2628 case 0:
2629 UNIMPLEMENTED();
2630 break;
2631 case 1:
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002632 if (instr->Bits(9, 6) == 1) {
2633 if (instr->Bit(20) == 0) {
2634 if (instr->Bits(19, 16) == 0xF) {
2635 // Sxtb.
2636 int32_t rm_val = get_register(instr->RmValue());
2637 int32_t rotate = instr->Bits(11, 10);
2638 switch (rotate) {
2639 case 0:
2640 break;
2641 case 1:
2642 rm_val = (rm_val >> 8) | (rm_val << 24);
2643 break;
2644 case 2:
2645 rm_val = (rm_val >> 16) | (rm_val << 16);
2646 break;
2647 case 3:
2648 rm_val = (rm_val >> 24) | (rm_val << 8);
2649 break;
2650 }
2651 set_register(rd, static_cast<int8_t>(rm_val));
2652 } else {
2653 // Sxtab.
2654 int32_t rn_val = get_register(rn);
2655 int32_t rm_val = get_register(instr->RmValue());
2656 int32_t rotate = instr->Bits(11, 10);
2657 switch (rotate) {
2658 case 0:
2659 break;
2660 case 1:
2661 rm_val = (rm_val >> 8) | (rm_val << 24);
2662 break;
2663 case 2:
2664 rm_val = (rm_val >> 16) | (rm_val << 16);
2665 break;
2666 case 3:
2667 rm_val = (rm_val >> 24) | (rm_val << 8);
2668 break;
2669 }
2670 set_register(rd, rn_val + static_cast<int8_t>(rm_val));
2671 }
2672 } else {
2673 if (instr->Bits(19, 16) == 0xF) {
2674 // Sxth.
2675 int32_t rm_val = get_register(instr->RmValue());
2676 int32_t rotate = instr->Bits(11, 10);
2677 switch (rotate) {
2678 case 0:
2679 break;
2680 case 1:
2681 rm_val = (rm_val >> 8) | (rm_val << 24);
2682 break;
2683 case 2:
2684 rm_val = (rm_val >> 16) | (rm_val << 16);
2685 break;
2686 case 3:
2687 rm_val = (rm_val >> 24) | (rm_val << 8);
2688 break;
2689 }
2690 set_register(rd, static_cast<int16_t>(rm_val));
2691 } else {
2692 // Sxtah.
2693 int32_t rn_val = get_register(rn);
2694 int32_t rm_val = get_register(instr->RmValue());
2695 int32_t rotate = instr->Bits(11, 10);
2696 switch (rotate) {
2697 case 0:
2698 break;
2699 case 1:
2700 rm_val = (rm_val >> 8) | (rm_val << 24);
2701 break;
2702 case 2:
2703 rm_val = (rm_val >> 16) | (rm_val << 16);
2704 break;
2705 case 3:
2706 rm_val = (rm_val >> 24) | (rm_val << 8);
2707 break;
2708 }
2709 set_register(rd, rn_val + static_cast<int16_t>(rm_val));
2710 }
2711 }
2712 } else {
2713 UNREACHABLE();
2714 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002715 break;
2716 case 2:
2717 if ((instr->Bit(20) == 0) && (instr->Bits(9, 6) == 1)) {
2718 if (instr->Bits(19, 16) == 0xF) {
2719 // Uxtb16.
2720 uint32_t rm_val = get_register(instr->RmValue());
2721 int32_t rotate = instr->Bits(11, 10);
2722 switch (rotate) {
2723 case 0:
2724 break;
2725 case 1:
2726 rm_val = (rm_val >> 8) | (rm_val << 24);
2727 break;
2728 case 2:
2729 rm_val = (rm_val >> 16) | (rm_val << 16);
2730 break;
2731 case 3:
2732 rm_val = (rm_val >> 24) | (rm_val << 8);
2733 break;
2734 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002735 set_register(rd, (rm_val & 0xFF) | (rm_val & 0xFF0000));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002736 } else {
2737 UNIMPLEMENTED();
2738 }
2739 } else {
2740 UNIMPLEMENTED();
2741 }
2742 break;
2743 case 3:
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002744 if ((instr->Bits(9, 6) == 1)) {
2745 if (instr->Bit(20) == 0) {
2746 if (instr->Bits(19, 16) == 0xF) {
2747 // Uxtb.
2748 uint32_t rm_val = get_register(instr->RmValue());
2749 int32_t rotate = instr->Bits(11, 10);
2750 switch (rotate) {
2751 case 0:
2752 break;
2753 case 1:
2754 rm_val = (rm_val >> 8) | (rm_val << 24);
2755 break;
2756 case 2:
2757 rm_val = (rm_val >> 16) | (rm_val << 16);
2758 break;
2759 case 3:
2760 rm_val = (rm_val >> 24) | (rm_val << 8);
2761 break;
2762 }
2763 set_register(rd, (rm_val & 0xFF));
2764 } else {
2765 // Uxtab.
2766 uint32_t rn_val = get_register(rn);
2767 uint32_t rm_val = get_register(instr->RmValue());
2768 int32_t rotate = instr->Bits(11, 10);
2769 switch (rotate) {
2770 case 0:
2771 break;
2772 case 1:
2773 rm_val = (rm_val >> 8) | (rm_val << 24);
2774 break;
2775 case 2:
2776 rm_val = (rm_val >> 16) | (rm_val << 16);
2777 break;
2778 case 3:
2779 rm_val = (rm_val >> 24) | (rm_val << 8);
2780 break;
2781 }
2782 set_register(rd, rn_val + (rm_val & 0xFF));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002783 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002784 } else {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002785 if (instr->Bits(19, 16) == 0xF) {
2786 // Uxth.
2787 uint32_t rm_val = get_register(instr->RmValue());
2788 int32_t rotate = instr->Bits(11, 10);
2789 switch (rotate) {
2790 case 0:
2791 break;
2792 case 1:
2793 rm_val = (rm_val >> 8) | (rm_val << 24);
2794 break;
2795 case 2:
2796 rm_val = (rm_val >> 16) | (rm_val << 16);
2797 break;
2798 case 3:
2799 rm_val = (rm_val >> 24) | (rm_val << 8);
2800 break;
2801 }
2802 set_register(rd, (rm_val & 0xFFFF));
2803 } else {
2804 // Uxtah.
2805 uint32_t rn_val = get_register(rn);
2806 uint32_t rm_val = get_register(instr->RmValue());
2807 int32_t rotate = instr->Bits(11, 10);
2808 switch (rotate) {
2809 case 0:
2810 break;
2811 case 1:
2812 rm_val = (rm_val >> 8) | (rm_val << 24);
2813 break;
2814 case 2:
2815 rm_val = (rm_val >> 16) | (rm_val << 16);
2816 break;
2817 case 3:
2818 rm_val = (rm_val >> 24) | (rm_val << 8);
2819 break;
2820 }
2821 set_register(rd, rn_val + (rm_val & 0xFFFF));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002822 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002823 }
2824 } else {
2825 UNIMPLEMENTED();
2826 }
2827 break;
Kristian Monsen50ef84f2010-07-29 15:18:00 +01002828 }
Kristian Monsen50ef84f2010-07-29 15:18:00 +01002829 }
2830 return;
Kristian Monsen50ef84f2010-07-29 15:18:00 +01002831 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002832 break;
2833 }
Steve Block1e0659c2011-05-24 12:43:12 +01002834 case db_x: {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002835 if (instr->Bits(22, 20) == 0x5) {
2836 if (instr->Bits(7, 4) == 0x1) {
2837 int rm = instr->RmValue();
2838 int32_t rm_val = get_register(rm);
2839 int rs = instr->RsValue();
2840 int32_t rs_val = get_register(rs);
2841 if (instr->Bits(15, 12) == 0xF) {
2842 // SMMUL (in V8 notation matching ARM ISA format)
2843 // Format(instr, "smmul'cond 'rn, 'rm, 'rs");
2844 rn_val = base::bits::SignedMulHigh32(rm_val, rs_val);
2845 } else {
2846 // SMMLA (in V8 notation matching ARM ISA format)
2847 // Format(instr, "smmla'cond 'rn, 'rm, 'rs, 'rd");
2848 int rd = instr->RdValue();
2849 int32_t rd_val = get_register(rd);
2850 rn_val = base::bits::SignedMulHighAndAdd32(rm_val, rs_val, rd_val);
2851 }
2852 set_register(rn, rn_val);
2853 return;
2854 }
2855 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002856 if (FLAG_enable_sudiv) {
2857 if (instr->Bits(5, 4) == 0x1) {
2858 if ((instr->Bit(22) == 0x0) && (instr->Bit(20) == 0x1)) {
2859 // (s/u)div (in V8 notation matching ARM ISA format) rn = rm/rs
2860 // Format(instr, "'(s/u)div'cond'b 'rn, 'rm, 'rs);
2861 int rm = instr->RmValue();
2862 int32_t rm_val = get_register(rm);
2863 int rs = instr->RsValue();
2864 int32_t rs_val = get_register(rs);
2865 int32_t ret_val = 0;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002866 // udiv
2867 if (instr->Bit(21) == 0x1) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002868 ret_val = bit_cast<int32_t>(base::bits::UnsignedDiv32(
2869 bit_cast<uint32_t>(rm_val), bit_cast<uint32_t>(rs_val)));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002870 } else {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002871 ret_val = base::bits::SignedDiv32(rm_val, rs_val);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002872 }
2873 set_register(rn, ret_val);
2874 return;
2875 }
2876 }
2877 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002878 // Format(instr, "'memop'cond'b 'rd, ['rn, -'shift_rm]'w");
2879 addr = rn_val - shifter_operand;
2880 if (instr->HasW()) {
2881 set_register(rn, addr);
2882 }
2883 break;
2884 }
Steve Block1e0659c2011-05-24 12:43:12 +01002885 case ib_x: {
Andrei Popescu31002712010-02-23 13:46:05 +00002886 if (instr->HasW() && (instr->Bits(6, 4) == 0x5)) {
2887 uint32_t widthminus1 = static_cast<uint32_t>(instr->Bits(20, 16));
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002888 uint32_t lsbit = static_cast<uint32_t>(instr->Bits(11, 7));
Andrei Popescu31002712010-02-23 13:46:05 +00002889 uint32_t msbit = widthminus1 + lsbit;
2890 if (msbit <= 31) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002891 if (instr->Bit(22)) {
2892 // ubfx - unsigned bitfield extract.
2893 uint32_t rm_val =
Steve Block1e0659c2011-05-24 12:43:12 +01002894 static_cast<uint32_t>(get_register(instr->RmValue()));
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002895 uint32_t extr_val = rm_val << (31 - msbit);
2896 extr_val = extr_val >> (31 - widthminus1);
Steve Block1e0659c2011-05-24 12:43:12 +01002897 set_register(instr->RdValue(), extr_val);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002898 } else {
2899 // sbfx - signed bitfield extract.
Steve Block1e0659c2011-05-24 12:43:12 +01002900 int32_t rm_val = get_register(instr->RmValue());
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002901 int32_t extr_val = rm_val << (31 - msbit);
2902 extr_val = extr_val >> (31 - widthminus1);
Steve Block1e0659c2011-05-24 12:43:12 +01002903 set_register(instr->RdValue(), extr_val);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002904 }
2905 } else {
2906 UNREACHABLE();
2907 }
2908 return;
2909 } else if (!instr->HasW() && (instr->Bits(6, 4) == 0x1)) {
2910 uint32_t lsbit = static_cast<uint32_t>(instr->Bits(11, 7));
2911 uint32_t msbit = static_cast<uint32_t>(instr->Bits(20, 16));
2912 if (msbit >= lsbit) {
2913 // bfc or bfi - bitfield clear/insert.
2914 uint32_t rd_val =
Steve Block1e0659c2011-05-24 12:43:12 +01002915 static_cast<uint32_t>(get_register(instr->RdValue()));
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002916 uint32_t bitcount = msbit - lsbit + 1;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002917 uint32_t mask = 0xffffffffu >> (32 - bitcount);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002918 rd_val &= ~(mask << lsbit);
Steve Block1e0659c2011-05-24 12:43:12 +01002919 if (instr->RmValue() != 15) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002920 // bfi - bitfield insert.
2921 uint32_t rm_val =
Steve Block1e0659c2011-05-24 12:43:12 +01002922 static_cast<uint32_t>(get_register(instr->RmValue()));
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002923 rm_val &= mask;
2924 rd_val |= rm_val << lsbit;
2925 }
Steve Block1e0659c2011-05-24 12:43:12 +01002926 set_register(instr->RdValue(), rd_val);
Andrei Popescu31002712010-02-23 13:46:05 +00002927 } else {
2928 UNREACHABLE();
2929 }
2930 return;
2931 } else {
2932 // Format(instr, "'memop'cond'b 'rd, ['rn, +'shift_rm]'w");
2933 addr = rn_val + shifter_operand;
2934 if (instr->HasW()) {
2935 set_register(rn, addr);
2936 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002937 }
2938 break;
2939 }
2940 default: {
2941 UNREACHABLE();
2942 break;
2943 }
2944 }
2945 if (instr->HasB()) {
2946 if (instr->HasL()) {
2947 uint8_t byte = ReadB(addr);
2948 set_register(rd, byte);
2949 } else {
Andrei Popescu31002712010-02-23 13:46:05 +00002950 uint8_t byte = get_register(rd);
2951 WriteB(addr, byte);
Steve Blocka7e24c12009-10-30 11:49:00 +00002952 }
2953 } else {
2954 if (instr->HasL()) {
2955 set_register(rd, ReadW(addr, instr));
2956 } else {
2957 WriteW(addr, get_register(rd), instr);
2958 }
2959 }
2960}
2961
2962
Steve Block1e0659c2011-05-24 12:43:12 +01002963void Simulator::DecodeType4(Instruction* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002964 DCHECK(instr->Bit(22) == 0); // only allowed to be set in privileged mode
Steve Blocka7e24c12009-10-30 11:49:00 +00002965 if (instr->HasL()) {
2966 // Format(instr, "ldm'cond'pu 'rn'w, 'rlist");
2967 HandleRList(instr, true);
2968 } else {
2969 // Format(instr, "stm'cond'pu 'rn'w, 'rlist");
2970 HandleRList(instr, false);
2971 }
2972}
2973
2974
Steve Block1e0659c2011-05-24 12:43:12 +01002975void Simulator::DecodeType5(Instruction* instr) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002976 // Format(instr, "b'l'cond 'target");
Steve Block1e0659c2011-05-24 12:43:12 +01002977 int off = (instr->SImmed24Value() << 2);
Steve Blocka7e24c12009-10-30 11:49:00 +00002978 intptr_t pc_address = get_pc();
2979 if (instr->HasLink()) {
Steve Block1e0659c2011-05-24 12:43:12 +01002980 set_register(lr, pc_address + Instruction::kInstrSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00002981 }
2982 int pc_reg = get_register(pc);
2983 set_pc(pc_reg + off);
2984}
2985
2986
Steve Block1e0659c2011-05-24 12:43:12 +01002987void Simulator::DecodeType6(Instruction* instr) {
Steve Blockd0582a62009-12-15 09:54:21 +00002988 DecodeType6CoprocessorIns(instr);
Steve Blocka7e24c12009-10-30 11:49:00 +00002989}
2990
2991
Steve Block1e0659c2011-05-24 12:43:12 +01002992void Simulator::DecodeType7(Instruction* instr) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002993 if (instr->Bit(24) == 1) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002994 SoftwareInterrupt(instr);
2995 } else {
Steve Blockd0582a62009-12-15 09:54:21 +00002996 DecodeTypeVFP(instr);
Steve Blocka7e24c12009-10-30 11:49:00 +00002997 }
2998}
2999
3000
Steve Block1e0659c2011-05-24 12:43:12 +01003001// void Simulator::DecodeTypeVFP(Instruction* instr)
Steve Blockd0582a62009-12-15 09:54:21 +00003002// The Following ARMv7 VFPv instructions are currently supported.
Leon Clarkee46be812010-01-19 14:06:41 +00003003// vmov :Sn = Rt
3004// vmov :Rt = Sn
3005// vcvt: Dd = Sm
3006// vcvt: Sd = Dm
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003007// vcvt.f64.s32 Dd, Dd, #<fbits>
Steve Block44f0eee2011-05-26 01:26:41 +01003008// Dd = vabs(Dm)
3009// Dd = vneg(Dm)
Leon Clarkee46be812010-01-19 14:06:41 +00003010// Dd = vadd(Dn, Dm)
3011// Dd = vsub(Dn, Dm)
3012// Dd = vmul(Dn, Dm)
3013// Dd = vdiv(Dn, Dm)
Steve Blockd0582a62009-12-15 09:54:21 +00003014// vcmp(Dd, Dm)
Steve Block8defd9f2010-07-08 12:39:36 +01003015// vmrs
3016// Dd = vsqrt(Dm)
Steve Block1e0659c2011-05-24 12:43:12 +01003017void Simulator::DecodeTypeVFP(Instruction* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003018 DCHECK((instr->TypeValue() == 7) && (instr->Bit(24) == 0x0) );
3019 DCHECK(instr->Bits(11, 9) == 0x5);
Steve Blockd0582a62009-12-15 09:54:21 +00003020
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003021 // Obtain double precision register codes.
Steve Block1e0659c2011-05-24 12:43:12 +01003022 int vm = instr->VFPMRegValue(kDoublePrecision);
3023 int vd = instr->VFPDRegValue(kDoublePrecision);
3024 int vn = instr->VFPNRegValue(kDoublePrecision);
Steve Blockd0582a62009-12-15 09:54:21 +00003025
Steve Block6ded16b2010-05-10 14:33:55 +01003026 if (instr->Bit(4) == 0) {
Steve Block1e0659c2011-05-24 12:43:12 +01003027 if (instr->Opc1Value() == 0x7) {
Steve Block6ded16b2010-05-10 14:33:55 +01003028 // Other data processing instructions
Steve Block1e0659c2011-05-24 12:43:12 +01003029 if ((instr->Opc2Value() == 0x0) && (instr->Opc3Value() == 0x1)) {
Steve Block8defd9f2010-07-08 12:39:36 +01003030 // vmov register to register.
Steve Block1e0659c2011-05-24 12:43:12 +01003031 if (instr->SzValue() == 0x1) {
3032 int m = instr->VFPMRegValue(kDoublePrecision);
3033 int d = instr->VFPDRegValue(kDoublePrecision);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003034 set_d_register_from_double(d, get_double_from_d_register(m));
Steve Block8defd9f2010-07-08 12:39:36 +01003035 } else {
Steve Block1e0659c2011-05-24 12:43:12 +01003036 int m = instr->VFPMRegValue(kSinglePrecision);
3037 int d = instr->VFPDRegValue(kSinglePrecision);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003038 set_s_register_from_float(d, get_float_from_s_register(m));
Steve Block8defd9f2010-07-08 12:39:36 +01003039 }
Steve Block1e0659c2011-05-24 12:43:12 +01003040 } else if ((instr->Opc2Value() == 0x0) && (instr->Opc3Value() == 0x3)) {
3041 // vabs
3042 double dm_value = get_double_from_d_register(vm);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003043 double dd_value = std::fabs(dm_value);
3044 dd_value = canonicalizeNaN(dd_value);
Steve Block1e0659c2011-05-24 12:43:12 +01003045 set_d_register_from_double(vd, dd_value);
Steve Block44f0eee2011-05-26 01:26:41 +01003046 } else if ((instr->Opc2Value() == 0x1) && (instr->Opc3Value() == 0x1)) {
3047 // vneg
3048 double dm_value = get_double_from_d_register(vm);
3049 double dd_value = -dm_value;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003050 dd_value = canonicalizeNaN(dd_value);
Steve Block44f0eee2011-05-26 01:26:41 +01003051 set_d_register_from_double(vd, dd_value);
Steve Block1e0659c2011-05-24 12:43:12 +01003052 } else if ((instr->Opc2Value() == 0x7) && (instr->Opc3Value() == 0x3)) {
Steve Block6ded16b2010-05-10 14:33:55 +01003053 DecodeVCVTBetweenDoubleAndSingle(instr);
Steve Block1e0659c2011-05-24 12:43:12 +01003054 } else if ((instr->Opc2Value() == 0x8) && (instr->Opc3Value() & 0x1)) {
Steve Block6ded16b2010-05-10 14:33:55 +01003055 DecodeVCVTBetweenFloatingPointAndInteger(instr);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003056 } else if ((instr->Opc2Value() == 0xA) && (instr->Opc3Value() == 0x3) &&
3057 (instr->Bit(8) == 1)) {
3058 // vcvt.f64.s32 Dd, Dd, #<fbits>
3059 int fraction_bits = 32 - ((instr->Bits(3, 0) << 1) | instr->Bit(5));
3060 int fixed_value = get_sinteger_from_s_register(vd * 2);
3061 double divide = 1 << fraction_bits;
3062 set_d_register_from_double(vd, fixed_value / divide);
Steve Block1e0659c2011-05-24 12:43:12 +01003063 } else if (((instr->Opc2Value() >> 1) == 0x6) &&
3064 (instr->Opc3Value() & 0x1)) {
Steve Block6ded16b2010-05-10 14:33:55 +01003065 DecodeVCVTBetweenFloatingPointAndInteger(instr);
Steve Block1e0659c2011-05-24 12:43:12 +01003066 } else if (((instr->Opc2Value() == 0x4) || (instr->Opc2Value() == 0x5)) &&
3067 (instr->Opc3Value() & 0x1)) {
Steve Block6ded16b2010-05-10 14:33:55 +01003068 DecodeVCMP(instr);
Steve Block1e0659c2011-05-24 12:43:12 +01003069 } else if (((instr->Opc2Value() == 0x1)) && (instr->Opc3Value() == 0x3)) {
Steve Block8defd9f2010-07-08 12:39:36 +01003070 // vsqrt
3071 double dm_value = get_double_from_d_register(vm);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003072 double dd_value = std::sqrt(dm_value);
3073 dd_value = canonicalizeNaN(dd_value);
Steve Block8defd9f2010-07-08 12:39:36 +01003074 set_d_register_from_double(vd, dd_value);
Steve Block1e0659c2011-05-24 12:43:12 +01003075 } else if (instr->Opc3Value() == 0x0) {
Ben Murdoch3bec4d22010-07-22 14:51:16 +01003076 // vmov immediate.
Steve Block1e0659c2011-05-24 12:43:12 +01003077 if (instr->SzValue() == 0x1) {
Ben Murdoch3bec4d22010-07-22 14:51:16 +01003078 set_d_register_from_double(vd, instr->DoubleImmedVmov());
3079 } else {
3080 UNREACHABLE(); // Not used by v8.
3081 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003082 } else if (((instr->Opc2Value() == 0x6)) && (instr->Opc3Value() == 0x3)) {
3083 // vrintz - truncate
3084 double dm_value = get_double_from_d_register(vm);
3085 double dd_value = trunc(dm_value);
3086 dd_value = canonicalizeNaN(dd_value);
3087 set_d_register_from_double(vd, dd_value);
Steve Block6ded16b2010-05-10 14:33:55 +01003088 } else {
3089 UNREACHABLE(); // Not used by V8.
3090 }
Steve Block1e0659c2011-05-24 12:43:12 +01003091 } else if (instr->Opc1Value() == 0x3) {
3092 if (instr->SzValue() != 0x1) {
Steve Block6ded16b2010-05-10 14:33:55 +01003093 UNREACHABLE(); // Not used by V8.
3094 }
3095
Steve Block1e0659c2011-05-24 12:43:12 +01003096 if (instr->Opc3Value() & 0x1) {
Steve Block6ded16b2010-05-10 14:33:55 +01003097 // vsub
3098 double dn_value = get_double_from_d_register(vn);
3099 double dm_value = get_double_from_d_register(vm);
3100 double dd_value = dn_value - dm_value;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003101 dd_value = canonicalizeNaN(dd_value);
Steve Block6ded16b2010-05-10 14:33:55 +01003102 set_d_register_from_double(vd, dd_value);
3103 } else {
3104 // vadd
3105 double dn_value = get_double_from_d_register(vn);
3106 double dm_value = get_double_from_d_register(vm);
3107 double dd_value = dn_value + dm_value;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003108 dd_value = canonicalizeNaN(dd_value);
Steve Block6ded16b2010-05-10 14:33:55 +01003109 set_d_register_from_double(vd, dd_value);
3110 }
Steve Block1e0659c2011-05-24 12:43:12 +01003111 } else if ((instr->Opc1Value() == 0x2) && !(instr->Opc3Value() & 0x1)) {
Steve Block6ded16b2010-05-10 14:33:55 +01003112 // vmul
Steve Block1e0659c2011-05-24 12:43:12 +01003113 if (instr->SzValue() != 0x1) {
Steve Block6ded16b2010-05-10 14:33:55 +01003114 UNREACHABLE(); // Not used by V8.
3115 }
3116
3117 double dn_value = get_double_from_d_register(vn);
3118 double dm_value = get_double_from_d_register(vm);
3119 double dd_value = dn_value * dm_value;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003120 dd_value = canonicalizeNaN(dd_value);
Steve Block6ded16b2010-05-10 14:33:55 +01003121 set_d_register_from_double(vd, dd_value);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003122 } else if ((instr->Opc1Value() == 0x0)) {
3123 // vmla, vmls
3124 const bool is_vmls = (instr->Opc3Value() & 0x1);
3125
3126 if (instr->SzValue() != 0x1) {
3127 UNREACHABLE(); // Not used by V8.
3128 }
3129
3130 const double dd_val = get_double_from_d_register(vd);
3131 const double dn_val = get_double_from_d_register(vn);
3132 const double dm_val = get_double_from_d_register(vm);
3133
3134 // Note: we do the mul and add/sub in separate steps to avoid getting a
3135 // result with too high precision.
3136 set_d_register_from_double(vd, dn_val * dm_val);
3137 if (is_vmls) {
3138 set_d_register_from_double(
3139 vd,
3140 canonicalizeNaN(dd_val - get_double_from_d_register(vd)));
3141 } else {
3142 set_d_register_from_double(
3143 vd,
3144 canonicalizeNaN(dd_val + get_double_from_d_register(vd)));
3145 }
Steve Block1e0659c2011-05-24 12:43:12 +01003146 } else if ((instr->Opc1Value() == 0x4) && !(instr->Opc3Value() & 0x1)) {
Steve Block6ded16b2010-05-10 14:33:55 +01003147 // vdiv
Steve Block1e0659c2011-05-24 12:43:12 +01003148 if (instr->SzValue() != 0x1) {
Steve Block6ded16b2010-05-10 14:33:55 +01003149 UNREACHABLE(); // Not used by V8.
3150 }
3151
Steve Blockd0582a62009-12-15 09:54:21 +00003152 double dn_value = get_double_from_d_register(vn);
3153 double dm_value = get_double_from_d_register(vm);
3154 double dd_value = dn_value / dm_value;
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003155 div_zero_vfp_flag_ = (dm_value == 0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003156 dd_value = canonicalizeNaN(dd_value);
Steve Blockd0582a62009-12-15 09:54:21 +00003157 set_d_register_from_double(vd, dd_value);
Steve Block6ded16b2010-05-10 14:33:55 +01003158 } else {
3159 UNIMPLEMENTED(); // Not used by V8.
3160 }
3161 } else {
Steve Block1e0659c2011-05-24 12:43:12 +01003162 if ((instr->VCValue() == 0x0) &&
3163 (instr->VAValue() == 0x0)) {
Steve Block6ded16b2010-05-10 14:33:55 +01003164 DecodeVMOVBetweenCoreAndSinglePrecisionRegisters(instr);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003165 } else if ((instr->VLValue() == 0x0) &&
3166 (instr->VCValue() == 0x1) &&
3167 (instr->Bit(23) == 0x0)) {
3168 // vmov (ARM core register to scalar)
3169 int vd = instr->Bits(19, 16) | (instr->Bit(7) << 4);
3170 double dd_value = get_double_from_d_register(vd);
3171 int32_t data[2];
3172 memcpy(data, &dd_value, 8);
3173 data[instr->Bit(21)] = get_register(instr->RtValue());
3174 memcpy(&dd_value, data, 8);
3175 set_d_register_from_double(vd, dd_value);
3176 } else if ((instr->VLValue() == 0x1) &&
3177 (instr->VCValue() == 0x1) &&
3178 (instr->Bit(23) == 0x0)) {
3179 // vmov (scalar to ARM core register)
3180 int vn = instr->Bits(19, 16) | (instr->Bit(7) << 4);
3181 double dn_value = get_double_from_d_register(vn);
3182 int32_t data[2];
3183 memcpy(data, &dn_value, 8);
3184 set_register(instr->RtValue(), data[instr->Bit(21)]);
Steve Block1e0659c2011-05-24 12:43:12 +01003185 } else if ((instr->VLValue() == 0x1) &&
3186 (instr->VCValue() == 0x0) &&
3187 (instr->VAValue() == 0x7) &&
Steve Block6ded16b2010-05-10 14:33:55 +01003188 (instr->Bits(19, 16) == 0x1)) {
3189 // vmrs
Steve Block1e0659c2011-05-24 12:43:12 +01003190 uint32_t rt = instr->RtValue();
Russell Brenner90bac252010-11-18 13:33:46 -08003191 if (rt == 0xF) {
Steve Blockd0582a62009-12-15 09:54:21 +00003192 Copy_FPSCR_to_APSR();
Russell Brenner90bac252010-11-18 13:33:46 -08003193 } else {
3194 // Emulate FPSCR from the Simulator flags.
3195 uint32_t fpscr = (n_flag_FPSCR_ << 31) |
3196 (z_flag_FPSCR_ << 30) |
3197 (c_flag_FPSCR_ << 29) |
3198 (v_flag_FPSCR_ << 28) |
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003199 (FPSCR_default_NaN_mode_ << 25) |
Russell Brenner90bac252010-11-18 13:33:46 -08003200 (inexact_vfp_flag_ << 4) |
3201 (underflow_vfp_flag_ << 3) |
3202 (overflow_vfp_flag_ << 2) |
3203 (div_zero_vfp_flag_ << 1) |
3204 (inv_op_vfp_flag_ << 0) |
Steve Block1e0659c2011-05-24 12:43:12 +01003205 (FPSCR_rounding_mode_);
Russell Brenner90bac252010-11-18 13:33:46 -08003206 set_register(rt, fpscr);
3207 }
Steve Block1e0659c2011-05-24 12:43:12 +01003208 } else if ((instr->VLValue() == 0x0) &&
3209 (instr->VCValue() == 0x0) &&
3210 (instr->VAValue() == 0x7) &&
Russell Brenner90bac252010-11-18 13:33:46 -08003211 (instr->Bits(19, 16) == 0x1)) {
3212 // vmsr
Steve Block1e0659c2011-05-24 12:43:12 +01003213 uint32_t rt = instr->RtValue();
Russell Brenner90bac252010-11-18 13:33:46 -08003214 if (rt == pc) {
3215 UNREACHABLE();
3216 } else {
3217 uint32_t rt_value = get_register(rt);
3218 n_flag_FPSCR_ = (rt_value >> 31) & 1;
3219 z_flag_FPSCR_ = (rt_value >> 30) & 1;
3220 c_flag_FPSCR_ = (rt_value >> 29) & 1;
3221 v_flag_FPSCR_ = (rt_value >> 28) & 1;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003222 FPSCR_default_NaN_mode_ = (rt_value >> 25) & 1;
Russell Brenner90bac252010-11-18 13:33:46 -08003223 inexact_vfp_flag_ = (rt_value >> 4) & 1;
3224 underflow_vfp_flag_ = (rt_value >> 3) & 1;
3225 overflow_vfp_flag_ = (rt_value >> 2) & 1;
3226 div_zero_vfp_flag_ = (rt_value >> 1) & 1;
3227 inv_op_vfp_flag_ = (rt_value >> 0) & 1;
3228 FPSCR_rounding_mode_ =
Steve Block1e0659c2011-05-24 12:43:12 +01003229 static_cast<VFPRoundingMode>((rt_value) & kVFPRoundingModeMask);
Russell Brenner90bac252010-11-18 13:33:46 -08003230 }
Steve Blockd0582a62009-12-15 09:54:21 +00003231 } else {
3232 UNIMPLEMENTED(); // Not used by V8.
3233 }
Steve Block6ded16b2010-05-10 14:33:55 +01003234 }
3235}
3236
3237
Steve Block1e0659c2011-05-24 12:43:12 +01003238void Simulator::DecodeVMOVBetweenCoreAndSinglePrecisionRegisters(
3239 Instruction* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003240 DCHECK((instr->Bit(4) == 1) && (instr->VCValue() == 0x0) &&
Steve Block1e0659c2011-05-24 12:43:12 +01003241 (instr->VAValue() == 0x0));
Steve Block6ded16b2010-05-10 14:33:55 +01003242
Steve Block1e0659c2011-05-24 12:43:12 +01003243 int t = instr->RtValue();
3244 int n = instr->VFPNRegValue(kSinglePrecision);
3245 bool to_arm_register = (instr->VLValue() == 0x1);
Steve Block6ded16b2010-05-10 14:33:55 +01003246
3247 if (to_arm_register) {
3248 int32_t int_value = get_sinteger_from_s_register(n);
3249 set_register(t, int_value);
3250 } else {
3251 int32_t rs_val = get_register(t);
3252 set_s_register_from_sinteger(n, rs_val);
3253 }
3254}
3255
3256
Steve Block1e0659c2011-05-24 12:43:12 +01003257void Simulator::DecodeVCMP(Instruction* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003258 DCHECK((instr->Bit(4) == 0) && (instr->Opc1Value() == 0x7));
3259 DCHECK(((instr->Opc2Value() == 0x4) || (instr->Opc2Value() == 0x5)) &&
Steve Block1e0659c2011-05-24 12:43:12 +01003260 (instr->Opc3Value() & 0x1));
Steve Block6ded16b2010-05-10 14:33:55 +01003261 // Comparison.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003262
3263 VFPRegPrecision precision = kSinglePrecision;
Steve Block1e0659c2011-05-24 12:43:12 +01003264 if (instr->SzValue() == 1) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003265 precision = kDoublePrecision;
3266 }
Steve Block6ded16b2010-05-10 14:33:55 +01003267
Steve Block1e0659c2011-05-24 12:43:12 +01003268 int d = instr->VFPDRegValue(precision);
Iain Merrick75681382010-08-19 15:07:18 +01003269 int m = 0;
Steve Block1e0659c2011-05-24 12:43:12 +01003270 if (instr->Opc2Value() == 0x4) {
3271 m = instr->VFPMRegValue(precision);
Iain Merrick75681382010-08-19 15:07:18 +01003272 }
Steve Block6ded16b2010-05-10 14:33:55 +01003273
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003274 if (precision == kDoublePrecision) {
Steve Block6ded16b2010-05-10 14:33:55 +01003275 double dd_value = get_double_from_d_register(d);
Iain Merrick75681382010-08-19 15:07:18 +01003276 double dm_value = 0.0;
Steve Block1e0659c2011-05-24 12:43:12 +01003277 if (instr->Opc2Value() == 0x4) {
Iain Merrick75681382010-08-19 15:07:18 +01003278 dm_value = get_double_from_d_register(m);
3279 }
Steve Block6ded16b2010-05-10 14:33:55 +01003280
Ben Murdoch086aeea2011-05-13 15:57:08 +01003281 // Raise exceptions for quiet NaNs if necessary.
3282 if (instr->Bit(7) == 1) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003283 if (std::isnan(dd_value)) {
Ben Murdoch086aeea2011-05-13 15:57:08 +01003284 inv_op_vfp_flag_ = true;
3285 }
3286 }
3287
Steve Block6ded16b2010-05-10 14:33:55 +01003288 Compute_FPSCR_Flags(dd_value, dm_value);
3289 } else {
3290 UNIMPLEMENTED(); // Not used by V8.
3291 }
3292}
3293
3294
Steve Block1e0659c2011-05-24 12:43:12 +01003295void Simulator::DecodeVCVTBetweenDoubleAndSingle(Instruction* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003296 DCHECK((instr->Bit(4) == 0) && (instr->Opc1Value() == 0x7));
3297 DCHECK((instr->Opc2Value() == 0x7) && (instr->Opc3Value() == 0x3));
Steve Block6ded16b2010-05-10 14:33:55 +01003298
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003299 VFPRegPrecision dst_precision = kDoublePrecision;
3300 VFPRegPrecision src_precision = kSinglePrecision;
Steve Block1e0659c2011-05-24 12:43:12 +01003301 if (instr->SzValue() == 1) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003302 dst_precision = kSinglePrecision;
3303 src_precision = kDoublePrecision;
3304 }
Steve Block6ded16b2010-05-10 14:33:55 +01003305
Steve Block1e0659c2011-05-24 12:43:12 +01003306 int dst = instr->VFPDRegValue(dst_precision);
3307 int src = instr->VFPMRegValue(src_precision);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003308
3309 if (dst_precision == kSinglePrecision) {
Steve Block6ded16b2010-05-10 14:33:55 +01003310 double val = get_double_from_d_register(src);
3311 set_s_register_from_float(dst, static_cast<float>(val));
3312 } else {
3313 float val = get_float_from_s_register(src);
3314 set_d_register_from_double(dst, static_cast<double>(val));
3315 }
3316}
3317
Steve Block1e0659c2011-05-24 12:43:12 +01003318bool get_inv_op_vfp_flag(VFPRoundingMode mode,
3319 double val,
3320 bool unsigned_) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003321 DCHECK((mode == RN) || (mode == RM) || (mode == RZ));
Steve Block1e0659c2011-05-24 12:43:12 +01003322 double max_uint = static_cast<double>(0xffffffffu);
3323 double max_int = static_cast<double>(kMaxInt);
3324 double min_int = static_cast<double>(kMinInt);
Steve Block6ded16b2010-05-10 14:33:55 +01003325
Steve Block1e0659c2011-05-24 12:43:12 +01003326 // Check for NaN.
3327 if (val != val) {
3328 return true;
3329 }
3330
3331 // Check for overflow. This code works because 32bit integers can be
3332 // exactly represented by ieee-754 64bit floating-point values.
3333 switch (mode) {
3334 case RN:
3335 return unsigned_ ? (val >= (max_uint + 0.5)) ||
3336 (val < -0.5)
3337 : (val >= (max_int + 0.5)) ||
3338 (val < (min_int - 0.5));
3339
3340 case RM:
3341 return unsigned_ ? (val >= (max_uint + 1.0)) ||
3342 (val < 0)
3343 : (val >= (max_int + 1.0)) ||
3344 (val < min_int);
3345
3346 case RZ:
3347 return unsigned_ ? (val >= (max_uint + 1.0)) ||
3348 (val <= -1)
3349 : (val >= (max_int + 1.0)) ||
3350 (val <= (min_int - 1.0));
3351 default:
3352 UNREACHABLE();
3353 return true;
3354 }
3355}
3356
3357
3358// We call this function only if we had a vfp invalid exception.
3359// It returns the correct saturated value.
3360int VFPConversionSaturate(double val, bool unsigned_res) {
3361 if (val != val) {
3362 return 0;
3363 } else {
3364 if (unsigned_res) {
3365 return (val < 0) ? 0 : 0xffffffffu;
3366 } else {
3367 return (val < 0) ? kMinInt : kMaxInt;
3368 }
3369 }
3370}
3371
3372
3373void Simulator::DecodeVCVTBetweenFloatingPointAndInteger(Instruction* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003374 DCHECK((instr->Bit(4) == 0) && (instr->Opc1Value() == 0x7) &&
Steve Block1e0659c2011-05-24 12:43:12 +01003375 (instr->Bits(27, 23) == 0x1D));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003376 DCHECK(((instr->Opc2Value() == 0x8) && (instr->Opc3Value() & 0x1)) ||
Steve Block1e0659c2011-05-24 12:43:12 +01003377 (((instr->Opc2Value() >> 1) == 0x6) && (instr->Opc3Value() & 0x1)));
Steve Block6ded16b2010-05-10 14:33:55 +01003378
3379 // Conversion between floating-point and integer.
Steve Block6ded16b2010-05-10 14:33:55 +01003380 bool to_integer = (instr->Bit(18) == 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003381
Steve Block1e0659c2011-05-24 12:43:12 +01003382 VFPRegPrecision src_precision = (instr->SzValue() == 1) ? kDoublePrecision
3383 : kSinglePrecision;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003384
Steve Block6ded16b2010-05-10 14:33:55 +01003385 if (to_integer) {
Steve Block1e0659c2011-05-24 12:43:12 +01003386 // We are playing with code close to the C++ standard's limits below,
3387 // hence the very simple code and heavy checks.
3388 //
3389 // Note:
3390 // C++ defines default type casting from floating point to integer as
3391 // (close to) rounding toward zero ("fractional part discarded").
3392
3393 int dst = instr->VFPDRegValue(kSinglePrecision);
3394 int src = instr->VFPMRegValue(src_precision);
3395
3396 // Bit 7 in vcvt instructions indicates if we should use the FPSCR rounding
3397 // mode or the default Round to Zero mode.
3398 VFPRoundingMode mode = (instr->Bit(7) != 1) ? FPSCR_rounding_mode_
3399 : RZ;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003400 DCHECK((mode == RM) || (mode == RZ) || (mode == RN));
Steve Block1e0659c2011-05-24 12:43:12 +01003401
Steve Block6ded16b2010-05-10 14:33:55 +01003402 bool unsigned_integer = (instr->Bit(16) == 0);
Steve Block1e0659c2011-05-24 12:43:12 +01003403 bool double_precision = (src_precision == kDoublePrecision);
3404
3405 double val = double_precision ? get_double_from_d_register(src)
3406 : get_float_from_s_register(src);
3407
3408 int temp = unsigned_integer ? static_cast<uint32_t>(val)
3409 : static_cast<int32_t>(val);
3410
3411 inv_op_vfp_flag_ = get_inv_op_vfp_flag(mode, val, unsigned_integer);
3412
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003413 double abs_diff =
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003414 unsigned_integer ? std::fabs(val - static_cast<uint32_t>(temp))
3415 : std::fabs(val - temp);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003416
3417 inexact_vfp_flag_ = (abs_diff != 0);
3418
Steve Block1e0659c2011-05-24 12:43:12 +01003419 if (inv_op_vfp_flag_) {
3420 temp = VFPConversionSaturate(val, unsigned_integer);
Russell Brenner90bac252010-11-18 13:33:46 -08003421 } else {
Steve Block1e0659c2011-05-24 12:43:12 +01003422 switch (mode) {
3423 case RN: {
Steve Block1e0659c2011-05-24 12:43:12 +01003424 int val_sign = (val > 0) ? 1 : -1;
3425 if (abs_diff > 0.5) {
3426 temp += val_sign;
3427 } else if (abs_diff == 0.5) {
3428 // Round to even if exactly halfway.
3429 temp = ((temp % 2) == 0) ? temp : temp + val_sign;
3430 }
3431 break;
3432 }
3433
3434 case RM:
3435 temp = temp > val ? temp - 1 : temp;
3436 break;
3437
3438 case RZ:
3439 // Nothing to do.
3440 break;
3441
3442 default:
3443 UNREACHABLE();
3444 }
Steve Blockd0582a62009-12-15 09:54:21 +00003445 }
Steve Block6ded16b2010-05-10 14:33:55 +01003446
Steve Block1e0659c2011-05-24 12:43:12 +01003447 // Update the destination register.
3448 set_s_register_from_sinteger(dst, temp);
Russell Brenner90bac252010-11-18 13:33:46 -08003449
Steve Block6ded16b2010-05-10 14:33:55 +01003450 } else {
3451 bool unsigned_integer = (instr->Bit(7) == 0);
3452
Steve Block1e0659c2011-05-24 12:43:12 +01003453 int dst = instr->VFPDRegValue(src_precision);
3454 int src = instr->VFPMRegValue(kSinglePrecision);
Steve Block6ded16b2010-05-10 14:33:55 +01003455
3456 int val = get_sinteger_from_s_register(src);
3457
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003458 if (src_precision == kDoublePrecision) {
Steve Block6ded16b2010-05-10 14:33:55 +01003459 if (unsigned_integer) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003460 set_d_register_from_double(
3461 dst, static_cast<double>(static_cast<uint32_t>(val)));
Steve Block6ded16b2010-05-10 14:33:55 +01003462 } else {
3463 set_d_register_from_double(dst, static_cast<double>(val));
3464 }
3465 } else {
3466 if (unsigned_integer) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003467 set_s_register_from_float(
3468 dst, static_cast<float>(static_cast<uint32_t>(val)));
Steve Block6ded16b2010-05-10 14:33:55 +01003469 } else {
3470 set_s_register_from_float(dst, static_cast<float>(val));
3471 }
Steve Blockd0582a62009-12-15 09:54:21 +00003472 }
3473 }
3474}
3475
3476
Steve Block1e0659c2011-05-24 12:43:12 +01003477// void Simulator::DecodeType6CoprocessorIns(Instruction* instr)
Steve Blockd0582a62009-12-15 09:54:21 +00003478// Decode Type 6 coprocessor instructions.
Leon Clarkee46be812010-01-19 14:06:41 +00003479// Dm = vmov(Rt, Rt2)
3480// <Rt, Rt2> = vmov(Dm)
Leon Clarked91b9f72010-01-27 17:25:45 +00003481// Ddst = MEM(Rbase + 4*offset).
3482// MEM(Rbase + 4*offset) = Dsrc.
Steve Block1e0659c2011-05-24 12:43:12 +01003483void Simulator::DecodeType6CoprocessorIns(Instruction* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003484 DCHECK((instr->TypeValue() == 6));
Steve Blockd0582a62009-12-15 09:54:21 +00003485
Steve Block1e0659c2011-05-24 12:43:12 +01003486 if (instr->CoprocessorValue() == 0xA) {
3487 switch (instr->OpcodeValue()) {
Steve Block6ded16b2010-05-10 14:33:55 +01003488 case 0x8:
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003489 case 0xA:
3490 case 0xC:
3491 case 0xE: { // Load and store single precision float to memory.
Steve Block1e0659c2011-05-24 12:43:12 +01003492 int rn = instr->RnValue();
3493 int vd = instr->VFPDRegValue(kSinglePrecision);
3494 int offset = instr->Immed8Value();
Steve Block6ded16b2010-05-10 14:33:55 +01003495 if (!instr->HasU()) {
3496 offset = -offset;
3497 }
3498
3499 int32_t address = get_register(rn) + 4 * offset;
3500 if (instr->HasL()) {
3501 // Load double from memory: vldr.
3502 set_s_register_from_sinteger(vd, ReadW(address, instr));
3503 } else {
3504 // Store double to memory: vstr.
3505 WriteW(address, get_sinteger_from_s_register(vd), instr);
3506 }
3507 break;
3508 }
Ben Murdoch8b112d22011-06-08 16:22:53 +01003509 case 0x4:
3510 case 0x5:
3511 case 0x6:
3512 case 0x7:
3513 case 0x9:
3514 case 0xB:
3515 // Load/store multiple single from memory: vldm/vstm.
3516 HandleVList(instr);
3517 break;
Steve Block6ded16b2010-05-10 14:33:55 +01003518 default:
3519 UNIMPLEMENTED(); // Not used by V8.
Steve Block6ded16b2010-05-10 14:33:55 +01003520 }
Steve Block1e0659c2011-05-24 12:43:12 +01003521 } else if (instr->CoprocessorValue() == 0xB) {
3522 switch (instr->OpcodeValue()) {
Leon Clarked91b9f72010-01-27 17:25:45 +00003523 case 0x2:
3524 // Load and store double to two GP registers
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003525 if (instr->Bits(7, 6) != 0 || instr->Bit(4) != 1) {
Leon Clarked91b9f72010-01-27 17:25:45 +00003526 UNIMPLEMENTED(); // Not used by V8.
3527 } else {
Steve Block1e0659c2011-05-24 12:43:12 +01003528 int rt = instr->RtValue();
3529 int rn = instr->RnValue();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003530 int vm = instr->VFPMRegValue(kDoublePrecision);
Leon Clarked91b9f72010-01-27 17:25:45 +00003531 if (instr->HasL()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003532 int32_t data[2];
3533 double d = get_double_from_d_register(vm);
3534 memcpy(data, &d, 8);
3535 set_register(rt, data[0]);
3536 set_register(rn, data[1]);
Leon Clarked91b9f72010-01-27 17:25:45 +00003537 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003538 int32_t data[] = { get_register(rt), get_register(rn) };
3539 double d;
3540 memcpy(&d, data, 8);
3541 set_d_register_from_double(vm, d);
Leon Clarked91b9f72010-01-27 17:25:45 +00003542 }
3543 }
3544 break;
3545 case 0x8:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003546 case 0xA:
3547 case 0xC:
3548 case 0xE: { // Load and store double to memory.
Steve Block1e0659c2011-05-24 12:43:12 +01003549 int rn = instr->RnValue();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003550 int vd = instr->VFPDRegValue(kDoublePrecision);
Steve Block1e0659c2011-05-24 12:43:12 +01003551 int offset = instr->Immed8Value();
Leon Clarked91b9f72010-01-27 17:25:45 +00003552 if (!instr->HasU()) {
3553 offset = -offset;
3554 }
3555 int32_t address = get_register(rn) + 4 * offset;
3556 if (instr->HasL()) {
3557 // Load double from memory: vldr.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003558 int32_t data[] = {
3559 ReadW(address, instr),
3560 ReadW(address + 4, instr)
3561 };
3562 double val;
3563 memcpy(&val, data, 8);
3564 set_d_register_from_double(vd, val);
Leon Clarked91b9f72010-01-27 17:25:45 +00003565 } else {
3566 // Store double to memory: vstr.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003567 int32_t data[2];
3568 double val = get_double_from_d_register(vd);
3569 memcpy(data, &val, 8);
3570 WriteW(address, data[0], instr);
3571 WriteW(address + 4, data[1], instr);
Leon Clarked91b9f72010-01-27 17:25:45 +00003572 }
3573 break;
3574 }
Ben Murdoch8b112d22011-06-08 16:22:53 +01003575 case 0x4:
3576 case 0x5:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003577 case 0x6:
3578 case 0x7:
Ben Murdoch8b112d22011-06-08 16:22:53 +01003579 case 0x9:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003580 case 0xB:
Ben Murdoch8b112d22011-06-08 16:22:53 +01003581 // Load/store multiple double from memory: vldm/vstm.
3582 HandleVList(instr);
3583 break;
Leon Clarked91b9f72010-01-27 17:25:45 +00003584 default:
3585 UNIMPLEMENTED(); // Not used by V8.
Leon Clarked91b9f72010-01-27 17:25:45 +00003586 }
Steve Block6ded16b2010-05-10 14:33:55 +01003587 } else {
3588 UNIMPLEMENTED(); // Not used by V8.
Steve Blockd0582a62009-12-15 09:54:21 +00003589 }
3590}
3591
3592
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003593void Simulator::DecodeSpecialCondition(Instruction* instr) {
3594 switch (instr->SpecialValue()) {
3595 case 5:
3596 if ((instr->Bits(18, 16) == 0) && (instr->Bits(11, 6) == 0x28) &&
3597 (instr->Bit(4) == 1)) {
3598 // vmovl signed
3599 if ((instr->VdValue() & 1) != 0) UNIMPLEMENTED();
3600 int Vd = (instr->Bit(22) << 3) | (instr->VdValue() >> 1);
3601 int Vm = (instr->Bit(5) << 4) | instr->VmValue();
3602 int imm3 = instr->Bits(21, 19);
3603 if ((imm3 != 1) && (imm3 != 2) && (imm3 != 4)) UNIMPLEMENTED();
3604 int esize = 8 * imm3;
3605 int elements = 64 / esize;
3606 int8_t from[8];
3607 get_d_register(Vm, reinterpret_cast<uint64_t*>(from));
3608 int16_t to[8];
3609 int e = 0;
3610 while (e < elements) {
3611 to[e] = from[e];
3612 e++;
3613 }
3614 set_q_register(Vd, reinterpret_cast<uint64_t*>(to));
3615 } else {
3616 UNIMPLEMENTED();
3617 }
3618 break;
3619 case 7:
3620 if ((instr->Bits(18, 16) == 0) && (instr->Bits(11, 6) == 0x28) &&
3621 (instr->Bit(4) == 1)) {
3622 // vmovl unsigned
3623 if ((instr->VdValue() & 1) != 0) UNIMPLEMENTED();
3624 int Vd = (instr->Bit(22) << 3) | (instr->VdValue() >> 1);
3625 int Vm = (instr->Bit(5) << 4) | instr->VmValue();
3626 int imm3 = instr->Bits(21, 19);
3627 if ((imm3 != 1) && (imm3 != 2) && (imm3 != 4)) UNIMPLEMENTED();
3628 int esize = 8 * imm3;
3629 int elements = 64 / esize;
3630 uint8_t from[8];
3631 get_d_register(Vm, reinterpret_cast<uint64_t*>(from));
3632 uint16_t to[8];
3633 int e = 0;
3634 while (e < elements) {
3635 to[e] = from[e];
3636 e++;
3637 }
3638 set_q_register(Vd, reinterpret_cast<uint64_t*>(to));
3639 } else {
3640 UNIMPLEMENTED();
3641 }
3642 break;
3643 case 8:
3644 if (instr->Bits(21, 20) == 0) {
3645 // vst1
3646 int Vd = (instr->Bit(22) << 4) | instr->VdValue();
3647 int Rn = instr->VnValue();
3648 int type = instr->Bits(11, 8);
3649 int Rm = instr->VmValue();
3650 int32_t address = get_register(Rn);
3651 int regs = 0;
3652 switch (type) {
3653 case nlt_1:
3654 regs = 1;
3655 break;
3656 case nlt_2:
3657 regs = 2;
3658 break;
3659 case nlt_3:
3660 regs = 3;
3661 break;
3662 case nlt_4:
3663 regs = 4;
3664 break;
3665 default:
3666 UNIMPLEMENTED();
3667 break;
3668 }
3669 int r = 0;
3670 while (r < regs) {
3671 uint32_t data[2];
3672 get_d_register(Vd + r, data);
3673 WriteW(address, data[0], instr);
3674 WriteW(address + 4, data[1], instr);
3675 address += 8;
3676 r++;
3677 }
3678 if (Rm != 15) {
3679 if (Rm == 13) {
3680 set_register(Rn, address);
3681 } else {
3682 set_register(Rn, get_register(Rn) + get_register(Rm));
3683 }
3684 }
3685 } else if (instr->Bits(21, 20) == 2) {
3686 // vld1
3687 int Vd = (instr->Bit(22) << 4) | instr->VdValue();
3688 int Rn = instr->VnValue();
3689 int type = instr->Bits(11, 8);
3690 int Rm = instr->VmValue();
3691 int32_t address = get_register(Rn);
3692 int regs = 0;
3693 switch (type) {
3694 case nlt_1:
3695 regs = 1;
3696 break;
3697 case nlt_2:
3698 regs = 2;
3699 break;
3700 case nlt_3:
3701 regs = 3;
3702 break;
3703 case nlt_4:
3704 regs = 4;
3705 break;
3706 default:
3707 UNIMPLEMENTED();
3708 break;
3709 }
3710 int r = 0;
3711 while (r < regs) {
3712 uint32_t data[2];
3713 data[0] = ReadW(address, instr);
3714 data[1] = ReadW(address + 4, instr);
3715 set_d_register(Vd + r, data);
3716 address += 8;
3717 r++;
3718 }
3719 if (Rm != 15) {
3720 if (Rm == 13) {
3721 set_register(Rn, address);
3722 } else {
3723 set_register(Rn, get_register(Rn) + get_register(Rm));
3724 }
3725 }
3726 } else {
3727 UNIMPLEMENTED();
3728 }
3729 break;
3730 case 0xA:
3731 case 0xB:
3732 if ((instr->Bits(22, 20) == 5) && (instr->Bits(15, 12) == 0xf)) {
3733 // pld: ignore instruction.
3734 } else {
3735 UNIMPLEMENTED();
3736 }
3737 break;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003738 case 0x1D:
3739 if (instr->Opc1Value() == 0x7 && instr->Opc3Value() == 0x1 &&
3740 instr->Bits(11, 9) == 0x5 && instr->Bits(19, 18) == 0x2 &&
3741 instr->Bit(8) == 0x1) {
3742 int vm = instr->VFPMRegValue(kDoublePrecision);
3743 int vd = instr->VFPDRegValue(kDoublePrecision);
3744 double dm_value = get_double_from_d_register(vm);
3745 double dd_value = 0.0;
3746 int rounding_mode = instr->Bits(17, 16);
3747 switch (rounding_mode) {
3748 case 0x0: // vrinta - round with ties to away from zero
3749 dd_value = round(dm_value);
3750 break;
3751 case 0x1: { // vrintn - round with ties to even
3752 dd_value = std::floor(dm_value);
3753 double error = dm_value - dd_value;
3754 // Take care of correctly handling the range [-0.5, -0.0], which
3755 // must yield -0.0.
3756 if ((-0.5 <= dm_value) && (dm_value < 0.0)) {
3757 dd_value = -0.0;
3758 // If the error is greater than 0.5, or is equal to 0.5 and the
3759 // integer result is odd, round up.
3760 } else if ((error > 0.5) ||
3761 ((error == 0.5) && (fmod(dd_value, 2) != 0))) {
3762 dd_value++;
3763 }
3764 break;
3765 }
3766 case 0x2: // vrintp - ceil
3767 dd_value = std::ceil(dm_value);
3768 break;
3769 case 0x3: // vrintm - floor
3770 dd_value = std::floor(dm_value);
3771 break;
3772 default:
3773 UNREACHABLE(); // Case analysis is exhaustive.
3774 break;
3775 }
3776 dd_value = canonicalizeNaN(dd_value);
3777 set_d_register_from_double(vd, dd_value);
3778 } else {
3779 UNIMPLEMENTED();
3780 }
3781 break;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003782 default:
3783 UNIMPLEMENTED();
3784 break;
3785 }
3786}
3787
3788
Steve Blocka7e24c12009-10-30 11:49:00 +00003789// Executes the current instruction.
Steve Block1e0659c2011-05-24 12:43:12 +01003790void Simulator::InstructionDecode(Instruction* instr) {
Steve Block6ded16b2010-05-10 14:33:55 +01003791 if (v8::internal::FLAG_check_icache) {
Steve Block44f0eee2011-05-26 01:26:41 +01003792 CheckICache(isolate_->simulator_i_cache(), instr);
Steve Block6ded16b2010-05-10 14:33:55 +01003793 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003794 pc_modified_ = false;
3795 if (::v8::internal::FLAG_trace_sim) {
3796 disasm::NameConverter converter;
3797 disasm::Disassembler dasm(converter);
3798 // use a reasonably large buffer
3799 v8::internal::EmbeddedVector<char, 256> buffer;
3800 dasm.InstructionDecode(buffer,
3801 reinterpret_cast<byte*>(instr));
Ben Murdochf87a2032010-10-22 12:50:53 +01003802 PrintF(" 0x%08x %s\n", reinterpret_cast<intptr_t>(instr), buffer.start());
Steve Blocka7e24c12009-10-30 11:49:00 +00003803 }
Steve Block1e0659c2011-05-24 12:43:12 +01003804 if (instr->ConditionField() == kSpecialCondition) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003805 DecodeSpecialCondition(instr);
Steve Blocka7e24c12009-10-30 11:49:00 +00003806 } else if (ConditionallyExecute(instr)) {
Steve Block1e0659c2011-05-24 12:43:12 +01003807 switch (instr->TypeValue()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00003808 case 0:
3809 case 1: {
3810 DecodeType01(instr);
3811 break;
3812 }
3813 case 2: {
3814 DecodeType2(instr);
3815 break;
3816 }
3817 case 3: {
3818 DecodeType3(instr);
3819 break;
3820 }
3821 case 4: {
3822 DecodeType4(instr);
3823 break;
3824 }
3825 case 5: {
3826 DecodeType5(instr);
3827 break;
3828 }
3829 case 6: {
3830 DecodeType6(instr);
3831 break;
3832 }
3833 case 7: {
3834 DecodeType7(instr);
3835 break;
3836 }
3837 default: {
3838 UNIMPLEMENTED();
3839 break;
3840 }
3841 }
Steve Block1e0659c2011-05-24 12:43:12 +01003842 // If the instruction is a non taken conditional stop, we need to skip the
3843 // inlined message address.
3844 } else if (instr->IsStop()) {
3845 set_pc(get_pc() + 2 * Instruction::kInstrSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00003846 }
3847 if (!pc_modified_) {
Steve Block1e0659c2011-05-24 12:43:12 +01003848 set_register(pc, reinterpret_cast<int32_t>(instr)
3849 + Instruction::kInstrSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00003850 }
3851}
3852
3853
Steve Blocka7e24c12009-10-30 11:49:00 +00003854void Simulator::Execute() {
3855 // Get the PC to simulate. Cannot use the accessor here as we need the
3856 // raw PC value and not the one used as input to arithmetic instructions.
3857 int program_counter = get_pc();
3858
3859 if (::v8::internal::FLAG_stop_sim_at == 0) {
3860 // Fast version of the dispatch loop without checking whether the simulator
3861 // should be stopping at a particular executed instruction.
3862 while (program_counter != end_sim_pc) {
Steve Block1e0659c2011-05-24 12:43:12 +01003863 Instruction* instr = reinterpret_cast<Instruction*>(program_counter);
Steve Blocka7e24c12009-10-30 11:49:00 +00003864 icount_++;
3865 InstructionDecode(instr);
3866 program_counter = get_pc();
3867 }
3868 } else {
3869 // FLAG_stop_sim_at is at the non-default value. Stop in the debugger when
3870 // we reach the particular instuction count.
3871 while (program_counter != end_sim_pc) {
Steve Block1e0659c2011-05-24 12:43:12 +01003872 Instruction* instr = reinterpret_cast<Instruction*>(program_counter);
Steve Blocka7e24c12009-10-30 11:49:00 +00003873 icount_++;
3874 if (icount_ == ::v8::internal::FLAG_stop_sim_at) {
Steve Block44f0eee2011-05-26 01:26:41 +01003875 ArmDebugger dbg(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00003876 dbg.Debug();
3877 } else {
3878 InstructionDecode(instr);
3879 }
3880 program_counter = get_pc();
3881 }
3882 }
3883}
3884
3885
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003886void Simulator::CallInternal(byte* entry) {
Steve Blocka7e24c12009-10-30 11:49:00 +00003887 // Prepare to execute the code at entry
3888 set_register(pc, reinterpret_cast<int32_t>(entry));
3889 // Put down marker for end of simulation. The simulator will stop simulation
3890 // when the PC reaches this value. By saving the "end simulation" value into
3891 // the LR the simulation stops when returning to this call point.
3892 set_register(lr, end_sim_pc);
3893
3894 // Remember the values of callee-saved registers.
3895 // The code below assumes that r9 is not used as sb (static base) in
3896 // simulator code and therefore is regarded as a callee-saved register.
3897 int32_t r4_val = get_register(r4);
3898 int32_t r5_val = get_register(r5);
3899 int32_t r6_val = get_register(r6);
3900 int32_t r7_val = get_register(r7);
3901 int32_t r8_val = get_register(r8);
3902 int32_t r9_val = get_register(r9);
3903 int32_t r10_val = get_register(r10);
3904 int32_t r11_val = get_register(r11);
3905
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003906 // Set up the callee-saved registers with a known value. To be able to check
Steve Blocka7e24c12009-10-30 11:49:00 +00003907 // that they are preserved properly across JS execution.
3908 int32_t callee_saved_value = icount_;
3909 set_register(r4, callee_saved_value);
3910 set_register(r5, callee_saved_value);
3911 set_register(r6, callee_saved_value);
3912 set_register(r7, callee_saved_value);
3913 set_register(r8, callee_saved_value);
3914 set_register(r9, callee_saved_value);
3915 set_register(r10, callee_saved_value);
3916 set_register(r11, callee_saved_value);
3917
3918 // Start the simulation
3919 Execute();
3920
3921 // Check that the callee-saved registers have been preserved.
3922 CHECK_EQ(callee_saved_value, get_register(r4));
3923 CHECK_EQ(callee_saved_value, get_register(r5));
3924 CHECK_EQ(callee_saved_value, get_register(r6));
3925 CHECK_EQ(callee_saved_value, get_register(r7));
3926 CHECK_EQ(callee_saved_value, get_register(r8));
3927 CHECK_EQ(callee_saved_value, get_register(r9));
3928 CHECK_EQ(callee_saved_value, get_register(r10));
3929 CHECK_EQ(callee_saved_value, get_register(r11));
3930
3931 // Restore callee-saved registers with the original value.
3932 set_register(r4, r4_val);
3933 set_register(r5, r5_val);
3934 set_register(r6, r6_val);
3935 set_register(r7, r7_val);
3936 set_register(r8, r8_val);
3937 set_register(r9, r9_val);
3938 set_register(r10, r10_val);
3939 set_register(r11, r11_val);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003940}
3941
3942
3943int32_t Simulator::Call(byte* entry, int argument_count, ...) {
3944 va_list parameters;
3945 va_start(parameters, argument_count);
3946 // Set up arguments
3947
3948 // First four arguments passed in registers.
3949 DCHECK(argument_count >= 4);
3950 set_register(r0, va_arg(parameters, int32_t));
3951 set_register(r1, va_arg(parameters, int32_t));
3952 set_register(r2, va_arg(parameters, int32_t));
3953 set_register(r3, va_arg(parameters, int32_t));
3954
3955 // Remaining arguments passed on stack.
3956 int original_stack = get_register(sp);
3957 // Compute position of stack on entry to generated code.
3958 int entry_stack = (original_stack - (argument_count - 4) * sizeof(int32_t));
3959 if (base::OS::ActivationFrameAlignment() != 0) {
3960 entry_stack &= -base::OS::ActivationFrameAlignment();
3961 }
3962 // Store remaining arguments on stack, from low to high memory.
3963 intptr_t* stack_argument = reinterpret_cast<intptr_t*>(entry_stack);
3964 for (int i = 4; i < argument_count; i++) {
3965 stack_argument[i - 4] = va_arg(parameters, int32_t);
3966 }
3967 va_end(parameters);
3968 set_register(sp, entry_stack);
3969
3970 CallInternal(entry);
Steve Blocka7e24c12009-10-30 11:49:00 +00003971
3972 // Pop stack passed arguments.
3973 CHECK_EQ(entry_stack, get_register(sp));
3974 set_register(sp, original_stack);
3975
3976 int32_t result = get_register(r0);
3977 return result;
3978}
3979
Steve Blockd0582a62009-12-15 09:54:21 +00003980
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003981void Simulator::CallFP(byte* entry, double d0, double d1) {
3982 if (use_eabi_hardfloat()) {
3983 set_d_register_from_double(0, d0);
3984 set_d_register_from_double(1, d1);
3985 } else {
3986 set_register_pair_from_double(0, &d0);
3987 set_register_pair_from_double(2, &d1);
3988 }
3989 CallInternal(entry);
3990}
3991
3992
3993int32_t Simulator::CallFPReturnsInt(byte* entry, double d0, double d1) {
3994 CallFP(entry, d0, d1);
3995 int32_t result = get_register(r0);
3996 return result;
3997}
3998
3999
4000double Simulator::CallFPReturnsDouble(byte* entry, double d0, double d1) {
4001 CallFP(entry, d0, d1);
4002 if (use_eabi_hardfloat()) {
4003 return get_double_from_d_register(0);
4004 } else {
4005 return get_double_from_register_pair(0);
4006 }
4007}
4008
4009
Steve Blockd0582a62009-12-15 09:54:21 +00004010uintptr_t Simulator::PushAddress(uintptr_t address) {
4011 int new_sp = get_register(sp) - sizeof(uintptr_t);
4012 uintptr_t* stack_slot = reinterpret_cast<uintptr_t*>(new_sp);
4013 *stack_slot = address;
4014 set_register(sp, new_sp);
4015 return new_sp;
4016}
4017
4018
4019uintptr_t Simulator::PopAddress() {
4020 int current_sp = get_register(sp);
4021 uintptr_t* stack_slot = reinterpret_cast<uintptr_t*>(current_sp);
4022 uintptr_t address = *stack_slot;
4023 set_register(sp, current_sp + sizeof(uintptr_t));
4024 return address;
4025}
4026
Steve Block1e0659c2011-05-24 12:43:12 +01004027} } // namespace v8::internal
Steve Blocka7e24c12009-10-30 11:49:00 +00004028
John Reck59135872010-11-02 12:39:01 -07004029#endif // USE_SIMULATOR
Leon Clarkef7060e22010-06-03 12:02:55 +01004030
4031#endif // V8_TARGET_ARCH_ARM