blob: b98f37f42961b6c2bb6053e99174c7cfcb782ce1 [file] [log] [blame]
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001//===-- 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
Nguyen Anh Quynh6023ef72014-04-29 11:21:04 +080016/* Capstone Disassembly Engine */
17/* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2014 */
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080018
Nguyen Anh Quynhae3649f2014-01-02 13:15:07 +080019#ifndef CS_MCINST_H
20#define CS_MCINST_H
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080021
tandasate51eeae2016-04-23 15:58:31 -070022#if !defined(_MSC_VER) || !defined(_KERNEL_MODE)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080023#include <stdint.h>
tandasate51eeae2016-04-23 15:58:31 -070024#endif
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080025#include "include/capstone.h"
26
27typedef struct MCInst MCInst;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +080028typedef struct cs_struct cs_struct;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080029typedef struct MCOperand MCOperand;
30
31/// MCOperand - Instances of this class represent operands of the MCInst class.
32/// This is a simple discriminated union.
33struct 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
49bool MCOperand_isValid(const MCOperand *op);
50
51bool MCOperand_isReg(const MCOperand *op);
52
53bool MCOperand_isImm(const MCOperand *op);
54
55bool MCOperand_isFPImm(const MCOperand *op);
56
57bool MCOperand_isInst(const MCOperand *op);
58
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080059/// getReg - Returns the register number.
60unsigned MCOperand_getReg(const MCOperand *op);
61
62/// setReg - Set the register number.
63void MCOperand_setReg(MCOperand *op, unsigned Reg);
64
65int64_t MCOperand_getImm(MCOperand *op);
66
67void MCOperand_setImm(MCOperand *op, int64_t Val);
68
69double MCOperand_getFPImm(const MCOperand *op);
70
71void MCOperand_setFPImm(MCOperand *op, double Val);
72
73const MCInst *MCOperand_getInst(const MCOperand *op);
74
75void MCOperand_setInst(MCOperand *op, const MCInst *Val);
76
Nguyen Anh Quynh0f648ea2014-06-10 01:01:23 +070077// create Reg operand in the next slot
Nguyen Anh Quynhcf081382014-06-06 00:56:46 +080078void MCOperand_CreateReg0(MCInst *inst, unsigned Reg);
Nguyen Anh Quynh937e4832014-06-04 22:51:51 +070079
Nguyen Anh Quynh0f648ea2014-06-10 01:01:23 +070080// create Reg operand use the last-unused slot
81MCOperand *MCOperand_CreateReg1(MCInst *inst, unsigned Reg);
82
83// create Imm operand in the next slot
Nguyen Anh Quynhcf081382014-06-06 00:56:46 +080084void MCOperand_CreateImm0(MCInst *inst, int64_t Val);
Nguyen Anh Quynh937e4832014-06-04 22:51:51 +070085
Nguyen Anh Quynh0f648ea2014-06-10 01:01:23 +070086// create Imm operand in the last-unused slot
87MCOperand *MCOperand_CreateImm1(MCInst *inst, int64_t Val);
88
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080089/// MCInst - Instances of this class represent a single low-level machine
90/// instruction.
91struct MCInst {
Nguyen Anh Quynh495295e2014-06-16 15:54:32 +080092 unsigned OpcodePub;
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +080093 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
Nguyen Anh Quynhf1ec5262014-06-25 22:03:18 +080096 unsigned Opcode;
97 MCOperand Operands[48];
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +070098 cs_insn *flat_insn; // insn to be exposed to public
Nguyen Anh Quynha209e672013-12-14 00:23:41 +080099 uint64_t address; // address of this insn
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800100 cs_struct *csh; // save the main csh
Nguyen Anh Quynh10850732014-06-18 12:16:24 +0800101 uint8_t x86opsize; // opsize for [mem] operand
Nguyen Anh Quynha5ffdc32014-05-07 08:25:24 +0800102
Nguyen Anh Quynh10850732014-06-18 12:16:24 +0800103 // (Optional) instruction prefix, which can be up to 4 bytes.
Nguyen Anh Quynha5ffdc32014-05-07 08:25:24 +0800104 // A prefix byte gets value 0 when irrelevant.
105 // This is copied from cs_x86 struct
Nguyen Anh Quynh46b66932014-06-04 19:04:46 +0800106 uint8_t x86_prefix[4];
Nguyen Anh Quynhf1ec5262014-06-25 22:03:18 +0800107 uint8_t imm_size; // immediate size for X86_OP_IMM operand
Nguyen Anh Quynhe19490e2015-01-21 12:15:14 +0800108 bool writeback; // writeback for ARM
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800109};
110
Nguyen Anh Quynh495295e2014-06-16 15:54:32 +0800111void MCInst_Init(MCInst *inst);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800112
113void MCInst_clear(MCInst *inst);
114
Nguyen Anh Quynh0f648ea2014-06-10 01:01:23 +0700115// do not free operand after inserting
116void MCInst_insert0(MCInst *inst, int index, MCOperand *Op);
117
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800118void MCInst_setOpcode(MCInst *inst, unsigned Op);
119
120unsigned MCInst_getOpcode(const MCInst*);
121
Nguyen Anh Quynh6b7abe32013-11-30 00:54:24 +0800122void MCInst_setOpcodePub(MCInst *inst, unsigned Op);
123
124unsigned MCInst_getOpcodePub(const MCInst*);
125
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800126MCOperand *MCInst_getOperand(MCInst *inst, unsigned i);
127
128unsigned MCInst_getNumOperands(const MCInst *inst);
129
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800130// This addOperand2 function doesnt free Op
Nguyen Anh Quynh264ca372014-06-16 14:52:09 +0800131void MCInst_addOperand2(MCInst *inst, MCOperand *Op);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800132
133#endif