Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 1 | //===-- X86IntelInstPrinter.cpp - Intel assembly instruction printing -----===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file includes code for rendering MCInst instances as Intel-style |
| 11 | // assembly. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | /* Capstone Disassembler Engine */ |
| 16 | /* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013> */ |
| 17 | |
| 18 | #include <ctype.h> |
| 19 | #include <inttypes.h> |
Axel 0vercl0k Souchet | 779d4c7 | 2014-05-08 23:44:49 +0100 | [diff] [blame] | 20 | #include <stdbool.h> |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 | |
| 25 | #include "../../utils.h" |
| 26 | #include "../../MCInst.h" |
| 27 | #include "../../SStream.h" |
Nguyen Anh Quynh | e51e227 | 2014-01-12 20:27:54 +0800 | [diff] [blame] | 28 | #include "../../MCRegisterInfo.h" |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 29 | |
Nguyen Anh Quynh | f328f30 | 2014-01-20 09:47:21 +0800 | [diff] [blame] | 30 | #include "X86Mapping.h" |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 31 | |
Nguyen Anh Quynh | e93179b | 2014-04-25 11:18:40 +0800 | [diff] [blame] | 32 | #define GET_INSTRINFO_ENUM |
| 33 | #ifdef CAPSTONE_X86_REDUCE |
| 34 | #include "X86GenInstrInfo_reduce.inc" |
| 35 | #else |
| 36 | #include "X86GenInstrInfo.inc" |
| 37 | #endif |
| 38 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 39 | static void printMemReference(MCInst *MI, unsigned Op, SStream *O); |
Nguyen Anh Quynh | 13f40d2 | 2014-02-07 22:06:33 +0800 | [diff] [blame] | 40 | static void printOperand(MCInst *MI, unsigned OpNo, SStream *O); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 41 | |
| 42 | static void printopaquemem(MCInst *MI, unsigned OpNo, SStream *O) |
| 43 | { |
Nguyen Anh Quynh | 38c1322 | 2014-01-19 17:23:08 +0800 | [diff] [blame] | 44 | SStream_concat(O, "ptr "); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 45 | printMemReference(MI, OpNo, O); |
| 46 | } |
| 47 | |
| 48 | static void printi8mem(MCInst *MI, unsigned OpNo, SStream *O) |
| 49 | { |
| 50 | SStream_concat(O, "byte ptr "); |
| 51 | printMemReference(MI, OpNo, O); |
| 52 | } |
| 53 | |
| 54 | static void printi16mem(MCInst *MI, unsigned OpNo, SStream *O) |
| 55 | { |
Nguyen Anh Quynh | e93179b | 2014-04-25 11:18:40 +0800 | [diff] [blame] | 56 | if (MI->Opcode == X86_BOUNDS16rm) |
| 57 | SStream_concat(O, "dword ptr "); |
| 58 | else |
| 59 | SStream_concat(O, "word ptr "); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 60 | printMemReference(MI, OpNo, O); |
| 61 | } |
| 62 | |
| 63 | static void printi32mem(MCInst *MI, unsigned OpNo, SStream *O) |
| 64 | { |
Nguyen Anh Quynh | e93179b | 2014-04-25 11:18:40 +0800 | [diff] [blame] | 65 | if (MI->Opcode == X86_BOUNDS32rm) |
| 66 | SStream_concat(O, "qword ptr "); |
| 67 | else |
| 68 | SStream_concat(O, "dword ptr "); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 69 | printMemReference(MI, OpNo, O); |
| 70 | } |
| 71 | |
| 72 | static void printi64mem(MCInst *MI, unsigned OpNo, SStream *O) |
| 73 | { |
| 74 | SStream_concat(O, "qword ptr "); |
| 75 | printMemReference(MI, OpNo, O); |
| 76 | } |
| 77 | |
| 78 | static void printi128mem(MCInst *MI, unsigned OpNo, SStream *O) |
| 79 | { |
| 80 | SStream_concat(O, "xmmword ptr "); |
| 81 | printMemReference(MI, OpNo, O); |
| 82 | } |
| 83 | |
Nguyen Anh Quynh | 59b5489 | 2014-03-27 10:54:44 +0800 | [diff] [blame] | 84 | #ifndef CAPSTONE_X86_REDUCE |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 85 | static void printi256mem(MCInst *MI, unsigned OpNo, SStream *O) |
| 86 | { |
| 87 | SStream_concat(O, "ymmword ptr "); |
| 88 | printMemReference(MI, OpNo, O); |
| 89 | } |
| 90 | |
| 91 | static void printi512mem(MCInst *MI, unsigned OpNo, SStream *O) |
| 92 | { |
| 93 | SStream_concat(O, "zmmword ptr "); |
| 94 | printMemReference(MI, OpNo, O); |
| 95 | } |
| 96 | |
| 97 | static void printf32mem(MCInst *MI, unsigned OpNo, SStream *O) |
| 98 | { |
| 99 | SStream_concat(O, "dword ptr "); |
| 100 | printMemReference(MI, OpNo, O); |
| 101 | } |
| 102 | |
| 103 | static void printf64mem(MCInst *MI, unsigned OpNo, SStream *O) |
| 104 | { |
| 105 | SStream_concat(O, "qword ptr "); |
| 106 | printMemReference(MI, OpNo, O); |
| 107 | } |
| 108 | |
| 109 | static void printf80mem(MCInst *MI, unsigned OpNo, SStream *O) |
| 110 | { |
| 111 | SStream_concat(O, "xword ptr "); |
| 112 | printMemReference(MI, OpNo, O); |
| 113 | } |
| 114 | |
| 115 | static void printf128mem(MCInst *MI, unsigned OpNo, SStream *O) |
| 116 | { |
| 117 | SStream_concat(O, "xmmword ptr "); |
| 118 | printMemReference(MI, OpNo, O); |
| 119 | } |
| 120 | |
| 121 | static void printf256mem(MCInst *MI, unsigned OpNo, SStream *O) |
| 122 | { |
| 123 | SStream_concat(O, "ymmword ptr "); |
| 124 | printMemReference(MI, OpNo, O); |
| 125 | } |
| 126 | |
| 127 | static void printf512mem(MCInst *MI, unsigned OpNo, SStream *O) |
| 128 | { |
| 129 | SStream_concat(O, "zmmword ptr "); |
| 130 | printMemReference(MI, OpNo, O); |
| 131 | } |
| 132 | |
Nguyen Anh Quynh | 9518148 | 2014-03-25 23:20:41 +0800 | [diff] [blame] | 133 | static void printSSECC(MCInst *MI, unsigned Op, SStream *OS) |
| 134 | { |
| 135 | int64_t Imm = MCOperand_getImm(MCInst_getOperand(MI, Op)) & 0xf; |
| 136 | switch (Imm) { |
| 137 | default: break; // never reach |
| 138 | case 0: SStream_concat(OS, "eq"); break; |
| 139 | case 1: SStream_concat(OS, "lt"); break; |
| 140 | case 2: SStream_concat(OS, "le"); break; |
| 141 | case 3: SStream_concat(OS, "unord"); break; |
| 142 | case 4: SStream_concat(OS, "neq"); break; |
| 143 | case 5: SStream_concat(OS, "nlt"); break; |
| 144 | case 6: SStream_concat(OS, "nle"); break; |
| 145 | case 7: SStream_concat(OS, "ord"); break; |
| 146 | case 8: SStream_concat(OS, "eq_uq"); break; |
| 147 | case 9: SStream_concat(OS, "nge"); break; |
| 148 | case 0xa: SStream_concat(OS, "ngt"); break; |
| 149 | case 0xb: SStream_concat(OS, "false"); break; |
| 150 | case 0xc: SStream_concat(OS, "neq_oq"); break; |
| 151 | case 0xd: SStream_concat(OS, "ge"); break; |
| 152 | case 0xe: SStream_concat(OS, "gt"); break; |
| 153 | case 0xf: SStream_concat(OS, "true"); break; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | static void printAVXCC(MCInst *MI, unsigned Op, SStream *O) |
| 158 | { |
| 159 | int64_t Imm = MCOperand_getImm(MCInst_getOperand(MI, Op)) & 0x1f; |
| 160 | switch (Imm) { |
| 161 | default: break;//printf("Invalid avxcc argument!\n"); break; |
| 162 | case 0: SStream_concat(O, "eq"); break; |
| 163 | case 1: SStream_concat(O, "lt"); break; |
| 164 | case 2: SStream_concat(O, "le"); break; |
| 165 | case 3: SStream_concat(O, "unord"); break; |
| 166 | case 4: SStream_concat(O, "neq"); break; |
| 167 | case 5: SStream_concat(O, "nlt"); break; |
| 168 | case 6: SStream_concat(O, "nle"); break; |
| 169 | case 7: SStream_concat(O, "ord"); break; |
| 170 | case 8: SStream_concat(O, "eq_uq"); break; |
| 171 | case 9: SStream_concat(O, "nge"); break; |
| 172 | case 0xa: SStream_concat(O, "ngt"); break; |
| 173 | case 0xb: SStream_concat(O, "false"); break; |
| 174 | case 0xc: SStream_concat(O, "neq_oq"); break; |
| 175 | case 0xd: SStream_concat(O, "ge"); break; |
| 176 | case 0xe: SStream_concat(O, "gt"); break; |
| 177 | case 0xf: SStream_concat(O, "true"); break; |
| 178 | case 0x10: SStream_concat(O, "eq_os"); break; |
| 179 | case 0x11: SStream_concat(O, "lt_oq"); break; |
| 180 | case 0x12: SStream_concat(O, "le_oq"); break; |
| 181 | case 0x13: SStream_concat(O, "unord_s"); break; |
| 182 | case 0x14: SStream_concat(O, "neq_us"); break; |
| 183 | case 0x15: SStream_concat(O, "nlt_uq"); break; |
| 184 | case 0x16: SStream_concat(O, "nle_uq"); break; |
| 185 | case 0x17: SStream_concat(O, "ord_s"); break; |
| 186 | case 0x18: SStream_concat(O, "eq_us"); break; |
| 187 | case 0x19: SStream_concat(O, "nge_uq"); break; |
| 188 | case 0x1a: SStream_concat(O, "ngt_uq"); break; |
| 189 | case 0x1b: SStream_concat(O, "false_os"); break; |
| 190 | case 0x1c: SStream_concat(O, "neq_os"); break; |
| 191 | case 0x1d: SStream_concat(O, "ge_oq"); break; |
| 192 | case 0x1e: SStream_concat(O, "gt_oq"); break; |
| 193 | case 0x1f: SStream_concat(O, "true_us"); break; |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | static void printRoundingControl(MCInst *MI, unsigned Op, SStream *O) |
| 198 | { |
| 199 | int64_t Imm = MCOperand_getImm(MCInst_getOperand(MI, Op)) & 0x3; |
| 200 | switch (Imm) { |
| 201 | case 0: SStream_concat(O, "{rn-sae}"); break; |
| 202 | case 1: SStream_concat(O, "{rd-sae}"); break; |
| 203 | case 2: SStream_concat(O, "{ru-sae}"); break; |
| 204 | case 3: SStream_concat(O, "{rz-sae}"); break; |
| 205 | default: break; // never reach |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | #endif |
| 210 | |
Nguyen Anh Quynh | 13f40d2 | 2014-02-07 22:06:33 +0800 | [diff] [blame] | 211 | static void printSrcIdx(MCInst *MI, unsigned Op, SStream *O) |
| 212 | { |
| 213 | MCOperand *SegReg; |
Nguyen Anh Quynh | 42706a3 | 2014-05-09 07:33:35 +0800 | [diff] [blame^] | 214 | |
Nguyen Anh Quynh | 13f40d2 | 2014-02-07 22:06:33 +0800 | [diff] [blame] | 215 | SegReg = MCInst_getOperand(MI, Op+1); |
| 216 | |
| 217 | // If this has a segment register, print it. |
| 218 | if (MCOperand_getReg(SegReg)) { |
| 219 | printOperand(MI, Op+1, O); |
| 220 | SStream_concat(O, ":"); |
| 221 | } |
| 222 | |
| 223 | SStream_concat(O, "["); |
| 224 | printOperand(MI, Op, O); |
| 225 | SStream_concat(O, "]"); |
| 226 | } |
| 227 | |
| 228 | static void printDstIdx(MCInst *MI, unsigned Op, SStream *O) |
| 229 | { |
| 230 | // DI accesses are always ES-based. |
| 231 | SStream_concat(O, "es:["); |
| 232 | printOperand(MI, Op, O); |
| 233 | SStream_concat(O, "]"); |
| 234 | } |
| 235 | |
| 236 | void printSrcIdx8(MCInst *MI, unsigned OpNo, SStream *O) |
| 237 | { |
| 238 | SStream_concat(O, "byte ptr "); |
| 239 | printSrcIdx(MI, OpNo, O); |
| 240 | } |
| 241 | |
| 242 | void printSrcIdx16(MCInst *MI, unsigned OpNo, SStream *O) |
| 243 | { |
| 244 | SStream_concat(O, "word ptr "); |
| 245 | printSrcIdx(MI, OpNo, O); |
| 246 | } |
| 247 | |
| 248 | void printSrcIdx32(MCInst *MI, unsigned OpNo, SStream *O) |
| 249 | { |
| 250 | SStream_concat(O, "dword ptr "); |
| 251 | printSrcIdx(MI, OpNo, O); |
| 252 | } |
| 253 | |
| 254 | void printSrcIdx64(MCInst *MI, unsigned OpNo, SStream *O) |
| 255 | { |
| 256 | SStream_concat(O, "qword ptr "); |
| 257 | printSrcIdx(MI, OpNo, O); |
| 258 | } |
| 259 | |
| 260 | void printDstIdx8(MCInst *MI, unsigned OpNo, SStream *O) |
| 261 | { |
| 262 | SStream_concat(O, "byte ptr "); |
| 263 | printDstIdx(MI, OpNo, O); |
| 264 | } |
| 265 | |
| 266 | void printDstIdx16(MCInst *MI, unsigned OpNo, SStream *O) |
| 267 | { |
| 268 | SStream_concat(O, "word ptr "); |
| 269 | printDstIdx(MI, OpNo, O); |
| 270 | } |
| 271 | |
| 272 | void printDstIdx32(MCInst *MI, unsigned OpNo, SStream *O) |
| 273 | { |
| 274 | SStream_concat(O, "dword ptr "); |
| 275 | printDstIdx(MI, OpNo, O); |
| 276 | } |
| 277 | |
| 278 | void printDstIdx64(MCInst *MI, unsigned OpNo, SStream *O) |
| 279 | { |
| 280 | SStream_concat(O, "qword ptr "); |
| 281 | printDstIdx(MI, OpNo, O); |
| 282 | } |
| 283 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 284 | static void printMemOffset(MCInst *MI, unsigned Op, SStream *O) |
| 285 | { |
| 286 | MCOperand *DispSpec = MCInst_getOperand(MI, Op); |
Nguyen Anh Quynh | 13f40d2 | 2014-02-07 22:06:33 +0800 | [diff] [blame] | 287 | MCOperand *SegReg = MCInst_getOperand(MI, Op+1); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 288 | |
Nguyen Anh Quynh | 42c6b1a | 2013-12-30 00:15:25 +0800 | [diff] [blame] | 289 | if (MI->csh->detail) { |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 290 | MI->flat_insn.x86.operands[MI->flat_insn.x86.op_count].type = X86_OP_MEM; |
| 291 | MI->flat_insn.x86.operands[MI->flat_insn.x86.op_count].mem.base = X86_REG_INVALID; |
| 292 | MI->flat_insn.x86.operands[MI->flat_insn.x86.op_count].mem.index = X86_REG_INVALID; |
| 293 | MI->flat_insn.x86.operands[MI->flat_insn.x86.op_count].mem.scale = 1; |
| 294 | MI->flat_insn.x86.operands[MI->flat_insn.x86.op_count].mem.disp = 0; |
Nguyen Anh Quynh | a209e67 | 2013-12-14 00:23:41 +0800 | [diff] [blame] | 295 | } |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 296 | |
Nguyen Anh Quynh | 13f40d2 | 2014-02-07 22:06:33 +0800 | [diff] [blame] | 297 | // If this has a segment register, print it. |
| 298 | if (MCOperand_getReg(SegReg)) { |
| 299 | printOperand(MI, Op+1, O); |
| 300 | SStream_concat(O, ":"); |
| 301 | } |
| 302 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 303 | SStream_concat(O, "["); |
| 304 | |
| 305 | if (MCOperand_isImm(DispSpec)) { |
| 306 | int64_t imm = MCOperand_getImm(DispSpec); |
Nguyen Anh Quynh | 42c6b1a | 2013-12-30 00:15:25 +0800 | [diff] [blame] | 307 | if (MI->csh->detail) |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 308 | MI->flat_insn.x86.operands[MI->flat_insn.x86.op_count].mem.disp = imm; |
Nguyen Anh Quynh | be90639 | 2013-12-13 15:37:57 +0800 | [diff] [blame] | 309 | if (imm < 0) { |
Nguyen Anh Quynh | 6d3d800 | 2014-03-29 17:26:51 +0800 | [diff] [blame] | 310 | SStream_concat(O, "0x%"PRIx64, arch_masks[MI->csh->mode] & imm); |
Nguyen Anh Quynh | be90639 | 2013-12-13 15:37:57 +0800 | [diff] [blame] | 311 | } else { |
Nguyen Anh Quynh | f22557b | 2013-12-13 09:37:52 +0800 | [diff] [blame] | 312 | if (imm > HEX_THRESHOLD) |
| 313 | SStream_concat(O, "0x%"PRIx64, imm); |
| 314 | else |
| 315 | SStream_concat(O, "%"PRIu64, imm); |
| 316 | } |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | SStream_concat(O, "]"); |
| 320 | |
Nguyen Anh Quynh | 42c6b1a | 2013-12-30 00:15:25 +0800 | [diff] [blame] | 321 | if (MI->csh->detail) |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 322 | MI->flat_insn.x86.op_count++; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | static void printMemOffs8(MCInst *MI, unsigned OpNo, SStream *O) |
| 326 | { |
| 327 | SStream_concat(O, "byte ptr "); |
Nguyen Anh Quynh | b9b3d29 | 2013-12-02 16:21:55 +0800 | [diff] [blame] | 328 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 329 | printMemOffset(MI, OpNo, O); |
| 330 | } |
| 331 | |
| 332 | static void printMemOffs16(MCInst *MI, unsigned OpNo, SStream *O) |
| 333 | { |
| 334 | SStream_concat(O, "word ptr "); |
Nguyen Anh Quynh | b9b3d29 | 2013-12-02 16:21:55 +0800 | [diff] [blame] | 335 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 336 | printMemOffset(MI, OpNo, O); |
| 337 | |
| 338 | } |
| 339 | |
| 340 | static void printMemOffs32(MCInst *MI, unsigned OpNo, SStream *O) |
| 341 | { |
| 342 | SStream_concat(O, "dword ptr "); |
Nguyen Anh Quynh | b9b3d29 | 2013-12-02 16:21:55 +0800 | [diff] [blame] | 343 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 344 | printMemOffset(MI, OpNo, O); |
| 345 | } |
| 346 | |
| 347 | static void printMemOffs64(MCInst *MI, unsigned OpNo, SStream *O) |
| 348 | { |
| 349 | SStream_concat(O, "qword ptr "); |
| 350 | printMemOffset(MI, OpNo, O); |
| 351 | } |
| 352 | |
Nguyen Anh Quynh | 27b9a96 | 2014-02-19 10:13:47 +0800 | [diff] [blame] | 353 | static char *printAliasInstr(MCInst *MI, SStream *OS, void *info); |
Nguyen Anh Quynh | e51e227 | 2014-01-12 20:27:54 +0800 | [diff] [blame] | 354 | static void printInstruction(MCInst *MI, SStream *O, MCRegisterInfo *MRI); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 355 | void X86_Intel_printInst(MCInst *MI, SStream *O, void *Info) |
| 356 | { |
Nguyen Anh Quynh | 27b9a96 | 2014-02-19 10:13:47 +0800 | [diff] [blame] | 357 | char *mnem; |
Nguyen Anh Quynh | 85cddef | 2014-02-18 11:59:36 +0800 | [diff] [blame] | 358 | x86_reg reg; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 359 | |
Nguyen Anh Quynh | 005c514 | 2014-02-18 11:11:46 +0800 | [diff] [blame] | 360 | // Try to print any aliases first. |
Nguyen Anh Quynh | 27b9a96 | 2014-02-19 10:13:47 +0800 | [diff] [blame] | 361 | mnem = printAliasInstr(MI, O, NULL); |
| 362 | if (mnem) |
| 363 | cs_mem_free(mnem); |
| 364 | else |
Nguyen Anh Quynh | e51e227 | 2014-01-12 20:27:54 +0800 | [diff] [blame] | 365 | printInstruction(MI, O, NULL); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 366 | |
Nguyen Anh Quynh | 42c6b1a | 2013-12-30 00:15:25 +0800 | [diff] [blame] | 367 | if (MI->csh->detail) { |
Nguyen Anh Quynh | 85cddef | 2014-02-18 11:59:36 +0800 | [diff] [blame] | 368 | // first op can be embedded in the asm by llvm. |
| 369 | // so we have to handle that case to not miss the first op. |
Nguyen Anh Quynh | 27b9a96 | 2014-02-19 10:13:47 +0800 | [diff] [blame] | 370 | reg = X86_insn_reg(MCInst_getOpcode(MI)); |
Nguyen Anh Quynh | 85cddef | 2014-02-18 11:59:36 +0800 | [diff] [blame] | 371 | if (reg) { |
| 372 | // shift all the ops right to leave 1st slot for this new register op |
| 373 | memmove(&(MI->flat_insn.x86.operands[1]), &(MI->flat_insn.x86.operands[0]), |
| 374 | sizeof(MI->flat_insn.x86.operands[0]) * (ARR_SIZE(MI->flat_insn.x86.operands) - 1)); |
| 375 | MI->flat_insn.x86.operands[0].type = X86_OP_REG; |
| 376 | MI->flat_insn.x86.operands[0].reg = reg; |
| 377 | MI->flat_insn.x86.op_count++; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 378 | } |
Nguyen Anh Quynh | 85cddef | 2014-02-18 11:59:36 +0800 | [diff] [blame] | 379 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 380 | } |
| 381 | } |
| 382 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 383 | /// printPCRelImm - This is used to print an immediate value that ends up |
| 384 | /// being encoded as a pc-relative value. |
| 385 | static void printPCRelImm(MCInst *MI, unsigned OpNo, SStream *O) |
| 386 | { |
| 387 | MCOperand *Op = MCInst_getOperand(MI, OpNo); |
| 388 | if (MCOperand_isImm(Op)) { |
Nguyen Anh Quynh | a209e67 | 2013-12-14 00:23:41 +0800 | [diff] [blame] | 389 | int64_t imm = MCOperand_getImm(Op) + MI->insn_size + MI->address; |
Nguyen Anh Quynh | be90639 | 2013-12-13 15:37:57 +0800 | [diff] [blame] | 390 | if (imm < 0) { |
Nguyen Anh Quynh | 017df60 | 2014-03-20 15:38:51 +0800 | [diff] [blame] | 391 | if (imm < -HEX_THRESHOLD) |
Nguyen Anh Quynh | be90639 | 2013-12-13 15:37:57 +0800 | [diff] [blame] | 392 | SStream_concat(O, "-0x%"PRIx64, -imm); |
| 393 | else |
| 394 | SStream_concat(O, "-%"PRIu64, -imm); |
| 395 | } else { |
Nguyen Anh Quynh | f22557b | 2013-12-13 09:37:52 +0800 | [diff] [blame] | 396 | if (imm > HEX_THRESHOLD) |
| 397 | SStream_concat(O, "0x%"PRIx64, imm); |
| 398 | else |
| 399 | SStream_concat(O, "%"PRIu64, imm); |
| 400 | } |
Nguyen Anh Quynh | 42c6b1a | 2013-12-30 00:15:25 +0800 | [diff] [blame] | 401 | if (MI->csh->detail) { |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 402 | MI->flat_insn.x86.operands[MI->flat_insn.x86.op_count].type = X86_OP_IMM; |
| 403 | MI->flat_insn.x86.operands[MI->flat_insn.x86.op_count].imm = imm; |
| 404 | MI->flat_insn.x86.op_count++; |
Nguyen Anh Quynh | a209e67 | 2013-12-14 00:23:41 +0800 | [diff] [blame] | 405 | } |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 406 | } |
| 407 | } |
| 408 | |
Nguyen Anh Quynh | 9dfdae6 | 2014-01-19 22:40:31 +0800 | [diff] [blame] | 409 | static char *getRegisterName(unsigned RegNo); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 410 | static void printRegName(SStream *OS, unsigned RegNo) |
| 411 | { |
| 412 | SStream_concat(OS, getRegisterName(RegNo)); |
| 413 | } |
| 414 | |
| 415 | static void printOperand(MCInst *MI, unsigned OpNo, SStream *O) |
| 416 | { |
| 417 | MCOperand *Op = MCInst_getOperand(MI, OpNo); |
| 418 | if (MCOperand_isReg(Op)) { |
| 419 | printRegName(O, MCOperand_getReg(Op)); |
Nguyen Anh Quynh | 42c6b1a | 2013-12-30 00:15:25 +0800 | [diff] [blame] | 420 | if (MI->csh->detail) { |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 421 | MI->flat_insn.x86.operands[MI->flat_insn.x86.op_count].type = X86_OP_REG; |
| 422 | MI->flat_insn.x86.operands[MI->flat_insn.x86.op_count].reg = MCOperand_getReg(Op); |
| 423 | MI->flat_insn.x86.op_count++; |
Nguyen Anh Quynh | a209e67 | 2013-12-14 00:23:41 +0800 | [diff] [blame] | 424 | } |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 425 | } else if (MCOperand_isImm(Op)) { |
| 426 | int64_t imm = MCOperand_getImm(Op); |
Nguyen Anh Quynh | 94990c9 | 2013-12-13 15:56:08 +0800 | [diff] [blame] | 427 | if (imm >= 0) { |
Nguyen Anh Quynh | be90639 | 2013-12-13 15:37:57 +0800 | [diff] [blame] | 428 | if (imm > HEX_THRESHOLD) |
| 429 | SStream_concat(O, "0x%"PRIx64, imm); |
| 430 | else |
| 431 | SStream_concat(O, "%"PRIu64, imm); |
| 432 | } else { |
Nguyen Anh Quynh | 81a6df4 | 2014-04-01 07:23:39 +0800 | [diff] [blame] | 433 | if (imm < -HEX_THRESHOLD) |
| 434 | SStream_concat(O, "-0x%"PRIx64, -imm); |
| 435 | else |
| 436 | SStream_concat(O, "-%"PRIu64, -imm); |
Nguyen Anh Quynh | be90639 | 2013-12-13 15:37:57 +0800 | [diff] [blame] | 437 | } |
| 438 | |
Nguyen Anh Quynh | 42c6b1a | 2013-12-30 00:15:25 +0800 | [diff] [blame] | 439 | if (MI->csh->detail) { |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 440 | MI->flat_insn.x86.operands[MI->flat_insn.x86.op_count].type = X86_OP_IMM; |
| 441 | MI->flat_insn.x86.operands[MI->flat_insn.x86.op_count].imm = imm; |
| 442 | MI->flat_insn.x86.op_count++; |
Nguyen Anh Quynh | a209e67 | 2013-12-14 00:23:41 +0800 | [diff] [blame] | 443 | } |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 444 | } |
| 445 | } |
| 446 | |
| 447 | // local printOperand, without updating public operands |
| 448 | static void _printOperand(MCInst *MI, unsigned OpNo, SStream *O) |
| 449 | { |
| 450 | MCOperand *Op = MCInst_getOperand(MI, OpNo); |
| 451 | if (MCOperand_isReg(Op)) { |
| 452 | printRegName(O, MCOperand_getReg(Op)); |
| 453 | } else if (MCOperand_isImm(Op)) { |
| 454 | int64_t imm = MCOperand_getImm(Op); |
Nguyen Anh Quynh | be90639 | 2013-12-13 15:37:57 +0800 | [diff] [blame] | 455 | if (imm < 0) { |
Nguyen Anh Quynh | 81a6df4 | 2014-04-01 07:23:39 +0800 | [diff] [blame] | 456 | if (imm < -HEX_THRESHOLD) |
| 457 | SStream_concat(O, "-0x%"PRIx64, -imm); |
| 458 | else |
| 459 | SStream_concat(O, "-%"PRIu64, -imm); |
| 460 | |
Nguyen Anh Quynh | be90639 | 2013-12-13 15:37:57 +0800 | [diff] [blame] | 461 | } else { |
Nguyen Anh Quynh | f22557b | 2013-12-13 09:37:52 +0800 | [diff] [blame] | 462 | if (imm > HEX_THRESHOLD) |
| 463 | SStream_concat(O, "0x%"PRIx64, imm); |
| 464 | else |
| 465 | SStream_concat(O, "%"PRIu64, imm); |
| 466 | } |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 467 | } |
| 468 | } |
| 469 | |
Nguyen Anh Quynh | 125f504 | 2014-03-29 12:02:21 +0800 | [diff] [blame] | 470 | static void printMemReference(MCInst *MI, unsigned Op, SStream *O) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 471 | { |
Nguyen Anh Quynh | 42706a3 | 2014-05-09 07:33:35 +0800 | [diff] [blame^] | 472 | bool NeedPlus = false; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 473 | MCOperand *BaseReg = MCInst_getOperand(MI, Op); |
Alex Ionescu | 46018db | 2014-01-22 09:45:00 -0800 | [diff] [blame] | 474 | uint64_t ScaleVal = MCOperand_getImm(MCInst_getOperand(MI, Op+1)); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 475 | MCOperand *IndexReg = MCInst_getOperand(MI, Op+2); |
| 476 | MCOperand *DispSpec = MCInst_getOperand(MI, Op+3); |
| 477 | MCOperand *SegReg = MCInst_getOperand(MI, Op+4); |
| 478 | |
Nguyen Anh Quynh | 42c6b1a | 2013-12-30 00:15:25 +0800 | [diff] [blame] | 479 | if (MI->csh->detail) { |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 480 | MI->flat_insn.x86.operands[MI->flat_insn.x86.op_count].type = X86_OP_MEM; |
| 481 | MI->flat_insn.x86.operands[MI->flat_insn.x86.op_count].mem.base = MCOperand_getReg(BaseReg); |
| 482 | MI->flat_insn.x86.operands[MI->flat_insn.x86.op_count].mem.index = MCOperand_getReg(IndexReg); |
Alex Ionescu | 46018db | 2014-01-22 09:45:00 -0800 | [diff] [blame] | 483 | MI->flat_insn.x86.operands[MI->flat_insn.x86.op_count].mem.scale = (int)ScaleVal; |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 484 | MI->flat_insn.x86.operands[MI->flat_insn.x86.op_count].mem.disp = 0; |
Nguyen Anh Quynh | a209e67 | 2013-12-14 00:23:41 +0800 | [diff] [blame] | 485 | } |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 486 | |
| 487 | // If this has a segment register, print it. |
| 488 | if (MCOperand_getReg(SegReg)) { |
| 489 | _printOperand(MI, Op+4, O); |
| 490 | SStream_concat(O, ":"); |
| 491 | } |
| 492 | |
| 493 | SStream_concat(O, "["); |
| 494 | |
Axel 0vercl0k Souchet | 779d4c7 | 2014-05-08 23:44:49 +0100 | [diff] [blame] | 495 | NeedPlus = false; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 496 | if (MCOperand_getReg(BaseReg)) { |
| 497 | _printOperand(MI, Op, O); |
| 498 | NeedPlus = true; |
| 499 | } |
| 500 | |
| 501 | if (MCOperand_getReg(IndexReg)) { |
| 502 | if (NeedPlus) SStream_concat(O, " + "); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 503 | _printOperand(MI, Op+2, O); |
Nguyen Anh Quynh | f9e3216 | 2013-12-05 21:58:31 +0800 | [diff] [blame] | 504 | if (ScaleVal != 1) |
| 505 | SStream_concat(O, "*%u", ScaleVal); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 506 | NeedPlus = true; |
| 507 | } |
| 508 | |
| 509 | if (!MCOperand_isImm(DispSpec)) { |
| 510 | if (NeedPlus) SStream_concat(O, " + "); |
| 511 | //assert(DispSpec.isExpr() && "non-immediate displacement for LEA?"); |
| 512 | } else { |
| 513 | int64_t DispVal = MCOperand_getImm(DispSpec); |
Nguyen Anh Quynh | 42c6b1a | 2013-12-30 00:15:25 +0800 | [diff] [blame] | 514 | if (MI->csh->detail) |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 515 | MI->flat_insn.x86.operands[MI->flat_insn.x86.op_count].mem.disp = DispVal; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 516 | if (DispVal || (!MCOperand_getReg(IndexReg) && !MCOperand_getReg(BaseReg))) { |
| 517 | if (NeedPlus) { |
Nguyen Anh Quynh | 125f504 | 2014-03-29 12:02:21 +0800 | [diff] [blame] | 518 | SStream_concat(O, " + "); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 519 | } |
Nguyen Anh Quynh | 125f504 | 2014-03-29 12:02:21 +0800 | [diff] [blame] | 520 | |
Nguyen Anh Quynh | be90639 | 2013-12-13 15:37:57 +0800 | [diff] [blame] | 521 | if (DispVal < 0) { |
Nguyen Anh Quynh | 6d3d800 | 2014-03-29 17:26:51 +0800 | [diff] [blame] | 522 | SStream_concat(O, "0x%"PRIx64, arch_masks[MI->csh->mode] & DispVal); |
Nguyen Anh Quynh | be90639 | 2013-12-13 15:37:57 +0800 | [diff] [blame] | 523 | } else { |
Nguyen Anh Quynh | f22557b | 2013-12-13 09:37:52 +0800 | [diff] [blame] | 524 | if (DispVal > HEX_THRESHOLD) |
| 525 | SStream_concat(O, "0x%"PRIx64, DispVal); |
| 526 | else |
| 527 | SStream_concat(O, "%"PRIu64, DispVal); |
| 528 | } |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 529 | } |
| 530 | } |
| 531 | |
| 532 | SStream_concat(O, "]"); |
Nguyen Anh Quynh | a209e67 | 2013-12-14 00:23:41 +0800 | [diff] [blame] | 533 | |
Nguyen Anh Quynh | 42c6b1a | 2013-12-30 00:15:25 +0800 | [diff] [blame] | 534 | if (MI->csh->detail) |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 535 | MI->flat_insn.x86.op_count++; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 536 | } |
| 537 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 538 | #define PRINT_ALIAS_INSTR |
Nguyen Anh Quynh | 59b5489 | 2014-03-27 10:54:44 +0800 | [diff] [blame] | 539 | #ifdef CAPSTONE_X86_REDUCE |
| 540 | #include "X86GenAsmWriter1_reduce.inc" |
Nguyen Anh Quynh | 9518148 | 2014-03-25 23:20:41 +0800 | [diff] [blame] | 541 | #else |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 542 | #include "X86GenAsmWriter1.inc" |
Nguyen Anh Quynh | 9518148 | 2014-03-25 23:20:41 +0800 | [diff] [blame] | 543 | #endif |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 544 | |