Path: blob/master/libs/capstone/arch/X86/X86IntelInstPrinter.c
4389 views
//===-- X86IntelInstPrinter.cpp - Intel 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 Intel-style10// assembly.11//12//===----------------------------------------------------------------------===//1314/* Capstone Disassembly Engine */15/* By Nguyen Anh Quynh <[email protected]>, 2013-2019 */1617#ifdef CAPSTONE_HAS_X861819#if defined (WIN32) || defined (WIN64) || defined (_WIN32) || defined (_WIN64)20#pragma warning(disable:4996) // disable MSVC's warning on strncpy()21#pragma warning(disable:28719) // disable MSVC's warning on strncpy()22#endif2324#if !defined(CAPSTONE_HAS_OSXKERNEL)25#include <ctype.h>26#endif27#include <capstone/platform.h>2829#if defined(CAPSTONE_HAS_OSXKERNEL)30#include <Availability.h>31#include <libkern/libkern.h>32#else33#include <stdio.h>34#include <stdlib.h>35#endif36#include <string.h>3738#include "../../utils.h"39#include "../../MCInst.h"40#include "../../SStream.h"41#include "../../MCRegisterInfo.h"4243#include "X86InstPrinter.h"44#include "X86Mapping.h"45#include "X86InstPrinterCommon.h"4647#define GET_INSTRINFO_ENUM48#ifdef CAPSTONE_X86_REDUCE49#include "X86GenInstrInfo_reduce.inc"50#else51#include "X86GenInstrInfo.inc"52#endif5354#define GET_REGINFO_ENUM55#include "X86GenRegisterInfo.inc"5657#include "X86BaseInfo.h"5859static void printMemReference(MCInst *MI, unsigned Op, SStream *O);60static void printOperand(MCInst *MI, unsigned OpNo, SStream *O);616263static void set_mem_access(MCInst *MI, bool status)64{65if (MI->csh->detail != CS_OPT_ON)66return;6768MI->csh->doing_mem = status;69if (!status)70// done, create the next operand slot71MI->flat_insn->detail->x86.op_count++;7273}7475static void printopaquemem(MCInst *MI, unsigned OpNo, SStream *O)76{77// FIXME: do this with autogen78// printf(">>> ID = %u\n", MI->flat_insn->id);79switch(MI->flat_insn->id) {80default:81SStream_concat0(O, "ptr ");82break;83case X86_INS_SGDT:84case X86_INS_SIDT:85case X86_INS_LGDT:86case X86_INS_LIDT:87case X86_INS_FXRSTOR:88case X86_INS_FXSAVE:89case X86_INS_LJMP:90case X86_INS_LCALL:91// do not print "ptr"92break;93}9495switch(MI->csh->mode) {96case CS_MODE_16:97switch(MI->flat_insn->id) {98default:99MI->x86opsize = 2;100break;101case X86_INS_LJMP:102case X86_INS_LCALL:103MI->x86opsize = 4;104break;105case X86_INS_SGDT:106case X86_INS_SIDT:107case X86_INS_LGDT:108case X86_INS_LIDT:109MI->x86opsize = 6;110break;111}112break;113case CS_MODE_32:114switch(MI->flat_insn->id) {115default:116MI->x86opsize = 4;117break;118case X86_INS_LJMP:119case X86_INS_JMP:120case X86_INS_LCALL:121case X86_INS_SGDT:122case X86_INS_SIDT:123case X86_INS_LGDT:124case X86_INS_LIDT:125MI->x86opsize = 6;126break;127}128break;129case CS_MODE_64:130switch(MI->flat_insn->id) {131default:132MI->x86opsize = 8;133break;134case X86_INS_LJMP:135case X86_INS_LCALL:136case X86_INS_SGDT:137case X86_INS_SIDT:138case X86_INS_LGDT:139case X86_INS_LIDT:140MI->x86opsize = 10;141break;142}143break;144default: // never reach145break;146}147148printMemReference(MI, OpNo, O);149}150151static void printi8mem(MCInst *MI, unsigned OpNo, SStream *O)152{153SStream_concat0(O, "byte ptr ");154MI->x86opsize = 1;155printMemReference(MI, OpNo, O);156}157158static void printi16mem(MCInst *MI, unsigned OpNo, SStream *O)159{160MI->x86opsize = 2;161SStream_concat0(O, "word ptr ");162printMemReference(MI, OpNo, O);163}164165static void printi32mem(MCInst *MI, unsigned OpNo, SStream *O)166{167MI->x86opsize = 4;168SStream_concat0(O, "dword ptr ");169printMemReference(MI, OpNo, O);170}171172static void printi64mem(MCInst *MI, unsigned OpNo, SStream *O)173{174SStream_concat0(O, "qword ptr ");175MI->x86opsize = 8;176printMemReference(MI, OpNo, O);177}178179static void printi128mem(MCInst *MI, unsigned OpNo, SStream *O)180{181SStream_concat0(O, "xmmword ptr ");182MI->x86opsize = 16;183printMemReference(MI, OpNo, O);184}185186static void printi512mem(MCInst *MI, unsigned OpNo, SStream *O)187{188SStream_concat0(O, "zmmword ptr ");189MI->x86opsize = 64;190printMemReference(MI, OpNo, O);191}192193#ifndef CAPSTONE_X86_REDUCE194static void printi256mem(MCInst *MI, unsigned OpNo, SStream *O)195{196SStream_concat0(O, "ymmword ptr ");197MI->x86opsize = 32;198printMemReference(MI, OpNo, O);199}200201static void printf32mem(MCInst *MI, unsigned OpNo, SStream *O)202{203switch(MCInst_getOpcode(MI)) {204default:205SStream_concat0(O, "dword ptr ");206MI->x86opsize = 4;207break;208case X86_FSTENVm:209case X86_FLDENVm:210// TODO: fix this in tablegen instead211switch(MI->csh->mode) {212default: // never reach213break;214case CS_MODE_16:215MI->x86opsize = 14;216break;217case CS_MODE_32:218case CS_MODE_64:219MI->x86opsize = 28;220break;221}222break;223}224225printMemReference(MI, OpNo, O);226}227228static void printf64mem(MCInst *MI, unsigned OpNo, SStream *O)229{230// TODO: fix COMISD in Tablegen instead (#1456)231if (MI->op1_size == 16) {232// printf("printf64mem id = %u\n", MCInst_getOpcode(MI));233switch(MCInst_getOpcode(MI)) {234default:235SStream_concat0(O, "qword ptr ");236MI->x86opsize = 8;237break;238case X86_MOVPQI2QImr:239case X86_COMISDrm:240SStream_concat0(O, "xmmword ptr ");241MI->x86opsize = 16;242break;243}244} else {245SStream_concat0(O, "qword ptr ");246MI->x86opsize = 8;247}248249printMemReference(MI, OpNo, O);250}251252static void printf80mem(MCInst *MI, unsigned OpNo, SStream *O)253{254switch(MCInst_getOpcode(MI)) {255default:256SStream_concat0(O, "xword ptr ");257break;258case X86_FBLDm:259case X86_FBSTPm:260break;261}262263MI->x86opsize = 10;264printMemReference(MI, OpNo, O);265}266267static void printf128mem(MCInst *MI, unsigned OpNo, SStream *O)268{269SStream_concat0(O, "xmmword ptr ");270MI->x86opsize = 16;271printMemReference(MI, OpNo, O);272}273274static void printf256mem(MCInst *MI, unsigned OpNo, SStream *O)275{276SStream_concat0(O, "ymmword ptr ");277MI->x86opsize = 32;278printMemReference(MI, OpNo, O);279}280281static void printf512mem(MCInst *MI, unsigned OpNo, SStream *O)282{283SStream_concat0(O, "zmmword ptr ");284MI->x86opsize = 64;285printMemReference(MI, OpNo, O);286}287#endif288289static const char *getRegisterName(unsigned RegNo);290static void printRegName(SStream *OS, unsigned RegNo)291{292SStream_concat0(OS, getRegisterName(RegNo));293}294295// for MASM syntax, 0x123 = 123h, 0xA123 = 0A123h296// this function tell us if we need to have prefix 0 in front of a number297static bool need_zero_prefix(uint64_t imm)298{299// find the first hex letter representing imm300while(imm >= 0x10)301imm >>= 4;302303if (imm < 0xa)304return false;305else // this need 0 prefix306return true;307}308309static void printImm(MCInst *MI, SStream *O, int64_t imm, bool positive)310{311if (positive) {312// always print this number in positive form313if (MI->csh->syntax == CS_OPT_SYNTAX_MASM) {314if (imm < 0) {315if (MI->op1_size) {316switch(MI->op1_size) {317default:318break;319case 1:320imm &= 0xff;321break;322case 2:323imm &= 0xffff;324break;325case 4:326imm &= 0xffffffff;327break;328}329}330331if (imm == 0x8000000000000000LL) // imm == -imm332SStream_concat0(O, "8000000000000000h");333else if (need_zero_prefix(imm))334SStream_concat(O, "0%"PRIx64"h", imm);335else336SStream_concat(O, "%"PRIx64"h", imm);337} else {338if (imm > HEX_THRESHOLD) {339if (need_zero_prefix(imm))340SStream_concat(O, "0%"PRIx64"h", imm);341else342SStream_concat(O, "%"PRIx64"h", imm);343} else344SStream_concat(O, "%"PRIu64, imm);345}346} else { // Intel syntax347if (imm < 0) {348if (MI->op1_size) {349switch(MI->op1_size) {350default:351break;352case 1:353imm &= 0xff;354break;355case 2:356imm &= 0xffff;357break;358case 4:359imm &= 0xffffffff;360break;361}362}363364SStream_concat(O, "0x%"PRIx64, imm);365} else {366if (imm > HEX_THRESHOLD)367SStream_concat(O, "0x%"PRIx64, imm);368else369SStream_concat(O, "%"PRIu64, imm);370}371}372} else {373if (MI->csh->syntax == CS_OPT_SYNTAX_MASM) {374if (imm < 0) {375if (imm == 0x8000000000000000LL) // imm == -imm376SStream_concat0(O, "8000000000000000h");377else if (imm < -HEX_THRESHOLD) {378if (need_zero_prefix(imm))379SStream_concat(O, "-0%"PRIx64"h", -imm);380else381SStream_concat(O, "-%"PRIx64"h", -imm);382} else383SStream_concat(O, "-%"PRIu64, -imm);384} else {385if (imm > HEX_THRESHOLD) {386if (need_zero_prefix(imm))387SStream_concat(O, "0%"PRIx64"h", imm);388else389SStream_concat(O, "%"PRIx64"h", imm);390} else391SStream_concat(O, "%"PRIu64, imm);392}393} else { // Intel syntax394if (imm < 0) {395if (imm == 0x8000000000000000LL) // imm == -imm396SStream_concat0(O, "0x8000000000000000");397else if (imm < -HEX_THRESHOLD)398SStream_concat(O, "-0x%"PRIx64, -imm);399else400SStream_concat(O, "-%"PRIu64, -imm);401402} else {403if (imm > HEX_THRESHOLD)404SStream_concat(O, "0x%"PRIx64, imm);405else406SStream_concat(O, "%"PRIu64, imm);407}408}409}410}411412// local printOperand, without updating public operands413static void _printOperand(MCInst *MI, unsigned OpNo, SStream *O)414{415MCOperand *Op = MCInst_getOperand(MI, OpNo);416if (MCOperand_isReg(Op)) {417printRegName(O, MCOperand_getReg(Op));418} else if (MCOperand_isImm(Op)) {419int64_t imm = MCOperand_getImm(Op);420printImm(MI, O, imm, MI->csh->imm_unsigned);421}422}423424#ifndef CAPSTONE_DIET425// copy & normalize access info426static void get_op_access(cs_struct *h, unsigned int id, uint8_t *access, uint64_t *eflags)427{428#ifndef CAPSTONE_DIET429uint8_t i;430const uint8_t *arr = X86_get_op_access(h, id, eflags);431432if (!arr) {433access[0] = 0;434return;435}436437// copy to access but zero out CS_AC_IGNORE438for(i = 0; arr[i]; i++) {439if (arr[i] != CS_AC_IGNORE)440access[i] = arr[i];441else442access[i] = 0;443}444445// mark the end of array446access[i] = 0;447#endif448}449#endif450451static void printSrcIdx(MCInst *MI, unsigned Op, SStream *O)452{453MCOperand *SegReg;454int reg;455456if (MI->csh->detail) {457#ifndef CAPSTONE_DIET458uint8_t access[6];459#endif460461MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].type = X86_OP_MEM;462MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].size = MI->x86opsize;463MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.segment = X86_REG_INVALID;464MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.base = X86_REG_INVALID;465MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.index = X86_REG_INVALID;466MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.scale = 1;467MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.disp = 0;468469#ifndef CAPSTONE_DIET470get_op_access(MI->csh, MCInst_getOpcode(MI), access, &MI->flat_insn->detail->x86.eflags);471MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].access = access[MI->flat_insn->detail->x86.op_count];472#endif473}474475SegReg = MCInst_getOperand(MI, Op + 1);476reg = MCOperand_getReg(SegReg);477478// If this has a segment register, print it.479if (reg) {480_printOperand(MI, Op + 1, O);481if (MI->csh->detail) {482MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.segment = X86_register_map(reg);483}484SStream_concat0(O, ":");485}486487SStream_concat0(O, "[");488set_mem_access(MI, true);489printOperand(MI, Op, O);490SStream_concat0(O, "]");491set_mem_access(MI, false);492}493494static void printDstIdx(MCInst *MI, unsigned Op, SStream *O)495{496if (MI->csh->detail) {497#ifndef CAPSTONE_DIET498uint8_t access[6];499#endif500501MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].type = X86_OP_MEM;502MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].size = MI->x86opsize;503MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.segment = X86_REG_INVALID;504MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.base = X86_REG_INVALID;505MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.index = X86_REG_INVALID;506MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.scale = 1;507MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.disp = 0;508509#ifndef CAPSTONE_DIET510get_op_access(MI->csh, MCInst_getOpcode(MI), access, &MI->flat_insn->detail->x86.eflags);511MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].access = access[MI->flat_insn->detail->x86.op_count];512#endif513}514515// DI accesses are always ES-based on non-64bit mode516if (MI->csh->mode != CS_MODE_64) {517SStream_concat0(O, "es:[");518if (MI->csh->detail) {519MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.segment = X86_REG_ES;520}521} else522SStream_concat0(O, "[");523524set_mem_access(MI, true);525printOperand(MI, Op, O);526SStream_concat0(O, "]");527set_mem_access(MI, false);528}529530static void printSrcIdx8(MCInst *MI, unsigned OpNo, SStream *O)531{532SStream_concat0(O, "byte ptr ");533MI->x86opsize = 1;534printSrcIdx(MI, OpNo, O);535}536537static void printSrcIdx16(MCInst *MI, unsigned OpNo, SStream *O)538{539SStream_concat0(O, "word ptr ");540MI->x86opsize = 2;541printSrcIdx(MI, OpNo, O);542}543544static void printSrcIdx32(MCInst *MI, unsigned OpNo, SStream *O)545{546SStream_concat0(O, "dword ptr ");547MI->x86opsize = 4;548printSrcIdx(MI, OpNo, O);549}550551static void printSrcIdx64(MCInst *MI, unsigned OpNo, SStream *O)552{553SStream_concat0(O, "qword ptr ");554MI->x86opsize = 8;555printSrcIdx(MI, OpNo, O);556}557558static void printDstIdx8(MCInst *MI, unsigned OpNo, SStream *O)559{560SStream_concat0(O, "byte ptr ");561MI->x86opsize = 1;562printDstIdx(MI, OpNo, O);563}564565static void printDstIdx16(MCInst *MI, unsigned OpNo, SStream *O)566{567SStream_concat0(O, "word ptr ");568MI->x86opsize = 2;569printDstIdx(MI, OpNo, O);570}571572static void printDstIdx32(MCInst *MI, unsigned OpNo, SStream *O)573{574SStream_concat0(O, "dword ptr ");575MI->x86opsize = 4;576printDstIdx(MI, OpNo, O);577}578579static void printDstIdx64(MCInst *MI, unsigned OpNo, SStream *O)580{581SStream_concat0(O, "qword ptr ");582MI->x86opsize = 8;583printDstIdx(MI, OpNo, O);584}585586static void printMemOffset(MCInst *MI, unsigned Op, SStream *O)587{588MCOperand *DispSpec = MCInst_getOperand(MI, Op);589MCOperand *SegReg = MCInst_getOperand(MI, Op + 1);590int reg;591592if (MI->csh->detail) {593#ifndef CAPSTONE_DIET594uint8_t access[6];595#endif596597MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].type = X86_OP_MEM;598MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].size = MI->x86opsize;599MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.segment = X86_REG_INVALID;600MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.base = X86_REG_INVALID;601MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.index = X86_REG_INVALID;602MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.scale = 1;603MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.disp = 0;604605#ifndef CAPSTONE_DIET606get_op_access(MI->csh, MCInst_getOpcode(MI), access, &MI->flat_insn->detail->x86.eflags);607MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].access = access[MI->flat_insn->detail->x86.op_count];608#endif609}610611// If this has a segment register, print it.612reg = MCOperand_getReg(SegReg);613if (reg) {614_printOperand(MI, Op + 1, O);615SStream_concat0(O, ":");616if (MI->csh->detail) {617MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.segment = X86_register_map(reg);618}619}620621SStream_concat0(O, "[");622623if (MCOperand_isImm(DispSpec)) {624int64_t imm = MCOperand_getImm(DispSpec);625if (MI->csh->detail)626MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.disp = imm;627628if (imm < 0)629printImm(MI, O, arch_masks[MI->csh->mode] & imm, true);630else631printImm(MI, O, imm, true);632}633634SStream_concat0(O, "]");635636if (MI->csh->detail)637MI->flat_insn->detail->x86.op_count++;638639if (MI->op1_size == 0)640MI->op1_size = MI->x86opsize;641}642643static void printU8Imm(MCInst *MI, unsigned Op, SStream *O)644{645uint8_t val = MCOperand_getImm(MCInst_getOperand(MI, Op)) & 0xff;646647printImm(MI, O, val, true);648649if (MI->csh->detail) {650#ifndef CAPSTONE_DIET651uint8_t access[6];652#endif653654MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].type = X86_OP_IMM;655MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].imm = val;656MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].size = 1;657658#ifndef CAPSTONE_DIET659get_op_access(MI->csh, MCInst_getOpcode(MI), access, &MI->flat_insn->detail->x86.eflags);660MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].access = access[MI->flat_insn->detail->x86.op_count];661#endif662663MI->flat_insn->detail->x86.op_count++;664}665}666667static void printMemOffs8(MCInst *MI, unsigned OpNo, SStream *O)668{669SStream_concat0(O, "byte ptr ");670MI->x86opsize = 1;671printMemOffset(MI, OpNo, O);672}673674static void printMemOffs16(MCInst *MI, unsigned OpNo, SStream *O)675{676SStream_concat0(O, "word ptr ");677MI->x86opsize = 2;678printMemOffset(MI, OpNo, O);679}680681static void printMemOffs32(MCInst *MI, unsigned OpNo, SStream *O)682{683SStream_concat0(O, "dword ptr ");684MI->x86opsize = 4;685printMemOffset(MI, OpNo, O);686}687688static void printMemOffs64(MCInst *MI, unsigned OpNo, SStream *O)689{690SStream_concat0(O, "qword ptr ");691MI->x86opsize = 8;692printMemOffset(MI, OpNo, O);693}694695static void printInstruction(MCInst *MI, SStream *O);696697void X86_Intel_printInst(MCInst *MI, SStream *O, void *Info)698{699x86_reg reg, reg2;700enum cs_ac_type access1, access2;701702// printf("opcode = %u\n", MCInst_getOpcode(MI));703704// perhaps this instruction does not need printer705if (MI->assembly[0]) {706strncpy(O->buffer, MI->assembly, sizeof(O->buffer));707return;708}709710X86_lockrep(MI, O);711printInstruction(MI, O);712713reg = X86_insn_reg_intel(MCInst_getOpcode(MI), &access1);714if (MI->csh->detail) {715#ifndef CAPSTONE_DIET716uint8_t access[6] = {0};717#endif718719// first op can be embedded in the asm by llvm.720// so we have to add the missing register as the first operand721if (reg) {722// shift all the ops right to leave 1st slot for this new register op723memmove(&(MI->flat_insn->detail->x86.operands[1]), &(MI->flat_insn->detail->x86.operands[0]),724sizeof(MI->flat_insn->detail->x86.operands[0]) * (ARR_SIZE(MI->flat_insn->detail->x86.operands) - 1));725MI->flat_insn->detail->x86.operands[0].type = X86_OP_REG;726MI->flat_insn->detail->x86.operands[0].reg = reg;727MI->flat_insn->detail->x86.operands[0].size = MI->csh->regsize_map[reg];728MI->flat_insn->detail->x86.operands[0].access = access1;729MI->flat_insn->detail->x86.op_count++;730} else {731if (X86_insn_reg_intel2(MCInst_getOpcode(MI), ®, &access1, ®2, &access2)) {732MI->flat_insn->detail->x86.operands[0].type = X86_OP_REG;733MI->flat_insn->detail->x86.operands[0].reg = reg;734MI->flat_insn->detail->x86.operands[0].size = MI->csh->regsize_map[reg];735MI->flat_insn->detail->x86.operands[0].access = access1;736MI->flat_insn->detail->x86.operands[1].type = X86_OP_REG;737MI->flat_insn->detail->x86.operands[1].reg = reg2;738MI->flat_insn->detail->x86.operands[1].size = MI->csh->regsize_map[reg2];739MI->flat_insn->detail->x86.operands[1].access = access2;740MI->flat_insn->detail->x86.op_count = 2;741}742}743744#ifndef CAPSTONE_DIET745get_op_access(MI->csh, MCInst_getOpcode(MI), access, &MI->flat_insn->detail->x86.eflags);746MI->flat_insn->detail->x86.operands[0].access = access[0];747MI->flat_insn->detail->x86.operands[1].access = access[1];748#endif749}750751if (MI->op1_size == 0 && reg)752MI->op1_size = MI->csh->regsize_map[reg];753}754755/// printPCRelImm - This is used to print an immediate value that ends up756/// being encoded as a pc-relative value.757static void printPCRelImm(MCInst *MI, unsigned OpNo, SStream *O)758{759MCOperand *Op = MCInst_getOperand(MI, OpNo);760if (MCOperand_isImm(Op)) {761int64_t imm = MCOperand_getImm(Op) + MI->flat_insn->size + MI->address;762uint8_t opsize = X86_immediate_size(MI->Opcode, NULL);763764// truncat imm for non-64bit765if (MI->csh->mode != CS_MODE_64) {766imm = imm & 0xffffffff;767}768769printImm(MI, O, imm, true);770771if (MI->csh->detail) {772#ifndef CAPSTONE_DIET773uint8_t access[6];774#endif775776MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].type = X86_OP_IMM;777// if op_count > 0, then this operand's size is taken from the destination op778if (MI->flat_insn->detail->x86.op_count > 0)779MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].size = MI->flat_insn->detail->x86.operands[0].size;780else if (opsize > 0)781MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].size = opsize;782else783MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].size = MI->imm_size;784MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].imm = imm;785786#ifndef CAPSTONE_DIET787get_op_access(MI->csh, MCInst_getOpcode(MI), access, &MI->flat_insn->detail->x86.eflags);788MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].access = access[MI->flat_insn->detail->x86.op_count];789#endif790791MI->flat_insn->detail->x86.op_count++;792}793794if (MI->op1_size == 0)795MI->op1_size = MI->imm_size;796}797}798799static void printOperand(MCInst *MI, unsigned OpNo, SStream *O)800{801MCOperand *Op = MCInst_getOperand(MI, OpNo);802803if (MCOperand_isReg(Op)) {804unsigned int reg = MCOperand_getReg(Op);805806printRegName(O, reg);807if (MI->csh->detail) {808if (MI->csh->doing_mem) {809MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.base = X86_register_map(reg);810} else {811#ifndef CAPSTONE_DIET812uint8_t access[6];813#endif814815MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].type = X86_OP_REG;816MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].reg = X86_register_map(reg);817MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].size = MI->csh->regsize_map[X86_register_map(reg)];818819#ifndef CAPSTONE_DIET820get_op_access(MI->csh, MCInst_getOpcode(MI), access, &MI->flat_insn->detail->x86.eflags);821MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].access = access[MI->flat_insn->detail->x86.op_count];822#endif823824MI->flat_insn->detail->x86.op_count++;825}826}827828if (MI->op1_size == 0)829MI->op1_size = MI->csh->regsize_map[X86_register_map(reg)];830} else if (MCOperand_isImm(Op)) {831uint8_t encsize;832int64_t imm = MCOperand_getImm(Op);833uint8_t opsize = X86_immediate_size(MCInst_getOpcode(MI), &encsize);834835if (opsize == 1) // print 1 byte immediate in positive form836imm = imm & 0xff;837838// printf(">>> id = %u\n", MI->flat_insn->id);839switch(MI->flat_insn->id) {840default:841printImm(MI, O, imm, MI->csh->imm_unsigned);842break;843844case X86_INS_MOVABS:845case X86_INS_MOV:846// do not print number in negative form847printImm(MI, O, imm, true);848break;849850case X86_INS_IN:851case X86_INS_OUT:852case X86_INS_INT:853// do not print number in negative form854imm = imm & 0xff;855printImm(MI, O, imm, true);856break;857858case X86_INS_LCALL:859case X86_INS_LJMP:860case X86_INS_JMP:861// always print address in positive form862if (OpNo == 1) { // ptr16 part863imm = imm & 0xffff;864opsize = 2;865} else866opsize = 4;867printImm(MI, O, imm, true);868break;869870case X86_INS_AND:871case X86_INS_OR:872case X86_INS_XOR:873// do not print number in negative form874if (imm >= 0 && imm <= HEX_THRESHOLD)875printImm(MI, O, imm, true);876else {877imm = arch_masks[opsize? opsize : MI->imm_size] & imm;878printImm(MI, O, imm, true);879}880break;881882case X86_INS_RET:883case X86_INS_RETF:884// RET imm16885if (imm >= 0 && imm <= HEX_THRESHOLD)886printImm(MI, O, imm, true);887else {888imm = 0xffff & imm;889printImm(MI, O, imm, true);890}891break;892}893894if (MI->csh->detail) {895if (MI->csh->doing_mem) {896MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.disp = imm;897} else {898#ifndef CAPSTONE_DIET899uint8_t access[6];900#endif901902MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].type = X86_OP_IMM;903if (opsize > 0) {904MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].size = opsize;905MI->flat_insn->detail->x86.encoding.imm_size = encsize;906} else if (MI->flat_insn->detail->x86.op_count > 0) {907if (MI->flat_insn->id != X86_INS_LCALL && MI->flat_insn->id != X86_INS_LJMP) {908MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].size =909MI->flat_insn->detail->x86.operands[0].size;910} else911MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].size = MI->imm_size;912} else913MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].size = MI->imm_size;914MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].imm = imm;915916#ifndef CAPSTONE_DIET917get_op_access(MI->csh, MCInst_getOpcode(MI), access, &MI->flat_insn->detail->x86.eflags);918MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].access = access[MI->flat_insn->detail->x86.op_count];919#endif920921MI->flat_insn->detail->x86.op_count++;922}923}924}925}926927static void printMemReference(MCInst *MI, unsigned Op, SStream *O)928{929bool NeedPlus = false;930MCOperand *BaseReg = MCInst_getOperand(MI, Op + X86_AddrBaseReg);931uint64_t ScaleVal = MCOperand_getImm(MCInst_getOperand(MI, Op + X86_AddrScaleAmt));932MCOperand *IndexReg = MCInst_getOperand(MI, Op + X86_AddrIndexReg);933MCOperand *DispSpec = MCInst_getOperand(MI, Op + X86_AddrDisp);934MCOperand *SegReg = MCInst_getOperand(MI, Op + X86_AddrSegmentReg);935int reg;936937if (MI->csh->detail) {938#ifndef CAPSTONE_DIET939uint8_t access[6];940#endif941942MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].type = X86_OP_MEM;943MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].size = MI->x86opsize;944MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.segment = X86_REG_INVALID;945MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.base = X86_register_map(MCOperand_getReg(BaseReg));946if (MCOperand_getReg(IndexReg) != X86_EIZ) {947MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.index = X86_register_map(MCOperand_getReg(IndexReg));948}949MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.scale = (int)ScaleVal;950MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.disp = 0;951952#ifndef CAPSTONE_DIET953get_op_access(MI->csh, MCInst_getOpcode(MI), access, &MI->flat_insn->detail->x86.eflags);954MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].access = access[MI->flat_insn->detail->x86.op_count];955#endif956}957958// If this has a segment register, print it.959reg = MCOperand_getReg(SegReg);960if (reg) {961_printOperand(MI, Op + X86_AddrSegmentReg, O);962if (MI->csh->detail) {963MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.segment = X86_register_map(reg);964}965SStream_concat0(O, ":");966}967968SStream_concat0(O, "[");969970if (MCOperand_getReg(BaseReg)) {971_printOperand(MI, Op + X86_AddrBaseReg, O);972NeedPlus = true;973}974975if (MCOperand_getReg(IndexReg) && MCOperand_getReg(IndexReg) != X86_EIZ) {976if (NeedPlus) SStream_concat0(O, " + ");977_printOperand(MI, Op + X86_AddrIndexReg, O);978if (ScaleVal != 1)979SStream_concat(O, "*%u", ScaleVal);980NeedPlus = true;981}982983if (MCOperand_isImm(DispSpec)) {984int64_t DispVal = MCOperand_getImm(DispSpec);985if (MI->csh->detail)986MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.disp = DispVal;987if (DispVal) {988if (NeedPlus) {989if (DispVal < 0) {990SStream_concat0(O, " - ");991printImm(MI, O, -DispVal, true);992} else {993SStream_concat0(O, " + ");994printImm(MI, O, DispVal, true);995}996} else {997// memory reference to an immediate address998if (MI->csh->mode == CS_MODE_64)999MI->op1_size = 8;1000if (DispVal < 0) {1001printImm(MI, O, arch_masks[MI->csh->mode] & DispVal, true);1002} else {1003printImm(MI, O, DispVal, true);1004}1005}10061007} else {1008// DispVal = 01009if (!NeedPlus) // [0]1010SStream_concat0(O, "0");1011}1012}10131014SStream_concat0(O, "]");10151016if (MI->csh->detail)1017MI->flat_insn->detail->x86.op_count++;10181019if (MI->op1_size == 0)1020MI->op1_size = MI->x86opsize;1021}10221023static void printanymem(MCInst *MI, unsigned OpNo, SStream *O)1024{1025switch(MI->Opcode) {1026default: break;1027case X86_LEA16r:1028MI->x86opsize = 2;1029break;1030case X86_LEA32r:1031case X86_LEA64_32r:1032MI->x86opsize = 4;1033break;1034case X86_LEA64r:1035MI->x86opsize = 8;1036break;1037case X86_BNDCL32rm:1038case X86_BNDCN32rm:1039case X86_BNDCU32rm:1040case X86_BNDSTXmr:1041case X86_BNDLDXrm:1042case X86_BNDCL64rm:1043case X86_BNDCN64rm:1044case X86_BNDCU64rm:1045MI->x86opsize = 16;1046break;1047}10481049printMemReference(MI, OpNo, O);1050}10511052#ifdef CAPSTONE_X86_REDUCE1053#include "X86GenAsmWriter1_reduce.inc"1054#else1055#include "X86GenAsmWriter1.inc"1056#endif10571058#include "X86GenRegisterName1.inc"10591060#endif106110621063