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