Procedure.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. #pragma once
  2. #include <llvm/ADT/ilist.h>
  3. //#include <llvm/ADT/ilist_node.h>
  4. #include <bitset>
  5. #include <map>
  6. #include "BasicBlock.h"
  7. #include "locident.h"
  8. #include "state.h"
  9. #include "icode.h"
  10. #include "StackFrame.h"
  11. #include "CallConvention.h"
  12. /* PROCEDURE NODE */
  13. struct CALL_GRAPH;
  14. struct Expr;
  15. struct Disassembler;
  16. struct Function;
  17. struct CALL_GRAPH;
  18. struct PROG;
  19. struct Function;
  20. namespace llvm
  21. {
  22. // Traits for intrusive list of basic blocks...
  23. template<>
  24. struct ilist_traits<BB> : public ilist_default_traits<BB>
  25. {
  26. // createSentinel is used to get hold of the node that marks the end of the
  27. // list... (same trick used here as in ilist_traits<Instruction>)
  28. BB *createSentinel() const {
  29. return static_cast<BB*>(&Sentinel);
  30. }
  31. static void destroySentinel(BB*) {}
  32. BB *provideInitialHead() const { return createSentinel(); }
  33. BB *ensureHead(BB*) const { return createSentinel(); }
  34. static void noteHead(BB*, BB*) {}
  35. //static ValueSymbolTable *getSymTab(Function *ItemParent);
  36. private:
  37. mutable ilist_half_node<BB> Sentinel;
  38. };
  39. }
  40. /* Procedure FLAGS */
  41. enum PROC_FLAGS
  42. {
  43. PROC_BADINST=0x00000100,/* Proc contains invalid or 386 instruction */
  44. PROC_IJMP =0x00000200,/* Proc incomplete due to indirect jmp */
  45. PROC_ICALL =0x00000400, /* Proc incomplete due to indirect call */
  46. PROC_HLL =0x00001000, /* Proc is likely to be from a HLL */
  47. // CALL_PASCAL =0x00002000, /* Proc uses Pascal calling convention */
  48. // CALL_C =0x00004000, /* Proc uses C calling convention */
  49. // CALL_UNKNOWN=0x00008000, /* Proc uses unknown calling convention */
  50. PROC_NEAR =0x00010000, /* Proc exits with near return */
  51. PROC_FAR =0x00020000, /* Proc exits with far return */
  52. GRAPH_IRRED =0x00100000, /* Proc generates an irreducible graph */
  53. SI_REGVAR =0x00200000, /* SI is used as a stack variable */
  54. DI_REGVAR =0x00400000, /* DI is used as a stack variable */
  55. PROC_IS_FUNC=0x00800000, /* Proc is a function */
  56. REG_ARGS =0x01000000, /* Proc has registers as arguments */
  57. // PROC_VARARG =0x02000000, /* Proc has variable arguments */
  58. PROC_OUTPUT =0x04000000, /* C for this proc has been output */
  59. PROC_RUNTIME=0x08000000, /* Proc is part of the runtime support */
  60. PROC_ISLIB =0x10000000, /* Proc is a library function */
  61. PROC_ASM =0x20000000, /* Proc is an intrinsic assembler routine */
  62. PROC_IS_HLL =0x40000000 /* Proc has HLL prolog code */
  63. //#define CALL_MASK 0xFFFF9FFF /* Masks off CALL_C and CALL_PASCAL */
  64. };
  65. struct FunctionType
  66. {
  67. bool m_vararg=false;
  68. bool isVarArg() const {return m_vararg;}
  69. };
  70. struct Assignment
  71. {
  72. Expr *lhs;
  73. Expr *rhs;
  74. };
  75. struct JumpTable
  76. {
  77. uint32_t start;
  78. uint32_t finish;
  79. bool valid() {return start<finish;}
  80. size_t size() { return (finish-start)/2;}
  81. size_t entrySize() { return 2;}
  82. void pruneEntries(uint16_t cs);
  83. };
  84. class FunctionCfg
  85. {
  86. std::list<BB*> m_listBB; /* Ptr. to BB list/CFG */
  87. public:
  88. typedef std::list<BB*>::iterator iterator;
  89. iterator begin() {
  90. return m_listBB.begin();
  91. }
  92. iterator end() {
  93. return m_listBB.end();
  94. }
  95. BB * &front() { return m_listBB.front();}
  96. void nodeSplitting()
  97. {
  98. /* Converts the irreducible graph G into an equivalent reducible one, by
  99. * means of node splitting. */
  100. fprintf(stderr,"Attempt to perform node splitting: NOT IMPLEMENTED\n");
  101. }
  102. void push_back(BB *v) { m_listBB.push_back(v);}
  103. };
  104. struct Function : public llvm::ilist_node<Function>
  105. {
  106. typedef llvm::iplist<BB> BasicBlockListType;
  107. // BasicBlock iterators...
  108. typedef BasicBlockListType::iterator iterator;
  109. typedef BasicBlockListType::const_iterator const_iterator;
  110. protected:
  111. BasicBlockListType BasicBlocks; ///< The basic blocks
  112. Function(FunctionType */*ty*/) : procEntry(0),depth(0),flg(0),cbParam(0),m_dfsLast(0),numBBs(0),
  113. hasCase(false),liveAnal(0)
  114. {
  115. type = new FunctionType;
  116. callingConv(CConv::UNKNOWN);
  117. }
  118. public:
  119. FunctionType * type;
  120. CConv * m_call_conv;
  121. uint32_t procEntry; /* label number */
  122. std::string name; /* Meaningful name for this proc */
  123. STATE state; /* Entry state */
  124. int depth; /* Depth at which we found it - for printing */
  125. uint32_t flg; /* Combination of Icode & Proc flags */
  126. int16_t cbParam; /* Probable no. of bytes of parameters */
  127. STKFRAME args; /* Array of arguments */
  128. LOCAL_ID localId; /* Local identifiers */
  129. ID retVal; /* Return value - identifier */
  130. /* Icodes and control flow graph */
  131. CIcodeRec Icode; /* Object with ICODE records */
  132. FunctionCfg m_actual_cfg;
  133. std::vector<BB*> m_dfsLast;
  134. std::map<int,BB*> m_ip_to_bb;
  135. // * (reverse postorder) order */
  136. size_t numBBs; /* Number of BBs in the graph cfg */
  137. bool hasCase; /* Procedure has a case node */
  138. /* For interprocedural live analysis */
  139. LivenessSet liveIn; /* Registers used before defined */
  140. LivenessSet liveOut; /* Registers that may be used in successors */
  141. bool liveAnal; /* Procedure has been analysed already */
  142. virtual ~Function() {
  143. delete type;
  144. }
  145. public:
  146. static Function *Create(FunctionType *ty=0,int /*Linkage*/=0,const std::string &nm="",void */*module*/=0)
  147. {
  148. Function *r=new Function(ty);
  149. r->name = nm;
  150. return r;
  151. }
  152. FunctionType *getFunctionType() const {
  153. return type;
  154. }
  155. CConv *callingConv() const { return m_call_conv;}
  156. void callingConv(CConv::Type v);
  157. // bool anyFlagsSet(uint32_t t) { return (flg&t)!=0;}
  158. bool hasRegArgs() const { return (flg & REG_ARGS)!=0;}
  159. bool isLibrary() const { return (flg & PROC_ISLIB)!=0;}
  160. void compoundCond();
  161. void writeProcComments();
  162. void lowLevelAnalysis();
  163. void bindIcodeOff();
  164. void dataFlow(LivenessSet &liveOut);
  165. void compressCFG();
  166. void highLevelGen();
  167. void structure(derSeq *derivedG);
  168. derSeq *checkReducibility();
  169. void createCFG();
  170. void markImpure();
  171. void findImmedDom();
  172. void FollowCtrl(CALL_GRAPH *pcallGraph, STATE *pstate);
  173. void process_operands(ICODE &pIcode, STATE *pstate);
  174. bool process_JMP(ICODE &pIcode, STATE *pstate, CALL_GRAPH *pcallGraph);
  175. bool process_CALL(ICODE &pIcode, CALL_GRAPH *pcallGraph, STATE *pstate);
  176. void freeCFG();
  177. void codeGen(std::ostream &fs);
  178. void mergeFallThrough(BB *pBB);
  179. void structIfs();
  180. void structLoops(derSeq *derivedG);
  181. void buildCFG(Disassembler &ds);
  182. void controlFlowAnalysis();
  183. void newRegArg(iICODE picode, iICODE ticode);
  184. void writeProcComments(std::ostream &ostr);
  185. void displayCFG();
  186. void displayStats();
  187. void processHliCall(Expr *exp, iICODE picode);
  188. void preprocessReturnDU(LivenessSet &_liveOut);
  189. Expr * adjustActArgType(Expr *_exp, hlType forType);
  190. std::string writeCall(Function *tproc, STKFRAME &args, int *numLoc);
  191. void processDosInt(STATE *pstate, PROG &prog, bool done);
  192. ICODE *translate_DIV(LLInst *ll, ICODE &_Icode);
  193. ICODE *translate_XCHG(LLInst *ll, ICODE &_Icode);
  194. protected:
  195. void extractJumpTableRange(ICODE& pIcode, STATE *pstate, JumpTable &table);
  196. bool followAllTableEntries(JumpTable &table, uint32_t cs, ICODE &pIcode, CALL_GRAPH *pcallGraph, STATE *pstate);
  197. bool removeInEdge_Flag_and_ProcessLatch(BB *pbb, BB *a, BB *b);
  198. bool Case_X_and_Y(BB* pbb, BB* thenBB, BB* elseBB);
  199. bool Case_X_or_Y(BB* pbb, BB* thenBB, BB* elseBB);
  200. bool Case_notX_or_Y(BB* pbb, BB* thenBB, BB* elseBB);
  201. bool Case_notX_and_Y(BB* pbb, BB* thenBB, BB* elseBB);
  202. void replaceInEdge(BB* where, BB* which, BB* with);
  203. void processExpPush(int &numHlIcodes, iICODE picode);
  204. // TODO: replace those with friend visitor ?
  205. void propLongReg(int loc_ident_idx, const ID &pLocId);
  206. void propLongStk(int i, const ID &pLocId);
  207. void propLongGlb(int i, const ID &pLocId);
  208. void processTargetIcode(iICODE picode, int &numHlIcodes, iICODE ticode, bool isLong);
  209. int findBackwarLongDefs(int loc_ident_idx, const ID &pLocId, iICODE iter);
  210. int findForwardLongUses(int loc_ident_idx, const ID &pLocId, iICODE beg);
  211. void structCases();
  212. void findExps();
  213. void genDU1();
  214. void elimCondCodes();
  215. void liveRegAnalysis(LivenessSet &in_liveOut);
  216. void findIdioms();
  217. void propLong();
  218. void genLiveKtes();
  219. bool findDerivedSeq(derSeq &derivedGi);
  220. bool nextOrderGraph(derSeq &derivedGi);
  221. };
  222. namespace llvm {
  223. template<> struct ilist_traits<typename ::Function>
  224. : public ilist_default_traits<typename ::Function> {
  225. // createSentinel is used to get hold of the node that marks the end of the
  226. // list... (same trick used here as in ilist_traits<Instruction>)
  227. typename ::Function *createSentinel() const {
  228. return static_cast<typename ::Function*>(&Sentinel);
  229. }
  230. static void destroySentinel(typename ::Function*) {}
  231. typename ::Function *provideInitialHead() const { return createSentinel(); }
  232. typename ::Function *ensureHead(::Function*) const { return createSentinel(); }
  233. static void noteHead(typename ::Function*, typename ::Function*) {}
  234. private:
  235. mutable ilist_node<typename ::Function> Sentinel;
  236. };
  237. }
  238. typedef llvm::iplist<Function> FunctionListType;
  239. typedef FunctionListType lFunction;
  240. typedef lFunction::iterator ilFunction;