/*
Copyright (C) 2006 - 2009 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 ARCHPROCESSOR_20070312_H_
#define ARCHPROCESSOR_20070312_H_

#include <QVector>
#include <QStringList>
#include <QMap>
#include <QString>
#include "Types.h"
#include "State.h"
#include "ArchProcessorInterface.h"

class QTreeWidgetItem;

class ArchProcessor : public ArchProcessorInterface {
public:
	ArchProcessor();
	virtual ~ArchProcessor() {}
	
private:
	Q_DISABLE_COPY(ArchProcessor)
	
private:
	enum Register {
		REG_NULL,
		REG_RAX,
		REG_RBX,
		REG_RCX,
		REG_RDX,
		REG_RBP,
		REG_RSP,
		REG_RSI,
		REG_RDI,
		REG_R8,
		REG_R9,
		REG_R10,
		REG_R11,
		REG_R12,
		REG_R13,
		REG_R14,
		REG_R15,
		REG_RFLAGS,
		REG_RIP,
		REG_CS,
		REG_DS,
		REG_ES,
		REG_FS,
		REG_GS,
		REG_SS,
		REG_ST0,
		REG_ST1,
		REG_ST2,
		REG_ST3,
		REG_ST4,
		REG_ST5,
		REG_ST6,
		REG_ST7
	};
	
public:
	virtual void setupRegisterView(QCategoryList *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 edb::Instruction &insn) const;
	virtual bool canStepOver(const edb::Instruction &insn) const;
	virtual edb::address_t getEffectiveAddress(const edb::Operand &op) const;
	virtual edb::address_t getEffectiveAddress(const edb::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(QCategoryList *catagoryList, QTreeWidgetItem *parent, Register reg);

private:
	void doFunctionParamterResolution(const QString &symname, int offset, QStringList &ret) const;
	void analyzeCall(const edb::Instruction &insn, QStringList &ret) const;
	void analyzeJump(const edb::Instruction &insn, QStringList &ret) const;
	void analyzeReturn(const edb::Instruction &insn, QStringList &ret) const;
	void analyzeOperands(const edb::Instruction &insn, QStringList &ret) const;
	void analyzeCMov(const edb::Instruction &insn, QStringList &ret) const;
	
	void analyzeJumpTargets(const edb::Instruction &insn, QStringList &ret) const;
	
	bool jccTaken(quint8 instructionByte) const;
	
private:
	QVector<QTreeWidgetItem *>	m_RegisterViewItems;
	QTreeWidgetItem *m_SplitFlags;
	State m_LastState;
};

#endif

