blob: 6e193885b046f5801e87ad843af0474a02fd03c3 [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// Copyright 2012 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Steve Blocka7e24c12009-10-30 11:49:00 +00004
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005#include <stdarg.h>
Steve Blocka7e24c12009-10-30 11:49:00 +00006#include <stdlib.h>
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007#include <cmath>
Steve Blocka7e24c12009-10-30 11:49:00 +00008
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009#if V8_TARGET_ARCH_ARM
10
11#include "src/arm/constants-arm.h"
12#include "src/arm/simulator-arm.h"
13#include "src/assembler.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000014#include "src/base/bits.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015#include "src/codegen.h"
16#include "src/disasm.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000017
John Reck59135872010-11-02 12:39:01 -070018#if defined(USE_SIMULATOR)
Steve Blocka7e24c12009-10-30 11:49:00 +000019
20// Only build the simulator if not compiling for real ARM hardware.
Steve Block1e0659c2011-05-24 12:43:12 +010021namespace v8 {
22namespace internal {
Steve Blocka7e24c12009-10-30 11:49:00 +000023
24// This macro provides a platform independent use of sscanf. The reason for
Leon Clarked91b9f72010-01-27 17:25:45 +000025// SScanF not being implemented in a platform independent way through
26// ::v8::internal::OS in the same way as SNPrintF is that the
27// Windows C Run-Time Library does not provide vsscanf.
Steve Blocka7e24c12009-10-30 11:49:00 +000028#define SScanF sscanf // NOLINT
29
Steve Block44f0eee2011-05-26 01:26:41 +010030// The ArmDebugger class is used by the simulator while debugging simulated ARM
Steve Blocka7e24c12009-10-30 11:49:00 +000031// code.
Steve Block44f0eee2011-05-26 01:26:41 +010032class ArmDebugger {
Steve Blocka7e24c12009-10-30 11:49:00 +000033 public:
Ben Murdoch3ef787d2012-04-12 10:51:47 +010034 explicit ArmDebugger(Simulator* sim) : sim_(sim) { }
Steve Block44f0eee2011-05-26 01:26:41 +010035 ~ArmDebugger();
Steve Blocka7e24c12009-10-30 11:49:00 +000036
Steve Block1e0659c2011-05-24 12:43:12 +010037 void Stop(Instruction* instr);
Steve Blocka7e24c12009-10-30 11:49:00 +000038 void Debug();
39
40 private:
Steve Block1e0659c2011-05-24 12:43:12 +010041 static const Instr kBreakpointInstr =
42 (al | (7*B25) | (1*B24) | kBreakpoint);
43 static const Instr kNopInstr = (al | (13*B21));
Steve Blocka7e24c12009-10-30 11:49:00 +000044
45 Simulator* sim_;
46
47 int32_t GetRegisterValue(int regnum);
Ben Murdoch8b112d22011-06-08 16:22:53 +010048 double GetRegisterPairDoubleValue(int regnum);
Ben Murdochb0fe1622011-05-05 13:52:32 +010049 double GetVFPDoubleRegisterValue(int regnum);
Steve Blocka7e24c12009-10-30 11:49:00 +000050 bool GetValue(const char* desc, int32_t* value);
Steve Block6ded16b2010-05-10 14:33:55 +010051 bool GetVFPSingleValue(const char* desc, float* value);
52 bool GetVFPDoubleValue(const char* desc, double* value);
Steve Blocka7e24c12009-10-30 11:49:00 +000053
54 // Set or delete a breakpoint. Returns true if successful.
Steve Block1e0659c2011-05-24 12:43:12 +010055 bool SetBreakpoint(Instruction* breakpc);
56 bool DeleteBreakpoint(Instruction* breakpc);
Steve Blocka7e24c12009-10-30 11:49:00 +000057
58 // Undo and redo all breakpoints. This is needed to bracket disassembly and
59 // execution to skip past breakpoints when run from the debugger.
60 void UndoBreakpoints();
61 void RedoBreakpoints();
62};
63
64
Steve Block44f0eee2011-05-26 01:26:41 +010065ArmDebugger::~ArmDebugger() {
Steve Blocka7e24c12009-10-30 11:49:00 +000066}
67
68
69
70#ifdef GENERATED_CODE_COVERAGE
71static FILE* coverage_log = NULL;
72
73
74static void InitializeCoverage() {
75 char* file_name = getenv("V8_GENERATED_CODE_COVERAGE_LOG");
76 if (file_name != NULL) {
77 coverage_log = fopen(file_name, "aw+");
78 }
79}
80
81
Steve Block44f0eee2011-05-26 01:26:41 +010082void ArmDebugger::Stop(Instruction* instr) {
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -080083 // Get the stop code.
Steve Block1e0659c2011-05-24 12:43:12 +010084 uint32_t code = instr->SvcValue() & kStopCodeMask;
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -080085 // Retrieve the encoded address, which comes just after this stop.
86 char** msg_address =
Steve Block1e0659c2011-05-24 12:43:12 +010087 reinterpret_cast<char**>(sim_->get_pc() + Instruction::kInstrSize);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -080088 char* msg = *msg_address;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000089 DCHECK(msg != NULL);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -080090
91 // Update this stop description.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000092 if (isWatchedStop(code) && !watched_stops_[code].desc) {
93 watched_stops_[code].desc = msg;
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -080094 }
95
96 if (strlen(msg) > 0) {
Steve Blocka7e24c12009-10-30 11:49:00 +000097 if (coverage_log != NULL) {
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -080098 fprintf(coverage_log, "%s\n", msg);
Steve Blocka7e24c12009-10-30 11:49:00 +000099 fflush(coverage_log);
100 }
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800101 // Overwrite the instruction and address with nops.
102 instr->SetInstructionBits(kNopInstr);
Steve Block1e0659c2011-05-24 12:43:12 +0100103 reinterpret_cast<Instruction*>(msg_address)->SetInstructionBits(kNopInstr);
Steve Blocka7e24c12009-10-30 11:49:00 +0000104 }
Steve Block1e0659c2011-05-24 12:43:12 +0100105 sim_->set_pc(sim_->get_pc() + 2 * Instruction::kInstrSize);
Steve Blocka7e24c12009-10-30 11:49:00 +0000106}
107
108#else // ndef GENERATED_CODE_COVERAGE
109
110static void InitializeCoverage() {
111}
112
113
Steve Block44f0eee2011-05-26 01:26:41 +0100114void ArmDebugger::Stop(Instruction* instr) {
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800115 // Get the stop code.
Steve Block1e0659c2011-05-24 12:43:12 +0100116 uint32_t code = instr->SvcValue() & kStopCodeMask;
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800117 // Retrieve the encoded address, which comes just after this stop.
Steve Block1e0659c2011-05-24 12:43:12 +0100118 char* msg = *reinterpret_cast<char**>(sim_->get_pc()
119 + Instruction::kInstrSize);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800120 // Update this stop description.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000121 if (sim_->isWatchedStop(code) && !sim_->watched_stops_[code].desc) {
122 sim_->watched_stops_[code].desc = msg;
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800123 }
Steve Block1e0659c2011-05-24 12:43:12 +0100124 // Print the stop message and code if it is not the default code.
125 if (code != kMaxStopCode) {
126 PrintF("Simulator hit stop %u: %s\n", code, msg);
127 } else {
128 PrintF("Simulator hit %s\n", msg);
129 }
130 sim_->set_pc(sim_->get_pc() + 2 * Instruction::kInstrSize);
Steve Blocka7e24c12009-10-30 11:49:00 +0000131 Debug();
132}
133#endif
134
135
Steve Block44f0eee2011-05-26 01:26:41 +0100136int32_t ArmDebugger::GetRegisterValue(int regnum) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000137 if (regnum == kPCRegister) {
138 return sim_->get_pc();
139 } else {
140 return sim_->get_register(regnum);
141 }
142}
143
144
Ben Murdoch8b112d22011-06-08 16:22:53 +0100145double ArmDebugger::GetRegisterPairDoubleValue(int regnum) {
146 return sim_->get_double_from_register_pair(regnum);
147}
148
149
Steve Block44f0eee2011-05-26 01:26:41 +0100150double ArmDebugger::GetVFPDoubleRegisterValue(int regnum) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100151 return sim_->get_double_from_d_register(regnum);
152}
153
154
Steve Block44f0eee2011-05-26 01:26:41 +0100155bool ArmDebugger::GetValue(const char* desc, int32_t* value) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000156 int regnum = Registers::Number(desc);
157 if (regnum != kNoRegister) {
158 *value = GetRegisterValue(regnum);
159 return true;
160 } else {
Steve Block6ded16b2010-05-10 14:33:55 +0100161 if (strncmp(desc, "0x", 2) == 0) {
162 return SScanF(desc + 2, "%x", reinterpret_cast<uint32_t*>(value)) == 1;
163 } else {
164 return SScanF(desc, "%u", reinterpret_cast<uint32_t*>(value)) == 1;
165 }
166 }
167 return false;
168}
169
170
Steve Block44f0eee2011-05-26 01:26:41 +0100171bool ArmDebugger::GetVFPSingleValue(const char* desc, float* value) {
Steve Block6ded16b2010-05-10 14:33:55 +0100172 bool is_double;
173 int regnum = VFPRegisters::Number(desc, &is_double);
174 if (regnum != kNoRegister && !is_double) {
175 *value = sim_->get_float_from_s_register(regnum);
176 return true;
177 }
178 return false;
179}
180
181
Steve Block44f0eee2011-05-26 01:26:41 +0100182bool ArmDebugger::GetVFPDoubleValue(const char* desc, double* value) {
Steve Block6ded16b2010-05-10 14:33:55 +0100183 bool is_double;
184 int regnum = VFPRegisters::Number(desc, &is_double);
185 if (regnum != kNoRegister && is_double) {
186 *value = sim_->get_double_from_d_register(regnum);
187 return true;
Steve Blocka7e24c12009-10-30 11:49:00 +0000188 }
189 return false;
190}
191
192
Steve Block44f0eee2011-05-26 01:26:41 +0100193bool ArmDebugger::SetBreakpoint(Instruction* breakpc) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000194 // Check if a breakpoint can be set. If not return without any side-effects.
195 if (sim_->break_pc_ != NULL) {
196 return false;
197 }
198
199 // Set the breakpoint.
200 sim_->break_pc_ = breakpc;
201 sim_->break_instr_ = breakpc->InstructionBits();
202 // Not setting the breakpoint instruction in the code itself. It will be set
203 // when the debugger shell continues.
204 return true;
205}
206
207
Steve Block44f0eee2011-05-26 01:26:41 +0100208bool ArmDebugger::DeleteBreakpoint(Instruction* breakpc) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000209 if (sim_->break_pc_ != NULL) {
210 sim_->break_pc_->SetInstructionBits(sim_->break_instr_);
211 }
212
213 sim_->break_pc_ = NULL;
214 sim_->break_instr_ = 0;
215 return true;
216}
217
218
Steve Block44f0eee2011-05-26 01:26:41 +0100219void ArmDebugger::UndoBreakpoints() {
Steve Blocka7e24c12009-10-30 11:49:00 +0000220 if (sim_->break_pc_ != NULL) {
221 sim_->break_pc_->SetInstructionBits(sim_->break_instr_);
222 }
223}
224
225
Steve Block44f0eee2011-05-26 01:26:41 +0100226void ArmDebugger::RedoBreakpoints() {
Steve Blocka7e24c12009-10-30 11:49:00 +0000227 if (sim_->break_pc_ != NULL) {
228 sim_->break_pc_->SetInstructionBits(kBreakpointInstr);
229 }
230}
231
232
Steve Block44f0eee2011-05-26 01:26:41 +0100233void ArmDebugger::Debug() {
Steve Blocka7e24c12009-10-30 11:49:00 +0000234 intptr_t last_pc = -1;
235 bool done = false;
236
237#define COMMAND_SIZE 63
238#define ARG_SIZE 255
239
240#define STR(a) #a
241#define XSTR(a) STR(a)
242
243 char cmd[COMMAND_SIZE + 1];
244 char arg1[ARG_SIZE + 1];
245 char arg2[ARG_SIZE + 1];
Steve Block6ded16b2010-05-10 14:33:55 +0100246 char* argv[3] = { cmd, arg1, arg2 };
Steve Blocka7e24c12009-10-30 11:49:00 +0000247
248 // make sure to have a proper terminating character if reaching the limit
249 cmd[COMMAND_SIZE] = 0;
250 arg1[ARG_SIZE] = 0;
251 arg2[ARG_SIZE] = 0;
252
253 // Undo all set breakpoints while running in the debugger shell. This will
254 // make them invisible to all commands.
255 UndoBreakpoints();
256
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000257 while (!done && !sim_->has_bad_pc()) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000258 if (last_pc != sim_->get_pc()) {
259 disasm::NameConverter converter;
260 disasm::Disassembler dasm(converter);
261 // use a reasonably large buffer
262 v8::internal::EmbeddedVector<char, 256> buffer;
263 dasm.InstructionDecode(buffer,
264 reinterpret_cast<byte*>(sim_->get_pc()));
265 PrintF(" 0x%08x %s\n", sim_->get_pc(), buffer.start());
266 last_pc = sim_->get_pc();
267 }
268 char* line = ReadLine("sim> ");
269 if (line == NULL) {
270 break;
271 } else {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100272 char* last_input = sim_->last_debugger_input();
273 if (strcmp(line, "\n") == 0 && last_input != NULL) {
274 line = last_input;
275 } else {
276 // Ownership is transferred to sim_;
277 sim_->set_last_debugger_input(line);
278 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000279 // Use sscanf to parse the individual parts of the command line. At the
280 // moment no command expects more than two parameters.
Steve Block6ded16b2010-05-10 14:33:55 +0100281 int argc = SScanF(line,
Steve Blocka7e24c12009-10-30 11:49:00 +0000282 "%" XSTR(COMMAND_SIZE) "s "
283 "%" XSTR(ARG_SIZE) "s "
284 "%" XSTR(ARG_SIZE) "s",
285 cmd, arg1, arg2);
286 if ((strcmp(cmd, "si") == 0) || (strcmp(cmd, "stepi") == 0)) {
Steve Block1e0659c2011-05-24 12:43:12 +0100287 sim_->InstructionDecode(reinterpret_cast<Instruction*>(sim_->get_pc()));
Steve Blocka7e24c12009-10-30 11:49:00 +0000288 } else if ((strcmp(cmd, "c") == 0) || (strcmp(cmd, "cont") == 0)) {
289 // Execute the one instruction we broke at with breakpoints disabled.
Steve Block1e0659c2011-05-24 12:43:12 +0100290 sim_->InstructionDecode(reinterpret_cast<Instruction*>(sim_->get_pc()));
Steve Blocka7e24c12009-10-30 11:49:00 +0000291 // Leave the debugger shell.
292 done = true;
293 } else if ((strcmp(cmd, "p") == 0) || (strcmp(cmd, "print") == 0)) {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100294 if (argc == 2 || (argc == 3 && strcmp(arg2, "fp") == 0)) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000295 int32_t value;
Steve Block6ded16b2010-05-10 14:33:55 +0100296 float svalue;
297 double dvalue;
Steve Blocka7e24c12009-10-30 11:49:00 +0000298 if (strcmp(arg1, "all") == 0) {
299 for (int i = 0; i < kNumRegisters; i++) {
300 value = GetRegisterValue(i);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000301 PrintF("%3s: 0x%08x %10d", Register::from_code(i).ToString(),
302 value, value);
Ben Murdoch8b112d22011-06-08 16:22:53 +0100303 if ((argc == 3 && strcmp(arg2, "fp") == 0) &&
304 i < 8 &&
305 (i % 2) == 0) {
306 dvalue = GetRegisterPairDoubleValue(i);
307 PrintF(" (%f)\n", dvalue);
308 } else {
309 PrintF("\n");
310 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000311 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000312 for (int i = 0; i < DwVfpRegister::NumRegisters(); i++) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100313 dvalue = GetVFPDoubleRegisterValue(i);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000314 uint64_t as_words = bit_cast<uint64_t>(dvalue);
Steve Block44f0eee2011-05-26 01:26:41 +0100315 PrintF("%3s: %f 0x%08x %08x\n",
316 VFPRegisters::Name(i, true),
317 dvalue,
318 static_cast<uint32_t>(as_words >> 32),
319 static_cast<uint32_t>(as_words & 0xffffffff));
Ben Murdochb0fe1622011-05-05 13:52:32 +0100320 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000321 } else {
322 if (GetValue(arg1, &value)) {
323 PrintF("%s: 0x%08x %d \n", arg1, value, value);
Steve Block6ded16b2010-05-10 14:33:55 +0100324 } else if (GetVFPSingleValue(arg1, &svalue)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000325 uint32_t as_word = bit_cast<uint32_t>(svalue);
Steve Block44f0eee2011-05-26 01:26:41 +0100326 PrintF("%s: %f 0x%08x\n", arg1, svalue, as_word);
Steve Block6ded16b2010-05-10 14:33:55 +0100327 } else if (GetVFPDoubleValue(arg1, &dvalue)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000328 uint64_t as_words = bit_cast<uint64_t>(dvalue);
Steve Block44f0eee2011-05-26 01:26:41 +0100329 PrintF("%s: %f 0x%08x %08x\n",
330 arg1,
331 dvalue,
332 static_cast<uint32_t>(as_words >> 32),
333 static_cast<uint32_t>(as_words & 0xffffffff));
Steve Blocka7e24c12009-10-30 11:49:00 +0000334 } else {
335 PrintF("%s unrecognized\n", arg1);
336 }
337 }
338 } else {
339 PrintF("print <register>\n");
340 }
341 } else if ((strcmp(cmd, "po") == 0)
342 || (strcmp(cmd, "printobject") == 0)) {
Steve Block6ded16b2010-05-10 14:33:55 +0100343 if (argc == 2) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000344 int32_t value;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000345 OFStream os(stdout);
Steve Blocka7e24c12009-10-30 11:49:00 +0000346 if (GetValue(arg1, &value)) {
347 Object* obj = reinterpret_cast<Object*>(value);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000348 os << arg1 << ": \n";
Steve Blocka7e24c12009-10-30 11:49:00 +0000349#ifdef DEBUG
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000350 obj->Print(os);
351 os << "\n";
Steve Blocka7e24c12009-10-30 11:49:00 +0000352#else
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000353 os << Brief(obj) << "\n";
Steve Blocka7e24c12009-10-30 11:49:00 +0000354#endif
355 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000356 os << arg1 << " unrecognized\n";
Steve Blocka7e24c12009-10-30 11:49:00 +0000357 }
358 } else {
359 PrintF("printobject <value>\n");
360 }
Steve Block6ded16b2010-05-10 14:33:55 +0100361 } else if (strcmp(cmd, "stack") == 0 || strcmp(cmd, "mem") == 0) {
362 int32_t* cur = NULL;
363 int32_t* end = NULL;
364 int next_arg = 1;
365
366 if (strcmp(cmd, "stack") == 0) {
367 cur = reinterpret_cast<int32_t*>(sim_->get_register(Simulator::sp));
368 } else { // "mem"
369 int32_t value;
370 if (!GetValue(arg1, &value)) {
371 PrintF("%s unrecognized\n", arg1);
372 continue;
373 }
374 cur = reinterpret_cast<int32_t*>(value);
375 next_arg++;
376 }
377
378 int32_t words;
379 if (argc == next_arg) {
380 words = 10;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000381 } else {
Steve Block6ded16b2010-05-10 14:33:55 +0100382 if (!GetValue(argv[next_arg], &words)) {
383 words = 10;
384 }
385 }
386 end = cur + words;
387
388 while (cur < end) {
Steve Block44f0eee2011-05-26 01:26:41 +0100389 PrintF(" 0x%08x: 0x%08x %10d",
Ben Murdochf87a2032010-10-22 12:50:53 +0100390 reinterpret_cast<intptr_t>(cur), *cur, *cur);
Steve Block44f0eee2011-05-26 01:26:41 +0100391 HeapObject* obj = reinterpret_cast<HeapObject*>(*cur);
392 int value = *cur;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000393 Heap* current_heap = sim_->isolate_->heap();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000394 if (((value & 1) == 0) || current_heap->Contains(obj)) {
Steve Block44f0eee2011-05-26 01:26:41 +0100395 PrintF(" (");
396 if ((value & 1) == 0) {
397 PrintF("smi %d", value / 2);
398 } else {
399 obj->ShortPrint();
400 }
401 PrintF(")");
402 }
403 PrintF("\n");
Steve Block6ded16b2010-05-10 14:33:55 +0100404 cur++;
405 }
Steve Block44f0eee2011-05-26 01:26:41 +0100406 } else if (strcmp(cmd, "disasm") == 0 || strcmp(cmd, "di") == 0) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000407 disasm::NameConverter converter;
408 disasm::Disassembler dasm(converter);
409 // use a reasonably large buffer
410 v8::internal::EmbeddedVector<char, 256> buffer;
411
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800412 byte* prev = NULL;
Steve Blocka7e24c12009-10-30 11:49:00 +0000413 byte* cur = NULL;
414 byte* end = NULL;
415
Steve Block6ded16b2010-05-10 14:33:55 +0100416 if (argc == 1) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000417 cur = reinterpret_cast<byte*>(sim_->get_pc());
Steve Block1e0659c2011-05-24 12:43:12 +0100418 end = cur + (10 * Instruction::kInstrSize);
Steve Block6ded16b2010-05-10 14:33:55 +0100419 } else if (argc == 2) {
Steve Block44f0eee2011-05-26 01:26:41 +0100420 int regnum = Registers::Number(arg1);
421 if (regnum != kNoRegister || strncmp(arg1, "0x", 2) == 0) {
422 // The argument is an address or a register name.
423 int32_t value;
424 if (GetValue(arg1, &value)) {
425 cur = reinterpret_cast<byte*>(value);
426 // Disassemble 10 instructions at <arg1>.
427 end = cur + (10 * Instruction::kInstrSize);
428 }
429 } else {
430 // The argument is the number of instructions.
431 int32_t value;
432 if (GetValue(arg1, &value)) {
433 cur = reinterpret_cast<byte*>(sim_->get_pc());
434 // Disassemble <arg1> instructions.
435 end = cur + (value * Instruction::kInstrSize);
436 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000437 }
438 } else {
439 int32_t value1;
440 int32_t value2;
441 if (GetValue(arg1, &value1) && GetValue(arg2, &value2)) {
442 cur = reinterpret_cast<byte*>(value1);
Steve Block1e0659c2011-05-24 12:43:12 +0100443 end = cur + (value2 * Instruction::kInstrSize);
Steve Blocka7e24c12009-10-30 11:49:00 +0000444 }
445 }
446
447 while (cur < end) {
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800448 prev = cur;
449 cur += dasm.InstructionDecode(buffer, cur);
Ben Murdochf87a2032010-10-22 12:50:53 +0100450 PrintF(" 0x%08x %s\n",
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800451 reinterpret_cast<intptr_t>(prev), buffer.start());
Steve Blocka7e24c12009-10-30 11:49:00 +0000452 }
453 } else if (strcmp(cmd, "gdb") == 0) {
454 PrintF("relinquishing control to gdb\n");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000455 v8::base::OS::DebugBreak();
Steve Blocka7e24c12009-10-30 11:49:00 +0000456 PrintF("regaining control from gdb\n");
457 } else if (strcmp(cmd, "break") == 0) {
Steve Block6ded16b2010-05-10 14:33:55 +0100458 if (argc == 2) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000459 int32_t value;
460 if (GetValue(arg1, &value)) {
Steve Block1e0659c2011-05-24 12:43:12 +0100461 if (!SetBreakpoint(reinterpret_cast<Instruction*>(value))) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000462 PrintF("setting breakpoint failed\n");
463 }
464 } else {
465 PrintF("%s unrecognized\n", arg1);
466 }
467 } else {
468 PrintF("break <address>\n");
469 }
470 } else if (strcmp(cmd, "del") == 0) {
471 if (!DeleteBreakpoint(NULL)) {
472 PrintF("deleting breakpoint failed\n");
473 }
474 } else if (strcmp(cmd, "flags") == 0) {
475 PrintF("N flag: %d; ", sim_->n_flag_);
476 PrintF("Z flag: %d; ", sim_->z_flag_);
477 PrintF("C flag: %d; ", sim_->c_flag_);
478 PrintF("V flag: %d\n", sim_->v_flag_);
Steve Blockd0582a62009-12-15 09:54:21 +0000479 PrintF("INVALID OP flag: %d; ", sim_->inv_op_vfp_flag_);
480 PrintF("DIV BY ZERO flag: %d; ", sim_->div_zero_vfp_flag_);
481 PrintF("OVERFLOW flag: %d; ", sim_->overflow_vfp_flag_);
482 PrintF("UNDERFLOW flag: %d; ", sim_->underflow_vfp_flag_);
Steve Block1e0659c2011-05-24 12:43:12 +0100483 PrintF("INEXACT flag: %d;\n", sim_->inexact_vfp_flag_);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800484 } else if (strcmp(cmd, "stop") == 0) {
485 int32_t value;
Steve Block1e0659c2011-05-24 12:43:12 +0100486 intptr_t stop_pc = sim_->get_pc() - 2 * Instruction::kInstrSize;
487 Instruction* stop_instr = reinterpret_cast<Instruction*>(stop_pc);
488 Instruction* msg_address =
489 reinterpret_cast<Instruction*>(stop_pc + Instruction::kInstrSize);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800490 if ((argc == 2) && (strcmp(arg1, "unstop") == 0)) {
491 // Remove the current stop.
492 if (sim_->isStopInstruction(stop_instr)) {
493 stop_instr->SetInstructionBits(kNopInstr);
494 msg_address->SetInstructionBits(kNopInstr);
495 } else {
496 PrintF("Not at debugger stop.\n");
497 }
498 } else if (argc == 3) {
499 // Print information about all/the specified breakpoint(s).
500 if (strcmp(arg1, "info") == 0) {
501 if (strcmp(arg2, "all") == 0) {
502 PrintF("Stop information:\n");
503 for (uint32_t i = 0; i < sim_->kNumOfWatchedStops; i++) {
504 sim_->PrintStopInfo(i);
505 }
506 } else if (GetValue(arg2, &value)) {
507 sim_->PrintStopInfo(value);
508 } else {
509 PrintF("Unrecognized argument.\n");
510 }
511 } else if (strcmp(arg1, "enable") == 0) {
512 // Enable all/the specified breakpoint(s).
513 if (strcmp(arg2, "all") == 0) {
514 for (uint32_t i = 0; i < sim_->kNumOfWatchedStops; i++) {
515 sim_->EnableStop(i);
516 }
517 } else if (GetValue(arg2, &value)) {
518 sim_->EnableStop(value);
519 } else {
520 PrintF("Unrecognized argument.\n");
521 }
522 } else if (strcmp(arg1, "disable") == 0) {
523 // Disable all/the specified breakpoint(s).
524 if (strcmp(arg2, "all") == 0) {
525 for (uint32_t i = 0; i < sim_->kNumOfWatchedStops; i++) {
526 sim_->DisableStop(i);
527 }
528 } else if (GetValue(arg2, &value)) {
529 sim_->DisableStop(value);
530 } else {
531 PrintF("Unrecognized argument.\n");
532 }
533 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000534 } else {
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800535 PrintF("Wrong usage. Use help command for more information.\n");
Steve Blocka7e24c12009-10-30 11:49:00 +0000536 }
Leon Clarkee46be812010-01-19 14:06:41 +0000537 } else if ((strcmp(cmd, "t") == 0) || strcmp(cmd, "trace") == 0) {
538 ::v8::internal::FLAG_trace_sim = !::v8::internal::FLAG_trace_sim;
539 PrintF("Trace of executed instructions is %s\n",
540 ::v8::internal::FLAG_trace_sim ? "on" : "off");
Steve Blocka7e24c12009-10-30 11:49:00 +0000541 } else if ((strcmp(cmd, "h") == 0) || (strcmp(cmd, "help") == 0)) {
542 PrintF("cont\n");
543 PrintF(" continue execution (alias 'c')\n");
544 PrintF("stepi\n");
545 PrintF(" step one instruction (alias 'si')\n");
546 PrintF("print <register>\n");
547 PrintF(" print register content (alias 'p')\n");
548 PrintF(" use register name 'all' to print all registers\n");
Ben Murdoch8b112d22011-06-08 16:22:53 +0100549 PrintF(" add argument 'fp' to print register pair double values\n");
Steve Blocka7e24c12009-10-30 11:49:00 +0000550 PrintF("printobject <register>\n");
551 PrintF(" print an object from a register (alias 'po')\n");
552 PrintF("flags\n");
553 PrintF(" print flags\n");
Steve Block6ded16b2010-05-10 14:33:55 +0100554 PrintF("stack [<words>]\n");
555 PrintF(" dump stack content, default dump 10 words)\n");
556 PrintF("mem <address> [<words>]\n");
557 PrintF(" dump memory content, default dump 10 words)\n");
Steve Blocka7e24c12009-10-30 11:49:00 +0000558 PrintF("disasm [<instructions>]\n");
Steve Block44f0eee2011-05-26 01:26:41 +0100559 PrintF("disasm [<address/register>]\n");
560 PrintF("disasm [[<address/register>] <instructions>]\n");
561 PrintF(" disassemble code, default is 10 instructions\n");
562 PrintF(" from pc (alias 'di')\n");
Steve Blocka7e24c12009-10-30 11:49:00 +0000563 PrintF("gdb\n");
564 PrintF(" enter gdb\n");
565 PrintF("break <address>\n");
566 PrintF(" set a break point on the address\n");
567 PrintF("del\n");
568 PrintF(" delete the breakpoint\n");
Leon Clarkee46be812010-01-19 14:06:41 +0000569 PrintF("trace (alias 't')\n");
Steve Block6ded16b2010-05-10 14:33:55 +0100570 PrintF(" toogle the tracing of all executed statements\n");
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800571 PrintF("stop feature:\n");
572 PrintF(" Description:\n");
573 PrintF(" Stops are debug instructions inserted by\n");
574 PrintF(" the Assembler::stop() function.\n");
575 PrintF(" When hitting a stop, the Simulator will\n");
Steve Block44f0eee2011-05-26 01:26:41 +0100576 PrintF(" stop and and give control to the ArmDebugger.\n");
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800577 PrintF(" The first %d stop codes are watched:\n",
578 Simulator::kNumOfWatchedStops);
579 PrintF(" - They can be enabled / disabled: the Simulator\n");
Steve Block44f0eee2011-05-26 01:26:41 +0100580 PrintF(" will / won't stop when hitting them.\n");
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800581 PrintF(" - The Simulator keeps track of how many times they \n");
582 PrintF(" are met. (See the info command.) Going over a\n");
583 PrintF(" disabled stop still increases its counter. \n");
584 PrintF(" Commands:\n");
585 PrintF(" stop info all/<code> : print infos about number <code>\n");
586 PrintF(" or all stop(s).\n");
587 PrintF(" stop enable/disable all/<code> : enables / disables\n");
588 PrintF(" all or number <code> stop(s)\n");
589 PrintF(" stop unstop\n");
590 PrintF(" ignore the stop instruction at the current location\n");
591 PrintF(" from now on\n");
Steve Blocka7e24c12009-10-30 11:49:00 +0000592 } else {
593 PrintF("Unknown command: %s\n", cmd);
594 }
595 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000596 }
597
598 // Add all the breakpoints back to stop execution and enter the debugger
599 // shell when hit.
600 RedoBreakpoints();
601
602#undef COMMAND_SIZE
603#undef ARG_SIZE
604
605#undef STR
606#undef XSTR
607}
608
609
Steve Block6ded16b2010-05-10 14:33:55 +0100610static bool ICacheMatch(void* one, void* two) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000611 DCHECK((reinterpret_cast<intptr_t>(one) & CachePage::kPageMask) == 0);
612 DCHECK((reinterpret_cast<intptr_t>(two) & CachePage::kPageMask) == 0);
Steve Block6ded16b2010-05-10 14:33:55 +0100613 return one == two;
614}
615
616
617static uint32_t ICacheHash(void* key) {
618 return static_cast<uint32_t>(reinterpret_cast<uintptr_t>(key)) >> 2;
619}
620
621
622static bool AllOnOnePage(uintptr_t start, int size) {
623 intptr_t start_page = (start & ~CachePage::kPageMask);
624 intptr_t end_page = ((start + size) & ~CachePage::kPageMask);
625 return start_page == end_page;
626}
627
628
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100629void Simulator::set_last_debugger_input(char* input) {
630 DeleteArray(last_debugger_input_);
631 last_debugger_input_ = input;
632}
633
634
Steve Block44f0eee2011-05-26 01:26:41 +0100635void Simulator::FlushICache(v8::internal::HashMap* i_cache,
636 void* start_addr,
637 size_t size) {
Steve Block6ded16b2010-05-10 14:33:55 +0100638 intptr_t start = reinterpret_cast<intptr_t>(start_addr);
639 int intra_line = (start & CachePage::kLineMask);
640 start -= intra_line;
641 size += intra_line;
642 size = ((size - 1) | CachePage::kLineMask) + 1;
643 int offset = (start & CachePage::kPageMask);
644 while (!AllOnOnePage(start, size - 1)) {
645 int bytes_to_flush = CachePage::kPageSize - offset;
Steve Block44f0eee2011-05-26 01:26:41 +0100646 FlushOnePage(i_cache, start, bytes_to_flush);
Steve Block6ded16b2010-05-10 14:33:55 +0100647 start += bytes_to_flush;
648 size -= bytes_to_flush;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000649 DCHECK_EQ(0, start & CachePage::kPageMask);
Steve Block6ded16b2010-05-10 14:33:55 +0100650 offset = 0;
651 }
652 if (size != 0) {
Steve Block44f0eee2011-05-26 01:26:41 +0100653 FlushOnePage(i_cache, start, size);
Steve Block6ded16b2010-05-10 14:33:55 +0100654 }
655}
656
657
Steve Block44f0eee2011-05-26 01:26:41 +0100658CachePage* Simulator::GetCachePage(v8::internal::HashMap* i_cache, void* page) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000659 v8::internal::HashMap::Entry* entry =
660 i_cache->LookupOrInsert(page, ICacheHash(page));
Steve Block6ded16b2010-05-10 14:33:55 +0100661 if (entry->value == NULL) {
662 CachePage* new_page = new CachePage();
663 entry->value = new_page;
664 }
665 return reinterpret_cast<CachePage*>(entry->value);
666}
667
668
669// Flush from start up to and not including start + size.
Steve Block44f0eee2011-05-26 01:26:41 +0100670void Simulator::FlushOnePage(v8::internal::HashMap* i_cache,
671 intptr_t start,
672 int size) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000673 DCHECK(size <= CachePage::kPageSize);
674 DCHECK(AllOnOnePage(start, size - 1));
675 DCHECK((start & CachePage::kLineMask) == 0);
676 DCHECK((size & CachePage::kLineMask) == 0);
Steve Block6ded16b2010-05-10 14:33:55 +0100677 void* page = reinterpret_cast<void*>(start & (~CachePage::kPageMask));
678 int offset = (start & CachePage::kPageMask);
Steve Block44f0eee2011-05-26 01:26:41 +0100679 CachePage* cache_page = GetCachePage(i_cache, page);
Steve Block6ded16b2010-05-10 14:33:55 +0100680 char* valid_bytemap = cache_page->ValidityByte(offset);
681 memset(valid_bytemap, CachePage::LINE_INVALID, size >> CachePage::kLineShift);
682}
683
684
Steve Block44f0eee2011-05-26 01:26:41 +0100685void Simulator::CheckICache(v8::internal::HashMap* i_cache,
686 Instruction* instr) {
Steve Block6ded16b2010-05-10 14:33:55 +0100687 intptr_t address = reinterpret_cast<intptr_t>(instr);
688 void* page = reinterpret_cast<void*>(address & (~CachePage::kPageMask));
689 void* line = reinterpret_cast<void*>(address & (~CachePage::kLineMask));
690 int offset = (address & CachePage::kPageMask);
Steve Block44f0eee2011-05-26 01:26:41 +0100691 CachePage* cache_page = GetCachePage(i_cache, page);
Steve Block6ded16b2010-05-10 14:33:55 +0100692 char* cache_valid_byte = cache_page->ValidityByte(offset);
693 bool cache_hit = (*cache_valid_byte == CachePage::LINE_VALID);
694 char* cached_line = cache_page->CachedData(offset & ~CachePage::kLineMask);
695 if (cache_hit) {
696 // Check that the data in memory matches the contents of the I-cache.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000697 CHECK_EQ(0,
698 memcmp(reinterpret_cast<void*>(instr),
699 cache_page->CachedData(offset), Instruction::kInstrSize));
Steve Block6ded16b2010-05-10 14:33:55 +0100700 } else {
701 // Cache miss. Load memory into the cache.
702 memcpy(cached_line, line, CachePage::kLineLength);
703 *cache_valid_byte = CachePage::LINE_VALID;
704 }
705}
706
707
Ben Murdoch257744e2011-11-30 15:57:28 +0000708void Simulator::Initialize(Isolate* isolate) {
709 if (isolate->simulator_initialized()) return;
710 isolate->set_simulator_initialized(true);
711 ::v8::internal::ExternalReference::set_redirector(isolate,
712 &RedirectExternalReference);
Steve Blocka7e24c12009-10-30 11:49:00 +0000713}
714
715
Ben Murdoch257744e2011-11-30 15:57:28 +0000716Simulator::Simulator(Isolate* isolate) : isolate_(isolate) {
Steve Block44f0eee2011-05-26 01:26:41 +0100717 i_cache_ = isolate_->simulator_i_cache();
Steve Block6ded16b2010-05-10 14:33:55 +0100718 if (i_cache_ == NULL) {
719 i_cache_ = new v8::internal::HashMap(&ICacheMatch);
Steve Block44f0eee2011-05-26 01:26:41 +0100720 isolate_->set_simulator_i_cache(i_cache_);
Steve Block6ded16b2010-05-10 14:33:55 +0100721 }
Ben Murdoch257744e2011-11-30 15:57:28 +0000722 Initialize(isolate);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100723 // Set up simulator support first. Some of this information is needed to
Steve Blocka7e24c12009-10-30 11:49:00 +0000724 // setup the architecture state.
725 size_t stack_size = 1 * 1024*1024; // allocate 1MB for stack
726 stack_ = reinterpret_cast<char*>(malloc(stack_size));
727 pc_modified_ = false;
728 icount_ = 0;
729 break_pc_ = NULL;
730 break_instr_ = 0;
731
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100732 // Set up architecture state.
Steve Blocka7e24c12009-10-30 11:49:00 +0000733 // All registers are initialized to zero to start with.
734 for (int i = 0; i < num_registers; i++) {
735 registers_[i] = 0;
736 }
737 n_flag_ = false;
738 z_flag_ = false;
739 c_flag_ = false;
740 v_flag_ = false;
741
Steve Blockd0582a62009-12-15 09:54:21 +0000742 // Initializing VFP registers.
743 // All registers are initialized to zero to start with
744 // even though s_registers_ & d_registers_ share the same
745 // physical registers in the target.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000746 for (int i = 0; i < num_d_registers * 2; i++) {
747 vfp_registers_[i] = 0;
Steve Blockd0582a62009-12-15 09:54:21 +0000748 }
749 n_flag_FPSCR_ = false;
750 z_flag_FPSCR_ = false;
751 c_flag_FPSCR_ = false;
752 v_flag_FPSCR_ = false;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000753 FPSCR_rounding_mode_ = RN;
754 FPSCR_default_NaN_mode_ = false;
Steve Blockd0582a62009-12-15 09:54:21 +0000755
756 inv_op_vfp_flag_ = false;
757 div_zero_vfp_flag_ = false;
758 overflow_vfp_flag_ = false;
759 underflow_vfp_flag_ = false;
760 inexact_vfp_flag_ = false;
761
Steve Blocka7e24c12009-10-30 11:49:00 +0000762 // The sp is initialized to point to the bottom (high address) of the
763 // allocated stack area. To be safe in potential stack underflows we leave
764 // some buffer below.
765 registers_[sp] = reinterpret_cast<int32_t>(stack_) + stack_size - 64;
766 // The lr and pc are initialized to a known bad value that will cause an
767 // access violation if the simulator ever tries to execute it.
768 registers_[pc] = bad_lr;
769 registers_[lr] = bad_lr;
770 InitializeCoverage();
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100771
772 last_debugger_input_ = NULL;
Steve Blocka7e24c12009-10-30 11:49:00 +0000773}
774
775
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000776Simulator::~Simulator() { free(stack_); }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000777
778
Steve Blocka7e24c12009-10-30 11:49:00 +0000779// When the generated code calls an external reference we need to catch that in
780// the simulator. The external reference will be a function compiled for the
781// host architecture. We need to call that function instead of trying to
782// execute it with the simulator. We do that by redirecting the external
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800783// reference to a svc (Supervisor Call) instruction that is handled by
Steve Blocka7e24c12009-10-30 11:49:00 +0000784// the simulator. We write the original destination of the jump just at a known
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800785// offset from the svc instruction so the simulator knows what to call.
Steve Blocka7e24c12009-10-30 11:49:00 +0000786class Redirection {
787 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000788 Redirection(Isolate* isolate, void* external_function,
789 ExternalReference::Type type)
Steve Blocka7e24c12009-10-30 11:49:00 +0000790 : external_function_(external_function),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000791 swi_instruction_(al | (0xf * B24) | kCallRtRedirected),
Steve Block1e0659c2011-05-24 12:43:12 +0100792 type_(type),
Steve Block44f0eee2011-05-26 01:26:41 +0100793 next_(NULL) {
Steve Block44f0eee2011-05-26 01:26:41 +0100794 next_ = isolate->simulator_redirection();
795 Simulator::current(isolate)->
796 FlushICache(isolate->simulator_i_cache(),
797 reinterpret_cast<void*>(&swi_instruction_),
798 Instruction::kInstrSize);
799 isolate->set_simulator_redirection(this);
Steve Blocka7e24c12009-10-30 11:49:00 +0000800 }
801
802 void* address_of_swi_instruction() {
803 return reinterpret_cast<void*>(&swi_instruction_);
804 }
805
806 void* external_function() { return external_function_; }
Steve Block1e0659c2011-05-24 12:43:12 +0100807 ExternalReference::Type type() { return type_; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000808
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000809 static Redirection* Get(Isolate* isolate, void* external_function,
Steve Block1e0659c2011-05-24 12:43:12 +0100810 ExternalReference::Type type) {
Steve Block44f0eee2011-05-26 01:26:41 +0100811 Redirection* current = isolate->simulator_redirection();
812 for (; current != NULL; current = current->next_) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000813 if (current->external_function_ == external_function) {
814 DCHECK_EQ(current->type(), type);
815 return current;
816 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000817 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000818 return new Redirection(isolate, external_function, type);
Steve Blocka7e24c12009-10-30 11:49:00 +0000819 }
820
Steve Block1e0659c2011-05-24 12:43:12 +0100821 static Redirection* FromSwiInstruction(Instruction* swi_instruction) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000822 char* addr_of_swi = reinterpret_cast<char*>(swi_instruction);
823 char* addr_of_redirection =
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000824 addr_of_swi - offsetof(Redirection, swi_instruction_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000825 return reinterpret_cast<Redirection*>(addr_of_redirection);
826 }
827
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000828 static void* ReverseRedirection(int32_t reg) {
829 Redirection* redirection = FromSwiInstruction(
830 reinterpret_cast<Instruction*>(reinterpret_cast<void*>(reg)));
831 return redirection->external_function();
832 }
833
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000834 static void DeleteChain(Redirection* redirection) {
835 while (redirection != nullptr) {
836 Redirection* next = redirection->next_;
837 delete redirection;
838 redirection = next;
839 }
840 }
841
Steve Blocka7e24c12009-10-30 11:49:00 +0000842 private:
843 void* external_function_;
844 uint32_t swi_instruction_;
Steve Block1e0659c2011-05-24 12:43:12 +0100845 ExternalReference::Type type_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000846 Redirection* next_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000847};
848
849
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000850// static
851void Simulator::TearDown(HashMap* i_cache, Redirection* first) {
852 Redirection::DeleteChain(first);
853 if (i_cache != nullptr) {
854 for (HashMap::Entry* entry = i_cache->Start(); entry != nullptr;
855 entry = i_cache->Next(entry)) {
856 delete static_cast<CachePage*>(entry->value);
857 }
858 delete i_cache;
859 }
860}
861
862
863void* Simulator::RedirectExternalReference(Isolate* isolate,
864 void* external_function,
Steve Block1e0659c2011-05-24 12:43:12 +0100865 ExternalReference::Type type) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000866 Redirection* redirection = Redirection::Get(isolate, external_function, type);
Steve Blocka7e24c12009-10-30 11:49:00 +0000867 return redirection->address_of_swi_instruction();
868}
869
870
871// Get the active Simulator for the current thread.
Steve Block44f0eee2011-05-26 01:26:41 +0100872Simulator* Simulator::current(Isolate* isolate) {
873 v8::internal::Isolate::PerIsolateThreadData* isolate_data =
Ben Murdoch257744e2011-11-30 15:57:28 +0000874 isolate->FindOrAllocatePerThreadDataForThisThread();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000875 DCHECK(isolate_data != NULL);
Steve Block44f0eee2011-05-26 01:26:41 +0100876
877 Simulator* sim = isolate_data->simulator();
Steve Blocka7e24c12009-10-30 11:49:00 +0000878 if (sim == NULL) {
Steve Block44f0eee2011-05-26 01:26:41 +0100879 // TODO(146): delete the simulator object when a thread/isolate goes away.
Ben Murdoch257744e2011-11-30 15:57:28 +0000880 sim = new Simulator(isolate);
Steve Block44f0eee2011-05-26 01:26:41 +0100881 isolate_data->set_simulator(sim);
Steve Blocka7e24c12009-10-30 11:49:00 +0000882 }
883 return sim;
884}
885
886
887// Sets the register in the architecture state. It will also deal with updating
888// Simulator internal state for special registers such as PC.
889void Simulator::set_register(int reg, int32_t value) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000890 DCHECK((reg >= 0) && (reg < num_registers));
Steve Blocka7e24c12009-10-30 11:49:00 +0000891 if (reg == pc) {
892 pc_modified_ = true;
893 }
894 registers_[reg] = value;
895}
896
897
898// Get the register from the architecture state. This function does handle
899// the special case of accessing the PC register.
900int32_t Simulator::get_register(int reg) const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000901 DCHECK((reg >= 0) && (reg < num_registers));
Steve Block791712a2010-08-27 10:21:07 +0100902 // Stupid code added to avoid bug in GCC.
903 // See: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43949
904 if (reg >= num_registers) return 0;
905 // End stupid code.
Steve Block1e0659c2011-05-24 12:43:12 +0100906 return registers_[reg] + ((reg == pc) ? Instruction::kPCReadOffset : 0);
Steve Blocka7e24c12009-10-30 11:49:00 +0000907}
908
909
Ben Murdoch8b112d22011-06-08 16:22:53 +0100910double Simulator::get_double_from_register_pair(int reg) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000911 DCHECK((reg >= 0) && (reg < num_registers) && ((reg % 2) == 0));
Ben Murdoch8b112d22011-06-08 16:22:53 +0100912
913 double dm_val = 0.0;
914 // Read the bits from the unsigned integer register_[] array
915 // into the double precision floating point value and return it.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000916 char buffer[2 * sizeof(vfp_registers_[0])];
Ben Murdoch8b112d22011-06-08 16:22:53 +0100917 memcpy(buffer, &registers_[reg], 2 * sizeof(registers_[0]));
918 memcpy(&dm_val, buffer, 2 * sizeof(registers_[0]));
919 return(dm_val);
920}
921
922
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000923void Simulator::set_register_pair_from_double(int reg, double* value) {
924 DCHECK((reg >= 0) && (reg < num_registers) && ((reg % 2) == 0));
925 memcpy(registers_ + reg, value, sizeof(*value));
926}
927
928
Kristian Monsen25f61362010-05-21 11:50:48 +0100929void Simulator::set_dw_register(int dreg, const int* dbl) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000930 DCHECK((dreg >= 0) && (dreg < num_d_registers));
Kristian Monsen25f61362010-05-21 11:50:48 +0100931 registers_[dreg] = dbl[0];
932 registers_[dreg + 1] = dbl[1];
933}
934
935
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000936void Simulator::get_d_register(int dreg, uint64_t* value) {
937 DCHECK((dreg >= 0) && (dreg < DwVfpRegister::NumRegisters()));
938 memcpy(value, vfp_registers_ + dreg * 2, sizeof(*value));
939}
940
941
942void Simulator::set_d_register(int dreg, const uint64_t* value) {
943 DCHECK((dreg >= 0) && (dreg < DwVfpRegister::NumRegisters()));
944 memcpy(vfp_registers_ + dreg * 2, value, sizeof(*value));
945}
946
947
948void Simulator::get_d_register(int dreg, uint32_t* value) {
949 DCHECK((dreg >= 0) && (dreg < DwVfpRegister::NumRegisters()));
950 memcpy(value, vfp_registers_ + dreg * 2, sizeof(*value) * 2);
951}
952
953
954void Simulator::set_d_register(int dreg, const uint32_t* value) {
955 DCHECK((dreg >= 0) && (dreg < DwVfpRegister::NumRegisters()));
956 memcpy(vfp_registers_ + dreg * 2, value, sizeof(*value) * 2);
957}
958
959
960void Simulator::get_q_register(int qreg, uint64_t* value) {
961 DCHECK((qreg >= 0) && (qreg < num_q_registers));
962 memcpy(value, vfp_registers_ + qreg * 4, sizeof(*value) * 2);
963}
964
965
966void Simulator::set_q_register(int qreg, const uint64_t* value) {
967 DCHECK((qreg >= 0) && (qreg < num_q_registers));
968 memcpy(vfp_registers_ + qreg * 4, value, sizeof(*value) * 2);
969}
970
971
972void Simulator::get_q_register(int qreg, uint32_t* value) {
973 DCHECK((qreg >= 0) && (qreg < num_q_registers));
974 memcpy(value, vfp_registers_ + qreg * 4, sizeof(*value) * 4);
975}
976
977
978void Simulator::set_q_register(int qreg, const uint32_t* value) {
979 DCHECK((qreg >= 0) && (qreg < num_q_registers));
980 memcpy(vfp_registers_ + qreg * 4, value, sizeof(*value) * 4);
981}
982
983
Steve Blocka7e24c12009-10-30 11:49:00 +0000984// Raw access to the PC register.
985void Simulator::set_pc(int32_t value) {
986 pc_modified_ = true;
987 registers_[pc] = value;
988}
989
990
Ben Murdochb0fe1622011-05-05 13:52:32 +0100991bool Simulator::has_bad_pc() const {
992 return ((registers_[pc] == bad_lr) || (registers_[pc] == end_sim_pc));
993}
994
995
Steve Blocka7e24c12009-10-30 11:49:00 +0000996// Raw access to the PC register without the special adjustment when reading.
997int32_t Simulator::get_pc() const {
998 return registers_[pc];
999}
1000
1001
Steve Blockd0582a62009-12-15 09:54:21 +00001002// Getting from and setting into VFP registers.
1003void Simulator::set_s_register(int sreg, unsigned int value) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001004 DCHECK((sreg >= 0) && (sreg < num_s_registers));
1005 vfp_registers_[sreg] = value;
Steve Blockd0582a62009-12-15 09:54:21 +00001006}
1007
1008
1009unsigned int Simulator::get_s_register(int sreg) const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001010 DCHECK((sreg >= 0) && (sreg < num_s_registers));
1011 return vfp_registers_[sreg];
Steve Blockd0582a62009-12-15 09:54:21 +00001012}
1013
1014
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001015template<class InputType, int register_size>
1016void Simulator::SetVFPRegister(int reg_index, const InputType& value) {
1017 DCHECK(reg_index >= 0);
1018 if (register_size == 1) DCHECK(reg_index < num_s_registers);
1019 if (register_size == 2) DCHECK(reg_index < DwVfpRegister::NumRegisters());
1020
1021 char buffer[register_size * sizeof(vfp_registers_[0])];
1022 memcpy(buffer, &value, register_size * sizeof(vfp_registers_[0]));
1023 memcpy(&vfp_registers_[reg_index * register_size], buffer,
1024 register_size * sizeof(vfp_registers_[0]));
Steve Blockd0582a62009-12-15 09:54:21 +00001025}
1026
1027
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001028template<class ReturnType, int register_size>
1029ReturnType Simulator::GetFromVFPRegister(int reg_index) {
1030 DCHECK(reg_index >= 0);
1031 if (register_size == 1) DCHECK(reg_index < num_s_registers);
1032 if (register_size == 2) DCHECK(reg_index < DwVfpRegister::NumRegisters());
1033
1034 ReturnType value = 0;
1035 char buffer[register_size * sizeof(vfp_registers_[0])];
1036 memcpy(buffer, &vfp_registers_[register_size * reg_index],
1037 register_size * sizeof(vfp_registers_[0]));
1038 memcpy(&value, buffer, register_size * sizeof(vfp_registers_[0]));
1039 return value;
Steve Blockd0582a62009-12-15 09:54:21 +00001040}
1041
1042
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001043// Runtime FP routines take:
1044// - two double arguments
1045// - one double argument and zero or one integer arguments.
1046// All are consructed here from r0-r3 or d0, d1 and r0.
1047void Simulator::GetFpArgs(double* x, double* y, int32_t* z) {
Ben Murdoch257744e2011-11-30 15:57:28 +00001048 if (use_eabi_hardfloat()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001049 *x = get_double_from_d_register(0);
1050 *y = get_double_from_d_register(1);
1051 *z = get_register(0);
Ben Murdoch257744e2011-11-30 15:57:28 +00001052 } else {
Ben Murdoch257744e2011-11-30 15:57:28 +00001053 // Registers 0 and 1 -> x.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001054 *x = get_double_from_register_pair(0);
1055 // Register 2 and 3 -> y.
1056 *y = get_double_from_register_pair(2);
1057 // Register 2 -> z
1058 *z = get_register(2);
Ben Murdoch257744e2011-11-30 15:57:28 +00001059 }
1060}
1061
1062
1063// The return value is either in r0/r1 or d0.
Steve Blocka7e24c12009-10-30 11:49:00 +00001064void Simulator::SetFpResult(const double& result) {
Ben Murdoch257744e2011-11-30 15:57:28 +00001065 if (use_eabi_hardfloat()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001066 char buffer[2 * sizeof(vfp_registers_[0])];
Ben Murdoch257744e2011-11-30 15:57:28 +00001067 memcpy(buffer, &result, sizeof(buffer));
1068 // Copy result to d0.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001069 memcpy(vfp_registers_, buffer, sizeof(buffer));
Ben Murdoch257744e2011-11-30 15:57:28 +00001070 } else {
1071 char buffer[2 * sizeof(registers_[0])];
1072 memcpy(buffer, &result, sizeof(buffer));
1073 // Copy result to r0 and r1.
1074 memcpy(registers_, buffer, sizeof(buffer));
1075 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001076}
1077
1078
1079void Simulator::TrashCallerSaveRegisters() {
1080 // We don't trash the registers with the return value.
1081 registers_[2] = 0x50Bad4U;
1082 registers_[3] = 0x50Bad4U;
1083 registers_[12] = 0x50Bad4U;
1084}
1085
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001086
Kristian Monsen25f61362010-05-21 11:50:48 +01001087// Some Operating Systems allow unaligned access on ARMv7 targets. We
1088// assume that unaligned accesses are not allowed unless the v8 build system
1089// defines the CAN_USE_UNALIGNED_ACCESSES macro to be non-zero.
1090// The following statements below describes the behavior of the ARM CPUs
1091// that don't support unaligned access.
1092// Some ARM platforms raise an interrupt on detecting unaligned access.
1093// On others it does a funky rotation thing. For now we
1094// simply disallow unaligned reads. Note that simulator runs have the runtime
Steve Blocka7e24c12009-10-30 11:49:00 +00001095// system running directly on the host system and only generated code is
1096// executed in the simulator. Since the host is typically IA32 we will not
Kristian Monsen25f61362010-05-21 11:50:48 +01001097// get the correct ARM-like behaviour on unaligned accesses for those ARM
1098// targets that don't support unaligned loads and stores.
1099
Steve Blocka7e24c12009-10-30 11:49:00 +00001100
Steve Block1e0659c2011-05-24 12:43:12 +01001101int Simulator::ReadW(int32_t addr, Instruction* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001102 if (FLAG_enable_unaligned_accesses || (addr & 3) == 0) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001103 intptr_t* ptr = reinterpret_cast<intptr_t*>(addr);
1104 return *ptr;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001105 } else {
1106 PrintF("Unaligned read at 0x%08x, pc=0x%08" V8PRIxPTR "\n",
1107 addr,
1108 reinterpret_cast<intptr_t>(instr));
1109 UNIMPLEMENTED();
1110 return 0;
Steve Blocka7e24c12009-10-30 11:49:00 +00001111 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001112}
1113
1114
Steve Block1e0659c2011-05-24 12:43:12 +01001115void Simulator::WriteW(int32_t addr, int value, Instruction* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001116 if (FLAG_enable_unaligned_accesses || (addr & 3) == 0) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001117 intptr_t* ptr = reinterpret_cast<intptr_t*>(addr);
1118 *ptr = value;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001119 } else {
1120 PrintF("Unaligned write at 0x%08x, pc=0x%08" V8PRIxPTR "\n",
1121 addr,
1122 reinterpret_cast<intptr_t>(instr));
1123 UNIMPLEMENTED();
Steve Blocka7e24c12009-10-30 11:49:00 +00001124 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001125}
1126
1127
Steve Block1e0659c2011-05-24 12:43:12 +01001128uint16_t Simulator::ReadHU(int32_t addr, Instruction* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001129 if (FLAG_enable_unaligned_accesses || (addr & 1) == 0) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001130 uint16_t* ptr = reinterpret_cast<uint16_t*>(addr);
1131 return *ptr;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001132 } else {
1133 PrintF("Unaligned unsigned halfword read at 0x%08x, pc=0x%08"
1134 V8PRIxPTR "\n",
1135 addr,
1136 reinterpret_cast<intptr_t>(instr));
1137 UNIMPLEMENTED();
1138 return 0;
Steve Blocka7e24c12009-10-30 11:49:00 +00001139 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001140}
1141
1142
Steve Block1e0659c2011-05-24 12:43:12 +01001143int16_t Simulator::ReadH(int32_t addr, Instruction* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001144 if (FLAG_enable_unaligned_accesses || (addr & 1) == 0) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001145 int16_t* ptr = reinterpret_cast<int16_t*>(addr);
1146 return *ptr;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001147 } else {
1148 PrintF("Unaligned signed halfword read at 0x%08x\n", addr);
1149 UNIMPLEMENTED();
1150 return 0;
Steve Blocka7e24c12009-10-30 11:49:00 +00001151 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001152}
1153
1154
Steve Block1e0659c2011-05-24 12:43:12 +01001155void Simulator::WriteH(int32_t addr, uint16_t value, Instruction* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001156 if (FLAG_enable_unaligned_accesses || (addr & 1) == 0) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001157 uint16_t* ptr = reinterpret_cast<uint16_t*>(addr);
1158 *ptr = value;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001159 } else {
1160 PrintF("Unaligned unsigned halfword write at 0x%08x, pc=0x%08"
1161 V8PRIxPTR "\n",
1162 addr,
1163 reinterpret_cast<intptr_t>(instr));
1164 UNIMPLEMENTED();
Steve Blocka7e24c12009-10-30 11:49:00 +00001165 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001166}
1167
1168
Steve Block1e0659c2011-05-24 12:43:12 +01001169void Simulator::WriteH(int32_t addr, int16_t value, Instruction* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001170 if (FLAG_enable_unaligned_accesses || (addr & 1) == 0) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001171 int16_t* ptr = reinterpret_cast<int16_t*>(addr);
1172 *ptr = value;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001173 } else {
1174 PrintF("Unaligned halfword write at 0x%08x, pc=0x%08" V8PRIxPTR "\n",
1175 addr,
1176 reinterpret_cast<intptr_t>(instr));
1177 UNIMPLEMENTED();
Steve Blocka7e24c12009-10-30 11:49:00 +00001178 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001179}
1180
1181
1182uint8_t Simulator::ReadBU(int32_t addr) {
1183 uint8_t* ptr = reinterpret_cast<uint8_t*>(addr);
1184 return *ptr;
1185}
1186
1187
1188int8_t Simulator::ReadB(int32_t addr) {
1189 int8_t* ptr = reinterpret_cast<int8_t*>(addr);
1190 return *ptr;
1191}
1192
1193
1194void Simulator::WriteB(int32_t addr, uint8_t value) {
1195 uint8_t* ptr = reinterpret_cast<uint8_t*>(addr);
1196 *ptr = value;
1197}
1198
1199
1200void Simulator::WriteB(int32_t addr, int8_t value) {
1201 int8_t* ptr = reinterpret_cast<int8_t*>(addr);
1202 *ptr = value;
1203}
1204
1205
Kristian Monsen25f61362010-05-21 11:50:48 +01001206int32_t* Simulator::ReadDW(int32_t addr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001207 if (FLAG_enable_unaligned_accesses || (addr & 3) == 0) {
Kristian Monsen25f61362010-05-21 11:50:48 +01001208 int32_t* ptr = reinterpret_cast<int32_t*>(addr);
1209 return ptr;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001210 } else {
1211 PrintF("Unaligned read at 0x%08x\n", addr);
1212 UNIMPLEMENTED();
1213 return 0;
Kristian Monsen25f61362010-05-21 11:50:48 +01001214 }
Kristian Monsen25f61362010-05-21 11:50:48 +01001215}
1216
1217
1218void Simulator::WriteDW(int32_t addr, int32_t value1, int32_t value2) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001219 if (FLAG_enable_unaligned_accesses || (addr & 3) == 0) {
Kristian Monsen25f61362010-05-21 11:50:48 +01001220 int32_t* ptr = reinterpret_cast<int32_t*>(addr);
1221 *ptr++ = value1;
1222 *ptr = value2;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001223 } else {
1224 PrintF("Unaligned write at 0x%08x\n", addr);
1225 UNIMPLEMENTED();
Kristian Monsen25f61362010-05-21 11:50:48 +01001226 }
Kristian Monsen25f61362010-05-21 11:50:48 +01001227}
1228
1229
Steve Blocka7e24c12009-10-30 11:49:00 +00001230// Returns the limit of the stack area to enable checking for stack overflows.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001231uintptr_t Simulator::StackLimit(uintptr_t c_limit) const {
1232 // The simulator uses a separate JS stack. If we have exhausted the C stack,
1233 // we also drop down the JS limit to reflect the exhaustion on the JS stack.
1234 if (GetCurrentStackPosition() < c_limit) {
1235 return reinterpret_cast<uintptr_t>(get_sp());
1236 }
1237
1238 // Otherwise the limit is the JS stack. Leave a safety margin of 1024 bytes
1239 // to prevent overrunning the stack when pushing values.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001240 return reinterpret_cast<uintptr_t>(stack_) + 1024;
Steve Blocka7e24c12009-10-30 11:49:00 +00001241}
1242
1243
1244// Unsupported instructions use Format to print an error and stop execution.
Steve Block1e0659c2011-05-24 12:43:12 +01001245void Simulator::Format(Instruction* instr, const char* format) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001246 PrintF("Simulator found unsupported instruction:\n 0x%08x: %s\n",
Ben Murdochf87a2032010-10-22 12:50:53 +01001247 reinterpret_cast<intptr_t>(instr), format);
Steve Blocka7e24c12009-10-30 11:49:00 +00001248 UNIMPLEMENTED();
1249}
1250
1251
1252// Checks if the current instruction should be executed based on its
1253// condition bits.
Steve Block1e0659c2011-05-24 12:43:12 +01001254bool Simulator::ConditionallyExecute(Instruction* instr) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001255 switch (instr->ConditionField()) {
Steve Block1e0659c2011-05-24 12:43:12 +01001256 case eq: return z_flag_;
1257 case ne: return !z_flag_;
1258 case cs: return c_flag_;
1259 case cc: return !c_flag_;
1260 case mi: return n_flag_;
1261 case pl: return !n_flag_;
1262 case vs: return v_flag_;
1263 case vc: return !v_flag_;
1264 case hi: return c_flag_ && !z_flag_;
1265 case ls: return !c_flag_ || z_flag_;
1266 case ge: return n_flag_ == v_flag_;
1267 case lt: return n_flag_ != v_flag_;
1268 case gt: return !z_flag_ && (n_flag_ == v_flag_);
1269 case le: return z_flag_ || (n_flag_ != v_flag_);
1270 case al: return true;
Steve Blocka7e24c12009-10-30 11:49:00 +00001271 default: UNREACHABLE();
1272 }
1273 return false;
1274}
1275
1276
1277// Calculate and set the Negative and Zero flags.
1278void Simulator::SetNZFlags(int32_t val) {
1279 n_flag_ = (val < 0);
1280 z_flag_ = (val == 0);
1281}
1282
1283
1284// Set the Carry flag.
1285void Simulator::SetCFlag(bool val) {
1286 c_flag_ = val;
1287}
1288
1289
1290// Set the oVerflow flag.
1291void Simulator::SetVFlag(bool val) {
1292 v_flag_ = val;
1293}
1294
1295
1296// Calculate C flag value for additions.
Ben Murdoch257744e2011-11-30 15:57:28 +00001297bool Simulator::CarryFrom(int32_t left, int32_t right, int32_t carry) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001298 uint32_t uleft = static_cast<uint32_t>(left);
1299 uint32_t uright = static_cast<uint32_t>(right);
1300 uint32_t urest = 0xffffffffU - uleft;
1301
Ben Murdoch257744e2011-11-30 15:57:28 +00001302 return (uright > urest) ||
1303 (carry && (((uright + 1) > urest) || (uright > (urest - 1))));
Steve Blocka7e24c12009-10-30 11:49:00 +00001304}
1305
1306
1307// Calculate C flag value for subtractions.
1308bool Simulator::BorrowFrom(int32_t left, int32_t right) {
1309 uint32_t uleft = static_cast<uint32_t>(left);
1310 uint32_t uright = static_cast<uint32_t>(right);
1311
1312 return (uright > uleft);
1313}
1314
1315
1316// Calculate V flag value for additions and subtractions.
1317bool Simulator::OverflowFrom(int32_t alu_out,
1318 int32_t left, int32_t right, bool addition) {
1319 bool overflow;
1320 if (addition) {
1321 // operands have the same sign
1322 overflow = ((left >= 0 && right >= 0) || (left < 0 && right < 0))
1323 // and operands and result have different sign
1324 && ((left < 0 && alu_out >= 0) || (left >= 0 && alu_out < 0));
1325 } else {
1326 // operands have different signs
1327 overflow = ((left < 0 && right >= 0) || (left >= 0 && right < 0))
1328 // and first operand and result have different signs
1329 && ((left < 0 && alu_out >= 0) || (left >= 0 && alu_out < 0));
1330 }
1331 return overflow;
1332}
1333
1334
Steve Blockd0582a62009-12-15 09:54:21 +00001335// Support for VFP comparisons.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001336void Simulator::Compute_FPSCR_Flags(float val1, float val2) {
1337 if (std::isnan(val1) || std::isnan(val2)) {
1338 n_flag_FPSCR_ = false;
1339 z_flag_FPSCR_ = false;
1340 c_flag_FPSCR_ = true;
1341 v_flag_FPSCR_ = true;
1342 // All non-NaN cases.
1343 } else if (val1 == val2) {
1344 n_flag_FPSCR_ = false;
1345 z_flag_FPSCR_ = true;
1346 c_flag_FPSCR_ = true;
1347 v_flag_FPSCR_ = false;
1348 } else if (val1 < val2) {
1349 n_flag_FPSCR_ = true;
1350 z_flag_FPSCR_ = false;
1351 c_flag_FPSCR_ = false;
1352 v_flag_FPSCR_ = false;
1353 } else {
1354 // Case when (val1 > val2).
1355 n_flag_FPSCR_ = false;
1356 z_flag_FPSCR_ = false;
1357 c_flag_FPSCR_ = true;
1358 v_flag_FPSCR_ = false;
1359 }
1360}
1361
1362
Steve Blockd0582a62009-12-15 09:54:21 +00001363void Simulator::Compute_FPSCR_Flags(double val1, double val2) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001364 if (std::isnan(val1) || std::isnan(val2)) {
Leon Clarkee46be812010-01-19 14:06:41 +00001365 n_flag_FPSCR_ = false;
1366 z_flag_FPSCR_ = false;
1367 c_flag_FPSCR_ = true;
1368 v_flag_FPSCR_ = true;
Steve Blockd0582a62009-12-15 09:54:21 +00001369 // All non-NaN cases.
Leon Clarkee46be812010-01-19 14:06:41 +00001370 } else if (val1 == val2) {
Steve Blockd0582a62009-12-15 09:54:21 +00001371 n_flag_FPSCR_ = false;
1372 z_flag_FPSCR_ = true;
1373 c_flag_FPSCR_ = true;
1374 v_flag_FPSCR_ = false;
1375 } else if (val1 < val2) {
1376 n_flag_FPSCR_ = true;
1377 z_flag_FPSCR_ = false;
1378 c_flag_FPSCR_ = false;
1379 v_flag_FPSCR_ = false;
1380 } else {
1381 // Case when (val1 > val2).
1382 n_flag_FPSCR_ = false;
1383 z_flag_FPSCR_ = false;
1384 c_flag_FPSCR_ = true;
1385 v_flag_FPSCR_ = false;
1386 }
1387}
1388
1389
1390void Simulator::Copy_FPSCR_to_APSR() {
1391 n_flag_ = n_flag_FPSCR_;
1392 z_flag_ = z_flag_FPSCR_;
1393 c_flag_ = c_flag_FPSCR_;
1394 v_flag_ = v_flag_FPSCR_;
1395}
1396
1397
Steve Blocka7e24c12009-10-30 11:49:00 +00001398// Addressing Mode 1 - Data-processing operands:
1399// Get the value based on the shifter_operand with register.
Steve Block1e0659c2011-05-24 12:43:12 +01001400int32_t Simulator::GetShiftRm(Instruction* instr, bool* carry_out) {
1401 ShiftOp shift = instr->ShiftField();
1402 int shift_amount = instr->ShiftAmountValue();
1403 int32_t result = get_register(instr->RmValue());
Steve Blocka7e24c12009-10-30 11:49:00 +00001404 if (instr->Bit(4) == 0) {
1405 // by immediate
1406 if ((shift == ROR) && (shift_amount == 0)) {
1407 UNIMPLEMENTED();
1408 return result;
1409 } else if (((shift == LSR) || (shift == ASR)) && (shift_amount == 0)) {
1410 shift_amount = 32;
1411 }
1412 switch (shift) {
1413 case ASR: {
1414 if (shift_amount == 0) {
1415 if (result < 0) {
1416 result = 0xffffffff;
1417 *carry_out = true;
1418 } else {
1419 result = 0;
1420 *carry_out = false;
1421 }
1422 } else {
1423 result >>= (shift_amount - 1);
1424 *carry_out = (result & 1) == 1;
1425 result >>= 1;
1426 }
1427 break;
1428 }
1429
1430 case LSL: {
1431 if (shift_amount == 0) {
1432 *carry_out = c_flag_;
1433 } else {
1434 result <<= (shift_amount - 1);
1435 *carry_out = (result < 0);
1436 result <<= 1;
1437 }
1438 break;
1439 }
1440
1441 case LSR: {
1442 if (shift_amount == 0) {
1443 result = 0;
1444 *carry_out = c_flag_;
1445 } else {
1446 uint32_t uresult = static_cast<uint32_t>(result);
1447 uresult >>= (shift_amount - 1);
1448 *carry_out = (uresult & 1) == 1;
1449 uresult >>= 1;
1450 result = static_cast<int32_t>(uresult);
1451 }
1452 break;
1453 }
1454
1455 case ROR: {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001456 if (shift_amount == 0) {
1457 *carry_out = c_flag_;
1458 } else {
1459 uint32_t left = static_cast<uint32_t>(result) >> shift_amount;
1460 uint32_t right = static_cast<uint32_t>(result) << (32 - shift_amount);
1461 result = right | left;
1462 *carry_out = (static_cast<uint32_t>(result) >> 31) != 0;
1463 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001464 break;
1465 }
1466
1467 default: {
1468 UNREACHABLE();
1469 break;
1470 }
1471 }
1472 } else {
1473 // by register
Steve Block1e0659c2011-05-24 12:43:12 +01001474 int rs = instr->RsValue();
Steve Blocka7e24c12009-10-30 11:49:00 +00001475 shift_amount = get_register(rs) &0xff;
1476 switch (shift) {
1477 case ASR: {
1478 if (shift_amount == 0) {
1479 *carry_out = c_flag_;
1480 } else if (shift_amount < 32) {
1481 result >>= (shift_amount - 1);
1482 *carry_out = (result & 1) == 1;
1483 result >>= 1;
1484 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001485 DCHECK(shift_amount >= 32);
Steve Blocka7e24c12009-10-30 11:49:00 +00001486 if (result < 0) {
1487 *carry_out = true;
1488 result = 0xffffffff;
1489 } else {
1490 *carry_out = false;
1491 result = 0;
1492 }
1493 }
1494 break;
1495 }
1496
1497 case LSL: {
1498 if (shift_amount == 0) {
1499 *carry_out = c_flag_;
1500 } else if (shift_amount < 32) {
1501 result <<= (shift_amount - 1);
1502 *carry_out = (result < 0);
1503 result <<= 1;
1504 } else if (shift_amount == 32) {
1505 *carry_out = (result & 1) == 1;
1506 result = 0;
1507 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001508 DCHECK(shift_amount > 32);
Steve Blocka7e24c12009-10-30 11:49:00 +00001509 *carry_out = false;
1510 result = 0;
1511 }
1512 break;
1513 }
1514
1515 case LSR: {
1516 if (shift_amount == 0) {
1517 *carry_out = c_flag_;
1518 } else if (shift_amount < 32) {
1519 uint32_t uresult = static_cast<uint32_t>(result);
1520 uresult >>= (shift_amount - 1);
1521 *carry_out = (uresult & 1) == 1;
1522 uresult >>= 1;
1523 result = static_cast<int32_t>(uresult);
1524 } else if (shift_amount == 32) {
1525 *carry_out = (result < 0);
1526 result = 0;
1527 } else {
1528 *carry_out = false;
1529 result = 0;
1530 }
1531 break;
1532 }
1533
1534 case ROR: {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001535 if (shift_amount == 0) {
1536 *carry_out = c_flag_;
1537 } else {
1538 uint32_t left = static_cast<uint32_t>(result) >> shift_amount;
1539 uint32_t right = static_cast<uint32_t>(result) << (32 - shift_amount);
1540 result = right | left;
1541 *carry_out = (static_cast<uint32_t>(result) >> 31) != 0;
1542 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001543 break;
1544 }
1545
1546 default: {
1547 UNREACHABLE();
1548 break;
1549 }
1550 }
1551 }
1552 return result;
1553}
1554
1555
1556// Addressing Mode 1 - Data-processing operands:
1557// Get the value based on the shifter_operand with immediate.
Steve Block1e0659c2011-05-24 12:43:12 +01001558int32_t Simulator::GetImm(Instruction* instr, bool* carry_out) {
1559 int rotate = instr->RotateValue() * 2;
1560 int immed8 = instr->Immed8Value();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001561 int imm = base::bits::RotateRight32(immed8, rotate);
Steve Blocka7e24c12009-10-30 11:49:00 +00001562 *carry_out = (rotate == 0) ? c_flag_ : (imm < 0);
1563 return imm;
1564}
1565
1566
1567static int count_bits(int bit_vector) {
1568 int count = 0;
1569 while (bit_vector != 0) {
1570 if ((bit_vector & 1) != 0) {
1571 count++;
1572 }
1573 bit_vector >>= 1;
1574 }
1575 return count;
1576}
1577
1578
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001579int32_t Simulator::ProcessPU(Instruction* instr,
1580 int num_regs,
1581 int reg_size,
1582 intptr_t* start_address,
1583 intptr_t* end_address) {
Steve Block1e0659c2011-05-24 12:43:12 +01001584 int rn = instr->RnValue();
Steve Blocka7e24c12009-10-30 11:49:00 +00001585 int32_t rn_val = get_register(rn);
Steve Blocka7e24c12009-10-30 11:49:00 +00001586 switch (instr->PUField()) {
Steve Block1e0659c2011-05-24 12:43:12 +01001587 case da_x: {
Steve Blocka7e24c12009-10-30 11:49:00 +00001588 UNIMPLEMENTED();
1589 break;
1590 }
Steve Block1e0659c2011-05-24 12:43:12 +01001591 case ia_x: {
Ben Murdoch8b112d22011-06-08 16:22:53 +01001592 *start_address = rn_val;
1593 *end_address = rn_val + (num_regs * reg_size) - reg_size;
1594 rn_val = rn_val + (num_regs * reg_size);
Steve Blocka7e24c12009-10-30 11:49:00 +00001595 break;
1596 }
Steve Block1e0659c2011-05-24 12:43:12 +01001597 case db_x: {
Ben Murdoch8b112d22011-06-08 16:22:53 +01001598 *start_address = rn_val - (num_regs * reg_size);
1599 *end_address = rn_val - reg_size;
1600 rn_val = *start_address;
Steve Blocka7e24c12009-10-30 11:49:00 +00001601 break;
1602 }
Steve Block1e0659c2011-05-24 12:43:12 +01001603 case ib_x: {
Ben Murdoch8b112d22011-06-08 16:22:53 +01001604 *start_address = rn_val + reg_size;
1605 *end_address = rn_val + (num_regs * reg_size);
1606 rn_val = *end_address;
Steve Blocka7e24c12009-10-30 11:49:00 +00001607 break;
1608 }
1609 default: {
1610 UNREACHABLE();
1611 break;
1612 }
1613 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001614 return rn_val;
Ben Murdoch8b112d22011-06-08 16:22:53 +01001615}
1616
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001617
Ben Murdoch8b112d22011-06-08 16:22:53 +01001618// Addressing Mode 4 - Load and Store Multiple
1619void Simulator::HandleRList(Instruction* instr, bool load) {
1620 int rlist = instr->RlistValue();
1621 int num_regs = count_bits(rlist);
1622
1623 intptr_t start_address = 0;
1624 intptr_t end_address = 0;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001625 int32_t rn_val =
1626 ProcessPU(instr, num_regs, kPointerSize, &start_address, &end_address);
Ben Murdoch8b112d22011-06-08 16:22:53 +01001627
Steve Blocka7e24c12009-10-30 11:49:00 +00001628 intptr_t* address = reinterpret_cast<intptr_t*>(start_address);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001629 // Catch null pointers a little earlier.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001630 DCHECK(start_address > 8191 || start_address < 0);
Steve Blocka7e24c12009-10-30 11:49:00 +00001631 int reg = 0;
1632 while (rlist != 0) {
1633 if ((rlist & 1) != 0) {
1634 if (load) {
1635 set_register(reg, *address);
1636 } else {
1637 *address = get_register(reg);
1638 }
1639 address += 1;
1640 }
1641 reg++;
1642 rlist >>= 1;
1643 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001644 DCHECK(end_address == ((intptr_t)address) - 4);
1645 if (instr->HasW()) {
1646 set_register(instr->RnValue(), rn_val);
1647 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001648}
1649
1650
Ben Murdoch8b112d22011-06-08 16:22:53 +01001651// Addressing Mode 6 - Load and Store Multiple Coprocessor registers.
1652void Simulator::HandleVList(Instruction* instr) {
1653 VFPRegPrecision precision =
1654 (instr->SzValue() == 0) ? kSinglePrecision : kDoublePrecision;
1655 int operand_size = (precision == kSinglePrecision) ? 4 : 8;
1656
1657 bool load = (instr->VLValue() == 0x1);
1658
1659 int vd;
1660 int num_regs;
1661 vd = instr->VFPDRegValue(precision);
1662 if (precision == kSinglePrecision) {
1663 num_regs = instr->Immed8Value();
1664 } else {
1665 num_regs = instr->Immed8Value() / 2;
1666 }
1667
1668 intptr_t start_address = 0;
1669 intptr_t end_address = 0;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001670 int32_t rn_val =
1671 ProcessPU(instr, num_regs, operand_size, &start_address, &end_address);
Ben Murdoch8b112d22011-06-08 16:22:53 +01001672
1673 intptr_t* address = reinterpret_cast<intptr_t*>(start_address);
1674 for (int reg = vd; reg < vd + num_regs; reg++) {
1675 if (precision == kSinglePrecision) {
1676 if (load) {
1677 set_s_register_from_sinteger(
1678 reg, ReadW(reinterpret_cast<int32_t>(address), instr));
1679 } else {
1680 WriteW(reinterpret_cast<int32_t>(address),
1681 get_sinteger_from_s_register(reg), instr);
1682 }
1683 address += 1;
1684 } else {
1685 if (load) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001686 int32_t data[] = {
1687 ReadW(reinterpret_cast<int32_t>(address), instr),
1688 ReadW(reinterpret_cast<int32_t>(address + 1), instr)
1689 };
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001690 set_d_register(reg, reinterpret_cast<uint32_t*>(data));
Ben Murdoch8b112d22011-06-08 16:22:53 +01001691 } else {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001692 uint32_t data[2];
1693 get_d_register(reg, data);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001694 WriteW(reinterpret_cast<int32_t>(address), data[0], instr);
1695 WriteW(reinterpret_cast<int32_t>(address + 1), data[1], instr);
Ben Murdoch8b112d22011-06-08 16:22:53 +01001696 }
1697 address += 2;
1698 }
1699 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001700 DCHECK(reinterpret_cast<intptr_t>(address) - operand_size == end_address);
1701 if (instr->HasW()) {
1702 set_register(instr->RnValue(), rn_val);
1703 }
Ben Murdoch8b112d22011-06-08 16:22:53 +01001704}
1705
1706
Steve Blocka7e24c12009-10-30 11:49:00 +00001707// Calls into the V8 runtime are based on this very simple interface.
1708// Note: To be able to return two values from some calls the code in runtime.cc
1709// uses the ObjectPair which is essentially two 32-bit values stuffed into a
1710// 64-bit value. With the code below we assume that all runtime calls return
1711// 64 bits of result. If they don't, the r1 result register contains a bogus
1712// value, which is fine because it is caller-saved.
1713typedef int64_t (*SimulatorRuntimeCall)(int32_t arg0,
1714 int32_t arg1,
1715 int32_t arg2,
Ben Murdochb0fe1622011-05-05 13:52:32 +01001716 int32_t arg3,
Steve Block44f0eee2011-05-26 01:26:41 +01001717 int32_t arg4,
1718 int32_t arg5);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001719
1720// These prototypes handle the four types of FP calls.
1721typedef int64_t (*SimulatorRuntimeCompareCall)(double darg0, double darg1);
1722typedef double (*SimulatorRuntimeFPFPCall)(double darg0, double darg1);
1723typedef double (*SimulatorRuntimeFPCall)(double darg0);
1724typedef double (*SimulatorRuntimeFPIntCall)(double darg0, int32_t arg0);
Steve Blocka7e24c12009-10-30 11:49:00 +00001725
Steve Block1e0659c2011-05-24 12:43:12 +01001726// This signature supports direct call in to API function native callback
1727// (refer to InvocationCallback in v8.h).
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001728typedef void (*SimulatorRuntimeDirectApiCall)(int32_t arg0);
1729typedef void (*SimulatorRuntimeProfilingApiCall)(int32_t arg0, void* arg1);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001730
1731// This signature supports direct call to accessor getter callback.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001732typedef void (*SimulatorRuntimeDirectGetterCall)(int32_t arg0, int32_t arg1);
1733typedef void (*SimulatorRuntimeProfilingGetterCall)(
1734 int32_t arg0, int32_t arg1, void* arg2);
Steve Blocka7e24c12009-10-30 11:49:00 +00001735
1736// Software interrupt instructions are used by the simulator to call into the
1737// C-based V8 runtime.
Steve Block1e0659c2011-05-24 12:43:12 +01001738void Simulator::SoftwareInterrupt(Instruction* instr) {
1739 int svc = instr->SvcValue();
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001740 switch (svc) {
Steve Block1e0659c2011-05-24 12:43:12 +01001741 case kCallRtRedirected: {
Steve Block6ded16b2010-05-10 14:33:55 +01001742 // Check if stack is aligned. Error if not aligned is reported below to
1743 // include information on the function called.
1744 bool stack_aligned =
1745 (get_register(sp)
1746 & (::v8::internal::FLAG_sim_stack_alignment - 1)) == 0;
Steve Blocka7e24c12009-10-30 11:49:00 +00001747 Redirection* redirection = Redirection::FromSwiInstruction(instr);
1748 int32_t arg0 = get_register(r0);
1749 int32_t arg1 = get_register(r1);
1750 int32_t arg2 = get_register(r2);
1751 int32_t arg3 = get_register(r3);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001752 int32_t* stack_pointer = reinterpret_cast<int32_t*>(get_register(sp));
Steve Block44f0eee2011-05-26 01:26:41 +01001753 int32_t arg4 = stack_pointer[0];
1754 int32_t arg5 = stack_pointer[1];
Ben Murdoch257744e2011-11-30 15:57:28 +00001755 bool fp_call =
1756 (redirection->type() == ExternalReference::BUILTIN_FP_FP_CALL) ||
1757 (redirection->type() == ExternalReference::BUILTIN_COMPARE_CALL) ||
1758 (redirection->type() == ExternalReference::BUILTIN_FP_CALL) ||
1759 (redirection->type() == ExternalReference::BUILTIN_FP_INT_CALL);
Steve Blocka7e24c12009-10-30 11:49:00 +00001760 // This is dodgy but it works because the C entry stubs are never moved.
1761 // See comment in codegen-arm.cc and bug 1242173.
1762 int32_t saved_lr = get_register(lr);
Steve Block1e0659c2011-05-24 12:43:12 +01001763 intptr_t external =
1764 reinterpret_cast<intptr_t>(redirection->external_function());
Ben Murdoch257744e2011-11-30 15:57:28 +00001765 if (fp_call) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001766 double dval0, dval1; // one or two double parameters
1767 int32_t ival; // zero or one integer parameters
1768 int64_t iresult = 0; // integer return value
1769 double dresult = 0; // double return value
1770 GetFpArgs(&dval0, &dval1, &ival);
Steve Block6ded16b2010-05-10 14:33:55 +01001771 if (::v8::internal::FLAG_trace_sim || !stack_aligned) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001772 SimulatorRuntimeCall generic_target =
1773 reinterpret_cast<SimulatorRuntimeCall>(external);
Ben Murdoch257744e2011-11-30 15:57:28 +00001774 switch (redirection->type()) {
1775 case ExternalReference::BUILTIN_FP_FP_CALL:
1776 case ExternalReference::BUILTIN_COMPARE_CALL:
Ben Murdoch257744e2011-11-30 15:57:28 +00001777 PrintF("Call to host function at %p with args %f, %f",
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001778 FUNCTION_ADDR(generic_target), dval0, dval1);
Ben Murdoch257744e2011-11-30 15:57:28 +00001779 break;
1780 case ExternalReference::BUILTIN_FP_CALL:
Ben Murdoch257744e2011-11-30 15:57:28 +00001781 PrintF("Call to host function at %p with arg %f",
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001782 FUNCTION_ADDR(generic_target), dval0);
Ben Murdoch257744e2011-11-30 15:57:28 +00001783 break;
1784 case ExternalReference::BUILTIN_FP_INT_CALL:
Ben Murdoch257744e2011-11-30 15:57:28 +00001785 PrintF("Call to host function at %p with args %f, %d",
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001786 FUNCTION_ADDR(generic_target), dval0, ival);
Ben Murdoch257744e2011-11-30 15:57:28 +00001787 break;
1788 default:
1789 UNREACHABLE();
1790 break;
1791 }
Steve Block6ded16b2010-05-10 14:33:55 +01001792 if (!stack_aligned) {
1793 PrintF(" with unaligned stack %08x\n", get_register(sp));
1794 }
1795 PrintF("\n");
Steve Blocka7e24c12009-10-30 11:49:00 +00001796 }
Steve Block6ded16b2010-05-10 14:33:55 +01001797 CHECK(stack_aligned);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001798 switch (redirection->type()) {
1799 case ExternalReference::BUILTIN_COMPARE_CALL: {
1800 SimulatorRuntimeCompareCall target =
1801 reinterpret_cast<SimulatorRuntimeCompareCall>(external);
1802 iresult = target(dval0, dval1);
1803 set_register(r0, static_cast<int32_t>(iresult));
1804 set_register(r1, static_cast<int32_t>(iresult >> 32));
1805 break;
1806 }
1807 case ExternalReference::BUILTIN_FP_FP_CALL: {
1808 SimulatorRuntimeFPFPCall target =
1809 reinterpret_cast<SimulatorRuntimeFPFPCall>(external);
1810 dresult = target(dval0, dval1);
1811 SetFpResult(dresult);
1812 break;
1813 }
1814 case ExternalReference::BUILTIN_FP_CALL: {
Ben Murdoch257744e2011-11-30 15:57:28 +00001815 SimulatorRuntimeFPCall target =
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001816 reinterpret_cast<SimulatorRuntimeFPCall>(external);
1817 dresult = target(dval0);
1818 SetFpResult(dresult);
1819 break;
1820 }
1821 case ExternalReference::BUILTIN_FP_INT_CALL: {
1822 SimulatorRuntimeFPIntCall target =
1823 reinterpret_cast<SimulatorRuntimeFPIntCall>(external);
1824 dresult = target(dval0, ival);
1825 SetFpResult(dresult);
1826 break;
1827 }
1828 default:
1829 UNREACHABLE();
1830 break;
1831 }
1832 if (::v8::internal::FLAG_trace_sim || !stack_aligned) {
1833 switch (redirection->type()) {
1834 case ExternalReference::BUILTIN_COMPARE_CALL:
1835 PrintF("Returned %08x\n", static_cast<int32_t>(iresult));
1836 break;
1837 case ExternalReference::BUILTIN_FP_FP_CALL:
1838 case ExternalReference::BUILTIN_FP_CALL:
1839 case ExternalReference::BUILTIN_FP_INT_CALL:
1840 PrintF("Returned %f\n", dresult);
1841 break;
1842 default:
1843 UNREACHABLE();
1844 break;
Ben Murdoch257744e2011-11-30 15:57:28 +00001845 }
Ben Murdoch257744e2011-11-30 15:57:28 +00001846 }
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001847 } else if (redirection->type() == ExternalReference::DIRECT_API_CALL) {
Steve Block1e0659c2011-05-24 12:43:12 +01001848 if (::v8::internal::FLAG_trace_sim || !stack_aligned) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001849 PrintF("Call to host function at %p args %08x",
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001850 reinterpret_cast<void*>(external), arg0);
Steve Block1e0659c2011-05-24 12:43:12 +01001851 if (!stack_aligned) {
1852 PrintF(" with unaligned stack %08x\n", get_register(sp));
1853 }
1854 PrintF("\n");
1855 }
1856 CHECK(stack_aligned);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001857 SimulatorRuntimeDirectApiCall target =
1858 reinterpret_cast<SimulatorRuntimeDirectApiCall>(external);
1859 target(arg0);
1860 } else if (
1861 redirection->type() == ExternalReference::PROFILING_API_CALL) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001862 if (::v8::internal::FLAG_trace_sim || !stack_aligned) {
1863 PrintF("Call to host function at %p args %08x %08x",
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001864 reinterpret_cast<void*>(external), arg0, arg1);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001865 if (!stack_aligned) {
1866 PrintF(" with unaligned stack %08x\n", get_register(sp));
1867 }
1868 PrintF("\n");
1869 }
1870 CHECK(stack_aligned);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001871 SimulatorRuntimeProfilingApiCall target =
1872 reinterpret_cast<SimulatorRuntimeProfilingApiCall>(external);
1873 target(arg0, Redirection::ReverseRedirection(arg1));
1874 } else if (
1875 redirection->type() == ExternalReference::DIRECT_GETTER_CALL) {
1876 if (::v8::internal::FLAG_trace_sim || !stack_aligned) {
1877 PrintF("Call to host function at %p args %08x %08x",
1878 reinterpret_cast<void*>(external), arg0, arg1);
1879 if (!stack_aligned) {
1880 PrintF(" with unaligned stack %08x\n", get_register(sp));
1881 }
1882 PrintF("\n");
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001883 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001884 CHECK(stack_aligned);
1885 SimulatorRuntimeDirectGetterCall target =
1886 reinterpret_cast<SimulatorRuntimeDirectGetterCall>(external);
1887 target(arg0, arg1);
1888 } else if (
1889 redirection->type() == ExternalReference::PROFILING_GETTER_CALL) {
1890 if (::v8::internal::FLAG_trace_sim || !stack_aligned) {
1891 PrintF("Call to host function at %p args %08x %08x %08x",
1892 reinterpret_cast<void*>(external), arg0, arg1, arg2);
1893 if (!stack_aligned) {
1894 PrintF(" with unaligned stack %08x\n", get_register(sp));
1895 }
1896 PrintF("\n");
1897 }
1898 CHECK(stack_aligned);
1899 SimulatorRuntimeProfilingGetterCall target =
1900 reinterpret_cast<SimulatorRuntimeProfilingGetterCall>(
1901 external);
1902 target(arg0, arg1, Redirection::ReverseRedirection(arg2));
Steve Blocka7e24c12009-10-30 11:49:00 +00001903 } else {
Steve Block1e0659c2011-05-24 12:43:12 +01001904 // builtin call.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001905 DCHECK(redirection->type() == ExternalReference::BUILTIN_CALL);
Steve Blocka7e24c12009-10-30 11:49:00 +00001906 SimulatorRuntimeCall target =
1907 reinterpret_cast<SimulatorRuntimeCall>(external);
Steve Block6ded16b2010-05-10 14:33:55 +01001908 if (::v8::internal::FLAG_trace_sim || !stack_aligned) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001909 PrintF(
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001910 "Call to host function at %p "
Steve Block44f0eee2011-05-26 01:26:41 +01001911 "args %08x, %08x, %08x, %08x, %08x, %08x",
Steve Blocka7e24c12009-10-30 11:49:00 +00001912 FUNCTION_ADDR(target),
1913 arg0,
1914 arg1,
1915 arg2,
Ben Murdochb0fe1622011-05-05 13:52:32 +01001916 arg3,
Steve Block44f0eee2011-05-26 01:26:41 +01001917 arg4,
1918 arg5);
Steve Block6ded16b2010-05-10 14:33:55 +01001919 if (!stack_aligned) {
1920 PrintF(" with unaligned stack %08x\n", get_register(sp));
1921 }
1922 PrintF("\n");
Steve Blocka7e24c12009-10-30 11:49:00 +00001923 }
Steve Block6ded16b2010-05-10 14:33:55 +01001924 CHECK(stack_aligned);
Steve Block44f0eee2011-05-26 01:26:41 +01001925 int64_t result = target(arg0, arg1, arg2, arg3, arg4, arg5);
Steve Blocka7e24c12009-10-30 11:49:00 +00001926 int32_t lo_res = static_cast<int32_t>(result);
1927 int32_t hi_res = static_cast<int32_t>(result >> 32);
1928 if (::v8::internal::FLAG_trace_sim) {
1929 PrintF("Returned %08x\n", lo_res);
1930 }
1931 set_register(r0, lo_res);
1932 set_register(r1, hi_res);
1933 }
1934 set_register(lr, saved_lr);
1935 set_pc(get_register(lr));
1936 break;
1937 }
Steve Block1e0659c2011-05-24 12:43:12 +01001938 case kBreakpoint: {
Steve Block44f0eee2011-05-26 01:26:41 +01001939 ArmDebugger dbg(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001940 dbg.Debug();
1941 break;
1942 }
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001943 // stop uses all codes greater than 1 << 23.
Steve Blocka7e24c12009-10-30 11:49:00 +00001944 default: {
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001945 if (svc >= (1 << 23)) {
1946 uint32_t code = svc & kStopCodeMask;
1947 if (isWatchedStop(code)) {
1948 IncreaseStopCounter(code);
1949 }
1950 // Stop if it is enabled, otherwise go on jumping over the stop
1951 // and the message address.
1952 if (isEnabledStop(code)) {
Steve Block44f0eee2011-05-26 01:26:41 +01001953 ArmDebugger dbg(this);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001954 dbg.Stop(instr);
1955 } else {
Steve Block1e0659c2011-05-24 12:43:12 +01001956 set_pc(get_pc() + 2 * Instruction::kInstrSize);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001957 }
1958 } else {
1959 // This is not a valid svc code.
1960 UNREACHABLE();
1961 break;
1962 }
1963 }
1964 }
1965}
1966
1967
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001968float Simulator::canonicalizeNaN(float value) {
1969 // Default NaN value, see "NaN handling" in "IEEE 754 standard implementation
1970 // choices" of the ARM Reference Manual.
1971 const uint32_t kDefaultNaN = 0x7FC00000u;
1972 if (FPSCR_default_NaN_mode_ && std::isnan(value)) {
1973 value = bit_cast<float>(kDefaultNaN);
1974 }
1975 return value;
1976}
1977
1978
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001979double Simulator::canonicalizeNaN(double value) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001980 // Default NaN value, see "NaN handling" in "IEEE 754 standard implementation
1981 // choices" of the ARM Reference Manual.
1982 const uint64_t kDefaultNaN = V8_UINT64_C(0x7FF8000000000000);
1983 if (FPSCR_default_NaN_mode_ && std::isnan(value)) {
1984 value = bit_cast<double>(kDefaultNaN);
1985 }
1986 return value;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001987}
1988
1989
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001990// Stop helper functions.
Steve Block1e0659c2011-05-24 12:43:12 +01001991bool Simulator::isStopInstruction(Instruction* instr) {
1992 return (instr->Bits(27, 24) == 0xF) && (instr->SvcValue() >= kStopCode);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001993}
1994
1995
1996bool Simulator::isWatchedStop(uint32_t code) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001997 DCHECK(code <= kMaxStopCode);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001998 return code < kNumOfWatchedStops;
1999}
2000
2001
2002bool Simulator::isEnabledStop(uint32_t code) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002003 DCHECK(code <= kMaxStopCode);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08002004 // Unwatched stops are always enabled.
2005 return !isWatchedStop(code) ||
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002006 !(watched_stops_[code].count & kStopDisabledBit);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08002007}
2008
2009
2010void Simulator::EnableStop(uint32_t code) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002011 DCHECK(isWatchedStop(code));
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08002012 if (!isEnabledStop(code)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002013 watched_stops_[code].count &= ~kStopDisabledBit;
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08002014 }
2015}
2016
2017
2018void Simulator::DisableStop(uint32_t code) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002019 DCHECK(isWatchedStop(code));
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08002020 if (isEnabledStop(code)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002021 watched_stops_[code].count |= kStopDisabledBit;
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08002022 }
2023}
2024
2025
2026void Simulator::IncreaseStopCounter(uint32_t code) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002027 DCHECK(code <= kMaxStopCode);
2028 DCHECK(isWatchedStop(code));
2029 if ((watched_stops_[code].count & ~(1 << 31)) == 0x7fffffff) {
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08002030 PrintF("Stop counter for code %i has overflowed.\n"
2031 "Enabling this code and reseting the counter to 0.\n", code);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002032 watched_stops_[code].count = 0;
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08002033 EnableStop(code);
2034 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002035 watched_stops_[code].count++;
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08002036 }
2037}
2038
2039
2040// Print a stop status.
2041void Simulator::PrintStopInfo(uint32_t code) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002042 DCHECK(code <= kMaxStopCode);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08002043 if (!isWatchedStop(code)) {
2044 PrintF("Stop not watched.");
2045 } else {
2046 const char* state = isEnabledStop(code) ? "Enabled" : "Disabled";
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002047 int32_t count = watched_stops_[code].count & ~kStopDisabledBit;
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08002048 // Don't print the state of unused breakpoints.
2049 if (count != 0) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002050 if (watched_stops_[code].desc) {
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08002051 PrintF("stop %i - 0x%x: \t%s, \tcounter = %i, \t%s\n",
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002052 code, code, state, count, watched_stops_[code].desc);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08002053 } else {
2054 PrintF("stop %i - 0x%x: \t%s, \tcounter = %i\n",
2055 code, code, state, count);
2056 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002057 }
2058 }
2059}
2060
2061
2062// Handle execution based on instruction types.
2063
2064// Instruction types 0 and 1 are both rolled into one function because they
2065// only differ in the handling of the shifter_operand.
Steve Block1e0659c2011-05-24 12:43:12 +01002066void Simulator::DecodeType01(Instruction* instr) {
2067 int type = instr->TypeValue();
Steve Blocka7e24c12009-10-30 11:49:00 +00002068 if ((type == 0) && instr->IsSpecialType0()) {
2069 // multiply instruction or extra loads and stores
2070 if (instr->Bits(7, 4) == 9) {
2071 if (instr->Bit(24) == 0) {
2072 // Raw field decoding here. Multiply instructions have their Rd in
2073 // funny places.
Steve Block1e0659c2011-05-24 12:43:12 +01002074 int rn = instr->RnValue();
2075 int rm = instr->RmValue();
2076 int rs = instr->RsValue();
Steve Blocka7e24c12009-10-30 11:49:00 +00002077 int32_t rs_val = get_register(rs);
2078 int32_t rm_val = get_register(rm);
2079 if (instr->Bit(23) == 0) {
2080 if (instr->Bit(21) == 0) {
2081 // The MUL instruction description (A 4.1.33) refers to Rd as being
2082 // the destination for the operation, but it confusingly uses the
2083 // Rn field to encode it.
2084 // Format(instr, "mul'cond's 'rn, 'rm, 'rs");
2085 int rd = rn; // Remap the rn field to the Rd register.
2086 int32_t alu_out = rm_val * rs_val;
2087 set_register(rd, alu_out);
2088 if (instr->HasS()) {
2089 SetNZFlags(alu_out);
2090 }
2091 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002092 int rd = instr->RdValue();
2093 int32_t acc_value = get_register(rd);
2094 if (instr->Bit(22) == 0) {
2095 // The MLA instruction description (A 4.1.28) refers to the order
2096 // of registers as "Rd, Rm, Rs, Rn". But confusingly it uses the
2097 // Rn field to encode the Rd register and the Rd field to encode
2098 // the Rn register.
2099 // Format(instr, "mla'cond's 'rn, 'rm, 'rs, 'rd");
2100 int32_t mul_out = rm_val * rs_val;
2101 int32_t result = acc_value + mul_out;
2102 set_register(rn, result);
2103 } else {
2104 // Format(instr, "mls'cond's 'rn, 'rm, 'rs, 'rd");
2105 int32_t mul_out = rm_val * rs_val;
2106 int32_t result = acc_value - mul_out;
2107 set_register(rn, result);
2108 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002109 }
2110 } else {
2111 // The signed/long multiply instructions use the terms RdHi and RdLo
2112 // when referring to the target registers. They are mapped to the Rn
2113 // and Rd fields as follows:
2114 // RdLo == Rd
2115 // RdHi == Rn (This is confusingly stored in variable rd here
2116 // because the mul instruction from above uses the
2117 // Rn field to encode the Rd register. Good luck figuring
2118 // this out without reading the ARM instruction manual
2119 // at a very detailed level.)
2120 // Format(instr, "'um'al'cond's 'rd, 'rn, 'rs, 'rm");
2121 int rd_hi = rn; // Remap the rn field to the RdHi register.
Steve Block1e0659c2011-05-24 12:43:12 +01002122 int rd_lo = instr->RdValue();
Steve Blocka7e24c12009-10-30 11:49:00 +00002123 int32_t hi_res = 0;
2124 int32_t lo_res = 0;
2125 if (instr->Bit(22) == 1) {
2126 int64_t left_op = static_cast<int32_t>(rm_val);
2127 int64_t right_op = static_cast<int32_t>(rs_val);
2128 uint64_t result = left_op * right_op;
2129 hi_res = static_cast<int32_t>(result >> 32);
2130 lo_res = static_cast<int32_t>(result & 0xffffffff);
2131 } else {
2132 // unsigned multiply
2133 uint64_t left_op = static_cast<uint32_t>(rm_val);
2134 uint64_t right_op = static_cast<uint32_t>(rs_val);
2135 uint64_t result = left_op * right_op;
2136 hi_res = static_cast<int32_t>(result >> 32);
2137 lo_res = static_cast<int32_t>(result & 0xffffffff);
2138 }
2139 set_register(rd_lo, lo_res);
2140 set_register(rd_hi, hi_res);
2141 if (instr->HasS()) {
2142 UNIMPLEMENTED();
2143 }
2144 }
2145 } else {
Steve Blockd0582a62009-12-15 09:54:21 +00002146 UNIMPLEMENTED(); // Not used by V8.
Steve Blocka7e24c12009-10-30 11:49:00 +00002147 }
2148 } else {
2149 // extra load/store instructions
Steve Block1e0659c2011-05-24 12:43:12 +01002150 int rd = instr->RdValue();
2151 int rn = instr->RnValue();
Steve Blocka7e24c12009-10-30 11:49:00 +00002152 int32_t rn_val = get_register(rn);
2153 int32_t addr = 0;
2154 if (instr->Bit(22) == 0) {
Steve Block1e0659c2011-05-24 12:43:12 +01002155 int rm = instr->RmValue();
Steve Blocka7e24c12009-10-30 11:49:00 +00002156 int32_t rm_val = get_register(rm);
2157 switch (instr->PUField()) {
Steve Block1e0659c2011-05-24 12:43:12 +01002158 case da_x: {
Steve Blocka7e24c12009-10-30 11:49:00 +00002159 // Format(instr, "'memop'cond'sign'h 'rd, ['rn], -'rm");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002160 DCHECK(!instr->HasW());
Steve Blocka7e24c12009-10-30 11:49:00 +00002161 addr = rn_val;
2162 rn_val -= rm_val;
2163 set_register(rn, rn_val);
2164 break;
2165 }
Steve Block1e0659c2011-05-24 12:43:12 +01002166 case ia_x: {
Steve Blocka7e24c12009-10-30 11:49:00 +00002167 // Format(instr, "'memop'cond'sign'h 'rd, ['rn], +'rm");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002168 DCHECK(!instr->HasW());
Steve Blocka7e24c12009-10-30 11:49:00 +00002169 addr = rn_val;
2170 rn_val += rm_val;
2171 set_register(rn, rn_val);
2172 break;
2173 }
Steve Block1e0659c2011-05-24 12:43:12 +01002174 case db_x: {
Steve Blocka7e24c12009-10-30 11:49:00 +00002175 // Format(instr, "'memop'cond'sign'h 'rd, ['rn, -'rm]'w");
2176 rn_val -= rm_val;
2177 addr = rn_val;
2178 if (instr->HasW()) {
2179 set_register(rn, rn_val);
2180 }
2181 break;
2182 }
Steve Block1e0659c2011-05-24 12:43:12 +01002183 case ib_x: {
Steve Blocka7e24c12009-10-30 11:49:00 +00002184 // Format(instr, "'memop'cond'sign'h 'rd, ['rn, +'rm]'w");
2185 rn_val += rm_val;
2186 addr = rn_val;
2187 if (instr->HasW()) {
2188 set_register(rn, rn_val);
2189 }
2190 break;
2191 }
2192 default: {
2193 // The PU field is a 2-bit field.
2194 UNREACHABLE();
2195 break;
2196 }
2197 }
2198 } else {
Steve Block1e0659c2011-05-24 12:43:12 +01002199 int32_t imm_val = (instr->ImmedHValue() << 4) | instr->ImmedLValue();
Steve Blocka7e24c12009-10-30 11:49:00 +00002200 switch (instr->PUField()) {
Steve Block1e0659c2011-05-24 12:43:12 +01002201 case da_x: {
Steve Blocka7e24c12009-10-30 11:49:00 +00002202 // Format(instr, "'memop'cond'sign'h 'rd, ['rn], #-'off8");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002203 DCHECK(!instr->HasW());
Steve Blocka7e24c12009-10-30 11:49:00 +00002204 addr = rn_val;
2205 rn_val -= imm_val;
2206 set_register(rn, rn_val);
2207 break;
2208 }
Steve Block1e0659c2011-05-24 12:43:12 +01002209 case ia_x: {
Steve Blocka7e24c12009-10-30 11:49:00 +00002210 // Format(instr, "'memop'cond'sign'h 'rd, ['rn], #+'off8");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002211 DCHECK(!instr->HasW());
Steve Blocka7e24c12009-10-30 11:49:00 +00002212 addr = rn_val;
2213 rn_val += imm_val;
2214 set_register(rn, rn_val);
2215 break;
2216 }
Steve Block1e0659c2011-05-24 12:43:12 +01002217 case db_x: {
Steve Blocka7e24c12009-10-30 11:49:00 +00002218 // Format(instr, "'memop'cond'sign'h 'rd, ['rn, #-'off8]'w");
2219 rn_val -= imm_val;
2220 addr = rn_val;
2221 if (instr->HasW()) {
2222 set_register(rn, rn_val);
2223 }
2224 break;
2225 }
Steve Block1e0659c2011-05-24 12:43:12 +01002226 case ib_x: {
Steve Blocka7e24c12009-10-30 11:49:00 +00002227 // Format(instr, "'memop'cond'sign'h 'rd, ['rn, #+'off8]'w");
2228 rn_val += imm_val;
2229 addr = rn_val;
2230 if (instr->HasW()) {
2231 set_register(rn, rn_val);
2232 }
2233 break;
2234 }
2235 default: {
2236 // The PU field is a 2-bit field.
2237 UNREACHABLE();
2238 break;
2239 }
2240 }
2241 }
Kristian Monsen25f61362010-05-21 11:50:48 +01002242 if (((instr->Bits(7, 4) & 0xd) == 0xd) && (instr->Bit(20) == 0)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002243 DCHECK((rd % 2) == 0);
Kristian Monsen25f61362010-05-21 11:50:48 +01002244 if (instr->HasH()) {
2245 // The strd instruction.
2246 int32_t value1 = get_register(rd);
2247 int32_t value2 = get_register(rd+1);
2248 WriteDW(addr, value1, value2);
2249 } else {
2250 // The ldrd instruction.
2251 int* rn_data = ReadDW(addr);
2252 set_dw_register(rd, rn_data);
2253 }
2254 } else if (instr->HasH()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002255 if (instr->HasSign()) {
2256 if (instr->HasL()) {
2257 int16_t val = ReadH(addr, instr);
2258 set_register(rd, val);
2259 } else {
2260 int16_t val = get_register(rd);
2261 WriteH(addr, val, instr);
2262 }
2263 } else {
2264 if (instr->HasL()) {
2265 uint16_t val = ReadHU(addr, instr);
2266 set_register(rd, val);
2267 } else {
2268 uint16_t val = get_register(rd);
2269 WriteH(addr, val, instr);
2270 }
2271 }
2272 } else {
2273 // signed byte loads
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002274 DCHECK(instr->HasSign());
2275 DCHECK(instr->HasL());
Steve Blocka7e24c12009-10-30 11:49:00 +00002276 int8_t val = ReadB(addr);
2277 set_register(rd, val);
2278 }
2279 return;
2280 }
Steve Block6ded16b2010-05-10 14:33:55 +01002281 } else if ((type == 0) && instr->IsMiscType0()) {
2282 if (instr->Bits(22, 21) == 1) {
Steve Block1e0659c2011-05-24 12:43:12 +01002283 int rm = instr->RmValue();
2284 switch (instr->BitField(7, 4)) {
Steve Block6ded16b2010-05-10 14:33:55 +01002285 case BX:
2286 set_pc(get_register(rm));
2287 break;
2288 case BLX: {
2289 uint32_t old_pc = get_pc();
2290 set_pc(get_register(rm));
Steve Block1e0659c2011-05-24 12:43:12 +01002291 set_register(lr, old_pc + Instruction::kInstrSize);
Steve Block6ded16b2010-05-10 14:33:55 +01002292 break;
2293 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01002294 case BKPT: {
Steve Block44f0eee2011-05-26 01:26:41 +01002295 ArmDebugger dbg(this);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002296 PrintF("Simulator hit BKPT.\n");
2297 dbg.Debug();
Steve Block6ded16b2010-05-10 14:33:55 +01002298 break;
Ben Murdochb0fe1622011-05-05 13:52:32 +01002299 }
Steve Block6ded16b2010-05-10 14:33:55 +01002300 default:
2301 UNIMPLEMENTED();
2302 }
2303 } else if (instr->Bits(22, 21) == 3) {
Steve Block1e0659c2011-05-24 12:43:12 +01002304 int rm = instr->RmValue();
2305 int rd = instr->RdValue();
2306 switch (instr->BitField(7, 4)) {
Steve Block6ded16b2010-05-10 14:33:55 +01002307 case CLZ: {
2308 uint32_t bits = get_register(rm);
2309 int leading_zeros = 0;
2310 if (bits == 0) {
2311 leading_zeros = 32;
2312 } else {
2313 while ((bits & 0x80000000u) == 0) {
2314 bits <<= 1;
2315 leading_zeros++;
2316 }
2317 }
2318 set_register(rd, leading_zeros);
2319 break;
2320 }
2321 default:
2322 UNIMPLEMENTED();
2323 }
2324 } else {
2325 PrintF("%08x\n", instr->InstructionBits());
2326 UNIMPLEMENTED();
2327 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002328 } else if ((type == 1) && instr->IsNopType1()) {
2329 // NOP.
Steve Blocka7e24c12009-10-30 11:49:00 +00002330 } else {
Steve Block1e0659c2011-05-24 12:43:12 +01002331 int rd = instr->RdValue();
2332 int rn = instr->RnValue();
Steve Blocka7e24c12009-10-30 11:49:00 +00002333 int32_t rn_val = get_register(rn);
2334 int32_t shifter_operand = 0;
2335 bool shifter_carry_out = 0;
2336 if (type == 0) {
2337 shifter_operand = GetShiftRm(instr, &shifter_carry_out);
2338 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002339 DCHECK(instr->TypeValue() == 1);
Steve Blocka7e24c12009-10-30 11:49:00 +00002340 shifter_operand = GetImm(instr, &shifter_carry_out);
2341 }
2342 int32_t alu_out;
2343
2344 switch (instr->OpcodeField()) {
2345 case AND: {
2346 // Format(instr, "and'cond's 'rd, 'rn, 'shift_rm");
2347 // Format(instr, "and'cond's 'rd, 'rn, 'imm");
2348 alu_out = rn_val & shifter_operand;
2349 set_register(rd, alu_out);
2350 if (instr->HasS()) {
2351 SetNZFlags(alu_out);
2352 SetCFlag(shifter_carry_out);
2353 }
2354 break;
2355 }
2356
2357 case EOR: {
2358 // Format(instr, "eor'cond's 'rd, 'rn, 'shift_rm");
2359 // Format(instr, "eor'cond's 'rd, 'rn, 'imm");
2360 alu_out = rn_val ^ shifter_operand;
2361 set_register(rd, alu_out);
2362 if (instr->HasS()) {
2363 SetNZFlags(alu_out);
2364 SetCFlag(shifter_carry_out);
2365 }
2366 break;
2367 }
2368
2369 case SUB: {
2370 // Format(instr, "sub'cond's 'rd, 'rn, 'shift_rm");
2371 // Format(instr, "sub'cond's 'rd, 'rn, 'imm");
2372 alu_out = rn_val - shifter_operand;
2373 set_register(rd, alu_out);
2374 if (instr->HasS()) {
2375 SetNZFlags(alu_out);
2376 SetCFlag(!BorrowFrom(rn_val, shifter_operand));
2377 SetVFlag(OverflowFrom(alu_out, rn_val, shifter_operand, false));
2378 }
2379 break;
2380 }
2381
2382 case RSB: {
2383 // Format(instr, "rsb'cond's 'rd, 'rn, 'shift_rm");
2384 // Format(instr, "rsb'cond's 'rd, 'rn, 'imm");
2385 alu_out = shifter_operand - rn_val;
2386 set_register(rd, alu_out);
2387 if (instr->HasS()) {
2388 SetNZFlags(alu_out);
2389 SetCFlag(!BorrowFrom(shifter_operand, rn_val));
2390 SetVFlag(OverflowFrom(alu_out, shifter_operand, rn_val, false));
2391 }
2392 break;
2393 }
2394
2395 case ADD: {
2396 // Format(instr, "add'cond's 'rd, 'rn, 'shift_rm");
2397 // Format(instr, "add'cond's 'rd, 'rn, 'imm");
2398 alu_out = rn_val + shifter_operand;
2399 set_register(rd, alu_out);
2400 if (instr->HasS()) {
2401 SetNZFlags(alu_out);
2402 SetCFlag(CarryFrom(rn_val, shifter_operand));
2403 SetVFlag(OverflowFrom(alu_out, rn_val, shifter_operand, true));
2404 }
2405 break;
2406 }
2407
2408 case ADC: {
Ben Murdoch257744e2011-11-30 15:57:28 +00002409 // Format(instr, "adc'cond's 'rd, 'rn, 'shift_rm");
2410 // Format(instr, "adc'cond's 'rd, 'rn, 'imm");
2411 alu_out = rn_val + shifter_operand + GetCarry();
2412 set_register(rd, alu_out);
2413 if (instr->HasS()) {
2414 SetNZFlags(alu_out);
2415 SetCFlag(CarryFrom(rn_val, shifter_operand, GetCarry()));
2416 SetVFlag(OverflowFrom(alu_out, rn_val, shifter_operand, true));
2417 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002418 break;
2419 }
2420
2421 case SBC: {
2422 Format(instr, "sbc'cond's 'rd, 'rn, 'shift_rm");
2423 Format(instr, "sbc'cond's 'rd, 'rn, 'imm");
2424 break;
2425 }
2426
2427 case RSC: {
2428 Format(instr, "rsc'cond's 'rd, 'rn, 'shift_rm");
2429 Format(instr, "rsc'cond's 'rd, 'rn, 'imm");
2430 break;
2431 }
2432
2433 case TST: {
2434 if (instr->HasS()) {
2435 // Format(instr, "tst'cond 'rn, 'shift_rm");
2436 // Format(instr, "tst'cond 'rn, 'imm");
2437 alu_out = rn_val & shifter_operand;
2438 SetNZFlags(alu_out);
2439 SetCFlag(shifter_carry_out);
2440 } else {
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01002441 // Format(instr, "movw'cond 'rd, 'imm").
Steve Block1e0659c2011-05-24 12:43:12 +01002442 alu_out = instr->ImmedMovwMovtValue();
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01002443 set_register(rd, alu_out);
Steve Blocka7e24c12009-10-30 11:49:00 +00002444 }
2445 break;
2446 }
2447
2448 case TEQ: {
2449 if (instr->HasS()) {
2450 // Format(instr, "teq'cond 'rn, 'shift_rm");
2451 // Format(instr, "teq'cond 'rn, 'imm");
2452 alu_out = rn_val ^ shifter_operand;
2453 SetNZFlags(alu_out);
2454 SetCFlag(shifter_carry_out);
2455 } else {
Steve Block6ded16b2010-05-10 14:33:55 +01002456 // Other instructions matching this pattern are handled in the
2457 // miscellaneous instructions part above.
2458 UNREACHABLE();
Steve Blocka7e24c12009-10-30 11:49:00 +00002459 }
2460 break;
2461 }
2462
2463 case CMP: {
2464 if (instr->HasS()) {
2465 // Format(instr, "cmp'cond 'rn, 'shift_rm");
2466 // Format(instr, "cmp'cond 'rn, 'imm");
2467 alu_out = rn_val - shifter_operand;
2468 SetNZFlags(alu_out);
2469 SetCFlag(!BorrowFrom(rn_val, shifter_operand));
2470 SetVFlag(OverflowFrom(alu_out, rn_val, shifter_operand, false));
2471 } else {
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01002472 // Format(instr, "movt'cond 'rd, 'imm").
2473 alu_out = (get_register(rd) & 0xffff) |
Steve Block1e0659c2011-05-24 12:43:12 +01002474 (instr->ImmedMovwMovtValue() << 16);
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01002475 set_register(rd, alu_out);
Steve Blocka7e24c12009-10-30 11:49:00 +00002476 }
2477 break;
2478 }
2479
2480 case CMN: {
2481 if (instr->HasS()) {
2482 // Format(instr, "cmn'cond 'rn, 'shift_rm");
2483 // Format(instr, "cmn'cond 'rn, 'imm");
2484 alu_out = rn_val + shifter_operand;
2485 SetNZFlags(alu_out);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002486 SetCFlag(CarryFrom(rn_val, shifter_operand));
Steve Blocka7e24c12009-10-30 11:49:00 +00002487 SetVFlag(OverflowFrom(alu_out, rn_val, shifter_operand, true));
2488 } else {
Steve Block6ded16b2010-05-10 14:33:55 +01002489 // Other instructions matching this pattern are handled in the
2490 // miscellaneous instructions part above.
2491 UNREACHABLE();
Steve Blocka7e24c12009-10-30 11:49:00 +00002492 }
2493 break;
2494 }
2495
2496 case ORR: {
2497 // Format(instr, "orr'cond's 'rd, 'rn, 'shift_rm");
2498 // Format(instr, "orr'cond's 'rd, 'rn, 'imm");
2499 alu_out = rn_val | shifter_operand;
2500 set_register(rd, alu_out);
2501 if (instr->HasS()) {
2502 SetNZFlags(alu_out);
2503 SetCFlag(shifter_carry_out);
2504 }
2505 break;
2506 }
2507
2508 case MOV: {
2509 // Format(instr, "mov'cond's 'rd, 'shift_rm");
2510 // Format(instr, "mov'cond's 'rd, 'imm");
2511 alu_out = shifter_operand;
2512 set_register(rd, alu_out);
2513 if (instr->HasS()) {
2514 SetNZFlags(alu_out);
2515 SetCFlag(shifter_carry_out);
2516 }
2517 break;
2518 }
2519
2520 case BIC: {
2521 // Format(instr, "bic'cond's 'rd, 'rn, 'shift_rm");
2522 // Format(instr, "bic'cond's 'rd, 'rn, 'imm");
2523 alu_out = rn_val & ~shifter_operand;
2524 set_register(rd, alu_out);
2525 if (instr->HasS()) {
2526 SetNZFlags(alu_out);
2527 SetCFlag(shifter_carry_out);
2528 }
2529 break;
2530 }
2531
2532 case MVN: {
2533 // Format(instr, "mvn'cond's 'rd, 'shift_rm");
2534 // Format(instr, "mvn'cond's 'rd, 'imm");
2535 alu_out = ~shifter_operand;
2536 set_register(rd, alu_out);
2537 if (instr->HasS()) {
2538 SetNZFlags(alu_out);
2539 SetCFlag(shifter_carry_out);
2540 }
2541 break;
2542 }
2543
2544 default: {
2545 UNREACHABLE();
2546 break;
2547 }
2548 }
2549 }
2550}
2551
2552
Steve Block1e0659c2011-05-24 12:43:12 +01002553void Simulator::DecodeType2(Instruction* instr) {
2554 int rd = instr->RdValue();
2555 int rn = instr->RnValue();
Steve Blocka7e24c12009-10-30 11:49:00 +00002556 int32_t rn_val = get_register(rn);
Steve Block1e0659c2011-05-24 12:43:12 +01002557 int32_t im_val = instr->Offset12Value();
Steve Blocka7e24c12009-10-30 11:49:00 +00002558 int32_t addr = 0;
2559 switch (instr->PUField()) {
Steve Block1e0659c2011-05-24 12:43:12 +01002560 case da_x: {
Steve Blocka7e24c12009-10-30 11:49:00 +00002561 // Format(instr, "'memop'cond'b 'rd, ['rn], #-'off12");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002562 DCHECK(!instr->HasW());
Steve Blocka7e24c12009-10-30 11:49:00 +00002563 addr = rn_val;
2564 rn_val -= im_val;
2565 set_register(rn, rn_val);
2566 break;
2567 }
Steve Block1e0659c2011-05-24 12:43:12 +01002568 case ia_x: {
Steve Blocka7e24c12009-10-30 11:49:00 +00002569 // Format(instr, "'memop'cond'b 'rd, ['rn], #+'off12");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002570 DCHECK(!instr->HasW());
Steve Blocka7e24c12009-10-30 11:49:00 +00002571 addr = rn_val;
2572 rn_val += im_val;
2573 set_register(rn, rn_val);
2574 break;
2575 }
Steve Block1e0659c2011-05-24 12:43:12 +01002576 case db_x: {
Steve Blocka7e24c12009-10-30 11:49:00 +00002577 // Format(instr, "'memop'cond'b 'rd, ['rn, #-'off12]'w");
2578 rn_val -= im_val;
2579 addr = rn_val;
2580 if (instr->HasW()) {
2581 set_register(rn, rn_val);
2582 }
2583 break;
2584 }
Steve Block1e0659c2011-05-24 12:43:12 +01002585 case ib_x: {
Steve Blocka7e24c12009-10-30 11:49:00 +00002586 // Format(instr, "'memop'cond'b 'rd, ['rn, #+'off12]'w");
2587 rn_val += im_val;
2588 addr = rn_val;
2589 if (instr->HasW()) {
2590 set_register(rn, rn_val);
2591 }
2592 break;
2593 }
2594 default: {
2595 UNREACHABLE();
2596 break;
2597 }
2598 }
2599 if (instr->HasB()) {
2600 if (instr->HasL()) {
2601 byte val = ReadBU(addr);
2602 set_register(rd, val);
2603 } else {
2604 byte val = get_register(rd);
2605 WriteB(addr, val);
2606 }
2607 } else {
2608 if (instr->HasL()) {
2609 set_register(rd, ReadW(addr, instr));
2610 } else {
2611 WriteW(addr, get_register(rd), instr);
2612 }
2613 }
2614}
2615
2616
Steve Block1e0659c2011-05-24 12:43:12 +01002617void Simulator::DecodeType3(Instruction* instr) {
2618 int rd = instr->RdValue();
2619 int rn = instr->RnValue();
Steve Blocka7e24c12009-10-30 11:49:00 +00002620 int32_t rn_val = get_register(rn);
2621 bool shifter_carry_out = 0;
2622 int32_t shifter_operand = GetShiftRm(instr, &shifter_carry_out);
2623 int32_t addr = 0;
2624 switch (instr->PUField()) {
Steve Block1e0659c2011-05-24 12:43:12 +01002625 case da_x: {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002626 DCHECK(!instr->HasW());
Steve Blocka7e24c12009-10-30 11:49:00 +00002627 Format(instr, "'memop'cond'b 'rd, ['rn], -'shift_rm");
Kristian Monsen50ef84f2010-07-29 15:18:00 +01002628 UNIMPLEMENTED();
Steve Blocka7e24c12009-10-30 11:49:00 +00002629 break;
2630 }
Steve Block1e0659c2011-05-24 12:43:12 +01002631 case ia_x: {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002632 if (instr->Bit(4) == 0) {
2633 // Memop.
2634 } else {
2635 if (instr->Bit(5) == 0) {
2636 switch (instr->Bits(22, 21)) {
2637 case 0:
2638 if (instr->Bit(20) == 0) {
2639 if (instr->Bit(6) == 0) {
2640 // Pkhbt.
2641 uint32_t rn_val = get_register(rn);
2642 uint32_t rm_val = get_register(instr->RmValue());
2643 int32_t shift = instr->Bits(11, 7);
2644 rm_val <<= shift;
2645 set_register(rd, (rn_val & 0xFFFF) | (rm_val & 0xFFFF0000U));
2646 } else {
2647 // Pkhtb.
2648 uint32_t rn_val = get_register(rn);
2649 int32_t rm_val = get_register(instr->RmValue());
2650 int32_t shift = instr->Bits(11, 7);
2651 if (shift == 0) {
2652 shift = 32;
2653 }
2654 rm_val >>= shift;
2655 set_register(rd, (rn_val & 0xFFFF0000U) | (rm_val & 0xFFFF));
2656 }
2657 } else {
2658 UNIMPLEMENTED();
2659 }
2660 break;
2661 case 1:
2662 UNIMPLEMENTED();
2663 break;
2664 case 2:
2665 UNIMPLEMENTED();
2666 break;
2667 case 3: {
2668 // Usat.
2669 int32_t sat_pos = instr->Bits(20, 16);
2670 int32_t sat_val = (1 << sat_pos) - 1;
2671 int32_t shift = instr->Bits(11, 7);
2672 int32_t shift_type = instr->Bit(6);
2673 int32_t rm_val = get_register(instr->RmValue());
2674 if (shift_type == 0) { // LSL
2675 rm_val <<= shift;
2676 } else { // ASR
2677 rm_val >>= shift;
2678 }
2679 // If saturation occurs, the Q flag should be set in the CPSR.
2680 // There is no Q flag yet, and no instruction (MRS) to read the
2681 // CPSR directly.
2682 if (rm_val > sat_val) {
2683 rm_val = sat_val;
2684 } else if (rm_val < 0) {
2685 rm_val = 0;
2686 }
2687 set_register(rd, rm_val);
2688 break;
2689 }
Kristian Monsen50ef84f2010-07-29 15:18:00 +01002690 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002691 } else {
2692 switch (instr->Bits(22, 21)) {
2693 case 0:
2694 UNIMPLEMENTED();
2695 break;
2696 case 1:
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002697 if (instr->Bits(9, 6) == 1) {
2698 if (instr->Bit(20) == 0) {
2699 if (instr->Bits(19, 16) == 0xF) {
2700 // Sxtb.
2701 int32_t rm_val = get_register(instr->RmValue());
2702 int32_t rotate = instr->Bits(11, 10);
2703 switch (rotate) {
2704 case 0:
2705 break;
2706 case 1:
2707 rm_val = (rm_val >> 8) | (rm_val << 24);
2708 break;
2709 case 2:
2710 rm_val = (rm_val >> 16) | (rm_val << 16);
2711 break;
2712 case 3:
2713 rm_val = (rm_val >> 24) | (rm_val << 8);
2714 break;
2715 }
2716 set_register(rd, static_cast<int8_t>(rm_val));
2717 } else {
2718 // Sxtab.
2719 int32_t rn_val = get_register(rn);
2720 int32_t rm_val = get_register(instr->RmValue());
2721 int32_t rotate = instr->Bits(11, 10);
2722 switch (rotate) {
2723 case 0:
2724 break;
2725 case 1:
2726 rm_val = (rm_val >> 8) | (rm_val << 24);
2727 break;
2728 case 2:
2729 rm_val = (rm_val >> 16) | (rm_val << 16);
2730 break;
2731 case 3:
2732 rm_val = (rm_val >> 24) | (rm_val << 8);
2733 break;
2734 }
2735 set_register(rd, rn_val + static_cast<int8_t>(rm_val));
2736 }
2737 } else {
2738 if (instr->Bits(19, 16) == 0xF) {
2739 // Sxth.
2740 int32_t rm_val = get_register(instr->RmValue());
2741 int32_t rotate = instr->Bits(11, 10);
2742 switch (rotate) {
2743 case 0:
2744 break;
2745 case 1:
2746 rm_val = (rm_val >> 8) | (rm_val << 24);
2747 break;
2748 case 2:
2749 rm_val = (rm_val >> 16) | (rm_val << 16);
2750 break;
2751 case 3:
2752 rm_val = (rm_val >> 24) | (rm_val << 8);
2753 break;
2754 }
2755 set_register(rd, static_cast<int16_t>(rm_val));
2756 } else {
2757 // Sxtah.
2758 int32_t rn_val = get_register(rn);
2759 int32_t rm_val = get_register(instr->RmValue());
2760 int32_t rotate = instr->Bits(11, 10);
2761 switch (rotate) {
2762 case 0:
2763 break;
2764 case 1:
2765 rm_val = (rm_val >> 8) | (rm_val << 24);
2766 break;
2767 case 2:
2768 rm_val = (rm_val >> 16) | (rm_val << 16);
2769 break;
2770 case 3:
2771 rm_val = (rm_val >> 24) | (rm_val << 8);
2772 break;
2773 }
2774 set_register(rd, rn_val + static_cast<int16_t>(rm_val));
2775 }
2776 }
2777 } else {
2778 UNREACHABLE();
2779 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002780 break;
2781 case 2:
2782 if ((instr->Bit(20) == 0) && (instr->Bits(9, 6) == 1)) {
2783 if (instr->Bits(19, 16) == 0xF) {
2784 // Uxtb16.
2785 uint32_t rm_val = get_register(instr->RmValue());
2786 int32_t rotate = instr->Bits(11, 10);
2787 switch (rotate) {
2788 case 0:
2789 break;
2790 case 1:
2791 rm_val = (rm_val >> 8) | (rm_val << 24);
2792 break;
2793 case 2:
2794 rm_val = (rm_val >> 16) | (rm_val << 16);
2795 break;
2796 case 3:
2797 rm_val = (rm_val >> 24) | (rm_val << 8);
2798 break;
2799 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002800 set_register(rd, (rm_val & 0xFF) | (rm_val & 0xFF0000));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002801 } else {
2802 UNIMPLEMENTED();
2803 }
2804 } else {
2805 UNIMPLEMENTED();
2806 }
2807 break;
2808 case 3:
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002809 if ((instr->Bits(9, 6) == 1)) {
2810 if (instr->Bit(20) == 0) {
2811 if (instr->Bits(19, 16) == 0xF) {
2812 // Uxtb.
2813 uint32_t rm_val = get_register(instr->RmValue());
2814 int32_t rotate = instr->Bits(11, 10);
2815 switch (rotate) {
2816 case 0:
2817 break;
2818 case 1:
2819 rm_val = (rm_val >> 8) | (rm_val << 24);
2820 break;
2821 case 2:
2822 rm_val = (rm_val >> 16) | (rm_val << 16);
2823 break;
2824 case 3:
2825 rm_val = (rm_val >> 24) | (rm_val << 8);
2826 break;
2827 }
2828 set_register(rd, (rm_val & 0xFF));
2829 } else {
2830 // Uxtab.
2831 uint32_t rn_val = get_register(rn);
2832 uint32_t rm_val = get_register(instr->RmValue());
2833 int32_t rotate = instr->Bits(11, 10);
2834 switch (rotate) {
2835 case 0:
2836 break;
2837 case 1:
2838 rm_val = (rm_val >> 8) | (rm_val << 24);
2839 break;
2840 case 2:
2841 rm_val = (rm_val >> 16) | (rm_val << 16);
2842 break;
2843 case 3:
2844 rm_val = (rm_val >> 24) | (rm_val << 8);
2845 break;
2846 }
2847 set_register(rd, rn_val + (rm_val & 0xFF));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002848 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002849 } else {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002850 if (instr->Bits(19, 16) == 0xF) {
2851 // Uxth.
2852 uint32_t rm_val = get_register(instr->RmValue());
2853 int32_t rotate = instr->Bits(11, 10);
2854 switch (rotate) {
2855 case 0:
2856 break;
2857 case 1:
2858 rm_val = (rm_val >> 8) | (rm_val << 24);
2859 break;
2860 case 2:
2861 rm_val = (rm_val >> 16) | (rm_val << 16);
2862 break;
2863 case 3:
2864 rm_val = (rm_val >> 24) | (rm_val << 8);
2865 break;
2866 }
2867 set_register(rd, (rm_val & 0xFFFF));
2868 } else {
2869 // Uxtah.
2870 uint32_t rn_val = get_register(rn);
2871 uint32_t rm_val = get_register(instr->RmValue());
2872 int32_t rotate = instr->Bits(11, 10);
2873 switch (rotate) {
2874 case 0:
2875 break;
2876 case 1:
2877 rm_val = (rm_val >> 8) | (rm_val << 24);
2878 break;
2879 case 2:
2880 rm_val = (rm_val >> 16) | (rm_val << 16);
2881 break;
2882 case 3:
2883 rm_val = (rm_val >> 24) | (rm_val << 8);
2884 break;
2885 }
2886 set_register(rd, rn_val + (rm_val & 0xFFFF));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002887 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002888 }
2889 } else {
2890 UNIMPLEMENTED();
2891 }
2892 break;
Kristian Monsen50ef84f2010-07-29 15:18:00 +01002893 }
Kristian Monsen50ef84f2010-07-29 15:18:00 +01002894 }
2895 return;
Kristian Monsen50ef84f2010-07-29 15:18:00 +01002896 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002897 break;
2898 }
Steve Block1e0659c2011-05-24 12:43:12 +01002899 case db_x: {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002900 if (instr->Bits(22, 20) == 0x5) {
2901 if (instr->Bits(7, 4) == 0x1) {
2902 int rm = instr->RmValue();
2903 int32_t rm_val = get_register(rm);
2904 int rs = instr->RsValue();
2905 int32_t rs_val = get_register(rs);
2906 if (instr->Bits(15, 12) == 0xF) {
2907 // SMMUL (in V8 notation matching ARM ISA format)
2908 // Format(instr, "smmul'cond 'rn, 'rm, 'rs");
2909 rn_val = base::bits::SignedMulHigh32(rm_val, rs_val);
2910 } else {
2911 // SMMLA (in V8 notation matching ARM ISA format)
2912 // Format(instr, "smmla'cond 'rn, 'rm, 'rs, 'rd");
2913 int rd = instr->RdValue();
2914 int32_t rd_val = get_register(rd);
2915 rn_val = base::bits::SignedMulHighAndAdd32(rm_val, rs_val, rd_val);
2916 }
2917 set_register(rn, rn_val);
2918 return;
2919 }
2920 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002921 if (FLAG_enable_sudiv) {
2922 if (instr->Bits(5, 4) == 0x1) {
2923 if ((instr->Bit(22) == 0x0) && (instr->Bit(20) == 0x1)) {
2924 // (s/u)div (in V8 notation matching ARM ISA format) rn = rm/rs
2925 // Format(instr, "'(s/u)div'cond'b 'rn, 'rm, 'rs);
2926 int rm = instr->RmValue();
2927 int32_t rm_val = get_register(rm);
2928 int rs = instr->RsValue();
2929 int32_t rs_val = get_register(rs);
2930 int32_t ret_val = 0;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002931 // udiv
2932 if (instr->Bit(21) == 0x1) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002933 ret_val = bit_cast<int32_t>(base::bits::UnsignedDiv32(
2934 bit_cast<uint32_t>(rm_val), bit_cast<uint32_t>(rs_val)));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002935 } else {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002936 ret_val = base::bits::SignedDiv32(rm_val, rs_val);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002937 }
2938 set_register(rn, ret_val);
2939 return;
2940 }
2941 }
2942 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002943 // Format(instr, "'memop'cond'b 'rd, ['rn, -'shift_rm]'w");
2944 addr = rn_val - shifter_operand;
2945 if (instr->HasW()) {
2946 set_register(rn, addr);
2947 }
2948 break;
2949 }
Steve Block1e0659c2011-05-24 12:43:12 +01002950 case ib_x: {
Andrei Popescu31002712010-02-23 13:46:05 +00002951 if (instr->HasW() && (instr->Bits(6, 4) == 0x5)) {
2952 uint32_t widthminus1 = static_cast<uint32_t>(instr->Bits(20, 16));
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002953 uint32_t lsbit = static_cast<uint32_t>(instr->Bits(11, 7));
Andrei Popescu31002712010-02-23 13:46:05 +00002954 uint32_t msbit = widthminus1 + lsbit;
2955 if (msbit <= 31) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002956 if (instr->Bit(22)) {
2957 // ubfx - unsigned bitfield extract.
2958 uint32_t rm_val =
Steve Block1e0659c2011-05-24 12:43:12 +01002959 static_cast<uint32_t>(get_register(instr->RmValue()));
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002960 uint32_t extr_val = rm_val << (31 - msbit);
2961 extr_val = extr_val >> (31 - widthminus1);
Steve Block1e0659c2011-05-24 12:43:12 +01002962 set_register(instr->RdValue(), extr_val);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002963 } else {
2964 // sbfx - signed bitfield extract.
Steve Block1e0659c2011-05-24 12:43:12 +01002965 int32_t rm_val = get_register(instr->RmValue());
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002966 int32_t extr_val = rm_val << (31 - msbit);
2967 extr_val = extr_val >> (31 - widthminus1);
Steve Block1e0659c2011-05-24 12:43:12 +01002968 set_register(instr->RdValue(), extr_val);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002969 }
2970 } else {
2971 UNREACHABLE();
2972 }
2973 return;
2974 } else if (!instr->HasW() && (instr->Bits(6, 4) == 0x1)) {
2975 uint32_t lsbit = static_cast<uint32_t>(instr->Bits(11, 7));
2976 uint32_t msbit = static_cast<uint32_t>(instr->Bits(20, 16));
2977 if (msbit >= lsbit) {
2978 // bfc or bfi - bitfield clear/insert.
2979 uint32_t rd_val =
Steve Block1e0659c2011-05-24 12:43:12 +01002980 static_cast<uint32_t>(get_register(instr->RdValue()));
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002981 uint32_t bitcount = msbit - lsbit + 1;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002982 uint32_t mask = 0xffffffffu >> (32 - bitcount);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002983 rd_val &= ~(mask << lsbit);
Steve Block1e0659c2011-05-24 12:43:12 +01002984 if (instr->RmValue() != 15) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002985 // bfi - bitfield insert.
2986 uint32_t rm_val =
Steve Block1e0659c2011-05-24 12:43:12 +01002987 static_cast<uint32_t>(get_register(instr->RmValue()));
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002988 rm_val &= mask;
2989 rd_val |= rm_val << lsbit;
2990 }
Steve Block1e0659c2011-05-24 12:43:12 +01002991 set_register(instr->RdValue(), rd_val);
Andrei Popescu31002712010-02-23 13:46:05 +00002992 } else {
2993 UNREACHABLE();
2994 }
2995 return;
2996 } else {
2997 // Format(instr, "'memop'cond'b 'rd, ['rn, +'shift_rm]'w");
2998 addr = rn_val + shifter_operand;
2999 if (instr->HasW()) {
3000 set_register(rn, addr);
3001 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003002 }
3003 break;
3004 }
3005 default: {
3006 UNREACHABLE();
3007 break;
3008 }
3009 }
3010 if (instr->HasB()) {
3011 if (instr->HasL()) {
3012 uint8_t byte = ReadB(addr);
3013 set_register(rd, byte);
3014 } else {
Andrei Popescu31002712010-02-23 13:46:05 +00003015 uint8_t byte = get_register(rd);
3016 WriteB(addr, byte);
Steve Blocka7e24c12009-10-30 11:49:00 +00003017 }
3018 } else {
3019 if (instr->HasL()) {
3020 set_register(rd, ReadW(addr, instr));
3021 } else {
3022 WriteW(addr, get_register(rd), instr);
3023 }
3024 }
3025}
3026
3027
Steve Block1e0659c2011-05-24 12:43:12 +01003028void Simulator::DecodeType4(Instruction* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003029 DCHECK(instr->Bit(22) == 0); // only allowed to be set in privileged mode
Steve Blocka7e24c12009-10-30 11:49:00 +00003030 if (instr->HasL()) {
3031 // Format(instr, "ldm'cond'pu 'rn'w, 'rlist");
3032 HandleRList(instr, true);
3033 } else {
3034 // Format(instr, "stm'cond'pu 'rn'w, 'rlist");
3035 HandleRList(instr, false);
3036 }
3037}
3038
3039
Steve Block1e0659c2011-05-24 12:43:12 +01003040void Simulator::DecodeType5(Instruction* instr) {
Steve Blocka7e24c12009-10-30 11:49:00 +00003041 // Format(instr, "b'l'cond 'target");
Steve Block1e0659c2011-05-24 12:43:12 +01003042 int off = (instr->SImmed24Value() << 2);
Steve Blocka7e24c12009-10-30 11:49:00 +00003043 intptr_t pc_address = get_pc();
3044 if (instr->HasLink()) {
Steve Block1e0659c2011-05-24 12:43:12 +01003045 set_register(lr, pc_address + Instruction::kInstrSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00003046 }
3047 int pc_reg = get_register(pc);
3048 set_pc(pc_reg + off);
3049}
3050
3051
Steve Block1e0659c2011-05-24 12:43:12 +01003052void Simulator::DecodeType6(Instruction* instr) {
Steve Blockd0582a62009-12-15 09:54:21 +00003053 DecodeType6CoprocessorIns(instr);
Steve Blocka7e24c12009-10-30 11:49:00 +00003054}
3055
3056
Steve Block1e0659c2011-05-24 12:43:12 +01003057void Simulator::DecodeType7(Instruction* instr) {
Steve Blocka7e24c12009-10-30 11:49:00 +00003058 if (instr->Bit(24) == 1) {
Steve Blocka7e24c12009-10-30 11:49:00 +00003059 SoftwareInterrupt(instr);
3060 } else {
Steve Blockd0582a62009-12-15 09:54:21 +00003061 DecodeTypeVFP(instr);
Steve Blocka7e24c12009-10-30 11:49:00 +00003062 }
3063}
3064
3065
Steve Block1e0659c2011-05-24 12:43:12 +01003066// void Simulator::DecodeTypeVFP(Instruction* instr)
Steve Blockd0582a62009-12-15 09:54:21 +00003067// The Following ARMv7 VFPv instructions are currently supported.
Leon Clarkee46be812010-01-19 14:06:41 +00003068// vmov :Sn = Rt
3069// vmov :Rt = Sn
3070// vcvt: Dd = Sm
3071// vcvt: Sd = Dm
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003072// vcvt.f64.s32 Dd, Dd, #<fbits>
Steve Block44f0eee2011-05-26 01:26:41 +01003073// Dd = vabs(Dm)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003074// Sd = vabs(Sm)
Steve Block44f0eee2011-05-26 01:26:41 +01003075// Dd = vneg(Dm)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003076// Sd = vneg(Sm)
Leon Clarkee46be812010-01-19 14:06:41 +00003077// Dd = vadd(Dn, Dm)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003078// Sd = vadd(Sn, Sm)
Leon Clarkee46be812010-01-19 14:06:41 +00003079// Dd = vsub(Dn, Dm)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003080// Sd = vsub(Sn, Sm)
Leon Clarkee46be812010-01-19 14:06:41 +00003081// Dd = vmul(Dn, Dm)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003082// Sd = vmul(Sn, Sm)
Leon Clarkee46be812010-01-19 14:06:41 +00003083// Dd = vdiv(Dn, Dm)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003084// Sd = vdiv(Sn, Sm)
Steve Blockd0582a62009-12-15 09:54:21 +00003085// vcmp(Dd, Dm)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003086// vcmp(Sd, Sm)
Steve Block8defd9f2010-07-08 12:39:36 +01003087// Dd = vsqrt(Dm)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003088// Sd = vsqrt(Sm)
3089// vmrs
Steve Block1e0659c2011-05-24 12:43:12 +01003090void Simulator::DecodeTypeVFP(Instruction* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003091 DCHECK((instr->TypeValue() == 7) && (instr->Bit(24) == 0x0) );
3092 DCHECK(instr->Bits(11, 9) == 0x5);
Steve Blockd0582a62009-12-15 09:54:21 +00003093
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003094 // Obtain single precision register codes.
3095 int m = instr->VFPMRegValue(kSinglePrecision);
3096 int d = instr->VFPDRegValue(kSinglePrecision);
3097 int n = instr->VFPNRegValue(kSinglePrecision);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003098 // Obtain double precision register codes.
Steve Block1e0659c2011-05-24 12:43:12 +01003099 int vm = instr->VFPMRegValue(kDoublePrecision);
3100 int vd = instr->VFPDRegValue(kDoublePrecision);
3101 int vn = instr->VFPNRegValue(kDoublePrecision);
Steve Blockd0582a62009-12-15 09:54:21 +00003102
Steve Block6ded16b2010-05-10 14:33:55 +01003103 if (instr->Bit(4) == 0) {
Steve Block1e0659c2011-05-24 12:43:12 +01003104 if (instr->Opc1Value() == 0x7) {
Steve Block6ded16b2010-05-10 14:33:55 +01003105 // Other data processing instructions
Steve Block1e0659c2011-05-24 12:43:12 +01003106 if ((instr->Opc2Value() == 0x0) && (instr->Opc3Value() == 0x1)) {
Steve Block8defd9f2010-07-08 12:39:36 +01003107 // vmov register to register.
Steve Block1e0659c2011-05-24 12:43:12 +01003108 if (instr->SzValue() == 0x1) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003109 uint32_t data[2];
3110 get_d_register(vm, data);
3111 set_d_register(vd, data);
Steve Block8defd9f2010-07-08 12:39:36 +01003112 } else {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003113 set_s_register(d, get_s_register(m));
Steve Block8defd9f2010-07-08 12:39:36 +01003114 }
Steve Block1e0659c2011-05-24 12:43:12 +01003115 } else if ((instr->Opc2Value() == 0x0) && (instr->Opc3Value() == 0x3)) {
3116 // vabs
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003117 if (instr->SzValue() == 0x1) {
3118 double dm_value = get_double_from_d_register(vm);
3119 double dd_value = std::fabs(dm_value);
3120 dd_value = canonicalizeNaN(dd_value);
3121 set_d_register_from_double(vd, dd_value);
3122 } else {
3123 float sm_value = get_float_from_s_register(m);
3124 float sd_value = std::fabs(sm_value);
3125 sd_value = canonicalizeNaN(sd_value);
3126 set_s_register_from_float(d, sd_value);
3127 }
Steve Block44f0eee2011-05-26 01:26:41 +01003128 } else if ((instr->Opc2Value() == 0x1) && (instr->Opc3Value() == 0x1)) {
3129 // vneg
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003130 if (instr->SzValue() == 0x1) {
3131 double dm_value = get_double_from_d_register(vm);
3132 double dd_value = -dm_value;
3133 dd_value = canonicalizeNaN(dd_value);
3134 set_d_register_from_double(vd, dd_value);
3135 } else {
3136 float sm_value = get_float_from_s_register(m);
3137 float sd_value = -sm_value;
3138 sd_value = canonicalizeNaN(sd_value);
3139 set_s_register_from_float(d, sd_value);
3140 }
Steve Block1e0659c2011-05-24 12:43:12 +01003141 } else if ((instr->Opc2Value() == 0x7) && (instr->Opc3Value() == 0x3)) {
Steve Block6ded16b2010-05-10 14:33:55 +01003142 DecodeVCVTBetweenDoubleAndSingle(instr);
Steve Block1e0659c2011-05-24 12:43:12 +01003143 } else if ((instr->Opc2Value() == 0x8) && (instr->Opc3Value() & 0x1)) {
Steve Block6ded16b2010-05-10 14:33:55 +01003144 DecodeVCVTBetweenFloatingPointAndInteger(instr);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003145 } else if ((instr->Opc2Value() == 0xA) && (instr->Opc3Value() == 0x3) &&
3146 (instr->Bit(8) == 1)) {
3147 // vcvt.f64.s32 Dd, Dd, #<fbits>
3148 int fraction_bits = 32 - ((instr->Bits(3, 0) << 1) | instr->Bit(5));
3149 int fixed_value = get_sinteger_from_s_register(vd * 2);
3150 double divide = 1 << fraction_bits;
3151 set_d_register_from_double(vd, fixed_value / divide);
Steve Block1e0659c2011-05-24 12:43:12 +01003152 } else if (((instr->Opc2Value() >> 1) == 0x6) &&
3153 (instr->Opc3Value() & 0x1)) {
Steve Block6ded16b2010-05-10 14:33:55 +01003154 DecodeVCVTBetweenFloatingPointAndInteger(instr);
Steve Block1e0659c2011-05-24 12:43:12 +01003155 } else if (((instr->Opc2Value() == 0x4) || (instr->Opc2Value() == 0x5)) &&
3156 (instr->Opc3Value() & 0x1)) {
Steve Block6ded16b2010-05-10 14:33:55 +01003157 DecodeVCMP(instr);
Steve Block1e0659c2011-05-24 12:43:12 +01003158 } else if (((instr->Opc2Value() == 0x1)) && (instr->Opc3Value() == 0x3)) {
Steve Block8defd9f2010-07-08 12:39:36 +01003159 // vsqrt
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003160 lazily_initialize_fast_sqrt(isolate_);
3161 if (instr->SzValue() == 0x1) {
3162 double dm_value = get_double_from_d_register(vm);
3163 double dd_value = fast_sqrt(dm_value, isolate_);
3164 dd_value = canonicalizeNaN(dd_value);
3165 set_d_register_from_double(vd, dd_value);
3166 } else {
3167 float sm_value = get_float_from_s_register(m);
3168 float sd_value = fast_sqrt(sm_value, isolate_);
3169 sd_value = canonicalizeNaN(sd_value);
3170 set_s_register_from_float(d, sd_value);
3171 }
Steve Block1e0659c2011-05-24 12:43:12 +01003172 } else if (instr->Opc3Value() == 0x0) {
Ben Murdoch3bec4d22010-07-22 14:51:16 +01003173 // vmov immediate.
Steve Block1e0659c2011-05-24 12:43:12 +01003174 if (instr->SzValue() == 0x1) {
Ben Murdoch3bec4d22010-07-22 14:51:16 +01003175 set_d_register_from_double(vd, instr->DoubleImmedVmov());
3176 } else {
3177 UNREACHABLE(); // Not used by v8.
3178 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003179 } else if (((instr->Opc2Value() == 0x6)) && (instr->Opc3Value() == 0x3)) {
3180 // vrintz - truncate
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003181 if (instr->SzValue() == 0x1) {
3182 double dm_value = get_double_from_d_register(vm);
3183 double dd_value = trunc(dm_value);
3184 dd_value = canonicalizeNaN(dd_value);
3185 set_d_register_from_double(vd, dd_value);
3186 } else {
3187 float sm_value = get_float_from_s_register(m);
3188 float sd_value = truncf(sm_value);
3189 sd_value = canonicalizeNaN(sd_value);
3190 set_s_register_from_float(d, sd_value);
3191 }
Steve Block6ded16b2010-05-10 14:33:55 +01003192 } else {
3193 UNREACHABLE(); // Not used by V8.
3194 }
Steve Block1e0659c2011-05-24 12:43:12 +01003195 } else if (instr->Opc1Value() == 0x3) {
Steve Block1e0659c2011-05-24 12:43:12 +01003196 if (instr->Opc3Value() & 0x1) {
Steve Block6ded16b2010-05-10 14:33:55 +01003197 // vsub
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003198 if (instr->SzValue() == 0x1) {
3199 double dn_value = get_double_from_d_register(vn);
3200 double dm_value = get_double_from_d_register(vm);
3201 double dd_value = dn_value - dm_value;
3202 dd_value = canonicalizeNaN(dd_value);
3203 set_d_register_from_double(vd, dd_value);
3204 } else {
3205 float sn_value = get_float_from_s_register(n);
3206 float sm_value = get_float_from_s_register(m);
3207 float sd_value = sn_value - sm_value;
3208 sd_value = canonicalizeNaN(sd_value);
3209 set_s_register_from_float(d, sd_value);
3210 }
Steve Block6ded16b2010-05-10 14:33:55 +01003211 } else {
3212 // vadd
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003213 if (instr->SzValue() == 0x1) {
3214 double dn_value = get_double_from_d_register(vn);
3215 double dm_value = get_double_from_d_register(vm);
3216 double dd_value = dn_value + dm_value;
3217 dd_value = canonicalizeNaN(dd_value);
3218 set_d_register_from_double(vd, dd_value);
3219 } else {
3220 float sn_value = get_float_from_s_register(n);
3221 float sm_value = get_float_from_s_register(m);
3222 float sd_value = sn_value + sm_value;
3223 sd_value = canonicalizeNaN(sd_value);
3224 set_s_register_from_float(d, sd_value);
3225 }
Steve Block6ded16b2010-05-10 14:33:55 +01003226 }
Steve Block1e0659c2011-05-24 12:43:12 +01003227 } else if ((instr->Opc1Value() == 0x2) && !(instr->Opc3Value() & 0x1)) {
Steve Block6ded16b2010-05-10 14:33:55 +01003228 // vmul
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003229 if (instr->SzValue() == 0x1) {
3230 double dn_value = get_double_from_d_register(vn);
3231 double dm_value = get_double_from_d_register(vm);
3232 double dd_value = dn_value * dm_value;
3233 dd_value = canonicalizeNaN(dd_value);
3234 set_d_register_from_double(vd, dd_value);
3235 } else {
3236 float sn_value = get_float_from_s_register(n);
3237 float sm_value = get_float_from_s_register(m);
3238 float sd_value = sn_value * sm_value;
3239 sd_value = canonicalizeNaN(sd_value);
3240 set_s_register_from_float(d, sd_value);
Steve Block6ded16b2010-05-10 14:33:55 +01003241 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003242 } else if ((instr->Opc1Value() == 0x0)) {
3243 // vmla, vmls
3244 const bool is_vmls = (instr->Opc3Value() & 0x1);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003245 if (instr->SzValue() == 0x1) {
3246 const double dd_val = get_double_from_d_register(vd);
3247 const double dn_val = get_double_from_d_register(vn);
3248 const double dm_val = get_double_from_d_register(vm);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003249
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003250 // Note: we do the mul and add/sub in separate steps to avoid getting a
3251 // result with too high precision.
3252 set_d_register_from_double(vd, dn_val * dm_val);
3253 if (is_vmls) {
3254 set_d_register_from_double(
3255 vd, canonicalizeNaN(dd_val - get_double_from_d_register(vd)));
3256 } else {
3257 set_d_register_from_double(
3258 vd, canonicalizeNaN(dd_val + get_double_from_d_register(vd)));
3259 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003260 } else {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003261 const float sd_val = get_float_from_s_register(d);
3262 const float sn_val = get_float_from_s_register(n);
3263 const float sm_val = get_float_from_s_register(m);
3264
3265 // Note: we do the mul and add/sub in separate steps to avoid getting a
3266 // result with too high precision.
3267 set_s_register_from_float(d, sn_val * sm_val);
3268 if (is_vmls) {
3269 set_s_register_from_float(
3270 d, canonicalizeNaN(sd_val - get_float_from_s_register(d)));
3271 } else {
3272 set_s_register_from_float(
3273 d, canonicalizeNaN(sd_val + get_float_from_s_register(d)));
3274 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003275 }
Steve Block1e0659c2011-05-24 12:43:12 +01003276 } else if ((instr->Opc1Value() == 0x4) && !(instr->Opc3Value() & 0x1)) {
Steve Block6ded16b2010-05-10 14:33:55 +01003277 // vdiv
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003278 if (instr->SzValue() == 0x1) {
3279 double dn_value = get_double_from_d_register(vn);
3280 double dm_value = get_double_from_d_register(vm);
3281 double dd_value = dn_value / dm_value;
3282 div_zero_vfp_flag_ = (dm_value == 0);
3283 dd_value = canonicalizeNaN(dd_value);
3284 set_d_register_from_double(vd, dd_value);
3285 } else {
3286 float sn_value = get_float_from_s_register(n);
3287 float sm_value = get_float_from_s_register(m);
3288 float sd_value = sn_value / sm_value;
3289 div_zero_vfp_flag_ = (sm_value == 0);
3290 sd_value = canonicalizeNaN(sd_value);
3291 set_s_register_from_float(d, sd_value);
Steve Block6ded16b2010-05-10 14:33:55 +01003292 }
Steve Block6ded16b2010-05-10 14:33:55 +01003293 } else {
3294 UNIMPLEMENTED(); // Not used by V8.
3295 }
3296 } else {
Steve Block1e0659c2011-05-24 12:43:12 +01003297 if ((instr->VCValue() == 0x0) &&
3298 (instr->VAValue() == 0x0)) {
Steve Block6ded16b2010-05-10 14:33:55 +01003299 DecodeVMOVBetweenCoreAndSinglePrecisionRegisters(instr);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003300 } else if ((instr->VLValue() == 0x0) &&
3301 (instr->VCValue() == 0x1) &&
3302 (instr->Bit(23) == 0x0)) {
3303 // vmov (ARM core register to scalar)
3304 int vd = instr->Bits(19, 16) | (instr->Bit(7) << 4);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003305 uint32_t data[2];
3306 get_d_register(vd, data);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003307 data[instr->Bit(21)] = get_register(instr->RtValue());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003308 set_d_register(vd, data);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003309 } else if ((instr->VLValue() == 0x1) &&
3310 (instr->VCValue() == 0x1) &&
3311 (instr->Bit(23) == 0x0)) {
3312 // vmov (scalar to ARM core register)
3313 int vn = instr->Bits(19, 16) | (instr->Bit(7) << 4);
3314 double dn_value = get_double_from_d_register(vn);
3315 int32_t data[2];
3316 memcpy(data, &dn_value, 8);
3317 set_register(instr->RtValue(), data[instr->Bit(21)]);
Steve Block1e0659c2011-05-24 12:43:12 +01003318 } else if ((instr->VLValue() == 0x1) &&
3319 (instr->VCValue() == 0x0) &&
3320 (instr->VAValue() == 0x7) &&
Steve Block6ded16b2010-05-10 14:33:55 +01003321 (instr->Bits(19, 16) == 0x1)) {
3322 // vmrs
Steve Block1e0659c2011-05-24 12:43:12 +01003323 uint32_t rt = instr->RtValue();
Russell Brenner90bac252010-11-18 13:33:46 -08003324 if (rt == 0xF) {
Steve Blockd0582a62009-12-15 09:54:21 +00003325 Copy_FPSCR_to_APSR();
Russell Brenner90bac252010-11-18 13:33:46 -08003326 } else {
3327 // Emulate FPSCR from the Simulator flags.
3328 uint32_t fpscr = (n_flag_FPSCR_ << 31) |
3329 (z_flag_FPSCR_ << 30) |
3330 (c_flag_FPSCR_ << 29) |
3331 (v_flag_FPSCR_ << 28) |
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003332 (FPSCR_default_NaN_mode_ << 25) |
Russell Brenner90bac252010-11-18 13:33:46 -08003333 (inexact_vfp_flag_ << 4) |
3334 (underflow_vfp_flag_ << 3) |
3335 (overflow_vfp_flag_ << 2) |
3336 (div_zero_vfp_flag_ << 1) |
3337 (inv_op_vfp_flag_ << 0) |
Steve Block1e0659c2011-05-24 12:43:12 +01003338 (FPSCR_rounding_mode_);
Russell Brenner90bac252010-11-18 13:33:46 -08003339 set_register(rt, fpscr);
3340 }
Steve Block1e0659c2011-05-24 12:43:12 +01003341 } else if ((instr->VLValue() == 0x0) &&
3342 (instr->VCValue() == 0x0) &&
3343 (instr->VAValue() == 0x7) &&
Russell Brenner90bac252010-11-18 13:33:46 -08003344 (instr->Bits(19, 16) == 0x1)) {
3345 // vmsr
Steve Block1e0659c2011-05-24 12:43:12 +01003346 uint32_t rt = instr->RtValue();
Russell Brenner90bac252010-11-18 13:33:46 -08003347 if (rt == pc) {
3348 UNREACHABLE();
3349 } else {
3350 uint32_t rt_value = get_register(rt);
3351 n_flag_FPSCR_ = (rt_value >> 31) & 1;
3352 z_flag_FPSCR_ = (rt_value >> 30) & 1;
3353 c_flag_FPSCR_ = (rt_value >> 29) & 1;
3354 v_flag_FPSCR_ = (rt_value >> 28) & 1;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003355 FPSCR_default_NaN_mode_ = (rt_value >> 25) & 1;
Russell Brenner90bac252010-11-18 13:33:46 -08003356 inexact_vfp_flag_ = (rt_value >> 4) & 1;
3357 underflow_vfp_flag_ = (rt_value >> 3) & 1;
3358 overflow_vfp_flag_ = (rt_value >> 2) & 1;
3359 div_zero_vfp_flag_ = (rt_value >> 1) & 1;
3360 inv_op_vfp_flag_ = (rt_value >> 0) & 1;
3361 FPSCR_rounding_mode_ =
Steve Block1e0659c2011-05-24 12:43:12 +01003362 static_cast<VFPRoundingMode>((rt_value) & kVFPRoundingModeMask);
Russell Brenner90bac252010-11-18 13:33:46 -08003363 }
Steve Blockd0582a62009-12-15 09:54:21 +00003364 } else {
3365 UNIMPLEMENTED(); // Not used by V8.
3366 }
Steve Block6ded16b2010-05-10 14:33:55 +01003367 }
3368}
3369
3370
Steve Block1e0659c2011-05-24 12:43:12 +01003371void Simulator::DecodeVMOVBetweenCoreAndSinglePrecisionRegisters(
3372 Instruction* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003373 DCHECK((instr->Bit(4) == 1) && (instr->VCValue() == 0x0) &&
Steve Block1e0659c2011-05-24 12:43:12 +01003374 (instr->VAValue() == 0x0));
Steve Block6ded16b2010-05-10 14:33:55 +01003375
Steve Block1e0659c2011-05-24 12:43:12 +01003376 int t = instr->RtValue();
3377 int n = instr->VFPNRegValue(kSinglePrecision);
3378 bool to_arm_register = (instr->VLValue() == 0x1);
Steve Block6ded16b2010-05-10 14:33:55 +01003379
3380 if (to_arm_register) {
3381 int32_t int_value = get_sinteger_from_s_register(n);
3382 set_register(t, int_value);
3383 } else {
3384 int32_t rs_val = get_register(t);
3385 set_s_register_from_sinteger(n, rs_val);
3386 }
3387}
3388
3389
Steve Block1e0659c2011-05-24 12:43:12 +01003390void Simulator::DecodeVCMP(Instruction* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003391 DCHECK((instr->Bit(4) == 0) && (instr->Opc1Value() == 0x7));
3392 DCHECK(((instr->Opc2Value() == 0x4) || (instr->Opc2Value() == 0x5)) &&
Steve Block1e0659c2011-05-24 12:43:12 +01003393 (instr->Opc3Value() & 0x1));
Steve Block6ded16b2010-05-10 14:33:55 +01003394 // Comparison.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003395
3396 VFPRegPrecision precision = kSinglePrecision;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003397 if (instr->SzValue() == 0x1) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003398 precision = kDoublePrecision;
3399 }
Steve Block6ded16b2010-05-10 14:33:55 +01003400
Steve Block1e0659c2011-05-24 12:43:12 +01003401 int d = instr->VFPDRegValue(precision);
Iain Merrick75681382010-08-19 15:07:18 +01003402 int m = 0;
Steve Block1e0659c2011-05-24 12:43:12 +01003403 if (instr->Opc2Value() == 0x4) {
3404 m = instr->VFPMRegValue(precision);
Iain Merrick75681382010-08-19 15:07:18 +01003405 }
Steve Block6ded16b2010-05-10 14:33:55 +01003406
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003407 if (precision == kDoublePrecision) {
Steve Block6ded16b2010-05-10 14:33:55 +01003408 double dd_value = get_double_from_d_register(d);
Iain Merrick75681382010-08-19 15:07:18 +01003409 double dm_value = 0.0;
Steve Block1e0659c2011-05-24 12:43:12 +01003410 if (instr->Opc2Value() == 0x4) {
Iain Merrick75681382010-08-19 15:07:18 +01003411 dm_value = get_double_from_d_register(m);
3412 }
Steve Block6ded16b2010-05-10 14:33:55 +01003413
Ben Murdoch086aeea2011-05-13 15:57:08 +01003414 // Raise exceptions for quiet NaNs if necessary.
3415 if (instr->Bit(7) == 1) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003416 if (std::isnan(dd_value)) {
Ben Murdoch086aeea2011-05-13 15:57:08 +01003417 inv_op_vfp_flag_ = true;
3418 }
3419 }
3420
Steve Block6ded16b2010-05-10 14:33:55 +01003421 Compute_FPSCR_Flags(dd_value, dm_value);
3422 } else {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003423 float sd_value = get_float_from_s_register(d);
3424 float sm_value = 0.0;
3425 if (instr->Opc2Value() == 0x4) {
3426 sm_value = get_float_from_s_register(m);
3427 }
3428
3429 // Raise exceptions for quiet NaNs if necessary.
3430 if (instr->Bit(7) == 1) {
3431 if (std::isnan(sd_value)) {
3432 inv_op_vfp_flag_ = true;
3433 }
3434 }
3435
3436 Compute_FPSCR_Flags(sd_value, sm_value);
Steve Block6ded16b2010-05-10 14:33:55 +01003437 }
3438}
3439
3440
Steve Block1e0659c2011-05-24 12:43:12 +01003441void Simulator::DecodeVCVTBetweenDoubleAndSingle(Instruction* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003442 DCHECK((instr->Bit(4) == 0) && (instr->Opc1Value() == 0x7));
3443 DCHECK((instr->Opc2Value() == 0x7) && (instr->Opc3Value() == 0x3));
Steve Block6ded16b2010-05-10 14:33:55 +01003444
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003445 VFPRegPrecision dst_precision = kDoublePrecision;
3446 VFPRegPrecision src_precision = kSinglePrecision;
Steve Block1e0659c2011-05-24 12:43:12 +01003447 if (instr->SzValue() == 1) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003448 dst_precision = kSinglePrecision;
3449 src_precision = kDoublePrecision;
3450 }
Steve Block6ded16b2010-05-10 14:33:55 +01003451
Steve Block1e0659c2011-05-24 12:43:12 +01003452 int dst = instr->VFPDRegValue(dst_precision);
3453 int src = instr->VFPMRegValue(src_precision);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003454
3455 if (dst_precision == kSinglePrecision) {
Steve Block6ded16b2010-05-10 14:33:55 +01003456 double val = get_double_from_d_register(src);
3457 set_s_register_from_float(dst, static_cast<float>(val));
3458 } else {
3459 float val = get_float_from_s_register(src);
3460 set_d_register_from_double(dst, static_cast<double>(val));
3461 }
3462}
3463
Steve Block1e0659c2011-05-24 12:43:12 +01003464bool get_inv_op_vfp_flag(VFPRoundingMode mode,
3465 double val,
3466 bool unsigned_) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003467 DCHECK((mode == RN) || (mode == RM) || (mode == RZ));
Steve Block1e0659c2011-05-24 12:43:12 +01003468 double max_uint = static_cast<double>(0xffffffffu);
3469 double max_int = static_cast<double>(kMaxInt);
3470 double min_int = static_cast<double>(kMinInt);
Steve Block6ded16b2010-05-10 14:33:55 +01003471
Steve Block1e0659c2011-05-24 12:43:12 +01003472 // Check for NaN.
3473 if (val != val) {
3474 return true;
3475 }
3476
3477 // Check for overflow. This code works because 32bit integers can be
3478 // exactly represented by ieee-754 64bit floating-point values.
3479 switch (mode) {
3480 case RN:
3481 return unsigned_ ? (val >= (max_uint + 0.5)) ||
3482 (val < -0.5)
3483 : (val >= (max_int + 0.5)) ||
3484 (val < (min_int - 0.5));
3485
3486 case RM:
3487 return unsigned_ ? (val >= (max_uint + 1.0)) ||
3488 (val < 0)
3489 : (val >= (max_int + 1.0)) ||
3490 (val < min_int);
3491
3492 case RZ:
3493 return unsigned_ ? (val >= (max_uint + 1.0)) ||
3494 (val <= -1)
3495 : (val >= (max_int + 1.0)) ||
3496 (val <= (min_int - 1.0));
3497 default:
3498 UNREACHABLE();
3499 return true;
3500 }
3501}
3502
3503
3504// We call this function only if we had a vfp invalid exception.
3505// It returns the correct saturated value.
3506int VFPConversionSaturate(double val, bool unsigned_res) {
3507 if (val != val) {
3508 return 0;
3509 } else {
3510 if (unsigned_res) {
3511 return (val < 0) ? 0 : 0xffffffffu;
3512 } else {
3513 return (val < 0) ? kMinInt : kMaxInt;
3514 }
3515 }
3516}
3517
3518
3519void Simulator::DecodeVCVTBetweenFloatingPointAndInteger(Instruction* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003520 DCHECK((instr->Bit(4) == 0) && (instr->Opc1Value() == 0x7) &&
Steve Block1e0659c2011-05-24 12:43:12 +01003521 (instr->Bits(27, 23) == 0x1D));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003522 DCHECK(((instr->Opc2Value() == 0x8) && (instr->Opc3Value() & 0x1)) ||
Steve Block1e0659c2011-05-24 12:43:12 +01003523 (((instr->Opc2Value() >> 1) == 0x6) && (instr->Opc3Value() & 0x1)));
Steve Block6ded16b2010-05-10 14:33:55 +01003524
3525 // Conversion between floating-point and integer.
Steve Block6ded16b2010-05-10 14:33:55 +01003526 bool to_integer = (instr->Bit(18) == 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003527
Steve Block1e0659c2011-05-24 12:43:12 +01003528 VFPRegPrecision src_precision = (instr->SzValue() == 1) ? kDoublePrecision
3529 : kSinglePrecision;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003530
Steve Block6ded16b2010-05-10 14:33:55 +01003531 if (to_integer) {
Steve Block1e0659c2011-05-24 12:43:12 +01003532 // We are playing with code close to the C++ standard's limits below,
3533 // hence the very simple code and heavy checks.
3534 //
3535 // Note:
3536 // C++ defines default type casting from floating point to integer as
3537 // (close to) rounding toward zero ("fractional part discarded").
3538
3539 int dst = instr->VFPDRegValue(kSinglePrecision);
3540 int src = instr->VFPMRegValue(src_precision);
3541
3542 // Bit 7 in vcvt instructions indicates if we should use the FPSCR rounding
3543 // mode or the default Round to Zero mode.
3544 VFPRoundingMode mode = (instr->Bit(7) != 1) ? FPSCR_rounding_mode_
3545 : RZ;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003546 DCHECK((mode == RM) || (mode == RZ) || (mode == RN));
Steve Block1e0659c2011-05-24 12:43:12 +01003547
Steve Block6ded16b2010-05-10 14:33:55 +01003548 bool unsigned_integer = (instr->Bit(16) == 0);
Steve Block1e0659c2011-05-24 12:43:12 +01003549 bool double_precision = (src_precision == kDoublePrecision);
3550
3551 double val = double_precision ? get_double_from_d_register(src)
3552 : get_float_from_s_register(src);
3553
3554 int temp = unsigned_integer ? static_cast<uint32_t>(val)
3555 : static_cast<int32_t>(val);
3556
3557 inv_op_vfp_flag_ = get_inv_op_vfp_flag(mode, val, unsigned_integer);
3558
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003559 double abs_diff =
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003560 unsigned_integer ? std::fabs(val - static_cast<uint32_t>(temp))
3561 : std::fabs(val - temp);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003562
3563 inexact_vfp_flag_ = (abs_diff != 0);
3564
Steve Block1e0659c2011-05-24 12:43:12 +01003565 if (inv_op_vfp_flag_) {
3566 temp = VFPConversionSaturate(val, unsigned_integer);
Russell Brenner90bac252010-11-18 13:33:46 -08003567 } else {
Steve Block1e0659c2011-05-24 12:43:12 +01003568 switch (mode) {
3569 case RN: {
Steve Block1e0659c2011-05-24 12:43:12 +01003570 int val_sign = (val > 0) ? 1 : -1;
3571 if (abs_diff > 0.5) {
3572 temp += val_sign;
3573 } else if (abs_diff == 0.5) {
3574 // Round to even if exactly halfway.
3575 temp = ((temp % 2) == 0) ? temp : temp + val_sign;
3576 }
3577 break;
3578 }
3579
3580 case RM:
3581 temp = temp > val ? temp - 1 : temp;
3582 break;
3583
3584 case RZ:
3585 // Nothing to do.
3586 break;
3587
3588 default:
3589 UNREACHABLE();
3590 }
Steve Blockd0582a62009-12-15 09:54:21 +00003591 }
Steve Block6ded16b2010-05-10 14:33:55 +01003592
Steve Block1e0659c2011-05-24 12:43:12 +01003593 // Update the destination register.
3594 set_s_register_from_sinteger(dst, temp);
Russell Brenner90bac252010-11-18 13:33:46 -08003595
Steve Block6ded16b2010-05-10 14:33:55 +01003596 } else {
3597 bool unsigned_integer = (instr->Bit(7) == 0);
3598
Steve Block1e0659c2011-05-24 12:43:12 +01003599 int dst = instr->VFPDRegValue(src_precision);
3600 int src = instr->VFPMRegValue(kSinglePrecision);
Steve Block6ded16b2010-05-10 14:33:55 +01003601
3602 int val = get_sinteger_from_s_register(src);
3603
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003604 if (src_precision == kDoublePrecision) {
Steve Block6ded16b2010-05-10 14:33:55 +01003605 if (unsigned_integer) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003606 set_d_register_from_double(
3607 dst, static_cast<double>(static_cast<uint32_t>(val)));
Steve Block6ded16b2010-05-10 14:33:55 +01003608 } else {
3609 set_d_register_from_double(dst, static_cast<double>(val));
3610 }
3611 } else {
3612 if (unsigned_integer) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003613 set_s_register_from_float(
3614 dst, static_cast<float>(static_cast<uint32_t>(val)));
Steve Block6ded16b2010-05-10 14:33:55 +01003615 } else {
3616 set_s_register_from_float(dst, static_cast<float>(val));
3617 }
Steve Blockd0582a62009-12-15 09:54:21 +00003618 }
3619 }
3620}
3621
3622
Steve Block1e0659c2011-05-24 12:43:12 +01003623// void Simulator::DecodeType6CoprocessorIns(Instruction* instr)
Steve Blockd0582a62009-12-15 09:54:21 +00003624// Decode Type 6 coprocessor instructions.
Leon Clarkee46be812010-01-19 14:06:41 +00003625// Dm = vmov(Rt, Rt2)
3626// <Rt, Rt2> = vmov(Dm)
Leon Clarked91b9f72010-01-27 17:25:45 +00003627// Ddst = MEM(Rbase + 4*offset).
3628// MEM(Rbase + 4*offset) = Dsrc.
Steve Block1e0659c2011-05-24 12:43:12 +01003629void Simulator::DecodeType6CoprocessorIns(Instruction* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003630 DCHECK((instr->TypeValue() == 6));
Steve Blockd0582a62009-12-15 09:54:21 +00003631
Steve Block1e0659c2011-05-24 12:43:12 +01003632 if (instr->CoprocessorValue() == 0xA) {
3633 switch (instr->OpcodeValue()) {
Steve Block6ded16b2010-05-10 14:33:55 +01003634 case 0x8:
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003635 case 0xA:
3636 case 0xC:
3637 case 0xE: { // Load and store single precision float to memory.
Steve Block1e0659c2011-05-24 12:43:12 +01003638 int rn = instr->RnValue();
3639 int vd = instr->VFPDRegValue(kSinglePrecision);
3640 int offset = instr->Immed8Value();
Steve Block6ded16b2010-05-10 14:33:55 +01003641 if (!instr->HasU()) {
3642 offset = -offset;
3643 }
3644
3645 int32_t address = get_register(rn) + 4 * offset;
3646 if (instr->HasL()) {
3647 // Load double from memory: vldr.
3648 set_s_register_from_sinteger(vd, ReadW(address, instr));
3649 } else {
3650 // Store double to memory: vstr.
3651 WriteW(address, get_sinteger_from_s_register(vd), instr);
3652 }
3653 break;
3654 }
Ben Murdoch8b112d22011-06-08 16:22:53 +01003655 case 0x4:
3656 case 0x5:
3657 case 0x6:
3658 case 0x7:
3659 case 0x9:
3660 case 0xB:
3661 // Load/store multiple single from memory: vldm/vstm.
3662 HandleVList(instr);
3663 break;
Steve Block6ded16b2010-05-10 14:33:55 +01003664 default:
3665 UNIMPLEMENTED(); // Not used by V8.
Steve Block6ded16b2010-05-10 14:33:55 +01003666 }
Steve Block1e0659c2011-05-24 12:43:12 +01003667 } else if (instr->CoprocessorValue() == 0xB) {
3668 switch (instr->OpcodeValue()) {
Leon Clarked91b9f72010-01-27 17:25:45 +00003669 case 0x2:
3670 // Load and store double to two GP registers
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003671 if (instr->Bits(7, 6) != 0 || instr->Bit(4) != 1) {
Leon Clarked91b9f72010-01-27 17:25:45 +00003672 UNIMPLEMENTED(); // Not used by V8.
3673 } else {
Steve Block1e0659c2011-05-24 12:43:12 +01003674 int rt = instr->RtValue();
3675 int rn = instr->RnValue();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003676 int vm = instr->VFPMRegValue(kDoublePrecision);
Leon Clarked91b9f72010-01-27 17:25:45 +00003677 if (instr->HasL()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003678 uint32_t data[2];
3679 get_d_register(vm, data);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003680 set_register(rt, data[0]);
3681 set_register(rn, data[1]);
Leon Clarked91b9f72010-01-27 17:25:45 +00003682 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003683 int32_t data[] = { get_register(rt), get_register(rn) };
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003684 set_d_register(vm, reinterpret_cast<uint32_t*>(data));
Leon Clarked91b9f72010-01-27 17:25:45 +00003685 }
3686 }
3687 break;
3688 case 0x8:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003689 case 0xA:
3690 case 0xC:
3691 case 0xE: { // Load and store double to memory.
Steve Block1e0659c2011-05-24 12:43:12 +01003692 int rn = instr->RnValue();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003693 int vd = instr->VFPDRegValue(kDoublePrecision);
Steve Block1e0659c2011-05-24 12:43:12 +01003694 int offset = instr->Immed8Value();
Leon Clarked91b9f72010-01-27 17:25:45 +00003695 if (!instr->HasU()) {
3696 offset = -offset;
3697 }
3698 int32_t address = get_register(rn) + 4 * offset;
3699 if (instr->HasL()) {
3700 // Load double from memory: vldr.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003701 int32_t data[] = {
3702 ReadW(address, instr),
3703 ReadW(address + 4, instr)
3704 };
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003705 set_d_register(vd, reinterpret_cast<uint32_t*>(data));
Leon Clarked91b9f72010-01-27 17:25:45 +00003706 } else {
3707 // Store double to memory: vstr.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003708 uint32_t data[2];
3709 get_d_register(vd, data);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003710 WriteW(address, data[0], instr);
3711 WriteW(address + 4, data[1], instr);
Leon Clarked91b9f72010-01-27 17:25:45 +00003712 }
3713 break;
3714 }
Ben Murdoch8b112d22011-06-08 16:22:53 +01003715 case 0x4:
3716 case 0x5:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003717 case 0x6:
3718 case 0x7:
Ben Murdoch8b112d22011-06-08 16:22:53 +01003719 case 0x9:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003720 case 0xB:
Ben Murdoch8b112d22011-06-08 16:22:53 +01003721 // Load/store multiple double from memory: vldm/vstm.
3722 HandleVList(instr);
3723 break;
Leon Clarked91b9f72010-01-27 17:25:45 +00003724 default:
3725 UNIMPLEMENTED(); // Not used by V8.
Leon Clarked91b9f72010-01-27 17:25:45 +00003726 }
Steve Block6ded16b2010-05-10 14:33:55 +01003727 } else {
3728 UNIMPLEMENTED(); // Not used by V8.
Steve Blockd0582a62009-12-15 09:54:21 +00003729 }
3730}
3731
3732
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003733void Simulator::DecodeSpecialCondition(Instruction* instr) {
3734 switch (instr->SpecialValue()) {
3735 case 5:
3736 if ((instr->Bits(18, 16) == 0) && (instr->Bits(11, 6) == 0x28) &&
3737 (instr->Bit(4) == 1)) {
3738 // vmovl signed
3739 if ((instr->VdValue() & 1) != 0) UNIMPLEMENTED();
3740 int Vd = (instr->Bit(22) << 3) | (instr->VdValue() >> 1);
3741 int Vm = (instr->Bit(5) << 4) | instr->VmValue();
3742 int imm3 = instr->Bits(21, 19);
3743 if ((imm3 != 1) && (imm3 != 2) && (imm3 != 4)) UNIMPLEMENTED();
3744 int esize = 8 * imm3;
3745 int elements = 64 / esize;
3746 int8_t from[8];
3747 get_d_register(Vm, reinterpret_cast<uint64_t*>(from));
3748 int16_t to[8];
3749 int e = 0;
3750 while (e < elements) {
3751 to[e] = from[e];
3752 e++;
3753 }
3754 set_q_register(Vd, reinterpret_cast<uint64_t*>(to));
3755 } else {
3756 UNIMPLEMENTED();
3757 }
3758 break;
3759 case 7:
3760 if ((instr->Bits(18, 16) == 0) && (instr->Bits(11, 6) == 0x28) &&
3761 (instr->Bit(4) == 1)) {
3762 // vmovl unsigned
3763 if ((instr->VdValue() & 1) != 0) UNIMPLEMENTED();
3764 int Vd = (instr->Bit(22) << 3) | (instr->VdValue() >> 1);
3765 int Vm = (instr->Bit(5) << 4) | instr->VmValue();
3766 int imm3 = instr->Bits(21, 19);
3767 if ((imm3 != 1) && (imm3 != 2) && (imm3 != 4)) UNIMPLEMENTED();
3768 int esize = 8 * imm3;
3769 int elements = 64 / esize;
3770 uint8_t from[8];
3771 get_d_register(Vm, reinterpret_cast<uint64_t*>(from));
3772 uint16_t to[8];
3773 int e = 0;
3774 while (e < elements) {
3775 to[e] = from[e];
3776 e++;
3777 }
3778 set_q_register(Vd, reinterpret_cast<uint64_t*>(to));
3779 } else {
3780 UNIMPLEMENTED();
3781 }
3782 break;
3783 case 8:
3784 if (instr->Bits(21, 20) == 0) {
3785 // vst1
3786 int Vd = (instr->Bit(22) << 4) | instr->VdValue();
3787 int Rn = instr->VnValue();
3788 int type = instr->Bits(11, 8);
3789 int Rm = instr->VmValue();
3790 int32_t address = get_register(Rn);
3791 int regs = 0;
3792 switch (type) {
3793 case nlt_1:
3794 regs = 1;
3795 break;
3796 case nlt_2:
3797 regs = 2;
3798 break;
3799 case nlt_3:
3800 regs = 3;
3801 break;
3802 case nlt_4:
3803 regs = 4;
3804 break;
3805 default:
3806 UNIMPLEMENTED();
3807 break;
3808 }
3809 int r = 0;
3810 while (r < regs) {
3811 uint32_t data[2];
3812 get_d_register(Vd + r, data);
3813 WriteW(address, data[0], instr);
3814 WriteW(address + 4, data[1], instr);
3815 address += 8;
3816 r++;
3817 }
3818 if (Rm != 15) {
3819 if (Rm == 13) {
3820 set_register(Rn, address);
3821 } else {
3822 set_register(Rn, get_register(Rn) + get_register(Rm));
3823 }
3824 }
3825 } else if (instr->Bits(21, 20) == 2) {
3826 // vld1
3827 int Vd = (instr->Bit(22) << 4) | instr->VdValue();
3828 int Rn = instr->VnValue();
3829 int type = instr->Bits(11, 8);
3830 int Rm = instr->VmValue();
3831 int32_t address = get_register(Rn);
3832 int regs = 0;
3833 switch (type) {
3834 case nlt_1:
3835 regs = 1;
3836 break;
3837 case nlt_2:
3838 regs = 2;
3839 break;
3840 case nlt_3:
3841 regs = 3;
3842 break;
3843 case nlt_4:
3844 regs = 4;
3845 break;
3846 default:
3847 UNIMPLEMENTED();
3848 break;
3849 }
3850 int r = 0;
3851 while (r < regs) {
3852 uint32_t data[2];
3853 data[0] = ReadW(address, instr);
3854 data[1] = ReadW(address + 4, instr);
3855 set_d_register(Vd + r, data);
3856 address += 8;
3857 r++;
3858 }
3859 if (Rm != 15) {
3860 if (Rm == 13) {
3861 set_register(Rn, address);
3862 } else {
3863 set_register(Rn, get_register(Rn) + get_register(Rm));
3864 }
3865 }
3866 } else {
3867 UNIMPLEMENTED();
3868 }
3869 break;
3870 case 0xA:
3871 case 0xB:
3872 if ((instr->Bits(22, 20) == 5) && (instr->Bits(15, 12) == 0xf)) {
3873 // pld: ignore instruction.
3874 } else {
3875 UNIMPLEMENTED();
3876 }
3877 break;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003878 case 0x1D:
3879 if (instr->Opc1Value() == 0x7 && instr->Opc3Value() == 0x1 &&
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003880 instr->Bits(11, 9) == 0x5 && instr->Bits(19, 18) == 0x2) {
3881 if (instr->SzValue() == 0x1) {
3882 int vm = instr->VFPMRegValue(kDoublePrecision);
3883 int vd = instr->VFPDRegValue(kDoublePrecision);
3884 double dm_value = get_double_from_d_register(vm);
3885 double dd_value = 0.0;
3886 int rounding_mode = instr->Bits(17, 16);
3887 switch (rounding_mode) {
3888 case 0x0: // vrinta - round with ties to away from zero
3889 dd_value = round(dm_value);
3890 break;
3891 case 0x1: { // vrintn - round with ties to even
3892 dd_value = nearbyint(dm_value);
3893 break;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003894 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003895 case 0x2: // vrintp - ceil
3896 dd_value = ceil(dm_value);
3897 break;
3898 case 0x3: // vrintm - floor
3899 dd_value = floor(dm_value);
3900 break;
3901 default:
3902 UNREACHABLE(); // Case analysis is exhaustive.
3903 break;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003904 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003905 dd_value = canonicalizeNaN(dd_value);
3906 set_d_register_from_double(vd, dd_value);
3907 } else {
3908 int m = instr->VFPMRegValue(kSinglePrecision);
3909 int d = instr->VFPDRegValue(kSinglePrecision);
3910 float sm_value = get_float_from_s_register(m);
3911 float sd_value = 0.0;
3912 int rounding_mode = instr->Bits(17, 16);
3913 switch (rounding_mode) {
3914 case 0x0: // vrinta - round with ties to away from zero
3915 sd_value = roundf(sm_value);
3916 break;
3917 case 0x1: { // vrintn - round with ties to even
3918 sd_value = nearbyintf(sm_value);
3919 break;
3920 }
3921 case 0x2: // vrintp - ceil
3922 sd_value = ceilf(sm_value);
3923 break;
3924 case 0x3: // vrintm - floor
3925 sd_value = floorf(sm_value);
3926 break;
3927 default:
3928 UNREACHABLE(); // Case analysis is exhaustive.
3929 break;
3930 }
3931 sd_value = canonicalizeNaN(sd_value);
3932 set_s_register_from_float(d, sd_value);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003933 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003934 } else {
3935 UNIMPLEMENTED();
3936 }
3937 break;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003938 default:
3939 UNIMPLEMENTED();
3940 break;
3941 }
3942}
3943
3944
Steve Blocka7e24c12009-10-30 11:49:00 +00003945// Executes the current instruction.
Steve Block1e0659c2011-05-24 12:43:12 +01003946void Simulator::InstructionDecode(Instruction* instr) {
Steve Block6ded16b2010-05-10 14:33:55 +01003947 if (v8::internal::FLAG_check_icache) {
Steve Block44f0eee2011-05-26 01:26:41 +01003948 CheckICache(isolate_->simulator_i_cache(), instr);
Steve Block6ded16b2010-05-10 14:33:55 +01003949 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003950 pc_modified_ = false;
3951 if (::v8::internal::FLAG_trace_sim) {
3952 disasm::NameConverter converter;
3953 disasm::Disassembler dasm(converter);
3954 // use a reasonably large buffer
3955 v8::internal::EmbeddedVector<char, 256> buffer;
3956 dasm.InstructionDecode(buffer,
3957 reinterpret_cast<byte*>(instr));
Ben Murdochf87a2032010-10-22 12:50:53 +01003958 PrintF(" 0x%08x %s\n", reinterpret_cast<intptr_t>(instr), buffer.start());
Steve Blocka7e24c12009-10-30 11:49:00 +00003959 }
Steve Block1e0659c2011-05-24 12:43:12 +01003960 if (instr->ConditionField() == kSpecialCondition) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003961 DecodeSpecialCondition(instr);
Steve Blocka7e24c12009-10-30 11:49:00 +00003962 } else if (ConditionallyExecute(instr)) {
Steve Block1e0659c2011-05-24 12:43:12 +01003963 switch (instr->TypeValue()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00003964 case 0:
3965 case 1: {
3966 DecodeType01(instr);
3967 break;
3968 }
3969 case 2: {
3970 DecodeType2(instr);
3971 break;
3972 }
3973 case 3: {
3974 DecodeType3(instr);
3975 break;
3976 }
3977 case 4: {
3978 DecodeType4(instr);
3979 break;
3980 }
3981 case 5: {
3982 DecodeType5(instr);
3983 break;
3984 }
3985 case 6: {
3986 DecodeType6(instr);
3987 break;
3988 }
3989 case 7: {
3990 DecodeType7(instr);
3991 break;
3992 }
3993 default: {
3994 UNIMPLEMENTED();
3995 break;
3996 }
3997 }
Steve Block1e0659c2011-05-24 12:43:12 +01003998 // If the instruction is a non taken conditional stop, we need to skip the
3999 // inlined message address.
4000 } else if (instr->IsStop()) {
4001 set_pc(get_pc() + 2 * Instruction::kInstrSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00004002 }
4003 if (!pc_modified_) {
Steve Block1e0659c2011-05-24 12:43:12 +01004004 set_register(pc, reinterpret_cast<int32_t>(instr)
4005 + Instruction::kInstrSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00004006 }
4007}
4008
4009
Steve Blocka7e24c12009-10-30 11:49:00 +00004010void Simulator::Execute() {
4011 // Get the PC to simulate. Cannot use the accessor here as we need the
4012 // raw PC value and not the one used as input to arithmetic instructions.
4013 int program_counter = get_pc();
4014
4015 if (::v8::internal::FLAG_stop_sim_at == 0) {
4016 // Fast version of the dispatch loop without checking whether the simulator
4017 // should be stopping at a particular executed instruction.
4018 while (program_counter != end_sim_pc) {
Steve Block1e0659c2011-05-24 12:43:12 +01004019 Instruction* instr = reinterpret_cast<Instruction*>(program_counter);
Steve Blocka7e24c12009-10-30 11:49:00 +00004020 icount_++;
4021 InstructionDecode(instr);
4022 program_counter = get_pc();
4023 }
4024 } else {
4025 // FLAG_stop_sim_at is at the non-default value. Stop in the debugger when
4026 // we reach the particular instuction count.
4027 while (program_counter != end_sim_pc) {
Steve Block1e0659c2011-05-24 12:43:12 +01004028 Instruction* instr = reinterpret_cast<Instruction*>(program_counter);
Steve Blocka7e24c12009-10-30 11:49:00 +00004029 icount_++;
4030 if (icount_ == ::v8::internal::FLAG_stop_sim_at) {
Steve Block44f0eee2011-05-26 01:26:41 +01004031 ArmDebugger dbg(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00004032 dbg.Debug();
4033 } else {
4034 InstructionDecode(instr);
4035 }
4036 program_counter = get_pc();
4037 }
4038 }
4039}
4040
4041
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004042void Simulator::CallInternal(byte* entry) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004043 // Adjust JS-based stack limit to C-based stack limit.
4044 isolate_->stack_guard()->AdjustStackLimitForSimulator();
4045
Steve Blocka7e24c12009-10-30 11:49:00 +00004046 // Prepare to execute the code at entry
4047 set_register(pc, reinterpret_cast<int32_t>(entry));
4048 // Put down marker for end of simulation. The simulator will stop simulation
4049 // when the PC reaches this value. By saving the "end simulation" value into
4050 // the LR the simulation stops when returning to this call point.
4051 set_register(lr, end_sim_pc);
4052
4053 // Remember the values of callee-saved registers.
4054 // The code below assumes that r9 is not used as sb (static base) in
4055 // simulator code and therefore is regarded as a callee-saved register.
4056 int32_t r4_val = get_register(r4);
4057 int32_t r5_val = get_register(r5);
4058 int32_t r6_val = get_register(r6);
4059 int32_t r7_val = get_register(r7);
4060 int32_t r8_val = get_register(r8);
4061 int32_t r9_val = get_register(r9);
4062 int32_t r10_val = get_register(r10);
4063 int32_t r11_val = get_register(r11);
4064
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004065 // Set up the callee-saved registers with a known value. To be able to check
Steve Blocka7e24c12009-10-30 11:49:00 +00004066 // that they are preserved properly across JS execution.
4067 int32_t callee_saved_value = icount_;
4068 set_register(r4, callee_saved_value);
4069 set_register(r5, callee_saved_value);
4070 set_register(r6, callee_saved_value);
4071 set_register(r7, callee_saved_value);
4072 set_register(r8, callee_saved_value);
4073 set_register(r9, callee_saved_value);
4074 set_register(r10, callee_saved_value);
4075 set_register(r11, callee_saved_value);
4076
4077 // Start the simulation
4078 Execute();
4079
4080 // Check that the callee-saved registers have been preserved.
4081 CHECK_EQ(callee_saved_value, get_register(r4));
4082 CHECK_EQ(callee_saved_value, get_register(r5));
4083 CHECK_EQ(callee_saved_value, get_register(r6));
4084 CHECK_EQ(callee_saved_value, get_register(r7));
4085 CHECK_EQ(callee_saved_value, get_register(r8));
4086 CHECK_EQ(callee_saved_value, get_register(r9));
4087 CHECK_EQ(callee_saved_value, get_register(r10));
4088 CHECK_EQ(callee_saved_value, get_register(r11));
4089
4090 // Restore callee-saved registers with the original value.
4091 set_register(r4, r4_val);
4092 set_register(r5, r5_val);
4093 set_register(r6, r6_val);
4094 set_register(r7, r7_val);
4095 set_register(r8, r8_val);
4096 set_register(r9, r9_val);
4097 set_register(r10, r10_val);
4098 set_register(r11, r11_val);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004099}
4100
4101
4102int32_t Simulator::Call(byte* entry, int argument_count, ...) {
4103 va_list parameters;
4104 va_start(parameters, argument_count);
4105 // Set up arguments
4106
4107 // First four arguments passed in registers.
4108 DCHECK(argument_count >= 4);
4109 set_register(r0, va_arg(parameters, int32_t));
4110 set_register(r1, va_arg(parameters, int32_t));
4111 set_register(r2, va_arg(parameters, int32_t));
4112 set_register(r3, va_arg(parameters, int32_t));
4113
4114 // Remaining arguments passed on stack.
4115 int original_stack = get_register(sp);
4116 // Compute position of stack on entry to generated code.
4117 int entry_stack = (original_stack - (argument_count - 4) * sizeof(int32_t));
4118 if (base::OS::ActivationFrameAlignment() != 0) {
4119 entry_stack &= -base::OS::ActivationFrameAlignment();
4120 }
4121 // Store remaining arguments on stack, from low to high memory.
4122 intptr_t* stack_argument = reinterpret_cast<intptr_t*>(entry_stack);
4123 for (int i = 4; i < argument_count; i++) {
4124 stack_argument[i - 4] = va_arg(parameters, int32_t);
4125 }
4126 va_end(parameters);
4127 set_register(sp, entry_stack);
4128
4129 CallInternal(entry);
Steve Blocka7e24c12009-10-30 11:49:00 +00004130
4131 // Pop stack passed arguments.
4132 CHECK_EQ(entry_stack, get_register(sp));
4133 set_register(sp, original_stack);
4134
4135 int32_t result = get_register(r0);
4136 return result;
4137}
4138
Steve Blockd0582a62009-12-15 09:54:21 +00004139
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004140void Simulator::CallFP(byte* entry, double d0, double d1) {
4141 if (use_eabi_hardfloat()) {
4142 set_d_register_from_double(0, d0);
4143 set_d_register_from_double(1, d1);
4144 } else {
4145 set_register_pair_from_double(0, &d0);
4146 set_register_pair_from_double(2, &d1);
4147 }
4148 CallInternal(entry);
4149}
4150
4151
4152int32_t Simulator::CallFPReturnsInt(byte* entry, double d0, double d1) {
4153 CallFP(entry, d0, d1);
4154 int32_t result = get_register(r0);
4155 return result;
4156}
4157
4158
4159double Simulator::CallFPReturnsDouble(byte* entry, double d0, double d1) {
4160 CallFP(entry, d0, d1);
4161 if (use_eabi_hardfloat()) {
4162 return get_double_from_d_register(0);
4163 } else {
4164 return get_double_from_register_pair(0);
4165 }
4166}
4167
4168
Steve Blockd0582a62009-12-15 09:54:21 +00004169uintptr_t Simulator::PushAddress(uintptr_t address) {
4170 int new_sp = get_register(sp) - sizeof(uintptr_t);
4171 uintptr_t* stack_slot = reinterpret_cast<uintptr_t*>(new_sp);
4172 *stack_slot = address;
4173 set_register(sp, new_sp);
4174 return new_sp;
4175}
4176
4177
4178uintptr_t Simulator::PopAddress() {
4179 int current_sp = get_register(sp);
4180 uintptr_t* stack_slot = reinterpret_cast<uintptr_t*>(current_sp);
4181 uintptr_t address = *stack_slot;
4182 set_register(sp, current_sp + sizeof(uintptr_t));
4183 return address;
4184}
4185
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004186} // namespace internal
4187} // namespace v8
Steve Blocka7e24c12009-10-30 11:49:00 +00004188
John Reck59135872010-11-02 12:39:01 -07004189#endif // USE_SIMULATOR
Leon Clarkef7060e22010-06-03 12:02:55 +01004190
4191#endif // V8_TARGET_ARCH_ARM