From 902a5ec3d85f4afdaa7dfaa7c472e2838f51b206 Mon Sep 17 00:00:00 2001 From: Artur K Date: Tue, 13 Mar 2012 01:22:13 +0100 Subject: [PATCH] Extracted commonalities between SYM and STKSYM into SymbolCommon and between SYMTAB and STKFRAME into SymbolTableCommon --- include/StackFrame.h | 29 +++------- include/dcc.h | 24 +-------- include/locident.h | 8 +-- include/symtab.h | 70 ++++++++++++++++++++++++ src/ast.cpp | 27 +++++----- src/backend.cpp | 110 +++++++++++++++++++------------------- src/chklib.cpp | 4 +- src/comwrite.cpp | 2 +- src/dataflow.cpp | 16 +++--- src/hlicode.cpp | 20 +++---- src/idioms.cpp | 2 +- src/locident.cpp | 31 +++++------ src/parser.cpp | 123 +++++++++++++------------------------------ src/procs.cpp | 80 ++++++++++++++-------------- src/symtab.cpp | 55 +++++++++++++++++++ 15 files changed, 311 insertions(+), 290 deletions(-) diff --git a/include/StackFrame.h b/include/StackFrame.h index 111a757..6d45b61 100644 --- a/include/StackFrame.h +++ b/include/StackFrame.h @@ -3,39 +3,22 @@ #include #include "types.h" #include "Enums.h" -struct COND_EXPR; -/* STACK FRAME */ -struct STKSYM -{ - COND_EXPR *actual; /* Expression tree of actual parameter */ - COND_EXPR *regs; /* For register arguments only */ - int16_t off; /* Immediate off from BP (+:args, -:params) */ - uint8_t regOff; /* Offset is a register (e.g. SI, DI) */ - int size; /* Size */ - hlType type; /* Probable type */ - eDuVal duVal; /* DEF, USE, VAL */ - boolT hasMacro; /* This type needs a macro */ - char macro[10]; /* Macro name */ - char name[10]; /* Name for this symbol/argument */ - bool invalid; /* Boolean: invalid entry in formal arg list*/ - STKSYM() - { - memset(this,0,sizeof(STKSYM)); - } -}; +#include "symtab.h" -struct STKFRAME +struct STKFRAME : public SymbolTableCommon { - std::vector sym; + //std::vector sym; //STKSYM * sym; /* Symbols */ int16_t m_minOff; /* Initial offset in stack frame*/ int16_t maxOff; /* Maximum offset in stack frame*/ int cb; /* Number of bytes in arguments */ int numArgs; /* No. of arguments in the table*/ void adjustForArgType(int numArg_, hlType actType_); - STKFRAME() : sym(0),m_minOff(0),maxOff(0),cb(0),numArgs(0) + STKFRAME() : m_minOff(0),maxOff(0),cb(0),numArgs(0) { } size_t getLocVar(int off); +public: + void updateFrameOff(int16_t off, int size, uint16_t duFlag); }; diff --git a/include/dcc.h b/include/dcc.h index 473106e..da3ace2 100644 --- a/include/dcc.h +++ b/include/dcc.h @@ -21,27 +21,6 @@ #include "Procedure.h" #include "BasicBlock.h" -typedef llvm::iplist FunctionListType; -typedef FunctionListType lFunction; -typedef lFunction::iterator ilFunction; - - -/* SYMBOL TABLE */ -struct SYM -{ - SYM() : label(0),size(0),flg(0),type(TYPE_UNKNOWN) - { - - } - char name[10]; /* New name for this variable */ - uint32_t label; /* physical address (20 bit) */ - int size; /* maximum size */ - uint32_t flg; /* SEG_IMMED, IMPURE, WORD_OFF */ - hlType type; /* probable type */ - eDuVal duVal; /* DEF, USE, VAL */ -}; - -typedef std::vector SYMTAB; /* CALL GRAPH NODE */ struct CALL_GRAPH { @@ -134,7 +113,6 @@ extern STATS stats; /* Icode statistics */ /**** Global function prototypes ****/ void FrontEnd(char *filename, CALL_GRAPH * *); /* frontend.c */ -void *allocMem(int cb); /* frontend.c */ void udm(void); /* udm.c */ void freeCFG(BB * cfg); /* graph.c */ @@ -167,7 +145,7 @@ hlType expType (const COND_EXPR *, Function *); /* Exported functions from hlicode.c */ -std::string writeCall (Function *, STKFRAME *, Function *, int *); +std::string writeCall (Function *, STKFRAME &, Function *, int *); char *writeJcond (const HLTYPE &, Function *, int *); char *writeJcondInv (HLTYPE, Function *, int *); diff --git a/include/locident.h b/include/locident.h index f161ab9..0673a3d 100644 --- a/include/locident.h +++ b/include/locident.h @@ -65,7 +65,7 @@ struct ID frameType loc; /* Frame location */ bool hasMacro; /* Identifier requires a macro */ char macro[10]; /* Macro for this identifier */ - char name[20]; /* Identifier's name */ + std::string name; /* Identifier's name */ union { /* Different types of identifiers */ eReg regi; /* For TYPE_BYTE(uint16_t)_(UN)SIGN registers */ struct { /* For TYPE_BYTE(uint16_t)_(UN)SIGN on the stack */ @@ -95,9 +95,9 @@ struct ID } void setLocalName(int i) { - // char buf[32]; - //sprintf (buf, "loc%ld", i); - //name=buf; + char buf[32]; + sprintf (buf, "loc%ld", i); + name=buf; } }; diff --git a/include/symtab.h b/include/symtab.h index adb2f7b..386723c 100644 --- a/include/symtab.h +++ b/include/symtab.h @@ -12,6 +12,76 @@ struct TypeContainer; /* * * * * * * * * * * * * * * * * */ /* Symbol table structs and protos */ /* * * * * * * * * * * * * * * * * */ +struct SymbolCommon +{ + std::string name; /* New name for this variable/symbol/argument */ + int size; /* Size/maximum size */ + hlType type; /* probable type */ + eDuVal duVal; /* DEF, USE, VAL */ + SymbolCommon() : size(0),type(TYPE_UNKNOWN) + {} +}; +struct SYM : public SymbolCommon +{ + SYM() : label(0),flg(0) + { + + } + int32_t label; /* physical address (20 bit) */ + uint32_t flg; /* SEG_IMMED, IMPURE, WORD_OFF */ +}; +/* STACK FRAME */ +struct STKSYM : public SymbolCommon +{ + COND_EXPR *actual; /* Expression tree of actual parameter */ + COND_EXPR *regs; /* For register arguments only */ + int16_t label; /* Immediate off from BP (+:args, -:params) */ + uint8_t regOff; /* Offset is a register (e.g. SI, DI) */ + bool hasMacro; /* This type needs a macro */ + std::string macro; /* Macro name */ + bool invalid; /* Boolean: invalid entry in formal arg list*/ + STKSYM() + { + actual=regs=0; + label=0; + regOff=0; + invalid=hasMacro = false; + } + void setArgName(int i) + { + char buf[32]; + sprintf (buf, "arg%ld", i); + name = buf; + } +}; +template +class SymbolTableCommon : public std::vector +{ +public: + typedef typename std::vector::iterator iterator; + typedef typename std::vector::const_iterator const_iterator; + iterator findByLabel(int lab) + { + auto iter = std::find_if(this->begin(),this->end(), + [lab](T &s)->bool {return s.label==lab;}); + return iter; + } + const_iterator findByLabel(int lab) const + { + auto iter = std::find_if(this->begin(),this->end(), + [lab](const T &s)->bool {return s.label==lab;}); + return iter; + } + +}; +/* SYMBOL TABLE */ +class SYMTAB : public SymbolTableCommon +{ + +public: + void updateSymType(uint32_t symbol, const TypeContainer &tc); + SYM *updateGlobSym(uint32_t operand, int size, uint16_t duFlag, bool &inserted_new); +}; struct Function; struct SYMTABLE { diff --git a/src/ast.cpp b/src/ast.cpp index 40725b9..de234ac 100644 --- a/src/ast.cpp +++ b/src/ast.cpp @@ -183,7 +183,7 @@ COND_EXPR *COND_EXPR::idLoc(int off, LOCAL_ID *localId) if (i == localId->csym()) printf ("Error, cannot find local var\n"); newExp->expr.ident.idNode.localIdx = i; - sprintf (localId->id_arr[i].name, "loc%ld", i); + localId->id_arr[i].setLocalName(i); return (newExp); } @@ -192,15 +192,13 @@ COND_EXPR *COND_EXPR::idLoc(int off, LOCAL_ID *localId) COND_EXPR *COND_EXPR::idParam(int off, const STKFRAME * argSymtab) { COND_EXPR *newExp; - size_t i; newExp = new COND_EXPR(IDENTIFIER); newExp->expr.ident.idType = PARAM; - for (i = 0; i < argSymtab->sym.size(); i++) - if (argSymtab->sym[i].off == off) - break; - if (i == argSymtab->sym.size()) printf ("Error, cannot find argument var\n"); - newExp->expr.ident.idNode.localIdx = i; + auto iter=argSymtab->findByLabel(off); + if (iter == argSymtab->end()) + printf ("Error, cannot find argument var\n"); + newExp->expr.ident.idNode.localIdx = distance(argSymtab->begin(),iter); return (newExp); } @@ -489,7 +487,7 @@ int hlTypeSize (const COND_EXPR *expr, Function * pproc) case LOCAL_VAR: return (hlSize[pproc->localId.id_arr[expr->expr.ident.idNode.localIdx].type]); case PARAM: - return (hlSize[pproc->args.sym[expr->expr.ident.idNode.paramIdx].type]); + return (hlSize[pproc->args[expr->expr.ident.idNode.paramIdx].type]); case GLOB_VAR_IDX: return (hlSize[pproc->localId.id_arr[expr->expr.ident.idNode.idxGlbIdx].type]); case CONSTANT: @@ -552,7 +550,7 @@ hlType expType (const COND_EXPR *expr, Function * pproc) case LOCAL_VAR: return (pproc->localId.id_arr[expr->expr.ident.idNode.localIdx].type); case PARAM: - return (pproc->args.sym[expr->expr.ident.idNode.paramIdx].type); + return (pproc->args[expr->expr.ident.idNode.paramIdx].type); case GLOB_VAR_IDX: return (pproc->localId.id_arr[expr->expr.ident.idNode.idxGlbIdx].type); case CONSTANT: @@ -708,7 +706,7 @@ string walkCondExpr (const COND_EXPR* expr, Function * pProc, int* numLoc) id = &pProc->localId.id_arr[expr->expr.ident.idNode.regiIdx]; if (id->name[0] == '\0') /* no name */ { - sprintf (id->name, "loc%ld", ++(*numLoc)); + id->setLocalName(++(*numLoc)); codeOut <type)<< " "<name<<"; "; codeOut <<"/* "<id.regi)<<" */\n"; } @@ -723,7 +721,7 @@ string walkCondExpr (const COND_EXPR* expr, Function * pProc, int* numLoc) break; case PARAM: - psym = &pProc->args.sym[expr->expr.ident.idNode.paramIdx]; + psym = &pProc->args[expr->expr.ident.idNode.paramIdx]; if (psym->hasMacro) o << psym->macro<<"("<name<< ")"; else @@ -752,12 +750,12 @@ string walkCondExpr (const COND_EXPR* expr, Function * pProc, int* numLoc) o << id->name; else if (id->loc == REG_FRAME) { - sprintf (id->name, "loc%ld", ++(*numLoc)); + id->setLocalName(++(*numLoc)); codeOut <type)<< " "<name<<"; "; codeOut <<"/* "<id.longId.h) << ":" << Machine_X86::regName(id->id.longId.l) << " */\n"; o << id->name; - pProc->localId.propLongId (id->id.longId.l,id->id.longId.h, id->name); + pProc->localId.propLongId (id->id.longId.l,id->id.longId.h, id->name.c_str()); } else /* GLB_FRAME */ { @@ -769,7 +767,7 @@ string walkCondExpr (const COND_EXPR* expr, Function * pProc, int* numLoc) break; case FUNCTION: - o << writeCall (expr->expr.ident.idNode.call.proc,expr->expr.ident.idNode.call.args, pProc, numLoc); + o << writeCall (expr->expr.ident.idNode.call.proc,*expr->expr.ident.idNode.call.args, pProc, numLoc); break; case OTHER: @@ -786,7 +784,6 @@ string walkCondExpr (const COND_EXPR* expr, Function * pProc, int* numLoc) break; } cCode.appendDecl(codeOut.str()); - return outStr.str(); } diff --git a/src/backend.cpp b/src/backend.cpp index 3e5fe18..af61aa1 100644 --- a/src/backend.cpp +++ b/src/backend.cpp @@ -94,35 +94,39 @@ char *cChar (uint8_t c) * Note: to get to the value of the variable: * com file: prog.Image[operand] * exe file: prog.Image[operand+0x100] */ +static void printGlobVar (std::ostream &ostr,SYM * psym); static void printGlobVar (SYM * psym) +{ + std::ostringstream ostr; + printGlobVar(ostr,psym); + cCode.appendDecl(ostr.str()); +} +static void printGlobVar (std::ostream &ostr,SYM * psym) { int j; uint32_t relocOp = prog.fCOM ? psym->label : psym->label + 0x100; switch (psym->size) { - case 1: cCode.appendDecl( "uint8_t\t%s = %ld;\n", - psym->name, prog.Image[relocOp]); + case 1: + ostr << "uint8_t\t"<name<<" = "<name, LH(prog.Image+relocOp)); + case 2: + ostr << "uint16_t\t"<name<<" = "<type == TYPE_PTR) /* pointer */ - cCode.appendDecl( "uint16_t\t*%s = %ld;\n", - psym->name, LH(prog.Image+relocOp)); + ostr << "uint16_t *\t"<name<<" = "<name, prog.Image[relocOp], - prog.Image[relocOp+1], prog.Image[relocOp+2], - prog.Image[relocOp+3]); + ostr << "char\t"<name<<"[4] = \""<< + prog.Image[relocOp]<size; j++) strContents << cChar(prog.Image[relocOp + j]); - cCode.appendDecl( "char\t*%s = \"%s\";\n", psym->name, strContents.str().c_str()); + ostr << "char\t*"<name<<" = \""<args.numArgs; i++) { - psym = &this->args.sym[i]; + psym = &this->args[i]; ostr << " * "<name<<" = "; if (psym->regs->expr.ident.idType == REGISTER) { diff --git a/src/dataflow.cpp b/src/dataflow.cpp index aec2e88..0ecb602 100644 --- a/src/dataflow.cpp +++ b/src/dataflow.cpp @@ -75,12 +75,8 @@ ExpStack g_exp_stk; * is in the stack frame provided. */ size_t STKFRAME::getLocVar(int off) { - size_t i; - - for (i = 0; i < sym.size(); i++) - if (sym[i].off == off) - break; - return (i); + auto iter=findByLabel(off); + return distance(begin(),iter); } @@ -686,11 +682,11 @@ static int processCArg (Function * pp, Function * pProc, ICODE * picode, int num if (pp->args.numArgs > 0) if (pp->flg & PROC_VARARG) { - if (numArgs < pp->args.sym.size()) - adjustActArgType (_exp, pp->args.sym[numArgs].type, pProc); + if (numArgs < pp->args.size()) + adjustActArgType (_exp, pp->args[numArgs].type, pProc); } else - adjustActArgType (_exp, pp->args.sym[numArgs].type, pProc); + adjustActArgType (_exp, pp->args[numArgs].type, pProc); } else /* user function */ { @@ -774,7 +770,7 @@ void Function::processHliCall(COND_EXPR *_exp, iICODE picode) if (pp->flg & PROC_ISLIB) /* library function */ { if (pp->args.numArgs > 0) - adjustActArgType(_exp, pp->args.sym[numArgs].type, this); + adjustActArgType(_exp, pp->args[numArgs].type, this); res = picode->newStkArg (_exp, (llIcode)picode->ll()->getOpcode(), this); } else /* user function */ diff --git a/src/hlicode.cpp b/src/hlicode.cpp index c41a8fa..eea08b5 100644 --- a/src/hlicode.cpp +++ b/src/hlicode.cpp @@ -487,20 +487,20 @@ COND_EXPR *COND_EXPR::inverse () const /* Returns the string that represents the procedure call of tproc (ie. with * actual parameters) */ -std::string writeCall (Function * tproc, STKFRAME * args, Function * pproc, int *numLoc) +std::string writeCall (Function * tproc, STKFRAME & args, Function * pproc, int *numLoc) { int i; /* counter of # arguments */ string condExp; - ostringstream s; - s<name<<" ("; - for (i = 0; i < args->sym.size(); i++) + ostringstream ostr; + ostr<name<<" ("; + for(const STKSYM &sym : args) { - s << walkCondExpr (args->sym[i].actual, pproc, numLoc); - if (i < (args->sym.size() - 1)) - s << ", "; + ostr << walkCondExpr (sym.actual, pproc, numLoc); + if((&sym)!=&(args.back())) + ostr << ", "; } - s << ")"; - return s.str(); + ostr << ")"; + return ostr.str(); } @@ -547,7 +547,7 @@ string AssignType::writeOut(Function *pProc, int *numLoc) string CallType::writeOut(Function *pProc, int *numLoc) { ostringstream ostr; - ostr << writeCall (proc, args, pProc,numLoc); + ostr << writeCall (proc, *args, pProc,numLoc); ostr << ";\n"; return ostr.str(); } diff --git a/src/idioms.cpp b/src/idioms.cpp index f3bd4a4..3743577 100644 --- a/src/idioms.cpp +++ b/src/idioms.cpp @@ -204,7 +204,7 @@ void Function::findIdioms() } /* Check if number of parameter bytes match their calling convention */ - if ((flg & PROC_HLL) && (!args.sym.empty())) + if ((flg & PROC_HLL) && (!args.empty())) { args.m_minOff += (flg & PROC_FAR ? 4 : 2); delta = args.maxOff - args.m_minOff; diff --git a/src/locident.cpp b/src/locident.cpp index 8d05d45..0399a72 100644 --- a/src/locident.cpp +++ b/src/locident.cpp @@ -420,28 +420,23 @@ eReg otherLongRegi (eReg regi, int idx, LOCAL_ID *locTbl) * long register identifier. */ void LOCAL_ID::propLongId (uint8_t regL, uint8_t regH, const char *name) { - int i; - ID *_id; - - for (i = 0; i < id_arr.size(); i++) + for (ID &rid : id_arr) { - _id = &id_arr[i]; - if (_id->typeBitsize()==16) + if (rid.typeBitsize()!=16) + continue; + if ( (rid.id.regi != regL) and (rid.id.regi != regH) ) + continue; + // otherwise at least 1 is ok + rid.name = name; + rid.hasMacro = true; + rid.illegal = true; + if (rid.id.regi == regL) { - if (_id->id.regi == regL) - { - strcpy (_id->name, name); - strcpy (_id->macro, "LO"); - _id->hasMacro = true; - _id->illegal = true; + strcpy (rid.macro, "LO"); } - else if (_id->id.regi == regH) + else // if (rid.id.regi == regH) { - strcpy (_id->name, name); - strcpy (_id->macro, "HI"); - _id->hasMacro = true; - _id->illegal = true; - } + strcpy (rid.macro, "HI"); } } } diff --git a/src/parser.cpp b/src/parser.cpp index a5f853b..b229fcf 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -14,7 +14,7 @@ using namespace std; //static void FollowCtrl (Function * pProc, CALL_GRAPH * pcallGraph, STATE * pstate); static boolT process_JMP (ICODE * pIcode, STATE * pstate, CALL_GRAPH * pcallGraph); static void setBits(int16_t type, uint32_t start, uint32_t len); -static SYM * updateGlobSym(uint32_t operand, int size, uint16_t duFlag); +//static SYM * updateGlobSym(uint32_t operand, int size, uint16_t duFlag,bool &created); static void process_MOV(LLInst &ll, STATE * pstate); static SYM * lookupAddr (LLOperand *pm, STATE * pstate, int size, uint16_t duFlag); void interactDis(Function * initProc, int ic); @@ -718,77 +718,32 @@ static void process_MOV(LLInst & ll, STATE * pstate) } -/* Type of the symbol according to the number of bytes it uses */ -static hlType cbType[] = {TYPE_UNKNOWN, TYPE_BYTE_UNSIGN, TYPE_WORD_SIGN, - TYPE_UNKNOWN, TYPE_LONG_SIGN}; - -/* Creates an entry in the global symbol table (symtab) if the variable - * is not there yet. If it is part of the symtab, the size of the variable - * is checked and updated if the old size was less than the new size (ie. - * the maximum size is always saved). */ -static SYM * updateGlobSym (uint32_t operand, int size, uint16_t duFlag) -{ - int i; - - /* Check for symbol in symbol table */ - for (i = 0; i < symtab.size(); i++) - if (symtab[i].label == operand) - { - if (symtab[i].size < size) - symtab[i].size = size; - break; - } - - /* New symbol, not in symbol table */ - if (i == symtab.size()) - { - SYM v; - sprintf (v.name, "var%05lX", operand); - v.label = operand; - v.size = size; - v.type = cbType[size]; - if (duFlag == eDuVal::USE) /* must already have init value */ - { - v.duVal.use =1; // USEVAL; - v.duVal.val =1; - } - else - { - v.duVal.setFlags(duFlag); - } - symtab.push_back(v); - } - return (&symtab[i]); -} - /* Updates the offset entry to the stack frame table (arguments), * and returns a pointer to such entry. */ -static void updateFrameOff (STKFRAME * ps, int16_t off, int size, uint16_t duFlag) +void STKFRAME::updateFrameOff ( int16_t off, int _size, uint16_t duFlag) { int i; /* Check for symbol in stack frame table */ - for (i = 0; i < ps->sym.size(); i++) + auto iter=findByLabel(off); + if(iter!=end()) { - if (ps->sym[i].off == off) + if (iter->size < _size) { - if (ps->sym[i].size < size) - { - ps->sym[i].size = size; + iter->size = _size; } - break; } - } - - /* New symbol, not in table */ - if (i == ps->sym.size()) + else { + char nm[16]; STKSYM new_sym; - sprintf (new_sym.name, "arg%ld", i); - new_sym.off = off; - new_sym.size = size; - new_sym.type = cbType[size]; + + sprintf (nm, "arg%ld", size()); + new_sym.name = nm; + new_sym.label= off; + new_sym.size = _size; + new_sym.type = TypeContainer::defaultTypeForSize(_size); if (duFlag == eDuVal::USE) /* must already have init value */ { new_sym.duVal.use=1; @@ -798,13 +753,13 @@ static void updateFrameOff (STKFRAME * ps, int16_t off, int size, uint16_t duFla { new_sym.duVal.setFlags(duFlag); } - ps->sym.push_back(new_sym); - ps->numArgs++; + push_back(new_sym); + this->numArgs++; } /* Save maximum argument offset */ - if ((uint32_t)ps->maxOff < (off + (uint32_t)size)) - ps->maxOff = off + (int16_t)size; + if ((uint32_t)this->maxOff < (off + (uint32_t)_size)) + this->maxOff = off + (int16_t)_size; } @@ -815,29 +770,26 @@ static void updateFrameOff (STKFRAME * ps, int16_t off, int size, uint16_t duFla static SYM * lookupAddr (LLOperand *pm, STATE *pstate, int size, uint16_t duFlag) { int i; - SYM * psym; + SYM * psym=nullptr; uint32_t operand; + bool created_new=false; + if (pm->regi != rUNDEF) + return nullptr; // register or indexed - if (pm->regi == 0) - { /* Global var */ - if (pm->segValue) { /* there is a value in the seg field */ + if (pm->segValue) /* there is a value in the seg field */ + { operand = opAdr (pm->segValue, pm->off); - psym = updateGlobSym (operand, size, duFlag); - - /* Check for out of bounds */ - if (psym->label >= (uint32_t)prog.cbImage) - return (NULL); - return (psym); + psym = symtab.updateGlobSym (operand, size, duFlag,created_new); } - else if (pstate->f[pm->seg]) { /* new value */ + else if (pstate->f[pm->seg]) /* new value */ + { pm->segValue = pstate->r[pm->seg]; operand = opAdr(pm->segValue, pm->off); - i = symtab.size(); - psym = updateGlobSym (operand, size, duFlag); + psym = symtab.updateGlobSym (operand, size, duFlag,created_new); /* Flag new memory locations that are segment values */ - if (symtab.size() > i) + if (created_new) { if (size == 4) operand += 2; /* High uint16_t */ @@ -847,14 +799,11 @@ static SYM * lookupAddr (LLOperand *pm, STATE *pstate, int size, uint16_t duFlag break; } } - - /* Check for out of bounds */ - if (psym->label >= (uint32_t)prog.cbImage) - return (NULL); - return (psym); } - } - return (NULL); + /* Check for out of bounds */ + if (psym && (psym->label>=0) and (psym->label < (uint32_t)prog.cbImage)) + return psym; + return nullptr; } @@ -944,7 +893,7 @@ static void use (opLoc d, ICODE & pIcode, Function * pProc, STATE * pstate, int if (pm->regi == INDEX_BP) /* indexed on bp */ { if (pm->off >= 2) - updateFrameOff (&pProc->args, pm->off, size, eDuVal::USE); + pProc->args.updateFrameOff ( pm->off, size, eDuVal::USE); else if (pm->off < 0) pProc->localId.newByteWordStk (TYPE_WORD_SIGN, pm->off, 0); } @@ -991,7 +940,7 @@ static void def (opLoc d, ICODE & pIcode, Function * pProc, STATE * pstate, int if (pm->regi == INDEX_BP) /* indexed on bp */ { if (pm->off >= 2) - updateFrameOff (&pProc->args, pm->off, size, eDEF); + pProc->args.updateFrameOff ( pm->off, size, eDEF); else if (pm->off < 0) pProc->localId.newByteWordStk (TYPE_WORD_SIGN, pm->off, 0); } @@ -1016,7 +965,7 @@ static void def (opLoc d, ICODE & pIcode, Function * pProc, STATE * pstate, int { setBits(BM_DATA, psym->label, (uint32_t)size); pIcode.ll()->setFlags(SYM_DEF); - pIcode.ll()->caseTbl.numEntries = psym - &symtab[0]; + pIcode.ll()->caseTbl.numEntries = distance(&symtab[0],psym); } } diff --git a/src/procs.cpp b/src/procs.cpp index 7f3b6eb..b2a40ec 100644 --- a/src/procs.cpp +++ b/src/procs.cpp @@ -132,7 +132,7 @@ void Function::newRegArg(iICODE picode, iICODE ticode) /* Check if register argument already on the formal argument list */ regExist = false; - for(STKSYM &tgt_sym : target_stackframe->sym) + for(STKSYM &tgt_sym : *target_stackframe) { if (type == REGISTER) { @@ -158,7 +158,9 @@ void Function::newRegArg(iICODE picode, iICODE ticode) if (regExist == false) { STKSYM newsym; - sprintf (newsym.name, "arg%ld", target_stackframe->sym.size()); + + newsym.setArgName(target_stackframe->size()); + if (type == REGISTER) { if (regL < rAL) @@ -171,23 +173,22 @@ void Function::newRegArg(iICODE picode, iICODE ticode) newsym.type = TYPE_BYTE_SIGN; newsym.regs = COND_EXPR::idRegIdx(tidx, BYTE_REG); } - sprintf (tproc->localId.id_arr[tidx].name, "arg%ld", target_stackframe->sym.size()); + tproc->localId.id_arr[tidx].name = newsym.name; } else if (type == LONG_VAR) { newsym.regs = COND_EXPR::idLongIdx (tidx); newsym.type = TYPE_LONG_SIGN; - sprintf (tproc->localId.id_arr[tidx].name, "arg%ld", target_stackframe->sym.size()); - tproc->localId.propLongId (regL, regH, - tproc->localId.id_arr[tidx].name); + tproc->localId.id_arr[tidx].name = newsym.name; + tproc->localId.propLongId (regL, regH, tproc->localId.id_arr[tidx].name.c_str()); } - target_stackframe->sym.push_back(newsym); + target_stackframe->push_back(newsym); target_stackframe->numArgs++; } /* Do ps (actual arguments) */ STKSYM newsym; - sprintf (newsym.name, "arg%ld", call_args_stackframe->sym.size()); + newsym.setArgName(call_args_stackframe->size()); newsym.actual = picode->hl()->asgn.rhs; newsym.regs = lhs; /* Mask off high and low register(s) in picode */ @@ -207,15 +208,15 @@ void Function::newRegArg(iICODE picode, iICODE ticode) newsym.type = TYPE_LONG_SIGN; break; } - call_args_stackframe->sym.push_back(newsym); + call_args_stackframe->push_back(newsym); call_args_stackframe->numArgs++; } /** Inserts the new expression (ie. the actual parameter) on the argument * list. - * @return TRUE if it was a near call that made use of a segment register. - * FALSE elsewhere + * @return true if it was a near call that made use of a segment register. + * false elsewhere */ bool CallType::newStkArg(COND_EXPR *exp, llIcode opcode, Function * pproc) { @@ -239,7 +240,7 @@ bool CallType::newStkArg(COND_EXPR *exp, llIcode opcode, Function * pproc) /* Place register argument on the argument list */ STKSYM newsym; newsym.actual = exp; - args->sym.push_back(newsym); + args->push_back(newsym); args->numArgs++; return false; } @@ -249,8 +250,8 @@ bool CallType::newStkArg(COND_EXPR *exp, llIcode opcode, Function * pproc) * argument list of picode. */ void CallType::placeStkArg (COND_EXPR *exp, int pos) { - args->sym[pos].actual = exp; - sprintf (args->sym[pos].name, "arg%ld", pos); + (*args)[pos].actual = exp; + (*args)[pos].setArgName(pos); } @@ -317,35 +318,32 @@ void STKFRAME::adjustForArgType(int numArg_, hlType actType_) hlType forType; STKSYM * psym, * nsym; int off, i; - - /* Find stack offset for this argument */ - off = m_minOff; - for (i = 0; i < numArg_; i++) - { - if(i>=sym.size()) - { - break; //TODO: verify - } - off += sym[i].size; - } - - /* Find formal argument */ - if (numArg_ < sym.size()) - { - psym = &sym[numArg_]; - i = numArg_; - //auto iter=std::find_if(sym.begin(),sym.end(),[off](STKSYM &s)->bool {s.off==off;}); - auto iter=std::find_if(sym.begin()+numArg_,sym.end(),[off](STKSYM &s)->bool {s.off==off;}); - if(iter==sym.end()) // symbol not found - return; - psym = &(*iter); - } /* If formal argument does not exist, do not create new ones, just * ignore actual argument */ - else + if(numArg_>size()) return; + /* Find stack offset for this argument */ + off = m_minOff; + i=0; + for(STKSYM &s : *this) // walk formal arguments upto numArg_ + { + if(i>=numArg_) + break; + off+=s.size; + i++; + } + + /* Find formal argument */ + //psym = &at(numArg_); + //i = numArg_; + //auto iter=std::find_if(sym.begin(),sym.end(),[off](STKSYM &s)->bool {s.off==off;}); + auto iter=std::find_if(begin()+numArg_,end(),[off](STKSYM &s)->bool {s.label==off;}); + if(iter==end()) // symbol not found + return; + psym = &(*iter); + forType = psym->type; if (forType != actType_) { @@ -364,11 +362,11 @@ void STKFRAME::adjustForArgType(int numArg_, hlType actType_) psym->type = actType_; psym->size = 4; nsym = psym + 1; - sprintf (nsym->macro, "HI"); - sprintf (psym->macro, "LO"); + nsym->macro = "HI"; + psym->macro = "LO"; nsym->hasMacro = true; psym->hasMacro = true; - sprintf (nsym->name, "%s", psym->name); + nsym->name = psym->name; nsym->invalid = true; numArgs--; } diff --git a/src/symtab.cpp b/src/symtab.cpp index 2dcf8e9..a1541af 100644 --- a/src/symtab.cpp +++ b/src/symtab.cpp @@ -137,3 +137,58 @@ boolT readVal(std::ostringstream &symName, uint32_t symOff, Function * symProc) { return false; // no symbolic names for now } + +/* Updates the type of the symbol in the symbol table. The size is updated + * if necessary (0 means no update necessary). */ +void SYMTAB::updateSymType (uint32_t symbol,const TypeContainer &tc) +{ + int i; + auto iter=findByLabel(symbol); + if(iter==end()) + return; + iter->type = tc.m_type; + if (tc.m_size != 0) + iter->size = tc.m_size; +} + +/* Creates an entry in the global symbol table (symtab) if the variable + * is not there yet. If it is part of the symtab, the size of the variable + * is checked and updated if the old size was less than the new size (ie. + * the maximum size is always saved). */ +//TODO: SYMTAB::updateGlobSym should be renamed to insertOrUpdateSym +SYM * SYMTAB::updateGlobSym (uint32_t operand, int size, uint16_t duFlag,bool &inserted_new) +{ + /* Check for symbol in symbol table */ + auto iter = findByLabel(operand); + if(iter!=end()) + { + if(iter->sizesize = size; + inserted_new=false; + return &(*iter); + } + + /* New symbol, not in symbol table */ + SYM v; + char buf[32]={}; + sprintf (buf, "var%05X", operand); + v.name = buf; + v.label = operand; + v.size = size; + v.type = TypeContainer::defaultTypeForSize(size); + if (duFlag == eDuVal::USE) /* must already have init value */ + { + v.duVal.use =1; // USEVAL; + v.duVal.val =1; + } + else + { + v.duVal.setFlags(duFlag); + } + push_back(v); + inserted_new=true; + return (&back()); +} + +//template<> class SymbolTableCommon; +//template<> class SymbolTableCommon;