OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2016, 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_DBC. |
| 8 #if defined(TARGET_ARCH_DBC) |
| 9 |
| 10 #include "platform/assert.h" |
| 11 #include "vm/cpu.h" |
| 12 |
| 13 namespace dart { |
| 14 |
| 15 static const char* kOpcodeNames[] = { |
| 16 #define BYTECODE_NAME(name, encoding, op1, op2, op3) #name, |
| 17 BYTECODES_LIST(BYTECODE_NAME) |
| 18 #undef BYTECODE_NAME |
| 19 }; |
| 20 |
| 21 static const size_t kOpcodeCount = |
| 22 sizeof(kOpcodeNames) / sizeof(kOpcodeNames[0]); |
| 23 |
| 24 typedef void (*BytecodeFormatter)(char* buffer, intptr_t size, uword pc, |
| 25 uint32_t bc); |
| 26 typedef void (*Fmt)(char** buf, intptr_t* size, uword pc, int32_t value); |
| 27 |
| 28 |
| 29 template<typename ValueType> |
| 30 void FormatOperand(char** buf, |
| 31 intptr_t* size, |
| 32 const char* fmt, |
| 33 ValueType value) { |
| 34 intptr_t written = OS::SNPrint(*buf, *size, fmt, value); |
| 35 if (written < *size) { |
| 36 *buf += written; |
| 37 *size += written; |
| 38 } else { |
| 39 *size = -1; |
| 40 } |
| 41 } |
| 42 |
| 43 |
| 44 |
| 45 static void Fmt___(char** buf, intptr_t* size, uword pc, int32_t value) {} |
| 46 |
| 47 |
| 48 static void Fmttgt(char** buf, intptr_t* size, uword pc, int32_t value) { |
| 49 FormatOperand(buf, size, "-> %" Px, pc + (value << 2)); |
| 50 } |
| 51 |
| 52 |
| 53 static void Fmtlit(char** buf, intptr_t* size, uword pc, int32_t value) { |
| 54 FormatOperand(buf, size, "k%d", value); |
| 55 } |
| 56 |
| 57 |
| 58 static void Fmtreg(char** buf, intptr_t* size, uword pc, int32_t value) { |
| 59 FormatOperand(buf, size, "r%d", value); |
| 60 } |
| 61 |
| 62 |
| 63 static void Fmtxeg(char** buf, intptr_t* size, uword pc, int32_t value) { |
| 64 FormatOperand(buf, size, "R(%d)", value); |
| 65 } |
| 66 |
| 67 |
| 68 static void Fmtnum(char** buf, intptr_t* size, uword pc, int32_t value) { |
| 69 FormatOperand(buf, size, "#%d", value); |
| 70 } |
| 71 |
| 72 |
| 73 static void Apply(char** buf, intptr_t* size, uword pc, Fmt fmt, int32_t value, |
| 74 const char* suffix) { |
| 75 if (*size <= 0) { |
| 76 return; |
| 77 } |
| 78 |
| 79 fmt(buf, size, pc, value); |
| 80 if (*size > 0) { |
| 81 FormatOperand(buf, size, "%s", suffix); |
| 82 } |
| 83 } |
| 84 |
| 85 |
| 86 static void Format0(char* buf, intptr_t size, uword pc, uint32_t op, Fmt op1, |
| 87 Fmt op2, Fmt op3) {} |
| 88 |
| 89 |
| 90 static void FormatT(char* buf, intptr_t size, uword pc, uint32_t op, Fmt op1, |
| 91 Fmt op2, Fmt op3) { |
| 92 const int32_t x = static_cast<int32_t>(op) >> 8; |
| 93 Apply(&buf, &size, pc, op1, x, ""); |
| 94 } |
| 95 |
| 96 |
| 97 static void FormatA(char* buf, intptr_t size, uword pc, uint32_t op, Fmt op1, |
| 98 Fmt op2, Fmt op3) { |
| 99 const int32_t a = (op & 0xFF00) >> 8; |
| 100 Apply(&buf, &size, pc, op1, a, ""); |
| 101 } |
| 102 |
| 103 |
| 104 static void FormatA_D(char* buf, intptr_t size, uword pc, uint32_t op, Fmt op1, |
| 105 Fmt op2, Fmt op3) { |
| 106 const int32_t a = (op & 0xFF00) >> 8; |
| 107 const int32_t bc = op >> 16; |
| 108 Apply(&buf, &size, pc, op1, a, ", "); |
| 109 Apply(&buf, &size, pc, op2, bc, ""); |
| 110 } |
| 111 |
| 112 |
| 113 static void FormatA_X(char* buf, intptr_t size, uword pc, uint32_t op, Fmt op1, |
| 114 Fmt op2, Fmt op3) { |
| 115 const int32_t a = (op & 0xFF00) >> 8; |
| 116 const int32_t bc = static_cast<int32_t>(op) >> 16; |
| 117 Apply(&buf, &size, pc, op1, a, ", "); |
| 118 Apply(&buf, &size, pc, op2, bc, ""); |
| 119 } |
| 120 |
| 121 |
| 122 static void FormatX(char* buf, intptr_t size, uword pc, uint32_t op, Fmt op1, |
| 123 Fmt op2, Fmt op3) { |
| 124 const int32_t bc = static_cast<int32_t>(op) >> 16; |
| 125 Apply(&buf, &size, pc, op1, bc, ""); |
| 126 } |
| 127 |
| 128 |
| 129 static void FormatD(char* buf, intptr_t size, uword pc, uint32_t op, Fmt op1, |
| 130 Fmt op2, Fmt op3) { |
| 131 const int32_t bc = op >> 16; |
| 132 Apply(&buf, &size, pc, op1, bc, ""); |
| 133 } |
| 134 |
| 135 |
| 136 static void FormatA_B_C(char* buf, intptr_t size, uword pc, uint32_t op, |
| 137 Fmt op1, Fmt op2, Fmt op3) { |
| 138 const int32_t a = (op >> 8) & 0xFF; |
| 139 const int32_t b = (op >> 16) & 0xFF; |
| 140 const int32_t c = (op >> 24) & 0xFF; |
| 141 Apply(&buf, &size, pc, op1, a, ", "); |
| 142 Apply(&buf, &size, pc, op2, b, ", "); |
| 143 Apply(&buf, &size, pc, op3, c, ""); |
| 144 } |
| 145 |
| 146 |
| 147 #define BYTECODE_FORMATTER(name, encoding, op1, op2, op3) \ |
| 148 static void Format##name(char* buf, intptr_t size, uword pc, uint32_t op) { \ |
| 149 Format##encoding(buf, size, pc, op, Fmt##op1, Fmt##op2, Fmt##op3); \ |
| 150 } |
| 151 BYTECODES_LIST(BYTECODE_FORMATTER) |
| 152 #undef BYTECODE_FORMATTER |
| 153 |
| 154 |
| 155 static BytecodeFormatter kFormatters[] = { |
| 156 #define BYTECODE_FORMATTER(name, encoding, op1, op2, op3) &Format##name, |
| 157 BYTECODES_LIST(BYTECODE_FORMATTER) |
| 158 #undef BYTECODE_FORMATTER |
| 159 }; |
| 160 |
| 161 |
| 162 void Disassembler::DecodeInstruction(char* hex_buffer, intptr_t hex_size, |
| 163 char* human_buffer, intptr_t human_size, |
| 164 int* out_instr_size, uword pc) { |
| 165 const uint32_t instr = *reinterpret_cast<uint32_t*>(pc); |
| 166 const uint8_t opcode = instr & 0xFF; |
| 167 ASSERT(opcode < kOpcodeCount); |
| 168 size_t name_size = |
| 169 OS::SNPrint(human_buffer, human_size, "%-10s\t", kOpcodeNames[opcode]); |
| 170 |
| 171 human_buffer += name_size; |
| 172 human_size -= name_size; |
| 173 kFormatters[opcode](human_buffer, human_size, pc, instr); |
| 174 |
| 175 OS::SNPrint(hex_buffer, hex_size, "%08x", instr); |
| 176 if (out_instr_size) { |
| 177 *out_instr_size = sizeof(uint32_t); |
| 178 } |
| 179 } |
| 180 |
| 181 |
| 182 } // namespace dart |
| 183 |
| 184 #endif // defined TARGET_ARCH_DBC |
OLD | NEW |