1 //===- Operator.cpp -------------------------------------------------------===//
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 #include "mcld/Script/Operator.h"
10 
11 #include "mcld/Script/BinaryOp.h"
12 #include "mcld/Script/NullaryOp.h"
13 #include "mcld/Script/Operand.h"
14 #include "mcld/Script/UnaryOp.h"
15 #include "mcld/Script/TernaryOp.h"
16 #include "mcld/Support/raw_ostream.h"
17 
18 namespace mcld {
19 
20 //===----------------------------------------------------------------------===//
21 // Operator
22 //===----------------------------------------------------------------------===//
23 const char* Operator::OpNames[] = {
24     "+",                      "-",                  "!",
25     "~",                      "*",                  "/",
26     "%",                      "+",                  "-",
27     "<<",                     ">>",                 "<",
28     "<=",                     ">",                  ">=",
29     "==",                     "!=",                 "&",
30     "^",                      "|",                  "&&",
31     "||",                     "?:",                 "=",
32     "+=",                     "-=",                 "*=",
33     "/=",                     "&=",                 "|=",
34     "<<=",                    ">>=",                "ABSOLUTE",
35     "ADDR",                   "ALIGN",              "ALIGNOF",
36     "BLOCK",                  "DATA_SEGMENT_ALIGN", "DATA_SEGMENT_END",
37     "DATA_SEGMENT_RELRO_END", "DEFINED",            "LENGTH",
38     "LOADADDR",               "MAX",                "MIN",
39     "NEXT",                   "ORIGIN",             "SEGMENT_START",
40     "SIZEOF",                 "SIZEOF_HEADERS",     "MAXPAGESIZE",
41     "COMMONPAGESIZE"};
42 
Operator(Arity pArity,Type pType)43 Operator::Operator(Arity pArity, Type pType)
44     : ExprToken(ExprToken::OPERATOR), m_Arity(pArity), m_Type(pType) {
45   m_pIntOperand = IntOperand::create(0);
46 }
47 
~Operator()48 Operator::~Operator() {
49 }
50 
dump() const51 void Operator::dump() const {
52   mcld::outs() << OpNames[type()];
53 }
54 
55 /* Nullary operator */
56 template <>
create()57 Operator& Operator::create<Operator::SIZEOF_HEADERS>() {
58   static NullaryOp<Operator::SIZEOF_HEADERS> op;
59   return op;
60 }
61 
62 template <>
create()63 Operator& Operator::create<Operator::MAXPAGESIZE>() {
64   static NullaryOp<Operator::MAXPAGESIZE> op;
65   return op;
66 }
67 
68 template <>
create()69 Operator& Operator::create<Operator::COMMONPAGESIZE>() {
70   static NullaryOp<Operator::COMMONPAGESIZE> op;
71   return op;
72 }
73 
74 /* Unary operator */
75 template <>
create()76 Operator& Operator::create<Operator::UNARY_PLUS>() {
77   static UnaryOp<Operator::UNARY_PLUS> op;
78   return op;
79 }
80 
81 template <>
create()82 Operator& Operator::create<Operator::UNARY_MINUS>() {
83   static UnaryOp<Operator::UNARY_MINUS> op;
84   return op;
85 }
86 
87 template <>
create()88 Operator& Operator::create<Operator::LOGICAL_NOT>() {
89   static UnaryOp<Operator::LOGICAL_NOT> op;
90   return op;
91 }
92 
93 template <>
create()94 Operator& Operator::create<Operator::BITWISE_NOT>() {
95   static UnaryOp<Operator::BITWISE_NOT> op;
96   return op;
97 }
98 
99 template <>
create()100 Operator& Operator::create<Operator::ABSOLUTE>() {
101   static UnaryOp<Operator::ABSOLUTE> op;
102   return op;
103 }
104 
105 template <>
create()106 Operator& Operator::create<Operator::ADDR>() {
107   static UnaryOp<Operator::ADDR> op;
108   return op;
109 }
110 
111 template <>
create()112 Operator& Operator::create<Operator::ALIGNOF>() {
113   static UnaryOp<Operator::ALIGNOF> op;
114   return op;
115 }
116 
117 template <>
create()118 Operator& Operator::create<Operator::DATA_SEGMENT_END>() {
119   static UnaryOp<Operator::DATA_SEGMENT_END> op;
120   return op;
121 }
122 
123 template <>
create()124 Operator& Operator::create<Operator::DEFINED>() {
125   static UnaryOp<Operator::DEFINED> op;
126   return op;
127 }
128 
129 template <>
create()130 Operator& Operator::create<Operator::LENGTH>() {
131   static UnaryOp<Operator::LENGTH> op;
132   return op;
133 }
134 
135 template <>
create()136 Operator& Operator::create<Operator::LOADADDR>() {
137   static UnaryOp<Operator::LOADADDR> op;
138   return op;
139 }
140 
141 template <>
create()142 Operator& Operator::create<Operator::NEXT>() {
143   static UnaryOp<Operator::NEXT> op;
144   return op;
145 }
146 
147 template <>
create()148 Operator& Operator::create<Operator::ORIGIN>() {
149   static UnaryOp<Operator::ORIGIN> op;
150   return op;
151 }
152 
153 template <>
create()154 Operator& Operator::create<Operator::SIZEOF>() {
155   static UnaryOp<Operator::SIZEOF> op;
156   return op;
157 }
158 
159 /* Binary operator */
160 template <>
create()161 Operator& Operator::create<Operator::MUL>() {
162   static BinaryOp<Operator::MUL> op;
163   return op;
164 }
165 
166 template <>
create()167 Operator& Operator::create<Operator::DIV>() {
168   static BinaryOp<Operator::DIV> op;
169   return op;
170 }
171 
172 template <>
create()173 Operator& Operator::create<Operator::MOD>() {
174   static BinaryOp<Operator::MOD> op;
175   return op;
176 }
177 
178 template <>
create()179 Operator& Operator::create<Operator::ADD>() {
180   static BinaryOp<Operator::ADD> op;
181   return op;
182 }
183 
184 template <>
create()185 Operator& Operator::create<Operator::SUB>() {
186   static BinaryOp<Operator::SUB> op;
187   return op;
188 }
189 
190 template <>
create()191 Operator& Operator::create<Operator::LSHIFT>() {
192   static BinaryOp<Operator::LSHIFT> op;
193   return op;
194 }
195 
196 template <>
create()197 Operator& Operator::create<Operator::RSHIFT>() {
198   static BinaryOp<Operator::RSHIFT> op;
199   return op;
200 }
201 
202 template <>
create()203 Operator& Operator::create<Operator::LT>() {
204   static BinaryOp<Operator::LT> op;
205   return op;
206 }
207 
208 template <>
create()209 Operator& Operator::create<Operator::LE>() {
210   static BinaryOp<Operator::LE> op;
211   return op;
212 }
213 
214 template <>
create()215 Operator& Operator::create<Operator::GT>() {
216   static BinaryOp<Operator::GT> op;
217   return op;
218 }
219 
220 template <>
create()221 Operator& Operator::create<Operator::GE>() {
222   static BinaryOp<Operator::GE> op;
223   return op;
224 }
225 
226 template <>
create()227 Operator& Operator::create<Operator::EQ>() {
228   static BinaryOp<Operator::EQ> op;
229   return op;
230 }
231 
232 template <>
create()233 Operator& Operator::create<Operator::NE>() {
234   static BinaryOp<Operator::NE> op;
235   return op;
236 }
237 
238 template <>
create()239 Operator& Operator::create<Operator::BITWISE_AND>() {
240   static BinaryOp<Operator::BITWISE_AND> op;
241   return op;
242 }
243 
244 template <>
create()245 Operator& Operator::create<Operator::BITWISE_XOR>() {
246   static BinaryOp<Operator::BITWISE_XOR> op;
247   return op;
248 }
249 
250 template <>
create()251 Operator& Operator::create<Operator::BITWISE_OR>() {
252   static BinaryOp<Operator::BITWISE_OR> op;
253   return op;
254 }
255 
256 template <>
create()257 Operator& Operator::create<Operator::LOGICAL_AND>() {
258   static BinaryOp<Operator::LOGICAL_AND> op;
259   return op;
260 }
261 
262 template <>
create()263 Operator& Operator::create<Operator::LOGICAL_OR>() {
264   static BinaryOp<Operator::LOGICAL_OR> op;
265   return op;
266 }
267 
268 template <>
create()269 Operator& Operator::create<Operator::ALIGN>() {
270   static BinaryOp<Operator::ALIGN> op;
271   return op;
272 }
273 
274 template <>
create()275 Operator& Operator::create<Operator::DATA_SEGMENT_RELRO_END>() {
276   static BinaryOp<Operator::DATA_SEGMENT_RELRO_END> op;
277   return op;
278 }
279 
280 template <>
create()281 Operator& Operator::create<Operator::MAX>() {
282   static BinaryOp<Operator::MAX> op;
283   return op;
284 }
285 
286 template <>
create()287 Operator& Operator::create<Operator::MIN>() {
288   static BinaryOp<Operator::MIN> op;
289   return op;
290 }
291 
292 template <>
create()293 Operator& Operator::create<Operator::SEGMENT_START>() {
294   static BinaryOp<Operator::SEGMENT_START> op;
295   return op;
296 }
297 
298 /* Ternary operator */
299 template <>
create()300 Operator& Operator::create<Operator::TERNARY_IF>() {
301   static TernaryOp<Operator::TERNARY_IF> op;
302   return op;
303 }
304 
305 template <>
create()306 Operator& Operator::create<Operator::DATA_SEGMENT_ALIGN>() {
307   static TernaryOp<Operator::DATA_SEGMENT_ALIGN> op;
308   return op;
309 }
310 
311 }  // namespace mcld
312