ast.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * File: ast.h
  3. * Purpose: definition of the abstract syntax tree ADT.
  4. * Date: September 1993
  5. * (C) Cristina Cifuentes
  6. */
  7. #pragma once
  8. #include <cstring>
  9. #include <list>
  10. #include <boost/range/iterator_range.hpp>
  11. #include "Enums.h"
  12. static const int operandSize=20;
  13. /* The following definitions and types define the Conditional Expression
  14. * attributed syntax tree, as defined by the following EBNF:
  15. CondExp ::= CondTerm AND CondTerm | CondTerm
  16. CondTerm ::= (CondFactor op CondFactor)
  17. CondFactor ::= Identifier | ! CondFactor
  18. Identifier ::= globalVar | register | localVar | parameter | constant
  19. op ::= <= | < | = | != | > | >=
  20. */
  21. /* High-level BOOLEAN conditions for iJB..iJNS icodes */
  22. static const condOp condOpJCond[12] = {LESS, LESS_EQUAL, GREATER_EQUAL, GREATER,
  23. EQUAL, NOT_EQUAL, LESS, GREATER_EQUAL,
  24. LESS_EQUAL, GREATER, GREATER_EQUAL, LESS};
  25. struct Function;
  26. struct STKFRAME;
  27. struct LOCAL_ID;
  28. struct ICODE;
  29. struct LLInst;
  30. struct ID;
  31. typedef std::list<ICODE>::iterator iICODE;
  32. typedef boost::iterator_range<iICODE> rICODE;
  33. #include "IdentType.h"
  34. /* Expression data type */
  35. struct COND_EXPR
  36. {
  37. protected:
  38. struct /* for BOOLEAN_OP */
  39. {
  40. condOp op;
  41. COND_EXPR *lhs;
  42. COND_EXPR *rhs;
  43. } boolExpr;
  44. public:
  45. condNodeType m_type; /* Conditional Expression Node Type */
  46. union _exprNode { /* Different cond expr nodes */
  47. COND_EXPR *unaryExp; /* for NEGATION,ADDRESSOF,DEREFERENCE*/
  48. IDENTTYPE ident; /* for IDENTIFIER */
  49. } expr;
  50. COND_EXPR *lhs()
  51. {
  52. assert(m_type==BOOLEAN_OP);
  53. return boolExpr.lhs;
  54. }
  55. const COND_EXPR *lhs() const
  56. {
  57. assert(m_type==BOOLEAN_OP);
  58. return boolExpr.lhs;
  59. }
  60. COND_EXPR *rhs()
  61. {
  62. assert(m_type==BOOLEAN_OP);
  63. return boolExpr.rhs;
  64. }
  65. const COND_EXPR *rhs() const
  66. {
  67. assert(m_type==BOOLEAN_OP);
  68. return boolExpr.rhs;
  69. }
  70. condOp op() const { return boolExpr.op;}
  71. public:
  72. static COND_EXPR * idRegIdx(int idx, regType reg_type);
  73. static COND_EXPR * idKte(uint32_t kte, uint8_t size);
  74. static COND_EXPR * idLoc(int off, LOCAL_ID *localId);
  75. static COND_EXPR * idReg(eReg regi, uint32_t icodeFlg, LOCAL_ID *locsym);
  76. static COND_EXPR * idLongIdx(int idx);
  77. static COND_EXPR * idOther(eReg seg, eReg regi, int16_t off);
  78. static COND_EXPR * idParam(int off, const STKFRAME *argSymtab);
  79. static COND_EXPR * unary(condNodeType t, COND_EXPR *sub_expr);
  80. static COND_EXPR * idLong(LOCAL_ID *localId, opLoc sd, iICODE pIcode, hlFirst f, iICODE ix, operDu du, LLInst &atOffset);
  81. static COND_EXPR * idFunc(Function *pproc, STKFRAME *args);
  82. static COND_EXPR * idID(const ID *retVal, LOCAL_ID *locsym, iICODE ix_);
  83. static COND_EXPR * id(const LLInst &ll_insn, opLoc sd, Function *pProc, iICODE ix_, ICODE &duIcode, operDu du);
  84. static COND_EXPR * boolOp(COND_EXPR *_lhs, COND_EXPR *_rhs, condOp _op);
  85. static bool insertSubTreeLongReg(COND_EXPR *exp, COND_EXPR **tree, int longIdx);
  86. static bool insertSubTreeReg(COND_EXPR *&tree, COND_EXPR *_expr, eReg regi, const LOCAL_ID *locsym);
  87. public:
  88. virtual COND_EXPR *clone() const;
  89. void release();
  90. void changeBoolOp(condOp newOp);
  91. COND_EXPR(const COND_EXPR &other)
  92. {
  93. m_type=other.m_type;
  94. expr=other.expr;
  95. boolExpr=other.boolExpr;
  96. }
  97. COND_EXPR(condNodeType t=UNKNOWN_OP) : m_type(t)
  98. {
  99. memset(&expr,0,sizeof(_exprNode));
  100. memset(&boolExpr,0,sizeof(boolExpr));
  101. }
  102. virtual ~COND_EXPR() {}
  103. public:
  104. virtual COND_EXPR *inverse() const; // return new COND_EXPR that is invarse of this
  105. virtual bool xClear(rICODE range_to_check, iICODE lastBBinst, const LOCAL_ID &locId);
  106. virtual COND_EXPR *insertSubTreeReg(COND_EXPR *_expr, eReg regi, const LOCAL_ID *locsym);
  107. virtual COND_EXPR *insertSubTreeLongReg(COND_EXPR *_expr, int longIdx);
  108. virtual hlType expType(Function *pproc) const;
  109. };
  110. struct BinaryOperator : public COND_EXPR
  111. {
  112. condOp m_op;
  113. COND_EXPR *m_lhs;
  114. COND_EXPR *m_rhs;
  115. BinaryOperator(condOp o)
  116. {
  117. m_op = o;
  118. m_lhs=m_rhs=nullptr;
  119. }
  120. static BinaryOperator *Create(condOp o,COND_EXPR *l,COND_EXPR *r);
  121. static BinaryOperator *CreateAdd(COND_EXPR *l,COND_EXPR *r);
  122. virtual COND_EXPR *inverse() const;
  123. virtual COND_EXPR *clone() const;
  124. virtual bool xClear(rICODE range_to_check, iICODE lastBBinst, const LOCAL_ID &locs);
  125. virtual COND_EXPR *insertSubTreeReg(COND_EXPR *_expr, eReg regi, const LOCAL_ID *locsym);
  126. virtual COND_EXPR *insertSubTreeLongReg(COND_EXPR *_expr, int longIdx);
  127. COND_EXPR *lhs()
  128. {
  129. assert(m_type==BOOLEAN_OP);
  130. return m_lhs;
  131. }
  132. const COND_EXPR *lhs() const
  133. {
  134. assert(m_type==BOOLEAN_OP);
  135. return m_lhs;
  136. }
  137. COND_EXPR *rhs()
  138. {
  139. assert(m_type==BOOLEAN_OP);
  140. return m_rhs;
  141. }
  142. const COND_EXPR *rhs() const
  143. {
  144. assert(m_type==BOOLEAN_OP);
  145. return m_rhs;
  146. }
  147. condOp op() const { return m_op;}
  148. /* Changes the boolean conditional operator at the root of this expression */
  149. void op(condOp o) { m_op=o;}
  150. };
  151. struct UnaryOperator : public COND_EXPR
  152. {
  153. condOp op;
  154. COND_EXPR *unaryExp;
  155. virtual COND_EXPR *inverse() const;
  156. virtual COND_EXPR *clone() const;
  157. virtual bool xClear(rICODE range_to_check, iICODE lastBBinst, const LOCAL_ID &locs);
  158. static UnaryOperator *Create(condNodeType t, COND_EXPR *sub_expr)
  159. {
  160. UnaryOperator *newExp = new UnaryOperator();
  161. newExp->m_type=t;
  162. newExp->unaryExp = sub_expr;
  163. return (newExp);
  164. }
  165. };
  166. struct GlobalVariable : public COND_EXPR
  167. {
  168. static COND_EXPR *Create(int16_t segValue, int16_t off);
  169. };
  170. struct Constant : public COND_EXPR
  171. {};