1 //===-- llvm/MC/MCInst.h - MCInst class -------------------------*- C++ -*-===//
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 contains the declaration of the MCInst and MCOperand classes, which
11 // is the basic representation used to represent low-level machine code
12 // instructions.
13 //
14 //===----------------------------------------------------------------------===//
15 
16 /* Capstone Disassembly Engine */
17 /* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2014 */
18 
19 #ifndef CS_MCINST_H
20 #define CS_MCINST_H
21 
22 #if !defined(_MSC_VER) || !defined(_KERNEL_MODE)
23 #include <stdint.h>
24 #endif
25 #include "include/capstone.h"
26 
27 typedef struct MCInst MCInst;
28 typedef struct cs_struct cs_struct;
29 typedef struct MCOperand MCOperand;
30 
31 /// MCOperand - Instances of this class represent operands of the MCInst class.
32 /// This is a simple discriminated union.
33 struct MCOperand {
34 	enum {
35 		kInvalid = 0,                 ///< Uninitialized.
36 		kRegister,                ///< Register operand.
37 		kImmediate,               ///< Immediate operand.
38 		kFPImmediate,             ///< Floating-point immediate operand.
39 	} MachineOperandType;
40 	unsigned char Kind;
41 
42 	union {
43 		unsigned RegVal;
44 		int64_t ImmVal;
45 		double FPImmVal;
46 	};
47 };
48 
49 bool MCOperand_isValid(const MCOperand *op);
50 
51 bool MCOperand_isReg(const MCOperand *op);
52 
53 bool MCOperand_isImm(const MCOperand *op);
54 
55 bool MCOperand_isFPImm(const MCOperand *op);
56 
57 bool MCOperand_isInst(const MCOperand *op);
58 
59 /// getReg - Returns the register number.
60 unsigned MCOperand_getReg(const MCOperand *op);
61 
62 /// setReg - Set the register number.
63 void MCOperand_setReg(MCOperand *op, unsigned Reg);
64 
65 int64_t MCOperand_getImm(MCOperand *op);
66 
67 void MCOperand_setImm(MCOperand *op, int64_t Val);
68 
69 double MCOperand_getFPImm(const MCOperand *op);
70 
71 void MCOperand_setFPImm(MCOperand *op, double Val);
72 
73 const MCInst *MCOperand_getInst(const MCOperand *op);
74 
75 void MCOperand_setInst(MCOperand *op, const MCInst *Val);
76 
77 // create Reg operand in the next slot
78 void MCOperand_CreateReg0(MCInst *inst, unsigned Reg);
79 
80 // create Reg operand use the last-unused slot
81 MCOperand *MCOperand_CreateReg1(MCInst *inst, unsigned Reg);
82 
83 // create Imm operand in the next slot
84 void MCOperand_CreateImm0(MCInst *inst, int64_t Val);
85 
86 // create Imm operand in the last-unused slot
87 MCOperand *MCOperand_CreateImm1(MCInst *inst, int64_t Val);
88 
89 /// MCInst - Instances of this class represent a single low-level machine
90 /// instruction.
91 struct MCInst {
92 	unsigned OpcodePub;
93 	uint8_t size;	// number of operands
94 	bool has_imm;	// indicate this instruction has an X86_OP_IMM operand - used for ATT syntax
95 	uint8_t op1_size; // size of 1st operand - for X86 Intel syntax
96 	unsigned Opcode;
97 	MCOperand Operands[48];
98 	cs_insn *flat_insn;	// insn to be exposed to public
99 	uint64_t address;	// address of this insn
100 	cs_struct *csh;	// save the main csh
101 	uint8_t x86opsize;	// opsize for [mem] operand
102 
103 	// (Optional) instruction prefix, which can be up to 4 bytes.
104 	// A prefix byte gets value 0 when irrelevant.
105 	// This is copied from cs_x86 struct
106 	uint8_t x86_prefix[4];
107 	uint8_t imm_size;	// immediate size for X86_OP_IMM operand
108 	bool writeback;	// writeback for ARM
109 };
110 
111 void MCInst_Init(MCInst *inst);
112 
113 void MCInst_clear(MCInst *inst);
114 
115 // do not free operand after inserting
116 void MCInst_insert0(MCInst *inst, int index, MCOperand *Op);
117 
118 void MCInst_setOpcode(MCInst *inst, unsigned Op);
119 
120 unsigned MCInst_getOpcode(const MCInst*);
121 
122 void MCInst_setOpcodePub(MCInst *inst, unsigned Op);
123 
124 unsigned MCInst_getOpcodePub(const MCInst*);
125 
126 MCOperand *MCInst_getOperand(MCInst *inst, unsigned i);
127 
128 unsigned MCInst_getNumOperands(const MCInst *inst);
129 
130 // This addOperand2 function doesnt free Op
131 void MCInst_addOperand2(MCInst *inst, MCOperand *Op);
132 
133 #endif
134