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