moving on

This commit is contained in:
Artur K
2012-02-22 11:17:54 +01:00
parent fa2eac979d
commit ed6f24a79a
29 changed files with 1662 additions and 1656 deletions

View File

@@ -29,9 +29,6 @@ static const char *condOpSym[] = { " <= ", " < ", " == ", " != ", " > ", " >= ",
// COND_EXPR *exp;
// struct _EXP_STK *next;
//} EXP_STK;
typedef std::list<COND_EXPR *> EXP_STK;
static EXP_STK expStk; /* local expression stack */
/* Returns the integer i in C hexadecimal format */
static char *hexStr (uint16_t i)
@@ -950,47 +947,3 @@ void COND_EXPR::release()
}
delete (this);
}
/***************************************************************************
* Expression stack functions
**************************************************************************/
/* Reinitalizes the expression stack (expStk) to NULL, by freeing all the
* space allocated (if any). */
void initExpStk()
{
expStk.clear();
}
/* Pushes the given expression onto the local stack (expStk). */
void pushExpStk (COND_EXPR *expr)
{
expStk.push_back(expr);
}
/* Returns the element on the top of the local expression stack (expStk),
* and deallocates the space allocated by this node.
* If there are no elements on the stack, returns NULL. */
COND_EXPR *popExpStk()
{
if(expStk.empty())
return 0;
COND_EXPR *topExp = expStk.back();
expStk.pop_back();
return topExp;
}
/* Returns the number of elements available in the expression stack */
Int numElemExpStk()
{
return expStk.size();
}
/* Returns whether the expression stack is empty or not */
boolT emptyExpStk()
{
return expStk.empty();
}

View File

@@ -426,9 +426,6 @@ void
CleanupLibCheck(void)
{
/* Deallocate all the stuff allocated in SetupLibCheck() */
if (T1base) free(T1base);
if (T1base) free(T2base);
if (g) free(g);
if (ht) free(ht);
if (pFunc)free(pFunc);
}
@@ -456,7 +453,7 @@ boolT LibCheck(Function & pProc)
{
/* Easy - this function is called main! */
strcpy(pProc.name, "main");
return FALSE;
return false;
}
memmove(pat, &prog.Image[fileOffset], PATLEN);

View File

@@ -7,8 +7,65 @@
#include "dcc.h"
#include <string.h>
#include <iostream>
#include <iomanip>
#include <stdio.h>
struct ExpStack
{
typedef std::list<COND_EXPR *> EXP_STK;
EXP_STK expStk; /* local expression stack */
void init();
void push(COND_EXPR *);
COND_EXPR *pop();
Int numElem();
boolT empty();
};
/***************************************************************************
* Expression stack functions
**************************************************************************/
/* Reinitalizes the expression stack (expStk) to NULL, by freeing all the
* space allocated (if any). */
void ExpStack::init()
{
expStk.clear();
}
/* Pushes the given expression onto the local stack (expStk). */
void ExpStack::push(COND_EXPR *expr)
{
expStk.push_back(expr);
}
/* Returns the element on the top of the local expression stack (expStk),
* and deallocates the space allocated by this node.
* If there are no elements on the stack, returns NULL. */
COND_EXPR *ExpStack::pop()
{
if(expStk.empty())
return 0;
COND_EXPR *topExp = expStk.back();
expStk.pop_back();
return topExp;
}
/* Returns the number of elements available in the expression stack */
Int ExpStack::numElem()
{
return expStk.size();
}
/* Returns whether the expression stack is empty or not */
boolT ExpStack::empty()
{
return expStk.empty();
}
using namespace std;
ExpStack g_exp_stk;
/* Returns the index of the local variable or parameter at offset off, if it
* is in the stack frame provided. */
@@ -114,13 +171,13 @@ void Function::elimCondCodes ()
default:
notSup = TRUE;
std::cout << hex<<defAt->loc_ip;
reportError (JX_NOT_DEF, defAt->GetLlOpcode());
flg |= PROC_ASM; /* generate asm */
}
if (! notSup)
{
exp = COND_EXPR::boolOp (lhs, rhs,
condOpJCond[useAt->GetLlOpcode()-iJB]);
exp = COND_EXPR::boolOp (lhs, rhs,condOpJCond[useAt->GetLlOpcode()-iJB]);
useAt->setJCond(exp);
}
}
@@ -602,7 +659,7 @@ static void processCArg (Function * pp, Function * pProc, ICODE * picode, Int nu
/* if (numArgs == 0)
return; */
exp = popExpStk();
exp = g_exp_stk.pop();
if (pp->flg & PROC_ISLIB) /* library function */
{
if (pp->args.numArgs > 0)
@@ -628,7 +685,6 @@ static void processCArg (Function * pp, Function * pProc, ICODE * picode, Int nu
*k += hlTypeSize (exp, pProc);
}
/* Eliminates extraneous intermediate icode instructions when finding
* expressions. Generates new hlIcodes in the form of expression trees.
* For HLI_CALL hlIcodes, places the arguments in the argument list. */
@@ -647,7 +703,7 @@ void Function::findExps()
ID *retVal; /* function return value */
/* Initialize expression stack */
initExpStk();
g_exp_stk.init();
/* Traverse tree in dfsLast order */
for (i = 0; i < numBBs; i++)
@@ -726,7 +782,7 @@ void Function::findExps()
(ticode->ic.hl.opcode != HLI_RET)))
continue;
exp = popExpStk(); /* pop last exp pushed */
exp = g_exp_stk.pop(); /* pop last exp pushed */
switch (ticode->ic.hl.opcode) {
case HLI_ASSIGN:
forwardSubs (picode->ic.hl.oper.exp, exp,
@@ -866,7 +922,7 @@ void Function::findExps()
(ticode->ic.hl.opcode != HLI_RET)))
continue;
exp = popExpStk(); /* pop last exp pushed */
exp = g_exp_stk.pop(); /* pop last exp pushed */
switch (ticode->ic.hl.opcode) {
case HLI_ASSIGN:
forwardSubsLong (picode->ic.hl.oper.exp->expr.ident.idNode.longIdx,
@@ -945,7 +1001,7 @@ void Function::findExps()
* expression stack */
else if (picode->ic.hl.opcode == HLI_PUSH)
{
pushExpStk (picode->ic.hl.oper.exp);
g_exp_stk.push(picode->ic.hl.oper.exp);
picode->invalidate();
numHlIcodes--;
}
@@ -965,7 +1021,7 @@ void Function::findExps()
cb = pp->cbParam; /* fixed # arguments */
for (k = 0, numArgs = 0; k < cb; numArgs++)
{
exp = popExpStk();
exp = g_exp_stk.pop();
if (pp->flg & PROC_ISLIB) /* library function */
{
if (pp->args.numArgs > 0)
@@ -990,7 +1046,7 @@ void Function::findExps()
for (k = 0; k < cb; numArgs++)
processCArg (pp, this, &(*picode), numArgs, &k);
else if ((cb == 0) && (picode->ic.ll.flg & REST_STK))
while (! emptyExpStk())
while (! g_exp_stk.empty())
{
processCArg (pp, this, &(*picode), numArgs, &k);
numArgs++;

View File

@@ -37,6 +37,10 @@ void ICODE::setAsgn(COND_EXPR *lhs, COND_EXPR *rhs)
ic.hl.oper.asgn.lhs = lhs;
ic.hl.oper.asgn.rhs = rhs;
}
void ICODE::checkHlCall()
{
//assert((ic.ll.immed.proc.cb != 0)||ic.ll.immed.proc.proc!=0);
}
/* Places the new HLI_CALL high-level operand in the high-level icode array */
void ICODE::newCallHl()
{
@@ -44,10 +48,16 @@ void ICODE::newCallHl()
ic.hl.opcode = HLI_CALL;
ic.hl.oper.call.proc = ic.ll.immed.proc.proc;
ic.hl.oper.call.args = new STKFRAME;
if (ic.ll.immed.proc.cb != 0)
ic.hl.oper.call.args->cb = ic.ll.immed.proc.cb;
else
else if(ic.hl.oper.call.proc)
ic.hl.oper.call.args->cb =ic.hl.oper.call.proc->cbParam;
else
{
printf("Function with no cb set, and no valid oper.call.proc , probaby indirect call\n");
ic.hl.oper.call.args->cb = 0;
}
}
@@ -148,16 +158,20 @@ void Function::highLevelGen()
}
switch (pIcode->ic.ll.opcode) {
case iADD: rhs = COND_EXPR::boolOp (lhs, rhs, ADD);
case iADD:
rhs = COND_EXPR::boolOp (lhs, rhs, ADD);
pIcode->setAsgn(lhs, rhs);
break;
case iAND: rhs = COND_EXPR::boolOp (lhs, rhs, AND);
case iAND:
rhs = COND_EXPR::boolOp (lhs, rhs, AND);
pIcode->setAsgn(lhs, rhs);
break;
case iCALL:
case iCALLF: pIcode->newCallHl();
case iCALLF:
pIcode->checkHlCall();
pIcode->newCallHl();
break;
case iDEC:
@@ -259,7 +273,8 @@ void Function::highLevelGen()
case iXCHG:
break;
case iXOR: rhs = COND_EXPR::boolOp (lhs, rhs, XOR);
case iXOR:
rhs = COND_EXPR::boolOp (lhs, rhs, XOR);
pIcode->setAsgn(lhs, rhs);
break;
}

View File

@@ -64,6 +64,7 @@ void parse (CALL_GRAPH * *pcallGraph)
else
{
/* Create initial procedure at program start address */
strcpy(pProcList.front().name, "start");
pProcList.front().procEntry = (dword)state.IP;
}
/* The state info is for the first procedure */
@@ -109,7 +110,7 @@ Int strSize (byte *sym, char delim)
for (i = 0; *sym++ != delim; i++) ;
return (i+1);
}
Function *fakeproc=Function::Create(0,0,"fake");
/* FollowCtrl - Given an initial procedure, state information and symbol table
* builds a list of procedures reachable from the initial procedure
@@ -513,6 +514,7 @@ boolT Function::process_JMP (ICODE * pIcode, STATE *pstate, CALL_GRAPH * pcallGr
* be assumed that if an assembler program contains a CALL that the
* programmer expected it to come back - otherwise surely a JMP would
* have been used. */
boolT Function::process_CALL (ICODE * pIcode, CALL_GRAPH * pcallGraph, STATE *pstate)
{
Int ip = Icode.GetNumIcodes() - 1;
@@ -522,16 +524,17 @@ boolT Function::process_CALL (ICODE * pIcode, CALL_GRAPH * pcallGraph, STATE *ps
/* For Indirect Calls, find the function address */
indirect = FALSE;
if (! (pIcode->ic.ll.flg & I))
//pIcode->ic.ll.immed.proc.proc=fakeproc;
if ( not pIcode->isLlFlag(I) )
{
/* Not immediate, i.e. indirect call */
if (pIcode->ic.ll.dst.regi && (!option.Calls))
{
/* We have not set the brave option to attempt to follow
the execution path through register indirect calls.
So we just exit this function, and ignore the call.
We probably should not have parsed this deep, anyway.
the execution path through register indirect calls.
So we just exit this function, and ignore the call.
We probably should not have parsed this deep, anyway.
*/
return FALSE;
}
@@ -616,7 +619,8 @@ boolT Function::process_CALL (ICODE * pIcode, CALL_GRAPH * pcallGraph, STATE *ps
else
pcallGraph->insertCallGraph (this, iter);
Icode.GetIcode(ip)->ic.ll.immed.proc.proc = &(*iter); // ^ target proc
Icode[ip].ic.ll.immed.proc.proc = &(*iter); // ^ target proc
/* return ((p->flg & TERMINATES) != 0); */
return FALSE;
}
@@ -905,8 +909,7 @@ dword duReg[] = { 0x00,
* pstate: ptr to current procedure state
* size : size of the operand
* ix : current index into icode array */
static void use (opLoc d, ICODE * pIcode, Function * pProc, STATE * pstate, Int size,
Int ix)
static void use (opLoc d, ICODE * pIcode, Function * pProc, STATE * pstate, Int size, Int ix)
{
ICODEMEM * pm = (d == SRC)? &pIcode->ic.ll.src: &pIcode->ic.ll.dst;
SYM * psym;

View File

@@ -380,109 +380,110 @@ void Function::propLongReg (Int i, ID *pLocId)
}
/* If no definition backwards, check forward for a use of this long reg */
if (idx <= 0)
for (idx = pLocId->idx[j] + 1; idx < this->Icode.GetNumIcodes() - 1; idx++)
{
pIcode = Icode.begin()+(idx);
if ((pIcode->type == HIGH_LEVEL) || (pIcode->invalid == TRUE))
continue;
if (idx > 0)
continue;
for (idx = pLocId->idx[j] + 1; idx < Icode.size() - 1; idx++)
{
pIcode = Icode.begin()+(idx);
if ((pIcode->type == HIGH_LEVEL) || (pIcode->invalid == TRUE))
continue;
if (pIcode->ic.ll.opcode == (pIcode+1)->ic.ll.opcode)
switch (pIcode->ic.ll.opcode) {
case iMOV:
if ((pLocId->id.longId.h == pIcode->ic.ll.src.regi) &&
(pLocId->id.longId.l == (pIcode+1)->ic.ll.src.regi))
{
rhs = COND_EXPR::idLongIdx (i);
pIcode->setRegDU( (pIcode+1)->ic.ll.src.regi, eUSE);
lhs = COND_EXPR::idLong (&this->localId, DST, pIcode,
if (pIcode->ic.ll.opcode == (pIcode+1)->ic.ll.opcode)
switch (pIcode->ic.ll.opcode) {
case iMOV:
if ((pLocId->id.longId.h == pIcode->ic.ll.src.regi) &&
(pLocId->id.longId.l == (pIcode+1)->ic.ll.src.regi))
{
rhs = COND_EXPR::idLongIdx (i);
pIcode->setRegDU( (pIcode+1)->ic.ll.src.regi, eUSE);
lhs = COND_EXPR::idLong (&this->localId, DST, pIcode,
HIGH_FIRST, idx, eDEF, 1);
pIcode->setAsgn(lhs, rhs);
(pIcode+1)->invalidate();
idx = this->Icode.GetNumIcodes(); /* to exit the loop */
}
break;
pIcode->setAsgn(lhs, rhs);
(pIcode+1)->invalidate();
idx = this->Icode.GetNumIcodes(); /* to exit the loop */
}
break;
case iPUSH:
if ((pLocId->id.longId.h == pIcode->ic.ll.src.regi) &&
(pLocId->id.longId.l == (pIcode+1)->ic.ll.src.regi))
{
rhs = COND_EXPR::idLongIdx (i);
pIcode->setRegDU( (pIcode+1)->ic.ll.src.regi, eUSE);
pIcode->setUnary(HLI_PUSH, lhs);
(pIcode+1)->invalidate();
}
idx = this->Icode.GetNumIcodes(); /* to exit the loop */
break;
case iPUSH:
if ((pLocId->id.longId.h == pIcode->ic.ll.src.regi) &&
(pLocId->id.longId.l == (pIcode+1)->ic.ll.src.regi))
{
rhs = COND_EXPR::idLongIdx (i);
pIcode->setRegDU( (pIcode+1)->ic.ll.src.regi, eUSE);
pIcode->setUnary(HLI_PUSH, lhs);
(pIcode+1)->invalidate();
}
idx = this->Icode.GetNumIcodes(); /* to exit the loop */
break;
/*** others missing ****/
/*** others missing ****/
case iAND: case iOR: case iXOR:
pmL = &pIcode->ic.ll.dst;
pmH = &(pIcode+1)->ic.ll.dst;
if ((pLocId->id.longId.h == pmH->regi) &&
(pLocId->id.longId.l == pmL->regi))
{
lhs = COND_EXPR::idLongIdx (i);
pIcode->setRegDU( pmH->regi, USE_DEF);
rhs = COND_EXPR::idLong (&this->localId, SRC, pIcode,
case iAND: case iOR: case iXOR:
pmL = &pIcode->ic.ll.dst;
pmH = &(pIcode+1)->ic.ll.dst;
if ((pLocId->id.longId.h == pmH->regi) &&
(pLocId->id.longId.l == pmL->regi))
{
lhs = COND_EXPR::idLongIdx (i);
pIcode->setRegDU( pmH->regi, USE_DEF);
rhs = COND_EXPR::idLong (&this->localId, SRC, pIcode,
LOW_FIRST, idx, eUSE, 1);
switch (pIcode->ic.ll.opcode) {
case iAND: rhs = COND_EXPR::boolOp (lhs, rhs, AND);
break;
case iOR: rhs = COND_EXPR::boolOp (lhs, rhs, OR);
break;
case iXOR: rhs = COND_EXPR::boolOp (lhs, rhs, XOR);
break;
}
pIcode->setAsgn(lhs, rhs);
(pIcode+1)->invalidate();
idx = 0;
switch (pIcode->ic.ll.opcode) {
case iAND: rhs = COND_EXPR::boolOp (lhs, rhs, AND);
break;
case iOR: rhs = COND_EXPR::boolOp (lhs, rhs, OR);
break;
case iXOR: rhs = COND_EXPR::boolOp (lhs, rhs, XOR);
break;
}
break;
} /* eos */
pIcode->setAsgn(lhs, rhs);
(pIcode+1)->invalidate();
idx = 0;
}
break;
} /* eos */
/* Check long conditional (i.e. 2 CMPs and 3 branches */
else if ((pIcode->ic.ll.opcode == iCMP) &&
(isLong23 (idx, pIcode->inBB, &off, &arc)))
{
if (checkLongRegEq (pLocId->id.longId, pIcode, i, idx, this,
&rhs, &lhs, off) == TRUE)
longJCond23 (rhs, lhs, pIcode, &idx, this, arc, off);
}
/* Check long conditional (i.e. 2 CMPs and 3 branches */
else if ((pIcode->ic.ll.opcode == iCMP) &&
(isLong23 (idx, pIcode->inBB, &off, &arc)))
{
if (checkLongRegEq (pLocId->id.longId, pIcode, i, idx, this,
&rhs, &lhs, off) == TRUE)
longJCond23 (rhs, lhs, pIcode, &idx, this, arc, off);
}
/* Check for long conditional equality or inequality. This requires
/* Check for long conditional equality or inequality. This requires
* 2 CMPs and 2 branches */
else if ((pIcode->ic.ll.opcode == iCMP) &&
(isLong22 (pIcode, pEnd, &off)))
{
if (checkLongRegEq (pLocId->id.longId, pIcode, i, idx, this,
&rhs, &lhs, off) == TRUE)
longJCond22 (rhs, lhs, pIcode, &idx);
}
else if ((pIcode->ic.ll.opcode == iCMP) &&
(isLong22 (pIcode, pEnd, &off)))
{
if (checkLongRegEq (pLocId->id.longId, pIcode, i, idx, this,
&rhs, &lhs, off) == TRUE)
longJCond22 (rhs, lhs, pIcode, &idx);
}
/* Check for OR regH, regL
/* Check for OR regH, regL
* JX lab
* => HLI_JCOND (regH:regL X 0) lab
* This is better code than HLI_JCOND (HI(regH:regL) | LO(regH:regL)) */
else if ((pIcode->ic.ll.opcode == iOR) && ((pIcode+1) < pEnd) &&
(isJCond ((pIcode+1)->ic.ll.opcode)))
else if ((pIcode->ic.ll.opcode == iOR) && ((pIcode+1) < pEnd) &&
(isJCond ((pIcode+1)->ic.ll.opcode)))
{
if ((pIcode->ic.ll.dst.regi == pLocId->id.longId.h) &&
(pIcode->ic.ll.src.regi == pLocId->id.longId.l))
{
if ((pIcode->ic.ll.dst.regi == pLocId->id.longId.h) &&
(pIcode->ic.ll.src.regi == pLocId->id.longId.l))
{
lhs = COND_EXPR::idLongIdx (i);
lhs = COND_EXPR::idLongIdx (i);
rhs = COND_EXPR::idKte (0, 4); /* long 0 */
lhs = COND_EXPR::boolOp (lhs, rhs,
condOpJCond[(pIcode+1)->ic.ll.opcode - iJB]);
(pIcode+1)->setJCond(lhs);
(pIcode+1)->copyDU(*pIcode, eUSE, eUSE);
pIcode->invalidate();
}
rhs = COND_EXPR::idKte (0, 4); /* long 0 */
lhs = COND_EXPR::boolOp (lhs, rhs,
condOpJCond[(pIcode+1)->ic.ll.opcode - iJB]);
(pIcode+1)->setJCond(lhs);
(pIcode+1)->copyDU(*pIcode, eUSE, eUSE);
pIcode->invalidate();
}
}
} /* end for */
} /* end for */
} /* end for */
}