2014-05-08 14:54:58 +00:00
|
|
|
/* Capstone Disassembly Engine */
|
2015-03-04 09:45:23 +00:00
|
|
|
/* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2015 */
|
2013-11-27 04:11:31 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "MCInst.h"
|
|
|
|
#include "utils.h"
|
|
|
|
|
2014-06-04 15:51:51 +00:00
|
|
|
#define MCINST_CACHE (ARR_SIZE(mcInst->Operands) - 1)
|
|
|
|
|
2014-06-16 07:54:32 +00:00
|
|
|
void MCInst_Init(MCInst *inst)
|
2013-11-27 04:11:31 +00:00
|
|
|
{
|
2014-06-16 07:54:32 +00:00
|
|
|
inst->OpcodePub = 0;
|
|
|
|
inst->size = 0;
|
2014-11-03 08:32:06 +00:00
|
|
|
inst->has_imm = false;
|
|
|
|
inst->op1_size = 0;
|
2015-01-21 04:15:14 +00:00
|
|
|
inst->writeback = false;
|
2013-11-27 04:11:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MCInst_clear(MCInst *inst)
|
|
|
|
{
|
|
|
|
inst->size = 0;
|
|
|
|
}
|
|
|
|
|
2014-06-09 18:01:23 +00:00
|
|
|
// do not free @Op
|
2014-06-04 15:51:51 +00:00
|
|
|
void MCInst_insert0(MCInst *inst, int index, MCOperand *Op)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for(i = inst->size; i > index; i--)
|
|
|
|
//memcpy(&(inst->Operands[i]), &(inst->Operands[i-1]), sizeof(MCOperand));
|
|
|
|
inst->Operands[i] = inst->Operands[i-1];
|
|
|
|
|
|
|
|
inst->Operands[index] = *Op;
|
|
|
|
inst->size++;
|
|
|
|
}
|
|
|
|
|
2013-11-27 04:11:31 +00:00
|
|
|
void MCInst_setOpcode(MCInst *inst, unsigned Op)
|
|
|
|
{
|
|
|
|
inst->Opcode = Op;
|
|
|
|
}
|
|
|
|
|
2013-11-29 16:54:24 +00:00
|
|
|
void MCInst_setOpcodePub(MCInst *inst, unsigned Op)
|
|
|
|
{
|
|
|
|
inst->OpcodePub = Op;
|
|
|
|
}
|
|
|
|
|
2013-11-27 04:11:31 +00:00
|
|
|
unsigned MCInst_getOpcode(const MCInst *inst)
|
|
|
|
{
|
|
|
|
return inst->Opcode;
|
|
|
|
}
|
|
|
|
|
2013-11-29 16:54:24 +00:00
|
|
|
unsigned MCInst_getOpcodePub(const MCInst *inst)
|
|
|
|
{
|
|
|
|
return inst->OpcodePub;
|
|
|
|
}
|
|
|
|
|
2013-11-27 04:11:31 +00:00
|
|
|
MCOperand *MCInst_getOperand(MCInst *inst, unsigned i)
|
|
|
|
{
|
|
|
|
return &inst->Operands[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned MCInst_getNumOperands(const MCInst *inst)
|
|
|
|
{
|
|
|
|
return inst->size;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This addOperand2 function doesnt free Op
|
2014-06-16 06:52:09 +00:00
|
|
|
void MCInst_addOperand2(MCInst *inst, MCOperand *Op)
|
2013-11-27 04:11:31 +00:00
|
|
|
{
|
|
|
|
inst->Operands[inst->size] = *Op;
|
|
|
|
|
|
|
|
inst->size++;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MCOperand_Init(MCOperand *op)
|
|
|
|
{
|
|
|
|
op->Kind = kInvalid;
|
|
|
|
op->FPImmVal = 0.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MCOperand_isValid(const MCOperand *op)
|
|
|
|
{
|
|
|
|
return op->Kind != kInvalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MCOperand_isReg(const MCOperand *op)
|
|
|
|
{
|
|
|
|
return op->Kind == kRegister;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MCOperand_isImm(const MCOperand *op)
|
|
|
|
{
|
|
|
|
return op->Kind == kImmediate;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MCOperand_isFPImm(const MCOperand *op)
|
|
|
|
{
|
|
|
|
return op->Kind == kFPImmediate;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// getReg - Returns the register number.
|
|
|
|
unsigned MCOperand_getReg(const MCOperand *op)
|
|
|
|
{
|
|
|
|
return op->RegVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// setReg - Set the register number.
|
|
|
|
void MCOperand_setReg(MCOperand *op, unsigned Reg)
|
|
|
|
{
|
|
|
|
op->RegVal = Reg;
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t MCOperand_getImm(MCOperand *op)
|
|
|
|
{
|
|
|
|
return op->ImmVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MCOperand_setImm(MCOperand *op, int64_t Val)
|
|
|
|
{
|
|
|
|
op->ImmVal = Val;
|
|
|
|
}
|
|
|
|
|
|
|
|
double MCOperand_getFPImm(const MCOperand *op)
|
|
|
|
{
|
|
|
|
return op->FPImmVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MCOperand_setFPImm(MCOperand *op, double Val)
|
|
|
|
{
|
|
|
|
op->FPImmVal = Val;
|
|
|
|
}
|
|
|
|
|
2014-06-09 18:01:23 +00:00
|
|
|
MCOperand *MCOperand_CreateReg1(MCInst *mcInst, unsigned Reg)
|
2014-06-04 15:51:51 +00:00
|
|
|
{
|
|
|
|
MCOperand *op = &(mcInst->Operands[MCINST_CACHE]);
|
|
|
|
|
|
|
|
op->Kind = kRegister;
|
|
|
|
op->RegVal = Reg;
|
|
|
|
|
|
|
|
return op;
|
|
|
|
}
|
2014-06-05 16:56:46 +00:00
|
|
|
|
|
|
|
void MCOperand_CreateReg0(MCInst *mcInst, unsigned Reg)
|
|
|
|
{
|
|
|
|
MCOperand *op = &(mcInst->Operands[mcInst->size]);
|
|
|
|
mcInst->size++;
|
|
|
|
|
|
|
|
op->Kind = kRegister;
|
|
|
|
op->RegVal = Reg;
|
|
|
|
}
|
2014-06-04 15:51:51 +00:00
|
|
|
|
2014-06-09 18:01:23 +00:00
|
|
|
MCOperand *MCOperand_CreateImm1(MCInst *mcInst, int64_t Val)
|
2014-06-04 15:51:51 +00:00
|
|
|
{
|
|
|
|
MCOperand *op = &(mcInst->Operands[MCINST_CACHE]);
|
|
|
|
|
|
|
|
op->Kind = kImmediate;
|
|
|
|
op->ImmVal = Val;
|
|
|
|
|
|
|
|
return op;
|
|
|
|
}
|
2014-06-05 16:56:46 +00:00
|
|
|
|
|
|
|
void MCOperand_CreateImm0(MCInst *mcInst, int64_t Val)
|
|
|
|
{
|
|
|
|
MCOperand *op = &(mcInst->Operands[mcInst->size]);
|
|
|
|
mcInst->size++;
|
|
|
|
|
|
|
|
op->Kind = kImmediate;
|
|
|
|
op->ImmVal = Val;
|
|
|
|
}
|