Browse Source

slowly replacing ICODE * with iterators

Artur K 12 years ago
parent
commit
b3fa570792
10 changed files with 59 additions and 47 deletions
  1. 5 0
      include/BasicBlock.h
  2. 1 0
      include/icode.h
  3. 1 0
      include/state.h
  4. 10 0
      src/BasicBlock.cpp
  5. 12 14
      src/dataflow.cpp
  6. 10 10
      src/graph.cpp
  7. 2 4
      src/hlicode.cpp
  8. 1 0
      src/icode.cpp
  9. 8 8
      src/parser.cpp
  10. 9 11
      src/proplong.cpp

+ 5 - 0
include/BasicBlock.h

@@ -6,12 +6,14 @@
 #include <llvm/ADT/ilist_node.h>
 #include "types.h"
 #include "graph.h"
+#include "icode.h"
 /* Basic block (BB) node definition */
 struct Function;
 class CIcodeRec;
 struct BB;
 struct interval;
 struct ICODE;
+
 typedef union
 {
     dword         ip;             /* Out edge icode address       */
@@ -38,6 +40,8 @@ private:
 
 public:
     Int    begin();
+    iICODE begin2();
+    iICODE end2();
     Int	   end();
     Int    rbegin();
     Int    rend();
@@ -103,6 +107,7 @@ public:
     const Function *getParent() const { return Parent; }
     Function *getParent()       { return Parent; }
     void writeBB(ICODE *hli, Int lev, Function *pProc, Int *numLoc);
+    BB *rmJMP(Int marker, BB *pBB);
 private:
     Function *Parent;
 

+ 1 - 0
include/icode.h

@@ -372,3 +372,4 @@ public:
     boolT	labelSrch(dword target, Int *pIndex);
     ICODE *	GetIcode(int ip);
 };
+typedef CIcodeRec::iterator iICODE;

+ 1 - 0
include/state.h

@@ -4,6 +4,7 @@
  ****************************************************************************/
 
 /* STATE TABLE */
+#include <cstring>
 struct STATE
 {
     dword       IP;             /* Offset into Image                    */

+ 10 - 0
src/BasicBlock.cpp

@@ -354,6 +354,16 @@ int BB::begin()
 {
     return start;
 }
+
+iICODE BB::begin2()
+{
+    return Parent->Icode.begin()+start;
+}
+
+iICODE BB::end2()
+{
+    return Parent->Icode.begin()+start+length;
+}
 int BB::rbegin()
 {
     return start+length-1;

+ 12 - 14
src/dataflow.cpp

@@ -159,13 +159,12 @@ void Function::elimCondCodes ()
                     (Icode.GetLlOpcode(useAt-1) >= iJB) &&
                     (Icode.GetLlOpcode(useAt-1) <= iJNS))
                 {
-                    prev = Icode.GetIcode(pBB->inEdges[0]->start +
-                                                 pBB->inEdges[0]->length - 1);
-                    if (prev->ic.hl.opcode == HLI_JCOND)
+                    ICODE & prev(pBB->back());
+                    if (prev.ic.hl.opcode == HLI_JCOND)
                     {
-                        exp = prev->ic.hl.oper.exp->clone();
+                        exp = prev.ic.hl.oper.exp->clone();
                         exp->changeBoolOp (condOpJCond[Icode.GetLlOpcode(useAt-1)-iJB]);
-                        Icode[useAt-1].copyDU(*prev, eUSE, eUSE);
+                        Icode[useAt-1].copyDU(prev, eUSE, eUSE);
                         Icode[useAt-1].setJCond(exp);
                     }
                 }
@@ -187,9 +186,9 @@ void Function::elimCondCodes ()
  *       analysis (eg: push si, would include si in LiveUse; although it
  *       is not really meant to be a register that is used before defined). */
 void Function::genLiveKtes ()
-{ Int i, j;
+{
+    Int i;
     BB * pbb;
-    ICODE * picode;
     dword liveUse, def;
 
     for (i = 0; i < numBBs; i++)
@@ -198,13 +197,12 @@ void Function::genLiveKtes ()
         pbb = dfsLast[i];
         if (pbb->flg & INVALID_BB)
             continue;	/* skip invalid BBs */
-        for (j = pbb->start; j < (pbb->start + pbb->length); j++)
+        for (auto j = pbb->begin2(); j != pbb->end2(); j++)
         {
-            picode = Icode.GetIcode(j);
-            if ((picode->type == HIGH_LEVEL) && (picode->invalid == FALSE))
+            if ((j->type == HIGH_LEVEL) && (j->invalid == FALSE))
             {
-                liveUse |= (picode->du.use & ~def);
-                def |= picode->du.def;
+                liveUse |= (j->du.use & ~def);
+                def |= j->du.def;
             }
         }
         pbb->liveUse = liveUse;
@@ -257,8 +255,8 @@ void Function::liveRegAnalysis (dword in_liveOut)
                     picode = Icode.GetIcode(pbb->start + pbb->length - 1);
                     if (picode->ic.hl.opcode == HLI_RET)
                     {
-                        picode->ic.hl.oper.exp = COND_EXPR::idID (&retVal,
-                                                              &localId, pbb->start + pbb->length - 1);
+                        assert(pbb->back().loc_ip == pbb->start+pbb->length-1);
+                        picode->ic.hl.oper.exp = COND_EXPR::idID (&retVal, &localId, pbb->back().loc_ip);
                         picode->du.use = in_liveOut;
                     }
                 }

+ 10 - 10
src/graph.cpp

@@ -12,7 +12,7 @@
 #endif
 #include "graph.h"
 
-static BB *  rmJMP(Function * pProc, Int marker, BB * pBB);
+//static BB *  rmJMP(Function * pProc, Int marker, BB * pBB);
 static void mergeFallThrough(Function * pProc, BB * pBB);
 static void dfsNumbering(BB * pBB, std::vector<BB*> &dfsLast, Int *first, Int *last);
 
@@ -163,12 +163,12 @@ void Function::markImpure()
     {
         if (Icode.GetLlFlag(i) & (SYM_USE | SYM_DEF))
         {
-            psym = &symtab[Icode.GetIcode(i)->ic.ll.caseTbl.numEntries];
+            psym = &symtab[Icode[i].ic.ll.caseTbl.numEntries];
             for (int c = (Int)psym->label; c < (Int)psym->label+psym->size; c++)
             {
                 if (BITMAP(c, BM_CODE))
                 {
-                    Icode.SetLlFlag(i, IMPURE);
+                    Icode[i].SetLlFlag(IMPURE);
                     flg |= IMPURE;
                     break;
                 }
@@ -212,7 +212,7 @@ void Function::compressCFG()
         for (i = 0; i < pBB->edges.size(); i++)
         {
             ip   = pBB->rbegin();
-            pNxt = rmJMP(this, ip, pBB->edges[i].BBptr);
+            pNxt = pBB->edges[i].BBptr->rmJMP(ip, pBB->edges[i].BBptr);
 
             if (not pBB->edges.empty())   /* Might have been clobbered */
             {
@@ -240,7 +240,6 @@ void Function::compressCFG()
                 pBB->index = UN_INIT;
             else
             {
-                pBB->edges.clear();
                 delete pBB;
                 stats.numBBaft--;
             }
@@ -264,7 +263,7 @@ void Function::compressCFG()
 /****************************************************************************
  * rmJMP - If BB addressed is just a JMP it is replaced with its target
  ***************************************************************************/
-static BB * rmJMP(Function * pProc, Int marker, BB * pBB)
+BB *BB::rmJMP(Int marker, BB * pBB)
 {
     marker += DFS_JMP;
 
@@ -290,15 +289,16 @@ static BB * rmJMP(Function * pProc, Int marker, BB * pBB)
         {
             /* We are going around in circles */
             pBB->nodeType = NOWHERE_NODE;
-            pProc->Icode.GetIcode(pBB->start)->ic.ll.immed.op = (dword)pBB->start;
-            pProc->Icode.SetImmediateOp(pBB->start, (dword)pBB->start);
+            pBB->front().ic.ll.immed.op = (dword)pBB->start;
             do {
                 pBB = pBB->edges[0].BBptr;
                 pBB->inEdges.pop_back(); // was --numInedges
                 if (! pBB->inEdges.empty())
                 {
-                    pProc->Icode.SetLlFlag(pBB->start, NO_CODE);
-                    pProc->Icode.SetLlInvalid(pBB->start, TRUE);
+                    pBB->front().SetLlFlag(NO_CODE);
+                    pBB->front().invalidate();
+//                    pProc->Icode.SetLlFlag(pBB->start, NO_CODE);
+//                    pProc->Icode.SetLlInvalid(pBB->start, TRUE);
                 }
             } while (pBB->nodeType != NOWHERE_NODE);
 

+ 2 - 4
src/hlicode.cpp

@@ -183,8 +183,7 @@ void Function::highLevelGen()
                     break;
 
                 case iIMUL:   rhs = COND_EXPR::boolOp (lhs, rhs, MUL);
-                    lhs = COND_EXPR::id (pIcode, LHS_OP, this, i, pIcode,
-                                     NONE);
+                    lhs = COND_EXPR::id (pIcode, LHS_OP, this, i, pIcode, NONE);
                     pIcode->setAsgn(lhs, rhs);
                     break;
 
@@ -215,8 +214,7 @@ void Function::highLevelGen()
                     break;
 
                 case iMUL:    rhs = COND_EXPR::boolOp (lhs, rhs, MUL);
-                    lhs = COND_EXPR::id (pIcode, LHS_OP, this, i, pIcode,
-                                     NONE);
+                    lhs = COND_EXPR::id (pIcode, LHS_OP, this, i, pIcode, NONE);
                     pIcode->setAsgn(lhs, rhs);
                     break;
 

+ 1 - 0
src/icode.cpp

@@ -27,6 +27,7 @@ CIcodeRec::~CIcodeRec()
 ICODE * CIcodeRec::addIcode(ICODE *pIcode)
 {
     push_back(*pIcode);
+    back().loc_ip = size()-1;
     return &back();
 }
 

+ 8 - 8
src/parser.cpp

@@ -244,8 +244,8 @@ void Function::FollowCtrl(CALL_GRAPH * pcallGraph, STATE *pstate)
             case iJO:   case iJNO:      case iJP:   case iJNP:
             case iJCXZ:
             {   STATE   StCopy;
-                int     ip      = Icode.GetNumIcodes()-1;	/* Index of this jump */
-                ICODE *  prev   = Icode.GetIcode(ip-1); /* Previous icode */
+                int     ip      = Icode.size()-1;	/* Index of this jump */
+                ICODE  &prev(Icode.back()); /* Previous icode */
                 boolT   fBranch = FALSE;
 
                 pstate->JCond.regi = 0;
@@ -253,13 +253,13 @@ void Function::FollowCtrl(CALL_GRAPH * pcallGraph, STATE *pstate)
                 /* This sets up range check for indexed JMPs hopefully
              * Handles JA/JAE for fall through and JB/JBE on branch
             */
-                if (ip > 0 && prev->ic.ll.opcode == iCMP && (prev->ic.ll.flg & I))
+                if (ip > 0 && prev.ic.ll.opcode == iCMP && (prev.ic.ll.flg & I))
                 {
-                    pstate->JCond.immed = (int16)prev->ic.ll.immed.op;
+                    pstate->JCond.immed = (int16)prev.ic.ll.immed.op;
                     if (_Icode.ic.ll.opcode == iJA || _Icode.ic.ll.opcode == iJBE)
                         pstate->JCond.immed++;
                     if (_Icode.ic.ll.opcode == iJAE || _Icode.ic.ll.opcode == iJA)
-                        pstate->JCond.regi = prev->ic.ll.dst.regi;
+                        pstate->JCond.regi = prev.ic.ll.dst.regi;
                     fBranch = (boolT)
                             (_Icode.ic.ll.opcode == iJB || _Icode.ic.ll.opcode == iJBE);
                 }
@@ -271,7 +271,7 @@ void Function::FollowCtrl(CALL_GRAPH * pcallGraph, STATE *pstate)
 
                 if (fBranch)                /* Do branching code */
                 {
-                    pstate->JCond.regi = prev->ic.ll.dst.regi;
+                    pstate->JCond.regi = prev.ic.ll.dst.regi;
                 }
                 /* Next icode. Note: not the same as GetLastIcode() because of the call
                                 to FollowCtrl() */
@@ -687,8 +687,8 @@ static hlType cbType[] = {TYPE_UNKNOWN, TYPE_BYTE_UNSIGN, TYPE_WORD_SIGN,
  * is checked and updated if the old size was less than the new size (ie.
  * the maximum size is always saved).   */
 static SYM * updateGlobSym (dword operand, Int size, word duFlag)
-{	
-		Int i;
+{
+        Int i;
 
         /* Check for symbol in symbol table */
     for (i = 0; i < symtab.size(); i++)

+ 9 - 11
src/proplong.cpp

@@ -22,7 +22,7 @@ static boolT isJCond (llIcode opcode)
 
 
 /* Returns whether the conditions for a 2-3 long variable are satisfied */
-static boolT isLong23 (Int i, BB * pbb, ICODE * icode, Int *off, Int *arc)
+static boolT isLong23 (Int i, BB * pbb, Int *off, Int *arc)
 {
  BB * t, * e, * obb2;
 
@@ -36,7 +36,7 @@ static boolT isLong23 (Int i, BB * pbb, ICODE * icode, Int *off, Int *arc)
     {
         obb2 = t->edges[THEN].BBptr;
         if ((obb2->length == 2) && (obb2->nodeType == TWO_BRANCH) &&
-                (icode[obb2->start].ic.ll.opcode == iCMP))
+                (obb2->front().ic.ll.opcode == iCMP))
         {
             *off = obb2->start - i;
             *arc = THEN;
@@ -50,7 +50,7 @@ static boolT isLong23 (Int i, BB * pbb, ICODE * icode, Int *off, Int *arc)
     {
         obb2 = e->edges[THEN].BBptr;
         if ((obb2->length == 2) && (obb2->nodeType == TWO_BRANCH) &&
-                (icode[obb2->start].ic.ll.opcode == iCMP))
+                (obb2->front().ic.ll.opcode == iCMP))
         {
             *off = obb2->start - i;
             *arc = ELSE;
@@ -156,17 +156,16 @@ static void longJCond23 (COND_EXPR *rhs, COND_EXPR *lhs, ICODE * pIcode,
     stats.numBBaft -= 2;
 
     pIcode->invalidate();
-    pProc->Icode.GetIcode(obb1->start)->invalidate();
-    pProc->Icode.GetIcode(obb2->start)->invalidate();
-    pProc->Icode.GetIcode(obb2->start+1)->invalidate();
+    obb1->front().invalidate();
+    obb2->front().invalidate();
+    (obb2->begin2()+1)->invalidate();
 }
 
 
 /* Creates a long conditional equality or inequality at (pIcode+1).
  * Removes excess nodes from the graph by flagging them, and updates
  * the new edges for the remaining nodes.	*/
-static void longJCond22 (COND_EXPR *rhs, COND_EXPR *lhs, ICODE * pIcode,
-                         Int *idx)
+static void longJCond22 (COND_EXPR *rhs, COND_EXPR *lhs, ICODE * pIcode, Int *idx)
 {
     Int j;
     BB * pbb, * obb1, * tbb;
@@ -283,7 +282,7 @@ void Function::propLongStk (Int i, ID *pLocId)
         }
 
         /* Check long conditional (i.e. 2 CMPs and 3 branches */
-        else if ((pIcode->ic.ll.opcode == iCMP) && (isLong23 (idx, pIcode->inBB, this->Icode.GetFirstIcode(),&off, &arc)))
+        else if ((pIcode->ic.ll.opcode == iCMP) && (isLong23 (idx, pIcode->inBB, &off, &arc)))
         {
             if (checkLongEq (pLocId->id.longStkId, pIcode, i, idx, this, &rhs, &lhs, off) == TRUE)
                 longJCond23 (rhs, lhs, pIcode, &idx, this, arc, off);
@@ -448,8 +447,7 @@ void Function::propLongReg (Int i, ID *pLocId)
 
                 /* Check long conditional (i.e. 2 CMPs and 3 branches */
                 else if ((pIcode->ic.ll.opcode == iCMP) &&
-                         (isLong23 (idx, pIcode->inBB, this->Icode.GetFirstIcode(),
-                                    &off, &arc)))
+                         (isLong23 (idx, pIcode->inBB, &off, &arc)))
                 {
                     if (checkLongRegEq (pLocId->id.longId, pIcode, i, idx, this,
                                         &rhs, &lhs, off) == TRUE)