Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 Print("x"); | |
|
regis
2014/04/01 22:25:15
You could skip printing the 'x', to be in sync wit
zra
2014/04/02 00:04:25
I'll keep the x for now while I'm getting started,
| |
| 222 } else { | |
| 223 Print("w"); | |
| 224 } | |
| 225 return 2; | |
| 226 } else if (format[1] == ' ') { | |
| 227 if (instr->HasS()) { | |
| 228 Print("s"); | |
| 229 } | |
| 230 return 1; | |
| 231 } else { | |
| 232 UNREACHABLE(); | |
| 233 } | |
| 234 } | |
| 235 case 'r': { | |
| 236 return FormatRegister(instr, format); | |
| 237 } | |
| 238 default: { | |
| 239 UNREACHABLE(); | |
| 240 break; | |
| 241 } | |
| 242 } | |
| 243 UNREACHABLE(); | |
| 244 return -1; | |
| 245 } | |
| 246 | |
| 247 | |
| 248 // Format takes a formatting string for a whole instruction and prints it into | |
| 249 // the output buffer. All escaped options are handed to FormatOption to be | |
| 250 // parsed further. | |
| 251 void ARM64Decoder::Format(Instr* instr, const char* format) { | |
| 252 char cur = *format++; | |
| 253 while ((cur != 0) && (buffer_pos_ < (buffer_size_ - 1))) { | |
| 254 if (cur == '\'') { // Single quote is used as the formatting escape. | |
| 255 format += FormatOption(instr, format); | |
| 256 } else { | |
| 257 buffer_[buffer_pos_++] = cur; | |
| 258 } | |
| 259 cur = *format++; | |
| 260 } | |
| 261 buffer_[buffer_pos_] = '\0'; | |
| 262 } | |
| 263 | |
| 264 | |
| 265 // For currently unimplemented decodings the disassembler calls Unknown(instr) | |
| 266 // which will just print "unknown" of the instruction bits. | |
| 267 void ARM64Decoder::Unknown(Instr* instr) { | |
| 268 Format(instr, "unknown"); | |
| 269 } | |
| 270 | |
| 271 | |
| 272 void ARM64Decoder::DecodeMoveWide(Instr* instr) { | |
| 273 switch (instr->Bits(29, 2)) { | |
| 274 case 0: | |
| 275 Format(instr, "movn'sf 'rd, 'imm16"); | |
| 276 break; | |
| 277 case 2: | |
| 278 Format(instr, "movz'sf 'rd, 'imm16"); | |
| 279 break; | |
| 280 case 3: | |
| 281 Format(instr, "movk'sf 'rd, 'imm16"); | |
| 282 break; | |
| 283 default: | |
| 284 Unknown(instr); | |
| 285 break; | |
| 286 } | |
| 287 } | |
| 288 | |
| 289 | |
| 290 void ARM64Decoder::DecodeAddSubImm(Instr* instr) { | |
| 291 switch (instr->Bit(30)) { | |
| 292 case 0: | |
| 293 Format(instr, "addi'sf's 'rd, 'rn, 'imm12s"); | |
| 294 break; | |
| 295 case 1: | |
| 296 Format(instr, "subi'sf's 'rd, 'rn, 'imm12s"); | |
| 297 break; | |
| 298 default: | |
| 299 Unknown(instr); | |
| 300 break; | |
| 301 } | |
| 302 } | |
| 303 | |
| 304 void ARM64Decoder::DecodeDPImmediate(Instr* instr) { | |
| 305 if (instr->IsMoveWideOp()) { | |
| 306 DecodeMoveWide(instr); | |
| 307 } else if (instr->IsAddSubImmOp()) { | |
| 308 DecodeAddSubImm(instr); | |
| 309 } else { | |
| 310 Unknown(instr); | |
| 311 } | |
| 312 } | |
| 313 | |
| 314 | |
| 315 void ARM64Decoder::DecodeExceptionGen(Instr* instr) { | |
| 316 if ((instr->Bits(0, 2) == 1) && (instr->Bits(2, 3) == 0) && | |
| 317 (instr->Bits(21, 3) == 0)) { | |
| 318 Format(instr, "svc 'imm16"); | |
| 319 } else if ((instr->Bits(0, 2) == 0) && (instr->Bits(2, 3) == 0) && | |
| 320 (instr->Bits(21, 3) == 1)) { | |
| 321 Format(instr, "brk 'imm16"); | |
| 322 } else if ((instr->Bits(0, 2) == 0) && (instr->Bits(2, 3) == 0) && | |
| 323 (instr->Bits(21, 3) == 2)) { | |
| 324 Format(instr, "hlt 'imm16"); | |
| 325 } | |
| 326 } | |
| 327 | |
| 328 | |
| 329 void ARM64Decoder::DecodeSystem(Instr* instr) { | |
| 330 if ((instr->Bits(0, 8) == 0x5f) && (instr->Bits(12, 4) == 2) && | |
| 331 (instr->Bits(16, 3) == 3) && (instr->Bits(19, 2) == 0) && | |
| 332 (instr->Bit(21) == 0)) { | |
| 333 if (instr->Bits(8, 4) == 0) { | |
| 334 Format(instr, "nop"); | |
| 335 } else { | |
| 336 Unknown(instr); | |
| 337 } | |
| 338 } else { | |
| 339 Unknown(instr); | |
| 340 } | |
| 341 } | |
| 342 | |
| 343 | |
| 344 void ARM64Decoder::DecodeUnconditionalBranchReg(Instr* instr) { | |
| 345 if ((instr->Bits(0, 5) == 0) && (instr->Bits(10, 5) == 0) && | |
| 346 (instr->Bits(16, 5) == 0x1f)) { | |
| 347 switch (instr->Bits(21, 4)) { | |
| 348 case 0: | |
| 349 Format(instr, "br 'rn"); | |
| 350 break; | |
| 351 case 1: | |
| 352 Format(instr, "blr 'rn"); | |
| 353 break; | |
| 354 case 2: | |
| 355 Format(instr, "ret 'rn"); | |
| 356 break; | |
| 357 default: | |
| 358 Unknown(instr); | |
| 359 break; | |
| 360 } | |
| 361 } | |
| 362 } | |
| 363 | |
| 364 | |
| 365 void ARM64Decoder::DecodeCompareBranch(Instr* instr) { | |
| 366 if (instr->IsExceptionGenOp()) { | |
| 367 DecodeExceptionGen(instr); | |
| 368 } else if (instr->IsSystemOp()) { | |
| 369 DecodeSystem(instr); | |
| 370 } else if (instr->IsUnconditionalBranchRegOp()) { | |
| 371 DecodeUnconditionalBranchReg(instr); | |
| 372 } else { | |
| 373 Unknown(instr); | |
| 374 } | |
| 375 } | |
| 376 | |
| 377 | |
| 378 void ARM64Decoder::DecodeLoadStore(Instr* instr) { | |
| 379 Unknown(instr); | |
| 380 } | |
| 381 | |
| 382 | |
| 383 void ARM64Decoder::DecodeAddSubShiftExt(Instr* instr) { | |
| 384 switch (instr->Bit(30)) { | |
| 385 case 0: | |
| 386 Format(instr, "add'sf's 'rd, 'rn, 'shift_op"); | |
| 387 break; | |
| 388 case 1: | |
| 389 Format(instr, "sub'sf's 'rd, 'rn, 'shift_op"); | |
| 390 break; | |
| 391 default: | |
| 392 UNREACHABLE(); | |
| 393 break; | |
| 394 } | |
| 395 } | |
| 396 | |
| 397 | |
| 398 void ARM64Decoder::DecodeDPRegister(Instr* instr) { | |
| 399 if (instr->IsAddSubShiftExtOp()) { | |
| 400 DecodeAddSubShiftExt(instr); | |
| 401 } else { | |
| 402 Unknown(instr); | |
| 403 } | |
| 404 } | |
| 405 | |
| 406 | |
| 407 void ARM64Decoder::DecodeDPSimd1(Instr* instr) { | |
| 408 Unknown(instr); | |
| 409 } | |
| 410 | |
| 411 | |
| 412 void ARM64Decoder::DecodeDPSimd2(Instr* instr) { | |
| 413 Unknown(instr); | |
| 414 } | |
| 415 | |
| 416 | |
| 417 void ARM64Decoder::InstructionDecode(uword pc) { | |
| 418 Instr* instr = Instr::At(pc); | |
| 419 | |
| 420 if (instr->IsDPImmediateOp()) { | |
| 421 DecodeDPImmediate(instr); | |
| 422 } else if (instr->IsCompareBranchOp()) { | |
| 423 DecodeCompareBranch(instr); | |
| 424 } else if (instr->IsLoadStoreOp()) { | |
| 425 DecodeLoadStore(instr); | |
| 426 } else if (instr->IsDPRegisterOp()) { | |
| 427 DecodeDPRegister(instr); | |
| 428 } else if (instr->IsDPSimd1Op()) { | |
| 429 DecodeDPSimd1(instr); | |
| 430 } else { | |
| 431 ASSERT(instr->IsDPSimd2Op()); | |
| 432 DecodeDPSimd2(instr); | |
| 433 } | |
| 434 } | |
| 435 | |
| 436 | |
| 437 void Disassembler::DecodeInstruction(char* hex_buffer, intptr_t hex_size, | |
| 438 char* human_buffer, intptr_t human_size, | |
| 439 int* out_instr_size, uword pc) { | |
| 440 ARM64Decoder decoder(human_buffer, human_size); | |
| 441 decoder.InstructionDecode(pc); | |
| 442 int32_t instruction_bits = Instr::At(pc)->InstructionBits(); | |
| 443 OS::SNPrint(hex_buffer, hex_size, "%08x", instruction_bits); | |
| 444 if (out_instr_size) { | |
| 445 *out_instr_size = Instr::kInstrSize; | |
| 446 } | |
| 447 } | |
| 448 | |
| 449 | |
| 450 void Disassembler::Disassemble(uword start, | |
| 451 uword end, | |
| 452 DisassemblyFormatter* formatter, | |
| 453 const Code::Comments& comments) { | |
| 454 ASSERT(formatter != NULL); | |
| 455 char hex_buffer[kHexadecimalBufferSize]; // Instruction in hexadecimal form. | |
| 456 char human_buffer[kUserReadableBufferSize]; // Human-readable instruction. | |
| 457 uword pc = start; | |
| 458 intptr_t comment_finger = 0; | |
| 459 while (pc < end) { | |
| 460 const intptr_t offset = pc - start; | |
| 461 while (comment_finger < comments.Length() && | |
| 462 comments.PCOffsetAt(comment_finger) <= offset) { | |
| 463 formatter->Print( | |
| 464 " ;; %s\n", | |
| 465 String::Handle(comments.CommentAt(comment_finger)).ToCString()); | |
| 466 comment_finger++; | |
| 467 } | |
| 468 int instruction_length; | |
| 469 DecodeInstruction(hex_buffer, sizeof(hex_buffer), | |
| 470 human_buffer, sizeof(human_buffer), | |
| 471 &instruction_length, pc); | |
| 472 | |
| 473 formatter->ConsumeInstruction(hex_buffer, | |
| 474 sizeof(hex_buffer), | |
| 475 human_buffer, | |
| 476 sizeof(human_buffer), | |
| 477 pc); | |
| 478 pc += instruction_length; | |
| 479 } | |
| 480 } | |
| 481 | |
| 482 } // namespace dart | |
| 483 | |
| 484 #endif // defined TARGET_ARCH_ARM | |
| OLD | NEW |