ast.h 5.7 KB

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