blob: ab0ee52205d82f89abeea5f11227d1f7fad6d19a [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
Ian Rogersb23a7722012-10-09 16:54:26 -070028size_t DisassemblerX86::Dump(std::ostream& os, const uint8_t* begin) {
29 return DumpInstruction(os, begin);
30}
31
Ian Rogers706a10e2012-03-23 17:00:55 -070032void DisassemblerX86::Dump(std::ostream& os, const uint8_t* begin, const uint8_t* end) {
33 size_t length = 0;
34 for (const uint8_t* cur = begin; cur < end; cur += length) {
35 length = DumpInstruction(os, cur);
36 }
37}
38
39static const char* gReg8Names[] = { "al", "cl", "dl", "bl", "ah", "ch", "dh", "bh" };
jeffhao703f2cd2012-07-13 17:25:52 -070040static const char* gReg16Names[] = { "ax", "cx", "dx", "bx", "sp", "bp", "si", "di" };
41static const char* gReg32Names[] = { "eax", "ecx", "edx", "ebx", "esp", "ebp", "esi", "edi" };
Ian Rogers38e12032014-03-14 14:06:14 -070042static const char* gReg64Names[] = {
43 "rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi",
44 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
45};
Ian Rogers706a10e2012-03-23 17:00:55 -070046
Ian Rogers38e12032014-03-14 14:06:14 -070047static void DumpReg0(std::ostream& os, uint8_t rex, size_t reg,
Ian Rogers706a10e2012-03-23 17:00:55 -070048 bool byte_operand, uint8_t size_override) {
Ian Rogers38e12032014-03-14 14:06:14 -070049 DCHECK_LT(reg, (rex == 0) ? 8u : 16u);
50 bool rex_w = (rex & 0b1000) != 0;
51 size_t size = byte_operand ? 1 : (size_override == 0x66 ? 2 : (rex_w ? 8 :4));
Ian Rogers706a10e2012-03-23 17:00:55 -070052 switch (size) {
53 case 1: os << gReg8Names[reg]; break;
54 case 2: os << gReg16Names[reg]; break;
55 case 4: os << gReg32Names[reg]; break;
Ian Rogers38e12032014-03-14 14:06:14 -070056 case 8: os << gReg64Names[reg]; break;
Ian Rogers706a10e2012-03-23 17:00:55 -070057 default: LOG(FATAL) << "unexpected size " << size;
58 }
59}
60
Ian Rogersbf989802012-04-16 16:07:49 -070061enum RegFile { GPR, MMX, SSE };
62
Ian Rogers706a10e2012-03-23 17:00:55 -070063static void DumpReg(std::ostream& os, uint8_t rex, uint8_t reg,
Ian Rogersbf989802012-04-16 16:07:49 -070064 bool byte_operand, uint8_t size_override, RegFile reg_file) {
Ian Rogers38e12032014-03-14 14:06:14 -070065 bool rex_r = (rex & 0b0100) != 0;
66 size_t reg_num = rex_r ? (reg + 8) : reg;
Ian Rogersbf989802012-04-16 16:07:49 -070067 if (reg_file == GPR) {
68 DumpReg0(os, rex, reg_num, byte_operand, size_override);
69 } else if (reg_file == SSE) {
70 os << "xmm" << reg_num;
71 } else {
72 os << "mm" << reg_num;
73 }
Ian Rogers706a10e2012-03-23 17:00:55 -070074}
75
Ian Rogers7caad772012-03-30 01:07:54 -070076static void DumpBaseReg(std::ostream& os, uint8_t rex, uint8_t reg) {
Ian Rogers38e12032014-03-14 14:06:14 -070077 bool rex_b = (rex & 0b0001) != 0;
78 size_t reg_num = rex_b ? (reg + 8) : reg;
Ian Rogers7caad772012-03-30 01:07:54 -070079 DumpReg0(os, rex, reg_num, false, 0);
Ian Rogers706a10e2012-03-23 17:00:55 -070080}
81
Ian Rogers7caad772012-03-30 01:07:54 -070082static void DumpIndexReg(std::ostream& os, uint8_t rex, uint8_t reg) {
Ian Rogers38e12032014-03-14 14:06:14 -070083 bool rex_x = (rex & 0b0010) != 0;
84 uint8_t reg_num = rex_x ? (reg + 8) : reg;
Ian Rogers7caad772012-03-30 01:07:54 -070085 DumpReg0(os, rex, reg_num, false, 0);
Ian Rogers706a10e2012-03-23 17:00:55 -070086}
87
Elliott Hughes92301d92012-04-10 15:57:52 -070088enum SegmentPrefix {
89 kCs = 0x2e,
90 kSs = 0x36,
91 kDs = 0x3e,
92 kEs = 0x26,
93 kFs = 0x64,
94 kGs = 0x65,
95};
96
Ian Rogers706a10e2012-03-23 17:00:55 -070097static void DumpSegmentOverride(std::ostream& os, uint8_t segment_prefix) {
98 switch (segment_prefix) {
Elliott Hughes92301d92012-04-10 15:57:52 -070099 case kCs: os << "cs:"; break;
100 case kSs: os << "ss:"; break;
101 case kDs: os << "ds:"; break;
102 case kEs: os << "es:"; break;
103 case kFs: os << "fs:"; break;
104 case kGs: os << "gs:"; break;
Ian Rogers706a10e2012-03-23 17:00:55 -0700105 default: break;
106 }
107}
108
109size_t DisassemblerX86::DumpInstruction(std::ostream& os, const uint8_t* instr) {
110 const uint8_t* begin_instr = instr;
111 bool have_prefixes = true;
112 uint8_t prefix[4] = {0, 0, 0, 0};
113 const char** modrm_opcodes = NULL;
114 do {
115 switch (*instr) {
Ian Rogers7caad772012-03-30 01:07:54 -0700116 // Group 1 - lock and repeat prefixes:
Ian Rogers706a10e2012-03-23 17:00:55 -0700117 case 0xF0:
118 case 0xF2:
119 case 0xF3:
120 prefix[0] = *instr;
121 break;
122 // Group 2 - segment override prefixes:
Elliott Hughes92301d92012-04-10 15:57:52 -0700123 case kCs:
124 case kSs:
125 case kDs:
126 case kEs:
127 case kFs:
128 case kGs:
Ian Rogers706a10e2012-03-23 17:00:55 -0700129 prefix[1] = *instr;
130 break;
131 // Group 3 - operand size override:
132 case 0x66:
133 prefix[2] = *instr;
134 break;
135 // Group 4 - address size override:
136 case 0x67:
137 prefix[3] = *instr;
138 break;
139 default:
140 have_prefixes = false;
141 break;
142 }
143 if (have_prefixes) {
144 instr++;
145 }
146 } while (have_prefixes);
Ian Rogers38e12032014-03-14 14:06:14 -0700147 uint8_t rex = (supports_rex_ && (*instr >= 0x40) && (*instr <= 0x4F)) ? *instr : 0;
Ian Rogers706a10e2012-03-23 17:00:55 -0700148 bool has_modrm = false;
149 bool reg_is_opcode = false;
150 size_t immediate_bytes = 0;
151 size_t branch_bytes = 0;
152 std::ostringstream opcode;
153 bool store = false; // stores to memory (ie rm is on the left)
154 bool load = false; // loads from memory (ie rm is on the right)
155 bool byte_operand = false;
156 bool ax = false; // implicit use of ax
jeffhaoe2962482012-06-28 11:29:57 -0700157 bool cx = false; // implicit use of cx
Ian Rogers706a10e2012-03-23 17:00:55 -0700158 bool reg_in_opcode = false; // low 3-bits of opcode encode register parameter
jeffhao703f2cd2012-07-13 17:25:52 -0700159 bool no_ops = false;
Ian Rogersbf989802012-04-16 16:07:49 -0700160 RegFile src_reg_file = GPR;
161 RegFile dst_reg_file = GPR;
Ian Rogers706a10e2012-03-23 17:00:55 -0700162 switch (*instr) {
163#define DISASSEMBLER_ENTRY(opname, \
164 rm8_r8, rm32_r32, \
165 r8_rm8, r32_rm32, \
166 ax8_i8, ax32_i32) \
167 case rm8_r8: opcode << #opname; store = true; has_modrm = true; byte_operand = true; break; \
168 case rm32_r32: opcode << #opname; store = true; has_modrm = true; break; \
169 case r8_rm8: opcode << #opname; load = true; has_modrm = true; byte_operand = true; break; \
170 case r32_rm32: opcode << #opname; load = true; has_modrm = true; break; \
171 case ax8_i8: opcode << #opname; ax = true; immediate_bytes = 1; byte_operand = true; break; \
172 case ax32_i32: opcode << #opname; ax = true; immediate_bytes = 4; break;
173
174DISASSEMBLER_ENTRY(add,
175 0x00 /* RegMem8/Reg8 */, 0x01 /* RegMem32/Reg32 */,
176 0x02 /* Reg8/RegMem8 */, 0x03 /* Reg32/RegMem32 */,
177 0x04 /* Rax8/imm8 opcode */, 0x05 /* Rax32/imm32 */)
178DISASSEMBLER_ENTRY(or,
179 0x08 /* RegMem8/Reg8 */, 0x09 /* RegMem32/Reg32 */,
180 0x0A /* Reg8/RegMem8 */, 0x0B /* Reg32/RegMem32 */,
181 0x0C /* Rax8/imm8 opcode */, 0x0D /* Rax32/imm32 */)
182DISASSEMBLER_ENTRY(adc,
183 0x10 /* RegMem8/Reg8 */, 0x11 /* RegMem32/Reg32 */,
184 0x12 /* Reg8/RegMem8 */, 0x13 /* Reg32/RegMem32 */,
185 0x14 /* Rax8/imm8 opcode */, 0x15 /* Rax32/imm32 */)
186DISASSEMBLER_ENTRY(sbb,
187 0x18 /* RegMem8/Reg8 */, 0x19 /* RegMem32/Reg32 */,
188 0x1A /* Reg8/RegMem8 */, 0x1B /* Reg32/RegMem32 */,
189 0x1C /* Rax8/imm8 opcode */, 0x1D /* Rax32/imm32 */)
190DISASSEMBLER_ENTRY(and,
191 0x20 /* RegMem8/Reg8 */, 0x21 /* RegMem32/Reg32 */,
192 0x22 /* Reg8/RegMem8 */, 0x23 /* Reg32/RegMem32 */,
193 0x24 /* Rax8/imm8 opcode */, 0x25 /* Rax32/imm32 */)
194DISASSEMBLER_ENTRY(sub,
195 0x28 /* RegMem8/Reg8 */, 0x29 /* RegMem32/Reg32 */,
196 0x2A /* Reg8/RegMem8 */, 0x2B /* Reg32/RegMem32 */,
197 0x2C /* Rax8/imm8 opcode */, 0x2D /* Rax32/imm32 */)
198DISASSEMBLER_ENTRY(xor,
199 0x30 /* RegMem8/Reg8 */, 0x31 /* RegMem32/Reg32 */,
200 0x32 /* Reg8/RegMem8 */, 0x33 /* Reg32/RegMem32 */,
201 0x34 /* Rax8/imm8 opcode */, 0x35 /* Rax32/imm32 */)
202DISASSEMBLER_ENTRY(cmp,
203 0x38 /* RegMem8/Reg8 */, 0x39 /* RegMem32/Reg32 */,
204 0x3A /* Reg8/RegMem8 */, 0x3B /* Reg32/RegMem32 */,
205 0x3C /* Rax8/imm8 opcode */, 0x3D /* Rax32/imm32 */)
206
207#undef DISASSEMBLER_ENTRY
208 case 0x50: case 0x51: case 0x52: case 0x53: case 0x54: case 0x55: case 0x56: case 0x57:
209 opcode << "push";
210 reg_in_opcode = true;
211 break;
212 case 0x58: case 0x59: case 0x5A: case 0x5B: case 0x5C: case 0x5D: case 0x5E: case 0x5F:
213 opcode << "pop";
214 reg_in_opcode = true;
215 break;
216 case 0x68: opcode << "push"; immediate_bytes = 4; break;
Mark Mendelld19b55a2013-12-12 09:55:34 -0800217 case 0x69: opcode << "imul"; load = true; has_modrm = true; immediate_bytes = 4; break;
Ian Rogers706a10e2012-03-23 17:00:55 -0700218 case 0x6A: opcode << "push"; immediate_bytes = 1; break;
Mark Mendelld19b55a2013-12-12 09:55:34 -0800219 case 0x6B: opcode << "imul"; load = true; has_modrm = true; immediate_bytes = 1; break;
Ian Rogers706a10e2012-03-23 17:00:55 -0700220 case 0x70: case 0x71: case 0x72: case 0x73: case 0x74: case 0x75: case 0x76: case 0x77:
221 case 0x78: case 0x79: case 0x7A: case 0x7B: case 0x7C: case 0x7D: case 0x7E: case 0x7F:
222 static const char* condition_codes[] =
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700223 {"o", "no", "b/nae/c", "nb/ae/nc", "z/eq", "nz/ne", "be/na", "nbe/a",
224 "s", "ns", "p/pe", "np/po", "l/nge", "nl/ge", "le/ng", "nle/g"
Ian Rogers706a10e2012-03-23 17:00:55 -0700225 };
226 opcode << "j" << condition_codes[*instr & 0xF];
227 branch_bytes = 1;
228 break;
229 case 0x88: opcode << "mov"; store = true; has_modrm = true; byte_operand = true; break;
230 case 0x89: opcode << "mov"; store = true; has_modrm = true; break;
231 case 0x8A: opcode << "mov"; load = true; has_modrm = true; byte_operand = true; break;
232 case 0x8B: opcode << "mov"; load = true; has_modrm = true; break;
233
234 case 0x0F: // 2 byte extended opcode
235 instr++;
236 switch (*instr) {
Ian Rogers7caad772012-03-30 01:07:54 -0700237 case 0x10: case 0x11:
238 if (prefix[0] == 0xF2) {
239 opcode << "movsd";
jeffhaofdffdf82012-07-11 16:08:43 -0700240 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogers7caad772012-03-30 01:07:54 -0700241 } else if (prefix[0] == 0xF3) {
242 opcode << "movss";
jeffhaofdffdf82012-07-11 16:08:43 -0700243 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogers7caad772012-03-30 01:07:54 -0700244 } else if (prefix[2] == 0x66) {
245 opcode << "movupd";
jeffhaofdffdf82012-07-11 16:08:43 -0700246 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogers7caad772012-03-30 01:07:54 -0700247 } else {
248 opcode << "movups";
249 }
250 has_modrm = true;
Ian Rogersbf989802012-04-16 16:07:49 -0700251 src_reg_file = dst_reg_file = SSE;
Ian Rogers7caad772012-03-30 01:07:54 -0700252 load = *instr == 0x10;
253 store = !load;
254 break;
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800255 case 0x12: case 0x13:
256 if (prefix[2] == 0x66) {
257 opcode << "movlpd";
258 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
259 } else if (prefix[0] == 0) {
260 opcode << "movlps";
261 }
262 has_modrm = true;
263 src_reg_file = dst_reg_file = SSE;
264 load = *instr == 0x12;
265 store = !load;
266 break;
267 case 0x16: case 0x17:
268 if (prefix[2] == 0x66) {
269 opcode << "movhpd";
270 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
271 } else if (prefix[0] == 0) {
272 opcode << "movhps";
273 }
274 has_modrm = true;
275 src_reg_file = dst_reg_file = SSE;
276 load = *instr == 0x16;
277 store = !load;
278 break;
279 case 0x28: case 0x29:
280 if (prefix[2] == 0x66) {
281 opcode << "movapd";
282 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
283 } else if (prefix[0] == 0) {
284 opcode << "movaps";
285 }
286 has_modrm = true;
287 src_reg_file = dst_reg_file = SSE;
288 load = *instr == 0x28;
289 store = !load;
290 break;
jeffhaofdffdf82012-07-11 16:08:43 -0700291 case 0x2A:
292 if (prefix[2] == 0x66) {
293 opcode << "cvtpi2pd";
294 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
295 } else if (prefix[0] == 0xF2) {
296 opcode << "cvtsi2sd";
297 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
298 } else if (prefix[0] == 0xF3) {
299 opcode << "cvtsi2ss";
300 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
301 } else {
302 opcode << "cvtpi2ps";
303 }
304 load = true;
305 has_modrm = true;
306 dst_reg_file = SSE;
307 break;
308 case 0x2C:
309 if (prefix[2] == 0x66) {
310 opcode << "cvttpd2pi";
311 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
312 } else if (prefix[0] == 0xF2) {
313 opcode << "cvttsd2si";
314 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
315 } else if (prefix[0] == 0xF3) {
316 opcode << "cvttss2si";
317 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
318 } else {
319 opcode << "cvttps2pi";
320 }
321 load = true;
322 has_modrm = true;
323 src_reg_file = SSE;
324 break;
325 case 0x2D:
326 if (prefix[2] == 0x66) {
327 opcode << "cvtpd2pi";
328 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
329 } else if (prefix[0] == 0xF2) {
330 opcode << "cvtsd2si";
331 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
332 } else if (prefix[0] == 0xF3) {
333 opcode << "cvtss2si";
334 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
335 } else {
336 opcode << "cvtps2pi";
337 }
338 load = true;
339 has_modrm = true;
340 src_reg_file = SSE;
341 break;
342 case 0x2E:
343 opcode << "u";
344 // FALLTHROUGH
345 case 0x2F:
346 if (prefix[2] == 0x66) {
347 opcode << "comisd";
348 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
349 } else {
350 opcode << "comiss";
351 }
352 has_modrm = true;
353 load = true;
354 src_reg_file = dst_reg_file = SSE;
355 break;
Ian Rogers706a10e2012-03-23 17:00:55 -0700356 case 0x38: // 3 byte extended opcode
357 opcode << StringPrintf("unknown opcode '0F 38 %02X'", *instr);
358 break;
359 case 0x3A: // 3 byte extended opcode
360 opcode << StringPrintf("unknown opcode '0F 3A %02X'", *instr);
361 break;
Razvan A Lupusorubd288c22013-12-20 17:27:23 -0800362 case 0x40: case 0x41: case 0x42: case 0x43: case 0x44: case 0x45: case 0x46: case 0x47:
363 case 0x48: case 0x49: case 0x4A: case 0x4B: case 0x4C: case 0x4D: case 0x4E: case 0x4F:
364 opcode << "cmov" << condition_codes[*instr & 0xF];
365 has_modrm = true;
366 load = true;
367 break;
Ian Rogersbf989802012-04-16 16:07:49 -0700368 case 0x50: case 0x51: case 0x52: case 0x53: case 0x54: case 0x55: case 0x56: case 0x57:
369 case 0x58: case 0x59: case 0x5C: case 0x5D: case 0x5E: case 0x5F: {
370 switch (*instr) {
371 case 0x50: opcode << "movmsk"; break;
372 case 0x51: opcode << "sqrt"; break;
373 case 0x52: opcode << "rsqrt"; break;
374 case 0x53: opcode << "rcp"; break;
375 case 0x54: opcode << "and"; break;
376 case 0x55: opcode << "andn"; break;
377 case 0x56: opcode << "or"; break;
378 case 0x57: opcode << "xor"; break;
379 case 0x58: opcode << "add"; break;
380 case 0x59: opcode << "mul"; break;
381 case 0x5C: opcode << "sub"; break;
382 case 0x5D: opcode << "min"; break;
383 case 0x5E: opcode << "div"; break;
384 case 0x5F: opcode << "max"; break;
385 default: LOG(FATAL) << "Unreachable";
386 }
387 if (prefix[2] == 0x66) {
388 opcode << "pd";
jeffhaofdffdf82012-07-11 16:08:43 -0700389 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogersbf989802012-04-16 16:07:49 -0700390 } else if (prefix[0] == 0xF2) {
391 opcode << "sd";
jeffhaofdffdf82012-07-11 16:08:43 -0700392 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogersbf989802012-04-16 16:07:49 -0700393 } else if (prefix[0] == 0xF3) {
394 opcode << "ss";
jeffhaofdffdf82012-07-11 16:08:43 -0700395 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogersbf989802012-04-16 16:07:49 -0700396 } else {
397 opcode << "ps";
398 }
399 load = true;
400 has_modrm = true;
401 src_reg_file = dst_reg_file = SSE;
402 break;
403 }
404 case 0x5A:
405 if (prefix[2] == 0x66) {
406 opcode << "cvtpd2ps";
jeffhaofdffdf82012-07-11 16:08:43 -0700407 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogersbf989802012-04-16 16:07:49 -0700408 } else if (prefix[0] == 0xF2) {
409 opcode << "cvtsd2ss";
jeffhaofdffdf82012-07-11 16:08:43 -0700410 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogersbf989802012-04-16 16:07:49 -0700411 } else if (prefix[0] == 0xF3) {
412 opcode << "cvtss2sd";
jeffhaofdffdf82012-07-11 16:08:43 -0700413 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogersbf989802012-04-16 16:07:49 -0700414 } else {
415 opcode << "cvtps2pd";
416 }
417 load = true;
418 has_modrm = true;
419 src_reg_file = dst_reg_file = SSE;
420 break;
421 case 0x5B:
422 if (prefix[2] == 0x66) {
423 opcode << "cvtps2dq";
jeffhaofdffdf82012-07-11 16:08:43 -0700424 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogersbf989802012-04-16 16:07:49 -0700425 } else if (prefix[0] == 0xF2) {
426 opcode << "bad opcode F2 0F 5B";
427 } else if (prefix[0] == 0xF3) {
428 opcode << "cvttps2dq";
jeffhaofdffdf82012-07-11 16:08:43 -0700429 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogersbf989802012-04-16 16:07:49 -0700430 } else {
431 opcode << "cvtdq2ps";
432 }
433 load = true;
434 has_modrm = true;
435 src_reg_file = dst_reg_file = SSE;
436 break;
Razvan A Lupusorud3266bc2014-01-24 12:55:31 -0800437 case 0x62:
438 if (prefix[2] == 0x66) {
439 src_reg_file = dst_reg_file = SSE;
440 prefix[2] = 0; // Clear prefix now. It has served its purpose as part of the opcode.
441 } else {
442 src_reg_file = dst_reg_file = MMX;
443 }
444 opcode << "punpckldq";
445 load = true;
446 has_modrm = true;
447 break;
Ian Rogersbf989802012-04-16 16:07:49 -0700448 case 0x6E:
449 if (prefix[2] == 0x66) {
450 dst_reg_file = SSE;
jeffhaofdffdf82012-07-11 16:08:43 -0700451 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogersbf989802012-04-16 16:07:49 -0700452 } else {
453 dst_reg_file = MMX;
Ian Rogersbf989802012-04-16 16:07:49 -0700454 }
jeffhaofdffdf82012-07-11 16:08:43 -0700455 opcode << "movd";
Ian Rogersbf989802012-04-16 16:07:49 -0700456 load = true;
457 has_modrm = true;
458 break;
459 case 0x6F:
460 if (prefix[2] == 0x66) {
461 dst_reg_file = SSE;
462 opcode << "movdqa";
jeffhaofdffdf82012-07-11 16:08:43 -0700463 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogersbf989802012-04-16 16:07:49 -0700464 } else if (prefix[0] == 0xF3) {
465 dst_reg_file = SSE;
466 opcode << "movdqu";
jeffhaofdffdf82012-07-11 16:08:43 -0700467 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogersbf989802012-04-16 16:07:49 -0700468 } else {
469 dst_reg_file = MMX;
470 opcode << "movq";
471 }
472 load = true;
473 has_modrm = true;
474 break;
jeffhaofdffdf82012-07-11 16:08:43 -0700475 case 0x71:
476 if (prefix[2] == 0x66) {
477 dst_reg_file = SSE;
478 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
479 } else {
480 dst_reg_file = MMX;
481 }
482 static const char* x71_opcodes[] = {"unknown-71", "unknown-71", "psrlw", "unknown-71", "psraw", "unknown-71", "psllw", "unknown-71"};
483 modrm_opcodes = x71_opcodes;
484 reg_is_opcode = true;
485 has_modrm = true;
486 store = true;
487 immediate_bytes = 1;
488 break;
489 case 0x72:
490 if (prefix[2] == 0x66) {
491 dst_reg_file = SSE;
492 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
493 } else {
494 dst_reg_file = MMX;
495 }
496 static const char* x72_opcodes[] = {"unknown-72", "unknown-72", "psrld", "unknown-72", "psrad", "unknown-72", "pslld", "unknown-72"};
497 modrm_opcodes = x72_opcodes;
498 reg_is_opcode = true;
499 has_modrm = true;
500 store = true;
501 immediate_bytes = 1;
502 break;
503 case 0x73:
504 if (prefix[2] == 0x66) {
505 dst_reg_file = SSE;
506 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
507 } else {
508 dst_reg_file = MMX;
509 }
510 static const char* x73_opcodes[] = {"unknown-73", "unknown-73", "psrlq", "unknown-73", "unknown-73", "unknown-73", "psllq", "unknown-73"};
511 modrm_opcodes = x73_opcodes;
512 reg_is_opcode = true;
513 has_modrm = true;
514 store = true;
515 immediate_bytes = 1;
516 break;
517 case 0x7E:
518 if (prefix[2] == 0x66) {
519 src_reg_file = SSE;
520 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
521 } else {
522 src_reg_file = MMX;
523 }
524 opcode << "movd";
525 has_modrm = true;
526 store = true;
527 break;
Ian Rogers706a10e2012-03-23 17:00:55 -0700528 case 0x80: case 0x81: case 0x82: case 0x83: case 0x84: case 0x85: case 0x86: case 0x87:
529 case 0x88: case 0x89: case 0x8A: case 0x8B: case 0x8C: case 0x8D: case 0x8E: case 0x8F:
530 opcode << "j" << condition_codes[*instr & 0xF];
531 branch_bytes = 4;
532 break;
Ian Rogers7caad772012-03-30 01:07:54 -0700533 case 0x90: case 0x91: case 0x92: case 0x93: case 0x94: case 0x95: case 0x96: case 0x97:
534 case 0x98: case 0x99: case 0x9A: case 0x9B: case 0x9C: case 0x9D: case 0x9E: case 0x9F:
535 opcode << "set" << condition_codes[*instr & 0xF];
536 modrm_opcodes = NULL;
537 reg_is_opcode = true;
538 has_modrm = true;
539 store = true;
540 break;
Mark Mendell4708dcd2014-01-22 09:05:18 -0800541 case 0xA4:
542 opcode << "shld";
543 has_modrm = true;
544 load = true;
545 immediate_bytes = 1;
546 break;
547 case 0xAC:
548 opcode << "shrd";
549 has_modrm = true;
550 load = true;
551 immediate_bytes = 1;
552 break;
jeffhao703f2cd2012-07-13 17:25:52 -0700553 case 0xAE:
554 if (prefix[0] == 0xF3) {
Ian Rogers5e588b32013-02-21 15:05:09 -0800555 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
jeffhao703f2cd2012-07-13 17:25:52 -0700556 static const char* xAE_opcodes[] = {"rdfsbase", "rdgsbase", "wrfsbase", "wrgsbase", "unknown-AE", "unknown-AE", "unknown-AE", "unknown-AE"};
557 modrm_opcodes = xAE_opcodes;
558 reg_is_opcode = true;
559 has_modrm = true;
560 uint8_t reg_or_opcode = (instr[1] >> 3) & 7;
561 switch (reg_or_opcode) {
562 case 0:
563 prefix[1] = kFs;
564 load = true;
565 break;
566 case 1:
567 prefix[1] = kGs;
568 load = true;
569 break;
570 case 2:
571 prefix[1] = kFs;
572 store = true;
573 break;
574 case 3:
575 prefix[1] = kGs;
576 store = true;
577 break;
578 default:
579 load = true;
580 break;
581 }
582 } else {
583 static const char* xAE_opcodes[] = {"unknown-AE", "unknown-AE", "unknown-AE", "unknown-AE", "unknown-AE", "lfence", "mfence", "sfence"};
584 modrm_opcodes = xAE_opcodes;
585 reg_is_opcode = true;
586 has_modrm = true;
587 load = true;
588 no_ops = true;
589 }
590 break;
Mark Mendellf723f0c2013-12-11 17:50:58 -0800591 case 0xAF: opcode << "imul"; has_modrm = true; load = true; break;
jeffhao83025762012-08-02 11:08:56 -0700592 case 0xB1: opcode << "cmpxchg"; has_modrm = true; store = true; break;
Ian Rogers7caad772012-03-30 01:07:54 -0700593 case 0xB6: opcode << "movzxb"; has_modrm = true; load = true; break;
594 case 0xB7: opcode << "movzxw"; has_modrm = true; load = true; break;
jeffhao854029c2012-07-23 17:31:30 -0700595 case 0xBE: opcode << "movsxb"; has_modrm = true; load = true; break;
596 case 0xBF: opcode << "movsxw"; has_modrm = true; load = true; break;
Vladimir Marko70b797d2013-12-03 15:25:24 +0000597 case 0xC7:
598 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" };
599 modrm_opcodes = x0FxC7_opcodes;
600 has_modrm = true;
601 reg_is_opcode = true;
602 store = true;
603 break;
Vladimir Markoa8b4caf2013-10-24 15:08:57 +0100604 case 0xC8: case 0xC9: case 0xCA: case 0xCB: case 0xCC: case 0xCD: case 0xCE: case 0xCF:
605 opcode << "bswap";
606 reg_in_opcode = true;
607 break;
Ian Rogers706a10e2012-03-23 17:00:55 -0700608 default:
609 opcode << StringPrintf("unknown opcode '0F %02X'", *instr);
610 break;
611 }
612 break;
613 case 0x80: case 0x81: case 0x82: case 0x83:
614 static const char* x80_opcodes[] = {"add", "or", "adc", "sbb", "and", "sub", "xor", "cmp"};
615 modrm_opcodes = x80_opcodes;
616 has_modrm = true;
617 reg_is_opcode = true;
618 store = true;
619 byte_operand = (*instr & 1) == 0;
620 immediate_bytes = *instr == 0x81 ? 4 : 1;
621 break;
jeffhao703f2cd2012-07-13 17:25:52 -0700622 case 0x84: case 0x85:
623 opcode << "test";
624 has_modrm = true;
625 load = true;
626 byte_operand = (*instr & 1) == 0;
627 break;
Ian Rogers7caad772012-03-30 01:07:54 -0700628 case 0x8D:
629 opcode << "lea";
630 has_modrm = true;
631 load = true;
632 break;
jeffhao703f2cd2012-07-13 17:25:52 -0700633 case 0x8F:
634 opcode << "pop";
635 has_modrm = true;
636 reg_is_opcode = true;
637 store = true;
638 break;
Mark Mendell2bf31e62014-01-23 12:13:40 -0800639 case 0x99:
640 opcode << "cdq";
641 break;
Mark Mendell4028a6c2014-02-19 20:06:20 -0800642 case 0xAF:
643 opcode << (prefix[2] == 0x66 ? "scasw" : "scasl");
644 break;
Ian Rogers706a10e2012-03-23 17:00:55 -0700645 case 0xB0: case 0xB1: case 0xB2: case 0xB3: case 0xB4: case 0xB5: case 0xB6: case 0xB7:
646 opcode << "mov";
647 immediate_bytes = 1;
648 reg_in_opcode = true;
649 break;
650 case 0xB8: case 0xB9: case 0xBA: case 0xBB: case 0xBC: case 0xBD: case 0xBE: case 0xBF:
651 opcode << "mov";
652 immediate_bytes = 4;
653 reg_in_opcode = true;
654 break;
Ian Rogers7caad772012-03-30 01:07:54 -0700655 case 0xC0: case 0xC1:
jeffhaoe2962482012-06-28 11:29:57 -0700656 case 0xD0: case 0xD1: case 0xD2: case 0xD3:
Ian Rogers7caad772012-03-30 01:07:54 -0700657 static const char* shift_opcodes[] =
658 {"rol", "ror", "rcl", "rcr", "shl", "shr", "unknown-shift", "sar"};
659 modrm_opcodes = shift_opcodes;
660 has_modrm = true;
661 reg_is_opcode = true;
662 store = true;
Elliott Hughes16b5c292012-04-16 20:37:16 -0700663 immediate_bytes = ((*instr & 0xf0) == 0xc0) ? 1 : 0;
jeffhaoe2962482012-06-28 11:29:57 -0700664 cx = (*instr == 0xD2) || (*instr == 0xD3);
665 byte_operand = (*instr == 0xC0);
Ian Rogers7caad772012-03-30 01:07:54 -0700666 break;
Ian Rogers706a10e2012-03-23 17:00:55 -0700667 case 0xC3: opcode << "ret"; break;
Elliott Hughes0589ca92012-04-09 18:26:20 -0700668 case 0xC7:
669 static const char* c7_opcodes[] = {"mov", "unknown-c7", "unknown-c7", "unknown-c7", "unknown-c7", "unknown-c7", "unknown-c7", "unknown-c7"};
670 modrm_opcodes = c7_opcodes;
671 store = true;
672 immediate_bytes = 4;
673 has_modrm = true;
674 reg_is_opcode = true;
675 break;
Ian Rogers7caad772012-03-30 01:07:54 -0700676 case 0xCC: opcode << "int 3"; break;
Mark Mendelld19b55a2013-12-12 09:55:34 -0800677 case 0xD9:
678 static const char* d9_opcodes[] = {"flds", "unknown-d9", "fsts", "fstps", "fldenv", "fldcw", "fnstenv", "fnstcw"};
679 modrm_opcodes = d9_opcodes;
680 store = true;
681 has_modrm = true;
682 reg_is_opcode = true;
683 break;
Razvan A Lupusoru614c2b42014-01-28 17:05:21 -0800684 case 0xDB:
685 static const char* db_opcodes[] = {"fildl", "unknown-db", "unknown-db", "unknown-db", "unknown-db", "unknown-db", "unknown-db", "unknown-db"};
686 modrm_opcodes = db_opcodes;
687 load = true;
688 has_modrm = true;
689 reg_is_opcode = true;
690 break;
Mark Mendelld19b55a2013-12-12 09:55:34 -0800691 case 0xDD:
692 static const char* dd_opcodes[] = {"fldl", "fisttp", "fstl", "fstpl", "frstor", "unknown-dd", "fnsave", "fnstsw"};
693 modrm_opcodes = dd_opcodes;
694 store = true;
695 has_modrm = true;
696 reg_is_opcode = true;
697 break;
Razvan A Lupusoru614c2b42014-01-28 17:05:21 -0800698 case 0xDF:
699 static const char* df_opcodes[] = {"fild", "unknown-df", "unknown-df", "unknown-df", "unknown-df", "fildll", "unknown-df", "unknown-df"};
700 modrm_opcodes = df_opcodes;
701 load = true;
702 has_modrm = true;
703 reg_is_opcode = true;
704 break;
Mark Mendell4028a6c2014-02-19 20:06:20 -0800705 case 0xE3: opcode << "jecxz"; branch_bytes = 1; break;
Ian Rogers7caad772012-03-30 01:07:54 -0700706 case 0xE8: opcode << "call"; branch_bytes = 4; break;
Ian Rogers706a10e2012-03-23 17:00:55 -0700707 case 0xE9: opcode << "jmp"; branch_bytes = 4; break;
708 case 0xEB: opcode << "jmp"; branch_bytes = 1; break;
jeffhao77ae36b2012-08-07 14:18:16 -0700709 case 0xF5: opcode << "cmc"; break;
jeffhao174651d2012-04-19 15:27:22 -0700710 case 0xF6: case 0xF7:
711 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 /"};
712 modrm_opcodes = f7_opcodes;
713 has_modrm = true;
714 reg_is_opcode = true;
715 store = true;
716 immediate_bytes = ((instr[1] & 0x38) == 0) ? 1 : 0;
717 break;
Ian Rogers706a10e2012-03-23 17:00:55 -0700718 case 0xFF:
719 static const char* ff_opcodes[] = {"inc", "dec", "call", "call", "jmp", "jmp", "push", "unknown-ff"};
720 modrm_opcodes = ff_opcodes;
721 has_modrm = true;
722 reg_is_opcode = true;
723 load = true;
724 break;
725 default:
726 opcode << StringPrintf("unknown opcode '%02X'", *instr);
727 break;
728 }
729 std::ostringstream args;
730 if (reg_in_opcode) {
731 DCHECK(!has_modrm);
Ian Rogersbf989802012-04-16 16:07:49 -0700732 DumpReg(args, rex, *instr & 0x7, false, prefix[2], GPR);
Ian Rogers706a10e2012-03-23 17:00:55 -0700733 }
734 instr++;
Elliott Hughes92301d92012-04-10 15:57:52 -0700735 uint32_t address_bits = 0;
Ian Rogers706a10e2012-03-23 17:00:55 -0700736 if (has_modrm) {
737 uint8_t modrm = *instr;
738 instr++;
739 uint8_t mod = modrm >> 6;
740 uint8_t reg_or_opcode = (modrm >> 3) & 7;
741 uint8_t rm = modrm & 7;
742 std::ostringstream address;
743 if (mod == 0 && rm == 5) { // fixed address
Elliott Hughes92301d92012-04-10 15:57:52 -0700744 address_bits = *reinterpret_cast<const uint32_t*>(instr);
745 address << StringPrintf("[0x%x]", address_bits);
Ian Rogers706a10e2012-03-23 17:00:55 -0700746 instr += 4;
747 } else if (rm == 4 && mod != 3) { // SIB
748 uint8_t sib = *instr;
749 instr++;
750 uint8_t ss = (sib >> 6) & 3;
751 uint8_t index = (sib >> 3) & 7;
752 uint8_t base = sib & 7;
753 address << "[";
754 if (base != 5 || mod != 0) {
Ian Rogers7caad772012-03-30 01:07:54 -0700755 DumpBaseReg(address, rex, base);
Ian Rogers706a10e2012-03-23 17:00:55 -0700756 if (index != 4) {
757 address << " + ";
758 }
759 }
760 if (index != 4) {
Ian Rogers7caad772012-03-30 01:07:54 -0700761 DumpIndexReg(address, rex, index);
Ian Rogers706a10e2012-03-23 17:00:55 -0700762 if (ss != 0) {
763 address << StringPrintf(" * %d", 1 << ss);
764 }
765 }
766 if (mod == 1) {
767 address << StringPrintf(" + %d", *reinterpret_cast<const int8_t*>(instr));
768 instr++;
769 } else if (mod == 2) {
770 address << StringPrintf(" + %d", *reinterpret_cast<const int32_t*>(instr));
771 instr += 4;
772 }
773 address << "]";
774 } else {
Ian Rogersbf989802012-04-16 16:07:49 -0700775 if (mod == 3) {
jeffhao703f2cd2012-07-13 17:25:52 -0700776 if (!no_ops) {
777 DumpReg(address, rex, rm, byte_operand, prefix[2], load ? src_reg_file : dst_reg_file);
778 }
Ian Rogersbf989802012-04-16 16:07:49 -0700779 } else {
Ian Rogers706a10e2012-03-23 17:00:55 -0700780 address << "[";
Ian Rogersbf989802012-04-16 16:07:49 -0700781 DumpBaseReg(address, rex, rm);
782 if (mod == 1) {
783 address << StringPrintf(" + %d", *reinterpret_cast<const int8_t*>(instr));
784 instr++;
785 } else if (mod == 2) {
786 address << StringPrintf(" + %d", *reinterpret_cast<const int32_t*>(instr));
787 instr += 4;
788 }
Ian Rogers706a10e2012-03-23 17:00:55 -0700789 address << "]";
790 }
791 }
792
Ian Rogers7caad772012-03-30 01:07:54 -0700793 if (reg_is_opcode && modrm_opcodes != NULL) {
Ian Rogers706a10e2012-03-23 17:00:55 -0700794 opcode << modrm_opcodes[reg_or_opcode];
795 }
796 if (load) {
797 if (!reg_is_opcode) {
Ian Rogersbf989802012-04-16 16:07:49 -0700798 DumpReg(args, rex, reg_or_opcode, byte_operand, prefix[2], dst_reg_file);
Ian Rogers706a10e2012-03-23 17:00:55 -0700799 args << ", ";
800 }
801 DumpSegmentOverride(args, prefix[1]);
802 args << address.str();
803 } else {
804 DCHECK(store);
805 DumpSegmentOverride(args, prefix[1]);
806 args << address.str();
807 if (!reg_is_opcode) {
808 args << ", ";
Ian Rogersbf989802012-04-16 16:07:49 -0700809 DumpReg(args, rex, reg_or_opcode, byte_operand, prefix[2], src_reg_file);
Ian Rogers706a10e2012-03-23 17:00:55 -0700810 }
811 }
812 }
813 if (ax) {
jeffhaofdffdf82012-07-11 16:08:43 -0700814 // If this opcode implicitly uses ax, ax is always the first arg.
Ian Rogersbf989802012-04-16 16:07:49 -0700815 DumpReg(args, rex, 0 /* EAX */, byte_operand, prefix[2], GPR);
Ian Rogers706a10e2012-03-23 17:00:55 -0700816 }
jeffhaoe2962482012-06-28 11:29:57 -0700817 if (cx) {
818 args << ", ";
819 DumpReg(args, rex, 1 /* ECX */, true, prefix[2], GPR);
820 }
Ian Rogers706a10e2012-03-23 17:00:55 -0700821 if (immediate_bytes > 0) {
jeffhaoe2962482012-06-28 11:29:57 -0700822 if (has_modrm || reg_in_opcode || ax || cx) {
Ian Rogers706a10e2012-03-23 17:00:55 -0700823 args << ", ";
824 }
825 if (immediate_bytes == 1) {
826 args << StringPrintf("%d", *reinterpret_cast<const int8_t*>(instr));
827 instr++;
828 } else {
829 CHECK_EQ(immediate_bytes, 4u);
830 args << StringPrintf("%d", *reinterpret_cast<const int32_t*>(instr));
831 instr += 4;
832 }
833 } else if (branch_bytes > 0) {
834 DCHECK(!has_modrm);
835 int32_t displacement;
836 if (branch_bytes == 1) {
837 displacement = *reinterpret_cast<const int8_t*>(instr);
838 instr++;
839 } else {
840 CHECK_EQ(branch_bytes, 4u);
841 displacement = *reinterpret_cast<const int32_t*>(instr);
842 instr += 4;
843 }
Elliott Hughes14178a92012-04-16 17:24:51 -0700844 args << StringPrintf("%+d (%p)", displacement, instr + displacement);
Ian Rogers706a10e2012-03-23 17:00:55 -0700845 }
Elliott Hughes92301d92012-04-10 15:57:52 -0700846 if (prefix[1] == kFs) {
847 args << " ; ";
848 Thread::DumpThreadOffset(args, address_bits, 4);
849 }
Elliott Hughes28fa76d2012-04-09 17:31:46 -0700850 std::stringstream hex;
Ian Rogers706a10e2012-03-23 17:00:55 -0700851 for (size_t i = 0; begin_instr + i < instr; ++i) {
Elliott Hughes28fa76d2012-04-09 17:31:46 -0700852 hex << StringPrintf("%02X", begin_instr[i]);
Ian Rogers706a10e2012-03-23 17:00:55 -0700853 }
Ian Rogers5e588b32013-02-21 15:05:09 -0800854 std::stringstream prefixed_opcode;
855 switch (prefix[0]) {
856 case 0xF0: prefixed_opcode << "lock "; break;
857 case 0xF2: prefixed_opcode << "repne "; break;
858 case 0xF3: prefixed_opcode << "repe "; break;
859 case 0: break;
860 default: LOG(FATAL) << "Unreachable";
861 }
862 prefixed_opcode << opcode.str();
863 os << StringPrintf("%p: %22s \t%-7s ", begin_instr, hex.str().c_str(),
864 prefixed_opcode.str().c_str())
865 << args.str() << '\n';
Ian Rogers706a10e2012-03-23 17:00:55 -0700866 return instr - begin_instr;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700867} // NOLINT(readability/fn_size)
Ian Rogers706a10e2012-03-23 17:00:55 -0700868
869} // namespace x86
870} // namespace art