/*
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 I386ARCHPROCESSOR_20070312_H_
#define I386ARCHPROCESSOR_20070312_H_

#include <QVector>
#include <QStringList>
#include <QMap>
#include <QString>
#include "FunctionInfo.h"
#include "EDBTypes.h"
#include "State.h"
#include "ArchProcessor.h"

#define FPU

class QTreeWidgetItem;
class Instruction;
class Operand;

class i386ArchProcessor : public ArchProcessor {
public:
	i386ArchProcessor();
	
private:
	enum Register {
		REG_INVALID,
		REG_EAX,
		REG_EBX,
		REG_ECX,
		REG_EDX,
		REG_EBP,
		REG_ESP,
		REG_ESI,
		REG_EDI,
		REG_EFLAGS,
		REG_EIP,
		REG_CS,
		REG_DS,
		REG_ES,
		REG_FS,
		REG_GS,
		REG_SS,
#ifdef FPU
		REG_ST0,
		REG_ST1,
		REG_ST2,
		REG_ST3,
		REG_ST4,
		REG_ST5,
		REG_ST6,
		REG_ST7
#endif
	};
	
public:
	virtual void setupRegisterView(QCatagoryList *catagoryList);
	virtual void updateRegisterView(const QString &defaultRegionName);
	virtual QStringList updateInstructionInfo(edb::address_t address);
	virtual edb::reg_t *valueFromItem(const QTreeWidgetItem &item, unsigned int mode);
	virtual void reset();
public:
	virtual bool isFilling(const Instruction &insn, const QByteArray &bytes) const;
	virtual bool canStepOver(const Instruction &insn) const;
	virtual edb::address_t getEffectiveAddress(const Operand &op) const;
	virtual edb::address_t getEffectiveAddress(const Operand &op, const State &state) const;
private:
	QTreeWidgetItem *getRegisterItem(unsigned int index);
	void doUpdateRegister(QTreeWidgetItem *item, const QString &name, edb::reg_t value) const;
	void setupRegisterItem(QCatagoryList *catagoryList, QTreeWidgetItem *parent, Register reg);

private:
	QStringList doFunctionParamterResolution(const QString &symname, int offset) const;
	void analyzeCall(const Operand *operands, const Instruction &insn, QStringList &ret, const uint8_t *insn_buf) const;
	void analyzeJump(const Operand *operands, const Instruction &insn, QStringList &ret, const uint8_t *insn_buf) const;
	void analyzeReturn(const Operand *operands, const Instruction &insn, QStringList &ret, const uint8_t *insn_buf) const;
	void analyzeOperands(const Operand *operands, const Instruction &insn, QStringList &ret, const uint8_t *insn_buf) const;
	void analyzeCMov(const Operand *operands, const Instruction &insn, QStringList &ret, const uint8_t *insn_buf) const;
	bool jccTaken(uint8_t instructionByte) const;
	
private:
	QVector<QTreeWidgetItem *>	m_RegisterViewItems;
	QTreeWidgetItem *m_SplitEFlags;
	State m_LastState;

private:
	// TODO: move this somewhere more general
	static QMap<QString, FunctionInfo> m_FunctionInfo;
	
	struct Function {
		const char *name;
		FunctionInfo f;
	};
	
	static const Function funcs[];
	
	static int m_FuncInitialized;
};

#endif

