From 26b9ab1e00ad0401ae9c2d5a1adf16ed4087d289 Mon Sep 17 00:00:00 2001 From: Artur K Date: Sat, 17 Mar 2012 22:57:54 +0100 Subject: [PATCH] Added setters/creation methods to LLOperand Moved PROG global into Project instance. Still need to refactor all usages of prog. Split fseek offset calculation in frontend.cpp to allow for easier debugging. Added alreadyDecoded method to CIcodeRec LONGID_TYPE now has a method matching srcDstRegMatch icode's src a dst regs --- include/dcc.h | 2 +- include/icode.h | 36 ++++++++++++++++++++++++++++++++---- src/ast.cpp | 1 + src/backend.cpp | 2 ++ src/chklib.cpp | 4 ++++ src/dcc.cpp | 2 +- src/disassem.cpp | 3 ++- src/frontend.cpp | 11 ++++++++--- src/graph.cpp | 1 + src/icode.cpp | 7 +++++++ src/locident.cpp | 5 +++++ src/parser.cpp | 13 ++++++++++--- src/procs.cpp | 1 + src/project.cpp | 8 +++++++- src/proplong.cpp | 2 +- src/scanner.cpp | 7 ++++++- 16 files changed, 89 insertions(+), 16 deletions(-) diff --git a/include/dcc.h b/include/dcc.h index 8fc2f9e..072f143 100644 --- a/include/dcc.h +++ b/include/dcc.h @@ -63,7 +63,7 @@ extern OPTION option; /* Command line options */ #include "BinaryImage.h" -extern PROG prog; /* Loaded program image parameters */ +//extern PROG prog; /* Loaded program image parameters */ extern std::bitset<32> duReg[30]; /* def/use bits for registers */ //extern uint32_t duReg[30]; /* def/use bits for registers */ diff --git a/include/icode.h b/include/icode.h index 50391b9..46733ef 100644 --- a/include/icode.h +++ b/include/icode.h @@ -186,10 +186,30 @@ struct LLOperand proc.cb=0; } uint32_t op() const {return opz;} - void SetImmediateOp(uint32_t dw) {opz=dw;} + int64_t getImm2() const {return opz;} + void SetImmediateOp(uint32_t dw) + { + opz=dw; + } + eReg getReg2() {return regi;} bool isReg() const; - - + static LLOperand CreateImm2(int64_t Val) + { + LLOperand Op; + //Op.Kind = kImmediate; + //Op.ImmVal = Val; + Op.opz = Val; + return Op; + } + static LLOperand CreateReg2(unsigned Val) + { + LLOperand Op; +// Op.Kind = kRegister; +// Op.RegVal = Reg; + Op.regi = (eReg)Val; + return Op; + } + void addProcInformation(int param_count,uint32_t call_conv); }; struct LLInst : public llvm::MCInst //: public llvm::ilist_node { @@ -280,6 +300,14 @@ public: caseTbl.numEntries=0; setOpcode(0); } + void replaceDst(const LLOperand &with) + { + dst = with; + } + void replaceDst(eReg r) + { + dst = LLOperand::CreateReg2(r); + } ICODE *m_link; }; @@ -438,9 +466,9 @@ public: CIcodeRec(); // Constructor ICODE * addIcode(ICODE *pIcode); - void SetInBB(int start, int end, BB* pnewBB); void SetInBB(rCODE &rang, BB* pnewBB); bool labelSrch(uint32_t target, uint32_t &pIndex); iterator labelSrch(uint32_t target); ICODE * GetIcode(int ip); + bool alreadyDecoded(uint32_t target); }; diff --git a/src/ast.cpp b/src/ast.cpp index 95a4792..3d6662e 100644 --- a/src/ast.cpp +++ b/src/ast.cpp @@ -604,6 +604,7 @@ void HlTypeSupport::performLongRemoval (eReg regi, LOCAL_ID *locId, COND_EXPR *t /* Returns the string located in image, formatted in C format. */ static std::string getString (int offset) { + PROG &prog(Project::get()->prog); ostringstream o; int strLen, i; diff --git a/src/backend.cpp b/src/backend.cpp index 4a680d5..372cb59 100644 --- a/src/backend.cpp +++ b/src/backend.cpp @@ -97,6 +97,7 @@ char *cChar (uint8_t c) static void printGlobVar (std::ostream &ostr,SYM * psym) { int j; + PROG &prog(Project::get()->prog); uint32_t relocOp = prog.fCOM ? psym->label : psym->label + 0x100; switch (psym->size) @@ -162,6 +163,7 @@ void Project::writeGlobSymTable() * fp. */ static void writeHeader (std::ostream &_ios, char *fileName) { + PROG &prog(Project::get()->prog); /* Write header information */ cCode.init(); cCode.appendDecl( "/*\n"); diff --git a/src/chklib.cpp b/src/chklib.cpp index 5c2272d..a4800c4 100644 --- a/src/chklib.cpp +++ b/src/chklib.cpp @@ -14,6 +14,7 @@ #endif #include #include "dcc.h" +#include "project.h" #include "perfhlib.h" #define NIL -1 /* Used like NULL, but 0 is valid */ @@ -297,6 +298,7 @@ static uint8_t pattMsChkstk[] = /* This procedure is called to initialise the library check code */ void SetupLibCheck(void) { + PROG &prog(Project::get()->prog); uint16_t w, len; int i; @@ -436,6 +438,7 @@ void CleanupLibCheck(void) */ bool LibCheck(Function & pProc) { + PROG &prog(Project::get()->prog); long fileOffset; int h, i, j, arg; int Idx; @@ -619,6 +622,7 @@ static boolT locatePattern(uint8_t *source, int iMin, int iMax, uint8_t *pattern void STATE::checkStartup() { + PROG &prog(Project::get()->prog); /* This function checks the startup code for various compilers' way of loading DS. If found, it sets DS. This may not be needed in the future if pushing and popping of registers is implemented. diff --git a/src/dcc.cpp b/src/dcc.cpp index a7a52f0..30f5113 100644 --- a/src/dcc.cpp +++ b/src/dcc.cpp @@ -12,7 +12,7 @@ char *asm1_name, *asm2_name; /* Assembler output filenames */ SYMTAB symtab; /* Global symbol table */ STATS stats; /* cfg statistics */ -PROG prog; /* programs fields */ +//PROG prog; /* programs fields */ OPTION option; /* Command line options */ //Function * pProcList; /* List of procedures, topologically sort */ //Function * pLastProc; /* Pointer to last node in procedure list */ diff --git a/src/disassem.cpp b/src/disassem.cpp index f9c920b..d813879 100644 --- a/src/disassem.cpp +++ b/src/disassem.cpp @@ -13,7 +13,7 @@ #include "dcc.h" #include "symtab.h" #include "disassem.h" - +#include "project.h" // Note: for the time being, there is no interactive disassembler // for unix @@ -205,6 +205,7 @@ void Disassembler::disassem(Function * ppProc) ****************************************************************************/ void Disassembler::dis1Line(LLInst &inst,int loc_ip, int pass) { + PROG &prog(Project::get()->prog); ostringstream oper_stream; ostringstream hex_bytes; ostringstream result_stream; diff --git a/src/frontend.cpp b/src/frontend.cpp index 0c5e4ee..32ca9c8 100644 --- a/src/frontend.cpp +++ b/src/frontend.cpp @@ -112,6 +112,7 @@ bool DccFrontend::FrontEnd () ***************************************************************************/ static void displayLoadInfo(void) { + PROG &prog(Project::get()->prog); int i; printf("File type is %s\n", (prog.fCOM)?"COM":"EXE"); @@ -145,14 +146,14 @@ static void displayLoadInfo(void) ****************************************************************************/ static void fill(int ip, char *bf) { + PROG &prog(Project::get()->prog); static uint8_t type[4] = {'.', 'd', 'c', 'x'}; uint8_t i; for (i = 0; i < 16; i++, ip++) { *bf++ = ' '; - *bf++ = (ip < prog.cbImage)? - type[(prog.map[ip >> 2] >> ((ip & 3) * 2)) & 3]: ' '; + *bf++ = (ip < prog.cbImage)? type[(prog.map[ip >> 2] >> ((ip & 3) * 2)) & 3]: ' '; } *bf = '\0'; } @@ -163,6 +164,8 @@ static void fill(int ip, char *bf) ****************************************************************************/ static void displayMemMap(void) { + PROG &prog(Project::get()->prog); + char c, b1[33], b2[33], b3[33]; uint8_t i; int ip = 0; @@ -199,6 +202,7 @@ static void displayMemMap(void) ****************************************************************************/ void DccFrontend::LoadImage(Project &proj) { + PROG &prog(Project::get()->prog); FILE *fp; int i, cb; uint8_t buf[4]; @@ -269,7 +273,8 @@ void DccFrontend::LoadImage(Project &proj) } } /* Seek to start of image */ - fseek(fp, (int)LH(&header.numParaHeader) * 16, SEEK_SET); + uint32_t start_of_image= LH(&header.numParaHeader) * 16; + fseek(fp, start_of_image, SEEK_SET); } else { /* COM file diff --git a/src/graph.cpp b/src/graph.cpp index 29c61e4..2f9ebd2 100644 --- a/src/graph.cpp +++ b/src/graph.cpp @@ -172,6 +172,7 @@ CondJumps: void Function::markImpure() { + PROG &prog(Project::get()->prog); SYM * psym; for(ICODE &icod : Icode) { diff --git a/src/icode.cpp b/src/icode.cpp index a358c15..e58d667 100644 --- a/src/icode.cpp +++ b/src/icode.cpp @@ -46,6 +46,13 @@ bool CIcodeRec::labelSrch(uint32_t target, uint32_t &pIndex) pIndex=location->loc_ip; return true; } +bool CIcodeRec::alreadyDecoded(uint32_t target) +{ + iICODE location=labelSrch(target); + if(end()==location) + return false; + return true; +} CIcodeRec::iterator CIcodeRec::labelSrch(uint32_t target) { return find_if(begin(),end(),[target](ICODE &l) -> bool {return l.ll()->label==target;}); diff --git a/src/locident.cpp b/src/locident.cpp index 1d26cc0..b81929a 100644 --- a/src/locident.cpp +++ b/src/locident.cpp @@ -8,6 +8,11 @@ #include #include "locident.h" #include "dcc.h" +bool LONGID_TYPE::srcDstRegMatch(iICODE a, iICODE b) const +{ + return (a->ll()->src.getReg2()==l) and (b->ll()->dst.getReg2()==h); +} + ID::ID() : type(TYPE_UNKNOWN),illegal(false),loc(STK_FRAME),hasMacro(false) { diff --git a/src/parser.cpp b/src/parser.cpp index 92fabf7..0c28e11 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -25,6 +25,7 @@ static uint32_t SynthLab; * procedures found */ void DccFrontend::parse(Project &proj) { + PROG &prog(proj.prog); STATE state; /* Set initial state */ @@ -82,6 +83,7 @@ void DccFrontend::parse(Project &proj) * Size includes delimiter. */ int strSize (uint8_t *sym, char delim) { + PROG &prog(Project::get()->prog); int till_end = sym-prog.Image; uint8_t *end_ptr=std::find(sym,sym+(prog.cbImage-(till_end)),delim); return end_ptr-sym+1; @@ -93,12 +95,13 @@ Function *fakeproc=Function::Create(0,0,"fake"); * using a depth first search. */ void Function::FollowCtrl(CALL_GRAPH * pcallGraph, STATE *pstate) { + PROG &prog(Project::get()->prog); ICODE _Icode, *pIcode; /* This gets copied to pProc->Icode[] later */ ICODE eIcode; /* extra icodes for iDIV, iIDIV, iXCHG */ SYM * psym; uint32_t offset; eErrorId err; - boolT done = false; + bool done = false; SYMTAB &global_symbol_table(g_proj.symtab); if (name.find("chkstk") != string::npos) { @@ -376,6 +379,7 @@ void Function::FollowCtrl(CALL_GRAPH * pcallGraph, STATE *pstate) /* process_JMP - Handles JMPs, returns true if we should end recursion */ boolT Function::process_JMP (ICODE & pIcode, STATE *pstate, CALL_GRAPH * pcallGraph) { + PROG &prog(Project::get()->prog); static uint8_t i2r[4] = {rSI, rDI, rBP, rBX}; ICODE _Icode; uint32_t cs, offTable, endTable; @@ -503,6 +507,7 @@ boolT Function::process_JMP (ICODE & pIcode, STATE *pstate, CALL_GRAPH * pcallGr boolT Function::process_CALL (ICODE & pIcode, CALL_GRAPH * pcallGraph, STATE *pstate) { + PROG &prog(Project::get()->prog); ICODE &last_insn(Icode.back()); STATE localState; /* Local copy of the machine state */ uint32_t off; @@ -633,6 +638,7 @@ boolT Function::process_CALL (ICODE & pIcode, CALL_GRAPH * pcallGraph, STATE *ps /* process_MOV - Handles state changes due to simple assignments */ static void process_MOV(LLInst & ll, STATE * pstate) { + PROG &prog(Project::get()->prog); SYM * psym, *psym2; /* Pointer to symbol in global symbol table */ uint8_t dstReg = ll.dst.regi; uint8_t srcReg = ll.src.regi; @@ -742,6 +748,7 @@ void STKFRAME::updateFrameOff ( int16_t off, int _size, uint16_t duFlag) * symbol table, or Null if it's not a direct memory offset. */ static SYM * lookupAddr (LLOperand *pm, STATE *pstate, int size, uint16_t duFlag) { + PROG &prog(Project::get()->prog); int i; SYM * psym=nullptr; uint32_t operand; @@ -816,10 +823,10 @@ void STATE::setState(uint16_t reg, int16_t value) replaces *pIndex with an icode index */ - -static void setBits(int16_t type, uint32_t start, uint32_t len) /* setBits - Sets memory bitmap bits for BM_CODE or BM_DATA (additively) */ +static void setBits(int16_t type, uint32_t start, uint32_t len) { + PROG &prog(Project::get()->prog); uint32_t i; if (start < (uint32_t)prog.cbImage) diff --git a/src/procs.cpp b/src/procs.cpp index f164167..622529e 100644 --- a/src/procs.cpp +++ b/src/procs.cpp @@ -263,6 +263,7 @@ COND_EXPR *CallType::toId() * the actual argument gets modified */ void adjustActArgType (COND_EXPR *exp, hlType forType, Function * pproc) { + PROG &prog(Project::get()->prog); hlType actType; int offset, offL; diff --git a/src/project.cpp b/src/project.cpp index 789bbb9..85e0874 100644 --- a/src/project.cpp +++ b/src/project.cpp @@ -61,7 +61,13 @@ const std::string &Project::symbolName(size_t idx) assert(validSymIdx(idx)); return symtab[idx].name; } -Project *get() +Project *Project::get() { return &g_proj; } + + +SourceMachine *Project::machine() +{ + return nullptr; +} diff --git a/src/proplong.cpp b/src/proplong.cpp index dfc1575..71bac10 100644 --- a/src/proplong.cpp +++ b/src/proplong.cpp @@ -480,7 +480,7 @@ int Function::findForwardLongUses(int loc_ident_idx, const ID &pLocId, iICODE be * This is better code than HLI_JCOND (HI(regH:regL) | LO(regH:regL)) */ else if (pIcode->ll()->match(iOR) && (next1 != pEnd) && (isJCond ((llIcode)next1->ll()->getOpcode()))) { - if ((pIcode->ll()->dst.regi == pLocId.id.longId.h) && (pIcode->ll()->src.regi == pLocId.id.longId.l)) + if (pLocId.id.longId.srcDstRegMatch(pIcode,pIcode)) { asgn.lhs = COND_EXPR::idLongIdx (loc_ident_idx); asgn.rhs = COND_EXPR::idKte (0, 4); /* long 0 */ diff --git a/src/scanner.cpp b/src/scanner.cpp index 94b670d..c85cb6d 100644 --- a/src/scanner.cpp +++ b/src/scanner.cpp @@ -9,6 +9,7 @@ #include "dcc.h" #include "scanner.h" +#include "project.h" /* Parser flags */ #define TO_REG 0x000100 /* rm is source */ #define S_EXT 0x000200 /* sign extend */ @@ -326,6 +327,7 @@ static ICODE * pIcode; /* Ptr to Icode record filled in by scan() */ ****************************************************************************/ eErrorId scan(uint32_t ip, ICODE &p) { + PROG &prog(Project::get()->prog); int op; p = ICODE(); p.type = LOW_LEVEL; @@ -367,8 +369,9 @@ eErrorId scan(uint32_t ip, ICODE &p) /*************************************************************************** relocItem - returns true if uint16_t pointed at is in relocation table **************************************************************************/ -static boolT relocItem(uint8_t *p) +static bool relocItem(uint8_t *p) { + PROG &prog(Project::get()->prog); int i; uint32_t off = p - prog.Image; @@ -736,6 +739,7 @@ static void dispM(int i) ****************************************************************************/ static void dispN(int ) { + PROG &prog(Project::get()->prog); long off = (short)getWord(); /* Signed displacement */ /* Note: the result of the subtraction could be between 32k and 64k, and @@ -751,6 +755,7 @@ static void dispN(int ) ***************************************************************************/ static void dispS(int ) { + PROG &prog(Project::get()->prog); long off = signex(*pInst++); /* Signed displacement */ pIcode->ll()->src.SetImmediateOp((uint32_t)(off + (unsigned)(pInst - prog.Image)));