/*
Copyright (C) 2006 Evan Teran
                   eteran@alum.rit.edu

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

#include "DisassemblyLib.h"
#include <cstring>

//------------------------------------------------------------------------------
// Name: 
//------------------------------------------------------------------------------
Instruction::Instruction() {
	std::memset(&m_Internal_instruction, 0, sizeof(m_Internal_instruction));
}

//------------------------------------------------------------------------------
// Name: 
//------------------------------------------------------------------------------
Instruction::~Instruction() {
	x86_oplist_free(&m_Internal_instruction);
}

//------------------------------------------------------------------------------
// Name: 
//------------------------------------------------------------------------------
void Instruction::format(char *buf, std::size_t len, bool valid, Configuration::Syntax syntax) const {
	if (valid) {
		if(syntax == Configuration::Intel) {
			x86_format_insn(const_cast<x86_insn_t *>(&m_Internal_instruction), buf, len, intel_syntax);
		} else {
			x86_format_insn(const_cast<x86_insn_t *>(&m_Internal_instruction), buf, len, att_syntax);
		}
	} else {
		const uint8_t byte = m_Internal_instruction.bytes[0] & 0xff;
		if(syntax == Configuration::Intel) {
			snprintf(buf, len, "db %02xh", byte);
		} else {
			snprintf(buf, len, ".byte %#02x", byte);
		}
	}
}

//------------------------------------------------------------------------------
// Name: 
//------------------------------------------------------------------------------
void Operand::format(char *buf, std::size_t len, Configuration::Syntax syntax) const {
	
	if(syntax == Configuration::Intel) {
		x86_format_operand(const_cast<Operand *>(this), buf, len, intel_syntax);
	} else {
		x86_format_operand(const_cast<Operand *>(this), buf, len, att_syntax);
	}
}

//------------------------------------------------------------------------------
// Name: 
//------------------------------------------------------------------------------
BaseTypes::address_t Operand::effectiveAddress(const State &state) const {
	
	
	x86_insn_t *const owner = reinterpret_cast<x86_insn_t *>(insn);
	
	uint32_t ret = 0;
	switch(type()) {
	case op_register:
		ret = state.value(data.reg.name);
		break;
			
	case op_relative_near:
		ret = owner->addr + owner->size + data.relative_near;
		break;
		
	case op_relative_far:
		ret = owner->addr + owner->size + data.relative_far;
		break;
		
	case op_expression:
		do {
			const BaseTypes::reg_t base		= state.value(data.expression.base.name);
			const BaseTypes::reg_t index	= state.value(data.expression.index.name);
			ret								= base + index * data.expression.scale + data.expression.disp;
		} while(0);
		break;
		
	default:
	case op_absolute:
	case op_offset:
	case op_immediate:
		break;
	}
	
	return ret;
}

//------------------------------------------------------------------------------
// Name: 
//------------------------------------------------------------------------------
int Instruction::disassemble(const uint8_t *buffer, std::size_t size, BaseTypes::address_t rva, int offset) {
	return x86_disasm(
		const_cast<uint8_t *>(buffer),
		size, 
		rva, 
		offset, 
		&m_Internal_instruction
		);
}

//------------------------------------------------------------------------------
// Name: 
//------------------------------------------------------------------------------
void disassm_init() {
	x86_init(opt_none, 0, 0);
}

//------------------------------------------------------------------------------
// Name: 
//------------------------------------------------------------------------------
void disassm_fini() {
	x86_cleanup();
}


