Procedure.h 6.5 KB

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