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