H++ Assembly Unit helper in C/C++   Leave a comment

// assembly_source_unit.h: interface for the assembly_source_unit class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_ASSEMBLY_SOURCE_UNIT_H__4003CFF4_DFCA_4960_B47E_4F0DC73AD181__INCLUDED_)
#define AFX_ASSEMBLY_SOURCE_UNIT_H__4003CFF4_DFCA_4960_B47E_4F0DC73AD181__INCLUDED_

/*
********************************************************************************************************
*																									   *
*																									   *
*	MODULE			: <assembly_source_unit.h>													   *	
*																									   *
*	DESCRIPTION		: HCC Compiler Assembly Source Unit class										   *
*																									   *
*	AUTHOR			: Harold L. Marzan																   *	
*																									   *
*	LAST-MODIFIED	: (unknown)																		   *	
*																									   *
********************************************************************************************************
*/
#if !defined(_DEBUG)
#define __STREAM_TO_FILE
#endif

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include "HCCLib\corecommon.h"
#include "HCCLib\errors.h"

#include <iostream>
#include <iomanip>

using namespace std;

class assembly_source_unit
{
	__tostream* out_ptr;
	static int m_nMaxCharsPerLine;
	int m_nTextLineLength;	
public:
	assembly_source_unit(const char* output_file) : m_nTextLineLength(0) {

#ifdef __STREAM_TO_FILE	
		out_ptr = new __tofstream(output_file);
#else
		out_ptr = &cout;
#endif

		if(!out_ptr)
			HccErrorManager::AbortTranslation(HccErrorManager::abortAssemblyFileOpenFailed);
	}
	virtual ~assembly_source_unit();
public:
	void close();
	void flush();
	void attach(__tostream* new_out_ptr);
	__tostream* detach();
	assembly_source_unit& operator<<(const __tstring& code)
	{		
		if((unsigned long)code.length() + m_nTextLineLength <= (unsigned long)m_nMaxCharsPerLine){
			*out_ptr << code;
			m_nTextLineLength += code.length();
		}else{
			//*out_ptr << _T('\n') << code; //<-- This lines causes unpredictable syntax errors in MASM when breaking with newlines
			*out_ptr << code;
			m_nTextLineLength = code.length();
		}
		return *this;
	}
	assembly_source_unit& operator<<(const TCHAR ch)
	{
		if(ch==_T('\n')) m_nTextLineLength = 0;

		if(++m_nTextLineLength <= m_nMaxCharsPerLine)
			*out_ptr << ch;			
		else{
			*out_ptr << _T('\n') << ch;			
			m_nTextLineLength = 1;
		}
		return *this;
	}

	assembly_source_unit& operator<<(const int nValue)
	{		
		if(++m_nTextLineLength <= m_nMaxCharsPerLine)
			*out_ptr << setw(9) 
					<< uppercase
					<< setfill('0') 
					<< hex << nValue 
					<< nouppercase
					<< _T("h");
		else{
			*out_ptr << _T('\n') 
					<< setw(9) 
					<< uppercase
					<< setfill('0') 
					<< hex << nValue 
					<< nouppercase
					<< _T("h");
			m_nTextLineLength = 1;
		}
		return *this;
	}

	assembly_source_unit& operator< m_nMaxCharsPerLine)
		{
			*out_ptr << _T('\n');
			m_nTextLineLength = 1;
		}

		*out_ptr << setw(17)
				<< uppercase
				<< setfill('0') 
				<< hex << nValue 
				<< nouppercase
				<< _T("h");

		return *this;
	}

	assembly_source_unit& operator<<(const double dValue)
	{		
		if(++m_nTextLineLength <= m_nMaxCharsPerLine)
			*out_ptr << dValue;
		else{
			*out_ptr << _T('\n') << dValue;
			m_nTextLineLength = 1;
		}
		return *this;
	}

	assembly_source_unit& operator<<( assembly_source_unit& (*fnManip)(assembly_source_unit&))
	{		
		return fnManip(*this);
	}

	friend assembly_source_unit& hex(assembly_source_unit& unit);
	friend assembly_source_unit& dec(assembly_source_unit& unit);
};

assembly_source_unit& endl(assembly_source_unit& unit);
assembly_source_unit& ctab(assembly_source_unit& unit);
assembly_source_unit& space(assembly_source_unit& unit);
assembly_source_unit& comma(assembly_source_unit& unit);

#endif // !defined(AFX_ASSEMBLY_SOURCE_UNIT_H__4003CFF4_DFCA_4960_B47E_4F0DC73AD181__INCLUDED_)

// assembly_source_unit.cpp: implementation of the assembly_source_unit class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "assembly_source_unit.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;	//this is private/unique by file
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
int assembly_source_unit::m_nMaxCharsPerLine = 1024;


assembly_source_unit::~assembly_source_unit()
{
#ifdef __STREAM_TO_FILE
	attach(reinterpret_cast(0xDEADBEEF));
#endif
}


assembly_source_unit& endl(assembly_source_unit& unit)
{
	return unit << _T('\n');	
}

assembly_source_unit& ctab(assembly_source_unit& unit)
{
	return unit << _T('\t');
}
assembly_source_unit& space(assembly_source_unit& unit)
{
	return unit << _T(' ');
}
assembly_source_unit& comma(assembly_source_unit& unit)
{
	return unit <setf(ios_base::hex, ios_base::dec);
	return unit;
}

assembly_source_unit& dec(assembly_source_unit& unit)
{
	unit.out_ptr->setf(ios_base::dec, ios_base::hex);
	return unit;
}

__tostream* assembly_source_unit::detach()
{
	__tostream* tmp_ptr = out_ptr;
	out_ptr = NULL;
	return tmp_ptr;
}

void assembly_source_unit::attach(__tostream *new_out_ptr)
{
	assert(new_out_ptr!=NULL);
	if(out_ptr){
		delete out_ptr;
		out_ptr = NULL;
	}

	out_ptr = new_out_ptr;
}

void assembly_source_unit::flush()
{
	out_ptr->flush();
}

void assembly_source_unit::close()
{
	/*
	__tofstream* file_stream = dynamic_cast(out_ptr);
	if(file_stream!=NULL)
		file_stream->close();
	*/
	flush();
}

Advertisements

Posted February 2, 2013 by hmarzan

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: