• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- Operator.h ---------------------------------------------------------===//
2 //
3 //                     The MCLinker Project
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 #ifndef MCLD_SCRIPT_OPERATOR_INTERFACE_H
10 #define MCLD_SCRIPT_OPERATOR_INTERFACE_H
11 
12 #include <mcld/Script/ExprToken.h>
13 #include <llvm/Support/DataTypes.h>
14 
15 namespace mcld
16 {
17 
18 class Operand;
19 class IntOperand;
20 class Module;
21 class TargetLDBackend;
22 
23 /** \class Operator
24  *  \brief This class defines the interfaces to an operator token.
25  */
26 
27 class Operator : public ExprToken
28 {
29 public:
30   enum Arity {
31     NULLARY,
32     UNARY,
33     BINARY,
34     TERNARY
35   };
36 
37   enum Type {
38     /* arithmetic operator */
39     UNARY_PLUS  = 0,
40     UNARY_MINUS = 1,
41     LOGICAL_NOT = 2,
42     BITWISE_NOT = 3,
43     MUL         = 4,
44     DIV         = 5,
45     MOD         = 6,
46     ADD         = 7,
47     SUB         = 8,
48     LSHIFT      = 9,
49     RSHIFT      = 10,
50     LT          = 11,
51     LE          = 12,
52     GT          = 13,
53     GE          = 14,
54     EQ          = 15,
55     NE          = 16,
56     BITWISE_AND = 17,
57     BITWISE_XOR = 18,
58     BITWISE_OR  = 19,
59     LOGICAL_AND = 20,
60     LOGICAL_OR  = 21,
61     TERNARY_IF  = 22,
62     ASSIGN      = 23,
63     ADD_ASSIGN  = 24,
64     SUB_ASSIGN  = 25,
65     MUL_ASSIGN  = 26,
66     DIV_ASSIGN  = 27,
67     AND_ASSIGN  = 28,
68     OR_ASSIGN   = 29,
69     LS_ASSIGN   = 30,
70     RS_ASSIGN   = 31,
71     /* function */
72     ABSOLUTE               = 32,
73     ADDR                   = 33,
74     ALIGN                  = 34,
75     ALIGNOF                = 35,
76     BLOCK                  = 36,
77     DATA_SEGMENT_ALIGN     = 37,
78     DATA_SEGMENT_END       = 38,
79     DATA_SEGMENT_RELRO_END = 39,
80     DEFINED                = 40,
81     LENGTH                 = 41,
82     LOADADDR               = 42,
83     MAX                    = 43,
84     MIN                    = 44,
85     NEXT                   = 45,
86     ORIGIN                 = 46,
87     SEGMENT_START          = 47,
88     SIZEOF                 = 48,
89     SIZEOF_HEADERS         = 49,
90     MAXPAGESIZE            = 50,
91     COMMONPAGESIZE         = 51
92   };
93 
94   static const char* OpNames[];
95 
96 protected:
97   Operator(Arity pArity, Type pType);
98 
result()99   const IntOperand* result() const { return m_pIntOperand; }
result()100   IntOperand*       result()       { return m_pIntOperand; }
101 
102 public:
103   virtual ~Operator();
104 
arity()105   Arity arity() const { return m_Arity; }
106 
type()107   Type type() const { return m_Type; }
108 
109   virtual void dump() const;
110 
111   virtual IntOperand* eval(const Module& pModule,
112                            const TargetLDBackend& pBackend) = 0;
113 
114   virtual void appendOperand(Operand* pOperand) = 0;
115 
classof(const ExprToken * pToken)116   static bool classof(const ExprToken* pToken)
117   {
118     return pToken->kind() == ExprToken::OPERATOR;
119   }
120 
121   template<Operator::Type TYPE>
122   static Operator& create();
123 
124 private:
125   Arity m_Arity;
126   Type m_Type;
127   IntOperand* m_pIntOperand;
128 };
129 
130 /* Nullary operator */
131 template<>
132 Operator& Operator::create<Operator::SIZEOF_HEADERS>();
133 template<>
134 Operator& Operator::create<Operator::MAXPAGESIZE>();
135 template<>
136 Operator& Operator::create<Operator::COMMONPAGESIZE>();
137 
138 /* Unary operator */
139 template<>
140 Operator& Operator::create<Operator::UNARY_PLUS>();
141 template<>
142 Operator& Operator::create<Operator::UNARY_MINUS>();
143 template<>
144 Operator& Operator::create<Operator::LOGICAL_NOT>();
145 template<>
146 Operator& Operator::create<Operator::BITWISE_NOT>();
147 
148 template<>
149 Operator& Operator::create<Operator::ABSOLUTE>();
150 template<>
151 Operator& Operator::create<Operator::ADDR>();
152 template<>
153 Operator& Operator::create<Operator::ALIGNOF>();
154 template<>
155 Operator& Operator::create<Operator::DATA_SEGMENT_END>();
156 template<>
157 Operator& Operator::create<Operator::DEFINED>();
158 template<>
159 Operator& Operator::create<Operator::LENGTH>();
160 template<>
161 Operator& Operator::create<Operator::LOADADDR>();
162 template<>
163 Operator& Operator::create<Operator::NEXT>();
164 template<>
165 Operator& Operator::create<Operator::ORIGIN>();
166 template<>
167 Operator& Operator::create<Operator::SIZEOF>();
168 
169 /* Binary operator */
170 template<>
171 Operator& Operator::create<Operator::MUL>();
172 template<>
173 Operator& Operator::create<Operator::DIV>();
174 template<>
175 Operator& Operator::create<Operator::MOD>();
176 template<>
177 Operator& Operator::create<Operator::ADD>();
178 template<>
179 Operator& Operator::create<Operator::SUB>();
180 template<>
181 Operator& Operator::create<Operator::LSHIFT>();
182 template<>
183 Operator& Operator::create<Operator::RSHIFT>();
184 template<>
185 Operator& Operator::create<Operator::LT>();
186 template<>
187 Operator& Operator::create<Operator::LE>();
188 template<>
189 Operator& Operator::create<Operator::GT>();
190 template<>
191 Operator& Operator::create<Operator::GE>();
192 template<>
193 Operator& Operator::create<Operator::EQ>();
194 template<>
195 Operator& Operator::create<Operator::NE>();
196 template<>
197 Operator& Operator::create<Operator::BITWISE_AND>();
198 template<>
199 Operator& Operator::create<Operator::BITWISE_XOR>();
200 template<>
201 Operator& Operator::create<Operator::BITWISE_OR>();
202 template<>
203 Operator& Operator::create<Operator::LOGICAL_AND>();
204 template<>
205 Operator& Operator::create<Operator::LOGICAL_OR>();
206 
207 template<>
208 Operator& Operator::create<Operator::ALIGN>();
209 template<>
210 Operator& Operator::create<Operator::DATA_SEGMENT_RELRO_END>();
211 template<>
212 Operator& Operator::create<Operator::MAX>();
213 template<>
214 Operator& Operator::create<Operator::MIN>();
215 template<>
216 Operator& Operator::create<Operator::SEGMENT_START>();
217 
218 /* Ternary operator */
219 template<>
220 Operator& Operator::create<Operator::TERNARY_IF>();
221 
222 template<>
223 Operator&
224 Operator::create<Operator::DATA_SEGMENT_ALIGN>();
225 } // namespace of mcld
226 
227 #endif
228 
229