blob: d45d641f97c93c74ad328a7303aa0768af6ad305 [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" };
jeffhao703f2cd2012-07-13 17:25:52 -070039static const char* gReg16Names[] = { "ax", "cx", "dx", "bx", "sp", "bp", "si", "di" };
40static const char* gReg32Names[] = { "eax", "ecx", "edx", "ebx", "esp", "ebp", "esi", "edi" };
Ian Rogers706a10e2012-03-23 17:00:55 -070041
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
jeffhao703f2cd2012-07-13 17:25:52 -0700150 bool no_ops = false;
Ian Rogersbf989802012-04-16 16:07:49 -0700151 RegFile src_reg_file = GPR;
152 RegFile dst_reg_file = GPR;
Ian Rogers706a10e2012-03-23 17:00:55 -0700153 switch (*instr) {
154#define DISASSEMBLER_ENTRY(opname, \
155 rm8_r8, rm32_r32, \
156 r8_rm8, r32_rm32, \
157 ax8_i8, ax32_i32) \
158 case rm8_r8: opcode << #opname; store = true; has_modrm = true; byte_operand = true; break; \
159 case rm32_r32: opcode << #opname; store = true; has_modrm = true; break; \
160 case r8_rm8: opcode << #opname; load = true; has_modrm = true; byte_operand = true; break; \
161 case r32_rm32: opcode << #opname; load = true; has_modrm = true; break; \
162 case ax8_i8: opcode << #opname; ax = true; immediate_bytes = 1; byte_operand = true; break; \
163 case ax32_i32: opcode << #opname; ax = true; immediate_bytes = 4; break;
164
165DISASSEMBLER_ENTRY(add,
166 0x00 /* RegMem8/Reg8 */, 0x01 /* RegMem32/Reg32 */,
167 0x02 /* Reg8/RegMem8 */, 0x03 /* Reg32/RegMem32 */,
168 0x04 /* Rax8/imm8 opcode */, 0x05 /* Rax32/imm32 */)
169DISASSEMBLER_ENTRY(or,
170 0x08 /* RegMem8/Reg8 */, 0x09 /* RegMem32/Reg32 */,
171 0x0A /* Reg8/RegMem8 */, 0x0B /* Reg32/RegMem32 */,
172 0x0C /* Rax8/imm8 opcode */, 0x0D /* Rax32/imm32 */)
173DISASSEMBLER_ENTRY(adc,
174 0x10 /* RegMem8/Reg8 */, 0x11 /* RegMem32/Reg32 */,
175 0x12 /* Reg8/RegMem8 */, 0x13 /* Reg32/RegMem32 */,
176 0x14 /* Rax8/imm8 opcode */, 0x15 /* Rax32/imm32 */)
177DISASSEMBLER_ENTRY(sbb,
178 0x18 /* RegMem8/Reg8 */, 0x19 /* RegMem32/Reg32 */,
179 0x1A /* Reg8/RegMem8 */, 0x1B /* Reg32/RegMem32 */,
180 0x1C /* Rax8/imm8 opcode */, 0x1D /* Rax32/imm32 */)
181DISASSEMBLER_ENTRY(and,
182 0x20 /* RegMem8/Reg8 */, 0x21 /* RegMem32/Reg32 */,
183 0x22 /* Reg8/RegMem8 */, 0x23 /* Reg32/RegMem32 */,
184 0x24 /* Rax8/imm8 opcode */, 0x25 /* Rax32/imm32 */)
185DISASSEMBLER_ENTRY(sub,
186 0x28 /* RegMem8/Reg8 */, 0x29 /* RegMem32/Reg32 */,
187 0x2A /* Reg8/RegMem8 */, 0x2B /* Reg32/RegMem32 */,
188 0x2C /* Rax8/imm8 opcode */, 0x2D /* Rax32/imm32 */)
189DISASSEMBLER_ENTRY(xor,
190 0x30 /* RegMem8/Reg8 */, 0x31 /* RegMem32/Reg32 */,
191 0x32 /* Reg8/RegMem8 */, 0x33 /* Reg32/RegMem32 */,
192 0x34 /* Rax8/imm8 opcode */, 0x35 /* Rax32/imm32 */)
193DISASSEMBLER_ENTRY(cmp,
194 0x38 /* RegMem8/Reg8 */, 0x39 /* RegMem32/Reg32 */,
195 0x3A /* Reg8/RegMem8 */, 0x3B /* Reg32/RegMem32 */,
196 0x3C /* Rax8/imm8 opcode */, 0x3D /* Rax32/imm32 */)
197
198#undef DISASSEMBLER_ENTRY
199 case 0x50: case 0x51: case 0x52: case 0x53: case 0x54: case 0x55: case 0x56: case 0x57:
200 opcode << "push";
201 reg_in_opcode = true;
202 break;
203 case 0x58: case 0x59: case 0x5A: case 0x5B: case 0x5C: case 0x5D: case 0x5E: case 0x5F:
204 opcode << "pop";
205 reg_in_opcode = true;
206 break;
207 case 0x68: opcode << "push"; immediate_bytes = 4; break;
208 case 0x6A: opcode << "push"; immediate_bytes = 1; break;
209 case 0x70: case 0x71: case 0x72: case 0x73: case 0x74: case 0x75: case 0x76: case 0x77:
210 case 0x78: case 0x79: case 0x7A: case 0x7B: case 0x7C: case 0x7D: case 0x7E: case 0x7F:
211 static const char* condition_codes[] =
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700212 {"o", "no", "b/nae/c", "nb/ae/nc", "z/eq", "nz/ne", "be/na", "nbe/a",
213 "s", "ns", "p/pe", "np/po", "l/nge", "nl/ge", "le/ng", "nle/g"
Ian Rogers706a10e2012-03-23 17:00:55 -0700214 };
215 opcode << "j" << condition_codes[*instr & 0xF];
216 branch_bytes = 1;
217 break;
218 case 0x88: opcode << "mov"; store = true; has_modrm = true; byte_operand = true; break;
219 case 0x89: opcode << "mov"; store = true; has_modrm = true; break;
220 case 0x8A: opcode << "mov"; load = true; has_modrm = true; byte_operand = true; break;
221 case 0x8B: opcode << "mov"; load = true; has_modrm = true; break;
222
223 case 0x0F: // 2 byte extended opcode
224 instr++;
225 switch (*instr) {
Ian Rogers7caad772012-03-30 01:07:54 -0700226 case 0x10: case 0x11:
227 if (prefix[0] == 0xF2) {
228 opcode << "movsd";
jeffhaofdffdf82012-07-11 16:08:43 -0700229 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogers7caad772012-03-30 01:07:54 -0700230 } else if (prefix[0] == 0xF3) {
231 opcode << "movss";
jeffhaofdffdf82012-07-11 16:08:43 -0700232 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogers7caad772012-03-30 01:07:54 -0700233 } else if (prefix[2] == 0x66) {
234 opcode << "movupd";
jeffhaofdffdf82012-07-11 16:08:43 -0700235 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogers7caad772012-03-30 01:07:54 -0700236 } else {
237 opcode << "movups";
238 }
239 has_modrm = true;
Ian Rogersbf989802012-04-16 16:07:49 -0700240 src_reg_file = dst_reg_file = SSE;
Ian Rogers7caad772012-03-30 01:07:54 -0700241 load = *instr == 0x10;
242 store = !load;
243 break;
jeffhaofdffdf82012-07-11 16:08:43 -0700244 case 0x2A:
245 if (prefix[2] == 0x66) {
246 opcode << "cvtpi2pd";
247 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
248 } else if (prefix[0] == 0xF2) {
249 opcode << "cvtsi2sd";
250 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
251 } else if (prefix[0] == 0xF3) {
252 opcode << "cvtsi2ss";
253 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
254 } else {
255 opcode << "cvtpi2ps";
256 }
257 load = true;
258 has_modrm = true;
259 dst_reg_file = SSE;
260 break;
261 case 0x2C:
262 if (prefix[2] == 0x66) {
263 opcode << "cvttpd2pi";
264 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
265 } else if (prefix[0] == 0xF2) {
266 opcode << "cvttsd2si";
267 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
268 } else if (prefix[0] == 0xF3) {
269 opcode << "cvttss2si";
270 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
271 } else {
272 opcode << "cvttps2pi";
273 }
274 load = true;
275 has_modrm = true;
276 src_reg_file = SSE;
277 break;
278 case 0x2D:
279 if (prefix[2] == 0x66) {
280 opcode << "cvtpd2pi";
281 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
282 } else if (prefix[0] == 0xF2) {
283 opcode << "cvtsd2si";
284 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
285 } else if (prefix[0] == 0xF3) {
286 opcode << "cvtss2si";
287 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
288 } else {
289 opcode << "cvtps2pi";
290 }
291 load = true;
292 has_modrm = true;
293 src_reg_file = SSE;
294 break;
295 case 0x2E:
296 opcode << "u";
297 // FALLTHROUGH
298 case 0x2F:
299 if (prefix[2] == 0x66) {
300 opcode << "comisd";
301 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
302 } else {
303 opcode << "comiss";
304 }
305 has_modrm = true;
306 load = true;
307 src_reg_file = dst_reg_file = SSE;
308 break;
Ian Rogers706a10e2012-03-23 17:00:55 -0700309 case 0x38: // 3 byte extended opcode
310 opcode << StringPrintf("unknown opcode '0F 38 %02X'", *instr);
311 break;
312 case 0x3A: // 3 byte extended opcode
313 opcode << StringPrintf("unknown opcode '0F 3A %02X'", *instr);
314 break;
Ian Rogersbf989802012-04-16 16:07:49 -0700315 case 0x50: case 0x51: case 0x52: case 0x53: case 0x54: case 0x55: case 0x56: case 0x57:
316 case 0x58: case 0x59: case 0x5C: case 0x5D: case 0x5E: case 0x5F: {
317 switch (*instr) {
318 case 0x50: opcode << "movmsk"; break;
319 case 0x51: opcode << "sqrt"; break;
320 case 0x52: opcode << "rsqrt"; break;
321 case 0x53: opcode << "rcp"; break;
322 case 0x54: opcode << "and"; break;
323 case 0x55: opcode << "andn"; break;
324 case 0x56: opcode << "or"; break;
325 case 0x57: opcode << "xor"; break;
326 case 0x58: opcode << "add"; break;
327 case 0x59: opcode << "mul"; break;
328 case 0x5C: opcode << "sub"; break;
329 case 0x5D: opcode << "min"; break;
330 case 0x5E: opcode << "div"; break;
331 case 0x5F: opcode << "max"; break;
332 default: LOG(FATAL) << "Unreachable";
333 }
334 if (prefix[2] == 0x66) {
335 opcode << "pd";
jeffhaofdffdf82012-07-11 16:08:43 -0700336 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogersbf989802012-04-16 16:07:49 -0700337 } else if (prefix[0] == 0xF2) {
338 opcode << "sd";
jeffhaofdffdf82012-07-11 16:08:43 -0700339 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogersbf989802012-04-16 16:07:49 -0700340 } else if (prefix[0] == 0xF3) {
341 opcode << "ss";
jeffhaofdffdf82012-07-11 16:08:43 -0700342 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogersbf989802012-04-16 16:07:49 -0700343 } else {
344 opcode << "ps";
345 }
346 load = true;
347 has_modrm = true;
348 src_reg_file = dst_reg_file = SSE;
349 break;
350 }
351 case 0x5A:
352 if (prefix[2] == 0x66) {
353 opcode << "cvtpd2ps";
jeffhaofdffdf82012-07-11 16:08:43 -0700354 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogersbf989802012-04-16 16:07:49 -0700355 } else if (prefix[0] == 0xF2) {
356 opcode << "cvtsd2ss";
jeffhaofdffdf82012-07-11 16:08:43 -0700357 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogersbf989802012-04-16 16:07:49 -0700358 } else if (prefix[0] == 0xF3) {
359 opcode << "cvtss2sd";
jeffhaofdffdf82012-07-11 16:08:43 -0700360 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogersbf989802012-04-16 16:07:49 -0700361 } else {
362 opcode << "cvtps2pd";
363 }
364 load = true;
365 has_modrm = true;
366 src_reg_file = dst_reg_file = SSE;
367 break;
368 case 0x5B:
369 if (prefix[2] == 0x66) {
370 opcode << "cvtps2dq";
jeffhaofdffdf82012-07-11 16:08:43 -0700371 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogersbf989802012-04-16 16:07:49 -0700372 } else if (prefix[0] == 0xF2) {
373 opcode << "bad opcode F2 0F 5B";
374 } else if (prefix[0] == 0xF3) {
375 opcode << "cvttps2dq";
jeffhaofdffdf82012-07-11 16:08:43 -0700376 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogersbf989802012-04-16 16:07:49 -0700377 } else {
378 opcode << "cvtdq2ps";
379 }
380 load = true;
381 has_modrm = true;
382 src_reg_file = dst_reg_file = SSE;
383 break;
384 case 0x6E:
385 if (prefix[2] == 0x66) {
386 dst_reg_file = SSE;
jeffhaofdffdf82012-07-11 16:08:43 -0700387 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogersbf989802012-04-16 16:07:49 -0700388 } else {
389 dst_reg_file = MMX;
Ian Rogersbf989802012-04-16 16:07:49 -0700390 }
jeffhaofdffdf82012-07-11 16:08:43 -0700391 opcode << "movd";
Ian Rogersbf989802012-04-16 16:07:49 -0700392 load = true;
393 has_modrm = true;
394 break;
395 case 0x6F:
396 if (prefix[2] == 0x66) {
397 dst_reg_file = SSE;
398 opcode << "movdqa";
jeffhaofdffdf82012-07-11 16:08:43 -0700399 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogersbf989802012-04-16 16:07:49 -0700400 } else if (prefix[0] == 0xF3) {
401 dst_reg_file = SSE;
402 opcode << "movdqu";
jeffhaofdffdf82012-07-11 16:08:43 -0700403 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogersbf989802012-04-16 16:07:49 -0700404 } else {
405 dst_reg_file = MMX;
406 opcode << "movq";
407 }
408 load = true;
409 has_modrm = true;
410 break;
jeffhaofdffdf82012-07-11 16:08:43 -0700411 case 0x71:
412 if (prefix[2] == 0x66) {
413 dst_reg_file = SSE;
414 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
415 } else {
416 dst_reg_file = MMX;
417 }
418 static const char* x71_opcodes[] = {"unknown-71", "unknown-71", "psrlw", "unknown-71", "psraw", "unknown-71", "psllw", "unknown-71"};
419 modrm_opcodes = x71_opcodes;
420 reg_is_opcode = true;
421 has_modrm = true;
422 store = true;
423 immediate_bytes = 1;
424 break;
425 case 0x72:
426 if (prefix[2] == 0x66) {
427 dst_reg_file = SSE;
428 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
429 } else {
430 dst_reg_file = MMX;
431 }
432 static const char* x72_opcodes[] = {"unknown-72", "unknown-72", "psrld", "unknown-72", "psrad", "unknown-72", "pslld", "unknown-72"};
433 modrm_opcodes = x72_opcodes;
434 reg_is_opcode = true;
435 has_modrm = true;
436 store = true;
437 immediate_bytes = 1;
438 break;
439 case 0x73:
440 if (prefix[2] == 0x66) {
441 dst_reg_file = SSE;
442 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
443 } else {
444 dst_reg_file = MMX;
445 }
446 static const char* x73_opcodes[] = {"unknown-73", "unknown-73", "psrlq", "unknown-73", "unknown-73", "unknown-73", "psllq", "unknown-73"};
447 modrm_opcodes = x73_opcodes;
448 reg_is_opcode = true;
449 has_modrm = true;
450 store = true;
451 immediate_bytes = 1;
452 break;
453 case 0x7E:
454 if (prefix[2] == 0x66) {
455 src_reg_file = SSE;
456 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
457 } else {
458 src_reg_file = MMX;
459 }
460 opcode << "movd";
461 has_modrm = true;
462 store = true;
463 break;
Ian Rogers706a10e2012-03-23 17:00:55 -0700464 case 0x80: case 0x81: case 0x82: case 0x83: case 0x84: case 0x85: case 0x86: case 0x87:
465 case 0x88: case 0x89: case 0x8A: case 0x8B: case 0x8C: case 0x8D: case 0x8E: case 0x8F:
466 opcode << "j" << condition_codes[*instr & 0xF];
467 branch_bytes = 4;
468 break;
Ian Rogers7caad772012-03-30 01:07:54 -0700469 case 0x90: case 0x91: case 0x92: case 0x93: case 0x94: case 0x95: case 0x96: case 0x97:
470 case 0x98: case 0x99: case 0x9A: case 0x9B: case 0x9C: case 0x9D: case 0x9E: case 0x9F:
471 opcode << "set" << condition_codes[*instr & 0xF];
472 modrm_opcodes = NULL;
473 reg_is_opcode = true;
474 has_modrm = true;
475 store = true;
476 break;
jeffhao703f2cd2012-07-13 17:25:52 -0700477 case 0xAE:
478 if (prefix[0] == 0xF3) {
479 static const char* xAE_opcodes[] = {"rdfsbase", "rdgsbase", "wrfsbase", "wrgsbase", "unknown-AE", "unknown-AE", "unknown-AE", "unknown-AE"};
480 modrm_opcodes = xAE_opcodes;
481 reg_is_opcode = true;
482 has_modrm = true;
483 uint8_t reg_or_opcode = (instr[1] >> 3) & 7;
484 switch (reg_or_opcode) {
485 case 0:
486 prefix[1] = kFs;
487 load = true;
488 break;
489 case 1:
490 prefix[1] = kGs;
491 load = true;
492 break;
493 case 2:
494 prefix[1] = kFs;
495 store = true;
496 break;
497 case 3:
498 prefix[1] = kGs;
499 store = true;
500 break;
501 default:
502 load = true;
503 break;
504 }
505 } else {
506 static const char* xAE_opcodes[] = {"unknown-AE", "unknown-AE", "unknown-AE", "unknown-AE", "unknown-AE", "lfence", "mfence", "sfence"};
507 modrm_opcodes = xAE_opcodes;
508 reg_is_opcode = true;
509 has_modrm = true;
510 load = true;
511 no_ops = true;
512 }
513 break;
Ian Rogers7caad772012-03-30 01:07:54 -0700514 case 0xB6: opcode << "movzxb"; has_modrm = true; load = true; break;
515 case 0xB7: opcode << "movzxw"; has_modrm = true; load = true; break;
Ian Rogers706a10e2012-03-23 17:00:55 -0700516 default:
517 opcode << StringPrintf("unknown opcode '0F %02X'", *instr);
518 break;
519 }
520 break;
521 case 0x80: case 0x81: case 0x82: case 0x83:
522 static const char* x80_opcodes[] = {"add", "or", "adc", "sbb", "and", "sub", "xor", "cmp"};
523 modrm_opcodes = x80_opcodes;
524 has_modrm = true;
525 reg_is_opcode = true;
526 store = true;
527 byte_operand = (*instr & 1) == 0;
528 immediate_bytes = *instr == 0x81 ? 4 : 1;
529 break;
jeffhao703f2cd2012-07-13 17:25:52 -0700530 case 0x84: case 0x85:
531 opcode << "test";
532 has_modrm = true;
533 load = true;
534 byte_operand = (*instr & 1) == 0;
535 break;
Ian Rogers7caad772012-03-30 01:07:54 -0700536 case 0x8D:
537 opcode << "lea";
538 has_modrm = true;
539 load = true;
540 break;
jeffhao703f2cd2012-07-13 17:25:52 -0700541 case 0x8F:
542 opcode << "pop";
543 has_modrm = true;
544 reg_is_opcode = true;
545 store = true;
546 break;
Ian Rogers706a10e2012-03-23 17:00:55 -0700547 case 0xB0: case 0xB1: case 0xB2: case 0xB3: case 0xB4: case 0xB5: case 0xB6: case 0xB7:
548 opcode << "mov";
549 immediate_bytes = 1;
550 reg_in_opcode = true;
551 break;
552 case 0xB8: case 0xB9: case 0xBA: case 0xBB: case 0xBC: case 0xBD: case 0xBE: case 0xBF:
553 opcode << "mov";
554 immediate_bytes = 4;
555 reg_in_opcode = true;
556 break;
Ian Rogers7caad772012-03-30 01:07:54 -0700557 case 0xC0: case 0xC1:
jeffhaoe2962482012-06-28 11:29:57 -0700558 case 0xD0: case 0xD1: case 0xD2: case 0xD3:
Ian Rogers7caad772012-03-30 01:07:54 -0700559 static const char* shift_opcodes[] =
560 {"rol", "ror", "rcl", "rcr", "shl", "shr", "unknown-shift", "sar"};
561 modrm_opcodes = shift_opcodes;
562 has_modrm = true;
563 reg_is_opcode = true;
564 store = true;
Elliott Hughes16b5c292012-04-16 20:37:16 -0700565 immediate_bytes = ((*instr & 0xf0) == 0xc0) ? 1 : 0;
jeffhaoe2962482012-06-28 11:29:57 -0700566 cx = (*instr == 0xD2) || (*instr == 0xD3);
567 byte_operand = (*instr == 0xC0);
Ian Rogers7caad772012-03-30 01:07:54 -0700568 break;
Ian Rogers706a10e2012-03-23 17:00:55 -0700569 case 0xC3: opcode << "ret"; break;
Elliott Hughes0589ca92012-04-09 18:26:20 -0700570 case 0xC7:
571 static const char* c7_opcodes[] = {"mov", "unknown-c7", "unknown-c7", "unknown-c7", "unknown-c7", "unknown-c7", "unknown-c7", "unknown-c7"};
572 modrm_opcodes = c7_opcodes;
573 store = true;
574 immediate_bytes = 4;
575 has_modrm = true;
576 reg_is_opcode = true;
577 break;
Ian Rogers7caad772012-03-30 01:07:54 -0700578 case 0xCC: opcode << "int 3"; break;
579 case 0xE8: opcode << "call"; branch_bytes = 4; break;
Ian Rogers706a10e2012-03-23 17:00:55 -0700580 case 0xE9: opcode << "jmp"; branch_bytes = 4; break;
581 case 0xEB: opcode << "jmp"; branch_bytes = 1; break;
jeffhao174651d2012-04-19 15:27:22 -0700582 case 0xF6: case 0xF7:
583 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 /"};
584 modrm_opcodes = f7_opcodes;
585 has_modrm = true;
586 reg_is_opcode = true;
587 store = true;
588 immediate_bytes = ((instr[1] & 0x38) == 0) ? 1 : 0;
589 break;
Ian Rogers706a10e2012-03-23 17:00:55 -0700590 case 0xFF:
591 static const char* ff_opcodes[] = {"inc", "dec", "call", "call", "jmp", "jmp", "push", "unknown-ff"};
592 modrm_opcodes = ff_opcodes;
593 has_modrm = true;
594 reg_is_opcode = true;
595 load = true;
596 break;
597 default:
598 opcode << StringPrintf("unknown opcode '%02X'", *instr);
599 break;
600 }
601 std::ostringstream args;
602 if (reg_in_opcode) {
603 DCHECK(!has_modrm);
Ian Rogersbf989802012-04-16 16:07:49 -0700604 DumpReg(args, rex, *instr & 0x7, false, prefix[2], GPR);
Ian Rogers706a10e2012-03-23 17:00:55 -0700605 }
606 instr++;
Elliott Hughes92301d92012-04-10 15:57:52 -0700607 uint32_t address_bits = 0;
Ian Rogers706a10e2012-03-23 17:00:55 -0700608 if (has_modrm) {
609 uint8_t modrm = *instr;
610 instr++;
611 uint8_t mod = modrm >> 6;
612 uint8_t reg_or_opcode = (modrm >> 3) & 7;
613 uint8_t rm = modrm & 7;
614 std::ostringstream address;
615 if (mod == 0 && rm == 5) { // fixed address
Elliott Hughes92301d92012-04-10 15:57:52 -0700616 address_bits = *reinterpret_cast<const uint32_t*>(instr);
617 address << StringPrintf("[0x%x]", address_bits);
Ian Rogers706a10e2012-03-23 17:00:55 -0700618 instr += 4;
619 } else if (rm == 4 && mod != 3) { // SIB
620 uint8_t sib = *instr;
621 instr++;
622 uint8_t ss = (sib >> 6) & 3;
623 uint8_t index = (sib >> 3) & 7;
624 uint8_t base = sib & 7;
625 address << "[";
626 if (base != 5 || mod != 0) {
Ian Rogers7caad772012-03-30 01:07:54 -0700627 DumpBaseReg(address, rex, base);
Ian Rogers706a10e2012-03-23 17:00:55 -0700628 if (index != 4) {
629 address << " + ";
630 }
631 }
632 if (index != 4) {
Ian Rogers7caad772012-03-30 01:07:54 -0700633 DumpIndexReg(address, rex, index);
Ian Rogers706a10e2012-03-23 17:00:55 -0700634 if (ss != 0) {
635 address << StringPrintf(" * %d", 1 << ss);
636 }
637 }
638 if (mod == 1) {
639 address << StringPrintf(" + %d", *reinterpret_cast<const int8_t*>(instr));
640 instr++;
641 } else if (mod == 2) {
642 address << StringPrintf(" + %d", *reinterpret_cast<const int32_t*>(instr));
643 instr += 4;
644 }
645 address << "]";
646 } else {
Ian Rogersbf989802012-04-16 16:07:49 -0700647 if (mod == 3) {
jeffhao703f2cd2012-07-13 17:25:52 -0700648 if (!no_ops) {
649 DumpReg(address, rex, rm, byte_operand, prefix[2], load ? src_reg_file : dst_reg_file);
650 }
Ian Rogersbf989802012-04-16 16:07:49 -0700651 } else {
Ian Rogers706a10e2012-03-23 17:00:55 -0700652 address << "[";
Ian Rogersbf989802012-04-16 16:07:49 -0700653 DumpBaseReg(address, rex, rm);
654 if (mod == 1) {
655 address << StringPrintf(" + %d", *reinterpret_cast<const int8_t*>(instr));
656 instr++;
657 } else if (mod == 2) {
658 address << StringPrintf(" + %d", *reinterpret_cast<const int32_t*>(instr));
659 instr += 4;
660 }
Ian Rogers706a10e2012-03-23 17:00:55 -0700661 address << "]";
662 }
663 }
664
Ian Rogers7caad772012-03-30 01:07:54 -0700665 if (reg_is_opcode && modrm_opcodes != NULL) {
Ian Rogers706a10e2012-03-23 17:00:55 -0700666 opcode << modrm_opcodes[reg_or_opcode];
667 }
668 if (load) {
669 if (!reg_is_opcode) {
Ian Rogersbf989802012-04-16 16:07:49 -0700670 DumpReg(args, rex, reg_or_opcode, byte_operand, prefix[2], dst_reg_file);
Ian Rogers706a10e2012-03-23 17:00:55 -0700671 args << ", ";
672 }
673 DumpSegmentOverride(args, prefix[1]);
674 args << address.str();
675 } else {
676 DCHECK(store);
677 DumpSegmentOverride(args, prefix[1]);
678 args << address.str();
679 if (!reg_is_opcode) {
680 args << ", ";
Ian Rogersbf989802012-04-16 16:07:49 -0700681 DumpReg(args, rex, reg_or_opcode, byte_operand, prefix[2], src_reg_file);
Ian Rogers706a10e2012-03-23 17:00:55 -0700682 }
683 }
684 }
685 if (ax) {
jeffhaofdffdf82012-07-11 16:08:43 -0700686 // If this opcode implicitly uses ax, ax is always the first arg.
Ian Rogersbf989802012-04-16 16:07:49 -0700687 DumpReg(args, rex, 0 /* EAX */, byte_operand, prefix[2], GPR);
Ian Rogers706a10e2012-03-23 17:00:55 -0700688 }
jeffhaoe2962482012-06-28 11:29:57 -0700689 if (cx) {
690 args << ", ";
691 DumpReg(args, rex, 1 /* ECX */, true, prefix[2], GPR);
692 }
Ian Rogers706a10e2012-03-23 17:00:55 -0700693 if (immediate_bytes > 0) {
jeffhaoe2962482012-06-28 11:29:57 -0700694 if (has_modrm || reg_in_opcode || ax || cx) {
Ian Rogers706a10e2012-03-23 17:00:55 -0700695 args << ", ";
696 }
697 if (immediate_bytes == 1) {
698 args << StringPrintf("%d", *reinterpret_cast<const int8_t*>(instr));
699 instr++;
700 } else {
701 CHECK_EQ(immediate_bytes, 4u);
702 args << StringPrintf("%d", *reinterpret_cast<const int32_t*>(instr));
703 instr += 4;
704 }
705 } else if (branch_bytes > 0) {
706 DCHECK(!has_modrm);
707 int32_t displacement;
708 if (branch_bytes == 1) {
709 displacement = *reinterpret_cast<const int8_t*>(instr);
710 instr++;
711 } else {
712 CHECK_EQ(branch_bytes, 4u);
713 displacement = *reinterpret_cast<const int32_t*>(instr);
714 instr += 4;
715 }
Elliott Hughes14178a92012-04-16 17:24:51 -0700716 args << StringPrintf("%+d (%p)", displacement, instr + displacement);
Ian Rogers706a10e2012-03-23 17:00:55 -0700717 }
Elliott Hughes92301d92012-04-10 15:57:52 -0700718 if (prefix[1] == kFs) {
719 args << " ; ";
720 Thread::DumpThreadOffset(args, address_bits, 4);
721 }
Elliott Hughes28fa76d2012-04-09 17:31:46 -0700722 std::stringstream hex;
Ian Rogers706a10e2012-03-23 17:00:55 -0700723 for (size_t i = 0; begin_instr + i < instr; ++i) {
Elliott Hughes28fa76d2012-04-09 17:31:46 -0700724 hex << StringPrintf("%02X", begin_instr[i]);
Ian Rogers706a10e2012-03-23 17:00:55 -0700725 }
Elliott Hughes28fa76d2012-04-09 17:31:46 -0700726 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 -0700727 return instr - begin_instr;
728}
729
730} // namespace x86
731} // namespace art