blob: 4e631b08a364424543a3a6aea2798c931503dc17 [file] [log] [blame]
Ben Murdoch8b112d22011-06-08 16:22:53 +01001// Copyright 2011 the V8 project authors. All rights reserved.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002// 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
5// A Disassembler object is used to disassemble a block of code instruction by
6// instruction. The default implementation of the NameConverter object can be
7// overriden to modify register names or to do symbol lookup on addresses.
8//
9// The example below will disassemble a block of code and print it to stdout.
10//
11// NameConverter converter;
12// Disassembler d(converter);
13// for (byte* pc = begin; pc < end;) {
Steve Block6ded16b2010-05-10 14:33:55 +010014// v8::internal::EmbeddedVector<char, 256> buffer;
Steve Blocka7e24c12009-10-30 11:49:00 +000015// byte* prev_pc = pc;
Steve Block6ded16b2010-05-10 14:33:55 +010016// pc += d.InstructionDecode(buffer, pc);
Steve Blocka7e24c12009-10-30 11:49:00 +000017// printf("%p %08x %s\n",
18// prev_pc, *reinterpret_cast<int32_t*>(prev_pc), buffer);
19// }
20//
21// The Disassembler class also has a convenience method to disassemble a block
22// of code into a FILE*, meaning that the above functionality could also be
23// achieved by just calling Disassembler::Disassemble(stdout, begin, end);
24
25
26#include <assert.h>
Steve Blocka7e24c12009-10-30 11:49:00 +000027#include <stdarg.h>
Ben Murdochb8a8cc12014-11-26 15:28:44 +000028#include <stdio.h>
Steve Blocka7e24c12009-10-30 11:49:00 +000029#include <string.h>
Steve Blocka7e24c12009-10-30 11:49:00 +000030
Ben Murdochb8a8cc12014-11-26 15:28:44 +000031#include "src/v8.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000032
Ben Murdochb8a8cc12014-11-26 15:28:44 +000033#if V8_TARGET_ARCH_ARM
Leon Clarkef7060e22010-06-03 12:02:55 +010034
Ben Murdochb8a8cc12014-11-26 15:28:44 +000035#include "src/arm/constants-arm.h"
36#include "src/base/platform/platform.h"
37#include "src/disasm.h"
38#include "src/macro-assembler.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000039
40
Steve Block1e0659c2011-05-24 12:43:12 +010041namespace v8 {
42namespace internal {
Steve Blocka7e24c12009-10-30 11:49:00 +000043
44
45//------------------------------------------------------------------------------
46
47// Decoder decodes and disassembles instructions into an output buffer.
48// It uses the converter to convert register names and call destinations into
49// more informative description.
50class Decoder {
51 public:
52 Decoder(const disasm::NameConverter& converter,
Steve Block1e0659c2011-05-24 12:43:12 +010053 Vector<char> out_buffer)
Steve Blocka7e24c12009-10-30 11:49:00 +000054 : converter_(converter),
55 out_buffer_(out_buffer),
56 out_buffer_pos_(0) {
57 out_buffer_[out_buffer_pos_] = '\0';
58 }
59
60 ~Decoder() {}
61
62 // Writes one disassembled instruction into 'buffer' (0-terminated).
63 // Returns the length of the disassembled machine instruction in bytes.
64 int InstructionDecode(byte* instruction);
65
Steve Block44f0eee2011-05-26 01:26:41 +010066 static bool IsConstantPoolAt(byte* instr_ptr);
67 static int ConstantPoolSizeAt(byte* instr_ptr);
68
Steve Blocka7e24c12009-10-30 11:49:00 +000069 private:
70 // Bottleneck functions to print into the out_buffer.
71 void PrintChar(const char ch);
72 void Print(const char* str);
73
74 // Printing of common values.
75 void PrintRegister(int reg);
Steve Blockd0582a62009-12-15 09:54:21 +000076 void PrintSRegister(int reg);
77 void PrintDRegister(int reg);
Steve Block1e0659c2011-05-24 12:43:12 +010078 int FormatVFPRegister(Instruction* instr, const char* format);
79 void PrintMovwMovt(Instruction* instr);
80 int FormatVFPinstruction(Instruction* instr, const char* format);
81 void PrintCondition(Instruction* instr);
82 void PrintShiftRm(Instruction* instr);
83 void PrintShiftImm(Instruction* instr);
84 void PrintShiftSat(Instruction* instr);
85 void PrintPU(Instruction* instr);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -080086 void PrintSoftwareInterrupt(SoftwareInterruptCodes svc);
Steve Blocka7e24c12009-10-30 11:49:00 +000087
88 // Handle formatting of instructions and their options.
Steve Block1e0659c2011-05-24 12:43:12 +010089 int FormatRegister(Instruction* instr, const char* option);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000090 void FormatNeonList(int Vd, int type);
91 void FormatNeonMemory(int Rn, int align, int Rm);
Steve Block1e0659c2011-05-24 12:43:12 +010092 int FormatOption(Instruction* instr, const char* option);
93 void Format(Instruction* instr, const char* format);
94 void Unknown(Instruction* instr);
Steve Blocka7e24c12009-10-30 11:49:00 +000095
96 // Each of these functions decodes one particular instruction type, a 3-bit
97 // field in the instruction encoding.
98 // Types 0 and 1 are combined as they are largely the same except for the way
99 // they interpret the shifter operand.
Steve Block1e0659c2011-05-24 12:43:12 +0100100 void DecodeType01(Instruction* instr);
101 void DecodeType2(Instruction* instr);
102 void DecodeType3(Instruction* instr);
103 void DecodeType4(Instruction* instr);
104 void DecodeType5(Instruction* instr);
105 void DecodeType6(Instruction* instr);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800106 // Type 7 includes special Debugger instructions.
Steve Block1e0659c2011-05-24 12:43:12 +0100107 int DecodeType7(Instruction* instr);
Steve Blockd0582a62009-12-15 09:54:21 +0000108 // For VFP support.
Steve Block1e0659c2011-05-24 12:43:12 +0100109 void DecodeTypeVFP(Instruction* instr);
110 void DecodeType6CoprocessorIns(Instruction* instr);
Steve Blockd0582a62009-12-15 09:54:21 +0000111
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000112 void DecodeSpecialCondition(Instruction* instr);
113
Steve Block1e0659c2011-05-24 12:43:12 +0100114 void DecodeVMOVBetweenCoreAndSinglePrecisionRegisters(Instruction* instr);
115 void DecodeVCMP(Instruction* instr);
116 void DecodeVCVTBetweenDoubleAndSingle(Instruction* instr);
117 void DecodeVCVTBetweenFloatingPointAndInteger(Instruction* instr);
Steve Blocka7e24c12009-10-30 11:49:00 +0000118
119 const disasm::NameConverter& converter_;
Steve Block1e0659c2011-05-24 12:43:12 +0100120 Vector<char> out_buffer_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000121 int out_buffer_pos_;
122
123 DISALLOW_COPY_AND_ASSIGN(Decoder);
124};
125
126
127// Support for assertions in the Decoder formatting functions.
128#define STRING_STARTS_WITH(string, compare_string) \
129 (strncmp(string, compare_string, strlen(compare_string)) == 0)
130
131
132// Append the ch to the output buffer.
133void Decoder::PrintChar(const char ch) {
134 out_buffer_[out_buffer_pos_++] = ch;
135}
136
137
138// Append the str to the output buffer.
139void Decoder::Print(const char* str) {
140 char cur = *str++;
141 while (cur != '\0' && (out_buffer_pos_ < (out_buffer_.length() - 1))) {
142 PrintChar(cur);
143 cur = *str++;
144 }
145 out_buffer_[out_buffer_pos_] = 0;
146}
147
148
149// These condition names are defined in a way to match the native disassembler
150// formatting. See for example the command "objdump -d <binary file>".
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400151static const char* const cond_names[kNumberOfConditions] = {
Steve Blocka7e24c12009-10-30 11:49:00 +0000152 "eq", "ne", "cs" , "cc" , "mi" , "pl" , "vs" , "vc" ,
153 "hi", "ls", "ge", "lt", "gt", "le", "", "invalid",
154};
155
156
157// Print the condition guarding the instruction.
Steve Block1e0659c2011-05-24 12:43:12 +0100158void Decoder::PrintCondition(Instruction* instr) {
159 Print(cond_names[instr->ConditionValue()]);
Steve Blocka7e24c12009-10-30 11:49:00 +0000160}
161
162
163// Print the register name according to the active name converter.
164void Decoder::PrintRegister(int reg) {
165 Print(converter_.NameOfCPURegister(reg));
166}
167
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000168
Steve Blockd0582a62009-12-15 09:54:21 +0000169// Print the VFP S register name according to the active name converter.
170void Decoder::PrintSRegister(int reg) {
Steve Block1e0659c2011-05-24 12:43:12 +0100171 Print(VFPRegisters::Name(reg, false));
Steve Blockd0582a62009-12-15 09:54:21 +0000172}
173
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000174
175// Print the VFP D register name according to the active name converter.
Steve Blockd0582a62009-12-15 09:54:21 +0000176void Decoder::PrintDRegister(int reg) {
Steve Block1e0659c2011-05-24 12:43:12 +0100177 Print(VFPRegisters::Name(reg, true));
Steve Blockd0582a62009-12-15 09:54:21 +0000178}
179
Steve Blocka7e24c12009-10-30 11:49:00 +0000180
181// These shift names are defined in a way to match the native disassembler
182// formatting. See for example the command "objdump -d <binary file>".
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000183static const char* const shift_names[kNumberOfShifts] = {
Steve Blocka7e24c12009-10-30 11:49:00 +0000184 "lsl", "lsr", "asr", "ror"
185};
186
187
188// Print the register shift operands for the instruction. Generally used for
189// data processing instructions.
Steve Block1e0659c2011-05-24 12:43:12 +0100190void Decoder::PrintShiftRm(Instruction* instr) {
191 ShiftOp shift = instr->ShiftField();
192 int shift_index = instr->ShiftValue();
193 int shift_amount = instr->ShiftAmountValue();
194 int rm = instr->RmValue();
Steve Blocka7e24c12009-10-30 11:49:00 +0000195
196 PrintRegister(rm);
197
Steve Block1e0659c2011-05-24 12:43:12 +0100198 if ((instr->RegShiftValue() == 0) && (shift == LSL) && (shift_amount == 0)) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000199 // Special case for using rm only.
200 return;
201 }
Steve Block1e0659c2011-05-24 12:43:12 +0100202 if (instr->RegShiftValue() == 0) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000203 // by immediate
204 if ((shift == ROR) && (shift_amount == 0)) {
205 Print(", RRX");
206 return;
207 } else if (((shift == LSR) || (shift == ASR)) && (shift_amount == 0)) {
208 shift_amount = 32;
209 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000210 out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
211 ", %s #%d",
212 shift_names[shift_index],
213 shift_amount);
Steve Blocka7e24c12009-10-30 11:49:00 +0000214 } else {
215 // by register
Steve Block1e0659c2011-05-24 12:43:12 +0100216 int rs = instr->RsValue();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000217 out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
218 ", %s ", shift_names[shift_index]);
Steve Blocka7e24c12009-10-30 11:49:00 +0000219 PrintRegister(rs);
220 }
221}
222
223
224// Print the immediate operand for the instruction. Generally used for data
225// processing instructions.
Steve Block1e0659c2011-05-24 12:43:12 +0100226void Decoder::PrintShiftImm(Instruction* instr) {
227 int rotate = instr->RotateValue() * 2;
228 int immed8 = instr->Immed8Value();
Steve Blocka7e24c12009-10-30 11:49:00 +0000229 int imm = (immed8 >> rotate) | (immed8 << (32 - rotate));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000230 out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_, "#%d", imm);
Steve Blocka7e24c12009-10-30 11:49:00 +0000231}
232
233
Kristian Monsen50ef84f2010-07-29 15:18:00 +0100234// Print the optional shift and immediate used by saturating instructions.
Steve Block1e0659c2011-05-24 12:43:12 +0100235void Decoder::PrintShiftSat(Instruction* instr) {
Kristian Monsen50ef84f2010-07-29 15:18:00 +0100236 int shift = instr->Bits(11, 7);
237 if (shift > 0) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000238 out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
239 ", %s #%d",
240 shift_names[instr->Bit(6) * 2],
241 instr->Bits(11, 7));
Kristian Monsen50ef84f2010-07-29 15:18:00 +0100242 }
243}
244
245
Steve Blocka7e24c12009-10-30 11:49:00 +0000246// Print PU formatting to reduce complexity of FormatOption.
Steve Block1e0659c2011-05-24 12:43:12 +0100247void Decoder::PrintPU(Instruction* instr) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000248 switch (instr->PUField()) {
Steve Block1e0659c2011-05-24 12:43:12 +0100249 case da_x: {
Steve Blocka7e24c12009-10-30 11:49:00 +0000250 Print("da");
251 break;
252 }
Steve Block1e0659c2011-05-24 12:43:12 +0100253 case ia_x: {
Steve Blocka7e24c12009-10-30 11:49:00 +0000254 Print("ia");
255 break;
256 }
Steve Block1e0659c2011-05-24 12:43:12 +0100257 case db_x: {
Steve Blocka7e24c12009-10-30 11:49:00 +0000258 Print("db");
259 break;
260 }
Steve Block1e0659c2011-05-24 12:43:12 +0100261 case ib_x: {
Steve Blocka7e24c12009-10-30 11:49:00 +0000262 Print("ib");
263 break;
264 }
265 default: {
266 UNREACHABLE();
267 break;
268 }
269 }
270}
271
272
273// Print SoftwareInterrupt codes. Factoring this out reduces the complexity of
274// the FormatOption method.
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800275void Decoder::PrintSoftwareInterrupt(SoftwareInterruptCodes svc) {
276 switch (svc) {
Steve Block1e0659c2011-05-24 12:43:12 +0100277 case kCallRtRedirected:
278 Print("call rt redirected");
Steve Blocka7e24c12009-10-30 11:49:00 +0000279 return;
Steve Block1e0659c2011-05-24 12:43:12 +0100280 case kBreakpoint:
281 Print("breakpoint");
Steve Blocka7e24c12009-10-30 11:49:00 +0000282 return;
283 default:
Steve Block1e0659c2011-05-24 12:43:12 +0100284 if (svc >= kStopCode) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000285 out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
286 "%d - 0x%x",
287 svc & kStopCodeMask,
288 svc & kStopCodeMask);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800289 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000290 out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
291 "%d",
292 svc);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800293 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000294 return;
295 }
296}
297
298
299// Handle all register based formatting in this function to reduce the
300// complexity of FormatOption.
Steve Block1e0659c2011-05-24 12:43:12 +0100301int Decoder::FormatRegister(Instruction* instr, const char* format) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000302 DCHECK(format[0] == 'r');
Steve Blocka7e24c12009-10-30 11:49:00 +0000303 if (format[1] == 'n') { // 'rn: Rn register
Steve Block1e0659c2011-05-24 12:43:12 +0100304 int reg = instr->RnValue();
Steve Blocka7e24c12009-10-30 11:49:00 +0000305 PrintRegister(reg);
306 return 2;
307 } else if (format[1] == 'd') { // 'rd: Rd register
Steve Block1e0659c2011-05-24 12:43:12 +0100308 int reg = instr->RdValue();
Steve Blocka7e24c12009-10-30 11:49:00 +0000309 PrintRegister(reg);
310 return 2;
311 } else if (format[1] == 's') { // 'rs: Rs register
Steve Block1e0659c2011-05-24 12:43:12 +0100312 int reg = instr->RsValue();
Steve Blocka7e24c12009-10-30 11:49:00 +0000313 PrintRegister(reg);
314 return 2;
315 } else if (format[1] == 'm') { // 'rm: Rm register
Steve Block1e0659c2011-05-24 12:43:12 +0100316 int reg = instr->RmValue();
Steve Blocka7e24c12009-10-30 11:49:00 +0000317 PrintRegister(reg);
318 return 2;
Steve Blockd0582a62009-12-15 09:54:21 +0000319 } else if (format[1] == 't') { // 'rt: Rt register
Steve Block1e0659c2011-05-24 12:43:12 +0100320 int reg = instr->RtValue();
Steve Blockd0582a62009-12-15 09:54:21 +0000321 PrintRegister(reg);
322 return 2;
Steve Blocka7e24c12009-10-30 11:49:00 +0000323 } else if (format[1] == 'l') {
324 // 'rlist: register list for load and store multiple instructions
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000325 DCHECK(STRING_STARTS_WITH(format, "rlist"));
Steve Block1e0659c2011-05-24 12:43:12 +0100326 int rlist = instr->RlistValue();
Steve Blocka7e24c12009-10-30 11:49:00 +0000327 int reg = 0;
328 Print("{");
329 // Print register list in ascending order, by scanning the bit mask.
330 while (rlist != 0) {
331 if ((rlist & 1) != 0) {
332 PrintRegister(reg);
333 if ((rlist >> 1) != 0) {
334 Print(", ");
335 }
336 }
337 reg++;
338 rlist >>= 1;
339 }
340 Print("}");
341 return 5;
342 }
343 UNREACHABLE();
344 return -1;
345}
346
347
Steve Blockd0582a62009-12-15 09:54:21 +0000348// Handle all VFP register based formatting in this function to reduce the
349// complexity of FormatOption.
Steve Block1e0659c2011-05-24 12:43:12 +0100350int Decoder::FormatVFPRegister(Instruction* instr, const char* format) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000351 DCHECK((format[0] == 'S') || (format[0] == 'D'));
Steve Blockd0582a62009-12-15 09:54:21 +0000352
Ben Murdoch8b112d22011-06-08 16:22:53 +0100353 VFPRegPrecision precision =
354 format[0] == 'D' ? kDoublePrecision : kSinglePrecision;
355
356 int retval = 2;
357 int reg = -1;
Steve Blockd0582a62009-12-15 09:54:21 +0000358 if (format[1] == 'n') {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100359 reg = instr->VFPNRegValue(precision);
Steve Blockd0582a62009-12-15 09:54:21 +0000360 } else if (format[1] == 'm') {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100361 reg = instr->VFPMRegValue(precision);
Steve Blockd0582a62009-12-15 09:54:21 +0000362 } else if (format[1] == 'd') {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000363 if ((instr->TypeValue() == 7) &&
364 (instr->Bit(24) == 0x0) &&
365 (instr->Bits(11, 9) == 0x5) &&
366 (instr->Bit(4) == 0x1)) {
367 // vmov.32 has Vd in a different place.
368 reg = instr->Bits(19, 16) | (instr->Bit(7) << 4);
369 } else {
370 reg = instr->VFPDRegValue(precision);
371 }
372
Ben Murdoch8b112d22011-06-08 16:22:53 +0100373 if (format[2] == '+') {
374 int immed8 = instr->Immed8Value();
375 if (format[0] == 'S') reg += immed8 - 1;
376 if (format[0] == 'D') reg += (immed8 / 2 - 1);
377 }
378 if (format[2] == '+') retval = 3;
379 } else {
380 UNREACHABLE();
Steve Blockd0582a62009-12-15 09:54:21 +0000381 }
382
Ben Murdoch8b112d22011-06-08 16:22:53 +0100383 if (precision == kSinglePrecision) {
384 PrintSRegister(reg);
385 } else {
386 PrintDRegister(reg);
387 }
388
389 return retval;
Steve Blockd0582a62009-12-15 09:54:21 +0000390}
391
392
Steve Block1e0659c2011-05-24 12:43:12 +0100393int Decoder::FormatVFPinstruction(Instruction* instr, const char* format) {
Steve Blockd0582a62009-12-15 09:54:21 +0000394 Print(format);
395 return 0;
396}
397
398
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000399void Decoder::FormatNeonList(int Vd, int type) {
400 if (type == nlt_1) {
401 out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
402 "{d%d}", Vd);
403 } else if (type == nlt_2) {
404 out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
405 "{d%d, d%d}", Vd, Vd + 1);
406 } else if (type == nlt_3) {
407 out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
408 "{d%d, d%d, d%d}", Vd, Vd + 1, Vd + 2);
409 } else if (type == nlt_4) {
410 out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
411 "{d%d, d%d, d%d, d%d}", Vd, Vd + 1, Vd + 2, Vd + 3);
412 }
413}
414
415
416void Decoder::FormatNeonMemory(int Rn, int align, int Rm) {
417 out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
418 "[r%d", Rn);
419 if (align != 0) {
420 out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
421 ":%d", (1 << align) << 6);
422 }
423 if (Rm == 15) {
424 Print("]");
425 } else if (Rm == 13) {
426 Print("]!");
427 } else {
428 out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
429 "], r%d", Rm);
430 }
431}
432
433
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100434// Print the movw or movt instruction.
Steve Block1e0659c2011-05-24 12:43:12 +0100435void Decoder::PrintMovwMovt(Instruction* instr) {
436 int imm = instr->ImmedMovwMovtValue();
437 int rd = instr->RdValue();
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100438 PrintRegister(rd);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000439 out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_, ", #%d", imm);
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100440}
441
442
Steve Blocka7e24c12009-10-30 11:49:00 +0000443// FormatOption takes a formatting string and interprets it based on
444// the current instructions. The format string points to the first
445// character of the option string (the option escape has already been
446// consumed by the caller.) FormatOption returns the number of
447// characters that were consumed from the formatting string.
Steve Block1e0659c2011-05-24 12:43:12 +0100448int Decoder::FormatOption(Instruction* instr, const char* format) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000449 switch (format[0]) {
450 case 'a': { // 'a: accumulate multiplies
451 if (instr->Bit(21) == 0) {
452 Print("ul");
453 } else {
454 Print("la");
455 }
456 return 1;
457 }
458 case 'b': { // 'b: byte loads or stores
459 if (instr->HasB()) {
460 Print("b");
461 }
462 return 1;
463 }
464 case 'c': { // 'cond: conditional execution
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000465 DCHECK(STRING_STARTS_WITH(format, "cond"));
Steve Blocka7e24c12009-10-30 11:49:00 +0000466 PrintCondition(instr);
467 return 4;
468 }
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100469 case 'd': { // 'd: vmov double immediate.
470 double d = instr->DoubleImmedVmov();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000471 out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_, "#%g", d);
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100472 return 1;
473 }
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100474 case 'f': { // 'f: bitfield instructions - v7 and above.
475 uint32_t lsbit = instr->Bits(11, 7);
476 uint32_t width = instr->Bits(20, 16) + 1;
477 if (instr->Bit(21) == 0) {
478 // BFC/BFI:
479 // Bits 20-16 represent most-significant bit. Covert to width.
480 width -= lsbit;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000481 DCHECK(width > 0);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100482 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000483 DCHECK((width + lsbit) <= 32);
484 out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
485 "#%d, #%d", lsbit, width);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100486 return 1;
487 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000488 case 'h': { // 'h: halfword operation for extra loads and stores
489 if (instr->HasH()) {
490 Print("h");
491 } else {
492 Print("b");
493 }
494 return 1;
495 }
Kristian Monsen50ef84f2010-07-29 15:18:00 +0100496 case 'i': { // 'i: immediate value from adjacent bits.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100497 // Expects tokens in the form imm%02d@%02d, i.e. imm05@07, imm10@16
Kristian Monsen50ef84f2010-07-29 15:18:00 +0100498 int width = (format[3] - '0') * 10 + (format[4] - '0');
499 int lsb = (format[6] - '0') * 10 + (format[7] - '0');
500
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000501 DCHECK((width >= 1) && (width <= 32));
502 DCHECK((lsb >= 0) && (lsb <= 31));
503 DCHECK((width + lsb) <= 32);
Kristian Monsen50ef84f2010-07-29 15:18:00 +0100504
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000505 out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
506 "%d",
507 instr->Bits(width + lsb - 1, lsb));
Kristian Monsen50ef84f2010-07-29 15:18:00 +0100508 return 8;
509 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000510 case 'l': { // 'l: branch and link
511 if (instr->HasLink()) {
512 Print("l");
513 }
514 return 1;
515 }
516 case 'm': {
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100517 if (format[1] == 'w') {
518 // 'mw: movt/movw instructions.
519 PrintMovwMovt(instr);
520 return 2;
521 }
522 if (format[1] == 'e') { // 'memop: load/store instructions.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000523 DCHECK(STRING_STARTS_WITH(format, "memop"));
Steve Blocka7e24c12009-10-30 11:49:00 +0000524 if (instr->HasL()) {
525 Print("ldr");
526 } else {
Ben Murdoch257744e2011-11-30 15:57:28 +0000527 if ((instr->Bits(27, 25) == 0) && (instr->Bit(20) == 0) &&
528 (instr->Bits(7, 6) == 3) && (instr->Bit(4) == 1)) {
529 if (instr->Bit(5) == 1) {
530 Print("strd");
531 } else {
532 Print("ldrd");
533 }
534 return 5;
535 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000536 Print("str");
537 }
538 return 5;
539 }
540 // 'msg: for simulator break instructions
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000541 DCHECK(STRING_STARTS_WITH(format, "msg"));
Steve Blocka7e24c12009-10-30 11:49:00 +0000542 byte* str =
543 reinterpret_cast<byte*>(instr->InstructionBits() & 0x0fffffff);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000544 out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
545 "%s", converter_.NameInCode(str));
Steve Blocka7e24c12009-10-30 11:49:00 +0000546 return 3;
547 }
548 case 'o': {
Andrei Popescu31002712010-02-23 13:46:05 +0000549 if ((format[3] == '1') && (format[4] == '2')) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000550 // 'off12: 12-bit offset for load and store instructions
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000551 DCHECK(STRING_STARTS_WITH(format, "off12"));
552 out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
553 "%d", instr->Offset12Value());
Steve Blocka7e24c12009-10-30 11:49:00 +0000554 return 5;
Steve Block6ded16b2010-05-10 14:33:55 +0100555 } else if (format[3] == '0') {
556 // 'off0to3and8to19 16-bit immediate encoded in bits 19-8 and 3-0.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000557 DCHECK(STRING_STARTS_WITH(format, "off0to3and8to19"));
558 out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
559 "%d",
560 (instr->Bits(19, 8) << 4) +
561 instr->Bits(3, 0));
Steve Block6ded16b2010-05-10 14:33:55 +0100562 return 15;
Steve Blocka7e24c12009-10-30 11:49:00 +0000563 }
564 // 'off8: 8-bit offset for extra load and store instructions
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000565 DCHECK(STRING_STARTS_WITH(format, "off8"));
Steve Block1e0659c2011-05-24 12:43:12 +0100566 int offs8 = (instr->ImmedHValue() << 4) | instr->ImmedLValue();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000567 out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_, "%d", offs8);
Steve Blocka7e24c12009-10-30 11:49:00 +0000568 return 4;
569 }
570 case 'p': { // 'pu: P and U bits for load and store instructions
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000571 DCHECK(STRING_STARTS_WITH(format, "pu"));
Steve Blocka7e24c12009-10-30 11:49:00 +0000572 PrintPU(instr);
573 return 2;
574 }
575 case 'r': {
576 return FormatRegister(instr, format);
577 }
578 case 's': {
Kristian Monsen50ef84f2010-07-29 15:18:00 +0100579 if (format[1] == 'h') { // 'shift_op or 'shift_rm or 'shift_sat.
Steve Blocka7e24c12009-10-30 11:49:00 +0000580 if (format[6] == 'o') { // 'shift_op
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000581 DCHECK(STRING_STARTS_WITH(format, "shift_op"));
Steve Block1e0659c2011-05-24 12:43:12 +0100582 if (instr->TypeValue() == 0) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000583 PrintShiftRm(instr);
584 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000585 DCHECK(instr->TypeValue() == 1);
Steve Blocka7e24c12009-10-30 11:49:00 +0000586 PrintShiftImm(instr);
587 }
588 return 8;
Kristian Monsen50ef84f2010-07-29 15:18:00 +0100589 } else if (format[6] == 's') { // 'shift_sat.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000590 DCHECK(STRING_STARTS_WITH(format, "shift_sat"));
Kristian Monsen50ef84f2010-07-29 15:18:00 +0100591 PrintShiftSat(instr);
592 return 9;
Steve Blocka7e24c12009-10-30 11:49:00 +0000593 } else { // 'shift_rm
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000594 DCHECK(STRING_STARTS_WITH(format, "shift_rm"));
Steve Blocka7e24c12009-10-30 11:49:00 +0000595 PrintShiftRm(instr);
596 return 8;
597 }
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800598 } else if (format[1] == 'v') { // 'svc
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000599 DCHECK(STRING_STARTS_WITH(format, "svc"));
Steve Block1e0659c2011-05-24 12:43:12 +0100600 PrintSoftwareInterrupt(instr->SvcValue());
Steve Blocka7e24c12009-10-30 11:49:00 +0000601 return 3;
602 } else if (format[1] == 'i') { // 'sign: signed extra loads and stores
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000603 DCHECK(STRING_STARTS_WITH(format, "sign"));
Steve Blocka7e24c12009-10-30 11:49:00 +0000604 if (instr->HasSign()) {
605 Print("s");
606 }
607 return 4;
608 }
609 // 's: S field of data processing instructions
610 if (instr->HasS()) {
611 Print("s");
612 }
613 return 1;
614 }
615 case 't': { // 'target: target of branch instructions
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000616 DCHECK(STRING_STARTS_WITH(format, "target"));
Steve Block1e0659c2011-05-24 12:43:12 +0100617 int off = (instr->SImmed24Value() << 2) + 8;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000618 out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
619 "%+d -> %s",
620 off,
621 converter_.NameOfAddress(
622 reinterpret_cast<byte*>(instr) + off));
Steve Blocka7e24c12009-10-30 11:49:00 +0000623 return 6;
624 }
625 case 'u': { // 'u: signed or unsigned multiplies
626 // The manual gets the meaning of bit 22 backwards in the multiply
627 // instruction overview on page A3.16.2. The instructions that
628 // exist in u and s variants are the following:
629 // smull A4.1.87
630 // umull A4.1.129
631 // umlal A4.1.128
632 // smlal A4.1.76
633 // For these 0 means u and 1 means s. As can be seen on their individual
634 // pages. The other 18 mul instructions have the bit set or unset in
635 // arbitrary ways that are unrelated to the signedness of the instruction.
636 // None of these 18 instructions exist in both a 'u' and an 's' variant.
637
638 if (instr->Bit(22) == 0) {
639 Print("u");
640 } else {
641 Print("s");
642 }
643 return 1;
644 }
Steve Blockd0582a62009-12-15 09:54:21 +0000645 case 'v': {
646 return FormatVFPinstruction(instr, format);
647 }
648 case 'S':
649 case 'D': {
650 return FormatVFPRegister(instr, format);
651 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000652 case 'w': { // 'w: W field of load and store instructions
653 if (instr->HasW()) {
654 Print("!");
655 }
656 return 1;
657 }
658 default: {
659 UNREACHABLE();
660 break;
661 }
662 }
663 UNREACHABLE();
664 return -1;
665}
666
667
668// Format takes a formatting string for a whole instruction and prints it into
669// the output buffer. All escaped options are handed to FormatOption to be
670// parsed further.
Steve Block1e0659c2011-05-24 12:43:12 +0100671void Decoder::Format(Instruction* instr, const char* format) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000672 char cur = *format++;
673 while ((cur != 0) && (out_buffer_pos_ < (out_buffer_.length() - 1))) {
674 if (cur == '\'') { // Single quote is used as the formatting escape.
675 format += FormatOption(instr, format);
676 } else {
677 out_buffer_[out_buffer_pos_++] = cur;
678 }
679 cur = *format++;
680 }
681 out_buffer_[out_buffer_pos_] = '\0';
682}
683
684
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100685// The disassembler may end up decoding data inlined in the code. We do not want
686// it to crash if the data does not ressemble any known instruction.
687#define VERIFY(condition) \
688if(!(condition)) { \
689 Unknown(instr); \
690 return; \
691}
692
693
Steve Blocka7e24c12009-10-30 11:49:00 +0000694// For currently unimplemented decodings the disassembler calls Unknown(instr)
695// which will just print "unknown" of the instruction bits.
Steve Block1e0659c2011-05-24 12:43:12 +0100696void Decoder::Unknown(Instruction* instr) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000697 Format(instr, "unknown");
698}
699
700
Steve Block1e0659c2011-05-24 12:43:12 +0100701void Decoder::DecodeType01(Instruction* instr) {
702 int type = instr->TypeValue();
Steve Blocka7e24c12009-10-30 11:49:00 +0000703 if ((type == 0) && instr->IsSpecialType0()) {
704 // multiply instruction or extra loads and stores
705 if (instr->Bits(7, 4) == 9) {
706 if (instr->Bit(24) == 0) {
707 // multiply instructions
708 if (instr->Bit(23) == 0) {
709 if (instr->Bit(21) == 0) {
710 // The MUL instruction description (A 4.1.33) refers to Rd as being
711 // the destination for the operation, but it confusingly uses the
712 // Rn field to encode it.
713 Format(instr, "mul'cond's 'rn, 'rm, 'rs");
714 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000715 if (instr->Bit(22) == 0) {
716 // The MLA instruction description (A 4.1.28) refers to the order
717 // of registers as "Rd, Rm, Rs, Rn". But confusingly it uses the
718 // Rn field to encode the Rd register and the Rd field to encode
719 // the Rn register.
720 Format(instr, "mla'cond's 'rn, 'rm, 'rs, 'rd");
721 } else {
722 // The MLS instruction description (A 4.1.29) refers to the order
723 // of registers as "Rd, Rm, Rs, Rn". But confusingly it uses the
724 // Rn field to encode the Rd register and the Rd field to encode
725 // the Rn register.
726 Format(instr, "mls'cond's 'rn, 'rm, 'rs, 'rd");
727 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000728 }
729 } else {
730 // The signed/long multiply instructions use the terms RdHi and RdLo
731 // when referring to the target registers. They are mapped to the Rn
732 // and Rd fields as follows:
733 // RdLo == Rd field
734 // RdHi == Rn field
735 // The order of registers is: <RdLo>, <RdHi>, <Rm>, <Rs>
736 Format(instr, "'um'al'cond's 'rd, 'rn, 'rm, 'rs");
737 }
738 } else {
739 Unknown(instr); // not used by V8
740 }
Kristian Monsen25f61362010-05-21 11:50:48 +0100741 } else if ((instr->Bit(20) == 0) && ((instr->Bits(7, 4) & 0xd) == 0xd)) {
742 // ldrd, strd
743 switch (instr->PUField()) {
Steve Block1e0659c2011-05-24 12:43:12 +0100744 case da_x: {
Kristian Monsen25f61362010-05-21 11:50:48 +0100745 if (instr->Bit(22) == 0) {
746 Format(instr, "'memop'cond's 'rd, ['rn], -'rm");
747 } else {
748 Format(instr, "'memop'cond's 'rd, ['rn], #-'off8");
749 }
750 break;
751 }
Steve Block1e0659c2011-05-24 12:43:12 +0100752 case ia_x: {
Kristian Monsen25f61362010-05-21 11:50:48 +0100753 if (instr->Bit(22) == 0) {
754 Format(instr, "'memop'cond's 'rd, ['rn], +'rm");
755 } else {
756 Format(instr, "'memop'cond's 'rd, ['rn], #+'off8");
757 }
758 break;
759 }
Steve Block1e0659c2011-05-24 12:43:12 +0100760 case db_x: {
Kristian Monsen25f61362010-05-21 11:50:48 +0100761 if (instr->Bit(22) == 0) {
762 Format(instr, "'memop'cond's 'rd, ['rn, -'rm]'w");
763 } else {
764 Format(instr, "'memop'cond's 'rd, ['rn, #-'off8]'w");
765 }
766 break;
767 }
Steve Block1e0659c2011-05-24 12:43:12 +0100768 case ib_x: {
Kristian Monsen25f61362010-05-21 11:50:48 +0100769 if (instr->Bit(22) == 0) {
770 Format(instr, "'memop'cond's 'rd, ['rn, +'rm]'w");
771 } else {
772 Format(instr, "'memop'cond's 'rd, ['rn, #+'off8]'w");
773 }
774 break;
775 }
776 default: {
777 // The PU field is a 2-bit field.
778 UNREACHABLE();
779 break;
780 }
781 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000782 } else {
783 // extra load/store instructions
784 switch (instr->PUField()) {
Steve Block1e0659c2011-05-24 12:43:12 +0100785 case da_x: {
Steve Blocka7e24c12009-10-30 11:49:00 +0000786 if (instr->Bit(22) == 0) {
787 Format(instr, "'memop'cond'sign'h 'rd, ['rn], -'rm");
788 } else {
789 Format(instr, "'memop'cond'sign'h 'rd, ['rn], #-'off8");
790 }
791 break;
792 }
Steve Block1e0659c2011-05-24 12:43:12 +0100793 case ia_x: {
Steve Blocka7e24c12009-10-30 11:49:00 +0000794 if (instr->Bit(22) == 0) {
795 Format(instr, "'memop'cond'sign'h 'rd, ['rn], +'rm");
796 } else {
797 Format(instr, "'memop'cond'sign'h 'rd, ['rn], #+'off8");
798 }
799 break;
800 }
Steve Block1e0659c2011-05-24 12:43:12 +0100801 case db_x: {
Steve Blocka7e24c12009-10-30 11:49:00 +0000802 if (instr->Bit(22) == 0) {
803 Format(instr, "'memop'cond'sign'h 'rd, ['rn, -'rm]'w");
804 } else {
805 Format(instr, "'memop'cond'sign'h 'rd, ['rn, #-'off8]'w");
806 }
807 break;
808 }
Steve Block1e0659c2011-05-24 12:43:12 +0100809 case ib_x: {
Steve Blocka7e24c12009-10-30 11:49:00 +0000810 if (instr->Bit(22) == 0) {
811 Format(instr, "'memop'cond'sign'h 'rd, ['rn, +'rm]'w");
812 } else {
813 Format(instr, "'memop'cond'sign'h 'rd, ['rn, #+'off8]'w");
814 }
815 break;
816 }
817 default: {
818 // The PU field is a 2-bit field.
819 UNREACHABLE();
820 break;
821 }
822 }
823 return;
824 }
Steve Block6ded16b2010-05-10 14:33:55 +0100825 } else if ((type == 0) && instr->IsMiscType0()) {
826 if (instr->Bits(22, 21) == 1) {
Steve Block1e0659c2011-05-24 12:43:12 +0100827 switch (instr->BitField(7, 4)) {
Steve Block6ded16b2010-05-10 14:33:55 +0100828 case BX:
829 Format(instr, "bx'cond 'rm");
830 break;
831 case BLX:
832 Format(instr, "blx'cond 'rm");
833 break;
834 case BKPT:
835 Format(instr, "bkpt 'off0to3and8to19");
836 break;
837 default:
838 Unknown(instr); // not used by V8
839 break;
840 }
841 } else if (instr->Bits(22, 21) == 3) {
Steve Block1e0659c2011-05-24 12:43:12 +0100842 switch (instr->BitField(7, 4)) {
Steve Block6ded16b2010-05-10 14:33:55 +0100843 case CLZ:
844 Format(instr, "clz'cond 'rd, 'rm");
845 break;
846 default:
847 Unknown(instr); // not used by V8
848 break;
849 }
850 } else {
851 Unknown(instr); // not used by V8
852 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000853 } else if ((type == 1) && instr->IsNopType1()) {
854 Format(instr, "nop'cond");
Steve Blocka7e24c12009-10-30 11:49:00 +0000855 } else {
856 switch (instr->OpcodeField()) {
857 case AND: {
858 Format(instr, "and'cond's 'rd, 'rn, 'shift_op");
859 break;
860 }
861 case EOR: {
862 Format(instr, "eor'cond's 'rd, 'rn, 'shift_op");
863 break;
864 }
865 case SUB: {
866 Format(instr, "sub'cond's 'rd, 'rn, 'shift_op");
867 break;
868 }
869 case RSB: {
870 Format(instr, "rsb'cond's 'rd, 'rn, 'shift_op");
871 break;
872 }
873 case ADD: {
874 Format(instr, "add'cond's 'rd, 'rn, 'shift_op");
875 break;
876 }
877 case ADC: {
878 Format(instr, "adc'cond's 'rd, 'rn, 'shift_op");
879 break;
880 }
881 case SBC: {
882 Format(instr, "sbc'cond's 'rd, 'rn, 'shift_op");
883 break;
884 }
885 case RSC: {
886 Format(instr, "rsc'cond's 'rd, 'rn, 'shift_op");
887 break;
888 }
889 case TST: {
890 if (instr->HasS()) {
891 Format(instr, "tst'cond 'rn, 'shift_op");
892 } else {
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100893 Format(instr, "movw'cond 'mw");
Steve Blocka7e24c12009-10-30 11:49:00 +0000894 }
895 break;
896 }
897 case TEQ: {
898 if (instr->HasS()) {
899 Format(instr, "teq'cond 'rn, 'shift_op");
900 } else {
Steve Block6ded16b2010-05-10 14:33:55 +0100901 // Other instructions matching this pattern are handled in the
902 // miscellaneous instructions part above.
903 UNREACHABLE();
Steve Blocka7e24c12009-10-30 11:49:00 +0000904 }
905 break;
906 }
907 case CMP: {
908 if (instr->HasS()) {
909 Format(instr, "cmp'cond 'rn, 'shift_op");
910 } else {
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100911 Format(instr, "movt'cond 'mw");
Steve Blocka7e24c12009-10-30 11:49:00 +0000912 }
913 break;
914 }
915 case CMN: {
916 if (instr->HasS()) {
917 Format(instr, "cmn'cond 'rn, 'shift_op");
918 } else {
Steve Block6ded16b2010-05-10 14:33:55 +0100919 // Other instructions matching this pattern are handled in the
920 // miscellaneous instructions part above.
921 UNREACHABLE();
Steve Blocka7e24c12009-10-30 11:49:00 +0000922 }
923 break;
924 }
925 case ORR: {
926 Format(instr, "orr'cond's 'rd, 'rn, 'shift_op");
927 break;
928 }
929 case MOV: {
930 Format(instr, "mov'cond's 'rd, 'shift_op");
931 break;
932 }
933 case BIC: {
934 Format(instr, "bic'cond's 'rd, 'rn, 'shift_op");
935 break;
936 }
937 case MVN: {
938 Format(instr, "mvn'cond's 'rd, 'shift_op");
939 break;
940 }
941 default: {
942 // The Opcode field is a 4-bit field.
943 UNREACHABLE();
944 break;
945 }
946 }
947 }
948}
949
950
Steve Block1e0659c2011-05-24 12:43:12 +0100951void Decoder::DecodeType2(Instruction* instr) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000952 switch (instr->PUField()) {
Steve Block1e0659c2011-05-24 12:43:12 +0100953 case da_x: {
Steve Blocka7e24c12009-10-30 11:49:00 +0000954 if (instr->HasW()) {
955 Unknown(instr); // not used in V8
Steve Block44f0eee2011-05-26 01:26:41 +0100956 return;
Steve Blocka7e24c12009-10-30 11:49:00 +0000957 }
958 Format(instr, "'memop'cond'b 'rd, ['rn], #-'off12");
959 break;
960 }
Steve Block1e0659c2011-05-24 12:43:12 +0100961 case ia_x: {
Steve Blocka7e24c12009-10-30 11:49:00 +0000962 if (instr->HasW()) {
963 Unknown(instr); // not used in V8
Steve Block44f0eee2011-05-26 01:26:41 +0100964 return;
Steve Blocka7e24c12009-10-30 11:49:00 +0000965 }
966 Format(instr, "'memop'cond'b 'rd, ['rn], #+'off12");
967 break;
968 }
Steve Block1e0659c2011-05-24 12:43:12 +0100969 case db_x: {
Steve Blocka7e24c12009-10-30 11:49:00 +0000970 Format(instr, "'memop'cond'b 'rd, ['rn, #-'off12]'w");
971 break;
972 }
Steve Block1e0659c2011-05-24 12:43:12 +0100973 case ib_x: {
Steve Blocka7e24c12009-10-30 11:49:00 +0000974 Format(instr, "'memop'cond'b 'rd, ['rn, #+'off12]'w");
975 break;
976 }
977 default: {
978 // The PU field is a 2-bit field.
979 UNREACHABLE();
980 break;
981 }
982 }
983}
984
985
Steve Block1e0659c2011-05-24 12:43:12 +0100986void Decoder::DecodeType3(Instruction* instr) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000987 switch (instr->PUField()) {
Steve Block1e0659c2011-05-24 12:43:12 +0100988 case da_x: {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100989 VERIFY(!instr->HasW());
Steve Blocka7e24c12009-10-30 11:49:00 +0000990 Format(instr, "'memop'cond'b 'rd, ['rn], -'shift_rm");
991 break;
992 }
Steve Block1e0659c2011-05-24 12:43:12 +0100993 case ia_x: {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000994 if (instr->Bit(4) == 0) {
Kristian Monsen50ef84f2010-07-29 15:18:00 +0100995 Format(instr, "'memop'cond'b 'rd, ['rn], +'shift_rm");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000996 } else {
997 if (instr->Bit(5) == 0) {
998 switch (instr->Bits(22, 21)) {
999 case 0:
1000 if (instr->Bit(20) == 0) {
1001 if (instr->Bit(6) == 0) {
1002 Format(instr, "pkhbt'cond 'rd, 'rn, 'rm, lsl #'imm05@07");
1003 } else {
1004 if (instr->Bits(11, 7) == 0) {
1005 Format(instr, "pkhtb'cond 'rd, 'rn, 'rm, asr #32");
1006 } else {
1007 Format(instr, "pkhtb'cond 'rd, 'rn, 'rm, asr #'imm05@07");
1008 }
1009 }
1010 } else {
1011 UNREACHABLE();
1012 }
1013 break;
1014 case 1:
1015 UNREACHABLE();
1016 break;
1017 case 2:
1018 UNREACHABLE();
1019 break;
1020 case 3:
1021 Format(instr, "usat 'rd, #'imm05@16, 'rm'shift_sat");
1022 break;
1023 }
1024 } else {
1025 switch (instr->Bits(22, 21)) {
1026 case 0:
1027 UNREACHABLE();
1028 break;
1029 case 1:
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001030 if (instr->Bits(9, 6) == 1) {
1031 if (instr->Bit(20) == 0) {
1032 if (instr->Bits(19, 16) == 0xF) {
1033 switch (instr->Bits(11, 10)) {
1034 case 0:
1035 Format(instr, "sxtb'cond 'rd, 'rm");
1036 break;
1037 case 1:
1038 Format(instr, "sxtb'cond 'rd, 'rm, ror #8");
1039 break;
1040 case 2:
1041 Format(instr, "sxtb'cond 'rd, 'rm, ror #16");
1042 break;
1043 case 3:
1044 Format(instr, "sxtb'cond 'rd, 'rm, ror #24");
1045 break;
1046 }
1047 } else {
1048 switch (instr->Bits(11, 10)) {
1049 case 0:
1050 Format(instr, "sxtab'cond 'rd, 'rn, 'rm");
1051 break;
1052 case 1:
1053 Format(instr, "sxtab'cond 'rd, 'rn, 'rm, ror #8");
1054 break;
1055 case 2:
1056 Format(instr, "sxtab'cond 'rd, 'rn, 'rm, ror #16");
1057 break;
1058 case 3:
1059 Format(instr, "sxtab'cond 'rd, 'rn, 'rm, ror #24");
1060 break;
1061 }
1062 }
1063 } else {
1064 if (instr->Bits(19, 16) == 0xF) {
1065 switch (instr->Bits(11, 10)) {
1066 case 0:
1067 Format(instr, "sxth'cond 'rd, 'rm");
1068 break;
1069 case 1:
1070 Format(instr, "sxth'cond 'rd, 'rm, ror #8");
1071 break;
1072 case 2:
1073 Format(instr, "sxth'cond 'rd, 'rm, ror #16");
1074 break;
1075 case 3:
1076 Format(instr, "sxth'cond 'rd, 'rm, ror #24");
1077 break;
1078 }
1079 } else {
1080 switch (instr->Bits(11, 10)) {
1081 case 0:
1082 Format(instr, "sxtah'cond 'rd, 'rn, 'rm");
1083 break;
1084 case 1:
1085 Format(instr, "sxtah'cond 'rd, 'rn, 'rm, ror #8");
1086 break;
1087 case 2:
1088 Format(instr, "sxtah'cond 'rd, 'rn, 'rm, ror #16");
1089 break;
1090 case 3:
1091 Format(instr, "sxtah'cond 'rd, 'rn, 'rm, ror #24");
1092 break;
1093 }
1094 }
1095 }
1096 } else {
1097 UNREACHABLE();
1098 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001099 break;
1100 case 2:
1101 if ((instr->Bit(20) == 0) && (instr->Bits(9, 6) == 1)) {
1102 if (instr->Bits(19, 16) == 0xF) {
1103 switch (instr->Bits(11, 10)) {
1104 case 0:
1105 Format(instr, "uxtb16'cond 'rd, 'rm");
1106 break;
1107 case 1:
1108 Format(instr, "uxtb16'cond 'rd, 'rm, ror #8");
1109 break;
1110 case 2:
1111 Format(instr, "uxtb16'cond 'rd, 'rm, ror #16");
1112 break;
1113 case 3:
1114 Format(instr, "uxtb16'cond 'rd, 'rm, ror #24");
1115 break;
1116 }
1117 } else {
1118 UNREACHABLE();
1119 }
1120 } else {
1121 UNREACHABLE();
1122 }
1123 break;
1124 case 3:
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001125 if ((instr->Bits(9, 6) == 1)) {
1126 if ((instr->Bit(20) == 0)) {
1127 if (instr->Bits(19, 16) == 0xF) {
1128 switch (instr->Bits(11, 10)) {
1129 case 0:
1130 Format(instr, "uxtb'cond 'rd, 'rm");
1131 break;
1132 case 1:
1133 Format(instr, "uxtb'cond 'rd, 'rm, ror #8");
1134 break;
1135 case 2:
1136 Format(instr, "uxtb'cond 'rd, 'rm, ror #16");
1137 break;
1138 case 3:
1139 Format(instr, "uxtb'cond 'rd, 'rm, ror #24");
1140 break;
1141 }
1142 } else {
1143 switch (instr->Bits(11, 10)) {
1144 case 0:
1145 Format(instr, "uxtab'cond 'rd, 'rn, 'rm");
1146 break;
1147 case 1:
1148 Format(instr, "uxtab'cond 'rd, 'rn, 'rm, ror #8");
1149 break;
1150 case 2:
1151 Format(instr, "uxtab'cond 'rd, 'rn, 'rm, ror #16");
1152 break;
1153 case 3:
1154 Format(instr, "uxtab'cond 'rd, 'rn, 'rm, ror #24");
1155 break;
1156 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001157 }
1158 } else {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001159 if (instr->Bits(19, 16) == 0xF) {
1160 switch (instr->Bits(11, 10)) {
1161 case 0:
1162 Format(instr, "uxth'cond 'rd, 'rm");
1163 break;
1164 case 1:
1165 Format(instr, "uxth'cond 'rd, 'rm, ror #8");
1166 break;
1167 case 2:
1168 Format(instr, "uxth'cond 'rd, 'rm, ror #16");
1169 break;
1170 case 3:
1171 Format(instr, "uxth'cond 'rd, 'rm, ror #24");
1172 break;
1173 }
1174 } else {
1175 switch (instr->Bits(11, 10)) {
1176 case 0:
1177 Format(instr, "uxtah'cond 'rd, 'rn, 'rm");
1178 break;
1179 case 1:
1180 Format(instr, "uxtah'cond 'rd, 'rn, 'rm, ror #8");
1181 break;
1182 case 2:
1183 Format(instr, "uxtah'cond 'rd, 'rn, 'rm, ror #16");
1184 break;
1185 case 3:
1186 Format(instr, "uxtah'cond 'rd, 'rn, 'rm, ror #24");
1187 break;
1188 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001189 }
1190 }
1191 } else {
1192 UNREACHABLE();
1193 }
1194 break;
1195 }
1196 }
Kristian Monsen50ef84f2010-07-29 15:18:00 +01001197 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001198 break;
1199 }
Steve Block1e0659c2011-05-24 12:43:12 +01001200 case db_x: {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001201 if (instr->Bits(22, 20) == 0x5) {
1202 if (instr->Bits(7, 4) == 0x1) {
1203 if (instr->Bits(15, 12) == 0xF) {
1204 Format(instr, "smmul'cond 'rn, 'rm, 'rs");
1205 } else {
1206 // SMMLA (in V8 notation matching ARM ISA format)
1207 Format(instr, "smmla'cond 'rn, 'rm, 'rs, 'rd");
1208 }
1209 break;
1210 }
1211 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001212 if (FLAG_enable_sudiv) {
1213 if (instr->Bits(5, 4) == 0x1) {
1214 if ((instr->Bit(22) == 0x0) && (instr->Bit(20) == 0x1)) {
1215 if (instr->Bit(21) == 0x1) {
1216 // UDIV (in V8 notation matching ARM ISA format) rn = rm/rs
1217 Format(instr, "udiv'cond'b 'rn, 'rm, 'rs");
1218 } else {
1219 // SDIV (in V8 notation matching ARM ISA format) rn = rm/rs
1220 Format(instr, "sdiv'cond'b 'rn, 'rm, 'rs");
1221 }
1222 break;
1223 }
1224 }
1225 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001226 Format(instr, "'memop'cond'b 'rd, ['rn, -'shift_rm]'w");
1227 break;
1228 }
Steve Block1e0659c2011-05-24 12:43:12 +01001229 case ib_x: {
Andrei Popescu31002712010-02-23 13:46:05 +00001230 if (instr->HasW() && (instr->Bits(6, 4) == 0x5)) {
1231 uint32_t widthminus1 = static_cast<uint32_t>(instr->Bits(20, 16));
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001232 uint32_t lsbit = static_cast<uint32_t>(instr->Bits(11, 7));
Andrei Popescu31002712010-02-23 13:46:05 +00001233 uint32_t msbit = widthminus1 + lsbit;
1234 if (msbit <= 31) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001235 if (instr->Bit(22)) {
1236 Format(instr, "ubfx'cond 'rd, 'rm, 'f");
1237 } else {
1238 Format(instr, "sbfx'cond 'rd, 'rm, 'f");
1239 }
1240 } else {
1241 UNREACHABLE();
1242 }
1243 } else if (!instr->HasW() && (instr->Bits(6, 4) == 0x1)) {
1244 uint32_t lsbit = static_cast<uint32_t>(instr->Bits(11, 7));
1245 uint32_t msbit = static_cast<uint32_t>(instr->Bits(20, 16));
1246 if (msbit >= lsbit) {
Steve Block1e0659c2011-05-24 12:43:12 +01001247 if (instr->RmValue() == 15) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001248 Format(instr, "bfc'cond 'rd, 'f");
1249 } else {
1250 Format(instr, "bfi'cond 'rd, 'rm, 'f");
1251 }
Andrei Popescu31002712010-02-23 13:46:05 +00001252 } else {
1253 UNREACHABLE();
1254 }
1255 } else {
1256 Format(instr, "'memop'cond'b 'rd, ['rn, +'shift_rm]'w");
1257 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001258 break;
1259 }
1260 default: {
1261 // The PU field is a 2-bit field.
1262 UNREACHABLE();
1263 break;
1264 }
1265 }
1266}
1267
1268
Steve Block1e0659c2011-05-24 12:43:12 +01001269void Decoder::DecodeType4(Instruction* instr) {
Steve Block44f0eee2011-05-26 01:26:41 +01001270 if (instr->Bit(22) != 0) {
1271 // Privileged mode currently not supported.
1272 Unknown(instr);
Steve Blocka7e24c12009-10-30 11:49:00 +00001273 } else {
Steve Block44f0eee2011-05-26 01:26:41 +01001274 if (instr->HasL()) {
1275 Format(instr, "ldm'cond'pu 'rn'w, 'rlist");
1276 } else {
1277 Format(instr, "stm'cond'pu 'rn'w, 'rlist");
1278 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001279 }
1280}
1281
1282
Steve Block1e0659c2011-05-24 12:43:12 +01001283void Decoder::DecodeType5(Instruction* instr) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001284 Format(instr, "b'l'cond 'target");
1285}
1286
1287
Steve Block1e0659c2011-05-24 12:43:12 +01001288void Decoder::DecodeType6(Instruction* instr) {
Steve Blockd0582a62009-12-15 09:54:21 +00001289 DecodeType6CoprocessorIns(instr);
Steve Blocka7e24c12009-10-30 11:49:00 +00001290}
1291
1292
Steve Block1e0659c2011-05-24 12:43:12 +01001293int Decoder::DecodeType7(Instruction* instr) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001294 if (instr->Bit(24) == 1) {
Steve Block1e0659c2011-05-24 12:43:12 +01001295 if (instr->SvcValue() >= kStopCode) {
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001296 Format(instr, "stop'cond 'svc");
1297 // Also print the stop message. Its address is encoded
1298 // in the following 4 bytes.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001299 out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
1300 "\n %p %08x stop message: %s",
1301 reinterpret_cast<void*>(instr
1302 + Instruction::kInstrSize),
1303 *reinterpret_cast<uint32_t*>(instr
1304 + Instruction::kInstrSize),
1305 *reinterpret_cast<char**>(instr
1306 + Instruction::kInstrSize));
Steve Block1e0659c2011-05-24 12:43:12 +01001307 // We have decoded 2 * Instruction::kInstrSize bytes.
1308 return 2 * Instruction::kInstrSize;
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001309 } else {
1310 Format(instr, "svc'cond 'svc");
1311 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001312 } else {
Steve Blockd0582a62009-12-15 09:54:21 +00001313 DecodeTypeVFP(instr);
Steve Blocka7e24c12009-10-30 11:49:00 +00001314 }
Steve Block1e0659c2011-05-24 12:43:12 +01001315 return Instruction::kInstrSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00001316}
1317
1318
Steve Block1e0659c2011-05-24 12:43:12 +01001319// void Decoder::DecodeTypeVFP(Instruction* instr)
Leon Clarkee46be812010-01-19 14:06:41 +00001320// vmov: Sn = Rt
1321// vmov: Rt = Sn
1322// vcvt: Dd = Sm
1323// vcvt: Sd = Dm
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001324// vcvt.f64.s32 Dd, Dd, #<fbits>
Steve Block44f0eee2011-05-26 01:26:41 +01001325// Dd = vabs(Dm)
1326// Dd = vneg(Dm)
Leon Clarkee46be812010-01-19 14:06:41 +00001327// Dd = vadd(Dn, Dm)
1328// Dd = vsub(Dn, Dm)
1329// Dd = vmul(Dn, Dm)
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001330// Dd = vmla(Dn, Dm)
1331// Dd = vmls(Dn, Dm)
Leon Clarkee46be812010-01-19 14:06:41 +00001332// Dd = vdiv(Dn, Dm)
Steve Blockd0582a62009-12-15 09:54:21 +00001333// vcmp(Dd, Dm)
Steve Block8defd9f2010-07-08 12:39:36 +01001334// vmrs
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001335// vmsr
Steve Block8defd9f2010-07-08 12:39:36 +01001336// Dd = vsqrt(Dm)
Steve Block1e0659c2011-05-24 12:43:12 +01001337void Decoder::DecodeTypeVFP(Instruction* instr) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001338 VERIFY((instr->TypeValue() == 7) && (instr->Bit(24) == 0x0) );
1339 VERIFY(instr->Bits(11, 9) == 0x5);
Steve Blockd0582a62009-12-15 09:54:21 +00001340
Steve Block6ded16b2010-05-10 14:33:55 +01001341 if (instr->Bit(4) == 0) {
Steve Block1e0659c2011-05-24 12:43:12 +01001342 if (instr->Opc1Value() == 0x7) {
Steve Block6ded16b2010-05-10 14:33:55 +01001343 // Other data processing instructions
Steve Block1e0659c2011-05-24 12:43:12 +01001344 if ((instr->Opc2Value() == 0x0) && (instr->Opc3Value() == 0x1)) {
Steve Block8defd9f2010-07-08 12:39:36 +01001345 // vmov register to register.
Steve Block1e0659c2011-05-24 12:43:12 +01001346 if (instr->SzValue() == 0x1) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001347 Format(instr, "vmov'cond.f64 'Dd, 'Dm");
Steve Block8defd9f2010-07-08 12:39:36 +01001348 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001349 Format(instr, "vmov'cond.f32 'Sd, 'Sm");
Steve Block8defd9f2010-07-08 12:39:36 +01001350 }
Steve Block1e0659c2011-05-24 12:43:12 +01001351 } else if ((instr->Opc2Value() == 0x0) && (instr->Opc3Value() == 0x3)) {
1352 // vabs
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001353 Format(instr, "vabs'cond.f64 'Dd, 'Dm");
Steve Block44f0eee2011-05-26 01:26:41 +01001354 } else if ((instr->Opc2Value() == 0x1) && (instr->Opc3Value() == 0x1)) {
1355 // vneg
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001356 Format(instr, "vneg'cond.f64 'Dd, 'Dm");
Steve Block1e0659c2011-05-24 12:43:12 +01001357 } else if ((instr->Opc2Value() == 0x7) && (instr->Opc3Value() == 0x3)) {
Steve Block6ded16b2010-05-10 14:33:55 +01001358 DecodeVCVTBetweenDoubleAndSingle(instr);
Steve Block1e0659c2011-05-24 12:43:12 +01001359 } else if ((instr->Opc2Value() == 0x8) && (instr->Opc3Value() & 0x1)) {
Steve Block6ded16b2010-05-10 14:33:55 +01001360 DecodeVCVTBetweenFloatingPointAndInteger(instr);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001361 } else if ((instr->Opc2Value() == 0xA) && (instr->Opc3Value() == 0x3) &&
1362 (instr->Bit(8) == 1)) {
1363 // vcvt.f64.s32 Dd, Dd, #<fbits>
1364 int fraction_bits = 32 - ((instr->Bits(3, 0) << 1) | instr->Bit(5));
1365 Format(instr, "vcvt'cond.f64.s32 'Dd, 'Dd");
1366 out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
1367 ", #%d", fraction_bits);
Steve Block1e0659c2011-05-24 12:43:12 +01001368 } else if (((instr->Opc2Value() >> 1) == 0x6) &&
1369 (instr->Opc3Value() & 0x1)) {
Steve Block6ded16b2010-05-10 14:33:55 +01001370 DecodeVCVTBetweenFloatingPointAndInteger(instr);
Steve Block1e0659c2011-05-24 12:43:12 +01001371 } else if (((instr->Opc2Value() == 0x4) || (instr->Opc2Value() == 0x5)) &&
1372 (instr->Opc3Value() & 0x1)) {
Steve Block6ded16b2010-05-10 14:33:55 +01001373 DecodeVCMP(instr);
Steve Block1e0659c2011-05-24 12:43:12 +01001374 } else if (((instr->Opc2Value() == 0x1)) && (instr->Opc3Value() == 0x3)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001375 Format(instr, "vsqrt'cond.f64 'Dd, 'Dm");
Steve Block1e0659c2011-05-24 12:43:12 +01001376 } else if (instr->Opc3Value() == 0x0) {
1377 if (instr->SzValue() == 0x1) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001378 Format(instr, "vmov'cond.f64 'Dd, 'd");
Ben Murdoch3bec4d22010-07-22 14:51:16 +01001379 } else {
1380 Unknown(instr); // Not used by V8.
1381 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001382 } else if (((instr->Opc2Value() == 0x6)) && instr->Opc3Value() == 0x3) {
1383 bool dp_operation = (instr->SzValue() == 1);
1384 // vrintz - round towards zero (truncate)
1385 if (dp_operation) {
1386 Format(instr, "vrintz'cond.f64.f64 'Dd, 'Dm");
1387 } else {
1388 Unknown(instr); // Not used by V8.
1389 }
Steve Block6ded16b2010-05-10 14:33:55 +01001390 } else {
1391 Unknown(instr); // Not used by V8.
1392 }
Steve Block1e0659c2011-05-24 12:43:12 +01001393 } else if (instr->Opc1Value() == 0x3) {
1394 if (instr->SzValue() == 0x1) {
1395 if (instr->Opc3Value() & 0x1) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001396 Format(instr, "vsub'cond.f64 'Dd, 'Dn, 'Dm");
Steve Block6ded16b2010-05-10 14:33:55 +01001397 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001398 Format(instr, "vadd'cond.f64 'Dd, 'Dn, 'Dm");
Steve Block6ded16b2010-05-10 14:33:55 +01001399 }
1400 } else {
1401 Unknown(instr); // Not used by V8.
1402 }
Steve Block1e0659c2011-05-24 12:43:12 +01001403 } else if ((instr->Opc1Value() == 0x2) && !(instr->Opc3Value() & 0x1)) {
1404 if (instr->SzValue() == 0x1) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001405 Format(instr, "vmul'cond.f64 'Dd, 'Dn, 'Dm");
1406 } else {
1407 Unknown(instr); // Not used by V8.
1408 }
1409 } else if ((instr->Opc1Value() == 0x0) && !(instr->Opc3Value() & 0x1)) {
1410 if (instr->SzValue() == 0x1) {
1411 Format(instr, "vmla'cond.f64 'Dd, 'Dn, 'Dm");
1412 } else {
1413 Unknown(instr); // Not used by V8.
1414 }
1415 } else if ((instr->Opc1Value() == 0x0) && (instr->Opc3Value() & 0x1)) {
1416 if (instr->SzValue() == 0x1) {
1417 Format(instr, "vmls'cond.f64 'Dd, 'Dn, 'Dm");
Steve Block6ded16b2010-05-10 14:33:55 +01001418 } else {
1419 Unknown(instr); // Not used by V8.
1420 }
Steve Block1e0659c2011-05-24 12:43:12 +01001421 } else if ((instr->Opc1Value() == 0x4) && !(instr->Opc3Value() & 0x1)) {
1422 if (instr->SzValue() == 0x1) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001423 Format(instr, "vdiv'cond.f64 'Dd, 'Dn, 'Dm");
Steve Block6ded16b2010-05-10 14:33:55 +01001424 } else {
1425 Unknown(instr); // Not used by V8.
1426 }
Steve Blockd0582a62009-12-15 09:54:21 +00001427 } else {
1428 Unknown(instr); // Not used by V8.
1429 }
1430 } else {
Steve Block1e0659c2011-05-24 12:43:12 +01001431 if ((instr->VCValue() == 0x0) &&
1432 (instr->VAValue() == 0x0)) {
Steve Block6ded16b2010-05-10 14:33:55 +01001433 DecodeVMOVBetweenCoreAndSinglePrecisionRegisters(instr);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001434 } else if ((instr->VLValue() == 0x0) &&
1435 (instr->VCValue() == 0x1) &&
1436 (instr->Bit(23) == 0x0)) {
1437 if (instr->Bit(21) == 0x0) {
1438 Format(instr, "vmov'cond.32 'Dd[0], 'rt");
1439 } else {
1440 Format(instr, "vmov'cond.32 'Dd[1], 'rt");
1441 }
1442 } else if ((instr->VLValue() == 0x1) &&
1443 (instr->VCValue() == 0x1) &&
1444 (instr->Bit(23) == 0x0)) {
1445 if (instr->Bit(21) == 0x0) {
1446 Format(instr, "vmov'cond.32 'rt, 'Dd[0]");
1447 } else {
1448 Format(instr, "vmov'cond.32 'rt, 'Dd[1]");
1449 }
Steve Block1e0659c2011-05-24 12:43:12 +01001450 } else if ((instr->VCValue() == 0x0) &&
1451 (instr->VAValue() == 0x7) &&
Steve Block6ded16b2010-05-10 14:33:55 +01001452 (instr->Bits(19, 16) == 0x1)) {
Steve Block1e0659c2011-05-24 12:43:12 +01001453 if (instr->VLValue() == 0) {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001454 if (instr->Bits(15, 12) == 0xF) {
1455 Format(instr, "vmsr'cond FPSCR, APSR");
1456 } else {
1457 Format(instr, "vmsr'cond FPSCR, 'rt");
1458 }
1459 } else {
1460 if (instr->Bits(15, 12) == 0xF) {
1461 Format(instr, "vmrs'cond APSR, FPSCR");
1462 } else {
1463 Format(instr, "vmrs'cond 'rt, FPSCR");
1464 }
1465 }
Steve Blockd0582a62009-12-15 09:54:21 +00001466 }
1467 }
1468}
1469
1470
Steve Block1e0659c2011-05-24 12:43:12 +01001471void Decoder::DecodeVMOVBetweenCoreAndSinglePrecisionRegisters(
1472 Instruction* instr) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001473 VERIFY((instr->Bit(4) == 1) && (instr->VCValue() == 0x0) &&
Steve Block1e0659c2011-05-24 12:43:12 +01001474 (instr->VAValue() == 0x0));
Steve Block6ded16b2010-05-10 14:33:55 +01001475
Steve Block1e0659c2011-05-24 12:43:12 +01001476 bool to_arm_register = (instr->VLValue() == 0x1);
Steve Block6ded16b2010-05-10 14:33:55 +01001477
1478 if (to_arm_register) {
1479 Format(instr, "vmov'cond 'rt, 'Sn");
1480 } else {
1481 Format(instr, "vmov'cond 'Sn, 'rt");
1482 }
1483}
1484
1485
Steve Block1e0659c2011-05-24 12:43:12 +01001486void Decoder::DecodeVCMP(Instruction* instr) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001487 VERIFY((instr->Bit(4) == 0) && (instr->Opc1Value() == 0x7));
1488 VERIFY(((instr->Opc2Value() == 0x4) || (instr->Opc2Value() == 0x5)) &&
Steve Block1e0659c2011-05-24 12:43:12 +01001489 (instr->Opc3Value() & 0x1));
Steve Block6ded16b2010-05-10 14:33:55 +01001490
1491 // Comparison.
Steve Block1e0659c2011-05-24 12:43:12 +01001492 bool dp_operation = (instr->SzValue() == 1);
Steve Block6ded16b2010-05-10 14:33:55 +01001493 bool raise_exception_for_qnan = (instr->Bit(7) == 0x1);
1494
1495 if (dp_operation && !raise_exception_for_qnan) {
Steve Block1e0659c2011-05-24 12:43:12 +01001496 if (instr->Opc2Value() == 0x4) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001497 Format(instr, "vcmp'cond.f64 'Dd, 'Dm");
Steve Block1e0659c2011-05-24 12:43:12 +01001498 } else if (instr->Opc2Value() == 0x5) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001499 Format(instr, "vcmp'cond.f64 'Dd, #0.0");
Iain Merrick75681382010-08-19 15:07:18 +01001500 } else {
1501 Unknown(instr); // invalid
1502 }
Steve Block6ded16b2010-05-10 14:33:55 +01001503 } else {
1504 Unknown(instr); // Not used by V8.
1505 }
1506}
1507
1508
Steve Block1e0659c2011-05-24 12:43:12 +01001509void Decoder::DecodeVCVTBetweenDoubleAndSingle(Instruction* instr) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001510 VERIFY((instr->Bit(4) == 0) && (instr->Opc1Value() == 0x7));
1511 VERIFY((instr->Opc2Value() == 0x7) && (instr->Opc3Value() == 0x3));
Steve Block6ded16b2010-05-10 14:33:55 +01001512
Steve Block1e0659c2011-05-24 12:43:12 +01001513 bool double_to_single = (instr->SzValue() == 1);
Steve Block6ded16b2010-05-10 14:33:55 +01001514
1515 if (double_to_single) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001516 Format(instr, "vcvt'cond.f32.f64 'Sd, 'Dm");
Steve Block6ded16b2010-05-10 14:33:55 +01001517 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001518 Format(instr, "vcvt'cond.f64.f32 'Dd, 'Sm");
Steve Block6ded16b2010-05-10 14:33:55 +01001519 }
1520}
1521
1522
Steve Block1e0659c2011-05-24 12:43:12 +01001523void Decoder::DecodeVCVTBetweenFloatingPointAndInteger(Instruction* instr) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001524 VERIFY((instr->Bit(4) == 0) && (instr->Opc1Value() == 0x7));
1525 VERIFY(((instr->Opc2Value() == 0x8) && (instr->Opc3Value() & 0x1)) ||
Steve Block1e0659c2011-05-24 12:43:12 +01001526 (((instr->Opc2Value() >> 1) == 0x6) && (instr->Opc3Value() & 0x1)));
Steve Block6ded16b2010-05-10 14:33:55 +01001527
1528 bool to_integer = (instr->Bit(18) == 1);
Steve Block1e0659c2011-05-24 12:43:12 +01001529 bool dp_operation = (instr->SzValue() == 1);
Steve Block6ded16b2010-05-10 14:33:55 +01001530 if (to_integer) {
1531 bool unsigned_integer = (instr->Bit(16) == 0);
1532
1533 if (dp_operation) {
1534 if (unsigned_integer) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001535 Format(instr, "vcvt'cond.u32.f64 'Sd, 'Dm");
Steve Block6ded16b2010-05-10 14:33:55 +01001536 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001537 Format(instr, "vcvt'cond.s32.f64 'Sd, 'Dm");
Steve Block6ded16b2010-05-10 14:33:55 +01001538 }
1539 } else {
1540 if (unsigned_integer) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001541 Format(instr, "vcvt'cond.u32.f32 'Sd, 'Sm");
Steve Block6ded16b2010-05-10 14:33:55 +01001542 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001543 Format(instr, "vcvt'cond.s32.f32 'Sd, 'Sm");
Steve Block6ded16b2010-05-10 14:33:55 +01001544 }
1545 }
1546 } else {
1547 bool unsigned_integer = (instr->Bit(7) == 0);
1548
1549 if (dp_operation) {
1550 if (unsigned_integer) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001551 Format(instr, "vcvt'cond.f64.u32 'Dd, 'Sm");
Steve Block6ded16b2010-05-10 14:33:55 +01001552 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001553 Format(instr, "vcvt'cond.f64.s32 'Dd, 'Sm");
Steve Block6ded16b2010-05-10 14:33:55 +01001554 }
1555 } else {
1556 if (unsigned_integer) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001557 Format(instr, "vcvt'cond.f32.u32 'Sd, 'Sm");
Steve Block6ded16b2010-05-10 14:33:55 +01001558 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001559 Format(instr, "vcvt'cond.f32.s32 'Sd, 'Sm");
Steve Block6ded16b2010-05-10 14:33:55 +01001560 }
1561 }
1562 }
1563}
1564
1565
Steve Blockd0582a62009-12-15 09:54:21 +00001566// Decode Type 6 coprocessor instructions.
Leon Clarkee46be812010-01-19 14:06:41 +00001567// Dm = vmov(Rt, Rt2)
1568// <Rt, Rt2> = vmov(Dm)
Leon Clarked91b9f72010-01-27 17:25:45 +00001569// Ddst = MEM(Rbase + 4*offset).
1570// MEM(Rbase + 4*offset) = Dsrc.
Steve Block1e0659c2011-05-24 12:43:12 +01001571void Decoder::DecodeType6CoprocessorIns(Instruction* instr) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001572 VERIFY(instr->TypeValue() == 6);
Steve Blockd0582a62009-12-15 09:54:21 +00001573
Steve Block1e0659c2011-05-24 12:43:12 +01001574 if (instr->CoprocessorValue() == 0xA) {
1575 switch (instr->OpcodeValue()) {
Steve Block6ded16b2010-05-10 14:33:55 +01001576 case 0x8:
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001577 case 0xA:
Steve Block6ded16b2010-05-10 14:33:55 +01001578 if (instr->HasL()) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001579 Format(instr, "vldr'cond 'Sd, ['rn - 4*'imm08@00]");
Steve Block6ded16b2010-05-10 14:33:55 +01001580 } else {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001581 Format(instr, "vstr'cond 'Sd, ['rn - 4*'imm08@00]");
Steve Block6ded16b2010-05-10 14:33:55 +01001582 }
1583 break;
1584 case 0xC:
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001585 case 0xE:
Steve Block6ded16b2010-05-10 14:33:55 +01001586 if (instr->HasL()) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001587 Format(instr, "vldr'cond 'Sd, ['rn + 4*'imm08@00]");
Steve Block6ded16b2010-05-10 14:33:55 +01001588 } else {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001589 Format(instr, "vstr'cond 'Sd, ['rn + 4*'imm08@00]");
Steve Block6ded16b2010-05-10 14:33:55 +01001590 }
1591 break;
Ben Murdoch8b112d22011-06-08 16:22:53 +01001592 case 0x4:
1593 case 0x5:
1594 case 0x6:
1595 case 0x7:
1596 case 0x9:
1597 case 0xB: {
1598 bool to_vfp_register = (instr->VLValue() == 0x1);
1599 if (to_vfp_register) {
1600 Format(instr, "vldm'cond'pu 'rn'w, {'Sd-'Sd+}");
1601 } else {
1602 Format(instr, "vstm'cond'pu 'rn'w, {'Sd-'Sd+}");
1603 }
1604 break;
1605 }
Steve Block6ded16b2010-05-10 14:33:55 +01001606 default:
1607 Unknown(instr); // Not used by V8.
Steve Block6ded16b2010-05-10 14:33:55 +01001608 }
Steve Block1e0659c2011-05-24 12:43:12 +01001609 } else if (instr->CoprocessorValue() == 0xB) {
1610 switch (instr->OpcodeValue()) {
Leon Clarked91b9f72010-01-27 17:25:45 +00001611 case 0x2:
1612 // Load and store double to two GP registers
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001613 if (instr->Bits(7, 6) != 0 || instr->Bit(4) != 1) {
Leon Clarked91b9f72010-01-27 17:25:45 +00001614 Unknown(instr); // Not used by V8.
1615 } else if (instr->HasL()) {
1616 Format(instr, "vmov'cond 'rt, 'rn, 'Dm");
1617 } else {
1618 Format(instr, "vmov'cond 'Dm, 'rt, 'rn");
1619 }
1620 break;
1621 case 0x8:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001622 case 0xA:
Leon Clarked91b9f72010-01-27 17:25:45 +00001623 if (instr->HasL()) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001624 Format(instr, "vldr'cond 'Dd, ['rn - 4*'imm08@00]");
Leon Clarked91b9f72010-01-27 17:25:45 +00001625 } else {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001626 Format(instr, "vstr'cond 'Dd, ['rn - 4*'imm08@00]");
Leon Clarked91b9f72010-01-27 17:25:45 +00001627 }
1628 break;
1629 case 0xC:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001630 case 0xE:
Leon Clarked91b9f72010-01-27 17:25:45 +00001631 if (instr->HasL()) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001632 Format(instr, "vldr'cond 'Dd, ['rn + 4*'imm08@00]");
Leon Clarked91b9f72010-01-27 17:25:45 +00001633 } else {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001634 Format(instr, "vstr'cond 'Dd, ['rn + 4*'imm08@00]");
Leon Clarked91b9f72010-01-27 17:25:45 +00001635 }
1636 break;
Ben Murdoch8b112d22011-06-08 16:22:53 +01001637 case 0x4:
1638 case 0x5:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001639 case 0x6:
1640 case 0x7:
1641 case 0x9:
1642 case 0xB: {
Ben Murdoch8b112d22011-06-08 16:22:53 +01001643 bool to_vfp_register = (instr->VLValue() == 0x1);
1644 if (to_vfp_register) {
1645 Format(instr, "vldm'cond'pu 'rn'w, {'Dd-'Dd+}");
1646 } else {
1647 Format(instr, "vstm'cond'pu 'rn'w, {'Dd-'Dd+}");
1648 }
1649 break;
1650 }
Leon Clarked91b9f72010-01-27 17:25:45 +00001651 default:
1652 Unknown(instr); // Not used by V8.
Leon Clarked91b9f72010-01-27 17:25:45 +00001653 }
Steve Block6ded16b2010-05-10 14:33:55 +01001654 } else {
Steve Block44f0eee2011-05-26 01:26:41 +01001655 Unknown(instr); // Not used by V8.
1656 }
1657}
1658
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001659
1660void Decoder::DecodeSpecialCondition(Instruction* instr) {
1661 switch (instr->SpecialValue()) {
1662 case 5:
1663 if ((instr->Bits(18, 16) == 0) && (instr->Bits(11, 6) == 0x28) &&
1664 (instr->Bit(4) == 1)) {
1665 // vmovl signed
1666 if ((instr->VdValue() & 1) != 0) Unknown(instr);
1667 int Vd = (instr->Bit(22) << 3) | (instr->VdValue() >> 1);
1668 int Vm = (instr->Bit(5) << 4) | instr->VmValue();
1669 int imm3 = instr->Bits(21, 19);
1670 out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
1671 "vmovl.s%d q%d, d%d", imm3*8, Vd, Vm);
1672 } else {
1673 Unknown(instr);
1674 }
1675 break;
1676 case 7:
1677 if ((instr->Bits(18, 16) == 0) && (instr->Bits(11, 6) == 0x28) &&
1678 (instr->Bit(4) == 1)) {
1679 // vmovl unsigned
1680 if ((instr->VdValue() & 1) != 0) Unknown(instr);
1681 int Vd = (instr->Bit(22) << 3) | (instr->VdValue() >> 1);
1682 int Vm = (instr->Bit(5) << 4) | instr->VmValue();
1683 int imm3 = instr->Bits(21, 19);
1684 out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
1685 "vmovl.u%d q%d, d%d", imm3*8, Vd, Vm);
1686 } else {
1687 Unknown(instr);
1688 }
1689 break;
1690 case 8:
1691 if (instr->Bits(21, 20) == 0) {
1692 // vst1
1693 int Vd = (instr->Bit(22) << 4) | instr->VdValue();
1694 int Rn = instr->VnValue();
1695 int type = instr->Bits(11, 8);
1696 int size = instr->Bits(7, 6);
1697 int align = instr->Bits(5, 4);
1698 int Rm = instr->VmValue();
1699 out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
1700 "vst1.%d ", (1 << size) << 3);
1701 FormatNeonList(Vd, type);
1702 Print(", ");
1703 FormatNeonMemory(Rn, align, Rm);
1704 } else if (instr->Bits(21, 20) == 2) {
1705 // vld1
1706 int Vd = (instr->Bit(22) << 4) | instr->VdValue();
1707 int Rn = instr->VnValue();
1708 int type = instr->Bits(11, 8);
1709 int size = instr->Bits(7, 6);
1710 int align = instr->Bits(5, 4);
1711 int Rm = instr->VmValue();
1712 out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
1713 "vld1.%d ", (1 << size) << 3);
1714 FormatNeonList(Vd, type);
1715 Print(", ");
1716 FormatNeonMemory(Rn, align, Rm);
1717 } else {
1718 Unknown(instr);
1719 }
1720 break;
1721 case 0xA:
1722 case 0xB:
1723 if ((instr->Bits(22, 20) == 5) && (instr->Bits(15, 12) == 0xf)) {
1724 int Rn = instr->Bits(19, 16);
1725 int offset = instr->Bits(11, 0);
1726 if (offset == 0) {
1727 out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
1728 "pld [r%d]", Rn);
1729 } else if (instr->Bit(23) == 0) {
1730 out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
1731 "pld [r%d, #-%d]", Rn, offset);
1732 } else {
1733 out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
1734 "pld [r%d, #+%d]", Rn, offset);
1735 }
1736 } else {
1737 Unknown(instr);
1738 }
1739 break;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001740 case 0x1D:
1741 if (instr->Opc1Value() == 0x7 && instr->Bits(19, 18) == 0x2 &&
1742 instr->Bits(11, 9) == 0x5 && instr->Bits(7, 6) == 0x1 &&
1743 instr->Bit(4) == 0x0) {
1744 // VRINTA, VRINTN, VRINTP, VRINTM (floating-point)
1745 bool dp_operation = (instr->SzValue() == 1);
1746 int rounding_mode = instr->Bits(17, 16);
1747 switch (rounding_mode) {
1748 case 0x0:
1749 if (dp_operation) {
1750 Format(instr, "vrinta.f64.f64 'Dd, 'Dm");
1751 } else {
1752 Unknown(instr);
1753 }
1754 break;
1755 case 0x1:
1756 if (dp_operation) {
1757 Format(instr, "vrintn.f64.f64 'Dd, 'Dm");
1758 } else {
1759 Unknown(instr);
1760 }
1761 break;
1762 case 0x2:
1763 if (dp_operation) {
1764 Format(instr, "vrintp.f64.f64 'Dd, 'Dm");
1765 } else {
1766 Unknown(instr);
1767 }
1768 break;
1769 case 0x3:
1770 if (dp_operation) {
1771 Format(instr, "vrintm.f64.f64 'Dd, 'Dm");
1772 } else {
1773 Unknown(instr);
1774 }
1775 break;
1776 default:
1777 UNREACHABLE(); // Case analysis is exhaustive.
1778 break;
1779 }
1780 } else {
1781 Unknown(instr);
1782 }
1783 break;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001784 default:
1785 Unknown(instr);
1786 break;
1787 }
1788}
1789
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001790#undef VERIFIY
Steve Block44f0eee2011-05-26 01:26:41 +01001791
1792bool Decoder::IsConstantPoolAt(byte* instr_ptr) {
1793 int instruction_bits = *(reinterpret_cast<int*>(instr_ptr));
1794 return (instruction_bits & kConstantPoolMarkerMask) == kConstantPoolMarker;
1795}
1796
1797
1798int Decoder::ConstantPoolSizeAt(byte* instr_ptr) {
1799 if (IsConstantPoolAt(instr_ptr)) {
1800 int instruction_bits = *(reinterpret_cast<int*>(instr_ptr));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001801 return DecodeConstantPoolLength(instruction_bits);
Steve Block44f0eee2011-05-26 01:26:41 +01001802 } else {
1803 return -1;
Steve Blockd0582a62009-12-15 09:54:21 +00001804 }
1805}
1806
1807
Steve Blocka7e24c12009-10-30 11:49:00 +00001808// Disassemble the instruction at *instr_ptr into the output buffer.
1809int Decoder::InstructionDecode(byte* instr_ptr) {
Steve Block1e0659c2011-05-24 12:43:12 +01001810 Instruction* instr = Instruction::At(instr_ptr);
Steve Blocka7e24c12009-10-30 11:49:00 +00001811 // Print raw instruction bytes.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001812 out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
1813 "%08x ",
1814 instr->InstructionBits());
Steve Block1e0659c2011-05-24 12:43:12 +01001815 if (instr->ConditionField() == kSpecialCondition) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001816 DecodeSpecialCondition(instr);
Steve Block44f0eee2011-05-26 01:26:41 +01001817 return Instruction::kInstrSize;
1818 }
1819 int instruction_bits = *(reinterpret_cast<int*>(instr_ptr));
1820 if ((instruction_bits & kConstantPoolMarkerMask) == kConstantPoolMarker) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001821 out_buffer_pos_ += SNPrintF(out_buffer_ + out_buffer_pos_,
1822 "constant pool begin (length %d)",
1823 DecodeConstantPoolLength(instruction_bits));
Steve Block1e0659c2011-05-24 12:43:12 +01001824 return Instruction::kInstrSize;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001825 } else if (instruction_bits == kCodeAgeJumpInstruction) {
1826 // The code age prologue has a constant immediatly following the jump
1827 // instruction.
1828 Instruction* target = Instruction::At(instr_ptr + Instruction::kInstrSize);
1829 DecodeType2(instr);
1830 SNPrintF(out_buffer_ + out_buffer_pos_,
1831 " (0x%08x)", target->InstructionBits());
1832 return 2 * Instruction::kInstrSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00001833 }
Steve Block1e0659c2011-05-24 12:43:12 +01001834 switch (instr->TypeValue()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001835 case 0:
1836 case 1: {
1837 DecodeType01(instr);
1838 break;
1839 }
1840 case 2: {
1841 DecodeType2(instr);
1842 break;
1843 }
1844 case 3: {
1845 DecodeType3(instr);
1846 break;
1847 }
1848 case 4: {
1849 DecodeType4(instr);
1850 break;
1851 }
1852 case 5: {
1853 DecodeType5(instr);
1854 break;
1855 }
1856 case 6: {
1857 DecodeType6(instr);
1858 break;
1859 }
1860 case 7: {
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001861 return DecodeType7(instr);
Steve Blocka7e24c12009-10-30 11:49:00 +00001862 }
1863 default: {
1864 // The type field is 3-bits in the ARM encoding.
1865 UNREACHABLE();
1866 break;
1867 }
1868 }
Steve Block1e0659c2011-05-24 12:43:12 +01001869 return Instruction::kInstrSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00001870}
1871
1872
Steve Block1e0659c2011-05-24 12:43:12 +01001873} } // namespace v8::internal
Steve Blocka7e24c12009-10-30 11:49:00 +00001874
1875
1876
1877//------------------------------------------------------------------------------
1878
1879namespace disasm {
1880
Steve Blocka7e24c12009-10-30 11:49:00 +00001881
1882const char* NameConverter::NameOfAddress(byte* addr) const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001883 v8::internal::SNPrintF(tmp_buffer_, "%p", addr);
Steve Block44f0eee2011-05-26 01:26:41 +01001884 return tmp_buffer_.start();
Steve Blocka7e24c12009-10-30 11:49:00 +00001885}
1886
1887
1888const char* NameConverter::NameOfConstant(byte* addr) const {
1889 return NameOfAddress(addr);
1890}
1891
1892
1893const char* NameConverter::NameOfCPURegister(int reg) const {
Steve Block1e0659c2011-05-24 12:43:12 +01001894 return v8::internal::Registers::Name(reg);
Steve Blocka7e24c12009-10-30 11:49:00 +00001895}
1896
1897
1898const char* NameConverter::NameOfByteCPURegister(int reg) const {
1899 UNREACHABLE(); // ARM does not have the concept of a byte register
1900 return "nobytereg";
1901}
1902
1903
1904const char* NameConverter::NameOfXMMRegister(int reg) const {
1905 UNREACHABLE(); // ARM does not have any XMM registers
1906 return "noxmmreg";
1907}
1908
1909
1910const char* NameConverter::NameInCode(byte* addr) const {
1911 // The default name converter is called for unknown code. So we will not try
1912 // to access any memory.
1913 return "";
1914}
1915
1916
1917//------------------------------------------------------------------------------
1918
1919Disassembler::Disassembler(const NameConverter& converter)
1920 : converter_(converter) {}
1921
1922
1923Disassembler::~Disassembler() {}
1924
1925
1926int Disassembler::InstructionDecode(v8::internal::Vector<char> buffer,
1927 byte* instruction) {
Steve Block1e0659c2011-05-24 12:43:12 +01001928 v8::internal::Decoder d(converter_, buffer);
Steve Blocka7e24c12009-10-30 11:49:00 +00001929 return d.InstructionDecode(instruction);
1930}
1931
1932
1933int Disassembler::ConstantPoolSizeAt(byte* instruction) {
Steve Block44f0eee2011-05-26 01:26:41 +01001934 return v8::internal::Decoder::ConstantPoolSizeAt(instruction);
Steve Blocka7e24c12009-10-30 11:49:00 +00001935}
1936
1937
1938void Disassembler::Disassemble(FILE* f, byte* begin, byte* end) {
1939 NameConverter converter;
1940 Disassembler d(converter);
1941 for (byte* pc = begin; pc < end;) {
1942 v8::internal::EmbeddedVector<char, 128> buffer;
1943 buffer[0] = '\0';
1944 byte* prev_pc = pc;
1945 pc += d.InstructionDecode(buffer, pc);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001946 v8::internal::PrintF(
1947 f, "%p %08x %s\n",
1948 prev_pc, *reinterpret_cast<int32_t*>(prev_pc), buffer.start());
Steve Blocka7e24c12009-10-30 11:49:00 +00001949 }
1950}
1951
1952
1953} // namespace disasm
Leon Clarkef7060e22010-06-03 12:02:55 +01001954
1955#endif // V8_TARGET_ARCH_ARM