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 | |
Nguyen Anh Quynh | bb0744d | 2014-05-12 13:41:49 +0800 | [diff] [blame] | 15 | /* Capstone Disassembly Engine */ |
| 16 | /* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2014 */ |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 17 | |
Nguyen Anh Quynh | 8598a21 | 2014-05-14 11:26:41 +0800 | [diff] [blame] | 18 | #ifdef CAPSTONE_HAS_X86 |
| 19 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 20 | #include <ctype.h> |
| 21 | #include <inttypes.h> |
| 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | |
| 26 | #include "../../utils.h" |
| 27 | #include "../../MCInst.h" |
| 28 | #include "../../SStream.h" |
Nguyen Anh Quynh | e51e227 | 2014-01-12 20:27:54 +0800 | [diff] [blame] | 29 | #include "../../MCRegisterInfo.h" |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 30 | |
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 | |
Nguyen Anh Quynh | e93179b | 2014-04-25 11:18:40 +0800 | [diff] [blame] | 33 | #define GET_INSTRINFO_ENUM |
| 34 | #ifdef CAPSTONE_X86_REDUCE |
| 35 | #include "X86GenInstrInfo_reduce.inc" |
| 36 | #else |
| 37 | #include "X86GenInstrInfo.inc" |
| 38 | #endif |
| 39 | |
Nguyen Anh Quynh | 0b69038 | 2014-08-13 13:01:50 +0800 | [diff] [blame^] | 40 | #include "X86BaseInfo.h" |
| 41 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 42 | static void printMemReference(MCInst *MI, unsigned Op, SStream *O); |
Nguyen Anh Quynh | 13f40d2 | 2014-02-07 22:06:33 +0800 | [diff] [blame] | 43 | static void printOperand(MCInst *MI, unsigned OpNo, SStream *O); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 44 | |
Nguyen Anh Quynh | 1085073 | 2014-06-18 12:16:24 +0800 | [diff] [blame] | 45 | |
| 46 | static void set_mem_access(MCInst *MI, bool status) |
| 47 | { |
| 48 | if (MI->csh->detail != CS_OPT_ON) |
| 49 | return; |
| 50 | |
| 51 | MI->csh->doing_mem = status; |
Nguyen Anh Quynh | 14ba46b | 2014-06-24 14:32:01 +0800 | [diff] [blame] | 52 | if (!status) |
| 53 | // done, create the next operand slot |
| 54 | MI->flat_insn->detail->x86.op_count++; |
| 55 | |
Nguyen Anh Quynh | 1085073 | 2014-06-18 12:16:24 +0800 | [diff] [blame] | 56 | } |
| 57 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 58 | static void printopaquemem(MCInst *MI, unsigned OpNo, SStream *O) |
| 59 | { |
Nguyen Anh Quynh | b76233c | 2014-06-04 18:31:02 +0800 | [diff] [blame] | 60 | SStream_concat0(O, "ptr "); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 61 | printMemReference(MI, OpNo, O); |
| 62 | } |
| 63 | |
| 64 | static void printi8mem(MCInst *MI, unsigned OpNo, SStream *O) |
| 65 | { |
Nguyen Anh Quynh | b76233c | 2014-06-04 18:31:02 +0800 | [diff] [blame] | 66 | SStream_concat0(O, "byte ptr "); |
Nguyen Anh Quynh | 1085073 | 2014-06-18 12:16:24 +0800 | [diff] [blame] | 67 | MI->x86opsize = 1; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 68 | printMemReference(MI, OpNo, O); |
| 69 | } |
| 70 | |
| 71 | static void printi16mem(MCInst *MI, unsigned OpNo, SStream *O) |
| 72 | { |
Nguyen Anh Quynh | 1085073 | 2014-06-18 12:16:24 +0800 | [diff] [blame] | 73 | if (MI->Opcode == X86_BOUNDS16rm) { |
Nguyen Anh Quynh | b76233c | 2014-06-04 18:31:02 +0800 | [diff] [blame] | 74 | SStream_concat0(O, "dword ptr "); |
Nguyen Anh Quynh | 1085073 | 2014-06-18 12:16:24 +0800 | [diff] [blame] | 75 | MI->x86opsize = 4; |
| 76 | } else { |
Nguyen Anh Quynh | b76233c | 2014-06-04 18:31:02 +0800 | [diff] [blame] | 77 | SStream_concat0(O, "word ptr "); |
Nguyen Anh Quynh | 1085073 | 2014-06-18 12:16:24 +0800 | [diff] [blame] | 78 | MI->x86opsize = 2; |
| 79 | } |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 80 | printMemReference(MI, OpNo, O); |
| 81 | } |
| 82 | |
| 83 | static void printi32mem(MCInst *MI, unsigned OpNo, SStream *O) |
| 84 | { |
Nguyen Anh Quynh | 1085073 | 2014-06-18 12:16:24 +0800 | [diff] [blame] | 85 | if (MI->Opcode == X86_BOUNDS32rm) { |
Nguyen Anh Quynh | b76233c | 2014-06-04 18:31:02 +0800 | [diff] [blame] | 86 | SStream_concat0(O, "qword ptr "); |
Nguyen Anh Quynh | 1085073 | 2014-06-18 12:16:24 +0800 | [diff] [blame] | 87 | MI->x86opsize = 8; |
| 88 | } else { |
Nguyen Anh Quynh | b76233c | 2014-06-04 18:31:02 +0800 | [diff] [blame] | 89 | SStream_concat0(O, "dword ptr "); |
Nguyen Anh Quynh | 1085073 | 2014-06-18 12:16:24 +0800 | [diff] [blame] | 90 | MI->x86opsize = 4; |
| 91 | } |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 92 | printMemReference(MI, OpNo, O); |
| 93 | } |
| 94 | |
| 95 | static void printi64mem(MCInst *MI, unsigned OpNo, SStream *O) |
| 96 | { |
Nguyen Anh Quynh | b76233c | 2014-06-04 18:31:02 +0800 | [diff] [blame] | 97 | SStream_concat0(O, "qword ptr "); |
Nguyen Anh Quynh | 1085073 | 2014-06-18 12:16:24 +0800 | [diff] [blame] | 98 | MI->x86opsize = 8; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 99 | printMemReference(MI, OpNo, O); |
| 100 | } |
| 101 | |
| 102 | static void printi128mem(MCInst *MI, unsigned OpNo, SStream *O) |
| 103 | { |
Nguyen Anh Quynh | b76233c | 2014-06-04 18:31:02 +0800 | [diff] [blame] | 104 | SStream_concat0(O, "xmmword ptr "); |
Nguyen Anh Quynh | 1085073 | 2014-06-18 12:16:24 +0800 | [diff] [blame] | 105 | MI->x86opsize = 16; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 106 | printMemReference(MI, OpNo, O); |
| 107 | } |
| 108 | |
Nguyen Anh Quynh | 59b5489 | 2014-03-27 10:54:44 +0800 | [diff] [blame] | 109 | #ifndef CAPSTONE_X86_REDUCE |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 110 | static void printi256mem(MCInst *MI, unsigned OpNo, SStream *O) |
| 111 | { |
Nguyen Anh Quynh | b76233c | 2014-06-04 18:31:02 +0800 | [diff] [blame] | 112 | SStream_concat0(O, "ymmword ptr "); |
Nguyen Anh Quynh | 1085073 | 2014-06-18 12:16:24 +0800 | [diff] [blame] | 113 | MI->x86opsize = 32; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 114 | printMemReference(MI, OpNo, O); |
| 115 | } |
| 116 | |
| 117 | static void printi512mem(MCInst *MI, unsigned OpNo, SStream *O) |
| 118 | { |
Nguyen Anh Quynh | b76233c | 2014-06-04 18:31:02 +0800 | [diff] [blame] | 119 | SStream_concat0(O, "zmmword ptr "); |
Nguyen Anh Quynh | 1085073 | 2014-06-18 12:16:24 +0800 | [diff] [blame] | 120 | MI->x86opsize = 64; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 121 | printMemReference(MI, OpNo, O); |
| 122 | } |
| 123 | |
| 124 | static void printf32mem(MCInst *MI, unsigned OpNo, SStream *O) |
| 125 | { |
Nguyen Anh Quynh | b76233c | 2014-06-04 18:31:02 +0800 | [diff] [blame] | 126 | SStream_concat0(O, "dword ptr "); |
Nguyen Anh Quynh | 1085073 | 2014-06-18 12:16:24 +0800 | [diff] [blame] | 127 | MI->x86opsize = 4; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 128 | printMemReference(MI, OpNo, O); |
| 129 | } |
| 130 | |
| 131 | static void printf64mem(MCInst *MI, unsigned OpNo, SStream *O) |
| 132 | { |
Nguyen Anh Quynh | b76233c | 2014-06-04 18:31:02 +0800 | [diff] [blame] | 133 | SStream_concat0(O, "qword ptr "); |
Nguyen Anh Quynh | 1085073 | 2014-06-18 12:16:24 +0800 | [diff] [blame] | 134 | MI->x86opsize = 8; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 135 | printMemReference(MI, OpNo, O); |
| 136 | } |
| 137 | |
| 138 | static void printf80mem(MCInst *MI, unsigned OpNo, SStream *O) |
| 139 | { |
Nguyen Anh Quynh | b76233c | 2014-06-04 18:31:02 +0800 | [diff] [blame] | 140 | SStream_concat0(O, "xword ptr "); |
Nguyen Anh Quynh | 1085073 | 2014-06-18 12:16:24 +0800 | [diff] [blame] | 141 | MI->x86opsize = 10; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 142 | printMemReference(MI, OpNo, O); |
| 143 | } |
| 144 | |
| 145 | static void printf128mem(MCInst *MI, unsigned OpNo, SStream *O) |
| 146 | { |
Nguyen Anh Quynh | b76233c | 2014-06-04 18:31:02 +0800 | [diff] [blame] | 147 | SStream_concat0(O, "xmmword ptr "); |
Nguyen Anh Quynh | 1085073 | 2014-06-18 12:16:24 +0800 | [diff] [blame] | 148 | MI->x86opsize = 16; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 149 | printMemReference(MI, OpNo, O); |
| 150 | } |
| 151 | |
| 152 | static void printf256mem(MCInst *MI, unsigned OpNo, SStream *O) |
| 153 | { |
Nguyen Anh Quynh | b76233c | 2014-06-04 18:31:02 +0800 | [diff] [blame] | 154 | SStream_concat0(O, "ymmword ptr "); |
Nguyen Anh Quynh | 1085073 | 2014-06-18 12:16:24 +0800 | [diff] [blame] | 155 | MI->x86opsize = 32; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 156 | printMemReference(MI, OpNo, O); |
| 157 | } |
| 158 | |
| 159 | static void printf512mem(MCInst *MI, unsigned OpNo, SStream *O) |
| 160 | { |
Nguyen Anh Quynh | b76233c | 2014-06-04 18:31:02 +0800 | [diff] [blame] | 161 | SStream_concat0(O, "zmmword ptr "); |
Nguyen Anh Quynh | 1085073 | 2014-06-18 12:16:24 +0800 | [diff] [blame] | 162 | MI->x86opsize = 64; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 163 | printMemReference(MI, OpNo, O); |
| 164 | } |
| 165 | |
Nguyen Anh Quynh | 9518148 | 2014-03-25 23:20:41 +0800 | [diff] [blame] | 166 | static void printSSECC(MCInst *MI, unsigned Op, SStream *OS) |
| 167 | { |
| 168 | int64_t Imm = MCOperand_getImm(MCInst_getOperand(MI, Op)) & 0xf; |
| 169 | switch (Imm) { |
| 170 | default: break; // never reach |
Nguyen Anh Quynh | 4c5eabc | 2014-06-24 23:50:41 +0800 | [diff] [blame] | 171 | case 0: SStream_concat0(OS, "eq"); op_addSseCC(MI, X86_SSE_CC_EQ); break; |
| 172 | case 1: SStream_concat0(OS, "lt"); op_addSseCC(MI, X86_SSE_CC_LT); break; |
| 173 | case 2: SStream_concat0(OS, "le"); op_addSseCC(MI, X86_SSE_CC_LE); break; |
| 174 | case 3: SStream_concat0(OS, "unord"); op_addSseCC(MI, X86_SSE_CC_UNORD); break; |
| 175 | case 4: SStream_concat0(OS, "neq"); op_addSseCC(MI, X86_SSE_CC_NEQ); break; |
| 176 | case 5: SStream_concat0(OS, "nlt"); op_addSseCC(MI, X86_SSE_CC_NLT); break; |
| 177 | case 6: SStream_concat0(OS, "nle"); op_addSseCC(MI, X86_SSE_CC_NLE); break; |
| 178 | case 7: SStream_concat0(OS, "ord"); op_addSseCC(MI, X86_SSE_CC_ORD); break; |
| 179 | case 8: SStream_concat0(OS, "eq_uq"); op_addSseCC(MI, X86_SSE_CC_EQ_UQ); break; |
| 180 | case 9: SStream_concat0(OS, "nge"); op_addSseCC(MI, X86_SSE_CC_NGE); break; |
| 181 | case 0xa: SStream_concat0(OS, "ngt"); op_addSseCC(MI, X86_SSE_CC_NGT); break; |
| 182 | case 0xb: SStream_concat0(OS, "false"); op_addSseCC(MI, X86_SSE_CC_FALSE); break; |
| 183 | case 0xc: SStream_concat0(OS, "neq_oq"); op_addSseCC(MI, X86_SSE_CC_NEQ_OQ); break; |
| 184 | case 0xd: SStream_concat0(OS, "ge"); op_addSseCC(MI, X86_SSE_CC_GE); break; |
| 185 | case 0xe: SStream_concat0(OS, "gt"); op_addSseCC(MI, X86_SSE_CC_GT); break; |
| 186 | case 0xf: SStream_concat0(OS, "true"); op_addSseCC(MI, X86_SSE_CC_TRUE); break; |
Nguyen Anh Quynh | 9518148 | 2014-03-25 23:20:41 +0800 | [diff] [blame] | 187 | } |
| 188 | } |
| 189 | |
| 190 | static void printAVXCC(MCInst *MI, unsigned Op, SStream *O) |
| 191 | { |
| 192 | int64_t Imm = MCOperand_getImm(MCInst_getOperand(MI, Op)) & 0x1f; |
| 193 | switch (Imm) { |
| 194 | default: break;//printf("Invalid avxcc argument!\n"); break; |
Nguyen Anh Quynh | 4c5eabc | 2014-06-24 23:50:41 +0800 | [diff] [blame] | 195 | case 0: SStream_concat0(O, "eq"); op_addAvxCC(MI, X86_AVX_CC_EQ); break; |
| 196 | case 1: SStream_concat0(O, "lt"); op_addAvxCC(MI, X86_AVX_CC_LT); break; |
| 197 | case 2: SStream_concat0(O, "le"); op_addAvxCC(MI, X86_AVX_CC_LE); break; |
| 198 | case 3: SStream_concat0(O, "unord"); op_addAvxCC(MI, X86_AVX_CC_UNORD); break; |
| 199 | case 4: SStream_concat0(O, "neq"); op_addAvxCC(MI, X86_AVX_CC_NEQ); break; |
| 200 | case 5: SStream_concat0(O, "nlt"); op_addAvxCC(MI, X86_AVX_CC_NLT); break; |
| 201 | case 6: SStream_concat0(O, "nle"); op_addAvxCC(MI, X86_AVX_CC_NLE); break; |
| 202 | case 7: SStream_concat0(O, "ord"); op_addAvxCC(MI, X86_AVX_CC_ORD); break; |
| 203 | case 8: SStream_concat0(O, "eq_uq"); op_addAvxCC(MI, X86_AVX_CC_EQ_UQ); break; |
| 204 | case 9: SStream_concat0(O, "nge"); op_addAvxCC(MI, X86_AVX_CC_NGE); break; |
| 205 | case 0xa: SStream_concat0(O, "ngt"); op_addAvxCC(MI, X86_AVX_CC_NGT); break; |
| 206 | case 0xb: SStream_concat0(O, "false"); op_addAvxCC(MI, X86_AVX_CC_FALSE); break; |
| 207 | case 0xc: SStream_concat0(O, "neq_oq"); op_addAvxCC(MI, X86_AVX_CC_NEQ_OQ); break; |
| 208 | case 0xd: SStream_concat0(O, "ge"); op_addAvxCC(MI, X86_AVX_CC_GE); break; |
| 209 | case 0xe: SStream_concat0(O, "gt"); op_addAvxCC(MI, X86_AVX_CC_GT); break; |
| 210 | case 0xf: SStream_concat0(O, "true"); op_addAvxCC(MI, X86_AVX_CC_TRUE); break; |
| 211 | case 0x10: SStream_concat0(O, "eq_os"); op_addAvxCC(MI, X86_AVX_CC_EQ_OS); break; |
| 212 | case 0x11: SStream_concat0(O, "lt_oq"); op_addAvxCC(MI, X86_AVX_CC_LT_OQ); break; |
| 213 | case 0x12: SStream_concat0(O, "le_oq"); op_addAvxCC(MI, X86_AVX_CC_LE_OQ); break; |
| 214 | case 0x13: SStream_concat0(O, "unord_s"); op_addAvxCC(MI, X86_AVX_CC_UNORD_S); break; |
| 215 | case 0x14: SStream_concat0(O, "neq_us"); op_addAvxCC(MI, X86_AVX_CC_NEQ_US); break; |
| 216 | case 0x15: SStream_concat0(O, "nlt_uq"); op_addAvxCC(MI, X86_AVX_CC_NLT_UQ); break; |
| 217 | case 0x16: SStream_concat0(O, "nle_uq"); op_addAvxCC(MI, X86_AVX_CC_NLE_UQ); break; |
| 218 | case 0x17: SStream_concat0(O, "ord_s"); op_addAvxCC(MI, X86_AVX_CC_ORD_S); break; |
| 219 | case 0x18: SStream_concat0(O, "eq_us"); op_addAvxCC(MI, X86_AVX_CC_EQ_US); break; |
| 220 | case 0x19: SStream_concat0(O, "nge_uq"); op_addAvxCC(MI, X86_AVX_CC_NGE_UQ); break; |
| 221 | case 0x1a: SStream_concat0(O, "ngt_uq"); op_addAvxCC(MI, X86_AVX_CC_NGT_UQ); break; |
| 222 | case 0x1b: SStream_concat0(O, "false_os"); op_addAvxCC(MI, X86_AVX_CC_FALSE_OS); break; |
| 223 | case 0x1c: SStream_concat0(O, "neq_os"); op_addAvxCC(MI, X86_AVX_CC_NEQ_OS); break; |
| 224 | case 0x1d: SStream_concat0(O, "ge_oq"); op_addAvxCC(MI, X86_AVX_CC_GE_OQ); break; |
| 225 | case 0x1e: SStream_concat0(O, "gt_oq"); op_addAvxCC(MI, X86_AVX_CC_GT_OQ); break; |
| 226 | case 0x1f: SStream_concat0(O, "true_us"); op_addAvxCC(MI, X86_AVX_CC_TRUE_US); break; |
Nguyen Anh Quynh | 9518148 | 2014-03-25 23:20:41 +0800 | [diff] [blame] | 227 | } |
| 228 | } |
| 229 | |
| 230 | static void printRoundingControl(MCInst *MI, unsigned Op, SStream *O) |
| 231 | { |
| 232 | int64_t Imm = MCOperand_getImm(MCInst_getOperand(MI, Op)) & 0x3; |
| 233 | switch (Imm) { |
Nguyen Anh Quynh | 1a66fec | 2014-06-26 12:09:15 +0800 | [diff] [blame] | 234 | case 0: SStream_concat0(O, "{rn-sae}"); op_addAvxSae(MI); op_addAvxRoundingMode(MI, X86_AVX_RM_RN); break; |
| 235 | case 1: SStream_concat0(O, "{rd-sae}"); op_addAvxSae(MI); op_addAvxRoundingMode(MI, X86_AVX_RM_RD); break; |
| 236 | case 2: SStream_concat0(O, "{ru-sae}"); op_addAvxSae(MI); op_addAvxRoundingMode(MI, X86_AVX_RM_RU); break; |
| 237 | case 3: SStream_concat0(O, "{rz-sae}"); op_addAvxSae(MI); op_addAvxRoundingMode(MI, X86_AVX_RM_RZ); break; |
Nguyen Anh Quynh | 9518148 | 2014-03-25 23:20:41 +0800 | [diff] [blame] | 238 | default: break; // never reach |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | #endif |
| 243 | |
Nguyen Anh Quynh | 14ba46b | 2014-06-24 14:32:01 +0800 | [diff] [blame] | 244 | static char *getRegisterName(unsigned RegNo); |
| 245 | static void printRegName(SStream *OS, unsigned RegNo) |
| 246 | { |
| 247 | SStream_concat0(OS, getRegisterName(RegNo)); |
| 248 | } |
| 249 | |
| 250 | // local printOperand, without updating public operands |
| 251 | static void _printOperand(MCInst *MI, unsigned OpNo, SStream *O) |
| 252 | { |
| 253 | MCOperand *Op = MCInst_getOperand(MI, OpNo); |
| 254 | if (MCOperand_isReg(Op)) { |
| 255 | printRegName(O, MCOperand_getReg(Op)); |
| 256 | } else if (MCOperand_isImm(Op)) { |
| 257 | int64_t imm = MCOperand_getImm(Op); |
| 258 | if (imm < 0) { |
| 259 | if (imm < -HEX_THRESHOLD) |
| 260 | SStream_concat(O, "-0x%"PRIx64, -imm); |
| 261 | else |
| 262 | SStream_concat(O, "-%"PRIu64, -imm); |
| 263 | |
| 264 | } else { |
| 265 | if (imm > HEX_THRESHOLD) |
| 266 | SStream_concat(O, "0x%"PRIx64, imm); |
| 267 | else |
| 268 | SStream_concat(O, "%"PRIu64, imm); |
| 269 | } |
| 270 | } |
| 271 | } |
| 272 | |
Nguyen Anh Quynh | 13f40d2 | 2014-02-07 22:06:33 +0800 | [diff] [blame] | 273 | static void printSrcIdx(MCInst *MI, unsigned Op, SStream *O) |
| 274 | { |
| 275 | MCOperand *SegReg; |
Nguyen Anh Quynh | 14ba46b | 2014-06-24 14:32:01 +0800 | [diff] [blame] | 276 | int reg; |
| 277 | |
| 278 | if (MI->csh->detail) { |
| 279 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].type = X86_OP_MEM; |
| 280 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].size = MI->x86opsize; |
| 281 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.segment = X86_REG_INVALID; |
| 282 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.base = X86_REG_INVALID; |
| 283 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.index = X86_REG_INVALID; |
| 284 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.scale = 1; |
| 285 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.disp = 0; |
| 286 | } |
Nguyen Anh Quynh | 42706a3 | 2014-05-09 07:33:35 +0800 | [diff] [blame] | 287 | |
Nguyen Anh Quynh | 13f40d2 | 2014-02-07 22:06:33 +0800 | [diff] [blame] | 288 | SegReg = MCInst_getOperand(MI, Op+1); |
Nguyen Anh Quynh | 14ba46b | 2014-06-24 14:32:01 +0800 | [diff] [blame] | 289 | reg = MCOperand_getReg(SegReg); |
Nguyen Anh Quynh | 13f40d2 | 2014-02-07 22:06:33 +0800 | [diff] [blame] | 290 | |
| 291 | // If this has a segment register, print it. |
Nguyen Anh Quynh | 14ba46b | 2014-06-24 14:32:01 +0800 | [diff] [blame] | 292 | if (reg) { |
| 293 | _printOperand(MI, Op+1, O); |
| 294 | if (MI->csh->detail) { |
| 295 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.segment = reg; |
| 296 | } |
Nguyen Anh Quynh | b76233c | 2014-06-04 18:31:02 +0800 | [diff] [blame] | 297 | SStream_concat0(O, ":"); |
Nguyen Anh Quynh | 13f40d2 | 2014-02-07 22:06:33 +0800 | [diff] [blame] | 298 | } |
| 299 | |
Nguyen Anh Quynh | b76233c | 2014-06-04 18:31:02 +0800 | [diff] [blame] | 300 | SStream_concat0(O, "["); |
Nguyen Anh Quynh | 1085073 | 2014-06-18 12:16:24 +0800 | [diff] [blame] | 301 | set_mem_access(MI, true); |
Nguyen Anh Quynh | 13f40d2 | 2014-02-07 22:06:33 +0800 | [diff] [blame] | 302 | printOperand(MI, Op, O); |
Nguyen Anh Quynh | b76233c | 2014-06-04 18:31:02 +0800 | [diff] [blame] | 303 | SStream_concat0(O, "]"); |
Nguyen Anh Quynh | 1085073 | 2014-06-18 12:16:24 +0800 | [diff] [blame] | 304 | set_mem_access(MI, false); |
Nguyen Anh Quynh | 13f40d2 | 2014-02-07 22:06:33 +0800 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | static void printDstIdx(MCInst *MI, unsigned Op, SStream *O) |
| 308 | { |
Nguyen Anh Quynh | 14ba46b | 2014-06-24 14:32:01 +0800 | [diff] [blame] | 309 | if (MI->csh->detail) { |
| 310 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].type = X86_OP_MEM; |
| 311 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].size = MI->x86opsize; |
| 312 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.segment = X86_REG_INVALID; |
| 313 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.base = X86_REG_INVALID; |
| 314 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.index = X86_REG_INVALID; |
| 315 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.scale = 1; |
| 316 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.disp = 0; |
| 317 | } |
| 318 | |
Nguyen Anh Quynh | 0e534bf | 2014-06-05 17:05:16 +0700 | [diff] [blame] | 319 | // DI accesses are always ES-based on non-64bit mode |
Nguyen Anh Quynh | 14ba46b | 2014-06-24 14:32:01 +0800 | [diff] [blame] | 320 | if (MI->csh->mode != CS_MODE_64) { |
Nguyen Anh Quynh | 9417ad6 | 2014-06-05 17:03:52 +0700 | [diff] [blame] | 321 | SStream_concat(O, "es:["); |
Nguyen Anh Quynh | 14ba46b | 2014-06-24 14:32:01 +0800 | [diff] [blame] | 322 | if (MI->csh->detail) { |
| 323 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.segment = X86_REG_ES; |
| 324 | } |
| 325 | } else |
Nguyen Anh Quynh | 9417ad6 | 2014-06-05 17:03:52 +0700 | [diff] [blame] | 326 | SStream_concat(O, "["); |
Nguyen Anh Quynh | 14ba46b | 2014-06-24 14:32:01 +0800 | [diff] [blame] | 327 | |
Nguyen Anh Quynh | 1085073 | 2014-06-18 12:16:24 +0800 | [diff] [blame] | 328 | set_mem_access(MI, true); |
Nguyen Anh Quynh | 13f40d2 | 2014-02-07 22:06:33 +0800 | [diff] [blame] | 329 | printOperand(MI, Op, O); |
Nguyen Anh Quynh | b76233c | 2014-06-04 18:31:02 +0800 | [diff] [blame] | 330 | SStream_concat0(O, "]"); |
Nguyen Anh Quynh | 1085073 | 2014-06-18 12:16:24 +0800 | [diff] [blame] | 331 | set_mem_access(MI, false); |
Nguyen Anh Quynh | 13f40d2 | 2014-02-07 22:06:33 +0800 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | void printSrcIdx8(MCInst *MI, unsigned OpNo, SStream *O) |
| 335 | { |
Nguyen Anh Quynh | b76233c | 2014-06-04 18:31:02 +0800 | [diff] [blame] | 336 | SStream_concat0(O, "byte ptr "); |
Nguyen Anh Quynh | 1085073 | 2014-06-18 12:16:24 +0800 | [diff] [blame] | 337 | MI->x86opsize = 1; |
Nguyen Anh Quynh | 13f40d2 | 2014-02-07 22:06:33 +0800 | [diff] [blame] | 338 | printSrcIdx(MI, OpNo, O); |
| 339 | } |
| 340 | |
| 341 | void printSrcIdx16(MCInst *MI, unsigned OpNo, SStream *O) |
| 342 | { |
Nguyen Anh Quynh | b76233c | 2014-06-04 18:31:02 +0800 | [diff] [blame] | 343 | SStream_concat0(O, "word ptr "); |
Nguyen Anh Quynh | 1085073 | 2014-06-18 12:16:24 +0800 | [diff] [blame] | 344 | MI->x86opsize = 2; |
Nguyen Anh Quynh | 13f40d2 | 2014-02-07 22:06:33 +0800 | [diff] [blame] | 345 | printSrcIdx(MI, OpNo, O); |
| 346 | } |
| 347 | |
| 348 | void printSrcIdx32(MCInst *MI, unsigned OpNo, SStream *O) |
| 349 | { |
Nguyen Anh Quynh | b76233c | 2014-06-04 18:31:02 +0800 | [diff] [blame] | 350 | SStream_concat0(O, "dword ptr "); |
Nguyen Anh Quynh | 1085073 | 2014-06-18 12:16:24 +0800 | [diff] [blame] | 351 | MI->x86opsize = 4; |
Nguyen Anh Quynh | 13f40d2 | 2014-02-07 22:06:33 +0800 | [diff] [blame] | 352 | printSrcIdx(MI, OpNo, O); |
| 353 | } |
| 354 | |
| 355 | void printSrcIdx64(MCInst *MI, unsigned OpNo, SStream *O) |
| 356 | { |
Nguyen Anh Quynh | b76233c | 2014-06-04 18:31:02 +0800 | [diff] [blame] | 357 | SStream_concat0(O, "qword ptr "); |
Nguyen Anh Quynh | 1085073 | 2014-06-18 12:16:24 +0800 | [diff] [blame] | 358 | MI->x86opsize = 8; |
Nguyen Anh Quynh | 13f40d2 | 2014-02-07 22:06:33 +0800 | [diff] [blame] | 359 | printSrcIdx(MI, OpNo, O); |
| 360 | } |
| 361 | |
| 362 | void printDstIdx8(MCInst *MI, unsigned OpNo, SStream *O) |
| 363 | { |
Nguyen Anh Quynh | b76233c | 2014-06-04 18:31:02 +0800 | [diff] [blame] | 364 | SStream_concat0(O, "byte ptr "); |
Nguyen Anh Quynh | 1085073 | 2014-06-18 12:16:24 +0800 | [diff] [blame] | 365 | MI->x86opsize = 1; |
Nguyen Anh Quynh | 13f40d2 | 2014-02-07 22:06:33 +0800 | [diff] [blame] | 366 | printDstIdx(MI, OpNo, O); |
| 367 | } |
| 368 | |
| 369 | void printDstIdx16(MCInst *MI, unsigned OpNo, SStream *O) |
| 370 | { |
Nguyen Anh Quynh | b76233c | 2014-06-04 18:31:02 +0800 | [diff] [blame] | 371 | SStream_concat0(O, "word ptr "); |
Nguyen Anh Quynh | 1085073 | 2014-06-18 12:16:24 +0800 | [diff] [blame] | 372 | MI->x86opsize = 2; |
Nguyen Anh Quynh | 13f40d2 | 2014-02-07 22:06:33 +0800 | [diff] [blame] | 373 | printDstIdx(MI, OpNo, O); |
| 374 | } |
| 375 | |
| 376 | void printDstIdx32(MCInst *MI, unsigned OpNo, SStream *O) |
| 377 | { |
Nguyen Anh Quynh | b76233c | 2014-06-04 18:31:02 +0800 | [diff] [blame] | 378 | SStream_concat0(O, "dword ptr "); |
Nguyen Anh Quynh | 1085073 | 2014-06-18 12:16:24 +0800 | [diff] [blame] | 379 | MI->x86opsize = 4; |
Nguyen Anh Quynh | 13f40d2 | 2014-02-07 22:06:33 +0800 | [diff] [blame] | 380 | printDstIdx(MI, OpNo, O); |
| 381 | } |
| 382 | |
| 383 | void printDstIdx64(MCInst *MI, unsigned OpNo, SStream *O) |
| 384 | { |
Nguyen Anh Quynh | b76233c | 2014-06-04 18:31:02 +0800 | [diff] [blame] | 385 | SStream_concat0(O, "qword ptr "); |
Nguyen Anh Quynh | 1085073 | 2014-06-18 12:16:24 +0800 | [diff] [blame] | 386 | MI->x86opsize = 8; |
Nguyen Anh Quynh | 13f40d2 | 2014-02-07 22:06:33 +0800 | [diff] [blame] | 387 | printDstIdx(MI, OpNo, O); |
| 388 | } |
| 389 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 390 | static void printMemOffset(MCInst *MI, unsigned Op, SStream *O) |
| 391 | { |
| 392 | MCOperand *DispSpec = MCInst_getOperand(MI, Op); |
Nguyen Anh Quynh | 14ba46b | 2014-06-24 14:32:01 +0800 | [diff] [blame] | 393 | MCOperand *SegReg = MCInst_getOperand(MI, Op + 1); |
| 394 | int reg; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 395 | |
Nguyen Anh Quynh | 42c6b1a | 2013-12-30 00:15:25 +0800 | [diff] [blame] | 396 | if (MI->csh->detail) { |
Nguyen Anh Quynh | 5329a6f | 2014-06-08 23:35:52 +0700 | [diff] [blame] | 397 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].type = X86_OP_MEM; |
Nguyen Anh Quynh | 1085073 | 2014-06-18 12:16:24 +0800 | [diff] [blame] | 398 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].size = MI->x86opsize; |
Nguyen Anh Quynh | 14ba46b | 2014-06-24 14:32:01 +0800 | [diff] [blame] | 399 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.segment = X86_REG_INVALID; |
Nguyen Anh Quynh | 5329a6f | 2014-06-08 23:35:52 +0700 | [diff] [blame] | 400 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.base = X86_REG_INVALID; |
| 401 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.index = X86_REG_INVALID; |
| 402 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.scale = 1; |
| 403 | 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] | 404 | } |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 405 | |
Nguyen Anh Quynh | 13f40d2 | 2014-02-07 22:06:33 +0800 | [diff] [blame] | 406 | // If this has a segment register, print it. |
Nguyen Anh Quynh | 14ba46b | 2014-06-24 14:32:01 +0800 | [diff] [blame] | 407 | reg = MCOperand_getReg(SegReg); |
| 408 | if (reg) { |
| 409 | _printOperand(MI, Op + 1, O); |
Nguyen Anh Quynh | b76233c | 2014-06-04 18:31:02 +0800 | [diff] [blame] | 410 | SStream_concat0(O, ":"); |
Nguyen Anh Quynh | 14ba46b | 2014-06-24 14:32:01 +0800 | [diff] [blame] | 411 | if (MI->csh->detail) { |
| 412 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.segment = reg; |
| 413 | } |
Nguyen Anh Quynh | 13f40d2 | 2014-02-07 22:06:33 +0800 | [diff] [blame] | 414 | } |
| 415 | |
Nguyen Anh Quynh | b76233c | 2014-06-04 18:31:02 +0800 | [diff] [blame] | 416 | SStream_concat0(O, "["); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 417 | |
| 418 | if (MCOperand_isImm(DispSpec)) { |
| 419 | int64_t imm = MCOperand_getImm(DispSpec); |
Nguyen Anh Quynh | 42c6b1a | 2013-12-30 00:15:25 +0800 | [diff] [blame] | 420 | if (MI->csh->detail) |
Nguyen Anh Quynh | 5329a6f | 2014-06-08 23:35:52 +0700 | [diff] [blame] | 421 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.disp = imm; |
Nguyen Anh Quynh | be90639 | 2013-12-13 15:37:57 +0800 | [diff] [blame] | 422 | if (imm < 0) { |
Nguyen Anh Quynh | 6d3d800 | 2014-03-29 17:26:51 +0800 | [diff] [blame] | 423 | SStream_concat(O, "0x%"PRIx64, arch_masks[MI->csh->mode] & imm); |
Nguyen Anh Quynh | be90639 | 2013-12-13 15:37:57 +0800 | [diff] [blame] | 424 | } else { |
Nguyen Anh Quynh | f22557b | 2013-12-13 09:37:52 +0800 | [diff] [blame] | 425 | if (imm > HEX_THRESHOLD) |
| 426 | SStream_concat(O, "0x%"PRIx64, imm); |
| 427 | else |
| 428 | SStream_concat(O, "%"PRIu64, imm); |
| 429 | } |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 430 | } |
| 431 | |
Nguyen Anh Quynh | b76233c | 2014-06-04 18:31:02 +0800 | [diff] [blame] | 432 | SStream_concat0(O, "]"); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 433 | |
Nguyen Anh Quynh | 42c6b1a | 2013-12-30 00:15:25 +0800 | [diff] [blame] | 434 | if (MI->csh->detail) |
Nguyen Anh Quynh | 5329a6f | 2014-06-08 23:35:52 +0700 | [diff] [blame] | 435 | MI->flat_insn->detail->x86.op_count++; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | static void printMemOffs8(MCInst *MI, unsigned OpNo, SStream *O) |
| 439 | { |
Nguyen Anh Quynh | b76233c | 2014-06-04 18:31:02 +0800 | [diff] [blame] | 440 | SStream_concat0(O, "byte ptr "); |
Nguyen Anh Quynh | 1085073 | 2014-06-18 12:16:24 +0800 | [diff] [blame] | 441 | MI->x86opsize = 1; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 442 | printMemOffset(MI, OpNo, O); |
| 443 | } |
| 444 | |
| 445 | static void printMemOffs16(MCInst *MI, unsigned OpNo, SStream *O) |
| 446 | { |
Nguyen Anh Quynh | b76233c | 2014-06-04 18:31:02 +0800 | [diff] [blame] | 447 | SStream_concat0(O, "word ptr "); |
Nguyen Anh Quynh | 1085073 | 2014-06-18 12:16:24 +0800 | [diff] [blame] | 448 | MI->x86opsize = 2; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 449 | printMemOffset(MI, OpNo, O); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | static void printMemOffs32(MCInst *MI, unsigned OpNo, SStream *O) |
| 453 | { |
Nguyen Anh Quynh | b76233c | 2014-06-04 18:31:02 +0800 | [diff] [blame] | 454 | SStream_concat0(O, "dword ptr "); |
Nguyen Anh Quynh | 1085073 | 2014-06-18 12:16:24 +0800 | [diff] [blame] | 455 | MI->x86opsize = 4; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 456 | printMemOffset(MI, OpNo, O); |
| 457 | } |
| 458 | |
| 459 | static void printMemOffs64(MCInst *MI, unsigned OpNo, SStream *O) |
| 460 | { |
Nguyen Anh Quynh | b76233c | 2014-06-04 18:31:02 +0800 | [diff] [blame] | 461 | SStream_concat0(O, "qword ptr "); |
Nguyen Anh Quynh | 1085073 | 2014-06-18 12:16:24 +0800 | [diff] [blame] | 462 | MI->x86opsize = 8; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 463 | printMemOffset(MI, OpNo, O); |
| 464 | } |
| 465 | |
Nguyen Anh Quynh | 27b9a96 | 2014-02-19 10:13:47 +0800 | [diff] [blame] | 466 | static char *printAliasInstr(MCInst *MI, SStream *OS, void *info); |
Nguyen Anh Quynh | e51e227 | 2014-01-12 20:27:54 +0800 | [diff] [blame] | 467 | static void printInstruction(MCInst *MI, SStream *O, MCRegisterInfo *MRI); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 468 | void X86_Intel_printInst(MCInst *MI, SStream *O, void *Info) |
| 469 | { |
Nguyen Anh Quynh | 27b9a96 | 2014-02-19 10:13:47 +0800 | [diff] [blame] | 470 | char *mnem; |
Nguyen Anh Quynh | 85cddef | 2014-02-18 11:59:36 +0800 | [diff] [blame] | 471 | x86_reg reg; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 472 | |
Nguyen Anh Quynh | 005c514 | 2014-02-18 11:11:46 +0800 | [diff] [blame] | 473 | // Try to print any aliases first. |
Nguyen Anh Quynh | 0b69038 | 2014-08-13 13:01:50 +0800 | [diff] [blame^] | 474 | mnem = printAliasInstr(MI, O, Info); |
Nguyen Anh Quynh | 27b9a96 | 2014-02-19 10:13:47 +0800 | [diff] [blame] | 475 | if (mnem) |
| 476 | cs_mem_free(mnem); |
| 477 | else |
Nguyen Anh Quynh | 0b69038 | 2014-08-13 13:01:50 +0800 | [diff] [blame^] | 478 | printInstruction(MI, O, Info); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 479 | |
Nguyen Anh Quynh | 42c6b1a | 2013-12-30 00:15:25 +0800 | [diff] [blame] | 480 | if (MI->csh->detail) { |
Nguyen Anh Quynh | 85cddef | 2014-02-18 11:59:36 +0800 | [diff] [blame] | 481 | // first op can be embedded in the asm by llvm. |
Nguyen Anh Quynh | 14ba46b | 2014-06-24 14:32:01 +0800 | [diff] [blame] | 482 | // so we have to add the missing register as the first operand |
| 483 | reg = X86_insn_reg_intel(MCInst_getOpcode(MI)); |
Nguyen Anh Quynh | 85cddef | 2014-02-18 11:59:36 +0800 | [diff] [blame] | 484 | if (reg) { |
| 485 | // shift all the ops right to leave 1st slot for this new register op |
Nguyen Anh Quynh | 5329a6f | 2014-06-08 23:35:52 +0700 | [diff] [blame] | 486 | memmove(&(MI->flat_insn->detail->x86.operands[1]), &(MI->flat_insn->detail->x86.operands[0]), |
| 487 | sizeof(MI->flat_insn->detail->x86.operands[0]) * (ARR_SIZE(MI->flat_insn->detail->x86.operands) - 1)); |
| 488 | MI->flat_insn->detail->x86.operands[0].type = X86_OP_REG; |
| 489 | MI->flat_insn->detail->x86.operands[0].reg = reg; |
Nguyen Anh Quynh | 1085073 | 2014-06-18 12:16:24 +0800 | [diff] [blame] | 490 | MI->flat_insn->detail->x86.operands[0].size = MI->csh->regsize_map[reg]; |
Nguyen Anh Quynh | 5329a6f | 2014-06-08 23:35:52 +0700 | [diff] [blame] | 491 | MI->flat_insn->detail->x86.op_count++; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 492 | } |
| 493 | } |
| 494 | } |
| 495 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 496 | /// printPCRelImm - This is used to print an immediate value that ends up |
| 497 | /// being encoded as a pc-relative value. |
| 498 | static void printPCRelImm(MCInst *MI, unsigned OpNo, SStream *O) |
| 499 | { |
| 500 | MCOperand *Op = MCInst_getOperand(MI, OpNo); |
| 501 | if (MCOperand_isImm(Op)) { |
Nguyen Anh Quynh | 5329a6f | 2014-06-08 23:35:52 +0700 | [diff] [blame] | 502 | int64_t imm = MCOperand_getImm(Op) + MI->flat_insn->size + MI->address; |
Nguyen Anh Quynh | be90639 | 2013-12-13 15:37:57 +0800 | [diff] [blame] | 503 | if (imm < 0) { |
Nguyen Anh Quynh | 017df60 | 2014-03-20 15:38:51 +0800 | [diff] [blame] | 504 | if (imm < -HEX_THRESHOLD) |
Nguyen Anh Quynh | be90639 | 2013-12-13 15:37:57 +0800 | [diff] [blame] | 505 | SStream_concat(O, "-0x%"PRIx64, -imm); |
| 506 | else |
| 507 | SStream_concat(O, "-%"PRIu64, -imm); |
| 508 | } else { |
Nguyen Anh Quynh | 2d34251 | 2014-05-11 15:33:11 +0800 | [diff] [blame] | 509 | // handle 16bit segment bound |
| 510 | if (MI->csh->mode == CS_MODE_16 && imm > 0x100000) |
| 511 | imm -= 0x10000; |
| 512 | |
Nguyen Anh Quynh | f22557b | 2013-12-13 09:37:52 +0800 | [diff] [blame] | 513 | if (imm > HEX_THRESHOLD) |
| 514 | SStream_concat(O, "0x%"PRIx64, imm); |
| 515 | else |
| 516 | SStream_concat(O, "%"PRIu64, imm); |
| 517 | } |
Nguyen Anh Quynh | 42c6b1a | 2013-12-30 00:15:25 +0800 | [diff] [blame] | 518 | if (MI->csh->detail) { |
Nguyen Anh Quynh | 5329a6f | 2014-06-08 23:35:52 +0700 | [diff] [blame] | 519 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].type = X86_OP_IMM; |
Nguyen Anh Quynh | f1ec526 | 2014-06-25 22:03:18 +0800 | [diff] [blame] | 520 | // if op_count > 0, then this operand's size is taken from the destination op |
| 521 | if (MI->flat_insn->detail->x86.op_count > 0) |
| 522 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].size = MI->flat_insn->detail->x86.operands[0].size; |
| 523 | else |
| 524 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].size = MI->imm_size; |
Nguyen Anh Quynh | 5329a6f | 2014-06-08 23:35:52 +0700 | [diff] [blame] | 525 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].imm = imm; |
| 526 | MI->flat_insn->detail->x86.op_count++; |
Nguyen Anh Quynh | a209e67 | 2013-12-14 00:23:41 +0800 | [diff] [blame] | 527 | } |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 528 | } |
| 529 | } |
| 530 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 531 | static void printOperand(MCInst *MI, unsigned OpNo, SStream *O) |
| 532 | { |
| 533 | MCOperand *Op = MCInst_getOperand(MI, OpNo); |
| 534 | if (MCOperand_isReg(Op)) { |
| 535 | printRegName(O, MCOperand_getReg(Op)); |
Nguyen Anh Quynh | 42c6b1a | 2013-12-30 00:15:25 +0800 | [diff] [blame] | 536 | if (MI->csh->detail) { |
Nguyen Anh Quynh | 1085073 | 2014-06-18 12:16:24 +0800 | [diff] [blame] | 537 | unsigned int reg = MCOperand_getReg(Op); |
| 538 | if (MI->csh->doing_mem) { |
Nguyen Anh Quynh | 1085073 | 2014-06-18 12:16:24 +0800 | [diff] [blame] | 539 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.base = reg; |
Nguyen Anh Quynh | 1085073 | 2014-06-18 12:16:24 +0800 | [diff] [blame] | 540 | } else { |
| 541 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].type = X86_OP_REG; |
| 542 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].reg = reg; |
| 543 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].size = MI->csh->regsize_map[reg]; |
| 544 | MI->flat_insn->detail->x86.op_count++; |
| 545 | } |
Nguyen Anh Quynh | a209e67 | 2013-12-14 00:23:41 +0800 | [diff] [blame] | 546 | } |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 547 | } else if (MCOperand_isImm(Op)) { |
| 548 | int64_t imm = MCOperand_getImm(Op); |
Nguyen Anh Quynh | 94990c9 | 2013-12-13 15:56:08 +0800 | [diff] [blame] | 549 | if (imm >= 0) { |
Nguyen Anh Quynh | be90639 | 2013-12-13 15:37:57 +0800 | [diff] [blame] | 550 | if (imm > HEX_THRESHOLD) |
| 551 | SStream_concat(O, "0x%"PRIx64, imm); |
| 552 | else |
| 553 | SStream_concat(O, "%"PRIu64, imm); |
| 554 | } else { |
Nguyen Anh Quynh | 81a6df4 | 2014-04-01 07:23:39 +0800 | [diff] [blame] | 555 | if (imm < -HEX_THRESHOLD) |
| 556 | SStream_concat(O, "-0x%"PRIx64, -imm); |
| 557 | else |
| 558 | SStream_concat(O, "-%"PRIu64, -imm); |
Nguyen Anh Quynh | be90639 | 2013-12-13 15:37:57 +0800 | [diff] [blame] | 559 | } |
| 560 | |
Nguyen Anh Quynh | 42c6b1a | 2013-12-30 00:15:25 +0800 | [diff] [blame] | 561 | if (MI->csh->detail) { |
Nguyen Anh Quynh | 1085073 | 2014-06-18 12:16:24 +0800 | [diff] [blame] | 562 | if (MI->csh->doing_mem) { |
Nguyen Anh Quynh | 1085073 | 2014-06-18 12:16:24 +0800 | [diff] [blame] | 563 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.disp = imm; |
Nguyen Anh Quynh | 1085073 | 2014-06-18 12:16:24 +0800 | [diff] [blame] | 564 | } else { |
| 565 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].type = X86_OP_IMM; |
Nguyen Anh Quynh | f1ec526 | 2014-06-25 22:03:18 +0800 | [diff] [blame] | 566 | if (MI->flat_insn->detail->x86.op_count > 0) |
| 567 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].size = MI->flat_insn->detail->x86.operands[0].size; |
| 568 | else |
| 569 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].size = MI->imm_size; |
Nguyen Anh Quynh | 1085073 | 2014-06-18 12:16:24 +0800 | [diff] [blame] | 570 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].imm = imm; |
| 571 | MI->flat_insn->detail->x86.op_count++; |
| 572 | } |
Nguyen Anh Quynh | a209e67 | 2013-12-14 00:23:41 +0800 | [diff] [blame] | 573 | } |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 574 | } |
| 575 | } |
| 576 | |
Nguyen Anh Quynh | 125f504 | 2014-03-29 12:02:21 +0800 | [diff] [blame] | 577 | static void printMemReference(MCInst *MI, unsigned Op, SStream *O) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 578 | { |
Nguyen Anh Quynh | 42706a3 | 2014-05-09 07:33:35 +0800 | [diff] [blame] | 579 | bool NeedPlus = false; |
Nguyen Anh Quynh | 0b69038 | 2014-08-13 13:01:50 +0800 | [diff] [blame^] | 580 | MCOperand *BaseReg = MCInst_getOperand(MI, Op + X86_AddrBaseReg); |
| 581 | uint64_t ScaleVal = MCOperand_getImm(MCInst_getOperand(MI, Op + X86_AddrScaleAmt)); |
| 582 | MCOperand *IndexReg = MCInst_getOperand(MI, Op + X86_AddrIndexReg); |
| 583 | MCOperand *DispSpec = MCInst_getOperand(MI, Op + X86_AddrDisp); |
| 584 | MCOperand *SegReg = MCInst_getOperand(MI, Op + X86_AddrSegmentReg); |
Nguyen Anh Quynh | 14ba46b | 2014-06-24 14:32:01 +0800 | [diff] [blame] | 585 | int reg; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 586 | |
Nguyen Anh Quynh | 42c6b1a | 2013-12-30 00:15:25 +0800 | [diff] [blame] | 587 | if (MI->csh->detail) { |
Nguyen Anh Quynh | 5329a6f | 2014-06-08 23:35:52 +0700 | [diff] [blame] | 588 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].type = X86_OP_MEM; |
Nguyen Anh Quynh | 1085073 | 2014-06-18 12:16:24 +0800 | [diff] [blame] | 589 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].size = MI->x86opsize; |
Nguyen Anh Quynh | 14ba46b | 2014-06-24 14:32:01 +0800 | [diff] [blame] | 590 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.segment = X86_REG_INVALID; |
Nguyen Anh Quynh | 5329a6f | 2014-06-08 23:35:52 +0700 | [diff] [blame] | 591 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.base = MCOperand_getReg(BaseReg); |
| 592 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.index = MCOperand_getReg(IndexReg); |
| 593 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.scale = (int)ScaleVal; |
| 594 | 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] | 595 | } |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 596 | |
| 597 | // If this has a segment register, print it. |
Nguyen Anh Quynh | 14ba46b | 2014-06-24 14:32:01 +0800 | [diff] [blame] | 598 | reg = MCOperand_getReg(SegReg); |
| 599 | if (reg) { |
Nguyen Anh Quynh | 0b69038 | 2014-08-13 13:01:50 +0800 | [diff] [blame^] | 600 | _printOperand(MI, Op + X86_AddrSegmentReg, O); |
Nguyen Anh Quynh | 14ba46b | 2014-06-24 14:32:01 +0800 | [diff] [blame] | 601 | if (MI->csh->detail) { |
| 602 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.segment = reg; |
| 603 | } |
Nguyen Anh Quynh | b76233c | 2014-06-04 18:31:02 +0800 | [diff] [blame] | 604 | SStream_concat0(O, ":"); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 605 | } |
| 606 | |
Nguyen Anh Quynh | b76233c | 2014-06-04 18:31:02 +0800 | [diff] [blame] | 607 | SStream_concat0(O, "["); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 608 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 609 | if (MCOperand_getReg(BaseReg)) { |
Nguyen Anh Quynh | 0b69038 | 2014-08-13 13:01:50 +0800 | [diff] [blame^] | 610 | _printOperand(MI, Op + X86_AddrBaseReg, O); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 611 | NeedPlus = true; |
| 612 | } |
| 613 | |
| 614 | if (MCOperand_getReg(IndexReg)) { |
Nguyen Anh Quynh | b76233c | 2014-06-04 18:31:02 +0800 | [diff] [blame] | 615 | if (NeedPlus) SStream_concat0(O, " + "); |
Nguyen Anh Quynh | 0b69038 | 2014-08-13 13:01:50 +0800 | [diff] [blame^] | 616 | _printOperand(MI, Op + X86_AddrIndexReg, O); |
Nguyen Anh Quynh | f9e3216 | 2013-12-05 21:58:31 +0800 | [diff] [blame] | 617 | if (ScaleVal != 1) |
| 618 | SStream_concat(O, "*%u", ScaleVal); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 619 | NeedPlus = true; |
| 620 | } |
| 621 | |
| 622 | if (!MCOperand_isImm(DispSpec)) { |
Nguyen Anh Quynh | 7a65ad7 | 2014-05-21 16:18:56 +0800 | [diff] [blame] | 623 | if (NeedPlus) |
Nguyen Anh Quynh | b76233c | 2014-06-04 18:31:02 +0800 | [diff] [blame] | 624 | SStream_concat0(O, " + "); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 625 | } else { |
| 626 | int64_t DispVal = MCOperand_getImm(DispSpec); |
Nguyen Anh Quynh | 42c6b1a | 2013-12-30 00:15:25 +0800 | [diff] [blame] | 627 | if (MI->csh->detail) |
Nguyen Anh Quynh | 5329a6f | 2014-06-08 23:35:52 +0700 | [diff] [blame] | 628 | 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] | 629 | if (DispVal || (!MCOperand_getReg(IndexReg) && !MCOperand_getReg(BaseReg))) { |
| 630 | if (NeedPlus) { |
Nguyen Anh Quynh | b76233c | 2014-06-04 18:31:02 +0800 | [diff] [blame] | 631 | SStream_concat0(O, " + "); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 632 | } |
Nguyen Anh Quynh | 125f504 | 2014-03-29 12:02:21 +0800 | [diff] [blame] | 633 | |
Nguyen Anh Quynh | be90639 | 2013-12-13 15:37:57 +0800 | [diff] [blame] | 634 | if (DispVal < 0) { |
Nguyen Anh Quynh | 6d3d800 | 2014-03-29 17:26:51 +0800 | [diff] [blame] | 635 | SStream_concat(O, "0x%"PRIx64, arch_masks[MI->csh->mode] & DispVal); |
Nguyen Anh Quynh | be90639 | 2013-12-13 15:37:57 +0800 | [diff] [blame] | 636 | } else { |
Nguyen Anh Quynh | f22557b | 2013-12-13 09:37:52 +0800 | [diff] [blame] | 637 | if (DispVal > HEX_THRESHOLD) |
| 638 | SStream_concat(O, "0x%"PRIx64, DispVal); |
| 639 | else |
| 640 | SStream_concat(O, "%"PRIu64, DispVal); |
| 641 | } |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 642 | } |
| 643 | } |
| 644 | |
Nguyen Anh Quynh | b76233c | 2014-06-04 18:31:02 +0800 | [diff] [blame] | 645 | SStream_concat0(O, "]"); |
Nguyen Anh Quynh | a209e67 | 2013-12-14 00:23:41 +0800 | [diff] [blame] | 646 | |
Nguyen Anh Quynh | 42c6b1a | 2013-12-30 00:15:25 +0800 | [diff] [blame] | 647 | if (MI->csh->detail) |
Nguyen Anh Quynh | 5329a6f | 2014-06-08 23:35:52 +0700 | [diff] [blame] | 648 | MI->flat_insn->detail->x86.op_count++; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 649 | } |
| 650 | |
Nguyen Anh Quynh | 0b69038 | 2014-08-13 13:01:50 +0800 | [diff] [blame^] | 651 | #define GET_REGINFO_ENUM |
| 652 | #include "X86GenRegisterInfo.inc" |
| 653 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 654 | #define PRINT_ALIAS_INSTR |
Nguyen Anh Quynh | 59b5489 | 2014-03-27 10:54:44 +0800 | [diff] [blame] | 655 | #ifdef CAPSTONE_X86_REDUCE |
| 656 | #include "X86GenAsmWriter1_reduce.inc" |
Nguyen Anh Quynh | 9518148 | 2014-03-25 23:20:41 +0800 | [diff] [blame] | 657 | #else |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 658 | #include "X86GenAsmWriter1.inc" |
Nguyen Anh Quynh | 9518148 | 2014-03-25 23:20:41 +0800 | [diff] [blame] | 659 | #endif |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 660 | |
Nguyen Anh Quynh | 8598a21 | 2014-05-14 11:26:41 +0800 | [diff] [blame] | 661 | #endif |