blob: 614eca17105c5f741a19e21d90a66df3bf5dc40d [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;
Vladimir Kostyukove8861b32014-04-18 17:06:15 +0700148 if (rex != 0) {
149 instr++;
150 }
Ian Rogers706a10e2012-03-23 17:00:55 -0700151 bool has_modrm = false;
152 bool reg_is_opcode = false;
153 size_t immediate_bytes = 0;
154 size_t branch_bytes = 0;
155 std::ostringstream opcode;
156 bool store = false; // stores to memory (ie rm is on the left)
157 bool load = false; // loads from memory (ie rm is on the right)
158 bool byte_operand = false;
159 bool ax = false; // implicit use of ax
jeffhaoe2962482012-06-28 11:29:57 -0700160 bool cx = false; // implicit use of cx
Ian Rogers706a10e2012-03-23 17:00:55 -0700161 bool reg_in_opcode = false; // low 3-bits of opcode encode register parameter
jeffhao703f2cd2012-07-13 17:25:52 -0700162 bool no_ops = false;
Ian Rogersbf989802012-04-16 16:07:49 -0700163 RegFile src_reg_file = GPR;
164 RegFile dst_reg_file = GPR;
Ian Rogers706a10e2012-03-23 17:00:55 -0700165 switch (*instr) {
166#define DISASSEMBLER_ENTRY(opname, \
167 rm8_r8, rm32_r32, \
168 r8_rm8, r32_rm32, \
169 ax8_i8, ax32_i32) \
170 case rm8_r8: opcode << #opname; store = true; has_modrm = true; byte_operand = true; break; \
171 case rm32_r32: opcode << #opname; store = true; has_modrm = true; break; \
172 case r8_rm8: opcode << #opname; load = true; has_modrm = true; byte_operand = true; break; \
173 case r32_rm32: opcode << #opname; load = true; has_modrm = true; break; \
174 case ax8_i8: opcode << #opname; ax = true; immediate_bytes = 1; byte_operand = true; break; \
175 case ax32_i32: opcode << #opname; ax = true; immediate_bytes = 4; break;
176
177DISASSEMBLER_ENTRY(add,
178 0x00 /* RegMem8/Reg8 */, 0x01 /* RegMem32/Reg32 */,
179 0x02 /* Reg8/RegMem8 */, 0x03 /* Reg32/RegMem32 */,
180 0x04 /* Rax8/imm8 opcode */, 0x05 /* Rax32/imm32 */)
181DISASSEMBLER_ENTRY(or,
182 0x08 /* RegMem8/Reg8 */, 0x09 /* RegMem32/Reg32 */,
183 0x0A /* Reg8/RegMem8 */, 0x0B /* Reg32/RegMem32 */,
184 0x0C /* Rax8/imm8 opcode */, 0x0D /* Rax32/imm32 */)
185DISASSEMBLER_ENTRY(adc,
186 0x10 /* RegMem8/Reg8 */, 0x11 /* RegMem32/Reg32 */,
187 0x12 /* Reg8/RegMem8 */, 0x13 /* Reg32/RegMem32 */,
188 0x14 /* Rax8/imm8 opcode */, 0x15 /* Rax32/imm32 */)
189DISASSEMBLER_ENTRY(sbb,
190 0x18 /* RegMem8/Reg8 */, 0x19 /* RegMem32/Reg32 */,
191 0x1A /* Reg8/RegMem8 */, 0x1B /* Reg32/RegMem32 */,
192 0x1C /* Rax8/imm8 opcode */, 0x1D /* Rax32/imm32 */)
193DISASSEMBLER_ENTRY(and,
194 0x20 /* RegMem8/Reg8 */, 0x21 /* RegMem32/Reg32 */,
195 0x22 /* Reg8/RegMem8 */, 0x23 /* Reg32/RegMem32 */,
196 0x24 /* Rax8/imm8 opcode */, 0x25 /* Rax32/imm32 */)
197DISASSEMBLER_ENTRY(sub,
198 0x28 /* RegMem8/Reg8 */, 0x29 /* RegMem32/Reg32 */,
199 0x2A /* Reg8/RegMem8 */, 0x2B /* Reg32/RegMem32 */,
200 0x2C /* Rax8/imm8 opcode */, 0x2D /* Rax32/imm32 */)
201DISASSEMBLER_ENTRY(xor,
202 0x30 /* RegMem8/Reg8 */, 0x31 /* RegMem32/Reg32 */,
203 0x32 /* Reg8/RegMem8 */, 0x33 /* Reg32/RegMem32 */,
204 0x34 /* Rax8/imm8 opcode */, 0x35 /* Rax32/imm32 */)
205DISASSEMBLER_ENTRY(cmp,
206 0x38 /* RegMem8/Reg8 */, 0x39 /* RegMem32/Reg32 */,
207 0x3A /* Reg8/RegMem8 */, 0x3B /* Reg32/RegMem32 */,
208 0x3C /* Rax8/imm8 opcode */, 0x3D /* Rax32/imm32 */)
209
210#undef DISASSEMBLER_ENTRY
211 case 0x50: case 0x51: case 0x52: case 0x53: case 0x54: case 0x55: case 0x56: case 0x57:
212 opcode << "push";
213 reg_in_opcode = true;
214 break;
215 case 0x58: case 0x59: case 0x5A: case 0x5B: case 0x5C: case 0x5D: case 0x5E: case 0x5F:
216 opcode << "pop";
217 reg_in_opcode = true;
218 break;
219 case 0x68: opcode << "push"; immediate_bytes = 4; break;
Mark Mendelld19b55a2013-12-12 09:55:34 -0800220 case 0x69: opcode << "imul"; load = true; has_modrm = true; immediate_bytes = 4; break;
Ian Rogers706a10e2012-03-23 17:00:55 -0700221 case 0x6A: opcode << "push"; immediate_bytes = 1; break;
Mark Mendelld19b55a2013-12-12 09:55:34 -0800222 case 0x6B: opcode << "imul"; load = true; has_modrm = true; immediate_bytes = 1; break;
Ian Rogers706a10e2012-03-23 17:00:55 -0700223 case 0x70: case 0x71: case 0x72: case 0x73: case 0x74: case 0x75: case 0x76: case 0x77:
224 case 0x78: case 0x79: case 0x7A: case 0x7B: case 0x7C: case 0x7D: case 0x7E: case 0x7F:
225 static const char* condition_codes[] =
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700226 {"o", "no", "b/nae/c", "nb/ae/nc", "z/eq", "nz/ne", "be/na", "nbe/a",
227 "s", "ns", "p/pe", "np/po", "l/nge", "nl/ge", "le/ng", "nle/g"
Ian Rogers706a10e2012-03-23 17:00:55 -0700228 };
229 opcode << "j" << condition_codes[*instr & 0xF];
230 branch_bytes = 1;
231 break;
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800232 case 0x86: case 0x87:
233 opcode << "xchg";
234 store = true;
235 has_modrm = true;
236 byte_operand = (*instr == 0x86);
237 break;
Ian Rogers706a10e2012-03-23 17:00:55 -0700238 case 0x88: opcode << "mov"; store = true; has_modrm = true; byte_operand = true; break;
239 case 0x89: opcode << "mov"; store = true; has_modrm = true; break;
240 case 0x8A: opcode << "mov"; load = true; has_modrm = true; byte_operand = true; break;
241 case 0x8B: opcode << "mov"; load = true; has_modrm = true; break;
242
243 case 0x0F: // 2 byte extended opcode
244 instr++;
245 switch (*instr) {
Ian Rogers7caad772012-03-30 01:07:54 -0700246 case 0x10: case 0x11:
247 if (prefix[0] == 0xF2) {
248 opcode << "movsd";
jeffhaofdffdf82012-07-11 16:08:43 -0700249 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogers7caad772012-03-30 01:07:54 -0700250 } else if (prefix[0] == 0xF3) {
251 opcode << "movss";
jeffhaofdffdf82012-07-11 16:08:43 -0700252 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogers7caad772012-03-30 01:07:54 -0700253 } else if (prefix[2] == 0x66) {
254 opcode << "movupd";
jeffhaofdffdf82012-07-11 16:08:43 -0700255 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogers7caad772012-03-30 01:07:54 -0700256 } else {
257 opcode << "movups";
258 }
259 has_modrm = true;
Ian Rogersbf989802012-04-16 16:07:49 -0700260 src_reg_file = dst_reg_file = SSE;
Ian Rogers7caad772012-03-30 01:07:54 -0700261 load = *instr == 0x10;
262 store = !load;
263 break;
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800264 case 0x12: case 0x13:
265 if (prefix[2] == 0x66) {
266 opcode << "movlpd";
267 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
268 } else if (prefix[0] == 0) {
269 opcode << "movlps";
270 }
271 has_modrm = true;
272 src_reg_file = dst_reg_file = SSE;
273 load = *instr == 0x12;
274 store = !load;
275 break;
276 case 0x16: case 0x17:
277 if (prefix[2] == 0x66) {
278 opcode << "movhpd";
279 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
280 } else if (prefix[0] == 0) {
281 opcode << "movhps";
282 }
283 has_modrm = true;
284 src_reg_file = dst_reg_file = SSE;
285 load = *instr == 0x16;
286 store = !load;
287 break;
288 case 0x28: case 0x29:
289 if (prefix[2] == 0x66) {
290 opcode << "movapd";
291 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
292 } else if (prefix[0] == 0) {
293 opcode << "movaps";
294 }
295 has_modrm = true;
296 src_reg_file = dst_reg_file = SSE;
297 load = *instr == 0x28;
298 store = !load;
299 break;
jeffhaofdffdf82012-07-11 16:08:43 -0700300 case 0x2A:
301 if (prefix[2] == 0x66) {
302 opcode << "cvtpi2pd";
303 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
304 } else if (prefix[0] == 0xF2) {
305 opcode << "cvtsi2sd";
306 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
307 } else if (prefix[0] == 0xF3) {
308 opcode << "cvtsi2ss";
309 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
310 } else {
311 opcode << "cvtpi2ps";
312 }
313 load = true;
314 has_modrm = true;
315 dst_reg_file = SSE;
316 break;
317 case 0x2C:
318 if (prefix[2] == 0x66) {
319 opcode << "cvttpd2pi";
320 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
321 } else if (prefix[0] == 0xF2) {
322 opcode << "cvttsd2si";
323 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
324 } else if (prefix[0] == 0xF3) {
325 opcode << "cvttss2si";
326 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
327 } else {
328 opcode << "cvttps2pi";
329 }
330 load = true;
331 has_modrm = true;
332 src_reg_file = SSE;
333 break;
334 case 0x2D:
335 if (prefix[2] == 0x66) {
336 opcode << "cvtpd2pi";
337 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
338 } else if (prefix[0] == 0xF2) {
339 opcode << "cvtsd2si";
340 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
341 } else if (prefix[0] == 0xF3) {
342 opcode << "cvtss2si";
343 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
344 } else {
345 opcode << "cvtps2pi";
346 }
347 load = true;
348 has_modrm = true;
349 src_reg_file = SSE;
350 break;
351 case 0x2E:
352 opcode << "u";
353 // FALLTHROUGH
354 case 0x2F:
355 if (prefix[2] == 0x66) {
356 opcode << "comisd";
357 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
358 } else {
359 opcode << "comiss";
360 }
361 has_modrm = true;
362 load = true;
363 src_reg_file = dst_reg_file = SSE;
364 break;
Ian Rogers706a10e2012-03-23 17:00:55 -0700365 case 0x38: // 3 byte extended opcode
Mark Mendellfe945782014-05-22 09:52:36 -0400366 instr++;
367 if (prefix[2] == 0x66) {
368 switch (*instr) {
369 case 0x40:
370 opcode << "pmulld";
371 prefix[2] = 0;
372 has_modrm = true;
373 load = true;
374 src_reg_file = dst_reg_file = SSE;
375 break;
376 default:
377 opcode << StringPrintf("unknown opcode '0F 38 %02X'", *instr);
378 }
379 } else {
380 opcode << StringPrintf("unknown opcode '0F 38 %02X'", *instr);
381 }
Ian Rogers706a10e2012-03-23 17:00:55 -0700382 break;
383 case 0x3A: // 3 byte extended opcode
Mark Mendellfe945782014-05-22 09:52:36 -0400384 instr++;
385 if (prefix[2] == 0x66) {
386 switch (*instr) {
387 case 0x14:
388 opcode << "pextrb";
389 prefix[2] = 0;
390 has_modrm = true;
391 store = true;
392 dst_reg_file = SSE;
393 immediate_bytes = 1;
394 break;
395 case 0x16:
396 opcode << "pextrd";
397 prefix[2] = 0;
398 has_modrm = true;
399 store = true;
400 dst_reg_file = SSE;
401 immediate_bytes = 1;
402 break;
403 default:
404 opcode << StringPrintf("unknown opcode '0F 3A %02X'", *instr);
405 }
406 } else {
407 opcode << StringPrintf("unknown opcode '0F 3A %02X'", *instr);
408 }
Ian Rogers706a10e2012-03-23 17:00:55 -0700409 break;
Razvan A Lupusorubd288c22013-12-20 17:27:23 -0800410 case 0x40: case 0x41: case 0x42: case 0x43: case 0x44: case 0x45: case 0x46: case 0x47:
411 case 0x48: case 0x49: case 0x4A: case 0x4B: case 0x4C: case 0x4D: case 0x4E: case 0x4F:
412 opcode << "cmov" << condition_codes[*instr & 0xF];
413 has_modrm = true;
414 load = true;
415 break;
Ian Rogersbf989802012-04-16 16:07:49 -0700416 case 0x50: case 0x51: case 0x52: case 0x53: case 0x54: case 0x55: case 0x56: case 0x57:
417 case 0x58: case 0x59: case 0x5C: case 0x5D: case 0x5E: case 0x5F: {
418 switch (*instr) {
419 case 0x50: opcode << "movmsk"; break;
420 case 0x51: opcode << "sqrt"; break;
421 case 0x52: opcode << "rsqrt"; break;
422 case 0x53: opcode << "rcp"; break;
423 case 0x54: opcode << "and"; break;
424 case 0x55: opcode << "andn"; break;
425 case 0x56: opcode << "or"; break;
426 case 0x57: opcode << "xor"; break;
427 case 0x58: opcode << "add"; break;
428 case 0x59: opcode << "mul"; break;
429 case 0x5C: opcode << "sub"; break;
430 case 0x5D: opcode << "min"; break;
431 case 0x5E: opcode << "div"; break;
432 case 0x5F: opcode << "max"; break;
433 default: LOG(FATAL) << "Unreachable";
434 }
435 if (prefix[2] == 0x66) {
436 opcode << "pd";
jeffhaofdffdf82012-07-11 16:08:43 -0700437 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogersbf989802012-04-16 16:07:49 -0700438 } else if (prefix[0] == 0xF2) {
439 opcode << "sd";
jeffhaofdffdf82012-07-11 16:08:43 -0700440 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogersbf989802012-04-16 16:07:49 -0700441 } else if (prefix[0] == 0xF3) {
442 opcode << "ss";
jeffhaofdffdf82012-07-11 16:08:43 -0700443 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogersbf989802012-04-16 16:07:49 -0700444 } else {
445 opcode << "ps";
446 }
447 load = true;
448 has_modrm = true;
449 src_reg_file = dst_reg_file = SSE;
450 break;
451 }
452 case 0x5A:
453 if (prefix[2] == 0x66) {
454 opcode << "cvtpd2ps";
jeffhaofdffdf82012-07-11 16:08:43 -0700455 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogersbf989802012-04-16 16:07:49 -0700456 } else if (prefix[0] == 0xF2) {
457 opcode << "cvtsd2ss";
jeffhaofdffdf82012-07-11 16:08:43 -0700458 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogersbf989802012-04-16 16:07:49 -0700459 } else if (prefix[0] == 0xF3) {
460 opcode << "cvtss2sd";
jeffhaofdffdf82012-07-11 16:08:43 -0700461 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogersbf989802012-04-16 16:07:49 -0700462 } else {
463 opcode << "cvtps2pd";
464 }
465 load = true;
466 has_modrm = true;
467 src_reg_file = dst_reg_file = SSE;
468 break;
469 case 0x5B:
470 if (prefix[2] == 0x66) {
471 opcode << "cvtps2dq";
jeffhaofdffdf82012-07-11 16:08:43 -0700472 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogersbf989802012-04-16 16:07:49 -0700473 } else if (prefix[0] == 0xF2) {
474 opcode << "bad opcode F2 0F 5B";
475 } else if (prefix[0] == 0xF3) {
476 opcode << "cvttps2dq";
jeffhaofdffdf82012-07-11 16:08:43 -0700477 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogersbf989802012-04-16 16:07:49 -0700478 } else {
479 opcode << "cvtdq2ps";
480 }
481 load = true;
482 has_modrm = true;
483 src_reg_file = dst_reg_file = SSE;
484 break;
Razvan A Lupusorud3266bc2014-01-24 12:55:31 -0800485 case 0x62:
486 if (prefix[2] == 0x66) {
487 src_reg_file = dst_reg_file = SSE;
488 prefix[2] = 0; // Clear prefix now. It has served its purpose as part of the opcode.
489 } else {
490 src_reg_file = dst_reg_file = MMX;
491 }
492 opcode << "punpckldq";
493 load = true;
494 has_modrm = true;
495 break;
Ian Rogersbf989802012-04-16 16:07:49 -0700496 case 0x6E:
497 if (prefix[2] == 0x66) {
498 dst_reg_file = SSE;
jeffhaofdffdf82012-07-11 16:08:43 -0700499 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogersbf989802012-04-16 16:07:49 -0700500 } else {
501 dst_reg_file = MMX;
Ian Rogersbf989802012-04-16 16:07:49 -0700502 }
jeffhaofdffdf82012-07-11 16:08:43 -0700503 opcode << "movd";
Ian Rogersbf989802012-04-16 16:07:49 -0700504 load = true;
505 has_modrm = true;
506 break;
507 case 0x6F:
508 if (prefix[2] == 0x66) {
Mark Mendellfe945782014-05-22 09:52:36 -0400509 src_reg_file = dst_reg_file = SSE;
Ian Rogersbf989802012-04-16 16:07:49 -0700510 opcode << "movdqa";
jeffhaofdffdf82012-07-11 16:08:43 -0700511 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogersbf989802012-04-16 16:07:49 -0700512 } else if (prefix[0] == 0xF3) {
Mark Mendellfe945782014-05-22 09:52:36 -0400513 src_reg_file = dst_reg_file = SSE;
Ian Rogersbf989802012-04-16 16:07:49 -0700514 opcode << "movdqu";
jeffhaofdffdf82012-07-11 16:08:43 -0700515 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
Ian Rogersbf989802012-04-16 16:07:49 -0700516 } else {
517 dst_reg_file = MMX;
518 opcode << "movq";
519 }
520 load = true;
521 has_modrm = true;
522 break;
Mark Mendellfe945782014-05-22 09:52:36 -0400523 case 0x70:
524 if (prefix[2] == 0x66) {
525 opcode << "pshufd";
526 prefix[2] = 0;
527 has_modrm = true;
528 store = true;
529 src_reg_file = dst_reg_file = SSE;
530 immediate_bytes = 1;
531 } else if (prefix[0] == 0xF2) {
532 opcode << "pshuflw";
533 prefix[0] = 0;
534 has_modrm = true;
535 store = true;
536 src_reg_file = dst_reg_file = SSE;
537 immediate_bytes = 1;
538 } else {
539 opcode << StringPrintf("unknown opcode '0F %02X'", *instr);
540 }
541 break;
jeffhaofdffdf82012-07-11 16:08:43 -0700542 case 0x71:
543 if (prefix[2] == 0x66) {
544 dst_reg_file = SSE;
545 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
546 } else {
547 dst_reg_file = MMX;
548 }
549 static const char* x71_opcodes[] = {"unknown-71", "unknown-71", "psrlw", "unknown-71", "psraw", "unknown-71", "psllw", "unknown-71"};
550 modrm_opcodes = x71_opcodes;
551 reg_is_opcode = true;
552 has_modrm = true;
553 store = true;
554 immediate_bytes = 1;
555 break;
556 case 0x72:
557 if (prefix[2] == 0x66) {
558 dst_reg_file = SSE;
559 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
560 } else {
561 dst_reg_file = MMX;
562 }
563 static const char* x72_opcodes[] = {"unknown-72", "unknown-72", "psrld", "unknown-72", "psrad", "unknown-72", "pslld", "unknown-72"};
564 modrm_opcodes = x72_opcodes;
565 reg_is_opcode = true;
566 has_modrm = true;
567 store = true;
568 immediate_bytes = 1;
569 break;
570 case 0x73:
571 if (prefix[2] == 0x66) {
572 dst_reg_file = SSE;
573 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
574 } else {
575 dst_reg_file = MMX;
576 }
577 static const char* x73_opcodes[] = {"unknown-73", "unknown-73", "psrlq", "unknown-73", "unknown-73", "unknown-73", "psllq", "unknown-73"};
578 modrm_opcodes = x73_opcodes;
579 reg_is_opcode = true;
580 has_modrm = true;
581 store = true;
582 immediate_bytes = 1;
583 break;
584 case 0x7E:
585 if (prefix[2] == 0x66) {
586 src_reg_file = SSE;
587 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
588 } else {
589 src_reg_file = MMX;
590 }
591 opcode << "movd";
592 has_modrm = true;
593 store = true;
594 break;
Ian Rogers706a10e2012-03-23 17:00:55 -0700595 case 0x80: case 0x81: case 0x82: case 0x83: case 0x84: case 0x85: case 0x86: case 0x87:
596 case 0x88: case 0x89: case 0x8A: case 0x8B: case 0x8C: case 0x8D: case 0x8E: case 0x8F:
597 opcode << "j" << condition_codes[*instr & 0xF];
598 branch_bytes = 4;
599 break;
Ian Rogers7caad772012-03-30 01:07:54 -0700600 case 0x90: case 0x91: case 0x92: case 0x93: case 0x94: case 0x95: case 0x96: case 0x97:
601 case 0x98: case 0x99: case 0x9A: case 0x9B: case 0x9C: case 0x9D: case 0x9E: case 0x9F:
602 opcode << "set" << condition_codes[*instr & 0xF];
603 modrm_opcodes = NULL;
604 reg_is_opcode = true;
605 has_modrm = true;
606 store = true;
607 break;
Mark Mendell4708dcd2014-01-22 09:05:18 -0800608 case 0xA4:
609 opcode << "shld";
610 has_modrm = true;
611 load = true;
612 immediate_bytes = 1;
613 break;
614 case 0xAC:
615 opcode << "shrd";
616 has_modrm = true;
617 load = true;
618 immediate_bytes = 1;
619 break;
jeffhao703f2cd2012-07-13 17:25:52 -0700620 case 0xAE:
621 if (prefix[0] == 0xF3) {
Ian Rogers5e588b32013-02-21 15:05:09 -0800622 prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode
jeffhao703f2cd2012-07-13 17:25:52 -0700623 static const char* xAE_opcodes[] = {"rdfsbase", "rdgsbase", "wrfsbase", "wrgsbase", "unknown-AE", "unknown-AE", "unknown-AE", "unknown-AE"};
624 modrm_opcodes = xAE_opcodes;
625 reg_is_opcode = true;
626 has_modrm = true;
627 uint8_t reg_or_opcode = (instr[1] >> 3) & 7;
628 switch (reg_or_opcode) {
629 case 0:
630 prefix[1] = kFs;
631 load = true;
632 break;
633 case 1:
634 prefix[1] = kGs;
635 load = true;
636 break;
637 case 2:
638 prefix[1] = kFs;
639 store = true;
640 break;
641 case 3:
642 prefix[1] = kGs;
643 store = true;
644 break;
645 default:
646 load = true;
647 break;
648 }
649 } else {
650 static const char* xAE_opcodes[] = {"unknown-AE", "unknown-AE", "unknown-AE", "unknown-AE", "unknown-AE", "lfence", "mfence", "sfence"};
651 modrm_opcodes = xAE_opcodes;
652 reg_is_opcode = true;
653 has_modrm = true;
654 load = true;
655 no_ops = true;
656 }
657 break;
Mark Mendellf723f0c2013-12-11 17:50:58 -0800658 case 0xAF: opcode << "imul"; has_modrm = true; load = true; break;
jeffhao83025762012-08-02 11:08:56 -0700659 case 0xB1: opcode << "cmpxchg"; has_modrm = true; store = true; break;
Ian Rogers7caad772012-03-30 01:07:54 -0700660 case 0xB6: opcode << "movzxb"; has_modrm = true; load = true; break;
661 case 0xB7: opcode << "movzxw"; has_modrm = true; load = true; break;
jeffhao854029c2012-07-23 17:31:30 -0700662 case 0xBE: opcode << "movsxb"; has_modrm = true; load = true; break;
663 case 0xBF: opcode << "movsxw"; has_modrm = true; load = true; break;
Mark Mendellfe945782014-05-22 09:52:36 -0400664 case 0xC5:
665 if (prefix[2] == 0x66) {
666 opcode << "pextrw";
667 prefix[2] = 0;
668 has_modrm = true;
669 store = true;
670 src_reg_file = dst_reg_file = SSE;
671 immediate_bytes = 1;
672 } else {
673 opcode << StringPrintf("unknown opcode '0F %02X'", *instr);
674 }
675 break;
Vladimir Marko70b797d2013-12-03 15:25:24 +0000676 case 0xC7:
677 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" };
678 modrm_opcodes = x0FxC7_opcodes;
679 has_modrm = true;
680 reg_is_opcode = true;
681 store = true;
682 break;
Vladimir Markoa8b4caf2013-10-24 15:08:57 +0100683 case 0xC8: case 0xC9: case 0xCA: case 0xCB: case 0xCC: case 0xCD: case 0xCE: case 0xCF:
684 opcode << "bswap";
685 reg_in_opcode = true;
686 break;
Mark Mendellfe945782014-05-22 09:52:36 -0400687 case 0xDB:
688 if (prefix[2] == 0x66) {
689 src_reg_file = dst_reg_file = SSE;
690 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
691 } else {
692 src_reg_file = dst_reg_file = MMX;
693 }
694 opcode << "pand";
695 prefix[2] = 0;
696 has_modrm = true;
697 load = true;
698 break;
699 case 0xD5:
700 if (prefix[2] == 0x66) {
701 opcode << "pmullw";
702 prefix[2] = 0;
703 has_modrm = true;
704 load = true;
705 src_reg_file = dst_reg_file = SSE;
706 } else {
707 opcode << StringPrintf("unknown opcode '0F %02X'", *instr);
708 }
709 break;
710 case 0xEB:
711 if (prefix[2] == 0x66) {
712 src_reg_file = dst_reg_file = SSE;
713 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
714 } else {
715 src_reg_file = dst_reg_file = MMX;
716 }
717 opcode << "por";
718 prefix[2] = 0;
719 has_modrm = true;
720 load = true;
721 break;
722 case 0xEF:
723 if (prefix[2] == 0x66) {
724 src_reg_file = dst_reg_file = SSE;
725 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
726 } else {
727 src_reg_file = dst_reg_file = MMX;
728 }
729 opcode << "pxor";
730 prefix[2] = 0;
731 has_modrm = true;
732 load = true;
733 break;
734 case 0xF8:
735 if (prefix[2] == 0x66) {
736 src_reg_file = dst_reg_file = SSE;
737 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
738 } else {
739 src_reg_file = dst_reg_file = MMX;
740 }
741 opcode << "psubb";
742 prefix[2] = 0;
743 has_modrm = true;
744 load = true;
745 break;
746 case 0xF9:
747 if (prefix[2] == 0x66) {
748 src_reg_file = dst_reg_file = SSE;
749 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
750 } else {
751 src_reg_file = dst_reg_file = MMX;
752 }
753 opcode << "psubw";
754 prefix[2] = 0;
755 has_modrm = true;
756 load = true;
757 break;
758 case 0xFA:
759 if (prefix[2] == 0x66) {
760 src_reg_file = dst_reg_file = SSE;
761 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
762 } else {
763 src_reg_file = dst_reg_file = MMX;
764 }
765 opcode << "psubd";
766 prefix[2] = 0;
767 has_modrm = true;
768 load = true;
769 break;
770 case 0xFC:
771 if (prefix[2] == 0x66) {
772 src_reg_file = dst_reg_file = SSE;
773 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
774 } else {
775 src_reg_file = dst_reg_file = MMX;
776 }
777 opcode << "paddb";
778 prefix[2] = 0;
779 has_modrm = true;
780 load = true;
781 break;
782 case 0xFD:
783 if (prefix[2] == 0x66) {
784 src_reg_file = dst_reg_file = SSE;
785 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
786 } else {
787 src_reg_file = dst_reg_file = MMX;
788 }
789 opcode << "paddw";
790 prefix[2] = 0;
791 has_modrm = true;
792 load = true;
793 break;
794 case 0xFE:
795 if (prefix[2] == 0x66) {
796 src_reg_file = dst_reg_file = SSE;
797 prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode
798 } else {
799 src_reg_file = dst_reg_file = MMX;
800 }
801 opcode << "paddd";
802 prefix[2] = 0;
803 has_modrm = true;
804 load = true;
805 break;
Ian Rogers706a10e2012-03-23 17:00:55 -0700806 default:
807 opcode << StringPrintf("unknown opcode '0F %02X'", *instr);
808 break;
809 }
810 break;
811 case 0x80: case 0x81: case 0x82: case 0x83:
812 static const char* x80_opcodes[] = {"add", "or", "adc", "sbb", "and", "sub", "xor", "cmp"};
813 modrm_opcodes = x80_opcodes;
814 has_modrm = true;
815 reg_is_opcode = true;
816 store = true;
817 byte_operand = (*instr & 1) == 0;
818 immediate_bytes = *instr == 0x81 ? 4 : 1;
819 break;
jeffhao703f2cd2012-07-13 17:25:52 -0700820 case 0x84: case 0x85:
821 opcode << "test";
822 has_modrm = true;
823 load = true;
824 byte_operand = (*instr & 1) == 0;
825 break;
Ian Rogers7caad772012-03-30 01:07:54 -0700826 case 0x8D:
827 opcode << "lea";
828 has_modrm = true;
829 load = true;
830 break;
jeffhao703f2cd2012-07-13 17:25:52 -0700831 case 0x8F:
832 opcode << "pop";
833 has_modrm = true;
834 reg_is_opcode = true;
835 store = true;
836 break;
Mark Mendell2bf31e62014-01-23 12:13:40 -0800837 case 0x99:
838 opcode << "cdq";
839 break;
Mark Mendell4028a6c2014-02-19 20:06:20 -0800840 case 0xAF:
841 opcode << (prefix[2] == 0x66 ? "scasw" : "scasl");
842 break;
Ian Rogers706a10e2012-03-23 17:00:55 -0700843 case 0xB0: case 0xB1: case 0xB2: case 0xB3: case 0xB4: case 0xB5: case 0xB6: case 0xB7:
844 opcode << "mov";
845 immediate_bytes = 1;
846 reg_in_opcode = true;
847 break;
848 case 0xB8: case 0xB9: case 0xBA: case 0xBB: case 0xBC: case 0xBD: case 0xBE: case 0xBF:
849 opcode << "mov";
850 immediate_bytes = 4;
851 reg_in_opcode = true;
852 break;
Ian Rogers7caad772012-03-30 01:07:54 -0700853 case 0xC0: case 0xC1:
jeffhaoe2962482012-06-28 11:29:57 -0700854 case 0xD0: case 0xD1: case 0xD2: case 0xD3:
Ian Rogers7caad772012-03-30 01:07:54 -0700855 static const char* shift_opcodes[] =
856 {"rol", "ror", "rcl", "rcr", "shl", "shr", "unknown-shift", "sar"};
857 modrm_opcodes = shift_opcodes;
858 has_modrm = true;
859 reg_is_opcode = true;
860 store = true;
Elliott Hughes16b5c292012-04-16 20:37:16 -0700861 immediate_bytes = ((*instr & 0xf0) == 0xc0) ? 1 : 0;
jeffhaoe2962482012-06-28 11:29:57 -0700862 cx = (*instr == 0xD2) || (*instr == 0xD3);
863 byte_operand = (*instr == 0xC0);
Ian Rogers7caad772012-03-30 01:07:54 -0700864 break;
Ian Rogers706a10e2012-03-23 17:00:55 -0700865 case 0xC3: opcode << "ret"; break;
Elliott Hughes0589ca92012-04-09 18:26:20 -0700866 case 0xC7:
867 static const char* c7_opcodes[] = {"mov", "unknown-c7", "unknown-c7", "unknown-c7", "unknown-c7", "unknown-c7", "unknown-c7", "unknown-c7"};
868 modrm_opcodes = c7_opcodes;
869 store = true;
870 immediate_bytes = 4;
871 has_modrm = true;
872 reg_is_opcode = true;
873 break;
Ian Rogers7caad772012-03-30 01:07:54 -0700874 case 0xCC: opcode << "int 3"; break;
Mark Mendelld19b55a2013-12-12 09:55:34 -0800875 case 0xD9:
876 static const char* d9_opcodes[] = {"flds", "unknown-d9", "fsts", "fstps", "fldenv", "fldcw", "fnstenv", "fnstcw"};
877 modrm_opcodes = d9_opcodes;
878 store = true;
879 has_modrm = true;
880 reg_is_opcode = true;
881 break;
Razvan A Lupusoru614c2b42014-01-28 17:05:21 -0800882 case 0xDB:
883 static const char* db_opcodes[] = {"fildl", "unknown-db", "unknown-db", "unknown-db", "unknown-db", "unknown-db", "unknown-db", "unknown-db"};
884 modrm_opcodes = db_opcodes;
885 load = true;
886 has_modrm = true;
887 reg_is_opcode = true;
888 break;
Mark Mendelld19b55a2013-12-12 09:55:34 -0800889 case 0xDD:
890 static const char* dd_opcodes[] = {"fldl", "fisttp", "fstl", "fstpl", "frstor", "unknown-dd", "fnsave", "fnstsw"};
891 modrm_opcodes = dd_opcodes;
892 store = true;
893 has_modrm = true;
894 reg_is_opcode = true;
895 break;
Razvan A Lupusoru614c2b42014-01-28 17:05:21 -0800896 case 0xDF:
897 static const char* df_opcodes[] = {"fild", "unknown-df", "unknown-df", "unknown-df", "unknown-df", "fildll", "unknown-df", "unknown-df"};
898 modrm_opcodes = df_opcodes;
899 load = true;
900 has_modrm = true;
901 reg_is_opcode = true;
902 break;
Mark Mendell4028a6c2014-02-19 20:06:20 -0800903 case 0xE3: opcode << "jecxz"; branch_bytes = 1; break;
Ian Rogers7caad772012-03-30 01:07:54 -0700904 case 0xE8: opcode << "call"; branch_bytes = 4; break;
Ian Rogers706a10e2012-03-23 17:00:55 -0700905 case 0xE9: opcode << "jmp"; branch_bytes = 4; break;
906 case 0xEB: opcode << "jmp"; branch_bytes = 1; break;
jeffhao77ae36b2012-08-07 14:18:16 -0700907 case 0xF5: opcode << "cmc"; break;
jeffhao174651d2012-04-19 15:27:22 -0700908 case 0xF6: case 0xF7:
909 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 /"};
910 modrm_opcodes = f7_opcodes;
911 has_modrm = true;
912 reg_is_opcode = true;
913 store = true;
914 immediate_bytes = ((instr[1] & 0x38) == 0) ? 1 : 0;
915 break;
Ian Rogers706a10e2012-03-23 17:00:55 -0700916 case 0xFF:
917 static const char* ff_opcodes[] = {"inc", "dec", "call", "call", "jmp", "jmp", "push", "unknown-ff"};
918 modrm_opcodes = ff_opcodes;
919 has_modrm = true;
920 reg_is_opcode = true;
921 load = true;
922 break;
923 default:
924 opcode << StringPrintf("unknown opcode '%02X'", *instr);
925 break;
926 }
927 std::ostringstream args;
928 if (reg_in_opcode) {
929 DCHECK(!has_modrm);
Vladimir Kostyukovfba52f12014-04-15 15:41:47 +0700930 DumpBaseReg(args, rex, *instr & 0x7);
Ian Rogers706a10e2012-03-23 17:00:55 -0700931 }
932 instr++;
Elliott Hughes92301d92012-04-10 15:57:52 -0700933 uint32_t address_bits = 0;
Ian Rogers706a10e2012-03-23 17:00:55 -0700934 if (has_modrm) {
935 uint8_t modrm = *instr;
936 instr++;
937 uint8_t mod = modrm >> 6;
938 uint8_t reg_or_opcode = (modrm >> 3) & 7;
939 uint8_t rm = modrm & 7;
940 std::ostringstream address;
Vladimir Kostyukove8861b32014-04-18 17:06:15 +0700941 if (mod == 0 && rm == 5) {
942 if (!supports_rex_) { // Absolute address.
943 address_bits = *reinterpret_cast<const uint32_t*>(instr);
944 address << StringPrintf("[0x%x]", address_bits);
945 } else { // 64-bit RIP relative addressing.
946 address << StringPrintf("[RIP + 0x%x]", *reinterpret_cast<const uint32_t*>(instr));
947 }
Ian Rogers706a10e2012-03-23 17:00:55 -0700948 instr += 4;
949 } else if (rm == 4 && mod != 3) { // SIB
950 uint8_t sib = *instr;
951 instr++;
Vladimir Kostyukove8861b32014-04-18 17:06:15 +0700952 uint8_t scale = (sib >> 6) & 3;
Ian Rogers706a10e2012-03-23 17:00:55 -0700953 uint8_t index = (sib >> 3) & 7;
954 uint8_t base = sib & 7;
955 address << "[";
956 if (base != 5 || mod != 0) {
Ian Rogers7caad772012-03-30 01:07:54 -0700957 DumpBaseReg(address, rex, base);
Ian Rogers706a10e2012-03-23 17:00:55 -0700958 if (index != 4) {
959 address << " + ";
960 }
961 }
962 if (index != 4) {
Ian Rogers7caad772012-03-30 01:07:54 -0700963 DumpIndexReg(address, rex, index);
Vladimir Kostyukove8861b32014-04-18 17:06:15 +0700964 if (scale != 0) {
965 address << StringPrintf(" * %d", 1 << scale);
Ian Rogers706a10e2012-03-23 17:00:55 -0700966 }
967 }
Vladimir Kostyukove8861b32014-04-18 17:06:15 +0700968 if (mod == 0) {
969 if (base == 5) {
970 if (index != 4) {
971 address << StringPrintf(" + %d", *reinterpret_cast<const int32_t*>(instr));
972 } else {
973 // 64-bit low 32-bit absolute address, redundant absolute address encoding on 32-bit.
974 address_bits = *reinterpret_cast<const uint32_t*>(instr);
975 address << StringPrintf("%d", address_bits);
976 }
977 instr += 4;
978 }
979 } else if (mod == 1) {
Ian Rogers706a10e2012-03-23 17:00:55 -0700980 address << StringPrintf(" + %d", *reinterpret_cast<const int8_t*>(instr));
981 instr++;
982 } else if (mod == 2) {
983 address << StringPrintf(" + %d", *reinterpret_cast<const int32_t*>(instr));
984 instr += 4;
985 }
986 address << "]";
987 } else {
Ian Rogersbf989802012-04-16 16:07:49 -0700988 if (mod == 3) {
jeffhao703f2cd2012-07-13 17:25:52 -0700989 if (!no_ops) {
990 DumpReg(address, rex, rm, byte_operand, prefix[2], load ? src_reg_file : dst_reg_file);
991 }
Ian Rogersbf989802012-04-16 16:07:49 -0700992 } else {
Ian Rogers706a10e2012-03-23 17:00:55 -0700993 address << "[";
Ian Rogersbf989802012-04-16 16:07:49 -0700994 DumpBaseReg(address, rex, rm);
995 if (mod == 1) {
996 address << StringPrintf(" + %d", *reinterpret_cast<const int8_t*>(instr));
997 instr++;
998 } else if (mod == 2) {
999 address << StringPrintf(" + %d", *reinterpret_cast<const int32_t*>(instr));
1000 instr += 4;
1001 }
Ian Rogers706a10e2012-03-23 17:00:55 -07001002 address << "]";
1003 }
1004 }
1005
Ian Rogers7caad772012-03-30 01:07:54 -07001006 if (reg_is_opcode && modrm_opcodes != NULL) {
Ian Rogers706a10e2012-03-23 17:00:55 -07001007 opcode << modrm_opcodes[reg_or_opcode];
1008 }
1009 if (load) {
1010 if (!reg_is_opcode) {
Ian Rogersbf989802012-04-16 16:07:49 -07001011 DumpReg(args, rex, reg_or_opcode, byte_operand, prefix[2], dst_reg_file);
Ian Rogers706a10e2012-03-23 17:00:55 -07001012 args << ", ";
1013 }
1014 DumpSegmentOverride(args, prefix[1]);
1015 args << address.str();
1016 } else {
1017 DCHECK(store);
1018 DumpSegmentOverride(args, prefix[1]);
1019 args << address.str();
1020 if (!reg_is_opcode) {
1021 args << ", ";
Ian Rogersbf989802012-04-16 16:07:49 -07001022 DumpReg(args, rex, reg_or_opcode, byte_operand, prefix[2], src_reg_file);
Ian Rogers706a10e2012-03-23 17:00:55 -07001023 }
1024 }
1025 }
1026 if (ax) {
jeffhaofdffdf82012-07-11 16:08:43 -07001027 // If this opcode implicitly uses ax, ax is always the first arg.
Ian Rogersbf989802012-04-16 16:07:49 -07001028 DumpReg(args, rex, 0 /* EAX */, byte_operand, prefix[2], GPR);
Ian Rogers706a10e2012-03-23 17:00:55 -07001029 }
jeffhaoe2962482012-06-28 11:29:57 -07001030 if (cx) {
1031 args << ", ";
1032 DumpReg(args, rex, 1 /* ECX */, true, prefix[2], GPR);
1033 }
Ian Rogers706a10e2012-03-23 17:00:55 -07001034 if (immediate_bytes > 0) {
jeffhaoe2962482012-06-28 11:29:57 -07001035 if (has_modrm || reg_in_opcode || ax || cx) {
Ian Rogers706a10e2012-03-23 17:00:55 -07001036 args << ", ";
1037 }
1038 if (immediate_bytes == 1) {
1039 args << StringPrintf("%d", *reinterpret_cast<const int8_t*>(instr));
1040 instr++;
1041 } else {
1042 CHECK_EQ(immediate_bytes, 4u);
Mark Mendell67d18be2014-05-30 15:05:09 -04001043 if (prefix[2] == 0x66) { // Operand size override from 32-bit to 16-bit.
1044 args << StringPrintf("%d", *reinterpret_cast<const int16_t*>(instr));
1045 instr += 2;
1046 } else {
1047 args << StringPrintf("%d", *reinterpret_cast<const int32_t*>(instr));
1048 instr += 4;
1049 }
Ian Rogers706a10e2012-03-23 17:00:55 -07001050 }
1051 } else if (branch_bytes > 0) {
1052 DCHECK(!has_modrm);
1053 int32_t displacement;
1054 if (branch_bytes == 1) {
1055 displacement = *reinterpret_cast<const int8_t*>(instr);
1056 instr++;
1057 } else {
1058 CHECK_EQ(branch_bytes, 4u);
1059 displacement = *reinterpret_cast<const int32_t*>(instr);
1060 instr += 4;
1061 }
Elliott Hughes14178a92012-04-16 17:24:51 -07001062 args << StringPrintf("%+d (%p)", displacement, instr + displacement);
Ian Rogers706a10e2012-03-23 17:00:55 -07001063 }
Ian Rogersdd7624d2014-03-14 17:43:00 -07001064 if (prefix[1] == kFs && !supports_rex_) {
Elliott Hughes92301d92012-04-10 15:57:52 -07001065 args << " ; ";
Ian Rogersdd7624d2014-03-14 17:43:00 -07001066 Thread::DumpThreadOffset<4>(args, address_bits);
1067 }
1068 if (prefix[1] == kGs && supports_rex_) {
1069 args << " ; ";
1070 Thread::DumpThreadOffset<8>(args, address_bits);
Elliott Hughes92301d92012-04-10 15:57:52 -07001071 }
Elliott Hughes28fa76d2012-04-09 17:31:46 -07001072 std::stringstream hex;
Ian Rogers706a10e2012-03-23 17:00:55 -07001073 for (size_t i = 0; begin_instr + i < instr; ++i) {
Elliott Hughes28fa76d2012-04-09 17:31:46 -07001074 hex << StringPrintf("%02X", begin_instr[i]);
Ian Rogers706a10e2012-03-23 17:00:55 -07001075 }
Ian Rogers5e588b32013-02-21 15:05:09 -08001076 std::stringstream prefixed_opcode;
1077 switch (prefix[0]) {
1078 case 0xF0: prefixed_opcode << "lock "; break;
1079 case 0xF2: prefixed_opcode << "repne "; break;
1080 case 0xF3: prefixed_opcode << "repe "; break;
1081 case 0: break;
1082 default: LOG(FATAL) << "Unreachable";
1083 }
1084 prefixed_opcode << opcode.str();
1085 os << StringPrintf("%p: %22s \t%-7s ", begin_instr, hex.str().c_str(),
1086 prefixed_opcode.str().c_str())
1087 << args.str() << '\n';
Ian Rogers706a10e2012-03-23 17:00:55 -07001088 return instr - begin_instr;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001089} // NOLINT(readability/fn_size)
Ian Rogers706a10e2012-03-23 17:00:55 -07001090
1091} // namespace x86
1092} // namespace art