liveIn/Out def/use changed to use bitset<32>

This commit is contained in:
Artur K 2012-02-26 17:47:25 +01:00
parent 15deb26f2b
commit 74c5966579
23 changed files with 415 additions and 401 deletions

View File

@ -1,6 +1,6 @@
#!/bin/sh #!/bin/sh
lcov -z -d bld/CMakeFiles/dcc_original.dir/src lcov -z -d bld/CMakeFiles/dcc_original.dir/src
./regression_tester.rb bld/dcc_original -a2 -m -s -c -V ./regression_tester.rb bld/dcc_original -a2 -m -s -c -V 2>stderr >stdout
lcov -c -d bld/CMakeFiles/dcc_original.dir/src -o cover1.info lcov -c -d bld/CMakeFiles/dcc_original.dir/src -o cover1.info
lcov -e cover1.info *dcc_* -o cover2.info lcov -e cover1.info *dcc_* -o cover2.info
genhtml -o coverage -f --demangle-cpp cover2.info genhtml -o coverage -f --demangle-cpp cover2.info

View File

@ -75,10 +75,10 @@ public:
/* For live register analysis /* For live register analysis
* LiveIn(b) = LiveUse(b) U (LiveOut(b) - Def(b)) */ * LiveIn(b) = LiveUse(b) U (LiveOut(b) - Def(b)) */
dword liveUse; /* LiveUse(b) */ std::bitset<32> liveUse; /* LiveUse(b) */
dword def; /* Def(b) */ std::bitset<32> def; /* Def(b) */
dword liveIn; /* LiveIn(b) */ std::bitset<32> liveIn; /* LiveIn(b) */
dword liveOut; /* LiveOut(b) */ std::bitset<32> liveOut; /* LiveOut(b) */
/* For structuring analysis */ /* For structuring analysis */
Int dfsFirstNum; /* DFS #: first visit of node */ Int dfsFirstNum; /* DFS #: first visit of node */
@ -97,8 +97,8 @@ public:
Int caseTail; /* tail node for the case */ Int caseTail; /* tail node for the case */
Int index; /* Index, used in several ways */ Int index; /* Index, used in several ways */
static BB *Create(void *ctx=0,const std::string &s="",Function *parent=0,BB *insertBefore=0); static BB * Create(void *ctx=0,const std::string &s="",Function *parent=0,BB *insertBefore=0);
static BB *Create(Int start, Int ip, byte nodeType, Int numOutEdges, Function * parent); static BB * Create(Int start, Int ip, byte nodeType, Int numOutEdges, Function * parent);
void writeCode(Int indLevel, Function *pProc, Int *numLoc, Int latchNode, Int ifFollow); void writeCode(Int indLevel, Function *pProc, Int *numLoc, Int latchNode, Int ifFollow);
void mergeFallThrough(CIcodeRec &Icode); void mergeFallThrough(CIcodeRec &Icode);
void dfsNumbering(std::vector<BB *> &dfsLast, Int *first, Int *last); void dfsNumbering(std::vector<BB *> &dfsLast, Int *first, Int *last);

View File

@ -1,6 +1,7 @@
#pragma once #pragma once
#include <llvm/ADT/ilist.h> #include <llvm/ADT/ilist.h>
#include <llvm/ADT/ilist_node.h> #include <llvm/ADT/ilist_node.h>
#include <bitset>
#include "BasicBlock.h" #include "BasicBlock.h"
#include "types.h" #include "types.h"
#include "ast.h" #include "ast.h"
@ -68,8 +69,8 @@ public:
/* Icodes and control flow graph */ /* Icodes and control flow graph */
CIcodeRec Icode; /* Object with ICODE records */ CIcodeRec Icode; /* Object with ICODE records */
std::vector<BB*> cfg; /* Ptr. to BB list/CFG */ std::vector<BB*> m_cfg; /* Ptr. to BB list/CFG */
std::vector<BB*> dfsLast; std::vector<BB*> m_dfsLast;
std::vector<BB*> heldBBs; std::vector<BB*> heldBBs;
//BB * *dfsLast; /* Array of pointers to BBs in dfsLast //BB * *dfsLast; /* Array of pointers to BBs in dfsLast
// * (reverse postorder) order */ // * (reverse postorder) order */
@ -77,11 +78,11 @@ public:
boolT hasCase; /* Procedure has a case node */ boolT hasCase; /* Procedure has a case node */
/* For interprocedural live analysis */ /* For interprocedural live analysis */
dword liveIn; /* Registers used before defined */ std::bitset<32> liveIn; /* Registers used before defined */
dword liveOut; /* Registers that may be used in successors */ std::bitset<32> liveOut; /* Registers that may be used in successors */
boolT liveAnal; /* Procedure has been analysed already */ bool liveAnal; /* Procedure has been analysed already */
Function(void *ty=0) : procEntry(0),depth(0),flg(0),cbParam(0),cfg(0),dfsLast(0),numBBs(0), Function(void *ty=0) : procEntry(0),depth(0),flg(0),cbParam(0),m_cfg(0),m_dfsLast(0),numBBs(0),
hasCase(false),liveIn(0),liveOut(0),liveAnal(0)//,next(0),prev(0) hasCase(false),liveIn(0),liveOut(0),liveAnal(0)//,next(0),prev(0)
{ {
} }
@ -96,7 +97,7 @@ public:
void writeProcComments(); void writeProcComments();
void lowLevelAnalysis(); void lowLevelAnalysis();
void bindIcodeOff(); void bindIcodeOff();
void dataFlow(dword liveOut); void dataFlow(std::bitset<32> &liveOut);
void compressCFG(); void compressCFG();
void highLevelGen(); void highLevelGen();
void structure(derSeq *derivedG); void structure(derSeq *derivedG);
@ -120,17 +121,17 @@ public:
void newRegArg(ICODE *picode, ICODE *ticode); void newRegArg(ICODE *picode, ICODE *ticode);
protected: protected:
// TODO: replace those with friend visitor ? // TODO: replace those with friend visitor ?
void propLongReg(Int loc_ident_idx, const ID *pLocId); void propLongReg(Int loc_ident_idx, const ID &pLocId);
void propLongStk(Int i, ID *pLocId); void propLongStk(Int i, const ID &pLocId);
void propLongGlb(Int i, ID *pLocId); void propLongGlb(Int i, const ID &pLocId);
int checkBackwarLongDefs(int loc_ident_idx, const ID &pLocId, iICODE iter, Assignment &assign); int findBackwarLongDefs(int loc_ident_idx, const ID &pLocId, iICODE iter);
int checkForwardLongDefs(int loc_ident_idx, const ID &pLocId, iICODE beg, Assignment &asgn); int findForwardLongUses(int loc_ident_idx, const ID &pLocId, iICODE beg);
void structCases(); void structCases();
void findExps(); void findExps();
void genDU1(); void genDU1();
void elimCondCodes(); void elimCondCodes();
void liveRegAnalysis(dword in_liveOut); void liveRegAnalysis(std::bitset<32> &in_liveOut);
void findIdioms(); void findIdioms();
void propLong(); void propLong();
void genLiveKtes(); void genLiveKtes();

View File

@ -31,12 +31,12 @@ struct STKFRAME
{ {
std::vector<STKSYM> sym; std::vector<STKSYM> sym;
//STKSYM * sym; /* Symbols */ //STKSYM * sym; /* Symbols */
int16 minOff; /* Initial offset in stack frame*/ int16 m_minOff; /* Initial offset in stack frame*/
int16 maxOff; /* Maximum offset in stack frame*/ int16 maxOff; /* Maximum offset in stack frame*/
Int cb; /* Number of bytes in arguments */ Int cb; /* Number of bytes in arguments */
Int numArgs; /* No. of arguments in the table*/ Int numArgs; /* No. of arguments in the table*/
void adjustForArgType(Int numArg_, hlType actType_); void adjustForArgType(Int numArg_, hlType actType_);
STKFRAME() : sym(0),minOff(0),maxOff(0),cb(0),numArgs(0) STKFRAME() : sym(0),m_minOff(0),maxOff(0),cb(0),numArgs(0)
{ {
} }

View File

@ -124,7 +124,7 @@ struct PROG /* Loaded program image parameters */
}; };
extern PROG prog; /* Loaded program image parameters */ extern PROG prog; /* Loaded program image parameters */
extern dword duReg[30]; /* def/use bits for registers */ extern std::bitset<32> duReg[30]; /* def/use bits for registers */
//extern dword duReg[30]; /* def/use bits for registers */ //extern dword duReg[30]; /* def/use bits for registers */
extern dword maskDuReg[30]; /* masks off du bits for regs */ extern dword maskDuReg[30]; /* masks off du bits for regs */
@ -204,7 +204,7 @@ Int power2 (Int);
void inverseCondOp (COND_EXPR **); void inverseCondOp (COND_EXPR **);
/* Exported funcions from locident.c */ /* Exported funcions from locident.c */
boolT checkLongEq (LONG_STKID_TYPE, iICODE, Int, Function *, COND_EXPR **, COND_EXPR **, Int); boolT checkLongEq (LONG_STKID_TYPE, iICODE, Int, Function *, Assignment &asgn, Int);
boolT checkLongRegEq (LONGID_TYPE, iICODE, Int, Function *, COND_EXPR *&, COND_EXPR *&, Int); boolT checkLongRegEq (LONGID_TYPE, iICODE, Int, Function *, COND_EXPR *&, COND_EXPR *&, Int);
byte otherLongRegi (byte, Int, LOCAL_ID *); byte otherLongRegi (byte, Int, LOCAL_ID *);
void insertIdx (IDX_ARRAY *, Int); void insertIdx (IDX_ARRAY *, Int);

View File

@ -4,6 +4,7 @@
****************************************************************************/ ****************************************************************************/
#pragma once #pragma once
#include <vector> #include <vector>
#include <bitset>
#include <llvm/MC/MCInst.h> #include <llvm/MC/MCInst.h>
#include <llvm/MC/MCAsmInfo.h> #include <llvm/MC/MCAsmInfo.h>
#include "Enums.h" #include "Enums.h"
@ -245,9 +246,12 @@ struct DU
/* Def/Use of registers and stack variables */ /* Def/Use of registers and stack variables */
struct DU_ICODE struct DU_ICODE
{ {
dword def; /* For Registers: position in dword is reg index*/ std::bitset<32> def; // For Registers: position in bitset is reg index
dword lastDefRegi;/* Bit set if last def of this register in BB */ //dword def; // For Registers: position in dword is reg index
dword use; /* For Registers: position in dword is reg index*/ //dword def; // For Registers: position in dword is reg index
//dword lastDefRegi; // Bit set if last def of this register in BB
std::bitset<32> use; // For Registers: position in dword is reg index
std::bitset<32> lastDefRegi;
}; };
@ -322,7 +326,7 @@ struct ICODE
Int idx[MAX_REGS_DEF][MAX_USES]; /* inst that uses this def */ Int idx[MAX_REGS_DEF][MAX_USES]; /* inst that uses this def */
}; };
icodeType type; /* Icode type */ icodeType type; /* Icode type */
boolT invalid; /* Has no HIGH_LEVEL equivalent */ bool invalid; /* Has no HIGH_LEVEL equivalent */
BB *inBB; /* BB to which this icode belongs */ BB *inBB; /* BB to which this icode belongs */
DU_ICODE du; /* Def/use regs/vars */ DU_ICODE du; /* Def/use regs/vars */
DU1 du1; /* du chain 1 */ DU1 du1; /* du chain 1 */

View File

@ -109,6 +109,16 @@ struct ID
type=t; type=t;
loc=f; loc=f;
} }
bool isSigned() const { return (type==TYPE_BYTE_SIGN)||(type==TYPE_WORD_SIGN)||(type==TYPE_LONG_SIGN);}
uint16_t typeBitsize() const
{
switch(type)
{
case TYPE_WORD_SIGN: case TYPE_WORD_UNSIGN:
return 16;
}
return ~0;
}
}; };
struct LOCAL_ID struct LOCAL_ID

View File

@ -15,7 +15,7 @@ BB *BB::Create(void *ctx, const string &s, Function *parent, BB *insertBefore)
BB *BB::Create(Int start, Int ip, byte nodeType, Int numOutEdges, Function *parent) BB *BB::Create(Int start, Int ip, byte nodeType, Int numOutEdges, Function *parent)
{ {
parent->cfg; parent->m_cfg;
BB* pnewBB; BB* pnewBB;
pnewBB = new BB; pnewBB = new BB;
@ -36,7 +36,7 @@ BB *BB::Create(Int start, Int ip, byte nodeType, Int numOutEdges, Function *pare
if (start >= 0) if (start >= 0)
parent->Icode.SetInBB(start, ip, pnewBB); parent->Icode.SetInBB(start, ip, pnewBB);
parent->heldBBs.push_back(pnewBB); parent->heldBBs.push_back(pnewBB);
parent->cfg.push_back(pnewBB); parent->m_cfg.push_back(pnewBB);
pnewBB->Parent = parent; pnewBB->Parent = parent;
} }
if (start != -1) /* Only for code BB's */ if (start != -1) /* Only for code BB's */
@ -126,7 +126,7 @@ void BB::writeCode (Int indLevel, Function * pProc , Int *numLoc,Int latchNode,
repCond; /* Repeat condition for while() */ repCond; /* Repeat condition for while() */
/* Check if this basic block should be analysed */ /* Check if this basic block should be analysed */
if ((_ifFollow != UN_INIT) && (this == pProc->dfsLast[_ifFollow])) if ((_ifFollow != UN_INIT) && (this == pProc->m_dfsLast[_ifFollow]))
return; return;
if (traversed == DFS_ALPHA) if (traversed == DFS_ALPHA)
@ -139,7 +139,7 @@ void BB::writeCode (Int indLevel, Function * pProc , Int *numLoc,Int latchNode,
_loopType = loopType; _loopType = loopType;
if (_loopType) if (_loopType)
{ {
latch = pProc->dfsLast[this->latchNode]; latch = pProc->m_dfsLast[this->latchNode];
switch (_loopType) switch (_loopType)
{ {
case WHILE_TYPE: case WHILE_TYPE:
@ -236,7 +236,7 @@ void BB::writeCode (Int indLevel, Function * pProc , Int *numLoc,Int latchNode,
/* Recurse on the loop follow */ /* Recurse on the loop follow */
if (loopFollow != MAX) if (loopFollow != MAX)
{ {
succ = pProc->dfsLast[loopFollow]; succ = pProc->m_dfsLast[loopFollow];
if (succ->traversed != DFS_ALPHA) if (succ->traversed != DFS_ALPHA)
succ->writeCode (indLevel, pProc, numLoc, latchNode, _ifFollow); succ->writeCode (indLevel, pProc, numLoc, latchNode, _ifFollow);
else /* has been traversed so we need a goto */ else /* has been traversed so we need a goto */
@ -297,7 +297,7 @@ void BB::writeCode (Int indLevel, Function * pProc , Int *numLoc,Int latchNode,
cCode.appendCode( "%s}\n", indent(--indLevel)); cCode.appendCode( "%s}\n", indent(--indLevel));
/* Continue with the follow */ /* Continue with the follow */
succ = pProc->dfsLast[follow]; succ = pProc->m_dfsLast[follow];
if (succ->traversed != DFS_ALPHA) if (succ->traversed != DFS_ALPHA)
succ->writeCode (indLevel, pProc, numLoc, latchNode,_ifFollow); succ->writeCode (indLevel, pProc, numLoc, latchNode,_ifFollow);
} }

View File

@ -259,8 +259,6 @@ COND_EXPR *COND_EXPR::idLongIdx (Int idx)
/* Returns an identifier conditional expression node of type LONG_VAR */ /* Returns an identifier conditional expression node of type LONG_VAR */
COND_EXPR *COND_EXPR::idLong(LOCAL_ID *localId, opLoc sd, iICODE pIcode, hlFirst f, iICODE ix, operDu du, Int off) COND_EXPR *COND_EXPR::idLong(LOCAL_ID *localId, opLoc sd, iICODE pIcode, hlFirst f, iICODE ix, operDu du, Int off)
{ {
//assert(pIcode==ix);
//printf("**************** Not EQUAL ***************** \n");
Int idx; Int idx;
COND_EXPR *newExp = new COND_EXPR(IDENTIFIER); COND_EXPR *newExp = new COND_EXPR(IDENTIFIER);

View File

@ -196,7 +196,8 @@ static void writeHeader (std::ostream &ios, char *fileName)
/* Writes the registers that are set in the bitvector */ /* Writes the registers that are set in the bitvector */
static void writeBitVector (dword regi) static void writeBitVector (dword regi)
{ Int j; {
Int j;
for (j = 0; j < INDEXBASE; j++) for (j = 0; j < INDEXBASE; j++)
{ {
@ -204,6 +205,15 @@ static void writeBitVector (dword regi)
printf ("%s ", allRegs[j]); printf ("%s ", allRegs[j]);
} }
} }
static void writeBitVector (const std::bitset<32> &regi)
{ Int j;
for (j = 0; j < INDEXBASE; j++)
{
if (regi.test(j))
printf ("%s ", allRegs[j]);
}
}
@ -298,7 +308,7 @@ void Function::codeGen (std::ostream &fs)
if (flg & PROC_ASM) /* generate assembler */ if (flg & PROC_ASM) /* generate assembler */
disassem (3, this); disassem (3, this);
else /* generate C */ else /* generate C */
cfg.front()->writeCode (1, this, &numLoc, MAX, UN_INIT); m_cfg.front()->writeCode (1, this, &numLoc, MAX, UN_INIT);
cCode.appendCode( "}\n\n"); cCode.appendCode( "}\n\n");
writeBundle (fs, cCode); writeBundle (fs, cCode);
@ -308,7 +318,7 @@ void Function::codeGen (std::ostream &fs)
if (option.verbose) if (option.verbose)
for (i = 0; i < numBBs; i++) for (i = 0; i < numBBs; i++)
{ {
pBB = dfsLast[i]; pBB = m_dfsLast[i];
if (pBB->flg & INVALID_BB) continue; /* skip invalid BBs */ if (pBB->flg & INVALID_BB) continue; /* skip invalid BBs */
printf ("BB %d\n", i); printf ("BB %d\n", i);
printf (" Start = %d, end = %d\n", pBB->begin(), pBB->end()); printf (" Start = %d, end = %d\n", pBB->begin(), pBB->end());

View File

@ -54,9 +54,9 @@ static Int commonDom (Int currImmDom, Int predImmDom, Function * pProc)
(currImmDom != predImmDom)) (currImmDom != predImmDom))
{ {
if (currImmDom < predImmDom) if (currImmDom < predImmDom)
predImmDom = pProc->dfsLast[predImmDom]->immedDom; predImmDom = pProc->m_dfsLast[predImmDom]->immedDom;
else else
currImmDom = pProc->dfsLast[currImmDom]->immedDom; currImmDom = pProc->m_dfsLast[currImmDom]->immedDom;
} }
return (currImmDom); return (currImmDom);
} }
@ -73,7 +73,7 @@ void Function::findImmedDom ()
for (currIdx = 0; currIdx < numBBs; currIdx++) for (currIdx = 0; currIdx < numBBs; currIdx++)
{ {
currNode = dfsLast[currIdx]; currNode = m_dfsLast[currIdx];
if (currNode->flg & INVALID_BB) /* Do not process invalid BBs */ if (currNode->flg & INVALID_BB) /* Do not process invalid BBs */
continue; continue;
for (BB * inedge : currNode->inEdges) for (BB * inedge : currNode->inEdges)
@ -125,9 +125,9 @@ static void findEndlessFollow (Function * pProc, nodeList &loopNodes, BB * head)
nodeList::iterator p = loopNodes.begin(); nodeList::iterator p = loopNodes.begin();
for( ;p != loopNodes.end();++p) for( ;p != loopNodes.end();++p)
{ {
for (j = 0; j < pProc->dfsLast[*p]->edges.size(); j++) for (j = 0; j < pProc->m_dfsLast[*p]->edges.size(); j++)
{ {
succ = pProc->dfsLast[*p]->edges[j].BBptr->dfsLastNum; succ = pProc->m_dfsLast[*p]->edges[j].BBptr->dfsLastNum;
if ((! inList(loopNodes, succ)) && (succ < head->loopFollow)) if ((! inList(loopNodes, succ)) && (succ < head->loopFollow))
head->loopFollow = succ; head->loopFollow = succ;
} }
@ -152,15 +152,15 @@ static void findNodesInLoop(BB * latchNode,BB * head,Function * pProc,queue &int
insertList (loopNodes, headDfsNum); insertList (loopNodes, headDfsNum);
for (i = headDfsNum + 1; i < latchNode->dfsLastNum; i++) for (i = headDfsNum + 1; i < latchNode->dfsLastNum; i++)
{ {
if (pProc->dfsLast[i]->flg & INVALID_BB) /* skip invalid BBs */ if (pProc->m_dfsLast[i]->flg & INVALID_BB) /* skip invalid BBs */
continue; continue;
immedDom = pProc->dfsLast[i]->immedDom; immedDom = pProc->m_dfsLast[i]->immedDom;
if (inList (loopNodes, immedDom) && inInt(pProc->dfsLast[i], intNodes)) if (inList (loopNodes, immedDom) && inInt(pProc->m_dfsLast[i], intNodes))
{ {
insertList (loopNodes, i); insertList (loopNodes, i);
if (pProc->dfsLast[i]->loopHead == NO_NODE)/*not in other loop*/ if (pProc->m_dfsLast[i]->loopHead == NO_NODE)/*not in other loop*/
pProc->dfsLast[i]->loopHead = headDfsNum; pProc->m_dfsLast[i]->loopHead = headDfsNum;
} }
} }
latchNode->loopHead = headDfsNum; latchNode->loopHead = headDfsNum;
@ -233,10 +233,10 @@ static void findNodesInLoop(BB * latchNode,BB * head,Function * pProc,queue &int
findEndlessFollow (pProc, loopNodes, head); findEndlessFollow (pProc, loopNodes, head);
break; break;
} }
pbb = pProc->dfsLast[pbb->immedDom]; pbb = pProc->m_dfsLast[pbb->immedDom];
} }
if (pbb->dfsLastNum > head->dfsLastNum) if (pbb->dfsLastNum > head->dfsLastNum)
pProc->dfsLast[head->loopFollow]->loopHead = NO_NODE; /*****/ pProc->m_dfsLast[head->loopFollow]->loopHead = NO_NODE; /*****/
head->back().SetLlFlag(JX_LOOP); head->back().SetLlFlag(JX_LOOP);
} }
else else
@ -342,7 +342,7 @@ static boolT successor (Int s, Int h, Function * pProc)
Int i; Int i;
BB * header; BB * header;
header = pProc->dfsLast[h]; header = pProc->m_dfsLast[h];
for (i = 0; i < header->edges.size(); i++) for (i = 0; i < header->edges.size(); i++)
if (header->edges[i].BBptr->dfsLastNum == s) if (header->edges[i].BBptr->dfsLastNum == s)
return true; return true;
@ -382,27 +382,27 @@ void Function::structCases()
/* Linear scan of the nodes in reverse dfsLast order, searching for /* Linear scan of the nodes in reverse dfsLast order, searching for
* case nodes */ * case nodes */
for (i = numBBs - 1; i >= 0; i--) for (i = numBBs - 1; i >= 0; i--)
if (dfsLast[i]->nodeType == MULTI_BRANCH) if (m_dfsLast[i]->nodeType == MULTI_BRANCH)
{ {
caseHeader = dfsLast[i]; caseHeader = m_dfsLast[i];
/* Find descendant node which has as immediate predecessor /* Find descendant node which has as immediate predecessor
* the current header node, and is not a successor. */ * the current header node, and is not a successor. */
for (j = i + 2; j < numBBs; j++) for (j = i + 2; j < numBBs; j++)
{ {
if ((!successor(j, i, this)) && if ((!successor(j, i, this)) &&
(dfsLast[j]->immedDom == i)) (m_dfsLast[j]->immedDom == i))
if (exitNode == NO_NODE) if (exitNode == NO_NODE)
exitNode = j; exitNode = j;
else if (dfsLast[exitNode]->inEdges.size() < dfsLast[j]->inEdges.size()) else if (m_dfsLast[exitNode]->inEdges.size() < m_dfsLast[j]->inEdges.size())
exitNode = j; exitNode = j;
} }
dfsLast[i]->caseTail = exitNode; m_dfsLast[i]->caseTail = exitNode;
/* Tag nodes that belong to the case by recording the /* Tag nodes that belong to the case by recording the
* header field with caseHeader. */ * header field with caseHeader. */
insertList (caseNodes, i); insertList (caseNodes, i);
dfsLast[i]->caseHead = i; m_dfsLast[i]->caseHead = i;
for(TYPEADR_TYPE &pb : caseHeader->edges) for(TYPEADR_TYPE &pb : caseHeader->edges)
{ {
tagNodesInCase(pb.BBptr, caseNodes, i, exitNode); tagNodesInCase(pb.BBptr, caseNodes, i, exitNode);
@ -410,7 +410,7 @@ void Function::structCases()
//for (j = 0; j < caseHeader->edges[j]; j++) //for (j = 0; j < caseHeader->edges[j]; j++)
// tagNodesInCase (caseHeader->edges[j].BBptr, caseNodes, i, exitNode); // tagNodesInCase (caseHeader->edges[j].BBptr, caseNodes, i, exitNode);
if (exitNode != NO_NODE) if (exitNode != NO_NODE)
dfsLast[exitNode]->caseHead = i; m_dfsLast[exitNode]->caseHead = i;
} }
} }
@ -424,7 +424,7 @@ static void flagNodes (nodeList &l, Int f, Function * pProc)
p = l.begin(); p = l.begin();
while (p!=l.end()) while (p!=l.end())
{ {
pProc->dfsLast[*p]->ifFollow = f; pProc->m_dfsLast[*p]->ifFollow = f;
p = l.erase(p); p = l.erase(p);
} }
} }
@ -446,7 +446,7 @@ void Function::structIfs ()
/* Linear scan of nodes in reverse dfsLast order */ /* Linear scan of nodes in reverse dfsLast order */
for (curr = numBBs - 1; curr >= 0; curr--) for (curr = numBBs - 1; curr >= 0; curr--)
{ {
currNode = dfsLast[curr]; currNode = m_dfsLast[curr];
if (currNode->flg & INVALID_BB) /* Do not process invalid BBs */ if (currNode->flg & INVALID_BB) /* Do not process invalid BBs */
continue; continue;
@ -458,10 +458,10 @@ void Function::structIfs ()
/* Find all nodes that have this node as immediate dominator */ /* Find all nodes that have this node as immediate dominator */
for (desc = curr+1; desc < numBBs; desc++) for (desc = curr+1; desc < numBBs; desc++)
{ {
if (dfsLast[desc]->immedDom == curr) if (m_dfsLast[desc]->immedDom == curr)
{ {
insertList (domDesc, desc); insertList (domDesc, desc);
pbb = dfsLast[desc]; pbb = m_dfsLast[desc];
if ((pbb->inEdges.size() - pbb->numBackEdges) >= followInEdges) if ((pbb->inEdges.size() - pbb->numBackEdges) >= followInEdges)
{ {
follow = desc; follow = desc;
@ -507,7 +507,7 @@ void Function::compoundCond()
* compound condition is analysed first */ * compound condition is analysed first */
for (i = 0; i < this->numBBs; i++) for (i = 0; i < this->numBBs; i++)
{ {
pbb = this->dfsLast[i]; pbb = this->m_dfsLast[i];
if (pbb->flg & INVALID_BB) if (pbb->flg & INVALID_BB)
continue; continue;
@ -547,11 +547,11 @@ void Function::compoundCond()
t->flg |= INVALID_BB; t->flg |= INVALID_BB;
if (pbb->flg & IS_LATCH_NODE) if (pbb->flg & IS_LATCH_NODE)
this->dfsLast[t->dfsLastNum] = pbb; this->m_dfsLast[t->dfsLastNum] = pbb;
else else
i--; /* to repeat this analysis */ i--; /* to repeat this analysis */
change = TRUE; change = true;
} }
/* Check (!X && Y) case */ /* Check (!X && Y) case */
@ -585,7 +585,7 @@ void Function::compoundCond()
t->flg |= INVALID_BB; t->flg |= INVALID_BB;
if (pbb->flg & IS_LATCH_NODE) if (pbb->flg & IS_LATCH_NODE)
this->dfsLast[t->dfsLastNum] = pbb; this->m_dfsLast[t->dfsLastNum] = pbb;
else else
i--; /* to repeat this analysis */ i--; /* to repeat this analysis */
@ -620,7 +620,7 @@ void Function::compoundCond()
e->flg |= INVALID_BB; e->flg |= INVALID_BB;
if (pbb->flg & IS_LATCH_NODE) if (pbb->flg & IS_LATCH_NODE)
this->dfsLast[e->dfsLastNum] = pbb; this->m_dfsLast[e->dfsLastNum] = pbb;
else else
i--; /* to repeat this analysis */ i--; /* to repeat this analysis */
@ -657,7 +657,7 @@ void Function::compoundCond()
e->flg |= INVALID_BB; e->flg |= INVALID_BB;
if (pbb->flg & IS_LATCH_NODE) if (pbb->flg & IS_LATCH_NODE)
this->dfsLast[e->dfsLastNum] = pbb; this->m_dfsLast[e->dfsLastNum] = pbb;
else else
i--; /* to repeat this analysis */ i--; /* to repeat this analysis */

View File

@ -83,18 +83,14 @@ Int STKFRAME::getLocVar(Int off)
/* Returns a string with the source operand of Icode */ /* Returns a string with the source operand of Icode */
static COND_EXPR *srcIdent (const ICODE &Icode, Function * pProc, iICODE i, ICODE & duIcode, operDu du) static COND_EXPR *srcIdent (const ICODE &Icode, Function * pProc, iICODE i, ICODE & duIcode, operDu du)
{ {
COND_EXPR *n;
if (Icode.ic.ll.flg & I) /* immediate operand */ if (Icode.ic.ll.flg & I) /* immediate operand */
{ {
if (Icode.ic.ll.flg & B) if (Icode.ic.ll.flg & B)
n = COND_EXPR::idKte (Icode.ic.ll.src.op(), 1); return COND_EXPR::idKte (Icode.ic.ll.src.op(), 1);
else return COND_EXPR::idKte (Icode.ic.ll.src.op(), 2);
n = COND_EXPR::idKte (Icode.ic.ll.src.op(), 2);
} }
else // otherwise
n = COND_EXPR::id (Icode, SRC, pProc, i, duIcode, du); return COND_EXPR::id (Icode, SRC, pProc, i, duIcode, du);
return (n);
} }
@ -125,7 +121,7 @@ void Function::elimCondCodes ()
riICODE defAt; /* Instruction that defined flag */ riICODE defAt; /* Instruction that defined flag */
for (i = 0; i < numBBs; i++) for (i = 0; i < numBBs; i++)
{ {
pBB = dfsLast[i]; pBB = m_dfsLast[i];
if (pBB->flg & INVALID_BB) if (pBB->flg & INVALID_BB)
continue; /* Do not process invalid BBs */ continue; /* Do not process invalid BBs */
@ -225,7 +221,7 @@ void Function::elimCondCodes ()
} }
/* Generates the LiveUse() and Def() sets for each basic block in the graph. /** Generates the LiveUse() and Def() sets for each basic block in the graph.
* Note: these sets are constant and could have been constructed during * Note: these sets are constant and could have been constructed during
* the construction of the graph, but since the code hasn't been * the construction of the graph, but since the code hasn't been
* analyzed yet for idioms, the procedure preamble misleads the * analyzed yet for idioms, the procedure preamble misleads the
@ -235,14 +231,15 @@ void Function::genLiveKtes ()
{ {
Int i; Int i;
BB * pbb; BB * pbb;
dword liveUse, def; bitset<32> liveUse, def;
for (i = 0; i < numBBs; i++) for (i = 0; i < numBBs; i++)
{ {
liveUse = def = 0; liveUse.reset();
pbb = dfsLast[i]; def.reset();
pbb = m_dfsLast[i];
if (pbb->flg & INVALID_BB) if (pbb->flg & INVALID_BB)
continue; /* skip invalid BBs */ continue; // skip invalid BBs
for (auto j = pbb->begin2(); j != pbb->end2(); j++) for (auto j = pbb->begin2(); j != pbb->end2(); j++)
{ {
if ((j->type == HIGH_LEVEL) && (j->invalid == FALSE)) if ((j->type == HIGH_LEVEL) && (j->invalid == FALSE))
@ -260,14 +257,14 @@ void Function::genLiveKtes ()
/* Generates the liveIn() and liveOut() sets for each basic block via an /* Generates the liveIn() and liveOut() sets for each basic block via an
* iterative approach. * iterative approach.
* Propagates register usage information to the procedure call. */ * Propagates register usage information to the procedure call. */
void Function::liveRegAnalysis (dword in_liveOut) void Function::liveRegAnalysis (std::bitset<32> &in_liveOut)
{ {
Int i, j; Int i, j;
BB * pbb=0; /* pointer to current basic block */ BB * pbb=0; /* pointer to current basic block */
Function * pcallee; /* invoked subroutine */ Function * pcallee; /* invoked subroutine */
ICODE *ticode /* icode that invokes a subroutine */ //ICODE *ticode /* icode that invokes a subroutine */
; ;
dword prevLiveOut, /* previous live out */ std::bitset<32> prevLiveOut, /* previous live out */
prevLiveIn; /* previous live in */ prevLiveIn; /* previous live in */
boolT change; /* is there change in the live sets?*/ boolT change; /* is there change in the live sets?*/
@ -281,7 +278,7 @@ void Function::liveRegAnalysis (dword in_liveOut)
change = FALSE; change = FALSE;
for (i = numBBs; i > 0; i--) for (i = numBBs; i > 0; i--)
{ {
pbb = dfsLast[i-1]; pbb = m_dfsLast[i-1];
if (pbb->flg & INVALID_BB) /* Do not process invalid BBs */ if (pbb->flg & INVALID_BB) /* Do not process invalid BBs */
continue; continue;
@ -310,8 +307,8 @@ void Function::liveRegAnalysis (dword in_liveOut)
} }
else /* Check successors */ else /* Check successors */
{ {
for (j = 0; j < pbb->edges.size(); j++) for(TYPEADR_TYPE &e : pbb->edges)
pbb->liveOut |= pbb->edges[j].BBptr->liveIn; pbb->liveOut |= e.BBptr->liveIn;
/* propagate to invoked procedure */ /* propagate to invoked procedure */
if (pbb->nodeType == CALL_NODE) if (pbb->nodeType == CALL_NODE)
@ -328,8 +325,9 @@ void Function::liveRegAnalysis (dword in_liveOut)
} }
else /* library routine */ else /* library routine */
{ {
if ((pcallee->flg & PROC_IS_FUNC) && /* returns a value */ if ( (pcallee->flg & PROC_IS_FUNC) && /* returns a value */
(pcallee->liveOut & pbb->edges[0].BBptr->liveIn)) (pcallee->liveOut & pbb->edges[0].BBptr->liveIn).any()
)
pbb->liveOut = pcallee->liveOut; pbb->liveOut = pcallee->liveOut;
else else
pbb->liveOut = 0; pbb->liveOut = 0;
@ -345,6 +343,8 @@ void Function::liveRegAnalysis (dword in_liveOut)
case TYPE_BYTE_SIGN: case TYPE_BYTE_UNSIGN: case TYPE_BYTE_SIGN: case TYPE_BYTE_UNSIGN:
ticode.du1.numRegsDef = 1; ticode.du1.numRegsDef = 1;
break; break;
default:
fprintf(stderr,"Function::liveRegAnalysis : Unknow return type\n");
} /*eos*/ } /*eos*/
/* Propagate def/use results to calling icode */ /* Propagate def/use results to calling icode */
@ -388,15 +388,16 @@ void Function::genDU1 ()
Int i, k, defRegIdx, useIdx; Int i, k, defRegIdx, useIdx;
iICODE picode, ticode,lastInst;/* Current and target bb */ iICODE picode, ticode,lastInst;/* Current and target bb */
BB * pbb, *tbb; /* Current and target basic block */ BB * pbb, *tbb; /* Current and target basic block */
boolT res; bool res;
COND_EXPR *exp, *lhs; //COND_EXPR *exp, *lhs;
/* Traverse tree in dfsLast order */ /* Traverse tree in dfsLast order */
assert(dfsLast.size()==numBBs); assert(m_dfsLast.size()==numBBs);
for (i = 0; i < numBBs; i++) for (i = 0; i < numBBs; i++)
{ {
pbb = dfsLast[i]; pbb = m_dfsLast[i];
if (pbb->flg & INVALID_BB) continue; if (pbb->flg & INVALID_BB)
continue;
/* Process each register definition of a HIGH_LEVEL icode instruction. /* Process each register definition of a HIGH_LEVEL icode instruction.
* Note that register variables should not be considered registers. * Note that register variables should not be considered registers.
@ -404,133 +405,131 @@ void Function::genDU1 ()
lastInst = pbb->end2(); lastInst = pbb->end2();
for (picode = pbb->begin2(); picode != lastInst; picode++) for (picode = pbb->begin2(); picode != lastInst; picode++)
{ {
if (picode->type == HIGH_LEVEL) if (picode->type != HIGH_LEVEL)
continue;
regi = 0;
defRegIdx = 0;
for (k = 0; k < INDEXBASE; k++)
{ {
regi = 0; if (not picode->du.def.test(k))
defRegIdx = 0; continue;
for (k = 0; k < INDEXBASE; k++) regi = (byte)(k + 1); /* defined register */
picode->du1.regi[defRegIdx] = regi;
/* Check remaining instructions of the BB for all uses
* of register regi, before any definitions of the
* register */
if ((regi == rDI) && (flg & DI_REGVAR))
continue;
if ((regi == rSI) && (flg & SI_REGVAR))
continue;
if ((picode + 1) != lastInst) /* several instructions */
{ {
if ((picode->du.def & power2(k)) == 0) useIdx = 0;
continue; for (auto ricode = picode + 1; ricode != lastInst; ricode++)
regi = (byte)(k + 1); /* defined register */
picode->du1.regi[defRegIdx] = regi;
/* Check remaining instructions of the BB for all uses
* of register regi, before any definitions of the
* register */
if ((regi == rDI) && (flg & DI_REGVAR))
continue;
if ((regi == rSI) && (flg & SI_REGVAR))
continue;
if ((picode + 1) != lastInst) /* several instructions */
{ {
useIdx = 0; ticode=ricode;
for (auto ricode = picode + 1; ricode != lastInst; ricode++) /* Only check uses of HIGH_LEVEL icodes */
if (ricode->type == HIGH_LEVEL)
{ {
ticode=ricode; /* if used, get icode index */
/* Only check uses of HIGH_LEVEL icodes */ if ((ricode->du.use & duReg[regi]).any())
if (ricode->type == HIGH_LEVEL) picode->du1.idx[defRegIdx][useIdx++] = ricode->loc_ip;
{
/* if used, get icode index */
if (ricode->du.use & duReg[regi])
picode->du1.idx[defRegIdx][useIdx++] = ricode->loc_ip;
/* if defined, stop finding uses for this reg */ /* if defined, stop finding uses for this reg */
if (ricode->du.def & duReg[regi]) if ((ricode->du.def & duReg[regi]).any())
break; break;
}
} }
/* Check if last definition of this register */
if ((! (ticode->du.def & duReg[regi])) && (pbb->liveOut & duReg[regi]))
picode->du.lastDefRegi |= duReg[regi];
}
else /* only 1 instruction in this basic block */
{
/* Check if last definition of this register */
if (pbb->liveOut & duReg[regi])
picode->du.lastDefRegi |= duReg[regi];
} }
/* Find target icode for HLI_CALL icodes to procedures /* Check if last definition of this register */
* that are functions. The target icode is in the if ((not (ticode->du.def & duReg[regi]).any()) and (pbb->liveOut & duReg[regi]).any())
* next basic block (unoptimized code) or somewhere else picode->du.lastDefRegi |= duReg[regi];
* on optimized code. */ }
if ((picode->ic.hl.opcode == HLI_CALL) && else /* only 1 instruction in this basic block */
(picode->ic.hl.oper.call.proc->flg & PROC_IS_FUNC)) {
/* Check if last definition of this register */
if ((pbb->liveOut & duReg[regi]).any())
picode->du.lastDefRegi |= duReg[regi];
}
/* Find target icode for HLI_CALL icodes to procedures
* that are functions. The target icode is in the
* next basic block (unoptimized code) or somewhere else
* on optimized code. */
if ((picode->ic.hl.opcode == HLI_CALL) &&
(picode->ic.hl.oper.call.proc->flg & PROC_IS_FUNC))
{
tbb = pbb->edges[0].BBptr;
useIdx = 0;
for (ticode = tbb->begin2(); ticode != tbb->end2(); ticode++)
{ {
tbb = pbb->edges[0].BBptr; if (ticode->type != HIGH_LEVEL)
useIdx = 0; continue;
for (ticode = tbb->begin2(); ticode != tbb->end2(); ticode++) /* if used, get icode index */
{ if ((ticode->du.use & duReg[regi]).any())
if (ticode->type == HIGH_LEVEL) picode->du1.idx[defRegIdx][useIdx++] = ticode->loc_ip;
/* if defined, stop finding uses for this reg */
if ((ticode->du.def & duReg[regi]).any())
break;
}
/* if not used in this basic block, check if the
* register is live out, if so, make it the last
* definition of this register */
if ((picode->du1.idx[defRegIdx][useIdx] == 0) &&
(tbb->liveOut & duReg[regi]).any())
picode->du.lastDefRegi |= duReg[regi];
}
/* If not used within this bb or in successors of this
* bb (ie. not in liveOut), then register is useless,
* thus remove it. Also check that this is not a return
* from a library function (routines such as printf
* return an integer, which is normally not taken into
* account by the programmer). */
if ((picode->invalid == FALSE) &&
(picode->du1.idx[defRegIdx][0] == 0) &&
(not (picode->du.lastDefRegi & duReg[regi]).any()) &&
//(! ((picode->ic.hl.opcode != HLI_CALL) &&
(not ((picode->ic.hl.opcode == HLI_CALL) &&
(picode->ic.hl.oper.call.proc->flg & PROC_ISLIB))))
{
if (! (pbb->liveOut & duReg[regi]).any()) /* not liveOut */
{
res = picode->removeDefRegi (regi, defRegIdx+1,&localId);
/* Backpatch any uses of this instruction, within
* the same BB, if the instruction was invalidated */
if (res == TRUE)
for (auto ticode = riICODE(picode); ticode != pbb->rend2(); ticode++)
{ {
/* if used, get icode index */ for (int n = 0; n < MAX_USES; n++)
if (ticode->du.use & duReg[regi])
picode->du1.idx[defRegIdx][useIdx++] = ticode->loc_ip;
/* if defined, stop finding uses for this reg */
if (ticode->du.def & duReg[regi])
break;
}
}
/* if not used in this basic block, check if the
* register is live out, if so, make it the last
* definition of this register */
if ((picode->du1.idx[defRegIdx][useIdx] == 0) &&
(tbb->liveOut & duReg[regi]))
picode->du.lastDefRegi |= duReg[regi];
}
/* If not used within this bb or in successors of this
* bb (ie. not in liveOut), then register is useless,
* thus remove it. Also check that this is not a return
* from a library function (routines such as printf
* return an integer, which is normally not taken into
* account by the programmer). */
if ((picode->invalid == FALSE) &&
(picode->du1.idx[defRegIdx][0] == 0) &&
(! (picode->du.lastDefRegi & duReg[regi])) &&
// (! ((picode->ic.hl.opcode != HLI_CALL) &&
(! ((picode->ic.hl.opcode == HLI_CALL) &&
(picode->ic.hl.oper.call.proc->flg & PROC_ISLIB))))
{
if (! (pbb->liveOut & duReg[regi])) /* not liveOut */
{
res = picode->removeDefRegi (regi, defRegIdx+1,&localId);
/* Backpatch any uses of this instruction, within
* the same BB, if the instruction was invalidated */
if (res == TRUE)
for (auto ticode = riICODE(picode); ticode != pbb->rend2(); ticode++)
{ {
for (int n = 0; n < MAX_USES; n++) if (ticode->du1.idx[0][n] == picode->loc_ip)
{ {
if (ticode->du1.idx[0][n] == picode->loc_ip) if (n < MAX_USES - 1)
{ {
if (n < MAX_USES - 1) memmove (&ticode->du1.idx[0][n],
{ &ticode->du1.idx[0][n+1],
memmove (&ticode->du1.idx[0][n], (size_t)((MAX_USES - n - 1) * sizeof(Int)));
&ticode->du1.idx[0][n+1], n--;
(size_t)((MAX_USES - n - 1) * sizeof(Int)));
n--;
}
ticode->du1.idx[0][MAX_USES - 1] = 0;
} }
ticode->du1.idx[0][MAX_USES - 1] = 0;
} }
} }
} }
else /* liveOut */
picode->du.lastDefRegi |= duReg[regi];
} }
defRegIdx++; else /* liveOut */
picode->du.lastDefRegi |= duReg[regi];
/* Check if all defined registers have been processed */
if ((defRegIdx >= picode->du1.numRegsDef) ||
(defRegIdx == MAX_REGS_DEF))
break;
} }
defRegIdx++;
/* Check if all defined registers have been processed */
if ((defRegIdx >= picode->du1.numRegsDef) ||
(defRegIdx == MAX_REGS_DEF))
break;
} }
} }
} }
@ -541,7 +540,7 @@ void Function::genDU1 ()
/* Substitutes the rhs (or lhs if rhs not possible) of ticode for the rhs /* Substitutes the rhs (or lhs if rhs not possible) of ticode for the rhs
* of picode. */ * of picode. */
static void forwardSubs (COND_EXPR *lhs, COND_EXPR *rhs, ICODE * picode, static void forwardSubs (COND_EXPR *lhs, COND_EXPR *rhs, ICODE * picode,
ICODE * ticode, LOCAL_ID *locsym, Int *numHlIcodes) ICODE * ticode, LOCAL_ID *locsym, Int &numHlIcodes)
{ {
boolT res; boolT res;
@ -555,7 +554,7 @@ static void forwardSubs (COND_EXPR *lhs, COND_EXPR *rhs, ICODE * picode,
if (res) if (res)
{ {
picode->invalidate(); picode->invalidate();
(*numHlIcodes)--; numHlIcodes--;
} }
else else
{ {
@ -566,7 +565,7 @@ static void forwardSubs (COND_EXPR *lhs, COND_EXPR *rhs, ICODE * picode,
if (res) if (res)
{ {
picode->invalidate(); picode->invalidate();
(*numHlIcodes)--; numHlIcodes--;
} }
} }
} }
@ -576,7 +575,8 @@ static void forwardSubs (COND_EXPR *lhs, COND_EXPR *rhs, ICODE * picode,
* expression exp given */ * expression exp given */
static void forwardSubsLong (Int longIdx, COND_EXPR *exp, ICODE * picode, static void forwardSubsLong (Int longIdx, COND_EXPR *exp, ICODE * picode,
ICODE * ticode, Int *numHlIcodes) ICODE * ticode, Int *numHlIcodes)
{ boolT res; {
bool res;
if (exp == NULL) /* In case expression popped is NULL */ if (exp == NULL) /* In case expression popped is NULL */
return; return;
@ -613,16 +613,17 @@ static boolT xClear (COND_EXPR *rhs, iICODE f, Int t, iICODE lastBBinst, Functio
if (rhs == NULL) if (rhs == NULL)
return false; return false;
switch (rhs->type) { switch (rhs->type)
{
case IDENTIFIER: case IDENTIFIER:
if (rhs->expr.ident.idType == REGISTER) if (rhs->expr.ident.idType == REGISTER)
{ {
picode = &pproc->Icode.front(); picode = &pproc->Icode.front();
regi= pproc->localId.id_arr[rhs->expr.ident.idNode.regiIdx].id.regi; regi= pproc->localId.id_arr[rhs->expr.ident.idNode.regiIdx].id.regi;
for (i = (f + 1); (i != lastBBinst) && (i->loc_ip < t); i++) for (i = (f + 1); (i != lastBBinst) && (i->loc_ip < t); i++)
if ((i->type == HIGH_LEVEL) && (i->invalid == FALSE)) if ((i->type == HIGH_LEVEL) && ( not i->invalid ))
{ {
if (i->du.def & duReg[regi]) if ((i->du.def & duReg[regi]).any())
return false; return false;
} }
if (i != lastBBinst) if (i != lastBBinst)
@ -688,7 +689,7 @@ static void processCArg (Function * pp, Function * pProc, ICODE * picode, Int nu
*k += hlTypeSize (exp, pProc); *k += hlTypeSize (exp, pProc);
} }
/* Eliminates extraneous intermediate icode instructions when finding /** Eliminates extraneous intermediate icode instructions when finding
* expressions. Generates new hlIcodes in the form of expression trees. * expressions. Generates new hlIcodes in the form of expression trees.
* For HLI_CALL hlIcodes, places the arguments in the argument list. */ * For HLI_CALL hlIcodes, places the arguments in the argument list. */
void Function::findExps() void Function::findExps()
@ -701,7 +702,7 @@ void Function::findExps()
boolT res; boolT res;
COND_EXPR *exp, /* expression pointer - for HLI_POP and HLI_CALL */ COND_EXPR *exp, /* expression pointer - for HLI_POP and HLI_CALL */
*lhs; /* exp ptr for return value of a HLI_CALL */ *lhs; /* exp ptr for return value of a HLI_CALL */
STKFRAME * args; /* pointer to arguments - for HLI_CALL */ //STKFRAME * args; /* pointer to arguments - for HLI_CALL */
byte regi, regi2; /* register(s) to be forward substituted */ byte regi, regi2; /* register(s) to be forward substituted */
ID *retVal; /* function return value */ ID *retVal; /* function return value */
@ -712,7 +713,7 @@ void Function::findExps()
for (i = 0; i < numBBs; i++) for (i = 0; i < numBBs; i++)
{ {
/* Process one BB */ /* Process one BB */
pbb = dfsLast[i]; pbb = m_dfsLast[i];
if (pbb->flg & INVALID_BB) if (pbb->flg & INVALID_BB)
continue; continue;
lastInst = pbb->end2(); lastInst = pbb->end2();
@ -735,12 +736,13 @@ void Function::findExps()
regi = picode->du1.regi[0]; regi = picode->du1.regi[0];
/* Check if we can forward substitute this register */ /* Check if we can forward substitute this register */
switch (picode->ic.hl.opcode) { switch (picode->ic.hl.opcode)
{
case HLI_ASSIGN: case HLI_ASSIGN:
/* Replace rhs of current icode into target /* Replace rhs of current icode into target
* icode expression */ * icode expression */
ticode = Icode.begin()+picode->du1.idx[0][0]; ticode = Icode.begin()+picode->du1.idx[0][0];
if ((picode->du.lastDefRegi & duReg[regi]) && if ((picode->du.lastDefRegi & duReg[regi]).any() &&
((ticode->ic.hl.opcode != HLI_CALL) && ((ticode->ic.hl.opcode != HLI_CALL) &&
(ticode->ic.hl.opcode != HLI_RET))) (ticode->ic.hl.opcode != HLI_RET)))
continue; continue;
@ -753,7 +755,7 @@ void Function::findExps()
forwardSubs (picode->ic.hl.oper.asgn.lhs, forwardSubs (picode->ic.hl.oper.asgn.lhs,
picode->ic.hl.oper.asgn.rhs, picode->ic.hl.oper.asgn.rhs,
&(*picode), &(*ticode), &localId, &(*picode), &(*ticode), &localId,
&numHlIcodes); numHlIcodes);
break; break;
case HLI_JCOND: case HLI_PUSH: case HLI_RET: case HLI_JCOND: case HLI_PUSH: case HLI_RET:
@ -780,7 +782,7 @@ void Function::findExps()
case HLI_POP: case HLI_POP:
ticode = Icode.begin()+(picode->du1.idx[0][0]); ticode = Icode.begin()+(picode->du1.idx[0][0]);
if ((picode->du.lastDefRegi & duReg[regi]) && if ((picode->du.lastDefRegi & duReg[regi]).any() &&
((ticode->ic.hl.opcode != HLI_CALL) && ((ticode->ic.hl.opcode != HLI_CALL) &&
(ticode->ic.hl.opcode != HLI_RET))) (ticode->ic.hl.opcode != HLI_RET)))
continue; continue;
@ -790,7 +792,7 @@ void Function::findExps()
case HLI_ASSIGN: case HLI_ASSIGN:
forwardSubs (picode->ic.hl.oper.exp, exp, forwardSubs (picode->ic.hl.oper.exp, exp,
&(*picode), &(*ticode), &localId, &(*picode), &(*ticode), &localId,
&numHlIcodes); numHlIcodes);
break; break;
case HLI_JCOND: case HLI_PUSH: case HLI_RET: case HLI_JCOND: case HLI_PUSH: case HLI_RET:
@ -884,7 +886,7 @@ void Function::findExps()
if (picode->du1.idx[0][0] == picode->du1.idx[1][0]) if (picode->du1.idx[0][0] == picode->du1.idx[1][0])
{ {
ticode = Icode.begin()+(picode->du1.idx[0][0]); ticode = Icode.begin()+(picode->du1.idx[0][0]);
if ((picode->du.lastDefRegi & duReg[regi]) && if ((picode->du.lastDefRegi & duReg[regi]).any() &&
((ticode->ic.hl.opcode != HLI_CALL) && ((ticode->ic.hl.opcode != HLI_CALL) &&
(ticode->ic.hl.opcode != HLI_RET))) (ticode->ic.hl.opcode != HLI_RET)))
continue; continue;
@ -921,7 +923,7 @@ void Function::findExps()
if (picode->du1.idx[0][0] == picode->du1.idx[1][0]) if (picode->du1.idx[0][0] == picode->du1.idx[1][0])
{ {
ticode = Icode.begin()+(picode->du1.idx[0][0]); ticode = Icode.begin()+(picode->du1.idx[0][0]);
if ((picode->du.lastDefRegi & duReg[regi]) && if ((picode->du.lastDefRegi & duReg[regi]).any() &&
((ticode->ic.hl.opcode != HLI_CALL) && ((ticode->ic.hl.opcode != HLI_CALL) &&
(ticode->ic.hl.opcode != HLI_RET))) (ticode->ic.hl.opcode != HLI_RET)))
continue; continue;
@ -1080,10 +1082,10 @@ void Function::findExps()
} }
/* Invokes procedures related with data flow analysis. Works on a procedure /** Invokes procedures related with data flow analysis. Works on a procedure
* at a time basis. * at a time basis.
* Note: indirect recursion in liveRegAnalysis is possible. */ * Note: indirect recursion in liveRegAnalysis is possible. */
void Function::dataFlow(dword liveOut) void Function::dataFlow(std::bitset<32> &liveOut)
{ {
boolT isAx, isBx, isCx, isDx; boolT isAx, isBx, isCx, isDx;
Int idx; Int idx;
@ -1098,10 +1100,10 @@ void Function::dataFlow(dword liveOut)
if (liveOut != 0) if (liveOut != 0)
{ {
flg |= PROC_IS_FUNC; flg |= PROC_IS_FUNC;
isAx = (boolT)(liveOut & power2(rAX - rAX)); isAx = liveOut.test(rAX - rAX);
isBx = (boolT)(liveOut & power2(rBX - rAX)); isBx = liveOut.test(rBX - rAX);
isCx = (boolT)(liveOut & power2(rCX - rAX)); isCx = liveOut.test(rCX - rAX);
isDx = (boolT)(liveOut & power2(rDX - rAX)); isDx = liveOut.test(rDX - rAX);
if (isAx && isDx) /* long or pointer */ if (isAx && isDx) /* long or pointer */
{ {

View File

@ -157,7 +157,7 @@ struct POSSTACK_ENTRY
vector<POSSTACK_ENTRY> posStack; /* position stack */ vector<POSSTACK_ENTRY> posStack; /* position stack */
byte iPS; /* Index into the stack */ byte iPS; /* Index into the stack */
static char cbuf[256]; /* Has to be 256 for wgetstr() to work */ //static char cbuf[256]; /* Has to be 256 for wgetstr() to work */
// These are "curses equivalent" functions. (Used to use curses for all this, // These are "curses equivalent" functions. (Used to use curses for all this,
// but it was too much of a distribution hassle // but it was too much of a distribution hassle

View File

@ -8,11 +8,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#ifdef __BORLAND__
#include <alloc.h>
#else
#include <malloc.h> /* For malloc, free, realloc */ #include <malloc.h> /* For malloc, free, realloc */
#endif
typedef struct { /* PSP structure */ typedef struct { /* PSP structure */
word int20h; /* interrupt 20h */ word int20h; /* interrupt 20h */

View File

@ -203,8 +203,8 @@ void Function::compressCFG()
/* First pass over BB list removes redundant jumps of the form /* First pass over BB list removes redundant jumps of the form
* (Un)Conditional -> Unconditional jump */ * (Un)Conditional -> Unconditional jump */
std::vector<BB*>::iterator iter=cfg.begin(); auto iter=m_cfg.begin();
for (;iter!=cfg.end(); ++iter) for (;iter!=m_cfg.end(); ++iter)
{ {
pBB = *iter; pBB = *iter;
if(pBB->inEdges.empty() || (pBB->nodeType != ONE_BRANCH && pBB->nodeType != TWO_BRANCH)) if(pBB->inEdges.empty() || (pBB->nodeType != ONE_BRANCH && pBB->nodeType != TWO_BRANCH))
@ -225,18 +225,18 @@ void Function::compressCFG()
/* Next is a depth-first traversal merging any FALL_NODE or /* Next is a depth-first traversal merging any FALL_NODE or
* ONE_BRANCH that fall through to a node with that as their only * ONE_BRANCH that fall through to a node with that as their only
* in-edge. */ * in-edge. */
this->cfg.front()->mergeFallThrough(Icode); m_cfg.front()->mergeFallThrough(Icode);
/* Remove redundant BBs created by the above compressions /* Remove redundant BBs created by the above compressions
* and allocate in-edge arrays as required. */ * and allocate in-edge arrays as required. */
stats.numBBaft = stats.numBBbef; stats.numBBaft = stats.numBBbef;
for(auto iter=cfg.begin(); iter!=cfg.end(); ++iter) for(auto iter=m_cfg.begin(); iter!=m_cfg.end(); ++iter)
{ {
pBB = *iter; pBB = *iter;
if (pBB->inEdges.empty()) if (pBB->inEdges.empty())
{ {
if (iter == cfg.begin()) /* Init it misses out on */ if (iter == m_cfg.begin()) /* Init it misses out on */
pBB->index = UN_INIT; pBB->index = UN_INIT;
else else
{ {
@ -252,11 +252,11 @@ void Function::compressCFG()
/* Allocate storage for dfsLast[] array */ /* Allocate storage for dfsLast[] array */
numBBs = stats.numBBaft; numBBs = stats.numBBaft;
dfsLast.resize(numBBs,0); // = (BB **)allocMem(numBBs * sizeof(BB *)) m_dfsLast.resize(numBBs,0); // = (BB **)allocMem(numBBs * sizeof(BB *))
/* Now do a dfs numbering traversal and fill in the inEdges[] array */ /* Now do a dfs numbering traversal and fill in the inEdges[] array */
last = numBBs - 1; last = numBBs - 1;
cfg.front()->dfsNumbering(dfsLast, &first, &last); m_cfg.front()->dfsNumbering(m_dfsLast, &first, &last);
} }

View File

@ -99,34 +99,34 @@ boolT ICODE::removeDefRegi (byte regi, Int thisDefIdx, LOCAL_ID *locId)
numDefs = du1.numRegsDef; numDefs = du1.numRegsDef;
if (numDefs == thisDefIdx) if (numDefs == thisDefIdx)
{
for ( ; numDefs > 0; numDefs--) for ( ; numDefs > 0; numDefs--)
{ {
if ((du1.idx[numDefs-1][0] != 0)||(du.lastDefRegi)) if ((du1.idx[numDefs-1][0] != 0)||(du.lastDefRegi.any()))
break; break;
} }
}
if (numDefs == 0) if (numDefs == 0)
{ {
invalidate(); invalidate();
return true; return true;
} }
else switch (ic.hl.opcode)
{ {
switch (ic.hl.opcode) { case HLI_ASSIGN:
case HLI_ASSIGN: removeRegFromLong (regi, locId,ic.hl.oper.asgn.lhs);
removeRegFromLong (regi, locId,ic.hl.oper.asgn.lhs); du1.numRegsDef--;
du1.numRegsDef--; du.def &= maskDuReg[regi];
du.def &= maskDuReg[regi]; break;
break; case HLI_POP:
case HLI_POP: case HLI_PUSH:
case HLI_PUSH: removeRegFromLong (regi, locId, ic.hl.oper.exp);
removeRegFromLong (regi, locId, ic.hl.oper.exp); du1.numRegsDef--;
du1.numRegsDef--; du.def &= maskDuReg[regi];
du.def &= maskDuReg[regi]; break;
break;
}
return false;
} }
return false;
} }
@ -452,7 +452,7 @@ void ICODE::writeDU(Int idx)
buf[0] = '\0'; buf[0] = '\0';
for (i = 0; i < (INDEXBASE-1); i++) for (i = 0; i < (INDEXBASE-1); i++)
{ {
if ((du.def & power2(i)) != 0) if (du.def[i])
{ {
strcat (buf, allRegs[i]); strcat (buf, allRegs[i]);
strcat (buf, " "); strcat (buf, " ");
@ -465,7 +465,7 @@ void ICODE::writeDU(Int idx)
buf[0] = '\0'; buf[0] = '\0';
for (i = 0; i < INDEXBASE; i++) for (i = 0; i < INDEXBASE; i++)
{ {
if ((du.use & power2(i)) != 0) if (du.use[i])
{ {
strcat (buf, allRegs[i]); strcat (buf, allRegs[i]);
strcat (buf, " "); strcat (buf, " ");

View File

@ -137,7 +137,7 @@ static Int idiom1(iICODE pIcode, iICODE pEnd, Function * pProc)
&& pIcode->ic.ll.opcode == iMOV && pIcode->ic.ll.dst.regi == rBP && pIcode->ic.ll.opcode == iMOV && pIcode->ic.ll.dst.regi == rBP
&& pIcode->ic.ll.src.regi == rSP) && pIcode->ic.ll.src.regi == rSP)
{ {
pProc->args.minOff = 2; pProc->args.m_minOff = 2;
pProc->flg |= PROC_IS_HLL; pProc->flg |= PROC_IS_HLL;
/* Look for SUB SP, immed */ /* Look for SUB SP, immed */
@ -167,7 +167,7 @@ static Int idiom1(iICODE pIcode, iICODE pEnd, Function * pProc)
pIcode->ic.ll.dst.regi == rBP && pIcode->ic.ll.dst.regi == rBP &&
pIcode->ic.ll.src.regi == rSP) pIcode->ic.ll.src.regi == rSP)
{ {
pProc->args.minOff = 2 + (n * 2); pProc->args.m_minOff = 2 + (n * 2);
return (2 + n); return (2 + n);
} }
else return 0; // Cristina: check this please! else return 0; // Cristina: check this please!
@ -191,7 +191,7 @@ static Int idiom1x(cITER pIcode, cITER pEnd, Function * pProc)
&& pIcode->ic.ll.opcode == iMOV && pIcode->ic.ll.dst.regi == rBP && pIcode->ic.ll.opcode == iMOV && pIcode->ic.ll.dst.regi == rBP
&& pIcode->ic.ll.src.regi == rSP) && pIcode->ic.ll.src.regi == rSP)
{ {
pProc->args.minOff = 2; pProc->args.m_minOff = 2;
pProc->flg |= PROC_IS_HLL; pProc->flg |= PROC_IS_HLL;
/* Look for SUB SP, immed */ /* Look for SUB SP, immed */
@ -222,7 +222,7 @@ static Int idiom1x(cITER pIcode, cITER pEnd, Function * pProc)
pIcode->ic.ll.dst.regi == rBP && pIcode->ic.ll.dst.regi == rBP &&
pIcode->ic.ll.src.regi == rSP) pIcode->ic.ll.src.regi == rSP)
{ {
pProc->args.minOff = 2 + (n * 2); pProc->args.m_minOff = 2 + (n * 2);
return (2 + n); return (2 + n);
} }
else return 0; // Cristina: check this please! else return 0; // Cristina: check this please!
@ -1390,8 +1390,8 @@ void Function::findIdioms()
/* Check if number of parameter bytes match their calling convention */ /* Check if number of parameter bytes match their calling convention */
if ((flg & PROC_HLL) && (!args.sym.empty())) if ((flg & PROC_HLL) && (!args.sym.empty()))
{ {
args.minOff += (flg & PROC_FAR ? 4 : 2); args.m_minOff += (flg & PROC_FAR ? 4 : 2);
delta = args.maxOff - args.minOff; delta = args.maxOff - args.m_minOff;
if (cbParam != delta) if (cbParam != delta)
{ {
cbParam = delta; cbParam = delta;

View File

@ -32,9 +32,7 @@ Int LOCAL_ID::newByteWordReg(hlType t, byte regi)
/* Check for entry in the table */ /* Check for entry in the table */
auto found=std::find_if(id_arr.begin(),id_arr.end(),[t,regi](ID &el)->bool { auto found=std::find_if(id_arr.begin(),id_arr.end(),[t,regi](ID &el)->bool {
if ((el.type == t) && (el.id.regi == regi)) return ((el.type == t) && (el.id.regi == regi));
return true;
return false;
}); });
if(found!=id_arr.end()) if(found!=id_arr.end())
return found-id_arr.begin(); return found-id_arr.begin();
@ -56,8 +54,8 @@ void LOCAL_ID::flagByteWordId (Int off)
{ {
Int idx; Int idx;
auto found=std::find_if(id_arr.begin(),id_arr.end(),[off](ID &en)->bool { auto found=std::find_if(id_arr.begin(),id_arr.end(),[off](ID &en)->bool {
//if (((en.type == TYPE_WORD_SIGN) || (en.type == TYPE_BYTE_SIGN)) &&
if (((en.type == TYPE_WORD_SIGN) || (en.type == TYPE_BYTE_SIGN)) && if ((en.isSigned()) &&
(en.id.bwId.off == off) && (en.id.bwId.regOff == 0)) (en.id.bwId.off == off) && (en.id.bwId.regOff == 0))
return true; return true;
return false; return false;
@ -309,7 +307,7 @@ Int LOCAL_ID::newLong(opLoc sd, ICODE *pIcode, hlFirst f, iICODE ix,operDu du, I
* pProc : ptr to current procedure record * pProc : ptr to current procedure record
* rhs, lhs : return expressions if successful. */ * rhs, lhs : return expressions if successful. */
boolT checkLongEq (LONG_STKID_TYPE longId, iICODE pIcode, Int i, boolT checkLongEq (LONG_STKID_TYPE longId, iICODE pIcode, Int i,
Function * pProc, COND_EXPR **rhs, COND_EXPR **lhs, Int off) Function * pProc, Assignment &asgn, Int off)
{ {
LLOperand *pmHdst, *pmLdst, *pmHsrc, *pmLsrc; /* pointers to LOW_LEVEL icodes */ LLOperand *pmHdst, *pmLdst, *pmHsrc, *pmLsrc; /* pointers to LOW_LEVEL icodes */
@ -320,20 +318,18 @@ boolT checkLongEq (LONG_STKID_TYPE longId, iICODE pIcode, Int i,
if ((longId.offH == pmHdst->off) && (longId.offL == pmLdst->off)) if ((longId.offH == pmHdst->off) && (longId.offL == pmLdst->off))
{ {
*lhs = COND_EXPR::idLongIdx (i); asgn.lhs = COND_EXPR::idLongIdx (i);
if ((pIcode->ic.ll.flg & NO_SRC) != NO_SRC) if ((pIcode->ic.ll.flg & NO_SRC) != NO_SRC)
{ {
*rhs = COND_EXPR::idLong (&pProc->localId, SRC, pIcode, HIGH_FIRST, pIcode, eUSE, off); asgn.rhs = COND_EXPR::idLong (&pProc->localId, SRC, pIcode, HIGH_FIRST, pIcode, eUSE, off);
//*rhs = COND_EXPR::idLong (&pProc->localId, SRC, pIcode, HIGH_FIRST, idx, eUSE, off);
} }
return true; return true;
} }
else if ((longId.offH == pmHsrc->off) && (longId.offL == pmLsrc->off)) else if ((longId.offH == pmHsrc->off) && (longId.offL == pmLsrc->off))
{ {
*lhs = COND_EXPR::idLong (&pProc->localId, DST, pIcode, HIGH_FIRST, pIcode,eDEF, off); asgn.lhs = COND_EXPR::idLong (&pProc->localId, DST, pIcode, HIGH_FIRST, pIcode,eDEF, off);
//*lhs = COND_EXPR::idLong (&pProc->localId, DST, pIcode, HIGH_FIRST, idx,eDEF, off); asgn.rhs = COND_EXPR::idLongIdx (i);
*rhs = COND_EXPR::idLongIdx (i);
return true; return true;
} }
return false; return false;
@ -410,25 +406,25 @@ void LOCAL_ID::propLongId (byte regL, byte regH, const char *name)
Int i; Int i;
ID *_id; ID *_id;
for (i = 0; i < id_arr.size(); i++) for (i = 0; i < id_arr.size(); i++)
{
_id = &id_arr[i];
if (_id->typeBitsize()==16)
{ {
_id = &id_arr[i]; if (_id->id.regi == regL)
if ((_id->type == TYPE_WORD_SIGN) || (_id->type == TYPE_WORD_UNSIGN)) {
{ strcpy (_id->name, name);
if (_id->id.regi == regL) strcpy (_id->macro, "LO");
{ _id->hasMacro = TRUE;
strcpy (_id->name, name); _id->illegal = TRUE;
strcpy (_id->macro, "LO"); }
_id->hasMacro = TRUE; else if (_id->id.regi == regH)
_id->illegal = TRUE; {
} strcpy (_id->name, name);
else if (_id->id.regi == regH) strcpy (_id->macro, "HI");
{ _id->hasMacro = TRUE;
strcpy (_id->name, name); _id->illegal = TRUE;
strcpy (_id->macro, "HI"); }
_id->hasMacro = TRUE;
_id->illegal = TRUE;
}
}
} }
}
} }

View File

@ -891,7 +891,7 @@ static void setBits(int16 type, dword start, dword len)
} }
/* DU bit definitions for each reg value - including index registers */ /* DU bit definitions for each reg value - including index registers */
dword duReg[] = { 0x00, std::bitset<32> duReg[] = { 0x00,
0x11001, 0x22002, 0x44004, 0x88008, /* word regs */ 0x11001, 0x22002, 0x44004, 0x88008, /* word regs */
0x10, 0x20, 0x40, 0x80, 0x10, 0x20, 0x40, 0x80,
0x100, 0x200, 0x400, 0x800, /* seg regs */ 0x100, 0x200, 0x400, 0x800, /* seg regs */

View File

@ -15,8 +15,7 @@
static char indentBuf[indSize] = static char indentBuf[indSize] =
" "; " ";
static char *indent (Int indLevel) static char *indent (Int indLevel) // Indentation according to the depth of the statement
/* Indentation according to the depth of the statement */
{ {
return (&indentBuf[indSize-(indLevel*3)-1]); return (&indentBuf[indSize-(indLevel*3)-1]);
} }
@ -104,7 +103,7 @@ void Function::newRegArg(ICODE *picode, ICODE *ticode)
boolT regExist; boolT regExist;
condId type; condId type;
Function * tproc; Function * tproc;
byte regL, regH; /* Registers involved in arguments */ uint8_t regL, regH; /* Registers involved in arguments */
/* Flag ticode as having register arguments */ /* Flag ticode as having register arguments */
tproc = ticode->ic.hl.oper.call.proc; tproc = ticode->ic.hl.oper.call.proc;
@ -335,7 +334,7 @@ void STKFRAME::adjustForArgType(Int numArg_, hlType actType_)
Int off, i; Int off, i;
/* Find stack offset for this argument */ /* Find stack offset for this argument */
off = minOff; off = m_minOff;
for (i = 0; i < numArg_; i++) for (i = 0; i < numArg_; i++)
off += sym[i].size; off += sym[i].size;

View File

@ -256,10 +256,11 @@ static int longJCond22 (COND_EXPR *rhs, COND_EXPR *lhs, iICODE pIcode)
* Arguments: i : index into the local identifier table * Arguments: i : index into the local identifier table
* pLocId: ptr to the long local identifier * pLocId: ptr to the long local identifier
* pProc : ptr to current procedure's record. */ * pProc : ptr to current procedure's record. */
void Function::propLongStk (Int i, ID *pLocId) void Function::propLongStk (Int i, const ID &pLocId)
{ {
Int idx, off, arc; Int idx, off, arc;
COND_EXPR *lhs, *rhs; /* Pointers to left and right hand expression */ Assignment asgn;
//COND_EXPR *lhs, *rhs; /* Pointers to left and right hand expression */
iICODE pIcode, pEnd; iICODE pIcode, pEnd;
/* Check all icodes for offHi:offLo */ /* Check all icodes for offHi:offLo */
@ -275,39 +276,33 @@ void Function::propLongStk (Int i, ID *pLocId)
switch (pIcode->ic.ll.opcode) switch (pIcode->ic.ll.opcode)
{ {
case iMOV: case iMOV:
if (checkLongEq (pLocId->id.longStkId, pIcode, i, this, if (checkLongEq (pLocId.id.longStkId, pIcode, i, this, asgn, 1) == TRUE)
&rhs, &lhs, 1) == TRUE)
{ {
pIcode->setAsgn(lhs, rhs); pIcode->setAsgn(asgn.lhs, asgn.rhs);
(pIcode+1)->invalidate(); (pIcode+1)->invalidate();
idx++; idx++;
} }
break; break;
case iAND: case iOR: case iXOR: case iAND: case iOR: case iXOR:
if (checkLongEq (pLocId->id.longStkId, pIcode, i, this, if (checkLongEq (pLocId.id.longStkId, pIcode, i, this, asgn, 1) == TRUE)
&rhs, &lhs, 1) == TRUE)
{ {
switch (pIcode->ic.ll.opcode) switch (pIcode->ic.ll.opcode)
{ {
case iAND: rhs = COND_EXPR::boolOp (lhs, rhs, AND); case iAND: asgn.rhs = COND_EXPR::boolOp (asgn.lhs, asgn.rhs, AND); break;
break; case iOR: asgn.rhs = COND_EXPR::boolOp (asgn.lhs, asgn.rhs, OR); break;
case iOR: rhs = COND_EXPR::boolOp (lhs, rhs, OR); case iXOR: asgn.rhs = COND_EXPR::boolOp (asgn.lhs, asgn.rhs, XOR); break;
break;
case iXOR: rhs = COND_EXPR::boolOp (lhs, rhs, XOR);
break;
} }
pIcode->setAsgn(lhs, rhs); pIcode->setAsgn(asgn.lhs, asgn.rhs);
(pIcode+1)->invalidate(); (pIcode+1)->invalidate();
idx++; idx++;
} }
break; break;
case iPUSH: case iPUSH:
if (checkLongEq (pLocId->id.longStkId, pIcode, i, this, if (checkLongEq (pLocId.id.longStkId, pIcode, i, this, asgn, 1) == TRUE)
&rhs, &lhs, 1) == TRUE)
{ {
pIcode->setUnary( HLI_PUSH, lhs); pIcode->setUnary( HLI_PUSH, asgn.lhs);
(pIcode+1)->invalidate(); (pIcode+1)->invalidate();
idx++; idx++;
} }
@ -318,9 +313,9 @@ void Function::propLongStk (Int i, ID *pLocId)
/* Check long conditional (i.e. 2 CMPs and 3 branches */ /* Check long conditional (i.e. 2 CMPs and 3 branches */
else if ((pIcode->ic.ll.opcode == iCMP) && (isLong23 (idx, pIcode->inBB, &off, &arc))) else if ((pIcode->ic.ll.opcode == iCMP) && (isLong23 (idx, pIcode->inBB, &off, &arc)))
{ {
if ( checkLongEq (pLocId->id.longStkId, pIcode, i, this, &rhs, &lhs, off) ) if ( checkLongEq (pLocId.id.longStkId, pIcode, i, this, asgn, off) )
{ {
idx += longJCond23 (rhs, lhs, pIcode, arc, off); // idx += longJCond23 (asgn.rhs, asgn.lhs, pIcode, arc, off); //
} }
} }
@ -328,53 +323,59 @@ void Function::propLongStk (Int i, ID *pLocId)
* 2 CMPs and 2 branches */ * 2 CMPs and 2 branches */
else if ((pIcode->ic.ll.opcode == iCMP) && isLong22 (pIcode, pEnd, &off)) else if ((pIcode->ic.ll.opcode == iCMP) && isLong22 (pIcode, pEnd, &off))
{ {
if ( checkLongEq (pLocId->id.longStkId, pIcode, i, this,&rhs, &lhs, off) ) if ( checkLongEq (pLocId.id.longStkId, pIcode, i, this,asgn, off) )
{ {
idx += longJCond22 (rhs, lhs, pIcode); // maybe this should have -1 to offset loop autoincrement? idx += longJCond22 (asgn.rhs, asgn.lhs, pIcode); // maybe this should have -1 to offset loop autoincrement?
} }
} }
} }
} }
int Function::checkBackwarLongDefs(int loc_ident_idx, const ID &pLocId, iICODE beg,Assignment &asgn) int Function::findBackwarLongDefs(int loc_ident_idx, const ID &pLocId, iICODE beg)
{ {
Assignment asgn;
LLOperand * pmH,* pmL; LLOperand * pmH,* pmL;
iICODE pIcode; iICODE pIcode;
riICODE rev(beg); riICODE rev(beg);
bool forced_finish=false; bool forced_finish=false;
for (; not forced_finish and rev!=Icode.rend();rev++) //idx = pLocId_idx - 1; idx > 0 ; idx-- for (; not forced_finish and rev!=Icode.rend();rev++) //idx = pLocId_idx - 1; idx > 0 ; idx--
{ {
ICODE &icode(*rev);
ICODE &next1(*(rev-1)); // prev reverse is actually next instruction
pIcode = (rev+1).base();//Icode.begin()+(idx-1); pIcode = (rev+1).base();//Icode.begin()+(idx-1);
if ((pIcode->type == HIGH_LEVEL) || (pIcode->invalid == TRUE))
if ((icode.type == HIGH_LEVEL) || (icode.invalid == TRUE))
continue;
if (icode.ic.ll.opcode != next1.ic.ll.opcode)
continue; continue;
if (pIcode->ic.ll.opcode != (pIcode+1)->ic.ll.opcode) switch (icode.ic.ll.opcode)
continue;
switch (pIcode->ic.ll.opcode)
{ {
case iMOV: case iMOV:
pmH = &pIcode->ic.ll.dst; pmH = &icode.ic.ll.dst;
pmL = &(pIcode+1)->ic.ll.dst; pmL = &next1.ic.ll.dst;
if ((pLocId.id.longId.h == pmH->regi) && (pLocId.id.longId.l == pmL->regi)) if ((pLocId.id.longId.h == pmH->regi) && (pLocId.id.longId.l == pmL->regi))
{ {
localId.id_arr[loc_ident_idx].idx.push_back(pIcode);//idx-1//insert
icode.setRegDU( pmL->regi, eDEF);
asgn.lhs = COND_EXPR::idLongIdx (loc_ident_idx); asgn.lhs = COND_EXPR::idLongIdx (loc_ident_idx);
this->localId.id_arr[loc_ident_idx].idx.push_back(pIcode);//idx-1//insert asgn.rhs = COND_EXPR::idLong (&this->localId, SRC, pIcode, HIGH_FIRST, pIcode, eUSE, 1);
pIcode->setRegDU( pmL->regi, eDEF); icode.setAsgn(asgn.lhs, asgn.rhs);
asgn.rhs = COND_EXPR::idLong (&this->localId, SRC, pIcode, HIGH_FIRST, pIcode/*idx*/, eUSE, 1); next1.invalidate();
pIcode->setAsgn(asgn.lhs, asgn.rhs);
(pIcode+1)->invalidate();
forced_finish=true; /* to exit the loop */ forced_finish=true; /* to exit the loop */
} }
break; break;
case iPOP: case iPOP:
pmH = &(pIcode+1)->ic.ll.dst; pmH = &next1.ic.ll.dst;
pmL = &pIcode->ic.ll.dst; pmL = &icode.ic.ll.dst;
if ((pLocId.id.longId.h == pmH->regi) && (pLocId.id.longId.l == pmL->regi)) if ((pLocId.id.longId.h == pmH->regi) && (pLocId.id.longId.l == pmL->regi))
{ {
asgn.lhs = COND_EXPR::idLongIdx (loc_ident_idx); asgn.lhs = COND_EXPR::idLongIdx (loc_ident_idx);
pIcode->setRegDU( pmH->regi, eDEF); icode.setRegDU( pmH->regi, eDEF);
pIcode->setUnary(HLI_POP, asgn.lhs); icode.setUnary(HLI_POP, asgn.lhs);
(pIcode+1)->invalidate(); next1.invalidate();
asgn.lhs=0;
forced_finish=true; /* to exit the loop */ forced_finish=true; /* to exit the loop */
} }
break; break;
@ -382,14 +383,15 @@ int Function::checkBackwarLongDefs(int loc_ident_idx, const ID &pLocId, iICODE b
// /**** others missing ***/ // /**** others missing ***/
case iAND: case iOR: case iXOR: case iAND: case iOR: case iXOR:
pmL = &pIcode->ic.ll.dst; pmL = &icode.ic.ll.dst;
pmH = &(pIcode+1)->ic.ll.dst; pmH = &next1.ic.ll.dst;
if ((pLocId.id.longId.h == pmH->regi) && (pLocId.id.longId.l == pmL->regi)) if ((pLocId.id.longId.h == pmH->regi) && (pLocId.id.longId.l == pmL->regi))
{ {
asgn.lhs = COND_EXPR::idLongIdx (loc_ident_idx); asgn.lhs = COND_EXPR::idLongIdx (loc_ident_idx);
pIcode->setRegDU( pmH->regi, USE_DEF);
asgn.rhs = COND_EXPR::idLong (&this->localId, SRC, pIcode, LOW_FIRST, pIcode/*idx*/, eUSE, 1); asgn.rhs = COND_EXPR::idLong (&this->localId, SRC, pIcode, LOW_FIRST, pIcode/*idx*/, eUSE, 1);
switch (pIcode->ic.ll.opcode) { icode.setRegDU( pmH->regi, USE_DEF);
switch (icode.ic.ll.opcode)
{
case iAND: asgn.rhs = COND_EXPR::boolOp (asgn.lhs, asgn.rhs, AND); case iAND: asgn.rhs = COND_EXPR::boolOp (asgn.lhs, asgn.rhs, AND);
break; break;
case iOR: case iOR:
@ -398,8 +400,8 @@ int Function::checkBackwarLongDefs(int loc_ident_idx, const ID &pLocId, iICODE b
case iXOR: asgn.rhs = COND_EXPR::boolOp (asgn.lhs, asgn.rhs, XOR); case iXOR: asgn.rhs = COND_EXPR::boolOp (asgn.lhs, asgn.rhs, XOR);
break; break;
} /* eos */ } /* eos */
pIcode->setAsgn(asgn.lhs, asgn.rhs); icode.setAsgn(asgn.lhs, asgn.rhs);
(pIcode+1)->invalidate(); next1.invalidate();
forced_finish=true; /* to exit the loop */ forced_finish=true; /* to exit the loop */
} }
break; break;
@ -407,10 +409,11 @@ int Function::checkBackwarLongDefs(int loc_ident_idx, const ID &pLocId, iICODE b
} }
return rev!=Icode.rend(); return rev!=Icode.rend();
} }
int Function::checkForwardLongDefs(int loc_ident_idx, const ID &pLocId, iICODE beg,Assignment &asgn) int Function::findForwardLongUses(int loc_ident_idx, const ID &pLocId, iICODE beg)
{ {
bool forced_finish=false; bool forced_finish=false;
auto pEnd=Icode.end(); auto pEnd=Icode.end();
Assignment asgn;
for (auto pIcode=beg; not forced_finish and ((pIcode+1)!=Icode.end()); ++pIcode) for (auto pIcode=beg; not forced_finish and ((pIcode+1)!=Icode.end()); ++pIcode)
{ {
iICODE next1(pIcode+1); iICODE next1(pIcode+1);
@ -427,9 +430,11 @@ int Function::checkForwardLongDefs(int loc_ident_idx, const ID &pLocId, iICODE b
if ((pLocId.id.longId.h == pIcode->ic.ll.src.regi) && if ((pLocId.id.longId.h == pIcode->ic.ll.src.regi) &&
(pLocId.id.longId.l == next1->ic.ll.src.regi)) (pLocId.id.longId.l == next1->ic.ll.src.regi))
{ {
asgn.rhs = COND_EXPR::idLongIdx (loc_ident_idx);
pIcode->setRegDU( next1->ic.ll.src.regi, eUSE); pIcode->setRegDU( next1->ic.ll.src.regi, eUSE);
asgn.rhs = COND_EXPR::idLongIdx (loc_ident_idx);
asgn.lhs = COND_EXPR::idLong (&this->localId, DST, pIcode,HIGH_FIRST, pIcode, eDEF, 1); asgn.lhs = COND_EXPR::idLong (&this->localId, DST, pIcode,HIGH_FIRST, pIcode, eDEF, 1);
pIcode->setAsgn(asgn.lhs, asgn.rhs); pIcode->setAsgn(asgn.lhs, asgn.rhs);
next1->invalidate(); next1->invalidate();
forced_finish =true; /* to exit the loop */ forced_finish =true; /* to exit the loop */
@ -442,7 +447,7 @@ int Function::checkForwardLongDefs(int loc_ident_idx, const ID &pLocId, iICODE b
{ {
asgn.rhs = COND_EXPR::idLongIdx (loc_ident_idx); asgn.rhs = COND_EXPR::idLongIdx (loc_ident_idx);
pIcode->setRegDU( next1->ic.ll.src.regi, eUSE); pIcode->setRegDU( next1->ic.ll.src.regi, eUSE);
pIcode->setUnary(HLI_PUSH, asgn.lhs); pIcode->setUnary(HLI_PUSH, asgn.rhs);
next1->invalidate(); next1->invalidate();
} }
forced_finish =true; /* to exit the loop */ forced_finish =true; /* to exit the loop */
@ -481,8 +486,7 @@ int Function::checkForwardLongDefs(int loc_ident_idx, const ID &pLocId, iICODE b
/* Check long conditional (i.e. 2 CMPs and 3 branches */ /* Check long conditional (i.e. 2 CMPs and 3 branches */
else if ((pIcode->ic.ll.opcode == iCMP) && (isLong23 (pIcode, pIcode->inBB, &off, &arc))) else if ((pIcode->ic.ll.opcode == iCMP) && (isLong23 (pIcode, pIcode->inBB, &off, &arc)))
{ {
if (checkLongRegEq (pLocId.id.longId, pIcode, loc_ident_idx, this, if (checkLongRegEq (pLocId.id.longId, pIcode, loc_ident_idx, this, asgn.rhs, asgn.lhs, off) == TRUE)
asgn.rhs, asgn.lhs, off) == TRUE)
{ {
pIcode += longJCond23 (asgn.rhs, asgn.lhs, pIcode, arc, off); pIcode += longJCond23 (asgn.rhs, asgn.lhs, pIcode, arc, off);
} }
@ -523,40 +527,37 @@ int Function::checkForwardLongDefs(int loc_ident_idx, const ID &pLocId, iICODE b
} /* end for */ } /* end for */
} }
/* Finds the definition of the long register pointed to by pLocId, and /** Finds the definition of the long register pointed to by pLocId, and
* transforms that instruction into a HIGH_LEVEL icode instruction. * transforms that instruction into a HIGH_LEVEL icode instruction.
* Arguments: i : index into the local identifier table * @arg i index into the local identifier table
* pLocId: ptr to the long local identifier * @arg pLocId ptr to the long local identifier
* pProc : ptr to current procedure's record. */ *
void Function::propLongReg (Int loc_ident_idx, const ID *pLocId) */
void Function::propLongReg (Int loc_ident_idx, const ID &pLocId)
{ {
Int idx;
iICODE pEnd;
/* Process all definitions/uses of long registers at an icode position */ /* Process all definitions/uses of long registers at an icode position */
pEnd = this->Icode.end();
const IDX_ARRAY &lidx(pLocId->idx);
// for (int pLocId_idx : pLocId->idx)
// WARNING: this loop modifies the iterated-over container. // WARNING: this loop modifies the iterated-over container.
for (int j = 0; j < pLocId->idx.size(); j++) size_t initial_size=pLocId.idx.size();
for (int j = 0; j < pLocId.idx.size(); j++)
{ {
auto idx_iter=lidx.begin(); auto idx_iter=pLocId.idx.begin();
std::advance(idx_iter,j); std::advance(idx_iter,j);
//assert(*idx_iter==lidx.z[j]);
int pLocId_idx=std::distance(Icode.begin(),*idx_iter);
Assignment asgn;
/* Check backwards for a definition of this long register */ /* Check backwards for a definition of this long register */
if (checkBackwarLongDefs(loc_ident_idx,*pLocId,*idx_iter,asgn)) if (findBackwarLongDefs(loc_ident_idx,pLocId,*idx_iter))
{
//assert(initial_size==pLocId.idx.size());
continue; continue;
}
/* If no definition backwards, check forward for a use of this long reg */ /* If no definition backwards, check forward for a use of this long reg */
checkForwardLongDefs(loc_ident_idx,*pLocId,*idx_iter,asgn); findForwardLongUses(loc_ident_idx,pLocId,*idx_iter);
//assert(initial_size==pLocId.idx.size());
} /* end for */ } /* end for */
} }
/* Propagates the long global address across all LOW_LEVEL icodes. /* Propagates the long global address across all LOW_LEVEL icodes.
* Transforms some LOW_LEVEL icodes into HIGH_LEVEL */ * Transforms some LOW_LEVEL icodes into HIGH_LEVEL */
void Function::propLongGlb (Int i, ID *pLocId) void Function::propLongGlb (Int i, const ID &pLocId)
{ {
printf("WARN: Function::propLongGlb not implemented"); printf("WARN: Function::propLongGlb not implemented");
} }
@ -567,14 +568,14 @@ void Function::propLongGlb (Int i, ID *pLocId)
void Function::propLong() void Function::propLong()
{ {
Int i; Int i;
ID *pLocId; /* Pointer to current local identifier */ /* Pointer to current local identifier */
for (i = 0; i < localId.csym(); i++) for (i = 0; i < localId.csym(); i++)
{ {
pLocId = &localId.id_arr[i]; const ID &pLocId(localId.id_arr[i]);
if ((pLocId->type==TYPE_LONG_SIGN) || (pLocId->type==TYPE_LONG_UNSIGN)) if ((pLocId.type==TYPE_LONG_SIGN) || (pLocId.type==TYPE_LONG_UNSIGN))
{ {
switch (pLocId->loc) switch (pLocId.loc)
{ {
case STK_FRAME: case STK_FRAME:
propLongStk (i, pLocId); propLongStk (i, pLocId);

View File

@ -7,11 +7,7 @@
#include <cassert> #include <cassert>
#include "dcc.h" #include "dcc.h"
#include <stdio.h> #include <stdio.h>
#ifdef __BORLAND__
#include <alloc.h>
#else
#include <malloc.h> /* For free() */ #include <malloc.h> /* For free() */
#endif
#include <string.h> #include <string.h>
static Int numInt; /* Number of intervals */ static Int numInt; /* Number of intervals */
@ -31,7 +27,7 @@ bool trivialGraph(BB *G)
static BB *firstOfQueue (queue &Q) static BB *firstOfQueue (queue &Q)
{ {
assert(!Q.empty()); assert(!Q.empty());
BB *res=*Q.begin(); BB *res=Q.front();
Q.pop_front(); Q.pop_front();
return res; return res;
} }
@ -58,7 +54,7 @@ BB *interval::firstOfInt ()
{ {
auto pq = currNode; auto pq = currNode;
if (pq == nodes.end()) if (pq == nodes.end())
return (NULL); return 0;
++currNode; ++currNode;
return *pq; return *pq;
} }
@ -169,7 +165,7 @@ void derSeq_Entry::findIntervals (Function *c)
J = pI; J = pI;
} }
else /* first interval */ else /* first interval */
first = FALSE; first = false;
} }
} }
@ -187,7 +183,7 @@ static void displayIntervals (interval *pI)
{ {
if ((*nodePtr)->correspInt == NULL) /* real BBs */ if ((*nodePtr)->correspInt == NULL) /* real BBs */
printf (" Node: %ld\n", (*nodePtr)->begin()); printf (" Node: %ld\n", (*nodePtr)->begin());
else /* BBs represent intervals */ else // BBs represent intervals
printf (" Node (corresp int): %d\n", (*nodePtr)->correspInt->numInt); printf (" Node (corresp int): %d\n", (*nodePtr)->correspInt->numInt);
++nodePtr; ++nodePtr;
} }
@ -234,8 +230,8 @@ void freeDerivedSeq(derSeq &derivedG)
derSeq_Entry::~derSeq_Entry() derSeq_Entry::~derSeq_Entry()
{ {
freeInterval (&Ii); freeInterval (&Ii);
// if(Gi && Gi->nodeType == INTERVAL_NODE) // if(Gi && Gi->nodeType == INTERVAL_NODE)
// freeCFG (Gi); // freeCFG (Gi);
} }
/* Finds the next order graph of derivedGi->Gi according to its intervals /* Finds the next order graph of derivedGi->Gi according to its intervals
@ -244,8 +240,8 @@ bool Function::nextOrderGraph (derSeq *derivedGi)
{ {
interval *Ii; /* Interval being processed */ interval *Ii; /* Interval being processed */
BB *BBnode, /* New basic block of intervals */ BB *BBnode, /* New basic block of intervals */
*curr, /* BB being checked for out edges */ *curr, /* BB being checked for out edges */
*succ /* Successor node */ *succ /* Successor node */
; ;
//queue *listIi; /* List of intervals */ //queue *listIi; /* List of intervals */
Int i, /* Index to outEdges array */ Int i, /* Index to outEdges array */
@ -302,14 +298,14 @@ bool Function::nextOrderGraph (derSeq *derivedGi)
BBnode = new_entry.Gi; /* BB of an interval */ BBnode = new_entry.Gi; /* BB of an interval */
auto iter= std::find_if(bbs.begin(),bbs.end(), auto iter= std::find_if(bbs.begin(),bbs.end(),
[&edge](BB *node)->bool { return edge.intPtr==node->correspInt;}); [&edge](BB *node)->bool { return edge.intPtr==node->correspInt;});
if(iter==bbs.end()) if(iter==bbs.end())
fatalError (INVALID_INT_BB); fatalError (INVALID_INT_BB);
edge.BBptr = *iter; edge.BBptr = *iter;
(*iter)->inEdges.push_back(0); (*iter)->inEdges.push_back(0);
(*iter)->inEdgeCount++; (*iter)->inEdgeCount++;
}
} }
return (boolT)(! sameGraph); }
return (boolT)(! sameGraph);
} }
@ -352,7 +348,7 @@ byte Function::findDerivedSeq (derSeq *derivedGi)
* means of node splitting. */ * means of node splitting. */
static void nodeSplitting (std::vector<BB *> &G) static void nodeSplitting (std::vector<BB *> &G)
{ {
printf("Attempt to perform node splitting: NOT IMPLEMENTED\n"); fprintf(stderr,"Attempt to perform node splitting: NOT IMPLEMENTED\n");
} }
/* Displays the derived sequence and intervals of the graph G */ /* Displays the derived sequence and intervals of the graph G */
@ -378,19 +374,19 @@ void derSeq::display()
derSeq * Function::checkReducibility() derSeq * Function::checkReducibility()
{ {
derSeq * der_seq; derSeq * der_seq;
byte reducible; /* Reducible graph flag */ byte reducible; /* Reducible graph flag */
numInt = 1; /* reinitialize no. of intervals*/ numInt = 1; /* reinitialize no. of intervals*/
stats.nOrder = 1; /* nOrder(cfg) = 1 */ stats.nOrder = 1; /* nOrder(cfg) = 1 */
der_seq = new derSeq; der_seq = new derSeq;
der_seq->resize(1); der_seq->resize(1);
der_seq->back().Gi = cfg.front(); der_seq->back().Gi = m_cfg.front();
reducible = findDerivedSeq(der_seq); reducible = findDerivedSeq(der_seq);
if (! reducible) if (! reducible)
{ {
flg |= GRAPH_IRRED; flg |= GRAPH_IRRED;
nodeSplitting (cfg); nodeSplitting (m_cfg);
} }
return der_seq; return der_seq;
} }

View File

@ -56,7 +56,7 @@ void Function::controlFlowAnalysis()
if (option.verbose) if (option.verbose)
{ {
printf("\nDepth first traversal - Proc %s\n", name.c_str()); printf("\nDepth first traversal - Proc %s\n", name.c_str());
cfg.front()->displayDfs(); m_cfg.front()->displayDfs();
} }
/* Free storage occupied by this procedure */ /* Free storage occupied by this procedure */
@ -74,9 +74,10 @@ void udm(void)
} }
/* Data flow analysis - eliminate condition codes, extraneous registers /* Data flow analysis - eliminate condition codes, extraneous registers
* and intermediate instructions. Find expressions by forward * and intermediate instructions. Find expressions by forward
* substitution algorithm */ * substitution algorithm */
pProcList.front().dataFlow (0); std::bitset<32> live_regs;
pProcList.front().dataFlow (live_regs);
/* Control flow analysis - structuring algorithm */ /* Control flow analysis - structuring algorithm */
for (auto iter = pProcList.rbegin(); iter!=pProcList.rend(); ++iter) for (auto iter = pProcList.rbegin(); iter!=pProcList.rend(); ++iter)
@ -91,7 +92,7 @@ void udm(void)
void Function::displayCFG() void Function::displayCFG()
{ {
printf("\nBasic Block List - Proc %s", name.c_str()); printf("\nBasic Block List - Proc %s", name.c_str());
for (BB *pBB : cfg) for (BB *pBB : m_cfg)
{ {
pBB->display(); pBB->display();
} }