diff --git a/include/icode.h b/include/icode.h index 5cfeb69..50391b9 100644 --- a/include/icode.h +++ b/include/icode.h @@ -13,9 +13,12 @@ #include #include #include +#include #include +#include "libdis.h" #include "Enums.h" #include "state.h" // State depends on INDEXBASE, but later need STATE + //enum condId; struct LOCAL_ID; @@ -163,11 +166,12 @@ public: void setAsgn(COND_EXPR *lhs, COND_EXPR *rhs); } ; /* LOW_LEVEL icode operand record */ -struct LLOperand : public llvm::MCOperand +struct LLOperand { + llvm::MCOperand llvm_op; eReg seg; /* CS, DS, ES, SS */ - int16_t segValue; /* Value of segment seg during analysis */ eReg segOver; /* CS, DS, ES, SS if segment override */ + int16_t segValue; /* Value of segment seg during analysis */ eReg regi; /* 0 < regs < INDEXBASE <= index modes */ int16_t off; /* memory address offset */ uint32_t opz; /* idx of immed src op */ @@ -219,7 +223,6 @@ public: } flg &= ~flag; } - uint32_t getFlag() const {return flg;} //llIcode getOpcode() const { return opcode; } @@ -283,11 +286,15 @@ public: /* Icode definition: LOW_LEVEL and HIGH_LEVEL */ struct ICODE { + // use llvm names at least + typedef BB MachineBasicBlock; protected: LLInst m_ll; HLTYPE m_hl; + MachineBasicBlock * Parent; /* BB to which this icode belongs */ bool invalid; /* Has no HIGH_LEVEL equivalent */ public: + x86_insn_t insn; template struct FlagFilter { @@ -306,6 +313,8 @@ public: bool operator()(ICODE *ic) {return (ic->type==HIGH_LEVEL)&&(ic->valid());} bool operator()(ICODE &ic) {return (ic.type==HIGH_LEVEL)&&ic.valid();} }; + static TypeFilter select_high_level; + static TypeAndValidFilter select_valid_high_level; /* Def/Use of registers and stack variables */ struct DU_ICODE { @@ -366,7 +375,6 @@ public: } }; icodeType type; /* Icode type */ - BB *inBB; /* BB to which this icode belongs */ DU_ICODE du; /* Def/use regs/vars */ DU1 du1; /* du chain 1 */ int loc_ip; // used by CICodeRec to number ICODEs @@ -396,6 +404,7 @@ public: void emitGotoLabel(int indLevel); void copyDU(const ICODE &duIcode, operDu _du, operDu duDu); bool valid() {return not invalid;} + void setParent(MachineBasicBlock *P) { Parent = P; } public: bool removeDefRegi(eReg regi, int thisDefIdx, LOCAL_ID *locId); void checkHlCall(); @@ -403,14 +412,24 @@ public: { return hl()->call.newStkArg(exp,opcode,pproc); } - ICODE() : m_ll(this),type(NOT_SCANNED),inBB(0),loc_ip(0),invalid(false) + ICODE() : m_ll(this),type(NOT_SCANNED),Parent(0),loc_ip(0),invalid(false) { } +public: + const MachineBasicBlock* getParent() const { return Parent; } + MachineBasicBlock* getParent() { return Parent; } + //unsigned getNumOperands() const { return (unsigned)Operands.size(); } + }; +/** Map n low level instructions to m high level instructions +*/ struct MappingLLtoML { - std::list > m_low_level; - std::list > m_middle_level; + typedef llvm::iplist InstListType; + typedef boost::iterator_range rSourceRange; + typedef boost::iterator_range rTargetRange; + rSourceRange m_low_level; + rTargetRange m_middle_level; }; // This is the icode array object. class CIcodeRec : public std::list diff --git a/include/locident.h b/include/locident.h index ae79506..784f728 100644 --- a/include/locident.h +++ b/include/locident.h @@ -20,6 +20,7 @@ // TODO: why ? struct COND_EXPR; struct ICODE; +struct LLInst; typedef std::list::iterator iICODE; struct IDX_ARRAY : public std::vector { @@ -49,11 +50,12 @@ typedef struct int offH; /* high offset from BP */ int offL; /* low offset from BP */ } LONG_STKID_TYPE; -typedef struct +struct LONGID_TYPE { /* For TYPE_LONG_(UN)SIGN registers */ eReg h; /* high register */ eReg l; /* low register */ -} LONGID_TYPE; + bool srcDstRegMatch(iICODE a,iICODE b) const; +}; /* ID, LOCAL_ID */ diff --git a/src/icode.cpp b/src/icode.cpp index a84fe2f..a358c15 100644 --- a/src/icode.cpp +++ b/src/icode.cpp @@ -12,7 +12,8 @@ #define ICODE_DELTA 25 // Amount to allocate for new chunk - +ICODE::TypeFilter ICODE::select_high_level; +ICODE::TypeAndValidFilter ICODE::select_valid_high_level; CIcodeRec::CIcodeRec() { } @@ -27,20 +28,11 @@ ICODE * CIcodeRec::addIcode(ICODE *pIcode) return &back(); } -void CIcodeRec::SetInBB(int start, int _end, BB *pnewBB) -{ - for(ICODE &icode : *this) - { - if((icode.loc_ip>=start) and (icode.loc_ip<=_end)) - icode.inBB = pnewBB; - } -} - void CIcodeRec::SetInBB(rCODE &rang, BB *pnewBB) { for(ICODE &ic : rang) { - ic.inBB = pnewBB; + ic.setParent(pnewBB); } } diff --git a/src/proplong.cpp b/src/proplong.cpp index 65c70e6..dfc1575 100644 --- a/src/proplong.cpp +++ b/src/proplong.cpp @@ -22,7 +22,7 @@ static boolT isJCond (llIcode opcode) /* Returns whether the conditions for a 2-3 long variable are satisfied */ -static bool isLong23 (iICODE iter, BB * pbb, iICODE &off, int *arc) +static bool isLong23 (BB * pbb, iICODE &off, int *arc) { BB * t, * e, * obb2; @@ -90,7 +90,7 @@ static int longJCond23 (COND_EXPR *rhs, COND_EXPR *lhs, iICODE pIcode, int arc, if (arc == THEN) { /* Find intermediate basic blocks and target block */ - pbb = pIcode->inBB; + pbb = pIcode->getParent(); obb1 = pbb->edges[THEN].BBptr; obb2 = obb1->edges[THEN].BBptr; tbb = obb2->edges[THEN].BBptr; @@ -116,7 +116,7 @@ static int longJCond23 (COND_EXPR *rhs, COND_EXPR *lhs, iICODE pIcode, int arc, else /* ELSE arc */ { /* Find intermediate basic blocks and target block */ - pbb = pIcode->inBB; + pbb = pIcode->getParent(); obb1 = pbb->edges[ELSE].BBptr; obb2 = obb1->edges[THEN].BBptr; tbb = obb2->edges[THEN].BBptr; @@ -183,7 +183,7 @@ static int longJCond22 (COND_EXPR *rhs, COND_EXPR *lhs, iICODE pIcode,iICODE pEn icodes[1]->du.use |= icodes[2]->du.use; /* Adjust outEdges[0] to the new target basic block */ - pbb = icodes[0]->inBB; + pbb = icodes[0]->getParent(); if (pbb->back().loc_ip == icodes[1]->loc_ip) { /* Find intermediate and target basic blocks */ @@ -277,9 +277,9 @@ void Function::propLongStk (int i, const ID &pLocId) } /*eos*/ } } - + //TODO: Simplify this! /* Check long conditional (i.e. 2 CMPs and 3 branches */ - else if ((pIcode->ll()->getOpcode() == iCMP) && (isLong23 (pIcode, pIcode->inBB, l23, &arc))) + else if ((pIcode->ll()->getOpcode() == iCMP) && (isLong23 (pIcode->getParent(), l23, &arc))) { if ( checkLongEq (pLocId.id.longStkId, pIcode, i, this, asgn, l23) ) { @@ -455,7 +455,7 @@ int Function::findForwardLongUses(int loc_ident_idx, const ID &pLocId, iICODE be } /* eos */ /* Check long conditional (i.e. 2 CMPs and 3 branches */ - else if ((pIcode->ll()->getOpcode() == iCMP) && (isLong23 (pIcode, pIcode->inBB, long_loc, &arc))) + else if ((pIcode->ll()->getOpcode() == iCMP) && (isLong23 (pIcode->getParent(), long_loc, &arc))) { if (checkLongRegEq (pLocId.id.longId, pIcode, loc_ident_idx, this, asgn, long_loc)) {