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 MCAsmInfo *MAI) const {
36 return cast<MCTargetExpr>(this)->printImpl(OS, MAI);
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] == '$';
54 if (SRE.getKind() != MCSymbolRefExpr::VK_None)
55 SRE.printVariantKind(OS);
61 const MCUnaryExpr &UE = cast<MCUnaryExpr>(*this);
62 switch (UE.getOpcode()) {
63 case MCUnaryExpr::LNot: OS << '!'; break;
64 case MCUnaryExpr::Minus: OS << '-'; break;
65 case MCUnaryExpr::Not: OS << '~'; break;
66 case MCUnaryExpr::Plus: OS << '+'; break;
68 UE.getSubExpr()->print(OS, MAI);
72 case MCExpr::Binary: {
73 const MCBinaryExpr &BE = cast<MCBinaryExpr>(*this);
75 // Only print parens around the LHS if it is non-trivial.
76 if (isa<MCConstantExpr>(BE.getLHS()) || isa<MCSymbolRefExpr>(BE.getLHS())) {
77 BE.getLHS()->print(OS, MAI);
80 BE.getLHS()->print(OS, MAI);
84 switch (BE.getOpcode()) {
85 case MCBinaryExpr::Add:
86 // Print "X-42" instead of "X+-42".
87 if (const MCConstantExpr *RHSC = dyn_cast<MCConstantExpr>(BE.getRHS())) {
88 if (RHSC->getValue() < 0) {
89 OS << RHSC->getValue();
96 case MCBinaryExpr::AShr: OS << ">>"; break;
97 case MCBinaryExpr::And: OS << '&'; break;
98 case MCBinaryExpr::Div: OS << '/'; break;
99 case MCBinaryExpr::EQ: OS << "=="; break;
100 case MCBinaryExpr::GT: OS << '>'; break;
101 case MCBinaryExpr::GTE: OS << ">="; break;
102 case MCBinaryExpr::LAnd: OS << "&&"; break;
103 case MCBinaryExpr::LOr: OS << "||"; break;
104 case MCBinaryExpr::LShr: OS << ">>"; break;
105 case MCBinaryExpr::LT: OS << '<'; break;
106 case MCBinaryExpr::LTE: OS << "<="; break;
107 case MCBinaryExpr::Mod: OS << '%'; break;
108 case MCBinaryExpr::Mul: OS << '*'; break;
109 case MCBinaryExpr::NE: OS << "!="; break;
110 case MCBinaryExpr::Or: OS << '|'; break;
111 case MCBinaryExpr::Shl: OS << "<<"; break;
112 case MCBinaryExpr::Sub: OS << '-'; break;
113 case MCBinaryExpr::Xor: OS << '^'; break;
116 // Only print parens around the LHS if it is non-trivial.
117 if (isa<MCConstantExpr>(BE.getRHS()) || isa<MCSymbolRefExpr>(BE.getRHS())) {
118 BE.getRHS()->print(OS, MAI);
121 BE.getRHS()->print(OS, MAI);
128 llvm_unreachable("Invalid expression kind!");
131 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
132 void MCExpr::dump() const {
140 const MCBinaryExpr *MCBinaryExpr::create(Opcode Opc, const MCExpr *LHS,
141 const MCExpr *RHS, MCContext &Ctx) {
142 return new (Ctx) MCBinaryExpr(Opc, LHS, RHS);
145 const MCUnaryExpr *MCUnaryExpr::create(Opcode Opc, const MCExpr *Expr,
147 return new (Ctx) MCUnaryExpr(Opc, Expr);
150 const MCConstantExpr *MCConstantExpr::create(int64_t Value, MCContext &Ctx) {
151 return new (Ctx) MCConstantExpr(Value);
156 MCSymbolRefExpr::MCSymbolRefExpr(const MCSymbol *Symbol, VariantKind Kind,
157 const MCAsmInfo *MAI)
158 : MCExpr(MCExpr::SymbolRef), Kind(Kind),
159 UseParensForSymbolVariant(MAI->useParensForSymbolVariant()),
160 HasSubsectionsViaSymbols(MAI->hasSubsectionsViaSymbols()),
165 const MCSymbolRefExpr *MCSymbolRefExpr::create(const MCSymbol *Sym,
168 return new (Ctx) MCSymbolRefExpr(Sym, Kind, Ctx.getAsmInfo());
171 const MCSymbolRefExpr *MCSymbolRefExpr::create(StringRef Name, VariantKind Kind,
173 return create(Ctx.getOrCreateSymbol(Name), Kind, Ctx);
176 StringRef MCSymbolRefExpr::getVariantKindName(VariantKind Kind) {
178 case VK_Invalid: return "<<invalid>>";
179 case VK_None: return "<<none>>";
181 case VK_GOT: return "GOT";
182 case VK_GOTOFF: return "GOTOFF";
183 case VK_GOTPCREL: return "GOTPCREL";
184 case VK_GOTTPOFF: return "GOTTPOFF";
185 case VK_INDNTPOFF: return "INDNTPOFF";
186 case VK_NTPOFF: return "NTPOFF";
187 case VK_GOTNTPOFF: return "GOTNTPOFF";
188 case VK_PLT: return "PLT";
189 case VK_TLSGD: return "TLSGD";
190 case VK_TLSLD: return "TLSLD";
191 case VK_TLSLDM: return "TLSLDM";
192 case VK_TPOFF: return "TPOFF";
193 case VK_DTPOFF: return "DTPOFF";
194 case VK_TLVP: return "TLVP";
195 case VK_TLVPPAGE: return "TLVPPAGE";
196 case VK_TLVPPAGEOFF: return "TLVPPAGEOFF";
197 case VK_PAGE: return "PAGE";
198 case VK_PAGEOFF: return "PAGEOFF";
199 case VK_GOTPAGE: return "GOTPAGE";
200 case VK_GOTPAGEOFF: return "GOTPAGEOFF";
201 case VK_SECREL: return "SECREL32";
202 case VK_SIZE: return "SIZE";
203 case VK_WEAKREF: return "WEAKREF";
204 case VK_ARM_NONE: return "none";
205 case VK_ARM_TARGET1: return "target1";
206 case VK_ARM_TARGET2: return "target2";
207 case VK_ARM_PREL31: return "prel31";
208 case VK_ARM_SBREL: return "sbrel";
209 case VK_ARM_TLSLDO: return "tlsldo";
210 case VK_ARM_TLSCALL: return "tlscall";
211 case VK_ARM_TLSDESC: return "tlsdesc";
212 case VK_ARM_TLSDESCSEQ: return "tlsdescseq";
213 case VK_PPC_LO: return "l";
214 case VK_PPC_HI: return "h";
215 case VK_PPC_HA: return "ha";
216 case VK_PPC_HIGHER: return "higher";
217 case VK_PPC_HIGHERA: return "highera";
218 case VK_PPC_HIGHEST: return "highest";
219 case VK_PPC_HIGHESTA: return "highesta";
220 case VK_PPC_GOT_LO: return "got@l";
221 case VK_PPC_GOT_HI: return "got@h";
222 case VK_PPC_GOT_HA: return "got@ha";
223 case VK_PPC_TOCBASE: return "tocbase";
224 case VK_PPC_TOC: return "toc";
225 case VK_PPC_TOC_LO: return "toc@l";
226 case VK_PPC_TOC_HI: return "toc@h";
227 case VK_PPC_TOC_HA: return "toc@ha";
228 case VK_PPC_DTPMOD: return "dtpmod";
229 case VK_PPC_TPREL: return "tprel";
230 case VK_PPC_TPREL_LO: return "tprel@l";
231 case VK_PPC_TPREL_HI: return "tprel@h";
232 case VK_PPC_TPREL_HA: return "tprel@ha";
233 case VK_PPC_TPREL_HIGHER: return "tprel@higher";
234 case VK_PPC_TPREL_HIGHERA: return "tprel@highera";
235 case VK_PPC_TPREL_HIGHEST: return "tprel@highest";
236 case VK_PPC_TPREL_HIGHESTA: return "tprel@highesta";
237 case VK_PPC_DTPREL: return "dtprel";
238 case VK_PPC_DTPREL_LO: return "dtprel@l";
239 case VK_PPC_DTPREL_HI: return "dtprel@h";
240 case VK_PPC_DTPREL_HA: return "dtprel@ha";
241 case VK_PPC_DTPREL_HIGHER: return "dtprel@higher";
242 case VK_PPC_DTPREL_HIGHERA: return "dtprel@highera";
243 case VK_PPC_DTPREL_HIGHEST: return "dtprel@highest";
244 case VK_PPC_DTPREL_HIGHESTA: return "dtprel@highesta";
245 case VK_PPC_GOT_TPREL: return "got@tprel";
246 case VK_PPC_GOT_TPREL_LO: return "got@tprel@l";
247 case VK_PPC_GOT_TPREL_HI: return "got@tprel@h";
248 case VK_PPC_GOT_TPREL_HA: return "got@tprel@ha";
249 case VK_PPC_GOT_DTPREL: return "got@dtprel";
250 case VK_PPC_GOT_DTPREL_LO: return "got@dtprel@l";
251 case VK_PPC_GOT_DTPREL_HI: return "got@dtprel@h";
252 case VK_PPC_GOT_DTPREL_HA: return "got@dtprel@ha";
253 case VK_PPC_TLS: return "tls";
254 case VK_PPC_GOT_TLSGD: return "got@tlsgd";
255 case VK_PPC_GOT_TLSGD_LO: return "got@tlsgd@l";
256 case VK_PPC_GOT_TLSGD_HI: return "got@tlsgd@h";
257 case VK_PPC_GOT_TLSGD_HA: return "got@tlsgd@ha";
258 case VK_PPC_TLSGD: return "tlsgd";
259 case VK_PPC_GOT_TLSLD: return "got@tlsld";
260 case VK_PPC_GOT_TLSLD_LO: return "got@tlsld@l";
261 case VK_PPC_GOT_TLSLD_HI: return "got@tlsld@h";
262 case VK_PPC_GOT_TLSLD_HA: return "got@tlsld@ha";
263 case VK_PPC_TLSLD: return "tlsld";
264 case VK_PPC_LOCAL: return "local";
265 case VK_Mips_GPREL: return "GPREL";
266 case VK_Mips_GOT_CALL: return "GOT_CALL";
267 case VK_Mips_GOT16: return "GOT16";
268 case VK_Mips_GOT: return "GOT";
269 case VK_Mips_ABS_HI: return "ABS_HI";
270 case VK_Mips_ABS_LO: return "ABS_LO";
271 case VK_Mips_TLSGD: return "TLSGD";
272 case VK_Mips_TLSLDM: return "TLSLDM";
273 case VK_Mips_DTPREL_HI: return "DTPREL_HI";
274 case VK_Mips_DTPREL_LO: return "DTPREL_LO";
275 case VK_Mips_GOTTPREL: return "GOTTPREL";
276 case VK_Mips_TPREL_HI: return "TPREL_HI";
277 case VK_Mips_TPREL_LO: return "TPREL_LO";
278 case VK_Mips_GPOFF_HI: return "GPOFF_HI";
279 case VK_Mips_GPOFF_LO: return "GPOFF_LO";
280 case VK_Mips_GOT_DISP: return "GOT_DISP";
281 case VK_Mips_GOT_PAGE: return "GOT_PAGE";
282 case VK_Mips_GOT_OFST: return "GOT_OFST";
283 case VK_Mips_HIGHER: return "HIGHER";
284 case VK_Mips_HIGHEST: return "HIGHEST";
285 case VK_Mips_GOT_HI16: return "GOT_HI16";
286 case VK_Mips_GOT_LO16: return "GOT_LO16";
287 case VK_Mips_CALL_HI16: return "CALL_HI16";
288 case VK_Mips_CALL_LO16: return "CALL_LO16";
289 case VK_Mips_PCREL_HI16: return "PCREL_HI16";
290 case VK_Mips_PCREL_LO16: return "PCREL_LO16";
291 case VK_COFF_IMGREL32: return "IMGREL";
292 case VK_Hexagon_PCREL: return "PCREL";
293 case VK_Hexagon_LO16: return "LO16";
294 case VK_Hexagon_HI16: return "HI16";
295 case VK_Hexagon_GPREL: return "GPREL";
296 case VK_Hexagon_GD_GOT: return "GDGOT";
297 case VK_Hexagon_LD_GOT: return "LDGOT";
298 case VK_Hexagon_GD_PLT: return "GDPLT";
299 case VK_Hexagon_LD_PLT: return "LDPLT";
300 case VK_Hexagon_IE: return "IE";
301 case VK_Hexagon_IE_GOT: return "IEGOT";
302 case VK_TPREL: return "tprel";
303 case VK_DTPREL: return "dtprel";
305 llvm_unreachable("Invalid variant kind");
308 MCSymbolRefExpr::VariantKind
309 MCSymbolRefExpr::getVariantKindForName(StringRef Name) {
310 return StringSwitch<VariantKind>(Name.lower())
312 .Case("gotoff", VK_GOTOFF)
313 .Case("gotpcrel", VK_GOTPCREL)
314 .Case("got_prel", VK_GOTPCREL)
315 .Case("gottpoff", VK_GOTTPOFF)
316 .Case("indntpoff", VK_INDNTPOFF)
317 .Case("ntpoff", VK_NTPOFF)
318 .Case("gotntpoff", VK_GOTNTPOFF)
320 .Case("tlsgd", VK_TLSGD)
321 .Case("tlsld", VK_TLSLD)
322 .Case("tlsldm", VK_TLSLDM)
323 .Case("tpoff", VK_TPOFF)
324 .Case("dtpoff", VK_DTPOFF)
325 .Case("tlvp", VK_TLVP)
326 .Case("tlvppage", VK_TLVPPAGE)
327 .Case("tlvppageoff", VK_TLVPPAGEOFF)
328 .Case("page", VK_PAGE)
329 .Case("pageoff", VK_PAGEOFF)
330 .Case("gotpage", VK_GOTPAGE)
331 .Case("gotpageoff", VK_GOTPAGEOFF)
332 .Case("imgrel", VK_COFF_IMGREL32)
333 .Case("secrel32", VK_SECREL)
334 .Case("size", VK_SIZE)
335 .Case("l", VK_PPC_LO)
336 .Case("h", VK_PPC_HI)
337 .Case("ha", VK_PPC_HA)
338 .Case("higher", VK_PPC_HIGHER)
339 .Case("highera", VK_PPC_HIGHERA)
340 .Case("highest", VK_PPC_HIGHEST)
341 .Case("highesta", VK_PPC_HIGHESTA)
342 .Case("got@l", VK_PPC_GOT_LO)
343 .Case("got@h", VK_PPC_GOT_HI)
344 .Case("got@ha", VK_PPC_GOT_HA)
345 .Case("local", VK_PPC_LOCAL)
346 .Case("tocbase", VK_PPC_TOCBASE)
347 .Case("toc", VK_PPC_TOC)
348 .Case("toc@l", VK_PPC_TOC_LO)
349 .Case("toc@h", VK_PPC_TOC_HI)
350 .Case("toc@ha", VK_PPC_TOC_HA)
351 .Case("tls", VK_PPC_TLS)
352 .Case("dtpmod", VK_PPC_DTPMOD)
353 .Case("tprel", VK_PPC_TPREL)
354 .Case("tprel@l", VK_PPC_TPREL_LO)
355 .Case("tprel@h", VK_PPC_TPREL_HI)
356 .Case("tprel@ha", VK_PPC_TPREL_HA)
357 .Case("tprel@higher", VK_PPC_TPREL_HIGHER)
358 .Case("tprel@highera", VK_PPC_TPREL_HIGHERA)
359 .Case("tprel@highest", VK_PPC_TPREL_HIGHEST)
360 .Case("tprel@highesta", VK_PPC_TPREL_HIGHESTA)
361 .Case("dtprel", VK_PPC_DTPREL)
362 .Case("dtprel@l", VK_PPC_DTPREL_LO)
363 .Case("dtprel@h", VK_PPC_DTPREL_HI)
364 .Case("dtprel@ha", VK_PPC_DTPREL_HA)
365 .Case("dtprel@higher", VK_PPC_DTPREL_HIGHER)
366 .Case("dtprel@highera", VK_PPC_DTPREL_HIGHERA)
367 .Case("dtprel@highest", VK_PPC_DTPREL_HIGHEST)
368 .Case("dtprel@highesta", VK_PPC_DTPREL_HIGHESTA)
369 .Case("got@tprel", VK_PPC_GOT_TPREL)
370 .Case("got@tprel@l", VK_PPC_GOT_TPREL_LO)
371 .Case("got@tprel@h", VK_PPC_GOT_TPREL_HI)
372 .Case("got@tprel@ha", VK_PPC_GOT_TPREL_HA)
373 .Case("got@dtprel", VK_PPC_GOT_DTPREL)
374 .Case("got@dtprel@l", VK_PPC_GOT_DTPREL_LO)
375 .Case("got@dtprel@h", VK_PPC_GOT_DTPREL_HI)
376 .Case("got@dtprel@ha", VK_PPC_GOT_DTPREL_HA)
377 .Case("got@tlsgd", VK_PPC_GOT_TLSGD)
378 .Case("got@tlsgd@l", VK_PPC_GOT_TLSGD_LO)
379 .Case("got@tlsgd@h", VK_PPC_GOT_TLSGD_HI)
380 .Case("got@tlsgd@ha", VK_PPC_GOT_TLSGD_HA)
381 .Case("got@tlsld", VK_PPC_GOT_TLSLD)
382 .Case("got@tlsld@l", VK_PPC_GOT_TLSLD_LO)
383 .Case("got@tlsld@h", VK_PPC_GOT_TLSLD_HI)
384 .Case("got@tlsld@ha", VK_PPC_GOT_TLSLD_HA)
385 .Case("none", VK_ARM_NONE)
386 .Case("target1", VK_ARM_TARGET1)
387 .Case("target2", VK_ARM_TARGET2)
388 .Case("prel31", VK_ARM_PREL31)
389 .Case("sbrel", VK_ARM_SBREL)
390 .Case("tlsldo", VK_ARM_TLSLDO)
391 .Case("tlscall", VK_ARM_TLSCALL)
392 .Case("tlsdesc", VK_ARM_TLSDESC)
393 .Default(VK_Invalid);
396 void MCSymbolRefExpr::printVariantKind(raw_ostream &OS) const {
397 if (UseParensForSymbolVariant)
398 OS << '(' << MCSymbolRefExpr::getVariantKindName(getKind()) << ')';
400 OS << '@' << MCSymbolRefExpr::getVariantKindName(getKind());
405 void MCTargetExpr::anchor() {}
409 bool MCExpr::evaluateAsAbsolute(int64_t &Res) const {
410 return evaluateAsAbsolute(Res, nullptr, nullptr, nullptr);
413 bool MCExpr::evaluateAsAbsolute(int64_t &Res,
414 const MCAsmLayout &Layout) const {
415 return evaluateAsAbsolute(Res, &Layout.getAssembler(), &Layout, nullptr);
418 bool MCExpr::evaluateAsAbsolute(int64_t &Res,
419 const MCAsmLayout &Layout,
420 const SectionAddrMap &Addrs) const {
421 return evaluateAsAbsolute(Res, &Layout.getAssembler(), &Layout, &Addrs);
424 bool MCExpr::evaluateAsAbsolute(int64_t &Res, const MCAssembler &Asm) const {
425 return evaluateAsAbsolute(Res, &Asm, nullptr, nullptr);
428 bool MCExpr::evaluateKnownAbsolute(int64_t &Res,
429 const MCAsmLayout &Layout) const {
430 return evaluateAsAbsolute(Res, &Layout.getAssembler(), &Layout, nullptr,
434 bool MCExpr::evaluateAsAbsolute(int64_t &Res, const MCAssembler *Asm,
435 const MCAsmLayout *Layout,
436 const SectionAddrMap *Addrs) const {
437 // FIXME: The use if InSet = Addrs is a hack. Setting InSet causes us
438 // absolutize differences across sections and that is what the MachO writer
440 return evaluateAsAbsolute(Res, Asm, Layout, Addrs, Addrs);
443 bool MCExpr::evaluateAsAbsolute(int64_t &Res, const MCAssembler *Asm,
444 const MCAsmLayout *Layout,
445 const SectionAddrMap *Addrs, bool InSet) const {
448 // Fast path constants.
449 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(this)) {
450 Res = CE->getValue();
455 evaluateAsRelocatableImpl(Value, Asm, Layout, nullptr, Addrs, InSet);
457 // Record the current value.
458 Res = Value.getConstant();
460 return IsRelocatable && Value.isAbsolute();
463 /// \brief Helper method for \see EvaluateSymbolAdd().
464 static void AttemptToFoldSymbolOffsetDifference(
465 const MCAssembler *Asm, const MCAsmLayout *Layout,
466 const SectionAddrMap *Addrs, bool InSet, const MCSymbolRefExpr *&A,
467 const MCSymbolRefExpr *&B, int64_t &Addend) {
471 const MCSymbol &SA = A->getSymbol();
472 const MCSymbol &SB = B->getSymbol();
474 if (SA.isUndefined() || SB.isUndefined())
477 if (!Asm->getWriter().isSymbolRefDifferenceFullyResolved(*Asm, A, B, InSet))
480 if (SA.getFragment() == SB.getFragment()) {
481 Addend += (SA.getOffset() - SB.getOffset());
483 // Pointers to Thumb symbols need to have their low-bit set to allow
485 if (Asm->isThumbFunc(&SA))
488 // Clear the symbol expr pointers to indicate we have folded these
497 const MCSection &SecA = *SA.getFragment()->getParent();
498 const MCSection &SecB = *SB.getFragment()->getParent();
500 if ((&SecA != &SecB) && !Addrs)
504 Addend += Layout->getSymbolOffset(A->getSymbol()) -
505 Layout->getSymbolOffset(B->getSymbol());
506 if (Addrs && (&SecA != &SecB))
507 Addend += (Addrs->lookup(&SecA) - Addrs->lookup(&SecB));
509 // Pointers to Thumb symbols need to have their low-bit set to allow
511 if (Asm->isThumbFunc(&SA))
514 // Clear the symbol expr pointers to indicate we have folded these
519 /// \brief Evaluate the result of an add between (conceptually) two MCValues.
521 /// This routine conceptually attempts to construct an MCValue:
522 /// Result = (Result_A - Result_B + Result_Cst)
523 /// from two MCValue's LHS and RHS where
524 /// Result = LHS + RHS
526 /// Result = (LHS_A - LHS_B + LHS_Cst) + (RHS_A - RHS_B + RHS_Cst).
528 /// This routine attempts to aggresively fold the operands such that the result
529 /// is representable in an MCValue, but may not always succeed.
531 /// \returns True on success, false if the result is not representable in an
534 /// NOTE: It is really important to have both the Asm and Layout arguments.
535 /// They might look redundant, but this function can be used before layout
536 /// is done (see the object streamer for example) and having the Asm argument
537 /// lets us avoid relaxations early.
539 EvaluateSymbolicAdd(const MCAssembler *Asm, const MCAsmLayout *Layout,
540 const SectionAddrMap *Addrs, bool InSet, const MCValue &LHS,
541 const MCSymbolRefExpr *RHS_A, const MCSymbolRefExpr *RHS_B,
542 int64_t RHS_Cst, MCValue &Res) {
543 // FIXME: This routine (and other evaluation parts) are *incredibly* sloppy
544 // about dealing with modifiers. This will ultimately bite us, one day.
545 const MCSymbolRefExpr *LHS_A = LHS.getSymA();
546 const MCSymbolRefExpr *LHS_B = LHS.getSymB();
547 int64_t LHS_Cst = LHS.getConstant();
549 // Fold the result constant immediately.
550 int64_t Result_Cst = LHS_Cst + RHS_Cst;
552 assert((!Layout || Asm) &&
553 "Must have an assembler object if layout is given!");
555 // If we have a layout, we can fold resolved differences.
557 // First, fold out any differences which are fully resolved. By
558 // reassociating terms in
559 // Result = (LHS_A - LHS_B + LHS_Cst) + (RHS_A - RHS_B + RHS_Cst).
560 // we have the four possible differences:
565 // Since we are attempting to be as aggressive as possible about folding, we
566 // attempt to evaluate each possible alternative.
567 AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, LHS_A, LHS_B,
569 AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, LHS_A, RHS_B,
571 AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, RHS_A, LHS_B,
573 AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, RHS_A, RHS_B,
577 // We can't represent the addition or subtraction of two symbols.
578 if ((LHS_A && RHS_A) || (LHS_B && RHS_B))
581 // At this point, we have at most one additive symbol and one subtractive
582 // symbol -- find them.
583 const MCSymbolRefExpr *A = LHS_A ? LHS_A : RHS_A;
584 const MCSymbolRefExpr *B = LHS_B ? LHS_B : RHS_B;
586 // If we have a negated symbol, then we must have also have a non-negated
587 // symbol in order to encode the expression.
591 Res = MCValue::get(A, B, Result_Cst);
595 bool MCExpr::evaluateAsRelocatable(MCValue &Res,
596 const MCAsmLayout *Layout,
597 const MCFixup *Fixup) const {
598 MCAssembler *Assembler = Layout ? &Layout->getAssembler() : nullptr;
599 return evaluateAsRelocatableImpl(Res, Assembler, Layout, Fixup, nullptr,
603 bool MCExpr::evaluateAsValue(MCValue &Res, const MCAsmLayout &Layout) const {
604 MCAssembler *Assembler = &Layout.getAssembler();
605 return evaluateAsRelocatableImpl(Res, Assembler, &Layout, nullptr, nullptr,
609 static bool canExpand(const MCSymbol &Sym, const MCAssembler *Asm, bool InSet) {
610 const MCExpr *Expr = Sym.getVariableValue();
611 const auto *Inner = dyn_cast<MCSymbolRefExpr>(Expr);
613 if (Inner->getKind() == MCSymbolRefExpr::VK_WEAKREF)
621 return !Asm->getWriter().isWeak(Sym);
624 bool MCExpr::evaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm,
625 const MCAsmLayout *Layout,
626 const MCFixup *Fixup,
627 const SectionAddrMap *Addrs,
629 ++stats::MCExprEvaluate;
633 return cast<MCTargetExpr>(this)->evaluateAsRelocatableImpl(Res, Layout,
637 Res = MCValue::get(cast<MCConstantExpr>(this)->getValue());
641 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(this);
642 const MCSymbol &Sym = SRE->getSymbol();
644 // Evaluate recursively if this is a variable.
645 if (Sym.isVariable() && SRE->getKind() == MCSymbolRefExpr::VK_None &&
646 canExpand(Sym, Asm, InSet)) {
647 bool IsMachO = SRE->hasSubsectionsViaSymbols();
648 if (Sym.getVariableValue()->evaluateAsRelocatableImpl(
649 Res, Asm, Layout, Fixup, Addrs, InSet || IsMachO)) {
653 const MCSymbolRefExpr *A = Res.getSymA();
654 const MCSymbolRefExpr *B = Res.getSymB();
655 // FIXME: This is small hack. Given
658 // the OS X assembler will completely drop the 4. We should probably
659 // include it in the relocation or produce an error if that is not
666 Res = MCValue::get(SRE, nullptr, 0);
671 const MCUnaryExpr *AUE = cast<MCUnaryExpr>(this);
674 if (!AUE->getSubExpr()->evaluateAsRelocatableImpl(Value, Asm, Layout, Fixup,
678 switch (AUE->getOpcode()) {
679 case MCUnaryExpr::LNot:
680 if (!Value.isAbsolute())
682 Res = MCValue::get(!Value.getConstant());
684 case MCUnaryExpr::Minus:
685 /// -(a - b + const) ==> (b - a - const)
686 if (Value.getSymA() && !Value.getSymB())
688 Res = MCValue::get(Value.getSymB(), Value.getSymA(),
689 -Value.getConstant());
691 case MCUnaryExpr::Not:
692 if (!Value.isAbsolute())
694 Res = MCValue::get(~Value.getConstant());
696 case MCUnaryExpr::Plus:
705 const MCBinaryExpr *ABE = cast<MCBinaryExpr>(this);
706 MCValue LHSValue, RHSValue;
708 if (!ABE->getLHS()->evaluateAsRelocatableImpl(LHSValue, Asm, Layout, Fixup,
710 !ABE->getRHS()->evaluateAsRelocatableImpl(RHSValue, Asm, Layout, Fixup,
714 // We only support a few operations on non-constant expressions, handle
716 if (!LHSValue.isAbsolute() || !RHSValue.isAbsolute()) {
717 switch (ABE->getOpcode()) {
720 case MCBinaryExpr::Sub:
721 // Negate RHS and add.
722 return EvaluateSymbolicAdd(Asm, Layout, Addrs, InSet, LHSValue,
723 RHSValue.getSymB(), RHSValue.getSymA(),
724 -RHSValue.getConstant(), Res);
726 case MCBinaryExpr::Add:
727 return EvaluateSymbolicAdd(Asm, Layout, Addrs, InSet, LHSValue,
728 RHSValue.getSymA(), RHSValue.getSymB(),
729 RHSValue.getConstant(), Res);
733 // FIXME: We need target hooks for the evaluation. It may be limited in
734 // width, and gas defines the result of comparisons differently from
736 int64_t LHS = LHSValue.getConstant(), RHS = RHSValue.getConstant();
738 switch (ABE->getOpcode()) {
739 case MCBinaryExpr::AShr: Result = LHS >> RHS; break;
740 case MCBinaryExpr::Add: Result = LHS + RHS; break;
741 case MCBinaryExpr::And: Result = LHS & RHS; break;
742 case MCBinaryExpr::Div: Result = LHS / RHS; break;
743 case MCBinaryExpr::EQ: Result = LHS == RHS; break;
744 case MCBinaryExpr::GT: Result = LHS > RHS; break;
745 case MCBinaryExpr::GTE: Result = LHS >= RHS; break;
746 case MCBinaryExpr::LAnd: Result = LHS && RHS; break;
747 case MCBinaryExpr::LOr: Result = LHS || RHS; break;
748 case MCBinaryExpr::LShr: Result = uint64_t(LHS) >> uint64_t(RHS); break;
749 case MCBinaryExpr::LT: Result = LHS < RHS; break;
750 case MCBinaryExpr::LTE: Result = LHS <= RHS; break;
751 case MCBinaryExpr::Mod: Result = LHS % RHS; break;
752 case MCBinaryExpr::Mul: Result = LHS * RHS; break;
753 case MCBinaryExpr::NE: Result = LHS != RHS; break;
754 case MCBinaryExpr::Or: Result = LHS | RHS; break;
755 case MCBinaryExpr::Shl: Result = uint64_t(LHS) << uint64_t(RHS); break;
756 case MCBinaryExpr::Sub: Result = LHS - RHS; break;
757 case MCBinaryExpr::Xor: Result = LHS ^ RHS; break;
760 Res = MCValue::get(Result);
765 llvm_unreachable("Invalid assembly expression kind!");
768 MCSection *MCExpr::findAssociatedSection() const {
771 // We never look through target specific expressions.
772 return cast<MCTargetExpr>(this)->findAssociatedSection();
775 return MCSymbol::AbsolutePseudoSection;
778 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(this);
779 const MCSymbol &Sym = SRE->getSymbol();
782 return &Sym.getSection();
788 return cast<MCUnaryExpr>(this)->getSubExpr()->findAssociatedSection();
791 const MCBinaryExpr *BE = cast<MCBinaryExpr>(this);
792 MCSection *LHS_S = BE->getLHS()->findAssociatedSection();
793 MCSection *RHS_S = BE->getRHS()->findAssociatedSection();
795 // If either section is absolute, return the other.
796 if (LHS_S == MCSymbol::AbsolutePseudoSection)
798 if (RHS_S == MCSymbol::AbsolutePseudoSection)
801 // Not always correct, but probably the best we can do without more context.
802 if (BE->getOpcode() == MCBinaryExpr::Sub)
803 return MCSymbol::AbsolutePseudoSection;
805 // Otherwise, return the first non-null section.
806 return LHS_S ? LHS_S : RHS_S;
810 llvm_unreachable("Invalid assembly expression kind!");