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