blob: 297a2db5b2eb1650b3cea33601db66b073924574 [file] [log] [blame]
Leon Clarked91b9f72010-01-27 17:25:45 +00001// Copyright 2010 the V8 project authors. All rights reserved.
Steve Blocka7e24c12009-10-30 11:49:00 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28// A Disassembler object is used to disassemble a block of code instruction by
29// instruction. The default implementation of the NameConverter object can be
30// overriden to modify register names or to do symbol lookup on addresses.
31//
32// The example below will disassemble a block of code and print it to stdout.
33//
34// NameConverter converter;
35// Disassembler d(converter);
36// for (byte* pc = begin; pc < end;) {
Steve Block6ded16b2010-05-10 14:33:55 +010037// v8::internal::EmbeddedVector<char, 256> buffer;
Steve Blocka7e24c12009-10-30 11:49:00 +000038// byte* prev_pc = pc;
Steve Block6ded16b2010-05-10 14:33:55 +010039// pc += d.InstructionDecode(buffer, pc);
Steve Blocka7e24c12009-10-30 11:49:00 +000040// printf("%p %08x %s\n",
41// prev_pc, *reinterpret_cast<int32_t*>(prev_pc), buffer);
42// }
43//
44// The Disassembler class also has a convenience method to disassemble a block
45// of code into a FILE*, meaning that the above functionality could also be
46// achieved by just calling Disassembler::Disassemble(stdout, begin, end);
47
48
49#include <assert.h>
50#include <stdio.h>
51#include <stdarg.h>
52#include <string.h>
53#ifndef WIN32
54#include <stdint.h>
55#endif
56
57#include "v8.h"
58
Leon Clarkef7060e22010-06-03 12:02:55 +010059#if defined(V8_TARGET_ARCH_ARM)
60
Steve Blocka7e24c12009-10-30 11:49:00 +000061#include "constants-arm.h"
62#include "disasm.h"
63#include "macro-assembler.h"
64#include "platform.h"
65
66
67namespace assembler {
68namespace arm {
69
70namespace v8i = v8::internal;
71
72
73//------------------------------------------------------------------------------
74
75// Decoder decodes and disassembles instructions into an output buffer.
76// It uses the converter to convert register names and call destinations into
77// more informative description.
78class Decoder {
79 public:
80 Decoder(const disasm::NameConverter& converter,
81 v8::internal::Vector<char> out_buffer)
82 : converter_(converter),
83 out_buffer_(out_buffer),
84 out_buffer_pos_(0) {
85 out_buffer_[out_buffer_pos_] = '\0';
86 }
87
88 ~Decoder() {}
89
90 // Writes one disassembled instruction into 'buffer' (0-terminated).
91 // Returns the length of the disassembled machine instruction in bytes.
92 int InstructionDecode(byte* instruction);
93
94 private:
95 // Bottleneck functions to print into the out_buffer.
96 void PrintChar(const char ch);
97 void Print(const char* str);
98
99 // Printing of common values.
100 void PrintRegister(int reg);
Steve Blockd0582a62009-12-15 09:54:21 +0000101 void PrintSRegister(int reg);
102 void PrintDRegister(int reg);
103 int FormatVFPRegister(Instr* instr, const char* format);
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100104 void PrintMovwMovt(Instr* instr);
Steve Blockd0582a62009-12-15 09:54:21 +0000105 int FormatVFPinstruction(Instr* instr, const char* format);
Steve Blocka7e24c12009-10-30 11:49:00 +0000106 void PrintCondition(Instr* instr);
107 void PrintShiftRm(Instr* instr);
108 void PrintShiftImm(Instr* instr);
Kristian Monsen50ef84f2010-07-29 15:18:00 +0100109 void PrintShiftSat(Instr* instr);
Steve Blocka7e24c12009-10-30 11:49:00 +0000110 void PrintPU(Instr* instr);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800111 void PrintSoftwareInterrupt(SoftwareInterruptCodes svc);
Steve Blocka7e24c12009-10-30 11:49:00 +0000112
113 // Handle formatting of instructions and their options.
114 int FormatRegister(Instr* instr, const char* option);
115 int FormatOption(Instr* instr, const char* option);
116 void Format(Instr* instr, const char* format);
117 void Unknown(Instr* instr);
118
119 // Each of these functions decodes one particular instruction type, a 3-bit
120 // field in the instruction encoding.
121 // Types 0 and 1 are combined as they are largely the same except for the way
122 // they interpret the shifter operand.
123 void DecodeType01(Instr* instr);
124 void DecodeType2(Instr* instr);
125 void DecodeType3(Instr* instr);
126 void DecodeType4(Instr* instr);
127 void DecodeType5(Instr* instr);
128 void DecodeType6(Instr* instr);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800129 // Type 7 includes special Debugger instructions.
130 int DecodeType7(Instr* instr);
Steve Blockd0582a62009-12-15 09:54:21 +0000131 // For VFP support.
132 void DecodeTypeVFP(Instr* instr);
133 void DecodeType6CoprocessorIns(Instr* instr);
134
Steve Block6ded16b2010-05-10 14:33:55 +0100135 void DecodeVMOVBetweenCoreAndSinglePrecisionRegisters(Instr* instr);
136 void DecodeVCMP(Instr* instr);
137 void DecodeVCVTBetweenDoubleAndSingle(Instr* instr);
138 void DecodeVCVTBetweenFloatingPointAndInteger(Instr* instr);
Steve Blocka7e24c12009-10-30 11:49:00 +0000139
140 const disasm::NameConverter& converter_;
141 v8::internal::Vector<char> out_buffer_;
142 int out_buffer_pos_;
143
144 DISALLOW_COPY_AND_ASSIGN(Decoder);
145};
146
147
148// Support for assertions in the Decoder formatting functions.
149#define STRING_STARTS_WITH(string, compare_string) \
150 (strncmp(string, compare_string, strlen(compare_string)) == 0)
151
152
153// Append the ch to the output buffer.
154void Decoder::PrintChar(const char ch) {
155 out_buffer_[out_buffer_pos_++] = ch;
156}
157
158
159// Append the str to the output buffer.
160void Decoder::Print(const char* str) {
161 char cur = *str++;
162 while (cur != '\0' && (out_buffer_pos_ < (out_buffer_.length() - 1))) {
163 PrintChar(cur);
164 cur = *str++;
165 }
166 out_buffer_[out_buffer_pos_] = 0;
167}
168
169
170// These condition names are defined in a way to match the native disassembler
171// formatting. See for example the command "objdump -d <binary file>".
172static const char* cond_names[max_condition] = {
173 "eq", "ne", "cs" , "cc" , "mi" , "pl" , "vs" , "vc" ,
174 "hi", "ls", "ge", "lt", "gt", "le", "", "invalid",
175};
176
177
178// Print the condition guarding the instruction.
179void Decoder::PrintCondition(Instr* instr) {
180 Print(cond_names[instr->ConditionField()]);
181}
182
183
184// Print the register name according to the active name converter.
185void Decoder::PrintRegister(int reg) {
186 Print(converter_.NameOfCPURegister(reg));
187}
188
Steve Blockd0582a62009-12-15 09:54:21 +0000189// Print the VFP S register name according to the active name converter.
190void Decoder::PrintSRegister(int reg) {
Steve Block6ded16b2010-05-10 14:33:55 +0100191 Print(assembler::arm::VFPRegisters::Name(reg, false));
Steve Blockd0582a62009-12-15 09:54:21 +0000192}
193
194// Print the VFP D register name according to the active name converter.
195void Decoder::PrintDRegister(int reg) {
Steve Block6ded16b2010-05-10 14:33:55 +0100196 Print(assembler::arm::VFPRegisters::Name(reg, true));
Steve Blockd0582a62009-12-15 09:54:21 +0000197}
198
Steve Blocka7e24c12009-10-30 11:49:00 +0000199
200// These shift names are defined in a way to match the native disassembler
201// formatting. See for example the command "objdump -d <binary file>".
202static const char* shift_names[max_shift] = {
203 "lsl", "lsr", "asr", "ror"
204};
205
206
207// Print the register shift operands for the instruction. Generally used for
208// data processing instructions.
209void Decoder::PrintShiftRm(Instr* instr) {
210 Shift shift = instr->ShiftField();
211 int shift_amount = instr->ShiftAmountField();
212 int rm = instr->RmField();
213
214 PrintRegister(rm);
215
216 if ((instr->RegShiftField() == 0) && (shift == LSL) && (shift_amount == 0)) {
217 // Special case for using rm only.
218 return;
219 }
220 if (instr->RegShiftField() == 0) {
221 // by immediate
222 if ((shift == ROR) && (shift_amount == 0)) {
223 Print(", RRX");
224 return;
225 } else if (((shift == LSR) || (shift == ASR)) && (shift_amount == 0)) {
226 shift_amount = 32;
227 }
228 out_buffer_pos_ += v8i::OS::SNPrintF(out_buffer_ + out_buffer_pos_,
229 ", %s #%d",
230 shift_names[shift], shift_amount);
231 } else {
232 // by register
233 int rs = instr->RsField();
234 out_buffer_pos_ += v8i::OS::SNPrintF(out_buffer_ + out_buffer_pos_,
235 ", %s ", shift_names[shift]);
236 PrintRegister(rs);
237 }
238}
239
240
241// Print the immediate operand for the instruction. Generally used for data
242// processing instructions.
243void Decoder::PrintShiftImm(Instr* instr) {
244 int rotate = instr->RotateField() * 2;
245 int immed8 = instr->Immed8Field();
246 int imm = (immed8 >> rotate) | (immed8 << (32 - rotate));
247 out_buffer_pos_ += v8i::OS::SNPrintF(out_buffer_ + out_buffer_pos_,
248 "#%d", imm);
249}
250
251
Kristian Monsen50ef84f2010-07-29 15:18:00 +0100252// Print the optional shift and immediate used by saturating instructions.
253void Decoder::PrintShiftSat(Instr* instr) {
254 int shift = instr->Bits(11, 7);
255 if (shift > 0) {
256 out_buffer_pos_ += v8i::OS::SNPrintF(out_buffer_ + out_buffer_pos_,
257 ", %s #%d",
258 shift_names[instr->Bit(6) * 2],
259 instr->Bits(11, 7));
260 }
261}
262
263
Steve Blocka7e24c12009-10-30 11:49:00 +0000264// Print PU formatting to reduce complexity of FormatOption.
265void Decoder::PrintPU(Instr* instr) {
266 switch (instr->PUField()) {
267 case 0: {
268 Print("da");
269 break;
270 }
271 case 1: {
272 Print("ia");
273 break;
274 }
275 case 2: {
276 Print("db");
277 break;
278 }
279 case 3: {
280 Print("ib");
281 break;
282 }
283 default: {
284 UNREACHABLE();
285 break;
286 }
287 }
288}
289
290
291// Print SoftwareInterrupt codes. Factoring this out reduces the complexity of
292// the FormatOption method.
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800293void Decoder::PrintSoftwareInterrupt(SoftwareInterruptCodes svc) {
294 switch (svc) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000295 case call_rt_redirected:
296 Print("call_rt_redirected");
297 return;
298 case break_point:
299 Print("break_point");
300 return;
301 default:
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800302 if (svc >= stop) {
303 out_buffer_pos_ += v8i::OS::SNPrintF(out_buffer_ + out_buffer_pos_,
304 "%d - 0x%x",
305 svc & kStopCodeMask,
306 svc & kStopCodeMask);
307 } else {
308 out_buffer_pos_ += v8i::OS::SNPrintF(out_buffer_ + out_buffer_pos_,
309 "%d",
310 svc);
311 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000312 return;
313 }
314}
315
316
317// Handle all register based formatting in this function to reduce the
318// complexity of FormatOption.
319int Decoder::FormatRegister(Instr* instr, const char* format) {
320 ASSERT(format[0] == 'r');
321 if (format[1] == 'n') { // 'rn: Rn register
322 int reg = instr->RnField();
323 PrintRegister(reg);
324 return 2;
325 } else if (format[1] == 'd') { // 'rd: Rd register
326 int reg = instr->RdField();
327 PrintRegister(reg);
328 return 2;
329 } else if (format[1] == 's') { // 'rs: Rs register
330 int reg = instr->RsField();
331 PrintRegister(reg);
332 return 2;
333 } else if (format[1] == 'm') { // 'rm: Rm register
334 int reg = instr->RmField();
335 PrintRegister(reg);
336 return 2;
Steve Blockd0582a62009-12-15 09:54:21 +0000337 } else if (format[1] == 't') { // 'rt: Rt register
338 int reg = instr->RtField();
339 PrintRegister(reg);
340 return 2;
Steve Blocka7e24c12009-10-30 11:49:00 +0000341 } else if (format[1] == 'l') {
342 // 'rlist: register list for load and store multiple instructions
343 ASSERT(STRING_STARTS_WITH(format, "rlist"));
344 int rlist = instr->RlistField();
345 int reg = 0;
346 Print("{");
347 // Print register list in ascending order, by scanning the bit mask.
348 while (rlist != 0) {
349 if ((rlist & 1) != 0) {
350 PrintRegister(reg);
351 if ((rlist >> 1) != 0) {
352 Print(", ");
353 }
354 }
355 reg++;
356 rlist >>= 1;
357 }
358 Print("}");
359 return 5;
360 }
361 UNREACHABLE();
362 return -1;
363}
364
365
Steve Blockd0582a62009-12-15 09:54:21 +0000366// Handle all VFP register based formatting in this function to reduce the
367// complexity of FormatOption.
368int Decoder::FormatVFPRegister(Instr* instr, const char* format) {
369 ASSERT((format[0] == 'S') || (format[0] == 'D'));
370
371 if (format[1] == 'n') {
372 int reg = instr->VnField();
373 if (format[0] == 'S') PrintSRegister(((reg << 1) | instr->NField()));
374 if (format[0] == 'D') PrintDRegister(reg);
375 return 2;
376 } else if (format[1] == 'm') {
377 int reg = instr->VmField();
378 if (format[0] == 'S') PrintSRegister(((reg << 1) | instr->MField()));
379 if (format[0] == 'D') PrintDRegister(reg);
380 return 2;
381 } else if (format[1] == 'd') {
382 int reg = instr->VdField();
383 if (format[0] == 'S') PrintSRegister(((reg << 1) | instr->DField()));
384 if (format[0] == 'D') PrintDRegister(reg);
385 return 2;
386 }
387
388 UNREACHABLE();
389 return -1;
390}
391
392
393int Decoder::FormatVFPinstruction(Instr* instr, const char* format) {
394 Print(format);
395 return 0;
396}
397
398
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100399// Print the movw or movt instruction.
400void Decoder::PrintMovwMovt(Instr* instr) {
401 int imm = instr->ImmedMovwMovtField();
402 int rd = instr->RdField();
403 PrintRegister(rd);
404 out_buffer_pos_ += v8i::OS::SNPrintF(out_buffer_ + out_buffer_pos_,
405 ", #%d", imm);
406}
407
408
Steve Blocka7e24c12009-10-30 11:49:00 +0000409// FormatOption takes a formatting string and interprets it based on
410// the current instructions. The format string points to the first
411// character of the option string (the option escape has already been
412// consumed by the caller.) FormatOption returns the number of
413// characters that were consumed from the formatting string.
414int Decoder::FormatOption(Instr* instr, const char* format) {
415 switch (format[0]) {
416 case 'a': { // 'a: accumulate multiplies
417 if (instr->Bit(21) == 0) {
418 Print("ul");
419 } else {
420 Print("la");
421 }
422 return 1;
423 }
424 case 'b': { // 'b: byte loads or stores
425 if (instr->HasB()) {
426 Print("b");
427 }
428 return 1;
429 }
430 case 'c': { // 'cond: conditional execution
431 ASSERT(STRING_STARTS_WITH(format, "cond"));
432 PrintCondition(instr);
433 return 4;
434 }
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100435 case 'd': { // 'd: vmov double immediate.
436 double d = instr->DoubleImmedVmov();
437 out_buffer_pos_ += v8i::OS::SNPrintF(out_buffer_ + out_buffer_pos_,
438 "#%g", d);
439 return 1;
440 }
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100441 case 'f': { // 'f: bitfield instructions - v7 and above.
442 uint32_t lsbit = instr->Bits(11, 7);
443 uint32_t width = instr->Bits(20, 16) + 1;
444 if (instr->Bit(21) == 0) {
445 // BFC/BFI:
446 // Bits 20-16 represent most-significant bit. Covert to width.
447 width -= lsbit;
448 ASSERT(width > 0);
449 }
450 ASSERT((width + lsbit) <= 32);
451 out_buffer_pos_ += v8i::OS::SNPrintF(out_buffer_ + out_buffer_pos_,
452 "#%d, #%d", lsbit, width);
453 return 1;
454 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000455 case 'h': { // 'h: halfword operation for extra loads and stores
456 if (instr->HasH()) {
457 Print("h");
458 } else {
459 Print("b");
460 }
461 return 1;
462 }
Kristian Monsen50ef84f2010-07-29 15:18:00 +0100463 case 'i': { // 'i: immediate value from adjacent bits.
464 // Expects tokens in the form imm%02d@%02d, ie. imm05@07, imm10@16
465 int width = (format[3] - '0') * 10 + (format[4] - '0');
466 int lsb = (format[6] - '0') * 10 + (format[7] - '0');
467
468 ASSERT((width >= 1) && (width <= 32));
469 ASSERT((lsb >= 0) && (lsb <= 31));
470 ASSERT((width + lsb) <= 32);
471
472 out_buffer_pos_ += v8i::OS::SNPrintF(out_buffer_ + out_buffer_pos_,
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100473 "%d",
Kristian Monsen50ef84f2010-07-29 15:18:00 +0100474 instr->Bits(width + lsb - 1, lsb));
475 return 8;
476 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000477 case 'l': { // 'l: branch and link
478 if (instr->HasLink()) {
479 Print("l");
480 }
481 return 1;
482 }
483 case 'm': {
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100484 if (format[1] == 'w') {
485 // 'mw: movt/movw instructions.
486 PrintMovwMovt(instr);
487 return 2;
488 }
489 if (format[1] == 'e') { // 'memop: load/store instructions.
Steve Blocka7e24c12009-10-30 11:49:00 +0000490 ASSERT(STRING_STARTS_WITH(format, "memop"));
491 if (instr->HasL()) {
492 Print("ldr");
Kristian Monsen25f61362010-05-21 11:50:48 +0100493 } else if ((instr->Bits(27, 25) == 0) && (instr->Bit(20) == 0)) {
494 if (instr->Bits(7, 4) == 0xf) {
495 Print("strd");
496 } else {
497 Print("ldrd");
498 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000499 } else {
500 Print("str");
501 }
502 return 5;
503 }
504 // 'msg: for simulator break instructions
505 ASSERT(STRING_STARTS_WITH(format, "msg"));
506 byte* str =
507 reinterpret_cast<byte*>(instr->InstructionBits() & 0x0fffffff);
508 out_buffer_pos_ += v8i::OS::SNPrintF(out_buffer_ + out_buffer_pos_,
509 "%s", converter_.NameInCode(str));
510 return 3;
511 }
512 case 'o': {
Andrei Popescu31002712010-02-23 13:46:05 +0000513 if ((format[3] == '1') && (format[4] == '2')) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000514 // 'off12: 12-bit offset for load and store instructions
515 ASSERT(STRING_STARTS_WITH(format, "off12"));
516 out_buffer_pos_ += v8i::OS::SNPrintF(out_buffer_ + out_buffer_pos_,
517 "%d", instr->Offset12Field());
518 return 5;
Steve Block6ded16b2010-05-10 14:33:55 +0100519 } else if (format[3] == '0') {
520 // 'off0to3and8to19 16-bit immediate encoded in bits 19-8 and 3-0.
521 ASSERT(STRING_STARTS_WITH(format, "off0to3and8to19"));
522 out_buffer_pos_ += v8i::OS::SNPrintF(out_buffer_ + out_buffer_pos_,
523 "%d",
524 (instr->Bits(19, 8) << 4) +
525 instr->Bits(3, 0));
526 return 15;
Steve Blocka7e24c12009-10-30 11:49:00 +0000527 }
528 // 'off8: 8-bit offset for extra load and store instructions
529 ASSERT(STRING_STARTS_WITH(format, "off8"));
530 int offs8 = (instr->ImmedHField() << 4) | instr->ImmedLField();
531 out_buffer_pos_ += v8i::OS::SNPrintF(out_buffer_ + out_buffer_pos_,
532 "%d", offs8);
533 return 4;
534 }
535 case 'p': { // 'pu: P and U bits for load and store instructions
536 ASSERT(STRING_STARTS_WITH(format, "pu"));
537 PrintPU(instr);
538 return 2;
539 }
540 case 'r': {
541 return FormatRegister(instr, format);
542 }
543 case 's': {
Kristian Monsen50ef84f2010-07-29 15:18:00 +0100544 if (format[1] == 'h') { // 'shift_op or 'shift_rm or 'shift_sat.
Steve Blocka7e24c12009-10-30 11:49:00 +0000545 if (format[6] == 'o') { // 'shift_op
546 ASSERT(STRING_STARTS_WITH(format, "shift_op"));
547 if (instr->TypeField() == 0) {
548 PrintShiftRm(instr);
549 } else {
550 ASSERT(instr->TypeField() == 1);
551 PrintShiftImm(instr);
552 }
553 return 8;
Kristian Monsen50ef84f2010-07-29 15:18:00 +0100554 } else if (format[6] == 's') { // 'shift_sat.
555 ASSERT(STRING_STARTS_WITH(format, "shift_sat"));
556 PrintShiftSat(instr);
557 return 9;
Steve Blocka7e24c12009-10-30 11:49:00 +0000558 } else { // 'shift_rm
559 ASSERT(STRING_STARTS_WITH(format, "shift_rm"));
560 PrintShiftRm(instr);
561 return 8;
562 }
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800563 } else if (format[1] == 'v') { // 'svc
564 ASSERT(STRING_STARTS_WITH(format, "svc"));
565 PrintSoftwareInterrupt(instr->SvcField());
Steve Blocka7e24c12009-10-30 11:49:00 +0000566 return 3;
567 } else if (format[1] == 'i') { // 'sign: signed extra loads and stores
568 ASSERT(STRING_STARTS_WITH(format, "sign"));
569 if (instr->HasSign()) {
570 Print("s");
571 }
572 return 4;
573 }
574 // 's: S field of data processing instructions
575 if (instr->HasS()) {
576 Print("s");
577 }
578 return 1;
579 }
580 case 't': { // 'target: target of branch instructions
581 ASSERT(STRING_STARTS_WITH(format, "target"));
582 int off = (instr->SImmed24Field() << 2) + 8;
583 out_buffer_pos_ += v8i::OS::SNPrintF(
584 out_buffer_ + out_buffer_pos_,
585 "%+d -> %s",
586 off,
587 converter_.NameOfAddress(reinterpret_cast<byte*>(instr) + off));
588 return 6;
589 }
590 case 'u': { // 'u: signed or unsigned multiplies
591 // The manual gets the meaning of bit 22 backwards in the multiply
592 // instruction overview on page A3.16.2. The instructions that
593 // exist in u and s variants are the following:
594 // smull A4.1.87
595 // umull A4.1.129
596 // umlal A4.1.128
597 // smlal A4.1.76
598 // For these 0 means u and 1 means s. As can be seen on their individual
599 // pages. The other 18 mul instructions have the bit set or unset in
600 // arbitrary ways that are unrelated to the signedness of the instruction.
601 // None of these 18 instructions exist in both a 'u' and an 's' variant.
602
603 if (instr->Bit(22) == 0) {
604 Print("u");
605 } else {
606 Print("s");
607 }
608 return 1;
609 }
Steve Blockd0582a62009-12-15 09:54:21 +0000610 case 'v': {
611 return FormatVFPinstruction(instr, format);
612 }
613 case 'S':
614 case 'D': {
615 return FormatVFPRegister(instr, format);
616 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000617 case 'w': { // 'w: W field of load and store instructions
618 if (instr->HasW()) {
619 Print("!");
620 }
621 return 1;
622 }
623 default: {
624 UNREACHABLE();
625 break;
626 }
627 }
628 UNREACHABLE();
629 return -1;
630}
631
632
633// Format takes a formatting string for a whole instruction and prints it into
634// the output buffer. All escaped options are handed to FormatOption to be
635// parsed further.
636void Decoder::Format(Instr* instr, const char* format) {
637 char cur = *format++;
638 while ((cur != 0) && (out_buffer_pos_ < (out_buffer_.length() - 1))) {
639 if (cur == '\'') { // Single quote is used as the formatting escape.
640 format += FormatOption(instr, format);
641 } else {
642 out_buffer_[out_buffer_pos_++] = cur;
643 }
644 cur = *format++;
645 }
646 out_buffer_[out_buffer_pos_] = '\0';
647}
648
649
650// For currently unimplemented decodings the disassembler calls Unknown(instr)
651// which will just print "unknown" of the instruction bits.
652void Decoder::Unknown(Instr* instr) {
653 Format(instr, "unknown");
654}
655
656
657void Decoder::DecodeType01(Instr* instr) {
658 int type = instr->TypeField();
659 if ((type == 0) && instr->IsSpecialType0()) {
660 // multiply instruction or extra loads and stores
661 if (instr->Bits(7, 4) == 9) {
662 if (instr->Bit(24) == 0) {
663 // multiply instructions
664 if (instr->Bit(23) == 0) {
665 if (instr->Bit(21) == 0) {
666 // The MUL instruction description (A 4.1.33) refers to Rd as being
667 // the destination for the operation, but it confusingly uses the
668 // Rn field to encode it.
669 Format(instr, "mul'cond's 'rn, 'rm, 'rs");
670 } else {
671 // The MLA instruction description (A 4.1.28) refers to the order
672 // of registers as "Rd, Rm, Rs, Rn". But confusingly it uses the
673 // Rn field to encode the Rd register and the Rd field to encode
674 // the Rn register.
675 Format(instr, "mla'cond's 'rn, 'rm, 'rs, 'rd");
676 }
677 } else {
678 // The signed/long multiply instructions use the terms RdHi and RdLo
679 // when referring to the target registers. They are mapped to the Rn
680 // and Rd fields as follows:
681 // RdLo == Rd field
682 // RdHi == Rn field
683 // The order of registers is: <RdLo>, <RdHi>, <Rm>, <Rs>
684 Format(instr, "'um'al'cond's 'rd, 'rn, 'rm, 'rs");
685 }
686 } else {
687 Unknown(instr); // not used by V8
688 }
Kristian Monsen25f61362010-05-21 11:50:48 +0100689 } else if ((instr->Bit(20) == 0) && ((instr->Bits(7, 4) & 0xd) == 0xd)) {
690 // ldrd, strd
691 switch (instr->PUField()) {
692 case 0: {
693 if (instr->Bit(22) == 0) {
694 Format(instr, "'memop'cond's 'rd, ['rn], -'rm");
695 } else {
696 Format(instr, "'memop'cond's 'rd, ['rn], #-'off8");
697 }
698 break;
699 }
700 case 1: {
701 if (instr->Bit(22) == 0) {
702 Format(instr, "'memop'cond's 'rd, ['rn], +'rm");
703 } else {
704 Format(instr, "'memop'cond's 'rd, ['rn], #+'off8");
705 }
706 break;
707 }
708 case 2: {
709 if (instr->Bit(22) == 0) {
710 Format(instr, "'memop'cond's 'rd, ['rn, -'rm]'w");
711 } else {
712 Format(instr, "'memop'cond's 'rd, ['rn, #-'off8]'w");
713 }
714 break;
715 }
716 case 3: {
717 if (instr->Bit(22) == 0) {
718 Format(instr, "'memop'cond's 'rd, ['rn, +'rm]'w");
719 } else {
720 Format(instr, "'memop'cond's 'rd, ['rn, #+'off8]'w");
721 }
722 break;
723 }
724 default: {
725 // The PU field is a 2-bit field.
726 UNREACHABLE();
727 break;
728 }
729 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000730 } else {
731 // extra load/store instructions
732 switch (instr->PUField()) {
733 case 0: {
734 if (instr->Bit(22) == 0) {
735 Format(instr, "'memop'cond'sign'h 'rd, ['rn], -'rm");
736 } else {
737 Format(instr, "'memop'cond'sign'h 'rd, ['rn], #-'off8");
738 }
739 break;
740 }
741 case 1: {
742 if (instr->Bit(22) == 0) {
743 Format(instr, "'memop'cond'sign'h 'rd, ['rn], +'rm");
744 } else {
745 Format(instr, "'memop'cond'sign'h 'rd, ['rn], #+'off8");
746 }
747 break;
748 }
749 case 2: {
750 if (instr->Bit(22) == 0) {
751 Format(instr, "'memop'cond'sign'h 'rd, ['rn, -'rm]'w");
752 } else {
753 Format(instr, "'memop'cond'sign'h 'rd, ['rn, #-'off8]'w");
754 }
755 break;
756 }
757 case 3: {
758 if (instr->Bit(22) == 0) {
759 Format(instr, "'memop'cond'sign'h 'rd, ['rn, +'rm]'w");
760 } else {
761 Format(instr, "'memop'cond'sign'h 'rd, ['rn, #+'off8]'w");
762 }
763 break;
764 }
765 default: {
766 // The PU field is a 2-bit field.
767 UNREACHABLE();
768 break;
769 }
770 }
771 return;
772 }
Steve Block6ded16b2010-05-10 14:33:55 +0100773 } else if ((type == 0) && instr->IsMiscType0()) {
774 if (instr->Bits(22, 21) == 1) {
775 switch (instr->Bits(7, 4)) {
776 case BX:
777 Format(instr, "bx'cond 'rm");
778 break;
779 case BLX:
780 Format(instr, "blx'cond 'rm");
781 break;
782 case BKPT:
783 Format(instr, "bkpt 'off0to3and8to19");
784 break;
785 default:
786 Unknown(instr); // not used by V8
787 break;
788 }
789 } else if (instr->Bits(22, 21) == 3) {
790 switch (instr->Bits(7, 4)) {
791 case CLZ:
792 Format(instr, "clz'cond 'rd, 'rm");
793 break;
794 default:
795 Unknown(instr); // not used by V8
796 break;
797 }
798 } else {
799 Unknown(instr); // not used by V8
800 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000801 } else {
802 switch (instr->OpcodeField()) {
803 case AND: {
804 Format(instr, "and'cond's 'rd, 'rn, 'shift_op");
805 break;
806 }
807 case EOR: {
808 Format(instr, "eor'cond's 'rd, 'rn, 'shift_op");
809 break;
810 }
811 case SUB: {
812 Format(instr, "sub'cond's 'rd, 'rn, 'shift_op");
813 break;
814 }
815 case RSB: {
816 Format(instr, "rsb'cond's 'rd, 'rn, 'shift_op");
817 break;
818 }
819 case ADD: {
820 Format(instr, "add'cond's 'rd, 'rn, 'shift_op");
821 break;
822 }
823 case ADC: {
824 Format(instr, "adc'cond's 'rd, 'rn, 'shift_op");
825 break;
826 }
827 case SBC: {
828 Format(instr, "sbc'cond's 'rd, 'rn, 'shift_op");
829 break;
830 }
831 case RSC: {
832 Format(instr, "rsc'cond's 'rd, 'rn, 'shift_op");
833 break;
834 }
835 case TST: {
836 if (instr->HasS()) {
837 Format(instr, "tst'cond 'rn, 'shift_op");
838 } else {
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100839 Format(instr, "movw'cond 'mw");
Steve Blocka7e24c12009-10-30 11:49:00 +0000840 }
841 break;
842 }
843 case TEQ: {
844 if (instr->HasS()) {
845 Format(instr, "teq'cond 'rn, 'shift_op");
846 } else {
Steve Block6ded16b2010-05-10 14:33:55 +0100847 // Other instructions matching this pattern are handled in the
848 // miscellaneous instructions part above.
849 UNREACHABLE();
Steve Blocka7e24c12009-10-30 11:49:00 +0000850 }
851 break;
852 }
853 case CMP: {
854 if (instr->HasS()) {
855 Format(instr, "cmp'cond 'rn, 'shift_op");
856 } else {
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100857 Format(instr, "movt'cond 'mw");
Steve Blocka7e24c12009-10-30 11:49:00 +0000858 }
859 break;
860 }
861 case CMN: {
862 if (instr->HasS()) {
863 Format(instr, "cmn'cond 'rn, 'shift_op");
864 } else {
Steve Block6ded16b2010-05-10 14:33:55 +0100865 // Other instructions matching this pattern are handled in the
866 // miscellaneous instructions part above.
867 UNREACHABLE();
Steve Blocka7e24c12009-10-30 11:49:00 +0000868 }
869 break;
870 }
871 case ORR: {
872 Format(instr, "orr'cond's 'rd, 'rn, 'shift_op");
873 break;
874 }
875 case MOV: {
876 Format(instr, "mov'cond's 'rd, 'shift_op");
877 break;
878 }
879 case BIC: {
880 Format(instr, "bic'cond's 'rd, 'rn, 'shift_op");
881 break;
882 }
883 case MVN: {
884 Format(instr, "mvn'cond's 'rd, 'shift_op");
885 break;
886 }
887 default: {
888 // The Opcode field is a 4-bit field.
889 UNREACHABLE();
890 break;
891 }
892 }
893 }
894}
895
896
897void Decoder::DecodeType2(Instr* instr) {
898 switch (instr->PUField()) {
899 case 0: {
900 if (instr->HasW()) {
901 Unknown(instr); // not used in V8
902 }
903 Format(instr, "'memop'cond'b 'rd, ['rn], #-'off12");
904 break;
905 }
906 case 1: {
907 if (instr->HasW()) {
908 Unknown(instr); // not used in V8
909 }
910 Format(instr, "'memop'cond'b 'rd, ['rn], #+'off12");
911 break;
912 }
913 case 2: {
914 Format(instr, "'memop'cond'b 'rd, ['rn, #-'off12]'w");
915 break;
916 }
917 case 3: {
918 Format(instr, "'memop'cond'b 'rd, ['rn, #+'off12]'w");
919 break;
920 }
921 default: {
922 // The PU field is a 2-bit field.
923 UNREACHABLE();
924 break;
925 }
926 }
927}
928
929
930void Decoder::DecodeType3(Instr* instr) {
931 switch (instr->PUField()) {
932 case 0: {
933 ASSERT(!instr->HasW());
934 Format(instr, "'memop'cond'b 'rd, ['rn], -'shift_rm");
935 break;
936 }
937 case 1: {
Kristian Monsen50ef84f2010-07-29 15:18:00 +0100938 if (instr->HasW()) {
939 ASSERT(instr->Bits(5, 4) == 0x1);
940 if (instr->Bit(22) == 0x1) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100941 Format(instr, "usat 'rd, #'imm05@16, 'rm'shift_sat");
Kristian Monsen50ef84f2010-07-29 15:18:00 +0100942 } else {
943 UNREACHABLE(); // SSAT.
944 }
945 } else {
946 Format(instr, "'memop'cond'b 'rd, ['rn], +'shift_rm");
947 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000948 break;
949 }
950 case 2: {
951 Format(instr, "'memop'cond'b 'rd, ['rn, -'shift_rm]'w");
952 break;
953 }
954 case 3: {
Andrei Popescu31002712010-02-23 13:46:05 +0000955 if (instr->HasW() && (instr->Bits(6, 4) == 0x5)) {
956 uint32_t widthminus1 = static_cast<uint32_t>(instr->Bits(20, 16));
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100957 uint32_t lsbit = static_cast<uint32_t>(instr->Bits(11, 7));
Andrei Popescu31002712010-02-23 13:46:05 +0000958 uint32_t msbit = widthminus1 + lsbit;
959 if (msbit <= 31) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100960 if (instr->Bit(22)) {
961 Format(instr, "ubfx'cond 'rd, 'rm, 'f");
962 } else {
963 Format(instr, "sbfx'cond 'rd, 'rm, 'f");
964 }
965 } else {
966 UNREACHABLE();
967 }
968 } else if (!instr->HasW() && (instr->Bits(6, 4) == 0x1)) {
969 uint32_t lsbit = static_cast<uint32_t>(instr->Bits(11, 7));
970 uint32_t msbit = static_cast<uint32_t>(instr->Bits(20, 16));
971 if (msbit >= lsbit) {
972 if (instr->RmField() == 15) {
973 Format(instr, "bfc'cond 'rd, 'f");
974 } else {
975 Format(instr, "bfi'cond 'rd, 'rm, 'f");
976 }
Andrei Popescu31002712010-02-23 13:46:05 +0000977 } else {
978 UNREACHABLE();
979 }
980 } else {
981 Format(instr, "'memop'cond'b 'rd, ['rn, +'shift_rm]'w");
982 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000983 break;
984 }
985 default: {
986 // The PU field is a 2-bit field.
987 UNREACHABLE();
988 break;
989 }
990 }
991}
992
993
994void Decoder::DecodeType4(Instr* instr) {
995 ASSERT(instr->Bit(22) == 0); // Privileged mode currently not supported.
996 if (instr->HasL()) {
997 Format(instr, "ldm'cond'pu 'rn'w, 'rlist");
998 } else {
999 Format(instr, "stm'cond'pu 'rn'w, 'rlist");
1000 }
1001}
1002
1003
1004void Decoder::DecodeType5(Instr* instr) {
1005 Format(instr, "b'l'cond 'target");
1006}
1007
1008
1009void Decoder::DecodeType6(Instr* instr) {
Steve Blockd0582a62009-12-15 09:54:21 +00001010 DecodeType6CoprocessorIns(instr);
Steve Blocka7e24c12009-10-30 11:49:00 +00001011}
1012
1013
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001014int Decoder::DecodeType7(Instr* instr) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001015 if (instr->Bit(24) == 1) {
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001016 if (instr->SvcField() >= stop) {
1017 Format(instr, "stop'cond 'svc");
1018 // Also print the stop message. Its address is encoded
1019 // in the following 4 bytes.
1020 out_buffer_pos_ +=
1021 v8i::OS::SNPrintF(out_buffer_ + out_buffer_pos_,
1022 "\n %p %08x stop message: %s",
1023 reinterpret_cast<int32_t*>(instr + Instr::kInstrSize),
1024 *reinterpret_cast<char**>(instr + Instr::kInstrSize),
1025 *reinterpret_cast<char**>(instr + Instr::kInstrSize));
1026 // We have decoded 2 * Instr::kInstrSize bytes.
1027 return 2 * Instr::kInstrSize;
1028 } else {
1029 Format(instr, "svc'cond 'svc");
1030 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001031 } else {
Steve Blockd0582a62009-12-15 09:54:21 +00001032 DecodeTypeVFP(instr);
Steve Blocka7e24c12009-10-30 11:49:00 +00001033 }
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001034 return Instr::kInstrSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00001035}
1036
1037
Steve Blockd0582a62009-12-15 09:54:21 +00001038// void Decoder::DecodeTypeVFP(Instr* instr)
Leon Clarkee46be812010-01-19 14:06:41 +00001039// vmov: Sn = Rt
1040// vmov: Rt = Sn
1041// vcvt: Dd = Sm
1042// vcvt: Sd = Dm
1043// Dd = vadd(Dn, Dm)
1044// Dd = vsub(Dn, Dm)
1045// Dd = vmul(Dn, Dm)
1046// Dd = vdiv(Dn, Dm)
Steve Blockd0582a62009-12-15 09:54:21 +00001047// vcmp(Dd, Dm)
Steve Block8defd9f2010-07-08 12:39:36 +01001048// vmrs
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001049// vmsr
Steve Block8defd9f2010-07-08 12:39:36 +01001050// Dd = vsqrt(Dm)
Steve Blockd0582a62009-12-15 09:54:21 +00001051void Decoder::DecodeTypeVFP(Instr* instr) {
1052 ASSERT((instr->TypeField() == 7) && (instr->Bit(24) == 0x0) );
Steve Block6ded16b2010-05-10 14:33:55 +01001053 ASSERT(instr->Bits(11, 9) == 0x5);
Steve Blockd0582a62009-12-15 09:54:21 +00001054
Steve Block6ded16b2010-05-10 14:33:55 +01001055 if (instr->Bit(4) == 0) {
1056 if (instr->Opc1Field() == 0x7) {
1057 // Other data processing instructions
Steve Block8defd9f2010-07-08 12:39:36 +01001058 if ((instr->Opc2Field() == 0x0) && (instr->Opc3Field() == 0x1)) {
1059 // vmov register to register.
1060 if (instr->SzField() == 0x1) {
1061 Format(instr, "vmov.f64'cond 'Dd, 'Dm");
1062 } else {
Ben Murdoch3bec4d22010-07-22 14:51:16 +01001063 Format(instr, "vmov.f32'cond 'Sd, 'Sm");
Steve Block8defd9f2010-07-08 12:39:36 +01001064 }
1065 } else if ((instr->Opc2Field() == 0x7) && (instr->Opc3Field() == 0x3)) {
Steve Block6ded16b2010-05-10 14:33:55 +01001066 DecodeVCVTBetweenDoubleAndSingle(instr);
1067 } else if ((instr->Opc2Field() == 0x8) && (instr->Opc3Field() & 0x1)) {
1068 DecodeVCVTBetweenFloatingPointAndInteger(instr);
1069 } else if (((instr->Opc2Field() >> 1) == 0x6) &&
1070 (instr->Opc3Field() & 0x1)) {
1071 DecodeVCVTBetweenFloatingPointAndInteger(instr);
1072 } else if (((instr->Opc2Field() == 0x4) || (instr->Opc2Field() == 0x5)) &&
1073 (instr->Opc3Field() & 0x1)) {
1074 DecodeVCMP(instr);
Steve Block8defd9f2010-07-08 12:39:36 +01001075 } else if (((instr->Opc2Field() == 0x1)) && (instr->Opc3Field() == 0x3)) {
1076 Format(instr, "vsqrt.f64'cond 'Dd, 'Dm");
Ben Murdoch3bec4d22010-07-22 14:51:16 +01001077 } else if (instr->Opc3Field() == 0x0) {
1078 if (instr->SzField() == 0x1) {
1079 Format(instr, "vmov.f64'cond 'Dd, 'd");
1080 } else {
1081 Unknown(instr); // Not used by V8.
1082 }
Steve Block6ded16b2010-05-10 14:33:55 +01001083 } else {
1084 Unknown(instr); // Not used by V8.
1085 }
1086 } else if (instr->Opc1Field() == 0x3) {
1087 if (instr->SzField() == 0x1) {
1088 if (instr->Opc3Field() & 0x1) {
1089 Format(instr, "vsub.f64'cond 'Dd, 'Dn, 'Dm");
1090 } else {
1091 Format(instr, "vadd.f64'cond 'Dd, 'Dn, 'Dm");
1092 }
1093 } else {
1094 Unknown(instr); // Not used by V8.
1095 }
1096 } else if ((instr->Opc1Field() == 0x2) && !(instr->Opc3Field() & 0x1)) {
1097 if (instr->SzField() == 0x1) {
1098 Format(instr, "vmul.f64'cond 'Dd, 'Dn, 'Dm");
1099 } else {
1100 Unknown(instr); // Not used by V8.
1101 }
1102 } else if ((instr->Opc1Field() == 0x4) && !(instr->Opc3Field() & 0x1)) {
1103 if (instr->SzField() == 0x1) {
Steve Blockd0582a62009-12-15 09:54:21 +00001104 Format(instr, "vdiv.f64'cond 'Dd, 'Dn, 'Dm");
Steve Block6ded16b2010-05-10 14:33:55 +01001105 } else {
1106 Unknown(instr); // Not used by V8.
1107 }
Steve Blockd0582a62009-12-15 09:54:21 +00001108 } else {
1109 Unknown(instr); // Not used by V8.
1110 }
1111 } else {
Steve Block6ded16b2010-05-10 14:33:55 +01001112 if ((instr->VCField() == 0x0) &&
1113 (instr->VAField() == 0x0)) {
1114 DecodeVMOVBetweenCoreAndSinglePrecisionRegisters(instr);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001115 } else if ((instr->VCField() == 0x0) &&
Steve Block6ded16b2010-05-10 14:33:55 +01001116 (instr->VAField() == 0x7) &&
1117 (instr->Bits(19, 16) == 0x1)) {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001118 if (instr->VLField() == 0) {
1119 if (instr->Bits(15, 12) == 0xF) {
1120 Format(instr, "vmsr'cond FPSCR, APSR");
1121 } else {
1122 Format(instr, "vmsr'cond FPSCR, 'rt");
1123 }
1124 } else {
1125 if (instr->Bits(15, 12) == 0xF) {
1126 Format(instr, "vmrs'cond APSR, FPSCR");
1127 } else {
1128 Format(instr, "vmrs'cond 'rt, FPSCR");
1129 }
1130 }
Steve Blockd0582a62009-12-15 09:54:21 +00001131 }
1132 }
1133}
1134
1135
Steve Block6ded16b2010-05-10 14:33:55 +01001136void Decoder::DecodeVMOVBetweenCoreAndSinglePrecisionRegisters(Instr* instr) {
1137 ASSERT((instr->Bit(4) == 1) && (instr->VCField() == 0x0) &&
1138 (instr->VAField() == 0x0));
1139
1140 bool to_arm_register = (instr->VLField() == 0x1);
1141
1142 if (to_arm_register) {
1143 Format(instr, "vmov'cond 'rt, 'Sn");
1144 } else {
1145 Format(instr, "vmov'cond 'Sn, 'rt");
1146 }
1147}
1148
1149
1150void Decoder::DecodeVCMP(Instr* instr) {
1151 ASSERT((instr->Bit(4) == 0) && (instr->Opc1Field() == 0x7));
1152 ASSERT(((instr->Opc2Field() == 0x4) || (instr->Opc2Field() == 0x5)) &&
1153 (instr->Opc3Field() & 0x1));
1154
1155 // Comparison.
1156 bool dp_operation = (instr->SzField() == 1);
1157 bool raise_exception_for_qnan = (instr->Bit(7) == 0x1);
1158
1159 if (dp_operation && !raise_exception_for_qnan) {
Iain Merrick75681382010-08-19 15:07:18 +01001160 if (instr->Opc2Field() == 0x4) {
1161 Format(instr, "vcmp.f64'cond 'Dd, 'Dm");
1162 } else if (instr->Opc2Field() == 0x5) {
1163 Format(instr, "vcmp.f64'cond 'Dd, #0.0");
1164 } else {
1165 Unknown(instr); // invalid
1166 }
Steve Block6ded16b2010-05-10 14:33:55 +01001167 } else {
1168 Unknown(instr); // Not used by V8.
1169 }
1170}
1171
1172
1173void Decoder::DecodeVCVTBetweenDoubleAndSingle(Instr* instr) {
1174 ASSERT((instr->Bit(4) == 0) && (instr->Opc1Field() == 0x7));
1175 ASSERT((instr->Opc2Field() == 0x7) && (instr->Opc3Field() == 0x3));
1176
1177 bool double_to_single = (instr->SzField() == 1);
1178
1179 if (double_to_single) {
1180 Format(instr, "vcvt.f32.f64'cond 'Sd, 'Dm");
1181 } else {
1182 Format(instr, "vcvt.f64.f32'cond 'Dd, 'Sm");
1183 }
1184}
1185
1186
1187void Decoder::DecodeVCVTBetweenFloatingPointAndInteger(Instr* instr) {
1188 ASSERT((instr->Bit(4) == 0) && (instr->Opc1Field() == 0x7));
1189 ASSERT(((instr->Opc2Field() == 0x8) && (instr->Opc3Field() & 0x1)) ||
1190 (((instr->Opc2Field() >> 1) == 0x6) && (instr->Opc3Field() & 0x1)));
1191
1192 bool to_integer = (instr->Bit(18) == 1);
1193 bool dp_operation = (instr->SzField() == 1);
1194 if (to_integer) {
1195 bool unsigned_integer = (instr->Bit(16) == 0);
1196
1197 if (dp_operation) {
1198 if (unsigned_integer) {
1199 Format(instr, "vcvt.u32.f64'cond 'Sd, 'Dm");
1200 } else {
1201 Format(instr, "vcvt.s32.f64'cond 'Sd, 'Dm");
1202 }
1203 } else {
1204 if (unsigned_integer) {
1205 Format(instr, "vcvt.u32.f32'cond 'Sd, 'Sm");
1206 } else {
1207 Format(instr, "vcvt.s32.f32'cond 'Sd, 'Sm");
1208 }
1209 }
1210 } else {
1211 bool unsigned_integer = (instr->Bit(7) == 0);
1212
1213 if (dp_operation) {
1214 if (unsigned_integer) {
1215 Format(instr, "vcvt.f64.u32'cond 'Dd, 'Sm");
1216 } else {
1217 Format(instr, "vcvt.f64.s32'cond 'Dd, 'Sm");
1218 }
1219 } else {
1220 if (unsigned_integer) {
1221 Format(instr, "vcvt.f32.u32'cond 'Sd, 'Sm");
1222 } else {
1223 Format(instr, "vcvt.f32.s32'cond 'Sd, 'Sm");
1224 }
1225 }
1226 }
1227}
1228
1229
Steve Blockd0582a62009-12-15 09:54:21 +00001230// Decode Type 6 coprocessor instructions.
Leon Clarkee46be812010-01-19 14:06:41 +00001231// Dm = vmov(Rt, Rt2)
1232// <Rt, Rt2> = vmov(Dm)
Leon Clarked91b9f72010-01-27 17:25:45 +00001233// Ddst = MEM(Rbase + 4*offset).
1234// MEM(Rbase + 4*offset) = Dsrc.
Steve Blockd0582a62009-12-15 09:54:21 +00001235void Decoder::DecodeType6CoprocessorIns(Instr* instr) {
1236 ASSERT((instr->TypeField() == 6));
1237
Steve Block6ded16b2010-05-10 14:33:55 +01001238 if (instr->CoprocessorField() == 0xA) {
1239 switch (instr->OpcodeField()) {
1240 case 0x8:
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001241 case 0xA:
Steve Block6ded16b2010-05-10 14:33:55 +01001242 if (instr->HasL()) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001243 Format(instr, "vldr'cond 'Sd, ['rn - 4*'imm08@00]");
Steve Block6ded16b2010-05-10 14:33:55 +01001244 } else {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001245 Format(instr, "vstr'cond 'Sd, ['rn - 4*'imm08@00]");
Steve Block6ded16b2010-05-10 14:33:55 +01001246 }
1247 break;
1248 case 0xC:
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001249 case 0xE:
Steve Block6ded16b2010-05-10 14:33:55 +01001250 if (instr->HasL()) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001251 Format(instr, "vldr'cond 'Sd, ['rn + 4*'imm08@00]");
Steve Block6ded16b2010-05-10 14:33:55 +01001252 } else {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001253 Format(instr, "vstr'cond 'Sd, ['rn + 4*'imm08@00]");
Steve Block6ded16b2010-05-10 14:33:55 +01001254 }
1255 break;
1256 default:
1257 Unknown(instr); // Not used by V8.
1258 break;
1259 }
1260 } else if (instr->CoprocessorField() == 0xB) {
Leon Clarked91b9f72010-01-27 17:25:45 +00001261 switch (instr->OpcodeField()) {
1262 case 0x2:
1263 // Load and store double to two GP registers
1264 if (instr->Bits(7, 4) != 0x1) {
1265 Unknown(instr); // Not used by V8.
1266 } else if (instr->HasL()) {
1267 Format(instr, "vmov'cond 'rt, 'rn, 'Dm");
1268 } else {
1269 Format(instr, "vmov'cond 'Dm, 'rt, 'rn");
1270 }
1271 break;
1272 case 0x8:
1273 if (instr->HasL()) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001274 Format(instr, "vldr'cond 'Dd, ['rn - 4*'imm08@00]");
Leon Clarked91b9f72010-01-27 17:25:45 +00001275 } else {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001276 Format(instr, "vstr'cond 'Dd, ['rn - 4*'imm08@00]");
Leon Clarked91b9f72010-01-27 17:25:45 +00001277 }
1278 break;
1279 case 0xC:
1280 if (instr->HasL()) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001281 Format(instr, "vldr'cond 'Dd, ['rn + 4*'imm08@00]");
Leon Clarked91b9f72010-01-27 17:25:45 +00001282 } else {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001283 Format(instr, "vstr'cond 'Dd, ['rn + 4*'imm08@00]");
Leon Clarked91b9f72010-01-27 17:25:45 +00001284 }
1285 break;
1286 default:
1287 Unknown(instr); // Not used by V8.
1288 break;
1289 }
Steve Block6ded16b2010-05-10 14:33:55 +01001290 } else {
1291 UNIMPLEMENTED(); // Not used by V8.
Steve Blockd0582a62009-12-15 09:54:21 +00001292 }
1293}
1294
1295
Steve Blocka7e24c12009-10-30 11:49:00 +00001296// Disassemble the instruction at *instr_ptr into the output buffer.
1297int Decoder::InstructionDecode(byte* instr_ptr) {
1298 Instr* instr = Instr::At(instr_ptr);
1299 // Print raw instruction bytes.
1300 out_buffer_pos_ += v8i::OS::SNPrintF(out_buffer_ + out_buffer_pos_,
1301 "%08x ",
1302 instr->InstructionBits());
1303 if (instr->ConditionField() == special_condition) {
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001304 UNIMPLEMENTED();
Steve Blocka7e24c12009-10-30 11:49:00 +00001305 return Instr::kInstrSize;
1306 }
1307 switch (instr->TypeField()) {
1308 case 0:
1309 case 1: {
1310 DecodeType01(instr);
1311 break;
1312 }
1313 case 2: {
1314 DecodeType2(instr);
1315 break;
1316 }
1317 case 3: {
1318 DecodeType3(instr);
1319 break;
1320 }
1321 case 4: {
1322 DecodeType4(instr);
1323 break;
1324 }
1325 case 5: {
1326 DecodeType5(instr);
1327 break;
1328 }
1329 case 6: {
1330 DecodeType6(instr);
1331 break;
1332 }
1333 case 7: {
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001334 return DecodeType7(instr);
Steve Blocka7e24c12009-10-30 11:49:00 +00001335 }
1336 default: {
1337 // The type field is 3-bits in the ARM encoding.
1338 UNREACHABLE();
1339 break;
1340 }
1341 }
1342 return Instr::kInstrSize;
1343}
1344
1345
1346} } // namespace assembler::arm
1347
1348
1349
1350//------------------------------------------------------------------------------
1351
1352namespace disasm {
1353
1354namespace v8i = v8::internal;
1355
1356
1357const char* NameConverter::NameOfAddress(byte* addr) const {
1358 static v8::internal::EmbeddedVector<char, 32> tmp_buffer;
1359 v8::internal::OS::SNPrintF(tmp_buffer, "%p", addr);
1360 return tmp_buffer.start();
1361}
1362
1363
1364const char* NameConverter::NameOfConstant(byte* addr) const {
1365 return NameOfAddress(addr);
1366}
1367
1368
1369const char* NameConverter::NameOfCPURegister(int reg) const {
1370 return assembler::arm::Registers::Name(reg);
1371}
1372
1373
1374const char* NameConverter::NameOfByteCPURegister(int reg) const {
1375 UNREACHABLE(); // ARM does not have the concept of a byte register
1376 return "nobytereg";
1377}
1378
1379
1380const char* NameConverter::NameOfXMMRegister(int reg) const {
1381 UNREACHABLE(); // ARM does not have any XMM registers
1382 return "noxmmreg";
1383}
1384
1385
1386const char* NameConverter::NameInCode(byte* addr) const {
1387 // The default name converter is called for unknown code. So we will not try
1388 // to access any memory.
1389 return "";
1390}
1391
1392
1393//------------------------------------------------------------------------------
1394
1395Disassembler::Disassembler(const NameConverter& converter)
1396 : converter_(converter) {}
1397
1398
1399Disassembler::~Disassembler() {}
1400
1401
1402int Disassembler::InstructionDecode(v8::internal::Vector<char> buffer,
1403 byte* instruction) {
1404 assembler::arm::Decoder d(converter_, buffer);
1405 return d.InstructionDecode(instruction);
1406}
1407
1408
1409int Disassembler::ConstantPoolSizeAt(byte* instruction) {
1410 int instruction_bits = *(reinterpret_cast<int*>(instruction));
1411 if ((instruction_bits & 0xfff00000) == 0x03000000) {
1412 return instruction_bits & 0x0000ffff;
1413 } else {
1414 return -1;
1415 }
1416}
1417
1418
1419void Disassembler::Disassemble(FILE* f, byte* begin, byte* end) {
1420 NameConverter converter;
1421 Disassembler d(converter);
1422 for (byte* pc = begin; pc < end;) {
1423 v8::internal::EmbeddedVector<char, 128> buffer;
1424 buffer[0] = '\0';
1425 byte* prev_pc = pc;
1426 pc += d.InstructionDecode(buffer, pc);
1427 fprintf(f, "%p %08x %s\n",
1428 prev_pc, *reinterpret_cast<int32_t*>(prev_pc), buffer.start());
1429 }
1430}
1431
1432
1433} // namespace disasm
Leon Clarkef7060e22010-06-03 12:02:55 +01001434
1435#endif // V8_TARGET_ARCH_ARM