blob: 37401ed28f26857104623f7b1301a28b8a8aed7b [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);
109 void PrintPU(Instr* instr);
110 void PrintSoftwareInterrupt(SoftwareInterruptCodes swi);
111
112 // Handle formatting of instructions and their options.
113 int FormatRegister(Instr* instr, const char* option);
114 int FormatOption(Instr* instr, const char* option);
115 void Format(Instr* instr, const char* format);
116 void Unknown(Instr* instr);
117
118 // Each of these functions decodes one particular instruction type, a 3-bit
119 // field in the instruction encoding.
120 // Types 0 and 1 are combined as they are largely the same except for the way
121 // they interpret the shifter operand.
122 void DecodeType01(Instr* instr);
123 void DecodeType2(Instr* instr);
124 void DecodeType3(Instr* instr);
125 void DecodeType4(Instr* instr);
126 void DecodeType5(Instr* instr);
127 void DecodeType6(Instr* instr);
128 void DecodeType7(Instr* instr);
129 void DecodeUnconditional(Instr* instr);
Steve Blockd0582a62009-12-15 09:54:21 +0000130 // For VFP support.
131 void DecodeTypeVFP(Instr* instr);
132 void DecodeType6CoprocessorIns(Instr* instr);
133
Steve Block6ded16b2010-05-10 14:33:55 +0100134 void DecodeVMOVBetweenCoreAndSinglePrecisionRegisters(Instr* instr);
135 void DecodeVCMP(Instr* instr);
136 void DecodeVCVTBetweenDoubleAndSingle(Instr* instr);
137 void DecodeVCVTBetweenFloatingPointAndInteger(Instr* instr);
Steve Blocka7e24c12009-10-30 11:49:00 +0000138
139 const disasm::NameConverter& converter_;
140 v8::internal::Vector<char> out_buffer_;
141 int out_buffer_pos_;
142
143 DISALLOW_COPY_AND_ASSIGN(Decoder);
144};
145
146
147// Support for assertions in the Decoder formatting functions.
148#define STRING_STARTS_WITH(string, compare_string) \
149 (strncmp(string, compare_string, strlen(compare_string)) == 0)
150
151
152// Append the ch to the output buffer.
153void Decoder::PrintChar(const char ch) {
154 out_buffer_[out_buffer_pos_++] = ch;
155}
156
157
158// Append the str to the output buffer.
159void Decoder::Print(const char* str) {
160 char cur = *str++;
161 while (cur != '\0' && (out_buffer_pos_ < (out_buffer_.length() - 1))) {
162 PrintChar(cur);
163 cur = *str++;
164 }
165 out_buffer_[out_buffer_pos_] = 0;
166}
167
168
169// These condition names are defined in a way to match the native disassembler
170// formatting. See for example the command "objdump -d <binary file>".
171static const char* cond_names[max_condition] = {
172 "eq", "ne", "cs" , "cc" , "mi" , "pl" , "vs" , "vc" ,
173 "hi", "ls", "ge", "lt", "gt", "le", "", "invalid",
174};
175
176
177// Print the condition guarding the instruction.
178void Decoder::PrintCondition(Instr* instr) {
179 Print(cond_names[instr->ConditionField()]);
180}
181
182
183// Print the register name according to the active name converter.
184void Decoder::PrintRegister(int reg) {
185 Print(converter_.NameOfCPURegister(reg));
186}
187
Steve Blockd0582a62009-12-15 09:54:21 +0000188// Print the VFP S register name according to the active name converter.
189void Decoder::PrintSRegister(int reg) {
Steve Block6ded16b2010-05-10 14:33:55 +0100190 Print(assembler::arm::VFPRegisters::Name(reg, false));
Steve Blockd0582a62009-12-15 09:54:21 +0000191}
192
193// Print the VFP D register name according to the active name converter.
194void Decoder::PrintDRegister(int reg) {
Steve Block6ded16b2010-05-10 14:33:55 +0100195 Print(assembler::arm::VFPRegisters::Name(reg, true));
Steve Blockd0582a62009-12-15 09:54:21 +0000196}
197
Steve Blocka7e24c12009-10-30 11:49:00 +0000198
199// These shift names are defined in a way to match the native disassembler
200// formatting. See for example the command "objdump -d <binary file>".
201static const char* shift_names[max_shift] = {
202 "lsl", "lsr", "asr", "ror"
203};
204
205
206// Print the register shift operands for the instruction. Generally used for
207// data processing instructions.
208void Decoder::PrintShiftRm(Instr* instr) {
209 Shift shift = instr->ShiftField();
210 int shift_amount = instr->ShiftAmountField();
211 int rm = instr->RmField();
212
213 PrintRegister(rm);
214
215 if ((instr->RegShiftField() == 0) && (shift == LSL) && (shift_amount == 0)) {
216 // Special case for using rm only.
217 return;
218 }
219 if (instr->RegShiftField() == 0) {
220 // by immediate
221 if ((shift == ROR) && (shift_amount == 0)) {
222 Print(", RRX");
223 return;
224 } else if (((shift == LSR) || (shift == ASR)) && (shift_amount == 0)) {
225 shift_amount = 32;
226 }
227 out_buffer_pos_ += v8i::OS::SNPrintF(out_buffer_ + out_buffer_pos_,
228 ", %s #%d",
229 shift_names[shift], shift_amount);
230 } else {
231 // by register
232 int rs = instr->RsField();
233 out_buffer_pos_ += v8i::OS::SNPrintF(out_buffer_ + out_buffer_pos_,
234 ", %s ", shift_names[shift]);
235 PrintRegister(rs);
236 }
237}
238
239
240// Print the immediate operand for the instruction. Generally used for data
241// processing instructions.
242void Decoder::PrintShiftImm(Instr* instr) {
243 int rotate = instr->RotateField() * 2;
244 int immed8 = instr->Immed8Field();
245 int imm = (immed8 >> rotate) | (immed8 << (32 - rotate));
246 out_buffer_pos_ += v8i::OS::SNPrintF(out_buffer_ + out_buffer_pos_,
247 "#%d", imm);
248}
249
250
251// Print PU formatting to reduce complexity of FormatOption.
252void Decoder::PrintPU(Instr* instr) {
253 switch (instr->PUField()) {
254 case 0: {
255 Print("da");
256 break;
257 }
258 case 1: {
259 Print("ia");
260 break;
261 }
262 case 2: {
263 Print("db");
264 break;
265 }
266 case 3: {
267 Print("ib");
268 break;
269 }
270 default: {
271 UNREACHABLE();
272 break;
273 }
274 }
275}
276
277
278// Print SoftwareInterrupt codes. Factoring this out reduces the complexity of
279// the FormatOption method.
280void Decoder::PrintSoftwareInterrupt(SoftwareInterruptCodes swi) {
281 switch (swi) {
282 case call_rt_redirected:
283 Print("call_rt_redirected");
284 return;
285 case break_point:
286 Print("break_point");
287 return;
288 default:
289 out_buffer_pos_ += v8i::OS::SNPrintF(out_buffer_ + out_buffer_pos_,
290 "%d",
291 swi);
292 return;
293 }
294}
295
296
297// Handle all register based formatting in this function to reduce the
298// complexity of FormatOption.
299int Decoder::FormatRegister(Instr* instr, const char* format) {
300 ASSERT(format[0] == 'r');
301 if (format[1] == 'n') { // 'rn: Rn register
302 int reg = instr->RnField();
303 PrintRegister(reg);
304 return 2;
305 } else if (format[1] == 'd') { // 'rd: Rd register
306 int reg = instr->RdField();
307 PrintRegister(reg);
308 return 2;
309 } else if (format[1] == 's') { // 'rs: Rs register
310 int reg = instr->RsField();
311 PrintRegister(reg);
312 return 2;
313 } else if (format[1] == 'm') { // 'rm: Rm register
314 int reg = instr->RmField();
315 PrintRegister(reg);
316 return 2;
Steve Blockd0582a62009-12-15 09:54:21 +0000317 } else if (format[1] == 't') { // 'rt: Rt register
318 int reg = instr->RtField();
319 PrintRegister(reg);
320 return 2;
Steve Blocka7e24c12009-10-30 11:49:00 +0000321 } else if (format[1] == 'l') {
322 // 'rlist: register list for load and store multiple instructions
323 ASSERT(STRING_STARTS_WITH(format, "rlist"));
324 int rlist = instr->RlistField();
325 int reg = 0;
326 Print("{");
327 // Print register list in ascending order, by scanning the bit mask.
328 while (rlist != 0) {
329 if ((rlist & 1) != 0) {
330 PrintRegister(reg);
331 if ((rlist >> 1) != 0) {
332 Print(", ");
333 }
334 }
335 reg++;
336 rlist >>= 1;
337 }
338 Print("}");
339 return 5;
340 }
341 UNREACHABLE();
342 return -1;
343}
344
345
Steve Blockd0582a62009-12-15 09:54:21 +0000346// Handle all VFP register based formatting in this function to reduce the
347// complexity of FormatOption.
348int Decoder::FormatVFPRegister(Instr* instr, const char* format) {
349 ASSERT((format[0] == 'S') || (format[0] == 'D'));
350
351 if (format[1] == 'n') {
352 int reg = instr->VnField();
353 if (format[0] == 'S') PrintSRegister(((reg << 1) | instr->NField()));
354 if (format[0] == 'D') PrintDRegister(reg);
355 return 2;
356 } else if (format[1] == 'm') {
357 int reg = instr->VmField();
358 if (format[0] == 'S') PrintSRegister(((reg << 1) | instr->MField()));
359 if (format[0] == 'D') PrintDRegister(reg);
360 return 2;
361 } else if (format[1] == 'd') {
362 int reg = instr->VdField();
363 if (format[0] == 'S') PrintSRegister(((reg << 1) | instr->DField()));
364 if (format[0] == 'D') PrintDRegister(reg);
365 return 2;
366 }
367
368 UNREACHABLE();
369 return -1;
370}
371
372
373int Decoder::FormatVFPinstruction(Instr* instr, const char* format) {
374 Print(format);
375 return 0;
376}
377
378
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100379// Print the movw or movt instruction.
380void Decoder::PrintMovwMovt(Instr* instr) {
381 int imm = instr->ImmedMovwMovtField();
382 int rd = instr->RdField();
383 PrintRegister(rd);
384 out_buffer_pos_ += v8i::OS::SNPrintF(out_buffer_ + out_buffer_pos_,
385 ", #%d", imm);
386}
387
388
Steve Blocka7e24c12009-10-30 11:49:00 +0000389// FormatOption takes a formatting string and interprets it based on
390// the current instructions. The format string points to the first
391// character of the option string (the option escape has already been
392// consumed by the caller.) FormatOption returns the number of
393// characters that were consumed from the formatting string.
394int Decoder::FormatOption(Instr* instr, const char* format) {
395 switch (format[0]) {
396 case 'a': { // 'a: accumulate multiplies
397 if (instr->Bit(21) == 0) {
398 Print("ul");
399 } else {
400 Print("la");
401 }
402 return 1;
403 }
404 case 'b': { // 'b: byte loads or stores
405 if (instr->HasB()) {
406 Print("b");
407 }
408 return 1;
409 }
410 case 'c': { // 'cond: conditional execution
411 ASSERT(STRING_STARTS_WITH(format, "cond"));
412 PrintCondition(instr);
413 return 4;
414 }
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100415 case 'd': { // 'd: vmov double immediate.
416 double d = instr->DoubleImmedVmov();
417 out_buffer_pos_ += v8i::OS::SNPrintF(out_buffer_ + out_buffer_pos_,
418 "#%g", d);
419 return 1;
420 }
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100421 case 'f': { // 'f: bitfield instructions - v7 and above.
422 uint32_t lsbit = instr->Bits(11, 7);
423 uint32_t width = instr->Bits(20, 16) + 1;
424 if (instr->Bit(21) == 0) {
425 // BFC/BFI:
426 // Bits 20-16 represent most-significant bit. Covert to width.
427 width -= lsbit;
428 ASSERT(width > 0);
429 }
430 ASSERT((width + lsbit) <= 32);
431 out_buffer_pos_ += v8i::OS::SNPrintF(out_buffer_ + out_buffer_pos_,
432 "#%d, #%d", lsbit, width);
433 return 1;
434 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000435 case 'h': { // 'h: halfword operation for extra loads and stores
436 if (instr->HasH()) {
437 Print("h");
438 } else {
439 Print("b");
440 }
441 return 1;
442 }
443 case 'l': { // 'l: branch and link
444 if (instr->HasLink()) {
445 Print("l");
446 }
447 return 1;
448 }
449 case 'm': {
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100450 if (format[1] == 'w') {
451 // 'mw: movt/movw instructions.
452 PrintMovwMovt(instr);
453 return 2;
454 }
455 if (format[1] == 'e') { // 'memop: load/store instructions.
Steve Blocka7e24c12009-10-30 11:49:00 +0000456 ASSERT(STRING_STARTS_WITH(format, "memop"));
457 if (instr->HasL()) {
458 Print("ldr");
Kristian Monsen25f61362010-05-21 11:50:48 +0100459 } else if ((instr->Bits(27, 25) == 0) && (instr->Bit(20) == 0)) {
460 if (instr->Bits(7, 4) == 0xf) {
461 Print("strd");
462 } else {
463 Print("ldrd");
464 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000465 } else {
466 Print("str");
467 }
468 return 5;
469 }
470 // 'msg: for simulator break instructions
471 ASSERT(STRING_STARTS_WITH(format, "msg"));
472 byte* str =
473 reinterpret_cast<byte*>(instr->InstructionBits() & 0x0fffffff);
474 out_buffer_pos_ += v8i::OS::SNPrintF(out_buffer_ + out_buffer_pos_,
475 "%s", converter_.NameInCode(str));
476 return 3;
477 }
478 case 'o': {
Andrei Popescu31002712010-02-23 13:46:05 +0000479 if ((format[3] == '1') && (format[4] == '2')) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000480 // 'off12: 12-bit offset for load and store instructions
481 ASSERT(STRING_STARTS_WITH(format, "off12"));
482 out_buffer_pos_ += v8i::OS::SNPrintF(out_buffer_ + out_buffer_pos_,
483 "%d", instr->Offset12Field());
484 return 5;
Steve Block6ded16b2010-05-10 14:33:55 +0100485 } else if (format[3] == '0') {
486 // 'off0to3and8to19 16-bit immediate encoded in bits 19-8 and 3-0.
487 ASSERT(STRING_STARTS_WITH(format, "off0to3and8to19"));
488 out_buffer_pos_ += v8i::OS::SNPrintF(out_buffer_ + out_buffer_pos_,
489 "%d",
490 (instr->Bits(19, 8) << 4) +
491 instr->Bits(3, 0));
492 return 15;
Steve Blocka7e24c12009-10-30 11:49:00 +0000493 }
494 // 'off8: 8-bit offset for extra load and store instructions
495 ASSERT(STRING_STARTS_WITH(format, "off8"));
496 int offs8 = (instr->ImmedHField() << 4) | instr->ImmedLField();
497 out_buffer_pos_ += v8i::OS::SNPrintF(out_buffer_ + out_buffer_pos_,
498 "%d", offs8);
499 return 4;
500 }
501 case 'p': { // 'pu: P and U bits for load and store instructions
502 ASSERT(STRING_STARTS_WITH(format, "pu"));
503 PrintPU(instr);
504 return 2;
505 }
506 case 'r': {
507 return FormatRegister(instr, format);
508 }
509 case 's': {
510 if (format[1] == 'h') { // 'shift_op or 'shift_rm
511 if (format[6] == 'o') { // 'shift_op
512 ASSERT(STRING_STARTS_WITH(format, "shift_op"));
513 if (instr->TypeField() == 0) {
514 PrintShiftRm(instr);
515 } else {
516 ASSERT(instr->TypeField() == 1);
517 PrintShiftImm(instr);
518 }
519 return 8;
520 } else { // 'shift_rm
521 ASSERT(STRING_STARTS_WITH(format, "shift_rm"));
522 PrintShiftRm(instr);
523 return 8;
524 }
525 } else if (format[1] == 'w') { // 'swi
526 ASSERT(STRING_STARTS_WITH(format, "swi"));
527 PrintSoftwareInterrupt(instr->SwiField());
528 return 3;
529 } else if (format[1] == 'i') { // 'sign: signed extra loads and stores
530 ASSERT(STRING_STARTS_WITH(format, "sign"));
531 if (instr->HasSign()) {
532 Print("s");
533 }
534 return 4;
535 }
536 // 's: S field of data processing instructions
537 if (instr->HasS()) {
538 Print("s");
539 }
540 return 1;
541 }
542 case 't': { // 'target: target of branch instructions
543 ASSERT(STRING_STARTS_WITH(format, "target"));
544 int off = (instr->SImmed24Field() << 2) + 8;
545 out_buffer_pos_ += v8i::OS::SNPrintF(
546 out_buffer_ + out_buffer_pos_,
547 "%+d -> %s",
548 off,
549 converter_.NameOfAddress(reinterpret_cast<byte*>(instr) + off));
550 return 6;
551 }
552 case 'u': { // 'u: signed or unsigned multiplies
553 // The manual gets the meaning of bit 22 backwards in the multiply
554 // instruction overview on page A3.16.2. The instructions that
555 // exist in u and s variants are the following:
556 // smull A4.1.87
557 // umull A4.1.129
558 // umlal A4.1.128
559 // smlal A4.1.76
560 // For these 0 means u and 1 means s. As can be seen on their individual
561 // pages. The other 18 mul instructions have the bit set or unset in
562 // arbitrary ways that are unrelated to the signedness of the instruction.
563 // None of these 18 instructions exist in both a 'u' and an 's' variant.
564
565 if (instr->Bit(22) == 0) {
566 Print("u");
567 } else {
568 Print("s");
569 }
570 return 1;
571 }
Steve Blockd0582a62009-12-15 09:54:21 +0000572 case 'v': {
573 return FormatVFPinstruction(instr, format);
574 }
575 case 'S':
576 case 'D': {
577 return FormatVFPRegister(instr, format);
578 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000579 case 'w': { // 'w: W field of load and store instructions
580 if (instr->HasW()) {
581 Print("!");
582 }
583 return 1;
584 }
585 default: {
586 UNREACHABLE();
587 break;
588 }
589 }
590 UNREACHABLE();
591 return -1;
592}
593
594
595// Format takes a formatting string for a whole instruction and prints it into
596// the output buffer. All escaped options are handed to FormatOption to be
597// parsed further.
598void Decoder::Format(Instr* instr, const char* format) {
599 char cur = *format++;
600 while ((cur != 0) && (out_buffer_pos_ < (out_buffer_.length() - 1))) {
601 if (cur == '\'') { // Single quote is used as the formatting escape.
602 format += FormatOption(instr, format);
603 } else {
604 out_buffer_[out_buffer_pos_++] = cur;
605 }
606 cur = *format++;
607 }
608 out_buffer_[out_buffer_pos_] = '\0';
609}
610
611
612// For currently unimplemented decodings the disassembler calls Unknown(instr)
613// which will just print "unknown" of the instruction bits.
614void Decoder::Unknown(Instr* instr) {
615 Format(instr, "unknown");
616}
617
618
619void Decoder::DecodeType01(Instr* instr) {
620 int type = instr->TypeField();
621 if ((type == 0) && instr->IsSpecialType0()) {
622 // multiply instruction or extra loads and stores
623 if (instr->Bits(7, 4) == 9) {
624 if (instr->Bit(24) == 0) {
625 // multiply instructions
626 if (instr->Bit(23) == 0) {
627 if (instr->Bit(21) == 0) {
628 // The MUL instruction description (A 4.1.33) refers to Rd as being
629 // the destination for the operation, but it confusingly uses the
630 // Rn field to encode it.
631 Format(instr, "mul'cond's 'rn, 'rm, 'rs");
632 } else {
633 // The MLA instruction description (A 4.1.28) refers to the order
634 // of registers as "Rd, Rm, Rs, Rn". But confusingly it uses the
635 // Rn field to encode the Rd register and the Rd field to encode
636 // the Rn register.
637 Format(instr, "mla'cond's 'rn, 'rm, 'rs, 'rd");
638 }
639 } else {
640 // The signed/long multiply instructions use the terms RdHi and RdLo
641 // when referring to the target registers. They are mapped to the Rn
642 // and Rd fields as follows:
643 // RdLo == Rd field
644 // RdHi == Rn field
645 // The order of registers is: <RdLo>, <RdHi>, <Rm>, <Rs>
646 Format(instr, "'um'al'cond's 'rd, 'rn, 'rm, 'rs");
647 }
648 } else {
649 Unknown(instr); // not used by V8
650 }
Kristian Monsen25f61362010-05-21 11:50:48 +0100651 } else if ((instr->Bit(20) == 0) && ((instr->Bits(7, 4) & 0xd) == 0xd)) {
652 // ldrd, strd
653 switch (instr->PUField()) {
654 case 0: {
655 if (instr->Bit(22) == 0) {
656 Format(instr, "'memop'cond's 'rd, ['rn], -'rm");
657 } else {
658 Format(instr, "'memop'cond's 'rd, ['rn], #-'off8");
659 }
660 break;
661 }
662 case 1: {
663 if (instr->Bit(22) == 0) {
664 Format(instr, "'memop'cond's 'rd, ['rn], +'rm");
665 } else {
666 Format(instr, "'memop'cond's 'rd, ['rn], #+'off8");
667 }
668 break;
669 }
670 case 2: {
671 if (instr->Bit(22) == 0) {
672 Format(instr, "'memop'cond's 'rd, ['rn, -'rm]'w");
673 } else {
674 Format(instr, "'memop'cond's 'rd, ['rn, #-'off8]'w");
675 }
676 break;
677 }
678 case 3: {
679 if (instr->Bit(22) == 0) {
680 Format(instr, "'memop'cond's 'rd, ['rn, +'rm]'w");
681 } else {
682 Format(instr, "'memop'cond's 'rd, ['rn, #+'off8]'w");
683 }
684 break;
685 }
686 default: {
687 // The PU field is a 2-bit field.
688 UNREACHABLE();
689 break;
690 }
691 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000692 } else {
693 // extra load/store instructions
694 switch (instr->PUField()) {
695 case 0: {
696 if (instr->Bit(22) == 0) {
697 Format(instr, "'memop'cond'sign'h 'rd, ['rn], -'rm");
698 } else {
699 Format(instr, "'memop'cond'sign'h 'rd, ['rn], #-'off8");
700 }
701 break;
702 }
703 case 1: {
704 if (instr->Bit(22) == 0) {
705 Format(instr, "'memop'cond'sign'h 'rd, ['rn], +'rm");
706 } else {
707 Format(instr, "'memop'cond'sign'h 'rd, ['rn], #+'off8");
708 }
709 break;
710 }
711 case 2: {
712 if (instr->Bit(22) == 0) {
713 Format(instr, "'memop'cond'sign'h 'rd, ['rn, -'rm]'w");
714 } else {
715 Format(instr, "'memop'cond'sign'h 'rd, ['rn, #-'off8]'w");
716 }
717 break;
718 }
719 case 3: {
720 if (instr->Bit(22) == 0) {
721 Format(instr, "'memop'cond'sign'h 'rd, ['rn, +'rm]'w");
722 } else {
723 Format(instr, "'memop'cond'sign'h 'rd, ['rn, #+'off8]'w");
724 }
725 break;
726 }
727 default: {
728 // The PU field is a 2-bit field.
729 UNREACHABLE();
730 break;
731 }
732 }
733 return;
734 }
Steve Block6ded16b2010-05-10 14:33:55 +0100735 } else if ((type == 0) && instr->IsMiscType0()) {
736 if (instr->Bits(22, 21) == 1) {
737 switch (instr->Bits(7, 4)) {
738 case BX:
739 Format(instr, "bx'cond 'rm");
740 break;
741 case BLX:
742 Format(instr, "blx'cond 'rm");
743 break;
744 case BKPT:
745 Format(instr, "bkpt 'off0to3and8to19");
746 break;
747 default:
748 Unknown(instr); // not used by V8
749 break;
750 }
751 } else if (instr->Bits(22, 21) == 3) {
752 switch (instr->Bits(7, 4)) {
753 case CLZ:
754 Format(instr, "clz'cond 'rd, 'rm");
755 break;
756 default:
757 Unknown(instr); // not used by V8
758 break;
759 }
760 } else {
761 Unknown(instr); // not used by V8
762 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000763 } else {
764 switch (instr->OpcodeField()) {
765 case AND: {
766 Format(instr, "and'cond's 'rd, 'rn, 'shift_op");
767 break;
768 }
769 case EOR: {
770 Format(instr, "eor'cond's 'rd, 'rn, 'shift_op");
771 break;
772 }
773 case SUB: {
774 Format(instr, "sub'cond's 'rd, 'rn, 'shift_op");
775 break;
776 }
777 case RSB: {
778 Format(instr, "rsb'cond's 'rd, 'rn, 'shift_op");
779 break;
780 }
781 case ADD: {
782 Format(instr, "add'cond's 'rd, 'rn, 'shift_op");
783 break;
784 }
785 case ADC: {
786 Format(instr, "adc'cond's 'rd, 'rn, 'shift_op");
787 break;
788 }
789 case SBC: {
790 Format(instr, "sbc'cond's 'rd, 'rn, 'shift_op");
791 break;
792 }
793 case RSC: {
794 Format(instr, "rsc'cond's 'rd, 'rn, 'shift_op");
795 break;
796 }
797 case TST: {
798 if (instr->HasS()) {
799 Format(instr, "tst'cond 'rn, 'shift_op");
800 } else {
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100801 Format(instr, "movw'cond 'mw");
Steve Blocka7e24c12009-10-30 11:49:00 +0000802 }
803 break;
804 }
805 case TEQ: {
806 if (instr->HasS()) {
807 Format(instr, "teq'cond 'rn, 'shift_op");
808 } else {
Steve Block6ded16b2010-05-10 14:33:55 +0100809 // Other instructions matching this pattern are handled in the
810 // miscellaneous instructions part above.
811 UNREACHABLE();
Steve Blocka7e24c12009-10-30 11:49:00 +0000812 }
813 break;
814 }
815 case CMP: {
816 if (instr->HasS()) {
817 Format(instr, "cmp'cond 'rn, 'shift_op");
818 } else {
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100819 Format(instr, "movt'cond 'mw");
Steve Blocka7e24c12009-10-30 11:49:00 +0000820 }
821 break;
822 }
823 case CMN: {
824 if (instr->HasS()) {
825 Format(instr, "cmn'cond 'rn, 'shift_op");
826 } else {
Steve Block6ded16b2010-05-10 14:33:55 +0100827 // Other instructions matching this pattern are handled in the
828 // miscellaneous instructions part above.
829 UNREACHABLE();
Steve Blocka7e24c12009-10-30 11:49:00 +0000830 }
831 break;
832 }
833 case ORR: {
834 Format(instr, "orr'cond's 'rd, 'rn, 'shift_op");
835 break;
836 }
837 case MOV: {
838 Format(instr, "mov'cond's 'rd, 'shift_op");
839 break;
840 }
841 case BIC: {
842 Format(instr, "bic'cond's 'rd, 'rn, 'shift_op");
843 break;
844 }
845 case MVN: {
846 Format(instr, "mvn'cond's 'rd, 'shift_op");
847 break;
848 }
849 default: {
850 // The Opcode field is a 4-bit field.
851 UNREACHABLE();
852 break;
853 }
854 }
855 }
856}
857
858
859void Decoder::DecodeType2(Instr* instr) {
860 switch (instr->PUField()) {
861 case 0: {
862 if (instr->HasW()) {
863 Unknown(instr); // not used in V8
864 }
865 Format(instr, "'memop'cond'b 'rd, ['rn], #-'off12");
866 break;
867 }
868 case 1: {
869 if (instr->HasW()) {
870 Unknown(instr); // not used in V8
871 }
872 Format(instr, "'memop'cond'b 'rd, ['rn], #+'off12");
873 break;
874 }
875 case 2: {
876 Format(instr, "'memop'cond'b 'rd, ['rn, #-'off12]'w");
877 break;
878 }
879 case 3: {
880 Format(instr, "'memop'cond'b 'rd, ['rn, #+'off12]'w");
881 break;
882 }
883 default: {
884 // The PU field is a 2-bit field.
885 UNREACHABLE();
886 break;
887 }
888 }
889}
890
891
892void Decoder::DecodeType3(Instr* instr) {
893 switch (instr->PUField()) {
894 case 0: {
895 ASSERT(!instr->HasW());
896 Format(instr, "'memop'cond'b 'rd, ['rn], -'shift_rm");
897 break;
898 }
899 case 1: {
900 ASSERT(!instr->HasW());
901 Format(instr, "'memop'cond'b 'rd, ['rn], +'shift_rm");
902 break;
903 }
904 case 2: {
905 Format(instr, "'memop'cond'b 'rd, ['rn, -'shift_rm]'w");
906 break;
907 }
908 case 3: {
Andrei Popescu31002712010-02-23 13:46:05 +0000909 if (instr->HasW() && (instr->Bits(6, 4) == 0x5)) {
910 uint32_t widthminus1 = static_cast<uint32_t>(instr->Bits(20, 16));
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100911 uint32_t lsbit = static_cast<uint32_t>(instr->Bits(11, 7));
Andrei Popescu31002712010-02-23 13:46:05 +0000912 uint32_t msbit = widthminus1 + lsbit;
913 if (msbit <= 31) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100914 if (instr->Bit(22)) {
915 Format(instr, "ubfx'cond 'rd, 'rm, 'f");
916 } else {
917 Format(instr, "sbfx'cond 'rd, 'rm, 'f");
918 }
919 } else {
920 UNREACHABLE();
921 }
922 } else if (!instr->HasW() && (instr->Bits(6, 4) == 0x1)) {
923 uint32_t lsbit = static_cast<uint32_t>(instr->Bits(11, 7));
924 uint32_t msbit = static_cast<uint32_t>(instr->Bits(20, 16));
925 if (msbit >= lsbit) {
926 if (instr->RmField() == 15) {
927 Format(instr, "bfc'cond 'rd, 'f");
928 } else {
929 Format(instr, "bfi'cond 'rd, 'rm, 'f");
930 }
Andrei Popescu31002712010-02-23 13:46:05 +0000931 } else {
932 UNREACHABLE();
933 }
934 } else {
935 Format(instr, "'memop'cond'b 'rd, ['rn, +'shift_rm]'w");
936 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000937 break;
938 }
939 default: {
940 // The PU field is a 2-bit field.
941 UNREACHABLE();
942 break;
943 }
944 }
945}
946
947
948void Decoder::DecodeType4(Instr* instr) {
949 ASSERT(instr->Bit(22) == 0); // Privileged mode currently not supported.
950 if (instr->HasL()) {
951 Format(instr, "ldm'cond'pu 'rn'w, 'rlist");
952 } else {
953 Format(instr, "stm'cond'pu 'rn'w, 'rlist");
954 }
955}
956
957
958void Decoder::DecodeType5(Instr* instr) {
959 Format(instr, "b'l'cond 'target");
960}
961
962
963void Decoder::DecodeType6(Instr* instr) {
Steve Blockd0582a62009-12-15 09:54:21 +0000964 DecodeType6CoprocessorIns(instr);
Steve Blocka7e24c12009-10-30 11:49:00 +0000965}
966
967
968void Decoder::DecodeType7(Instr* instr) {
969 if (instr->Bit(24) == 1) {
970 Format(instr, "swi'cond 'swi");
971 } else {
Steve Blockd0582a62009-12-15 09:54:21 +0000972 DecodeTypeVFP(instr);
Steve Blocka7e24c12009-10-30 11:49:00 +0000973 }
974}
975
Steve Blocka7e24c12009-10-30 11:49:00 +0000976void Decoder::DecodeUnconditional(Instr* instr) {
977 if (instr->Bits(7, 4) == 0xB && instr->Bits(27, 25) == 0 && instr->HasL()) {
978 Format(instr, "'memop'h'pu 'rd, ");
979 bool immediate = instr->HasB();
980 switch (instr->PUField()) {
981 case 0: {
982 // Post index, negative.
983 if (instr->HasW()) {
984 Unknown(instr);
985 break;
986 }
987 if (immediate) {
988 Format(instr, "['rn], #-'imm12");
989 } else {
990 Format(instr, "['rn], -'rm");
991 }
992 break;
993 }
994 case 1: {
995 // Post index, positive.
996 if (instr->HasW()) {
997 Unknown(instr);
998 break;
999 }
1000 if (immediate) {
1001 Format(instr, "['rn], #+'imm12");
1002 } else {
1003 Format(instr, "['rn], +'rm");
1004 }
1005 break;
1006 }
1007 case 2: {
1008 // Pre index or offset, negative.
1009 if (immediate) {
1010 Format(instr, "['rn, #-'imm12]'w");
1011 } else {
1012 Format(instr, "['rn, -'rm]'w");
1013 }
1014 break;
1015 }
1016 case 3: {
1017 // Pre index or offset, positive.
1018 if (immediate) {
1019 Format(instr, "['rn, #+'imm12]'w");
1020 } else {
1021 Format(instr, "['rn, +'rm]'w");
1022 }
1023 break;
1024 }
1025 default: {
1026 // The PU field is a 2-bit field.
1027 UNREACHABLE();
1028 break;
1029 }
1030 }
1031 return;
1032 }
1033 Format(instr, "break 'msg");
1034}
1035
1036
Steve Blockd0582a62009-12-15 09:54:21 +00001037// void Decoder::DecodeTypeVFP(Instr* instr)
Leon Clarkee46be812010-01-19 14:06:41 +00001038// vmov: Sn = Rt
1039// vmov: Rt = Sn
1040// vcvt: Dd = Sm
1041// vcvt: Sd = Dm
1042// Dd = vadd(Dn, Dm)
1043// Dd = vsub(Dn, Dm)
1044// Dd = vmul(Dn, Dm)
1045// Dd = vdiv(Dn, Dm)
Steve Blockd0582a62009-12-15 09:54:21 +00001046// vcmp(Dd, Dm)
Steve Block8defd9f2010-07-08 12:39:36 +01001047// vmrs
1048// Dd = vsqrt(Dm)
Steve Blockd0582a62009-12-15 09:54:21 +00001049void Decoder::DecodeTypeVFP(Instr* instr) {
1050 ASSERT((instr->TypeField() == 7) && (instr->Bit(24) == 0x0) );
Steve Block6ded16b2010-05-10 14:33:55 +01001051 ASSERT(instr->Bits(11, 9) == 0x5);
Steve Blockd0582a62009-12-15 09:54:21 +00001052
Steve Block6ded16b2010-05-10 14:33:55 +01001053 if (instr->Bit(4) == 0) {
1054 if (instr->Opc1Field() == 0x7) {
1055 // Other data processing instructions
Steve Block8defd9f2010-07-08 12:39:36 +01001056 if ((instr->Opc2Field() == 0x0) && (instr->Opc3Field() == 0x1)) {
1057 // vmov register to register.
1058 if (instr->SzField() == 0x1) {
1059 Format(instr, "vmov.f64'cond 'Dd, 'Dm");
1060 } else {
Ben Murdoch3bec4d22010-07-22 14:51:16 +01001061 Format(instr, "vmov.f32'cond 'Sd, 'Sm");
Steve Block8defd9f2010-07-08 12:39:36 +01001062 }
1063 } else if ((instr->Opc2Field() == 0x7) && (instr->Opc3Field() == 0x3)) {
Steve Block6ded16b2010-05-10 14:33:55 +01001064 DecodeVCVTBetweenDoubleAndSingle(instr);
1065 } else if ((instr->Opc2Field() == 0x8) && (instr->Opc3Field() & 0x1)) {
1066 DecodeVCVTBetweenFloatingPointAndInteger(instr);
1067 } else if (((instr->Opc2Field() >> 1) == 0x6) &&
1068 (instr->Opc3Field() & 0x1)) {
1069 DecodeVCVTBetweenFloatingPointAndInteger(instr);
1070 } else if (((instr->Opc2Field() == 0x4) || (instr->Opc2Field() == 0x5)) &&
1071 (instr->Opc3Field() & 0x1)) {
1072 DecodeVCMP(instr);
Steve Block8defd9f2010-07-08 12:39:36 +01001073 } else if (((instr->Opc2Field() == 0x1)) && (instr->Opc3Field() == 0x3)) {
1074 Format(instr, "vsqrt.f64'cond 'Dd, 'Dm");
Ben Murdoch3bec4d22010-07-22 14:51:16 +01001075 } else if (instr->Opc3Field() == 0x0) {
1076 if (instr->SzField() == 0x1) {
1077 Format(instr, "vmov.f64'cond 'Dd, 'd");
1078 } else {
1079 Unknown(instr); // Not used by V8.
1080 }
Steve Block6ded16b2010-05-10 14:33:55 +01001081 } else {
1082 Unknown(instr); // Not used by V8.
1083 }
1084 } else if (instr->Opc1Field() == 0x3) {
1085 if (instr->SzField() == 0x1) {
1086 if (instr->Opc3Field() & 0x1) {
1087 Format(instr, "vsub.f64'cond 'Dd, 'Dn, 'Dm");
1088 } else {
1089 Format(instr, "vadd.f64'cond 'Dd, 'Dn, 'Dm");
1090 }
1091 } else {
1092 Unknown(instr); // Not used by V8.
1093 }
1094 } else if ((instr->Opc1Field() == 0x2) && !(instr->Opc3Field() & 0x1)) {
1095 if (instr->SzField() == 0x1) {
1096 Format(instr, "vmul.f64'cond 'Dd, 'Dn, 'Dm");
1097 } else {
1098 Unknown(instr); // Not used by V8.
1099 }
1100 } else if ((instr->Opc1Field() == 0x4) && !(instr->Opc3Field() & 0x1)) {
1101 if (instr->SzField() == 0x1) {
Steve Blockd0582a62009-12-15 09:54:21 +00001102 Format(instr, "vdiv.f64'cond 'Dd, 'Dn, 'Dm");
Steve Block6ded16b2010-05-10 14:33:55 +01001103 } else {
1104 Unknown(instr); // Not used by V8.
1105 }
Steve Blockd0582a62009-12-15 09:54:21 +00001106 } else {
1107 Unknown(instr); // Not used by V8.
1108 }
1109 } else {
Steve Block6ded16b2010-05-10 14:33:55 +01001110 if ((instr->VCField() == 0x0) &&
1111 (instr->VAField() == 0x0)) {
1112 DecodeVMOVBetweenCoreAndSinglePrecisionRegisters(instr);
1113 } else if ((instr->VLField() == 0x1) &&
1114 (instr->VCField() == 0x0) &&
1115 (instr->VAField() == 0x7) &&
1116 (instr->Bits(19, 16) == 0x1)) {
1117 if (instr->Bits(15, 12) == 0xF)
1118 Format(instr, "vmrs'cond APSR, FPSCR");
1119 else
1120 Unknown(instr); // Not used by V8.
Steve Blockd0582a62009-12-15 09:54:21 +00001121 } else {
1122 Unknown(instr); // Not used by V8.
1123 }
1124 }
1125}
1126
1127
Steve Block6ded16b2010-05-10 14:33:55 +01001128void Decoder::DecodeVMOVBetweenCoreAndSinglePrecisionRegisters(Instr* instr) {
1129 ASSERT((instr->Bit(4) == 1) && (instr->VCField() == 0x0) &&
1130 (instr->VAField() == 0x0));
1131
1132 bool to_arm_register = (instr->VLField() == 0x1);
1133
1134 if (to_arm_register) {
1135 Format(instr, "vmov'cond 'rt, 'Sn");
1136 } else {
1137 Format(instr, "vmov'cond 'Sn, 'rt");
1138 }
1139}
1140
1141
1142void Decoder::DecodeVCMP(Instr* instr) {
1143 ASSERT((instr->Bit(4) == 0) && (instr->Opc1Field() == 0x7));
1144 ASSERT(((instr->Opc2Field() == 0x4) || (instr->Opc2Field() == 0x5)) &&
1145 (instr->Opc3Field() & 0x1));
1146
1147 // Comparison.
1148 bool dp_operation = (instr->SzField() == 1);
1149 bool raise_exception_for_qnan = (instr->Bit(7) == 0x1);
1150
1151 if (dp_operation && !raise_exception_for_qnan) {
1152 Format(instr, "vcmp.f64'cond 'Dd, 'Dm");
1153 } else {
1154 Unknown(instr); // Not used by V8.
1155 }
1156}
1157
1158
1159void Decoder::DecodeVCVTBetweenDoubleAndSingle(Instr* instr) {
1160 ASSERT((instr->Bit(4) == 0) && (instr->Opc1Field() == 0x7));
1161 ASSERT((instr->Opc2Field() == 0x7) && (instr->Opc3Field() == 0x3));
1162
1163 bool double_to_single = (instr->SzField() == 1);
1164
1165 if (double_to_single) {
1166 Format(instr, "vcvt.f32.f64'cond 'Sd, 'Dm");
1167 } else {
1168 Format(instr, "vcvt.f64.f32'cond 'Dd, 'Sm");
1169 }
1170}
1171
1172
1173void Decoder::DecodeVCVTBetweenFloatingPointAndInteger(Instr* instr) {
1174 ASSERT((instr->Bit(4) == 0) && (instr->Opc1Field() == 0x7));
1175 ASSERT(((instr->Opc2Field() == 0x8) && (instr->Opc3Field() & 0x1)) ||
1176 (((instr->Opc2Field() >> 1) == 0x6) && (instr->Opc3Field() & 0x1)));
1177
1178 bool to_integer = (instr->Bit(18) == 1);
1179 bool dp_operation = (instr->SzField() == 1);
1180 if (to_integer) {
1181 bool unsigned_integer = (instr->Bit(16) == 0);
1182
1183 if (dp_operation) {
1184 if (unsigned_integer) {
1185 Format(instr, "vcvt.u32.f64'cond 'Sd, 'Dm");
1186 } else {
1187 Format(instr, "vcvt.s32.f64'cond 'Sd, 'Dm");
1188 }
1189 } else {
1190 if (unsigned_integer) {
1191 Format(instr, "vcvt.u32.f32'cond 'Sd, 'Sm");
1192 } else {
1193 Format(instr, "vcvt.s32.f32'cond 'Sd, 'Sm");
1194 }
1195 }
1196 } else {
1197 bool unsigned_integer = (instr->Bit(7) == 0);
1198
1199 if (dp_operation) {
1200 if (unsigned_integer) {
1201 Format(instr, "vcvt.f64.u32'cond 'Dd, 'Sm");
1202 } else {
1203 Format(instr, "vcvt.f64.s32'cond 'Dd, 'Sm");
1204 }
1205 } else {
1206 if (unsigned_integer) {
1207 Format(instr, "vcvt.f32.u32'cond 'Sd, 'Sm");
1208 } else {
1209 Format(instr, "vcvt.f32.s32'cond 'Sd, 'Sm");
1210 }
1211 }
1212 }
1213}
1214
1215
Steve Blockd0582a62009-12-15 09:54:21 +00001216// Decode Type 6 coprocessor instructions.
Leon Clarkee46be812010-01-19 14:06:41 +00001217// Dm = vmov(Rt, Rt2)
1218// <Rt, Rt2> = vmov(Dm)
Leon Clarked91b9f72010-01-27 17:25:45 +00001219// Ddst = MEM(Rbase + 4*offset).
1220// MEM(Rbase + 4*offset) = Dsrc.
Steve Blockd0582a62009-12-15 09:54:21 +00001221void Decoder::DecodeType6CoprocessorIns(Instr* instr) {
1222 ASSERT((instr->TypeField() == 6));
1223
Steve Block6ded16b2010-05-10 14:33:55 +01001224 if (instr->CoprocessorField() == 0xA) {
1225 switch (instr->OpcodeField()) {
1226 case 0x8:
1227 if (instr->HasL()) {
1228 Format(instr, "vldr'cond 'Sd, ['rn - 4*'off8]");
1229 } else {
1230 Format(instr, "vstr'cond 'Sd, ['rn - 4*'off8]");
1231 }
1232 break;
1233 case 0xC:
1234 if (instr->HasL()) {
1235 Format(instr, "vldr'cond 'Sd, ['rn + 4*'off8]");
1236 } else {
1237 Format(instr, "vstr'cond 'Sd, ['rn + 4*'off8]");
1238 }
1239 break;
1240 default:
1241 Unknown(instr); // Not used by V8.
1242 break;
1243 }
1244 } else if (instr->CoprocessorField() == 0xB) {
Leon Clarked91b9f72010-01-27 17:25:45 +00001245 switch (instr->OpcodeField()) {
1246 case 0x2:
1247 // Load and store double to two GP registers
1248 if (instr->Bits(7, 4) != 0x1) {
1249 Unknown(instr); // Not used by V8.
1250 } else if (instr->HasL()) {
1251 Format(instr, "vmov'cond 'rt, 'rn, 'Dm");
1252 } else {
1253 Format(instr, "vmov'cond 'Dm, 'rt, 'rn");
1254 }
1255 break;
1256 case 0x8:
1257 if (instr->HasL()) {
1258 Format(instr, "vldr'cond 'Dd, ['rn - 4*'off8]");
1259 } else {
1260 Format(instr, "vstr'cond 'Dd, ['rn - 4*'off8]");
1261 }
1262 break;
1263 case 0xC:
1264 if (instr->HasL()) {
1265 Format(instr, "vldr'cond 'Dd, ['rn + 4*'off8]");
1266 } else {
1267 Format(instr, "vstr'cond 'Dd, ['rn + 4*'off8]");
1268 }
1269 break;
1270 default:
1271 Unknown(instr); // Not used by V8.
1272 break;
1273 }
Steve Block6ded16b2010-05-10 14:33:55 +01001274 } else {
1275 UNIMPLEMENTED(); // Not used by V8.
Steve Blockd0582a62009-12-15 09:54:21 +00001276 }
1277}
1278
1279
Steve Blocka7e24c12009-10-30 11:49:00 +00001280// Disassemble the instruction at *instr_ptr into the output buffer.
1281int Decoder::InstructionDecode(byte* instr_ptr) {
1282 Instr* instr = Instr::At(instr_ptr);
1283 // Print raw instruction bytes.
1284 out_buffer_pos_ += v8i::OS::SNPrintF(out_buffer_ + out_buffer_pos_,
1285 "%08x ",
1286 instr->InstructionBits());
1287 if (instr->ConditionField() == special_condition) {
1288 DecodeUnconditional(instr);
1289 return Instr::kInstrSize;
1290 }
1291 switch (instr->TypeField()) {
1292 case 0:
1293 case 1: {
1294 DecodeType01(instr);
1295 break;
1296 }
1297 case 2: {
1298 DecodeType2(instr);
1299 break;
1300 }
1301 case 3: {
1302 DecodeType3(instr);
1303 break;
1304 }
1305 case 4: {
1306 DecodeType4(instr);
1307 break;
1308 }
1309 case 5: {
1310 DecodeType5(instr);
1311 break;
1312 }
1313 case 6: {
1314 DecodeType6(instr);
1315 break;
1316 }
1317 case 7: {
1318 DecodeType7(instr);
1319 break;
1320 }
1321 default: {
1322 // The type field is 3-bits in the ARM encoding.
1323 UNREACHABLE();
1324 break;
1325 }
1326 }
1327 return Instr::kInstrSize;
1328}
1329
1330
1331} } // namespace assembler::arm
1332
1333
1334
1335//------------------------------------------------------------------------------
1336
1337namespace disasm {
1338
1339namespace v8i = v8::internal;
1340
1341
1342const char* NameConverter::NameOfAddress(byte* addr) const {
1343 static v8::internal::EmbeddedVector<char, 32> tmp_buffer;
1344 v8::internal::OS::SNPrintF(tmp_buffer, "%p", addr);
1345 return tmp_buffer.start();
1346}
1347
1348
1349const char* NameConverter::NameOfConstant(byte* addr) const {
1350 return NameOfAddress(addr);
1351}
1352
1353
1354const char* NameConverter::NameOfCPURegister(int reg) const {
1355 return assembler::arm::Registers::Name(reg);
1356}
1357
1358
1359const char* NameConverter::NameOfByteCPURegister(int reg) const {
1360 UNREACHABLE(); // ARM does not have the concept of a byte register
1361 return "nobytereg";
1362}
1363
1364
1365const char* NameConverter::NameOfXMMRegister(int reg) const {
1366 UNREACHABLE(); // ARM does not have any XMM registers
1367 return "noxmmreg";
1368}
1369
1370
1371const char* NameConverter::NameInCode(byte* addr) const {
1372 // The default name converter is called for unknown code. So we will not try
1373 // to access any memory.
1374 return "";
1375}
1376
1377
1378//------------------------------------------------------------------------------
1379
1380Disassembler::Disassembler(const NameConverter& converter)
1381 : converter_(converter) {}
1382
1383
1384Disassembler::~Disassembler() {}
1385
1386
1387int Disassembler::InstructionDecode(v8::internal::Vector<char> buffer,
1388 byte* instruction) {
1389 assembler::arm::Decoder d(converter_, buffer);
1390 return d.InstructionDecode(instruction);
1391}
1392
1393
1394int Disassembler::ConstantPoolSizeAt(byte* instruction) {
1395 int instruction_bits = *(reinterpret_cast<int*>(instruction));
1396 if ((instruction_bits & 0xfff00000) == 0x03000000) {
1397 return instruction_bits & 0x0000ffff;
1398 } else {
1399 return -1;
1400 }
1401}
1402
1403
1404void Disassembler::Disassemble(FILE* f, byte* begin, byte* end) {
1405 NameConverter converter;
1406 Disassembler d(converter);
1407 for (byte* pc = begin; pc < end;) {
1408 v8::internal::EmbeddedVector<char, 128> buffer;
1409 buffer[0] = '\0';
1410 byte* prev_pc = pc;
1411 pc += d.InstructionDecode(buffer, pc);
1412 fprintf(f, "%p %08x %s\n",
1413 prev_pc, *reinterpret_cast<int32_t*>(prev_pc), buffer.start());
1414 }
1415}
1416
1417
1418} // namespace disasm
Leon Clarkef7060e22010-06-03 12:02:55 +01001419
1420#endif // V8_TARGET_ARCH_ARM