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:
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 writeProcComments();

View File

@ -194,13 +194,7 @@ Int hlTypeSize (const COND_EXPR *, Function *);
hlType expType (const COND_EXPR *, Function *);
boolT insertSubTreeReg (COND_EXPR *, COND_EXPR **, byte, LOCAL_ID *);
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 */
std::string writeCall (Function *, STKFRAME *, Function *, Int *);

View File

@ -6,16 +6,8 @@
/* High level icodes opcodes - def in file icode.h */
/*typedef enum {
HLI_ASSIGN,
INC,
DEC,
HLI_JCOND,
} hlIcode; */
typedef struct {
struct HLICODE
{
hlIcode opcode; /* hlIcode opcode */
union { /* different operands */
struct {
@ -25,12 +17,4 @@ typedef struct {
COND_EXPR *exp; /* for HLI_JCOND, INC, DEC */
} oper; /* operand */
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) */
dword op; /* idx of immed src op */
struct { /* Call & # actual arg bytes */
Function *proc; /* ^ target proc (for CALL(F))*/
Int cb; /* # actual arg bytes */
Function *proc; /* pointer to target proc (for CALL(F))*/
int cb; /* # actual arg bytes */
} proc;
} immed;
DU flagDU; /* def/use of flags */
@ -312,12 +312,12 @@ struct LLTYPE
/* Icode definition: LOW_LEVEL and HIGH_LEVEL */
struct ICODE
{
struct DU1
{
struct DU1
{
Int numRegsDef; /* # registers defined by this inst */
byte regi[MAX_REGS_DEF]; /* registers defined by this inst */
Int idx[MAX_REGS_DEF][MAX_USES]; /* inst that uses this def */
};
};
icodeType type; /* Icode type */
boolT invalid; /* Has no HIGH_LEVEL equivalent */
BB *inBB; /* BB to which this icode belongs */
@ -351,6 +351,7 @@ struct DU1
void copyDU(const ICODE &duIcode, operDu _du, operDu duDu);
public:
boolT removeDefRegi(byte regi, Int thisDefIdx, LOCAL_ID *locId);
void checkHlCall();
};
// This is the icode array object.

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,7 +524,8 @@ 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 */
@ -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,8 +380,9 @@ 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++)
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))

BIN
tests/inputs/ZORK2.COM Normal file

Binary file not shown.

View File

@ -31,7 +31,7 @@ long LMOD@ (long arg0, int arg2, int arg3)
OR bx, bx
JE L2
L1: TEST di, 1
L1: TEST di, 1
JNE L3
OR dx, dx
JNS L4
@ -40,20 +40,20 @@ L1: TEST di, 1
SBB dx, 0
OR di, 0Ch
L4: OR cx, cx
L4: OR cx, cx
JNS L3
NEG cx
NEG bx
SBB cx, 0
XOR di, 4
L3: MOV bp, cx
L3: MOV bp, cx
MOV cx, 20h
PUSH di
XOR di, 0
XOR si, 0
L5: SHL ax, 1
L5: SHL ax, 1
RCL dx, 1
RCL si, 1
RCL di, 1
@ -63,11 +63,11 @@ L5: SHL ax, 1
CMP si, bx
JB L6
L7: SUB si, bx
L7: SUB si, bx
SBB di, bp
INC ax
L6: LOOP L5
L6: LOOP L5
POP bx
TEST bx, 2
JE L8
@ -75,25 +75,25 @@ L6: LOOP L5
MOV dx, di
SHR bx, 1
L8: TEST bx, 4
L8: TEST bx, 4
JE L9
NEG dx
NEG ax
SBB dx, 0
L9: POP di
L9: POP di
POP si
POP bp
RETF 8
L2: MOV tmp, dx:ax ;Synthetic inst
L2: MOV tmp, dx:ax ;Synthetic inst
DIV bx
MOD bx ;Synthetic inst
TEST di, 2
JE L10
MOV ax, dx
L10: XOR dx, dx
L10: XOR dx, dx
JMP L9
}

View File

@ -1,4 +1,4 @@
PROC NEAR
start PROC NEAR
000 000100 55 PUSH bp
001 000101 8BEC MOV bp, sp
002 000103 83EC02 SUB sp, 2
@ -8,7 +8,7 @@
006 000111 7735 JA L1
007 000113 8BD8 MOV bx, ax
008 000115 D1E3 SHL bx, 1
009 000117 2EFFA71C00 JMP word ptr cs:[bx+1Ch] ;Switch
009 000117 2EFFA71C00 JMP word ptr cs:[bx+1Ch];Switch
010 00012A B80200 MOV ax, 2 ;Case l0
011 00012D EB1E JMP L2
@ -35,5 +35,5 @@
025 000143 B80D00 MOV ax, 0Dh ;Case l6
026 000146 EB05 JMP L2
ENDP
start ENDP

View File

@ -1,4 +1,4 @@
PROC NEAR
start PROC NEAR
000 000100 55 PUSH bp
001 000101 8BEC MOV bp, sp
002 000103 83EC02 SUB sp, 2
@ -8,7 +8,7 @@
006 000111 7735 JA L1
007 000113 8BD8 MOV bx, ax
008 000115 D1E3 SHL bx, 1
009 000117 2EFFA71C00 JMP word ptr cs:[bx+1Ch] ;Switch
009 000117 2EFFA71C00 JMP word ptr cs:[bx+1Ch];Switch
010 00012A B80200 MOV ax, 2 ;Case l0
@ -34,5 +34,5 @@
025 000143 B80D00 MOV ax, 0Dh ;Case l6
026 000146 EB05 JMP L2
ENDP
start ENDP

View File

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