rsp2elf.h 6.74 KB
#include <iostream.h>
#include <iomanip.h>

void nospace( void );
int GetFileOfAddr( unsigned long );

class symbol {
	char * symName;
	unsigned long symValue;
	int localIndex, globalIndex, symIndex, symType, symFile;
public:
	symbol * next, * pnext;

	symbol( char * name, unsigned long value, int type ) {
		next = 0; pnext = 0; localIndex = 0; globalIndex = 0;
                symIndex = 0;
		symName = name; symValue = value; symType = type;
		symFile = GetFileOfAddr( value ); }
	void print( void ) { cout << "  SYM " << symName << " Val " << hex <<
		symValue << dec << " loc strndx " << localIndex <<
		" glb strndx " << globalIndex << " type " << symType <<
		" file " << symFile << endl; }
	void AsgFile( void ) { symFile = GetFileOfAddr( symValue ); }
	int matches( char * s ) { return !strcmp( s, symName ); }
	void AsgGlobalIndex( int index ) { globalIndex = index; }
	void AsgLocalIndex( int index ) { localIndex = index; }
	void AsgSymIndex( int index ) { symIndex = index; }

	char * NameIs( void ) { return symName; }
	int GetGlobalIndex( void ) { return globalIndex; }
	int GetLocalIndex( void ) { return localIndex; }
	unsigned long GetValue( void ) { return symValue; }
	int GetType( void ) { return symType; }
	int GetSymIndex( void ) { return symIndex; }
	int GetFile( void ) { return symFile; }
};

class line {
	int lineNumber;
	friend class proc;
	friend class source;
public:
	line * next;

	line( int l ) { next = 0; lineNumber = l; }
	void print( void ) { cout << lineNumber << ' '; }
	int LineIs( void ) { return lineNumber; }
};

class proc {
	char * procName;
	unsigned long lowAddr, highAddr;
	line * lineList, * lineLast;
	int lowLine, highLine, localIndex, auxEntry, firstSym, 
		lineCount, lineIndex;
	symbol * symList, * symLast;	// symbols associated with proc
public:
	proc * next;

	proc() { next = 0; symList = 0; lowLine = 0; highLine = 0;
		lowAddr = 0; highAddr = 0; localIndex = 0; auxEntry = 0; 
		firstSym = 0; lineIndex = 0; lineList = 0; lineCount = 0; }
	void print( void );
	void callMe( char * s ) {
		procName = (char *) malloc( strlen( s ) + 1 );
		if ( !procName ) nospace();
		strcpy( procName, s ); }
	void newhigh( unsigned long addr, line * l ) {
		highAddr = addr; highLine = l->lineNumber; }
	void newlow( unsigned long addr, int line ) {
		lowAddr = addr; lowLine = line; }
	void InheritLines( line * first, line * last ) {
		lineList = first; lineLast = last; }
	void InheritLabels( symbol * first, symbol * last ) {
		symList = first; symLast = last; }
	char * nameIs() { return procName; }
	int contains( unsigned long addr );
	unsigned long FirstInst( void ) { return lowAddr; }
	unsigned long NextInst( void ) { return highAddr + 1; }
	int FirstLine( void ) { return lowLine; }
	int LastLine( void ) { return highLine; }
	void AddLine( line * l );
	void AddSymbol( symbol * sym ) {
		if ( symList )
			symLast->pnext = sym;
		else
			symList = sym;
		symLast = sym; }
	void AsgSymIndex( int index );
	symbol * GetLabels( void ) { return symList; }
	void AsgLocalIndex( int index ) { localIndex = index; }
	void AsgAux( int aux ) { auxEntry = aux; }
	void AsgFirstSym( int s ) { firstSym = s; }
	void AsgLineIndex( int li ) { lineIndex = li; }
	void NewLineRange( int low, int high ) { lowLine = low;
		highLine = high; }

	int GetAux( void ) { return auxEntry; }
	int GetStringIndex( void ) { return localIndex; }
	int GetFirstSym( void ) { return firstSym; }
	int GetLineIndex( void ) { return lineIndex; }
	line * GetLines( void ) { return lineList; }
};

class range {
	unsigned long lowAddr, highAddr;
	int lowLine, highLine;
	friend class source;
public:
	range * next;

	range( unsigned long addr, int line ) {
		next = 0; lowLine = line;
		highLine = line + 1;
		lowAddr = addr;
		highAddr = addr + 3;
	}
	void print( void ) { cout << " RANGE addr " << hex << lowAddr <<
		":" << highAddr << dec << " lines " << lowLine << ":" <<
                highLine << endl; }
	int inrange( unsigned long addr );
	unsigned long endOfRange( void ) { return highAddr; }
	void extend( int newline ) {
		highAddr += 4; highLine = newline;
	}
};

class source {
	char * fileName;
	range * rangeList, * rangeLast;
	line * lineList, * lineLast;
	proc * procList, * procLast;
	symbol * symList, * symLast;	// symbols with no proc
	int stringIndex, stringSize, firstSym, lineCount, lineBytes,
		lineBase, procCount, labelCount, auxBase;
public:
	source * next;

	source() { next = 0; rangeList = 0; lineList = 0; lineCount = 0;
		procCount = 0; procList = 0; labelCount = 0; lineBase = 0;
		auxBase = 0; symList = 0; }
	void print( void ) { cout << "FILE " << fileName << " strndx "
		<< stringIndex << " strsize " << stringSize << " 1stsym "
		<< firstSym << " lines " << lineCount << " 1stlin " <<
		lineBase << " procs " << procCount << endl; }
	void PrintProcs( void );
	void PrintSymbols( void );

	void callMe( char * s ) {
        	fileName = (char *) malloc( strlen( s ) + 1 );
		if ( !fileName ) nospace();
		strcpy( fileName, s ); }
	int containsAddr( unsigned long addr );
	void newRange( unsigned long addr, int sourceLine );
	void AddProc( proc * p );
	void JamProc( proc * p ) { procList = p; ++procCount; }
	void AddLine( line * l );
	void AddSymbol( symbol * sym );
	void MakeProcForFile( void );

	void AsgLineBase( int l ) { lineBase = l; }
	void AsgLineBytes( int l ) { lineBytes = l; }
	void AsgStringIndex( int l ) { stringIndex = l; }
	void AsgStringSize( int l ) { stringSize = l; }
	void AsgFirstSym( int l ) { firstSym = l; }
	void OneMoreLabel( void ) { ++labelCount; }
	void OneMoreLine( void ) { ++lineCount; }
	void AsgAuxBase( int a ) { auxBase = a; }

	char * filename( void ) { return fileName; }
	range * firstRange( void ) { return rangeList; }
	line * GetLines( void ) { return lineList; }
	proc * GetProcs( void ) { return procList; }
	int FirstLineIs( void ) { return lineList->lineNumber; }
	int LastLineIs( void ) { return lineLast->lineNumber; }
	int StringIndexIs( void ) { return stringIndex; }
	int StringSizeIs( void ) { return stringSize; }
	int FirstSymIs( void ) { return firstSym; }
	int ProcCountIs( void ) { return procCount; }
	int LineCountIs( void ) { return lineCount; }
	int LabelCountIs( void ) { return labelCount; }
	int LineBytesIs( void ) { return lineBytes; }
	unsigned long FirstInst( void ) { return rangeList->lowAddr; }
	unsigned long NextInst( void ) { return rangeLast->highAddr + 1; }
	symbol * GetLabels( void ) { return symList; }
	int LineBaseIs( void ) { return lineBase; }
	int GetAuxBase(  void ) { return auxBase; }
};

#define IMEM_START 0x4001000
#define DMEM_START 0x4000000

/* decls */

void WriteString( char * );
int NewSymbol( char * , unsigned long , int );
void BuildFileTable( void );
void WriteDebugInfo( void );
void AddSymToFile( symbol * sym );
void DumpFileTable( void );
void CleanupInfo( void );
void AddSyntheticProc( void );
int SymBaseOfFile( int );