/src/bloaty/third_party/capstone/arch/X86/X86ATTInstPrinter.c
Line | Count | Source (jump to first uncovered line) |
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 | | |
15 | | /* Capstone Disassembly Engine */ |
16 | | /* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2015 */ |
17 | | |
18 | | // this code is only relevant when DIET mode is disable |
19 | | #if defined(CAPSTONE_HAS_X86) && !defined(CAPSTONE_DIET) && !defined(CAPSTONE_X86_ATT_DISABLE) |
20 | | |
21 | | #if defined (WIN32) || defined (WIN64) || defined (_WIN32) || defined (_WIN64) |
22 | | #pragma warning(disable:4996) // disable MSVC's warning on strncpy() |
23 | | #pragma warning(disable:28719) // disable MSVC's warning on strncpy() |
24 | | #endif |
25 | | |
26 | | #if !defined(CAPSTONE_HAS_OSXKERNEL) |
27 | | #include <ctype.h> |
28 | | #endif |
29 | | #include <capstone/platform.h> |
30 | | |
31 | | #if defined(CAPSTONE_HAS_OSXKERNEL) |
32 | | #include <Availability.h> |
33 | | #include <libkern/libkern.h> |
34 | | #else |
35 | | #include <stdio.h> |
36 | | #include <stdlib.h> |
37 | | #endif |
38 | | |
39 | | #include <string.h> |
40 | | |
41 | | #include "../../utils.h" |
42 | | #include "../../MCInst.h" |
43 | | #include "../../SStream.h" |
44 | | #include "../../MCRegisterInfo.h" |
45 | | #include "X86Mapping.h" |
46 | | #include "X86BaseInfo.h" |
47 | | |
48 | | |
49 | | #define GET_INSTRINFO_ENUM |
50 | | #ifdef CAPSTONE_X86_REDUCE |
51 | | #include "X86GenInstrInfo_reduce.inc" |
52 | | #else |
53 | | #include "X86GenInstrInfo.inc" |
54 | | #endif |
55 | | |
56 | | static void printMemReference(MCInst *MI, unsigned Op, SStream *O); |
57 | | static void printOperand(MCInst *MI, unsigned OpNo, SStream *O); |
58 | | |
59 | | |
60 | | static void set_mem_access(MCInst *MI, bool status) |
61 | 0 | { |
62 | 0 | if (MI->csh->detail != CS_OPT_ON) |
63 | 0 | return; |
64 | | |
65 | 0 | MI->csh->doing_mem = status; |
66 | 0 | if (!status) |
67 | | // done, create the next operand slot |
68 | 0 | MI->flat_insn->detail->x86.op_count++; |
69 | 0 | } |
70 | | |
71 | | static void printopaquemem(MCInst *MI, unsigned OpNo, SStream *O) |
72 | 0 | { |
73 | 0 | switch(MI->csh->mode) { |
74 | 0 | case CS_MODE_16: |
75 | 0 | switch(MI->flat_insn->id) { |
76 | 0 | default: |
77 | 0 | MI->x86opsize = 2; |
78 | 0 | break; |
79 | 0 | case X86_INS_LJMP: |
80 | 0 | case X86_INS_LCALL: |
81 | 0 | MI->x86opsize = 4; |
82 | 0 | break; |
83 | 0 | case X86_INS_SGDT: |
84 | 0 | case X86_INS_SIDT: |
85 | 0 | case X86_INS_LGDT: |
86 | 0 | case X86_INS_LIDT: |
87 | 0 | MI->x86opsize = 6; |
88 | 0 | break; |
89 | 0 | } |
90 | 0 | break; |
91 | 0 | case CS_MODE_32: |
92 | 0 | switch(MI->flat_insn->id) { |
93 | 0 | default: |
94 | 0 | MI->x86opsize = 4; |
95 | 0 | break; |
96 | 0 | case X86_INS_LJMP: |
97 | 0 | case X86_INS_LCALL: |
98 | 0 | case X86_INS_SGDT: |
99 | 0 | case X86_INS_SIDT: |
100 | 0 | case X86_INS_LGDT: |
101 | 0 | case X86_INS_LIDT: |
102 | 0 | MI->x86opsize = 6; |
103 | 0 | break; |
104 | 0 | } |
105 | 0 | break; |
106 | 0 | case CS_MODE_64: |
107 | 0 | switch(MI->flat_insn->id) { |
108 | 0 | default: |
109 | 0 | MI->x86opsize = 8; |
110 | 0 | break; |
111 | 0 | case X86_INS_LJMP: |
112 | 0 | case X86_INS_LCALL: |
113 | 0 | case X86_INS_SGDT: |
114 | 0 | case X86_INS_SIDT: |
115 | 0 | case X86_INS_LGDT: |
116 | 0 | case X86_INS_LIDT: |
117 | 0 | MI->x86opsize = 10; |
118 | 0 | break; |
119 | 0 | } |
120 | 0 | break; |
121 | 0 | default: // never reach |
122 | 0 | break; |
123 | 0 | } |
124 | | |
125 | 0 | printMemReference(MI, OpNo, O); |
126 | 0 | } |
127 | | |
128 | | static void printi8mem(MCInst *MI, unsigned OpNo, SStream *O) |
129 | 0 | { |
130 | 0 | MI->x86opsize = 1; |
131 | 0 | printMemReference(MI, OpNo, O); |
132 | 0 | } |
133 | | |
134 | | static void printi16mem(MCInst *MI, unsigned OpNo, SStream *O) |
135 | 0 | { |
136 | 0 | MI->x86opsize = 2; |
137 | |
|
138 | 0 | printMemReference(MI, OpNo, O); |
139 | 0 | } |
140 | | |
141 | | static void printi32mem(MCInst *MI, unsigned OpNo, SStream *O) |
142 | 0 | { |
143 | 0 | MI->x86opsize = 4; |
144 | |
|
145 | 0 | printMemReference(MI, OpNo, O); |
146 | 0 | } |
147 | | |
148 | | static void printi64mem(MCInst *MI, unsigned OpNo, SStream *O) |
149 | 0 | { |
150 | 0 | MI->x86opsize = 8; |
151 | 0 | printMemReference(MI, OpNo, O); |
152 | 0 | } |
153 | | |
154 | | static void printi128mem(MCInst *MI, unsigned OpNo, SStream *O) |
155 | 0 | { |
156 | 0 | MI->x86opsize = 16; |
157 | 0 | printMemReference(MI, OpNo, O); |
158 | 0 | } |
159 | | |
160 | | #ifndef CAPSTONE_X86_REDUCE |
161 | | static void printi256mem(MCInst *MI, unsigned OpNo, SStream *O) |
162 | 0 | { |
163 | 0 | MI->x86opsize = 32; |
164 | 0 | printMemReference(MI, OpNo, O); |
165 | 0 | } |
166 | | |
167 | | static void printi512mem(MCInst *MI, unsigned OpNo, SStream *O) |
168 | 0 | { |
169 | 0 | MI->x86opsize = 64; |
170 | 0 | printMemReference(MI, OpNo, O); |
171 | 0 | } |
172 | | |
173 | | static void printf32mem(MCInst *MI, unsigned OpNo, SStream *O) |
174 | 0 | { |
175 | 0 | switch(MCInst_getOpcode(MI)) { |
176 | 0 | default: |
177 | 0 | MI->x86opsize = 4; |
178 | 0 | break; |
179 | 0 | case X86_FBSTPm: |
180 | 0 | case X86_FBLDm: |
181 | | // TODO: fix this in tablegen instead |
182 | 0 | MI->x86opsize = 10; |
183 | 0 | break; |
184 | 0 | case X86_FSTENVm: |
185 | 0 | case X86_FLDENVm: |
186 | | // TODO: fix this in tablegen instead |
187 | 0 | switch(MI->csh->mode) { |
188 | 0 | default: // never reach |
189 | 0 | break; |
190 | 0 | case CS_MODE_16: |
191 | 0 | MI->x86opsize = 14; |
192 | 0 | break; |
193 | 0 | case CS_MODE_32: |
194 | 0 | case CS_MODE_64: |
195 | 0 | MI->x86opsize = 28; |
196 | 0 | break; |
197 | 0 | } |
198 | 0 | break; |
199 | 0 | } |
200 | | |
201 | 0 | printMemReference(MI, OpNo, O); |
202 | 0 | } |
203 | | |
204 | | static void printf64mem(MCInst *MI, unsigned OpNo, SStream *O) |
205 | 0 | { |
206 | 0 | MI->x86opsize = 8; |
207 | 0 | printMemReference(MI, OpNo, O); |
208 | 0 | } |
209 | | |
210 | | static void printf80mem(MCInst *MI, unsigned OpNo, SStream *O) |
211 | 0 | { |
212 | 0 | MI->x86opsize = 10; |
213 | 0 | printMemReference(MI, OpNo, O); |
214 | 0 | } |
215 | | |
216 | | static void printf128mem(MCInst *MI, unsigned OpNo, SStream *O) |
217 | 0 | { |
218 | 0 | MI->x86opsize = 16; |
219 | 0 | printMemReference(MI, OpNo, O); |
220 | 0 | } |
221 | | |
222 | | static void printf256mem(MCInst *MI, unsigned OpNo, SStream *O) |
223 | 0 | { |
224 | 0 | MI->x86opsize = 32; |
225 | 0 | printMemReference(MI, OpNo, O); |
226 | 0 | } |
227 | | |
228 | | static void printf512mem(MCInst *MI, unsigned OpNo, SStream *O) |
229 | 0 | { |
230 | 0 | MI->x86opsize = 64; |
231 | 0 | printMemReference(MI, OpNo, O); |
232 | 0 | } |
233 | | |
234 | | static void printSSECC(MCInst *MI, unsigned Op, SStream *OS) |
235 | 0 | { |
236 | 0 | uint8_t Imm = (uint8_t)(MCOperand_getImm(MCInst_getOperand(MI, Op)) & 7); |
237 | 0 | switch (Imm) { |
238 | 0 | default: break; // never reach |
239 | 0 | case 0: SStream_concat0(OS, "eq"); op_addSseCC(MI, X86_SSE_CC_EQ); break; |
240 | 0 | case 1: SStream_concat0(OS, "lt"); op_addSseCC(MI, X86_SSE_CC_LT); break; |
241 | 0 | case 2: SStream_concat0(OS, "le"); op_addSseCC(MI, X86_SSE_CC_LE); break; |
242 | 0 | case 3: SStream_concat0(OS, "unord"); op_addSseCC(MI, X86_SSE_CC_UNORD); break; |
243 | 0 | case 4: SStream_concat0(OS, "neq"); op_addSseCC(MI, X86_SSE_CC_NEQ); break; |
244 | 0 | case 5: SStream_concat0(OS, "nlt"); op_addSseCC(MI, X86_SSE_CC_NLT); break; |
245 | 0 | case 6: SStream_concat0(OS, "nle"); op_addSseCC(MI, X86_SSE_CC_NLE); break; |
246 | 0 | case 7: SStream_concat0(OS, "ord"); op_addSseCC(MI, X86_SSE_CC_ORD); break; |
247 | 0 | } |
248 | | |
249 | 0 | MI->popcode_adjust = Imm + 1; |
250 | 0 | } |
251 | | |
252 | | static void printAVXCC(MCInst *MI, unsigned Op, SStream *O) |
253 | 0 | { |
254 | 0 | uint8_t Imm = (uint8_t)(MCOperand_getImm(MCInst_getOperand(MI, Op)) & 0x1f); |
255 | 0 | switch (Imm) { |
256 | 0 | default: break;//printf("Invalid avxcc argument!\n"); break; |
257 | 0 | case 0: SStream_concat0(O, "eq"); op_addAvxCC(MI, X86_AVX_CC_EQ); break; |
258 | 0 | case 1: SStream_concat0(O, "lt"); op_addAvxCC(MI, X86_AVX_CC_LT); break; |
259 | 0 | case 2: SStream_concat0(O, "le"); op_addAvxCC(MI, X86_AVX_CC_LE); break; |
260 | 0 | case 3: SStream_concat0(O, "unord"); op_addAvxCC(MI, X86_AVX_CC_UNORD); break; |
261 | 0 | case 4: SStream_concat0(O, "neq"); op_addAvxCC(MI, X86_AVX_CC_NEQ); break; |
262 | 0 | case 5: SStream_concat0(O, "nlt"); op_addAvxCC(MI, X86_AVX_CC_NLT); break; |
263 | 0 | case 6: SStream_concat0(O, "nle"); op_addAvxCC(MI, X86_AVX_CC_NLE); break; |
264 | 0 | case 7: SStream_concat0(O, "ord"); op_addAvxCC(MI, X86_AVX_CC_ORD); break; |
265 | 0 | case 8: SStream_concat0(O, "eq_uq"); op_addAvxCC(MI, X86_AVX_CC_EQ_UQ); break; |
266 | 0 | case 9: SStream_concat0(O, "nge"); op_addAvxCC(MI, X86_AVX_CC_NGE); break; |
267 | 0 | case 0xa: SStream_concat0(O, "ngt"); op_addAvxCC(MI, X86_AVX_CC_NGT); break; |
268 | 0 | case 0xb: SStream_concat0(O, "false"); op_addAvxCC(MI, X86_AVX_CC_FALSE); break; |
269 | 0 | case 0xc: SStream_concat0(O, "neq_oq"); op_addAvxCC(MI, X86_AVX_CC_NEQ_OQ); break; |
270 | 0 | case 0xd: SStream_concat0(O, "ge"); op_addAvxCC(MI, X86_AVX_CC_GE); break; |
271 | 0 | case 0xe: SStream_concat0(O, "gt"); op_addAvxCC(MI, X86_AVX_CC_GT); break; |
272 | 0 | case 0xf: SStream_concat0(O, "true"); op_addAvxCC(MI, X86_AVX_CC_TRUE); break; |
273 | 0 | case 0x10: SStream_concat0(O, "eq_os"); op_addAvxCC(MI, X86_AVX_CC_EQ_OS); break; |
274 | 0 | case 0x11: SStream_concat0(O, "lt_oq"); op_addAvxCC(MI, X86_AVX_CC_LT_OQ); break; |
275 | 0 | case 0x12: SStream_concat0(O, "le_oq"); op_addAvxCC(MI, X86_AVX_CC_LE_OQ); break; |
276 | 0 | case 0x13: SStream_concat0(O, "unord_s"); op_addAvxCC(MI, X86_AVX_CC_UNORD_S); break; |
277 | 0 | case 0x14: SStream_concat0(O, "neq_us"); op_addAvxCC(MI, X86_AVX_CC_NEQ_US); break; |
278 | 0 | case 0x15: SStream_concat0(O, "nlt_uq"); op_addAvxCC(MI, X86_AVX_CC_NLT_UQ); break; |
279 | 0 | case 0x16: SStream_concat0(O, "nle_uq"); op_addAvxCC(MI, X86_AVX_CC_NLE_UQ); break; |
280 | 0 | case 0x17: SStream_concat0(O, "ord_s"); op_addAvxCC(MI, X86_AVX_CC_ORD_S); break; |
281 | 0 | case 0x18: SStream_concat0(O, "eq_us"); op_addAvxCC(MI, X86_AVX_CC_EQ_US); break; |
282 | 0 | case 0x19: SStream_concat0(O, "nge_uq"); op_addAvxCC(MI, X86_AVX_CC_NGE_UQ); break; |
283 | 0 | case 0x1a: SStream_concat0(O, "ngt_uq"); op_addAvxCC(MI, X86_AVX_CC_NGT_UQ); break; |
284 | 0 | case 0x1b: SStream_concat0(O, "false_os"); op_addAvxCC(MI, X86_AVX_CC_FALSE_OS); break; |
285 | 0 | case 0x1c: SStream_concat0(O, "neq_os"); op_addAvxCC(MI, X86_AVX_CC_NEQ_OS); break; |
286 | 0 | case 0x1d: SStream_concat0(O, "ge_oq"); op_addAvxCC(MI, X86_AVX_CC_GE_OQ); break; |
287 | 0 | case 0x1e: SStream_concat0(O, "gt_oq"); op_addAvxCC(MI, X86_AVX_CC_GT_OQ); break; |
288 | 0 | case 0x1f: SStream_concat0(O, "true_us"); op_addAvxCC(MI, X86_AVX_CC_TRUE_US); break; |
289 | 0 | } |
290 | | |
291 | 0 | MI->popcode_adjust = Imm + 1; |
292 | 0 | } |
293 | | |
294 | | static void printXOPCC(MCInst *MI, unsigned Op, SStream *O) |
295 | 0 | { |
296 | 0 | int64_t Imm = MCOperand_getImm(MCInst_getOperand(MI, Op)); |
297 | |
|
298 | 0 | switch (Imm) { |
299 | 0 | default: // llvm_unreachable("Invalid xopcc argument!"); |
300 | 0 | case 0: SStream_concat0(O, "lt"); op_addXopCC(MI, X86_XOP_CC_LT); break; |
301 | 0 | case 1: SStream_concat0(O, "le"); op_addXopCC(MI, X86_XOP_CC_LE); break; |
302 | 0 | case 2: SStream_concat0(O, "gt"); op_addXopCC(MI, X86_XOP_CC_GT); break; |
303 | 0 | case 3: SStream_concat0(O, "ge"); op_addXopCC(MI, X86_XOP_CC_GE); break; |
304 | 0 | case 4: SStream_concat0(O, "eq"); op_addXopCC(MI, X86_XOP_CC_EQ); break; |
305 | 0 | case 5: SStream_concat0(O, "neq"); op_addXopCC(MI, X86_XOP_CC_NEQ); break; |
306 | 0 | case 6: SStream_concat0(O, "false"); op_addXopCC(MI, X86_XOP_CC_FALSE); break; |
307 | 0 | case 7: SStream_concat0(O, "true"); op_addXopCC(MI, X86_XOP_CC_TRUE); break; |
308 | 0 | } |
309 | 0 | } |
310 | | |
311 | | static void printRoundingControl(MCInst *MI, unsigned Op, SStream *O) |
312 | 0 | { |
313 | 0 | int64_t Imm = MCOperand_getImm(MCInst_getOperand(MI, Op)) & 0x3; |
314 | 0 | switch (Imm) { |
315 | 0 | case 0: SStream_concat0(O, "{rn-sae}"); op_addAvxSae(MI); op_addAvxRoundingMode(MI, X86_AVX_RM_RN); break; |
316 | 0 | case 1: SStream_concat0(O, "{rd-sae}"); op_addAvxSae(MI); op_addAvxRoundingMode(MI, X86_AVX_RM_RD); break; |
317 | 0 | case 2: SStream_concat0(O, "{ru-sae}"); op_addAvxSae(MI); op_addAvxRoundingMode(MI, X86_AVX_RM_RU); break; |
318 | 0 | case 3: SStream_concat0(O, "{rz-sae}"); op_addAvxSae(MI); op_addAvxRoundingMode(MI, X86_AVX_RM_RZ); break; |
319 | 0 | default: break; // nev0er reach |
320 | 0 | } |
321 | 0 | } |
322 | | |
323 | | #endif |
324 | | |
325 | | static void printRegName(SStream *OS, unsigned RegNo); |
326 | | |
327 | | // local printOperand, without updating public operands |
328 | | static void _printOperand(MCInst *MI, unsigned OpNo, SStream *O) |
329 | 0 | { |
330 | 0 | MCOperand *Op = MCInst_getOperand(MI, OpNo); |
331 | 0 | if (MCOperand_isReg(Op)) { |
332 | 0 | printRegName(O, MCOperand_getReg(Op)); |
333 | 0 | } else if (MCOperand_isImm(Op)) { |
334 | 0 | uint8_t encsize; |
335 | 0 | uint8_t opsize = X86_immediate_size(MCInst_getOpcode(MI), &encsize); |
336 | | |
337 | | // Print X86 immediates as signed values. |
338 | 0 | int64_t imm = MCOperand_getImm(Op); |
339 | 0 | if (imm < 0) { |
340 | 0 | if (MI->csh->imm_unsigned) { |
341 | 0 | if (opsize) { |
342 | 0 | switch(opsize) { |
343 | 0 | default: |
344 | 0 | break; |
345 | 0 | case 1: |
346 | 0 | imm &= 0xff; |
347 | 0 | break; |
348 | 0 | case 2: |
349 | 0 | imm &= 0xffff; |
350 | 0 | break; |
351 | 0 | case 4: |
352 | 0 | imm &= 0xffffffff; |
353 | 0 | break; |
354 | 0 | } |
355 | 0 | } |
356 | | |
357 | 0 | SStream_concat(O, "$0x%"PRIx64, imm); |
358 | 0 | } else { |
359 | 0 | if (imm < -HEX_THRESHOLD) |
360 | 0 | SStream_concat(O, "$-0x%"PRIx64, -imm); |
361 | 0 | else |
362 | 0 | SStream_concat(O, "$-%"PRIu64, -imm); |
363 | 0 | } |
364 | 0 | } else { |
365 | 0 | if (imm > HEX_THRESHOLD) |
366 | 0 | SStream_concat(O, "$0x%"PRIx64, imm); |
367 | 0 | else |
368 | 0 | SStream_concat(O, "$%"PRIu64, imm); |
369 | 0 | } |
370 | 0 | } |
371 | 0 | } |
372 | | |
373 | | // convert Intel access info to AT&T access info |
374 | | static void get_op_access(cs_struct *h, unsigned int id, uint8_t *access, uint64_t *eflags) |
375 | 0 | { |
376 | 0 | uint8_t count, i; |
377 | 0 | uint8_t *arr = X86_get_op_access(h, id, eflags); |
378 | |
|
379 | 0 | if (!arr) { |
380 | 0 | access[0] = 0; |
381 | 0 | return; |
382 | 0 | } |
383 | | |
384 | | // find the non-zero last entry |
385 | 0 | for(count = 0; arr[count]; count++); |
386 | |
|
387 | 0 | if (count == 0) |
388 | 0 | return; |
389 | | |
390 | | // copy in reverse order this access array from Intel syntax -> AT&T syntax |
391 | 0 | count--; |
392 | 0 | for(i = 0; i <= count; i++) { |
393 | 0 | if (arr[count - i] != CS_AC_IGNORE) |
394 | 0 | access[i] = arr[count - i]; |
395 | 0 | else |
396 | 0 | access[i] = 0; |
397 | 0 | } |
398 | 0 | } |
399 | | |
400 | | static void printSrcIdx(MCInst *MI, unsigned Op, SStream *O) |
401 | 0 | { |
402 | 0 | MCOperand *SegReg; |
403 | 0 | int reg; |
404 | |
|
405 | 0 | if (MI->csh->detail) { |
406 | 0 | uint8_t access[6]; |
407 | |
|
408 | 0 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].type = X86_OP_MEM; |
409 | 0 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].size = MI->x86opsize; |
410 | 0 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.segment = X86_REG_INVALID; |
411 | 0 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.base = X86_REG_INVALID; |
412 | 0 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.index = X86_REG_INVALID; |
413 | 0 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.scale = 1; |
414 | 0 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.disp = 0; |
415 | |
|
416 | 0 | get_op_access(MI->csh, MCInst_getOpcode(MI), access, &MI->flat_insn->detail->x86.eflags); |
417 | 0 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].access = access[MI->flat_insn->detail->x86.op_count]; |
418 | 0 | } |
419 | |
|
420 | 0 | SegReg = MCInst_getOperand(MI, Op+1); |
421 | 0 | reg = MCOperand_getReg(SegReg); |
422 | | |
423 | | // If this has a segment register, print it. |
424 | 0 | if (reg) { |
425 | 0 | _printOperand(MI, Op+1, O); |
426 | 0 | if (MI->csh->detail) { |
427 | 0 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.segment = reg; |
428 | 0 | } |
429 | |
|
430 | 0 | SStream_concat0(O, ":"); |
431 | 0 | } |
432 | |
|
433 | 0 | SStream_concat0(O, "("); |
434 | 0 | set_mem_access(MI, true); |
435 | |
|
436 | 0 | printOperand(MI, Op, O); |
437 | |
|
438 | 0 | SStream_concat0(O, ")"); |
439 | 0 | set_mem_access(MI, false); |
440 | 0 | } |
441 | | |
442 | | static void printDstIdx(MCInst *MI, unsigned Op, SStream *O) |
443 | 0 | { |
444 | 0 | if (MI->csh->detail) { |
445 | 0 | uint8_t access[6]; |
446 | |
|
447 | 0 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].type = X86_OP_MEM; |
448 | 0 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].size = MI->x86opsize; |
449 | 0 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.segment = X86_REG_INVALID; |
450 | 0 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.base = X86_REG_INVALID; |
451 | 0 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.index = X86_REG_INVALID; |
452 | 0 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.scale = 1; |
453 | 0 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.disp = 0; |
454 | |
|
455 | 0 | get_op_access(MI->csh, MCInst_getOpcode(MI), access, &MI->flat_insn->detail->x86.eflags); |
456 | 0 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].access = access[MI->flat_insn->detail->x86.op_count]; |
457 | 0 | } |
458 | | |
459 | | // DI accesses are always ES-based on non-64bit mode |
460 | 0 | if (MI->csh->mode != CS_MODE_64) { |
461 | 0 | SStream_concat0(O, "%es:("); |
462 | 0 | if (MI->csh->detail) { |
463 | 0 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.segment = X86_REG_ES; |
464 | 0 | } |
465 | 0 | } else |
466 | 0 | SStream_concat0(O, "("); |
467 | |
|
468 | 0 | set_mem_access(MI, true); |
469 | |
|
470 | 0 | printOperand(MI, Op, O); |
471 | |
|
472 | 0 | SStream_concat0(O, ")"); |
473 | 0 | set_mem_access(MI, false); |
474 | 0 | } |
475 | | |
476 | | static void printSrcIdx8(MCInst *MI, unsigned OpNo, SStream *O) |
477 | 0 | { |
478 | 0 | MI->x86opsize = 1; |
479 | 0 | printSrcIdx(MI, OpNo, O); |
480 | 0 | } |
481 | | |
482 | | static void printSrcIdx16(MCInst *MI, unsigned OpNo, SStream *O) |
483 | 0 | { |
484 | 0 | MI->x86opsize = 2; |
485 | 0 | printSrcIdx(MI, OpNo, O); |
486 | 0 | } |
487 | | |
488 | | static void printSrcIdx32(MCInst *MI, unsigned OpNo, SStream *O) |
489 | 0 | { |
490 | 0 | MI->x86opsize = 4; |
491 | 0 | printSrcIdx(MI, OpNo, O); |
492 | 0 | } |
493 | | |
494 | | static void printSrcIdx64(MCInst *MI, unsigned OpNo, SStream *O) |
495 | 0 | { |
496 | 0 | MI->x86opsize = 8; |
497 | 0 | printSrcIdx(MI, OpNo, O); |
498 | 0 | } |
499 | | |
500 | | static void printDstIdx8(MCInst *MI, unsigned OpNo, SStream *O) |
501 | 0 | { |
502 | 0 | MI->x86opsize = 1; |
503 | 0 | printDstIdx(MI, OpNo, O); |
504 | 0 | } |
505 | | |
506 | | static void printDstIdx16(MCInst *MI, unsigned OpNo, SStream *O) |
507 | 0 | { |
508 | 0 | MI->x86opsize = 2; |
509 | 0 | printDstIdx(MI, OpNo, O); |
510 | 0 | } |
511 | | |
512 | | static void printDstIdx32(MCInst *MI, unsigned OpNo, SStream *O) |
513 | 0 | { |
514 | 0 | MI->x86opsize = 4; |
515 | 0 | printDstIdx(MI, OpNo, O); |
516 | 0 | } |
517 | | |
518 | | static void printDstIdx64(MCInst *MI, unsigned OpNo, SStream *O) |
519 | 0 | { |
520 | 0 | MI->x86opsize = 8; |
521 | 0 | printDstIdx(MI, OpNo, O); |
522 | 0 | } |
523 | | |
524 | | static void printMemOffset(MCInst *MI, unsigned Op, SStream *O) |
525 | 0 | { |
526 | 0 | MCOperand *DispSpec = MCInst_getOperand(MI, Op); |
527 | 0 | MCOperand *SegReg = MCInst_getOperand(MI, Op+1); |
528 | 0 | int reg; |
529 | |
|
530 | 0 | if (MI->csh->detail) { |
531 | 0 | uint8_t access[6]; |
532 | |
|
533 | 0 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].type = X86_OP_MEM; |
534 | 0 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].size = MI->x86opsize; |
535 | 0 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.segment = X86_REG_INVALID; |
536 | 0 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.base = X86_REG_INVALID; |
537 | 0 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.index = X86_REG_INVALID; |
538 | 0 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.scale = 1; |
539 | 0 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.disp = 0; |
540 | |
|
541 | 0 | get_op_access(MI->csh, MCInst_getOpcode(MI), access, &MI->flat_insn->detail->x86.eflags); |
542 | 0 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].access = access[MI->flat_insn->detail->x86.op_count]; |
543 | 0 | } |
544 | | |
545 | | // If this has a segment register, print it. |
546 | 0 | reg = MCOperand_getReg(SegReg); |
547 | 0 | if (reg) { |
548 | 0 | _printOperand(MI, Op + 1, O); |
549 | 0 | SStream_concat0(O, ":"); |
550 | 0 | if (MI->csh->detail) { |
551 | 0 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.segment = reg; |
552 | 0 | } |
553 | 0 | } |
554 | |
|
555 | 0 | if (MCOperand_isImm(DispSpec)) { |
556 | 0 | int64_t imm = MCOperand_getImm(DispSpec); |
557 | 0 | if (MI->csh->detail) |
558 | 0 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.disp = imm; |
559 | 0 | if (imm < 0) { |
560 | 0 | SStream_concat(O, "0x%"PRIx64, arch_masks[MI->csh->mode] & imm); |
561 | 0 | } else { |
562 | 0 | if (imm > HEX_THRESHOLD) |
563 | 0 | SStream_concat(O, "0x%"PRIx64, imm); |
564 | 0 | else |
565 | 0 | SStream_concat(O, "%"PRIu64, imm); |
566 | 0 | } |
567 | 0 | } |
568 | |
|
569 | 0 | if (MI->csh->detail) |
570 | 0 | MI->flat_insn->detail->x86.op_count++; |
571 | 0 | } |
572 | | |
573 | | #ifndef CAPSTONE_X86_REDUCE |
574 | | static void printU8Imm(MCInst *MI, unsigned Op, SStream *O) |
575 | 0 | { |
576 | 0 | uint8_t val = MCOperand_getImm(MCInst_getOperand(MI, Op)) & 0xff; |
577 | |
|
578 | 0 | if (val > HEX_THRESHOLD) |
579 | 0 | SStream_concat(O, "$0x%x", val); |
580 | 0 | else |
581 | 0 | SStream_concat(O, "$%u", val); |
582 | |
|
583 | 0 | if (MI->csh->detail) { |
584 | 0 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].type = X86_OP_IMM; |
585 | 0 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].imm = val; |
586 | 0 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].size = 1; |
587 | 0 | MI->flat_insn->detail->x86.op_count++; |
588 | 0 | } |
589 | 0 | } |
590 | | #endif |
591 | | |
592 | | static void printMemOffs8(MCInst *MI, unsigned OpNo, SStream *O) |
593 | 0 | { |
594 | 0 | MI->x86opsize = 1; |
595 | 0 | printMemOffset(MI, OpNo, O); |
596 | 0 | } |
597 | | |
598 | | static void printMemOffs16(MCInst *MI, unsigned OpNo, SStream *O) |
599 | 0 | { |
600 | 0 | MI->x86opsize = 2; |
601 | 0 | printMemOffset(MI, OpNo, O); |
602 | 0 | } |
603 | | |
604 | | static void printMemOffs32(MCInst *MI, unsigned OpNo, SStream *O) |
605 | 0 | { |
606 | 0 | MI->x86opsize = 4; |
607 | 0 | printMemOffset(MI, OpNo, O); |
608 | 0 | } |
609 | | |
610 | | static void printMemOffs64(MCInst *MI, unsigned OpNo, SStream *O) |
611 | 0 | { |
612 | 0 | MI->x86opsize = 8; |
613 | 0 | printMemOffset(MI, OpNo, O); |
614 | 0 | } |
615 | | |
616 | | /// printPCRelImm - This is used to print an immediate value that ends up |
617 | | /// being encoded as a pc-relative value (e.g. for jumps and calls). These |
618 | | /// print slightly differently than normal immediates. For example, a $ is not |
619 | | /// emitted. |
620 | | static void printPCRelImm(MCInst *MI, unsigned OpNo, SStream *O) |
621 | 0 | { |
622 | 0 | MCOperand *Op = MCInst_getOperand(MI, OpNo); |
623 | 0 | if (MCOperand_isImm(Op)) { |
624 | 0 | int64_t imm = MCOperand_getImm(Op) + MI->flat_insn->size + MI->address; |
625 | | |
626 | | // truncat imm for non-64bit |
627 | 0 | if (MI->csh->mode != CS_MODE_64) { |
628 | 0 | imm = imm & 0xffffffff; |
629 | 0 | } |
630 | |
|
631 | 0 | if (MI->csh->mode == CS_MODE_16 && |
632 | 0 | (MI->Opcode != X86_JMP_4 && MI->Opcode != X86_CALLpcrel32)) |
633 | 0 | imm = imm & 0xffff; |
634 | | |
635 | | // Hack: X86 16bit with opcode X86_JMP_4 |
636 | 0 | if (MI->csh->mode == CS_MODE_16 && |
637 | 0 | (MI->Opcode == X86_JMP_4 && MI->x86_prefix[2] != 0x66)) |
638 | 0 | imm = imm & 0xffff; |
639 | | |
640 | | // CALL/JMP rel16 is special |
641 | 0 | if (MI->Opcode == X86_CALLpcrel16 || MI->Opcode == X86_JMP_2) |
642 | 0 | imm = imm & 0xffff; |
643 | |
|
644 | 0 | if (imm < 0) { |
645 | 0 | SStream_concat(O, "0x%"PRIx64, imm); |
646 | 0 | } else { |
647 | 0 | if (imm > HEX_THRESHOLD) |
648 | 0 | SStream_concat(O, "0x%"PRIx64, imm); |
649 | 0 | else |
650 | 0 | SStream_concat(O, "%"PRIu64, imm); |
651 | 0 | } |
652 | 0 | if (MI->csh->detail) { |
653 | 0 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].type = X86_OP_IMM; |
654 | 0 | MI->has_imm = true; |
655 | 0 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].imm = imm; |
656 | 0 | MI->flat_insn->detail->x86.op_count++; |
657 | 0 | } |
658 | 0 | } |
659 | 0 | } |
660 | | |
661 | | static void printOperand(MCInst *MI, unsigned OpNo, SStream *O) |
662 | 0 | { |
663 | 0 | MCOperand *Op = MCInst_getOperand(MI, OpNo); |
664 | 0 | if (MCOperand_isReg(Op)) { |
665 | 0 | unsigned int reg = MCOperand_getReg(Op); |
666 | 0 | printRegName(O, reg); |
667 | 0 | if (MI->csh->detail) { |
668 | 0 | if (MI->csh->doing_mem) { |
669 | 0 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.base = reg; |
670 | 0 | } else { |
671 | 0 | uint8_t access[6]; |
672 | |
|
673 | 0 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].type = X86_OP_REG; |
674 | 0 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].reg = reg; |
675 | 0 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].size = MI->csh->regsize_map[reg]; |
676 | |
|
677 | 0 | get_op_access(MI->csh, MCInst_getOpcode(MI), access, &MI->flat_insn->detail->x86.eflags); |
678 | 0 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].access = access[MI->flat_insn->detail->x86.op_count]; |
679 | |
|
680 | 0 | MI->flat_insn->detail->x86.op_count++; |
681 | 0 | } |
682 | 0 | } |
683 | 0 | } else if (MCOperand_isImm(Op)) { |
684 | | // Print X86 immediates as signed values. |
685 | 0 | uint8_t encsize; |
686 | 0 | int64_t imm = MCOperand_getImm(Op); |
687 | 0 | uint8_t opsize = X86_immediate_size(MCInst_getOpcode(MI), &encsize); |
688 | |
|
689 | 0 | if (opsize == 1) // print 1 byte immediate in positive form |
690 | 0 | imm = imm & 0xff; |
691 | |
|
692 | 0 | switch(MI->flat_insn->id) { |
693 | 0 | default: |
694 | 0 | if (imm >= 0) { |
695 | 0 | if (imm > HEX_THRESHOLD) |
696 | 0 | SStream_concat(O, "$0x%"PRIx64, imm); |
697 | 0 | else |
698 | 0 | SStream_concat(O, "$%"PRIu64, imm); |
699 | 0 | } else { |
700 | 0 | if (MI->csh->imm_unsigned) { |
701 | 0 | if (opsize) { |
702 | 0 | switch(opsize) { |
703 | 0 | default: |
704 | 0 | break; |
705 | 0 | case 1: |
706 | 0 | imm &= 0xff; |
707 | 0 | break; |
708 | 0 | case 2: |
709 | 0 | imm &= 0xffff; |
710 | 0 | break; |
711 | 0 | case 4: |
712 | 0 | imm &= 0xffffffff; |
713 | 0 | break; |
714 | 0 | } |
715 | 0 | } |
716 | | |
717 | 0 | SStream_concat(O, "$0x%"PRIx64, imm); |
718 | 0 | } else { |
719 | 0 | if (imm == 0x8000000000000000LL) // imm == -imm |
720 | 0 | SStream_concat0(O, "$0x8000000000000000"); |
721 | 0 | else if (imm < -HEX_THRESHOLD) |
722 | 0 | SStream_concat(O, "$-0x%"PRIx64, -imm); |
723 | 0 | else |
724 | 0 | SStream_concat(O, "$-%"PRIu64, -imm); |
725 | 0 | } |
726 | 0 | } |
727 | 0 | break; |
728 | | |
729 | 0 | case X86_INS_MOVABS: |
730 | | // do not print number in negative form |
731 | 0 | SStream_concat(O, "$0x%"PRIx64, imm); |
732 | 0 | break; |
733 | | |
734 | 0 | case X86_INS_IN: |
735 | 0 | case X86_INS_OUT: |
736 | 0 | case X86_INS_INT: |
737 | | // do not print number in negative form |
738 | 0 | imm = imm & 0xff; |
739 | 0 | if (imm >= 0 && imm <= HEX_THRESHOLD) |
740 | 0 | SStream_concat(O, "$%u", imm); |
741 | 0 | else { |
742 | 0 | SStream_concat(O, "$0x%x", imm); |
743 | 0 | } |
744 | 0 | break; |
745 | | |
746 | 0 | case X86_INS_LCALL: |
747 | 0 | case X86_INS_LJMP: |
748 | | // always print address in positive form |
749 | 0 | if (OpNo == 1) { // selector is ptr16 |
750 | 0 | imm = imm & 0xffff; |
751 | 0 | opsize = 2; |
752 | 0 | } |
753 | 0 | SStream_concat(O, "$0x%"PRIx64, imm); |
754 | 0 | break; |
755 | | |
756 | 0 | case X86_INS_AND: |
757 | 0 | case X86_INS_OR: |
758 | 0 | case X86_INS_XOR: |
759 | | // do not print number in negative form |
760 | 0 | if (imm >= 0 && imm <= HEX_THRESHOLD) |
761 | 0 | SStream_concat(O, "$%u", imm); |
762 | 0 | else { |
763 | 0 | imm = arch_masks[opsize? opsize : MI->imm_size] & imm; |
764 | 0 | SStream_concat(O, "$0x%"PRIx64, imm); |
765 | 0 | } |
766 | 0 | break; |
767 | | |
768 | 0 | case X86_INS_RET: |
769 | 0 | case X86_INS_RETF: |
770 | | // RET imm16 |
771 | 0 | if (imm >= 0 && imm <= HEX_THRESHOLD) |
772 | 0 | SStream_concat(O, "$%u", imm); |
773 | 0 | else { |
774 | 0 | imm = 0xffff & imm; |
775 | 0 | SStream_concat(O, "$0x%x", imm); |
776 | 0 | } |
777 | 0 | break; |
778 | 0 | } |
779 | | |
780 | 0 | if (MI->csh->detail) { |
781 | 0 | if (MI->csh->doing_mem) { |
782 | 0 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].type = X86_OP_MEM; |
783 | 0 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.disp = imm; |
784 | 0 | } else { |
785 | 0 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].type = X86_OP_IMM; |
786 | 0 | MI->has_imm = true; |
787 | 0 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].imm = imm; |
788 | |
|
789 | 0 | if (opsize > 0) { |
790 | 0 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].size = opsize; |
791 | 0 | MI->flat_insn->detail->x86.encoding.imm_size = encsize; |
792 | 0 | } else if (MI->op1_size > 0) |
793 | 0 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].size = MI->op1_size; |
794 | 0 | else |
795 | 0 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].size = MI->imm_size; |
796 | |
|
797 | 0 | MI->flat_insn->detail->x86.op_count++; |
798 | 0 | } |
799 | 0 | } |
800 | 0 | } |
801 | 0 | } |
802 | | |
803 | | static void printMemReference(MCInst *MI, unsigned Op, SStream *O) |
804 | 0 | { |
805 | 0 | MCOperand *BaseReg = MCInst_getOperand(MI, Op + X86_AddrBaseReg); |
806 | 0 | MCOperand *IndexReg = MCInst_getOperand(MI, Op + X86_AddrIndexReg); |
807 | 0 | MCOperand *DispSpec = MCInst_getOperand(MI, Op + X86_AddrDisp); |
808 | 0 | MCOperand *SegReg = MCInst_getOperand(MI, Op + X86_AddrSegmentReg); |
809 | 0 | uint64_t ScaleVal; |
810 | 0 | int segreg; |
811 | 0 | int64_t DispVal = 1; |
812 | |
|
813 | 0 | if (MI->csh->detail) { |
814 | 0 | uint8_t access[6]; |
815 | |
|
816 | 0 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].type = X86_OP_MEM; |
817 | 0 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].size = MI->x86opsize; |
818 | 0 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.segment = X86_REG_INVALID; |
819 | 0 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.base = MCOperand_getReg(BaseReg); |
820 | 0 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.index = MCOperand_getReg(IndexReg); |
821 | 0 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.scale = 1; |
822 | 0 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.disp = 0; |
823 | |
|
824 | 0 | get_op_access(MI->csh, MCInst_getOpcode(MI), access, &MI->flat_insn->detail->x86.eflags); |
825 | 0 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].access = access[MI->flat_insn->detail->x86.op_count]; |
826 | 0 | } |
827 | | |
828 | | // If this has a segment register, print it. |
829 | 0 | segreg = MCOperand_getReg(SegReg); |
830 | 0 | if (segreg) { |
831 | 0 | _printOperand(MI, Op + X86_AddrSegmentReg, O); |
832 | 0 | if (MI->csh->detail) { |
833 | 0 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.segment = segreg; |
834 | 0 | } |
835 | |
|
836 | 0 | SStream_concat0(O, ":"); |
837 | 0 | } |
838 | |
|
839 | 0 | if (MCOperand_isImm(DispSpec)) { |
840 | 0 | DispVal = MCOperand_getImm(DispSpec); |
841 | 0 | if (MI->csh->detail) |
842 | 0 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.disp = DispVal; |
843 | 0 | if (DispVal) { |
844 | 0 | if (MCOperand_getReg(IndexReg) || MCOperand_getReg(BaseReg)) { |
845 | 0 | printInt64(O, DispVal); |
846 | 0 | } else { |
847 | | // only immediate as address of memory |
848 | 0 | if (DispVal < 0) { |
849 | 0 | SStream_concat(O, "0x%"PRIx64, arch_masks[MI->csh->mode] & DispVal); |
850 | 0 | } else { |
851 | 0 | if (DispVal > HEX_THRESHOLD) |
852 | 0 | SStream_concat(O, "0x%"PRIx64, DispVal); |
853 | 0 | else |
854 | 0 | SStream_concat(O, "%"PRIu64, DispVal); |
855 | 0 | } |
856 | 0 | } |
857 | 0 | } else { |
858 | 0 | } |
859 | 0 | } |
860 | |
|
861 | 0 | if (MCOperand_getReg(IndexReg) || MCOperand_getReg(BaseReg)) { |
862 | 0 | SStream_concat0(O, "("); |
863 | |
|
864 | 0 | if (MCOperand_getReg(BaseReg)) |
865 | 0 | _printOperand(MI, Op + X86_AddrBaseReg, O); |
866 | |
|
867 | 0 | if (MCOperand_getReg(IndexReg)) { |
868 | 0 | SStream_concat0(O, ", "); |
869 | 0 | _printOperand(MI, Op + X86_AddrIndexReg, O); |
870 | 0 | ScaleVal = MCOperand_getImm(MCInst_getOperand(MI, Op + X86_AddrScaleAmt)); |
871 | 0 | if (MI->csh->detail) |
872 | 0 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.scale = (int)ScaleVal; |
873 | 0 | if (ScaleVal != 1) { |
874 | 0 | SStream_concat(O, ", %u", ScaleVal); |
875 | 0 | } |
876 | 0 | } |
877 | 0 | SStream_concat0(O, ")"); |
878 | 0 | } else { |
879 | 0 | if (!DispVal) |
880 | 0 | SStream_concat0(O, "0"); |
881 | 0 | } |
882 | |
|
883 | 0 | if (MI->csh->detail) |
884 | 0 | MI->flat_insn->detail->x86.op_count++; |
885 | 0 | } |
886 | | |
887 | | static void printanymem(MCInst *MI, unsigned OpNo, SStream *O) |
888 | 0 | { |
889 | 0 | switch(MI->Opcode) { |
890 | 0 | default: break; |
891 | 0 | case X86_LEA16r: |
892 | 0 | MI->x86opsize = 2; |
893 | 0 | break; |
894 | 0 | case X86_LEA32r: |
895 | 0 | case X86_LEA64_32r: |
896 | 0 | MI->x86opsize = 4; |
897 | 0 | break; |
898 | 0 | case X86_LEA64r: |
899 | 0 | MI->x86opsize = 8; |
900 | 0 | break; |
901 | 0 | } |
902 | 0 | printMemReference(MI, OpNo, O); |
903 | 0 | } |
904 | | |
905 | | #include "X86InstPrinter.h" |
906 | | |
907 | | #define GET_REGINFO_ENUM |
908 | | #include "X86GenRegisterInfo.inc" |
909 | | |
910 | | // Include the auto-generated portion of the assembly writer. |
911 | | #define PRINT_ALIAS_INSTR |
912 | | #ifdef CAPSTONE_X86_REDUCE |
913 | | #include "X86GenAsmWriter_reduce.inc" |
914 | | #else |
915 | | #include "X86GenAsmWriter.inc" |
916 | | #endif |
917 | | |
918 | | static void printRegName(SStream *OS, unsigned RegNo) |
919 | 0 | { |
920 | 0 | SStream_concat(OS, "%%%s", getRegisterName(RegNo)); |
921 | 0 | } |
922 | | |
923 | | void X86_ATT_printInst(MCInst *MI, SStream *OS, void *info) |
924 | 0 | { |
925 | 0 | char *mnem; |
926 | 0 | x86_reg reg, reg2; |
927 | 0 | enum cs_ac_type access1, access2; |
928 | 0 | int i; |
929 | | |
930 | | // perhaps this instruction does not need printer |
931 | 0 | if (MI->assembly[0]) { |
932 | 0 | strncpy(OS->buffer, MI->assembly, sizeof(OS->buffer)); |
933 | 0 | return; |
934 | 0 | } |
935 | | |
936 | | // Output CALLpcrel32 as "callq" in 64-bit mode. |
937 | | // In Intel annotation it's always emitted as "call". |
938 | | // |
939 | | // TODO: Probably this hack should be redesigned via InstAlias in |
940 | | // InstrInfo.td as soon as Requires clause is supported properly |
941 | | // for InstAlias. |
942 | 0 | if (MI->csh->mode == CS_MODE_64 && MCInst_getOpcode(MI) == X86_CALLpcrel32) { |
943 | 0 | SStream_concat0(OS, "callq\t"); |
944 | 0 | MCInst_setOpcodePub(MI, X86_INS_CALL); |
945 | 0 | printPCRelImm(MI, 0, OS); |
946 | 0 | return; |
947 | 0 | } |
948 | | |
949 | | // Try to print any aliases first. |
950 | 0 | mnem = printAliasInstr(MI, OS, info); |
951 | 0 | if (mnem) |
952 | 0 | cs_mem_free(mnem); |
953 | 0 | else |
954 | 0 | printInstruction(MI, OS, info); |
955 | | |
956 | | // HACK TODO: fix this in machine description |
957 | 0 | switch(MI->flat_insn->id) { |
958 | 0 | default: break; |
959 | 0 | case X86_INS_SYSEXIT: |
960 | 0 | SStream_Init(OS); |
961 | 0 | SStream_concat0(OS, "sysexit"); |
962 | 0 | break; |
963 | 0 | } |
964 | | |
965 | 0 | if (MI->has_imm) { |
966 | | // if op_count > 1, then this operand's size is taken from the destination op |
967 | 0 | if (MI->flat_insn->detail->x86.op_count > 1) { |
968 | 0 | if (MI->flat_insn->id != X86_INS_LCALL && MI->flat_insn->id != X86_INS_LJMP) { |
969 | 0 | for (i = 0; i < MI->flat_insn->detail->x86.op_count; i++) { |
970 | 0 | if (MI->flat_insn->detail->x86.operands[i].type == X86_OP_IMM) |
971 | 0 | MI->flat_insn->detail->x86.operands[i].size = |
972 | 0 | MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count - 1].size; |
973 | 0 | } |
974 | 0 | } |
975 | 0 | } else |
976 | 0 | MI->flat_insn->detail->x86.operands[0].size = MI->imm_size; |
977 | 0 | } |
978 | |
|
979 | 0 | if (MI->csh->detail) { |
980 | 0 | uint8_t access[6] = {0}; |
981 | | |
982 | | // some instructions need to supply immediate 1 in the first op |
983 | 0 | switch(MCInst_getOpcode(MI)) { |
984 | 0 | default: |
985 | 0 | break; |
986 | 0 | case X86_SHL8r1: |
987 | 0 | case X86_SHL16r1: |
988 | 0 | case X86_SHL32r1: |
989 | 0 | case X86_SHL64r1: |
990 | 0 | case X86_SAL8r1: |
991 | 0 | case X86_SAL16r1: |
992 | 0 | case X86_SAL32r1: |
993 | 0 | case X86_SAL64r1: |
994 | 0 | case X86_SHR8r1: |
995 | 0 | case X86_SHR16r1: |
996 | 0 | case X86_SHR32r1: |
997 | 0 | case X86_SHR64r1: |
998 | 0 | case X86_SAR8r1: |
999 | 0 | case X86_SAR16r1: |
1000 | 0 | case X86_SAR32r1: |
1001 | 0 | case X86_SAR64r1: |
1002 | 0 | case X86_RCL8r1: |
1003 | 0 | case X86_RCL16r1: |
1004 | 0 | case X86_RCL32r1: |
1005 | 0 | case X86_RCL64r1: |
1006 | 0 | case X86_RCR8r1: |
1007 | 0 | case X86_RCR16r1: |
1008 | 0 | case X86_RCR32r1: |
1009 | 0 | case X86_RCR64r1: |
1010 | 0 | case X86_ROL8r1: |
1011 | 0 | case X86_ROL16r1: |
1012 | 0 | case X86_ROL32r1: |
1013 | 0 | case X86_ROL64r1: |
1014 | 0 | case X86_ROR8r1: |
1015 | 0 | case X86_ROR16r1: |
1016 | 0 | case X86_ROR32r1: |
1017 | 0 | case X86_ROR64r1: |
1018 | 0 | case X86_SHL8m1: |
1019 | 0 | case X86_SHL16m1: |
1020 | 0 | case X86_SHL32m1: |
1021 | 0 | case X86_SHL64m1: |
1022 | 0 | case X86_SAL8m1: |
1023 | 0 | case X86_SAL16m1: |
1024 | 0 | case X86_SAL32m1: |
1025 | 0 | case X86_SAL64m1: |
1026 | 0 | case X86_SHR8m1: |
1027 | 0 | case X86_SHR16m1: |
1028 | 0 | case X86_SHR32m1: |
1029 | 0 | case X86_SHR64m1: |
1030 | 0 | case X86_SAR8m1: |
1031 | 0 | case X86_SAR16m1: |
1032 | 0 | case X86_SAR32m1: |
1033 | 0 | case X86_SAR64m1: |
1034 | 0 | case X86_RCL8m1: |
1035 | 0 | case X86_RCL16m1: |
1036 | 0 | case X86_RCL32m1: |
1037 | 0 | case X86_RCL64m1: |
1038 | 0 | case X86_RCR8m1: |
1039 | 0 | case X86_RCR16m1: |
1040 | 0 | case X86_RCR32m1: |
1041 | 0 | case X86_RCR64m1: |
1042 | 0 | case X86_ROL8m1: |
1043 | 0 | case X86_ROL16m1: |
1044 | 0 | case X86_ROL32m1: |
1045 | 0 | case X86_ROL64m1: |
1046 | 0 | case X86_ROR8m1: |
1047 | 0 | case X86_ROR16m1: |
1048 | 0 | case X86_ROR32m1: |
1049 | 0 | case X86_ROR64m1: |
1050 | | // shift all the ops right to leave 1st slot for this new register op |
1051 | 0 | memmove(&(MI->flat_insn->detail->x86.operands[1]), &(MI->flat_insn->detail->x86.operands[0]), |
1052 | 0 | sizeof(MI->flat_insn->detail->x86.operands[0]) * (ARR_SIZE(MI->flat_insn->detail->x86.operands) - 1)); |
1053 | 0 | MI->flat_insn->detail->x86.operands[0].type = X86_OP_IMM; |
1054 | 0 | MI->flat_insn->detail->x86.operands[0].imm = 1; |
1055 | 0 | MI->flat_insn->detail->x86.operands[0].size = 1; |
1056 | 0 | MI->flat_insn->detail->x86.op_count++; |
1057 | 0 | } |
1058 | | |
1059 | | // special instruction needs to supply register op |
1060 | | // first op can be embedded in the asm by llvm. |
1061 | | // so we have to add the missing register as the first operand |
1062 | | |
1063 | | //printf(">>> opcode = %u\n", MCInst_getOpcode(MI)); |
1064 | | |
1065 | 0 | reg = X86_insn_reg_att(MCInst_getOpcode(MI), &access1); |
1066 | 0 | if (reg) { |
1067 | | // shift all the ops right to leave 1st slot for this new register op |
1068 | 0 | memmove(&(MI->flat_insn->detail->x86.operands[1]), &(MI->flat_insn->detail->x86.operands[0]), |
1069 | 0 | sizeof(MI->flat_insn->detail->x86.operands[0]) * (ARR_SIZE(MI->flat_insn->detail->x86.operands) - 1)); |
1070 | 0 | MI->flat_insn->detail->x86.operands[0].type = X86_OP_REG; |
1071 | 0 | MI->flat_insn->detail->x86.operands[0].reg = reg; |
1072 | 0 | MI->flat_insn->detail->x86.operands[0].size = MI->csh->regsize_map[reg]; |
1073 | 0 | MI->flat_insn->detail->x86.operands[0].access = access1; |
1074 | |
|
1075 | 0 | MI->flat_insn->detail->x86.op_count++; |
1076 | 0 | } else { |
1077 | 0 | if (X86_insn_reg_att2(MCInst_getOpcode(MI), ®, &access1, ®2, &access2)) { |
1078 | |
|
1079 | 0 | MI->flat_insn->detail->x86.operands[0].type = X86_OP_REG; |
1080 | 0 | MI->flat_insn->detail->x86.operands[0].reg = reg; |
1081 | 0 | MI->flat_insn->detail->x86.operands[0].size = MI->csh->regsize_map[reg]; |
1082 | 0 | MI->flat_insn->detail->x86.operands[0].access = access1; |
1083 | 0 | MI->flat_insn->detail->x86.operands[1].type = X86_OP_REG; |
1084 | 0 | MI->flat_insn->detail->x86.operands[1].reg = reg2; |
1085 | 0 | MI->flat_insn->detail->x86.operands[1].size = MI->csh->regsize_map[reg2]; |
1086 | 0 | MI->flat_insn->detail->x86.operands[0].access = access2; |
1087 | 0 | MI->flat_insn->detail->x86.op_count = 2; |
1088 | 0 | } |
1089 | 0 | } |
1090 | |
|
1091 | 0 | #ifndef CAPSTONE_DIET |
1092 | 0 | get_op_access(MI->csh, MCInst_getOpcode(MI), access, &MI->flat_insn->detail->x86.eflags); |
1093 | 0 | MI->flat_insn->detail->x86.operands[0].access = access[0]; |
1094 | 0 | MI->flat_insn->detail->x86.operands[1].access = access[1]; |
1095 | 0 | #endif |
1096 | 0 | } |
1097 | 0 | } |
1098 | | |
1099 | | #endif |