Path: blob/master/libs/capstone/arch/X86/X86ATTInstPrinter.c
4389 views
//===-- X86ATTInstPrinter.cpp - AT&T assembly instruction printing --------===//1//2// The LLVM Compiler Infrastructure3//4// This file is distributed under the University of Illinois Open Source5// License. See LICENSE.TXT for details.6//7//===----------------------------------------------------------------------===//8//9// This file includes code for rendering MCInst instances as AT&T-style10// assembly.11//12//===----------------------------------------------------------------------===//1314/* Capstone Disassembly Engine */15/* By Nguyen Anh Quynh <[email protected]>, 2013-2019 */1617// this code is only relevant when DIET mode is disable18#if defined(CAPSTONE_HAS_X86) && !defined(CAPSTONE_DIET) && !defined(CAPSTONE_X86_ATT_DISABLE)1920#if defined (WIN32) || defined (WIN64) || defined (_WIN32) || defined (_WIN64)21#pragma warning(disable:4996) // disable MSVC's warning on strncpy()22#pragma warning(disable:28719) // disable MSVC's warning on strncpy()23#endif2425#if !defined(CAPSTONE_HAS_OSXKERNEL)26#include <ctype.h>27#endif28#include <capstone/platform.h>2930#if defined(CAPSTONE_HAS_OSXKERNEL)31#include <Availability.h>32#include <libkern/libkern.h>33#else34#include <stdio.h>35#include <stdlib.h>36#endif3738#include <string.h>3940#include "../../utils.h"41#include "../../MCInst.h"42#include "../../SStream.h"43#include "../../MCRegisterInfo.h"44#include "X86Mapping.h"45#include "X86BaseInfo.h"46#include "X86InstPrinterCommon.h"4748#define GET_INSTRINFO_ENUM49#ifdef CAPSTONE_X86_REDUCE50#include "X86GenInstrInfo_reduce.inc"51#else52#include "X86GenInstrInfo.inc"53#endif5455#define GET_REGINFO_ENUM56#include "X86GenRegisterInfo.inc"5758static void printMemReference(MCInst *MI, unsigned Op, SStream *O);59static void printOperand(MCInst *MI, unsigned OpNo, SStream *O);606162static void set_mem_access(MCInst *MI, bool status)63{64if (MI->csh->detail != CS_OPT_ON)65return;6667MI->csh->doing_mem = status;68if (!status)69// done, create the next operand slot70MI->flat_insn->detail->x86.op_count++;71}7273static void printopaquemem(MCInst *MI, unsigned OpNo, SStream *O)74{75switch(MI->csh->mode) {76case CS_MODE_16:77switch(MI->flat_insn->id) {78default:79MI->x86opsize = 2;80break;81case X86_INS_LJMP:82case X86_INS_LCALL:83MI->x86opsize = 4;84break;85case X86_INS_SGDT:86case X86_INS_SIDT:87case X86_INS_LGDT:88case X86_INS_LIDT:89MI->x86opsize = 6;90break;91}92break;93case CS_MODE_32:94switch(MI->flat_insn->id) {95default:96MI->x86opsize = 4;97break;98case X86_INS_LJMP:99case X86_INS_JMP:100case X86_INS_LCALL:101case X86_INS_SGDT:102case X86_INS_SIDT:103case X86_INS_LGDT:104case X86_INS_LIDT:105MI->x86opsize = 6;106break;107}108break;109case CS_MODE_64:110switch(MI->flat_insn->id) {111default:112MI->x86opsize = 8;113break;114case X86_INS_LJMP:115case X86_INS_LCALL:116case X86_INS_SGDT:117case X86_INS_SIDT:118case X86_INS_LGDT:119case X86_INS_LIDT:120MI->x86opsize = 10;121break;122}123break;124default: // never reach125break;126}127128printMemReference(MI, OpNo, O);129}130131static void printi8mem(MCInst *MI, unsigned OpNo, SStream *O)132{133MI->x86opsize = 1;134printMemReference(MI, OpNo, O);135}136137static void printi16mem(MCInst *MI, unsigned OpNo, SStream *O)138{139MI->x86opsize = 2;140141printMemReference(MI, OpNo, O);142}143144static void printi32mem(MCInst *MI, unsigned OpNo, SStream *O)145{146MI->x86opsize = 4;147148printMemReference(MI, OpNo, O);149}150151static void printi64mem(MCInst *MI, unsigned OpNo, SStream *O)152{153MI->x86opsize = 8;154printMemReference(MI, OpNo, O);155}156157static void printi128mem(MCInst *MI, unsigned OpNo, SStream *O)158{159MI->x86opsize = 16;160printMemReference(MI, OpNo, O);161}162163static void printi512mem(MCInst *MI, unsigned OpNo, SStream *O)164{165MI->x86opsize = 64;166printMemReference(MI, OpNo, O);167}168169#ifndef CAPSTONE_X86_REDUCE170static void printi256mem(MCInst *MI, unsigned OpNo, SStream *O)171{172MI->x86opsize = 32;173printMemReference(MI, OpNo, O);174}175176static void printf32mem(MCInst *MI, unsigned OpNo, SStream *O)177{178switch(MCInst_getOpcode(MI)) {179default:180MI->x86opsize = 4;181break;182case X86_FSTENVm:183case X86_FLDENVm:184// TODO: fix this in tablegen instead185switch(MI->csh->mode) {186default: // never reach187break;188case CS_MODE_16:189MI->x86opsize = 14;190break;191case CS_MODE_32:192case CS_MODE_64:193MI->x86opsize = 28;194break;195}196break;197}198199printMemReference(MI, OpNo, O);200}201202static void printf64mem(MCInst *MI, unsigned OpNo, SStream *O)203{204MI->x86opsize = 8;205printMemReference(MI, OpNo, O);206}207208static void printf80mem(MCInst *MI, unsigned OpNo, SStream *O)209{210MI->x86opsize = 10;211printMemReference(MI, OpNo, O);212}213214static void printf128mem(MCInst *MI, unsigned OpNo, SStream *O)215{216MI->x86opsize = 16;217printMemReference(MI, OpNo, O);218}219220static void printf256mem(MCInst *MI, unsigned OpNo, SStream *O)221{222MI->x86opsize = 32;223printMemReference(MI, OpNo, O);224}225226static void printf512mem(MCInst *MI, unsigned OpNo, SStream *O)227{228MI->x86opsize = 64;229printMemReference(MI, OpNo, O);230}231232#endif233234static void printRegName(SStream *OS, unsigned RegNo);235236// local printOperand, without updating public operands237static void _printOperand(MCInst *MI, unsigned OpNo, SStream *O)238{239MCOperand *Op = MCInst_getOperand(MI, OpNo);240if (MCOperand_isReg(Op)) {241printRegName(O, MCOperand_getReg(Op));242} else if (MCOperand_isImm(Op)) {243uint8_t encsize;244uint8_t opsize = X86_immediate_size(MCInst_getOpcode(MI), &encsize);245246// Print X86 immediates as signed values.247int64_t imm = MCOperand_getImm(Op);248if (imm < 0) {249if (MI->csh->imm_unsigned) {250if (opsize) {251switch(opsize) {252default:253break;254case 1:255imm &= 0xff;256break;257case 2:258imm &= 0xffff;259break;260case 4:261imm &= 0xffffffff;262break;263}264}265266SStream_concat(O, "$0x%"PRIx64, imm);267} else {268if (imm < -HEX_THRESHOLD)269SStream_concat(O, "$-0x%"PRIx64, -imm);270else271SStream_concat(O, "$-%"PRIu64, -imm);272}273} else {274if (imm > HEX_THRESHOLD)275SStream_concat(O, "$0x%"PRIx64, imm);276else277SStream_concat(O, "$%"PRIu64, imm);278}279}280}281282// convert Intel access info to AT&T access info283static void get_op_access(cs_struct *h, unsigned int id, uint8_t *access, uint64_t *eflags)284{285uint8_t count, i;286const uint8_t *arr = X86_get_op_access(h, id, eflags);287288if (!arr) {289access[0] = 0;290return;291}292293// find the non-zero last entry294for(count = 0; arr[count]; count++);295296if (count == 0)297return;298299// copy in reverse order this access array from Intel syntax -> AT&T syntax300count--;301for(i = 0; i <= count; i++) {302if (arr[count - i] != CS_AC_IGNORE)303access[i] = arr[count - i];304else305access[i] = 0;306}307}308309static void printSrcIdx(MCInst *MI, unsigned Op, SStream *O)310{311MCOperand *SegReg;312int reg;313314if (MI->csh->detail) {315uint8_t access[6];316317MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].type = X86_OP_MEM;318MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].size = MI->x86opsize;319MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.segment = X86_REG_INVALID;320MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.base = X86_REG_INVALID;321MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.index = X86_REG_INVALID;322MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.scale = 1;323MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.disp = 0;324325get_op_access(MI->csh, MCInst_getOpcode(MI), access, &MI->flat_insn->detail->x86.eflags);326MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].access = access[MI->flat_insn->detail->x86.op_count];327}328329SegReg = MCInst_getOperand(MI, Op+1);330reg = MCOperand_getReg(SegReg);331// If this has a segment register, print it.332if (reg) {333_printOperand(MI, Op + 1, O);334SStream_concat0(O, ":");335336if (MI->csh->detail) {337MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.segment = X86_register_map(reg);338}339}340341SStream_concat0(O, "(");342set_mem_access(MI, true);343344printOperand(MI, Op, O);345346SStream_concat0(O, ")");347set_mem_access(MI, false);348}349350static void printDstIdx(MCInst *MI, unsigned Op, SStream *O)351{352if (MI->csh->detail) {353uint8_t access[6];354355MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].type = X86_OP_MEM;356MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].size = MI->x86opsize;357MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.segment = X86_REG_INVALID;358MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.base = X86_REG_INVALID;359MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.index = X86_REG_INVALID;360MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.scale = 1;361MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.disp = 0;362363get_op_access(MI->csh, MCInst_getOpcode(MI), access, &MI->flat_insn->detail->x86.eflags);364MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].access = access[MI->flat_insn->detail->x86.op_count];365}366367// DI accesses are always ES-based on non-64bit mode368if (MI->csh->mode != CS_MODE_64) {369SStream_concat0(O, "%es:(");370if (MI->csh->detail) {371MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.segment = X86_REG_ES;372}373} else374SStream_concat0(O, "(");375376set_mem_access(MI, true);377378printOperand(MI, Op, O);379380SStream_concat0(O, ")");381set_mem_access(MI, false);382}383384static void printSrcIdx8(MCInst *MI, unsigned OpNo, SStream *O)385{386MI->x86opsize = 1;387printSrcIdx(MI, OpNo, O);388}389390static void printSrcIdx16(MCInst *MI, unsigned OpNo, SStream *O)391{392MI->x86opsize = 2;393printSrcIdx(MI, OpNo, O);394}395396static void printSrcIdx32(MCInst *MI, unsigned OpNo, SStream *O)397{398MI->x86opsize = 4;399printSrcIdx(MI, OpNo, O);400}401402static void printSrcIdx64(MCInst *MI, unsigned OpNo, SStream *O)403{404MI->x86opsize = 8;405printSrcIdx(MI, OpNo, O);406}407408static void printDstIdx8(MCInst *MI, unsigned OpNo, SStream *O)409{410MI->x86opsize = 1;411printDstIdx(MI, OpNo, O);412}413414static void printDstIdx16(MCInst *MI, unsigned OpNo, SStream *O)415{416MI->x86opsize = 2;417printDstIdx(MI, OpNo, O);418}419420static void printDstIdx32(MCInst *MI, unsigned OpNo, SStream *O)421{422MI->x86opsize = 4;423printDstIdx(MI, OpNo, O);424}425426static void printDstIdx64(MCInst *MI, unsigned OpNo, SStream *O)427{428MI->x86opsize = 8;429printDstIdx(MI, OpNo, O);430}431432static void printMemOffset(MCInst *MI, unsigned Op, SStream *O)433{434MCOperand *DispSpec = MCInst_getOperand(MI, Op);435MCOperand *SegReg = MCInst_getOperand(MI, Op+1);436int reg;437438if (MI->csh->detail) {439uint8_t access[6];440441MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].type = X86_OP_MEM;442MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].size = MI->x86opsize;443MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.segment = X86_REG_INVALID;444MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.base = X86_REG_INVALID;445MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.index = X86_REG_INVALID;446MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.scale = 1;447MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.disp = 0;448449get_op_access(MI->csh, MCInst_getOpcode(MI), access, &MI->flat_insn->detail->x86.eflags);450MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].access = access[MI->flat_insn->detail->x86.op_count];451}452453// If this has a segment register, print it.454reg = MCOperand_getReg(SegReg);455if (reg) {456_printOperand(MI, Op + 1, O);457SStream_concat0(O, ":");458459if (MI->csh->detail) {460MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.segment = X86_register_map(reg);461}462}463464if (MCOperand_isImm(DispSpec)) {465int64_t imm = MCOperand_getImm(DispSpec);466if (MI->csh->detail)467MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.disp = imm;468if (imm < 0) {469SStream_concat(O, "0x%"PRIx64, arch_masks[MI->csh->mode] & imm);470} else {471if (imm > HEX_THRESHOLD)472SStream_concat(O, "0x%"PRIx64, imm);473else474SStream_concat(O, "%"PRIu64, imm);475}476}477478if (MI->csh->detail)479MI->flat_insn->detail->x86.op_count++;480}481482static void printU8Imm(MCInst *MI, unsigned Op, SStream *O)483{484uint8_t val = MCOperand_getImm(MCInst_getOperand(MI, Op)) & 0xff;485486if (val > HEX_THRESHOLD)487SStream_concat(O, "$0x%x", val);488else489SStream_concat(O, "$%u", val);490491if (MI->csh->detail) {492MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].type = X86_OP_IMM;493MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].imm = val;494MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].size = 1;495MI->flat_insn->detail->x86.op_count++;496}497}498499static void printMemOffs8(MCInst *MI, unsigned OpNo, SStream *O)500{501MI->x86opsize = 1;502printMemOffset(MI, OpNo, O);503}504505static void printMemOffs16(MCInst *MI, unsigned OpNo, SStream *O)506{507MI->x86opsize = 2;508printMemOffset(MI, OpNo, O);509}510511static void printMemOffs32(MCInst *MI, unsigned OpNo, SStream *O)512{513MI->x86opsize = 4;514printMemOffset(MI, OpNo, O);515}516517static void printMemOffs64(MCInst *MI, unsigned OpNo, SStream *O)518{519MI->x86opsize = 8;520printMemOffset(MI, OpNo, O);521}522523/// printPCRelImm - This is used to print an immediate value that ends up524/// being encoded as a pc-relative value (e.g. for jumps and calls). These525/// print slightly differently than normal immediates. For example, a $ is not526/// emitted.527static void printPCRelImm(MCInst *MI, unsigned OpNo, SStream *O)528{529MCOperand *Op = MCInst_getOperand(MI, OpNo);530if (MCOperand_isImm(Op)) {531int64_t imm = MCOperand_getImm(Op) + MI->flat_insn->size + MI->address;532533// truncat imm for non-64bit534if (MI->csh->mode != CS_MODE_64) {535imm = imm & 0xffffffff;536}537538if (imm < 0) {539SStream_concat(O, "0x%"PRIx64, imm);540} else {541if (imm > HEX_THRESHOLD)542SStream_concat(O, "0x%"PRIx64, imm);543else544SStream_concat(O, "%"PRIu64, imm);545}546if (MI->csh->detail) {547MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].type = X86_OP_IMM;548MI->has_imm = true;549MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].imm = imm;550MI->flat_insn->detail->x86.op_count++;551}552}553}554555static void printOperand(MCInst *MI, unsigned OpNo, SStream *O)556{557MCOperand *Op = MCInst_getOperand(MI, OpNo);558if (MCOperand_isReg(Op)) {559unsigned int reg = MCOperand_getReg(Op);560printRegName(O, reg);561if (MI->csh->detail) {562if (MI->csh->doing_mem) {563MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.base = X86_register_map(reg);564} else {565uint8_t access[6];566567MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].type = X86_OP_REG;568MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].reg = X86_register_map(reg);569MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].size = MI->csh->regsize_map[X86_register_map(reg)];570571get_op_access(MI->csh, MCInst_getOpcode(MI), access, &MI->flat_insn->detail->x86.eflags);572MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].access = access[MI->flat_insn->detail->x86.op_count];573574MI->flat_insn->detail->x86.op_count++;575}576}577} else if (MCOperand_isImm(Op)) {578// Print X86 immediates as signed values.579uint8_t encsize;580int64_t imm = MCOperand_getImm(Op);581uint8_t opsize = X86_immediate_size(MCInst_getOpcode(MI), &encsize);582583if (opsize == 1) // print 1 byte immediate in positive form584imm = imm & 0xff;585586switch(MI->flat_insn->id) {587default:588if (imm >= 0) {589if (imm > HEX_THRESHOLD)590SStream_concat(O, "$0x%"PRIx64, imm);591else592SStream_concat(O, "$%"PRIu64, imm);593} else {594if (MI->csh->imm_unsigned) {595if (opsize) {596switch(opsize) {597default:598break;599case 1:600imm &= 0xff;601break;602case 2:603imm &= 0xffff;604break;605case 4:606imm &= 0xffffffff;607break;608}609}610611SStream_concat(O, "$0x%"PRIx64, imm);612} else {613if (imm == 0x8000000000000000LL) // imm == -imm614SStream_concat0(O, "$0x8000000000000000");615else if (imm < -HEX_THRESHOLD)616SStream_concat(O, "$-0x%"PRIx64, -imm);617else618SStream_concat(O, "$-%"PRIu64, -imm);619}620}621break;622623case X86_INS_MOVABS:624case X86_INS_MOV:625// do not print number in negative form626if (imm > HEX_THRESHOLD)627SStream_concat(O, "$0x%"PRIx64, imm);628else629SStream_concat(O, "$%"PRIu64, imm);630break;631632case X86_INS_IN:633case X86_INS_OUT:634case X86_INS_INT:635// do not print number in negative form636imm = imm & 0xff;637if (imm >= 0 && imm <= HEX_THRESHOLD)638SStream_concat(O, "$%u", imm);639else {640SStream_concat(O, "$0x%x", imm);641}642break;643644case X86_INS_LCALL:645case X86_INS_LJMP:646case X86_INS_JMP:647// always print address in positive form648if (OpNo == 1) { // selector is ptr16649imm = imm & 0xffff;650opsize = 2;651} else652opsize = 4;653SStream_concat(O, "$0x%"PRIx64, imm);654break;655656case X86_INS_AND:657case X86_INS_OR:658case X86_INS_XOR:659// do not print number in negative form660if (imm >= 0 && imm <= HEX_THRESHOLD)661SStream_concat(O, "$%u", imm);662else {663imm = arch_masks[opsize? opsize : MI->imm_size] & imm;664SStream_concat(O, "$0x%"PRIx64, imm);665}666break;667668case X86_INS_RET:669case X86_INS_RETF:670// RET imm16671if (imm >= 0 && imm <= HEX_THRESHOLD)672SStream_concat(O, "$%u", imm);673else {674imm = 0xffff & imm;675SStream_concat(O, "$0x%x", imm);676}677break;678}679680if (MI->csh->detail) {681if (MI->csh->doing_mem) {682MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].type = X86_OP_MEM;683MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.disp = imm;684} else {685MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].type = X86_OP_IMM;686MI->has_imm = true;687MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].imm = imm;688689if (opsize > 0) {690MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].size = opsize;691MI->flat_insn->detail->x86.encoding.imm_size = encsize;692} else if (MI->op1_size > 0)693MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].size = MI->op1_size;694else695MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].size = MI->imm_size;696697MI->flat_insn->detail->x86.op_count++;698}699}700}701}702703static void printMemReference(MCInst *MI, unsigned Op, SStream *O)704{705MCOperand *BaseReg = MCInst_getOperand(MI, Op + X86_AddrBaseReg);706MCOperand *IndexReg = MCInst_getOperand(MI, Op + X86_AddrIndexReg);707MCOperand *DispSpec = MCInst_getOperand(MI, Op + X86_AddrDisp);708MCOperand *SegReg = MCInst_getOperand(MI, Op + X86_AddrSegmentReg);709uint64_t ScaleVal;710int segreg;711int64_t DispVal = 1;712713if (MI->csh->detail) {714uint8_t access[6];715716MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].type = X86_OP_MEM;717MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].size = MI->x86opsize;718MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.segment = X86_REG_INVALID;719MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.base = X86_register_map(MCOperand_getReg(BaseReg));720if (MCOperand_getReg(IndexReg) != X86_EIZ) {721MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.index = X86_register_map(MCOperand_getReg(IndexReg));722}723MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.scale = 1;724MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.disp = 0;725726get_op_access(MI->csh, MCInst_getOpcode(MI), access, &MI->flat_insn->detail->x86.eflags);727MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].access = access[MI->flat_insn->detail->x86.op_count];728}729730// If this has a segment register, print it.731segreg = MCOperand_getReg(SegReg);732if (segreg) {733_printOperand(MI, Op + X86_AddrSegmentReg, O);734SStream_concat0(O, ":");735736if (MI->csh->detail) {737MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.segment = X86_register_map(segreg);738}739}740741if (MCOperand_isImm(DispSpec)) {742DispVal = MCOperand_getImm(DispSpec);743if (MI->csh->detail)744MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.disp = DispVal;745if (DispVal) {746if (MCOperand_getReg(IndexReg) || MCOperand_getReg(BaseReg)) {747printInt64(O, DispVal);748} else {749// only immediate as address of memory750if (DispVal < 0) {751SStream_concat(O, "0x%"PRIx64, arch_masks[MI->csh->mode] & DispVal);752} else {753if (DispVal > HEX_THRESHOLD)754SStream_concat(O, "0x%"PRIx64, DispVal);755else756SStream_concat(O, "%"PRIu64, DispVal);757}758}759}760}761762if (MCOperand_getReg(IndexReg) || MCOperand_getReg(BaseReg)) {763SStream_concat0(O, "(");764765if (MCOperand_getReg(BaseReg))766_printOperand(MI, Op + X86_AddrBaseReg, O);767768if (MCOperand_getReg(IndexReg) && MCOperand_getReg(IndexReg) != X86_EIZ) {769SStream_concat0(O, ", ");770_printOperand(MI, Op + X86_AddrIndexReg, O);771ScaleVal = MCOperand_getImm(MCInst_getOperand(MI, Op + X86_AddrScaleAmt));772if (MI->csh->detail)773MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.scale = (int)ScaleVal;774if (ScaleVal != 1) {775SStream_concat(O, ", %u", ScaleVal);776}777}778779SStream_concat0(O, ")");780} else {781if (!DispVal)782SStream_concat0(O, "0");783}784785if (MI->csh->detail)786MI->flat_insn->detail->x86.op_count++;787}788789static void printanymem(MCInst *MI, unsigned OpNo, SStream *O)790{791switch(MI->Opcode) {792default: break;793case X86_LEA16r:794MI->x86opsize = 2;795break;796case X86_LEA32r:797case X86_LEA64_32r:798MI->x86opsize = 4;799break;800case X86_LEA64r:801MI->x86opsize = 8;802break;803case X86_BNDCL32rm:804case X86_BNDCN32rm:805case X86_BNDCU32rm:806case X86_BNDSTXmr:807case X86_BNDLDXrm:808case X86_BNDCL64rm:809case X86_BNDCN64rm:810case X86_BNDCU64rm:811MI->x86opsize = 16;812break;813}814815printMemReference(MI, OpNo, O);816}817818#include "X86InstPrinter.h"819820// Include the auto-generated portion of the assembly writer.821#ifdef CAPSTONE_X86_REDUCE822#include "X86GenAsmWriter_reduce.inc"823#else824#include "X86GenAsmWriter.inc"825#endif826827#include "X86GenRegisterName.inc"828829static void printRegName(SStream *OS, unsigned RegNo)830{831SStream_concat(OS, "%%%s", getRegisterName(RegNo));832}833834void X86_ATT_printInst(MCInst *MI, SStream *OS, void *info)835{836x86_reg reg, reg2;837enum cs_ac_type access1, access2;838int i;839840// perhaps this instruction does not need printer841if (MI->assembly[0]) {842strncpy(OS->buffer, MI->assembly, sizeof(OS->buffer));843return;844}845846// Output CALLpcrel32 as "callq" in 64-bit mode.847// In Intel annotation it's always emitted as "call".848//849// TODO: Probably this hack should be redesigned via InstAlias in850// InstrInfo.td as soon as Requires clause is supported properly851// for InstAlias.852if (MI->csh->mode == CS_MODE_64 && MCInst_getOpcode(MI) == X86_CALLpcrel32) {853SStream_concat0(OS, "callq\t");854MCInst_setOpcodePub(MI, X86_INS_CALL);855printPCRelImm(MI, 0, OS);856return;857}858859X86_lockrep(MI, OS);860printInstruction(MI, OS);861862if (MI->has_imm) {863// if op_count > 1, then this operand's size is taken from the destination op864if (MI->flat_insn->detail->x86.op_count > 1) {865if (MI->flat_insn->id != X86_INS_LCALL && MI->flat_insn->id != X86_INS_LJMP && MI->flat_insn->id != X86_INS_JMP) {866for (i = 0; i < MI->flat_insn->detail->x86.op_count; i++) {867if (MI->flat_insn->detail->x86.operands[i].type == X86_OP_IMM)868MI->flat_insn->detail->x86.operands[i].size =869MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count - 1].size;870}871}872} else873MI->flat_insn->detail->x86.operands[0].size = MI->imm_size;874}875876if (MI->csh->detail) {877uint8_t access[6] = {0};878879// some instructions need to supply immediate 1 in the first op880switch(MCInst_getOpcode(MI)) {881default:882break;883case X86_SHL8r1:884case X86_SHL16r1:885case X86_SHL32r1:886case X86_SHL64r1:887case X86_SAL8r1:888case X86_SAL16r1:889case X86_SAL32r1:890case X86_SAL64r1:891case X86_SHR8r1:892case X86_SHR16r1:893case X86_SHR32r1:894case X86_SHR64r1:895case X86_SAR8r1:896case X86_SAR16r1:897case X86_SAR32r1:898case X86_SAR64r1:899case X86_RCL8r1:900case X86_RCL16r1:901case X86_RCL32r1:902case X86_RCL64r1:903case X86_RCR8r1:904case X86_RCR16r1:905case X86_RCR32r1:906case X86_RCR64r1:907case X86_ROL8r1:908case X86_ROL16r1:909case X86_ROL32r1:910case X86_ROL64r1:911case X86_ROR8r1:912case X86_ROR16r1:913case X86_ROR32r1:914case X86_ROR64r1:915case X86_SHL8m1:916case X86_SHL16m1:917case X86_SHL32m1:918case X86_SHL64m1:919case X86_SAL8m1:920case X86_SAL16m1:921case X86_SAL32m1:922case X86_SAL64m1:923case X86_SHR8m1:924case X86_SHR16m1:925case X86_SHR32m1:926case X86_SHR64m1:927case X86_SAR8m1:928case X86_SAR16m1:929case X86_SAR32m1:930case X86_SAR64m1:931case X86_RCL8m1:932case X86_RCL16m1:933case X86_RCL32m1:934case X86_RCL64m1:935case X86_RCR8m1:936case X86_RCR16m1:937case X86_RCR32m1:938case X86_RCR64m1:939case X86_ROL8m1:940case X86_ROL16m1:941case X86_ROL32m1:942case X86_ROL64m1:943case X86_ROR8m1:944case X86_ROR16m1:945case X86_ROR32m1:946case X86_ROR64m1:947// shift all the ops right to leave 1st slot for this new register op948memmove(&(MI->flat_insn->detail->x86.operands[1]), &(MI->flat_insn->detail->x86.operands[0]),949sizeof(MI->flat_insn->detail->x86.operands[0]) * (ARR_SIZE(MI->flat_insn->detail->x86.operands) - 1));950MI->flat_insn->detail->x86.operands[0].type = X86_OP_IMM;951MI->flat_insn->detail->x86.operands[0].imm = 1;952MI->flat_insn->detail->x86.operands[0].size = 1;953MI->flat_insn->detail->x86.op_count++;954}955956// special instruction needs to supply register op957// first op can be embedded in the asm by llvm.958// so we have to add the missing register as the first operand959960//printf(">>> opcode = %u\n", MCInst_getOpcode(MI));961962reg = X86_insn_reg_att(MCInst_getOpcode(MI), &access1);963if (reg) {964// shift all the ops right to leave 1st slot for this new register op965memmove(&(MI->flat_insn->detail->x86.operands[1]), &(MI->flat_insn->detail->x86.operands[0]),966sizeof(MI->flat_insn->detail->x86.operands[0]) * (ARR_SIZE(MI->flat_insn->detail->x86.operands) - 1));967MI->flat_insn->detail->x86.operands[0].type = X86_OP_REG;968MI->flat_insn->detail->x86.operands[0].reg = reg;969MI->flat_insn->detail->x86.operands[0].size = MI->csh->regsize_map[reg];970MI->flat_insn->detail->x86.operands[0].access = access1;971972MI->flat_insn->detail->x86.op_count++;973} else {974if (X86_insn_reg_att2(MCInst_getOpcode(MI), ®, &access1, ®2, &access2)) {975976MI->flat_insn->detail->x86.operands[0].type = X86_OP_REG;977MI->flat_insn->detail->x86.operands[0].reg = reg;978MI->flat_insn->detail->x86.operands[0].size = MI->csh->regsize_map[reg];979MI->flat_insn->detail->x86.operands[0].access = access1;980MI->flat_insn->detail->x86.operands[1].type = X86_OP_REG;981MI->flat_insn->detail->x86.operands[1].reg = reg2;982MI->flat_insn->detail->x86.operands[1].size = MI->csh->regsize_map[reg2];983MI->flat_insn->detail->x86.operands[0].access = access2;984MI->flat_insn->detail->x86.op_count = 2;985}986}987988#ifndef CAPSTONE_DIET989get_op_access(MI->csh, MCInst_getOpcode(MI), access, &MI->flat_insn->detail->x86.eflags);990MI->flat_insn->detail->x86.operands[0].access = access[0];991MI->flat_insn->detail->x86.operands[1].access = access[1];992#endif993}994}995996#endif997998999