MCExpr.cpp revision e579849652f2ba062e6c91a3af4d9a3843411b44
1//===- MCExpr.cpp - Assembly Level Expression Implementation --------------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9 10#include "llvm/MC/MCExpr.h" 11#include "llvm/MC/MCContext.h" 12#include "llvm/MC/MCSymbol.h" 13#include "llvm/MC/MCValue.h" 14#include "llvm/Support/raw_ostream.h" 15using namespace llvm; 16 17void MCExpr::print(raw_ostream &OS, const MCAsmInfo *MAI) const { 18 switch (getKind()) { 19 case MCExpr::Constant: 20 OS << cast<MCConstantExpr>(*this).getValue(); 21 return; 22 23 case MCExpr::SymbolRef: { 24 const MCSymbol &Sym = cast<MCSymbolRefExpr>(*this).getSymbol(); 25 26 // Parenthesize names that start with $ so that they don't look like 27 // absolute names. 28 if (Sym.getName()[0] == '$') { 29 OS << '('; 30 Sym.print(OS, MAI); 31 OS << ')'; 32 } else { 33 Sym.print(OS, MAI); 34 } 35 return; 36 } 37 38 case MCExpr::Unary: { 39 const MCUnaryExpr &UE = cast<MCUnaryExpr>(*this); 40 switch (UE.getOpcode()) { 41 default: assert(0 && "Invalid opcode!"); 42 case MCUnaryExpr::LNot: OS << '!'; break; 43 case MCUnaryExpr::Minus: OS << '-'; break; 44 case MCUnaryExpr::Not: OS << '~'; break; 45 case MCUnaryExpr::Plus: OS << '+'; break; 46 } 47 UE.getSubExpr()->print(OS, MAI); 48 return; 49 } 50 51 case MCExpr::Binary: { 52 const MCBinaryExpr &BE = cast<MCBinaryExpr>(*this); 53 54 // Only print parens around the LHS if it is non-trivial. 55 if (isa<MCConstantExpr>(BE.getLHS()) || isa<MCSymbolRefExpr>(BE.getLHS())) { 56 BE.getLHS()->print(OS, MAI); 57 } else { 58 OS << '('; 59 BE.getLHS()->print(OS, MAI); 60 OS << ')'; 61 } 62 63 switch (BE.getOpcode()) { 64 default: assert(0 && "Invalid opcode!"); 65 case MCBinaryExpr::Add: 66 // Print "X-42" instead of "X+-42". 67 if (const MCConstantExpr *RHSC = dyn_cast<MCConstantExpr>(BE.getRHS())) { 68 if (RHSC->getValue() < 0) { 69 OS << RHSC->getValue(); 70 return; 71 } 72 } 73 74 OS << '+'; 75 break; 76 case MCBinaryExpr::And: OS << '&'; break; 77 case MCBinaryExpr::Div: OS << '/'; break; 78 case MCBinaryExpr::EQ: OS << "=="; break; 79 case MCBinaryExpr::GT: OS << '>'; break; 80 case MCBinaryExpr::GTE: OS << ">="; break; 81 case MCBinaryExpr::LAnd: OS << "&&"; break; 82 case MCBinaryExpr::LOr: OS << "||"; break; 83 case MCBinaryExpr::LT: OS << '<'; break; 84 case MCBinaryExpr::LTE: OS << "<="; break; 85 case MCBinaryExpr::Mod: OS << '%'; break; 86 case MCBinaryExpr::Mul: OS << '*'; break; 87 case MCBinaryExpr::NE: OS << "!="; break; 88 case MCBinaryExpr::Or: OS << '|'; break; 89 case MCBinaryExpr::Shl: OS << "<<"; break; 90 case MCBinaryExpr::Shr: OS << ">>"; break; 91 case MCBinaryExpr::Sub: OS << '-'; break; 92 case MCBinaryExpr::Xor: OS << '^'; break; 93 } 94 95 // Only print parens around the LHS if it is non-trivial. 96 if (isa<MCConstantExpr>(BE.getRHS()) || isa<MCSymbolRefExpr>(BE.getRHS())) { 97 BE.getRHS()->print(OS, MAI); 98 } else { 99 OS << '('; 100 BE.getRHS()->print(OS, MAI); 101 OS << ')'; 102 } 103 return; 104 } 105 } 106 107 assert(0 && "Invalid expression kind!"); 108} 109 110void MCExpr::dump() const { 111 print(errs(), 0); 112 errs() << '\n'; 113} 114 115/* *** */ 116 117const MCBinaryExpr *MCBinaryExpr::Create(Opcode Opc, const MCExpr *LHS, 118 const MCExpr *RHS, MCContext &Ctx) { 119 return new (Ctx) MCBinaryExpr(Opc, LHS, RHS); 120} 121 122const MCUnaryExpr *MCUnaryExpr::Create(Opcode Opc, const MCExpr *Expr, 123 MCContext &Ctx) { 124 return new (Ctx) MCUnaryExpr(Opc, Expr); 125} 126 127const MCConstantExpr *MCConstantExpr::Create(int64_t Value, MCContext &Ctx) { 128 return new (Ctx) MCConstantExpr(Value); 129} 130 131const MCSymbolRefExpr *MCSymbolRefExpr::Create(const MCSymbol *Sym, 132 MCContext &Ctx) { 133 return new (Ctx) MCSymbolRefExpr(Sym); 134} 135 136const MCSymbolRefExpr *MCSymbolRefExpr::Create(const StringRef &Name, 137 MCContext &Ctx) { 138 return Create(Ctx.GetOrCreateSymbol(Name), Ctx); 139} 140 141 142/* *** */ 143 144bool MCExpr::EvaluateAsAbsolute(MCContext &Ctx, int64_t &Res) const { 145 MCValue Value; 146 147 if (!EvaluateAsRelocatable(Ctx, Value) || !Value.isAbsolute()) 148 return false; 149 150 Res = Value.getConstant(); 151 return true; 152} 153 154static bool EvaluateSymbolicAdd(const MCValue &LHS, const MCSymbol *RHS_A, 155 const MCSymbol *RHS_B, int64_t RHS_Cst, 156 MCValue &Res) { 157 // We can't add or subtract two symbols. 158 if ((LHS.getSymA() && RHS_A) || 159 (LHS.getSymB() && RHS_B)) 160 return false; 161 162 const MCSymbol *A = LHS.getSymA() ? LHS.getSymA() : RHS_A; 163 const MCSymbol *B = LHS.getSymB() ? LHS.getSymB() : RHS_B; 164 if (B) { 165 // If we have a negated symbol, then we must have also have a non-negated 166 // symbol in order to encode the expression. We can do this check later to 167 // permit expressions which eventually fold to a representable form -- such 168 // as (a + (0 - b)) -- if necessary. 169 if (!A) 170 return false; 171 } 172 Res = MCValue::get(A, B, LHS.getConstant() + RHS_Cst); 173 return true; 174} 175 176bool MCExpr::EvaluateAsRelocatable(MCContext &Ctx, MCValue &Res) const { 177 switch (getKind()) { 178 case Constant: 179 Res = MCValue::get(cast<MCConstantExpr>(this)->getValue()); 180 return true; 181 182 case SymbolRef: { 183 const MCSymbol &Sym = cast<MCSymbolRefExpr>(this)->getSymbol(); 184 if (const MCExpr *Value = Ctx.GetSymbolValue(&Sym)) 185 return Value->EvaluateAsRelocatable(Ctx, Res); 186 Res = MCValue::get(&Sym, 0, 0); 187 return true; 188 } 189 190 case Unary: { 191 const MCUnaryExpr *AUE = cast<MCUnaryExpr>(this); 192 MCValue Value; 193 194 if (!AUE->getSubExpr()->EvaluateAsRelocatable(Ctx, Value)) 195 return false; 196 197 switch (AUE->getOpcode()) { 198 case MCUnaryExpr::LNot: 199 if (!Value.isAbsolute()) 200 return false; 201 Res = MCValue::get(!Value.getConstant()); 202 break; 203 case MCUnaryExpr::Minus: 204 /// -(a - b + const) ==> (b - a - const) 205 if (Value.getSymA() && !Value.getSymB()) 206 return false; 207 Res = MCValue::get(Value.getSymB(), Value.getSymA(), 208 -Value.getConstant()); 209 break; 210 case MCUnaryExpr::Not: 211 if (!Value.isAbsolute()) 212 return false; 213 Res = MCValue::get(~Value.getConstant()); 214 break; 215 case MCUnaryExpr::Plus: 216 Res = Value; 217 break; 218 } 219 220 return true; 221 } 222 223 case Binary: { 224 const MCBinaryExpr *ABE = cast<MCBinaryExpr>(this); 225 MCValue LHSValue, RHSValue; 226 227 if (!ABE->getLHS()->EvaluateAsRelocatable(Ctx, LHSValue) || 228 !ABE->getRHS()->EvaluateAsRelocatable(Ctx, RHSValue)) 229 return false; 230 231 // We only support a few operations on non-constant expressions, handle 232 // those first. 233 if (!LHSValue.isAbsolute() || !RHSValue.isAbsolute()) { 234 switch (ABE->getOpcode()) { 235 default: 236 return false; 237 case MCBinaryExpr::Sub: 238 // Negate RHS and add. 239 return EvaluateSymbolicAdd(LHSValue, 240 RHSValue.getSymB(), RHSValue.getSymA(), 241 -RHSValue.getConstant(), 242 Res); 243 244 case MCBinaryExpr::Add: 245 return EvaluateSymbolicAdd(LHSValue, 246 RHSValue.getSymA(), RHSValue.getSymB(), 247 RHSValue.getConstant(), 248 Res); 249 } 250 } 251 252 // FIXME: We need target hooks for the evaluation. It may be limited in 253 // width, and gas defines the result of comparisons differently from Apple 254 // as (the result is sign extended). 255 int64_t LHS = LHSValue.getConstant(), RHS = RHSValue.getConstant(); 256 int64_t Result = 0; 257 switch (ABE->getOpcode()) { 258 case MCBinaryExpr::Add: Result = LHS + RHS; break; 259 case MCBinaryExpr::And: Result = LHS & RHS; break; 260 case MCBinaryExpr::Div: Result = LHS / RHS; break; 261 case MCBinaryExpr::EQ: Result = LHS == RHS; break; 262 case MCBinaryExpr::GT: Result = LHS > RHS; break; 263 case MCBinaryExpr::GTE: Result = LHS >= RHS; break; 264 case MCBinaryExpr::LAnd: Result = LHS && RHS; break; 265 case MCBinaryExpr::LOr: Result = LHS || RHS; break; 266 case MCBinaryExpr::LT: Result = LHS < RHS; break; 267 case MCBinaryExpr::LTE: Result = LHS <= RHS; break; 268 case MCBinaryExpr::Mod: Result = LHS % RHS; break; 269 case MCBinaryExpr::Mul: Result = LHS * RHS; break; 270 case MCBinaryExpr::NE: Result = LHS != RHS; break; 271 case MCBinaryExpr::Or: Result = LHS | RHS; break; 272 case MCBinaryExpr::Shl: Result = LHS << RHS; break; 273 case MCBinaryExpr::Shr: Result = LHS >> RHS; break; 274 case MCBinaryExpr::Sub: Result = LHS - RHS; break; 275 case MCBinaryExpr::Xor: Result = LHS ^ RHS; break; 276 } 277 278 Res = MCValue::get(Result); 279 return true; 280 } 281 } 282 283 assert(0 && "Invalid assembly expression kind!"); 284 return false; 285} 286