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 #include "llvm/MC/MCExpr.h"
11 #include "llvm/ADT/Statistic.h"
12 #include "llvm/ADT/StringSwitch.h"
13 #include "llvm/MC/MCAsmInfo.h"
14 #include "llvm/MC/MCAsmLayout.h"
15 #include "llvm/MC/MCAssembler.h"
16 #include "llvm/MC/MCContext.h"
17 #include "llvm/MC/MCObjectWriter.h"
18 #include "llvm/MC/MCSymbol.h"
19 #include "llvm/MC/MCValue.h"
20 #include "llvm/Support/Debug.h"
21 #include "llvm/Support/ErrorHandling.h"
22 #include "llvm/Support/raw_ostream.h"
25 #define DEBUG_TYPE "mcexpr"
29 STATISTIC(MCExprEvaluate, "Number of MCExpr evaluations");
33 void MCExpr::print(raw_ostream &OS) const {
36 return cast<MCTargetExpr>(this)->PrintImpl(OS);
37 case MCExpr::Constant:
38 OS << cast<MCConstantExpr>(*this).getValue();
41 case MCExpr::SymbolRef: {
42 const MCSymbolRefExpr &SRE = cast<MCSymbolRefExpr>(*this);
43 const MCSymbol &Sym = SRE.getSymbol();
44 // Parenthesize names that start with $ so that they don't look like
46 bool UseParens = Sym.getName()[0] == '$';
48 OS << '(' << Sym << ')';
52 if (SRE.getKind() != MCSymbolRefExpr::VK_None)
53 SRE.printVariantKind(OS);
59 const MCUnaryExpr &UE = cast<MCUnaryExpr>(*this);
60 switch (UE.getOpcode()) {
61 case MCUnaryExpr::LNot: OS << '!'; break;
62 case MCUnaryExpr::Minus: OS << '-'; break;
63 case MCUnaryExpr::Not: OS << '~'; break;
64 case MCUnaryExpr::Plus: OS << '+'; break;
66 OS << *UE.getSubExpr();
70 case MCExpr::Binary: {
71 const MCBinaryExpr &BE = cast<MCBinaryExpr>(*this);
73 // Only print parens around the LHS if it is non-trivial.
74 if (isa<MCConstantExpr>(BE.getLHS()) || isa<MCSymbolRefExpr>(BE.getLHS())) {
77 OS << '(' << *BE.getLHS() << ')';
80 switch (BE.getOpcode()) {
81 case MCBinaryExpr::Add:
82 // Print "X-42" instead of "X+-42".
83 if (const MCConstantExpr *RHSC = dyn_cast<MCConstantExpr>(BE.getRHS())) {
84 if (RHSC->getValue() < 0) {
85 OS << RHSC->getValue();
92 case MCBinaryExpr::And: OS << '&'; break;
93 case MCBinaryExpr::Div: OS << '/'; break;
94 case MCBinaryExpr::EQ: OS << "=="; break;
95 case MCBinaryExpr::GT: OS << '>'; break;
96 case MCBinaryExpr::GTE: OS << ">="; break;
97 case MCBinaryExpr::LAnd: OS << "&&"; break;
98 case MCBinaryExpr::LOr: OS << "||"; break;
99 case MCBinaryExpr::LT: OS << '<'; break;
100 case MCBinaryExpr::LTE: OS << "<="; break;
101 case MCBinaryExpr::Mod: OS << '%'; break;
102 case MCBinaryExpr::Mul: OS << '*'; break;
103 case MCBinaryExpr::NE: OS << "!="; break;
104 case MCBinaryExpr::Or: OS << '|'; break;
105 case MCBinaryExpr::Shl: OS << "<<"; break;
106 case MCBinaryExpr::Shr: OS << ">>"; break;
107 case MCBinaryExpr::Sub: OS << '-'; break;
108 case MCBinaryExpr::Xor: OS << '^'; break;
111 // Only print parens around the LHS if it is non-trivial.
112 if (isa<MCConstantExpr>(BE.getRHS()) || isa<MCSymbolRefExpr>(BE.getRHS())) {
115 OS << '(' << *BE.getRHS() << ')';
121 llvm_unreachable("Invalid expression kind!");
124 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
125 void MCExpr::dump() const {
133 const MCBinaryExpr *MCBinaryExpr::Create(Opcode Opc, const MCExpr *LHS,
134 const MCExpr *RHS, MCContext &Ctx) {
135 return new (Ctx) MCBinaryExpr(Opc, LHS, RHS);
138 const MCUnaryExpr *MCUnaryExpr::Create(Opcode Opc, const MCExpr *Expr,
140 return new (Ctx) MCUnaryExpr(Opc, Expr);
143 const MCConstantExpr *MCConstantExpr::Create(int64_t Value, MCContext &Ctx) {
144 return new (Ctx) MCConstantExpr(Value);
149 MCSymbolRefExpr::MCSymbolRefExpr(const MCSymbol *Symbol, VariantKind Kind,
150 const MCAsmInfo *MAI)
151 : MCExpr(MCExpr::SymbolRef), Kind(Kind),
152 UseParensForSymbolVariant(MAI->useParensForSymbolVariant()),
153 HasSubsectionsViaSymbols(MAI->hasSubsectionsViaSymbols()),
158 const MCSymbolRefExpr *MCSymbolRefExpr::Create(const MCSymbol *Sym,
161 return new (Ctx) MCSymbolRefExpr(Sym, Kind, Ctx.getAsmInfo());
164 const MCSymbolRefExpr *MCSymbolRefExpr::Create(StringRef Name, VariantKind Kind,
166 return Create(Ctx.GetOrCreateSymbol(Name), Kind, Ctx);
169 StringRef MCSymbolRefExpr::getVariantKindName(VariantKind Kind) {
171 case VK_Invalid: return "<<invalid>>";
172 case VK_None: return "<<none>>";
174 case VK_GOT: return "GOT";
175 case VK_GOTOFF: return "GOTOFF";
176 case VK_GOTPCREL: return "GOTPCREL";
177 case VK_GOTTPOFF: return "GOTTPOFF";
178 case VK_INDNTPOFF: return "INDNTPOFF";
179 case VK_NTPOFF: return "NTPOFF";
180 case VK_GOTNTPOFF: return "GOTNTPOFF";
181 case VK_PLT: return "PLT";
182 case VK_TLSGD: return "TLSGD";
183 case VK_TLSLD: return "TLSLD";
184 case VK_TLSLDM: return "TLSLDM";
185 case VK_TPOFF: return "TPOFF";
186 case VK_DTPOFF: return "DTPOFF";
187 case VK_TLVP: return "TLVP";
188 case VK_TLVPPAGE: return "TLVPPAGE";
189 case VK_TLVPPAGEOFF: return "TLVPPAGEOFF";
190 case VK_PAGE: return "PAGE";
191 case VK_PAGEOFF: return "PAGEOFF";
192 case VK_GOTPAGE: return "GOTPAGE";
193 case VK_GOTPAGEOFF: return "GOTPAGEOFF";
194 case VK_SECREL: return "SECREL32";
195 case VK_WEAKREF: return "WEAKREF";
196 case VK_ARM_NONE: return "none";
197 case VK_ARM_TARGET1: return "target1";
198 case VK_ARM_TARGET2: return "target2";
199 case VK_ARM_PREL31: return "prel31";
200 case VK_ARM_SBREL: return "sbrel";
201 case VK_ARM_TLSLDO: return "tlsldo";
202 case VK_ARM_TLSCALL: return "tlscall";
203 case VK_ARM_TLSDESC: return "tlsdesc";
204 case VK_ARM_TLSDESCSEQ: return "tlsdescseq";
205 case VK_PPC_LO: return "l";
206 case VK_PPC_HI: return "h";
207 case VK_PPC_HA: return "ha";
208 case VK_PPC_HIGHER: return "higher";
209 case VK_PPC_HIGHERA: return "highera";
210 case VK_PPC_HIGHEST: return "highest";
211 case VK_PPC_HIGHESTA: return "highesta";
212 case VK_PPC_GOT_LO: return "got@l";
213 case VK_PPC_GOT_HI: return "got@h";
214 case VK_PPC_GOT_HA: return "got@ha";
215 case VK_PPC_TOCBASE: return "tocbase";
216 case VK_PPC_TOC: return "toc";
217 case VK_PPC_TOC_LO: return "toc@l";
218 case VK_PPC_TOC_HI: return "toc@h";
219 case VK_PPC_TOC_HA: return "toc@ha";
220 case VK_PPC_DTPMOD: return "dtpmod";
221 case VK_PPC_TPREL: return "tprel";
222 case VK_PPC_TPREL_LO: return "tprel@l";
223 case VK_PPC_TPREL_HI: return "tprel@h";
224 case VK_PPC_TPREL_HA: return "tprel@ha";
225 case VK_PPC_TPREL_HIGHER: return "tprel@higher";
226 case VK_PPC_TPREL_HIGHERA: return "tprel@highera";
227 case VK_PPC_TPREL_HIGHEST: return "tprel@highest";
228 case VK_PPC_TPREL_HIGHESTA: return "tprel@highesta";
229 case VK_PPC_DTPREL: return "dtprel";
230 case VK_PPC_DTPREL_LO: return "dtprel@l";
231 case VK_PPC_DTPREL_HI: return "dtprel@h";
232 case VK_PPC_DTPREL_HA: return "dtprel@ha";
233 case VK_PPC_DTPREL_HIGHER: return "dtprel@higher";
234 case VK_PPC_DTPREL_HIGHERA: return "dtprel@highera";
235 case VK_PPC_DTPREL_HIGHEST: return "dtprel@highest";
236 case VK_PPC_DTPREL_HIGHESTA: return "dtprel@highesta";
237 case VK_PPC_GOT_TPREL: return "got@tprel";
238 case VK_PPC_GOT_TPREL_LO: return "got@tprel@l";
239 case VK_PPC_GOT_TPREL_HI: return "got@tprel@h";
240 case VK_PPC_GOT_TPREL_HA: return "got@tprel@ha";
241 case VK_PPC_GOT_DTPREL: return "got@dtprel";
242 case VK_PPC_GOT_DTPREL_LO: return "got@dtprel@l";
243 case VK_PPC_GOT_DTPREL_HI: return "got@dtprel@h";
244 case VK_PPC_GOT_DTPREL_HA: return "got@dtprel@ha";
245 case VK_PPC_TLS: return "tls";
246 case VK_PPC_GOT_TLSGD: return "got@tlsgd";
247 case VK_PPC_GOT_TLSGD_LO: return "got@tlsgd@l";
248 case VK_PPC_GOT_TLSGD_HI: return "got@tlsgd@h";
249 case VK_PPC_GOT_TLSGD_HA: return "got@tlsgd@ha";
250 case VK_PPC_TLSGD: return "tlsgd";
251 case VK_PPC_GOT_TLSLD: return "got@tlsld";
252 case VK_PPC_GOT_TLSLD_LO: return "got@tlsld@l";
253 case VK_PPC_GOT_TLSLD_HI: return "got@tlsld@h";
254 case VK_PPC_GOT_TLSLD_HA: return "got@tlsld@ha";
255 case VK_PPC_TLSLD: return "tlsld";
256 case VK_PPC_LOCAL: return "local";
257 case VK_Mips_GPREL: return "GPREL";
258 case VK_Mips_GOT_CALL: return "GOT_CALL";
259 case VK_Mips_GOT16: return "GOT16";
260 case VK_Mips_GOT: return "GOT";
261 case VK_Mips_ABS_HI: return "ABS_HI";
262 case VK_Mips_ABS_LO: return "ABS_LO";
263 case VK_Mips_TLSGD: return "TLSGD";
264 case VK_Mips_TLSLDM: return "TLSLDM";
265 case VK_Mips_DTPREL_HI: return "DTPREL_HI";
266 case VK_Mips_DTPREL_LO: return "DTPREL_LO";
267 case VK_Mips_GOTTPREL: return "GOTTPREL";
268 case VK_Mips_TPREL_HI: return "TPREL_HI";
269 case VK_Mips_TPREL_LO: return "TPREL_LO";
270 case VK_Mips_GPOFF_HI: return "GPOFF_HI";
271 case VK_Mips_GPOFF_LO: return "GPOFF_LO";
272 case VK_Mips_GOT_DISP: return "GOT_DISP";
273 case VK_Mips_GOT_PAGE: return "GOT_PAGE";
274 case VK_Mips_GOT_OFST: return "GOT_OFST";
275 case VK_Mips_HIGHER: return "HIGHER";
276 case VK_Mips_HIGHEST: return "HIGHEST";
277 case VK_Mips_GOT_HI16: return "GOT_HI16";
278 case VK_Mips_GOT_LO16: return "GOT_LO16";
279 case VK_Mips_CALL_HI16: return "CALL_HI16";
280 case VK_Mips_CALL_LO16: return "CALL_LO16";
281 case VK_Mips_PCREL_HI16: return "PCREL_HI16";
282 case VK_Mips_PCREL_LO16: return "PCREL_LO16";
283 case VK_COFF_IMGREL32: return "IMGREL";
285 llvm_unreachable("Invalid variant kind");
288 MCSymbolRefExpr::VariantKind
289 MCSymbolRefExpr::getVariantKindForName(StringRef Name) {
290 return StringSwitch<VariantKind>(Name.lower())
292 .Case("gotoff", VK_GOTOFF)
293 .Case("gotpcrel", VK_GOTPCREL)
294 .Case("got_prel", VK_GOTPCREL)
295 .Case("gottpoff", VK_GOTTPOFF)
296 .Case("indntpoff", VK_INDNTPOFF)
297 .Case("ntpoff", VK_NTPOFF)
298 .Case("gotntpoff", VK_GOTNTPOFF)
300 .Case("tlsgd", VK_TLSGD)
301 .Case("tlsld", VK_TLSLD)
302 .Case("tlsldm", VK_TLSLDM)
303 .Case("tpoff", VK_TPOFF)
304 .Case("dtpoff", VK_DTPOFF)
305 .Case("tlvp", VK_TLVP)
306 .Case("tlvppage", VK_TLVPPAGE)
307 .Case("tlvppageoff", VK_TLVPPAGEOFF)
308 .Case("page", VK_PAGE)
309 .Case("pageoff", VK_PAGEOFF)
310 .Case("gotpage", VK_GOTPAGE)
311 .Case("gotpageoff", VK_GOTPAGEOFF)
312 .Case("imgrel", VK_COFF_IMGREL32)
313 .Case("secrel32", VK_SECREL)
314 .Case("l", VK_PPC_LO)
315 .Case("h", VK_PPC_HI)
316 .Case("ha", VK_PPC_HA)
317 .Case("higher", VK_PPC_HIGHER)
318 .Case("highera", VK_PPC_HIGHERA)
319 .Case("highest", VK_PPC_HIGHEST)
320 .Case("highesta", VK_PPC_HIGHESTA)
321 .Case("got@l", VK_PPC_GOT_LO)
322 .Case("got@h", VK_PPC_GOT_HI)
323 .Case("got@ha", VK_PPC_GOT_HA)
324 .Case("local", VK_PPC_LOCAL)
325 .Case("tocbase", VK_PPC_TOCBASE)
326 .Case("toc", VK_PPC_TOC)
327 .Case("toc@l", VK_PPC_TOC_LO)
328 .Case("toc@h", VK_PPC_TOC_HI)
329 .Case("toc@ha", VK_PPC_TOC_HA)
330 .Case("tls", VK_PPC_TLS)
331 .Case("dtpmod", VK_PPC_DTPMOD)
332 .Case("tprel", VK_PPC_TPREL)
333 .Case("tprel@l", VK_PPC_TPREL_LO)
334 .Case("tprel@h", VK_PPC_TPREL_HI)
335 .Case("tprel@ha", VK_PPC_TPREL_HA)
336 .Case("tprel@higher", VK_PPC_TPREL_HIGHER)
337 .Case("tprel@highera", VK_PPC_TPREL_HIGHERA)
338 .Case("tprel@highest", VK_PPC_TPREL_HIGHEST)
339 .Case("tprel@highesta", VK_PPC_TPREL_HIGHESTA)
340 .Case("dtprel", VK_PPC_DTPREL)
341 .Case("dtprel@l", VK_PPC_DTPREL_LO)
342 .Case("dtprel@h", VK_PPC_DTPREL_HI)
343 .Case("dtprel@ha", VK_PPC_DTPREL_HA)
344 .Case("dtprel@higher", VK_PPC_DTPREL_HIGHER)
345 .Case("dtprel@highera", VK_PPC_DTPREL_HIGHERA)
346 .Case("dtprel@highest", VK_PPC_DTPREL_HIGHEST)
347 .Case("dtprel@highesta", VK_PPC_DTPREL_HIGHESTA)
348 .Case("got@tprel", VK_PPC_GOT_TPREL)
349 .Case("got@tprel@l", VK_PPC_GOT_TPREL_LO)
350 .Case("got@tprel@h", VK_PPC_GOT_TPREL_HI)
351 .Case("got@tprel@ha", VK_PPC_GOT_TPREL_HA)
352 .Case("got@dtprel", VK_PPC_GOT_DTPREL)
353 .Case("got@dtprel@l", VK_PPC_GOT_DTPREL_LO)
354 .Case("got@dtprel@h", VK_PPC_GOT_DTPREL_HI)
355 .Case("got@dtprel@ha", VK_PPC_GOT_DTPREL_HA)
356 .Case("got@tlsgd", VK_PPC_GOT_TLSGD)
357 .Case("got@tlsgd@l", VK_PPC_GOT_TLSGD_LO)
358 .Case("got@tlsgd@h", VK_PPC_GOT_TLSGD_HI)
359 .Case("got@tlsgd@ha", VK_PPC_GOT_TLSGD_HA)
360 .Case("got@tlsld", VK_PPC_GOT_TLSLD)
361 .Case("got@tlsld@l", VK_PPC_GOT_TLSLD_LO)
362 .Case("got@tlsld@h", VK_PPC_GOT_TLSLD_HI)
363 .Case("got@tlsld@ha", VK_PPC_GOT_TLSLD_HA)
364 .Case("none", VK_ARM_NONE)
365 .Case("target1", VK_ARM_TARGET1)
366 .Case("target2", VK_ARM_TARGET2)
367 .Case("prel31", VK_ARM_PREL31)
368 .Case("sbrel", VK_ARM_SBREL)
369 .Case("tlsldo", VK_ARM_TLSLDO)
370 .Case("tlscall", VK_ARM_TLSCALL)
371 .Case("tlsdesc", VK_ARM_TLSDESC)
372 .Default(VK_Invalid);
375 void MCSymbolRefExpr::printVariantKind(raw_ostream &OS) const {
376 if (UseParensForSymbolVariant)
377 OS << '(' << MCSymbolRefExpr::getVariantKindName(getKind()) << ')';
379 OS << '@' << MCSymbolRefExpr::getVariantKindName(getKind());
384 void MCTargetExpr::anchor() {}
388 bool MCExpr::EvaluateAsAbsolute(int64_t &Res) const {
389 return EvaluateAsAbsolute(Res, nullptr, nullptr, nullptr);
392 bool MCExpr::EvaluateAsAbsolute(int64_t &Res,
393 const MCAsmLayout &Layout) const {
394 return EvaluateAsAbsolute(Res, &Layout.getAssembler(), &Layout, nullptr);
397 bool MCExpr::EvaluateAsAbsolute(int64_t &Res,
398 const MCAsmLayout &Layout,
399 const SectionAddrMap &Addrs) const {
400 return EvaluateAsAbsolute(Res, &Layout.getAssembler(), &Layout, &Addrs);
403 bool MCExpr::EvaluateAsAbsolute(int64_t &Res, const MCAssembler &Asm) const {
404 return EvaluateAsAbsolute(Res, &Asm, nullptr, nullptr);
407 int64_t MCExpr::evaluateKnownAbsolute(const MCAsmLayout &Layout) const {
410 evaluateAsAbsolute(Res, &Layout.getAssembler(), &Layout, nullptr, true);
412 assert(Abs && "Not actually absolute");
416 bool MCExpr::EvaluateAsAbsolute(int64_t &Res, const MCAssembler *Asm,
417 const MCAsmLayout *Layout,
418 const SectionAddrMap *Addrs) const {
419 // FIXME: The use if InSet = Addrs is a hack. Setting InSet causes us
420 // absolutize differences across sections and that is what the MachO writer
422 return evaluateAsAbsolute(Res, Asm, Layout, Addrs, Addrs);
425 bool MCExpr::evaluateAsAbsolute(int64_t &Res, const MCAssembler *Asm,
426 const MCAsmLayout *Layout,
427 const SectionAddrMap *Addrs, bool InSet) const {
430 // Fast path constants.
431 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(this)) {
432 Res = CE->getValue();
436 bool IsRelocatable = EvaluateAsRelocatableImpl(
437 Value, Asm, Layout, nullptr, Addrs, InSet, /*ForceVarExpansion*/ false);
439 // Record the current value.
440 Res = Value.getConstant();
442 return IsRelocatable && Value.isAbsolute();
445 /// \brief Helper method for \see EvaluateSymbolAdd().
446 static void AttemptToFoldSymbolOffsetDifference(const MCAssembler *Asm,
447 const MCAsmLayout *Layout,
448 const SectionAddrMap *Addrs,
450 const MCSymbolRefExpr *&A,
451 const MCSymbolRefExpr *&B,
456 const MCSymbol &SA = A->getSymbol();
457 const MCSymbol &SB = B->getSymbol();
459 if (SA.isUndefined() || SB.isUndefined())
462 if (!Asm->getWriter().IsSymbolRefDifferenceFullyResolved(*Asm, A, B, InSet))
465 const MCSymbolData &AD = Asm->getSymbolData(SA);
466 const MCSymbolData &BD = Asm->getSymbolData(SB);
468 if (AD.getFragment() == BD.getFragment()) {
469 Addend += (AD.getOffset() - BD.getOffset());
471 // Pointers to Thumb symbols need to have their low-bit set to allow
473 if (Asm->isThumbFunc(&SA))
476 // Clear the symbol expr pointers to indicate we have folded these
485 const MCSectionData &SecA = *AD.getFragment()->getParent();
486 const MCSectionData &SecB = *BD.getFragment()->getParent();
488 if ((&SecA != &SecB) && !Addrs)
492 Addend += (Layout->getSymbolOffset(&Asm->getSymbolData(A->getSymbol())) -
493 Layout->getSymbolOffset(&Asm->getSymbolData(B->getSymbol())));
494 if (Addrs && (&SecA != &SecB))
495 Addend += (Addrs->lookup(&SecA) - Addrs->lookup(&SecB));
497 // Pointers to Thumb symbols need to have their low-bit set to allow
499 if (Asm->isThumbFunc(&SA))
502 // Clear the symbol expr pointers to indicate we have folded these
507 /// \brief Evaluate the result of an add between (conceptually) two MCValues.
509 /// This routine conceptually attempts to construct an MCValue:
510 /// Result = (Result_A - Result_B + Result_Cst)
511 /// from two MCValue's LHS and RHS where
512 /// Result = LHS + RHS
514 /// Result = (LHS_A - LHS_B + LHS_Cst) + (RHS_A - RHS_B + RHS_Cst).
516 /// This routine attempts to aggresively fold the operands such that the result
517 /// is representable in an MCValue, but may not always succeed.
519 /// \returns True on success, false if the result is not representable in an
522 /// NOTE: It is really important to have both the Asm and Layout arguments.
523 /// They might look redundant, but this function can be used before layout
524 /// is done (see the object streamer for example) and having the Asm argument
525 /// lets us avoid relaxations early.
526 static bool EvaluateSymbolicAdd(const MCAssembler *Asm,
527 const MCAsmLayout *Layout,
528 const SectionAddrMap *Addrs,
530 const MCValue &LHS,const MCSymbolRefExpr *RHS_A,
531 const MCSymbolRefExpr *RHS_B, int64_t RHS_Cst,
533 // FIXME: This routine (and other evaluation parts) are *incredibly* sloppy
534 // about dealing with modifiers. This will ultimately bite us, one day.
535 const MCSymbolRefExpr *LHS_A = LHS.getSymA();
536 const MCSymbolRefExpr *LHS_B = LHS.getSymB();
537 int64_t LHS_Cst = LHS.getConstant();
539 // Fold the result constant immediately.
540 int64_t Result_Cst = LHS_Cst + RHS_Cst;
542 assert((!Layout || Asm) &&
543 "Must have an assembler object if layout is given!");
545 // If we have a layout, we can fold resolved differences.
547 // First, fold out any differences which are fully resolved. By
548 // reassociating terms in
549 // Result = (LHS_A - LHS_B + LHS_Cst) + (RHS_A - RHS_B + RHS_Cst).
550 // we have the four possible differences:
555 // Since we are attempting to be as aggressive as possible about folding, we
556 // attempt to evaluate each possible alternative.
557 AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, LHS_A, LHS_B,
559 AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, LHS_A, RHS_B,
561 AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, RHS_A, LHS_B,
563 AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, RHS_A, RHS_B,
567 // We can't represent the addition or subtraction of two symbols.
568 if ((LHS_A && RHS_A) || (LHS_B && RHS_B))
571 // At this point, we have at most one additive symbol and one subtractive
572 // symbol -- find them.
573 const MCSymbolRefExpr *A = LHS_A ? LHS_A : RHS_A;
574 const MCSymbolRefExpr *B = LHS_B ? LHS_B : RHS_B;
576 // If we have a negated symbol, then we must have also have a non-negated
577 // symbol in order to encode the expression.
581 Res = MCValue::get(A, B, Result_Cst);
585 bool MCExpr::EvaluateAsRelocatable(MCValue &Res,
586 const MCAsmLayout *Layout,
587 const MCFixup *Fixup) const {
588 MCAssembler *Assembler = Layout ? &Layout->getAssembler() : nullptr;
589 return EvaluateAsRelocatableImpl(Res, Assembler, Layout, Fixup, nullptr,
590 false, /*ForceVarExpansion*/ false);
593 bool MCExpr::EvaluateAsValue(MCValue &Res, const MCAsmLayout *Layout,
594 const MCFixup *Fixup) const {
595 MCAssembler *Assembler = Layout ? &Layout->getAssembler() : nullptr;
596 return EvaluateAsRelocatableImpl(Res, Assembler, Layout, Fixup, nullptr,
597 false, /*ForceVarExpansion*/ true);
600 bool MCExpr::EvaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm,
601 const MCAsmLayout *Layout,
602 const MCFixup *Fixup,
603 const SectionAddrMap *Addrs, bool InSet,
604 bool ForceVarExpansion) const {
605 ++stats::MCExprEvaluate;
609 return cast<MCTargetExpr>(this)->EvaluateAsRelocatableImpl(Res, Layout,
613 Res = MCValue::get(cast<MCConstantExpr>(this)->getValue());
617 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(this);
618 const MCSymbol &Sym = SRE->getSymbol();
620 // Evaluate recursively if this is a variable.
621 if (Sym.isVariable() && SRE->getKind() == MCSymbolRefExpr::VK_None) {
622 if (Sym.getVariableValue()->EvaluateAsRelocatableImpl(
623 Res, Asm, Layout, Fixup, Addrs, true, ForceVarExpansion)) {
624 const MCSymbolRefExpr *A = Res.getSymA();
625 const MCSymbolRefExpr *B = Res.getSymB();
627 if (SRE->hasSubsectionsViaSymbols()) {
628 // FIXME: This is small hack. Given
631 // the OS X assembler will completely drop the 4. We should probably
632 // include it in the relocation or produce an error if that is not
637 if (ForceVarExpansion)
639 bool IsSymbol = A && A->getSymbol().isDefined();
646 Res = MCValue::get(SRE, nullptr, 0);
651 const MCUnaryExpr *AUE = cast<MCUnaryExpr>(this);
654 if (!AUE->getSubExpr()->EvaluateAsRelocatableImpl(Value, Asm, Layout,
659 switch (AUE->getOpcode()) {
660 case MCUnaryExpr::LNot:
661 if (!Value.isAbsolute())
663 Res = MCValue::get(!Value.getConstant());
665 case MCUnaryExpr::Minus:
666 /// -(a - b + const) ==> (b - a - const)
667 if (Value.getSymA() && !Value.getSymB())
669 Res = MCValue::get(Value.getSymB(), Value.getSymA(),
670 -Value.getConstant());
672 case MCUnaryExpr::Not:
673 if (!Value.isAbsolute())
675 Res = MCValue::get(~Value.getConstant());
677 case MCUnaryExpr::Plus:
686 const MCBinaryExpr *ABE = cast<MCBinaryExpr>(this);
687 MCValue LHSValue, RHSValue;
689 if (!ABE->getLHS()->EvaluateAsRelocatableImpl(LHSValue, Asm, Layout,
691 ForceVarExpansion) ||
692 !ABE->getRHS()->EvaluateAsRelocatableImpl(RHSValue, Asm, Layout,
697 // We only support a few operations on non-constant expressions, handle
699 if (!LHSValue.isAbsolute() || !RHSValue.isAbsolute()) {
700 switch (ABE->getOpcode()) {
703 case MCBinaryExpr::Sub:
704 // Negate RHS and add.
705 return EvaluateSymbolicAdd(Asm, Layout, Addrs, InSet, LHSValue,
706 RHSValue.getSymB(), RHSValue.getSymA(),
707 -RHSValue.getConstant(),
710 case MCBinaryExpr::Add:
711 return EvaluateSymbolicAdd(Asm, Layout, Addrs, InSet, LHSValue,
712 RHSValue.getSymA(), RHSValue.getSymB(),
713 RHSValue.getConstant(),
718 // FIXME: We need target hooks for the evaluation. It may be limited in
719 // width, and gas defines the result of comparisons and right shifts
720 // differently from Apple as.
721 int64_t LHS = LHSValue.getConstant(), RHS = RHSValue.getConstant();
723 switch (ABE->getOpcode()) {
724 case MCBinaryExpr::Add: Result = LHS + RHS; break;
725 case MCBinaryExpr::And: Result = LHS & RHS; break;
726 case MCBinaryExpr::Div: Result = LHS / RHS; break;
727 case MCBinaryExpr::EQ: Result = LHS == RHS; break;
728 case MCBinaryExpr::GT: Result = LHS > RHS; break;
729 case MCBinaryExpr::GTE: Result = LHS >= RHS; break;
730 case MCBinaryExpr::LAnd: Result = LHS && RHS; break;
731 case MCBinaryExpr::LOr: Result = LHS || RHS; break;
732 case MCBinaryExpr::LT: Result = LHS < RHS; break;
733 case MCBinaryExpr::LTE: Result = LHS <= RHS; break;
734 case MCBinaryExpr::Mod: Result = LHS % RHS; break;
735 case MCBinaryExpr::Mul: Result = LHS * RHS; break;
736 case MCBinaryExpr::NE: Result = LHS != RHS; break;
737 case MCBinaryExpr::Or: Result = LHS | RHS; break;
738 case MCBinaryExpr::Shl: Result = LHS << RHS; break;
739 case MCBinaryExpr::Shr: Result = LHS >> RHS; break;
740 case MCBinaryExpr::Sub: Result = LHS - RHS; break;
741 case MCBinaryExpr::Xor: Result = LHS ^ RHS; break;
744 Res = MCValue::get(Result);
749 llvm_unreachable("Invalid assembly expression kind!");
752 const MCSection *MCExpr::FindAssociatedSection() const {
755 // We never look through target specific expressions.
756 return cast<MCTargetExpr>(this)->FindAssociatedSection();
759 return MCSymbol::AbsolutePseudoSection;
762 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(this);
763 const MCSymbol &Sym = SRE->getSymbol();
766 return &Sym.getSection();
772 return cast<MCUnaryExpr>(this)->getSubExpr()->FindAssociatedSection();
775 const MCBinaryExpr *BE = cast<MCBinaryExpr>(this);
776 const MCSection *LHS_S = BE->getLHS()->FindAssociatedSection();
777 const MCSection *RHS_S = BE->getRHS()->FindAssociatedSection();
779 // If either section is absolute, return the other.
780 if (LHS_S == MCSymbol::AbsolutePseudoSection)
782 if (RHS_S == MCSymbol::AbsolutePseudoSection)
785 // Otherwise, return the first non-null section.
786 return LHS_S ? LHS_S : RHS_S;
790 llvm_unreachable("Invalid assembly expression kind!");