more fixes

This commit is contained in:
Artur K
2012-03-08 21:47:59 +01:00
parent 69d848ca78
commit eb6c1ac939
43 changed files with 611 additions and 534 deletions

View File

@@ -35,6 +35,7 @@ set(dcc_SOURCES
src/frontend.cpp src/frontend.cpp
src/graph.cpp src/graph.cpp
src/hlicode.cpp src/hlicode.cpp
src/machine_x86.cpp
src/icode.cpp src/icode.cpp
src/idioms.cpp src/idioms.cpp
src/idioms/idiom1.cpp src/idioms/idiom1.cpp
@@ -65,6 +66,7 @@ set(dcc_HEADERS
include/error.h include/error.h
include/graph.h include/graph.h
include/hlicode.h include/hlicode.h
include/machine_x86.h
include/icode.h include/icode.h
include/idioms/idiom.h include/idioms/idiom.h
include/idioms/idiom1.h include/idioms/idiom1.h

View File

@@ -42,7 +42,7 @@ private:
iICODE range_end; iICODE range_end;
public: public:
iICODE begin(); iICODE begin();
iICODE end(); iICODE end() const;
riICODE rbegin(); riICODE rbegin();
riICODE rend(); riICODE rend();
ICODE &front(); ICODE &front();

View File

@@ -1,34 +1,4 @@
#pragma once #pragma once
/* Machine registers */
enum eReg
{
rUNDEF = 0,
rAX = 1, /* These are numbered relative to real 8086 */
rCX = 2,
rDX = 3,
rBX = 4,
rSP = 5,
rBP = 6,
rSI = 7,
rDI = 8,
rES = 9,
rCS = 10,
rSS = 11,
rDS = 12,
rAL = 13,
rCL = 14,
rDL = 15,
rBL = 16,
rAH = 17,
rCH = 18,
rDH = 19,
rBH = 20,
rTMP= 21, /* temp register for DIV/IDIV/MOD */
INDEXBASE = 22 /* Indexed modes go from INDEXBASE to INDEXBASE+7 */
};
/* Register types */ /* Register types */
enum regType enum regType
@@ -285,11 +255,11 @@ enum hlType
TYPE_WORD_UNSIGN, /* unsigned word (16 bits) */ TYPE_WORD_UNSIGN, /* unsigned word (16 bits) */
TYPE_LONG_SIGN, /* signed long (32 bits) */ TYPE_LONG_SIGN, /* signed long (32 bits) */
TYPE_LONG_UNSIGN, /* unsigned long (32 bits) */ TYPE_LONG_UNSIGN, /* unsigned long (32 bits) */
TYPE_RECORD, /* record structure */ TYPE_RECORD, /* record structure */
TYPE_PTR, /* pointer (32 bit ptr) */ TYPE_PTR, /* pointer (32 bit ptr) */
TYPE_STR, /* string */ TYPE_STR, /* string */
TYPE_CONST, /* constant (any type) */ TYPE_CONST, /* constant (any type) */
TYPE_FLOAT, /* floating point */ TYPE_FLOAT, /* floating point */
TYPE_DOUBLE /* double precision float */ TYPE_DOUBLE /* double precision float */
}; };

View File

@@ -1,6 +1,7 @@
#pragma once #pragma once
#include "ast.h" #include "ast.h"
#include "types.h" #include "types.h"
#include "machine_x86.h"
struct IDENTTYPE struct IDENTTYPE
{ {
condId idType; condId idType;
@@ -23,8 +24,8 @@ struct IDENTTYPE
STKFRAME *args; STKFRAME *args;
} call; } call;
struct { /* for OTHER; tmp struct */ struct { /* for OTHER; tmp struct */
uint8_t seg; /* segment */ eReg seg; /* segment */
uint8_t regi; /* index mode */ eReg regi; /* index mode */
int16_t off; /* offset */ int16_t off; /* offset */
} other; } other;
} idNode; } idNode;

View File

@@ -102,8 +102,8 @@ public:
std::list<BB*> heldBBs; std::list<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 */
int numBBs; /* Number of BBs in the graph cfg */ size_t numBBs; /* Number of BBs in the graph cfg */
boolT hasCase; /* Procedure has a case node */ bool hasCase; /* Procedure has a case node */
/* For interprocedural live analysis */ /* For interprocedural live analysis */
std::bitset<32> liveIn; /* Registers used before defined */ std::bitset<32> liveIn; /* Registers used before defined */

View File

@@ -37,5 +37,5 @@ struct STKFRAME
{ {
} }
int getLocVar(int off); size_t getLocVar(int off);
}; };

View File

@@ -76,7 +76,7 @@ public:
static COND_EXPR *idLoc(int off, LOCAL_ID *localId); static COND_EXPR *idLoc(int off, LOCAL_ID *localId);
static COND_EXPR *idReg(eReg regi, uint32_t icodeFlg, LOCAL_ID *locsym); static COND_EXPR *idReg(eReg regi, uint32_t icodeFlg, LOCAL_ID *locsym);
static COND_EXPR *idLongIdx(int idx); static COND_EXPR *idLongIdx(int idx);
static COND_EXPR *idOther(uint8_t seg, uint8_t regi, int16_t off); static COND_EXPR *idOther(eReg seg, eReg regi, int16_t off);
static COND_EXPR *idParam(int off, const STKFRAME *argSymtab); static COND_EXPR *idParam(int off, const STKFRAME *argSymtab);
static COND_EXPR *unary(condNodeType t, COND_EXPR *sub_expr); static COND_EXPR *unary(condNodeType t, COND_EXPR *sub_expr);
static COND_EXPR *idLong(LOCAL_ID *localId, opLoc sd, iICODE pIcode, hlFirst f, iICODE ix, operDu du, iICODE atOffset); static COND_EXPR *idLong(LOCAL_ID *localId, opLoc sd, iICODE pIcode, hlFirst f, iICODE ix, operDu du, iICODE atOffset);

View File

@@ -83,7 +83,7 @@ struct PROG /* Loaded program image parameters */
int16_t initCS; int16_t initCS;
int16_t initIP; /* These are initial load values */ int16_t initIP; /* These are initial load values */
int16_t initSS; /* Probably not of great interest */ int16_t initSS; /* Probably not of great interest */
int16_t initSP; uint16_t initSP;
bool fCOM; /* Flag set if COM program (else EXE)*/ bool fCOM; /* Flag set if COM program (else EXE)*/
int cReloc; /* No. of relocation table entries */ int cReloc; /* No. of relocation table entries */
uint32_t * relocTable; /* Ptr. to relocation table */ uint32_t * relocTable; /* Ptr. to relocation table */
@@ -179,3 +179,4 @@ eReg otherLongRegi(eReg, int, LOCAL_ID *);
extern eReg subRegH(eReg reg); extern eReg subRegH(eReg reg);
extern eReg subRegL(eReg reg); extern eReg subRegL(eReg reg);
extern const char *indentStr(int level);

View File

@@ -30,7 +30,7 @@ enum eErrorId
WHILE_FAIL WHILE_FAIL
}; };
//lint -function(exit,fatalError)
void fatalError(eErrorId errId, ...); void fatalError(eErrorId errId, ...);
void reportError(eErrorId errId, ...); void reportError(eErrorId errId, ...);

View File

@@ -23,13 +23,6 @@ struct ICODE;
typedef std::list<ICODE>::iterator iICODE; typedef std::list<ICODE>::iterator iICODE;
typedef std::list<ICODE>::reverse_iterator riICODE; typedef std::list<ICODE>::reverse_iterator riICODE;
/* uint8_t and uint16_t registers */
static const char *const byteReg[9] = {"al", "cl", "dl", "bl",
"ah", "ch", "dh", "bh", "tmp" };
static const char *const wordReg[21] = {"ax", "cx", "dx", "bx", "sp", "bp",
"si", "di", "es", "cs", "ss", "ds",
"", "", "", "", "", "", "", "", "tmp"};
/* Def/use of flags - low 4 bits represent flags */ /* Def/use of flags - low 4 bits represent flags */
struct DU struct DU
{ {
@@ -251,7 +244,7 @@ public:
opcode = op; opcode = op;
} }
void emitGotoLabel(int indLevel); void emitGotoLabel(int indLevel);
void findJumpTargets(CIcodeRec &pc); void findJumpTargets(CIcodeRec &_pc);
void writeIntComment(std::ostringstream &s); void writeIntComment(std::ostringstream &s);
void dis1Line(int loc_ip, int pass); void dis1Line(int loc_ip, int pass);
std::ostringstream &strSrc(std::ostringstream &os,bool skip_comma=false); std::ostringstream &strSrc(std::ostringstream &os,bool skip_comma=false);
@@ -349,7 +342,7 @@ public:
type=HIGH_LEVEL; type=HIGH_LEVEL;
hl()->setAsgn(lhs,rhs); hl()->setAsgn(lhs,rhs);
} }
void setUnary(hlIcode op, COND_EXPR *exp); void setUnary(hlIcode op, COND_EXPR *_exp);
void setJCond(COND_EXPR *cexp); void setJCond(COND_EXPR *cexp);
void emitGotoLabel(int indLevel); void emitGotoLabel(int indLevel);

View File

@@ -13,6 +13,7 @@
#include <algorithm> #include <algorithm>
#include "Enums.h" #include "Enums.h"
#include "machine_x86.h"
/* Type definition */ /* Type definition */
// this array has to stay in-order of addition i.e. not std::set<iICODE,std::less<iICODE> > // this array has to stay in-order of addition i.e. not std::set<iICODE,std::less<iICODE> >
@@ -44,7 +45,7 @@ typedef struct
{ {
int16_t seg; /* segment value */ int16_t seg; /* segment value */
int16_t off; /* offset */ int16_t off; /* offset */
uint8_t regi; /* optional indexed register */ eReg regi; /* optional indexed register */
} BWGLB_TYPE; } BWGLB_TYPE;
@@ -100,6 +101,8 @@ struct ID
{ {
case TYPE_WORD_SIGN: case TYPE_WORD_UNSIGN: case TYPE_WORD_SIGN: case TYPE_WORD_UNSIGN:
return 16; return 16;
case TYPE_BYTE_SIGN: case TYPE_BYTE_UNSIGN:
return 8;
} }
return ~0; return ~0;
} }
@@ -113,9 +116,12 @@ public:
{ {
id_arr.reserve(256); id_arr.reserve(256);
} }
// interface to allow range based iteration
std::vector<ID>::iterator begin() {return id_arr.begin();}
std::vector<ID>::iterator end() {return id_arr.end();}
int newByteWordReg(hlType t, eReg regi); int newByteWordReg(hlType t, eReg regi);
int newByteWordStk(hlType t, int off, uint8_t regOff); int newByteWordStk(hlType t, int off, uint8_t regOff);
int newIntIdx(int16_t seg, int16_t off, uint8_t regi, int ix, hlType t); int newIntIdx(int16_t seg, int16_t off, eReg regi, int ix, hlType t);
int newLongReg(hlType t, eReg regH, eReg regL, iICODE ix_); int newLongReg(hlType t, eReg regH, eReg regL, iICODE ix_);
int newLong(opLoc sd, iICODE pIcode, hlFirst f, iICODE ix, operDu du, int off); int newLong(opLoc sd, iICODE pIcode, hlFirst f, iICODE ix, operDu du, int off);
int newLong(opLoc sd, iICODE pIcode, hlFirst f, iICODE ix, operDu du, iICODE atOffset); int newLong(opLoc sd, iICODE pIcode, hlFirst f, iICODE ix, operDu du, iICODE atOffset);

48
include/machine_x86.h Normal file
View File

@@ -0,0 +1,48 @@
#pragma once
#include <string>
/* Machine registers */
enum eReg
{
rUNDEF = 0,
rAX = 1, /* These are numbered relative to real 8086 */
rCX = 2,
rDX = 3,
rBX = 4,
rSP = 5,
rBP = 6,
rSI = 7,
rDI = 8,
rES = 9,
rCS = 10,
rSS = 11,
rDS = 12,
rAL = 13,
rCL = 14,
rDL = 15,
rBL = 16,
rAH = 17,
rCH = 18,
rDH = 19,
rBH = 20,
rTMP= 21, /* temp register for DIV/IDIV/MOD */
/* Indexed modes go from INDEXBASE to INDEXBASE+7 */
INDEX_BX_SI = 22, // "bx+si"
INDEX_BX_DI, // "bx+di"
INDEX_BP_SI, // "bp+si"
INDEX_BP_DI, // "bp+di"
INDEX_SI, // "si"
INDEX_DI, // "di"
INDEX_BP, // "bp"
INDEX_BX, // "bx"
LAST_REG
};
class Machine_X86
{
public:
Machine_X86();
static const std::string &regName(eReg r);
};

View File

@@ -1,11 +1,9 @@
#pragma once
/* Perfect hashing function library. Contains functions to generate perfect /* Perfect hashing function library. Contains functions to generate perfect
hashing functions hashing functions
* (C) Mike van Emmerik * (C) Mike van Emmerik
*/ */
#define TRUE 1
#define FALSE 0
//#define bool unsigned char //#define bool unsigned char
#define uint8_t unsigned char #define uint8_t unsigned char
#define uint16_t unsigned short #define uint16_t unsigned short

View File

@@ -5,13 +5,14 @@
#pragma once #pragma once
#include <stdint.h> #include <stdint.h>
#include <cstring> #include <cstring>
#include "Enums.h" #include "machine_x86.h"
/* STATE TABLE */ /* STATE TABLE */
struct STATE struct STATE
{ {
uint32_t IP; /* Offset into Image */ uint32_t IP; /* Offset into Image */
int16_t r[INDEXBASE]; /* Value of segs and AX */ int16_t r[INDEX_BX_SI]; /* Value of segs and AX */
uint8_t f[INDEXBASE]; /* True if r[.] has a value */ uint8_t f[INDEX_BX_SI]; /* True if r[.] has a value */
struct struct
{ /* For case stmt indexed reg */ { /* For case stmt indexed reg */
uint8_t regi; /* Last conditional jump */ uint8_t regi; /* Last conditional jump */
@@ -24,8 +25,8 @@ struct STATE
JCond.regi=0; JCond.regi=0;
JCond.immed=0; JCond.immed=0;
memset(r,0,sizeof(int16_t)*INDEXBASE); memset(r,0,sizeof(int16_t)*INDEX_BX_SI);
memset(f,0,sizeof(uint8_t)*INDEXBASE); memset(f,0,sizeof(uint8_t)*INDEX_BX_SI);
} }
}; };

View File

@@ -5,7 +5,7 @@
#include "Procedure.h" #include "Procedure.h"
#include "dcc.h" #include "dcc.h"
using namespace std; using namespace std;
extern char *indent (int indLevel);
BB *BB::Create(void *ctx, const string &s, Function *parent, BB *insertBefore) BB *BB::Create(void *ctx, const string &s, Function *parent, BB *insertBefore)
{ {
BB *pnewBB = new BB; BB *pnewBB = new BB;
@@ -13,13 +13,13 @@ BB *BB::Create(void *ctx, const string &s, Function *parent, BB *insertBefore)
return pnewBB; return pnewBB;
} }
BB *BB::Create(int start, int ip, uint8_t nodeType, int numOutEdges, Function *parent) BB *BB::Create(int start, int ip, uint8_t _nodeType, int numOutEdges, Function *parent)
{ {
parent->m_cfg; //parent->m_cfg;
BB* pnewBB; BB* pnewBB;
pnewBB = new BB; pnewBB = new BB;
pnewBB->nodeType = nodeType; /* Initialise */ pnewBB->nodeType = _nodeType; /* Initialise */
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;
@@ -68,7 +68,7 @@ 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", begin()->loc_ip, size(), 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 (size_t i = 0; i < edges.size(); i++)
printf(" outEdge[%2d] = %ld\n",i, edges[i].BBptr->begin()->loc_ip); printf(" outEdge[%2d] = %ld\n",i, edges[i].BBptr->begin()->loc_ip);
} }
/***************************************************************************** /*****************************************************************************
@@ -100,6 +100,7 @@ void BB::displayDfs()
printf("corresponding interval = %ld\n", correspInt->numInt); printf("corresponding interval = %ld\n", correspInt->numInt);
else else
{ {
int edge_idx=0;
#ifdef _lint #ifdef _lint
for(auto iter=inEdges.begin(); iter!=inEdges.end(); ++iter) for(auto iter=inEdges.begin(); iter!=inEdges.end(); ++iter)
{ {
@@ -108,7 +109,8 @@ void BB::displayDfs()
for(BB *node : inEdges) for(BB *node : inEdges)
{ {
#endif #endif
printf (" inEdge[%ld] = %ld\n", i, node->begin()->loc_ip); printf (" inEdge[%ld] = %ld\n", edge_idx, node->begin()->loc_ip);
edge_idx++;
} }
} }
@@ -148,14 +150,14 @@ void BB::displayDfs()
* current procedure. * current procedure.
* indLevel: indentation level - used for formatting. * indLevel: indentation level - used for formatting.
* numLoc: last # assigned to local variables */ * numLoc: last # assigned to local variables */
void BB::writeCode (int indLevel, Function * pProc , int *numLoc,int latchNode, int _ifFollow) void BB::writeCode (int indLevel, Function * pProc , int *numLoc,int _latchNode, int _ifFollow)
{ {
int follow, /* ifFollow */ int follow, /* ifFollow */
_loopType, /* Type of loop, if any */ _loopType, /* Type of loop, if any */
_nodeType; /* Type of node */ _nodeType; /* Type of node */
BB * succ, *latch; /* Successor and latching node */ BB * succ, *latch; /* Successor and latching node */
ICODE * picode; /* Pointer to HLI_JCOND instruction */ ICODE * picode; /* Pointer to HLI_JCOND instruction */
char *l; /* Pointer to HLI_JCOND expression */ char *l; /* Pointer to HLI_JCOND expression */
boolT emptyThen, /* THEN clause is empty */ boolT emptyThen, /* THEN clause is empty */
repCond; /* Repeat condition for while() */ repCond; /* Repeat condition for while() */
@@ -202,19 +204,19 @@ void BB::writeCode (int indLevel, Function * pProc , int *numLoc,int latchNode,
} }
{ {
string e=walkCondExpr (picode->hl()->expr(), pProc, numLoc); string e=walkCondExpr (picode->hl()->expr(), pProc, numLoc);
cCode.appendCode( "\n%swhile (%s) {\n", indent(indLevel),e.c_str()); cCode.appendCode( "\n%swhile (%s) {\n", indentStr(indLevel),e.c_str());
} }
picode->invalidate(); picode->invalidate();
break; break;
case REPEAT_TYPE: case REPEAT_TYPE:
cCode.appendCode( "\n%sdo {\n", indent(indLevel)); cCode.appendCode( "\n%sdo {\n", indentStr(indLevel));
picode = &latch->back(); picode = &latch->back();
picode->invalidate(); picode->invalidate();
break; break;
case ENDLESS_TYPE: case ENDLESS_TYPE:
cCode.appendCode( "\n%sfor (;;) {\n", indent(indLevel)); cCode.appendCode( "\n%sfor (;;) {\n", indentStr(indLevel));
} }
stats.numHLIcode += 1; stats.numHLIcode += 1;
indLevel++; indLevel++;
@@ -227,7 +229,7 @@ void BB::writeCode (int indLevel, Function * pProc , int *numLoc,int latchNode,
/* Check for end of path */ /* Check for end of path */
_nodeType = nodeType; _nodeType = nodeType;
if (_nodeType == RETURN_NODE || _nodeType == TERMINATE_NODE || if (_nodeType == RETURN_NODE || _nodeType == TERMINATE_NODE ||
_nodeType == NOWHERE_NODE || (dfsLastNum == latchNode)) _nodeType == NOWHERE_NODE || (dfsLastNum == _latchNode))
return; return;
/* Check type of loop/node and process code */ /* Check type of loop/node and process code */
@@ -258,17 +260,17 @@ void BB::writeCode (int indLevel, Function * pProc , int *numLoc,int latchNode,
* in while condition, then, emit the loop trailer */ * in while condition, then, emit the loop trailer */
if (repCond) if (repCond)
writeBB (indLevel+1, pProc, numLoc); writeBB (indLevel+1, pProc, numLoc);
cCode.appendCode( "%s} /* end of while */\n",indent(indLevel)); cCode.appendCode( "%s} /* end of while */\n",indentStr(indLevel));
} }
else if (_loopType == ENDLESS_TYPE) else if (_loopType == ENDLESS_TYPE)
cCode.appendCode( "%s} /* end of loop */\n",indent(indLevel)); cCode.appendCode( "%s} /* end of loop */\n",indentStr(indLevel));
else if (_loopType == REPEAT_TYPE) else if (_loopType == REPEAT_TYPE)
{ {
if (picode->hl()->opcode != HLI_JCOND) if (picode->hl()->opcode != HLI_JCOND)
reportError (REPEAT_FAIL); reportError (REPEAT_FAIL);
{ {
string e=walkCondExpr (picode->hl()->expr(), pProc, numLoc); string e=walkCondExpr (picode->hl()->expr(), pProc, numLoc);
cCode.appendCode( "%s} while (%s);\n", indent(indLevel),e.c_str()); cCode.appendCode( "%s} while (%s);\n", indentStr(indLevel),e.c_str());
} }
} }
@@ -277,7 +279,7 @@ void BB::writeCode (int indLevel, Function * pProc , int *numLoc,int latchNode,
{ {
succ = pProc->m_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 */
succ->front().ll()->emitGotoLabel (indLevel); succ->front().ll()->emitGotoLabel (indLevel);
} }
@@ -301,14 +303,14 @@ void BB::writeCode (int indLevel, Function * pProc , int *numLoc,int latchNode,
if (succ->dfsLastNum != follow) /* THEN part */ if (succ->dfsLastNum != follow) /* THEN part */
{ {
l = writeJcond ( *back().hl(), pProc, numLoc); l = writeJcond ( *back().hl(), pProc, numLoc);
cCode.appendCode( "\n%s%s", indent(indLevel-1), l); cCode.appendCode( "\n%s%s", indentStr(indLevel-1), l);
succ->writeCode (indLevel, pProc, numLoc, latchNode,follow); succ->writeCode (indLevel, pProc, numLoc, _latchNode,follow);
} }
else /* empty THEN part => negate ELSE part */ else /* empty THEN part => negate ELSE part */
{ {
l = writeJcondInv ( *back().hl(), pProc, numLoc); l = writeJcondInv ( *back().hl(), pProc, numLoc);
cCode.appendCode( "\n%s%s", indent(indLevel-1), l); cCode.appendCode( "\n%s%s", indentStr(indLevel-1), l);
edges[ELSE].BBptr->writeCode (indLevel, pProc, numLoc, latchNode, follow); edges[ELSE].BBptr->writeCode (indLevel, pProc, numLoc, _latchNode, follow);
emptyThen = true; emptyThen = true;
} }
} }
@@ -322,32 +324,32 @@ void BB::writeCode (int indLevel, Function * pProc , int *numLoc,int latchNode,
if (succ->dfsLastNum != follow) /* ELSE part */ if (succ->dfsLastNum != follow) /* ELSE part */
{ {
cCode.appendCode( "%s}\n%selse {\n", cCode.appendCode( "%s}\n%selse {\n",
indent(indLevel-1), indent(indLevel - 1)); indentStr(indLevel-1), indentStr(indLevel - 1));
succ->writeCode (indLevel, pProc, numLoc, latchNode, follow); succ->writeCode (indLevel, pProc, numLoc, _latchNode, follow);
} }
/* else (empty ELSE part) */ /* else (empty ELSE part) */
} }
else if (! emptyThen) /* already visited => emit label */ else if (! emptyThen) /* already visited => emit label */
{ {
cCode.appendCode( "%s}\n%selse {\n", cCode.appendCode( "%s}\n%selse {\n",
indent(indLevel-1), indent(indLevel - 1)); indentStr(indLevel-1), indentStr(indLevel - 1));
succ->front().ll()->emitGotoLabel (indLevel); succ->front().ll()->emitGotoLabel (indLevel);
} }
cCode.appendCode( "%s}\n", indent(--indLevel)); cCode.appendCode( "%s}\n", indentStr(--indLevel));
/* Continue with the follow */ /* Continue with the follow */
succ = pProc->m_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);
} }
else /* no follow => if..then..else */ else /* no follow => if..then..else */
{ {
l = writeJcond ( *back().hl(), pProc, numLoc); l = writeJcond ( *back().hl(), pProc, numLoc);
cCode.appendCode( "%s%s", indent(indLevel-1), l); cCode.appendCode( "%s%s", indentStr(indLevel-1), l);
edges[THEN].BBptr->writeCode (indLevel, pProc, numLoc, latchNode, _ifFollow); edges[THEN].BBptr->writeCode (indLevel, pProc, numLoc, _latchNode, _ifFollow);
cCode.appendCode( "%s}\n%selse {\n", indent(indLevel-1), indent(indLevel - 1)); cCode.appendCode( "%s}\n%selse {\n", indentStr(indLevel-1), indentStr(indLevel - 1));
edges[ELSE].BBptr->writeCode (indLevel, pProc, numLoc, latchNode, _ifFollow); edges[ELSE].BBptr->writeCode (indLevel, pProc, numLoc, _latchNode, _ifFollow);
cCode.appendCode( "%s}\n", indent(--indLevel)); cCode.appendCode( "%s}\n", indentStr(--indLevel));
} }
} }
@@ -355,7 +357,7 @@ void BB::writeCode (int indLevel, Function * pProc , int *numLoc,int latchNode,
{ {
succ = edges[0].BBptr; /* fall-through edge */ succ = edges[0].BBptr; /* fall-through edge */
if (succ->traversed != DFS_ALPHA) if (succ->traversed != DFS_ALPHA)
succ->writeCode (indLevel, pProc,numLoc, latchNode,_ifFollow); succ->writeCode (indLevel, pProc,numLoc, _latchNode,_ifFollow);
} }
} }
} }
@@ -386,7 +388,7 @@ void BB::writeBB(int lev, Function * pProc, int *numLoc)
std::string line = pHli.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", indentStr(lev), line.c_str());
stats.numHLIcode++; stats.numHLIcode++;
} }
if (option.verbose) if (option.verbose)
@@ -404,7 +406,7 @@ iICODE BB::begin()
return range_start; return range_start;
} }
iICODE BB::end() iICODE BB::end() const
{ {
return range_end; return range_end;
} }

View File

@@ -12,10 +12,8 @@
#include <cassert> #include <cassert>
#include "types.h" #include "types.h"
#include "dcc.h" #include "dcc.h"
#include "machine_x86.h"
using namespace std; using namespace std;
// Index registers **** temp solution
static const char * const idxReg[8] = {"bx+si", "bx+di", "bp+si", "bp+di",
"si", "di", "bp", "bx" };
// Conditional operator symbols in C. Index by condOp enumeration type // Conditional operator symbols in C. Index by condOp enumeration type
static const char * const condOpSym[] = { " <= ", " < ", " == ", " != ", " > ", " >= ", static const char * const condOpSym[] = { " <= ", " < ", " == ", " != ", " > ", " >= ",
" & ", " | ", " ^ ", " ~ ", " & ", " | ", " ^ ", " ~ ",
@@ -297,7 +295,7 @@ COND_EXPR *COND_EXPR::idFunc(Function * pproc, STKFRAME * args)
/* Returns an identifier conditional expression node of type OTHER. /* Returns an identifier conditional expression node of type OTHER.
* Temporary solution, should really be encoded as an indexed type (eg. * Temporary solution, should really be encoded as an indexed type (eg.
* arrays). */ * arrays). */
COND_EXPR *COND_EXPR::idOther(uint8_t seg, uint8_t regi, int16_t off) COND_EXPR *COND_EXPR::idOther(eReg seg, eReg regi, int16_t off)
{ {
COND_EXPR *newExp; COND_EXPR *newExp;
@@ -367,7 +365,7 @@ COND_EXPR *COND_EXPR::id(const LLInst &ll_insn, opLoc sd, Function * pProc, iICO
newExp = COND_EXPR::idKte (ll_insn.src.op(), 2); newExp = COND_EXPR::idKte (ll_insn.src.op(), 2);
else if (pm.regi == 0) /* global variable */ else if (pm.regi == 0) /* global variable */
newExp = GlobalVariable::Create(pm.segValue, pm.off); newExp = GlobalVariable::Create(pm.segValue, pm.off);
else if (pm.regi < INDEXBASE) /* register */ else if (pm.regi < INDEX_BX_SI) /* register */
{ {
newExp = COND_EXPR::idReg (pm.regi, (sd == SRC) ? ll_insn.getFlag() : newExp = COND_EXPR::idReg (pm.regi, (sd == SRC) ? ll_insn.getFlag() :
ll_insn.getFlag() & NO_SRC_B, ll_insn.getFlag() & NO_SRC_B,
@@ -377,14 +375,14 @@ COND_EXPR *COND_EXPR::id(const LLInst &ll_insn, opLoc sd, Function * pProc, iICO
else if (pm.off) /* offset */ else if (pm.off) /* offset */
{ {
if ((pm.seg == rSS) && (pm.regi == INDEXBASE + 6)) /* idx on bp */ if ((pm.seg == rSS) && (pm.regi == INDEX_BP)) /* idx on bp */
{ {
if (pm.off >= 0) /* argument */ if (pm.off >= 0) /* argument */
newExp = COND_EXPR::idParam (pm.off, &pProc->args); newExp = COND_EXPR::idParam (pm.off, &pProc->args);
else /* local variable */ else /* local variable */
newExp = COND_EXPR::idLoc (pm.off, &pProc->localId); newExp = COND_EXPR::idLoc (pm.off, &pProc->localId);
} }
else if ((pm.seg == rDS) && (pm.regi == INDEXBASE + 7)) /* bx */ else if ((pm.seg == rDS) && (pm.regi == INDEX_BX)) /* bx */
{ {
if (pm.off > 0) /* global variable */ if (pm.off > 0) /* global variable */
newExp = idCondExpIdxGlob (pm.segValue, pm.off, rBX,&pProc->localId); newExp = idCondExpIdxGlob (pm.segValue, pm.off, rBX,&pProc->localId);
@@ -399,21 +397,21 @@ COND_EXPR *COND_EXPR::id(const LLInst &ll_insn, opLoc sd, Function * pProc, iICO
else /* (pm->regi >= INDEXBASE && pm->off = 0) => indexed && no off */ else /* (pm->regi >= INDEXBASE && pm->off = 0) => indexed && no off */
{ {
if ((pm.seg == rDS) && (pm.regi > INDEXBASE + 3)) /* dereference */ if ((pm.seg == rDS) && (pm.regi > INDEX_BP_DI)) /* dereference */
{ {
switch (pm.regi) { switch (pm.regi) {
case INDEXBASE + 4: case INDEX_SI:
newExp = COND_EXPR::idReg(rSI, 0, &pProc->localId); newExp = COND_EXPR::idReg(rSI, 0, &pProc->localId);
duIcode.setRegDU( rSI, du); duIcode.setRegDU( rSI, du);
break; break;
case INDEXBASE + 5: case INDEX_DI:
newExp = COND_EXPR::idReg(rDI, 0, &pProc->localId); newExp = COND_EXPR::idReg(rDI, 0, &pProc->localId);
duIcode.setRegDU( rDI, du); duIcode.setRegDU( rDI, du);
break; break;
case INDEXBASE + 6: case INDEX_BP:
newExp = COND_EXPR::idReg(rBP, 0, &pProc->localId); newExp = COND_EXPR::idReg(rBP, 0, &pProc->localId);
break; break;
case INDEXBASE + 7: case INDEX_BX:
newExp = COND_EXPR::idReg(rBX, 0, &pProc->localId); newExp = COND_EXPR::idReg(rBX, 0, &pProc->localId);
duIcode.setRegDU( rBX, du); duIcode.setRegDU( rBX, du);
break; break;
@@ -440,9 +438,9 @@ condId ICODE::idType(opLoc sd)
return (CONSTANT); return (CONSTANT);
else if (pm.regi == 0) else if (pm.regi == 0)
return (GLOB_VAR); return (GLOB_VAR);
else if (pm.regi < INDEXBASE) else if (pm.regi < INDEX_BX_SI)
return (REGISTER); return (REGISTER);
else if ((pm.seg == rSS) && (pm.regi == INDEXBASE)) else if ((pm.seg == rSS) && (pm.regi == INDEX_BX_SI))
{ {
if (pm.off >= 0) if (pm.off >= 0)
return (PARAM); return (PARAM);
@@ -713,10 +711,7 @@ string walkCondExpr (const COND_EXPR* expr, Function * pProc, int* numLoc)
if (id->name[0] == '\0') /* no name */ if (id->name[0] == '\0') /* no name */
{ {
sprintf (id->name, "loc%ld", ++(*numLoc)); sprintf (id->name, "loc%ld", ++(*numLoc));
if (id->id.regi < rAL) cCode.appendDecl("%s %s; /* %s */\n",hlTypes[id->type], id->name,Machine_X86::regName(id->id.regi).c_str());
cCode.appendDecl("%s %s; /* %s */\n",hlTypes[id->type], id->name,wordReg[id->id.regi - rAX]);
else
cCode.appendDecl("%s %s; /* %s */\n",hlTypes[id->type], id->name,byteReg[id->id.regi - rAL]);
} }
if (id->hasMacro) if (id->hasMacro)
o << id->macro << "("<<id->name<<")"; o << id->macro << "("<<id->name<<")";
@@ -738,7 +733,7 @@ string walkCondExpr (const COND_EXPR* expr, Function * pProc, int* numLoc)
case GLOB_VAR_IDX: case GLOB_VAR_IDX:
bwGlb = &pProc->localId.id_arr[expr->expr.ident.idNode.idxGlbIdx].id.bwGlb; bwGlb = &pProc->localId.id_arr[expr->expr.ident.idNode.idxGlbIdx].id.bwGlb;
o << (bwGlb->seg << 4) + bwGlb->off << "["<<wordReg[bwGlb->regi - rAX]<<"]"; o << (bwGlb->seg << 4) + bwGlb->off << "["<<Machine_X86::regName(bwGlb->regi)<<"]";
break; break;
case CONSTANT: case CONSTANT:
@@ -759,7 +754,9 @@ string walkCondExpr (const COND_EXPR* expr, Function * pProc, int* numLoc)
else if (id->loc == REG_FRAME) else if (id->loc == REG_FRAME)
{ {
sprintf (id->name, "loc%ld", ++(*numLoc)); sprintf (id->name, "loc%ld", ++(*numLoc));
cCode.appendDecl("%s %s; /* %s:%s */\n",hlTypes[id->type], id->name,wordReg[id->id.longId.h - rAX],wordReg[id->id.longId.l - rAX]); cCode.appendDecl("%s %s; /* %s:%s */\n",hlTypes[id->type], id->name,
Machine_X86::regName(id->id.longId.h).c_str(),
Machine_X86::regName(id->id.longId.l).c_str());
o << id->name; o << id->name;
pProc->localId.propLongId (id->id.longId.l,id->id.longId.h, id->name); pProc->localId.propLongId (id->id.longId.l,id->id.longId.h, id->name);
} }
@@ -778,8 +775,8 @@ string walkCondExpr (const COND_EXPR* expr, Function * pProc, int* numLoc)
case OTHER: case OTHER:
off = expr->expr.ident.idNode.other.off; off = expr->expr.ident.idNode.other.off;
o << wordReg[expr->expr.ident.idNode.other.seg - rAX]<< "["; o << Machine_X86::regName(expr->expr.ident.idNode.other.seg)<< "[";
o << idxReg[expr->expr.ident.idNode.other.regi - INDEXBASE]; o << Machine_X86::regName(expr->expr.ident.idNode.other.regi);
if (off < 0) if (off < 0)
o << "-"<< hexStr (-off); o << "-"<< hexStr (-off);
else if (off>0) else if (off>0)

View File

@@ -15,19 +15,6 @@
bundle cCode; /* Procedure declaration and code */ bundle cCode; /* Procedure declaration and code */
using namespace std; using namespace std;
/* Indentation buffer */
#define indSize 81 /* size of the indentation buffer. Each indentation
* is of 4 spaces => max. 20 indentation levels */
static char indentBuf[indSize] =
" ";
/* Indentation according to the depth of the statement */
char *indent (int indLevel)
{
return (&indentBuf[indSize-(indLevel*4)-1]);
}
/* Returns a unique index to the next label */ /* Returns a unique index to the next label */
int getNextLabel() int getNextLabel()
@@ -144,23 +131,27 @@ static void printGlobVar (SYM * psym)
* initialization. */ * initialization. */
static void writeGlobSymTable() static void writeGlobSymTable()
{ {
int idx;
char type[10]; char type[10];
SYM * pSym;
if (not symtab.empty()) if (not symtab.empty())
{ {
cCode.appendDecl( "/* Global variables */\n"); cCode.appendDecl( "/* Global variables */\n");
for (idx = 0; idx < symtab.size(); idx++) #ifdef _lint
for (auto iter=symtab.begin(); iter!=symtab.end(); ++iter)
{ {
pSym = &symtab[idx]; SYM &sym(*iter);
if (symtab[idx].duVal.isUSE_VAL()) /* first used */ #else
printGlobVar (&symtab[idx]); for (SYM &sym : symtab)
{
#endif
// pSym = &symtab[idx];
if (sym.duVal.isUSE_VAL()) /* first used */
printGlobVar (&sym);
else { /* first defined */ else { /* first defined */
switch (pSym->size) { switch (sym.size) {
case 1: strcpy (type, "uint8_t\t"); break; case 1: strcpy (type, "uint8_t\t"); break;
case 2: strcpy (type, "int\t"); break; case 2: strcpy (type, "int\t"); break;
case 4: if (pSym->type == TYPE_PTR) case 4: if (sym.type == TYPE_PTR)
strcpy (type, "int\t*"); strcpy (type, "int\t*");
else else
strcpy (type, "char\t*"); strcpy (type, "char\t*");
@@ -168,7 +159,7 @@ static void writeGlobSymTable()
default: strcpy (type, "char\t*"); default: strcpy (type, "char\t*");
} }
cCode.appendDecl( "%s%s;\t/* size = %ld */\n", cCode.appendDecl( "%s%s;\t/* size = %ld */\n",
type, pSym->name, pSym->size); type, sym.name, sym.size);
} }
} }
cCode.appendDecl( "\n"); cCode.appendDecl( "\n");
@@ -199,7 +190,7 @@ static void writeBitVector (const std::bitset<32> &regi)
{ {
int j; int j;
for (j = rAX; j < INDEXBASE; j++) for (j = rAX; j < INDEX_BX_SI; j++)
{ {
if (regi.test(j)) if (regi.test(j))
printf ("%s ", allRegs[j-1]); printf ("%s ", allRegs[j-1]);
@@ -220,7 +211,7 @@ static void emitFwdGotoLabel (ICODE * pt, int indLevel)
pt->ll()->hllLabNum = getNextLabel(); pt->ll()->hllLabNum = getNextLabel();
pt->ll()->setFlags(HLL_LABEL); pt->ll()->setFlags(HLL_LABEL);
} }
cCode.appendCode( "%sgoto l%ld;\n", indent(indLevel), pt->ll()->hllLabNum); cCode.appendCode( "%sgoto l%ld;\n", indentStr(indLevel), pt->ll()->hllLabNum);
} }
@@ -228,11 +219,9 @@ static void emitFwdGotoLabel (ICODE * pt, int indLevel)
* and invokes the procedure that writes the code of the given record *hli */ * and invokes the procedure that writes the code of the given record *hli */
void Function::codeGen (std::ostream &fs) void Function::codeGen (std::ostream &fs)
{ {
int i, numLoc; int numLoc;
//STKFRAME * args; /* Procedure arguments */
char buf[200], /* Procedure's definition */ char buf[200], /* Procedure's definition */
arg[30]; /* One argument */ arg[30]; /* One argument */
ID *locid; /* Pointer to one local identifier */
BB *pBB; /* Pointer to basic block */ BB *pBB; /* Pointer to basic block */
/* Write procedure/function header */ /* Write procedure/function header */
@@ -244,7 +233,7 @@ void Function::codeGen (std::ostream &fs)
/* Write arguments */ /* Write arguments */
memset (buf, 0, sizeof(buf)); memset (buf, 0, sizeof(buf));
for (i = 0; i < args.sym.size(); i++) for (size_t i = 0; i < args.sym.size(); i++)
{ {
if (args.sym[i].invalid == FALSE) if (args.sym[i].invalid == FALSE)
{ {
@@ -264,30 +253,35 @@ void Function::codeGen (std::ostream &fs)
if (! (flg & PROC_ASM)) if (! (flg & PROC_ASM))
{ {
numLoc = 0; numLoc = 0;
for (i = 0; i < localId.csym(); i++) #ifdef _lint
for (size_t i = 0; i < localId.csym(); i++)
{ {
locid = &localId.id_arr[i]; ID &refId(localId.id_arr[i]);
#else
for (ID &refId : localId )
{
#endif
/* Output only non-invalidated entries */ /* Output only non-invalidated entries */
if (locid->illegal == FALSE) if (refId.illegal == FALSE)
{ {
if (locid->loc == REG_FRAME) if (refId.loc == REG_FRAME)
{ {
/* Register variables are assigned to a local variable */ /* Register variables are assigned to a local variable */
if (((flg & SI_REGVAR) && (locid->id.regi == rSI)) || if (((flg & SI_REGVAR) && (refId.id.regi == rSI)) ||
((flg & DI_REGVAR) && (locid->id.regi == rDI))) ((flg & DI_REGVAR) && (refId.id.regi == rDI)))
{ {
sprintf (locid->name, "loc%ld", ++numLoc); sprintf (refId.name, "loc%ld", ++numLoc);
cCode.appendDecl( "int %s;\n", locid->name); cCode.appendDecl( "int %s;\n", refId.name);
} }
/* Other registers are named when they are first used in /* Other registers are named when they are first used in
* the output C code, and appended to the proc decl. */ * the output C code, and appended to the proc decl. */
} }
else if (locid->loc == STK_FRAME) else if (refId.loc == STK_FRAME)
{ {
/* Name local variables and output appropriate type */ /* Name local variables and output appropriate type */
sprintf (locid->name, "loc%ld", ++numLoc); sprintf (refId.name, "loc%ld", ++numLoc);
cCode.appendDecl( "%s %s;\n",hlTypes[locid->type], locid->name); cCode.appendDecl( "%s %s;\n",hlTypes[refId.type], refId.name);
} }
} }
} }
@@ -304,7 +298,7 @@ void Function::codeGen (std::ostream &fs)
/* Write Live register analysis information */ /* Write Live register analysis information */
if (option.verbose) if (option.verbose)
for (i = 0; i < numBBs; i++) for (size_t i = 0; i < numBBs; i++)
{ {
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 */
@@ -325,10 +319,8 @@ void Function::codeGen (std::ostream &fs)
/* Recursive procedure. Displays the procedure's code in depth-first order /* Recursive procedure. Displays the procedure's code in depth-first order
* of the call graph. */ * of the call graph. */
static void backBackEnd (char *filename, CALL_GRAPH * pcallGraph, std::ostream &ios) static void backBackEnd (char *filename, CALL_GRAPH * pcallGraph, std::ostream &_ios)
{ {
int i;
// IFace.Yield(); /* This is a good place to yield to other apps */ // IFace.Yield(); /* This is a good place to yield to other apps */
/* Check if this procedure has been processed already */ /* Check if this procedure has been processed already */
@@ -338,15 +330,15 @@ static void backBackEnd (char *filename, CALL_GRAPH * pcallGraph, std::ostream &
pcallGraph->proc->flg |= PROC_OUTPUT; pcallGraph->proc->flg |= PROC_OUTPUT;
/* Dfs if this procedure has any successors */ /* Dfs if this procedure has any successors */
for (i = 0; i < pcallGraph->outEdges.size(); i++) for (size_t i = 0; i < pcallGraph->outEdges.size(); i++)
{ {
backBackEnd (filename, pcallGraph->outEdges[i], ios); backBackEnd (filename, pcallGraph->outEdges[i], _ios);
} }
/* Generate code for this procedure */ /* Generate code for this procedure */
stats.numLLIcode = pcallGraph->proc->Icode.size(); stats.numLLIcode = pcallGraph->proc->Icode.size();
stats.numHLIcode = 0; stats.numHLIcode = 0;
pcallGraph->proc->codeGen (ios); pcallGraph->proc->codeGen (_ios);
/* Generate statistics */ /* Generate statistics */
if (option.Stats) if (option.Stats)

View File

@@ -40,9 +40,7 @@ void addLabelBundle (strTable &strTab, int idx, int label)
/* Writes the contents of the string table on the file fp. */ /* Writes the contents of the string table on the file fp. */
static void writeStrTab (std::ostream &ios, strTable &strTab) static void writeStrTab (std::ostream &ios, strTable &strTab)
{ {
int i; for (size_t i = 0; i < strTab.size(); i++)
for (i = 0; i < strTab.size(); i++)
ios << strTab[i]; ios << strTab[i];
} }

View File

@@ -43,7 +43,7 @@ struct ph_func_tag
#define NUM_PLIST 64 /* Number of entries to increase allocation by */ #define NUM_PLIST 64 /* Number of entries to increase allocation by */
/* statics */ /* statics */
char buf[100]; /* A general purpose buffer */ static char buf[100]; /* A general purpose buffer */
int numKeys; /* Number of hash table entries (keys) */ 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) */

View File

@@ -7,6 +7,7 @@
****************************************************************************/ ****************************************************************************/
#include "dcc.h" #include "dcc.h"
#include "machine_x86.h"
#include <string.h> #include <string.h>
#include <sstream> #include <sstream>
using namespace std; using namespace std;
@@ -200,19 +201,15 @@ void Function::writeProcComments()
if (psym->regs->expr.ident.idType == REGISTER) if (psym->regs->expr.ident.idType == REGISTER)
{ {
id = &this->localId.id_arr[psym->regs->expr.ident.idNode.regiIdx]; id = &this->localId.id_arr[psym->regs->expr.ident.idNode.regiIdx];
if (psym->regs->expr.ident.regiType == WORD_REG) cCode.appendDecl(" * %s = %s.\n", psym->name,
cCode.appendDecl(" * %s = %s.\n", psym->name, Machine_X86::regName(id->id.regi).c_str());
wordReg[id->id.regi - rAX]);
else /* BYTE_REG */
cCode.appendDecl(" * %s = %s.\n", psym->name,
byteReg[id->id.regi - rAL]);
} }
else /* long register */ else /* long register */
{ {
id = &this->localId.id_arr[psym->regs->expr.ident.idNode.longIdx]; id = &this->localId.id_arr[psym->regs->expr.ident.idNode.longIdx];
cCode.appendDecl(" * %s = %s:%s.\n", psym->name, cCode.appendDecl(" * %s = %s:%s.\n", psym->name,
wordReg[id->id.longId.h - rAX], Machine_X86::regName(id->id.longId.h).c_str(),
wordReg[id->id.longId.l - rAX]); Machine_X86::regName(id->id.longId.l).c_str());
} }
} }

View File

@@ -123,13 +123,13 @@ static boolT inInt(BB * n, queue &q)
* The follow node is the closest node to the loop. */ * The follow node is the closest node to the loop. */
static void findEndlessFollow (Function * pProc, nodeList &loopNodes, BB * head) static void findEndlessFollow (Function * pProc, nodeList &loopNodes, BB * head)
{ {
int j, succ; int succ;
head->loopFollow = MAX; head->loopFollow = MAX;
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->m_dfsLast[*p]->edges.size(); j++) for (size_t j = 0; j < pProc->m_dfsLast[*p]->edges.size(); j++)
{ {
succ = pProc->m_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))
@@ -264,7 +264,6 @@ static void findNodesInInt (queue &intNodes, int level, interval *Ii)
for (auto i=Ii->nodes.begin(); i!=Ii->nodes.end(); ++i) for (auto i=Ii->nodes.begin(); i!=Ii->nodes.end(); ++i)
{ {
en=*i; en=*i;
#else #else
for(BB *en : Ii->nodes) for(BB *en : Ii->nodes)
{ {
@@ -273,6 +272,7 @@ static void findNodesInInt (queue &intNodes, int level, interval *Ii)
} }
} }
else else
{
#ifdef _lint #ifdef _lint
BB * en=0; BB * en=0;
for (auto i=Ii->nodes.begin(); i!=Ii->nodes.end(); ++i) for (auto i=Ii->nodes.begin(); i!=Ii->nodes.end(); ++i)
@@ -285,6 +285,7 @@ static void findNodesInInt (queue &intNodes, int level, interval *Ii)
#endif #endif
findNodesInInt(intNodes,level-1,en->correspInt); findNodesInInt(intNodes,level-1,en->correspInt);
} }
}
} }
@@ -295,7 +296,7 @@ void Function::structLoops(derSeq *derivedG)
BB * intHead, /* interval header node */ BB * intHead, /* interval header node */
* pred, /* predecessor node */ * pred, /* predecessor node */
* latchNode;/* latching node (in case of loops) */ * latchNode;/* latching node (in case of loops) */
int i, /* counter */ int
level = 0; /* derived sequence level */ level = 0; /* derived sequence level */
interval *initInt; /* initial interval */ interval *initInt; /* initial interval */
queue intNodes; /* list of interval nodes */ queue intNodes; /* list of interval nodes */
@@ -314,7 +315,7 @@ void Function::structLoops(derSeq *derivedG)
/* Find interval head (original BB node in G1) and create /* Find interval head (original BB node in G1) and create
* list of nodes of interval Ii. */ * list of nodes of interval Ii. */
initInt = Ii; initInt = Ii;
for (i = 1; i < level; i++) for (size_t i = 1; i < level; i++)
initInt = (*initInt->nodes.begin())->correspInt; initInt = (*initInt->nodes.begin())->correspInt;
intHead = *initInt->nodes.begin(); intHead = *initInt->nodes.begin();
@@ -322,7 +323,7 @@ void Function::structLoops(derSeq *derivedG)
findNodesInInt (intNodes, level, Ii); findNodesInInt (intNodes, level, Ii);
/* Find greatest enclosing back edge (if any) */ /* Find greatest enclosing back edge (if any) */
for (i = 0; i < intHead->inEdges.size(); i++) for (size_t i = 0; i < intHead->inEdges.size(); i++)
{ {
pred = intHead->inEdges[i]; pred = intHead->inEdges[i];
if (inInt(pred, intNodes) && isBackEdge(pred, intHead)) if (inInt(pred, intNodes) && isBackEdge(pred, intHead))

View File

@@ -73,9 +73,9 @@ ExpStack g_exp_stk;
/* Returns the index of the local variable or parameter at offset off, if it /* Returns the index of the local variable or parameter at offset off, if it
* is in the stack frame provided. */ * is in the stack frame provided. */
int STKFRAME::getLocVar(int off) size_t STKFRAME::getLocVar(int off)
{ {
int i; size_t i;
for (i = 0; i < sym.size(); i++) for (i = 0; i < sym.size(); i++)
if (sym[i].off == off) if (sym[i].off == off)
@@ -120,6 +120,7 @@ void Function::elimCondCodes ()
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 */
lhs=rhs=_expr=0;
for (i = 0; i < numBBs; i++) for (i = 0; i < numBBs; i++)
{ {
pBB = m_dfsLast[i]; pBB = m_dfsLast[i];
@@ -251,12 +252,18 @@ 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
#ifdef _lint
for (auto j = pbb->begin(); j != pbb->end(); j++) for (auto j = pbb->begin(); j != pbb->end(); j++)
{ {
if ((j->type == HIGH_LEVEL) && (j->invalid == FALSE)) ICODE &insn(*j);
#else
for(ICODE &insn : *pbb)
{
#endif
if ((insn.type == HIGH_LEVEL) && ( insn.valid() ))
{ {
liveUse |= (j->du.use & ~def); liveUse |= (insn.du.use & ~def);
def |= j->du.def; def |= insn.du.def;
} }
} }
pbb->liveUse = liveUse; pbb->liveUse = liveUse;
@@ -423,7 +430,7 @@ void BB::genDU1()
defRegIdx = 0; defRegIdx = 0;
// foreach defined register // foreach defined register
bitset<32> processed=0; bitset<32> processed=0;
for (k = 0; k < INDEXBASE; k++) for (k = 0; k < INDEX_BX_SI; k++)
{ {
if (not picode->du.def.test(k)) if (not picode->du.def.test(k))
continue; continue;
@@ -514,9 +521,9 @@ 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->rend(); ticode++) for (auto back_patch_at = riICODE(picode); back_patch_at != rend(); back_patch_at++)
{ {
ticode->du1.remove(0,picode); back_patch_at->du1.remove(0,picode);
} }
} }
else /* liveOut */ else /* liveOut */
@@ -587,16 +594,15 @@ static void forwardSubs (COND_EXPR *lhs, COND_EXPR *rhs, iICODE picode,
/* Substitutes the rhs (or lhs if rhs not possible) of ticode for the /* Substitutes the rhs (or lhs if rhs not possible) of ticode for the
* expression exp given */ * expression exp given */
static void forwardSubsLong (int longIdx, COND_EXPR *exp, iICODE picode, static void forwardSubsLong (int longIdx, COND_EXPR *_exp, iICODE picode, iICODE ticode, int *numHlIcodes)
iICODE ticode, int *numHlIcodes)
{ {
bool res; bool res;
if (exp == NULL) /* In case expression popped is NULL */ if (_exp == NULL) /* In case expression popped is NULL */
return; return;
/* Insert on rhs of ticode, if possible */ /* Insert on rhs of ticode, if possible */
res = COND_EXPR::insertSubTreeLongReg (exp, &ticode->hl()->asgn.rhs, longIdx); res = COND_EXPR::insertSubTreeLongReg (_exp, &ticode->hl()->asgn.rhs, longIdx);
if (res) if (res)
{ {
picode->invalidate(); picode->invalidate();
@@ -605,7 +611,7 @@ static void forwardSubsLong (int longIdx, COND_EXPR *exp, iICODE picode,
else else
{ {
/* Try to insert it on lhs of ticode*/ /* Try to insert it on lhs of ticode*/
res = COND_EXPR::insertSubTreeLongReg (exp, &ticode->hl()->asgn.lhs, longIdx); res = COND_EXPR::insertSubTreeLongReg (_exp, &ticode->hl()->asgn.lhs, longIdx);
if (res) if (res)
{ {
picode->invalidate(); picode->invalidate();
@@ -685,38 +691,40 @@ bool BinaryOperator::xClear(iICODE f, iICODE t, iICODE lastBBinst, Function *ppr
/* Checks the type of the formal argument as against to the actual argument, /* Checks the type of the formal argument as against to the actual argument,
* whenever possible, and then places the actual argument on the procedure's * whenever possible, and then places the actual argument on the procedure's
* argument list. */ * argument list. */
static void processCArg (Function * pp, Function * pProc, ICODE * picode, int numArgs, int *k) /// @returns the type size of the stored Arg
static int processCArg (Function * pp, Function * pProc, ICODE * picode, int numArgs)
{ {
COND_EXPR *exp; COND_EXPR *_exp;
boolT res; bool res;
/* if (numArgs == 0) /* if (numArgs == 0)
return; */ return; */
exp = g_exp_stk.pop(); _exp = g_exp_stk.pop();
if (pp->flg & PROC_ISLIB) /* library function */ if (pp->flg & PROC_ISLIB) /* library function */
{ {
if (pp->args.numArgs > 0) if (pp->args.numArgs > 0)
if (pp->flg & PROC_VARARG) if (pp->flg & PROC_VARARG)
{ {
if (numArgs < pp->args.sym.size()) if (numArgs < pp->args.sym.size())
adjustActArgType (exp, pp->args.sym[numArgs].type, pProc); adjustActArgType (_exp, pp->args.sym[numArgs].type, pProc);
} }
else else
adjustActArgType (exp, pp->args.sym[numArgs].type, pProc); adjustActArgType (_exp, pp->args.sym[numArgs].type, pProc);
res = picode->newStkArg (exp, picode->ll()->getOpcode(), pProc); res = picode->newStkArg (_exp, picode->ll()->getOpcode(), pProc);
} }
else /* user function */ else /* user function */
{ {
if (pp->args.numArgs > 0) if (pp->args.numArgs > 0)
pp->args.adjustForArgType (numArgs, expType (exp, pProc)); pp->args.adjustForArgType (numArgs, expType (_exp, pProc));
res = picode->newStkArg (exp, picode->ll()->getOpcode(), pProc); res = picode->newStkArg (_exp, picode->ll()->getOpcode(), pProc);
} }
/* Do not update the size of k if the expression was a segment register /* Do not update the size of k if the expression was a segment register
* in a near call */ * in a near call */
if (res == FALSE) if (res == false)
*k += hlTypeSize (exp, pProc); return hlTypeSize (_exp, pProc);
return 0; // be default we do not know the size of the argument
} }
/** Eliminates extraneous intermediate icode instructions when finding /** Eliminates extraneous intermediate icode instructions when finding
@@ -769,7 +777,7 @@ void Function::processTargetIcode(iICODE picode, int &numHlIcodes, iICODE ticode
} }
} }
void Function::processHliCall1(COND_EXPR *exp, iICODE picode) void Function::processHliCall1(COND_EXPR *_exp, iICODE picode)
{ {
Function * pp; Function * pp;
int cb, numArgs; int cb, numArgs;
@@ -779,58 +787,69 @@ void Function::processHliCall1(COND_EXPR *exp, iICODE picode)
if (pp->flg & CALL_PASCAL) if (pp->flg & CALL_PASCAL)
{ {
cb = pp->cbParam; /* fixed # arguments */ cb = pp->cbParam; /* fixed # arguments */
for (k = 0, numArgs = 0; k < cb; numArgs++) k = 0;
numArgs = 0;
while(k<cb)
{ {
exp = g_exp_stk.pop(); _exp = g_exp_stk.pop();
if (pp->flg & PROC_ISLIB) /* library function */ if (pp->flg & PROC_ISLIB) /* library function */
{ {
if (pp->args.numArgs > 0) if (pp->args.numArgs > 0)
adjustActArgType(exp, pp->args.sym[numArgs].type, this); adjustActArgType(_exp, pp->args.sym[numArgs].type, this);
res = picode->newStkArg (exp, picode->ll()->getOpcode(), this); res = picode->newStkArg (_exp, picode->ll()->getOpcode(), this);
} }
else /* user function */ else /* user function */
{ {
if (pp->args.numArgs >0) if (pp->args.numArgs >0)
pp->args.adjustForArgType (numArgs,expType (exp, this)); pp->args.adjustForArgType (numArgs,expType (_exp, this));
res = picode->newStkArg (exp,picode->ll()->getOpcode(), this); res = picode->newStkArg (_exp,picode->ll()->getOpcode(), this);
} }
if (res == FALSE) if (res == FALSE)
k += hlTypeSize (exp, this); k += hlTypeSize (_exp, this);
numArgs++;
} }
} }
else /* CALL_C */ else /* CALL_C */
{ {
cb = picode->hl()->call.args->cb; cb = picode->hl()->call.args->cb;
numArgs = 0; numArgs = 0;
k = 0;
if (cb) if (cb)
for (k = 0; k < cb; numArgs++) {
processCArg (pp, this, &(*picode), numArgs, &k); while ( k < cb )
else if ((cb == 0) && picode->ll()->testFlags(REST_STK))
while (! g_exp_stk.empty())
{ {
processCArg (pp, this, &(*picode), numArgs, &k); k+=processCArg (pp, this, &(*picode), numArgs);
numArgs++; numArgs++;
} }
}
else if ((cb == 0) && picode->ll()->testFlags(REST_STK))
{
while (! g_exp_stk.empty())
{
k+=processCArg (pp, this, &(*picode), numArgs);
numArgs++;
}
}
} }
} }
void Function::findExps() void Function::findExps()
{ {
int i, numHlIcodes; int i, numHlIcodes;
iICODE lastInst, iICODE
picode, // Current icode */ picode, // Current icode */
ticode; // Target icode */ ticode; // Target icode */
BB * pbb; // Current and next basic block */ BB * pbb; // Current and next basic block */
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 */
uint8_t regi; // register(s) to be forward substituted */ uint8_t regi; // register(s) to be forward substituted */
ID *retVal; // function return value ID *_retVal; // function return value
/* Initialize expression stack */ /* Initialize expression stack */
g_exp_stk.init(); g_exp_stk.init();
_exp = 0;
/* Traverse tree in dfsLast order */ /* Traverse tree in dfsLast order */
for (i = 0; i < numBBs; i++) for (i = 0; i < numBBs; i++)
{ {
@@ -838,30 +857,29 @@ 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->end();
numHlIcodes = 0; numHlIcodes = 0;
for (picode = pbb->begin(); picode != lastInst; picode++) for (picode = pbb->begin(); picode != pbb->end(); picode++)
{ {
if ((picode->type == HIGH_LEVEL) && (picode->invalid == FALSE)) if ((picode->type != HIGH_LEVEL) || ( ! picode->valid() ))
continue;
numHlIcodes++;
if (picode->du1.numRegsDef == 1) /* uint8_t/uint16_t regs */
{ {
numHlIcodes++; /* Check for only one use of this register. If this is
if (picode->du1.numRegsDef == 1) /* uint8_t/uint16_t regs */ * the last definition of the register in this BB, check
* that it is not liveOut from this basic block */
if (picode->du1.numUses(0)==1)
{ {
/* Check for only one use of this register. If this is /* Check that this register is not liveOut, if it
* the last definition of the register in this BB, check * is the last definition of the register */
* that it is not liveOut from this basic block */ regi = picode->du1.regi[0];
if (picode->du1.numUses(0)==1)
{
/* Check that this register is not liveOut, if it
* is the last definition of the register */
regi = picode->du1.regi[0];
/* Check if we can forward substitute this register */ /* Check if we can forward substitute this register */
switch (picode->hl()->opcode) switch (picode->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 = picode->du1.idx[0].uses.front(); ticode = picode->du1.idx[0].uses.front();
if ((picode->du.lastDefRegi & duReg[regi]).any() && if ((picode->du.lastDefRegi & duReg[regi]).any() &&
((ticode->hl()->opcode != HLI_CALL) && ((ticode->hl()->opcode != HLI_CALL) &&
@@ -869,7 +887,7 @@ void Function::findExps()
continue; continue;
if (picode->hl()->asgn.rhs->xClear (picode, if (picode->hl()->asgn.rhs->xClear (picode,
picode->du1.idx[0].uses[0], lastInst, this)) picode->du1.idx[0].uses[0], pbb->end(), this))
{ {
processTargetIcode(picode, numHlIcodes, ticode,false); processTargetIcode(picode, numHlIcodes, ticode,false);
} }
@@ -882,86 +900,86 @@ void Function::findExps()
(ticode->hl()->opcode != HLI_RET))) (ticode->hl()->opcode != HLI_RET)))
continue; continue;
exp = g_exp_stk.pop(); /* pop last exp pushed */ _exp = g_exp_stk.pop(); /* pop last exp pushed */
switch (ticode->hl()->opcode) { switch (ticode->hl()->opcode) {
case HLI_ASSIGN: case HLI_ASSIGN:
forwardSubs (picode->hl()->expr(), exp, forwardSubs (picode->hl()->expr(), _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:
res = COND_EXPR::insertSubTreeReg (ticode->hl()->exp.v, res = COND_EXPR::insertSubTreeReg (ticode->hl()->exp.v,
exp, _exp,
localId.id_arr[picode->hl()->expr()->expr.ident.idNode.regiIdx].id.regi, localId.id_arr[picode->hl()->expr()->expr.ident.idNode.regiIdx].id.regi,
&localId); &localId);
if (res) if (res)
{ {
picode->invalidate(); picode->invalidate();
numHlIcodes--; numHlIcodes--;
} }
break; break;
/****case HLI_CALL: /* register arguments /****case HLI_CALL: /* register arguments
newRegArg (pProc, picode, ticode); newRegArg (pProc, picode, ticode);
picode->invalidate(); picode->invalidate();
numHlIcodes--; numHlIcodes--;
break; */ break; */
} /* eos */ } /* eos */
break; break;
case HLI_CALL: case HLI_CALL:
ticode = picode->du1.idx[0].uses.front(); ticode = picode->du1.idx[0].uses.front();
HLTYPE *ti_hl(ticode->hl()); HLTYPE *ti_hl(ticode->hl());
retVal = &picode->hl()->call.proc->retVal; _retVal = &picode->hl()->call.proc->retVal;
switch (ti_hl->opcode) { switch (ti_hl->opcode) {
case HLI_ASSIGN: case HLI_ASSIGN:
assert(ti_hl->asgn.rhs); assert(ti_hl->asgn.rhs);
exp = COND_EXPR::idFunc ( picode->hl()->call.proc, picode->hl()->call.args); _exp = COND_EXPR::idFunc ( picode->hl()->call.proc, picode->hl()->call.args);
res = COND_EXPR::insertSubTreeReg (ti_hl->asgn.rhs,exp, retVal->id.regi, &localId); 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();
numHlIcodes--;
break;
case HLI_PUSH: case HLI_RET:
ti_hl->expr( COND_EXPR::idFunc ( picode->hl()->call.proc, picode->hl()->call.args) );
picode->invalidate();
numHlIcodes--;
break;
case HLI_JCOND:
exp = COND_EXPR::idFunc ( picode->hl()->call.proc, picode->hl()->call.args);
res = COND_EXPR::insertSubTreeReg (ti_hl->exp.v, exp, retVal->id.regi, &localId);
if (res) /* was substituted */
{
picode->invalidate(); picode->invalidate();
numHlIcodes--; numHlIcodes--;
} break;
else /* cannot substitute function */
{ case HLI_PUSH: case HLI_RET:
//picode->loc_ip ti_hl->expr( COND_EXPR::idFunc ( picode->hl()->call.proc, picode->hl()->call.args) );
lhs = COND_EXPR::idID(retVal,&localId,picode); picode->invalidate();
picode->setAsgn(lhs, exp); numHlIcodes--;
} break;
break;
case HLI_JCOND:
_exp = COND_EXPR::idFunc ( picode->hl()->call.proc, picode->hl()->call.args);
res = COND_EXPR::insertSubTreeReg (ti_hl->exp.v, _exp, _retVal->id.regi, &localId);
if (res) /* was substituted */
{
picode->invalidate();
numHlIcodes--;
}
else /* cannot substitute function */
{
//picode->loc_ip
lhs = COND_EXPR::idID(_retVal,&localId,picode);
picode->setAsgn(lhs, _exp);
}
break;
} /* eos */ } /* eos */
break; break;
} /* eos */ } /* eos */
}
} }
}
else if (picode->du1.numRegsDef == 2) /* long regs */ else if (picode->du1.numRegsDef == 2) /* long regs */
{
/* Check for only one use of these registers */
if ((picode->du1.numUses(0) == 1) and (picode->du1.numUses(1) == 1))
{ {
/* Check for only one use of these registers */ switch (picode->hl()->opcode) {
if ((picode->du1.numUses(0) == 1) and (picode->du1.numUses(1) == 1))
{
switch (picode->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 */
if (picode->du1.idx[0].uses[0] == picode->du1.idx[1].uses[0]) if (picode->du1.idx[0].uses[0] == picode->du1.idx[1].uses[0])
{ {
ticode = picode->du1.idx[0].uses.front(); ticode = picode->du1.idx[0].uses.front();
@@ -982,24 +1000,24 @@ void Function::findExps()
(ticode->hl()->opcode != HLI_RET))) (ticode->hl()->opcode != HLI_RET)))
continue; continue;
exp = g_exp_stk.pop(); /* pop last exp pushed */ _exp = g_exp_stk.pop(); /* pop last exp pushed */
switch (ticode->hl()->opcode) { switch (ticode->hl()->opcode) {
case HLI_ASSIGN: case HLI_ASSIGN:
forwardSubsLong (picode->hl()->expr()->expr.ident.idNode.longIdx, forwardSubsLong (picode->hl()->expr()->expr.ident.idNode.longIdx,
exp, picode, ticode, &numHlIcodes); _exp, picode, ticode, &numHlIcodes);
break; break;
case HLI_JCOND: case HLI_PUSH: case HLI_JCOND: case HLI_PUSH:
res = COND_EXPR::insertSubTreeLongReg (exp, res = COND_EXPR::insertSubTreeLongReg (_exp,
&ticode->hl()->exp.v, &ticode->hl()->exp.v,
picode->hl()->asgn.lhs->expr.ident.idNode.longIdx); picode->hl()->asgn.lhs->expr.ident.idNode.longIdx);
if (res) if (res)
{ {
picode->invalidate(); picode->invalidate();
numHlIcodes--; numHlIcodes--;
} }
break; break;
case HLI_CALL: /*** missing ***/ case HLI_CALL: /*** missing ***/
break; break;
} /* eos */ } /* eos */
} }
break; break;
@@ -1008,74 +1026,73 @@ void Function::findExps()
ticode = picode->du1.idx[0].uses.front(); ticode = picode->du1.idx[0].uses.front();
switch (ticode->hl()->opcode) switch (ticode->hl()->opcode)
{ {
case HLI_ASSIGN: case HLI_ASSIGN:
exp = COND_EXPR::idFunc ( picode->hl()->call.proc, picode->hl()->call.args); _exp = COND_EXPR::idFunc ( picode->hl()->call.proc, picode->hl()->call.args);
ticode->hl()->asgn.lhs = ticode->hl()->asgn.lhs =
COND_EXPR::idLong(&localId, DST, ticode,HIGH_FIRST, picode, eDEF, ++iICODE(ticode)); COND_EXPR::idLong(&localId, DST, ticode,HIGH_FIRST, picode, eDEF, ++iICODE(ticode));
ticode->hl()->asgn.rhs = exp; ticode->hl()->asgn.rhs = _exp;
picode->invalidate();
numHlIcodes--;
break;
case HLI_PUSH: case HLI_RET:
ticode->hl()->expr( COND_EXPR::idFunc ( picode->hl()->call.proc, picode->hl()->call.args) );
picode->invalidate();
numHlIcodes--;
break;
case HLI_JCOND:
exp = COND_EXPR::idFunc ( picode->hl()->call.proc, picode->hl()->call.args);
retVal = &picode->hl()->call.proc->retVal;
res = COND_EXPR::insertSubTreeLongReg (exp,
&ticode->hl()->exp.v,
localId.newLongReg ( retVal->type, retVal->id.longId.h,
retVal->id.longId.l, picode));
if (res) /* was substituted */
{
picode->invalidate(); picode->invalidate();
numHlIcodes--; numHlIcodes--;
} break;
else /* cannot substitute function */
{ case HLI_PUSH: case HLI_RET:
lhs = COND_EXPR::idID(retVal,&localId,picode/*picode->loc_ip*/); ticode->hl()->expr( COND_EXPR::idFunc ( picode->hl()->call.proc, picode->hl()->call.args) );
picode->setAsgn(lhs, exp); picode->invalidate();
} numHlIcodes--;
break; break;
case HLI_JCOND:
_exp = COND_EXPR::idFunc ( picode->hl()->call.proc, picode->hl()->call.args);
_retVal = &picode->hl()->call.proc->retVal;
res = COND_EXPR::insertSubTreeLongReg (_exp,
&ticode->hl()->exp.v,
localId.newLongReg ( _retVal->type, _retVal->id.longId.h,
_retVal->id.longId.l, picode));
if (res) /* was substituted */
{
picode->invalidate();
numHlIcodes--;
}
else /* cannot substitute function */
{
lhs = COND_EXPR::idID(_retVal,&localId,picode/*picode->loc_ip*/);
picode->setAsgn(lhs, _exp);
}
break;
} /* eos */ } /* eos */
} /* eos */ } /* eos */
}
} }
}
/* HLI_PUSH doesn't define any registers, only uses registers. /* HLI_PUSH doesn't define any registers, only uses registers.
* Push the associated expression to the register on the local * Push the associated expression to the register on the local
* expression stack */ * expression stack */
else if (picode->hl()->opcode == HLI_PUSH) else if (picode->hl()->opcode == HLI_PUSH)
{ {
g_exp_stk.push(picode->hl()->expr()); g_exp_stk.push(picode->hl()->expr());
picode->invalidate(); picode->invalidate();
numHlIcodes--; numHlIcodes--;
} }
/* For HLI_CALL instructions that use arguments from the stack, /* For HLI_CALL instructions that use arguments from the stack,
* pop them from the expression stack and place them on the * pop them from the expression stack and place them on the
* procedure's argument list */ * procedure's argument list */
if ((picode->hl()->opcode == HLI_CALL) && ! (picode->hl()->call.proc->flg & REG_ARGS)) if ((picode->hl()->opcode == HLI_CALL) && ! (picode->hl()->call.proc->flg & REG_ARGS))
{ {
processHliCall1(exp, picode); processHliCall1(_exp, picode);
} }
/* If we could not substitute the result of a function, /* If we could not substitute the result of a function,
* assign it to the corresponding registers */ * assign it to the corresponding registers */
if ((picode->hl()->opcode == HLI_CALL) && if ((picode->hl()->opcode == HLI_CALL) &&
((picode->hl()->call.proc->flg & PROC_ISLIB) != ((picode->hl()->call.proc->flg & PROC_ISLIB) !=
PROC_ISLIB) && (not picode->du1.used(0)) && PROC_ISLIB) && (not picode->du1.used(0)) &&
(picode->du1.numRegsDef > 0)) (picode->du1.numRegsDef > 0))
{ {
exp = COND_EXPR::idFunc (picode->hl()->call.proc, picode->hl()->call.args); _exp = COND_EXPR::idFunc (picode->hl()->call.proc, picode->hl()->call.args);
lhs = COND_EXPR::idID (&picode->hl()->call.proc->retVal, &localId, picode); lhs = COND_EXPR::idID (&picode->hl()->call.proc->retVal, &localId, picode);
picode->setAsgn(lhs, exp); picode->setAsgn(lhs, _exp);
}
} }
} }
@@ -1088,26 +1105,43 @@ 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(std::bitset<32> &liveOut) void Function::dataFlow(std::bitset<32> &_liveOut)
{ {
boolT isAx, isBx, isCx, isDx; bool isAx, isBx, isCx, isDx;
int idx; int idx;
/* Remove references to register variables */ /* Remove references to register variables */
if (flg & SI_REGVAR) if (flg & SI_REGVAR)
liveOut &= maskDuReg[rSI]; _liveOut &= maskDuReg[rSI];
if (flg & DI_REGVAR) if (flg & DI_REGVAR)
liveOut &= maskDuReg[rDI]; _liveOut &= maskDuReg[rDI];
/* Function - return value register(s) */ /* Function - return value register(s) */
if (liveOut != 0) if (_liveOut.any())
{ {
flg |= PROC_IS_FUNC; flg |= PROC_IS_FUNC;
isAx = liveOut.test(rAX - rAX); isAx = _liveOut.test(rAX - rAX);
isBx = liveOut.test(rBX - rAX); isBx = _liveOut.test(rBX - rAX);
isCx = liveOut.test(rCX - rAX); isCx = _liveOut.test(rCX - rAX);
isDx = liveOut.test(rDX - rAX); isDx = _liveOut.test(rDX - rAX);
bool isAL = !isAx && _liveOut.test(rAL - rAX);
bool isBL = !isBx && _liveOut.test(rBL - rAX);
bool isCL = !isCx && _liveOut.test(rCL - rAX);
bool isAH = !isAx && _liveOut.test(rAH - rAX);
bool isDL = !isDx && _liveOut.test(rDL - rAX);
bool isDH = !isDx && _liveOut.test(rDH - rAX);
if(isAL && isAH)
{
isAx = true;
printf("**************************************************************************** dataFlow Join discovered Ax\n");
isAH=isAL=false;
}
if(isDL && isDH)
{
isDx = true;
printf("**************************************************************************** dataFlow Join discovered Dx\n");
isDH=isDL=false;
}
if (isAx && isDx) /* long or pointer */ if (isAx && isDx) /* long or pointer */
{ {
retVal.type = TYPE_LONG_SIGN; retVal.type = TYPE_LONG_SIGN;
@@ -1131,13 +1165,29 @@ void Function::dataFlow(std::bitset<32> &liveOut)
retVal.id.regi = rDX; retVal.id.regi = rDX;
idx = localId.newByteWordReg(TYPE_WORD_SIGN,retVal.id.regi); idx = localId.newByteWordReg(TYPE_WORD_SIGN,retVal.id.regi);
} }
else if(isAL||isBL||isCL||isDL)
{
printf("**************************************************************************** AL/DL return \n");
retVal.type = TYPE_BYTE_SIGN;
retVal.loc = REG_FRAME;
if (isAL)
retVal.id.regi = rAL;
else if (isBL)
retVal.id.regi = rBL;
else if (isCL)
retVal.id.regi = rCL;
else
retVal.id.regi = rDL;
idx = localId.newByteWordReg(TYPE_BYTE_SIGN,retVal.id.regi);
}
} }
/* Data flow analysis */ /* Data flow analysis */
liveAnal = true; liveAnal = true;
elimCondCodes(); elimCondCodes();
genLiveKtes(); genLiveKtes();
liveRegAnalysis (liveOut); /* calls dataFlow() recursively */ liveRegAnalysis (_liveOut); /* calls dataFlow() recursively */
if (! (flg & PROC_ASM)) /* can generate C for pProc */ if (! (flg & PROC_ASM)) /* can generate C for pProc */
{ {
genDU1 (); /* generate def/use level 1 chain */ genDU1 (); /* generate def/use level 1 chain */

View File

@@ -8,7 +8,7 @@
#include <string.h> #include <string.h>
/* Global variables - extern to other modules */ /* Global variables - extern to other modules */
char *progname; /* argv[0] - for error msgs */ //char *progname; /* argv[0] - for error msgs */
char *asm1_name, *asm2_name; /* Assembler output filenames */ char *asm1_name, *asm2_name; /* Assembler output filenames */
SYMTAB symtab; /* Global symbol table */ SYMTAB symtab; /* Global symbol table */
STATS stats; /* cfg statistics */ STATS stats; /* cfg statistics */
@@ -73,7 +73,7 @@ int main(int argc, char *argv[])
static char *initargs(int argc, char *argv[]) static char *initargs(int argc, char *argv[])
{ {
char *pc; char *pc;
progname = *argv; /* Save invocation name for error messages */ //progname = *argv; /* Save invocation name for error messages */
while (--argc > 0 && (*++argv)[0] == '-') while (--argc > 0 && (*++argv)[0] == '-')
{ {
@@ -117,8 +117,8 @@ static char *initargs(int argc, char *argv[])
} }
//lint -fallthrough //lint -fallthrough
default: default:
fatalError(INVALID_ARG, *pc); fatalError(INVALID_ARG, *pc); // does not return
return *argv;
} }
NextArg:; NextArg:;
} }
@@ -146,7 +146,7 @@ NextArg:;
} }
fatalError(USAGE); fatalError(USAGE);
return *argv; return *argv; // does not reach this.
} }
static void static void

View File

@@ -139,12 +139,12 @@ boolT callArg(uint16_t off, char *temp); /* Check for procedure name */
static FILE *fp; static FILE *fp;
static CIcodeRec pc; static CIcodeRec pc;
static std::ostringstream buf;
static int cb, j, numIcode, allocIcode, eop; static int cb, j, numIcode, allocIcode;
static map<int,int> pl; static map<int,int> pl;
static uint32_t nextInst; static uint32_t nextInst;
static boolT fImpure; static boolT fImpure;
static int lab, prevPass; static int g_lab, prevPass;
static Function * pProc; /* Points to current proc struct */ static Function * pProc; /* Points to current proc struct */
struct POSSTACK_ENTRY struct POSSTACK_ENTRY
@@ -165,13 +165,13 @@ uint8_t iPS; /* Index into the stack */
#define dis_show() // Nothing to do unless using Curses #define dis_show() // Nothing to do unless using Curses
void LLInst::findJumpTargets(CIcodeRec &pc) void LLInst::findJumpTargets(CIcodeRec &_pc)
{ {
if (testFlags(I) && ! testFlags(JMP_ICODE) && isJmpInst()) if (testFlags(I) && ! testFlags(JMP_ICODE) && isJmpInst())
{ {
/* Replace the immediate operand with an icode index */ /* Replace the immediate operand with an icode index */
iICODE labTgt=pc.labelSrch(src.op()); iICODE labTgt=_pc.labelSrch(src.op());
if (labTgt!=pc.end()) if (labTgt!=_pc.end())
{ {
src.SetImmediateOp(labTgt->loc_ip); src.SetImmediateOp(labTgt->loc_ip);
/* This icode is the target of a jump */ /* This icode is the target of a jump */
@@ -194,13 +194,13 @@ void LLInst::findJumpTargets(CIcodeRec &pc)
****************************************************************************/ ****************************************************************************/
void disassem(int pass, Function * ppProc) void disassem(int pass, Function * ppProc)
{ {
int i;
pProc = ppProc; /* Save the passes pProc */ pProc = ppProc; /* Save the passes pProc */
if (pass != prevPass) if (pass != prevPass)
{ {
prevPass = pass; prevPass = pass;
lab = 0; /* Restart label numbers */ g_lab = 0; /* Restart label numbers */
} }
createSymTables(); createSymTables();
allocIcode = numIcode = pProc->Icode.size(); allocIcode = numIcode = pProc->Icode.size();
@@ -342,7 +342,7 @@ void LLInst::dis1Line(int loc_ip, int pass)
/* Print label */ /* Print label */
if (pl.count(loc_ip)==0) if (pl.count(loc_ip)==0)
{ {
pl[loc_ip] = ++lab; pl[loc_ip] = ++g_lab;
} }
lab_contents<< "L"<<pl[loc_ip]<<':'; lab_contents<< "L"<<pl[loc_ip]<<':';
} }
@@ -435,7 +435,7 @@ void LLInst::dis1Line(int loc_ip, int pass)
j = src.op(); j = src.op();
if (pl.count(j)==0) /* Forward jump */ if (pl.count(j)==0) /* Forward jump */
{ {
pl[j] = ++lab; pl[j] = ++g_lab;
} }
if (opcode == iJMPF) if (opcode == iJMPF)
{ {
@@ -641,12 +641,12 @@ static void formatRM(std::ostringstream &p, uint32_t flg, const LLOperand &pm)
p<<seg<<"["<<strHex((uint32_t)pm.off)<<"]"; p<<seg<<"["<<strHex((uint32_t)pm.off)<<"]";
} }
else if (pm.regi == (INDEXBASE - 1)) else if (pm.regi == (INDEX_BX_SI - 1))
{ {
p<<"tmp"; p<<"tmp";
} }
else if (pm.regi < INDEXBASE) else if (pm.regi < INDEX_BX_SI)
{ {
if(flg & B) if(flg & B)
p << szBreg[pm.regi - rAL]; p << szBreg[pm.regi - rAL];
@@ -658,15 +658,15 @@ static void formatRM(std::ostringstream &p, uint32_t flg, const LLOperand &pm)
{ {
if (pm.off < 0) if (pm.off < 0)
{ {
p <<seg<<"["<<szIndex[pm.regi - INDEXBASE]<<"-"<<strHex((uint32_t)(- pm.off))<<"]"; p <<seg<<"["<<szIndex[pm.regi - INDEX_BX_SI]<<"-"<<strHex((uint32_t)(- pm.off))<<"]";
} }
else else
{ {
p <<seg<<"["<<szIndex[pm.regi - INDEXBASE]<<"+"<<strHex((uint32_t)(pm.off))<<"]"; p <<seg<<"["<<szIndex[pm.regi - INDEX_BX_SI]<<"+"<<strHex((uint32_t)(pm.off))<<"]";
} }
} }
else else
p <<seg<<"["<<szIndex[pm.regi - INDEXBASE]<<"]"; p <<seg<<"["<<szIndex[pm.regi - INDEX_BX_SI]<<"]";
} }
@@ -677,7 +677,7 @@ static ostringstream & strDst(ostringstream &os,uint32_t flg, LLOperand &pm)
{ {
/* Immediates to memory require size descriptor */ /* Immediates to memory require size descriptor */
//os << setw(WID_PTR); //os << setw(WID_PTR);
if ((flg & I) && (pm.regi == 0 || pm.regi >= INDEXBASE)) if ((flg & I) && (pm.regi == 0 || pm.regi >= INDEX_BX_SI))
os << szPtr[flg & B]; os << szPtr[flg & B];
formatRM(os, flg, pm); formatRM(os, flg, pm);
return os; return os;
@@ -689,7 +689,7 @@ static ostringstream & strDst(ostringstream &os,uint32_t flg, LLOperand &pm)
****************************************************************************/ ****************************************************************************/
ostringstream &LLInst::strSrc(ostringstream &os,bool skip_comma) ostringstream &LLInst::strSrc(ostringstream &os,bool skip_comma)
{ {
static char buf[30] = {", "};
if(false==skip_comma) if(false==skip_comma)
os<<", "; os<<", ";
if (testFlags(I)) if (testFlags(I))
@@ -734,7 +734,7 @@ void LLInst::flops(std::ostringstream &out)
/* Note that op is set to the escape number, e.g. /* Note that op is set to the escape number, e.g.
esc 0x38 is FILD */ esc 0x38 is FILD */
if ((dst.regi == 0) || (dst.regi >= INDEXBASE)) if ((dst.regi == 0) || (dst.regi >= INDEX_BX_SI))
{ {
/* The mod/rm mod bits are not set to 11 (i.e. register). This is the normal floating point opcode */ /* The mod/rm mod bits are not set to 11 (i.e. register). This is the normal floating point opcode */
out<<szFlops1[op]<<' '; out<<szFlops1[op]<<' ';

View File

@@ -5,11 +5,7 @@
#include "dcc.h" #include "dcc.h"
#include <string.h> #include <string.h>
#if __BORLAND__
#include <alloc.h>
#else
#include <malloc.h> /* For free() */ #include <malloc.h> /* For free() */
#endif
#include "graph.h" #include "graph.h"
//static BB * rmJMP(Function * pProc, int marker, BB * pBB); //static BB * rmJMP(Function * pProc, int marker, BB * pBB);
@@ -134,7 +130,7 @@ CondJumps:
pBB->edges[0].ip = (uint32_t)start; pBB->edges[0].ip = (uint32_t)start;
} }
} }
break; break;
} }
} }
} }
@@ -143,20 +139,20 @@ CondJumps:
for (; iter!=heldBBs.end(); ++iter) for (; iter!=heldBBs.end(); ++iter)
{ {
pBB = *iter; pBB = *iter;
for (i = 0; i < pBB->edges.size(); i++) for (size_t edeg_idx = 0; edeg_idx < pBB->edges.size(); edeg_idx++)
{ {
ip = pBB->edges[i].ip; ip = pBB->edges[edeg_idx].ip;
if (ip >= SYNTHESIZED_MIN) if (ip >= SYNTHESIZED_MIN)
{ {
fatalError (INVALID_SYNTHETIC_BB); fatalError (INVALID_SYNTHETIC_BB);
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()->loc_ip==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;
pBB->edges[i].BBptr = psBB; pBB->edges[edeg_idx].BBptr = psBB;
psBB->inEdges.push_back((BB *)nullptr); psBB->inEdges.push_back((BB *)nullptr);
} }
} }
@@ -218,14 +214,14 @@ void Function::freeCFG()
****************************************************************************/ ****************************************************************************/
void Function::compressCFG() void Function::compressCFG()
{ {
BB * pBB, *pNxt; BB *pNxt;
int ip, first=0, last, i; int ip, first=0, last;
/* 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 #ifdef _lint
auto iter=m_cfg.begin();
for (;iter!=m_cfg.end(); ++iter) for (auto iter=m_cfg.begin(); iter!=m_cfg.end(); ++iter)
{ {
BB *pBB(*iter); BB *pBB(*iter);
#else #else
@@ -235,9 +231,9 @@ void Function::compressCFG()
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;
#ifdef _lint #ifdef _lint
for (auto iter2=pBB->edges().begin(); iter2!=pBB->edges().end(); ++iter2) for (auto iter2=pBB->edges.begin(); iter2!=pBB->edges.end(); ++iter2)
{ {
TYPEADR_TYPE &edgeRef(*iter); TYPEADR_TYPE &edgeRef(*iter2);
#else #else
for (TYPEADR_TYPE &edgeRef : pBB->edges) for (TYPEADR_TYPE &edgeRef : pBB->edges)
{ {
@@ -265,7 +261,7 @@ void Function::compressCFG()
for(auto iter=m_cfg.begin(); iter!=m_cfg.end(); ++iter) for(auto iter=m_cfg.begin(); iter!=m_cfg.end(); ++iter)
{ {
pBB = *iter; BB * pBB = *iter;
if (pBB->inEdges.empty()) if (pBB->inEdges.empty())
{ {
if (iter == m_cfg.begin()) /* Init it misses out on */ if (iter == m_cfg.begin()) /* Init it misses out on */
@@ -330,8 +326,8 @@ BB *BB::rmJMP(int marker, BB * pBB)
{ {
pBB->front().ll()->setFlags(NO_CODE); pBB->front().ll()->setFlags(NO_CODE);
pBB->front().invalidate(); pBB->front().invalidate();
// pProc->Icode.setFlags(pBB->start, NO_CODE); // pProc->Icode.setFlags(pBB->start, NO_CODE);
// pProc->Icode.SetLlInvalid(pBB->start, TRUE); // pProc->Icode.SetLlInvalid(pBB->start, TRUE);
} }
} while (pBB->nodeType != NOWHERE_NODE); } while (pBB->nodeType != NOWHERE_NODE);
@@ -364,7 +360,7 @@ void BB::mergeFallThrough( CIcodeRec &Icode)
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->end(),pChild->begin(),[](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->begin()) if (iter != pChild->begin())
break; break;
@@ -390,8 +386,8 @@ void BB::mergeFallThrough( CIcodeRec &Icode)
/* Process all out edges recursively */ /* Process all out edges recursively */
for (i = 0; i < edges.size(); i++) for (i = 0; i < edges.size(); i++)
if (edges[i].BBptr->traversed != DFS_MERGE) if (edges[i].BBptr->traversed != DFS_MERGE)
edges[i].BBptr->mergeFallThrough(Icode); edges[i].BBptr->mergeFallThrough(Icode);
} }
@@ -406,7 +402,7 @@ void BB::dfsNumbering(std::vector<BB *> &dfsLast, int *first, int *last)
dfsFirstNum = (*first)++; dfsFirstNum = (*first)++;
/* index is being used as an index to inEdges[]. */ /* index is being used as an index to inEdges[]. */
// for (i = 0; i < edges.size(); i++) // for (i = 0; i < edges.size(); i++)
#ifdef _lint #ifdef _lint
for (auto i=edges.begin(); i!=edges.end(); ++i) for (auto i=edges.begin(); i!=edges.end(); ++i)
{ {

View File

@@ -59,10 +59,10 @@ void ICODE::newCallHl()
/* Places the new HLI_POP/HLI_PUSH/HLI_RET high-level operand in the high-level icode /* Places the new HLI_POP/HLI_PUSH/HLI_RET high-level operand in the high-level icode
* array */ * array */
void ICODE::setUnary(hlIcode op, COND_EXPR *exp) void ICODE::setUnary(hlIcode op, COND_EXPR *_exp)
{ {
type = HIGH_LEVEL; type = HIGH_LEVEL;
hl()->set(op,exp); hl()->set(op,_exp);
} }
@@ -285,8 +285,9 @@ void Function::highLevelGen()
int numIcode; /* number of icode instructions */ int numIcode; /* number of icode instructions */
iICODE pIcode; /* ptr to current icode node */ iICODE pIcode; /* ptr to current icode node */
COND_EXPR *lhs, *rhs; /* left- and right-hand side of expression */ COND_EXPR *lhs, *rhs; /* left- and right-hand side of expression */
uint32_t flg; /* icode flags */ uint32_t _flg; /* icode flags */
numIcode = Icode.size(); numIcode = Icode.size();
lhs=rhs=0;
for (iICODE i = Icode.begin(); i!=Icode.end() ; ++i) for (iICODE i = Icode.begin(); i!=Icode.end() ; ++i)
{ {
assert(numIcode==Icode.size()); assert(numIcode==Icode.size());
@@ -296,9 +297,9 @@ 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);
@@ -606,7 +607,7 @@ void ICODE::writeDU()
int my_idx = loc_ip; int my_idx = loc_ip;
{ {
ostringstream ostr; ostringstream ostr;
for (int i = 0; i < (INDEXBASE-1); i++) for (int i = 0; i < (INDEX_BX_SI-1); i++)
if (du.def[i]) if (du.def[i])
ostr << allRegs[i] << " "; ostr << allRegs[i] << " ";
if (!ostr.str().empty()) if (!ostr.str().empty())
@@ -614,12 +615,12 @@ void ICODE::writeDU()
} }
{ {
ostringstream ostr; ostringstream ostr;
for (int i = 0; i < (INDEXBASE-1); i++) for (int i = 0; i < (INDEX_BX_SI-1); i++)
if (du.def[i]) if (du.def[i])
ostr << allRegs[i] << " "; ostr << allRegs[i] << " ";
if (!ostr.str().empty()) if (!ostr.str().empty())
printf ("Def (reg) = %s\n", ostr.str().c_str()); printf ("Def (reg) = %s\n", ostr.str().c_str());
for (int i = 0; i < INDEXBASE; i++) for (int i = 0; i < INDEX_BX_SI; i++)
{ {
if (du.use[i]) if (du.use[i])
ostr << allRegs[i] << " "; ostr << allRegs[i] << " ";

View File

@@ -27,7 +27,7 @@ ICODE * CIcodeRec::addIcode(ICODE *pIcode)
return &back(); return &back();
} }
void CIcodeRec::SetInBB(int start, int end, BB *pnewBB) void CIcodeRec::SetInBB(int start, int _end, BB *pnewBB)
{ {
#ifdef _lint #ifdef _lint
for (auto ik=this->begin(); ik!=this->end(); ++ik) for (auto ik=this->begin(); ik!=this->end(); ++ik)
@@ -37,18 +37,16 @@ void CIcodeRec::SetInBB(int start, int end, BB *pnewBB)
for(ICODE &icode : *this) for(ICODE &icode : *this)
{ {
#endif #endif
if((icode.loc_ip>=start) and (icode.loc_ip<=end)) if((icode.loc_ip>=start) and (icode.loc_ip<=_end))
icode.inBB = pnewBB; icode.inBB = pnewBB;
} }
// for (int i = start; i <= end; i++)
// at(i).inBB = pnewBB;
} }
/* labelSrchRepl - Searches the icodes for instruction with label = target, and /* labelSrchRepl - Searches the icodes for instruction with label = target, and
replaces *pIndex with an icode index */ replaces *pIndex with an icode index */
bool CIcodeRec::labelSrch(uint32_t target, uint32_t &pIndex) bool CIcodeRec::labelSrch(uint32_t target, uint32_t &pIndex)
{ {
int i;
iICODE location=labelSrch(target); iICODE location=labelSrch(target);
if(end()==location) if(end()==location)
return false; return false;
@@ -57,7 +55,7 @@ bool CIcodeRec::labelSrch(uint32_t target, uint32_t &pIndex)
} }
CIcodeRec::iterator CIcodeRec::labelSrch(uint32_t target) CIcodeRec::iterator CIcodeRec::labelSrch(uint32_t target)
{ {
int i;
return find_if(begin(),end(),[target](ICODE &l) -> bool {return l.ll()->label==target;}); return find_if(begin(),end(),[target](ICODE &l) -> bool {return l.ll()->label==target;});
} }
ICODE * CIcodeRec::GetIcode(int ip) ICODE * CIcodeRec::GetIcode(int ip)
@@ -68,7 +66,7 @@ ICODE * CIcodeRec::GetIcode(int ip)
return &(*res); return &(*res);
} }
extern char *indent(int level);
extern int getNextLabel(); extern int getNextLabel();
extern bundle cCode; extern bundle cCode;
/* Checks the given icode to determine whether it has a label associated /* Checks the given icode to determine whether it has a label associated
@@ -88,7 +86,7 @@ void LLInst::emitGotoLabel (int indLevel)
* the code */ * the code */
addLabelBundle (cCode.code, codeIdx, hllLabNum); addLabelBundle (cCode.code, codeIdx, hllLabNum);
} }
cCode.appendCode( "%sgoto L%ld;\n", indent(indLevel), hllLabNum); cCode.appendCode( "%sgoto L%ld;\n", indentStr(indLevel), hllLabNum);
stats.numHLIcode++; stats.numHLIcode++;
} }

View File

@@ -221,7 +221,7 @@ void Function::findIdioms()
* binds jump target addresses to icode offsets. */ * binds jump target addresses to icode offsets. */
void Function::bindIcodeOff() void Function::bindIcodeOff()
{ {
int i; /* idx into icode array */
iICODE pIcode; /* ptr icode array */ iICODE pIcode; /* ptr icode array */
uint32_t *p; /* for case table */ uint32_t *p; /* for case table */

View File

@@ -105,7 +105,7 @@ bool Idiom18::match(iICODE picode)
/* not supported yet */ /* not supported yet */
type = 0; type = 0;
} }
else if (m_icodes[1]->ll()->dst.regi < INDEXBASE) /* register */ else if (m_icodes[1]->ll()->dst.regi < INDEX_BX_SI) /* register */
{ {
if ((m_icodes[1]->ll()->dst.regi == rSI) && (m_func->flg & SI_REGVAR)) if ((m_icodes[1]->ll()->dst.regi == rSI) && (m_func->flg & SI_REGVAR))
type = 1; type = 1;
@@ -131,7 +131,7 @@ bool Idiom18::match(iICODE picode)
if (m_icodes[0]->ll()->match(iMOV) && (m_icodes[0]->ll()->src.regi == m_icodes[1]->ll()->dst.regi)) if (m_icodes[0]->ll()->match(iMOV) && (m_icodes[0]->ll()->src.regi == m_icodes[1]->ll()->dst.regi))
{ {
regi = m_icodes[0]->ll()->dst.regi; regi = m_icodes[0]->ll()->dst.regi;
if ((regi > 0) && (regi < INDEXBASE)) if ((regi > 0) && (regi < INDEX_BX_SI))
{ {
if ( m_icodes[2]->ll()->match(iCMP) && (m_icodes[2]->ll()->dst.regi == regi) && if ( m_icodes[2]->ll()->match(iCMP) && (m_icodes[2]->ll()->dst.regi == regi) &&
m_icodes[3]->ll()->conditionalJump() ) m_icodes[3]->ll()->conditionalJump() )
@@ -143,7 +143,7 @@ bool Idiom18::match(iICODE picode)
if (m_icodes[0]->ll()->match(iMOV) && (m_icodes[0]->ll()->src.off == m_icodes[1]->ll()->dst.off)) if (m_icodes[0]->ll()->match(iMOV) && (m_icodes[0]->ll()->src.off == m_icodes[1]->ll()->dst.off))
{ {
regi = m_icodes[0]->ll()->dst.regi; regi = m_icodes[0]->ll()->dst.regi;
if ((regi > 0) && (regi < INDEXBASE)) if ((regi > 0) && (regi < INDEX_BX_SI))
{ {
if ( m_icodes[2]->ll()->match(iCMP) && (m_icodes[2]->ll()->dst.regi == regi) && if ( m_icodes[2]->ll()->match(iCMP) && (m_icodes[2]->ll()->dst.regi == regi) &&
m_icodes[3]->ll()->conditionalJump() ) m_icodes[3]->ll()->conditionalJump() )
@@ -194,7 +194,7 @@ bool Idiom19::match(iICODE picode)
m_is_dec = m_icodes[0]->ll()->match(iDEC); m_is_dec = m_icodes[0]->ll()->match(iDEC);
if (m_icodes[0]->ll()->dst.regi == 0) /* global variable */ if (m_icodes[0]->ll()->dst.regi == 0) /* global variable */
/* not supported yet */ ; /* not supported yet */ ;
else if (m_icodes[0]->ll()->dst.regi < INDEXBASE) /* register */ else if (m_icodes[0]->ll()->dst.regi < INDEX_BX_SI) /* register */
{ {
// if (((picode->ll()->dst.regi == rSI) && (pproc->flg & SI_REGVAR)) || // if (((picode->ll()->dst.regi == rSI) && (pproc->flg & SI_REGVAR)) ||
// ((picode->ll()->dst.regi == rDI) && (pproc->flg & DI_REGVAR))) // ((picode->ll()->dst.regi == rDI) && (pproc->flg & DI_REGVAR)))
@@ -253,7 +253,7 @@ bool Idiom20::match(iICODE picode)
{ {
/* not supported yet */ ; /* not supported yet */ ;
} }
else if (m_icodes[0]->ll()->dst.regi < INDEXBASE) /* register */ else if (m_icodes[0]->ll()->dst.regi < INDEX_BX_SI) /* register */
{ {
if ((m_icodes[0]->ll()->dst.regi == rSI) && (m_func->flg & SI_REGVAR)) if ((m_icodes[0]->ll()->dst.regi == rSI) && (m_func->flg & SI_REGVAR))
type = 1; type = 1;
@@ -275,7 +275,7 @@ bool Idiom20::match(iICODE picode)
(m_icodes[1]->ll()->src.regi == m_icodes[0]->ll()->dst.regi)) (m_icodes[1]->ll()->src.regi == m_icodes[0]->ll()->dst.regi))
{ {
regi = m_icodes[1]->ll()->dst.regi; regi = m_icodes[1]->ll()->dst.regi;
if ((regi > 0) && (regi < INDEXBASE)) if ((regi > 0) && (regi < INDEX_BX_SI))
{ {
if (m_icodes[2]->ll()->match(iCMP,(eReg)regi) && if (m_icodes[2]->ll()->match(iCMP,(eReg)regi) &&
m_icodes[3]->ll()->conditionalJump()) m_icodes[3]->ll()->conditionalJump())
@@ -289,7 +289,7 @@ bool Idiom20::match(iICODE picode)
(m_icodes[1]->ll()->src.off == m_icodes[0]->ll()->dst.off)) (m_icodes[1]->ll()->src.off == m_icodes[0]->ll()->dst.off))
{ {
regi = m_icodes[1]->ll()->dst.regi; regi = m_icodes[1]->ll()->dst.regi;
if ((regi > 0) && (regi < INDEXBASE)) if ((regi > 0) && (regi < INDEX_BX_SI))
{ {
if (m_icodes[2]->ll()->match(iCMP,(eReg)regi) && if (m_icodes[2]->ll()->match(iCMP,(eReg)regi) &&
m_icodes[3]->ll()->conditionalJump()) m_icodes[3]->ll()->conditionalJump())

View File

@@ -101,7 +101,7 @@ int Idiom17::action()
m_icodes[0]->ll()->src.proc.proc->cbParam = (int16_t)m_param_count; m_icodes[0]->ll()->src.proc.proc->cbParam = (int16_t)m_param_count;
m_icodes[0]->ll()->src.proc.cb = m_param_count; m_icodes[0]->ll()->src.proc.cb = m_param_count;
m_icodes[0]->ll()->src.proc.proc->flg |= CALL_C; m_icodes[0]->ll()->src.proc.proc->flg |= CALL_C;
for(int idx=1; idx<m_icodes.size(); ++idx) for(size_t idx=1; idx<m_icodes.size(); ++idx)
{ {
m_icodes[idx]->invalidate(); m_icodes[idx]->invalidate();
} }

View File

@@ -50,13 +50,13 @@ int Idiom1::checkStkVars (iICODE pIcode)
****************************************************************************/ ****************************************************************************/
bool Idiom1::match(iICODE picode) bool Idiom1::match(iICODE picode)
{ {
uint8_t type = 0; /* type of variable: 1 = reg-var, 2 = local */ //uint8_t type = 0; /* type of variable: 1 = reg-var, 2 = local */
uint8_t regi; /* register of the MOV */ //uint8_t regi; /* register of the MOV */
if(m_func->flg & PROC_HLL) if(m_func->flg & PROC_HLL)
return false; return false;
if(picode==m_end) if(picode==m_end)
return false; return false;
int n; //int n;
m_icodes.clear(); m_icodes.clear();
m_min_off = 0; m_min_off = 0;
/* PUSH BP as first instruction of procedure */ /* PUSH BP as first instruction of procedure */
@@ -114,7 +114,7 @@ bool Idiom1::match(iICODE picode)
} }
else // push di [push si] / push si [push di] else // push di [push si] / push si [push di]
{ {
n = checkStkVars (picode); size_t n = checkStkVars (picode);
for(int i=0; i<n; ++i) for(int i=0; i<n; ++i)
m_icodes.push_back(picode++); m_icodes.push_back(picode++);

View File

@@ -80,7 +80,7 @@ bool Idiom16::match (iICODE picode)
m_icodes[i]=picode++; m_icodes[i]=picode++;
uint8_t regi = m_icodes[0]->ll()->dst.regi; uint8_t regi = m_icodes[0]->ll()->dst.regi;
if ((regi >= rAX) && (regi < INDEXBASE)) if ((regi >= rAX) && (regi < INDEX_BX_SI))
{ {
if (m_icodes[1]->ll()->match(iSBB) && m_icodes[2]->ll()->match(iINC)) if (m_icodes[1]->ll()->match(iSBB) && m_icodes[2]->ll()->match(iINC))
if ((m_icodes[1]->ll()->dst.regi == (m_icodes[1]->ll()->src.regi)) && if ((m_icodes[1]->ll()->dst.regi == (m_icodes[1]->ll()->src.regi)) &&

View File

@@ -57,7 +57,7 @@ int Idiom8::action()
****************************************************************************/ ****************************************************************************/
bool Idiom15::match(iICODE pIcode) bool Idiom15::match(iICODE pIcode)
{ {
int n = 1;
uint8_t regi; uint8_t regi;
if(distance(pIcode,m_end)<2) if(distance(pIcode,m_end)<2)
@@ -72,7 +72,7 @@ bool Idiom15::match(iICODE pIcode)
pIcode->ll()->match(iSHL,(eReg)regi,I) and pIcode->ll()->match(iSHL,(eReg)regi,I) and
(pIcode->ll()->src.op() == 1) ) (pIcode->ll()->src.op() == 1) )
{ {
n++;
m_icodes.push_back(pIcode++); m_icodes.push_back(pIcode++);
} }
return m_icodes.size()>1; return m_icodes.size()>1;
@@ -80,13 +80,13 @@ bool Idiom15::match(iICODE pIcode)
int Idiom15::action() int Idiom15::action()
{ {
COND_EXPR *lhs,*rhs,*exp; COND_EXPR *lhs,*rhs,*_exp;
lhs = COND_EXPR::idReg (m_icodes[0]->ll()->dst.regi, lhs = COND_EXPR::idReg (m_icodes[0]->ll()->dst.regi,
m_icodes[0]->ll()->getFlag() & NO_SRC_B, m_icodes[0]->ll()->getFlag() & NO_SRC_B,
&m_func->localId); &m_func->localId);
rhs = COND_EXPR::idKte (m_icodes.size(), 2); rhs = COND_EXPR::idKte (m_icodes.size(), 2);
exp = COND_EXPR::boolOp (lhs, rhs, SHL); _exp = COND_EXPR::boolOp (lhs, rhs, SHL);
m_icodes[0]->setAsgn(lhs, exp); m_icodes[0]->setAsgn(lhs, _exp);
for (size_t i=1; i<m_icodes.size()-1; ++i) for (size_t i=1; i<m_icodes.size()-1; ++i)
{ {
m_icodes[i]->invalidate(); m_icodes[i]->invalidate();

View File

@@ -28,7 +28,7 @@ bool Idiom21::match (iICODE picode)
dst = &m_icodes[0]->ll()->dst; dst = &m_icodes[0]->ll()->dst;
src = &m_icodes[0]->ll()->src; src = &m_icodes[0]->ll()->src;
if ((dst->regi == src->regi) && (dst->regi > 0) && (dst->regi < INDEXBASE)) if ((dst->regi == src->regi) && (dst->regi > 0) && (dst->regi < INDEX_BX_SI))
{ {
if ((dst->regi == rDX) && m_icodes[1]->ll()->match(rAX)) if ((dst->regi == rDX) && m_icodes[1]->ll()->match(rAX))
return true; return true;
@@ -68,12 +68,12 @@ bool Idiom7::match(iICODE picode)
if ((dst->segValue == src->segValue) && (dst->off == src->off)) if ((dst->segValue == src->segValue) && (dst->off == src->off))
return true; return true;
} }
else if (dst->regi < INDEXBASE) /* register */ else if (dst->regi < INDEX_BX_SI) /* register */
{ {
if (dst->regi == src->regi) if (dst->regi == src->regi)
return true; return true;
} }
else if ((dst->off) && (dst->seg == rSS) && (dst->regi == INDEXBASE + 6)) /* offset from BP */ else if ((dst->off) && (dst->seg == rSS) && (dst->regi == INDEX_BP)) /* offset from BP */
{ {
if ((dst->off == src->off) && (dst->seg == src->seg) && (dst->regi == src->regi)) if ((dst->off == src->off) && (dst->seg == src->seg) && (dst->regi == src->regi))
return true; return true;
@@ -115,7 +115,7 @@ bool Idiom10::match(iICODE pIcode)
/* Check OR reg, reg */ /* Check OR reg, reg */
if (not m_icodes[0]->ll()->testFlags(I) && if (not m_icodes[0]->ll()->testFlags(I) &&
(m_icodes[0]->ll()->src.regi > 0) && (m_icodes[0]->ll()->src.regi > 0) &&
(m_icodes[0]->ll()->src.regi < INDEXBASE) && (m_icodes[0]->ll()->src.regi < INDEX_BX_SI) &&
(m_icodes[0]->ll()->src.regi == m_icodes[0]->ll()->dst.regi)) (m_icodes[0]->ll()->src.regi == m_icodes[0]->ll()->dst.regi))
if (m_icodes[1]->ll()->match(iJNE)) //.conditionalJump() if (m_icodes[1]->ll()->match(iJNE)) //.conditionalJump()
{ {

View File

@@ -116,7 +116,7 @@ int LOCAL_ID::newByteWordStk(hlType t, int off, uint8_t regOff)
* regi: indexed register into global variable * regi: indexed register into global variable
* ix: index into icode array * ix: index into icode array
* t: HIGH_LEVEL type */ * t: HIGH_LEVEL type */
int LOCAL_ID::newIntIdx(int16_t seg, int16_t off, uint8_t regi, int ix, hlType t) int LOCAL_ID::newIntIdx(int16_t seg, int16_t off, eReg regi, int ix, hlType t)
{ {
int idx; int idx;
@@ -145,7 +145,7 @@ int LOCAL_ID::newIntIdx(int16_t seg, int16_t off, uint8_t regi, int ix, hlType t
* TYPE_LONG_(UN)SIGN and returns the index to this new entry. */ * TYPE_LONG_(UN)SIGN and returns the index to this new entry. */
int LOCAL_ID::newLongReg(hlType t, eReg regH, eReg regL, iICODE ix_) int LOCAL_ID::newLongReg(hlType t, eReg regH, eReg regL, iICODE ix_)
{ {
int idx; size_t idx;
//iICODE ix_; //iICODE ix_;
/* Check for entry in the table */ /* Check for entry in the table */
for (idx = 0; idx < id_arr.size(); idx++) for (idx = 0; idx < id_arr.size(); idx++)
@@ -183,7 +183,7 @@ int LOCAL_ID::newLongReg(hlType t, eReg regH, eReg regL, iICODE ix_)
* TYPE_LONG_(UN)SIGN and returns the index to this new entry. */ * TYPE_LONG_(UN)SIGN and returns the index to this new entry. */
int LOCAL_ID::newLongGlb(int16_t seg, int16_t offH, int16_t offL,hlType t) int LOCAL_ID::newLongGlb(int16_t seg, int16_t offH, int16_t offL,hlType t)
{ {
int idx; size_t idx;
/* Check for entry in the table */ /* Check for entry in the table */
for (idx = 0; idx < id_arr.size(); idx++) for (idx = 0; idx < id_arr.size(); idx++)
@@ -209,7 +209,8 @@ int LOCAL_ID::newLongGlb(int16_t seg, int16_t offH, int16_t offL,hlType t)
* entry; otherwise creates a new global identifier node of type * entry; otherwise creates a new global identifier node of type
* TYPE_LONG_(UN)SIGN and returns the index to this new entry. */ * TYPE_LONG_(UN)SIGN and returns the index to this new entry. */
int LOCAL_ID::newLongIdx( int16_t seg, int16_t offH, int16_t offL,uint8_t regi, hlType t) int LOCAL_ID::newLongIdx( int16_t seg, int16_t offH, int16_t offL,uint8_t regi, hlType t)
{ int idx; {
size_t idx;
/* Check for entry in the table */ /* Check for entry in the table */
for (idx = 0; idx < id_arr.size(); idx++) for (idx = 0; idx < id_arr.size(); idx++)
@@ -237,7 +238,7 @@ int LOCAL_ID::newLongIdx( int16_t seg, int16_t offH, int16_t offL,uint8_t regi,
* Returns the index to this entry. */ * Returns the index to this entry. */
int LOCAL_ID::newLongStk(hlType t, int offH, int offL) int LOCAL_ID::newLongStk(hlType t, int offH, int offL)
{ {
int idx; size_t idx;
/* Check for entry in the table */ /* Check for entry in the table */
for (idx = 0; idx < id_arr.size(); idx++) for (idx = 0; idx < id_arr.size(); idx++)
@@ -266,7 +267,7 @@ int LOCAL_ID::newLongStk(hlType t, int offH, int offL)
* number in an expression record. */ * number in an expression record. */
int LOCAL_ID::newLong(opLoc sd, iICODE pIcode, hlFirst f, iICODE ix,operDu du, iICODE atOffset) int LOCAL_ID::newLong(opLoc sd, iICODE pIcode, hlFirst f, iICODE ix,operDu du, iICODE atOffset)
{ {
int idx; size_t idx;
LLOperand *pmH, *pmL; LLOperand *pmH, *pmL;
// iICODE atOffset(pIcode); // iICODE atOffset(pIcode);
// advance(atOffset,off); // advance(atOffset,off);
@@ -285,7 +286,7 @@ int LOCAL_ID::newLong(opLoc sd, iICODE pIcode, hlFirst f, iICODE ix,operDu du, i
if (pmL->regi == 0) /* global variable */ if (pmL->regi == 0) /* global variable */
idx = newLongGlb(pmH->segValue, pmH->off, pmL->off, TYPE_LONG_SIGN); idx = newLongGlb(pmH->segValue, pmH->off, pmL->off, TYPE_LONG_SIGN);
else if (pmL->regi < INDEXBASE) /* register */ else if (pmL->regi < INDEX_BX_SI) /* register */
{ {
idx = newLongReg(TYPE_LONG_SIGN, pmH->regi, pmL->regi, ix); idx = newLongReg(TYPE_LONG_SIGN, pmH->regi, pmL->regi, ix);
if (f == HIGH_FIRST) if (f == HIGH_FIRST)
@@ -295,9 +296,9 @@ int LOCAL_ID::newLong(opLoc sd, iICODE pIcode, hlFirst f, iICODE ix,operDu du, i
} }
else if (pmL->off) { /* offset */ else if (pmL->off) { /* offset */
if ((pmL->seg == rSS) && (pmL->regi == INDEXBASE + 6)) /* idx on bp */ if ((pmL->seg == rSS) && (pmL->regi == INDEX_BP)) /* idx on bp */
idx = newLongStk(TYPE_LONG_SIGN, pmH->off, pmL->off); idx = newLongStk(TYPE_LONG_SIGN, pmH->off, pmL->off);
else if ((pmL->seg == rDS) && (pmL->regi == INDEXBASE + 7)) /* bx */ else if ((pmL->seg == rDS) && (pmL->regi == INDEX_BX)) /* bx */
{ /* glb var indexed on bx */ { /* glb var indexed on bx */
printf("Bx indexed global, BX is an unused parameter to newLongIdx\n"); printf("Bx indexed global, BX is an unused parameter to newLongIdx\n");
idx = newLongIdx(pmH->segValue, pmH->off, pmL->off,rBX,TYPE_LONG_SIGN); idx = newLongIdx(pmH->segValue, pmH->off, pmL->off,rBX,TYPE_LONG_SIGN);

26
src/machine_x86.cpp Normal file
View File

@@ -0,0 +1,26 @@
#include "machine_x86.h"
// Index registers **** temp solution
static const std::string regNames[] = {
"undef",
"ax", "cx", "dx", "bx",
"sp", "bp", "si", "di",
"es", "cs", "ss", "ds",
"al", "cl", "dl", "bl",
"ah", "ch", "dh", "bh",
"tmp",
"bx+si", "bx+di", "bp+si", "bp+di",
"si", "di", "bp", "bx"
};
/* uint8_t and uint16_t registers */
Machine_X86::Machine_X86()
{
static_assert((sizeof(regNames)/sizeof(std::string))==LAST_REG,
"Reg count not equal number of strings");
}
const std::string &Machine_X86::regName(eReg r)
{
return regNames[r];
}

View File

@@ -425,9 +425,9 @@ boolT Function::process_JMP (ICODE & pIcode, STATE *pstate, CALL_GRAPH * pcallGr
/* Ensure we have a uint16_t offset & valid seg */ /* Ensure we have a uint16_t offset & valid seg */
if (pIcode.ll()->match(iJMP) and (pIcode.ll()->testFlags(WORD_OFF)) && if (pIcode.ll()->match(iJMP) and (pIcode.ll()->testFlags(WORD_OFF)) &&
pstate->f[seg] && pstate->f[seg] &&
(pIcode.ll()->src.regi == INDEXBASE + 4 || (pIcode.ll()->src.regi == INDEX_SI ||
pIcode.ll()->src.regi == INDEXBASE + 5 || /* Idx reg. BX, SI, DI */ pIcode.ll()->src.regi == INDEX_DI || /* Idx reg. BX, SI, DI */
pIcode.ll()->src.regi == INDEXBASE + 7)) pIcode.ll()->src.regi == INDEX_BX))
{ {
offTable = ((uint32_t)(uint16_t)pstate->r[seg] << 4) + pIcode.ll()->src.off; offTable = ((uint32_t)(uint16_t)pstate->r[seg] << 4) + pIcode.ll()->src.off;
@@ -438,7 +438,7 @@ boolT Function::process_JMP (ICODE & pIcode, STATE *pstate, CALL_GRAPH * pcallGr
* This is stored in the current state as if we had just * This is stored in the current state as if we had just
* followed a JBE branch (i.e. [reg] lies between 0 - immed). * followed a JBE branch (i.e. [reg] lies between 0 - immed).
*/ */
if (pstate->JCond.regi == i2r[pIcode.ll()->src.regi-(INDEXBASE+4)]) if (pstate->JCond.regi == i2r[pIcode.ll()->src.regi-(INDEX_BX_SI+4)])
endTable = offTable + pstate->JCond.immed; endTable = offTable + pstate->JCond.immed;
else else
endTable = (uint32_t)prog.cbImage; endTable = (uint32_t)prog.cbImage;
@@ -488,7 +488,7 @@ boolT Function::process_JMP (ICODE & pIcode, STATE *pstate, CALL_GRAPH * pcallGr
for (i = offTable, k = 0; i < endTable; i += 2) for (i = offTable, k = 0; i < endTable; i += 2)
{ {
memcpy(&StCopy, pstate, sizeof(STATE)); StCopy = *pstate;
StCopy.IP = cs + LH(&prog.Image[i]); StCopy.IP = cs + LH(&prog.Image[i]);
iICODE last_current_insn = (++Icode.rbegin()).base(); iICODE last_current_insn = (++Icode.rbegin()).base();
ip = Icode.size(); ip = Icode.size();
@@ -643,7 +643,7 @@ static void process_MOV(LLInst & ll, STATE * pstate)
SYM * psym, *psym2; /* Pointer to symbol in global symbol table */ SYM * psym, *psym2; /* Pointer to symbol in global symbol table */
uint8_t dstReg = ll.dst.regi; uint8_t dstReg = ll.dst.regi;
uint8_t srcReg = ll.src.regi; uint8_t srcReg = ll.src.regi;
if (dstReg > 0 && dstReg < INDEXBASE) if (dstReg > 0 && dstReg < INDEX_BX_SI)
{ {
if (ll.testFlags(I)) if (ll.testFlags(I))
pstate->setState( dstReg, (int16_t)ll.src.op()); pstate->setState( dstReg, (int16_t)ll.src.op());
@@ -653,7 +653,7 @@ static void process_MOV(LLInst & ll, STATE * pstate)
if (psym && ((psym->flg & SEG_IMMED) || psym->duVal.val)) if (psym && ((psym->flg & SEG_IMMED) || psym->duVal.val))
pstate->setState( dstReg, LH(&prog.Image[psym->label])); pstate->setState( dstReg, LH(&prog.Image[psym->label]));
} }
else if (srcReg < INDEXBASE && pstate->f[srcReg]) /* reg */ else if (srcReg < INDEX_BX_SI && pstate->f[srcReg]) /* reg */
{ {
pstate->setState( dstReg, pstate->r[srcReg]); pstate->setState( dstReg, pstate->r[srcReg]);
@@ -682,12 +682,12 @@ static void process_MOV(LLInst & ll, STATE * pstate)
{ {
prog.Image[psym->label] = (uint8_t)prog.Image[psym2->label]; prog.Image[psym->label] = (uint8_t)prog.Image[psym2->label];
if(psym->size>1) if(psym->size>1)
prog.Image[psym->label+1] = (uint8_t)(prog.Image[psym2->label+1] >> 8); prog.Image[psym->label+1] = prog.Image[psym2->label+1];//(uint8_t)(prog.Image[psym2->label+1] >> 8);
psym->duVal.setFlags(eDuVal::DEF); psym->duVal.setFlags(eDuVal::DEF);
psym2->duVal.setFlags(eDuVal::USE); psym2->duVal.setFlags(eDuVal::USE);
} }
} }
else if (srcReg < INDEXBASE && pstate->f[srcReg]) /* reg */ else if (srcReg < INDEX_BX_SI && pstate->f[srcReg]) /* reg */
{ {
prog.Image[psym->label] = (uint8_t)pstate->r[srcReg]; prog.Image[psym->label] = (uint8_t)pstate->r[srcReg];
if(psym->size>1) if(psym->size>1)
@@ -917,9 +917,9 @@ static void use (opLoc d, ICODE & pIcode, Function * pProc, STATE * pstate, int
LLOperand * pm = (d == SRC)? &pIcode.ll()->src: &pIcode.ll()->dst; LLOperand * pm = (d == SRC)? &pIcode.ll()->src: &pIcode.ll()->dst;
SYM * psym; SYM * psym;
if (pm->regi == 0 || pm->regi >= INDEXBASE) if (pm->regi == 0 || pm->regi >= INDEX_BX_SI)
{ {
if (pm->regi == INDEXBASE + 6) /* indexed on bp */ if (pm->regi == INDEX_BP) /* indexed on bp */
{ {
if (pm->off >= 2) if (pm->off >= 2)
updateFrameOff (&pProc->args, pm->off, size, eDuVal::USE); updateFrameOff (&pProc->args, pm->off, size, eDuVal::USE);
@@ -927,13 +927,13 @@ static void use (opLoc d, ICODE & pIcode, Function * pProc, STATE * pstate, int
pProc->localId.newByteWordStk (TYPE_WORD_SIGN, pm->off, 0); pProc->localId.newByteWordStk (TYPE_WORD_SIGN, pm->off, 0);
} }
else if (pm->regi == INDEXBASE + 2 || pm->regi == INDEXBASE + 3) else if (pm->regi == INDEX_BP_SI || pm->regi == INDEX_BP_DI)
pProc->localId.newByteWordStk (TYPE_WORD_SIGN, pm->off, pProc->localId.newByteWordStk (TYPE_WORD_SIGN, pm->off,
(uint8_t)((pm->regi == INDEXBASE + 2) ? rSI : rDI)); (uint8_t)((pm->regi == INDEX_BP_SI) ? rSI : rDI));
else if ((pm->regi >= INDEXBASE + 4) && (pm->regi <= INDEXBASE + 7)) else if ((pm->regi >= INDEX_SI) && (pm->regi <= INDEX_BX))
{ {
if ((pm->seg == rDS) && (pm->regi == INDEXBASE + 7)) /* bx */ if ((pm->seg == rDS) && (pm->regi == INDEX_BX)) /* bx */
{ {
if (pm->off > 0) /* global indexed variable */ if (pm->off > 0) /* global indexed variable */
pProc->localId.newIntIdx(pm->segValue, pm->off, rBX,ix, TYPE_WORD_SIGN); pProc->localId.newIntIdx(pm->segValue, pm->off, rBX,ix, TYPE_WORD_SIGN);
@@ -964,9 +964,9 @@ static void def (opLoc d, ICODE & pIcode, Function * pProc, STATE * pstate, int
LLOperand *pm = (d == SRC)? &pIcode.ll()->src: &pIcode.ll()->dst; LLOperand *pm = (d == SRC)? &pIcode.ll()->src: &pIcode.ll()->dst;
SYM * psym; SYM * psym;
if (pm->regi == 0 || pm->regi >= INDEXBASE) if (pm->regi == 0 || pm->regi >= INDEX_BX_SI)
{ {
if (pm->regi == INDEXBASE + 6) /* indexed on bp */ if (pm->regi == INDEX_BP) /* indexed on bp */
{ {
if (pm->off >= 2) if (pm->off >= 2)
updateFrameOff (&pProc->args, pm->off, size, eDEF); updateFrameOff (&pProc->args, pm->off, size, eDEF);
@@ -974,15 +974,15 @@ static void def (opLoc d, ICODE & pIcode, Function * pProc, STATE * pstate, int
pProc->localId.newByteWordStk (TYPE_WORD_SIGN, pm->off, 0); pProc->localId.newByteWordStk (TYPE_WORD_SIGN, pm->off, 0);
} }
else if (pm->regi == INDEXBASE + 2 || pm->regi == INDEXBASE + 3) else if (pm->regi == INDEX_BP_SI || pm->regi == INDEX_BP_DI)
{ {
pProc->localId.newByteWordStk(TYPE_WORD_SIGN, pm->off, pProc->localId.newByteWordStk(TYPE_WORD_SIGN, pm->off,
(uint8_t)((pm->regi == INDEXBASE + 2) ? rSI : rDI)); (uint8_t)((pm->regi == INDEX_BP_SI) ? rSI : rDI));
} }
else if ((pm->regi >= INDEXBASE + 4) && (pm->regi <= INDEXBASE + 7)) else if ((pm->regi >= INDEX_SI) && (pm->regi <= INDEX_BX))
{ {
if ((pm->seg == rDS) && (pm->regi == INDEXBASE + 7)) /* bx */ if ((pm->seg == rDS) && (pm->regi == INDEX_BX)) /* bx */
{ {
if (pm->off > 0) /* global var */ if (pm->off > 0) /* global var */
pProc->localId.newIntIdx(pm->segValue, pm->off, rBX,ix, TYPE_WORD_SIGN); pProc->localId.newIntIdx(pm->segValue, pm->off, rBX,ix, TYPE_WORD_SIGN);
@@ -1017,7 +1017,7 @@ static void use_def(opLoc d, ICODE & pIcode, Function * pProc, STATE * pstate, i
use (d, pIcode, pProc, pstate, cb, ix); use (d, pIcode, pProc, pstate, cb, ix);
if (pm->regi < INDEXBASE) /* register */ if (pm->regi < INDEX_BX_SI) /* register */
{ {
pIcode.du.def |= duReg[pm->regi]; pIcode.du.def |= duReg[pm->regi];
pIcode.du1.numRegsDef++; pIcode.du1.numRegsDef++;
@@ -1043,6 +1043,7 @@ void Function::process_operands(ICODE & pIcode, STATE * pstate)
if (! Imm) { if (! Imm) {
use(SRC, pIcode, this, pstate, cb, ix); use(SRC, pIcode, this, pstate, cb, ix);
} }
//lint -fallthrough
case iINC: case iDEC: case iNEG: case iNOT: case iINC: case iDEC: case iNEG: case iNOT:
case iAAA: case iAAD: case iAAM: case iAAS: case iAAA: case iAAD: case iAAM: case iAAS:
case iDAA: case iDAS: case iDAA: case iDAS:

View File

@@ -47,11 +47,6 @@ PatternHasher::init(int _NumEntry, int _EntryLen, int _SetSize, char _SetMin,
g = new short [NumVert + 1]; g = new short [NumVert + 1];
// visited = new bool [NumVert + 1]; // visited = new bool [NumVert + 1];
return; return;
BadAlloc:
printf("Could not allocate memory\n");
cleanup();
exit(1);
} }
void PatternHasher::cleanup(void) void PatternHasher::cleanup(void)

View File

@@ -11,22 +11,23 @@
/* Static indentation buffer */ /* Static indentation buffer */
#define indSize 61 /* size of indentation buffer; max 20 */ static constexpr int indSize=81; /* size of indentation buffer; max 20 */
static char indentBuf[indSize] = static char indentBuf[indSize] =
" "; " ";
// not static, used in icode.cpp at emitGotoLabel
static char *indent (int indLevel) // Indentation according to the depth of the statement const char *indentStr(int indLevel) // Indentation according to the depth of the statement
{ {
return (&indentBuf[indSize-(indLevel*3)-1]); return (&indentBuf[indSize-(indLevel*4)-1]);
} }
/* Inserts an outEdge at the current callGraph pointer if the newProc does /* Inserts an outEdge at the current callGraph pointer if the newProc does
* not exist. */ * not exist. */
//lint -sem(vector<CALL_GRAPH *>::push,custodial(1))
void CALL_GRAPH::insertArc (ilFunction newProc) void CALL_GRAPH::insertArc (ilFunction newProc)
{ {
CALL_GRAPH *pcg; CALL_GRAPH *pcg;
int i;
/* Check if procedure already exists */ /* Check if procedure already exists */
auto res=std::find_if(outEdges.begin(),outEdges.end(),[newProc](CALL_GRAPH *e) {return e->proc==newProc;}); auto res=std::find_if(outEdges.begin(),outEdges.end(),[newProc](CALL_GRAPH *e) {return e->proc==newProc;});
@@ -73,7 +74,7 @@ void CALL_GRAPH::writeNodeCallGraph(int indIdx)
{ {
int i; int i;
printf ("%s%s\n", indent(indIdx), proc->name.c_str()); printf ("%s%s\n", indentStr(indIdx), proc->name.c_str());
for (i = 0; i < outEdges.size(); i++) for (i = 0; i < outEdges.size(); i++)
outEdges[i]->writeNodeCallGraph (indIdx + 1); outEdges[i]->writeNodeCallGraph (indIdx + 1);
} }
@@ -108,7 +109,7 @@ void Function::newRegArg(iICODE picode, iICODE ticode)
/* Flag ticode as having register arguments */ /* Flag ticode as having register arguments */
tproc = ticode->hl()->call.proc; tproc = ticode->hl()->call.proc;
tproc->flg |= REG_ARGS; tproc->flg |= REG_ARGS;
tidx = 0;
/* Get registers and index into target procedure's local list */ /* Get registers and index into target procedure's local list */
ps = ticode->hl()->call.args; ps = ticode->hl()->call.args;
ts = &tproc->args; ts = &tproc->args;
@@ -336,7 +337,8 @@ void STKFRAME::adjustForArgType(int numArg_, hlType actType_)
return; return;
} }
/* If formal argument does not exist, do not create new ones, just /* If formal argument does not exist, do not create new ones, just
* ignore actual argument */ * ignore actual argument
*/
else else
return; return;

View File

@@ -169,7 +169,7 @@ static int longJCond23 (COND_EXPR *rhs, COND_EXPR *lhs, iICODE pIcode, int arc,
*/ */
static int longJCond22 (COND_EXPR *rhs, COND_EXPR *lhs, iICODE pIcode,iICODE pEnd) static int longJCond22 (COND_EXPR *rhs, COND_EXPR *lhs, iICODE pIcode,iICODE pEnd)
{ {
int j;
BB * pbb, * obb1, * tbb; BB * pbb, * obb1, * tbb;
if(distance(pIcode,pEnd)<4) if(distance(pIcode,pEnd)<4)
return false; return false;
@@ -229,7 +229,7 @@ static int longJCond22 (COND_EXPR *rhs, COND_EXPR *lhs, iICODE pIcode,iICODE pEn
* pProc : ptr to current procedure's record. */ * pProc : ptr to current procedure's record. */
void Function::propLongStk (int i, const ID &pLocId) void Function::propLongStk (int i, const ID &pLocId)
{ {
int off, arc; int arc;
Assignment asgn; Assignment asgn;
//COND_EXPR *lhs, *rhs; /* Pointers to left and right hand expression */ //COND_EXPR *lhs, *rhs; /* Pointers to left and right hand expression */
iICODE next1, pEnd; iICODE next1, pEnd;
@@ -389,7 +389,7 @@ int Function::findForwardLongUses(int loc_ident_idx, const ID &pLocId, iICODE be
if(next1==pEnd) if(next1==pEnd)
break; break;
LLOperand * pmH,* pmL; /* Pointers to dst LOW_LEVEL icodes */ LLOperand * pmH,* pmL; /* Pointers to dst LOW_LEVEL icodes */
int off,arc; int arc;
if ((pIcode->type == HIGH_LEVEL) || (pIcode->invalid == TRUE)) if ((pIcode->type == HIGH_LEVEL) || (pIcode->invalid == TRUE))
continue; continue;

View File

@@ -100,7 +100,7 @@ static void appendNodeInt (queue &pqH, BB *node, interval *pI)
void derSeq_Entry::findIntervals (Function *c) void derSeq_Entry::findIntervals (Function *c)
{ {
interval *pI, /* Interval being processed */ interval *pI, /* Interval being processed */
*J; /* ^ last interval in derivedGi->Ii */ *J=0; /* ^ last interval in derivedGi->Ii */
BB *h, /* Node being processed */ BB *h, /* Node being processed */
*header, /* Current interval's header node */ *header, /* Current interval's header node */
*succ; /* Successor basic block */ *succ; /* Successor basic block */

View File

@@ -422,8 +422,8 @@ static void setAddress(int i, boolT fdst, uint16_t seg, int16_t reg, uint16_t of
} }
else else
{ /* no override, check indexed register */ { /* no override, check indexed register */
if ((reg >= INDEXBASE) && (reg == INDEXBASE + 2 || if ((reg >= INDEX_BX_SI) && (reg == INDEX_BP_SI ||
reg == INDEXBASE + 3 || reg == INDEXBASE + 6)) reg == INDEX_BP_DI || reg == INDEX_BP))
{ {
pm->seg = rSS; /* indexed on bp */ pm->seg = rSS; /* indexed on bp */
} }
@@ -435,7 +435,7 @@ static void setAddress(int i, boolT fdst, uint16_t seg, int16_t reg, uint16_t of
pm->regi = (eReg)reg; pm->regi = (eReg)reg;
pm->off = (int16_t)off; pm->off = (int16_t)off;
if (reg && reg < INDEXBASE && (stateTable[i].flg & B)) if (reg && reg < INDEX_BX_SI && (stateTable[i].flg & B))
{ {
pm->regi = subRegL(pm->regi); pm->regi = subRegL(pm->regi);
} }
@@ -462,15 +462,15 @@ static void rm(int i)
pIcode->ll()->setFlags(WORD_OFF); pIcode->ll()->setFlags(WORD_OFF);
} }
else else
setAddress(i, TRUE, SegPrefix, rm + INDEXBASE, 0); setAddress(i, TRUE, SegPrefix, rm + INDEX_BX_SI, 0);
break; break;
case 1: /* 1 uint8_t disp */ case 1: /* 1 uint8_t disp */
setAddress(i, TRUE, SegPrefix, rm+INDEXBASE, (uint16_t)signex(*pInst++)); setAddress(i, TRUE, SegPrefix, rm+INDEX_BX_SI, (uint16_t)signex(*pInst++));
break; break;
case 2: /* 2 uint8_t disp */ case 2: /* 2 uint8_t disp */
setAddress(i, TRUE, SegPrefix, rm + INDEXBASE, getWord()); setAddress(i, TRUE, SegPrefix, rm + INDEX_BX_SI, getWord());
pIcode->ll()->setFlags(WORD_OFF); pIcode->ll()->setFlags(WORD_OFF);
break; break;

View File

@@ -55,7 +55,7 @@ struct TABLEINFO_TYPE
{ {
symTab=valTab=0; symTab=valTab=0;
} }
void deleteVal(uint32_t symOff, Function *symProc, boolT bSymToo); //void deleteVal(uint32_t symOff, Function *symProc, boolT bSymToo);
void create(tableType type); void create(tableType type);
void destroy(); void destroy();
private: private: