From fc0d35cf06dbfb30b435a25971646ccf90cd8ed0 Mon Sep 17 00:00:00 2001 From: Artur K Date: Wed, 7 Mar 2012 22:34:36 +0100 Subject: [PATCH] More eReg's and fixed writeBitVector for registers --- include/dcc.h | 4 ++++ include/icode.h | 15 ++++----------- src/ast.cpp | 14 +++++++++----- src/backend.cpp | 37 +++++++++++++------------------------ src/dataflow.cpp | 26 +++++++++----------------- src/parser.cpp | 8 ++++++++ src/scanner.cpp | 2 +- 7 files changed, 48 insertions(+), 58 deletions(-) diff --git a/include/dcc.h b/include/dcc.h index 0fef78e..d3f3330 100644 --- a/include/dcc.h +++ b/include/dcc.h @@ -175,3 +175,7 @@ int power2 (int); boolT checkLongEq (LONG_STKID_TYPE, iICODE, int, Function *, Assignment &asgn, iICODE atOffset); boolT checkLongRegEq (LONGID_TYPE, iICODE, int, Function *, Assignment &asgn, iICODE); eReg otherLongRegi(eReg, int, LOCAL_ID *); + + +extern eReg subRegH(eReg reg); +extern eReg subRegL(eReg reg); diff --git a/include/icode.h b/include/icode.h index f184088..4a3f01a 100644 --- a/include/icode.h +++ b/include/icode.h @@ -145,6 +145,7 @@ public: opcode=l.opcode; asgn=l.asgn; call=l.call; + return *this; } public: std::string write1HlIcode(Function *pProc, int *numLoc); @@ -153,9 +154,9 @@ public: /* LOW_LEVEL icode operand record */ struct LLOperand //: public llvm::MCOperand { - uint8_t seg; /* CS, DS, ES, SS */ + eReg seg; /* CS, DS, ES, SS */ int16_t segValue; /* Value of segment seg during analysis */ - uint8_t segOver; /* CS, DS, ES, SS if segment override */ + eReg segOver; /* CS, DS, ES, SS if segment override */ eReg regi; /* 0 < regs < INDEXBASE <= index modes */ int16_t off; /* memory address offset */ uint32_t opz; /* idx of immed src op */ @@ -333,7 +334,7 @@ public: void hl(const HLTYPE &v) { m_hl=v;} int loc_ip; // used by CICodeRec to number ICODEs - void setRegDU(uint8_t regi, operDu du_in); + void setRegDU(eReg regi, operDu du_in); void invalidate(); void newCallHl(); void writeDU(int idx); @@ -372,11 +373,3 @@ public: iterator labelSrch(uint32_t target); ICODE * GetIcode(int ip); }; -constexpr eReg subRegH(eReg reg) -{ - return eReg((int)reg + (int)rAH-(int)rAX); -} -constexpr eReg subRegL(eReg reg) -{ - return eReg((int)reg + (int)rAL-(int)rAX); -} diff --git a/src/ast.cpp b/src/ast.cpp index e445c4c..3caf2b5 100644 --- a/src/ast.cpp +++ b/src/ast.cpp @@ -40,7 +40,7 @@ static const char *hexStr (uint16_t i) /* Sets the du record for registers according to the du flag */ -void ICODE::setRegDU (uint8_t regi, operDu du_in) +void ICODE::setRegDU (eReg regi, operDu du_in) { // printf("%s %d %x\n",__FUNCTION__,regi,int(du_in)); switch (du_in) @@ -402,15 +402,19 @@ COND_EXPR *COND_EXPR::id(const LLInst &ll_insn, opLoc sd, Function * pProc, iICO if ((pm.seg == rDS) && (pm.regi > INDEXBASE + 3)) /* dereference */ { switch (pm.regi) { - case INDEXBASE + 4: newExp = COND_EXPR::idReg(rSI, 0, &pProc->localId); + case INDEXBASE + 4: + newExp = COND_EXPR::idReg(rSI, 0, &pProc->localId); duIcode.setRegDU( rSI, du); break; - case INDEXBASE + 5: newExp = COND_EXPR::idReg(rDI, 0, &pProc->localId); + case INDEXBASE + 5: + newExp = COND_EXPR::idReg(rDI, 0, &pProc->localId); duIcode.setRegDU( rDI, du); break; - case INDEXBASE + 6: newExp = COND_EXPR::idReg(rBP, 0, &pProc->localId); + case INDEXBASE + 6: + newExp = COND_EXPR::idReg(rBP, 0, &pProc->localId); break; - case INDEXBASE + 7: newExp = COND_EXPR::idReg(rBX, 0, &pProc->localId); + case INDEXBASE + 7: + newExp = COND_EXPR::idReg(rBX, 0, &pProc->localId); duIcode.setRegDU( rBX, du); break; default: diff --git a/src/backend.cpp b/src/backend.cpp index 4680ced..55ec74e 100644 --- a/src/backend.cpp +++ b/src/backend.cpp @@ -9,6 +9,7 @@ #include "dcc.h" #include +#include #include #include @@ -108,9 +109,9 @@ static void printGlobVar (SYM * psym) { int j; uint32_t relocOp = prog.fCOM ? psym->label : psym->label + 0x100; - char *strContents; /* initial contents of variable */ - switch (psym->size) { + switch (psym->size) + { case 1: cCode.appendDecl( "uint8_t\t%s = %ld;\n", psym->name, prog.Image[relocOp]); break; @@ -128,13 +129,12 @@ static void printGlobVar (SYM * psym) prog.Image[relocOp+3]); break; default: - strContents = (char *)malloc((psym->size*2+1) *sizeof(char)); - strContents[0] = '\0'; + { + ostringstream strContents; for (j=0; j < psym->size; j++) - strcat (strContents, cChar(prog.Image[relocOp + j])); - cCode.appendDecl( "char\t*%s = \"%s\";\n", - psym->name, strContents); - free(strContents); + strContents << cChar(prog.Image[relocOp + j]); + cCode.appendDecl( "char\t*%s = \"%s\";\n", psym->name, strContents.str().c_str()); + } } } @@ -178,7 +178,7 @@ static void writeGlobSymTable() /* Writes the header information and global variables to the output C file * fp. */ -static void writeHeader (std::ostream &ios, char *fileName) +static void writeHeader (std::ostream &_ios, char *fileName) { /* Write header information */ newBundle (&cCode); @@ -189,34 +189,23 @@ static void writeHeader (std::ostream &ios, char *fileName) /* Write global symbol table */ /** writeGlobSymTable(); *** need to change them into locident fmt ***/ - writeBundle (ios, cCode); + writeBundle (_ios, cCode); freeBundle (&cCode); } /* Writes the registers that are set in the bitvector */ -static void writeBitVector (uint32_t regi) +static void writeBitVector (const std::bitset<32> ®i) { int j; - for (j = 0; j < INDEXBASE; j++) - { - if ((regi & power2(j)) != 0) - printf ("%s ", allRegs[j]); - } -} -static void writeBitVector (const std::bitset<32> ®i) -{ int j; - - for (j = 0; j < INDEXBASE; j++) + for (j = rAX; j < INDEXBASE; j++) { if (regi.test(j)) - printf ("%s ", allRegs[j]); + printf ("%s ", allRegs[j-1]); } } - - // Note: Not currently called! /* Checks the given icode to determine whether it has a label associated * to it. If so, a goto is emitted to this label; otherwise, a new label diff --git a/src/dataflow.cpp b/src/dataflow.cpp index 3cc70c5..e1abde8 100644 --- a/src/dataflow.cpp +++ b/src/dataflow.cpp @@ -909,37 +909,29 @@ void Function::findExps() case HLI_CALL: ticode = picode->du1.idx[0].uses.front(); - switch (ticode->hl()->opcode) { + HLTYPE *ti_hl(ticode->hl()); + retVal = &picode->hl()->call.proc->retVal; + switch (ti_hl->opcode) { case HLI_ASSIGN: - exp = COND_EXPR::idFunc ( - picode->hl()->call.proc, - picode->hl()->call.args); - res = COND_EXPR::insertSubTreeReg (ticode->hl()->asgn.rhs, - exp, - picode->hl()->call.proc->retVal.id.regi, - &localId); + assert(ti_hl->asgn.rhs); + exp = COND_EXPR::idFunc ( picode->hl()->call.proc, picode->hl()->call.args); + res = COND_EXPR::insertSubTreeReg (ti_hl->asgn.rhs,exp, retVal->id.regi, &localId); if (! res) - COND_EXPR::insertSubTreeReg (ticode->hl()->asgn.lhs, - exp, - picode->hl()->call.proc->retVal.id.regi, - &localId); + COND_EXPR::insertSubTreeReg (ti_hl->asgn.lhs, exp,retVal->id.regi, &localId); /*** TODO: HERE missing: 2 regs ****/ picode->invalidate(); numHlIcodes--; break; case HLI_PUSH: case HLI_RET: - ticode->hl()->expr( COND_EXPR::idFunc ( picode->hl()->call.proc, picode->hl()->call.args) ); + ti_hl->expr( COND_EXPR::idFunc ( picode->hl()->call.proc, picode->hl()->call.args) ); picode->invalidate(); numHlIcodes--; break; case HLI_JCOND: exp = COND_EXPR::idFunc ( picode->hl()->call.proc, picode->hl()->call.args); - retVal = &picode->hl()->call.proc->retVal, - res = COND_EXPR::insertSubTreeReg (ticode->hl()->exp.v, - exp, - retVal->id.regi, &localId); + res = COND_EXPR::insertSubTreeReg (ti_hl->exp.v, exp, retVal->id.regi, &localId); if (res) /* was substituted */ { picode->invalidate(); diff --git a/src/parser.cpp b/src/parser.cpp index 7da5684..3f3d701 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -20,6 +20,14 @@ static SYM * lookupAddr (LLOperand *pm, STATE * pstate, int size, uint16_t d void interactDis(Function * initProc, int ic); static uint32_t SynthLab; +/*constexpr */eReg subRegH(eReg reg) +{ + return eReg((int)reg + (int)rAH-(int)rAX); +} +/*constexpr */eReg subRegL(eReg reg) +{ + return eReg((int)reg + (int)rAL-(int)rAX); +} /* Parses the program, builds the call graph, and returns the list of * procedures found */ diff --git a/src/scanner.cpp b/src/scanner.cpp index 6983224..dbd2a0d 100644 --- a/src/scanner.cpp +++ b/src/scanner.cpp @@ -418,7 +418,7 @@ static void setAddress(int i, boolT fdst, uint16_t seg, int16_t reg, uint16_t of * provide the value of this segment in the field segValue. */ if (seg) /* segment override */ { - pm->seg = pm->segOver = (uint8_t)seg; + pm->seg = pm->segOver = (eReg)seg; } else { /* no override, check indexed register */