Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(18)

Side by Side Diff: runtime/vm/disassembler_arm64.cc

Issue 221133002: Begins work on ARM64, first assembler test. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: adds files Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « runtime/vm/debugger_test.cc ('k') | runtime/vm/disassembler_test.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4
5 #include "vm/disassembler.h"
6
7 #include "vm/globals.h" // Needed here to get TARGET_ARCH_ARM64.
8 #if defined(TARGET_ARCH_ARM64)
9 #include "platform/assert.h"
10
11 namespace dart {
12
13 class ARM64Decoder : public ValueObject {
14 public:
15 ARM64Decoder(char* buffer, size_t buffer_size)
16 : buffer_(buffer),
17 buffer_size_(buffer_size),
18 buffer_pos_(0) {
19 buffer_[buffer_pos_] = '\0';
20 }
21
22 ~ARM64Decoder() {}
23
24 // Writes one disassembled instruction into 'buffer' (0-terminated).
25 // Returns true if the instruction was successfully decoded, false otherwise.
26 void InstructionDecode(uword pc);
27
28 private:
29 // Bottleneck functions to print into the out_buffer.
30 void Print(const char* str);
31
32 // Printing of common values.
33 void PrintRegister(int reg);
34 void PrintShiftExtendRm(Instr* instr);
35 void PrintS(Instr* instr);
36
37 // Handle formatting of instructions and their options.
38 int FormatRegister(Instr* instr, const char* option);
39 int FormatOption(Instr* instr, const char* format);
40 void Format(Instr* instr, const char* format);
41 void Unknown(Instr* instr);
42
43 // Decode instructions.
44 #define DECODE_OP(op) \
45 void Decode##op(Instr* instr);
46 APPLY_OP_LIST(DECODE_OP)
47 #undef DECODE_OP
48
49
50 // Convenience functions.
51 char* get_buffer() const { return buffer_; }
52 char* current_position_in_buffer() { return buffer_ + buffer_pos_; }
53 size_t remaining_size_in_buffer() { return buffer_size_ - buffer_pos_; }
54
55 char* buffer_; // Decode instructions into this buffer.
56 size_t buffer_size_; // The size of the character buffer.
57 size_t buffer_pos_; // Current character position in buffer.
58
59 DISALLOW_ALLOCATION();
60 DISALLOW_COPY_AND_ASSIGN(ARM64Decoder);
61 };
62
63
64 // Support for assertions in the ARM64Decoder formatting functions.
65 #define STRING_STARTS_WITH(string, compare_string) \
66 (strncmp(string, compare_string, strlen(compare_string)) == 0)
67
68
69 // Append the str to the output buffer.
70 void ARM64Decoder::Print(const char* str) {
71 char cur = *str++;
72 while (cur != '\0' && (buffer_pos_ < (buffer_size_ - 1))) {
73 buffer_[buffer_pos_++] = cur;
74 cur = *str++;
75 }
76 buffer_[buffer_pos_] = '\0';
77 }
78
79
80 // These register names are defined in a way to match the native disassembler
81 // formatting, except for register aliases ctx (r9) and pp (r10).
82 // See for example the command "objdump -d <binary file>".
83 static const char* reg_names[kNumberOfCpuRegisters] = {
84 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
85 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
86 "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
87 "r24", "ip0", "ip1", "pp", "ctx", "fp", "lr", "r31",
88 };
89
90
91 // Print the register name according to the active name converter.
92 void ARM64Decoder::PrintRegister(int reg) {
93 ASSERT(0 <= reg);
94 ASSERT(reg < kNumberOfCpuRegisters);
95 Print(reg_names[reg]);
96 }
97
98
99 // These shift names are defined in a way to match the native disassembler
100 // formatting. See for example the command "objdump -d <binary file>".
101 static const char* shift_names[kMaxShift] = {
102 "lsl", "lsr", "asr", "ror"
103 };
104
105
106 static const char* extend_names[kMaxExtend] = {
107 "uxtb", "uxth", "uxtw", "uxtx",
108 "sxtb", "sxth", "sxtw", "sxtx",
109 };
110
111
112 // Print the register shift operands for the instruction. Generally used for
113 // data processing instructions.
114 void ARM64Decoder::PrintShiftExtendRm(Instr* instr) {
115 int rm = instr->RmField();
116 Shift shift = instr->ShiftTypeField();
117 int shift_amount = instr->ShiftAmountField();
118 Extend extend = instr->ExtendTypeField();
119 int extend_shift_amount = instr->ExtShiftAmountField();
120
121 PrintRegister(rm);
122
123 if (instr->IsShift() && (shift == LSL) && (shift_amount == 0)) {
124 // Special case for using rm only.
125 return;
126 }
127 if (instr->IsShift()) {
128 // by immediate
129 if ((shift == ROR) && (shift_amount == 0)) {
130 Print(", RRX");
131 return;
132 } else if (((shift == LSR) || (shift == ASR)) && (shift_amount == 0)) {
133 shift_amount = 32;
134 }
135 buffer_pos_ += OS::SNPrint(current_position_in_buffer(),
136 remaining_size_in_buffer(),
137 ", %s #%d",
138 shift_names[shift],
139 shift_amount);
140 } else {
141 ASSERT(instr->IsExtend());
142 // by register
143 buffer_pos_ += OS::SNPrint(current_position_in_buffer(),
144 remaining_size_in_buffer(),
145 ", %s",
146 extend_names[extend]);
147 if (((instr->SFField() == 1) && (extend == UXTX)) ||
148 ((instr->SFField() == 0) && (extend == UXTW))) {
149 // Shift amount.
150 buffer_pos_ += OS::SNPrint(current_position_in_buffer(),
151 remaining_size_in_buffer(),
152 " %d",
153 extend_shift_amount);
154 }
155 }
156 }
157
158
159 // Handle all register based formatting in these functions to reduce the
160 // complexity of FormatOption.
161 int ARM64Decoder::FormatRegister(Instr* instr, const char* format) {
162 ASSERT(format[0] == 'r');
163 if (format[1] == 'n') { // 'rn: Rn register
164 int reg = instr->RnField();
165 PrintRegister(reg);
166 return 2;
167 } else if (format[1] == 'd') { // 'rd: Rd register
168 int reg = instr->RdField();
169 PrintRegister(reg);
170 return 2;
171 } else if (format[1] == 'm') { // 'rm: Rm register
172 int reg = instr->RmField();
173 PrintRegister(reg);
174 return 2;
175 }
176 UNREACHABLE();
177 return -1;
178 }
179
180
181 // FormatOption takes a formatting string and interprets it based on
182 // the current instructions. The format string points to the first
183 // character of the option string (the option escape has already been
184 // consumed by the caller.) FormatOption returns the number of
185 // characters that were consumed from the formatting string.
186 int ARM64Decoder::FormatOption(Instr* instr, const char* format) {
187 switch (format[0]) {
188 case 'i': { // 'imm12, imm16
189 uint64_t imm;
190 int ret = 5;
191 if (format[4] == '2') {
192 ASSERT(STRING_STARTS_WITH(format, "imm12"));
193 imm = instr->Imm12Field();
194 if (format[5] == 's') {
195 // shifted immediate.
196 if (instr->Imm12ShiftField() == 1) {
197 imm = imm << 12;
198 } else if ((instr->Imm12ShiftField() & 0x2) != 0) {
199 Print("Unknown Shift");
200 }
201 ret = 6;
202 }
203 } else {
204 ASSERT(STRING_STARTS_WITH(format, "imm16"));
205 imm = instr->Imm16Field();
206 }
207 buffer_pos_ += OS::SNPrint(current_position_in_buffer(),
208 remaining_size_in_buffer(),
209 "0x%"Px64,
210 imm);
211 return ret;
212 }
213 case 's': { // 's: S flag.
214 if (format[1] == 'h') {
215 ASSERT(STRING_STARTS_WITH(format, "shift_op"));
216 PrintShiftExtendRm(instr);
217 return 8;
218 } else if (format[1] == 'f') {
219 ASSERT(STRING_STARTS_WITH(format, "sf"));
220 if (instr->SFField() == 1) {
221 // TODO(zra): If we don't use the w form much, we can omit printing
222 // this x.
223 Print("x");
224 } else {
225 Print("w");
226 }
227 return 2;
228 } else if (format[1] == ' ') {
229 if (instr->HasS()) {
230 Print("s");
231 }
232 return 1;
233 } else {
234 UNREACHABLE();
235 }
236 }
237 case 'r': {
238 return FormatRegister(instr, format);
239 }
240 default: {
241 UNREACHABLE();
242 break;
243 }
244 }
245 UNREACHABLE();
246 return -1;
247 }
248
249
250 // Format takes a formatting string for a whole instruction and prints it into
251 // the output buffer. All escaped options are handed to FormatOption to be
252 // parsed further.
253 void ARM64Decoder::Format(Instr* instr, const char* format) {
254 char cur = *format++;
255 while ((cur != 0) && (buffer_pos_ < (buffer_size_ - 1))) {
256 if (cur == '\'') { // Single quote is used as the formatting escape.
257 format += FormatOption(instr, format);
258 } else {
259 buffer_[buffer_pos_++] = cur;
260 }
261 cur = *format++;
262 }
263 buffer_[buffer_pos_] = '\0';
264 }
265
266
267 // For currently unimplemented decodings the disassembler calls Unknown(instr)
268 // which will just print "unknown" of the instruction bits.
269 void ARM64Decoder::Unknown(Instr* instr) {
270 Format(instr, "unknown");
271 }
272
273
274 void ARM64Decoder::DecodeMoveWide(Instr* instr) {
275 switch (instr->Bits(29, 2)) {
276 case 0:
277 Format(instr, "movn'sf 'rd, 'imm16");
278 break;
279 case 2:
280 Format(instr, "movz'sf 'rd, 'imm16");
281 break;
282 case 3:
283 Format(instr, "movk'sf 'rd, 'imm16");
284 break;
285 default:
286 Unknown(instr);
287 break;
288 }
289 }
290
291
292 void ARM64Decoder::DecodeAddSubImm(Instr* instr) {
293 switch (instr->Bit(30)) {
294 case 0:
295 Format(instr, "addi'sf's 'rd, 'rn, 'imm12s");
296 break;
297 case 1:
298 Format(instr, "subi'sf's 'rd, 'rn, 'imm12s");
299 break;
300 default:
301 Unknown(instr);
302 break;
303 }
304 }
305
306 void ARM64Decoder::DecodeDPImmediate(Instr* instr) {
307 if (instr->IsMoveWideOp()) {
308 DecodeMoveWide(instr);
309 } else if (instr->IsAddSubImmOp()) {
310 DecodeAddSubImm(instr);
311 } else {
312 Unknown(instr);
313 }
314 }
315
316
317 void ARM64Decoder::DecodeExceptionGen(Instr* instr) {
318 if ((instr->Bits(0, 2) == 1) && (instr->Bits(2, 3) == 0) &&
319 (instr->Bits(21, 3) == 0)) {
320 Format(instr, "svc 'imm16");
321 } else if ((instr->Bits(0, 2) == 0) && (instr->Bits(2, 3) == 0) &&
322 (instr->Bits(21, 3) == 1)) {
323 Format(instr, "brk 'imm16");
324 } else if ((instr->Bits(0, 2) == 0) && (instr->Bits(2, 3) == 0) &&
325 (instr->Bits(21, 3) == 2)) {
326 Format(instr, "hlt 'imm16");
327 }
328 }
329
330
331 void ARM64Decoder::DecodeSystem(Instr* instr) {
332 if ((instr->Bits(0, 8) == 0x5f) && (instr->Bits(12, 4) == 2) &&
333 (instr->Bits(16, 3) == 3) && (instr->Bits(19, 2) == 0) &&
334 (instr->Bit(21) == 0)) {
335 if (instr->Bits(8, 4) == 0) {
336 Format(instr, "nop");
337 } else {
338 Unknown(instr);
339 }
340 } else {
341 Unknown(instr);
342 }
343 }
344
345
346 void ARM64Decoder::DecodeUnconditionalBranchReg(Instr* instr) {
347 if ((instr->Bits(0, 5) == 0) && (instr->Bits(10, 5) == 0) &&
348 (instr->Bits(16, 5) == 0x1f)) {
349 switch (instr->Bits(21, 4)) {
350 case 0:
351 Format(instr, "br 'rn");
352 break;
353 case 1:
354 Format(instr, "blr 'rn");
355 break;
356 case 2:
357 Format(instr, "ret 'rn");
358 break;
359 default:
360 Unknown(instr);
361 break;
362 }
363 }
364 }
365
366
367 void ARM64Decoder::DecodeCompareBranch(Instr* instr) {
368 if (instr->IsExceptionGenOp()) {
369 DecodeExceptionGen(instr);
370 } else if (instr->IsSystemOp()) {
371 DecodeSystem(instr);
372 } else if (instr->IsUnconditionalBranchRegOp()) {
373 DecodeUnconditionalBranchReg(instr);
374 } else {
375 Unknown(instr);
376 }
377 }
378
379
380 void ARM64Decoder::DecodeLoadStore(Instr* instr) {
381 Unknown(instr);
382 }
383
384
385 void ARM64Decoder::DecodeAddSubShiftExt(Instr* instr) {
386 switch (instr->Bit(30)) {
387 case 0:
388 Format(instr, "add'sf's 'rd, 'rn, 'shift_op");
389 break;
390 case 1:
391 Format(instr, "sub'sf's 'rd, 'rn, 'shift_op");
392 break;
393 default:
394 UNREACHABLE();
395 break;
396 }
397 }
398
399
400 void ARM64Decoder::DecodeDPRegister(Instr* instr) {
401 if (instr->IsAddSubShiftExtOp()) {
402 DecodeAddSubShiftExt(instr);
403 } else {
404 Unknown(instr);
405 }
406 }
407
408
409 void ARM64Decoder::DecodeDPSimd1(Instr* instr) {
410 Unknown(instr);
411 }
412
413
414 void ARM64Decoder::DecodeDPSimd2(Instr* instr) {
415 Unknown(instr);
416 }
417
418
419 void ARM64Decoder::InstructionDecode(uword pc) {
420 Instr* instr = Instr::At(pc);
421
422 if (instr->IsDPImmediateOp()) {
423 DecodeDPImmediate(instr);
424 } else if (instr->IsCompareBranchOp()) {
425 DecodeCompareBranch(instr);
426 } else if (instr->IsLoadStoreOp()) {
427 DecodeLoadStore(instr);
428 } else if (instr->IsDPRegisterOp()) {
429 DecodeDPRegister(instr);
430 } else if (instr->IsDPSimd1Op()) {
431 DecodeDPSimd1(instr);
432 } else {
433 ASSERT(instr->IsDPSimd2Op());
434 DecodeDPSimd2(instr);
435 }
436 }
437
438
439 void Disassembler::DecodeInstruction(char* hex_buffer, intptr_t hex_size,
440 char* human_buffer, intptr_t human_size,
441 int* out_instr_size, uword pc) {
442 ARM64Decoder decoder(human_buffer, human_size);
443 decoder.InstructionDecode(pc);
444 int32_t instruction_bits = Instr::At(pc)->InstructionBits();
445 OS::SNPrint(hex_buffer, hex_size, "%08x", instruction_bits);
446 if (out_instr_size) {
447 *out_instr_size = Instr::kInstrSize;
448 }
449 }
450
451
452 void Disassembler::Disassemble(uword start,
453 uword end,
454 DisassemblyFormatter* formatter,
455 const Code::Comments& comments) {
456 ASSERT(formatter != NULL);
457 char hex_buffer[kHexadecimalBufferSize]; // Instruction in hexadecimal form.
458 char human_buffer[kUserReadableBufferSize]; // Human-readable instruction.
459 uword pc = start;
460 intptr_t comment_finger = 0;
461 while (pc < end) {
462 const intptr_t offset = pc - start;
463 while (comment_finger < comments.Length() &&
464 comments.PCOffsetAt(comment_finger) <= offset) {
465 formatter->Print(
466 " ;; %s\n",
467 String::Handle(comments.CommentAt(comment_finger)).ToCString());
468 comment_finger++;
469 }
470 int instruction_length;
471 DecodeInstruction(hex_buffer, sizeof(hex_buffer),
472 human_buffer, sizeof(human_buffer),
473 &instruction_length, pc);
474
475 formatter->ConsumeInstruction(hex_buffer,
476 sizeof(hex_buffer),
477 human_buffer,
478 sizeof(human_buffer),
479 pc);
480 pc += instruction_length;
481 }
482 }
483
484 } // namespace dart
485
486 #endif // defined TARGET_ARCH_ARM
OLDNEW
« no previous file with comments | « runtime/vm/debugger_test.cc ('k') | runtime/vm/disassembler_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698