blob: dc12ef705d7b0b107e173614dc7212621e6b83f0 [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
148 bool reg_in_opcode = false; // low 3-bits of opcode encode register parameter
Ian Rogersbf989802012-04-16 16:07:49 -0700149 RegFile src_reg_file = GPR;
150 RegFile dst_reg_file = GPR;
Ian Rogers706a10e2012-03-23 17:00:55 -0700151 switch (*instr) {
152#define DISASSEMBLER_ENTRY(opname, \
153 rm8_r8, rm32_r32, \
154 r8_rm8, r32_rm32, \
155 ax8_i8, ax32_i32) \
156 case rm8_r8: opcode << #opname; store = true; has_modrm = true; byte_operand = true; break; \
157 case rm32_r32: opcode << #opname; store = true; has_modrm = true; break; \
158 case r8_rm8: opcode << #opname; load = true; has_modrm = true; byte_operand = true; break; \
159 case r32_rm32: opcode << #opname; load = true; has_modrm = true; break; \
160 case ax8_i8: opcode << #opname; ax = true; immediate_bytes = 1; byte_operand = true; break; \
161 case ax32_i32: opcode << #opname; ax = true; immediate_bytes = 4; break;
162
163DISASSEMBLER_ENTRY(add,
164 0x00 /* RegMem8/Reg8 */, 0x01 /* RegMem32/Reg32 */,
165 0x02 /* Reg8/RegMem8 */, 0x03 /* Reg32/RegMem32 */,
166 0x04 /* Rax8/imm8 opcode */, 0x05 /* Rax32/imm32 */)
167DISASSEMBLER_ENTRY(or,
168 0x08 /* RegMem8/Reg8 */, 0x09 /* RegMem32/Reg32 */,
169 0x0A /* Reg8/RegMem8 */, 0x0B /* Reg32/RegMem32 */,
170 0x0C /* Rax8/imm8 opcode */, 0x0D /* Rax32/imm32 */)
171DISASSEMBLER_ENTRY(adc,
172 0x10 /* RegMem8/Reg8 */, 0x11 /* RegMem32/Reg32 */,
173 0x12 /* Reg8/RegMem8 */, 0x13 /* Reg32/RegMem32 */,
174 0x14 /* Rax8/imm8 opcode */, 0x15 /* Rax32/imm32 */)
175DISASSEMBLER_ENTRY(sbb,
176 0x18 /* RegMem8/Reg8 */, 0x19 /* RegMem32/Reg32 */,
177 0x1A /* Reg8/RegMem8 */, 0x1B /* Reg32/RegMem32 */,
178 0x1C /* Rax8/imm8 opcode */, 0x1D /* Rax32/imm32 */)
179DISASSEMBLER_ENTRY(and,
180 0x20 /* RegMem8/Reg8 */, 0x21 /* RegMem32/Reg32 */,
181 0x22 /* Reg8/RegMem8 */, 0x23 /* Reg32/RegMem32 */,
182 0x24 /* Rax8/imm8 opcode */, 0x25 /* Rax32/imm32 */)
183DISASSEMBLER_ENTRY(sub,
184 0x28 /* RegMem8/Reg8 */, 0x29 /* RegMem32/Reg32 */,
185 0x2A /* Reg8/RegMem8 */, 0x2B /* Reg32/RegMem32 */,
186 0x2C /* Rax8/imm8 opcode */, 0x2D /* Rax32/imm32 */)
187DISASSEMBLER_ENTRY(xor,
188 0x30 /* RegMem8/Reg8 */, 0x31 /* RegMem32/Reg32 */,
189 0x32 /* Reg8/RegMem8 */, 0x33 /* Reg32/RegMem32 */,
190 0x34 /* Rax8/imm8 opcode */, 0x35 /* Rax32/imm32 */)
191DISASSEMBLER_ENTRY(cmp,
192 0x38 /* RegMem8/Reg8 */, 0x39 /* RegMem32/Reg32 */,
193 0x3A /* Reg8/RegMem8 */, 0x3B /* Reg32/RegMem32 */,
194 0x3C /* Rax8/imm8 opcode */, 0x3D /* Rax32/imm32 */)
195
196#undef DISASSEMBLER_ENTRY
197 case 0x50: case 0x51: case 0x52: case 0x53: case 0x54: case 0x55: case 0x56: case 0x57:
198 opcode << "push";
199 reg_in_opcode = true;
200 break;
201 case 0x58: case 0x59: case 0x5A: case 0x5B: case 0x5C: case 0x5D: case 0x5E: case 0x5F:
202 opcode << "pop";
203 reg_in_opcode = true;
204 break;
205 case 0x68: opcode << "push"; immediate_bytes = 4; break;
206 case 0x6A: opcode << "push"; immediate_bytes = 1; break;
207 case 0x70: case 0x71: case 0x72: case 0x73: case 0x74: case 0x75: case 0x76: case 0x77:
208 case 0x78: case 0x79: case 0x7A: case 0x7B: case 0x7C: case 0x7D: case 0x7E: case 0x7F:
209 static const char* condition_codes[] =
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700210 {"o", "no", "b/nae/c", "nb/ae/nc", "z/eq", "nz/ne", "be/na", "nbe/a",
211 "s", "ns", "p/pe", "np/po", "l/nge", "nl/ge", "le/ng", "nle/g"
Ian Rogers706a10e2012-03-23 17:00:55 -0700212 };
213 opcode << "j" << condition_codes[*instr & 0xF];
214 branch_bytes = 1;
215 break;
216 case 0x88: opcode << "mov"; store = true; has_modrm = true; byte_operand = true; break;
217 case 0x89: opcode << "mov"; store = true; has_modrm = true; break;
218 case 0x8A: opcode << "mov"; load = true; has_modrm = true; byte_operand = true; break;
219 case 0x8B: opcode << "mov"; load = true; has_modrm = true; break;
220
221 case 0x0F: // 2 byte extended opcode
222 instr++;
223 switch (*instr) {
Ian Rogers7caad772012-03-30 01:07:54 -0700224 case 0x10: case 0x11:
225 if (prefix[0] == 0xF2) {
226 opcode << "movsd";
227 } else if (prefix[0] == 0xF3) {
228 opcode << "movss";
229 } else if (prefix[2] == 0x66) {
230 opcode << "movupd";
231 } else {
232 opcode << "movups";
233 }
234 has_modrm = true;
Ian Rogersbf989802012-04-16 16:07:49 -0700235 src_reg_file = dst_reg_file = SSE;
Ian Rogers7caad772012-03-30 01:07:54 -0700236 load = *instr == 0x10;
237 store = !load;
238 break;
Ian Rogers706a10e2012-03-23 17:00:55 -0700239 case 0x38: // 3 byte extended opcode
240 opcode << StringPrintf("unknown opcode '0F 38 %02X'", *instr);
241 break;
242 case 0x3A: // 3 byte extended opcode
243 opcode << StringPrintf("unknown opcode '0F 3A %02X'", *instr);
244 break;
Ian Rogersbf989802012-04-16 16:07:49 -0700245 case 0x50: case 0x51: case 0x52: case 0x53: case 0x54: case 0x55: case 0x56: case 0x57:
246 case 0x58: case 0x59: case 0x5C: case 0x5D: case 0x5E: case 0x5F: {
247 switch (*instr) {
248 case 0x50: opcode << "movmsk"; break;
249 case 0x51: opcode << "sqrt"; break;
250 case 0x52: opcode << "rsqrt"; break;
251 case 0x53: opcode << "rcp"; break;
252 case 0x54: opcode << "and"; break;
253 case 0x55: opcode << "andn"; break;
254 case 0x56: opcode << "or"; break;
255 case 0x57: opcode << "xor"; break;
256 case 0x58: opcode << "add"; break;
257 case 0x59: opcode << "mul"; break;
258 case 0x5C: opcode << "sub"; break;
259 case 0x5D: opcode << "min"; break;
260 case 0x5E: opcode << "div"; break;
261 case 0x5F: opcode << "max"; break;
262 default: LOG(FATAL) << "Unreachable";
263 }
264 if (prefix[2] == 0x66) {
265 opcode << "pd";
266 prefix[2] = 0; // clear prefix now its served its purpose as part of the opcode
267 } else if (prefix[0] == 0xF2) {
268 opcode << "sd";
269 prefix[0] = 0; // clear prefix now its served its purpose as part of the opcode
270 } else if (prefix[0] == 0xF3) {
271 opcode << "ss";
272 prefix[0] = 0; // clear prefix now its served its purpose as part of the opcode
273 } else {
274 opcode << "ps";
275 }
276 load = true;
277 has_modrm = true;
278 src_reg_file = dst_reg_file = SSE;
279 break;
280 }
281 case 0x5A:
282 if (prefix[2] == 0x66) {
283 opcode << "cvtpd2ps";
284 prefix[2] = 0; // clear prefix now its served its purpose as part of the opcode
285 } else if (prefix[0] == 0xF2) {
286 opcode << "cvtsd2ss";
287 prefix[0] = 0; // clear prefix now its served its purpose as part of the opcode
288 } else if (prefix[0] == 0xF3) {
289 opcode << "cvtss2sd";
290 prefix[0] = 0; // clear prefix now its served its purpose as part of the opcode
291 } else {
292 opcode << "cvtps2pd";
293 }
294 load = true;
295 has_modrm = true;
296 src_reg_file = dst_reg_file = SSE;
297 break;
298 case 0x5B:
299 if (prefix[2] == 0x66) {
300 opcode << "cvtps2dq";
301 prefix[2] = 0; // clear prefix now its served its purpose as part of the opcode
302 } else if (prefix[0] == 0xF2) {
303 opcode << "bad opcode F2 0F 5B";
304 } else if (prefix[0] == 0xF3) {
305 opcode << "cvttps2dq";
306 prefix[0] = 0; // clear prefix now its served its purpose as part of the opcode
307 } else {
308 opcode << "cvtdq2ps";
309 }
310 load = true;
311 has_modrm = true;
312 src_reg_file = dst_reg_file = SSE;
313 break;
314 case 0x6E:
315 if (prefix[2] == 0x66) {
316 dst_reg_file = SSE;
317 opcode << "movq";
318 prefix[2] = 0; // clear prefix now its served its purpose as part of the opcode
319 } else {
320 dst_reg_file = MMX;
321 opcode << "movd";
322 }
323 load = true;
324 has_modrm = true;
325 break;
326 case 0x6F:
327 if (prefix[2] == 0x66) {
328 dst_reg_file = SSE;
329 opcode << "movdqa";
330 } else if (prefix[0] == 0xF3) {
331 dst_reg_file = SSE;
332 opcode << "movdqu";
333 prefix[0] = 0; // clear prefix now its served its purpose as part of the opcode
334 } else {
335 dst_reg_file = MMX;
336 opcode << "movq";
337 }
338 load = true;
339 has_modrm = true;
340 break;
Ian Rogers706a10e2012-03-23 17:00:55 -0700341 case 0x80: case 0x81: case 0x82: case 0x83: case 0x84: case 0x85: case 0x86: case 0x87:
342 case 0x88: case 0x89: case 0x8A: case 0x8B: case 0x8C: case 0x8D: case 0x8E: case 0x8F:
343 opcode << "j" << condition_codes[*instr & 0xF];
344 branch_bytes = 4;
345 break;
Ian Rogers7caad772012-03-30 01:07:54 -0700346 case 0x90: case 0x91: case 0x92: case 0x93: case 0x94: case 0x95: case 0x96: case 0x97:
347 case 0x98: case 0x99: case 0x9A: case 0x9B: case 0x9C: case 0x9D: case 0x9E: case 0x9F:
348 opcode << "set" << condition_codes[*instr & 0xF];
349 modrm_opcodes = NULL;
350 reg_is_opcode = true;
351 has_modrm = true;
352 store = true;
353 break;
354 case 0xB6: opcode << "movzxb"; has_modrm = true; load = true; break;
355 case 0xB7: opcode << "movzxw"; has_modrm = true; load = true; break;
Ian Rogers706a10e2012-03-23 17:00:55 -0700356 default:
357 opcode << StringPrintf("unknown opcode '0F %02X'", *instr);
358 break;
359 }
360 break;
361 case 0x80: case 0x81: case 0x82: case 0x83:
362 static const char* x80_opcodes[] = {"add", "or", "adc", "sbb", "and", "sub", "xor", "cmp"};
363 modrm_opcodes = x80_opcodes;
364 has_modrm = true;
365 reg_is_opcode = true;
366 store = true;
367 byte_operand = (*instr & 1) == 0;
368 immediate_bytes = *instr == 0x81 ? 4 : 1;
369 break;
Ian Rogers7caad772012-03-30 01:07:54 -0700370 case 0x8D:
371 opcode << "lea";
372 has_modrm = true;
373 load = true;
374 break;
Ian Rogers706a10e2012-03-23 17:00:55 -0700375 case 0xB0: case 0xB1: case 0xB2: case 0xB3: case 0xB4: case 0xB5: case 0xB6: case 0xB7:
376 opcode << "mov";
377 immediate_bytes = 1;
378 reg_in_opcode = true;
379 break;
380 case 0xB8: case 0xB9: case 0xBA: case 0xBB: case 0xBC: case 0xBD: case 0xBE: case 0xBF:
381 opcode << "mov";
382 immediate_bytes = 4;
383 reg_in_opcode = true;
384 break;
Ian Rogers7caad772012-03-30 01:07:54 -0700385 case 0xC0: case 0xC1:
Elliott Hughes16b5c292012-04-16 20:37:16 -0700386 case 0xD0: case 0xD1:
Ian Rogers7caad772012-03-30 01:07:54 -0700387 static const char* shift_opcodes[] =
388 {"rol", "ror", "rcl", "rcr", "shl", "shr", "unknown-shift", "sar"};
389 modrm_opcodes = shift_opcodes;
390 has_modrm = true;
391 reg_is_opcode = true;
392 store = true;
Elliott Hughes16b5c292012-04-16 20:37:16 -0700393 immediate_bytes = ((*instr & 0xf0) == 0xc0) ? 1 : 0;
Ian Rogers7caad772012-03-30 01:07:54 -0700394 byte_operand = *instr == 0xC0;
395 break;
Ian Rogers706a10e2012-03-23 17:00:55 -0700396 case 0xC3: opcode << "ret"; break;
Elliott Hughes0589ca92012-04-09 18:26:20 -0700397 case 0xC7:
398 static const char* c7_opcodes[] = {"mov", "unknown-c7", "unknown-c7", "unknown-c7", "unknown-c7", "unknown-c7", "unknown-c7", "unknown-c7"};
399 modrm_opcodes = c7_opcodes;
400 store = true;
401 immediate_bytes = 4;
402 has_modrm = true;
403 reg_is_opcode = true;
404 break;
Ian Rogers7caad772012-03-30 01:07:54 -0700405 case 0xCC: opcode << "int 3"; break;
406 case 0xE8: opcode << "call"; branch_bytes = 4; break;
Ian Rogers706a10e2012-03-23 17:00:55 -0700407 case 0xE9: opcode << "jmp"; branch_bytes = 4; break;
408 case 0xEB: opcode << "jmp"; branch_bytes = 1; break;
jeffhao174651d2012-04-19 15:27:22 -0700409 case 0xF6: case 0xF7:
410 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 /"};
411 modrm_opcodes = f7_opcodes;
412 has_modrm = true;
413 reg_is_opcode = true;
414 store = true;
415 immediate_bytes = ((instr[1] & 0x38) == 0) ? 1 : 0;
416 break;
Ian Rogers706a10e2012-03-23 17:00:55 -0700417 case 0xFF:
418 static const char* ff_opcodes[] = {"inc", "dec", "call", "call", "jmp", "jmp", "push", "unknown-ff"};
419 modrm_opcodes = ff_opcodes;
420 has_modrm = true;
421 reg_is_opcode = true;
422 load = true;
423 break;
424 default:
425 opcode << StringPrintf("unknown opcode '%02X'", *instr);
426 break;
427 }
428 std::ostringstream args;
429 if (reg_in_opcode) {
430 DCHECK(!has_modrm);
Ian Rogersbf989802012-04-16 16:07:49 -0700431 DumpReg(args, rex, *instr & 0x7, false, prefix[2], GPR);
Ian Rogers706a10e2012-03-23 17:00:55 -0700432 }
433 instr++;
Elliott Hughes92301d92012-04-10 15:57:52 -0700434 uint32_t address_bits = 0;
Ian Rogers706a10e2012-03-23 17:00:55 -0700435 if (has_modrm) {
436 uint8_t modrm = *instr;
437 instr++;
438 uint8_t mod = modrm >> 6;
439 uint8_t reg_or_opcode = (modrm >> 3) & 7;
440 uint8_t rm = modrm & 7;
441 std::ostringstream address;
442 if (mod == 0 && rm == 5) { // fixed address
Elliott Hughes92301d92012-04-10 15:57:52 -0700443 address_bits = *reinterpret_cast<const uint32_t*>(instr);
444 address << StringPrintf("[0x%x]", address_bits);
Ian Rogers706a10e2012-03-23 17:00:55 -0700445 instr += 4;
446 } else if (rm == 4 && mod != 3) { // SIB
447 uint8_t sib = *instr;
448 instr++;
449 uint8_t ss = (sib >> 6) & 3;
450 uint8_t index = (sib >> 3) & 7;
451 uint8_t base = sib & 7;
452 address << "[";
453 if (base != 5 || mod != 0) {
Ian Rogers7caad772012-03-30 01:07:54 -0700454 DumpBaseReg(address, rex, base);
Ian Rogers706a10e2012-03-23 17:00:55 -0700455 if (index != 4) {
456 address << " + ";
457 }
458 }
459 if (index != 4) {
Ian Rogers7caad772012-03-30 01:07:54 -0700460 DumpIndexReg(address, rex, index);
Ian Rogers706a10e2012-03-23 17:00:55 -0700461 if (ss != 0) {
462 address << StringPrintf(" * %d", 1 << ss);
463 }
464 }
465 if (mod == 1) {
466 address << StringPrintf(" + %d", *reinterpret_cast<const int8_t*>(instr));
467 instr++;
468 } else if (mod == 2) {
469 address << StringPrintf(" + %d", *reinterpret_cast<const int32_t*>(instr));
470 instr += 4;
471 }
472 address << "]";
473 } else {
Ian Rogersbf989802012-04-16 16:07:49 -0700474 if (mod == 3) {
475 DumpReg(address, rex, rm, byte_operand, prefix[2], load ? src_reg_file : dst_reg_file);
476 } else {
Ian Rogers706a10e2012-03-23 17:00:55 -0700477 address << "[";
Ian Rogersbf989802012-04-16 16:07:49 -0700478 DumpBaseReg(address, rex, rm);
479 if (mod == 1) {
480 address << StringPrintf(" + %d", *reinterpret_cast<const int8_t*>(instr));
481 instr++;
482 } else if (mod == 2) {
483 address << StringPrintf(" + %d", *reinterpret_cast<const int32_t*>(instr));
484 instr += 4;
485 }
Ian Rogers706a10e2012-03-23 17:00:55 -0700486 address << "]";
487 }
488 }
489
Ian Rogers7caad772012-03-30 01:07:54 -0700490 if (reg_is_opcode && modrm_opcodes != NULL) {
Ian Rogers706a10e2012-03-23 17:00:55 -0700491 opcode << modrm_opcodes[reg_or_opcode];
492 }
493 if (load) {
494 if (!reg_is_opcode) {
Ian Rogersbf989802012-04-16 16:07:49 -0700495 DumpReg(args, rex, reg_or_opcode, byte_operand, prefix[2], dst_reg_file);
Ian Rogers706a10e2012-03-23 17:00:55 -0700496 args << ", ";
497 }
498 DumpSegmentOverride(args, prefix[1]);
499 args << address.str();
500 } else {
501 DCHECK(store);
502 DumpSegmentOverride(args, prefix[1]);
503 args << address.str();
504 if (!reg_is_opcode) {
505 args << ", ";
Ian Rogersbf989802012-04-16 16:07:49 -0700506 DumpReg(args, rex, reg_or_opcode, byte_operand, prefix[2], src_reg_file);
Ian Rogers706a10e2012-03-23 17:00:55 -0700507 }
508 }
509 }
510 if (ax) {
Ian Rogersbf989802012-04-16 16:07:49 -0700511 DumpReg(args, rex, 0 /* EAX */, byte_operand, prefix[2], GPR);
Ian Rogers706a10e2012-03-23 17:00:55 -0700512 }
513 if (immediate_bytes > 0) {
514 if (has_modrm || reg_in_opcode || ax) {
515 args << ", ";
516 }
517 if (immediate_bytes == 1) {
518 args << StringPrintf("%d", *reinterpret_cast<const int8_t*>(instr));
519 instr++;
520 } else {
521 CHECK_EQ(immediate_bytes, 4u);
522 args << StringPrintf("%d", *reinterpret_cast<const int32_t*>(instr));
523 instr += 4;
524 }
525 } else if (branch_bytes > 0) {
526 DCHECK(!has_modrm);
527 int32_t displacement;
528 if (branch_bytes == 1) {
529 displacement = *reinterpret_cast<const int8_t*>(instr);
530 instr++;
531 } else {
532 CHECK_EQ(branch_bytes, 4u);
533 displacement = *reinterpret_cast<const int32_t*>(instr);
534 instr += 4;
535 }
Elliott Hughes14178a92012-04-16 17:24:51 -0700536 args << StringPrintf("%+d (%p)", displacement, instr + displacement);
Ian Rogers706a10e2012-03-23 17:00:55 -0700537 }
Elliott Hughes92301d92012-04-10 15:57:52 -0700538 if (prefix[1] == kFs) {
539 args << " ; ";
540 Thread::DumpThreadOffset(args, address_bits, 4);
541 }
Elliott Hughes28fa76d2012-04-09 17:31:46 -0700542 std::stringstream hex;
Ian Rogers706a10e2012-03-23 17:00:55 -0700543 for (size_t i = 0; begin_instr + i < instr; ++i) {
Elliott Hughes28fa76d2012-04-09 17:31:46 -0700544 hex << StringPrintf("%02X", begin_instr[i]);
Ian Rogers706a10e2012-03-23 17:00:55 -0700545 }
Elliott Hughes28fa76d2012-04-09 17:31:46 -0700546 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 -0700547 return instr - begin_instr;
548}
549
550} // namespace x86
551} // namespace art