Procedure.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #pragma once
  2. #include <llvm/ADT/ilist.h>
  3. #include <llvm/ADT/ilist_node.h>
  4. #include <bitset>
  5. #include "BasicBlock.h"
  6. #include "locident.h"
  7. #include "state.h"
  8. #include "icode.h"
  9. #include "StackFrame.h"
  10. /* PROCEDURE NODE */
  11. struct CALL_GRAPH;
  12. struct COND_EXPR;
  13. struct Disassembler;
  14. struct Function;
  15. struct CALL_GRAPH;
  16. typedef llvm::iplist<Function> FunctionListType;
  17. typedef FunctionListType lFunction;
  18. typedef lFunction::iterator ilFunction;
  19. namespace llvm
  20. {
  21. // Traits for intrusive list of basic blocks...
  22. template<>
  23. struct ilist_traits<BB> : public ilist_default_traits<BB>
  24. {
  25. // createSentinel is used to get hold of the node that marks the end of the
  26. // list... (same trick used here as in ilist_traits<Instruction>)
  27. BB *createSentinel() const {
  28. return static_cast<BB*>(&Sentinel);
  29. }
  30. static void destroySentinel(BB*) {}
  31. BB *provideInitialHead() const { return createSentinel(); }
  32. BB *ensureHead(BB*) const { return createSentinel(); }
  33. static void noteHead(BB*, BB*) {}
  34. //static ValueSymbolTable *getSymTab(Function *ItemParent);
  35. private:
  36. mutable ilist_half_node<BB> Sentinel;
  37. };
  38. }
  39. /* Procedure FLAGS */
  40. enum PROC_FLAGS
  41. {
  42. PROC_BADINST=0x00000100,/* Proc contains invalid or 386 instruction */
  43. PROC_IJMP =0x00000200,/* Proc incomplete due to indirect jmp */
  44. PROC_ICALL =0x00000400, /* Proc incomplete due to indirect call */
  45. PROC_HLL =0x00001000, /* Proc is likely to be from a HLL */
  46. CALL_PASCAL =0x00002000, /* Proc uses Pascal calling convention */
  47. CALL_C =0x00004000, /* Proc uses C calling convention */
  48. CALL_UNKNOWN=0x00008000, /* Proc uses unknown calling convention */
  49. PROC_NEAR =0x00010000, /* Proc exits with near return */
  50. PROC_FAR =0x00020000, /* Proc exits with far return */
  51. GRAPH_IRRED =0x00100000, /* Proc generates an irreducible graph */
  52. SI_REGVAR =0x00200000, /* SI is used as a stack variable */
  53. DI_REGVAR =0x00400000, /* DI is used as a stack variable */
  54. PROC_IS_FUNC=0x00800000, /* Proc is a function */
  55. REG_ARGS =0x01000000, /* Proc has registers as arguments */
  56. PROC_VARARG =0x02000000, /* Proc has variable arguments */
  57. PROC_OUTPUT =0x04000000, /* C for this proc has been output */
  58. PROC_RUNTIME=0x08000000, /* Proc is part of the runtime support */
  59. PROC_ISLIB =0x10000000, /* Proc is a library function */
  60. PROC_ASM =0x20000000, /* Proc is an intrinsic assembler routine */
  61. PROC_IS_HLL =0x40000000 /* Proc has HLL prolog code */
  62. #define CALL_MASK 0xFFFF9FFF /* Masks off CALL_C and CALL_PASCAL */
  63. };
  64. struct FunctionType
  65. {
  66. bool m_vararg;
  67. bool isVarArg() const {return m_vararg;}
  68. };
  69. struct Assignment
  70. {
  71. COND_EXPR *lhs;
  72. COND_EXPR *rhs;
  73. };
  74. struct Function : public llvm::ilist_node<Function>
  75. {
  76. typedef llvm::iplist<BB> BasicBlockListType;
  77. // BasicBlock iterators...
  78. typedef BasicBlockListType::iterator iterator;
  79. typedef BasicBlockListType::const_iterator const_iterator;
  80. private:
  81. BasicBlockListType BasicBlocks; ///< The basic blocks
  82. public:
  83. uint32_t procEntry; /* label number */
  84. std::string name; /* Meaningful name for this proc */
  85. STATE state; /* Entry state */
  86. int depth; /* Depth at which we found it - for printing */
  87. uint32_t flg; /* Combination of Icode & Proc flags */
  88. int16_t cbParam; /* Probable no. of bytes of parameters */
  89. STKFRAME args; /* Array of arguments */
  90. LOCAL_ID localId; /* Local identifiers */
  91. ID retVal; /* Return value - identifier */
  92. /* Icodes and control flow graph */
  93. CIcodeRec Icode; /* Object with ICODE records */
  94. std::list<BB*> m_cfg; /* Ptr. to BB list/CFG */
  95. std::vector<BB*> m_dfsLast;
  96. std::list<BB*> heldBBs;
  97. //BB * *dfsLast; /* Array of pointers to BBs in dfsLast
  98. // * (reverse postorder) order */
  99. size_t numBBs; /* Number of BBs in the graph cfg */
  100. bool hasCase; /* Procedure has a case node */
  101. /* For interprocedural live analysis */
  102. std::bitset<32> liveIn; /* Registers used before defined */
  103. std::bitset<32> liveOut; /* Registers that may be used in successors */
  104. bool liveAnal; /* Procedure has been analysed already */
  105. Function(void *ty=0) : procEntry(0),depth(0),flg(0),cbParam(0),m_cfg(0),m_dfsLast(0),numBBs(0),
  106. hasCase(false),liveIn(0),liveOut(0),liveAnal(0)//,next(0),prev(0)
  107. {
  108. }
  109. public:
  110. static Function *Create(void *ty=0,int Linkage=0,const std::string &nm="",void *module=0)
  111. {
  112. Function *r=new Function(ty);
  113. r->name = nm;
  114. return r;
  115. }
  116. void compoundCond();
  117. void writeProcComments();
  118. void lowLevelAnalysis();
  119. void bindIcodeOff();
  120. void dataFlow(std::bitset<32> &liveOut);
  121. void compressCFG();
  122. void highLevelGen();
  123. void structure(derSeq *derivedG);
  124. derSeq *checkReducibility();
  125. void createCFG();
  126. void markImpure();
  127. void findImmedDom();
  128. void FollowCtrl(CALL_GRAPH *pcallGraph, STATE *pstate);
  129. void process_operands(ICODE &pIcode, STATE *pstate);
  130. boolT process_JMP(ICODE &pIcode, STATE *pstate, CALL_GRAPH *pcallGraph);
  131. boolT process_CALL(ICODE &pIcode, CALL_GRAPH *pcallGraph, STATE *pstate);
  132. void displayCFG();
  133. void freeCFG();
  134. void codeGen(std::ostream &fs);
  135. void displayStats();
  136. void mergeFallThrough(BB *pBB);
  137. void structIfs();
  138. void structLoops(derSeq *derivedG);
  139. void buildCFG(Disassembler &ds);
  140. void controlFlowAnalysis();
  141. void newRegArg(iICODE picode, iICODE ticode);
  142. void writeProcComments(std::ostream &ostr);
  143. bool Case_X_and_Y(BB* pbb, BB* thenBB, BB* elseBB);
  144. bool Case_X_or_Y(BB* pbb, BB* thenBB, BB* elseBB);
  145. bool Case_notX_or_Y(BB* pbb, BB* thenBB, BB* elseBB);
  146. bool Case_notX_and_Y(BB* pbb, BB* thenBB, BB* elseBB);
  147. void replaceInEdge(BB* where, BB* which, BB* with);
  148. protected:
  149. bool removeInEdge_Flag_and_ProcessLatch(BB *pbb, BB *a, BB *b);
  150. // TODO: replace those with friend visitor ?
  151. void propLongReg(int loc_ident_idx, const ID &pLocId);
  152. void propLongStk(int i, const ID &pLocId);
  153. void propLongGlb(int i, const ID &pLocId);
  154. void processTargetIcode(iICODE picode, int &numHlIcodes, iICODE ticode, bool isLong);
  155. void processHliCall(COND_EXPR *exp, iICODE picode);
  156. int findBackwarLongDefs(int loc_ident_idx, const ID &pLocId, iICODE iter);
  157. int findForwardLongUses(int loc_ident_idx, const ID &pLocId, iICODE beg);
  158. void structCases();
  159. void findExps();
  160. void genDU1();
  161. void elimCondCodes();
  162. void liveRegAnalysis(std::bitset<32> &in_liveOut);
  163. void findIdioms();
  164. void propLong();
  165. void genLiveKtes();
  166. uint8_t findDerivedSeq (derSeq &derivedGi);
  167. bool nextOrderGraph(derSeq &derivedGi);
  168. };