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

@ -83,7 +83,9 @@ public:
public: public:
static Function *Create(void *ty=0,int Linkage=0,const std::string &nm="",void *module=0) static Function *Create(void *ty=0,int Linkage=0,const std::string &nm="",void *module=0)
{ {
return new Function(ty); Function *r=new Function(ty);
strncpy(r->name,nm.c_str(),SYMLEN);
return r;
} }
void compoundCond(); void compoundCond();
void writeProcComments(); void writeProcComments();

View File

@ -194,13 +194,7 @@ Int hlTypeSize (const COND_EXPR *, Function *);
hlType expType (const COND_EXPR *, Function *); hlType expType (const COND_EXPR *, Function *);
boolT insertSubTreeReg (COND_EXPR *, COND_EXPR **, byte, LOCAL_ID *); boolT insertSubTreeReg (COND_EXPR *, COND_EXPR **, byte, LOCAL_ID *);
boolT insertSubTreeLongReg (COND_EXPR *, COND_EXPR **, Int); boolT insertSubTreeLongReg (COND_EXPR *, COND_EXPR **, Int);
//COND_EXPR *concatExps (SEQ_COND_EXPR *, COND_EXPR *, condNodeType);
void initExpStk();
void pushExpStk (COND_EXPR *);
COND_EXPR *popExpStk();
Int numElemExpStk();
boolT emptyExpStk();
/* Exported functions from hlicode.c */ /* Exported functions from hlicode.c */
std::string writeCall (Function *, STKFRAME *, Function *, Int *); std::string writeCall (Function *, STKFRAME *, Function *, Int *);

View File

@ -6,16 +6,8 @@
/* High level icodes opcodes - def in file icode.h */ /* High level icodes opcodes - def in file icode.h */
/*typedef enum { struct HLICODE
HLI_ASSIGN, {
INC,
DEC,
HLI_JCOND,
} hlIcode; */
typedef struct {
hlIcode opcode; /* hlIcode opcode */ hlIcode opcode; /* hlIcode opcode */
union { /* different operands */ union { /* different operands */
struct { struct {
@ -25,12 +17,4 @@ typedef struct {
COND_EXPR *exp; /* for HLI_JCOND, INC, DEC */ COND_EXPR *exp; /* for HLI_JCOND, INC, DEC */
} oper; /* operand */ } oper; /* operand */
boolT valid; /* has a valid hlIcode */ boolT valid; /* has a valid hlIcode */
} HLICODE; };
//typedef struct {
// Int numIcodes; /* No. of hlIcode reocrds written */
// Int numAlloc; /* No. of hlIcode records allocated */
// HLICODE *hlIcode; /* Array of high-level icodes */
//} HLICODEREC;

View File

@ -293,8 +293,8 @@ struct LLTYPE
union { /* Source operand if (flg & I) */ union { /* Source operand if (flg & I) */
dword op; /* idx of immed src op */ dword op; /* idx of immed src op */
struct { /* Call & # actual arg bytes */ struct { /* Call & # actual arg bytes */
Function *proc; /* ^ target proc (for CALL(F))*/ Function *proc; /* pointer to target proc (for CALL(F))*/
Int cb; /* # actual arg bytes */ int cb; /* # actual arg bytes */
} proc; } proc;
} immed; } immed;
DU flagDU; /* def/use of flags */ DU flagDU; /* def/use of flags */
@ -351,6 +351,7 @@ struct DU1
void copyDU(const ICODE &duIcode, operDu _du, operDu duDu); void copyDU(const ICODE &duIcode, operDu _du, operDu duDu);
public: public:
boolT removeDefRegi(byte regi, Int thisDefIdx, LOCAL_ID *locId); boolT removeDefRegi(byte regi, Int thisDefIdx, LOCAL_ID *locId);
void checkHlCall();
}; };
// This is the icode array object. // This is the icode array object.

View File

@ -29,9 +29,6 @@ static const char *condOpSym[] = { " <= ", " < ", " == ", " != ", " > ", " >= ",
// COND_EXPR *exp; // COND_EXPR *exp;
// struct _EXP_STK *next; // struct _EXP_STK *next;
//} EXP_STK; //} EXP_STK;
typedef std::list<COND_EXPR *> EXP_STK;
static EXP_STK expStk; /* local expression stack */
/* Returns the integer i in C hexadecimal format */ /* Returns the integer i in C hexadecimal format */
static char *hexStr (uint16_t i) static char *hexStr (uint16_t i)
@ -950,47 +947,3 @@ void COND_EXPR::release()
} }
delete (this); 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) CleanupLibCheck(void)
{ {
/* Deallocate all the stuff allocated in SetupLibCheck() */ /* Deallocate all the stuff allocated in SetupLibCheck() */
if (T1base) free(T1base);
if (T1base) free(T2base);
if (g) free(g);
if (ht) free(ht); if (ht) free(ht);
if (pFunc)free(pFunc); if (pFunc)free(pFunc);
} }
@ -456,7 +453,7 @@ boolT LibCheck(Function & pProc)
{ {
/* Easy - this function is called main! */ /* Easy - this function is called main! */
strcpy(pProc.name, "main"); strcpy(pProc.name, "main");
return FALSE; return false;
} }
memmove(pat, &prog.Image[fileOffset], PATLEN); memmove(pat, &prog.Image[fileOffset], PATLEN);

View File

@ -7,8 +7,65 @@
#include "dcc.h" #include "dcc.h"
#include <string.h> #include <string.h>
#include <iostream>
#include <iomanip>
#include <stdio.h> #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 /* 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. */
@ -114,13 +171,13 @@ void Function::elimCondCodes ()
default: default:
notSup = TRUE; notSup = TRUE;
std::cout << hex<<defAt->loc_ip;
reportError (JX_NOT_DEF, defAt->GetLlOpcode()); reportError (JX_NOT_DEF, defAt->GetLlOpcode());
flg |= PROC_ASM; /* generate asm */ flg |= PROC_ASM; /* generate asm */
} }
if (! notSup) if (! notSup)
{ {
exp = COND_EXPR::boolOp (lhs, rhs, exp = COND_EXPR::boolOp (lhs, rhs,condOpJCond[useAt->GetLlOpcode()-iJB]);
condOpJCond[useAt->GetLlOpcode()-iJB]);
useAt->setJCond(exp); useAt->setJCond(exp);
} }
} }
@ -602,7 +659,7 @@ static void processCArg (Function * pp, Function * pProc, ICODE * picode, Int nu
/* if (numArgs == 0) /* if (numArgs == 0)
return; */ return; */
exp = popExpStk(); 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)
@ -628,7 +685,6 @@ static void processCArg (Function * pp, Function * pProc, ICODE * picode, Int nu
*k += hlTypeSize (exp, pProc); *k += hlTypeSize (exp, pProc);
} }
/* Eliminates extraneous intermediate icode instructions when finding /* Eliminates extraneous intermediate icode instructions when finding
* expressions. Generates new hlIcodes in the form of expression trees. * expressions. Generates new hlIcodes in the form of expression trees.
* For HLI_CALL hlIcodes, places the arguments in the argument list. */ * For HLI_CALL hlIcodes, places the arguments in the argument list. */
@ -647,7 +703,7 @@ void Function::findExps()
ID *retVal; /* function return value */ ID *retVal; /* function return value */
/* Initialize expression stack */ /* Initialize expression stack */
initExpStk(); g_exp_stk.init();
/* Traverse tree in dfsLast order */ /* Traverse tree in dfsLast order */
for (i = 0; i < numBBs; i++) for (i = 0; i < numBBs; i++)
@ -726,7 +782,7 @@ void Function::findExps()
(ticode->ic.hl.opcode != HLI_RET))) (ticode->ic.hl.opcode != HLI_RET)))
continue; continue;
exp = popExpStk(); /* pop last exp pushed */ exp = g_exp_stk.pop(); /* pop last exp pushed */
switch (ticode->ic.hl.opcode) { switch (ticode->ic.hl.opcode) {
case HLI_ASSIGN: case HLI_ASSIGN:
forwardSubs (picode->ic.hl.oper.exp, exp, forwardSubs (picode->ic.hl.oper.exp, exp,
@ -866,7 +922,7 @@ void Function::findExps()
(ticode->ic.hl.opcode != HLI_RET))) (ticode->ic.hl.opcode != HLI_RET)))
continue; continue;
exp = popExpStk(); /* pop last exp pushed */ exp = g_exp_stk.pop(); /* pop last exp pushed */
switch (ticode->ic.hl.opcode) { switch (ticode->ic.hl.opcode) {
case HLI_ASSIGN: case HLI_ASSIGN:
forwardSubsLong (picode->ic.hl.oper.exp->expr.ident.idNode.longIdx, forwardSubsLong (picode->ic.hl.oper.exp->expr.ident.idNode.longIdx,
@ -945,7 +1001,7 @@ void Function::findExps()
* expression stack */ * expression stack */
else if (picode->ic.hl.opcode == HLI_PUSH) 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(); picode->invalidate();
numHlIcodes--; numHlIcodes--;
} }
@ -965,7 +1021,7 @@ void Function::findExps()
cb = pp->cbParam; /* fixed # arguments */ cb = pp->cbParam; /* fixed # arguments */
for (k = 0, numArgs = 0; k < cb; numArgs++) for (k = 0, numArgs = 0; k < cb; numArgs++)
{ {
exp = popExpStk(); 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)
@ -990,7 +1046,7 @@ void Function::findExps()
for (k = 0; k < cb; numArgs++) for (k = 0; k < cb; numArgs++)
processCArg (pp, this, &(*picode), numArgs, &k); processCArg (pp, this, &(*picode), numArgs, &k);
else if ((cb == 0) && (picode->ic.ll.flg & REST_STK)) else if ((cb == 0) && (picode->ic.ll.flg & REST_STK))
while (! emptyExpStk()) while (! g_exp_stk.empty())
{ {
processCArg (pp, this, &(*picode), numArgs, &k); processCArg (pp, this, &(*picode), numArgs, &k);
numArgs++; 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.lhs = lhs;
ic.hl.oper.asgn.rhs = rhs; 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 */ /* Places the new HLI_CALL high-level operand in the high-level icode array */
void ICODE::newCallHl() void ICODE::newCallHl()
{ {
@ -44,10 +48,16 @@ void ICODE::newCallHl()
ic.hl.opcode = HLI_CALL; ic.hl.opcode = HLI_CALL;
ic.hl.oper.call.proc = ic.ll.immed.proc.proc; ic.hl.oper.call.proc = ic.ll.immed.proc.proc;
ic.hl.oper.call.args = new STKFRAME; ic.hl.oper.call.args = new STKFRAME;
if (ic.ll.immed.proc.cb != 0) if (ic.ll.immed.proc.cb != 0)
ic.hl.oper.call.args->cb = ic.ll.immed.proc.cb; 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; 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) { 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); pIcode->setAsgn(lhs, rhs);
break; break;
case iAND: rhs = COND_EXPR::boolOp (lhs, rhs, AND); case iAND:
rhs = COND_EXPR::boolOp (lhs, rhs, AND);
pIcode->setAsgn(lhs, rhs); pIcode->setAsgn(lhs, rhs);
break; break;
case iCALL: case iCALL:
case iCALLF: pIcode->newCallHl(); case iCALLF:
pIcode->checkHlCall();
pIcode->newCallHl();
break; break;
case iDEC: case iDEC:
@ -259,7 +273,8 @@ void Function::highLevelGen()
case iXCHG: case iXCHG:
break; break;
case iXOR: rhs = COND_EXPR::boolOp (lhs, rhs, XOR); case iXOR:
rhs = COND_EXPR::boolOp (lhs, rhs, XOR);
pIcode->setAsgn(lhs, rhs); pIcode->setAsgn(lhs, rhs);
break; break;
} }

View File

@ -64,6 +64,7 @@ void parse (CALL_GRAPH * *pcallGraph)
else else
{ {
/* Create initial procedure at program start address */ /* Create initial procedure at program start address */
strcpy(pProcList.front().name, "start");
pProcList.front().procEntry = (dword)state.IP; pProcList.front().procEntry = (dword)state.IP;
} }
/* The state info is for the first procedure */ /* The state info is for the first procedure */
@ -109,7 +110,7 @@ Int strSize (byte *sym, char delim)
for (i = 0; *sym++ != delim; i++) ; for (i = 0; *sym++ != delim; i++) ;
return (i+1); return (i+1);
} }
Function *fakeproc=Function::Create(0,0,"fake");
/* FollowCtrl - Given an initial procedure, state information and symbol table /* FollowCtrl - Given an initial procedure, state information and symbol table
* builds a list of procedures reachable from the initial procedure * 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 * be assumed that if an assembler program contains a CALL that the
* programmer expected it to come back - otherwise surely a JMP would * programmer expected it to come back - otherwise surely a JMP would
* have been used. */ * have been used. */
boolT Function::process_CALL (ICODE * pIcode, CALL_GRAPH * pcallGraph, STATE *pstate) boolT Function::process_CALL (ICODE * pIcode, CALL_GRAPH * pcallGraph, STATE *pstate)
{ {
Int ip = Icode.GetNumIcodes() - 1; Int ip = Icode.GetNumIcodes() - 1;
@ -522,7 +524,8 @@ boolT Function::process_CALL (ICODE * pIcode, CALL_GRAPH * pcallGraph, STATE *ps
/* For Indirect Calls, find the function address */ /* For Indirect Calls, find the function address */
indirect = FALSE; 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 */ /* Not immediate, i.e. indirect call */
@ -616,7 +619,8 @@ boolT Function::process_CALL (ICODE * pIcode, CALL_GRAPH * pcallGraph, STATE *ps
else else
pcallGraph->insertCallGraph (this, iter); 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 ((p->flg & TERMINATES) != 0); */
return FALSE; return FALSE;
} }
@ -905,8 +909,7 @@ dword duReg[] = { 0x00,
* pstate: ptr to current procedure state * pstate: ptr to current procedure state
* size : size of the operand * size : size of the operand
* ix : current index into icode array */ * ix : current index into icode array */
static void use (opLoc d, ICODE * pIcode, Function * pProc, STATE * pstate, Int size, static void use (opLoc d, ICODE * pIcode, Function * pProc, STATE * pstate, Int size, Int ix)
Int ix)
{ {
ICODEMEM * pm = (d == SRC)? &pIcode->ic.ll.src: &pIcode->ic.ll.dst; ICODEMEM * pm = (d == SRC)? &pIcode->ic.ll.src: &pIcode->ic.ll.dst;
SYM * psym; SYM * psym;

View File

@ -380,8 +380,9 @@ void Function::propLongReg (Int i, ID *pLocId)
} }
/* If no definition backwards, check forward for a use of this long reg */ /* If no definition backwards, check forward for a use of this long reg */
if (idx <= 0) if (idx > 0)
for (idx = pLocId->idx[j] + 1; idx < this->Icode.GetNumIcodes() - 1; idx++) continue;
for (idx = pLocId->idx[j] + 1; idx < Icode.size() - 1; idx++)
{ {
pIcode = Icode.begin()+(idx); pIcode = Icode.begin()+(idx);
if ((pIcode->type == HIGH_LEVEL) || (pIcode->invalid == TRUE)) if ((pIcode->type == HIGH_LEVEL) || (pIcode->invalid == TRUE))

BIN
tests/inputs/ZORK2.COM Normal file

Binary file not shown.

View File

@ -1,4 +1,4 @@
PROC NEAR start PROC NEAR
000 000100 55 PUSH bp 000 000100 55 PUSH bp
001 000101 8BEC MOV bp, sp 001 000101 8BEC MOV bp, sp
002 000103 83EC02 SUB sp, 2 002 000103 83EC02 SUB sp, 2
@ -35,5 +35,5 @@
025 000143 B80D00 MOV ax, 0Dh ;Case l6 025 000143 B80D00 MOV ax, 0Dh ;Case l6
026 000146 EB05 JMP L2 026 000146 EB05 JMP L2
ENDP start ENDP

View File

@ -1,4 +1,4 @@
PROC NEAR start PROC NEAR
000 000100 55 PUSH bp 000 000100 55 PUSH bp
001 000101 8BEC MOV bp, sp 001 000101 8BEC MOV bp, sp
002 000103 83EC02 SUB sp, 2 002 000103 83EC02 SUB sp, 2
@ -34,5 +34,5 @@
025 000143 B80D00 MOV ax, 0Dh ;Case l6 025 000143 B80D00 MOV ax, 0Dh ;Case l6
026 000146 EB05 JMP L2 026 000146 EB05 JMP L2
ENDP start ENDP

View File

@ -6,7 +6,7 @@
#include "dcc.h" #include "dcc.h"
void () void start ()
/* Takes no parameters. /* Takes no parameters.
* High-level language prologue code. * High-level language prologue code.
*/ */