www.pudn.com > HEC-linux.zip > symtbl.cpp


/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
+                                                                   + 
+ symtbl.cpp - the symbol table and associated functions            + 
+                                                                   + 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ 
 
/* 
	SymTbl---+---GlobalVariable 
			 | 
			 +---Procedure-----+--StackFrame ( ret, arg, local ) 
						       | 
						       +--Label 
 
	Pass1 - uses directives to fill out GlobVar, Proc entries 
	Pass2 - uses above to generate bytecode 
*/ 
 
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
+ macros and private variables                                      + 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ 
 
#define GLOB_INIT	10		/* _INIT = initial array size */ 
#define GLOB_INC	5		/* _INC = increment size when expand array */ 
 
#define PROC_INIT	10 
#define PROC_INC	5 
 
#define ARG_INIT	5	 
#define ARG_INC		5	 
 
#define LOC_INIT	5 
#define LOC_INC		5 
 
#define LBL_INIT	10 
#define LBL_INC		10 
 
/*#define SYM_DEBUG		1*/ 
 
#ifdef	SYM_DEBUG 
#define	SYM_DEBUG0(arg);			printf(arg); 
#define SYM_DEBUG1(arg1,arg2);		printf(arg1,arg2); 
#define SYM_DEBUG2(arg1,arg2,arg3);	printf(arg1,arg2,arg3); 
#else 
#define	SYM_DEBUG0(arg); 
#define SYM_DEBUG1(arg1,arg2); 
#define SYM_DEBUG2(arg1,arg2,arg3); 
#endif 
 
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
+ declaration                                                       + 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ 
 
class SymbolTable 
{ 
	StringTable *strTbl; 
 
	/*called via public constructors/destructors*/ 
	void initProc(struct Procedure *ptr); 
	void freeProc(struct Procedure *ptr); 
 
	/*called via public addProc___HL */ 
	void setProcRet(struct Procedure *ptr, struct StackFrame *add); 
	void addProcArg(struct Procedure *ptr, struct StackFrame *add); 
	void addProcLoc(struct Procedure *ptr, struct StackFrame *add); 
	void addProcLbl(struct Procedure *ptr, struct Label *add); 
 
	/*called via public printSymTbl*/ 
	void printGlobVar(struct GlobalVariable *ptr); 
	void printProc(struct Procedure *ptr); 
 
	public: 
	struct GlobalVariable *globVar; 
	U4 nGlobVar; 
	U4 iGlobVar; 
 
	struct Procedure *proc; 
	U4 nProc; 
	U4 iProc; 
 
	SymbolTable(StringTable *st); 
	~SymbolTable(); 
 
	void addGlobVar(struct GlobalVariable *add); 
	void addProc(struct Procedure *add); 
 
	/*set elements of current procedure*/ 
	void setProcRetHL(struct StackFrame *add); 
	void addProcArgHL(struct StackFrame *add);  
	void addProcLocHL(struct StackFrame *add);  
	void addProcLblHL(struct Label *add);  
 
	void printSymTbl(); 
 
	void test(); 
}; 
 
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
+ definitions                                                       + 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ 
 
SymbolTable::SymbolTable(StringTable *st) 
{ 
	U4 i; 
 
	strTbl = st; 
 
	globVar = (struct GlobalVariable*) 
		      malloc(GLOB_INIT*sizeof(struct GlobalVariable)); 
 
	if(globVar==NULL) 
	{ 
		ERROR0("SymbolTable::SymbolTable(): out of memory\n"); 
		FATAL_ERROR(); 
	} 
	nGlobVar = GLOB_INIT; 
	iGlobVar = 0; 
 
	proc = (struct Procedure*) 
		   malloc(PROC_INIT*sizeof(struct Procedure)); 
 
	if(proc==NULL) 
	{ 
		ERROR0("SymbolTable::SymbolTable(): out of memory\n"); 
		FATAL_ERROR(); 
	} 
	nProc = PROC_INIT; 
	iProc = 0; 
	for(i=0;i