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/MCSymbol.h"
18 #include "llvm/MC/MCValue.h"
19 #include "llvm/Support/Debug.h"
20 #include "llvm/Support/raw_ostream.h"
21 #include "llvm/Target/TargetAsmBackend.h"
26 STATISTIC(MCExprEvaluate, "Number of MCExpr evaluations");
30 void MCExpr::print(raw_ostream &OS) const {
33 return cast<MCTargetExpr>(this)->PrintImpl(OS);
34 case MCExpr::Constant:
35 OS << cast<MCConstantExpr>(*this).getValue();
38 case MCExpr::SymbolRef: {
39 const MCSymbolRefExpr &SRE = cast<MCSymbolRefExpr>(*this);
40 const MCSymbol &Sym = SRE.getSymbol();
41 // Parenthesize names that start with $ so that they don't look like
43 bool UseParens = Sym.getName()[0] == '$';
45 if (SRE.getKind() == MCSymbolRefExpr::VK_ARM_HI16 ||
46 SRE.getKind() == MCSymbolRefExpr::VK_ARM_LO16)
47 OS << MCSymbolRefExpr::getVariantKindName(SRE.getKind());
49 if (SRE.getKind() == MCSymbolRefExpr::VK_PPC_HA16 ||
50 SRE.getKind() == MCSymbolRefExpr::VK_PPC_LO16) {
51 OS << MCSymbolRefExpr::getVariantKindName(SRE.getKind());
56 OS << '(' << Sym << ')';
60 if (SRE.getKind() == MCSymbolRefExpr::VK_ARM_PLT ||
61 SRE.getKind() == MCSymbolRefExpr::VK_ARM_TLSGD ||
62 SRE.getKind() == MCSymbolRefExpr::VK_ARM_GOT ||
63 SRE.getKind() == MCSymbolRefExpr::VK_ARM_GOTOFF ||
64 SRE.getKind() == MCSymbolRefExpr::VK_ARM_TPOFF ||
65 SRE.getKind() == MCSymbolRefExpr::VK_ARM_GOTTPOFF)
66 OS << MCSymbolRefExpr::getVariantKindName(SRE.getKind());
67 else if (SRE.getKind() != MCSymbolRefExpr::VK_None &&
68 SRE.getKind() != MCSymbolRefExpr::VK_ARM_HI16 &&
69 SRE.getKind() != MCSymbolRefExpr::VK_ARM_LO16 &&
70 SRE.getKind() != MCSymbolRefExpr::VK_PPC_HA16 &&
71 SRE.getKind() != MCSymbolRefExpr::VK_PPC_LO16)
72 OS << '@' << MCSymbolRefExpr::getVariantKindName(SRE.getKind());
78 const MCUnaryExpr &UE = cast<MCUnaryExpr>(*this);
79 switch (UE.getOpcode()) {
80 default: assert(0 && "Invalid opcode!");
81 case MCUnaryExpr::LNot: OS << '!'; break;
82 case MCUnaryExpr::Minus: OS << '-'; break;
83 case MCUnaryExpr::Not: OS << '~'; break;
84 case MCUnaryExpr::Plus: OS << '+'; break;
86 OS << *UE.getSubExpr();
90 case MCExpr::Binary: {
91 const MCBinaryExpr &BE = cast<MCBinaryExpr>(*this);
93 // Only print parens around the LHS if it is non-trivial.
94 if (isa<MCConstantExpr>(BE.getLHS()) || isa<MCSymbolRefExpr>(BE.getLHS())) {
97 OS << '(' << *BE.getLHS() << ')';
100 switch (BE.getOpcode()) {
101 default: assert(0 && "Invalid opcode!");
102 case MCBinaryExpr::Add:
103 // Print "X-42" instead of "X+-42".
104 if (const MCConstantExpr *RHSC = dyn_cast<MCConstantExpr>(BE.getRHS())) {
105 if (RHSC->getValue() < 0) {
106 OS << RHSC->getValue();
113 case MCBinaryExpr::And: OS << '&'; break;
114 case MCBinaryExpr::Div: OS << '/'; break;
115 case MCBinaryExpr::EQ: OS << "=="; break;
116 case MCBinaryExpr::GT: OS << '>'; break;
117 case MCBinaryExpr::GTE: OS << ">="; break;
118 case MCBinaryExpr::LAnd: OS << "&&"; break;
119 case MCBinaryExpr::LOr: OS << "||"; break;
120 case MCBinaryExpr::LT: OS << '<'; break;
121 case MCBinaryExpr::LTE: OS << "<="; break;
122 case MCBinaryExpr::Mod: OS << '%'; break;
123 case MCBinaryExpr::Mul: OS << '*'; break;
124 case MCBinaryExpr::NE: OS << "!="; break;
125 case MCBinaryExpr::Or: OS << '|'; break;
126 case MCBinaryExpr::Shl: OS << "<<"; break;
127 case MCBinaryExpr::Shr: OS << ">>"; break;
128 case MCBinaryExpr::Sub: OS << '-'; break;
129 case MCBinaryExpr::Xor: OS << '^'; break;
132 // Only print parens around the LHS if it is non-trivial.
133 if (isa<MCConstantExpr>(BE.getRHS()) || isa<MCSymbolRefExpr>(BE.getRHS())) {
136 OS << '(' << *BE.getRHS() << ')';
142 assert(0 && "Invalid expression kind!");
145 void MCExpr::dump() const {
152 const MCBinaryExpr *MCBinaryExpr::Create(Opcode Opc, const MCExpr *LHS,
153 const MCExpr *RHS, MCContext &Ctx) {
154 return new (Ctx) MCBinaryExpr(Opc, LHS, RHS);
157 const MCUnaryExpr *MCUnaryExpr::Create(Opcode Opc, const MCExpr *Expr,
159 return new (Ctx) MCUnaryExpr(Opc, Expr);
162 const MCConstantExpr *MCConstantExpr::Create(int64_t Value, MCContext &Ctx) {
163 return new (Ctx) MCConstantExpr(Value);
168 const MCSymbolRefExpr *MCSymbolRefExpr::Create(const MCSymbol *Sym,
171 return new (Ctx) MCSymbolRefExpr(Sym, Kind);
174 const MCSymbolRefExpr *MCSymbolRefExpr::Create(StringRef Name, VariantKind Kind,
176 return Create(Ctx.GetOrCreateSymbol(Name), Kind, Ctx);
179 StringRef MCSymbolRefExpr::getVariantKindName(VariantKind Kind) {
182 case VK_Invalid: return "<<invalid>>";
183 case VK_None: return "<<none>>";
185 case VK_GOT: return "GOT";
186 case VK_GOTOFF: return "GOTOFF";
187 case VK_GOTPCREL: return "GOTPCREL";
188 case VK_GOTTPOFF: return "GOTTPOFF";
189 case VK_INDNTPOFF: return "INDNTPOFF";
190 case VK_NTPOFF: return "NTPOFF";
191 case VK_GOTNTPOFF: return "GOTNTPOFF";
192 case VK_PLT: return "PLT";
193 case VK_TLSGD: return "TLSGD";
194 case VK_TLSLD: return "TLSLD";
195 case VK_TLSLDM: return "TLSLDM";
196 case VK_TPOFF: return "TPOFF";
197 case VK_DTPOFF: return "DTPOFF";
198 case VK_TLVP: return "TLVP";
199 case VK_ARM_HI16: return ":upper16:";
200 case VK_ARM_LO16: return ":lower16:";
201 case VK_ARM_PLT: return "(PLT)";
202 case VK_ARM_GOT: return "(GOT)";
203 case VK_ARM_GOTOFF: return "(GOTOFF)";
204 case VK_ARM_TPOFF: return "(tpoff)";
205 case VK_ARM_GOTTPOFF: return "(gottpoff)";
206 case VK_ARM_TLSGD: return "(tlsgd)";
207 case VK_PPC_TOC: return "toc";
208 case VK_PPC_HA16: return "ha16";
209 case VK_PPC_LO16: return "lo16";
213 MCSymbolRefExpr::VariantKind
214 MCSymbolRefExpr::getVariantKindForName(StringRef Name) {
215 return StringSwitch<VariantKind>(Name)
217 .Case("GOTOFF", VK_GOTOFF)
218 .Case("GOTPCREL", VK_GOTPCREL)
219 .Case("GOTTPOFF", VK_GOTTPOFF)
220 .Case("INDNTPOFF", VK_INDNTPOFF)
221 .Case("NTPOFF", VK_NTPOFF)
222 .Case("GOTNTPOFF", VK_GOTNTPOFF)
224 .Case("TLSGD", VK_TLSGD)
225 .Case("TLSLD", VK_TLSLD)
226 .Case("TLSLDM", VK_TLSLDM)
227 .Case("TPOFF", VK_TPOFF)
228 .Case("DTPOFF", VK_DTPOFF)
229 .Case("TLVP", VK_TLVP)
230 .Default(VK_Invalid);
235 void MCTargetExpr::Anchor() {}
239 bool MCExpr::EvaluateAsAbsolute(int64_t &Res) const {
240 return EvaluateAsAbsolute(Res, 0, 0, 0);
243 bool MCExpr::EvaluateAsAbsolute(int64_t &Res,
244 const MCAsmLayout &Layout) const {
245 return EvaluateAsAbsolute(Res, &Layout.getAssembler(), &Layout, 0);
248 bool MCExpr::EvaluateAsAbsolute(int64_t &Res,
249 const MCAsmLayout &Layout,
250 const SectionAddrMap &Addrs) const {
251 return EvaluateAsAbsolute(Res, &Layout.getAssembler(), &Layout, &Addrs);
254 bool MCExpr::EvaluateAsAbsolute(int64_t &Res, const MCAssembler &Asm) const {
255 return EvaluateAsAbsolute(Res, &Asm, 0, 0);
258 bool MCExpr::EvaluateAsAbsolute(int64_t &Res, const MCAssembler *Asm,
259 const MCAsmLayout *Layout,
260 const SectionAddrMap *Addrs) const {
263 // Fast path constants.
264 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(this)) {
265 Res = CE->getValue();
269 // FIXME: The use if InSet = Addrs is a hack. Setting InSet causes us
270 // absolutize differences across sections and that is what the MachO writer
272 if (!EvaluateAsRelocatableImpl(Value, Asm, Layout, Addrs, /*InSet*/ Addrs) ||
273 !Value.isAbsolute()) {
274 // EvaluateAsAbsolute is defined to return the "current value" of
275 // the expression if we are given a Layout object, even in cases
276 // when the value is not fixed.
278 Res = Value.getConstant();
279 if (Value.getSymA()) {
280 Res += Layout->getSymbolOffset(
281 &Layout->getAssembler().getSymbolData(Value.getSymA()->getSymbol()));
283 if (Value.getSymB()) {
284 Res -= Layout->getSymbolOffset(
285 &Layout->getAssembler().getSymbolData(Value.getSymB()->getSymbol()));
291 Res = Value.getConstant();
295 /// \brief Helper method for \see EvaluateSymbolAdd().
296 static void AttemptToFoldSymbolOffsetDifference(const MCAsmLayout *Layout,
297 const MCSymbolRefExpr *&A,
298 const MCSymbolRefExpr *&B,
300 const MCAssembler &Asm = Layout->getAssembler();
303 Asm.getWriter().IsSymbolRefDifferenceFullyResolved(Asm, A, B, false)) {
305 Addend += (Layout->getSymbolOffset(&Asm.getSymbolData(A->getSymbol())) -
306 Layout->getSymbolOffset(&Asm.getSymbolData(B->getSymbol())));
308 // Clear the symbol expr pointers to indicate we have folded these
314 /// \brief Evaluate the result of an add between (conceptually) two MCValues.
316 /// This routine conceptually attempts to construct an MCValue:
317 /// Result = (Result_A - Result_B + Result_Cst)
318 /// from two MCValue's LHS and RHS where
319 /// Result = LHS + RHS
321 /// Result = (LHS_A - LHS_B + LHS_Cst) + (RHS_A - RHS_B + RHS_Cst).
323 /// This routine attempts to aggresively fold the operands such that the result
324 /// is representable in an MCValue, but may not always succeed.
326 /// \returns True on success, false if the result is not representable in an
329 /// NOTE: It is really important to have both the Asm and Layout arguments.
330 /// They might look redundant, but this function can be used before layout
331 /// is done (see the object streamer for example) and having the Asm argument
332 /// lets us avoid relaxations early.
333 static bool EvaluateSymbolicAdd(const MCAssembler *Asm,
334 const MCAsmLayout *Layout,
335 const SectionAddrMap *Addrs,
337 const MCValue &LHS,const MCSymbolRefExpr *RHS_A,
338 const MCSymbolRefExpr *RHS_B, int64_t RHS_Cst,
340 // FIXME: This routine (and other evaluation parts) are *incredibly* sloppy
341 // about dealing with modifiers. This will ultimately bite us, one day.
342 const MCSymbolRefExpr *LHS_A = LHS.getSymA();
343 const MCSymbolRefExpr *LHS_B = LHS.getSymB();
344 int64_t LHS_Cst = LHS.getConstant();
346 // Fold the result constant immediately.
347 int64_t Result_Cst = LHS_Cst + RHS_Cst;
349 // If we have a layout, we can fold resolved differences.
351 // First, fold out any differences which are fully resolved. By
352 // reassociating terms in
353 // Result = (LHS_A - LHS_B + LHS_Cst) + (RHS_A - RHS_B + RHS_Cst).
354 // we have the four possible differences:
359 // Since we are attempting to be as aggresive as possible about folding, we
360 // attempt to evaluate each possible alternative.
361 AttemptToFoldSymbolOffsetDifference(Layout, LHS_A, LHS_B, Result_Cst);
362 AttemptToFoldSymbolOffsetDifference(Layout, LHS_A, RHS_B, Result_Cst);
363 AttemptToFoldSymbolOffsetDifference(Layout, RHS_A, LHS_B, Result_Cst);
364 AttemptToFoldSymbolOffsetDifference(Layout, RHS_A, RHS_B, Result_Cst);
367 // We can't represent the addition or subtraction of two symbols.
368 if ((LHS_A && RHS_A) || (LHS_B && RHS_B))
371 // At this point, we have at most one additive symbol and one subtractive
372 // symbol -- find them.
373 const MCSymbolRefExpr *A = LHS_A ? LHS_A : RHS_A;
374 const MCSymbolRefExpr *B = LHS_B ? LHS_B : RHS_B;
376 // If we have a negated symbol, then we must have also have a non-negated
377 // symbol in order to encode the expression.
381 // Absolutize symbol differences between defined symbols when we have a
382 // layout object and the target requests it.
384 assert((!Layout || Asm) &&
385 "Must have an assembler object if layout is given!");
388 if (A->getSymbol().isDefined() && B->getSymbol().isDefined() &&
389 Asm->getWriter().IsSymbolRefDifferenceFullyResolved(*Asm, A, B,
391 MCSymbolData &AD = Asm->getSymbolData(A->getSymbol());
392 MCSymbolData &BD = Asm->getSymbolData(B->getSymbol());
394 if (AD.getFragment() == BD.getFragment()) {
395 Res = MCValue::get(+ AD.getOffset()
402 const MCSectionData &SecA = *AD.getFragment()->getParent();
403 const MCSectionData &SecB = *BD.getFragment()->getParent();
404 int64_t Val = + Layout->getSymbolOffset(&AD)
405 - Layout->getSymbolOffset(&BD)
407 if (&SecA != &SecB) {
410 Val += Addrs->lookup(&SecA);
411 Val -= Addrs->lookup(&SecB);
413 Res = MCValue::get(Val);
419 Res = MCValue::get(A, B, Result_Cst);
423 bool MCExpr::EvaluateAsRelocatable(MCValue &Res,
424 const MCAsmLayout *Layout) const {
426 return EvaluateAsRelocatableImpl(Res, &Layout->getAssembler(), Layout,
429 return EvaluateAsRelocatableImpl(Res, 0, 0, 0, false);
432 bool MCExpr::EvaluateAsRelocatableImpl(MCValue &Res,
433 const MCAssembler *Asm,
434 const MCAsmLayout *Layout,
435 const SectionAddrMap *Addrs,
437 ++stats::MCExprEvaluate;
441 return cast<MCTargetExpr>(this)->EvaluateAsRelocatableImpl(Res, Layout);
444 Res = MCValue::get(cast<MCConstantExpr>(this)->getValue());
448 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(this);
449 const MCSymbol &Sym = SRE->getSymbol();
451 // Evaluate recursively if this is a variable.
452 if (Sym.isVariable() && SRE->getKind() == MCSymbolRefExpr::VK_None) {
453 bool Ret = Sym.getVariableValue()->EvaluateAsRelocatableImpl(Res, Asm,
457 // If we failed to simplify this to a constant, let the target
459 if (Ret && !Res.getSymA() && !Res.getSymB())
463 Res = MCValue::get(SRE, 0, 0);
468 const MCUnaryExpr *AUE = cast<MCUnaryExpr>(this);
471 if (!AUE->getSubExpr()->EvaluateAsRelocatableImpl(Value, Asm, Layout,
475 switch (AUE->getOpcode()) {
476 case MCUnaryExpr::LNot:
477 if (!Value.isAbsolute())
479 Res = MCValue::get(!Value.getConstant());
481 case MCUnaryExpr::Minus:
482 /// -(a - b + const) ==> (b - a - const)
483 if (Value.getSymA() && !Value.getSymB())
485 Res = MCValue::get(Value.getSymB(), Value.getSymA(),
486 -Value.getConstant());
488 case MCUnaryExpr::Not:
489 if (!Value.isAbsolute())
491 Res = MCValue::get(~Value.getConstant());
493 case MCUnaryExpr::Plus:
502 const MCBinaryExpr *ABE = cast<MCBinaryExpr>(this);
503 MCValue LHSValue, RHSValue;
505 if (!ABE->getLHS()->EvaluateAsRelocatableImpl(LHSValue, Asm, Layout,
507 !ABE->getRHS()->EvaluateAsRelocatableImpl(RHSValue, Asm, Layout,
511 // We only support a few operations on non-constant expressions, handle
513 if (!LHSValue.isAbsolute() || !RHSValue.isAbsolute()) {
514 switch (ABE->getOpcode()) {
517 case MCBinaryExpr::Sub:
518 // Negate RHS and add.
519 return EvaluateSymbolicAdd(Asm, Layout, Addrs, InSet, LHSValue,
520 RHSValue.getSymB(), RHSValue.getSymA(),
521 -RHSValue.getConstant(),
524 case MCBinaryExpr::Add:
525 return EvaluateSymbolicAdd(Asm, Layout, Addrs, InSet, LHSValue,
526 RHSValue.getSymA(), RHSValue.getSymB(),
527 RHSValue.getConstant(),
532 // FIXME: We need target hooks for the evaluation. It may be limited in
533 // width, and gas defines the result of comparisons and right shifts
534 // differently from Apple as.
535 int64_t LHS = LHSValue.getConstant(), RHS = RHSValue.getConstant();
537 switch (ABE->getOpcode()) {
538 case MCBinaryExpr::Add: Result = LHS + RHS; break;
539 case MCBinaryExpr::And: Result = LHS & RHS; break;
540 case MCBinaryExpr::Div: Result = LHS / RHS; break;
541 case MCBinaryExpr::EQ: Result = LHS == RHS; break;
542 case MCBinaryExpr::GT: Result = LHS > RHS; break;
543 case MCBinaryExpr::GTE: Result = LHS >= RHS; break;
544 case MCBinaryExpr::LAnd: Result = LHS && RHS; break;
545 case MCBinaryExpr::LOr: Result = LHS || RHS; break;
546 case MCBinaryExpr::LT: Result = LHS < RHS; break;
547 case MCBinaryExpr::LTE: Result = LHS <= RHS; break;
548 case MCBinaryExpr::Mod: Result = LHS % RHS; break;
549 case MCBinaryExpr::Mul: Result = LHS * RHS; break;
550 case MCBinaryExpr::NE: Result = LHS != RHS; break;
551 case MCBinaryExpr::Or: Result = LHS | RHS; break;
552 case MCBinaryExpr::Shl: Result = LHS << RHS; break;
553 case MCBinaryExpr::Shr: Result = LHS >> RHS; break;
554 case MCBinaryExpr::Sub: Result = LHS - RHS; break;
555 case MCBinaryExpr::Xor: Result = LHS ^ RHS; break;
558 Res = MCValue::get(Result);
563 assert(0 && "Invalid assembly expression kind!");