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