1 //===- MCExpr.cpp - Assembly Level Expression Implementation --------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #define DEBUG_TYPE "mcexpr"
11 #include "llvm/MC/MCExpr.h"
12 #include "llvm/ADT/Statistic.h"
13 #include "llvm/ADT/StringSwitch.h"
14 #include "llvm/MC/MCAsmLayout.h"
15 #include "llvm/MC/MCAssembler.h"
16 #include "llvm/MC/MCContext.h"
17 #include "llvm/MC/MCObjectFormat.h"
18 #include "llvm/MC/MCSymbol.h"
19 #include "llvm/MC/MCValue.h"
20 #include "llvm/Support/Debug.h"
21 #include "llvm/Support/raw_ostream.h"
22 #include "llvm/Target/TargetAsmBackend.h"
27 STATISTIC(MCExprEvaluate, "Number of MCExpr evaluations");
31 void MCExpr::print(raw_ostream &OS) const {
34 return cast<MCTargetExpr>(this)->PrintImpl(OS);
35 case MCExpr::Constant:
36 OS << cast<MCConstantExpr>(*this).getValue();
39 case MCExpr::SymbolRef: {
40 const MCSymbolRefExpr &SRE = cast<MCSymbolRefExpr>(*this);
41 const MCSymbol &Sym = SRE.getSymbol();
43 if (SRE.getKind() == MCSymbolRefExpr::VK_ARM_HI16 ||
44 SRE.getKind() == MCSymbolRefExpr::VK_ARM_LO16)
45 OS << MCSymbolRefExpr::getVariantKindName(SRE.getKind());
47 // Parenthesize names that start with $ so that they don't look like
49 if (Sym.getName()[0] == '$')
50 OS << '(' << Sym << ')';
54 if (SRE.getKind() == MCSymbolRefExpr::VK_ARM_PLT)
55 OS << MCSymbolRefExpr::getVariantKindName(SRE.getKind());
56 else if (SRE.getKind() != MCSymbolRefExpr::VK_None &&
57 SRE.getKind() != MCSymbolRefExpr::VK_ARM_HI16 &&
58 SRE.getKind() != MCSymbolRefExpr::VK_ARM_LO16)
59 OS << '@' << MCSymbolRefExpr::getVariantKindName(SRE.getKind());
65 const MCUnaryExpr &UE = cast<MCUnaryExpr>(*this);
66 switch (UE.getOpcode()) {
67 default: assert(0 && "Invalid opcode!");
68 case MCUnaryExpr::LNot: OS << '!'; break;
69 case MCUnaryExpr::Minus: OS << '-'; break;
70 case MCUnaryExpr::Not: OS << '~'; break;
71 case MCUnaryExpr::Plus: OS << '+'; break;
73 OS << *UE.getSubExpr();
77 case MCExpr::Binary: {
78 const MCBinaryExpr &BE = cast<MCBinaryExpr>(*this);
80 // Only print parens around the LHS if it is non-trivial.
81 if (isa<MCConstantExpr>(BE.getLHS()) || isa<MCSymbolRefExpr>(BE.getLHS())) {
84 OS << '(' << *BE.getLHS() << ')';
87 switch (BE.getOpcode()) {
88 default: assert(0 && "Invalid opcode!");
89 case MCBinaryExpr::Add:
90 // Print "X-42" instead of "X+-42".
91 if (const MCConstantExpr *RHSC = dyn_cast<MCConstantExpr>(BE.getRHS())) {
92 if (RHSC->getValue() < 0) {
93 OS << RHSC->getValue();
100 case MCBinaryExpr::And: OS << '&'; break;
101 case MCBinaryExpr::Div: OS << '/'; break;
102 case MCBinaryExpr::EQ: OS << "=="; break;
103 case MCBinaryExpr::GT: OS << '>'; break;
104 case MCBinaryExpr::GTE: OS << ">="; break;
105 case MCBinaryExpr::LAnd: OS << "&&"; break;
106 case MCBinaryExpr::LOr: OS << "||"; break;
107 case MCBinaryExpr::LT: OS << '<'; break;
108 case MCBinaryExpr::LTE: OS << "<="; break;
109 case MCBinaryExpr::Mod: OS << '%'; break;
110 case MCBinaryExpr::Mul: OS << '*'; break;
111 case MCBinaryExpr::NE: OS << "!="; break;
112 case MCBinaryExpr::Or: OS << '|'; break;
113 case MCBinaryExpr::Shl: OS << "<<"; break;
114 case MCBinaryExpr::Shr: OS << ">>"; break;
115 case MCBinaryExpr::Sub: OS << '-'; break;
116 case MCBinaryExpr::Xor: OS << '^'; break;
119 // Only print parens around the LHS if it is non-trivial.
120 if (isa<MCConstantExpr>(BE.getRHS()) || isa<MCSymbolRefExpr>(BE.getRHS())) {
123 OS << '(' << *BE.getRHS() << ')';
129 assert(0 && "Invalid expression kind!");
132 void MCExpr::dump() const {
139 const MCBinaryExpr *MCBinaryExpr::Create(Opcode Opc, const MCExpr *LHS,
140 const MCExpr *RHS, MCContext &Ctx) {
141 return new (Ctx) MCBinaryExpr(Opc, LHS, RHS);
144 const MCUnaryExpr *MCUnaryExpr::Create(Opcode Opc, const MCExpr *Expr,
146 return new (Ctx) MCUnaryExpr(Opc, Expr);
149 const MCConstantExpr *MCConstantExpr::Create(int64_t Value, MCContext &Ctx) {
150 return new (Ctx) MCConstantExpr(Value);
155 const MCSymbolRefExpr *MCSymbolRefExpr::Create(const MCSymbol *Sym,
158 return new (Ctx) MCSymbolRefExpr(Sym, Kind);
161 const MCSymbolRefExpr *MCSymbolRefExpr::Create(StringRef Name, VariantKind Kind,
163 return Create(Ctx.GetOrCreateSymbol(Name), Kind, Ctx);
166 StringRef MCSymbolRefExpr::getVariantKindName(VariantKind Kind) {
169 case VK_Invalid: return "<<invalid>>";
170 case VK_None: return "<<none>>";
172 case VK_GOT: return "GOT";
173 case VK_GOTOFF: return "GOTOFF";
174 case VK_GOTPCREL: return "GOTPCREL";
175 case VK_GOTTPOFF: return "GOTTPOFF";
176 case VK_INDNTPOFF: return "INDNTPOFF";
177 case VK_NTPOFF: return "NTPOFF";
178 case VK_GOTNTPOFF: return "GOTNTPOFF";
179 case VK_PLT: return "PLT";
180 case VK_TLSGD: return "TLSGD";
181 case VK_TLSLDM: return "TLSLDM";
182 case VK_TPOFF: return "TPOFF";
183 case VK_DTPOFF: return "DTPOFF";
184 case VK_ARM_HI16: return ":upper16:";
185 case VK_ARM_LO16: return ":lower16:";
186 case VK_ARM_PLT: return "(PLT)";
187 case VK_TLVP: return "TLVP";
191 MCSymbolRefExpr::VariantKind
192 MCSymbolRefExpr::getVariantKindForName(StringRef Name) {
193 return StringSwitch<VariantKind>(Name)
195 .Case("GOTOFF", VK_GOTOFF)
196 .Case("GOTPCREL", VK_GOTPCREL)
197 .Case("GOTTPOFF", VK_GOTTPOFF)
198 .Case("INDNTPOFF", VK_INDNTPOFF)
199 .Case("NTPOFF", VK_NTPOFF)
200 .Case("GOTNTPOFF", VK_GOTNTPOFF)
202 .Case("TLSGD", VK_TLSGD)
203 .Case("TLSLDM", VK_TLSLDM)
204 .Case("TPOFF", VK_TPOFF)
205 .Case("DTPOFF", VK_DTPOFF)
206 .Case("TLVP", VK_TLVP)
207 .Default(VK_Invalid);
212 void MCTargetExpr::Anchor() {}
216 bool MCExpr::EvaluateAsAbsolute(int64_t &Res, const MCAsmLayout *Layout) const {
219 // Fast path constants.
220 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(this)) {
221 Res = CE->getValue();
225 if (!EvaluateAsRelocatable(Value, Layout) || !Value.isAbsolute()) {
226 // EvaluateAsAbsolute is defined to return the "current value" of
227 // the expression if we are given a Layout object, even in cases
228 // when the value is not fixed.
230 Res = Value.getConstant();
231 if (Value.getSymA()) {
232 Res += Layout->getSymbolAddress(
233 &Layout->getAssembler().getSymbolData(Value.getSymA()->getSymbol()));
235 if (Value.getSymB()) {
236 Res -= Layout->getSymbolAddress(
237 &Layout->getAssembler().getSymbolData(Value.getSymB()->getSymbol()));
243 Res = Value.getConstant();
247 static bool EvaluateSymbolicAdd(const MCAsmLayout *Layout, bool InSet,
248 const MCValue &LHS,const MCSymbolRefExpr *RHS_A,
249 const MCSymbolRefExpr *RHS_B, int64_t RHS_Cst,
251 // We can't add or subtract two symbols.
252 if ((LHS.getSymA() && RHS_A) ||
253 (LHS.getSymB() && RHS_B))
256 const MCSymbolRefExpr *A = LHS.getSymA() ? LHS.getSymA() : RHS_A;
257 const MCSymbolRefExpr *B = LHS.getSymB() ? LHS.getSymB() : RHS_B;
259 // If we have a negated symbol, then we must have also have a non-negated
260 // symbol in order to encode the expression. We can do this check later to
261 // permit expressions which eventually fold to a representable form -- such
262 // as (a + (0 - b)) -- if necessary.
267 // Absolutize symbol differences between defined symbols when we have a
268 // layout object and the target requests it.
270 if (Layout && A && B) {
271 const MCSymbol &SA = A->getSymbol();
272 const MCSymbol &SB = B->getSymbol();
273 const MCObjectFormat &F =
274 Layout->getAssembler().getBackend().getObjectFormat();
275 if (SA.isDefined() && SB.isDefined() && F.isAbsolute(InSet, SA, SB)) {
276 const MCAssembler &Asm = Layout->getAssembler();
277 MCSymbolData &AD = Asm.getSymbolData(A->getSymbol());
278 MCSymbolData &BD = Asm.getSymbolData(B->getSymbol());
279 Res = MCValue::get(+ Layout->getSymbolAddress(&AD)
280 - Layout->getSymbolAddress(&BD)
288 Res = MCValue::get(A, B, LHS.getConstant() + RHS_Cst);
292 bool MCExpr::EvaluateAsRelocatable(MCValue &Res,
293 const MCAsmLayout *Layout) const {
294 return EvaluateAsRelocatableImpl(Res, Layout, false);
297 bool MCExpr::EvaluateAsRelocatableImpl(MCValue &Res,
298 const MCAsmLayout *Layout,
300 ++stats::MCExprEvaluate;
304 return cast<MCTargetExpr>(this)->EvaluateAsRelocatableImpl(Res, Layout);
307 Res = MCValue::get(cast<MCConstantExpr>(this)->getValue());
311 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(this);
312 const MCSymbol &Sym = SRE->getSymbol();
314 // Evaluate recursively if this is a variable.
315 if (Sym.isVariable() && SRE->getKind() == MCSymbolRefExpr::VK_None)
316 return Sym.getVariableValue()->EvaluateAsRelocatableImpl(Res, Layout,
319 Res = MCValue::get(SRE, 0, 0);
324 const MCUnaryExpr *AUE = cast<MCUnaryExpr>(this);
327 if (!AUE->getSubExpr()->EvaluateAsRelocatableImpl(Value, Layout, InSet))
330 switch (AUE->getOpcode()) {
331 case MCUnaryExpr::LNot:
332 if (!Value.isAbsolute())
334 Res = MCValue::get(!Value.getConstant());
336 case MCUnaryExpr::Minus:
337 /// -(a - b + const) ==> (b - a - const)
338 if (Value.getSymA() && !Value.getSymB())
340 Res = MCValue::get(Value.getSymB(), Value.getSymA(),
341 -Value.getConstant());
343 case MCUnaryExpr::Not:
344 if (!Value.isAbsolute())
346 Res = MCValue::get(~Value.getConstant());
348 case MCUnaryExpr::Plus:
357 const MCBinaryExpr *ABE = cast<MCBinaryExpr>(this);
358 MCValue LHSValue, RHSValue;
360 if (!ABE->getLHS()->EvaluateAsRelocatableImpl(LHSValue, Layout, InSet) ||
361 !ABE->getRHS()->EvaluateAsRelocatableImpl(RHSValue, Layout, InSet))
364 // We only support a few operations on non-constant expressions, handle
366 if (!LHSValue.isAbsolute() || !RHSValue.isAbsolute()) {
367 switch (ABE->getOpcode()) {
370 case MCBinaryExpr::Sub:
371 // Negate RHS and add.
372 return EvaluateSymbolicAdd(Layout, InSet, LHSValue,
373 RHSValue.getSymB(), RHSValue.getSymA(),
374 -RHSValue.getConstant(),
377 case MCBinaryExpr::Add:
378 return EvaluateSymbolicAdd(Layout, InSet, LHSValue,
379 RHSValue.getSymA(), RHSValue.getSymB(),
380 RHSValue.getConstant(),
385 // FIXME: We need target hooks for the evaluation. It may be limited in
386 // width, and gas defines the result of comparisons and right shifts
387 // differently from Apple as.
388 int64_t LHS = LHSValue.getConstant(), RHS = RHSValue.getConstant();
390 switch (ABE->getOpcode()) {
391 case MCBinaryExpr::Add: Result = LHS + RHS; break;
392 case MCBinaryExpr::And: Result = LHS & RHS; break;
393 case MCBinaryExpr::Div: Result = LHS / RHS; break;
394 case MCBinaryExpr::EQ: Result = LHS == RHS; break;
395 case MCBinaryExpr::GT: Result = LHS > RHS; break;
396 case MCBinaryExpr::GTE: Result = LHS >= RHS; break;
397 case MCBinaryExpr::LAnd: Result = LHS && RHS; break;
398 case MCBinaryExpr::LOr: Result = LHS || RHS; break;
399 case MCBinaryExpr::LT: Result = LHS < RHS; break;
400 case MCBinaryExpr::LTE: Result = LHS <= RHS; break;
401 case MCBinaryExpr::Mod: Result = LHS % RHS; break;
402 case MCBinaryExpr::Mul: Result = LHS * RHS; break;
403 case MCBinaryExpr::NE: Result = LHS != RHS; break;
404 case MCBinaryExpr::Or: Result = LHS | RHS; break;
405 case MCBinaryExpr::Shl: Result = LHS << RHS; break;
406 case MCBinaryExpr::Shr: Result = LHS >> RHS; break;
407 case MCBinaryExpr::Sub: Result = LHS - RHS; break;
408 case MCBinaryExpr::Xor: Result = LHS ^ RHS; break;
411 Res = MCValue::get(Result);
416 assert(0 && "Invalid assembly expression kind!");