Basic block is purer range now, it does not hold start and length members, it can be used in range based fors now

This commit is contained in:
Artur K 2012-03-08 00:20:13 +01:00
parent fc0d35cf06
commit 69d848ca78
17 changed files with 403 additions and 400 deletions

View File

@ -27,7 +27,7 @@ struct BB : public llvm::ilist_node<BB>
{ {
private: private:
BB(const BB&); BB(const BB&);
BB() : start(0),length(0),nodeType(0),traversed(0), BB() : nodeType(0),traversed(0),
numHlIcodes(0),flg(0), numHlIcodes(0),flg(0),
inEdges(0), inEdges(0),
edges(0),beenOnH(0),inEdgeCount(0),reachingInt(0), edges(0),beenOnH(0),inEdgeCount(0),reachingInt(0),
@ -38,19 +38,13 @@ private:
} }
//friend class SymbolTableListTraits<BB, Function>; //friend class SymbolTableListTraits<BB, Function>;
//int numInEdges; /* Number of in edges */ iICODE range_start;
int start; /* First instruction offset */ iICODE range_end;
int length; /* No. of instructions this BB */
public: public:
int begin(); iICODE begin();
iICODE begin2(); iICODE end();
iICODE end2(); riICODE rbegin();
int end(); riICODE rend();
int rbegin();
int rend();
riICODE rbegin2();
riICODE rend2();
ICODE &front(); ICODE &front();
ICODE &back(); ICODE &back();
size_t size(); size_t size();

View File

@ -87,10 +87,10 @@ public:
static bool insertSubTreeLongReg(COND_EXPR *exp, COND_EXPR **tree, int longIdx); static bool insertSubTreeLongReg(COND_EXPR *exp, COND_EXPR **tree, int longIdx);
static bool insertSubTreeReg(COND_EXPR *&tree, COND_EXPR *_expr, eReg regi, LOCAL_ID *locsym); static bool insertSubTreeReg(COND_EXPR *&tree, COND_EXPR *_expr, eReg regi, LOCAL_ID *locsym);
public: public:
virtual COND_EXPR *clone(); virtual COND_EXPR *clone() const;
void release(); void release();
void changeBoolOp(condOp newOp); void changeBoolOp(condOp newOp);
COND_EXPR(COND_EXPR &other) COND_EXPR(const COND_EXPR &other)
{ {
type=other.type; type=other.type;
expr=other.expr; expr=other.expr;
@ -104,7 +104,7 @@ public:
} }
virtual ~COND_EXPR() {} virtual ~COND_EXPR() {}
public: public:
virtual COND_EXPR *inverse(); // return new COND_EXPR that is invarse of this virtual COND_EXPR *inverse() const; // return new COND_EXPR that is invarse of this
virtual bool xClear(iICODE f, iICODE t, iICODE lastBBinst, Function *pproc); virtual bool xClear(iICODE f, iICODE t, iICODE lastBBinst, Function *pproc);
virtual COND_EXPR *insertSubTreeReg(COND_EXPR *_expr, eReg regi, LOCAL_ID *locsym); virtual COND_EXPR *insertSubTreeReg(COND_EXPR *_expr, eReg regi, LOCAL_ID *locsym);
virtual COND_EXPR *insertSubTreeLongReg(COND_EXPR *_expr, int longIdx); virtual COND_EXPR *insertSubTreeLongReg(COND_EXPR *_expr, int longIdx);

View File

@ -167,7 +167,7 @@ hlType expType (const COND_EXPR *, Function *);
/* Exported functions from hlicode.c */ /* Exported functions from hlicode.c */
std::string writeCall (Function *, STKFRAME *, Function *, int *); std::string writeCall (Function *, STKFRAME *, Function *, int *);
char *writeJcond (HLTYPE, Function *, int *); char *writeJcond (const HLTYPE &, Function *, int *);
char *writeJcondInv (HLTYPE, Function *, int *); char *writeJcondInv (HLTYPE, Function *, int *);
int power2 (int); int power2 (int);

View File

@ -78,6 +78,8 @@ struct AssignType : public HlTypeSupport
return true; return true;
} }
std::string writeOut(Function *pProc, int *numLoc); std::string writeOut(Function *pProc, int *numLoc);
AssignType() : lhs(0),rhs(0)
{}
}; };
struct ExpType : public HlTypeSupport struct ExpType : public HlTypeSupport
{ {
@ -120,6 +122,7 @@ public:
exp.v=e; exp.v=e;
} }
COND_EXPR * expr() { return exp.v;} COND_EXPR * expr() { return exp.v;}
const COND_EXPR * const expr() const { return exp.v;}
void set(hlIcode i,COND_EXPR *e) void set(hlIcode i,COND_EXPR *e)
{ {
if(i!=HLI_RET) if(i!=HLI_RET)
@ -337,7 +340,7 @@ public:
void setRegDU(eReg regi, operDu du_in); void setRegDU(eReg regi, operDu du_in);
void invalidate(); void invalidate();
void newCallHl(); void newCallHl();
void writeDU(int idx); void writeDU();
condId idType(opLoc sd); condId idType(opLoc sd);
// HLL setting functions // HLL setting functions
// set this icode to be an assign // set this icode to be an assign

View File

@ -20,12 +20,23 @@ BB *BB::Create(int start, int ip, uint8_t nodeType, int numOutEdges, Function *p
pnewBB = new BB; pnewBB = new BB;
pnewBB->nodeType = nodeType; /* Initialise */ pnewBB->nodeType = nodeType; /* Initialise */
pnewBB->start = start;
pnewBB->length = ip - start + 1;
pnewBB->immedDom = NO_DOM; pnewBB->immedDom = NO_DOM;
pnewBB->loopHead = pnewBB->caseHead = pnewBB->caseTail = pnewBB->loopHead = pnewBB->caseHead = pnewBB->caseTail =
pnewBB->latchNode= pnewBB->loopFollow = NO_NODE; pnewBB->latchNode= pnewBB->loopFollow = NO_NODE;
pnewBB->range_start = parent->Icode.begin();
pnewBB->range_end = parent->Icode.begin();
if(start!=-1)
{
advance(pnewBB->range_start,start);
advance(pnewBB->range_end,ip+1);
}
else
{
pnewBB->range_end = parent->Icode.end();
pnewBB->range_end = parent->Icode.end();
}
// pnewBB->range_start = parent->Icode.begin();
if (numOutEdges) if (numOutEdges)
pnewBB->edges.resize(numOutEdges); pnewBB->edges.resize(numOutEdges);
@ -55,10 +66,10 @@ static const char *const s_loopType[] = {"noLoop", "while", "repeat", "loop", "f
void BB::display() void BB::display()
{ {
printf("\nnode type = %s, ", s_nodeType[nodeType]); printf("\nnode type = %s, ", s_nodeType[nodeType]);
printf("start = %ld, length = %ld, #out edges = %ld\n", start, length, edges.size()); printf("start = %ld, length = %ld, #out edges = %ld\n", begin()->loc_ip, size(), edges.size());
for (int i = 0; i < edges.size(); i++) for (int i = 0; i < edges.size(); i++)
printf(" outEdge[%2d] = %ld\n",i, edges[i].BBptr->start); printf(" outEdge[%2d] = %ld\n",i, edges[i].BBptr->begin()->loc_ip);
} }
/***************************************************************************** /*****************************************************************************
* displayDfs - Displays the CFG using a depth first traversal * displayDfs - Displays the CFG using a depth first traversal
@ -71,7 +82,7 @@ void BB::displayDfs()
printf("node type = %s, ", s_nodeType[nodeType]); printf("node type = %s, ", s_nodeType[nodeType]);
printf("start = %ld, length = %ld, #in-edges = %ld, #out-edges = %ld\n", printf("start = %ld, length = %ld, #in-edges = %ld, #out-edges = %ld\n",
start, length, inEdges.size(), edges.size()); begin()->loc_ip, size(), inEdges.size(), edges.size());
printf("dfsFirst = %ld, dfsLast = %ld, immed dom = %ld\n", printf("dfsFirst = %ld, dfsLast = %ld, immed dom = %ld\n",
dfsFirstNum, dfsLastNum, dfsFirstNum, dfsLastNum,
immedDom == MAX ? -1 : immedDom); immedDom == MAX ? -1 : immedDom);
@ -88,16 +99,33 @@ void BB::displayDfs()
if (nodeType == INTERVAL_NODE) if (nodeType == INTERVAL_NODE)
printf("corresponding interval = %ld\n", correspInt->numInt); printf("corresponding interval = %ld\n", correspInt->numInt);
else else
for (i = 0; i < inEdges.size(); i++) {
printf (" inEdge[%ld] = %ld\n", i, inEdges[i]->begin()); #ifdef _lint
for(auto iter=inEdges.begin(); iter!=inEdges.end(); ++iter)
{
BB *node(*iter);
#else
for(BB *node : inEdges)
{
#endif
printf (" inEdge[%ld] = %ld\n", i, node->begin()->loc_ip);
}
}
/* Display out edges information */ /* Display out edges information */
for (i = 0; i < edges.size(); i++) #ifdef _lint
for(auto iter=edges.begin(); iter!=edges.end(); ++iter)
{
TYPEADR_TYPE &edg(*iter);
#else
for(TYPEADR_TYPE &edg : edges)
{
#endif
if (nodeType == INTERVAL_NODE) if (nodeType == INTERVAL_NODE)
printf(" outEdge[%ld] = %ld\n", i, printf(" outEdge[%ld] = %ld\n", i, edg.BBptr->correspInt->numInt);
edges[i].BBptr->correspInt->numInt);
else else
printf(" outEdge[%d] = %ld\n", i, edges[i].BBptr->begin()); printf(" outEdge[%d] = %ld\n", i, edg.BBptr->begin()->loc_ip);
}
printf("----\n"); printf("----\n");
/* Recursive call on successors of current node */ /* Recursive call on successors of current node */
@ -344,71 +372,62 @@ void BB::writeBB(int lev, Function * pProc, int *numLoc)
//for (i = start, last = i + length; i < last; i++) //for (i = start, last = i + length; i < last; i++)
/* Generate code for each hlicode that is not a HLI_JCOND */ /* Generate code for each hlicode that is not a HLI_JCOND */
int idx=start; //for();
for(iICODE hli=begin2(); hli!=end2(); ++hli) #ifdef _lint
for(iICODE hli=begin(); hli!=end(); ++hli)
{ {
if ((hli->type == HIGH_LEVEL) && (hli->invalid == FALSE)) ICODE &pHli(*hli);
#else
for(ICODE &pHli : *this)
{
#endif
if ((pHli.type == HIGH_LEVEL) && ( pHli.valid() )) //TODO: use filtering range here.
{ {
std::string line = hli->hl()->write1HlIcode(pProc, numLoc); std::string line = pHli.hl()->write1HlIcode(pProc, numLoc);
if (!line.empty()) if (!line.empty())
{ {
cCode.appendCode( "%s%s", indent(lev), line.c_str()); cCode.appendCode( "%s%s", indent(lev), line.c_str());
stats.numHLIcode++; stats.numHLIcode++;
} }
if (option.verbose) if (option.verbose)
hli->writeDU(idx); pHli.writeDU();
} }
idx++;
} }
} }
int BB::begin() //int BB::beginIdx()
//{
// return start;
//}
iICODE BB::begin()
{ {
return start; return range_start;
} }
iICODE BB::begin2() iICODE BB::end()
{ {
iICODE result(Parent->Icode.begin()); return range_end;
advance(result,start);
return result;
}
iICODE BB::end2()
{
iICODE result(Parent->Icode.begin());
advance(result,start+length);
return result;
}
int BB::rbegin()
{
return start+length-1;
}
int BB::end()
{
return start+length;
} }
ICODE &BB::back() ICODE &BB::back()
{ {
return *rbegin2(); return *rbegin();
} }
size_t BB::size() size_t BB::size()
{ {
return length; return distance(range_start,range_end);
} }
ICODE &BB::front() ICODE &BB::front()
{ {
return *begin2(); return *begin();
} }
riICODE BB::rbegin2() riICODE BB::rbegin()
{ {
riICODE res(end2()); return riICODE(end());
assert(res->loc_ip==rbegin());
return res;
} }
riICODE BB::rend2() riICODE BB::rend()
{ {
return riICODE(begin2()); return riICODE(begin());
} }

View File

@ -797,7 +797,7 @@ string walkCondExpr (const COND_EXPR* expr, Function * pProc, int* numLoc)
/* Makes a copy of the given expression. Allocates newExp storage for each /* Makes a copy of the given expression. Allocates newExp storage for each
* node. Returns the copy. */ * node. Returns the copy. */
//lint -sem(COND_EXPR::clone, @p!=0) //lint -sem(COND_EXPR::clone, @p!=0)
COND_EXPR *COND_EXPR::clone() COND_EXPR *COND_EXPR::clone() const
{ {
COND_EXPR* newExp=0; /* Expression node copy */ COND_EXPR* newExp=0; /* Expression node copy */

View File

@ -309,7 +309,7 @@ void Function::codeGen (std::ostream &fs)
pBB = m_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()->loc_ip, pBB->begin()->loc_ip+pBB->size());
printf (" LiveUse = "); printf (" LiveUse = ");
writeBitVector (pBB->liveUse); writeBitVector (pBB->liveUse);
printf ("\n Def = "); printf ("\n Def = ");

View File

@ -48,7 +48,7 @@ int numKeys; /* Number of hash table entries (keys) */
int numVert; /* Number of vertices in the graph (also size of g[]) */ int numVert; /* Number of vertices in the graph (also size of g[]) */
unsigned PatLen; /* Size of the keys (pattern length) */ unsigned PatLen; /* Size of the keys (pattern length) */
unsigned SymLen; /* Max size of the symbols, including null */ unsigned SymLen; /* Max size of the symbols, including null */
FILE *f; /* File being read */ static FILE *g_file; /* File being read */
static char sSigName[100]; /* Full path name of .sig file */ static char sSigName[100]; /* Full path name of .sig file */
static uint16_t *T1base, *T2base; /* Pointers to start of T1, T2 */ static uint16_t *T1base, *T2base; /* Pointers to start of T1, T2 */
@ -61,9 +61,9 @@ static int numArg; /* Number of param names actually stored
#define DCCLIBS "dcclibs.dat" /* Name of the prototypes data file */ #define DCCLIBS "dcclibs.dat" /* Name of the prototypes data file */
/* prototypes */ /* prototypes */
void grab(int n, FILE *f); void grab(int n, FILE *_file);
uint16_t readFileShort(FILE *f); uint16_t readFileShort(FILE *g_file);
void readFileSection(uint16_t* p, int len, FILE *f); void readFileSection(uint16_t* p, int len, FILE *g_file);
void cleanup(void); void cleanup(void);
void checkStartup(STATE *state); void checkStartup(STATE *state);
void readProtoFile(void); void readProtoFile(void);
@ -231,9 +231,9 @@ static uint8_t pattMainMedium[] =
0xFF, 0x36, WILD, WILD, /* Push argv */ 0xFF, 0x36, WILD, WILD, /* Push argv */
0xFF, 0x36, WILD, WILD, /* Push argc */ 0xFF, 0x36, WILD, WILD, /* Push argc */
0x9A, WILD, WILD, WILD, WILD /* call far _main */ 0x9A, WILD, WILD, WILD, WILD /* call far _main */
/* 0x50 /* push ax */ /* 0x50 /* push ax */
/* 0x0E, /* push cs NB not tested Borland */ /* 0x0E, /* push cs NB not tested Borland */
/* 0xE8 /* call _exit */ /* 0xE8 /* call _exit */
}; };
/* Num bytes from start pattern to the relative offset of main() */ /* Num bytes from start pattern to the relative offset of main() */
#define OFFMAINMEDIUM 13 #define OFFMAINMEDIUM 13
@ -300,7 +300,7 @@ void SetupLibCheck(void)
uint16_t w, len; uint16_t w, len;
int i; int i;
if ((f = fopen(sSigName, "rb")) == NULL) if ((g_file = fopen(sSigName, "rb")) == NULL)
{ {
printf("Warning: cannot open signature file %s\n", sSigName); printf("Warning: cannot open signature file %s\n", sSigName);
return; return;
@ -311,16 +311,16 @@ void SetupLibCheck(void)
prog.bSigs = FALSE; /* False unless everything goes right */ prog.bSigs = FALSE; /* False unless everything goes right */
/* Read the parameters */ /* Read the parameters */
grab(4, f); grab(4, g_file);
if (memcmp("dccs", buf, 4) != 0) if (memcmp("dccs", buf, 4) != 0)
{ {
printf("Not a dcc signature file!\n"); printf("Not a dcc signature file!\n");
exit(3); exit(3);
} }
numKeys = readFileShort(f); numKeys = readFileShort(g_file);
numVert = readFileShort(f); numVert = readFileShort(g_file);
PatLen = readFileShort(f); PatLen = readFileShort(g_file);
SymLen = readFileShort(f); SymLen = readFileShort(g_file);
if ((PatLen != PATLEN) || (SymLen != SYMLEN)) if ((PatLen != PATLEN) || (SymLen != SYMLEN))
{ {
printf("Sorry! Compiled for sym and pattern lengths of %d and %d\n", printf("Sorry! Compiled for sym and pattern lengths of %d and %d\n",
@ -342,50 +342,50 @@ void SetupLibCheck(void)
g = g_pattern_hasher.readG(); g = g_pattern_hasher.readG();
/* Read T1 and T2 tables */ /* Read T1 and T2 tables */
grab(2, f); grab(2, g_file);
if (memcmp("T1", buf, 2) != 0) if (memcmp("T1", buf, 2) != 0)
{ {
printf("Expected 'T1'\n"); printf("Expected 'T1'\n");
exit(3); exit(3);
} }
len = (uint16_t) (PatLen * 256 * sizeof(uint16_t)); len = (uint16_t) (PatLen * 256 * sizeof(uint16_t));
w = readFileShort(f); w = readFileShort(g_file);
if (w != len) if (w != len)
{ {
printf("Problem with size of T1: file %d, calc %d\n", w, len); printf("Problem with size of T1: file %d, calc %d\n", w, len);
exit(4); exit(4);
} }
readFileSection(T1base, len, f); readFileSection(T1base, len, g_file);
grab(2, f); grab(2, g_file);
if (memcmp("T2", buf, 2) != 0) if (memcmp("T2", buf, 2) != 0)
{ {
printf("Expected 'T2'\n"); printf("Expected 'T2'\n");
exit(3); exit(3);
} }
w = readFileShort(f); w = readFileShort(g_file);
if (w != len) if (w != len)
{ {
printf("Problem with size of T2: file %d, calc %d\n", w, len); printf("Problem with size of T2: file %d, calc %d\n", w, len);
exit(4); exit(4);
} }
readFileSection(T2base, len, f); readFileSection(T2base, len, g_file);
/* Now read the function g[] */ /* Now read the function g[] */
grab(2, f); grab(2, g_file);
if (memcmp("gg", buf, 2) != 0) if (memcmp("gg", buf, 2) != 0)
{ {
printf("Expected 'gg'\n"); printf("Expected 'gg'\n");
exit(3); exit(3);
} }
len = (uint16_t)(numVert * sizeof(uint16_t)); len = (uint16_t)(numVert * sizeof(uint16_t));
w = readFileShort(f); w = readFileShort(g_file);
if (w != len) if (w != len)
{ {
printf("Problem with size of g[]: file %d, calc %d\n", w, len); printf("Problem with size of g[]: file %d, calc %d\n", w, len);
exit(4); exit(4);
} }
readFileSection(g, len, f); readFileSection(g, len, g_file);
/* This is now the hash table */ /* This is now the hash table */
@ -396,13 +396,13 @@ void SetupLibCheck(void)
printf("Could not allocate hash table\n"); printf("Could not allocate hash table\n");
exit(1); exit(1);
} }
grab(2, f); grab(2, g_file);
if (memcmp("ht", buf, 2) != 0) if (memcmp("ht", buf, 2) != 0)
{ {
printf("Expected 'ht'\n"); printf("Expected 'ht'\n");
exit(3); exit(3);
} }
w = readFileShort(f); w = readFileShort(g_file);
if (w != numKeys * (SymLen + PatLen + sizeof(uint16_t))) if (w != numKeys * (SymLen + PatLen + sizeof(uint16_t)))
{ {
printf("Problem with size of hash table: file %d, calc %d\n", w, len); printf("Problem with size of hash table: file %d, calc %d\n", w, len);
@ -412,13 +412,13 @@ void SetupLibCheck(void)
for (i=0; i < numKeys; i++) for (i=0; i < numKeys; i++)
{ {
if (fread(&ht[i], 1, SymLen + PatLen, f) != SymLen + PatLen) if (fread(&ht[i], 1, SymLen + PatLen, g_file) != SymLen + PatLen)
{ {
printf("Could not read signature\n"); printf("Could not read signature\n");
exit(11); exit(11);
} }
} }
fclose(f); fclose(g_file);
prog.bSigs = TRUE; prog.bSigs = TRUE;
} }
@ -528,9 +528,9 @@ bool LibCheck(Function & pProc)
void grab(int n, FILE *f) void grab(int n, FILE *_file)
{ {
if (fread(buf, 1, n, f) != (unsigned)n) if (fread(buf, 1, n, _file) != (unsigned)n)
{ {
printf("Could not grab\n"); printf("Could not grab\n");
exit(11); exit(11);
@ -982,36 +982,4 @@ searchPList(char *name)
} }
} }
#if DEBUG_HEAP
void
checkHeap(char *msg)
/* HEAPCHK.C: This program checks the heap for
* consistency and prints an appropriate message.
*/
{
int heapstatus;
printf("%s\n", msg);
/* Check heap status */
heapstatus = _heapchk();
switch( heapstatus )
{
case _HEAPOK:
printf(" OK - heap is fine\n" );
break;
case _HEAPEMPTY:
printf(" OK - heap is empty\n" );
break;
case _HEAPBADBEGIN:
printf( "ERROR - bad start of heap\n" );
break;
case _HEAPBADNODE:
printf( "ERROR - bad node in heap\n" );
break;
}
}
#endif

View File

@ -8,11 +8,7 @@
#include "dcc.h" #include "dcc.h"
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#if __BORLAND__
#include <alloc.h>
#else
#include <malloc.h> #include <malloc.h>
#endif
//typedef struct list { //typedef struct list {
// int nodeIdx; // int nodeIdx;
@ -69,7 +65,7 @@ static int commonDom (int currImmDom, int predImmDom, Function * pProc)
void Function::findImmedDom () void Function::findImmedDom ()
{ {
BB * currNode; BB * currNode;
int currIdx, j, predIdx; int currIdx, predIdx;
for (currIdx = 0; currIdx < numBBs; currIdx++) for (currIdx = 0; currIdx < numBBs; currIdx++)
{ {
@ -525,11 +521,11 @@ void Function::structIfs ()
* into one block with the appropriate condition */ * into one block with the appropriate condition */
void Function::compoundCond() void Function::compoundCond()
{ {
int i, j, k, numOutEdges; int i; //j, k, numOutEdges
BB * pbb, * t, * e, * obb,* pred; BB * pbb, * t, * e, * obb;//,* pred;
ICODE * picode, * ticode; ICODE * picode, * ticode;
COND_EXPR *exp; //COND_EXPR *exp;
TYPEADR_TYPE *edges; //TYPEADR_TYPE *edges;
boolT change; boolT change;
change = TRUE; change = TRUE;

View File

@ -116,7 +116,7 @@ void Function::elimCondCodes ()
boolT notSup; /* Use/def combination not supported */ boolT notSup; /* Use/def combination not supported */
COND_EXPR *rhs; /* Source operand */ COND_EXPR *rhs; /* Source operand */
COND_EXPR *lhs; /* Destination operand */ COND_EXPR *lhs; /* Destination operand */
COND_EXPR *exp; /* Boolean expression */ COND_EXPR *_expr; /* Boolean expression */
BB * pBB; /* Pointer to BBs in dfs last ordering */ BB * pBB; /* Pointer to BBs in dfs last ordering */
riICODE useAt; /* Instruction that used flag */ riICODE useAt; /* Instruction that used flag */
riICODE defAt; /* Instruction that defined flag */ riICODE defAt; /* Instruction that defined flag */
@ -128,25 +128,27 @@ void Function::elimCondCodes ()
// auto v(pBB | boost::adaptors::reversed); // auto v(pBB | boost::adaptors::reversed);
// for (const ICODE &useAt : v) // for (const ICODE &useAt : v)
// {} // {}
for (useAt = pBB->rbegin2(); useAt != pBB->rend2(); useAt++) assert(distance(pBB->rbegin(),pBB->rend())==pBB->size());
for (useAt = pBB->rbegin(); useAt != pBB->rend(); useAt++)
{ {
llIcode useAtOp = useAt->ll()->getOpcode(); llIcode useAtOp = useAt->ll()->getOpcode();
if ((useAt->type == LOW_LEVEL) && (useAt->valid()) && (use = useAt->ll()->flagDU.u)) use = useAt->ll()->flagDU.u;
if ((useAt->type != LOW_LEVEL) || ( ! useAt->valid() ) || ( 0 == use ))
continue;
/* Find definition within the same basic block */
defAt=useAt;
++defAt;
for (; defAt != pBB->rend(); defAt++)
{ {
/* Find definition within the same basic block */ def = defAt->ll()->flagDU.d;
defAt=useAt; if ((use & def) != use)
++defAt; continue;
for (; defAt != pBB->rend2(); defAt++) notSup = false;
if ((useAtOp >= iJB) && (useAtOp <= iJNS))
{ {
def = defAt->ll()->flagDU.d; iICODE befDefAt = (++riICODE(defAt)).base();
if ((use & def) != use) switch (defAt->ll()->getOpcode())
continue;
notSup = FALSE;
if ((useAtOp >= iJB) && (useAtOp <= iJNS))
{ {
iICODE befDefAt = (++riICODE(defAt)).base();
switch (defAt->ll()->getOpcode())
{
case iCMP: case iCMP:
rhs = srcIdent (*defAt->ll(), this, befDefAt,*useAt, eUSE); rhs = srcIdent (*defAt->ll(), this, befDefAt,*useAt, eUSE);
lhs = dstIdent (*defAt->ll(), this, befDefAt,*useAt, eUSE); lhs = dstIdent (*defAt->ll(), this, befDefAt,*useAt, eUSE);
@ -172,57 +174,58 @@ void Function::elimCondCodes ()
break; break;
default: default:
notSup = TRUE; notSup = true;
std::cout << hex<<defAt->loc_ip; std::cout << hex<<defAt->loc_ip;
reportError (JX_NOT_DEF, defAt->ll()->getOpcode()); reportError (JX_NOT_DEF, defAt->ll()->getOpcode());
flg |= PROC_ASM; /* generate asm */ flg |= PROC_ASM; /* generate asm */
}
if (! notSup)
{
exp = COND_EXPR::boolOp (lhs, rhs,condOpJCond[useAtOp-iJB]);
useAt->setJCond(exp);
}
} }
if (! notSup)
else if (useAtOp == iJCXZ)
{ {
lhs = COND_EXPR::idReg (rCX, 0, &localId); assert(lhs);
useAt->setRegDU (rCX, eUSE); assert(rhs);
rhs = COND_EXPR::idKte (0, 2); _expr = COND_EXPR::boolOp (lhs, rhs,condOpJCond[useAtOp-iJB]);
exp = COND_EXPR::boolOp (lhs, rhs, EQUAL); useAt->setJCond(_expr);
useAt->setJCond(exp);
} }
// else if (useAt->getOpcode() == iRCL)
// {
// }
else
{
ICODE &a(*defAt);
ICODE &b(*useAt);
reportError (NOT_DEF_USE,a.ll()->getOpcode(),b.ll()->getOpcode());
flg |= PROC_ASM; /* generate asm */
}
break;
} }
/* Check for extended basic block */ else if (useAtOp == iJCXZ)
if ((pBB->size() == 1) &&(useAtOp >= iJB) && (useAtOp <= iJNS))
{ {
ICODE & prev(pBB->back()); /* For extended basic blocks - previous icode inst */ lhs = COND_EXPR::idReg (rCX, 0, &localId);
if (prev.hl()->opcode == HLI_JCOND) useAt->setRegDU (rCX, eUSE);
{ rhs = COND_EXPR::idKte (0, 2);
exp = prev.hl()->expr()->clone(); _expr = COND_EXPR::boolOp (lhs, rhs, EQUAL);
exp->changeBoolOp (condOpJCond[useAtOp-iJB]); useAt->setJCond(_expr);
useAt->copyDU(prev, eUSE, eUSE);
useAt->setJCond(exp);
}
} }
/* Error - definition not found for use of a cond code */ // else if (useAt->getOpcode() == iRCL)
else if (defAt == pBB->rend2()) // {
// }
else
{ {
reportError(DEF_NOT_FOUND,useAtOp); ICODE &a(*defAt);
//fatalError (DEF_NOT_FOUND, Icode.getOpcode(useAt-1)); ICODE &b(*useAt);
reportError (NOT_DEF_USE,a.ll()->getOpcode(),b.ll()->getOpcode());
flg |= PROC_ASM; /* generate asm */
} }
break;
}
/* Check for extended basic block */
if ((pBB->size() == 1) &&(useAtOp >= iJB) && (useAtOp <= iJNS))
{
ICODE & _prev(pBB->back()); /* For extended basic blocks - previous icode inst */
if (_prev.hl()->opcode == HLI_JCOND)
{
_expr = _prev.hl()->expr()->clone();
_expr->changeBoolOp (condOpJCond[useAtOp-iJB]);
useAt->copyDU(_prev, eUSE, eUSE);
useAt->setJCond(_expr);
}
}
/* Error - definition not found for use of a cond code */
else if (defAt == pBB->rend())
{
reportError(DEF_NOT_FOUND,useAtOp);
//fatalError (DEF_NOT_FOUND, Icode.getOpcode(useAt-1));
} }
} }
} }
@ -248,7 +251,7 @@ void Function::genLiveKtes ()
pbb = m_dfsLast[i]; 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->begin(); j != pbb->end(); j++)
{ {
if ((j->type == HIGH_LEVEL) && (j->invalid == FALSE)) if ((j->type == HIGH_LEVEL) && (j->invalid == FALSE))
{ {
@ -267,7 +270,6 @@ void Function::genLiveKtes ()
* Propagates register usage information to the procedure call. */ * Propagates register usage information to the procedure call. */
void Function::liveRegAnalysis (std::bitset<32> &in_liveOut) void Function::liveRegAnalysis (std::bitset<32> &in_liveOut)
{ {
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 */
@ -306,11 +308,11 @@ void Function::liveRegAnalysis (std::bitset<32> &in_liveOut)
/* Get return expression of function */ /* Get return expression of function */
if (flg & PROC_IS_FUNC) if (flg & PROC_IS_FUNC)
{ {
auto picode = pbb->rbegin2(); /* icode of function return */ auto picode = pbb->rbegin(); /* icode of function return */
if (picode->hl()->opcode == HLI_RET) if (picode->hl()->opcode == HLI_RET)
{ {
//pbb->back().loc_ip //pbb->back().loc_ip
picode->hl()->expr(COND_EXPR::idID (&retVal, &localId, (++pbb->rbegin2()).base())); picode->hl()->expr(COND_EXPR::idID (&retVal, &localId, (++pbb->rbegin()).base()));
picode->du.use = in_liveOut; picode->du.use = in_liveOut;
} }
} }
@ -362,7 +364,8 @@ void Function::liveRegAnalysis (std::bitset<32> &in_liveOut)
ticode.du1.numRegsDef = 1; ticode.du1.numRegsDef = 1;
break; break;
default: default:
fprintf(stderr,"Function::liveRegAnalysis : Unknown return type %d\n",pcallee->retVal.type); ticode.du1.numRegsDef = 0;
fprintf(stderr,"Function::liveRegAnalysis : Unknown return type %d, assume 0\n",pcallee->retVal.type);
} /*eos*/ } /*eos*/
/* Propagate def/use results to calling icode */ /* Propagate def/use results to calling icode */
@ -411,8 +414,8 @@ void BB::genDU1()
* Note that register variables should not be considered registers. * Note that register variables should not be considered registers.
*/ */
assert(0!=Parent); assert(0!=Parent);
lastInst = this->end2(); lastInst = this->end();
for (picode = this->begin2(); picode != lastInst; picode++) for (picode = this->begin(); picode != lastInst; picode++)
{ {
if (picode->type != HIGH_LEVEL) if (picode->type != HIGH_LEVEL)
continue; continue;
@ -470,7 +473,7 @@ void BB::genDU1()
(picode->hl()->call.proc->flg & PROC_IS_FUNC)) (picode->hl()->call.proc->flg & PROC_IS_FUNC))
{ {
tbb = this->edges[0].BBptr; tbb = this->edges[0].BBptr;
for (ticode = tbb->begin2(); ticode != tbb->end2(); ticode++) for (ticode = tbb->begin(); ticode != tbb->end(); ticode++)
{ {
if (ticode->type != HIGH_LEVEL) if (ticode->type != HIGH_LEVEL)
continue; continue;
@ -511,7 +514,7 @@ void BB::genDU1()
/* Backpatch any uses of this instruction, within /* Backpatch any uses of this instruction, within
* the same BB, if the instruction was invalidated */ * the same BB, if the instruction was invalidated */
for (auto ticode = riICODE(picode); ticode != this->rend2(); ticode++) for (auto ticode = riICODE(picode); ticode != this->rend(); ticode++)
{ {
ticode->du1.remove(0,picode); ticode->du1.remove(0,picode);
} }
@ -835,9 +838,9 @@ void Function::findExps()
pbb = m_dfsLast[i]; pbb = m_dfsLast[i];
if (pbb->flg & INVALID_BB) if (pbb->flg & INVALID_BB)
continue; continue;
lastInst = pbb->end2(); lastInst = pbb->end();
numHlIcodes = 0; numHlIcodes = 0;
for (picode = pbb->begin2(); picode != lastInst; picode++) for (picode = pbb->begin(); picode != lastInst; picode++)
{ {
if ((picode->type == HIGH_LEVEL) && (picode->invalid == FALSE)) if ((picode->type == HIGH_LEVEL) && (picode->invalid == FALSE))
{ {
@ -918,7 +921,7 @@ void Function::findExps()
res = COND_EXPR::insertSubTreeReg (ti_hl->asgn.rhs,exp, retVal->id.regi, &localId); res = COND_EXPR::insertSubTreeReg (ti_hl->asgn.rhs,exp, retVal->id.regi, &localId);
if (! res) if (! res)
COND_EXPR::insertSubTreeReg (ti_hl->asgn.lhs, exp,retVal->id.regi, &localId); COND_EXPR::insertSubTreeReg (ti_hl->asgn.lhs, exp,retVal->id.regi, &localId);
/*** TODO: HERE missing: 2 regs ****/ //TODO: HERE missing: 2 regs
picode->invalidate(); picode->invalidate();
numHlIcodes--; numHlIcodes--;
break; break;
@ -1131,7 +1134,7 @@ void Function::dataFlow(std::bitset<32> &liveOut)
} }
/* Data flow analysis */ /* Data flow analysis */
liveAnal = TRUE; liveAnal = true;
elimCondCodes(); elimCondCodes();
genLiveKtes(); genLiveKtes();
liveRegAnalysis (liveOut); /* calls dataFlow() recursively */ liveRegAnalysis (liveOut); /* calls dataFlow() recursively */

View File

@ -152,7 +152,7 @@ CondJumps:
return ; return ;
} }
auto iter2=std::find_if(heldBBs.begin(),heldBBs.end(), auto iter2=std::find_if(heldBBs.begin(),heldBBs.end(),
[ip](BB *psBB)->bool {return psBB->begin()==ip;}); [ip](BB *psBB)->bool {return psBB->begin()->loc_ip==ip;});
if(iter2==heldBBs.end()) if(iter2==heldBBs.end())
fatalError(NO_BB, ip, name.c_str()); fatalError(NO_BB, ip, name.c_str());
psBB = *iter2; psBB = *iter2;
@ -223,23 +223,33 @@ 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 */
#ifdef _lint
auto iter=m_cfg.begin(); auto iter=m_cfg.begin();
for (;iter!=m_cfg.end(); ++iter) for (;iter!=m_cfg.end(); ++iter)
{ {
pBB = *iter; BB *pBB(*iter);
#else
for (BB *pBB : m_cfg)
{
#endif
if(pBB->inEdges.empty() || (pBB->nodeType != ONE_BRANCH && pBB->nodeType != TWO_BRANCH)) if(pBB->inEdges.empty() || (pBB->nodeType != ONE_BRANCH && pBB->nodeType != TWO_BRANCH))
continue; continue;
for (i = 0; i < pBB->edges.size(); i++) #ifdef _lint
for (auto iter2=pBB->edges().begin(); iter2!=pBB->edges().end(); ++iter2)
{ {
ip = pBB->rbegin(); TYPEADR_TYPE &edgeRef(*iter);
pNxt = pBB->edges[i].BBptr->rmJMP(ip, pBB->edges[i].BBptr); #else
for (TYPEADR_TYPE &edgeRef : pBB->edges)
{
#endif
ip = pBB->rbegin()->loc_ip;
pNxt = edgeRef.BBptr->rmJMP(ip, edgeRef.BBptr);
if (not pBB->edges.empty()) /* Might have been clobbered */ if (not pBB->edges.empty()) /* Might have been clobbered */
{ {
pBB->edges[i].BBptr = pNxt; edgeRef.BBptr = pNxt;
assert(pBB->back().loc_ip==ip); assert(pBB->back().loc_ip==ip);
pBB->back().ll()->SetImmediateOp((uint32_t)pNxt->begin()); pBB->back().ll()->SetImmediateOp((uint32_t)pNxt->begin()->loc_ip);
//Icode[ip].SetImmediateOp((uint32_t)pNxt->begin());
} }
} }
} }
@ -353,15 +363,15 @@ void BB::mergeFallThrough( CIcodeRec &Icode)
assert(Parent==pChild->Parent); assert(Parent==pChild->Parent);
if(back().loc_ip>pChild->front().loc_ip) // back edege if(back().loc_ip>pChild->front().loc_ip) // back edege
break; break;
auto iter=std::find_if(this->end2(),pChild->begin2(),[](ICODE &c) auto iter=std::find_if(this->end(),pChild->begin(),[](ICODE &c)
{return not c.ll()->testFlags(NO_CODE);}); {return not c.ll()->testFlags(NO_CODE);});
if (iter != pChild->begin2()) if (iter != pChild->begin())
break; break;
back().ll()->setFlags(NO_CODE); back().ll()->setFlags(NO_CODE);
back().invalidate(); back().invalidate();
nodeType = FALL_NODE; nodeType = FALL_NODE;
length--; range_end--;
} }
/* If there's no other edges into child can merge */ /* If there's no other edges into child can merge */
@ -369,7 +379,7 @@ void BB::mergeFallThrough( CIcodeRec &Icode)
break; break;
nodeType = pChild->nodeType; nodeType = pChild->nodeType;
length = (pChild->start - start) + pChild->length ; range_end = pChild->range_end;
pChild->front().ll()->clrFlags(TARGET); pChild->front().ll()->clrFlags(TARGET);
edges.swap(pChild->edges); edges.swap(pChild->edges);

View File

@ -14,14 +14,14 @@ using namespace std;
/* Masks off bits set by duReg[] */ /* Masks off bits set by duReg[] */
std::bitset<32> maskDuReg[] = { 0x00, std::bitset<32> maskDuReg[] = { 0x00,
0xFEEFFE, 0xFDDFFD, 0xFBB00B, 0xF77007, /* uint16_t regs */ 0xFEEFFE, 0xFDDFFD, 0xFBB00B, 0xF77007, /* uint16_t regs */
0xFFFFEF, 0xFFFFDF, 0xFFFFBF, 0xFFFF7F, 0xFFFFEF, 0xFFFFDF, 0xFFFFBF, 0xFFFF7F,
0xFFFEFF, 0xFFFDFF, 0xFFFBFF, 0xFFF7FF, /* seg regs */ 0xFFFEFF, 0xFFFDFF, 0xFFFBFF, 0xFFF7FF, /* seg regs */
0xFFEFFF, 0xFFDFFF, 0xFFBFFF, 0xFF7FFF, /* uint8_t regs */ 0xFFEFFF, 0xFFDFFF, 0xFFBFFF, 0xFF7FFF, /* uint8_t regs */
0xFEFFFF, 0xFDFFFF, 0xFBFFFF, 0xF7FFFF, 0xFEFFFF, 0xFDFFFF, 0xFBFFFF, 0xF7FFFF,
0xEFFFFF, /* tmp reg */ 0xEFFFFF, /* tmp reg */
0xFFFFB7, 0xFFFF77, 0xFFFF9F, 0xFFFF5F, /* index regs */ 0xFFFFB7, 0xFFFF77, 0xFFFF9F, 0xFFFF5F, /* index regs */
0xFFFFBF, 0xFFFF7F, 0xFFFFDF, 0xFFFFF7 }; 0xFFFFBF, 0xFFFF7F, 0xFFFFDF, 0xFFFFF7 };
static char buf[lineSize]; /* Line buffer for hl icode output */ static char buf[lineSize]; /* Line buffer for hl icode output */
@ -141,138 +141,138 @@ HLTYPE LLInst::toHighLevel(COND_EXPR *lhs,COND_EXPR *rhs,Function *func)
switch (getOpcode()) switch (getOpcode())
{ {
case iADD: case iADD:
rhs = COND_EXPR::boolOp (lhs, rhs, ADD); rhs = COND_EXPR::boolOp (lhs, rhs, ADD);
res.setAsgn(lhs, rhs); res.setAsgn(lhs, rhs);
break; break;
case iAND: case iAND:
rhs = COND_EXPR::boolOp (lhs, rhs, AND); rhs = COND_EXPR::boolOp (lhs, rhs, AND);
res.setAsgn(lhs, rhs); res.setAsgn(lhs, rhs);
break; break;
case iCALL: case iCALL:
case iCALLF: case iCALLF:
//TODO: this is noop pIcode->checkHlCall(); //TODO: this is noop pIcode->checkHlCall();
res=createCall(); res=createCall();
break; break;
case iDEC: case iDEC:
rhs = COND_EXPR::idKte (1, 2); rhs = COND_EXPR::idKte (1, 2);
rhs = COND_EXPR::boolOp (lhs, rhs, SUB); rhs = COND_EXPR::boolOp (lhs, rhs, SUB);
res.setAsgn(lhs, rhs); res.setAsgn(lhs, rhs);
break; break;
case iDIV: case iDIV:
case iIDIV:/* should be signed div */ case iIDIV:/* should be signed div */
rhs = COND_EXPR::boolOp (lhs, rhs, DIV); rhs = COND_EXPR::boolOp (lhs, rhs, DIV);
if ( ll->testFlags(B) ) if ( ll->testFlags(B) )
{ {
lhs = COND_EXPR::idReg (rAL, 0, &localId); lhs = COND_EXPR::idReg (rAL, 0, &localId);
pIcode->setRegDU( rAL, eDEF); pIcode->setRegDU( rAL, eDEF);
} }
else else
{ {
lhs = COND_EXPR::idReg (rAX, 0, &localId); lhs = COND_EXPR::idReg (rAX, 0, &localId);
pIcode->setRegDU( rAX, eDEF); pIcode->setRegDU( rAX, eDEF);
} }
res.setAsgn(lhs, rhs); res.setAsgn(lhs, rhs);
break; break;
case iIMUL: case iIMUL:
rhs = COND_EXPR::boolOp (lhs, rhs, MUL); rhs = COND_EXPR::boolOp (lhs, rhs, MUL);
lhs = COND_EXPR::id (*pIcode, LHS_OP, func, i, *pIcode, NONE); lhs = COND_EXPR::id (*pIcode, LHS_OP, func, i, *pIcode, NONE);
res.setAsgn(lhs, rhs); res.setAsgn(lhs, rhs);
break; break;
case iINC: case iINC:
rhs = COND_EXPR::idKte (1, 2); rhs = COND_EXPR::idKte (1, 2);
rhs = COND_EXPR::boolOp (lhs, rhs, ADD); rhs = COND_EXPR::boolOp (lhs, rhs, ADD);
res.setAsgn(lhs, rhs); res.setAsgn(lhs, rhs);
break; break;
case iLEA: case iLEA:
rhs = COND_EXPR::unary (ADDRESSOF, rhs); rhs = COND_EXPR::unary (ADDRESSOF, rhs);
res.setAsgn(lhs, rhs); res.setAsgn(lhs, rhs);
break; break;
case iMOD: case iMOD:
rhs = COND_EXPR::boolOp (lhs, rhs, MOD); rhs = COND_EXPR::boolOp (lhs, rhs, MOD);
if ( ll->testFlags(B) ) if ( ll->testFlags(B) )
{ {
lhs = COND_EXPR::idReg (rAH, 0, &localId); lhs = COND_EXPR::idReg (rAH, 0, &localId);
pIcode->setRegDU( rAH, eDEF); pIcode->setRegDU( rAH, eDEF);
} }
else else
{ {
lhs = COND_EXPR::idReg (rDX, 0, &localId); lhs = COND_EXPR::idReg (rDX, 0, &localId);
pIcode->setRegDU( rDX, eDEF); pIcode->setRegDU( rDX, eDEF);
} }
res.setAsgn(lhs, rhs); res.setAsgn(lhs, rhs);
break; break;
case iMOV: res.setAsgn(lhs, rhs); case iMOV: res.setAsgn(lhs, rhs);
break; break;
case iMUL: case iMUL:
rhs = COND_EXPR::boolOp (lhs, rhs, MUL); 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);
res.setAsgn(lhs, rhs); res.setAsgn(lhs, rhs);
break; break;
case iNEG: case iNEG:
rhs = COND_EXPR::unary (NEGATION, lhs); rhs = COND_EXPR::unary (NEGATION, lhs);
res.setAsgn(lhs, rhs); res.setAsgn(lhs, rhs);
break; break;
case iNOT: case iNOT:
rhs = COND_EXPR::boolOp (NULL, rhs, NOT); rhs = COND_EXPR::boolOp (NULL, rhs, NOT);
res.setAsgn(lhs, rhs); res.setAsgn(lhs, rhs);
break; break;
case iOR: case iOR:
rhs = COND_EXPR::boolOp (lhs, rhs, OR); rhs = COND_EXPR::boolOp (lhs, rhs, OR);
res.setAsgn(lhs, rhs); res.setAsgn(lhs, rhs);
break; break;
case iPOP: res.set(HLI_POP, lhs); case iPOP: res.set(HLI_POP, lhs);
break; break;
case iPUSH: res.set(HLI_PUSH, lhs); case iPUSH: res.set(HLI_PUSH, lhs);
break; break;
case iRET: case iRET:
case iRETF: case iRETF:
res.set(HLI_RET, NULL); res.set(HLI_RET, NULL);
break; break;
case iSHL: case iSHL:
rhs = COND_EXPR::boolOp (lhs, rhs, SHL); rhs = COND_EXPR::boolOp (lhs, rhs, SHL);
res.setAsgn(lhs, rhs); res.setAsgn(lhs, rhs);
break; break;
case iSAR: /* signed */ case iSAR: /* signed */
case iSHR: case iSHR:
rhs = COND_EXPR::boolOp (lhs, rhs, SHR); /* unsigned*/ rhs = COND_EXPR::boolOp (lhs, rhs, SHR); /* unsigned*/
res.setAsgn(lhs, rhs); res.setAsgn(lhs, rhs);
break; break;
case iSIGNEX: case iSIGNEX:
res.setAsgn(lhs, rhs); res.setAsgn(lhs, rhs);
break; break;
case iSUB: case iSUB:
rhs = COND_EXPR::boolOp (lhs, rhs, SUB); rhs = COND_EXPR::boolOp (lhs, rhs, SUB);
res.setAsgn(lhs, rhs); res.setAsgn(lhs, rhs);
break; break;
case iXCHG: case iXCHG:
break; break;
case iXOR: case iXOR:
rhs = COND_EXPR::boolOp (lhs, rhs, XOR); rhs = COND_EXPR::boolOp (lhs, rhs, XOR);
res.setAsgn(lhs, rhs); res.setAsgn(lhs, rhs);
break; break;
} }
return res; return res;
} }
@ -296,17 +296,17 @@ void Function::highLevelGen()
pIcode->invalidate(); pIcode->invalidate();
if ((pIcode->type != LOW_LEVEL) or not pIcode->valid() ) if ((pIcode->type != LOW_LEVEL) or not pIcode->valid() )
continue; continue;
flg = ll->getFlag(); flg = ll->getFlag();
if ((flg & IM_OPS) != IM_OPS) /* not processing IM_OPS yet */ if ((flg & IM_OPS) != IM_OPS) /* not processing IM_OPS yet */
if ((flg & NO_OPS) != NO_OPS) /* if there are opers */ if ((flg & NO_OPS) != NO_OPS) /* if there are opers */
{ {
if ( not ll->testFlags(NO_SRC) ) /* if there is src op */ if ( not ll->testFlags(NO_SRC) ) /* if there is src op */
rhs = COND_EXPR::id (*pIcode->ll(), SRC, this, i, *pIcode, NONE); rhs = COND_EXPR::id (*pIcode->ll(), SRC, this, i, *pIcode, NONE);
lhs = COND_EXPR::id (*pIcode->ll(), DST, this, i, *pIcode, NONE); lhs = COND_EXPR::id (*pIcode->ll(), DST, this, i, *pIcode, NONE);
} }
switch (ll->getOpcode()) switch (ll->getOpcode())
{ {
case iADD: case iADD:
rhs = COND_EXPR::boolOp (lhs, rhs, ADD); rhs = COND_EXPR::boolOp (lhs, rhs, ADD);
pIcode->setAsgn(lhs, rhs); pIcode->setAsgn(lhs, rhs);
@ -319,8 +319,8 @@ void Function::highLevelGen()
case iCALL: case iCALL:
case iCALLF: case iCALLF:
pIcode->type = HIGH_LEVEL; pIcode->type = HIGH_LEVEL;
pIcode->hl( ll->createCall() ); pIcode->hl( ll->createCall() );
break; break;
case iDEC: case iDEC:
@ -347,7 +347,7 @@ void Function::highLevelGen()
case iIMUL: case iIMUL:
rhs = COND_EXPR::boolOp (lhs, rhs, MUL); rhs = COND_EXPR::boolOp (lhs, rhs, MUL);
lhs = COND_EXPR::id (*ll, LHS_OP, this, i, *pIcode, NONE); lhs = COND_EXPR::id (*ll, LHS_OP, this, i, *pIcode, NONE);
pIcode->setAsgn(lhs, rhs); pIcode->setAsgn(lhs, rhs);
break; break;
@ -382,11 +382,12 @@ void Function::highLevelGen()
case iMUL: case iMUL:
rhs = COND_EXPR::boolOp (lhs, rhs, MUL); rhs = COND_EXPR::boolOp (lhs, rhs, MUL);
lhs = COND_EXPR::id (*ll, LHS_OP, this, i, *pIcode, NONE); lhs = COND_EXPR::id (*ll, LHS_OP, this, i, *pIcode, NONE);
pIcode->setAsgn(lhs, rhs); pIcode->setAsgn(lhs, rhs);
break; break;
case iNEG: rhs = COND_EXPR::unary (NEGATION, lhs); case iNEG:
rhs = COND_EXPR::unary (NEGATION, lhs);
pIcode->setAsgn(lhs, rhs); pIcode->setAsgn(lhs, rhs);
break; break;
@ -424,8 +425,8 @@ void Function::highLevelGen()
case iSIGNEX: pIcode->setAsgn(lhs, rhs); case iSIGNEX: pIcode->setAsgn(lhs, rhs);
break; break;
case iSUB: case iSUB:
rhs = COND_EXPR::boolOp (lhs, rhs, SUB); rhs = COND_EXPR::boolOp (lhs, rhs, SUB);
pIcode->setAsgn(lhs, rhs); pIcode->setAsgn(lhs, rhs);
break; break;
@ -436,7 +437,7 @@ void Function::highLevelGen()
rhs = COND_EXPR::boolOp (lhs, rhs, XOR); rhs = COND_EXPR::boolOp (lhs, rhs, XOR);
pIcode->setAsgn(lhs, rhs); pIcode->setAsgn(lhs, rhs);
break; break;
} }
} }
} }
@ -445,7 +446,7 @@ void Function::highLevelGen()
/* Modifies the given conditional operator to its inverse. This is used /* Modifies the given conditional operator to its inverse. This is used
* in if..then[..else] statements, to reflect the condition that takes the * in if..then[..else] statements, to reflect the condition that takes the
* then part. */ * then part. */
COND_EXPR *COND_EXPR::inverse () COND_EXPR *COND_EXPR::inverse () const
{ {
static condOp invCondOp[] = {GREATER, GREATER_EQUAL, NOT_EQUAL, EQUAL, static condOp invCondOp[] = {GREATER, GREATER_EQUAL, NOT_EQUAL, EQUAL,
LESS_EQUAL, LESS, DUMMY,DUMMY,DUMMY,DUMMY, LESS_EQUAL, LESS, DUMMY,DUMMY,DUMMY,DUMMY,
@ -456,22 +457,22 @@ COND_EXPR *COND_EXPR::inverse ()
{ {
switch ( op() ) switch ( op() )
{ {
case LESS_EQUAL: case LESS: case EQUAL: case LESS_EQUAL: case LESS: case EQUAL:
case NOT_EQUAL: case GREATER: case GREATER_EQUAL: case NOT_EQUAL: case GREATER: case GREATER_EQUAL:
res = this->clone(); res = this->clone();
res->boolExpr.op = invCondOp[op()]; res->boolExpr.op = invCondOp[op()];
return res; return res;
case AND: case OR: case XOR: case NOT: case ADD: case AND: case OR: case XOR: case NOT: case ADD:
case SUB: case MUL: case DIV: case SHR: case SHL: case MOD: case SUB: case MUL: case DIV: case SHR: case SHL: case MOD:
return COND_EXPR::unary (NEGATION, this->clone()); return COND_EXPR::unary (NEGATION, this->clone());
case DBL_AND: case DBL_OR: case DBL_AND: case DBL_OR:
res = this->clone(); res = this->clone();
res->boolExpr.op = invCondOp[op()]; res->boolExpr.op = invCondOp[op()];
res->boolExpr.lhs=lhs()->inverse (); res->boolExpr.lhs=lhs()->inverse ();
res->boolExpr.rhs=rhs()->inverse (); res->boolExpr.rhs=rhs()->inverse ();
return res; return res;
} /* eos */ } /* eos */
} }
@ -503,7 +504,7 @@ std::string writeCall (Function * tproc, STKFRAME * args, Function * pproc, int
/* Displays the output of a HLI_JCOND icode. */ /* Displays the output of a HLI_JCOND icode. */
char *writeJcond (HLTYPE h, Function * pProc, int *numLoc) char *writeJcond (const HLTYPE &h, Function * pProc, int *numLoc)
{ {
assert(h.expr()); assert(h.expr());
memset (buf, ' ', sizeof(buf)); memset (buf, ' ', sizeof(buf));
@ -565,25 +566,25 @@ string HLTYPE::write1HlIcode (Function * pProc, int *numLoc)
HlTypeSupport *p = get(); HlTypeSupport *p = get();
switch (opcode) switch (opcode)
{ {
case HLI_ASSIGN: case HLI_ASSIGN:
return p->writeOut(pProc,numLoc); return p->writeOut(pProc,numLoc);
case HLI_CALL: case HLI_CALL:
return p->writeOut(pProc,numLoc); return p->writeOut(pProc,numLoc);
case HLI_RET: case HLI_RET:
e = p->writeOut(pProc,numLoc); e = p->writeOut(pProc,numLoc);
if (! e.empty()) if (! e.empty())
ostr << "return (" << e << ");\n"; ostr << "return (" << e << ");\n";
break; break;
case HLI_POP: case HLI_POP:
ostr << "HLI_POP "; ostr << "HLI_POP ";
ostr << p->writeOut(pProc,numLoc); ostr << p->writeOut(pProc,numLoc);
ostr << "\n"; ostr << "\n";
break; break;
case HLI_PUSH: case HLI_PUSH:
ostr << "HLI_PUSH "; ostr << "HLI_PUSH ";
ostr << p->writeOut(pProc,numLoc); ostr << p->writeOut(pProc,numLoc);
ostr << "\n"; ostr << "\n";
break; break;
} }
return ostr.str(); return ostr.str();
} }
@ -600,8 +601,9 @@ int power2 (int i)
/* Writes the registers/stack variables that are used and defined by this /* Writes the registers/stack variables that are used and defined by this
* instruction. */ * instruction. */
void ICODE::writeDU(int idx) void ICODE::writeDU()
{ {
int my_idx = loc_ip;
{ {
ostringstream ostr; ostringstream ostr;
for (int i = 0; i < (INDEXBASE-1); i++) for (int i = 0; i < (INDEXBASE-1); i++)
@ -633,7 +635,7 @@ void ICODE::writeDU(int idx)
{ {
if (du1.used(i)) if (du1.used(i))
{ {
printf ("%d: du1[%d][] = ", idx, i); printf ("%d: du1[%d][] = ", my_idx, i);
#ifdef _lint #ifdef _lint
for (auto ik=du1.idx[i].uses.begin(); ik!=du1.idx[i].uses.end(); ++ik) for (auto ik=du1.idx[i].uses.begin(); ik!=du1.idx[i].uses.end(); ++ik)
{ {

View File

@ -95,7 +95,7 @@ void Function::findIdioms()
case iPUSH: case iPUSH:
{ {
/* Idiom 1 */ /* Idiom 1 */
// todo add other push idioms. //TODO: add other push idioms.
advance(pIcode,i01(pIcode)); advance(pIcode,i01(pIcode));
break; break;
} }

View File

@ -132,12 +132,17 @@ bool Idiom4::match(iICODE pIcode)
if (pIcode->ll()->testFlags(I) ) if (pIcode->ll()->testFlags(I) )
{ {
m_param_count = (int16_t)pIcode->ll()->src.op(); m_param_count = (int16_t)pIcode->ll()->src.op();
return true;
} }
return false;
} }
int Idiom4::action() int Idiom4::action()
{ {
for(size_t idx=0; idx<m_icodes.size()-1; ++idx) // don't invalidate last entry if( ! m_icodes.empty()) // if not an empty RET[F] N
m_icodes[idx]->invalidate(); {
for(size_t idx=0; idx<m_icodes.size()-1; ++idx) // invalidate all but the RET
m_icodes[idx]->invalidate();
}
if(m_param_count) if(m_param_count)
{ {
m_func->cbParam = (int16_t)m_param_count; m_func->cbParam = (int16_t)m_param_count;

View File

@ -127,7 +127,7 @@ bool Idiom10::match(iICODE pIcode)
int Idiom10::action() int Idiom10::action()
{ {
m_icodes[0]->ll()->set(iCMP,I); m_icodes[0]->ll()->set(iCMP,I);
m_icodes[0]->ll()->src.SetImmediateOp(0); // todo check if proc should be zeroed too m_icodes[0]->ll()->src.SetImmediateOp(0); //TODO: check if proc should be zeroed too
m_icodes[0]->du.def = 0; m_icodes[0]->du.def = 0;
m_icodes[0]->du1.numRegsDef = 0; m_icodes[0]->du1.numRegsDef = 0;
return 2; return 2;

View File

@ -37,7 +37,7 @@ static bool isLong23 (iICODE iter, BB * pbb, iICODE &off, int *arc)
obb2 = t->edges[THEN].BBptr; obb2 = t->edges[THEN].BBptr;
if ((obb2->size() == 2) && (obb2->nodeType == TWO_BRANCH) && (obb2->front().ll()->getOpcode() == iCMP)) if ((obb2->size() == 2) && (obb2->nodeType == TWO_BRANCH) && (obb2->front().ll()->getOpcode() == iCMP))
{ {
off = obb2->begin2();//std::distance(iter,obb2->begin2()); off = obb2->begin();//std::distance(iter,obb2->begin2());
*arc = THEN; *arc = THEN;
return true; return true;
} }
@ -49,7 +49,7 @@ static bool isLong23 (iICODE iter, BB * pbb, iICODE &off, int *arc)
obb2 = e->edges[THEN].BBptr; obb2 = e->edges[THEN].BBptr;
if ((obb2->size() == 2) && (obb2->nodeType == TWO_BRANCH) && (obb2->front().ll()->getOpcode() == iCMP)) if ((obb2->size() == 2) && (obb2->nodeType == TWO_BRANCH) && (obb2->front().ll()->getOpcode() == iCMP))
{ {
off = obb2->begin2();//std::distance(iter,obb2->begin2());//obb2->front().loc_ip - i; off = obb2->begin();//std::distance(iter,obb2->begin2());//obb2->front().loc_ip - i;
*arc = ELSE; *arc = ELSE;
return true; return true;
} }
@ -155,7 +155,7 @@ static int longJCond23 (COND_EXPR *rhs, COND_EXPR *lhs, iICODE pIcode, int arc,
pIcode->invalidate(); pIcode->invalidate();
obb1->front().invalidate(); obb1->front().invalidate();
// invalidate 2 first instructions of BB 2 // invalidate 2 first instructions of BB 2
iICODE ibb2 = obb2->begin2(); iICODE ibb2 = obb2->begin();
(ibb2++)->invalidate(); (ibb2++)->invalidate();
(ibb2++)->invalidate(); (ibb2++)->invalidate();
return skipped_insn; return skipped_insn;

View File

@ -35,6 +35,7 @@ static BB *firstOfQueue (queue &Q)
/* Appends pointer to node at the end of the queue Q if node is not present /* Appends pointer to node at the end of the queue Q if node is not present
* in this queue. Returns the queue node just appended. */ * in this queue. Returns the queue node just appended. */
//lint -sem(appendQueue,custodial(1))
queue::iterator appendQueue (queue &Q, BB *node) queue::iterator appendQueue (queue &Q, BB *node)
{ {
auto iter=std::find(Q.begin(),Q.end(),node); auto iter=std::find(Q.begin(),Q.end(),node);
@ -173,19 +174,21 @@ void derSeq_Entry::findIntervals (Function *c)
/* Displays the intervals of the graph Gi. */ /* Displays the intervals of the graph Gi. */
static void displayIntervals (interval *pI) static void displayIntervals (interval *pI)
{ {
queue::iterator nodePtr;
while (pI) while (pI)
{ {
nodePtr = pI->nodes.begin();
printf (" Interval #: %ld\t#OutEdges: %ld\n", pI->numInt, pI->numOutEdges); printf (" Interval #: %ld\t#OutEdges: %ld\n", pI->numInt, pI->numOutEdges);
while (nodePtr!=pI->nodes.end()) #ifdef _lint
for(auto iter=pI->nodes.begin(); iter!=pI->nodes.end(); ++iter)
{ {
if ((*nodePtr)->correspInt == NULL) /* real BBs */ BB *node(*iter);
printf (" Node: %ld\n", (*nodePtr)->begin()); #else
for(BB *node : pI->nodes)
{
#endif
if (node->correspInt == NULL) /* real BBs */
printf (" Node: %ld\n", node->begin()->loc_ip);
else // BBs represent intervals else // BBs represent intervals
printf (" Node (corresp int): %d\n", (*nodePtr)->correspInt->numInt); printf (" Node (corresp int): %d\n", node->correspInt->numInt);
++nodePtr;
} }
pI = pI->next; pI = pI->next;
} }