blob: 989fb0543bebc34a05a4282444f52e61c091a7fb [file] [log] [blame]
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001//===-- 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 Quynh8598a212014-05-14 11:26:41 +080015/* Capstone Disassembly Engine */
16/* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2014 */
17
Nguyen Anh Quynhf7850262014-05-14 22:03:06 +080018// this code is only relevant when DIET mode is disable
baguette86e84502014-08-17 20:59:05 +020019#if defined(CAPSTONE_HAS_X86) && !defined(CAPSTONE_DIET) && !defined(CAPSTONE_X86_ATT_DISABLE)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080020
reverserbcf09f42015-04-09 18:28:19 +010021#if !defined(CAPSTONE_HAS_OSXKERNEL)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080022#include <ctype.h>
reverserbcf09f42015-04-09 18:28:19 +010023#endif
Cr4sh9d606072015-03-29 18:29:06 +080024#include "../../myinttypes.h"
reverserbcf09f42015-04-09 18:28:19 +010025#if defined(CAPSTONE_HAS_OSXKERNEL)
26#include <libkern/libkern.h>
27#else
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080028#include <stdio.h>
29#include <stdlib.h>
reverserbcf09f42015-04-09 18:28:19 +010030#endif
31
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080032#include <string.h>
33
34#include "../../utils.h"
35#include "../../MCInst.h"
36#include "../../SStream.h"
37#include "../../MCRegisterInfo.h"
Nguyen Anh Quynhf328f302014-01-20 09:47:21 +080038#include "X86Mapping.h"
Nguyen Anh Quynh0b690382014-08-13 13:01:50 +080039#include "X86BaseInfo.h"
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080040
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080041
Nguyen Anh Quynhe93179b2014-04-25 11:18:40 +080042#define GET_INSTRINFO_ENUM
43#ifdef CAPSTONE_X86_REDUCE
44#include "X86GenInstrInfo_reduce.inc"
45#else
46#include "X86GenInstrInfo.inc"
47#endif
48
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080049static void printMemReference(MCInst *MI, unsigned Op, SStream *O);
Nguyen Anh Quynh13f40d22014-02-07 22:06:33 +080050static void printOperand(MCInst *MI, unsigned OpNo, SStream *O);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080051
Nguyen Anh Quynh10850732014-06-18 12:16:24 +080052
53static void set_mem_access(MCInst *MI, bool status)
54{
55 if (MI->csh->detail != CS_OPT_ON)
56 return;
57
58 MI->csh->doing_mem = status;
Nguyen Anh Quynh14ba46b2014-06-24 14:32:01 +080059 if (!status)
60 // done, create the next operand slot
61 MI->flat_insn->detail->x86.op_count++;
Nguyen Anh Quynh10850732014-06-18 12:16:24 +080062}
63
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080064static void printopaquemem(MCInst *MI, unsigned OpNo, SStream *O)
65{
Nguyen Anh Quynhe10b53f2015-02-10 00:21:00 +080066 switch(MI->csh->mode) {
67 case CS_MODE_16:
68 MI->x86opsize = 2;
69 break;
70 case CS_MODE_32:
71 MI->x86opsize = 4;
72 break;
73 case CS_MODE_64:
74 MI->x86opsize = 8;
75 break;
76 default: // never reach
77 break;
78 }
79
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080080 printMemReference(MI, OpNo, O);
81}
82
83static void printi8mem(MCInst *MI, unsigned OpNo, SStream *O)
84{
Nguyen Anh Quynh10850732014-06-18 12:16:24 +080085 MI->x86opsize = 1;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080086 printMemReference(MI, OpNo, O);
87}
88
89static void printi16mem(MCInst *MI, unsigned OpNo, SStream *O)
90{
Nguyen Anh Quynhd319c112014-12-26 16:49:10 +080091 MI->x86opsize = 2;
Nguyen Anh Quynh10850732014-06-18 12:16:24 +080092
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080093 printMemReference(MI, OpNo, O);
94}
95
96static void printi32mem(MCInst *MI, unsigned OpNo, SStream *O)
97{
Nguyen Anh Quynhd319c112014-12-26 16:49:10 +080098 MI->x86opsize = 4;
Nguyen Anh Quynh10850732014-06-18 12:16:24 +080099
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800100 printMemReference(MI, OpNo, O);
101}
102
103static void printi64mem(MCInst *MI, unsigned OpNo, SStream *O)
104{
Nguyen Anh Quynh10850732014-06-18 12:16:24 +0800105 MI->x86opsize = 8;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800106 printMemReference(MI, OpNo, O);
107}
108
109static void printi128mem(MCInst *MI, unsigned OpNo, SStream *O)
110{
Nguyen Anh Quynh10850732014-06-18 12:16:24 +0800111 MI->x86opsize = 16;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800112 printMemReference(MI, OpNo, O);
113}
114
Nguyen Anh Quynh59b54892014-03-27 10:54:44 +0800115#ifndef CAPSTONE_X86_REDUCE
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800116static void printi256mem(MCInst *MI, unsigned OpNo, SStream *O)
117{
Nguyen Anh Quynh10850732014-06-18 12:16:24 +0800118 MI->x86opsize = 32;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800119 printMemReference(MI, OpNo, O);
120}
121
122static void printi512mem(MCInst *MI, unsigned OpNo, SStream *O)
123{
Nguyen Anh Quynh10850732014-06-18 12:16:24 +0800124 MI->x86opsize = 64;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800125 printMemReference(MI, OpNo, O);
126}
127
128static void printf32mem(MCInst *MI, unsigned OpNo, SStream *O)
129{
Nguyen Anh Quynh10850732014-06-18 12:16:24 +0800130 MI->x86opsize = 4;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800131 printMemReference(MI, OpNo, O);
132}
133
134static void printf64mem(MCInst *MI, unsigned OpNo, SStream *O)
135{
Nguyen Anh Quynh10850732014-06-18 12:16:24 +0800136 MI->x86opsize = 8;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800137 printMemReference(MI, OpNo, O);
138}
139
140static void printf80mem(MCInst *MI, unsigned OpNo, SStream *O)
141{
Nguyen Anh Quynh10850732014-06-18 12:16:24 +0800142 MI->x86opsize = 10;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800143 printMemReference(MI, OpNo, O);
144}
145
146static void printf128mem(MCInst *MI, unsigned OpNo, SStream *O)
147{
Nguyen Anh Quynh10850732014-06-18 12:16:24 +0800148 MI->x86opsize = 16;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800149 printMemReference(MI, OpNo, O);
150}
151
152static void printf256mem(MCInst *MI, unsigned OpNo, SStream *O)
153{
Nguyen Anh Quynh10850732014-06-18 12:16:24 +0800154 MI->x86opsize = 32;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800155 printMemReference(MI, OpNo, O);
156}
157
158static void printf512mem(MCInst *MI, unsigned OpNo, SStream *O)
159{
Nguyen Anh Quynh10850732014-06-18 12:16:24 +0800160 MI->x86opsize = 64;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800161 printMemReference(MI, OpNo, O);
162}
163
Nguyen Anh Quynh95181482014-03-25 23:20:41 +0800164static void printSSECC(MCInst *MI, unsigned Op, SStream *OS)
165{
Nguyen Anh Quynh9f694cc2014-12-26 17:54:11 +0800166 int64_t Imm = MCOperand_getImm(MCInst_getOperand(MI, Op)) & 7;
Nguyen Anh Quynh95181482014-03-25 23:20:41 +0800167 switch (Imm) {
168 default: break; // never reach
Nguyen Anh Quynh9f694cc2014-12-26 17:54:11 +0800169 case 0: SStream_concat0(OS, "eq"); op_addSseCC(MI, X86_SSE_CC_EQ); break;
170 case 1: SStream_concat0(OS, "lt"); op_addSseCC(MI, X86_SSE_CC_LT); break;
171 case 2: SStream_concat0(OS, "le"); op_addSseCC(MI, X86_SSE_CC_LE); break;
172 case 3: SStream_concat0(OS, "unord"); op_addSseCC(MI, X86_SSE_CC_UNORD); break;
173 case 4: SStream_concat0(OS, "neq"); op_addSseCC(MI, X86_SSE_CC_NEQ); break;
174 case 5: SStream_concat0(OS, "nlt"); op_addSseCC(MI, X86_SSE_CC_NLT); break;
175 case 6: SStream_concat0(OS, "nle"); op_addSseCC(MI, X86_SSE_CC_NLE); break;
176 case 7: SStream_concat0(OS, "ord"); op_addSseCC(MI, X86_SSE_CC_ORD); break;
177 case 8: SStream_concat0(OS, "eq_uq"); op_addSseCC(MI, X86_SSE_CC_EQ_UQ); break;
178 case 9: SStream_concat0(OS, "nge"); op_addSseCC(MI, X86_SSE_CC_NGE); break;
179 case 0xa: SStream_concat0(OS, "ngt"); op_addSseCC(MI, X86_SSE_CC_NGT); break;
180 case 0xb: SStream_concat0(OS, "false"); op_addSseCC(MI, X86_SSE_CC_FALSE); break;
181 case 0xc: SStream_concat0(OS, "neq_oq"); op_addSseCC(MI, X86_SSE_CC_NEQ_OQ); break;
182 case 0xd: SStream_concat0(OS, "ge"); op_addSseCC(MI, X86_SSE_CC_GE); break;
183 case 0xe: SStream_concat0(OS, "gt"); op_addSseCC(MI, X86_SSE_CC_GT); break;
184 case 0xf: SStream_concat0(OS, "true"); op_addSseCC(MI, X86_SSE_CC_TRUE); break;
Nguyen Anh Quynh95181482014-03-25 23:20:41 +0800185 }
186}
187
188static void printAVXCC(MCInst *MI, unsigned Op, SStream *O)
189{
190 int64_t Imm = MCOperand_getImm(MCInst_getOperand(MI, Op)) & 0x1f;
191 switch (Imm) {
192 default: break;//printf("Invalid avxcc argument!\n"); break;
Nguyen Anh Quynh9f694cc2014-12-26 17:54:11 +0800193 case 0: SStream_concat0(O, "eq"); op_addAvxCC(MI, X86_AVX_CC_EQ); break;
194 case 1: SStream_concat0(O, "lt"); op_addAvxCC(MI, X86_AVX_CC_LT); break;
195 case 2: SStream_concat0(O, "le"); op_addAvxCC(MI, X86_AVX_CC_LE); break;
196 case 3: SStream_concat0(O, "unord"); op_addAvxCC(MI, X86_AVX_CC_UNORD); break;
197 case 4: SStream_concat0(O, "neq"); op_addAvxCC(MI, X86_AVX_CC_NEQ); break;
198 case 5: SStream_concat0(O, "nlt"); op_addAvxCC(MI, X86_AVX_CC_NLT); break;
199 case 6: SStream_concat0(O, "nle"); op_addAvxCC(MI, X86_AVX_CC_NLE); break;
200 case 7: SStream_concat0(O, "ord"); op_addAvxCC(MI, X86_AVX_CC_ORD); break;
201 case 8: SStream_concat0(O, "eq_uq"); op_addAvxCC(MI, X86_AVX_CC_EQ_UQ); break;
202 case 9: SStream_concat0(O, "nge"); op_addAvxCC(MI, X86_AVX_CC_NGE); break;
203 case 0xa: SStream_concat0(O, "ngt"); op_addAvxCC(MI, X86_AVX_CC_NGT); break;
204 case 0xb: SStream_concat0(O, "false"); op_addAvxCC(MI, X86_AVX_CC_FALSE); break;
205 case 0xc: SStream_concat0(O, "neq_oq"); op_addAvxCC(MI, X86_AVX_CC_NEQ_OQ); break;
206 case 0xd: SStream_concat0(O, "ge"); op_addAvxCC(MI, X86_AVX_CC_GE); break;
207 case 0xe: SStream_concat0(O, "gt"); op_addAvxCC(MI, X86_AVX_CC_GT); break;
208 case 0xf: SStream_concat0(O, "true"); op_addAvxCC(MI, X86_AVX_CC_TRUE); break;
209 case 0x10: SStream_concat0(O, "eq_os"); op_addAvxCC(MI, X86_AVX_CC_EQ_OS); break;
210 case 0x11: SStream_concat0(O, "lt_oq"); op_addAvxCC(MI, X86_AVX_CC_LT_OQ); break;
211 case 0x12: SStream_concat0(O, "le_oq"); op_addAvxCC(MI, X86_AVX_CC_LE_OQ); break;
212 case 0x13: SStream_concat0(O, "unord_s"); op_addAvxCC(MI, X86_AVX_CC_UNORD_S); break;
213 case 0x14: SStream_concat0(O, "neq_us"); op_addAvxCC(MI, X86_AVX_CC_NEQ_US); break;
214 case 0x15: SStream_concat0(O, "nlt_uq"); op_addAvxCC(MI, X86_AVX_CC_NLT_UQ); break;
215 case 0x16: SStream_concat0(O, "nle_uq"); op_addAvxCC(MI, X86_AVX_CC_NLE_UQ); break;
216 case 0x17: SStream_concat0(O, "ord_s"); op_addAvxCC(MI, X86_AVX_CC_ORD_S); break;
217 case 0x18: SStream_concat0(O, "eq_us"); op_addAvxCC(MI, X86_AVX_CC_EQ_US); break;
218 case 0x19: SStream_concat0(O, "nge_uq"); op_addAvxCC(MI, X86_AVX_CC_NGE_UQ); break;
219 case 0x1a: SStream_concat0(O, "ngt_uq"); op_addAvxCC(MI, X86_AVX_CC_NGT_UQ); break;
220 case 0x1b: SStream_concat0(O, "false_os"); op_addAvxCC(MI, X86_AVX_CC_FALSE_OS); break;
221 case 0x1c: SStream_concat0(O, "neq_os"); op_addAvxCC(MI, X86_AVX_CC_NEQ_OS); break;
222 case 0x1d: SStream_concat0(O, "ge_oq"); op_addAvxCC(MI, X86_AVX_CC_GE_OQ); break;
223 case 0x1e: SStream_concat0(O, "gt_oq"); op_addAvxCC(MI, X86_AVX_CC_GT_OQ); break;
224 case 0x1f: SStream_concat0(O, "true_us"); op_addAvxCC(MI, X86_AVX_CC_TRUE_US); break;
Nguyen Anh Quynh95181482014-03-25 23:20:41 +0800225 }
226}
227
228static void printRoundingControl(MCInst *MI, unsigned Op, SStream *O)
229{
230 int64_t Imm = MCOperand_getImm(MCInst_getOperand(MI, Op)) & 0x3;
231 switch (Imm) {
Nguyen Anh Quynh1a66fec2014-06-26 12:09:15 +0800232 case 0: SStream_concat0(O, "{rn-sae}"); op_addAvxSae(MI); op_addAvxRoundingMode(MI, X86_AVX_RM_RN); break;
233 case 1: SStream_concat0(O, "{rd-sae}"); op_addAvxSae(MI); op_addAvxRoundingMode(MI, X86_AVX_RM_RD); break;
234 case 2: SStream_concat0(O, "{ru-sae}"); op_addAvxSae(MI); op_addAvxRoundingMode(MI, X86_AVX_RM_RU); break;
235 case 3: SStream_concat0(O, "{rz-sae}"); op_addAvxSae(MI); op_addAvxRoundingMode(MI, X86_AVX_RM_RZ); break;
Nguyen Anh Quynha62b9a02014-06-04 22:25:48 +0700236 default: break; // nev0er reach
Nguyen Anh Quynh95181482014-03-25 23:20:41 +0800237 }
238}
239
240#endif
241
Nguyen Anh Quynh14ba46b2014-06-24 14:32:01 +0800242static void printRegName(SStream *OS, unsigned RegNo);
243
244// local printOperand, without updating public operands
245static void _printOperand(MCInst *MI, unsigned OpNo, SStream *O)
246{
247 MCOperand *Op = MCInst_getOperand(MI, OpNo);
248 if (MCOperand_isReg(Op)) {
249 printRegName(O, MCOperand_getReg(Op));
250 } else if (MCOperand_isImm(Op)) {
251 // Print X86 immediates as signed values.
252 int64_t imm = MCOperand_getImm(Op);
253 if (imm < 0) {
254 if (imm < -HEX_THRESHOLD)
255 SStream_concat(O, "$-0x%"PRIx64, -imm);
256 else
257 SStream_concat(O, "$-%"PRIu64, -imm);
258 } else {
259 if (imm > HEX_THRESHOLD)
260 SStream_concat(O, "$0x%"PRIx64, imm);
261 else
262 SStream_concat(O, "$%"PRIu64, imm);
263 }
264 }
265}
266
Nguyen Anh Quynh13f40d22014-02-07 22:06:33 +0800267static void printSrcIdx(MCInst *MI, unsigned Op, SStream *O)
268{
269 MCOperand *SegReg;
Nguyen Anh Quynh14ba46b2014-06-24 14:32:01 +0800270 int reg;
271
272 if (MI->csh->detail) {
273 MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].type = X86_OP_MEM;
274 MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].size = MI->x86opsize;
275 MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.segment = X86_REG_INVALID;
276 MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.base = X86_REG_INVALID;
277 MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.index = X86_REG_INVALID;
278 MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.scale = 1;
279 MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.disp = 0;
280 }
Nguyen Anh Quynh13f40d22014-02-07 22:06:33 +0800281
282 SegReg = MCInst_getOperand(MI, Op+1);
Nguyen Anh Quynh14ba46b2014-06-24 14:32:01 +0800283 reg = MCOperand_getReg(SegReg);
Nguyen Anh Quynh13f40d22014-02-07 22:06:33 +0800284
Nguyen Anh Quynh13f40d22014-02-07 22:06:33 +0800285 // If this has a segment register, print it.
Nguyen Anh Quynh14ba46b2014-06-24 14:32:01 +0800286 if (reg) {
287 _printOperand(MI, Op+1, O);
288 if (MI->csh->detail) {
289 MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.segment = reg;
290 }
291
Nguyen Anh Quynha62b9a02014-06-04 22:25:48 +0700292 SStream_concat0(O, ":");
Nguyen Anh Quynh13f40d22014-02-07 22:06:33 +0800293 }
294
Nguyen Anh Quynha62b9a02014-06-04 22:25:48 +0700295 SStream_concat0(O, "(");
Nguyen Anh Quynh10850732014-06-18 12:16:24 +0800296 set_mem_access(MI, true);
Nguyen Anh Quynh13f40d22014-02-07 22:06:33 +0800297
298 printOperand(MI, Op, O);
299
Nguyen Anh Quynh1e688d42014-06-18 14:28:55 +0800300 SStream_concat0(O, ")");
Nguyen Anh Quynh10850732014-06-18 12:16:24 +0800301 set_mem_access(MI, false);
Nguyen Anh Quynh13f40d22014-02-07 22:06:33 +0800302}
303
304static void printDstIdx(MCInst *MI, unsigned Op, SStream *O)
305{
Nguyen Anh Quynh14ba46b2014-06-24 14:32:01 +0800306 if (MI->csh->detail) {
307 MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].type = X86_OP_MEM;
308 MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].size = MI->x86opsize;
309 MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.segment = X86_REG_INVALID;
310 MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.base = X86_REG_INVALID;
311 MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.index = X86_REG_INVALID;
312 MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.scale = 1;
313 MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.disp = 0;
314 }
315
Nguyen Anh Quynh10850732014-06-18 12:16:24 +0800316 // DI accesses are always ES-based on non-64bit mode
Nguyen Anh Quynh14ba46b2014-06-24 14:32:01 +0800317 if (MI->csh->mode != CS_MODE_64) {
Nguyen Anh Quynh1e688d42014-06-18 14:28:55 +0800318 SStream_concat0(O, "%es:(");
Nguyen Anh Quynh14ba46b2014-06-24 14:32:01 +0800319 if (MI->csh->detail) {
320 MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.segment = X86_REG_ES;
321 }
322 } else
Nguyen Anh Quynh1e688d42014-06-18 14:28:55 +0800323 SStream_concat0(O, "(");
Nguyen Anh Quynh14ba46b2014-06-24 14:32:01 +0800324
Nguyen Anh Quynh10850732014-06-18 12:16:24 +0800325 set_mem_access(MI, true);
326
Nguyen Anh Quynh13f40d22014-02-07 22:06:33 +0800327 printOperand(MI, Op, O);
328
Nguyen Anh Quynh1e688d42014-06-18 14:28:55 +0800329 SStream_concat0(O, ")");
Nguyen Anh Quynh10850732014-06-18 12:16:24 +0800330 set_mem_access(MI, false);
Nguyen Anh Quynh13f40d22014-02-07 22:06:33 +0800331}
332
333static void printSrcIdx8(MCInst *MI, unsigned OpNo, SStream *O)
334{
Nguyen Anh Quynh10850732014-06-18 12:16:24 +0800335 MI->x86opsize = 1;
Nguyen Anh Quynh13f40d22014-02-07 22:06:33 +0800336 printSrcIdx(MI, OpNo, O);
337}
338
339static void printSrcIdx16(MCInst *MI, unsigned OpNo, SStream *O)
340{
Nguyen Anh Quynh10850732014-06-18 12:16:24 +0800341 MI->x86opsize = 2;
Nguyen Anh Quynh13f40d22014-02-07 22:06:33 +0800342 printSrcIdx(MI, OpNo, O);
343}
344
345static void printSrcIdx32(MCInst *MI, unsigned OpNo, SStream *O)
346{
Nguyen Anh Quynh10850732014-06-18 12:16:24 +0800347 MI->x86opsize = 4;
Nguyen Anh Quynh13f40d22014-02-07 22:06:33 +0800348 printSrcIdx(MI, OpNo, O);
349}
350
351static void printSrcIdx64(MCInst *MI, unsigned OpNo, SStream *O)
352{
Nguyen Anh Quynh10850732014-06-18 12:16:24 +0800353 MI->x86opsize = 8;
Nguyen Anh Quynh13f40d22014-02-07 22:06:33 +0800354 printSrcIdx(MI, OpNo, O);
355}
356
357static void printDstIdx8(MCInst *MI, unsigned OpNo, SStream *O)
358{
Nguyen Anh Quynh10850732014-06-18 12:16:24 +0800359 MI->x86opsize = 1;
Nguyen Anh Quynh13f40d22014-02-07 22:06:33 +0800360 printDstIdx(MI, OpNo, O);
361}
362
363static void printDstIdx16(MCInst *MI, unsigned OpNo, SStream *O)
364{
Nguyen Anh Quynh10850732014-06-18 12:16:24 +0800365 MI->x86opsize = 2;
Nguyen Anh Quynh13f40d22014-02-07 22:06:33 +0800366 printDstIdx(MI, OpNo, O);
367}
368
369static void printDstIdx32(MCInst *MI, unsigned OpNo, SStream *O)
370{
Nguyen Anh Quynh10850732014-06-18 12:16:24 +0800371 MI->x86opsize = 4;
Nguyen Anh Quynh13f40d22014-02-07 22:06:33 +0800372 printDstIdx(MI, OpNo, O);
373}
374
375static void printDstIdx64(MCInst *MI, unsigned OpNo, SStream *O)
376{
Nguyen Anh Quynh10850732014-06-18 12:16:24 +0800377 MI->x86opsize = 8;
Nguyen Anh Quynh13f40d22014-02-07 22:06:33 +0800378 printDstIdx(MI, OpNo, O);
379}
380
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800381static void printMemOffset(MCInst *MI, unsigned Op, SStream *O)
382{
383 MCOperand *DispSpec = MCInst_getOperand(MI, Op);
Nguyen Anh Quynh13f40d22014-02-07 22:06:33 +0800384 MCOperand *SegReg = MCInst_getOperand(MI, Op+1);
Nguyen Anh Quynh14ba46b2014-06-24 14:32:01 +0800385 int reg;
Nguyen Anh Quynh13f40d22014-02-07 22:06:33 +0800386
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800387 if (MI->csh->detail) {
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700388 MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].type = X86_OP_MEM;
Nguyen Anh Quynh10850732014-06-18 12:16:24 +0800389 MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].size = MI->x86opsize;
Nguyen Anh Quynh14ba46b2014-06-24 14:32:01 +0800390 MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.segment = X86_REG_INVALID;
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700391 MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.base = X86_REG_INVALID;
392 MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.index = X86_REG_INVALID;
393 MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.scale = 1;
394 MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.disp = 0;
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800395 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800396
Nguyen Anh Quynh14ba46b2014-06-24 14:32:01 +0800397 // If this has a segment register, print it.
398 reg = MCOperand_getReg(SegReg);
399 if (reg) {
400 _printOperand(MI, Op + 1, O);
401 SStream_concat0(O, ":");
402 if (MI->csh->detail) {
403 MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.segment = reg;
404 }
405 }
406
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800407 if (MCOperand_isImm(DispSpec)) {
408 int64_t imm = MCOperand_getImm(DispSpec);
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800409 if (MI->csh->detail)
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700410 MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.disp = imm;
Nguyen Anh Quynh4fe42812013-12-13 12:26:26 +0800411 if (imm < 0) {
Nguyen Anh Quynh6d3d8002014-03-29 17:26:51 +0800412 SStream_concat(O, "0x%"PRIx64, arch_masks[MI->csh->mode] & imm);
Nguyen Anh Quynh4fe42812013-12-13 12:26:26 +0800413 } else {
Nguyen Anh Quynhf22557b2013-12-13 09:37:52 +0800414 if (imm > HEX_THRESHOLD)
415 SStream_concat(O, "0x%"PRIx64, imm);
416 else
417 SStream_concat(O, "%"PRIu64, imm);
418 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800419 }
420
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800421 if (MI->csh->detail)
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700422 MI->flat_insn->detail->x86.op_count++;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800423}
424
425static void printMemOffs8(MCInst *MI, unsigned OpNo, SStream *O)
426{
Nguyen Anh Quynh10850732014-06-18 12:16:24 +0800427 MI->x86opsize = 1;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800428 printMemOffset(MI, OpNo, O);
429}
430
431static void printMemOffs16(MCInst *MI, unsigned OpNo, SStream *O)
432{
Nguyen Anh Quynh10850732014-06-18 12:16:24 +0800433 MI->x86opsize = 2;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800434 printMemOffset(MI, OpNo, O);
435}
436
437static void printMemOffs32(MCInst *MI, unsigned OpNo, SStream *O)
438{
Nguyen Anh Quynh10850732014-06-18 12:16:24 +0800439 MI->x86opsize = 4;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800440 printMemOffset(MI, OpNo, O);
441}
442
443static void printMemOffs64(MCInst *MI, unsigned OpNo, SStream *O)
444{
Nguyen Anh Quynh10850732014-06-18 12:16:24 +0800445 MI->x86opsize = 8;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800446 printMemOffset(MI, OpNo, O);
447}
448
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800449/// printPCRelImm - This is used to print an immediate value that ends up
450/// being encoded as a pc-relative value (e.g. for jumps and calls). These
451/// print slightly differently than normal immediates. For example, a $ is not
452/// emitted.
453static void printPCRelImm(MCInst *MI, unsigned OpNo, SStream *O)
454{
455 MCOperand *Op = MCInst_getOperand(MI, OpNo);
456 if (MCOperand_isImm(Op)) {
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700457 int64_t imm = MCOperand_getImm(Op) + MI->flat_insn->size + MI->address;
Nguyen Anh Quynh4fe42812013-12-13 12:26:26 +0800458 if (imm < 0) {
Nguyen Anh Quynha90b0472014-10-02 12:04:35 +0800459 SStream_concat(O, "0x%"PRIx64, imm);
Nguyen Anh Quynh4fe42812013-12-13 12:26:26 +0800460 } else {
Nguyen Anh Quynh2d342512014-05-11 15:33:11 +0800461 // handle 16bit segment bound
462 if (MI->csh->mode == CS_MODE_16 && imm > 0x100000)
463 imm -= 0x10000;
464
Nguyen Anh Quynhf22557b2013-12-13 09:37:52 +0800465 if (imm > HEX_THRESHOLD)
466 SStream_concat(O, "0x%"PRIx64, imm);
467 else
468 SStream_concat(O, "%"PRIu64, imm);
469 }
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800470 if (MI->csh->detail) {
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700471 MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].type = X86_OP_IMM;
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800472 MI->has_imm = true;
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700473 MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].imm = imm;
474 MI->flat_insn->detail->x86.op_count++;
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800475 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800476 }
477}
478
479static void printOperand(MCInst *MI, unsigned OpNo, SStream *O)
480{
Nguyen Anh Quynh3f00a722015-06-06 00:04:03 +0800481 int opsize = 0;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800482 MCOperand *Op = MCInst_getOperand(MI, OpNo);
Nguyen Anh Quynh3f00a722015-06-06 00:04:03 +0800483
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800484 if (MCOperand_isReg(Op)) {
Nguyen Anh Quynhea3c0892014-10-02 10:17:55 +0800485 unsigned int reg = MCOperand_getReg(Op);
486 printRegName(O, reg);
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800487 if (MI->csh->detail) {
Nguyen Anh Quynh10850732014-06-18 12:16:24 +0800488 if (MI->csh->doing_mem) {
Nguyen Anh Quynh10850732014-06-18 12:16:24 +0800489 MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.base = reg;
Nguyen Anh Quynh10850732014-06-18 12:16:24 +0800490 } else {
491 MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].type = X86_OP_REG;
492 MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].reg = reg;
493 MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].size = MI->csh->regsize_map[reg];
494 MI->flat_insn->detail->x86.op_count++;
495 }
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800496 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800497 } else if (MCOperand_isImm(Op)) {
498 // Print X86 immediates as signed values.
499 int64_t imm = MCOperand_getImm(Op);
Nguyen Anh Quynh9cc87872014-11-04 11:04:16 +0800500
Nguyen Anh Quynh3f00a722015-06-06 00:04:03 +0800501 switch(MCInst_getOpcode(MI)) {
502 default:
503 break;
504
505 case X86_AAD8i8:
506 case X86_AAM8i8:
507 case X86_ADC8i8:
508 case X86_ADD8i8:
509 case X86_AND8i8:
510 case X86_CMP8i8:
511 case X86_OR8i8:
512 case X86_SBB8i8:
513 case X86_SUB8i8:
514 case X86_TEST8i8:
515 case X86_XOR8i8:
516 case X86_ROL8ri:
517 case X86_ADC8ri:
518 case X86_ADD8ri:
519 case X86_ADD8ri8:
520 case X86_AND8ri:
521 case X86_AND8ri8:
522 case X86_CMP8ri:
523 case X86_MOV8ri:
524 case X86_MOV8ri_alt:
525 case X86_OR8ri:
526 case X86_OR8ri8:
527 case X86_RCL8ri:
528 case X86_RCR8ri:
529 case X86_ROR8ri:
530 case X86_SAL8ri:
531 case X86_SAR8ri:
532 case X86_SBB8ri:
533 case X86_SHL8ri:
534 case X86_SHR8ri:
535 case X86_SUB8ri:
536 case X86_SUB8ri8:
537 case X86_TEST8ri:
538 case X86_TEST8ri_NOREX:
539 case X86_TEST8ri_alt:
540 case X86_XOR8ri:
541 case X86_XOR8ri8:
542 case X86_OUT8ir:
543
544 case X86_ADC8mi:
545 case X86_ADD8mi:
546 case X86_AND8mi:
547 case X86_CMP8mi:
548 case X86_LOCK_ADD8mi:
549 case X86_LOCK_AND8mi:
550 case X86_LOCK_OR8mi:
551 case X86_LOCK_SUB8mi:
552 case X86_LOCK_XOR8mi:
553 case X86_MOV8mi:
554 case X86_OR8mi:
555 case X86_RCL8mi:
556 case X86_RCR8mi:
557 case X86_ROL8mi:
558 case X86_ROR8mi:
559 case X86_SAL8mi:
560 case X86_SAR8mi:
561 case X86_SBB8mi:
562 case X86_SHL8mi:
563 case X86_SHR8mi:
564 case X86_SUB8mi:
565 case X86_TEST8mi:
566 case X86_TEST8mi_alt:
567 case X86_XOR8mi:
568 case X86_PUSH64i8:
569 case X86_CMP32ri8:
570 case X86_CMP64ri8:
571
572 imm = imm & 0xff;
573 opsize = 1; // immediate of 1 byte
574 break;
575 }
576
Nguyen Anh Quynh9cc87872014-11-04 11:04:16 +0800577 switch(MI->flat_insn->id) {
578 default:
579 if (imm >= 0) {
580 if (imm > HEX_THRESHOLD)
581 SStream_concat(O, "$0x%"PRIx64, imm);
582 else
583 SStream_concat(O, "$%"PRIu64, imm);
584 } else {
585 if (imm < -HEX_THRESHOLD)
586 SStream_concat(O, "$-0x%"PRIx64, -imm);
587 else
588 SStream_concat(O, "$-%"PRIu64, -imm);
589 }
590 break;
Nguyen Anh Quynh36d05942015-06-20 10:56:54 +0800591
592 case X86_INS_AND:
593 case X86_INS_OR:
594 case X86_INS_XOR:
595 // do not print number in negative form
596 if (imm >= 0 && imm <= HEX_THRESHOLD)
597 SStream_concat(O, "$%u", imm);
598 else {
599 imm = arch_masks[MI->op1_size? MI->op1_size : MI->imm_size] & imm;
600 SStream_concat(O, "$0x%"PRIx64, imm);
601 }
602 break;
603
Nguyen Anh Quynh9cc87872014-11-04 11:04:16 +0800604 case X86_INS_RET:
605 // RET imm16
606 if (imm >= 0 && imm <= HEX_THRESHOLD)
607 SStream_concat(O, "$%u", imm);
608 else {
609 imm = 0xffff & imm;
610 SStream_concat(O, "$0x%x", imm);
611 }
612 break;
Nguyen Anh Quynhbe906392013-12-13 15:37:57 +0800613 }
Nguyen Anh Quynh9cc87872014-11-04 11:04:16 +0800614
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800615 if (MI->csh->detail) {
Nguyen Anh Quynh10850732014-06-18 12:16:24 +0800616 if (MI->csh->doing_mem) {
617 MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].type = X86_OP_MEM;
Nguyen Anh Quynh10850732014-06-18 12:16:24 +0800618 MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.disp = imm;
Nguyen Anh Quynh10850732014-06-18 12:16:24 +0800619 } else {
620 MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].type = X86_OP_IMM;
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800621 MI->has_imm = true;
Nguyen Anh Quynh10850732014-06-18 12:16:24 +0800622 MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].imm = imm;
Nguyen Anh Quynh36d05942015-06-20 10:56:54 +0800623
Nguyen Anh Quynh3f00a722015-06-06 00:04:03 +0800624 if (opsize > 0)
625 MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].size = opsize;
626 else if (MI->op1_size > 0)
627 MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].size = MI->op1_size;
628 else
629 MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].size = MI->imm_size;
Nguyen Anh Quynh36d05942015-06-20 10:56:54 +0800630
Nguyen Anh Quynh10850732014-06-18 12:16:24 +0800631 MI->flat_insn->detail->x86.op_count++;
632 }
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800633 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800634 }
635}
636
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800637static void printMemReference(MCInst *MI, unsigned Op, SStream *O)
638{
Nguyen Anh Quynh0b690382014-08-13 13:01:50 +0800639 MCOperand *BaseReg = MCInst_getOperand(MI, Op + X86_AddrBaseReg);
640 MCOperand *IndexReg = MCInst_getOperand(MI, Op + X86_AddrIndexReg);
641 MCOperand *DispSpec = MCInst_getOperand(MI, Op + X86_AddrDisp);
642 MCOperand *SegReg = MCInst_getOperand(MI, Op + X86_AddrSegmentReg);
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800643 uint64_t ScaleVal;
Nguyen Anh Quynh14ba46b2014-06-24 14:32:01 +0800644 int reg;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800645
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800646 if (MI->csh->detail) {
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700647 MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].type = X86_OP_MEM;
Nguyen Anh Quynh10850732014-06-18 12:16:24 +0800648 MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].size = MI->x86opsize;
Nguyen Anh Quynh14ba46b2014-06-24 14:32:01 +0800649 MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.segment = X86_REG_INVALID;
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700650 MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.base = MCOperand_getReg(BaseReg);
651 MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.index = MCOperand_getReg(IndexReg);
652 MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.scale = 1;
653 MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.disp = 0;
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800654 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800655
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800656 // If this has a segment register, print it.
Nguyen Anh Quynh14ba46b2014-06-24 14:32:01 +0800657 reg = MCOperand_getReg(SegReg);
658 if (reg) {
Nguyen Anh Quynh0b690382014-08-13 13:01:50 +0800659 _printOperand(MI, Op + X86_AddrSegmentReg, O);
Nguyen Anh Quynh14ba46b2014-06-24 14:32:01 +0800660 if (MI->csh->detail) {
661 MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.segment = reg;
662 }
663
Nguyen Anh Quynha62b9a02014-06-04 22:25:48 +0700664 SStream_concat0(O, ":");
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800665 }
666
667 if (MCOperand_isImm(DispSpec)) {
668 int64_t DispVal = MCOperand_getImm(DispSpec);
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800669 if (MI->csh->detail)
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700670 MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.disp = DispVal;
Nguyen Anh Quynhb87f8552014-11-02 23:38:35 +0800671 if (DispVal) {
672 if (MCOperand_getReg(IndexReg) || MCOperand_getReg(BaseReg)) {
673 if (DispVal < 0) {
674 if (DispVal < -HEX_THRESHOLD)
Nguyen Anh Quynh9cc87872014-11-04 11:04:16 +0800675 SStream_concat(O, "-0x%"PRIx64, -DispVal);
Nguyen Anh Quynhb87f8552014-11-02 23:38:35 +0800676 else
Nguyen Anh Quynh9cc87872014-11-04 11:04:16 +0800677 SStream_concat(O, "-%"PRIu64, -DispVal);
Nguyen Anh Quynhb87f8552014-11-02 23:38:35 +0800678 } else {
679 if (DispVal > HEX_THRESHOLD)
680 SStream_concat(O, "0x%"PRIx64, DispVal);
681 else
682 SStream_concat(O, "%"PRIu64, DispVal);
683 }
Nguyen Anh Quynh4fe42812013-12-13 12:26:26 +0800684 } else {
Nguyen Anh Quynhb87f8552014-11-02 23:38:35 +0800685 // only immediate as address of memory
686 if (DispVal < 0) {
687 SStream_concat(O, "0x%"PRIx64, arch_masks[MI->csh->mode] & DispVal);
688 } else {
689 if (DispVal > HEX_THRESHOLD)
690 SStream_concat(O, "0x%"PRIx64, DispVal);
691 else
692 SStream_concat(O, "%"PRIu64, DispVal);
693 }
Nguyen Anh Quynhf22557b2013-12-13 09:37:52 +0800694 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800695 }
696 }
697
698 if (MCOperand_getReg(IndexReg) || MCOperand_getReg(BaseReg)) {
Nguyen Anh Quynha62b9a02014-06-04 22:25:48 +0700699 SStream_concat0(O, "(");
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800700
701 if (MCOperand_getReg(BaseReg))
Nguyen Anh Quynh0b690382014-08-13 13:01:50 +0800702 _printOperand(MI, Op + X86_AddrBaseReg, O);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800703
704 if (MCOperand_getReg(IndexReg)) {
Nguyen Anh Quynha62b9a02014-06-04 22:25:48 +0700705 SStream_concat0(O, ", ");
Nguyen Anh Quynh0b690382014-08-13 13:01:50 +0800706 _printOperand(MI, Op + X86_AddrIndexReg, O);
707 ScaleVal = MCOperand_getImm(MCInst_getOperand(MI, Op + X86_AddrScaleAmt));
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800708 if (MI->csh->detail)
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700709 MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.scale = (int)ScaleVal;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800710 if (ScaleVal != 1) {
Nguyen Anh Quynh1e688d42014-06-18 14:28:55 +0800711 SStream_concat(O, ", %u", ScaleVal);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800712 }
713 }
Nguyen Anh Quynha62b9a02014-06-04 22:25:48 +0700714 SStream_concat0(O, ")");
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800715 }
716
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800717 if (MI->csh->detail)
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700718 MI->flat_insn->detail->x86.op_count++;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800719}
720
721#include "X86InstPrinter.h"
722
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800723#define GET_REGINFO_ENUM
724#include "X86GenRegisterInfo.inc"
725
726// Include the auto-generated portion of the assembly writer.
727#define PRINT_ALIAS_INSTR
Nguyen Anh Quynh59b54892014-03-27 10:54:44 +0800728#ifdef CAPSTONE_X86_REDUCE
729#include "X86GenAsmWriter_reduce.inc"
Nguyen Anh Quynh95181482014-03-25 23:20:41 +0800730#else
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800731#include "X86GenAsmWriter.inc"
Nguyen Anh Quynh95181482014-03-25 23:20:41 +0800732#endif
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800733
734static void printRegName(SStream *OS, unsigned RegNo)
735{
Nguyen Anh Quynh1e688d42014-06-18 14:28:55 +0800736 SStream_concat(OS, "%%%s", getRegisterName(RegNo));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800737}
738
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800739void X86_ATT_printInst(MCInst *MI, SStream *OS, void *info)
740{
Nguyen Anh Quynh27b9a962014-02-19 10:13:47 +0800741 char *mnem;
Nguyen Anh Quynh95781852014-12-12 11:25:12 +0800742 x86_reg reg, reg2;
Nguyen Anh Quynhf1ec5262014-06-25 22:03:18 +0800743 int i;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800744
Nguyen Anh Quynh1f196d12014-09-09 23:59:38 +0800745 // Output CALLpcrel32 as "callq" in 64-bit mode.
746 // In Intel annotation it's always emitted as "call".
747 //
748 // TODO: Probably this hack should be redesigned via InstAlias in
749 // InstrInfo.td as soon as Requires clause is supported properly
750 // for InstAlias.
751 if (MI->csh->mode == CS_MODE_64 && MCInst_getOpcode(MI) == X86_CALLpcrel32) {
752 SStream_concat0(OS, "callq\t");
753 MCInst_setOpcodePub(MI, X86_INS_CALL);
754 printPCRelImm(MI, 0, OS);
755 return;
756 }
757
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800758 // Try to print any aliases first.
Nguyen Anh Quynh0b690382014-08-13 13:01:50 +0800759 mnem = printAliasInstr(MI, OS, info);
Nguyen Anh Quynh27b9a962014-02-19 10:13:47 +0800760 if (mnem)
761 cs_mem_free(mnem);
762 else
Nguyen Anh Quynh0b690382014-08-13 13:01:50 +0800763 printInstruction(MI, OS, info);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800764
Nguyen Anh Quynhf1ec5262014-06-25 22:03:18 +0800765 if (MI->has_imm) {
766 // if op_count > 1, then this operand's size is taken from the destination op
767 if (MI->flat_insn->detail->x86.op_count > 1) {
768 for (i = 0; i < MI->flat_insn->detail->x86.op_count; i++) {
769 if (MI->flat_insn->detail->x86.operands[i].type == X86_OP_IMM)
770 MI->flat_insn->detail->x86.operands[i].size = MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count - 1].size;
771 }
772 } else
773 MI->flat_insn->detail->x86.operands[0].size = MI->imm_size;
774 }
775
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800776 if (MI->csh->detail) {
Nguyen Anh Quynh27526f32015-05-08 14:23:08 +0800777 // some instructions need to supply immediate 1 in the first op
778 switch(MCInst_getOpcode(MI)) {
779 default:
780 break;
781 case X86_SHL8r1:
782 case X86_SHL16r1:
783 case X86_SHL32r1:
784 case X86_SHL64r1:
785 case X86_SAL8r1:
786 case X86_SAL16r1:
787 case X86_SAL32r1:
788 case X86_SAL64r1:
789 case X86_SHR8r1:
790 case X86_SHR16r1:
791 case X86_SHR32r1:
792 case X86_SHR64r1:
793 case X86_SAR8r1:
794 case X86_SAR16r1:
795 case X86_SAR32r1:
796 case X86_SAR64r1:
797 case X86_RCL8r1:
798 case X86_RCL16r1:
799 case X86_RCL32r1:
800 case X86_RCL64r1:
801 case X86_RCR8r1:
802 case X86_RCR16r1:
803 case X86_RCR32r1:
804 case X86_RCR64r1:
805 case X86_ROL8r1:
806 case X86_ROL16r1:
807 case X86_ROL32r1:
808 case X86_ROL64r1:
809 case X86_ROR8r1:
810 case X86_ROR16r1:
811 case X86_ROR32r1:
812 case X86_ROR64r1:
813 case X86_SHL8m1:
814 case X86_SHL16m1:
815 case X86_SHL32m1:
816 case X86_SHL64m1:
817 case X86_SAL8m1:
818 case X86_SAL16m1:
819 case X86_SAL32m1:
820 case X86_SAL64m1:
821 case X86_SHR8m1:
822 case X86_SHR16m1:
823 case X86_SHR32m1:
824 case X86_SHR64m1:
825 case X86_SAR8m1:
826 case X86_SAR16m1:
827 case X86_SAR32m1:
828 case X86_SAR64m1:
829 case X86_RCL8m1:
830 case X86_RCL16m1:
831 case X86_RCL32m1:
832 case X86_RCL64m1:
833 case X86_RCR8m1:
834 case X86_RCR16m1:
835 case X86_RCR32m1:
836 case X86_RCR64m1:
837 case X86_ROL8m1:
838 case X86_ROL16m1:
839 case X86_ROL32m1:
840 case X86_ROL64m1:
841 case X86_ROR8m1:
842 case X86_ROR16m1:
843 case X86_ROR32m1:
844 case X86_ROR64m1:
845 // shift all the ops right to leave 1st slot for this new register op
846 memmove(&(MI->flat_insn->detail->x86.operands[1]), &(MI->flat_insn->detail->x86.operands[0]),
847 sizeof(MI->flat_insn->detail->x86.operands[0]) * (ARR_SIZE(MI->flat_insn->detail->x86.operands) - 1));
848 MI->flat_insn->detail->x86.operands[0].type = X86_OP_IMM;
849 MI->flat_insn->detail->x86.operands[0].imm = 1;
850 MI->flat_insn->detail->x86.operands[0].size = 1;
851 MI->flat_insn->detail->x86.op_count++;
852 }
853
Nguyen Anh Quynh85cddef2014-02-18 11:59:36 +0800854 // special instruction needs to supply register op
Nguyen Anh Quynh14ba46b2014-06-24 14:32:01 +0800855 // first op can be embedded in the asm by llvm.
856 // so we have to add the missing register as the first operand
857 reg = X86_insn_reg_att(MCInst_getOpcode(MI));
Nguyen Anh Quynh85cddef2014-02-18 11:59:36 +0800858 if (reg) {
Nguyen Anh Quynh14ba46b2014-06-24 14:32:01 +0800859 // shift all the ops right to leave 1st slot for this new register op
860 memmove(&(MI->flat_insn->detail->x86.operands[1]), &(MI->flat_insn->detail->x86.operands[0]),
861 sizeof(MI->flat_insn->detail->x86.operands[0]) * (ARR_SIZE(MI->flat_insn->detail->x86.operands) - 1));
862 MI->flat_insn->detail->x86.operands[0].type = X86_OP_REG;
863 MI->flat_insn->detail->x86.operands[0].reg = reg;
864 MI->flat_insn->detail->x86.operands[0].size = MI->csh->regsize_map[reg];
865 MI->flat_insn->detail->x86.op_count++;
Nguyen Anh Quynh95781852014-12-12 11:25:12 +0800866 } else {
867 if (X86_insn_reg_att2(MCInst_getOpcode(MI), &reg, &reg2)) {
868 MI->flat_insn->detail->x86.operands[0].type = X86_OP_REG;
869 MI->flat_insn->detail->x86.operands[0].reg = reg;
870 MI->flat_insn->detail->x86.operands[0].size = MI->csh->regsize_map[reg];
871 MI->flat_insn->detail->x86.operands[1].type = X86_OP_REG;
872 MI->flat_insn->detail->x86.operands[1].reg = reg2;
873 MI->flat_insn->detail->x86.operands[1].size = MI->csh->regsize_map[reg2];
874 MI->flat_insn->detail->x86.op_count = 2;
875 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800876 }
877 }
878}
879
Nguyen Anh Quynh8598a212014-05-14 11:26:41 +0800880#endif