from work

This commit is contained in:
Artur K
2011-12-12 15:44:52 +01:00
parent 4c249fe5c4
commit 900438c453
53 changed files with 946 additions and 827 deletions

View File

@@ -2,12 +2,16 @@
#include <list>
#include <vector>
#include <string>
#include <llvm/ADT/ilist.h>
#include <llvm/ADT/ilist_node.h>
#include "types.h"
#include "graph.h"
/* Basic block (BB) node definition */
struct Function;
class CIcodeRec;
struct BB;
struct interval;
struct ICODE;
typedef union
{
dword ip; /* Out edge icode address */
@@ -15,13 +19,13 @@ typedef union
interval *intPtr; /* Out edge ptr to next interval*/
} TYPEADR_TYPE;
struct BB
struct BB : public llvm::ilist_node<BB>
{
protected:
private:
BB(const BB&);
BB() : nodeType(0),traversed(0),start(0),length(0),
numHlIcodes(0),flg(0),
numInEdges(0),inEdges(0),
inEdges(0),
numOutEdges(0),edges(0),beenOnH(0),inEdgeCount(0),reachingInt(0),
inInterval(0),correspInt(0),liveUse(0),def(0),liveIn(0),liveOut(0),
dfsFirstNum(0),dfsLastNum(0),immedDom(0),ifFollow(0),loopType(0),latchNode(0),
@@ -29,17 +33,25 @@ protected:
{
}
//friend class SymbolTableListTraits<BB, Function>;
//Int numInEdges; /* Number of in edges */
public:
Int begin();
Int end();
Int rbegin();
Int rend();
ICODE &front();
ICODE &back();
size_t size();
byte nodeType; /* Type of node */
Int traversed; /* Boolean: traversed yet? */
int traversed; /* Boolean: traversed yet? */
Int start; /* First instruction offset */
Int length; /* No. of instructions this BB */
Int numHlIcodes; /* No. of high-level icodes */
flags32 flg; /* BB flags */
/* In edges and out edges */
Int numInEdges; /* Number of in edges */
std::vector<BB *> inEdges; // does not own held pointers
Int numOutEdges; /* Number of out edges */
@@ -81,8 +93,17 @@ public:
Int index; /* Index, used in several ways */
static BB *Create(void *ctx=0,const std::string &s="",Function *parent=0,BB *insertBefore=0);
static BB *Create(Int start, Int ip, byte nodeType, Int numOutEdges, Function * parent);
void writeCode(Int indLevel, Function *pProc, Int *numLoc, Int latchNode, Int ifFollow);
void mergeFallThrough(CIcodeRec &Icode);
void dfsNumbering(std::vector<BB *> &dfsLast, Int *first, Int *last);
void displayDfs();
void writeCode(Int indLevel, Function *pProc, Int *numLoc, Int latchNode, Int ifFollow);
void mergeFallThrough(CIcodeRec &Icode);
void dfsNumbering(std::vector<BB *> &dfsLast, Int *first, Int *last);
void displayDfs();
void display();
/// getParent - Return the enclosing method, or null if none
///
const Function *getParent() const { return Parent; }
Function *getParent() { return Parent; }
void writeBB(ICODE *hli, Int lev, Function *pProc, Int *numLoc);
private:
Function *Parent;
};

View File

@@ -1,4 +1,7 @@
#pragma once
#include <llvm/ADT/ilist.h>
#include <llvm/ADT/ilist_node.h>
#include "BasicBlock.h"
#include "types.h"
#include "ast.h"
#include "icode.h"
@@ -9,8 +12,44 @@
#include "StackFrame.h"
/* PROCEDURE NODE */
struct CALL_GRAPH;
struct Function
namespace llvm
{
// Traits for intrusive list of basic blocks...
template<>
struct ilist_traits<BB> : public ilist_default_traits<BB>
{
// createSentinel is used to get hold of the node that marks the end of the
// list... (same trick used here as in ilist_traits<Instruction>)
BB *createSentinel() const {
return static_cast<BB*>(&Sentinel);
}
static void destroySentinel(BB*) {}
BB *provideInitialHead() const { return createSentinel(); }
BB *ensureHead(BB*) const { return createSentinel(); }
static void noteHead(BB*, BB*) {}
//static ValueSymbolTable *getSymTab(Function *ItemParent);
private:
mutable ilist_half_node<BB> Sentinel;
};
}
struct FunctionType
{
bool m_vararg;
bool isVarArg() const {return m_vararg;}
};
struct Function : public llvm::ilist_node<Function>
{
typedef llvm::iplist<BB> BasicBlockListType;
// BasicBlock iterators...
typedef BasicBlockListType::iterator iterator;
typedef BasicBlockListType::const_iterator const_iterator;
private:
BasicBlockListType BasicBlocks; ///< The basic blocks
public:
dword procEntry; /* label number */
char name[SYMLEN]; /* Meaningful name for this proc */
STATE state; /* Entry state */
@@ -36,15 +75,16 @@ struct Function
dword liveOut; /* Registers that may be used in successors */
boolT liveAnal; /* Procedure has been analysed already */
/* Double-linked list */
// Function *next;
// Function *prev;
public:
Function() : procEntry(0),depth(0),flg(0),cbParam(0),cfg(0),dfsLast(0),numBBs(0),
Function(void *ty=0) : procEntry(0),depth(0),flg(0),cbParam(0),cfg(0),dfsLast(0),numBBs(0),
hasCase(false),liveIn(0),liveOut(0),liveAnal(0)//,next(0),prev(0)
{
memset(name,0,SYMLEN);
}
public:
static Function *Create(void *ty=0,int Linkage=0,const std::string &nm="",void *module=0)
{
return new Function(ty);
}
void compoundCond();
void writeProcComments();
void lowLevelAnalysis();
@@ -66,7 +106,13 @@ public:
void codeGen(std::ostream &fs);
void displayStats();
void mergeFallThrough(BB *pBB);
void structIfs();
void structLoops(derSeq *derivedG);
void buildCFG();
void controlFlowAnalysis();
void newRegArg(ICODE *picode, ICODE *ticode);
protected:
void structCases();
void findExps();
void genDU1();
void elimCondCodes();
@@ -74,4 +120,6 @@ protected:
void findIdioms();
void propLong();
void genLiveKtes();
byte findDerivedSeq (derSeq *derivedGi);
bool nextOrderGraph(derSeq *derivedGi);
};

View File

@@ -3,7 +3,7 @@
* (C) Cristina Cifuentes, Mike van Emmerik
****************************************************************************/
#pragma once
#include <llvm/ADT/ilist.h>
#include "types.h"
#include "ast.h"
#include "icode.h"
@@ -13,8 +13,11 @@
#include "bundle.h"
#include "Procedure.h"
#include "BasicBlock.h"
typedef std::list<Function> lFunction;
typedef std::list<Function>::iterator ilFunction;
typedef llvm::iplist<Function> FunctionListType;
typedef FunctionListType lFunction;
typedef lFunction::iterator ilFunction;
/* SYMBOL TABLE */
struct SYM {
@@ -50,7 +53,8 @@ public:
void insertArc(ilFunction newProc);
};
#define NUM_PROCS_DELTA 5 /* delta # procs a proc invokes */
extern std::list<Function> pProcList;
//extern std::list<Function> pProcList;
extern FunctionListType pProcList;
extern CALL_GRAPH * callGraph; /* Pointer to the head of the call graph */
extern bundle cCode; /* Output C procedure's declaration and code */
@@ -179,7 +183,6 @@ boolT LibCheck(Function &p); /* chklib.c */
/* Exported functions from procs.c */
boolT insertCallGraph (CALL_GRAPH *, ilFunction, ilFunction);
void newRegArg (Function *, ICODE *, ICODE *);
boolT newStkArg (ICODE *, COND_EXPR *, llIcode, Function *);
void allocStkArgs (ICODE *, Int);
void placeStkArg (ICODE *, COND_EXPR *, Int);
@@ -190,7 +193,6 @@ void removeRegFromLong (byte, LOCAL_ID *, COND_EXPR *);
std::string walkCondExpr (const COND_EXPR *exp, Function * pProc, Int *);
Int hlTypeSize (const COND_EXPR *, Function *);
hlType expType (const COND_EXPR *, Function *);
void copyDU (ICODE *, const ICODE *, operDu, operDu);
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);
@@ -202,7 +204,6 @@ Int numElemExpStk();
boolT emptyExpStk();
/* Exported functions from hlicode.c */
boolT removeDefRegi (byte, ICODE *, Int, LOCAL_ID *);
std::string writeCall (Function *, STKFRAME *, Function *, Int *);
char *write1HlIcode (HLTYPE, Function *, Int *);
char *writeJcond (HLTYPE, Function *, Int *);

View File

@@ -5,6 +5,7 @@
#pragma once
#include <list>
#include <vector>
struct Function;
/* Types of basic block nodes */
/* Real basic blocks: type defined according to their out-edges */
enum eBBKind
@@ -88,7 +89,7 @@ struct derSeq_Entry
}
~derSeq_Entry();
public:
void findIntervals();
void findIntervals(Function *c);
};
class derSeq : public std::list<derSeq_Entry>
{

View File

@@ -6,7 +6,7 @@
#include <vector>
#include "Enums.h"
//enum condId;
struct LOCAL_ID;
/* LOW_LEVEL icode flags */
enum eLLFlags
{
@@ -45,16 +45,19 @@ enum eLLFlags
/* Parser flags */
#define TO_REG 0x000100 /* rm is source */
#define S 0x000200 /* sign extend */
#define S_EXT 0x000200 /* sign extend */
#define OP386 0x000400 /* 386 op-code */
#define NSP 0x000800 /* NOT_HLL if SP is src or dst */
#define ICODEMASK 0xFF00FF /* Masks off parser flags */
/* LOW_LEVEL icode, DU flag bits */
#define Cf 1
#define Sf 2
#define Zf 4
#define Df 8
enum eDuFlags
{
Cf=1,
Sf=2,
Zf=4,
Df=8
};
/* Machine registers */
#define rAX 1 /* These are numbered relative to real 8086 */
@@ -227,7 +230,7 @@ typedef enum {
HLI_RET, /* Return from procedure */
/* pseudo high-level icodes */
HLI_POP, /* Pop expression */
HLI_PUSH, /* Push expression */
HLI_PUSH /* Push expression */
} hlIcode;
/* Def/use of flags - low 4 bits represent flags */
@@ -250,13 +253,6 @@ struct DU_ICODE
#define MAX_REGS_DEF 2 /* 2 regs def'd for long-reg vars */
#define MAX_USES 5
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 */
};
/* LOW_LEVEL icode operand record */
struct ICODEMEM
@@ -312,6 +308,12 @@ typedef struct
/* Icode definition: LOW_LEVEL and HIGH_LEVEL */
struct ICODE
{
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 */
@@ -323,6 +325,13 @@ struct ICODE
HLTYPE hl; /* For HIGH_LEVEL icodes */
};
IC ic;/* intermediate code */
int loc_ip; // used by CICodeRec to number ICODEs
void SetLlFlag(dword flag) {ic.ll.flg |= flag;}
dword GetLlFlag() {return ic.ll.flg;}
bool isLlFlag(dword flg) {return (ic.ll.flg&flg)==flg;}
llIcode GetLlOpcode() const { return ic.ll.opcode; }
void writeIntComment(char *s);
void setRegDU(byte regi, operDu du_in);
void invalidate();
@@ -333,7 +342,10 @@ struct ICODE
void setAsgn(COND_EXPR *lhs, COND_EXPR *rhs); // set this icode to be an assign
void setUnary(hlIcode op, COND_EXPR *exp);
void setJCond(COND_EXPR *cexp);
int loc_ip; // used by CICodeRec to number ICODEs
void emitGotoLabel(Int indLevel);
void copyDU(const ICODE &duIcode, operDu _du, operDu duDu);
public:
boolT removeDefRegi(byte regi, Int thisDefIdx, LOCAL_ID *locId);
};
// This is the icode array object.

View File

@@ -8,6 +8,7 @@
#pragma once
#include <vector>
#include <algorithm>
#include "icode.h"
/* Type definition */
struct IDX_ARRAY : public std::vector<int>
{
@@ -111,7 +112,9 @@ struct LOCAL_ID
std::vector<ID> id_arr;
public:
LOCAL_ID()
{}
{
id_arr.reserve(256);
}
Int newByteWordReg(hlType t, byte regi);
Int newByteWordStk(hlType t, Int off, byte regOff);
Int newIntIdx(int16 seg, int16 off, byte regi, Int ix, hlType t);

View File

@@ -47,7 +47,7 @@ typedef unsigned char boolT; /* 8 bits */
/* Macro reads a LH word from the image regardless of host convention */
/* Returns a signed quantity, e.g. C000 is read into an Int as FFFFC000 */
#define LHS(p) (((byte *)(p))[0] + (((char *)(p))[1] << 8))
#define LH_SIGNED(p) (((byte *)(p))[0] + (((char *)(p))[1] << 8))
/* Macro tests bit b for type t in prog.map */
#define BITMAP(b, t) (prog.map[(b) >> 2] & ((t) << (((b) & 3) << 1)))