blob: 8781c7a274948203781eb9752a06feadba21bfe5 [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 Hughes07ed66b2012-12-12 18:34:25 -080021#include "base/logging.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080022#include "base/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
Brian Carlstrom1895ea32013-07-18 13:28:37 -070028DisassemblerX86::DisassemblerX86() {}
Ian Rogers706a10e2012-03-23 17:00:55 -070029
Ian Rogersb23a7722012-10-09 16:54:26 -070030size_t DisassemblerX86::Dump(std::ostream& os, const uint8_t* begin) {
31 return DumpInstruction(os, begin);
32}
33
Ian Rogers706a10e2012-03-23 17:00:55 -070034void DisassemblerX86::Dump(std::ostream& os, const uint8_t* begin, const uint8_t* end) {
35 size_t length = 0;
36 for (const uint8_t* cur = begin; cur < end; cur += length) {
37 length = DumpInstruction(os, cur);
38 }
39}
40
41static const char* gReg8Names[] = { "al", "cl", "dl", "bl", "ah", "ch", "dh", "bh" };
jeffhao703f2cd2012-07-13 17:25:52 -070042static const char* gReg16Names[] = { "ax", "cx", "dx", "bx", "sp", "bp", "si", "di" };
43static const char* gReg32Names[] = { "eax", "ecx", "edx", "ebx", "esp", "ebp", "esi", "edi" };
Ian Rogers706a10e2012-03-23 17:00:55 -070044
45static void DumpReg0(std::ostream& os, uint8_t /*rex*/, size_t reg,
46 bool byte_operand, uint8_t size_override) {
47 DCHECK_LT(reg, 8u);
48 // TODO: combine rex into size
49 size_t size = byte_operand ? 1 : (size_override == 0x66 ? 2 : 4);
50 switch (size) {
51 case 1: os << gReg8Names[reg]; break;
52 case 2: os << gReg16Names[reg]; break;
53 case 4: os << gReg32Names[reg]; break;
54 default: LOG(FATAL) << "unexpected size " << size;
55 }
56}
57
Ian Rogersbf989802012-04-16 16:07:49 -070058enum RegFile { GPR, MMX, SSE };
59
Ian Rogers706a10e2012-03-23 17:00:55 -070060static void DumpReg(std::ostream& os, uint8_t rex, uint8_t reg,
Ian Rogersbf989802012-04-16 16:07:49 -070061 bool byte_operand, uint8_t size_override, RegFile reg_file) {
Ian Rogers706a10e2012-03-23 17:00:55 -070062 size_t reg_num = reg; // TODO: combine with REX.R on 64bit
Ian Rogersbf989802012-04-16 16:07:49 -070063 if (reg_file == GPR) {
64 DumpReg0(os, rex, reg_num, byte_operand, size_override);
65 } else if (reg_file == SSE) {
66 os << "xmm" << reg_num;
67 } else {
68 os << "mm" << reg_num;
69 }
Ian Rogers706a10e2012-03-23 17:00:55 -070070}
71
Ian Rogers7caad772012-03-30 01:07:54 -070072static void DumpBaseReg(std::ostream& os, uint8_t rex, uint8_t reg) {
Ian Rogers706a10e2012-03-23 17:00:55 -070073 size_t reg_num = reg; // TODO: combine with REX.B on 64bit
Ian Rogers7caad772012-03-30 01:07:54 -070074 DumpReg0(os, rex, reg_num, false, 0);
Ian Rogers706a10e2012-03-23 17:00:55 -070075}
76
Ian Rogers7caad772012-03-30 01:07:54 -070077static void DumpIndexReg(std::ostream& os, uint8_t rex, uint8_t reg) {
Ian Rogers706a10e2012-03-23 17:00:55 -070078 int reg_num = reg; // TODO: combine with REX.X on 64bit
Ian Rogers7caad772012-03-30 01:07:54 -070079 DumpReg0(os, rex, reg_num, false, 0);
Ian Rogers706a10e2012-03-23 17:00:55 -070080}
81
Elliott Hughes92301d92012-04-10 15:57:52 -070082enum SegmentPrefix {
83 kCs = 0x2e,
84 kSs = 0x36,
85 kDs = 0x3e,
86 kEs = 0x26,
87 kFs = 0x64,
88 kGs = 0x65,
89};
90
Ian Rogers706a10e2012-03-23 17:00:55 -070091static void DumpSegmentOverride(std::ostream& os, uint8_t segment_prefix) {
92 switch (segment_prefix) {
Elliott Hughes92301d92012-04-10 15:57:52 -070093 case kCs: os << "cs:"; break;
94 case kSs: os << "ss:"; break;
95 case kDs: os << "ds:"; break;
96 case kEs: os << "es:"; break;
97 case kFs: os << "fs:"; break;
98 case kGs: os << "gs:"; break;
Ian Rogers706a10e2012-03-23 17:00:55 -070099 default: break;
100 }
101}
102
103size_t DisassemblerX86::DumpInstruction(std::ostream& os, const uint8_t* instr) {
104 const uint8_t* begin_instr = instr;
105 bool have_prefixes = true;
106 uint8_t prefix[4] = {0, 0, 0, 0};
107 const char** modrm_opcodes = NULL;
108 do {
109 switch (*instr) {
Ian Rogers7caad772012-03-30 01:07:54 -0700110 // Group 1 - lock and repeat prefixes:
Ian Rogers706a10e2012-03-23 17:00:55 -0700111 case 0xF0:
112 case 0xF2:
113 case 0xF3:
114 prefix[0] = *instr;
115 break;
116 // Group 2 - segment override prefixes:
Elliott Hughes92301d92012-04-10 15:57:52 -0700117 case kCs:
118 case kSs:
119 case kDs:
120 case kEs:
121 case kFs:
122 case kGs:
Ian Rogers706a10e2012-03-23 17:00:55 -0700123 prefix[1] = *instr;
124 break;
125 // Group 3 - operand size override:
126 case 0x66:
127 prefix[2] = *instr;
128 break;
129 // Group 4 - address size override:
130 case 0x67:
131 prefix[3] = *instr;
132 break;
133 default:
134 have_prefixes = false;
135 break;
136 }
137 if (have_prefixes) {
138 instr++;
139 }
140 } while (have_prefixes);
141 uint8_t rex = (*instr >= 0x40 && *instr <= 0x4F) ? *instr : 0;
142 bool has_modrm = false;
143 bool reg_is_opcode = false;
144 size_t immediate_bytes = 0;
145 size_t branch_bytes = 0;
146 std::ostringstream opcode;
147 bool store = false; // stores to memory (ie rm is on the left)
148 bool load = false; // loads from memory (ie rm is on the right)
149 bool byte_operand = false;
150 bool ax = false; // implicit use of ax
jeffhaoe2962482012-06-28 11:29:57 -0700151 bool cx = false; // implicit use of cx
Ian Rogers706a10e2012-03-23 17:00:55 -0700152 bool reg_in_opcode = false; // low 3-bits of opcode encode register parameter
jeffhao703f2cd2012-07-13 17:25:52 -0700153 bool no_ops = false;
Ian Rogersbf989802012-04-16 16:07:49 -0700154 RegFile src_reg_file = GPR;
155 RegFile dst_reg_file = GPR;
Ian Rogers706a10e2012-03-23 17:00:55 -0700156 switch (*instr) {
157#define DISASSEMBLER_ENTRY(opname, \
158 rm8_r8, rm32_r32, \
159 r8_rm8, r32_rm32, \
160 ax8_i8, ax32_i32) \
161 case rm8_r8: opcode << #opname; store = true; has_modrm = true; byte_operand = true; break; \
162 case rm32_r32: opcode << #opname; store = true; has_modrm = true; break; \
163 case r8_rm8: opcode << #opname; load = true; has_modrm = true; byte_operand = true; break; \
164 case r32_rm32: opcode << #opname; load = true; has_modrm = true; break; \
165 case ax8_i8: opcode << #opname; ax = true; immediate_bytes = 1; byte_operand = true; break; \
166 case ax32_i32: opcode << #opname; ax = true; immediate_bytes = 4; break;
167
168DISASSEMBLER_ENTRY(add,
169 0x00 /* RegMem8/Reg8 */, 0x01 /* RegMem32/Reg32 */,
170 0x02 /* Reg8/RegMem8 */, 0x03 /* Reg32/RegMem32 */,
171 0x04 /* Rax8/imm8 opcode */, 0x05 /* Rax32/imm32 */)
172DISASSEMBLER_ENTRY(or,
173 0x08 /* RegMem8/Reg8 */, 0x09 /* RegMem32/Reg32 */,
174 0x0A /* Reg8/RegMem8 */, 0x0B /* Reg32/RegMem32 */,
175 0x0C /* Rax8/imm8 opcode */, 0x0D /* Rax32/imm32 */)
176DISASSEMBLER_ENTRY(adc,
177 0x10 /* RegMem8/Reg8 */, 0x11 /* RegMem32/Reg32 */,
178 0x12 /* Reg8/RegMem8 */, 0x13 /* Reg32/RegMem32 */,
179 0x14 /* Rax8/imm8 opcode */, 0x15 /* Rax32/imm32 */)
180DISASSEMBLER_ENTRY(sbb,
181 0x18 /* RegMem8/Reg8 */, 0x19 /* RegMem32/Reg32 */,
182 0x1A /* Reg8/RegMem8 */, 0x1B /* Reg32/RegMem32 */,
183 0x1C /* Rax8/imm8 opcode */, 0x1D /* Rax32/imm32 */)
184DISASSEMBLER_ENTRY(and,
185 0x20 /* RegMem8/Reg8 */, 0x21 /* RegMem32/Reg32 */,
186 0x22 /* Reg8/RegMem8 */, 0x23 /* Reg32/RegMem32 */,
187 0x24 /* Rax8/imm8 opcode */, 0x25 /* Rax32/imm32 */)
188DISASSEMBLER_ENTRY(sub,
189 0x28 /* RegMem8/Reg8 */, 0x29 /* RegMem32/Reg32 */,
190 0x2A /* Reg8/RegMem8 */, 0x2B /* Reg32/RegMem32 */,
191 0x2C /* Rax8/imm8 opcode */, 0x2D /* Rax32/imm32 */)
192DISASSEMBLER_ENTRY(xor,
193 0x30 /* RegMem8/Reg8 */, 0x31 /* RegMem32/Reg32 */,
194 0x32 /* Reg8/RegMem8 */, 0x33 /* Reg32/RegMem32 */,
195 0x34 /* Rax8/imm8 opcode */, 0x35 /* Rax32/imm32 */)
196DISASSEMBLER_ENTRY(cmp,
197 0x38 /* RegMem8/Reg8 */, 0x39 /* RegMem32/Reg32 */,
198 0x3A /* Reg8/RegMem8 */, 0x3B /* Reg32/RegMem32 */,
199 0x3C /* Rax8/imm8 opcode */, 0x3D /* Rax32/imm32 */)
200
201#undef DISASSEMBLER_ENTRY
202 case 0x50: case 0x51: case 0x52: case 0x53: case 0x54: case 0x55: case 0x56: case 0x57:
203 opcode << "push";
204 reg_in_opcode = true;
205 break;
206 case 0x58: case 0x59: case 0x5A: case 0x5B: case 0x5C: case 0x5D: case 0x5E: case 0x5F:
207 opcode << "pop";
208 reg_in_opcode = true;
209 break;
210 case 0x68: opcode << "push"; immediate_bytes = 4; break;
211 case 0x6A: opcode << "push"; immediate_bytes = 1; break;
212 case 0x70: case 0x71: case 0x72: case 0x73: case 0x74: case 0x75: case 0x76: case 0x77:
213 case 0x78: case 0x79: case 0x7A: case 0x7B: case 0x7C: case 0x7D: case 0x7E: case 0x7F:
214 static const char* condition_codes[] =
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700215 {"o", "no", "b/nae/c", "nb/ae/nc", "z/eq", "nz/ne", "be/na", "nbe/a",
216 "s", "ns", "p/pe", "np/po", "l/nge", "nl/ge", "le/ng", "nle/g"
Ian Rogers706a10e2012-03-23 17:00:55 -0700217 };
218 opcode << "j" << condition_codes[*instr & 0xF];
219 branch_bytes = 1;
220 break;
221 case 0x88: opcode << "mov"; store = true; has_modrm = true; byte_operand = true; break;
222 case 0x89: opcode << "mov"; store = true; has_modrm = true; break;
223 case 0x8A: opcode << "mov"; load = true; has_modrm = true; byte_operand = true; break;
224 case 0x8B: opcode << "mov"; load = true; has_modrm = true; break;
225
226 case 0x0F: // 2 byte extended opcode
227 instr++;
228 switch (*instr) {
Ian Rogers7caad772012-03-30 01:07:54 -0700229 case 0x10: case 0x11:
230 if (prefix[0] == 0xF2) {
231 opcode << "movsd";
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[0] == 0xF3) {
234 opcode << "movss";
jeffhaofdffdf82012-07-11 16:08:43 -0700235 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogers7caad772012-03-30 01:07:54 -0700236 } else if (prefix[2] == 0x66) {
237 opcode << "movupd";
jeffhaofdffdf82012-07-11 16:08:43 -0700238 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogers7caad772012-03-30 01:07:54 -0700239 } else {
240 opcode << "movups";
241 }
242 has_modrm = true;
Ian Rogersbf989802012-04-16 16:07:49 -0700243 src_reg_file = dst_reg_file = SSE;
Ian Rogers7caad772012-03-30 01:07:54 -0700244 load = *instr == 0x10;
245 store = !load;
246 break;
jeffhaofdffdf82012-07-11 16:08:43 -0700247 case 0x2A:
248 if (prefix[2] == 0x66) {
249 opcode << "cvtpi2pd";
250 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
251 } else if (prefix[0] == 0xF2) {
252 opcode << "cvtsi2sd";
253 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
254 } else if (prefix[0] == 0xF3) {
255 opcode << "cvtsi2ss";
256 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
257 } else {
258 opcode << "cvtpi2ps";
259 }
260 load = true;
261 has_modrm = true;
262 dst_reg_file = SSE;
263 break;
264 case 0x2C:
265 if (prefix[2] == 0x66) {
266 opcode << "cvttpd2pi";
267 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
268 } else if (prefix[0] == 0xF2) {
269 opcode << "cvttsd2si";
270 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
271 } else if (prefix[0] == 0xF3) {
272 opcode << "cvttss2si";
273 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
274 } else {
275 opcode << "cvttps2pi";
276 }
277 load = true;
278 has_modrm = true;
279 src_reg_file = SSE;
280 break;
281 case 0x2D:
282 if (prefix[2] == 0x66) {
283 opcode << "cvtpd2pi";
284 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
285 } else if (prefix[0] == 0xF2) {
286 opcode << "cvtsd2si";
287 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
288 } else if (prefix[0] == 0xF3) {
289 opcode << "cvtss2si";
290 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
291 } else {
292 opcode << "cvtps2pi";
293 }
294 load = true;
295 has_modrm = true;
296 src_reg_file = SSE;
297 break;
298 case 0x2E:
299 opcode << "u";
300 // FALLTHROUGH
301 case 0x2F:
302 if (prefix[2] == 0x66) {
303 opcode << "comisd";
304 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
305 } else {
306 opcode << "comiss";
307 }
308 has_modrm = true;
309 load = true;
310 src_reg_file = dst_reg_file = SSE;
311 break;
Ian Rogers706a10e2012-03-23 17:00:55 -0700312 case 0x38: // 3 byte extended opcode
313 opcode << StringPrintf("unknown opcode '0F 38 %02X'", *instr);
314 break;
315 case 0x3A: // 3 byte extended opcode
316 opcode << StringPrintf("unknown opcode '0F 3A %02X'", *instr);
317 break;
Ian Rogersbf989802012-04-16 16:07:49 -0700318 case 0x50: case 0x51: case 0x52: case 0x53: case 0x54: case 0x55: case 0x56: case 0x57:
319 case 0x58: case 0x59: case 0x5C: case 0x5D: case 0x5E: case 0x5F: {
320 switch (*instr) {
321 case 0x50: opcode << "movmsk"; break;
322 case 0x51: opcode << "sqrt"; break;
323 case 0x52: opcode << "rsqrt"; break;
324 case 0x53: opcode << "rcp"; break;
325 case 0x54: opcode << "and"; break;
326 case 0x55: opcode << "andn"; break;
327 case 0x56: opcode << "or"; break;
328 case 0x57: opcode << "xor"; break;
329 case 0x58: opcode << "add"; break;
330 case 0x59: opcode << "mul"; break;
331 case 0x5C: opcode << "sub"; break;
332 case 0x5D: opcode << "min"; break;
333 case 0x5E: opcode << "div"; break;
334 case 0x5F: opcode << "max"; break;
335 default: LOG(FATAL) << "Unreachable";
336 }
337 if (prefix[2] == 0x66) {
338 opcode << "pd";
jeffhaofdffdf82012-07-11 16:08:43 -0700339 prefix[2] = 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] == 0xF2) {
341 opcode << "sd";
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 if (prefix[0] == 0xF3) {
344 opcode << "ss";
jeffhaofdffdf82012-07-11 16:08:43 -0700345 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogersbf989802012-04-16 16:07:49 -0700346 } else {
347 opcode << "ps";
348 }
349 load = true;
350 has_modrm = true;
351 src_reg_file = dst_reg_file = SSE;
352 break;
353 }
354 case 0x5A:
355 if (prefix[2] == 0x66) {
356 opcode << "cvtpd2ps";
jeffhaofdffdf82012-07-11 16:08:43 -0700357 prefix[2] = 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] == 0xF2) {
359 opcode << "cvtsd2ss";
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 if (prefix[0] == 0xF3) {
362 opcode << "cvtss2sd";
jeffhaofdffdf82012-07-11 16:08:43 -0700363 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogersbf989802012-04-16 16:07:49 -0700364 } else {
365 opcode << "cvtps2pd";
366 }
367 load = true;
368 has_modrm = true;
369 src_reg_file = dst_reg_file = SSE;
370 break;
371 case 0x5B:
372 if (prefix[2] == 0x66) {
373 opcode << "cvtps2dq";
jeffhaofdffdf82012-07-11 16:08:43 -0700374 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogersbf989802012-04-16 16:07:49 -0700375 } else if (prefix[0] == 0xF2) {
376 opcode << "bad opcode F2 0F 5B";
377 } else if (prefix[0] == 0xF3) {
378 opcode << "cvttps2dq";
jeffhaofdffdf82012-07-11 16:08:43 -0700379 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogersbf989802012-04-16 16:07:49 -0700380 } else {
381 opcode << "cvtdq2ps";
382 }
383 load = true;
384 has_modrm = true;
385 src_reg_file = dst_reg_file = SSE;
386 break;
387 case 0x6E:
388 if (prefix[2] == 0x66) {
389 dst_reg_file = SSE;
jeffhaofdffdf82012-07-11 16:08:43 -0700390 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogersbf989802012-04-16 16:07:49 -0700391 } else {
392 dst_reg_file = MMX;
Ian Rogersbf989802012-04-16 16:07:49 -0700393 }
jeffhaofdffdf82012-07-11 16:08:43 -0700394 opcode << "movd";
Ian Rogersbf989802012-04-16 16:07:49 -0700395 load = true;
396 has_modrm = true;
397 break;
398 case 0x6F:
399 if (prefix[2] == 0x66) {
400 dst_reg_file = SSE;
401 opcode << "movdqa";
jeffhaofdffdf82012-07-11 16:08:43 -0700402 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogersbf989802012-04-16 16:07:49 -0700403 } else if (prefix[0] == 0xF3) {
404 dst_reg_file = SSE;
405 opcode << "movdqu";
jeffhaofdffdf82012-07-11 16:08:43 -0700406 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogersbf989802012-04-16 16:07:49 -0700407 } else {
408 dst_reg_file = MMX;
409 opcode << "movq";
410 }
411 load = true;
412 has_modrm = true;
413 break;
jeffhaofdffdf82012-07-11 16:08:43 -0700414 case 0x71:
415 if (prefix[2] == 0x66) {
416 dst_reg_file = SSE;
417 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
418 } else {
419 dst_reg_file = MMX;
420 }
421 static const char* x71_opcodes[] = {"unknown-71", "unknown-71", "psrlw", "unknown-71", "psraw", "unknown-71", "psllw", "unknown-71"};
422 modrm_opcodes = x71_opcodes;
423 reg_is_opcode = true;
424 has_modrm = true;
425 store = true;
426 immediate_bytes = 1;
427 break;
428 case 0x72:
429 if (prefix[2] == 0x66) {
430 dst_reg_file = SSE;
431 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
432 } else {
433 dst_reg_file = MMX;
434 }
435 static const char* x72_opcodes[] = {"unknown-72", "unknown-72", "psrld", "unknown-72", "psrad", "unknown-72", "pslld", "unknown-72"};
436 modrm_opcodes = x72_opcodes;
437 reg_is_opcode = true;
438 has_modrm = true;
439 store = true;
440 immediate_bytes = 1;
441 break;
442 case 0x73:
443 if (prefix[2] == 0x66) {
444 dst_reg_file = SSE;
445 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
446 } else {
447 dst_reg_file = MMX;
448 }
449 static const char* x73_opcodes[] = {"unknown-73", "unknown-73", "psrlq", "unknown-73", "unknown-73", "unknown-73", "psllq", "unknown-73"};
450 modrm_opcodes = x73_opcodes;
451 reg_is_opcode = true;
452 has_modrm = true;
453 store = true;
454 immediate_bytes = 1;
455 break;
456 case 0x7E:
457 if (prefix[2] == 0x66) {
458 src_reg_file = SSE;
459 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
460 } else {
461 src_reg_file = MMX;
462 }
463 opcode << "movd";
464 has_modrm = true;
465 store = true;
466 break;
Ian Rogers706a10e2012-03-23 17:00:55 -0700467 case 0x80: case 0x81: case 0x82: case 0x83: case 0x84: case 0x85: case 0x86: case 0x87:
468 case 0x88: case 0x89: case 0x8A: case 0x8B: case 0x8C: case 0x8D: case 0x8E: case 0x8F:
469 opcode << "j" << condition_codes[*instr & 0xF];
470 branch_bytes = 4;
471 break;
Ian Rogers7caad772012-03-30 01:07:54 -0700472 case 0x90: case 0x91: case 0x92: case 0x93: case 0x94: case 0x95: case 0x96: case 0x97:
473 case 0x98: case 0x99: case 0x9A: case 0x9B: case 0x9C: case 0x9D: case 0x9E: case 0x9F:
474 opcode << "set" << condition_codes[*instr & 0xF];
475 modrm_opcodes = NULL;
476 reg_is_opcode = true;
477 has_modrm = true;
478 store = true;
479 break;
jeffhao703f2cd2012-07-13 17:25:52 -0700480 case 0xAE:
481 if (prefix[0] == 0xF3) {
Ian Rogers5e588b32013-02-21 15:05:09 -0800482 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
jeffhao703f2cd2012-07-13 17:25:52 -0700483 static const char* xAE_opcodes[] = {"rdfsbase", "rdgsbase", "wrfsbase", "wrgsbase", "unknown-AE", "unknown-AE", "unknown-AE", "unknown-AE"};
484 modrm_opcodes = xAE_opcodes;
485 reg_is_opcode = true;
486 has_modrm = true;
487 uint8_t reg_or_opcode = (instr[1] >> 3) & 7;
488 switch (reg_or_opcode) {
489 case 0:
490 prefix[1] = kFs;
491 load = true;
492 break;
493 case 1:
494 prefix[1] = kGs;
495 load = true;
496 break;
497 case 2:
498 prefix[1] = kFs;
499 store = true;
500 break;
501 case 3:
502 prefix[1] = kGs;
503 store = true;
504 break;
505 default:
506 load = true;
507 break;
508 }
509 } else {
510 static const char* xAE_opcodes[] = {"unknown-AE", "unknown-AE", "unknown-AE", "unknown-AE", "unknown-AE", "lfence", "mfence", "sfence"};
511 modrm_opcodes = xAE_opcodes;
512 reg_is_opcode = true;
513 has_modrm = true;
514 load = true;
515 no_ops = true;
516 }
517 break;
jeffhao83025762012-08-02 11:08:56 -0700518 case 0xB1: opcode << "cmpxchg"; has_modrm = true; store = true; break;
Ian Rogers7caad772012-03-30 01:07:54 -0700519 case 0xB6: opcode << "movzxb"; has_modrm = true; load = true; break;
520 case 0xB7: opcode << "movzxw"; has_modrm = true; load = true; break;
jeffhao854029c2012-07-23 17:31:30 -0700521 case 0xBE: opcode << "movsxb"; has_modrm = true; load = true; break;
522 case 0xBF: opcode << "movsxw"; has_modrm = true; load = true; break;
Vladimir Marko70b797d2013-12-03 15:25:24 +0000523 case 0xC7:
524 static const char* x0FxC7_opcodes[] = { "unknown-0f-c7", "cmpxchg8b", "unknown-0f-c7", "unknown-0f-c7", "unknown-0f-c7", "unknown-0f-c7", "unknown-0f-c7", "unknown-0f-c7" };
525 modrm_opcodes = x0FxC7_opcodes;
526 has_modrm = true;
527 reg_is_opcode = true;
528 store = true;
529 break;
Vladimir Markoa8b4caf2013-10-24 15:08:57 +0100530 case 0xC8: case 0xC9: case 0xCA: case 0xCB: case 0xCC: case 0xCD: case 0xCE: case 0xCF:
531 opcode << "bswap";
532 reg_in_opcode = true;
533 break;
Ian Rogers706a10e2012-03-23 17:00:55 -0700534 default:
535 opcode << StringPrintf("unknown opcode '0F %02X'", *instr);
536 break;
537 }
538 break;
539 case 0x80: case 0x81: case 0x82: case 0x83:
540 static const char* x80_opcodes[] = {"add", "or", "adc", "sbb", "and", "sub", "xor", "cmp"};
541 modrm_opcodes = x80_opcodes;
542 has_modrm = true;
543 reg_is_opcode = true;
544 store = true;
545 byte_operand = (*instr & 1) == 0;
546 immediate_bytes = *instr == 0x81 ? 4 : 1;
547 break;
jeffhao703f2cd2012-07-13 17:25:52 -0700548 case 0x84: case 0x85:
549 opcode << "test";
550 has_modrm = true;
551 load = true;
552 byte_operand = (*instr & 1) == 0;
553 break;
Ian Rogers7caad772012-03-30 01:07:54 -0700554 case 0x8D:
555 opcode << "lea";
556 has_modrm = true;
557 load = true;
558 break;
jeffhao703f2cd2012-07-13 17:25:52 -0700559 case 0x8F:
560 opcode << "pop";
561 has_modrm = true;
562 reg_is_opcode = true;
563 store = true;
564 break;
Ian Rogers706a10e2012-03-23 17:00:55 -0700565 case 0xB0: case 0xB1: case 0xB2: case 0xB3: case 0xB4: case 0xB5: case 0xB6: case 0xB7:
566 opcode << "mov";
567 immediate_bytes = 1;
568 reg_in_opcode = true;
569 break;
570 case 0xB8: case 0xB9: case 0xBA: case 0xBB: case 0xBC: case 0xBD: case 0xBE: case 0xBF:
571 opcode << "mov";
572 immediate_bytes = 4;
573 reg_in_opcode = true;
574 break;
Ian Rogers7caad772012-03-30 01:07:54 -0700575 case 0xC0: case 0xC1:
jeffhaoe2962482012-06-28 11:29:57 -0700576 case 0xD0: case 0xD1: case 0xD2: case 0xD3:
Ian Rogers7caad772012-03-30 01:07:54 -0700577 static const char* shift_opcodes[] =
578 {"rol", "ror", "rcl", "rcr", "shl", "shr", "unknown-shift", "sar"};
579 modrm_opcodes = shift_opcodes;
580 has_modrm = true;
581 reg_is_opcode = true;
582 store = true;
Elliott Hughes16b5c292012-04-16 20:37:16 -0700583 immediate_bytes = ((*instr & 0xf0) == 0xc0) ? 1 : 0;
jeffhaoe2962482012-06-28 11:29:57 -0700584 cx = (*instr == 0xD2) || (*instr == 0xD3);
585 byte_operand = (*instr == 0xC0);
Ian Rogers7caad772012-03-30 01:07:54 -0700586 break;
Ian Rogers706a10e2012-03-23 17:00:55 -0700587 case 0xC3: opcode << "ret"; break;
Elliott Hughes0589ca92012-04-09 18:26:20 -0700588 case 0xC7:
589 static const char* c7_opcodes[] = {"mov", "unknown-c7", "unknown-c7", "unknown-c7", "unknown-c7", "unknown-c7", "unknown-c7", "unknown-c7"};
590 modrm_opcodes = c7_opcodes;
591 store = true;
592 immediate_bytes = 4;
593 has_modrm = true;
594 reg_is_opcode = true;
595 break;
Ian Rogers7caad772012-03-30 01:07:54 -0700596 case 0xCC: opcode << "int 3"; break;
597 case 0xE8: opcode << "call"; branch_bytes = 4; break;
Ian Rogers706a10e2012-03-23 17:00:55 -0700598 case 0xE9: opcode << "jmp"; branch_bytes = 4; break;
599 case 0xEB: opcode << "jmp"; branch_bytes = 1; break;
jeffhao77ae36b2012-08-07 14:18:16 -0700600 case 0xF5: opcode << "cmc"; break;
jeffhao174651d2012-04-19 15:27:22 -0700601 case 0xF6: case 0xF7:
602 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 /"};
603 modrm_opcodes = f7_opcodes;
604 has_modrm = true;
605 reg_is_opcode = true;
606 store = true;
607 immediate_bytes = ((instr[1] & 0x38) == 0) ? 1 : 0;
608 break;
Ian Rogers706a10e2012-03-23 17:00:55 -0700609 case 0xFF:
610 static const char* ff_opcodes[] = {"inc", "dec", "call", "call", "jmp", "jmp", "push", "unknown-ff"};
611 modrm_opcodes = ff_opcodes;
612 has_modrm = true;
613 reg_is_opcode = true;
614 load = true;
615 break;
616 default:
617 opcode << StringPrintf("unknown opcode '%02X'", *instr);
618 break;
619 }
620 std::ostringstream args;
621 if (reg_in_opcode) {
622 DCHECK(!has_modrm);
Ian Rogersbf989802012-04-16 16:07:49 -0700623 DumpReg(args, rex, *instr & 0x7, false, prefix[2], GPR);
Ian Rogers706a10e2012-03-23 17:00:55 -0700624 }
625 instr++;
Elliott Hughes92301d92012-04-10 15:57:52 -0700626 uint32_t address_bits = 0;
Ian Rogers706a10e2012-03-23 17:00:55 -0700627 if (has_modrm) {
628 uint8_t modrm = *instr;
629 instr++;
630 uint8_t mod = modrm >> 6;
631 uint8_t reg_or_opcode = (modrm >> 3) & 7;
632 uint8_t rm = modrm & 7;
633 std::ostringstream address;
634 if (mod == 0 && rm == 5) { // fixed address
Elliott Hughes92301d92012-04-10 15:57:52 -0700635 address_bits = *reinterpret_cast<const uint32_t*>(instr);
636 address << StringPrintf("[0x%x]", address_bits);
Ian Rogers706a10e2012-03-23 17:00:55 -0700637 instr += 4;
638 } else if (rm == 4 && mod != 3) { // SIB
639 uint8_t sib = *instr;
640 instr++;
641 uint8_t ss = (sib >> 6) & 3;
642 uint8_t index = (sib >> 3) & 7;
643 uint8_t base = sib & 7;
644 address << "[";
645 if (base != 5 || mod != 0) {
Ian Rogers7caad772012-03-30 01:07:54 -0700646 DumpBaseReg(address, rex, base);
Ian Rogers706a10e2012-03-23 17:00:55 -0700647 if (index != 4) {
648 address << " + ";
649 }
650 }
651 if (index != 4) {
Ian Rogers7caad772012-03-30 01:07:54 -0700652 DumpIndexReg(address, rex, index);
Ian Rogers706a10e2012-03-23 17:00:55 -0700653 if (ss != 0) {
654 address << StringPrintf(" * %d", 1 << ss);
655 }
656 }
657 if (mod == 1) {
658 address << StringPrintf(" + %d", *reinterpret_cast<const int8_t*>(instr));
659 instr++;
660 } else if (mod == 2) {
661 address << StringPrintf(" + %d", *reinterpret_cast<const int32_t*>(instr));
662 instr += 4;
663 }
664 address << "]";
665 } else {
Ian Rogersbf989802012-04-16 16:07:49 -0700666 if (mod == 3) {
jeffhao703f2cd2012-07-13 17:25:52 -0700667 if (!no_ops) {
668 DumpReg(address, rex, rm, byte_operand, prefix[2], load ? src_reg_file : dst_reg_file);
669 }
Ian Rogersbf989802012-04-16 16:07:49 -0700670 } else {
Ian Rogers706a10e2012-03-23 17:00:55 -0700671 address << "[";
Ian Rogersbf989802012-04-16 16:07:49 -0700672 DumpBaseReg(address, rex, rm);
673 if (mod == 1) {
674 address << StringPrintf(" + %d", *reinterpret_cast<const int8_t*>(instr));
675 instr++;
676 } else if (mod == 2) {
677 address << StringPrintf(" + %d", *reinterpret_cast<const int32_t*>(instr));
678 instr += 4;
679 }
Ian Rogers706a10e2012-03-23 17:00:55 -0700680 address << "]";
681 }
682 }
683
Ian Rogers7caad772012-03-30 01:07:54 -0700684 if (reg_is_opcode && modrm_opcodes != NULL) {
Ian Rogers706a10e2012-03-23 17:00:55 -0700685 opcode << modrm_opcodes[reg_or_opcode];
686 }
687 if (load) {
688 if (!reg_is_opcode) {
Ian Rogersbf989802012-04-16 16:07:49 -0700689 DumpReg(args, rex, reg_or_opcode, byte_operand, prefix[2], dst_reg_file);
Ian Rogers706a10e2012-03-23 17:00:55 -0700690 args << ", ";
691 }
692 DumpSegmentOverride(args, prefix[1]);
693 args << address.str();
694 } else {
695 DCHECK(store);
696 DumpSegmentOverride(args, prefix[1]);
697 args << address.str();
698 if (!reg_is_opcode) {
699 args << ", ";
Ian Rogersbf989802012-04-16 16:07:49 -0700700 DumpReg(args, rex, reg_or_opcode, byte_operand, prefix[2], src_reg_file);
Ian Rogers706a10e2012-03-23 17:00:55 -0700701 }
702 }
703 }
704 if (ax) {
jeffhaofdffdf82012-07-11 16:08:43 -0700705 // If this opcode implicitly uses ax, ax is always the first arg.
Ian Rogersbf989802012-04-16 16:07:49 -0700706 DumpReg(args, rex, 0 /* EAX */, byte_operand, prefix[2], GPR);
Ian Rogers706a10e2012-03-23 17:00:55 -0700707 }
jeffhaoe2962482012-06-28 11:29:57 -0700708 if (cx) {
709 args << ", ";
710 DumpReg(args, rex, 1 /* ECX */, true, prefix[2], GPR);
711 }
Ian Rogers706a10e2012-03-23 17:00:55 -0700712 if (immediate_bytes > 0) {
jeffhaoe2962482012-06-28 11:29:57 -0700713 if (has_modrm || reg_in_opcode || ax || cx) {
Ian Rogers706a10e2012-03-23 17:00:55 -0700714 args << ", ";
715 }
716 if (immediate_bytes == 1) {
717 args << StringPrintf("%d", *reinterpret_cast<const int8_t*>(instr));
718 instr++;
719 } else {
720 CHECK_EQ(immediate_bytes, 4u);
721 args << StringPrintf("%d", *reinterpret_cast<const int32_t*>(instr));
722 instr += 4;
723 }
724 } else if (branch_bytes > 0) {
725 DCHECK(!has_modrm);
726 int32_t displacement;
727 if (branch_bytes == 1) {
728 displacement = *reinterpret_cast<const int8_t*>(instr);
729 instr++;
730 } else {
731 CHECK_EQ(branch_bytes, 4u);
732 displacement = *reinterpret_cast<const int32_t*>(instr);
733 instr += 4;
734 }
Elliott Hughes14178a92012-04-16 17:24:51 -0700735 args << StringPrintf("%+d (%p)", displacement, instr + displacement);
Ian Rogers706a10e2012-03-23 17:00:55 -0700736 }
Elliott Hughes92301d92012-04-10 15:57:52 -0700737 if (prefix[1] == kFs) {
738 args << " ; ";
739 Thread::DumpThreadOffset(args, address_bits, 4);
740 }
Elliott Hughes28fa76d2012-04-09 17:31:46 -0700741 std::stringstream hex;
Ian Rogers706a10e2012-03-23 17:00:55 -0700742 for (size_t i = 0; begin_instr + i < instr; ++i) {
Elliott Hughes28fa76d2012-04-09 17:31:46 -0700743 hex << StringPrintf("%02X", begin_instr[i]);
Ian Rogers706a10e2012-03-23 17:00:55 -0700744 }
Ian Rogers5e588b32013-02-21 15:05:09 -0800745 std::stringstream prefixed_opcode;
746 switch (prefix[0]) {
747 case 0xF0: prefixed_opcode << "lock "; break;
748 case 0xF2: prefixed_opcode << "repne "; break;
749 case 0xF3: prefixed_opcode << "repe "; break;
750 case 0: break;
751 default: LOG(FATAL) << "Unreachable";
752 }
753 prefixed_opcode << opcode.str();
754 os << StringPrintf("%p: %22s \t%-7s ", begin_instr, hex.str().c_str(),
755 prefixed_opcode.str().c_str())
756 << args.str() << '\n';
Ian Rogers706a10e2012-03-23 17:00:55 -0700757 return instr - begin_instr;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700758} // NOLINT(readability/fn_size)
Ian Rogers706a10e2012-03-23 17:00:55 -0700759
760} // namespace x86
761} // namespace art