blob: 4aff822997b2ef746c608e084138eabc3587e069 [file] [log] [blame]
Ian Rogers706a10e2012-03-23 17:00:55 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "disassembler_x86.h"
18
Ian Rogers706a10e2012-03-23 17:00:55 -070019#include <iostream>
20
Elliott Hughes0f3c5532012-03-30 14:51:51 -070021#include "logging.h"
22#include "stringprintf.h"
Elliott Hughes92301d92012-04-10 15:57:52 -070023#include "thread.h"
Elliott Hughes0f3c5532012-03-30 14:51:51 -070024
Ian Rogers706a10e2012-03-23 17:00:55 -070025namespace art {
26namespace x86 {
27
28DisassemblerX86::DisassemblerX86() {
29}
30
31void DisassemblerX86::Dump(std::ostream& os, const uint8_t* begin, const uint8_t* end) {
32 size_t length = 0;
33 for (const uint8_t* cur = begin; cur < end; cur += length) {
34 length = DumpInstruction(os, cur);
35 }
36}
37
38static const char* gReg8Names[] = { "al", "cl", "dl", "bl", "ah", "ch", "dh", "bh" };
39static const char* gReg16Names[] = { "ax", "cx", "dx", "bx", "sp", "bp", "di", "si" };
40static const char* gReg32Names[] = { "eax", "ecx", "edx", "ebx", "esp", "ebp", "edi", "esi" };
41
42static void DumpReg0(std::ostream& os, uint8_t /*rex*/, size_t reg,
43 bool byte_operand, uint8_t size_override) {
44 DCHECK_LT(reg, 8u);
45 // TODO: combine rex into size
46 size_t size = byte_operand ? 1 : (size_override == 0x66 ? 2 : 4);
47 switch (size) {
48 case 1: os << gReg8Names[reg]; break;
49 case 2: os << gReg16Names[reg]; break;
50 case 4: os << gReg32Names[reg]; break;
51 default: LOG(FATAL) << "unexpected size " << size;
52 }
53}
54
Ian Rogersbf989802012-04-16 16:07:49 -070055enum RegFile { GPR, MMX, SSE };
56
Ian Rogers706a10e2012-03-23 17:00:55 -070057static void DumpReg(std::ostream& os, uint8_t rex, uint8_t reg,
Ian Rogersbf989802012-04-16 16:07:49 -070058 bool byte_operand, uint8_t size_override, RegFile reg_file) {
Ian Rogers706a10e2012-03-23 17:00:55 -070059 size_t reg_num = reg; // TODO: combine with REX.R on 64bit
Ian Rogersbf989802012-04-16 16:07:49 -070060 if (reg_file == GPR) {
61 DumpReg0(os, rex, reg_num, byte_operand, size_override);
62 } else if (reg_file == SSE) {
63 os << "xmm" << reg_num;
64 } else {
65 os << "mm" << reg_num;
66 }
Ian Rogers706a10e2012-03-23 17:00:55 -070067}
68
Ian Rogers7caad772012-03-30 01:07:54 -070069static void DumpBaseReg(std::ostream& os, uint8_t rex, uint8_t reg) {
Ian Rogers706a10e2012-03-23 17:00:55 -070070 size_t reg_num = reg; // TODO: combine with REX.B on 64bit
Ian Rogers7caad772012-03-30 01:07:54 -070071 DumpReg0(os, rex, reg_num, false, 0);
Ian Rogers706a10e2012-03-23 17:00:55 -070072}
73
Ian Rogers7caad772012-03-30 01:07:54 -070074static void DumpIndexReg(std::ostream& os, uint8_t rex, uint8_t reg) {
Ian Rogers706a10e2012-03-23 17:00:55 -070075 int reg_num = reg; // TODO: combine with REX.X on 64bit
Ian Rogers7caad772012-03-30 01:07:54 -070076 DumpReg0(os, rex, reg_num, false, 0);
Ian Rogers706a10e2012-03-23 17:00:55 -070077}
78
Elliott Hughes92301d92012-04-10 15:57:52 -070079enum SegmentPrefix {
80 kCs = 0x2e,
81 kSs = 0x36,
82 kDs = 0x3e,
83 kEs = 0x26,
84 kFs = 0x64,
85 kGs = 0x65,
86};
87
Ian Rogers706a10e2012-03-23 17:00:55 -070088static void DumpSegmentOverride(std::ostream& os, uint8_t segment_prefix) {
89 switch (segment_prefix) {
Elliott Hughes92301d92012-04-10 15:57:52 -070090 case kCs: os << "cs:"; break;
91 case kSs: os << "ss:"; break;
92 case kDs: os << "ds:"; break;
93 case kEs: os << "es:"; break;
94 case kFs: os << "fs:"; break;
95 case kGs: os << "gs:"; break;
Ian Rogers706a10e2012-03-23 17:00:55 -070096 default: break;
97 }
98}
99
100size_t DisassemblerX86::DumpInstruction(std::ostream& os, const uint8_t* instr) {
101 const uint8_t* begin_instr = instr;
102 bool have_prefixes = true;
103 uint8_t prefix[4] = {0, 0, 0, 0};
104 const char** modrm_opcodes = NULL;
105 do {
106 switch (*instr) {
Ian Rogers7caad772012-03-30 01:07:54 -0700107 // Group 1 - lock and repeat prefixes:
Ian Rogers706a10e2012-03-23 17:00:55 -0700108 case 0xF0:
109 case 0xF2:
110 case 0xF3:
111 prefix[0] = *instr;
112 break;
113 // Group 2 - segment override prefixes:
Elliott Hughes92301d92012-04-10 15:57:52 -0700114 case kCs:
115 case kSs:
116 case kDs:
117 case kEs:
118 case kFs:
119 case kGs:
Ian Rogers706a10e2012-03-23 17:00:55 -0700120 prefix[1] = *instr;
121 break;
122 // Group 3 - operand size override:
123 case 0x66:
124 prefix[2] = *instr;
125 break;
126 // Group 4 - address size override:
127 case 0x67:
128 prefix[3] = *instr;
129 break;
130 default:
131 have_prefixes = false;
132 break;
133 }
134 if (have_prefixes) {
135 instr++;
136 }
137 } while (have_prefixes);
138 uint8_t rex = (*instr >= 0x40 && *instr <= 0x4F) ? *instr : 0;
139 bool has_modrm = false;
140 bool reg_is_opcode = false;
141 size_t immediate_bytes = 0;
142 size_t branch_bytes = 0;
143 std::ostringstream opcode;
144 bool store = false; // stores to memory (ie rm is on the left)
145 bool load = false; // loads from memory (ie rm is on the right)
146 bool byte_operand = false;
147 bool ax = false; // implicit use of ax
jeffhaoe2962482012-06-28 11:29:57 -0700148 bool cx = false; // implicit use of cx
Ian Rogers706a10e2012-03-23 17:00:55 -0700149 bool reg_in_opcode = false; // low 3-bits of opcode encode register parameter
Ian Rogersbf989802012-04-16 16:07:49 -0700150 RegFile src_reg_file = GPR;
151 RegFile dst_reg_file = GPR;
Ian Rogers706a10e2012-03-23 17:00:55 -0700152 switch (*instr) {
153#define DISASSEMBLER_ENTRY(opname, \
154 rm8_r8, rm32_r32, \
155 r8_rm8, r32_rm32, \
156 ax8_i8, ax32_i32) \
157 case rm8_r8: opcode << #opname; store = true; has_modrm = true; byte_operand = true; break; \
158 case rm32_r32: opcode << #opname; store = true; has_modrm = true; break; \
159 case r8_rm8: opcode << #opname; load = true; has_modrm = true; byte_operand = true; break; \
160 case r32_rm32: opcode << #opname; load = true; has_modrm = true; break; \
161 case ax8_i8: opcode << #opname; ax = true; immediate_bytes = 1; byte_operand = true; break; \
162 case ax32_i32: opcode << #opname; ax = true; immediate_bytes = 4; break;
163
164DISASSEMBLER_ENTRY(add,
165 0x00 /* RegMem8/Reg8 */, 0x01 /* RegMem32/Reg32 */,
166 0x02 /* Reg8/RegMem8 */, 0x03 /* Reg32/RegMem32 */,
167 0x04 /* Rax8/imm8 opcode */, 0x05 /* Rax32/imm32 */)
168DISASSEMBLER_ENTRY(or,
169 0x08 /* RegMem8/Reg8 */, 0x09 /* RegMem32/Reg32 */,
170 0x0A /* Reg8/RegMem8 */, 0x0B /* Reg32/RegMem32 */,
171 0x0C /* Rax8/imm8 opcode */, 0x0D /* Rax32/imm32 */)
172DISASSEMBLER_ENTRY(adc,
173 0x10 /* RegMem8/Reg8 */, 0x11 /* RegMem32/Reg32 */,
174 0x12 /* Reg8/RegMem8 */, 0x13 /* Reg32/RegMem32 */,
175 0x14 /* Rax8/imm8 opcode */, 0x15 /* Rax32/imm32 */)
176DISASSEMBLER_ENTRY(sbb,
177 0x18 /* RegMem8/Reg8 */, 0x19 /* RegMem32/Reg32 */,
178 0x1A /* Reg8/RegMem8 */, 0x1B /* Reg32/RegMem32 */,
179 0x1C /* Rax8/imm8 opcode */, 0x1D /* Rax32/imm32 */)
180DISASSEMBLER_ENTRY(and,
181 0x20 /* RegMem8/Reg8 */, 0x21 /* RegMem32/Reg32 */,
182 0x22 /* Reg8/RegMem8 */, 0x23 /* Reg32/RegMem32 */,
183 0x24 /* Rax8/imm8 opcode */, 0x25 /* Rax32/imm32 */)
184DISASSEMBLER_ENTRY(sub,
185 0x28 /* RegMem8/Reg8 */, 0x29 /* RegMem32/Reg32 */,
186 0x2A /* Reg8/RegMem8 */, 0x2B /* Reg32/RegMem32 */,
187 0x2C /* Rax8/imm8 opcode */, 0x2D /* Rax32/imm32 */)
188DISASSEMBLER_ENTRY(xor,
189 0x30 /* RegMem8/Reg8 */, 0x31 /* RegMem32/Reg32 */,
190 0x32 /* Reg8/RegMem8 */, 0x33 /* Reg32/RegMem32 */,
191 0x34 /* Rax8/imm8 opcode */, 0x35 /* Rax32/imm32 */)
192DISASSEMBLER_ENTRY(cmp,
193 0x38 /* RegMem8/Reg8 */, 0x39 /* RegMem32/Reg32 */,
194 0x3A /* Reg8/RegMem8 */, 0x3B /* Reg32/RegMem32 */,
195 0x3C /* Rax8/imm8 opcode */, 0x3D /* Rax32/imm32 */)
196
197#undef DISASSEMBLER_ENTRY
198 case 0x50: case 0x51: case 0x52: case 0x53: case 0x54: case 0x55: case 0x56: case 0x57:
199 opcode << "push";
200 reg_in_opcode = true;
201 break;
202 case 0x58: case 0x59: case 0x5A: case 0x5B: case 0x5C: case 0x5D: case 0x5E: case 0x5F:
203 opcode << "pop";
204 reg_in_opcode = true;
205 break;
206 case 0x68: opcode << "push"; immediate_bytes = 4; break;
207 case 0x6A: opcode << "push"; immediate_bytes = 1; break;
208 case 0x70: case 0x71: case 0x72: case 0x73: case 0x74: case 0x75: case 0x76: case 0x77:
209 case 0x78: case 0x79: case 0x7A: case 0x7B: case 0x7C: case 0x7D: case 0x7E: case 0x7F:
210 static const char* condition_codes[] =
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700211 {"o", "no", "b/nae/c", "nb/ae/nc", "z/eq", "nz/ne", "be/na", "nbe/a",
212 "s", "ns", "p/pe", "np/po", "l/nge", "nl/ge", "le/ng", "nle/g"
Ian Rogers706a10e2012-03-23 17:00:55 -0700213 };
214 opcode << "j" << condition_codes[*instr & 0xF];
215 branch_bytes = 1;
216 break;
217 case 0x88: opcode << "mov"; store = true; has_modrm = true; byte_operand = true; break;
218 case 0x89: opcode << "mov"; store = true; has_modrm = true; break;
219 case 0x8A: opcode << "mov"; load = true; has_modrm = true; byte_operand = true; break;
220 case 0x8B: opcode << "mov"; load = true; has_modrm = true; break;
221
222 case 0x0F: // 2 byte extended opcode
223 instr++;
224 switch (*instr) {
Ian Rogers7caad772012-03-30 01:07:54 -0700225 case 0x10: case 0x11:
226 if (prefix[0] == 0xF2) {
227 opcode << "movsd";
228 } else if (prefix[0] == 0xF3) {
229 opcode << "movss";
230 } else if (prefix[2] == 0x66) {
231 opcode << "movupd";
232 } else {
233 opcode << "movups";
234 }
235 has_modrm = true;
Ian Rogersbf989802012-04-16 16:07:49 -0700236 src_reg_file = dst_reg_file = SSE;
Ian Rogers7caad772012-03-30 01:07:54 -0700237 load = *instr == 0x10;
238 store = !load;
239 break;
Ian Rogers706a10e2012-03-23 17:00:55 -0700240 case 0x38: // 3 byte extended opcode
241 opcode << StringPrintf("unknown opcode '0F 38 %02X'", *instr);
242 break;
243 case 0x3A: // 3 byte extended opcode
244 opcode << StringPrintf("unknown opcode '0F 3A %02X'", *instr);
245 break;
Ian Rogersbf989802012-04-16 16:07:49 -0700246 case 0x50: case 0x51: case 0x52: case 0x53: case 0x54: case 0x55: case 0x56: case 0x57:
247 case 0x58: case 0x59: case 0x5C: case 0x5D: case 0x5E: case 0x5F: {
248 switch (*instr) {
249 case 0x50: opcode << "movmsk"; break;
250 case 0x51: opcode << "sqrt"; break;
251 case 0x52: opcode << "rsqrt"; break;
252 case 0x53: opcode << "rcp"; break;
253 case 0x54: opcode << "and"; break;
254 case 0x55: opcode << "andn"; break;
255 case 0x56: opcode << "or"; break;
256 case 0x57: opcode << "xor"; break;
257 case 0x58: opcode << "add"; break;
258 case 0x59: opcode << "mul"; break;
259 case 0x5C: opcode << "sub"; break;
260 case 0x5D: opcode << "min"; break;
261 case 0x5E: opcode << "div"; break;
262 case 0x5F: opcode << "max"; break;
263 default: LOG(FATAL) << "Unreachable";
264 }
265 if (prefix[2] == 0x66) {
266 opcode << "pd";
267 prefix[2] = 0; // clear prefix now its served its purpose as part of the opcode
268 } else if (prefix[0] == 0xF2) {
269 opcode << "sd";
270 prefix[0] = 0; // clear prefix now its served its purpose as part of the opcode
271 } else if (prefix[0] == 0xF3) {
272 opcode << "ss";
273 prefix[0] = 0; // clear prefix now its served its purpose as part of the opcode
274 } else {
275 opcode << "ps";
276 }
277 load = true;
278 has_modrm = true;
279 src_reg_file = dst_reg_file = SSE;
280 break;
281 }
282 case 0x5A:
283 if (prefix[2] == 0x66) {
284 opcode << "cvtpd2ps";
285 prefix[2] = 0; // clear prefix now its served its purpose as part of the opcode
286 } else if (prefix[0] == 0xF2) {
287 opcode << "cvtsd2ss";
288 prefix[0] = 0; // clear prefix now its served its purpose as part of the opcode
289 } else if (prefix[0] == 0xF3) {
290 opcode << "cvtss2sd";
291 prefix[0] = 0; // clear prefix now its served its purpose as part of the opcode
292 } else {
293 opcode << "cvtps2pd";
294 }
295 load = true;
296 has_modrm = true;
297 src_reg_file = dst_reg_file = SSE;
298 break;
299 case 0x5B:
300 if (prefix[2] == 0x66) {
301 opcode << "cvtps2dq";
302 prefix[2] = 0; // clear prefix now its served its purpose as part of the opcode
303 } else if (prefix[0] == 0xF2) {
304 opcode << "bad opcode F2 0F 5B";
305 } else if (prefix[0] == 0xF3) {
306 opcode << "cvttps2dq";
307 prefix[0] = 0; // clear prefix now its served its purpose as part of the opcode
308 } else {
309 opcode << "cvtdq2ps";
310 }
311 load = true;
312 has_modrm = true;
313 src_reg_file = dst_reg_file = SSE;
314 break;
315 case 0x6E:
316 if (prefix[2] == 0x66) {
317 dst_reg_file = SSE;
318 opcode << "movq";
319 prefix[2] = 0; // clear prefix now its served its purpose as part of the opcode
320 } else {
321 dst_reg_file = MMX;
322 opcode << "movd";
323 }
324 load = true;
325 has_modrm = true;
326 break;
327 case 0x6F:
328 if (prefix[2] == 0x66) {
329 dst_reg_file = SSE;
330 opcode << "movdqa";
331 } else if (prefix[0] == 0xF3) {
332 dst_reg_file = SSE;
333 opcode << "movdqu";
334 prefix[0] = 0; // clear prefix now its served its purpose as part of the opcode
335 } else {
336 dst_reg_file = MMX;
337 opcode << "movq";
338 }
339 load = true;
340 has_modrm = true;
341 break;
Ian Rogers706a10e2012-03-23 17:00:55 -0700342 case 0x80: case 0x81: case 0x82: case 0x83: case 0x84: case 0x85: case 0x86: case 0x87:
343 case 0x88: case 0x89: case 0x8A: case 0x8B: case 0x8C: case 0x8D: case 0x8E: case 0x8F:
344 opcode << "j" << condition_codes[*instr & 0xF];
345 branch_bytes = 4;
346 break;
Ian Rogers7caad772012-03-30 01:07:54 -0700347 case 0x90: case 0x91: case 0x92: case 0x93: case 0x94: case 0x95: case 0x96: case 0x97:
348 case 0x98: case 0x99: case 0x9A: case 0x9B: case 0x9C: case 0x9D: case 0x9E: case 0x9F:
349 opcode << "set" << condition_codes[*instr & 0xF];
350 modrm_opcodes = NULL;
351 reg_is_opcode = true;
352 has_modrm = true;
353 store = true;
354 break;
355 case 0xB6: opcode << "movzxb"; has_modrm = true; load = true; break;
356 case 0xB7: opcode << "movzxw"; has_modrm = true; load = true; break;
Ian Rogers706a10e2012-03-23 17:00:55 -0700357 default:
358 opcode << StringPrintf("unknown opcode '0F %02X'", *instr);
359 break;
360 }
361 break;
362 case 0x80: case 0x81: case 0x82: case 0x83:
363 static const char* x80_opcodes[] = {"add", "or", "adc", "sbb", "and", "sub", "xor", "cmp"};
364 modrm_opcodes = x80_opcodes;
365 has_modrm = true;
366 reg_is_opcode = true;
367 store = true;
368 byte_operand = (*instr & 1) == 0;
369 immediate_bytes = *instr == 0x81 ? 4 : 1;
370 break;
Ian Rogers7caad772012-03-30 01:07:54 -0700371 case 0x8D:
372 opcode << "lea";
373 has_modrm = true;
374 load = true;
375 break;
Ian Rogers706a10e2012-03-23 17:00:55 -0700376 case 0xB0: case 0xB1: case 0xB2: case 0xB3: case 0xB4: case 0xB5: case 0xB6: case 0xB7:
377 opcode << "mov";
378 immediate_bytes = 1;
379 reg_in_opcode = true;
380 break;
381 case 0xB8: case 0xB9: case 0xBA: case 0xBB: case 0xBC: case 0xBD: case 0xBE: case 0xBF:
382 opcode << "mov";
383 immediate_bytes = 4;
384 reg_in_opcode = true;
385 break;
Ian Rogers7caad772012-03-30 01:07:54 -0700386 case 0xC0: case 0xC1:
jeffhaoe2962482012-06-28 11:29:57 -0700387 case 0xD0: case 0xD1: case 0xD2: case 0xD3:
Ian Rogers7caad772012-03-30 01:07:54 -0700388 static const char* shift_opcodes[] =
389 {"rol", "ror", "rcl", "rcr", "shl", "shr", "unknown-shift", "sar"};
390 modrm_opcodes = shift_opcodes;
391 has_modrm = true;
392 reg_is_opcode = true;
393 store = true;
Elliott Hughes16b5c292012-04-16 20:37:16 -0700394 immediate_bytes = ((*instr & 0xf0) == 0xc0) ? 1 : 0;
jeffhaoe2962482012-06-28 11:29:57 -0700395 cx = (*instr == 0xD2) || (*instr == 0xD3);
396 byte_operand = (*instr == 0xC0);
Ian Rogers7caad772012-03-30 01:07:54 -0700397 break;
Ian Rogers706a10e2012-03-23 17:00:55 -0700398 case 0xC3: opcode << "ret"; break;
Elliott Hughes0589ca92012-04-09 18:26:20 -0700399 case 0xC7:
400 static const char* c7_opcodes[] = {"mov", "unknown-c7", "unknown-c7", "unknown-c7", "unknown-c7", "unknown-c7", "unknown-c7", "unknown-c7"};
401 modrm_opcodes = c7_opcodes;
402 store = true;
403 immediate_bytes = 4;
404 has_modrm = true;
405 reg_is_opcode = true;
406 break;
Ian Rogers7caad772012-03-30 01:07:54 -0700407 case 0xCC: opcode << "int 3"; break;
408 case 0xE8: opcode << "call"; branch_bytes = 4; break;
Ian Rogers706a10e2012-03-23 17:00:55 -0700409 case 0xE9: opcode << "jmp"; branch_bytes = 4; break;
410 case 0xEB: opcode << "jmp"; branch_bytes = 1; break;
jeffhao174651d2012-04-19 15:27:22 -0700411 case 0xF6: case 0xF7:
412 static const char* f7_opcodes[] = {"test", "unknown-f7", "not", "neg", "mul edx:eax, eax *", "imul edx:eax, eax *", "div edx:eax, edx:eax /", "idiv edx:eax, edx:eax /"};
413 modrm_opcodes = f7_opcodes;
414 has_modrm = true;
415 reg_is_opcode = true;
416 store = true;
417 immediate_bytes = ((instr[1] & 0x38) == 0) ? 1 : 0;
418 break;
Ian Rogers706a10e2012-03-23 17:00:55 -0700419 case 0xFF:
420 static const char* ff_opcodes[] = {"inc", "dec", "call", "call", "jmp", "jmp", "push", "unknown-ff"};
421 modrm_opcodes = ff_opcodes;
422 has_modrm = true;
423 reg_is_opcode = true;
424 load = true;
425 break;
426 default:
427 opcode << StringPrintf("unknown opcode '%02X'", *instr);
428 break;
429 }
430 std::ostringstream args;
431 if (reg_in_opcode) {
432 DCHECK(!has_modrm);
Ian Rogersbf989802012-04-16 16:07:49 -0700433 DumpReg(args, rex, *instr & 0x7, false, prefix[2], GPR);
Ian Rogers706a10e2012-03-23 17:00:55 -0700434 }
435 instr++;
Elliott Hughes92301d92012-04-10 15:57:52 -0700436 uint32_t address_bits = 0;
Ian Rogers706a10e2012-03-23 17:00:55 -0700437 if (has_modrm) {
438 uint8_t modrm = *instr;
439 instr++;
440 uint8_t mod = modrm >> 6;
441 uint8_t reg_or_opcode = (modrm >> 3) & 7;
442 uint8_t rm = modrm & 7;
443 std::ostringstream address;
444 if (mod == 0 && rm == 5) { // fixed address
Elliott Hughes92301d92012-04-10 15:57:52 -0700445 address_bits = *reinterpret_cast<const uint32_t*>(instr);
446 address << StringPrintf("[0x%x]", address_bits);
Ian Rogers706a10e2012-03-23 17:00:55 -0700447 instr += 4;
448 } else if (rm == 4 && mod != 3) { // SIB
449 uint8_t sib = *instr;
450 instr++;
451 uint8_t ss = (sib >> 6) & 3;
452 uint8_t index = (sib >> 3) & 7;
453 uint8_t base = sib & 7;
454 address << "[";
455 if (base != 5 || mod != 0) {
Ian Rogers7caad772012-03-30 01:07:54 -0700456 DumpBaseReg(address, rex, base);
Ian Rogers706a10e2012-03-23 17:00:55 -0700457 if (index != 4) {
458 address << " + ";
459 }
460 }
461 if (index != 4) {
Ian Rogers7caad772012-03-30 01:07:54 -0700462 DumpIndexReg(address, rex, index);
Ian Rogers706a10e2012-03-23 17:00:55 -0700463 if (ss != 0) {
464 address << StringPrintf(" * %d", 1 << ss);
465 }
466 }
467 if (mod == 1) {
468 address << StringPrintf(" + %d", *reinterpret_cast<const int8_t*>(instr));
469 instr++;
470 } else if (mod == 2) {
471 address << StringPrintf(" + %d", *reinterpret_cast<const int32_t*>(instr));
472 instr += 4;
473 }
474 address << "]";
475 } else {
Ian Rogersbf989802012-04-16 16:07:49 -0700476 if (mod == 3) {
477 DumpReg(address, rex, rm, byte_operand, prefix[2], load ? src_reg_file : dst_reg_file);
478 } else {
Ian Rogers706a10e2012-03-23 17:00:55 -0700479 address << "[";
Ian Rogersbf989802012-04-16 16:07:49 -0700480 DumpBaseReg(address, rex, rm);
481 if (mod == 1) {
482 address << StringPrintf(" + %d", *reinterpret_cast<const int8_t*>(instr));
483 instr++;
484 } else if (mod == 2) {
485 address << StringPrintf(" + %d", *reinterpret_cast<const int32_t*>(instr));
486 instr += 4;
487 }
Ian Rogers706a10e2012-03-23 17:00:55 -0700488 address << "]";
489 }
490 }
491
Ian Rogers7caad772012-03-30 01:07:54 -0700492 if (reg_is_opcode && modrm_opcodes != NULL) {
Ian Rogers706a10e2012-03-23 17:00:55 -0700493 opcode << modrm_opcodes[reg_or_opcode];
494 }
495 if (load) {
496 if (!reg_is_opcode) {
Ian Rogersbf989802012-04-16 16:07:49 -0700497 DumpReg(args, rex, reg_or_opcode, byte_operand, prefix[2], dst_reg_file);
Ian Rogers706a10e2012-03-23 17:00:55 -0700498 args << ", ";
499 }
500 DumpSegmentOverride(args, prefix[1]);
501 args << address.str();
502 } else {
503 DCHECK(store);
504 DumpSegmentOverride(args, prefix[1]);
505 args << address.str();
506 if (!reg_is_opcode) {
507 args << ", ";
Ian Rogersbf989802012-04-16 16:07:49 -0700508 DumpReg(args, rex, reg_or_opcode, byte_operand, prefix[2], src_reg_file);
Ian Rogers706a10e2012-03-23 17:00:55 -0700509 }
510 }
511 }
512 if (ax) {
jeffhaoe2962482012-06-28 11:29:57 -0700513 args << ", ";
Ian Rogersbf989802012-04-16 16:07:49 -0700514 DumpReg(args, rex, 0 /* EAX */, byte_operand, prefix[2], GPR);
Ian Rogers706a10e2012-03-23 17:00:55 -0700515 }
jeffhaoe2962482012-06-28 11:29:57 -0700516 if (cx) {
517 args << ", ";
518 DumpReg(args, rex, 1 /* ECX */, true, prefix[2], GPR);
519 }
Ian Rogers706a10e2012-03-23 17:00:55 -0700520 if (immediate_bytes > 0) {
jeffhaoe2962482012-06-28 11:29:57 -0700521 if (has_modrm || reg_in_opcode || ax || cx) {
Ian Rogers706a10e2012-03-23 17:00:55 -0700522 args << ", ";
523 }
524 if (immediate_bytes == 1) {
525 args << StringPrintf("%d", *reinterpret_cast<const int8_t*>(instr));
526 instr++;
527 } else {
528 CHECK_EQ(immediate_bytes, 4u);
529 args << StringPrintf("%d", *reinterpret_cast<const int32_t*>(instr));
530 instr += 4;
531 }
532 } else if (branch_bytes > 0) {
533 DCHECK(!has_modrm);
534 int32_t displacement;
535 if (branch_bytes == 1) {
536 displacement = *reinterpret_cast<const int8_t*>(instr);
537 instr++;
538 } else {
539 CHECK_EQ(branch_bytes, 4u);
540 displacement = *reinterpret_cast<const int32_t*>(instr);
541 instr += 4;
542 }
Elliott Hughes14178a92012-04-16 17:24:51 -0700543 args << StringPrintf("%+d (%p)", displacement, instr + displacement);
Ian Rogers706a10e2012-03-23 17:00:55 -0700544 }
Elliott Hughes92301d92012-04-10 15:57:52 -0700545 if (prefix[1] == kFs) {
546 args << " ; ";
547 Thread::DumpThreadOffset(args, address_bits, 4);
548 }
Elliott Hughes28fa76d2012-04-09 17:31:46 -0700549 std::stringstream hex;
Ian Rogers706a10e2012-03-23 17:00:55 -0700550 for (size_t i = 0; begin_instr + i < instr; ++i) {
Elliott Hughes28fa76d2012-04-09 17:31:46 -0700551 hex << StringPrintf("%02X", begin_instr[i]);
Ian Rogers706a10e2012-03-23 17:00:55 -0700552 }
Elliott Hughes28fa76d2012-04-09 17:31:46 -0700553 os << StringPrintf("\t\t\t%p: %22s \t%-7s ", begin_instr, hex.str().c_str(), opcode.str().c_str()) << args.str() << '\n';
Ian Rogers706a10e2012-03-23 17:00:55 -0700554 return instr - begin_instr;
555}
556
557} // namespace x86
558} // namespace art