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();
42 // Parenthesize names that start with $ so that they don't look like
44 bool UseParens = Sym.getName()[0] == '$';
46 if (SRE.getKind() == MCSymbolRefExpr::VK_ARM_HI16 ||
47 SRE.getKind() == MCSymbolRefExpr::VK_ARM_LO16)
48 OS << MCSymbolRefExpr::getVariantKindName(SRE.getKind());
50 if (SRE.getKind() == MCSymbolRefExpr::VK_PPC_HA16 ||
51 SRE.getKind() == MCSymbolRefExpr::VK_PPC_LO16) {
52 OS << MCSymbolRefExpr::getVariantKindName(SRE.getKind());
57 OS << '(' << Sym << ')';
61 if (SRE.getKind() == MCSymbolRefExpr::VK_ARM_PLT ||
62 SRE.getKind() == MCSymbolRefExpr::VK_ARM_TLSGD ||
63 SRE.getKind() == MCSymbolRefExpr::VK_ARM_GOT ||
64 SRE.getKind() == MCSymbolRefExpr::VK_ARM_GOTOFF ||
65 SRE.getKind() == MCSymbolRefExpr::VK_ARM_TPOFF ||
66 SRE.getKind() == MCSymbolRefExpr::VK_ARM_GOTTPOFF)
67 OS << MCSymbolRefExpr::getVariantKindName(SRE.getKind());
68 else if (SRE.getKind() != MCSymbolRefExpr::VK_None &&
69 SRE.getKind() != MCSymbolRefExpr::VK_ARM_HI16 &&
70 SRE.getKind() != MCSymbolRefExpr::VK_ARM_LO16 &&
71 SRE.getKind() != MCSymbolRefExpr::VK_PPC_HA16 &&
72 SRE.getKind() != MCSymbolRefExpr::VK_PPC_LO16)
73 OS << '@' << MCSymbolRefExpr::getVariantKindName(SRE.getKind());
79 const MCUnaryExpr &UE = cast<MCUnaryExpr>(*this);
80 switch (UE.getOpcode()) {
81 default: assert(0 && "Invalid opcode!");
82 case MCUnaryExpr::LNot: OS << '!'; break;
83 case MCUnaryExpr::Minus: OS << '-'; break;
84 case MCUnaryExpr::Not: OS << '~'; break;
85 case MCUnaryExpr::Plus: OS << '+'; break;
87 OS << *UE.getSubExpr();
91 case MCExpr::Binary: {
92 const MCBinaryExpr &BE = cast<MCBinaryExpr>(*this);
94 // Only print parens around the LHS if it is non-trivial.
95 if (isa<MCConstantExpr>(BE.getLHS()) || isa<MCSymbolRefExpr>(BE.getLHS())) {
98 OS << '(' << *BE.getLHS() << ')';
101 switch (BE.getOpcode()) {
102 default: assert(0 && "Invalid opcode!");
103 case MCBinaryExpr::Add:
104 // Print "X-42" instead of "X+-42".
105 if (const MCConstantExpr *RHSC = dyn_cast<MCConstantExpr>(BE.getRHS())) {
106 if (RHSC->getValue() < 0) {
107 OS << RHSC->getValue();
114 case MCBinaryExpr::And: OS << '&'; break;
115 case MCBinaryExpr::Div: OS << '/'; break;
116 case MCBinaryExpr::EQ: OS << "=="; break;
117 case MCBinaryExpr::GT: OS << '>'; break;
118 case MCBinaryExpr::GTE: OS << ">="; break;
119 case MCBinaryExpr::LAnd: OS << "&&"; break;
120 case MCBinaryExpr::LOr: OS << "||"; break;
121 case MCBinaryExpr::LT: OS << '<'; break;
122 case MCBinaryExpr::LTE: OS << "<="; break;
123 case MCBinaryExpr::Mod: OS << '%'; break;
124 case MCBinaryExpr::Mul: OS << '*'; break;
125 case MCBinaryExpr::NE: OS << "!="; break;
126 case MCBinaryExpr::Or: OS << '|'; break;
127 case MCBinaryExpr::Shl: OS << "<<"; break;
128 case MCBinaryExpr::Shr: OS << ">>"; break;
129 case MCBinaryExpr::Sub: OS << '-'; break;
130 case MCBinaryExpr::Xor: OS << '^'; break;
133 // Only print parens around the LHS if it is non-trivial.
134 if (isa<MCConstantExpr>(BE.getRHS()) || isa<MCSymbolRefExpr>(BE.getRHS())) {
137 OS << '(' << *BE.getRHS() << ')';
143 assert(0 && "Invalid expression kind!");
146 void MCExpr::dump() const {
153 const MCBinaryExpr *MCBinaryExpr::Create(Opcode Opc, const MCExpr *LHS,
154 const MCExpr *RHS, MCContext &Ctx) {
155 return new (Ctx) MCBinaryExpr(Opc, LHS, RHS);
158 const MCUnaryExpr *MCUnaryExpr::Create(Opcode Opc, const MCExpr *Expr,
160 return new (Ctx) MCUnaryExpr(Opc, Expr);
163 const MCConstantExpr *MCConstantExpr::Create(int64_t Value, MCContext &Ctx) {
164 return new (Ctx) MCConstantExpr(Value);
169 const MCSymbolRefExpr *MCSymbolRefExpr::Create(const MCSymbol *Sym,
172 return new (Ctx) MCSymbolRefExpr(Sym, Kind);
175 const MCSymbolRefExpr *MCSymbolRefExpr::Create(StringRef Name, VariantKind Kind,
177 return Create(Ctx.GetOrCreateSymbol(Name), Kind, Ctx);
180 StringRef MCSymbolRefExpr::getVariantKindName(VariantKind Kind) {
183 case VK_Invalid: return "<<invalid>>";
184 case VK_None: return "<<none>>";
186 case VK_GOT: return "GOT";
187 case VK_GOTOFF: return "GOTOFF";
188 case VK_GOTPCREL: return "GOTPCREL";
189 case VK_GOTTPOFF: return "GOTTPOFF";
190 case VK_INDNTPOFF: return "INDNTPOFF";
191 case VK_NTPOFF: return "NTPOFF";
192 case VK_GOTNTPOFF: return "GOTNTPOFF";
193 case VK_PLT: return "PLT";
194 case VK_TLSGD: return "TLSGD";
195 case VK_TLSLD: return "TLSLD";
196 case VK_TLSLDM: return "TLSLDM";
197 case VK_TPOFF: return "TPOFF";
198 case VK_DTPOFF: return "DTPOFF";
199 case VK_TLVP: return "TLVP";
200 case VK_ARM_HI16: return ":upper16:";
201 case VK_ARM_LO16: return ":lower16:";
202 case VK_ARM_PLT: return "(PLT)";
203 case VK_ARM_GOT: return "(GOT)";
204 case VK_ARM_GOTOFF: return "(GOTOFF)";
205 case VK_ARM_TPOFF: return "(tpoff)";
206 case VK_ARM_GOTTPOFF: return "(gottpoff)";
207 case VK_ARM_TLSGD: return "(tlsgd)";
208 case VK_PPC_TOC: return "toc";
209 case VK_PPC_HA16: return "ha16";
210 case VK_PPC_LO16: return "lo16";
214 MCSymbolRefExpr::VariantKind
215 MCSymbolRefExpr::getVariantKindForName(StringRef Name) {
216 return StringSwitch<VariantKind>(Name)
218 .Case("GOTOFF", VK_GOTOFF)
219 .Case("GOTPCREL", VK_GOTPCREL)
220 .Case("GOTTPOFF", VK_GOTTPOFF)
221 .Case("INDNTPOFF", VK_INDNTPOFF)
222 .Case("NTPOFF", VK_NTPOFF)
223 .Case("GOTNTPOFF", VK_GOTNTPOFF)
225 .Case("TLSGD", VK_TLSGD)
226 .Case("TLSLD", VK_TLSLD)
227 .Case("TLSLDM", VK_TLSLDM)
228 .Case("TPOFF", VK_TPOFF)
229 .Case("DTPOFF", VK_DTPOFF)
230 .Case("TLVP", VK_TLVP)
231 .Default(VK_Invalid);
236 void MCTargetExpr::Anchor() {}
240 bool MCExpr::EvaluateAsAbsolute(int64_t &Res) const {
241 return EvaluateAsAbsolute(Res, 0, 0, 0);
244 bool MCExpr::EvaluateAsAbsolute(int64_t &Res,
245 const MCAsmLayout &Layout) const {
246 return EvaluateAsAbsolute(Res, &Layout.getAssembler(), &Layout, 0);
249 bool MCExpr::EvaluateAsAbsolute(int64_t &Res,
250 const MCAsmLayout &Layout,
251 const SectionAddrMap &Addrs) const {
252 return EvaluateAsAbsolute(Res, &Layout.getAssembler(), &Layout, &Addrs);
255 bool MCExpr::EvaluateAsAbsolute(int64_t &Res, const MCAssembler &Asm) const {
256 return EvaluateAsAbsolute(Res, &Asm, 0, 0);
259 bool MCExpr::EvaluateAsAbsolute(int64_t &Res, const MCAssembler *Asm,
260 const MCAsmLayout *Layout,
261 const SectionAddrMap *Addrs) const {
264 // Fast path constants.
265 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(this)) {
266 Res = CE->getValue();
270 if (!EvaluateAsRelocatableImpl(Value, Asm, Layout, Addrs, Addrs) ||
271 !Value.isAbsolute()) {
272 // EvaluateAsAbsolute is defined to return the "current value" of
273 // the expression if we are given a Layout object, even in cases
274 // when the value is not fixed.
276 Res = Value.getConstant();
277 if (Value.getSymA()) {
278 Res += Layout->getSymbolOffset(
279 &Layout->getAssembler().getSymbolData(Value.getSymA()->getSymbol()));
281 if (Value.getSymB()) {
282 Res -= Layout->getSymbolOffset(
283 &Layout->getAssembler().getSymbolData(Value.getSymB()->getSymbol()));
289 Res = Value.getConstant();
293 /// \brief Evaluate the result of an add between (conceptually) two MCValues.
295 /// This routine conceptually attempts to construct an MCValue:
296 /// Result = (Result_A - Result_B + Result_Cst)
297 /// from two MCValue's LHS and RHS where
298 /// Result = LHS + RHS
300 /// Result = (LHS_A - LHS_B + LHS_Cst) + (RHS_A - RHS_B + RHS_Cst).
302 /// This routine attempts to aggresively fold the operands such that the result
303 /// is representable in an MCValue, but may not always succeed.
305 /// \returns True on success, false if the result is not representable in an
307 static bool EvaluateSymbolicAdd(const MCAssembler *Asm,
308 const MCAsmLayout *Layout,
309 const SectionAddrMap *Addrs,
311 const MCValue &LHS,const MCSymbolRefExpr *RHS_A,
312 const MCSymbolRefExpr *RHS_B, int64_t RHS_Cst,
314 // We can't add or subtract two symbols.
315 if ((LHS.getSymA() && RHS_A) ||
316 (LHS.getSymB() && RHS_B))
319 const MCSymbolRefExpr *A = LHS.getSymA() ? LHS.getSymA() : RHS_A;
320 const MCSymbolRefExpr *B = LHS.getSymB() ? LHS.getSymB() : RHS_B;
322 // If we have a negated symbol, then we must have also have a non-negated
323 // symbol in order to encode the expression. We can do this check later to
324 // permit expressions which eventually fold to a representable form -- such
325 // as (a + (0 - b)) -- if necessary.
330 // Absolutize symbol differences between defined symbols when we have a
331 // layout object and the target requests it.
333 assert(!(Layout && !Asm));
336 const MCSymbol &SA = A->getSymbol();
337 const MCSymbol &SB = B->getSymbol();
338 const MCObjectFormat &F = Asm->getBackend().getObjectFormat();
339 if (SA.isDefined() && SB.isDefined() && F.isAbsolute(InSet, SA, SB)) {
340 MCSymbolData &AD = Asm->getSymbolData(A->getSymbol());
341 MCSymbolData &BD = Asm->getSymbolData(B->getSymbol());
343 if (AD.getFragment() == BD.getFragment()) {
344 Res = MCValue::get(+ AD.getOffset()
352 const MCSectionData &SecA = *AD.getFragment()->getParent();
353 const MCSectionData &SecB = *BD.getFragment()->getParent();
354 int64_t Val = + Layout->getSymbolOffset(&AD)
355 - Layout->getSymbolOffset(&BD)
358 if (&SecA != &SecB) {
361 Val += Addrs->lookup(&SecA);
362 Val -= Addrs->lookup(&SecB);
364 Res = MCValue::get(Val);
370 Res = MCValue::get(A, B, LHS.getConstant() + RHS_Cst);
374 bool MCExpr::EvaluateAsRelocatable(MCValue &Res,
375 const MCAsmLayout *Layout) const {
377 return EvaluateAsRelocatableImpl(Res, &Layout->getAssembler(), Layout,
380 return EvaluateAsRelocatableImpl(Res, 0, 0, 0, false);
383 bool MCExpr::EvaluateAsRelocatableImpl(MCValue &Res,
384 const MCAssembler *Asm,
385 const MCAsmLayout *Layout,
386 const SectionAddrMap *Addrs,
388 ++stats::MCExprEvaluate;
392 return cast<MCTargetExpr>(this)->EvaluateAsRelocatableImpl(Res, Layout);
395 Res = MCValue::get(cast<MCConstantExpr>(this)->getValue());
399 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(this);
400 const MCSymbol &Sym = SRE->getSymbol();
402 // Evaluate recursively if this is a variable.
403 if (Sym.isVariable() && SRE->getKind() == MCSymbolRefExpr::VK_None) {
404 bool Ret = Sym.getVariableValue()->EvaluateAsRelocatableImpl(Res, Asm,
408 // If we failed to simplify this to a constant, let the target
410 if (Ret && !Res.getSymA() && !Res.getSymB())
414 Res = MCValue::get(SRE, 0, 0);
419 const MCUnaryExpr *AUE = cast<MCUnaryExpr>(this);
422 if (!AUE->getSubExpr()->EvaluateAsRelocatableImpl(Value, Asm, Layout,
426 switch (AUE->getOpcode()) {
427 case MCUnaryExpr::LNot:
428 if (!Value.isAbsolute())
430 Res = MCValue::get(!Value.getConstant());
432 case MCUnaryExpr::Minus:
433 /// -(a - b + const) ==> (b - a - const)
434 if (Value.getSymA() && !Value.getSymB())
436 Res = MCValue::get(Value.getSymB(), Value.getSymA(),
437 -Value.getConstant());
439 case MCUnaryExpr::Not:
440 if (!Value.isAbsolute())
442 Res = MCValue::get(~Value.getConstant());
444 case MCUnaryExpr::Plus:
453 const MCBinaryExpr *ABE = cast<MCBinaryExpr>(this);
454 MCValue LHSValue, RHSValue;
456 if (!ABE->getLHS()->EvaluateAsRelocatableImpl(LHSValue, Asm, Layout,
458 !ABE->getRHS()->EvaluateAsRelocatableImpl(RHSValue, Asm, Layout,
462 // We only support a few operations on non-constant expressions, handle
464 if (!LHSValue.isAbsolute() || !RHSValue.isAbsolute()) {
465 switch (ABE->getOpcode()) {
468 case MCBinaryExpr::Sub:
469 // Negate RHS and add.
470 return EvaluateSymbolicAdd(Asm, Layout, Addrs, InSet, LHSValue,
471 RHSValue.getSymB(), RHSValue.getSymA(),
472 -RHSValue.getConstant(),
475 case MCBinaryExpr::Add:
476 return EvaluateSymbolicAdd(Asm, Layout, Addrs, InSet, LHSValue,
477 RHSValue.getSymA(), RHSValue.getSymB(),
478 RHSValue.getConstant(),
483 // FIXME: We need target hooks for the evaluation. It may be limited in
484 // width, and gas defines the result of comparisons and right shifts
485 // differently from Apple as.
486 int64_t LHS = LHSValue.getConstant(), RHS = RHSValue.getConstant();
488 switch (ABE->getOpcode()) {
489 case MCBinaryExpr::Add: Result = LHS + RHS; break;
490 case MCBinaryExpr::And: Result = LHS & RHS; break;
491 case MCBinaryExpr::Div: Result = LHS / RHS; break;
492 case MCBinaryExpr::EQ: Result = LHS == RHS; break;
493 case MCBinaryExpr::GT: Result = LHS > RHS; break;
494 case MCBinaryExpr::GTE: Result = LHS >= RHS; break;
495 case MCBinaryExpr::LAnd: Result = LHS && RHS; break;
496 case MCBinaryExpr::LOr: Result = LHS || RHS; break;
497 case MCBinaryExpr::LT: Result = LHS < RHS; break;
498 case MCBinaryExpr::LTE: Result = LHS <= RHS; break;
499 case MCBinaryExpr::Mod: Result = LHS % RHS; break;
500 case MCBinaryExpr::Mul: Result = LHS * RHS; break;
501 case MCBinaryExpr::NE: Result = LHS != RHS; break;
502 case MCBinaryExpr::Or: Result = LHS | RHS; break;
503 case MCBinaryExpr::Shl: Result = LHS << RHS; break;
504 case MCBinaryExpr::Shr: Result = LHS >> RHS; break;
505 case MCBinaryExpr::Sub: Result = LHS - RHS; break;
506 case MCBinaryExpr::Xor: Result = LHS ^ RHS; break;
509 Res = MCValue::get(Result);
514 assert(0 && "Invalid assembly expression kind!");