#pragma once
#ifndef __ASSEMBLER_H__
#define __ASSEMBLER_H__
#include <vector>
#include "asmjit/asmjit.h"

using namespace asmjit;
using namespace x86;
class CAssembler
{
public:
	CAssembler(Arch arch = Arch::kX86) :
		m_Environment(arch)
	{
		Prepare();
	}

	~CAssembler() {}
public:
	void Prepare()
	{
		m_CodeHolder.reset();
		m_CodeHolder.init(m_Environment, 0);
		m_CodeHolder.attach(&m_Assembler);
		//m_Assembler.addDiagnosticOptions(asmjit::DiagnosticOptions::kValidateAssembler);
	}

	Assembler* GetAssembler()
	{
		return &m_Assembler;
	}

	Section* GetEmitCode()
	{
		return m_CodeHolder.textSection();
	}

	std::vector <unsigned char> GetBytes(bool clear = true)
	{
		Section* pSection = this->GetEmitCode();
		std::vector <unsigned char>  AsmCode;
		AsmCode.resize(pSection->bufferSize());
		memcpy(AsmCode.data(), pSection->data(), pSection->bufferSize());
		if (clear) this->Prepare();
		return AsmCode;
	}


protected:


private:
	CodeHolder m_CodeHolder;
	Environment m_Environment;
	Assembler m_Assembler;
};


#endif // __ASSEMBLER_H__

