Procedure.h 6.5 KB

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