/*
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.
*/

#ifndef DISASSEMBLYLIB_06122006_H_
#define DISASSEMBLYLIB_06122006_H_

#include <libdis.h>
#include <stdint.h>
#include <cstddef>
#include "types.h"
#include "Configuration.h"

struct Operand : private x86_op_t {
public:
	void format(char *buf, std::size_t len, Configuration::Syntax syntax) const;

public:
	x86_op_type type() const { return x86_op_t::type; }		
	BaseTypes::address_t effectiveAddress(const State &state) const;
};

struct Instruction {
public:
	Instruction();
	~Instruction();

private:
	// not copyable
	Instruction(const Instruction&);
	Instruction &operator=(const Instruction&);
	
public:
	const Operand *operand_1st() 		{ return reinterpret_cast<Operand *>(x86_operand_1st(&m_Internal_instruction)); }
	const Operand *operand_2nd() 		{ return reinterpret_cast<Operand *>(x86_operand_2nd(&m_Internal_instruction)); }
	const Operand *operand_3rd() 		{ return reinterpret_cast<Operand *>(x86_operand_3rd(&m_Internal_instruction)); }
	
	x86_insn_prefix prefix() const		{ return m_Internal_instruction.prefix; }
	x86_insn_type type() const			{ return m_Internal_instruction.type; }
	x86_flag_status flagsTested() const	{ return m_Internal_instruction.flags_tested; }
	int size() const					{ return m_Internal_instruction.size; }
	const uint8_t *data() const			{ return m_Internal_instruction.bytes; }
	
	void format(char *buf, std::size_t len, bool valid, Configuration::Syntax syntax) const;
	int disassemble(const uint8_t *buffer, std::size_t size, BaseTypes::address_t rva, int offset);

public:
	x86_insn_t m_Internal_instruction;
};

void disassm_init();
void disassm_fini();
#endif
