blob: 3ec5f449d8ab5a4e4faac0c910fe87be7669ab94 [file] [log] [blame]
Leon Clarked91b9f72010-01-27 17:25:45 +00001// Copyright 2010 the V8 project authors. All rights reserved.
Steve Blocka7e24c12009-10-30 11:49:00 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include <stdlib.h>
Steve Block8defd9f2010-07-08 12:39:36 +010029#include <math.h>
Steve Blocka7e24c12009-10-30 11:49:00 +000030#include <cstdarg>
31#include "v8.h"
32
Leon Clarkef7060e22010-06-03 12:02:55 +010033#if defined(V8_TARGET_ARCH_ARM)
34
Steve Blocka7e24c12009-10-30 11:49:00 +000035#include "disasm.h"
36#include "assembler.h"
37#include "arm/constants-arm.h"
38#include "arm/simulator-arm.h"
39
John Reck59135872010-11-02 12:39:01 -070040#if defined(USE_SIMULATOR)
Steve Blocka7e24c12009-10-30 11:49:00 +000041
42// Only build the simulator if not compiling for real ARM hardware.
43namespace assembler {
44namespace arm {
45
46using ::v8::internal::Object;
47using ::v8::internal::PrintF;
48using ::v8::internal::OS;
49using ::v8::internal::ReadLine;
50using ::v8::internal::DeleteArray;
51
52// This macro provides a platform independent use of sscanf. The reason for
Leon Clarked91b9f72010-01-27 17:25:45 +000053// SScanF not being implemented in a platform independent way through
54// ::v8::internal::OS in the same way as SNPrintF is that the
55// Windows C Run-Time Library does not provide vsscanf.
Steve Blocka7e24c12009-10-30 11:49:00 +000056#define SScanF sscanf // NOLINT
57
58// The Debugger class is used by the simulator while debugging simulated ARM
59// code.
60class Debugger {
61 public:
62 explicit Debugger(Simulator* sim);
63 ~Debugger();
64
65 void Stop(Instr* instr);
66 void Debug();
67
68 private:
69 static const instr_t kBreakpointInstr =
70 ((AL << 28) | (7 << 25) | (1 << 24) | break_point);
71 static const instr_t kNopInstr =
72 ((AL << 28) | (13 << 21));
73
74 Simulator* sim_;
75
76 int32_t GetRegisterValue(int regnum);
77 bool GetValue(const char* desc, int32_t* value);
Steve Block6ded16b2010-05-10 14:33:55 +010078 bool GetVFPSingleValue(const char* desc, float* value);
79 bool GetVFPDoubleValue(const char* desc, double* value);
Steve Blocka7e24c12009-10-30 11:49:00 +000080
81 // Set or delete a breakpoint. Returns true if successful.
82 bool SetBreakpoint(Instr* breakpc);
83 bool DeleteBreakpoint(Instr* breakpc);
84
85 // Undo and redo all breakpoints. This is needed to bracket disassembly and
86 // execution to skip past breakpoints when run from the debugger.
87 void UndoBreakpoints();
88 void RedoBreakpoints();
89};
90
91
92Debugger::Debugger(Simulator* sim) {
93 sim_ = sim;
94}
95
96
97Debugger::~Debugger() {
98}
99
100
101
102#ifdef GENERATED_CODE_COVERAGE
103static FILE* coverage_log = NULL;
104
105
106static void InitializeCoverage() {
107 char* file_name = getenv("V8_GENERATED_CODE_COVERAGE_LOG");
108 if (file_name != NULL) {
109 coverage_log = fopen(file_name, "aw+");
110 }
111}
112
113
114void Debugger::Stop(Instr* instr) {
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800115 // Get the stop code.
116 uint32_t code = instr->SvcField() & kStopCodeMask;
117 // Retrieve the encoded address, which comes just after this stop.
118 char** msg_address =
119 reinterpret_cast<char**>(sim_->get_pc() + Instr::kInstrSize);
120 char* msg = *msg_address;
121 ASSERT(msg != NULL);
122
123 // Update this stop description.
124 if (isWatchedStop(code) && !watched_stops[code].desc) {
125 watched_stops[code].desc = msg;
126 }
127
128 if (strlen(msg) > 0) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000129 if (coverage_log != NULL) {
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800130 fprintf(coverage_log, "%s\n", msg);
Steve Blocka7e24c12009-10-30 11:49:00 +0000131 fflush(coverage_log);
132 }
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800133 // Overwrite the instruction and address with nops.
134 instr->SetInstructionBits(kNopInstr);
135 reinterpret_cast<Instr*>(msg_address)->SetInstructionBits(kNopInstr);
Steve Blocka7e24c12009-10-30 11:49:00 +0000136 }
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800137 sim_->set_pc(sim_->get_pc() + 2 * Instr::kInstrSize);
Steve Blocka7e24c12009-10-30 11:49:00 +0000138}
139
140#else // ndef GENERATED_CODE_COVERAGE
141
142static void InitializeCoverage() {
143}
144
145
146void Debugger::Stop(Instr* instr) {
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800147 // Get the stop code.
148 uint32_t code = instr->SvcField() & kStopCodeMask;
149 // Retrieve the encoded address, which comes just after this stop.
150 char* msg = *reinterpret_cast<char**>(sim_->get_pc() + Instr::kInstrSize);
151 // Update this stop description.
152 if (sim_->isWatchedStop(code) && !sim_->watched_stops[code].desc) {
153 sim_->watched_stops[code].desc = msg;
154 }
155 PrintF("Simulator hit %s\n", msg);
156 sim_->set_pc(sim_->get_pc() + 2 * Instr::kInstrSize);
Steve Blocka7e24c12009-10-30 11:49:00 +0000157 Debug();
158}
159#endif
160
161
162int32_t Debugger::GetRegisterValue(int regnum) {
163 if (regnum == kPCRegister) {
164 return sim_->get_pc();
165 } else {
166 return sim_->get_register(regnum);
167 }
168}
169
170
171bool Debugger::GetValue(const char* desc, int32_t* value) {
172 int regnum = Registers::Number(desc);
173 if (regnum != kNoRegister) {
174 *value = GetRegisterValue(regnum);
175 return true;
176 } else {
Steve Block6ded16b2010-05-10 14:33:55 +0100177 if (strncmp(desc, "0x", 2) == 0) {
178 return SScanF(desc + 2, "%x", reinterpret_cast<uint32_t*>(value)) == 1;
179 } else {
180 return SScanF(desc, "%u", reinterpret_cast<uint32_t*>(value)) == 1;
181 }
182 }
183 return false;
184}
185
186
187bool Debugger::GetVFPSingleValue(const char* desc, float* value) {
188 bool is_double;
189 int regnum = VFPRegisters::Number(desc, &is_double);
190 if (regnum != kNoRegister && !is_double) {
191 *value = sim_->get_float_from_s_register(regnum);
192 return true;
193 }
194 return false;
195}
196
197
198bool Debugger::GetVFPDoubleValue(const char* desc, double* value) {
199 bool is_double;
200 int regnum = VFPRegisters::Number(desc, &is_double);
201 if (regnum != kNoRegister && is_double) {
202 *value = sim_->get_double_from_d_register(regnum);
203 return true;
Steve Blocka7e24c12009-10-30 11:49:00 +0000204 }
205 return false;
206}
207
208
209bool Debugger::SetBreakpoint(Instr* breakpc) {
210 // Check if a breakpoint can be set. If not return without any side-effects.
211 if (sim_->break_pc_ != NULL) {
212 return false;
213 }
214
215 // Set the breakpoint.
216 sim_->break_pc_ = breakpc;
217 sim_->break_instr_ = breakpc->InstructionBits();
218 // Not setting the breakpoint instruction in the code itself. It will be set
219 // when the debugger shell continues.
220 return true;
221}
222
223
224bool Debugger::DeleteBreakpoint(Instr* breakpc) {
225 if (sim_->break_pc_ != NULL) {
226 sim_->break_pc_->SetInstructionBits(sim_->break_instr_);
227 }
228
229 sim_->break_pc_ = NULL;
230 sim_->break_instr_ = 0;
231 return true;
232}
233
234
235void Debugger::UndoBreakpoints() {
236 if (sim_->break_pc_ != NULL) {
237 sim_->break_pc_->SetInstructionBits(sim_->break_instr_);
238 }
239}
240
241
242void Debugger::RedoBreakpoints() {
243 if (sim_->break_pc_ != NULL) {
244 sim_->break_pc_->SetInstructionBits(kBreakpointInstr);
245 }
246}
247
248
249void Debugger::Debug() {
250 intptr_t last_pc = -1;
251 bool done = false;
252
253#define COMMAND_SIZE 63
254#define ARG_SIZE 255
255
256#define STR(a) #a
257#define XSTR(a) STR(a)
258
259 char cmd[COMMAND_SIZE + 1];
260 char arg1[ARG_SIZE + 1];
261 char arg2[ARG_SIZE + 1];
Steve Block6ded16b2010-05-10 14:33:55 +0100262 char* argv[3] = { cmd, arg1, arg2 };
Steve Blocka7e24c12009-10-30 11:49:00 +0000263
264 // make sure to have a proper terminating character if reaching the limit
265 cmd[COMMAND_SIZE] = 0;
266 arg1[ARG_SIZE] = 0;
267 arg2[ARG_SIZE] = 0;
268
269 // Undo all set breakpoints while running in the debugger shell. This will
270 // make them invisible to all commands.
271 UndoBreakpoints();
272
273 while (!done) {
274 if (last_pc != sim_->get_pc()) {
275 disasm::NameConverter converter;
276 disasm::Disassembler dasm(converter);
277 // use a reasonably large buffer
278 v8::internal::EmbeddedVector<char, 256> buffer;
279 dasm.InstructionDecode(buffer,
280 reinterpret_cast<byte*>(sim_->get_pc()));
281 PrintF(" 0x%08x %s\n", sim_->get_pc(), buffer.start());
282 last_pc = sim_->get_pc();
283 }
284 char* line = ReadLine("sim> ");
285 if (line == NULL) {
286 break;
287 } else {
288 // Use sscanf to parse the individual parts of the command line. At the
289 // moment no command expects more than two parameters.
Steve Block6ded16b2010-05-10 14:33:55 +0100290 int argc = SScanF(line,
Steve Blocka7e24c12009-10-30 11:49:00 +0000291 "%" XSTR(COMMAND_SIZE) "s "
292 "%" XSTR(ARG_SIZE) "s "
293 "%" XSTR(ARG_SIZE) "s",
294 cmd, arg1, arg2);
295 if ((strcmp(cmd, "si") == 0) || (strcmp(cmd, "stepi") == 0)) {
296 sim_->InstructionDecode(reinterpret_cast<Instr*>(sim_->get_pc()));
297 } else if ((strcmp(cmd, "c") == 0) || (strcmp(cmd, "cont") == 0)) {
298 // Execute the one instruction we broke at with breakpoints disabled.
299 sim_->InstructionDecode(reinterpret_cast<Instr*>(sim_->get_pc()));
300 // Leave the debugger shell.
301 done = true;
302 } else if ((strcmp(cmd, "p") == 0) || (strcmp(cmd, "print") == 0)) {
Steve Block6ded16b2010-05-10 14:33:55 +0100303 if (argc == 2) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000304 int32_t value;
Steve Block6ded16b2010-05-10 14:33:55 +0100305 float svalue;
306 double dvalue;
Steve Blocka7e24c12009-10-30 11:49:00 +0000307 if (strcmp(arg1, "all") == 0) {
308 for (int i = 0; i < kNumRegisters; i++) {
309 value = GetRegisterValue(i);
310 PrintF("%3s: 0x%08x %10d\n", Registers::Name(i), value, value);
311 }
312 } else {
313 if (GetValue(arg1, &value)) {
314 PrintF("%s: 0x%08x %d \n", arg1, value, value);
Steve Block6ded16b2010-05-10 14:33:55 +0100315 } else if (GetVFPSingleValue(arg1, &svalue)) {
316 PrintF("%s: %f \n", arg1, svalue);
317 } else if (GetVFPDoubleValue(arg1, &dvalue)) {
Ben Murdochf87a2032010-10-22 12:50:53 +0100318 PrintF("%s: %f \n", arg1, dvalue);
Steve Blocka7e24c12009-10-30 11:49:00 +0000319 } else {
320 PrintF("%s unrecognized\n", arg1);
321 }
322 }
323 } else {
324 PrintF("print <register>\n");
325 }
326 } else if ((strcmp(cmd, "po") == 0)
327 || (strcmp(cmd, "printobject") == 0)) {
Steve Block6ded16b2010-05-10 14:33:55 +0100328 if (argc == 2) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000329 int32_t value;
330 if (GetValue(arg1, &value)) {
331 Object* obj = reinterpret_cast<Object*>(value);
332 PrintF("%s: \n", arg1);
333#ifdef DEBUG
334 obj->PrintLn();
335#else
336 obj->ShortPrint();
337 PrintF("\n");
338#endif
339 } else {
340 PrintF("%s unrecognized\n", arg1);
341 }
342 } else {
343 PrintF("printobject <value>\n");
344 }
Steve Block6ded16b2010-05-10 14:33:55 +0100345 } else if (strcmp(cmd, "stack") == 0 || strcmp(cmd, "mem") == 0) {
346 int32_t* cur = NULL;
347 int32_t* end = NULL;
348 int next_arg = 1;
349
350 if (strcmp(cmd, "stack") == 0) {
351 cur = reinterpret_cast<int32_t*>(sim_->get_register(Simulator::sp));
352 } else { // "mem"
353 int32_t value;
354 if (!GetValue(arg1, &value)) {
355 PrintF("%s unrecognized\n", arg1);
356 continue;
357 }
358 cur = reinterpret_cast<int32_t*>(value);
359 next_arg++;
360 }
361
362 int32_t words;
363 if (argc == next_arg) {
364 words = 10;
365 } else if (argc == next_arg + 1) {
366 if (!GetValue(argv[next_arg], &words)) {
367 words = 10;
368 }
369 }
370 end = cur + words;
371
372 while (cur < end) {
Ben Murdochf87a2032010-10-22 12:50:53 +0100373 PrintF(" 0x%08x: 0x%08x %10d\n",
374 reinterpret_cast<intptr_t>(cur), *cur, *cur);
Steve Block6ded16b2010-05-10 14:33:55 +0100375 cur++;
376 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000377 } else if (strcmp(cmd, "disasm") == 0) {
378 disasm::NameConverter converter;
379 disasm::Disassembler dasm(converter);
380 // use a reasonably large buffer
381 v8::internal::EmbeddedVector<char, 256> buffer;
382
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800383 byte* prev = NULL;
Steve Blocka7e24c12009-10-30 11:49:00 +0000384 byte* cur = NULL;
385 byte* end = NULL;
386
Steve Block6ded16b2010-05-10 14:33:55 +0100387 if (argc == 1) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000388 cur = reinterpret_cast<byte*>(sim_->get_pc());
389 end = cur + (10 * Instr::kInstrSize);
Steve Block6ded16b2010-05-10 14:33:55 +0100390 } else if (argc == 2) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000391 int32_t value;
392 if (GetValue(arg1, &value)) {
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800393 cur = reinterpret_cast<byte*>(sim_->get_pc());
394 // Disassemble <arg1> instructions.
395 end = cur + (value * Instr::kInstrSize);
Steve Blocka7e24c12009-10-30 11:49:00 +0000396 }
397 } else {
398 int32_t value1;
399 int32_t value2;
400 if (GetValue(arg1, &value1) && GetValue(arg2, &value2)) {
401 cur = reinterpret_cast<byte*>(value1);
402 end = cur + (value2 * Instr::kInstrSize);
403 }
404 }
405
406 while (cur < end) {
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800407 prev = cur;
408 cur += dasm.InstructionDecode(buffer, cur);
Ben Murdochf87a2032010-10-22 12:50:53 +0100409 PrintF(" 0x%08x %s\n",
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800410 reinterpret_cast<intptr_t>(prev), buffer.start());
Steve Blocka7e24c12009-10-30 11:49:00 +0000411 }
412 } else if (strcmp(cmd, "gdb") == 0) {
413 PrintF("relinquishing control to gdb\n");
414 v8::internal::OS::DebugBreak();
415 PrintF("regaining control from gdb\n");
416 } else if (strcmp(cmd, "break") == 0) {
Steve Block6ded16b2010-05-10 14:33:55 +0100417 if (argc == 2) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000418 int32_t value;
419 if (GetValue(arg1, &value)) {
420 if (!SetBreakpoint(reinterpret_cast<Instr*>(value))) {
421 PrintF("setting breakpoint failed\n");
422 }
423 } else {
424 PrintF("%s unrecognized\n", arg1);
425 }
426 } else {
427 PrintF("break <address>\n");
428 }
429 } else if (strcmp(cmd, "del") == 0) {
430 if (!DeleteBreakpoint(NULL)) {
431 PrintF("deleting breakpoint failed\n");
432 }
433 } else if (strcmp(cmd, "flags") == 0) {
434 PrintF("N flag: %d; ", sim_->n_flag_);
435 PrintF("Z flag: %d; ", sim_->z_flag_);
436 PrintF("C flag: %d; ", sim_->c_flag_);
437 PrintF("V flag: %d\n", sim_->v_flag_);
Steve Blockd0582a62009-12-15 09:54:21 +0000438 PrintF("INVALID OP flag: %d; ", sim_->inv_op_vfp_flag_);
439 PrintF("DIV BY ZERO flag: %d; ", sim_->div_zero_vfp_flag_);
440 PrintF("OVERFLOW flag: %d; ", sim_->overflow_vfp_flag_);
441 PrintF("UNDERFLOW flag: %d; ", sim_->underflow_vfp_flag_);
442 PrintF("INEXACT flag: %d; ", sim_->inexact_vfp_flag_);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800443 } else if (strcmp(cmd, "stop") == 0) {
444 int32_t value;
445 intptr_t stop_pc = sim_->get_pc() - 2 * Instr::kInstrSize;
Steve Blocka7e24c12009-10-30 11:49:00 +0000446 Instr* stop_instr = reinterpret_cast<Instr*>(stop_pc);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800447 Instr* msg_address =
448 reinterpret_cast<Instr*>(stop_pc + Instr::kInstrSize);
449 if ((argc == 2) && (strcmp(arg1, "unstop") == 0)) {
450 // Remove the current stop.
451 if (sim_->isStopInstruction(stop_instr)) {
452 stop_instr->SetInstructionBits(kNopInstr);
453 msg_address->SetInstructionBits(kNopInstr);
454 } else {
455 PrintF("Not at debugger stop.\n");
456 }
457 } else if (argc == 3) {
458 // Print information about all/the specified breakpoint(s).
459 if (strcmp(arg1, "info") == 0) {
460 if (strcmp(arg2, "all") == 0) {
461 PrintF("Stop information:\n");
462 for (uint32_t i = 0; i < sim_->kNumOfWatchedStops; i++) {
463 sim_->PrintStopInfo(i);
464 }
465 } else if (GetValue(arg2, &value)) {
466 sim_->PrintStopInfo(value);
467 } else {
468 PrintF("Unrecognized argument.\n");
469 }
470 } else if (strcmp(arg1, "enable") == 0) {
471 // Enable all/the specified breakpoint(s).
472 if (strcmp(arg2, "all") == 0) {
473 for (uint32_t i = 0; i < sim_->kNumOfWatchedStops; i++) {
474 sim_->EnableStop(i);
475 }
476 } else if (GetValue(arg2, &value)) {
477 sim_->EnableStop(value);
478 } else {
479 PrintF("Unrecognized argument.\n");
480 }
481 } else if (strcmp(arg1, "disable") == 0) {
482 // Disable all/the specified breakpoint(s).
483 if (strcmp(arg2, "all") == 0) {
484 for (uint32_t i = 0; i < sim_->kNumOfWatchedStops; i++) {
485 sim_->DisableStop(i);
486 }
487 } else if (GetValue(arg2, &value)) {
488 sim_->DisableStop(value);
489 } else {
490 PrintF("Unrecognized argument.\n");
491 }
492 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000493 } else {
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800494 PrintF("Wrong usage. Use help command for more information.\n");
Steve Blocka7e24c12009-10-30 11:49:00 +0000495 }
Leon Clarkee46be812010-01-19 14:06:41 +0000496 } else if ((strcmp(cmd, "t") == 0) || strcmp(cmd, "trace") == 0) {
497 ::v8::internal::FLAG_trace_sim = !::v8::internal::FLAG_trace_sim;
498 PrintF("Trace of executed instructions is %s\n",
499 ::v8::internal::FLAG_trace_sim ? "on" : "off");
Steve Blocka7e24c12009-10-30 11:49:00 +0000500 } else if ((strcmp(cmd, "h") == 0) || (strcmp(cmd, "help") == 0)) {
501 PrintF("cont\n");
502 PrintF(" continue execution (alias 'c')\n");
503 PrintF("stepi\n");
504 PrintF(" step one instruction (alias 'si')\n");
505 PrintF("print <register>\n");
506 PrintF(" print register content (alias 'p')\n");
507 PrintF(" use register name 'all' to print all registers\n");
508 PrintF("printobject <register>\n");
509 PrintF(" print an object from a register (alias 'po')\n");
510 PrintF("flags\n");
511 PrintF(" print flags\n");
Steve Block6ded16b2010-05-10 14:33:55 +0100512 PrintF("stack [<words>]\n");
513 PrintF(" dump stack content, default dump 10 words)\n");
514 PrintF("mem <address> [<words>]\n");
515 PrintF(" dump memory content, default dump 10 words)\n");
Steve Blocka7e24c12009-10-30 11:49:00 +0000516 PrintF("disasm [<instructions>]\n");
517 PrintF("disasm [[<address>] <instructions>]\n");
518 PrintF(" disassemble code, default is 10 instructions from pc\n");
519 PrintF("gdb\n");
520 PrintF(" enter gdb\n");
521 PrintF("break <address>\n");
522 PrintF(" set a break point on the address\n");
523 PrintF("del\n");
524 PrintF(" delete the breakpoint\n");
Leon Clarkee46be812010-01-19 14:06:41 +0000525 PrintF("trace (alias 't')\n");
Steve Block6ded16b2010-05-10 14:33:55 +0100526 PrintF(" toogle the tracing of all executed statements\n");
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800527 PrintF("stop feature:\n");
528 PrintF(" Description:\n");
529 PrintF(" Stops are debug instructions inserted by\n");
530 PrintF(" the Assembler::stop() function.\n");
531 PrintF(" When hitting a stop, the Simulator will\n");
532 PrintF(" stop and and give control to the Debugger.\n");
533 PrintF(" The first %d stop codes are watched:\n",
534 Simulator::kNumOfWatchedStops);
535 PrintF(" - They can be enabled / disabled: the Simulator\n");
536 PrintF(" will / won't stop when hitting them.\n");
537 PrintF(" - The Simulator keeps track of how many times they \n");
538 PrintF(" are met. (See the info command.) Going over a\n");
539 PrintF(" disabled stop still increases its counter. \n");
540 PrintF(" Commands:\n");
541 PrintF(" stop info all/<code> : print infos about number <code>\n");
542 PrintF(" or all stop(s).\n");
543 PrintF(" stop enable/disable all/<code> : enables / disables\n");
544 PrintF(" all or number <code> stop(s)\n");
545 PrintF(" stop unstop\n");
546 PrintF(" ignore the stop instruction at the current location\n");
547 PrintF(" from now on\n");
Steve Blocka7e24c12009-10-30 11:49:00 +0000548 } else {
549 PrintF("Unknown command: %s\n", cmd);
550 }
551 }
552 DeleteArray(line);
553 }
554
555 // Add all the breakpoints back to stop execution and enter the debugger
556 // shell when hit.
557 RedoBreakpoints();
558
559#undef COMMAND_SIZE
560#undef ARG_SIZE
561
562#undef STR
563#undef XSTR
564}
565
566
Steve Block6ded16b2010-05-10 14:33:55 +0100567static bool ICacheMatch(void* one, void* two) {
568 ASSERT((reinterpret_cast<intptr_t>(one) & CachePage::kPageMask) == 0);
569 ASSERT((reinterpret_cast<intptr_t>(two) & CachePage::kPageMask) == 0);
570 return one == two;
571}
572
573
574static uint32_t ICacheHash(void* key) {
575 return static_cast<uint32_t>(reinterpret_cast<uintptr_t>(key)) >> 2;
576}
577
578
579static bool AllOnOnePage(uintptr_t start, int size) {
580 intptr_t start_page = (start & ~CachePage::kPageMask);
581 intptr_t end_page = ((start + size) & ~CachePage::kPageMask);
582 return start_page == end_page;
583}
584
585
586void Simulator::FlushICache(void* start_addr, size_t size) {
587 intptr_t start = reinterpret_cast<intptr_t>(start_addr);
588 int intra_line = (start & CachePage::kLineMask);
589 start -= intra_line;
590 size += intra_line;
591 size = ((size - 1) | CachePage::kLineMask) + 1;
592 int offset = (start & CachePage::kPageMask);
593 while (!AllOnOnePage(start, size - 1)) {
594 int bytes_to_flush = CachePage::kPageSize - offset;
595 FlushOnePage(start, bytes_to_flush);
596 start += bytes_to_flush;
597 size -= bytes_to_flush;
598 ASSERT_EQ(0, start & CachePage::kPageMask);
599 offset = 0;
600 }
601 if (size != 0) {
602 FlushOnePage(start, size);
603 }
604}
605
606
607CachePage* Simulator::GetCachePage(void* page) {
608 v8::internal::HashMap::Entry* entry = i_cache_->Lookup(page,
609 ICacheHash(page),
610 true);
611 if (entry->value == NULL) {
612 CachePage* new_page = new CachePage();
613 entry->value = new_page;
614 }
615 return reinterpret_cast<CachePage*>(entry->value);
616}
617
618
619// Flush from start up to and not including start + size.
620void Simulator::FlushOnePage(intptr_t start, int size) {
621 ASSERT(size <= CachePage::kPageSize);
622 ASSERT(AllOnOnePage(start, size - 1));
623 ASSERT((start & CachePage::kLineMask) == 0);
624 ASSERT((size & CachePage::kLineMask) == 0);
625 void* page = reinterpret_cast<void*>(start & (~CachePage::kPageMask));
626 int offset = (start & CachePage::kPageMask);
627 CachePage* cache_page = GetCachePage(page);
628 char* valid_bytemap = cache_page->ValidityByte(offset);
629 memset(valid_bytemap, CachePage::LINE_INVALID, size >> CachePage::kLineShift);
630}
631
632
633void Simulator::CheckICache(Instr* instr) {
634 intptr_t address = reinterpret_cast<intptr_t>(instr);
635 void* page = reinterpret_cast<void*>(address & (~CachePage::kPageMask));
636 void* line = reinterpret_cast<void*>(address & (~CachePage::kLineMask));
637 int offset = (address & CachePage::kPageMask);
638 CachePage* cache_page = GetCachePage(page);
639 char* cache_valid_byte = cache_page->ValidityByte(offset);
640 bool cache_hit = (*cache_valid_byte == CachePage::LINE_VALID);
641 char* cached_line = cache_page->CachedData(offset & ~CachePage::kLineMask);
642 if (cache_hit) {
643 // Check that the data in memory matches the contents of the I-cache.
644 CHECK(memcmp(reinterpret_cast<void*>(instr),
645 cache_page->CachedData(offset),
646 Instr::kInstrSize) == 0);
647 } else {
648 // Cache miss. Load memory into the cache.
649 memcpy(cached_line, line, CachePage::kLineLength);
650 *cache_valid_byte = CachePage::LINE_VALID;
651 }
652}
653
654
Steve Blocka7e24c12009-10-30 11:49:00 +0000655// Create one simulator per thread and keep it in thread local storage.
656static v8::internal::Thread::LocalStorageKey simulator_key;
657
658
659bool Simulator::initialized_ = false;
660
661
662void Simulator::Initialize() {
663 if (initialized_) return;
664 simulator_key = v8::internal::Thread::CreateThreadLocalKey();
665 initialized_ = true;
666 ::v8::internal::ExternalReference::set_redirector(&RedirectExternalReference);
667}
668
669
Steve Block6ded16b2010-05-10 14:33:55 +0100670v8::internal::HashMap* Simulator::i_cache_ = NULL;
671
672
Steve Blocka7e24c12009-10-30 11:49:00 +0000673Simulator::Simulator() {
Steve Block6ded16b2010-05-10 14:33:55 +0100674 if (i_cache_ == NULL) {
675 i_cache_ = new v8::internal::HashMap(&ICacheMatch);
676 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000677 Initialize();
678 // Setup simulator support first. Some of this information is needed to
679 // setup the architecture state.
680 size_t stack_size = 1 * 1024*1024; // allocate 1MB for stack
681 stack_ = reinterpret_cast<char*>(malloc(stack_size));
682 pc_modified_ = false;
683 icount_ = 0;
684 break_pc_ = NULL;
685 break_instr_ = 0;
686
687 // Setup architecture state.
688 // All registers are initialized to zero to start with.
689 for (int i = 0; i < num_registers; i++) {
690 registers_[i] = 0;
691 }
692 n_flag_ = false;
693 z_flag_ = false;
694 c_flag_ = false;
695 v_flag_ = false;
696
Steve Blockd0582a62009-12-15 09:54:21 +0000697 // Initializing VFP registers.
698 // All registers are initialized to zero to start with
699 // even though s_registers_ & d_registers_ share the same
700 // physical registers in the target.
701 for (int i = 0; i < num_s_registers; i++) {
702 vfp_register[i] = 0;
703 }
704 n_flag_FPSCR_ = false;
705 z_flag_FPSCR_ = false;
706 c_flag_FPSCR_ = false;
707 v_flag_FPSCR_ = false;
Russell Brenner90bac252010-11-18 13:33:46 -0800708 FPSCR_rounding_mode_ = RZ;
Steve Blockd0582a62009-12-15 09:54:21 +0000709
710 inv_op_vfp_flag_ = false;
711 div_zero_vfp_flag_ = false;
712 overflow_vfp_flag_ = false;
713 underflow_vfp_flag_ = false;
714 inexact_vfp_flag_ = false;
715
Steve Blocka7e24c12009-10-30 11:49:00 +0000716 // The sp is initialized to point to the bottom (high address) of the
717 // allocated stack area. To be safe in potential stack underflows we leave
718 // some buffer below.
719 registers_[sp] = reinterpret_cast<int32_t>(stack_) + stack_size - 64;
720 // The lr and pc are initialized to a known bad value that will cause an
721 // access violation if the simulator ever tries to execute it.
722 registers_[pc] = bad_lr;
723 registers_[lr] = bad_lr;
724 InitializeCoverage();
725}
726
727
728// When the generated code calls an external reference we need to catch that in
729// the simulator. The external reference will be a function compiled for the
730// host architecture. We need to call that function instead of trying to
731// execute it with the simulator. We do that by redirecting the external
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800732// reference to a svc (Supervisor Call) instruction that is handled by
Steve Blocka7e24c12009-10-30 11:49:00 +0000733// the simulator. We write the original destination of the jump just at a known
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800734// offset from the svc instruction so the simulator knows what to call.
Steve Blocka7e24c12009-10-30 11:49:00 +0000735class Redirection {
736 public:
737 Redirection(void* external_function, bool fp_return)
738 : external_function_(external_function),
739 swi_instruction_((AL << 28) | (0xf << 24) | call_rt_redirected),
740 fp_return_(fp_return),
741 next_(list_) {
Steve Block6ded16b2010-05-10 14:33:55 +0100742 Simulator::current()->
743 FlushICache(reinterpret_cast<void*>(&swi_instruction_),
744 Instr::kInstrSize);
Steve Blocka7e24c12009-10-30 11:49:00 +0000745 list_ = this;
746 }
747
748 void* address_of_swi_instruction() {
749 return reinterpret_cast<void*>(&swi_instruction_);
750 }
751
752 void* external_function() { return external_function_; }
753 bool fp_return() { return fp_return_; }
754
755 static Redirection* Get(void* external_function, bool fp_return) {
756 Redirection* current;
757 for (current = list_; current != NULL; current = current->next_) {
758 if (current->external_function_ == external_function) return current;
759 }
760 return new Redirection(external_function, fp_return);
761 }
762
763 static Redirection* FromSwiInstruction(Instr* swi_instruction) {
764 char* addr_of_swi = reinterpret_cast<char*>(swi_instruction);
765 char* addr_of_redirection =
766 addr_of_swi - OFFSET_OF(Redirection, swi_instruction_);
767 return reinterpret_cast<Redirection*>(addr_of_redirection);
768 }
769
770 private:
771 void* external_function_;
772 uint32_t swi_instruction_;
773 bool fp_return_;
774 Redirection* next_;
775 static Redirection* list_;
776};
777
778
779Redirection* Redirection::list_ = NULL;
780
781
782void* Simulator::RedirectExternalReference(void* external_function,
783 bool fp_return) {
784 Redirection* redirection = Redirection::Get(external_function, fp_return);
785 return redirection->address_of_swi_instruction();
786}
787
788
789// Get the active Simulator for the current thread.
790Simulator* Simulator::current() {
791 Initialize();
792 Simulator* sim = reinterpret_cast<Simulator*>(
793 v8::internal::Thread::GetThreadLocal(simulator_key));
794 if (sim == NULL) {
795 // TODO(146): delete the simulator object when a thread goes away.
796 sim = new Simulator();
797 v8::internal::Thread::SetThreadLocal(simulator_key, sim);
798 }
799 return sim;
800}
801
802
803// Sets the register in the architecture state. It will also deal with updating
804// Simulator internal state for special registers such as PC.
805void Simulator::set_register(int reg, int32_t value) {
806 ASSERT((reg >= 0) && (reg < num_registers));
807 if (reg == pc) {
808 pc_modified_ = true;
809 }
810 registers_[reg] = value;
811}
812
813
814// Get the register from the architecture state. This function does handle
815// the special case of accessing the PC register.
816int32_t Simulator::get_register(int reg) const {
817 ASSERT((reg >= 0) && (reg < num_registers));
Steve Block791712a2010-08-27 10:21:07 +0100818 // Stupid code added to avoid bug in GCC.
819 // See: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43949
820 if (reg >= num_registers) return 0;
821 // End stupid code.
Steve Blocka7e24c12009-10-30 11:49:00 +0000822 return registers_[reg] + ((reg == pc) ? Instr::kPCReadOffset : 0);
823}
824
825
Kristian Monsen25f61362010-05-21 11:50:48 +0100826void Simulator::set_dw_register(int dreg, const int* dbl) {
827 ASSERT((dreg >= 0) && (dreg < num_d_registers));
828 registers_[dreg] = dbl[0];
829 registers_[dreg + 1] = dbl[1];
830}
831
832
Steve Blocka7e24c12009-10-30 11:49:00 +0000833// Raw access to the PC register.
834void Simulator::set_pc(int32_t value) {
835 pc_modified_ = true;
836 registers_[pc] = value;
837}
838
839
840// Raw access to the PC register without the special adjustment when reading.
841int32_t Simulator::get_pc() const {
842 return registers_[pc];
843}
844
845
Steve Blockd0582a62009-12-15 09:54:21 +0000846// Getting from and setting into VFP registers.
847void Simulator::set_s_register(int sreg, unsigned int value) {
848 ASSERT((sreg >= 0) && (sreg < num_s_registers));
849 vfp_register[sreg] = value;
850}
851
852
853unsigned int Simulator::get_s_register(int sreg) const {
854 ASSERT((sreg >= 0) && (sreg < num_s_registers));
855 return vfp_register[sreg];
856}
857
858
859void Simulator::set_s_register_from_float(int sreg, const float flt) {
860 ASSERT((sreg >= 0) && (sreg < num_s_registers));
861 // Read the bits from the single precision floating point value
862 // into the unsigned integer element of vfp_register[] given by index=sreg.
863 char buffer[sizeof(vfp_register[0])];
864 memcpy(buffer, &flt, sizeof(vfp_register[0]));
865 memcpy(&vfp_register[sreg], buffer, sizeof(vfp_register[0]));
866}
867
868
869void Simulator::set_s_register_from_sinteger(int sreg, const int sint) {
870 ASSERT((sreg >= 0) && (sreg < num_s_registers));
871 // Read the bits from the integer value into the unsigned integer element of
872 // vfp_register[] given by index=sreg.
873 char buffer[sizeof(vfp_register[0])];
874 memcpy(buffer, &sint, sizeof(vfp_register[0]));
875 memcpy(&vfp_register[sreg], buffer, sizeof(vfp_register[0]));
876}
877
878
879void Simulator::set_d_register_from_double(int dreg, const double& dbl) {
880 ASSERT((dreg >= 0) && (dreg < num_d_registers));
881 // Read the bits from the double precision floating point value into the two
882 // consecutive unsigned integer elements of vfp_register[] given by index
883 // 2*sreg and 2*sreg+1.
884 char buffer[2 * sizeof(vfp_register[0])];
885 memcpy(buffer, &dbl, 2 * sizeof(vfp_register[0]));
886#ifndef BIG_ENDIAN_FLOATING_POINT
887 memcpy(&vfp_register[dreg * 2], buffer, 2 * sizeof(vfp_register[0]));
888#else
889 memcpy(&vfp_register[dreg * 2], &buffer[4], sizeof(vfp_register[0]));
890 memcpy(&vfp_register[dreg * 2 + 1], &buffer[0], sizeof(vfp_register[0]));
891#endif
892}
893
894
895float Simulator::get_float_from_s_register(int sreg) {
896 ASSERT((sreg >= 0) && (sreg < num_s_registers));
897
898 float sm_val = 0.0;
899 // Read the bits from the unsigned integer vfp_register[] array
900 // into the single precision floating point value and return it.
901 char buffer[sizeof(vfp_register[0])];
902 memcpy(buffer, &vfp_register[sreg], sizeof(vfp_register[0]));
903 memcpy(&sm_val, buffer, sizeof(vfp_register[0]));
904 return(sm_val);
905}
906
907
908int Simulator::get_sinteger_from_s_register(int sreg) {
909 ASSERT((sreg >= 0) && (sreg < num_s_registers));
910
911 int sm_val = 0;
912 // Read the bits from the unsigned integer vfp_register[] array
913 // into the single precision floating point value and return it.
914 char buffer[sizeof(vfp_register[0])];
915 memcpy(buffer, &vfp_register[sreg], sizeof(vfp_register[0]));
916 memcpy(&sm_val, buffer, sizeof(vfp_register[0]));
917 return(sm_val);
918}
919
920
921double Simulator::get_double_from_d_register(int dreg) {
922 ASSERT((dreg >= 0) && (dreg < num_d_registers));
923
924 double dm_val = 0.0;
925 // Read the bits from the unsigned integer vfp_register[] array
926 // into the double precision floating point value and return it.
927 char buffer[2 * sizeof(vfp_register[0])];
928#ifdef BIG_ENDIAN_FLOATING_POINT
929 memcpy(&buffer[0], &vfp_register[2 * dreg + 1], sizeof(vfp_register[0]));
930 memcpy(&buffer[4], &vfp_register[2 * dreg], sizeof(vfp_register[0]));
931#else
932 memcpy(buffer, &vfp_register[2 * dreg], 2 * sizeof(vfp_register[0]));
933#endif
934 memcpy(&dm_val, buffer, 2 * sizeof(vfp_register[0]));
935 return(dm_val);
936}
937
938
Steve Blocka7e24c12009-10-30 11:49:00 +0000939// For use in calls that take two double values, constructed from r0, r1, r2
940// and r3.
941void Simulator::GetFpArgs(double* x, double* y) {
942 // We use a char buffer to get around the strict-aliasing rules which
943 // otherwise allow the compiler to optimize away the copy.
944 char buffer[2 * sizeof(registers_[0])];
945 // Registers 0 and 1 -> x.
946 memcpy(buffer, registers_, sizeof(buffer));
947 memcpy(x, buffer, sizeof(buffer));
948 // Registers 2 and 3 -> y.
949 memcpy(buffer, registers_ + 2, sizeof(buffer));
950 memcpy(y, buffer, sizeof(buffer));
951}
952
953
954void Simulator::SetFpResult(const double& result) {
955 char buffer[2 * sizeof(registers_[0])];
956 memcpy(buffer, &result, sizeof(buffer));
957 // result -> registers 0 and 1.
958 memcpy(registers_, buffer, sizeof(buffer));
959}
960
961
962void Simulator::TrashCallerSaveRegisters() {
963 // We don't trash the registers with the return value.
964 registers_[2] = 0x50Bad4U;
965 registers_[3] = 0x50Bad4U;
966 registers_[12] = 0x50Bad4U;
967}
968
Kristian Monsen25f61362010-05-21 11:50:48 +0100969// Some Operating Systems allow unaligned access on ARMv7 targets. We
970// assume that unaligned accesses are not allowed unless the v8 build system
971// defines the CAN_USE_UNALIGNED_ACCESSES macro to be non-zero.
972// The following statements below describes the behavior of the ARM CPUs
973// that don't support unaligned access.
974// Some ARM platforms raise an interrupt on detecting unaligned access.
975// On others it does a funky rotation thing. For now we
976// simply disallow unaligned reads. Note that simulator runs have the runtime
Steve Blocka7e24c12009-10-30 11:49:00 +0000977// system running directly on the host system and only generated code is
978// executed in the simulator. Since the host is typically IA32 we will not
Kristian Monsen25f61362010-05-21 11:50:48 +0100979// get the correct ARM-like behaviour on unaligned accesses for those ARM
980// targets that don't support unaligned loads and stores.
981
Steve Blocka7e24c12009-10-30 11:49:00 +0000982
983int Simulator::ReadW(int32_t addr, Instr* instr) {
Kristian Monsen25f61362010-05-21 11:50:48 +0100984#if V8_TARGET_CAN_READ_UNALIGNED
985 intptr_t* ptr = reinterpret_cast<intptr_t*>(addr);
986 return *ptr;
987#else
Steve Blocka7e24c12009-10-30 11:49:00 +0000988 if ((addr & 3) == 0) {
989 intptr_t* ptr = reinterpret_cast<intptr_t*>(addr);
990 return *ptr;
991 }
Kristian Monsen25f61362010-05-21 11:50:48 +0100992 PrintF("Unaligned read at 0x%08x, pc=%p\n", addr, instr);
Steve Blocka7e24c12009-10-30 11:49:00 +0000993 UNIMPLEMENTED();
994 return 0;
Kristian Monsen25f61362010-05-21 11:50:48 +0100995#endif
Steve Blocka7e24c12009-10-30 11:49:00 +0000996}
997
998
999void Simulator::WriteW(int32_t addr, int value, Instr* instr) {
Kristian Monsen25f61362010-05-21 11:50:48 +01001000#if V8_TARGET_CAN_READ_UNALIGNED
1001 intptr_t* ptr = reinterpret_cast<intptr_t*>(addr);
1002 *ptr = value;
1003 return;
1004#else
Steve Blocka7e24c12009-10-30 11:49:00 +00001005 if ((addr & 3) == 0) {
1006 intptr_t* ptr = reinterpret_cast<intptr_t*>(addr);
1007 *ptr = value;
1008 return;
1009 }
1010 PrintF("Unaligned write at 0x%08x, pc=%p\n", addr, instr);
1011 UNIMPLEMENTED();
Kristian Monsen25f61362010-05-21 11:50:48 +01001012#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00001013}
1014
1015
1016uint16_t Simulator::ReadHU(int32_t addr, Instr* instr) {
Kristian Monsen25f61362010-05-21 11:50:48 +01001017#if V8_TARGET_CAN_READ_UNALIGNED
1018 uint16_t* ptr = reinterpret_cast<uint16_t*>(addr);
1019 return *ptr;
1020#else
Steve Blocka7e24c12009-10-30 11:49:00 +00001021 if ((addr & 1) == 0) {
1022 uint16_t* ptr = reinterpret_cast<uint16_t*>(addr);
1023 return *ptr;
1024 }
1025 PrintF("Unaligned unsigned halfword read at 0x%08x, pc=%p\n", addr, instr);
1026 UNIMPLEMENTED();
1027 return 0;
Kristian Monsen25f61362010-05-21 11:50:48 +01001028#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00001029}
1030
1031
1032int16_t Simulator::ReadH(int32_t addr, Instr* instr) {
Kristian Monsen25f61362010-05-21 11:50:48 +01001033#if V8_TARGET_CAN_READ_UNALIGNED
1034 int16_t* ptr = reinterpret_cast<int16_t*>(addr);
1035 return *ptr;
1036#else
Steve Blocka7e24c12009-10-30 11:49:00 +00001037 if ((addr & 1) == 0) {
1038 int16_t* ptr = reinterpret_cast<int16_t*>(addr);
1039 return *ptr;
1040 }
1041 PrintF("Unaligned signed halfword read at 0x%08x\n", addr);
1042 UNIMPLEMENTED();
1043 return 0;
Kristian Monsen25f61362010-05-21 11:50:48 +01001044#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00001045}
1046
1047
1048void Simulator::WriteH(int32_t addr, uint16_t value, Instr* instr) {
Kristian Monsen25f61362010-05-21 11:50:48 +01001049#if V8_TARGET_CAN_READ_UNALIGNED
1050 uint16_t* ptr = reinterpret_cast<uint16_t*>(addr);
1051 *ptr = value;
1052 return;
1053#else
Steve Blocka7e24c12009-10-30 11:49:00 +00001054 if ((addr & 1) == 0) {
1055 uint16_t* ptr = reinterpret_cast<uint16_t*>(addr);
1056 *ptr = value;
1057 return;
1058 }
1059 PrintF("Unaligned unsigned halfword write at 0x%08x, pc=%p\n", addr, instr);
1060 UNIMPLEMENTED();
Kristian Monsen25f61362010-05-21 11:50:48 +01001061#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00001062}
1063
1064
1065void Simulator::WriteH(int32_t addr, int16_t value, Instr* instr) {
Kristian Monsen25f61362010-05-21 11:50:48 +01001066#if V8_TARGET_CAN_READ_UNALIGNED
1067 int16_t* ptr = reinterpret_cast<int16_t*>(addr);
1068 *ptr = value;
1069 return;
1070#else
Steve Blocka7e24c12009-10-30 11:49:00 +00001071 if ((addr & 1) == 0) {
1072 int16_t* ptr = reinterpret_cast<int16_t*>(addr);
1073 *ptr = value;
1074 return;
1075 }
1076 PrintF("Unaligned halfword write at 0x%08x, pc=%p\n", addr, instr);
1077 UNIMPLEMENTED();
Kristian Monsen25f61362010-05-21 11:50:48 +01001078#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00001079}
1080
1081
1082uint8_t Simulator::ReadBU(int32_t addr) {
1083 uint8_t* ptr = reinterpret_cast<uint8_t*>(addr);
1084 return *ptr;
1085}
1086
1087
1088int8_t Simulator::ReadB(int32_t addr) {
1089 int8_t* ptr = reinterpret_cast<int8_t*>(addr);
1090 return *ptr;
1091}
1092
1093
1094void Simulator::WriteB(int32_t addr, uint8_t value) {
1095 uint8_t* ptr = reinterpret_cast<uint8_t*>(addr);
1096 *ptr = value;
1097}
1098
1099
1100void Simulator::WriteB(int32_t addr, int8_t value) {
1101 int8_t* ptr = reinterpret_cast<int8_t*>(addr);
1102 *ptr = value;
1103}
1104
1105
Kristian Monsen25f61362010-05-21 11:50:48 +01001106int32_t* Simulator::ReadDW(int32_t addr) {
1107#if V8_TARGET_CAN_READ_UNALIGNED
1108 int32_t* ptr = reinterpret_cast<int32_t*>(addr);
1109 return ptr;
1110#else
1111 if ((addr & 3) == 0) {
1112 int32_t* ptr = reinterpret_cast<int32_t*>(addr);
1113 return ptr;
1114 }
1115 PrintF("Unaligned read at 0x%08x\n", addr);
1116 UNIMPLEMENTED();
1117 return 0;
1118#endif
1119}
1120
1121
1122void Simulator::WriteDW(int32_t addr, int32_t value1, int32_t value2) {
1123#if V8_TARGET_CAN_READ_UNALIGNED
1124 int32_t* ptr = reinterpret_cast<int32_t*>(addr);
1125 *ptr++ = value1;
1126 *ptr = value2;
1127 return;
1128#else
1129 if ((addr & 3) == 0) {
1130 int32_t* ptr = reinterpret_cast<int32_t*>(addr);
1131 *ptr++ = value1;
1132 *ptr = value2;
1133 return;
1134 }
1135 PrintF("Unaligned write at 0x%08x\n", addr);
1136 UNIMPLEMENTED();
1137#endif
1138}
1139
1140
Steve Blocka7e24c12009-10-30 11:49:00 +00001141// Returns the limit of the stack area to enable checking for stack overflows.
1142uintptr_t Simulator::StackLimit() const {
1143 // Leave a safety margin of 256 bytes to prevent overrunning the stack when
1144 // pushing values.
1145 return reinterpret_cast<uintptr_t>(stack_) + 256;
1146}
1147
1148
1149// Unsupported instructions use Format to print an error and stop execution.
1150void Simulator::Format(Instr* instr, const char* format) {
1151 PrintF("Simulator found unsupported instruction:\n 0x%08x: %s\n",
Ben Murdochf87a2032010-10-22 12:50:53 +01001152 reinterpret_cast<intptr_t>(instr), format);
Steve Blocka7e24c12009-10-30 11:49:00 +00001153 UNIMPLEMENTED();
1154}
1155
1156
1157// Checks if the current instruction should be executed based on its
1158// condition bits.
1159bool Simulator::ConditionallyExecute(Instr* instr) {
1160 switch (instr->ConditionField()) {
1161 case EQ: return z_flag_;
1162 case NE: return !z_flag_;
1163 case CS: return c_flag_;
1164 case CC: return !c_flag_;
1165 case MI: return n_flag_;
1166 case PL: return !n_flag_;
1167 case VS: return v_flag_;
1168 case VC: return !v_flag_;
1169 case HI: return c_flag_ && !z_flag_;
1170 case LS: return !c_flag_ || z_flag_;
1171 case GE: return n_flag_ == v_flag_;
1172 case LT: return n_flag_ != v_flag_;
1173 case GT: return !z_flag_ && (n_flag_ == v_flag_);
1174 case LE: return z_flag_ || (n_flag_ != v_flag_);
1175 case AL: return true;
1176 default: UNREACHABLE();
1177 }
1178 return false;
1179}
1180
1181
1182// Calculate and set the Negative and Zero flags.
1183void Simulator::SetNZFlags(int32_t val) {
1184 n_flag_ = (val < 0);
1185 z_flag_ = (val == 0);
1186}
1187
1188
1189// Set the Carry flag.
1190void Simulator::SetCFlag(bool val) {
1191 c_flag_ = val;
1192}
1193
1194
1195// Set the oVerflow flag.
1196void Simulator::SetVFlag(bool val) {
1197 v_flag_ = val;
1198}
1199
1200
1201// Calculate C flag value for additions.
1202bool Simulator::CarryFrom(int32_t left, int32_t right) {
1203 uint32_t uleft = static_cast<uint32_t>(left);
1204 uint32_t uright = static_cast<uint32_t>(right);
1205 uint32_t urest = 0xffffffffU - uleft;
1206
1207 return (uright > urest);
1208}
1209
1210
1211// Calculate C flag value for subtractions.
1212bool Simulator::BorrowFrom(int32_t left, int32_t right) {
1213 uint32_t uleft = static_cast<uint32_t>(left);
1214 uint32_t uright = static_cast<uint32_t>(right);
1215
1216 return (uright > uleft);
1217}
1218
1219
1220// Calculate V flag value for additions and subtractions.
1221bool Simulator::OverflowFrom(int32_t alu_out,
1222 int32_t left, int32_t right, bool addition) {
1223 bool overflow;
1224 if (addition) {
1225 // operands have the same sign
1226 overflow = ((left >= 0 && right >= 0) || (left < 0 && right < 0))
1227 // and operands and result have different sign
1228 && ((left < 0 && alu_out >= 0) || (left >= 0 && alu_out < 0));
1229 } else {
1230 // operands have different signs
1231 overflow = ((left < 0 && right >= 0) || (left >= 0 && right < 0))
1232 // and first operand and result have different signs
1233 && ((left < 0 && alu_out >= 0) || (left >= 0 && alu_out < 0));
1234 }
1235 return overflow;
1236}
1237
1238
Steve Blockd0582a62009-12-15 09:54:21 +00001239// Support for VFP comparisons.
1240void Simulator::Compute_FPSCR_Flags(double val1, double val2) {
Leon Clarkee46be812010-01-19 14:06:41 +00001241 if (isnan(val1) || isnan(val2)) {
1242 n_flag_FPSCR_ = false;
1243 z_flag_FPSCR_ = false;
1244 c_flag_FPSCR_ = true;
1245 v_flag_FPSCR_ = true;
Steve Blockd0582a62009-12-15 09:54:21 +00001246 // All non-NaN cases.
Leon Clarkee46be812010-01-19 14:06:41 +00001247 } else if (val1 == val2) {
Steve Blockd0582a62009-12-15 09:54:21 +00001248 n_flag_FPSCR_ = false;
1249 z_flag_FPSCR_ = true;
1250 c_flag_FPSCR_ = true;
1251 v_flag_FPSCR_ = false;
1252 } else if (val1 < val2) {
1253 n_flag_FPSCR_ = true;
1254 z_flag_FPSCR_ = false;
1255 c_flag_FPSCR_ = false;
1256 v_flag_FPSCR_ = false;
1257 } else {
1258 // Case when (val1 > val2).
1259 n_flag_FPSCR_ = false;
1260 z_flag_FPSCR_ = false;
1261 c_flag_FPSCR_ = true;
1262 v_flag_FPSCR_ = false;
1263 }
1264}
1265
1266
1267void Simulator::Copy_FPSCR_to_APSR() {
1268 n_flag_ = n_flag_FPSCR_;
1269 z_flag_ = z_flag_FPSCR_;
1270 c_flag_ = c_flag_FPSCR_;
1271 v_flag_ = v_flag_FPSCR_;
1272}
1273
1274
Steve Blocka7e24c12009-10-30 11:49:00 +00001275// Addressing Mode 1 - Data-processing operands:
1276// Get the value based on the shifter_operand with register.
1277int32_t Simulator::GetShiftRm(Instr* instr, bool* carry_out) {
1278 Shift shift = instr->ShiftField();
1279 int shift_amount = instr->ShiftAmountField();
1280 int32_t result = get_register(instr->RmField());
1281 if (instr->Bit(4) == 0) {
1282 // by immediate
1283 if ((shift == ROR) && (shift_amount == 0)) {
1284 UNIMPLEMENTED();
1285 return result;
1286 } else if (((shift == LSR) || (shift == ASR)) && (shift_amount == 0)) {
1287 shift_amount = 32;
1288 }
1289 switch (shift) {
1290 case ASR: {
1291 if (shift_amount == 0) {
1292 if (result < 0) {
1293 result = 0xffffffff;
1294 *carry_out = true;
1295 } else {
1296 result = 0;
1297 *carry_out = false;
1298 }
1299 } else {
1300 result >>= (shift_amount - 1);
1301 *carry_out = (result & 1) == 1;
1302 result >>= 1;
1303 }
1304 break;
1305 }
1306
1307 case LSL: {
1308 if (shift_amount == 0) {
1309 *carry_out = c_flag_;
1310 } else {
1311 result <<= (shift_amount - 1);
1312 *carry_out = (result < 0);
1313 result <<= 1;
1314 }
1315 break;
1316 }
1317
1318 case LSR: {
1319 if (shift_amount == 0) {
1320 result = 0;
1321 *carry_out = c_flag_;
1322 } else {
1323 uint32_t uresult = static_cast<uint32_t>(result);
1324 uresult >>= (shift_amount - 1);
1325 *carry_out = (uresult & 1) == 1;
1326 uresult >>= 1;
1327 result = static_cast<int32_t>(uresult);
1328 }
1329 break;
1330 }
1331
1332 case ROR: {
1333 UNIMPLEMENTED();
1334 break;
1335 }
1336
1337 default: {
1338 UNREACHABLE();
1339 break;
1340 }
1341 }
1342 } else {
1343 // by register
1344 int rs = instr->RsField();
1345 shift_amount = get_register(rs) &0xff;
1346 switch (shift) {
1347 case ASR: {
1348 if (shift_amount == 0) {
1349 *carry_out = c_flag_;
1350 } else if (shift_amount < 32) {
1351 result >>= (shift_amount - 1);
1352 *carry_out = (result & 1) == 1;
1353 result >>= 1;
1354 } else {
1355 ASSERT(shift_amount >= 32);
1356 if (result < 0) {
1357 *carry_out = true;
1358 result = 0xffffffff;
1359 } else {
1360 *carry_out = false;
1361 result = 0;
1362 }
1363 }
1364 break;
1365 }
1366
1367 case LSL: {
1368 if (shift_amount == 0) {
1369 *carry_out = c_flag_;
1370 } else if (shift_amount < 32) {
1371 result <<= (shift_amount - 1);
1372 *carry_out = (result < 0);
1373 result <<= 1;
1374 } else if (shift_amount == 32) {
1375 *carry_out = (result & 1) == 1;
1376 result = 0;
1377 } else {
1378 ASSERT(shift_amount > 32);
1379 *carry_out = false;
1380 result = 0;
1381 }
1382 break;
1383 }
1384
1385 case LSR: {
1386 if (shift_amount == 0) {
1387 *carry_out = c_flag_;
1388 } else if (shift_amount < 32) {
1389 uint32_t uresult = static_cast<uint32_t>(result);
1390 uresult >>= (shift_amount - 1);
1391 *carry_out = (uresult & 1) == 1;
1392 uresult >>= 1;
1393 result = static_cast<int32_t>(uresult);
1394 } else if (shift_amount == 32) {
1395 *carry_out = (result < 0);
1396 result = 0;
1397 } else {
1398 *carry_out = false;
1399 result = 0;
1400 }
1401 break;
1402 }
1403
1404 case ROR: {
1405 UNIMPLEMENTED();
1406 break;
1407 }
1408
1409 default: {
1410 UNREACHABLE();
1411 break;
1412 }
1413 }
1414 }
1415 return result;
1416}
1417
1418
1419// Addressing Mode 1 - Data-processing operands:
1420// Get the value based on the shifter_operand with immediate.
1421int32_t Simulator::GetImm(Instr* instr, bool* carry_out) {
1422 int rotate = instr->RotateField() * 2;
1423 int immed8 = instr->Immed8Field();
1424 int imm = (immed8 >> rotate) | (immed8 << (32 - rotate));
1425 *carry_out = (rotate == 0) ? c_flag_ : (imm < 0);
1426 return imm;
1427}
1428
1429
1430static int count_bits(int bit_vector) {
1431 int count = 0;
1432 while (bit_vector != 0) {
1433 if ((bit_vector & 1) != 0) {
1434 count++;
1435 }
1436 bit_vector >>= 1;
1437 }
1438 return count;
1439}
1440
1441
1442// Addressing Mode 4 - Load and Store Multiple
1443void Simulator::HandleRList(Instr* instr, bool load) {
1444 int rn = instr->RnField();
1445 int32_t rn_val = get_register(rn);
1446 int rlist = instr->RlistField();
1447 int num_regs = count_bits(rlist);
1448
1449 intptr_t start_address = 0;
1450 intptr_t end_address = 0;
1451 switch (instr->PUField()) {
1452 case 0: {
1453 // Print("da");
1454 UNIMPLEMENTED();
1455 break;
1456 }
1457 case 1: {
1458 // Print("ia");
1459 start_address = rn_val;
1460 end_address = rn_val + (num_regs * 4) - 4;
1461 rn_val = rn_val + (num_regs * 4);
1462 break;
1463 }
1464 case 2: {
1465 // Print("db");
1466 start_address = rn_val - (num_regs * 4);
1467 end_address = rn_val - 4;
1468 rn_val = start_address;
1469 break;
1470 }
1471 case 3: {
1472 // Print("ib");
Steve Block791712a2010-08-27 10:21:07 +01001473 start_address = rn_val + 4;
1474 end_address = rn_val + (num_regs * 4);
1475 rn_val = end_address;
Steve Blocka7e24c12009-10-30 11:49:00 +00001476 break;
1477 }
1478 default: {
1479 UNREACHABLE();
1480 break;
1481 }
1482 }
1483 if (instr->HasW()) {
1484 set_register(rn, rn_val);
1485 }
1486 intptr_t* address = reinterpret_cast<intptr_t*>(start_address);
1487 int reg = 0;
1488 while (rlist != 0) {
1489 if ((rlist & 1) != 0) {
1490 if (load) {
1491 set_register(reg, *address);
1492 } else {
1493 *address = get_register(reg);
1494 }
1495 address += 1;
1496 }
1497 reg++;
1498 rlist >>= 1;
1499 }
1500 ASSERT(end_address == ((intptr_t)address) - 4);
1501}
1502
1503
1504// Calls into the V8 runtime are based on this very simple interface.
1505// Note: To be able to return two values from some calls the code in runtime.cc
1506// uses the ObjectPair which is essentially two 32-bit values stuffed into a
1507// 64-bit value. With the code below we assume that all runtime calls return
1508// 64 bits of result. If they don't, the r1 result register contains a bogus
1509// value, which is fine because it is caller-saved.
1510typedef int64_t (*SimulatorRuntimeCall)(int32_t arg0,
1511 int32_t arg1,
1512 int32_t arg2,
1513 int32_t arg3);
1514typedef double (*SimulatorRuntimeFPCall)(int32_t arg0,
1515 int32_t arg1,
1516 int32_t arg2,
1517 int32_t arg3);
1518
1519
1520// Software interrupt instructions are used by the simulator to call into the
1521// C-based V8 runtime.
1522void Simulator::SoftwareInterrupt(Instr* instr) {
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001523 int svc = instr->SvcField();
1524 switch (svc) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001525 case call_rt_redirected: {
Steve Block6ded16b2010-05-10 14:33:55 +01001526 // Check if stack is aligned. Error if not aligned is reported below to
1527 // include information on the function called.
1528 bool stack_aligned =
1529 (get_register(sp)
1530 & (::v8::internal::FLAG_sim_stack_alignment - 1)) == 0;
Steve Blocka7e24c12009-10-30 11:49:00 +00001531 Redirection* redirection = Redirection::FromSwiInstruction(instr);
1532 int32_t arg0 = get_register(r0);
1533 int32_t arg1 = get_register(r1);
1534 int32_t arg2 = get_register(r2);
1535 int32_t arg3 = get_register(r3);
1536 // This is dodgy but it works because the C entry stubs are never moved.
1537 // See comment in codegen-arm.cc and bug 1242173.
1538 int32_t saved_lr = get_register(lr);
1539 if (redirection->fp_return()) {
1540 intptr_t external =
1541 reinterpret_cast<intptr_t>(redirection->external_function());
1542 SimulatorRuntimeFPCall target =
1543 reinterpret_cast<SimulatorRuntimeFPCall>(external);
Steve Block6ded16b2010-05-10 14:33:55 +01001544 if (::v8::internal::FLAG_trace_sim || !stack_aligned) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001545 double x, y;
1546 GetFpArgs(&x, &y);
Steve Block6ded16b2010-05-10 14:33:55 +01001547 PrintF("Call to host function at %p with args %f, %f",
Steve Blocka7e24c12009-10-30 11:49:00 +00001548 FUNCTION_ADDR(target), x, y);
Steve Block6ded16b2010-05-10 14:33:55 +01001549 if (!stack_aligned) {
1550 PrintF(" with unaligned stack %08x\n", get_register(sp));
1551 }
1552 PrintF("\n");
Steve Blocka7e24c12009-10-30 11:49:00 +00001553 }
Steve Block6ded16b2010-05-10 14:33:55 +01001554 CHECK(stack_aligned);
Steve Blocka7e24c12009-10-30 11:49:00 +00001555 double result = target(arg0, arg1, arg2, arg3);
1556 SetFpResult(result);
1557 } else {
1558 intptr_t external =
1559 reinterpret_cast<int32_t>(redirection->external_function());
1560 SimulatorRuntimeCall target =
1561 reinterpret_cast<SimulatorRuntimeCall>(external);
Steve Block6ded16b2010-05-10 14:33:55 +01001562 if (::v8::internal::FLAG_trace_sim || !stack_aligned) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001563 PrintF(
Steve Block6ded16b2010-05-10 14:33:55 +01001564 "Call to host function at %p with args %08x, %08x, %08x, %08x",
Steve Blocka7e24c12009-10-30 11:49:00 +00001565 FUNCTION_ADDR(target),
1566 arg0,
1567 arg1,
1568 arg2,
1569 arg3);
Steve Block6ded16b2010-05-10 14:33:55 +01001570 if (!stack_aligned) {
1571 PrintF(" with unaligned stack %08x\n", get_register(sp));
1572 }
1573 PrintF("\n");
Steve Blocka7e24c12009-10-30 11:49:00 +00001574 }
Steve Block6ded16b2010-05-10 14:33:55 +01001575 CHECK(stack_aligned);
Steve Blocka7e24c12009-10-30 11:49:00 +00001576 int64_t result = target(arg0, arg1, arg2, arg3);
1577 int32_t lo_res = static_cast<int32_t>(result);
1578 int32_t hi_res = static_cast<int32_t>(result >> 32);
1579 if (::v8::internal::FLAG_trace_sim) {
1580 PrintF("Returned %08x\n", lo_res);
1581 }
1582 set_register(r0, lo_res);
1583 set_register(r1, hi_res);
1584 }
1585 set_register(lr, saved_lr);
1586 set_pc(get_register(lr));
1587 break;
1588 }
1589 case break_point: {
1590 Debugger dbg(this);
1591 dbg.Debug();
1592 break;
1593 }
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001594 // stop uses all codes greater than 1 << 23.
Steve Blocka7e24c12009-10-30 11:49:00 +00001595 default: {
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001596 if (svc >= (1 << 23)) {
1597 uint32_t code = svc & kStopCodeMask;
1598 if (isWatchedStop(code)) {
1599 IncreaseStopCounter(code);
1600 }
1601 // Stop if it is enabled, otherwise go on jumping over the stop
1602 // and the message address.
1603 if (isEnabledStop(code)) {
1604 Debugger dbg(this);
1605 dbg.Stop(instr);
1606 } else {
1607 set_pc(get_pc() + 2 * Instr::kInstrSize);
1608 }
1609 } else {
1610 // This is not a valid svc code.
1611 UNREACHABLE();
1612 break;
1613 }
1614 }
1615 }
1616}
1617
1618
1619// Stop helper functions.
1620bool Simulator::isStopInstruction(Instr* instr) {
1621 return (instr->Bits(27, 24) == 0xF) && (instr->SvcField() >= stop);
1622}
1623
1624
1625bool Simulator::isWatchedStop(uint32_t code) {
1626 ASSERT(code <= kMaxStopCode);
1627 return code < kNumOfWatchedStops;
1628}
1629
1630
1631bool Simulator::isEnabledStop(uint32_t code) {
1632 ASSERT(code <= kMaxStopCode);
1633 // Unwatched stops are always enabled.
1634 return !isWatchedStop(code) ||
1635 !(watched_stops[code].count & kStopDisabledBit);
1636}
1637
1638
1639void Simulator::EnableStop(uint32_t code) {
1640 ASSERT(isWatchedStop(code));
1641 if (!isEnabledStop(code)) {
1642 watched_stops[code].count &= ~kStopDisabledBit;
1643 }
1644}
1645
1646
1647void Simulator::DisableStop(uint32_t code) {
1648 ASSERT(isWatchedStop(code));
1649 if (isEnabledStop(code)) {
1650 watched_stops[code].count |= kStopDisabledBit;
1651 }
1652}
1653
1654
1655void Simulator::IncreaseStopCounter(uint32_t code) {
1656 ASSERT(code <= kMaxStopCode);
1657 ASSERT(isWatchedStop(code));
1658 if ((watched_stops[code].count & ~(1 << 31)) == 0x7fffffff) {
1659 PrintF("Stop counter for code %i has overflowed.\n"
1660 "Enabling this code and reseting the counter to 0.\n", code);
1661 watched_stops[code].count = 0;
1662 EnableStop(code);
1663 } else {
1664 watched_stops[code].count++;
1665 }
1666}
1667
1668
1669// Print a stop status.
1670void Simulator::PrintStopInfo(uint32_t code) {
1671 ASSERT(code <= kMaxStopCode);
1672 if (!isWatchedStop(code)) {
1673 PrintF("Stop not watched.");
1674 } else {
1675 const char* state = isEnabledStop(code) ? "Enabled" : "Disabled";
1676 int32_t count = watched_stops[code].count & ~kStopDisabledBit;
1677 // Don't print the state of unused breakpoints.
1678 if (count != 0) {
1679 if (watched_stops[code].desc) {
1680 PrintF("stop %i - 0x%x: \t%s, \tcounter = %i, \t%s\n",
1681 code, code, state, count, watched_stops[code].desc);
1682 } else {
1683 PrintF("stop %i - 0x%x: \t%s, \tcounter = %i\n",
1684 code, code, state, count);
1685 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001686 }
1687 }
1688}
1689
1690
1691// Handle execution based on instruction types.
1692
1693// Instruction types 0 and 1 are both rolled into one function because they
1694// only differ in the handling of the shifter_operand.
1695void Simulator::DecodeType01(Instr* instr) {
1696 int type = instr->TypeField();
1697 if ((type == 0) && instr->IsSpecialType0()) {
1698 // multiply instruction or extra loads and stores
1699 if (instr->Bits(7, 4) == 9) {
1700 if (instr->Bit(24) == 0) {
1701 // Raw field decoding here. Multiply instructions have their Rd in
1702 // funny places.
1703 int rn = instr->RnField();
1704 int rm = instr->RmField();
1705 int rs = instr->RsField();
1706 int32_t rs_val = get_register(rs);
1707 int32_t rm_val = get_register(rm);
1708 if (instr->Bit(23) == 0) {
1709 if (instr->Bit(21) == 0) {
1710 // The MUL instruction description (A 4.1.33) refers to Rd as being
1711 // the destination for the operation, but it confusingly uses the
1712 // Rn field to encode it.
1713 // Format(instr, "mul'cond's 'rn, 'rm, 'rs");
1714 int rd = rn; // Remap the rn field to the Rd register.
1715 int32_t alu_out = rm_val * rs_val;
1716 set_register(rd, alu_out);
1717 if (instr->HasS()) {
1718 SetNZFlags(alu_out);
1719 }
1720 } else {
1721 // The MLA instruction description (A 4.1.28) refers to the order
1722 // of registers as "Rd, Rm, Rs, Rn". But confusingly it uses the
1723 // Rn field to encode the Rd register and the Rd field to encode
1724 // the Rn register.
1725 Format(instr, "mla'cond's 'rn, 'rm, 'rs, 'rd");
1726 }
1727 } else {
1728 // The signed/long multiply instructions use the terms RdHi and RdLo
1729 // when referring to the target registers. They are mapped to the Rn
1730 // and Rd fields as follows:
1731 // RdLo == Rd
1732 // RdHi == Rn (This is confusingly stored in variable rd here
1733 // because the mul instruction from above uses the
1734 // Rn field to encode the Rd register. Good luck figuring
1735 // this out without reading the ARM instruction manual
1736 // at a very detailed level.)
1737 // Format(instr, "'um'al'cond's 'rd, 'rn, 'rs, 'rm");
1738 int rd_hi = rn; // Remap the rn field to the RdHi register.
1739 int rd_lo = instr->RdField();
1740 int32_t hi_res = 0;
1741 int32_t lo_res = 0;
1742 if (instr->Bit(22) == 1) {
1743 int64_t left_op = static_cast<int32_t>(rm_val);
1744 int64_t right_op = static_cast<int32_t>(rs_val);
1745 uint64_t result = left_op * right_op;
1746 hi_res = static_cast<int32_t>(result >> 32);
1747 lo_res = static_cast<int32_t>(result & 0xffffffff);
1748 } else {
1749 // unsigned multiply
1750 uint64_t left_op = static_cast<uint32_t>(rm_val);
1751 uint64_t right_op = static_cast<uint32_t>(rs_val);
1752 uint64_t result = left_op * right_op;
1753 hi_res = static_cast<int32_t>(result >> 32);
1754 lo_res = static_cast<int32_t>(result & 0xffffffff);
1755 }
1756 set_register(rd_lo, lo_res);
1757 set_register(rd_hi, hi_res);
1758 if (instr->HasS()) {
1759 UNIMPLEMENTED();
1760 }
1761 }
1762 } else {
Steve Blockd0582a62009-12-15 09:54:21 +00001763 UNIMPLEMENTED(); // Not used by V8.
Steve Blocka7e24c12009-10-30 11:49:00 +00001764 }
1765 } else {
1766 // extra load/store instructions
1767 int rd = instr->RdField();
1768 int rn = instr->RnField();
1769 int32_t rn_val = get_register(rn);
1770 int32_t addr = 0;
1771 if (instr->Bit(22) == 0) {
1772 int rm = instr->RmField();
1773 int32_t rm_val = get_register(rm);
1774 switch (instr->PUField()) {
1775 case 0: {
1776 // Format(instr, "'memop'cond'sign'h 'rd, ['rn], -'rm");
1777 ASSERT(!instr->HasW());
1778 addr = rn_val;
1779 rn_val -= rm_val;
1780 set_register(rn, rn_val);
1781 break;
1782 }
1783 case 1: {
1784 // Format(instr, "'memop'cond'sign'h 'rd, ['rn], +'rm");
1785 ASSERT(!instr->HasW());
1786 addr = rn_val;
1787 rn_val += rm_val;
1788 set_register(rn, rn_val);
1789 break;
1790 }
1791 case 2: {
1792 // Format(instr, "'memop'cond'sign'h 'rd, ['rn, -'rm]'w");
1793 rn_val -= rm_val;
1794 addr = rn_val;
1795 if (instr->HasW()) {
1796 set_register(rn, rn_val);
1797 }
1798 break;
1799 }
1800 case 3: {
1801 // Format(instr, "'memop'cond'sign'h 'rd, ['rn, +'rm]'w");
1802 rn_val += rm_val;
1803 addr = rn_val;
1804 if (instr->HasW()) {
1805 set_register(rn, rn_val);
1806 }
1807 break;
1808 }
1809 default: {
1810 // The PU field is a 2-bit field.
1811 UNREACHABLE();
1812 break;
1813 }
1814 }
1815 } else {
1816 int32_t imm_val = (instr->ImmedHField() << 4) | instr->ImmedLField();
1817 switch (instr->PUField()) {
1818 case 0: {
1819 // Format(instr, "'memop'cond'sign'h 'rd, ['rn], #-'off8");
1820 ASSERT(!instr->HasW());
1821 addr = rn_val;
1822 rn_val -= imm_val;
1823 set_register(rn, rn_val);
1824 break;
1825 }
1826 case 1: {
1827 // Format(instr, "'memop'cond'sign'h 'rd, ['rn], #+'off8");
1828 ASSERT(!instr->HasW());
1829 addr = rn_val;
1830 rn_val += imm_val;
1831 set_register(rn, rn_val);
1832 break;
1833 }
1834 case 2: {
1835 // Format(instr, "'memop'cond'sign'h 'rd, ['rn, #-'off8]'w");
1836 rn_val -= imm_val;
1837 addr = rn_val;
1838 if (instr->HasW()) {
1839 set_register(rn, rn_val);
1840 }
1841 break;
1842 }
1843 case 3: {
1844 // Format(instr, "'memop'cond'sign'h 'rd, ['rn, #+'off8]'w");
1845 rn_val += imm_val;
1846 addr = rn_val;
1847 if (instr->HasW()) {
1848 set_register(rn, rn_val);
1849 }
1850 break;
1851 }
1852 default: {
1853 // The PU field is a 2-bit field.
1854 UNREACHABLE();
1855 break;
1856 }
1857 }
1858 }
Kristian Monsen25f61362010-05-21 11:50:48 +01001859 if (((instr->Bits(7, 4) & 0xd) == 0xd) && (instr->Bit(20) == 0)) {
1860 ASSERT((rd % 2) == 0);
1861 if (instr->HasH()) {
1862 // The strd instruction.
1863 int32_t value1 = get_register(rd);
1864 int32_t value2 = get_register(rd+1);
1865 WriteDW(addr, value1, value2);
1866 } else {
1867 // The ldrd instruction.
1868 int* rn_data = ReadDW(addr);
1869 set_dw_register(rd, rn_data);
1870 }
1871 } else if (instr->HasH()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001872 if (instr->HasSign()) {
1873 if (instr->HasL()) {
1874 int16_t val = ReadH(addr, instr);
1875 set_register(rd, val);
1876 } else {
1877 int16_t val = get_register(rd);
1878 WriteH(addr, val, instr);
1879 }
1880 } else {
1881 if (instr->HasL()) {
1882 uint16_t val = ReadHU(addr, instr);
1883 set_register(rd, val);
1884 } else {
1885 uint16_t val = get_register(rd);
1886 WriteH(addr, val, instr);
1887 }
1888 }
1889 } else {
1890 // signed byte loads
1891 ASSERT(instr->HasSign());
1892 ASSERT(instr->HasL());
1893 int8_t val = ReadB(addr);
1894 set_register(rd, val);
1895 }
1896 return;
1897 }
Steve Block6ded16b2010-05-10 14:33:55 +01001898 } else if ((type == 0) && instr->IsMiscType0()) {
1899 if (instr->Bits(22, 21) == 1) {
1900 int rm = instr->RmField();
1901 switch (instr->Bits(7, 4)) {
1902 case BX:
1903 set_pc(get_register(rm));
1904 break;
1905 case BLX: {
1906 uint32_t old_pc = get_pc();
1907 set_pc(get_register(rm));
1908 set_register(lr, old_pc + Instr::kInstrSize);
1909 break;
1910 }
1911 case BKPT:
1912 v8::internal::OS::DebugBreak();
1913 break;
1914 default:
1915 UNIMPLEMENTED();
1916 }
1917 } else if (instr->Bits(22, 21) == 3) {
1918 int rm = instr->RmField();
1919 int rd = instr->RdField();
1920 switch (instr->Bits(7, 4)) {
1921 case CLZ: {
1922 uint32_t bits = get_register(rm);
1923 int leading_zeros = 0;
1924 if (bits == 0) {
1925 leading_zeros = 32;
1926 } else {
1927 while ((bits & 0x80000000u) == 0) {
1928 bits <<= 1;
1929 leading_zeros++;
1930 }
1931 }
1932 set_register(rd, leading_zeros);
1933 break;
1934 }
1935 default:
1936 UNIMPLEMENTED();
1937 }
1938 } else {
1939 PrintF("%08x\n", instr->InstructionBits());
1940 UNIMPLEMENTED();
1941 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001942 } else {
1943 int rd = instr->RdField();
1944 int rn = instr->RnField();
1945 int32_t rn_val = get_register(rn);
1946 int32_t shifter_operand = 0;
1947 bool shifter_carry_out = 0;
1948 if (type == 0) {
1949 shifter_operand = GetShiftRm(instr, &shifter_carry_out);
1950 } else {
1951 ASSERT(instr->TypeField() == 1);
1952 shifter_operand = GetImm(instr, &shifter_carry_out);
1953 }
1954 int32_t alu_out;
1955
1956 switch (instr->OpcodeField()) {
1957 case AND: {
1958 // Format(instr, "and'cond's 'rd, 'rn, 'shift_rm");
1959 // Format(instr, "and'cond's 'rd, 'rn, 'imm");
1960 alu_out = rn_val & shifter_operand;
1961 set_register(rd, alu_out);
1962 if (instr->HasS()) {
1963 SetNZFlags(alu_out);
1964 SetCFlag(shifter_carry_out);
1965 }
1966 break;
1967 }
1968
1969 case EOR: {
1970 // Format(instr, "eor'cond's 'rd, 'rn, 'shift_rm");
1971 // Format(instr, "eor'cond's 'rd, 'rn, 'imm");
1972 alu_out = rn_val ^ shifter_operand;
1973 set_register(rd, alu_out);
1974 if (instr->HasS()) {
1975 SetNZFlags(alu_out);
1976 SetCFlag(shifter_carry_out);
1977 }
1978 break;
1979 }
1980
1981 case SUB: {
1982 // Format(instr, "sub'cond's 'rd, 'rn, 'shift_rm");
1983 // Format(instr, "sub'cond's 'rd, 'rn, 'imm");
1984 alu_out = rn_val - shifter_operand;
1985 set_register(rd, alu_out);
1986 if (instr->HasS()) {
1987 SetNZFlags(alu_out);
1988 SetCFlag(!BorrowFrom(rn_val, shifter_operand));
1989 SetVFlag(OverflowFrom(alu_out, rn_val, shifter_operand, false));
1990 }
1991 break;
1992 }
1993
1994 case RSB: {
1995 // Format(instr, "rsb'cond's 'rd, 'rn, 'shift_rm");
1996 // Format(instr, "rsb'cond's 'rd, 'rn, 'imm");
1997 alu_out = shifter_operand - rn_val;
1998 set_register(rd, alu_out);
1999 if (instr->HasS()) {
2000 SetNZFlags(alu_out);
2001 SetCFlag(!BorrowFrom(shifter_operand, rn_val));
2002 SetVFlag(OverflowFrom(alu_out, shifter_operand, rn_val, false));
2003 }
2004 break;
2005 }
2006
2007 case ADD: {
2008 // Format(instr, "add'cond's 'rd, 'rn, 'shift_rm");
2009 // Format(instr, "add'cond's 'rd, 'rn, 'imm");
2010 alu_out = rn_val + shifter_operand;
2011 set_register(rd, alu_out);
2012 if (instr->HasS()) {
2013 SetNZFlags(alu_out);
2014 SetCFlag(CarryFrom(rn_val, shifter_operand));
2015 SetVFlag(OverflowFrom(alu_out, rn_val, shifter_operand, true));
2016 }
2017 break;
2018 }
2019
2020 case ADC: {
2021 Format(instr, "adc'cond's 'rd, 'rn, 'shift_rm");
2022 Format(instr, "adc'cond's 'rd, 'rn, 'imm");
2023 break;
2024 }
2025
2026 case SBC: {
2027 Format(instr, "sbc'cond's 'rd, 'rn, 'shift_rm");
2028 Format(instr, "sbc'cond's 'rd, 'rn, 'imm");
2029 break;
2030 }
2031
2032 case RSC: {
2033 Format(instr, "rsc'cond's 'rd, 'rn, 'shift_rm");
2034 Format(instr, "rsc'cond's 'rd, 'rn, 'imm");
2035 break;
2036 }
2037
2038 case TST: {
2039 if (instr->HasS()) {
2040 // Format(instr, "tst'cond 'rn, 'shift_rm");
2041 // Format(instr, "tst'cond 'rn, 'imm");
2042 alu_out = rn_val & shifter_operand;
2043 SetNZFlags(alu_out);
2044 SetCFlag(shifter_carry_out);
2045 } else {
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01002046 // Format(instr, "movw'cond 'rd, 'imm").
2047 alu_out = instr->ImmedMovwMovtField();
2048 set_register(rd, alu_out);
Steve Blocka7e24c12009-10-30 11:49:00 +00002049 }
2050 break;
2051 }
2052
2053 case TEQ: {
2054 if (instr->HasS()) {
2055 // Format(instr, "teq'cond 'rn, 'shift_rm");
2056 // Format(instr, "teq'cond 'rn, 'imm");
2057 alu_out = rn_val ^ shifter_operand;
2058 SetNZFlags(alu_out);
2059 SetCFlag(shifter_carry_out);
2060 } else {
Steve Block6ded16b2010-05-10 14:33:55 +01002061 // Other instructions matching this pattern are handled in the
2062 // miscellaneous instructions part above.
2063 UNREACHABLE();
Steve Blocka7e24c12009-10-30 11:49:00 +00002064 }
2065 break;
2066 }
2067
2068 case CMP: {
2069 if (instr->HasS()) {
2070 // Format(instr, "cmp'cond 'rn, 'shift_rm");
2071 // Format(instr, "cmp'cond 'rn, 'imm");
2072 alu_out = rn_val - shifter_operand;
2073 SetNZFlags(alu_out);
2074 SetCFlag(!BorrowFrom(rn_val, shifter_operand));
2075 SetVFlag(OverflowFrom(alu_out, rn_val, shifter_operand, false));
2076 } else {
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01002077 // Format(instr, "movt'cond 'rd, 'imm").
2078 alu_out = (get_register(rd) & 0xffff) |
2079 (instr->ImmedMovwMovtField() << 16);
2080 set_register(rd, alu_out);
Steve Blocka7e24c12009-10-30 11:49:00 +00002081 }
2082 break;
2083 }
2084
2085 case CMN: {
2086 if (instr->HasS()) {
2087 // Format(instr, "cmn'cond 'rn, 'shift_rm");
2088 // Format(instr, "cmn'cond 'rn, 'imm");
2089 alu_out = rn_val + shifter_operand;
2090 SetNZFlags(alu_out);
2091 SetCFlag(!CarryFrom(rn_val, shifter_operand));
2092 SetVFlag(OverflowFrom(alu_out, rn_val, shifter_operand, true));
2093 } else {
Steve Block6ded16b2010-05-10 14:33:55 +01002094 // Other instructions matching this pattern are handled in the
2095 // miscellaneous instructions part above.
2096 UNREACHABLE();
Steve Blocka7e24c12009-10-30 11:49:00 +00002097 }
2098 break;
2099 }
2100
2101 case ORR: {
2102 // Format(instr, "orr'cond's 'rd, 'rn, 'shift_rm");
2103 // Format(instr, "orr'cond's 'rd, 'rn, 'imm");
2104 alu_out = rn_val | shifter_operand;
2105 set_register(rd, alu_out);
2106 if (instr->HasS()) {
2107 SetNZFlags(alu_out);
2108 SetCFlag(shifter_carry_out);
2109 }
2110 break;
2111 }
2112
2113 case MOV: {
2114 // Format(instr, "mov'cond's 'rd, 'shift_rm");
2115 // Format(instr, "mov'cond's 'rd, 'imm");
2116 alu_out = shifter_operand;
2117 set_register(rd, alu_out);
2118 if (instr->HasS()) {
2119 SetNZFlags(alu_out);
2120 SetCFlag(shifter_carry_out);
2121 }
2122 break;
2123 }
2124
2125 case BIC: {
2126 // Format(instr, "bic'cond's 'rd, 'rn, 'shift_rm");
2127 // Format(instr, "bic'cond's 'rd, 'rn, 'imm");
2128 alu_out = rn_val & ~shifter_operand;
2129 set_register(rd, alu_out);
2130 if (instr->HasS()) {
2131 SetNZFlags(alu_out);
2132 SetCFlag(shifter_carry_out);
2133 }
2134 break;
2135 }
2136
2137 case MVN: {
2138 // Format(instr, "mvn'cond's 'rd, 'shift_rm");
2139 // Format(instr, "mvn'cond's 'rd, 'imm");
2140 alu_out = ~shifter_operand;
2141 set_register(rd, alu_out);
2142 if (instr->HasS()) {
2143 SetNZFlags(alu_out);
2144 SetCFlag(shifter_carry_out);
2145 }
2146 break;
2147 }
2148
2149 default: {
2150 UNREACHABLE();
2151 break;
2152 }
2153 }
2154 }
2155}
2156
2157
2158void Simulator::DecodeType2(Instr* instr) {
2159 int rd = instr->RdField();
2160 int rn = instr->RnField();
2161 int32_t rn_val = get_register(rn);
2162 int32_t im_val = instr->Offset12Field();
2163 int32_t addr = 0;
2164 switch (instr->PUField()) {
2165 case 0: {
2166 // Format(instr, "'memop'cond'b 'rd, ['rn], #-'off12");
2167 ASSERT(!instr->HasW());
2168 addr = rn_val;
2169 rn_val -= im_val;
2170 set_register(rn, rn_val);
2171 break;
2172 }
2173 case 1: {
2174 // Format(instr, "'memop'cond'b 'rd, ['rn], #+'off12");
2175 ASSERT(!instr->HasW());
2176 addr = rn_val;
2177 rn_val += im_val;
2178 set_register(rn, rn_val);
2179 break;
2180 }
2181 case 2: {
2182 // Format(instr, "'memop'cond'b 'rd, ['rn, #-'off12]'w");
2183 rn_val -= im_val;
2184 addr = rn_val;
2185 if (instr->HasW()) {
2186 set_register(rn, rn_val);
2187 }
2188 break;
2189 }
2190 case 3: {
2191 // Format(instr, "'memop'cond'b 'rd, ['rn, #+'off12]'w");
2192 rn_val += im_val;
2193 addr = rn_val;
2194 if (instr->HasW()) {
2195 set_register(rn, rn_val);
2196 }
2197 break;
2198 }
2199 default: {
2200 UNREACHABLE();
2201 break;
2202 }
2203 }
2204 if (instr->HasB()) {
2205 if (instr->HasL()) {
2206 byte val = ReadBU(addr);
2207 set_register(rd, val);
2208 } else {
2209 byte val = get_register(rd);
2210 WriteB(addr, val);
2211 }
2212 } else {
2213 if (instr->HasL()) {
2214 set_register(rd, ReadW(addr, instr));
2215 } else {
2216 WriteW(addr, get_register(rd), instr);
2217 }
2218 }
2219}
2220
2221
2222void Simulator::DecodeType3(Instr* instr) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002223 int rd = instr->RdField();
2224 int rn = instr->RnField();
2225 int32_t rn_val = get_register(rn);
2226 bool shifter_carry_out = 0;
2227 int32_t shifter_operand = GetShiftRm(instr, &shifter_carry_out);
2228 int32_t addr = 0;
2229 switch (instr->PUField()) {
2230 case 0: {
2231 ASSERT(!instr->HasW());
2232 Format(instr, "'memop'cond'b 'rd, ['rn], -'shift_rm");
Kristian Monsen50ef84f2010-07-29 15:18:00 +01002233 UNIMPLEMENTED();
Steve Blocka7e24c12009-10-30 11:49:00 +00002234 break;
2235 }
2236 case 1: {
Kristian Monsen50ef84f2010-07-29 15:18:00 +01002237 if (instr->HasW()) {
2238 ASSERT(instr->Bits(5, 4) == 0x1);
2239
2240 if (instr->Bit(22) == 0x1) { // USAT.
2241 int32_t sat_pos = instr->Bits(20, 16);
2242 int32_t sat_val = (1 << sat_pos) - 1;
2243 int32_t shift = instr->Bits(11, 7);
2244 int32_t shift_type = instr->Bit(6);
2245 int32_t rm_val = get_register(instr->RmField());
2246 if (shift_type == 0) { // LSL
2247 rm_val <<= shift;
2248 } else { // ASR
2249 rm_val >>= shift;
2250 }
2251 // If saturation occurs, the Q flag should be set in the CPSR.
2252 // There is no Q flag yet, and no instruction (MRS) to read the
2253 // CPSR directly.
2254 if (rm_val > sat_val) {
2255 rm_val = sat_val;
2256 } else if (rm_val < 0) {
2257 rm_val = 0;
2258 }
2259 set_register(rd, rm_val);
2260 } else { // SSAT.
2261 UNIMPLEMENTED();
2262 }
2263 return;
2264 } else {
2265 Format(instr, "'memop'cond'b 'rd, ['rn], +'shift_rm");
2266 UNIMPLEMENTED();
2267 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002268 break;
2269 }
2270 case 2: {
2271 // Format(instr, "'memop'cond'b 'rd, ['rn, -'shift_rm]'w");
2272 addr = rn_val - shifter_operand;
2273 if (instr->HasW()) {
2274 set_register(rn, addr);
2275 }
2276 break;
2277 }
2278 case 3: {
Andrei Popescu31002712010-02-23 13:46:05 +00002279 if (instr->HasW() && (instr->Bits(6, 4) == 0x5)) {
2280 uint32_t widthminus1 = static_cast<uint32_t>(instr->Bits(20, 16));
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002281 uint32_t lsbit = static_cast<uint32_t>(instr->Bits(11, 7));
Andrei Popescu31002712010-02-23 13:46:05 +00002282 uint32_t msbit = widthminus1 + lsbit;
2283 if (msbit <= 31) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002284 if (instr->Bit(22)) {
2285 // ubfx - unsigned bitfield extract.
2286 uint32_t rm_val =
2287 static_cast<uint32_t>(get_register(instr->RmField()));
2288 uint32_t extr_val = rm_val << (31 - msbit);
2289 extr_val = extr_val >> (31 - widthminus1);
2290 set_register(instr->RdField(), extr_val);
2291 } else {
2292 // sbfx - signed bitfield extract.
2293 int32_t rm_val = get_register(instr->RmField());
2294 int32_t extr_val = rm_val << (31 - msbit);
2295 extr_val = extr_val >> (31 - widthminus1);
2296 set_register(instr->RdField(), extr_val);
2297 }
2298 } else {
2299 UNREACHABLE();
2300 }
2301 return;
2302 } else if (!instr->HasW() && (instr->Bits(6, 4) == 0x1)) {
2303 uint32_t lsbit = static_cast<uint32_t>(instr->Bits(11, 7));
2304 uint32_t msbit = static_cast<uint32_t>(instr->Bits(20, 16));
2305 if (msbit >= lsbit) {
2306 // bfc or bfi - bitfield clear/insert.
2307 uint32_t rd_val =
2308 static_cast<uint32_t>(get_register(instr->RdField()));
2309 uint32_t bitcount = msbit - lsbit + 1;
2310 uint32_t mask = (1 << bitcount) - 1;
2311 rd_val &= ~(mask << lsbit);
2312 if (instr->RmField() != 15) {
2313 // bfi - bitfield insert.
2314 uint32_t rm_val =
2315 static_cast<uint32_t>(get_register(instr->RmField()));
2316 rm_val &= mask;
2317 rd_val |= rm_val << lsbit;
2318 }
2319 set_register(instr->RdField(), rd_val);
Andrei Popescu31002712010-02-23 13:46:05 +00002320 } else {
2321 UNREACHABLE();
2322 }
2323 return;
2324 } else {
2325 // Format(instr, "'memop'cond'b 'rd, ['rn, +'shift_rm]'w");
2326 addr = rn_val + shifter_operand;
2327 if (instr->HasW()) {
2328 set_register(rn, addr);
2329 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002330 }
2331 break;
2332 }
2333 default: {
2334 UNREACHABLE();
2335 break;
2336 }
2337 }
2338 if (instr->HasB()) {
2339 if (instr->HasL()) {
2340 uint8_t byte = ReadB(addr);
2341 set_register(rd, byte);
2342 } else {
Andrei Popescu31002712010-02-23 13:46:05 +00002343 uint8_t byte = get_register(rd);
2344 WriteB(addr, byte);
Steve Blocka7e24c12009-10-30 11:49:00 +00002345 }
2346 } else {
2347 if (instr->HasL()) {
2348 set_register(rd, ReadW(addr, instr));
2349 } else {
2350 WriteW(addr, get_register(rd), instr);
2351 }
2352 }
2353}
2354
2355
2356void Simulator::DecodeType4(Instr* instr) {
2357 ASSERT(instr->Bit(22) == 0); // only allowed to be set in privileged mode
2358 if (instr->HasL()) {
2359 // Format(instr, "ldm'cond'pu 'rn'w, 'rlist");
2360 HandleRList(instr, true);
2361 } else {
2362 // Format(instr, "stm'cond'pu 'rn'w, 'rlist");
2363 HandleRList(instr, false);
2364 }
2365}
2366
2367
2368void Simulator::DecodeType5(Instr* instr) {
2369 // Format(instr, "b'l'cond 'target");
2370 int off = (instr->SImmed24Field() << 2);
2371 intptr_t pc_address = get_pc();
2372 if (instr->HasLink()) {
2373 set_register(lr, pc_address + Instr::kInstrSize);
2374 }
2375 int pc_reg = get_register(pc);
2376 set_pc(pc_reg + off);
2377}
2378
2379
2380void Simulator::DecodeType6(Instr* instr) {
Steve Blockd0582a62009-12-15 09:54:21 +00002381 DecodeType6CoprocessorIns(instr);
Steve Blocka7e24c12009-10-30 11:49:00 +00002382}
2383
2384
2385void Simulator::DecodeType7(Instr* instr) {
2386 if (instr->Bit(24) == 1) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002387 SoftwareInterrupt(instr);
2388 } else {
Steve Blockd0582a62009-12-15 09:54:21 +00002389 DecodeTypeVFP(instr);
Steve Blocka7e24c12009-10-30 11:49:00 +00002390 }
2391}
2392
2393
Steve Blockd0582a62009-12-15 09:54:21 +00002394// void Simulator::DecodeTypeVFP(Instr* instr)
2395// The Following ARMv7 VFPv instructions are currently supported.
Leon Clarkee46be812010-01-19 14:06:41 +00002396// vmov :Sn = Rt
2397// vmov :Rt = Sn
2398// vcvt: Dd = Sm
2399// vcvt: Sd = Dm
2400// Dd = vadd(Dn, Dm)
2401// Dd = vsub(Dn, Dm)
2402// Dd = vmul(Dn, Dm)
2403// Dd = vdiv(Dn, Dm)
Steve Blockd0582a62009-12-15 09:54:21 +00002404// vcmp(Dd, Dm)
Steve Block8defd9f2010-07-08 12:39:36 +01002405// vmrs
2406// Dd = vsqrt(Dm)
Steve Blockd0582a62009-12-15 09:54:21 +00002407void Simulator::DecodeTypeVFP(Instr* instr) {
2408 ASSERT((instr->TypeField() == 7) && (instr->Bit(24) == 0x0) );
Steve Block6ded16b2010-05-10 14:33:55 +01002409 ASSERT(instr->Bits(11, 9) == 0x5);
Steve Blockd0582a62009-12-15 09:54:21 +00002410
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002411 // Obtain double precision register codes.
2412 int vm = instr->VFPMRegCode(kDoublePrecision);
2413 int vd = instr->VFPDRegCode(kDoublePrecision);
2414 int vn = instr->VFPNRegCode(kDoublePrecision);
Steve Blockd0582a62009-12-15 09:54:21 +00002415
Steve Block6ded16b2010-05-10 14:33:55 +01002416 if (instr->Bit(4) == 0) {
2417 if (instr->Opc1Field() == 0x7) {
2418 // Other data processing instructions
Steve Block8defd9f2010-07-08 12:39:36 +01002419 if ((instr->Opc2Field() == 0x0) && (instr->Opc3Field() == 0x1)) {
2420 // vmov register to register.
2421 if (instr->SzField() == 0x1) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002422 int m = instr->VFPMRegCode(kDoublePrecision);
2423 int d = instr->VFPDRegCode(kDoublePrecision);
2424 set_d_register_from_double(d, get_double_from_d_register(m));
Steve Block8defd9f2010-07-08 12:39:36 +01002425 } else {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002426 int m = instr->VFPMRegCode(kSinglePrecision);
2427 int d = instr->VFPDRegCode(kSinglePrecision);
2428 set_s_register_from_float(d, get_float_from_s_register(m));
Steve Block8defd9f2010-07-08 12:39:36 +01002429 }
2430 } else if ((instr->Opc2Field() == 0x7) && (instr->Opc3Field() == 0x3)) {
Steve Block6ded16b2010-05-10 14:33:55 +01002431 DecodeVCVTBetweenDoubleAndSingle(instr);
2432 } else if ((instr->Opc2Field() == 0x8) && (instr->Opc3Field() & 0x1)) {
2433 DecodeVCVTBetweenFloatingPointAndInteger(instr);
2434 } else if (((instr->Opc2Field() >> 1) == 0x6) &&
2435 (instr->Opc3Field() & 0x1)) {
2436 DecodeVCVTBetweenFloatingPointAndInteger(instr);
2437 } else if (((instr->Opc2Field() == 0x4) || (instr->Opc2Field() == 0x5)) &&
2438 (instr->Opc3Field() & 0x1)) {
2439 DecodeVCMP(instr);
Steve Block8defd9f2010-07-08 12:39:36 +01002440 } else if (((instr->Opc2Field() == 0x1)) && (instr->Opc3Field() == 0x3)) {
2441 // vsqrt
2442 double dm_value = get_double_from_d_register(vm);
2443 double dd_value = sqrt(dm_value);
2444 set_d_register_from_double(vd, dd_value);
Ben Murdoch3bec4d22010-07-22 14:51:16 +01002445 } else if (instr->Opc3Field() == 0x0) {
2446 // vmov immediate.
2447 if (instr->SzField() == 0x1) {
2448 set_d_register_from_double(vd, instr->DoubleImmedVmov());
2449 } else {
2450 UNREACHABLE(); // Not used by v8.
2451 }
Steve Block6ded16b2010-05-10 14:33:55 +01002452 } else {
2453 UNREACHABLE(); // Not used by V8.
2454 }
2455 } else if (instr->Opc1Field() == 0x3) {
2456 if (instr->SzField() != 0x1) {
2457 UNREACHABLE(); // Not used by V8.
2458 }
2459
2460 if (instr->Opc3Field() & 0x1) {
2461 // vsub
2462 double dn_value = get_double_from_d_register(vn);
2463 double dm_value = get_double_from_d_register(vm);
2464 double dd_value = dn_value - dm_value;
2465 set_d_register_from_double(vd, dd_value);
2466 } else {
2467 // vadd
2468 double dn_value = get_double_from_d_register(vn);
2469 double dm_value = get_double_from_d_register(vm);
2470 double dd_value = dn_value + dm_value;
2471 set_d_register_from_double(vd, dd_value);
2472 }
2473 } else if ((instr->Opc1Field() == 0x2) && !(instr->Opc3Field() & 0x1)) {
2474 // vmul
2475 if (instr->SzField() != 0x1) {
2476 UNREACHABLE(); // Not used by V8.
2477 }
2478
2479 double dn_value = get_double_from_d_register(vn);
2480 double dm_value = get_double_from_d_register(vm);
2481 double dd_value = dn_value * dm_value;
2482 set_d_register_from_double(vd, dd_value);
2483 } else if ((instr->Opc1Field() == 0x4) && !(instr->Opc3Field() & 0x1)) {
2484 // vdiv
2485 if (instr->SzField() != 0x1) {
2486 UNREACHABLE(); // Not used by V8.
2487 }
2488
Steve Blockd0582a62009-12-15 09:54:21 +00002489 double dn_value = get_double_from_d_register(vn);
2490 double dm_value = get_double_from_d_register(vm);
2491 double dd_value = dn_value / dm_value;
2492 set_d_register_from_double(vd, dd_value);
Steve Block6ded16b2010-05-10 14:33:55 +01002493 } else {
2494 UNIMPLEMENTED(); // Not used by V8.
2495 }
2496 } else {
2497 if ((instr->VCField() == 0x0) &&
2498 (instr->VAField() == 0x0)) {
2499 DecodeVMOVBetweenCoreAndSinglePrecisionRegisters(instr);
2500 } else if ((instr->VLField() == 0x1) &&
2501 (instr->VCField() == 0x0) &&
2502 (instr->VAField() == 0x7) &&
2503 (instr->Bits(19, 16) == 0x1)) {
2504 // vmrs
Russell Brenner90bac252010-11-18 13:33:46 -08002505 uint32_t rt = instr->RtField();
2506 if (rt == 0xF) {
Steve Blockd0582a62009-12-15 09:54:21 +00002507 Copy_FPSCR_to_APSR();
Russell Brenner90bac252010-11-18 13:33:46 -08002508 } else {
2509 // Emulate FPSCR from the Simulator flags.
2510 uint32_t fpscr = (n_flag_FPSCR_ << 31) |
2511 (z_flag_FPSCR_ << 30) |
2512 (c_flag_FPSCR_ << 29) |
2513 (v_flag_FPSCR_ << 28) |
2514 (inexact_vfp_flag_ << 4) |
2515 (underflow_vfp_flag_ << 3) |
2516 (overflow_vfp_flag_ << 2) |
2517 (div_zero_vfp_flag_ << 1) |
2518 (inv_op_vfp_flag_ << 0) |
2519 (FPSCR_rounding_mode_ << 22);
2520 set_register(rt, fpscr);
2521 }
2522 } else if ((instr->VLField() == 0x0) &&
2523 (instr->VCField() == 0x0) &&
2524 (instr->VAField() == 0x7) &&
2525 (instr->Bits(19, 16) == 0x1)) {
2526 // vmsr
2527 uint32_t rt = instr->RtField();
2528 if (rt == pc) {
2529 UNREACHABLE();
2530 } else {
2531 uint32_t rt_value = get_register(rt);
2532 n_flag_FPSCR_ = (rt_value >> 31) & 1;
2533 z_flag_FPSCR_ = (rt_value >> 30) & 1;
2534 c_flag_FPSCR_ = (rt_value >> 29) & 1;
2535 v_flag_FPSCR_ = (rt_value >> 28) & 1;
2536 inexact_vfp_flag_ = (rt_value >> 4) & 1;
2537 underflow_vfp_flag_ = (rt_value >> 3) & 1;
2538 overflow_vfp_flag_ = (rt_value >> 2) & 1;
2539 div_zero_vfp_flag_ = (rt_value >> 1) & 1;
2540 inv_op_vfp_flag_ = (rt_value >> 0) & 1;
2541 FPSCR_rounding_mode_ =
2542 static_cast<FPSCRRoundingModes>((rt_value >> 22) & 3);
2543 }
Steve Blockd0582a62009-12-15 09:54:21 +00002544 } else {
2545 UNIMPLEMENTED(); // Not used by V8.
2546 }
Steve Block6ded16b2010-05-10 14:33:55 +01002547 }
2548}
2549
2550
2551void Simulator::DecodeVMOVBetweenCoreAndSinglePrecisionRegisters(Instr* instr) {
2552 ASSERT((instr->Bit(4) == 1) && (instr->VCField() == 0x0) &&
2553 (instr->VAField() == 0x0));
2554
2555 int t = instr->RtField();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002556 int n = instr->VFPNRegCode(kSinglePrecision);
Steve Block6ded16b2010-05-10 14:33:55 +01002557 bool to_arm_register = (instr->VLField() == 0x1);
2558
2559 if (to_arm_register) {
2560 int32_t int_value = get_sinteger_from_s_register(n);
2561 set_register(t, int_value);
2562 } else {
2563 int32_t rs_val = get_register(t);
2564 set_s_register_from_sinteger(n, rs_val);
2565 }
2566}
2567
2568
2569void Simulator::DecodeVCMP(Instr* instr) {
2570 ASSERT((instr->Bit(4) == 0) && (instr->Opc1Field() == 0x7));
2571 ASSERT(((instr->Opc2Field() == 0x4) || (instr->Opc2Field() == 0x5)) &&
2572 (instr->Opc3Field() & 0x1));
Steve Block6ded16b2010-05-10 14:33:55 +01002573 // Comparison.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002574
2575 VFPRegPrecision precision = kSinglePrecision;
2576 if (instr->SzField() == 1) {
2577 precision = kDoublePrecision;
2578 }
Steve Block6ded16b2010-05-10 14:33:55 +01002579
2580 if (instr->Bit(7) != 0) {
2581 // Raising exceptions for quiet NaNs are not supported.
2582 UNIMPLEMENTED(); // Not used by V8.
2583 }
2584
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002585 int d = instr->VFPDRegCode(precision);
Iain Merrick75681382010-08-19 15:07:18 +01002586 int m = 0;
2587 if (instr->Opc2Field() == 0x4) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002588 m = instr->VFPMRegCode(precision);
Iain Merrick75681382010-08-19 15:07:18 +01002589 }
Steve Block6ded16b2010-05-10 14:33:55 +01002590
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002591 if (precision == kDoublePrecision) {
Steve Block6ded16b2010-05-10 14:33:55 +01002592 double dd_value = get_double_from_d_register(d);
Iain Merrick75681382010-08-19 15:07:18 +01002593 double dm_value = 0.0;
2594 if (instr->Opc2Field() == 0x4) {
2595 dm_value = get_double_from_d_register(m);
2596 }
Steve Block6ded16b2010-05-10 14:33:55 +01002597
2598 Compute_FPSCR_Flags(dd_value, dm_value);
2599 } else {
2600 UNIMPLEMENTED(); // Not used by V8.
2601 }
2602}
2603
2604
2605void Simulator::DecodeVCVTBetweenDoubleAndSingle(Instr* instr) {
2606 ASSERT((instr->Bit(4) == 0) && (instr->Opc1Field() == 0x7));
2607 ASSERT((instr->Opc2Field() == 0x7) && (instr->Opc3Field() == 0x3));
2608
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002609 VFPRegPrecision dst_precision = kDoublePrecision;
2610 VFPRegPrecision src_precision = kSinglePrecision;
2611 if (instr->SzField() == 1) {
2612 dst_precision = kSinglePrecision;
2613 src_precision = kDoublePrecision;
2614 }
Steve Block6ded16b2010-05-10 14:33:55 +01002615
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002616 int dst = instr->VFPDRegCode(dst_precision);
2617 int src = instr->VFPMRegCode(src_precision);
2618
2619 if (dst_precision == kSinglePrecision) {
Steve Block6ded16b2010-05-10 14:33:55 +01002620 double val = get_double_from_d_register(src);
2621 set_s_register_from_float(dst, static_cast<float>(val));
2622 } else {
2623 float val = get_float_from_s_register(src);
2624 set_d_register_from_double(dst, static_cast<double>(val));
2625 }
2626}
2627
2628
2629void Simulator::DecodeVCVTBetweenFloatingPointAndInteger(Instr* instr) {
2630 ASSERT((instr->Bit(4) == 0) && (instr->Opc1Field() == 0x7));
2631 ASSERT(((instr->Opc2Field() == 0x8) && (instr->Opc3Field() & 0x1)) ||
2632 (((instr->Opc2Field() >> 1) == 0x6) && (instr->Opc3Field() & 0x1)));
2633
2634 // Conversion between floating-point and integer.
Steve Block6ded16b2010-05-10 14:33:55 +01002635 bool to_integer = (instr->Bit(18) == 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002636
2637 VFPRegPrecision src_precision = kSinglePrecision;
2638 if (instr->SzField() == 1) {
2639 src_precision = kDoublePrecision;
2640 }
2641
Steve Block6ded16b2010-05-10 14:33:55 +01002642 if (to_integer) {
2643 bool unsigned_integer = (instr->Bit(16) == 0);
Russell Brenner90bac252010-11-18 13:33:46 -08002644 FPSCRRoundingModes mode;
Steve Block6ded16b2010-05-10 14:33:55 +01002645 if (instr->Bit(7) != 1) {
Russell Brenner90bac252010-11-18 13:33:46 -08002646 // Use FPSCR defined rounding mode.
2647 mode = FPSCR_rounding_mode_;
2648 // Only RZ and RM modes are supported.
2649 ASSERT((mode == RM) || (mode == RZ));
2650 } else {
2651 // VFP uses round towards zero by default.
2652 mode = RZ;
Steve Blockd0582a62009-12-15 09:54:21 +00002653 }
Steve Block6ded16b2010-05-10 14:33:55 +01002654
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002655 int dst = instr->VFPDRegCode(kSinglePrecision);
2656 int src = instr->VFPMRegCode(src_precision);
Russell Brenner90bac252010-11-18 13:33:46 -08002657 int32_t kMaxInt = v8::internal::kMaxInt;
2658 int32_t kMinInt = v8::internal::kMinInt;
2659 switch (mode) {
2660 case RM:
2661 if (src_precision == kDoublePrecision) {
2662 double val = get_double_from_d_register(src);
Steve Block6ded16b2010-05-10 14:33:55 +01002663
Russell Brenner90bac252010-11-18 13:33:46 -08002664 inv_op_vfp_flag_ = (val > kMaxInt) || (val < kMinInt) || (val != val);
Steve Block6ded16b2010-05-10 14:33:55 +01002665
Russell Brenner90bac252010-11-18 13:33:46 -08002666 int sint = unsigned_integer ? static_cast<uint32_t>(val) :
2667 static_cast<int32_t>(val);
2668 sint = sint > val ? sint - 1 : sint;
Steve Block6ded16b2010-05-10 14:33:55 +01002669
Russell Brenner90bac252010-11-18 13:33:46 -08002670 set_s_register_from_sinteger(dst, sint);
2671 } else {
2672 float val = get_float_from_s_register(src);
Steve Block6ded16b2010-05-10 14:33:55 +01002673
Russell Brenner90bac252010-11-18 13:33:46 -08002674 inv_op_vfp_flag_ = (val > kMaxInt) || (val < kMinInt) || (val != val);
Steve Block6ded16b2010-05-10 14:33:55 +01002675
Russell Brenner90bac252010-11-18 13:33:46 -08002676 int sint = unsigned_integer ? static_cast<uint32_t>(val) :
2677 static_cast<int32_t>(val);
2678 sint = sint > val ? sint - 1 : sint;
2679
2680 set_s_register_from_sinteger(dst, sint);
2681 }
2682 break;
2683 case RZ:
2684 if (src_precision == kDoublePrecision) {
2685 double val = get_double_from_d_register(src);
2686
2687 inv_op_vfp_flag_ = (val > kMaxInt) || (val < kMinInt) || (val != val);
2688
2689 int sint = unsigned_integer ? static_cast<uint32_t>(val) :
2690 static_cast<int32_t>(val);
2691
2692 set_s_register_from_sinteger(dst, sint);
2693 } else {
2694 float val = get_float_from_s_register(src);
2695
2696 inv_op_vfp_flag_ = (val > kMaxInt) || (val < kMinInt) || (val != val);
2697
2698 int sint = unsigned_integer ? static_cast<uint32_t>(val) :
2699 static_cast<int32_t>(val);
2700
2701 set_s_register_from_sinteger(dst, sint);
2702 }
2703 break;
2704
2705 default:
2706 UNREACHABLE();
Steve Block6ded16b2010-05-10 14:33:55 +01002707 }
Russell Brenner90bac252010-11-18 13:33:46 -08002708
Steve Block6ded16b2010-05-10 14:33:55 +01002709 } else {
2710 bool unsigned_integer = (instr->Bit(7) == 0);
2711
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002712 int dst = instr->VFPDRegCode(src_precision);
2713 int src = instr->VFPMRegCode(kSinglePrecision);
Steve Block6ded16b2010-05-10 14:33:55 +01002714
2715 int val = get_sinteger_from_s_register(src);
2716
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002717 if (src_precision == kDoublePrecision) {
Steve Block6ded16b2010-05-10 14:33:55 +01002718 if (unsigned_integer) {
2719 set_d_register_from_double(dst,
2720 static_cast<double>((uint32_t)val));
2721 } else {
2722 set_d_register_from_double(dst, static_cast<double>(val));
2723 }
2724 } else {
2725 if (unsigned_integer) {
2726 set_s_register_from_float(dst,
2727 static_cast<float>((uint32_t)val));
2728 } else {
2729 set_s_register_from_float(dst, static_cast<float>(val));
2730 }
Steve Blockd0582a62009-12-15 09:54:21 +00002731 }
2732 }
2733}
2734
2735
2736// void Simulator::DecodeType6CoprocessorIns(Instr* instr)
2737// Decode Type 6 coprocessor instructions.
Leon Clarkee46be812010-01-19 14:06:41 +00002738// Dm = vmov(Rt, Rt2)
2739// <Rt, Rt2> = vmov(Dm)
Leon Clarked91b9f72010-01-27 17:25:45 +00002740// Ddst = MEM(Rbase + 4*offset).
2741// MEM(Rbase + 4*offset) = Dsrc.
Steve Blockd0582a62009-12-15 09:54:21 +00002742void Simulator::DecodeType6CoprocessorIns(Instr* instr) {
2743 ASSERT((instr->TypeField() == 6));
2744
Steve Block6ded16b2010-05-10 14:33:55 +01002745 if (instr->CoprocessorField() == 0xA) {
2746 switch (instr->OpcodeField()) {
2747 case 0x8:
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002748 case 0xA:
2749 case 0xC:
2750 case 0xE: { // Load and store single precision float to memory.
Steve Block6ded16b2010-05-10 14:33:55 +01002751 int rn = instr->RnField();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002752 int vd = instr->VFPDRegCode(kSinglePrecision);
Steve Block6ded16b2010-05-10 14:33:55 +01002753 int offset = instr->Immed8Field();
2754 if (!instr->HasU()) {
2755 offset = -offset;
2756 }
2757
2758 int32_t address = get_register(rn) + 4 * offset;
2759 if (instr->HasL()) {
2760 // Load double from memory: vldr.
2761 set_s_register_from_sinteger(vd, ReadW(address, instr));
2762 } else {
2763 // Store double to memory: vstr.
2764 WriteW(address, get_sinteger_from_s_register(vd), instr);
2765 }
2766 break;
2767 }
2768 default:
2769 UNIMPLEMENTED(); // Not used by V8.
2770 break;
2771 }
2772 } else if (instr->CoprocessorField() == 0xB) {
Leon Clarked91b9f72010-01-27 17:25:45 +00002773 switch (instr->OpcodeField()) {
2774 case 0x2:
2775 // Load and store double to two GP registers
2776 if (instr->Bits(7, 4) != 0x1) {
2777 UNIMPLEMENTED(); // Not used by V8.
2778 } else {
2779 int rt = instr->RtField();
2780 int rn = instr->RnField();
2781 int vm = instr->VmField();
2782 if (instr->HasL()) {
2783 int32_t rt_int_value = get_sinteger_from_s_register(2*vm);
2784 int32_t rn_int_value = get_sinteger_from_s_register(2*vm+1);
2785
2786 set_register(rt, rt_int_value);
2787 set_register(rn, rn_int_value);
2788 } else {
2789 int32_t rs_val = get_register(rt);
2790 int32_t rn_val = get_register(rn);
2791
2792 set_s_register_from_sinteger(2*vm, rs_val);
2793 set_s_register_from_sinteger((2*vm+1), rn_val);
2794 }
2795 }
2796 break;
2797 case 0x8:
2798 case 0xC: { // Load and store double to memory.
2799 int rn = instr->RnField();
2800 int vd = instr->VdField();
2801 int offset = instr->Immed8Field();
2802 if (!instr->HasU()) {
2803 offset = -offset;
2804 }
2805 int32_t address = get_register(rn) + 4 * offset;
2806 if (instr->HasL()) {
2807 // Load double from memory: vldr.
2808 set_s_register_from_sinteger(2*vd, ReadW(address, instr));
2809 set_s_register_from_sinteger(2*vd + 1, ReadW(address + 4, instr));
2810 } else {
2811 // Store double to memory: vstr.
2812 WriteW(address, get_sinteger_from_s_register(2*vd), instr);
2813 WriteW(address + 4, get_sinteger_from_s_register(2*vd + 1), instr);
2814 }
2815 break;
2816 }
2817 default:
2818 UNIMPLEMENTED(); // Not used by V8.
2819 break;
2820 }
Steve Block6ded16b2010-05-10 14:33:55 +01002821 } else {
2822 UNIMPLEMENTED(); // Not used by V8.
Steve Blockd0582a62009-12-15 09:54:21 +00002823 }
2824}
2825
2826
Steve Blocka7e24c12009-10-30 11:49:00 +00002827// Executes the current instruction.
2828void Simulator::InstructionDecode(Instr* instr) {
Steve Block6ded16b2010-05-10 14:33:55 +01002829 if (v8::internal::FLAG_check_icache) {
2830 CheckICache(instr);
2831 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002832 pc_modified_ = false;
2833 if (::v8::internal::FLAG_trace_sim) {
2834 disasm::NameConverter converter;
2835 disasm::Disassembler dasm(converter);
2836 // use a reasonably large buffer
2837 v8::internal::EmbeddedVector<char, 256> buffer;
2838 dasm.InstructionDecode(buffer,
2839 reinterpret_cast<byte*>(instr));
Ben Murdochf87a2032010-10-22 12:50:53 +01002840 PrintF(" 0x%08x %s\n", reinterpret_cast<intptr_t>(instr), buffer.start());
Steve Blocka7e24c12009-10-30 11:49:00 +00002841 }
2842 if (instr->ConditionField() == special_condition) {
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08002843 UNIMPLEMENTED();
Steve Blocka7e24c12009-10-30 11:49:00 +00002844 } else if (ConditionallyExecute(instr)) {
2845 switch (instr->TypeField()) {
2846 case 0:
2847 case 1: {
2848 DecodeType01(instr);
2849 break;
2850 }
2851 case 2: {
2852 DecodeType2(instr);
2853 break;
2854 }
2855 case 3: {
2856 DecodeType3(instr);
2857 break;
2858 }
2859 case 4: {
2860 DecodeType4(instr);
2861 break;
2862 }
2863 case 5: {
2864 DecodeType5(instr);
2865 break;
2866 }
2867 case 6: {
2868 DecodeType6(instr);
2869 break;
2870 }
2871 case 7: {
2872 DecodeType7(instr);
2873 break;
2874 }
2875 default: {
2876 UNIMPLEMENTED();
2877 break;
2878 }
2879 }
2880 }
2881 if (!pc_modified_) {
2882 set_register(pc, reinterpret_cast<int32_t>(instr) + Instr::kInstrSize);
2883 }
2884}
2885
2886
Steve Blocka7e24c12009-10-30 11:49:00 +00002887void Simulator::Execute() {
2888 // Get the PC to simulate. Cannot use the accessor here as we need the
2889 // raw PC value and not the one used as input to arithmetic instructions.
2890 int program_counter = get_pc();
2891
2892 if (::v8::internal::FLAG_stop_sim_at == 0) {
2893 // Fast version of the dispatch loop without checking whether the simulator
2894 // should be stopping at a particular executed instruction.
2895 while (program_counter != end_sim_pc) {
2896 Instr* instr = reinterpret_cast<Instr*>(program_counter);
2897 icount_++;
2898 InstructionDecode(instr);
2899 program_counter = get_pc();
2900 }
2901 } else {
2902 // FLAG_stop_sim_at is at the non-default value. Stop in the debugger when
2903 // we reach the particular instuction count.
2904 while (program_counter != end_sim_pc) {
2905 Instr* instr = reinterpret_cast<Instr*>(program_counter);
2906 icount_++;
2907 if (icount_ == ::v8::internal::FLAG_stop_sim_at) {
2908 Debugger dbg(this);
2909 dbg.Debug();
2910 } else {
2911 InstructionDecode(instr);
2912 }
2913 program_counter = get_pc();
2914 }
2915 }
2916}
2917
2918
2919int32_t Simulator::Call(byte* entry, int argument_count, ...) {
2920 va_list parameters;
2921 va_start(parameters, argument_count);
2922 // Setup arguments
2923
2924 // First four arguments passed in registers.
2925 ASSERT(argument_count >= 4);
2926 set_register(r0, va_arg(parameters, int32_t));
2927 set_register(r1, va_arg(parameters, int32_t));
2928 set_register(r2, va_arg(parameters, int32_t));
2929 set_register(r3, va_arg(parameters, int32_t));
2930
2931 // Remaining arguments passed on stack.
2932 int original_stack = get_register(sp);
2933 // Compute position of stack on entry to generated code.
2934 int entry_stack = (original_stack - (argument_count - 4) * sizeof(int32_t));
2935 if (OS::ActivationFrameAlignment() != 0) {
2936 entry_stack &= -OS::ActivationFrameAlignment();
2937 }
2938 // Store remaining arguments on stack, from low to high memory.
2939 intptr_t* stack_argument = reinterpret_cast<intptr_t*>(entry_stack);
2940 for (int i = 4; i < argument_count; i++) {
2941 stack_argument[i - 4] = va_arg(parameters, int32_t);
2942 }
2943 va_end(parameters);
2944 set_register(sp, entry_stack);
2945
2946 // Prepare to execute the code at entry
2947 set_register(pc, reinterpret_cast<int32_t>(entry));
2948 // Put down marker for end of simulation. The simulator will stop simulation
2949 // when the PC reaches this value. By saving the "end simulation" value into
2950 // the LR the simulation stops when returning to this call point.
2951 set_register(lr, end_sim_pc);
2952
2953 // Remember the values of callee-saved registers.
2954 // The code below assumes that r9 is not used as sb (static base) in
2955 // simulator code and therefore is regarded as a callee-saved register.
2956 int32_t r4_val = get_register(r4);
2957 int32_t r5_val = get_register(r5);
2958 int32_t r6_val = get_register(r6);
2959 int32_t r7_val = get_register(r7);
2960 int32_t r8_val = get_register(r8);
2961 int32_t r9_val = get_register(r9);
2962 int32_t r10_val = get_register(r10);
2963 int32_t r11_val = get_register(r11);
2964
2965 // Setup the callee-saved registers with a known value. To be able to check
2966 // that they are preserved properly across JS execution.
2967 int32_t callee_saved_value = icount_;
2968 set_register(r4, callee_saved_value);
2969 set_register(r5, callee_saved_value);
2970 set_register(r6, callee_saved_value);
2971 set_register(r7, callee_saved_value);
2972 set_register(r8, callee_saved_value);
2973 set_register(r9, callee_saved_value);
2974 set_register(r10, callee_saved_value);
2975 set_register(r11, callee_saved_value);
2976
2977 // Start the simulation
2978 Execute();
2979
2980 // Check that the callee-saved registers have been preserved.
2981 CHECK_EQ(callee_saved_value, get_register(r4));
2982 CHECK_EQ(callee_saved_value, get_register(r5));
2983 CHECK_EQ(callee_saved_value, get_register(r6));
2984 CHECK_EQ(callee_saved_value, get_register(r7));
2985 CHECK_EQ(callee_saved_value, get_register(r8));
2986 CHECK_EQ(callee_saved_value, get_register(r9));
2987 CHECK_EQ(callee_saved_value, get_register(r10));
2988 CHECK_EQ(callee_saved_value, get_register(r11));
2989
2990 // Restore callee-saved registers with the original value.
2991 set_register(r4, r4_val);
2992 set_register(r5, r5_val);
2993 set_register(r6, r6_val);
2994 set_register(r7, r7_val);
2995 set_register(r8, r8_val);
2996 set_register(r9, r9_val);
2997 set_register(r10, r10_val);
2998 set_register(r11, r11_val);
2999
3000 // Pop stack passed arguments.
3001 CHECK_EQ(entry_stack, get_register(sp));
3002 set_register(sp, original_stack);
3003
3004 int32_t result = get_register(r0);
3005 return result;
3006}
3007
Steve Blockd0582a62009-12-15 09:54:21 +00003008
3009uintptr_t Simulator::PushAddress(uintptr_t address) {
3010 int new_sp = get_register(sp) - sizeof(uintptr_t);
3011 uintptr_t* stack_slot = reinterpret_cast<uintptr_t*>(new_sp);
3012 *stack_slot = address;
3013 set_register(sp, new_sp);
3014 return new_sp;
3015}
3016
3017
3018uintptr_t Simulator::PopAddress() {
3019 int current_sp = get_register(sp);
3020 uintptr_t* stack_slot = reinterpret_cast<uintptr_t*>(current_sp);
3021 uintptr_t address = *stack_slot;
3022 set_register(sp, current_sp + sizeof(uintptr_t));
3023 return address;
3024}
3025
Steve Blocka7e24c12009-10-30 11:49:00 +00003026} } // namespace assembler::arm
3027
John Reck59135872010-11-02 12:39:01 -07003028#endif // USE_SIMULATOR
Leon Clarkef7060e22010-06-03 12:02:55 +01003029
3030#endif // V8_TARGET_ARCH_ARM