[X86] Remove int_x86_sse2_psll_dq_bs and int_x86_sse2_psrl_dq_bs intrinsics. The...
[oota-llvm.git] / lib / MC / MCExpr.cpp
1 //===- MCExpr.cpp - Assembly Level Expression Implementation --------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
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"
23 using namespace llvm;
24
25 #define DEBUG_TYPE "mcexpr"
26
27 namespace {
28 namespace stats {
29 STATISTIC(MCExprEvaluate, "Number of MCExpr evaluations");
30 }
31 }
32
33 void MCExpr::print(raw_ostream &OS) const {
34   switch (getKind()) {
35   case MCExpr::Target:
36     return cast<MCTargetExpr>(this)->PrintImpl(OS);
37   case MCExpr::Constant:
38     OS << cast<MCConstantExpr>(*this).getValue();
39     return;
40
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
45     // absolute names.
46     bool UseParens = Sym.getName()[0] == '$';
47     if (UseParens)
48       OS << '(' << Sym << ')';
49     else
50       OS << Sym;
51
52     if (SRE.getKind() != MCSymbolRefExpr::VK_None)
53       SRE.printVariantKind(OS);
54
55     return;
56   }
57
58   case MCExpr::Unary: {
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;
65     }
66     OS << *UE.getSubExpr();
67     return;
68   }
69
70   case MCExpr::Binary: {
71     const MCBinaryExpr &BE = cast<MCBinaryExpr>(*this);
72
73     // Only print parens around the LHS if it is non-trivial.
74     if (isa<MCConstantExpr>(BE.getLHS()) || isa<MCSymbolRefExpr>(BE.getLHS())) {
75       OS << *BE.getLHS();
76     } else {
77       OS << '(' << *BE.getLHS() << ')';
78     }
79
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();
86           return;
87         }
88       }
89
90       OS <<  '+';
91       break;
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;
109     }
110
111     // Only print parens around the LHS if it is non-trivial.
112     if (isa<MCConstantExpr>(BE.getRHS()) || isa<MCSymbolRefExpr>(BE.getRHS())) {
113       OS << *BE.getRHS();
114     } else {
115       OS << '(' << *BE.getRHS() << ')';
116     }
117     return;
118   }
119   }
120
121   llvm_unreachable("Invalid expression kind!");
122 }
123
124 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
125 void MCExpr::dump() const {
126   print(dbgs());
127   dbgs() << '\n';
128 }
129 #endif
130
131 /* *** */
132
133 const MCBinaryExpr *MCBinaryExpr::Create(Opcode Opc, const MCExpr *LHS,
134                                          const MCExpr *RHS, MCContext &Ctx) {
135   return new (Ctx) MCBinaryExpr(Opc, LHS, RHS);
136 }
137
138 const MCUnaryExpr *MCUnaryExpr::Create(Opcode Opc, const MCExpr *Expr,
139                                        MCContext &Ctx) {
140   return new (Ctx) MCUnaryExpr(Opc, Expr);
141 }
142
143 const MCConstantExpr *MCConstantExpr::Create(int64_t Value, MCContext &Ctx) {
144   return new (Ctx) MCConstantExpr(Value);
145 }
146
147 /* *** */
148
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()),
154       Symbol(Symbol) {
155   assert(Symbol);
156 }
157
158 const MCSymbolRefExpr *MCSymbolRefExpr::Create(const MCSymbol *Sym,
159                                                VariantKind Kind,
160                                                MCContext &Ctx) {
161   return new (Ctx) MCSymbolRefExpr(Sym, Kind, Ctx.getAsmInfo());
162 }
163
164 const MCSymbolRefExpr *MCSymbolRefExpr::Create(StringRef Name, VariantKind Kind,
165                                                MCContext &Ctx) {
166   return Create(Ctx.GetOrCreateSymbol(Name), Kind, Ctx);
167 }
168
169 StringRef MCSymbolRefExpr::getVariantKindName(VariantKind Kind) {
170   switch (Kind) {
171   case VK_Invalid: return "<<invalid>>";
172   case VK_None: return "<<none>>";
173
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";
284   }
285   llvm_unreachable("Invalid variant kind");
286 }
287
288 MCSymbolRefExpr::VariantKind
289 MCSymbolRefExpr::getVariantKindForName(StringRef Name) {
290   return StringSwitch<VariantKind>(Name.lower())
291     .Case("got", VK_GOT)
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)
299     .Case("plt", VK_PLT)
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);
373 }
374
375 void MCSymbolRefExpr::printVariantKind(raw_ostream &OS) const {
376   if (UseParensForSymbolVariant)
377     OS << '(' << MCSymbolRefExpr::getVariantKindName(getKind()) << ')';
378   else
379     OS << '@' << MCSymbolRefExpr::getVariantKindName(getKind());
380 }
381
382 /* *** */
383
384 void MCTargetExpr::anchor() {}
385
386 /* *** */
387
388 bool MCExpr::EvaluateAsAbsolute(int64_t &Res) const {
389   return EvaluateAsAbsolute(Res, nullptr, nullptr, nullptr);
390 }
391
392 bool MCExpr::EvaluateAsAbsolute(int64_t &Res,
393                                 const MCAsmLayout &Layout) const {
394   return EvaluateAsAbsolute(Res, &Layout.getAssembler(), &Layout, nullptr);
395 }
396
397 bool MCExpr::EvaluateAsAbsolute(int64_t &Res,
398                                 const MCAsmLayout &Layout,
399                                 const SectionAddrMap &Addrs) const {
400   return EvaluateAsAbsolute(Res, &Layout.getAssembler(), &Layout, &Addrs);
401 }
402
403 bool MCExpr::EvaluateAsAbsolute(int64_t &Res, const MCAssembler &Asm) const {
404   return EvaluateAsAbsolute(Res, &Asm, nullptr, nullptr);
405 }
406
407 int64_t MCExpr::evaluateKnownAbsolute(const MCAsmLayout &Layout) const {
408   int64_t Res;
409   bool Abs =
410       evaluateAsAbsolute(Res, &Layout.getAssembler(), &Layout, nullptr, true);
411   (void)Abs;
412   assert(Abs && "Not actually absolute");
413   return Res;
414 }
415
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
421   // uses Addrs for.
422   return evaluateAsAbsolute(Res, Asm, Layout, Addrs, Addrs);
423 }
424
425 bool MCExpr::evaluateAsAbsolute(int64_t &Res, const MCAssembler *Asm,
426                                 const MCAsmLayout *Layout,
427                                 const SectionAddrMap *Addrs, bool InSet) const {
428   MCValue Value;
429
430   // Fast path constants.
431   if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(this)) {
432     Res = CE->getValue();
433     return true;
434   }
435
436   bool IsRelocatable = EvaluateAsRelocatableImpl(
437       Value, Asm, Layout, nullptr, Addrs, InSet, /*ForceVarExpansion*/ false);
438
439   // Record the current value.
440   Res = Value.getConstant();
441
442   return IsRelocatable && Value.isAbsolute();
443 }
444
445 /// \brief Helper method for \see EvaluateSymbolAdd().
446 static void AttemptToFoldSymbolOffsetDifference(const MCAssembler *Asm,
447                                                 const MCAsmLayout *Layout,
448                                                 const SectionAddrMap *Addrs,
449                                                 bool InSet,
450                                                 const MCSymbolRefExpr *&A,
451                                                 const MCSymbolRefExpr *&B,
452                                                 int64_t &Addend) {
453   if (!A || !B)
454     return;
455
456   const MCSymbol &SA = A->getSymbol();
457   const MCSymbol &SB = B->getSymbol();
458
459   if (SA.isUndefined() || SB.isUndefined())
460     return;
461
462   if (!Asm->getWriter().IsSymbolRefDifferenceFullyResolved(*Asm, A, B, InSet))
463     return;
464
465   const MCSymbolData &AD = Asm->getSymbolData(SA);
466   const MCSymbolData &BD = Asm->getSymbolData(SB);
467
468   if (AD.getFragment() == BD.getFragment()) {
469     Addend += (AD.getOffset() - BD.getOffset());
470
471     // Pointers to Thumb symbols need to have their low-bit set to allow
472     // for interworking.
473     if (Asm->isThumbFunc(&SA))
474       Addend |= 1;
475
476     // Clear the symbol expr pointers to indicate we have folded these
477     // operands.
478     A = B = nullptr;
479     return;
480   }
481
482   if (!Layout)
483     return;
484
485   const MCSectionData &SecA = *AD.getFragment()->getParent();
486   const MCSectionData &SecB = *BD.getFragment()->getParent();
487
488   if ((&SecA != &SecB) && !Addrs)
489     return;
490
491   // Eagerly evaluate.
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));
496
497   // Pointers to Thumb symbols need to have their low-bit set to allow
498   // for interworking.
499   if (Asm->isThumbFunc(&SA))
500     Addend |= 1;
501
502   // Clear the symbol expr pointers to indicate we have folded these
503   // operands.
504   A = B = nullptr;
505 }
506
507 /// \brief Evaluate the result of an add between (conceptually) two MCValues.
508 ///
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
513 /// and
514 ///   Result = (LHS_A - LHS_B + LHS_Cst) + (RHS_A - RHS_B + RHS_Cst).
515 ///
516 /// This routine attempts to aggresively fold the operands such that the result
517 /// is representable in an MCValue, but may not always succeed.
518 ///
519 /// \returns True on success, false if the result is not representable in an
520 /// MCValue.
521
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,
529                                 bool InSet,
530                                 const MCValue &LHS,const MCSymbolRefExpr *RHS_A,
531                                 const MCSymbolRefExpr *RHS_B, int64_t RHS_Cst,
532                                 MCValue &Res) {
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();
538
539   // Fold the result constant immediately.
540   int64_t Result_Cst = LHS_Cst + RHS_Cst;
541
542   assert((!Layout || Asm) &&
543          "Must have an assembler object if layout is given!");
544
545   // If we have a layout, we can fold resolved differences.
546   if (Asm) {
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:
551     //   (LHS_A - LHS_B),
552     //   (LHS_A - RHS_B),
553     //   (RHS_A - LHS_B),
554     //   (RHS_A - RHS_B).
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,
558                                         Result_Cst);
559     AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, LHS_A, RHS_B,
560                                         Result_Cst);
561     AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, RHS_A, LHS_B,
562                                         Result_Cst);
563     AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, RHS_A, RHS_B,
564                                         Result_Cst);
565   }
566
567   // We can't represent the addition or subtraction of two symbols.
568   if ((LHS_A && RHS_A) || (LHS_B && RHS_B))
569     return false;
570
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;
575
576   // If we have a negated symbol, then we must have also have a non-negated
577   // symbol in order to encode the expression.
578   if (B && !A)
579     return false;
580
581   Res = MCValue::get(A, B, Result_Cst);
582   return true;
583 }
584
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);
591 }
592
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);
598 }
599
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;
606
607   switch (getKind()) {
608   case Target:
609     return cast<MCTargetExpr>(this)->EvaluateAsRelocatableImpl(Res, Layout,
610                                                                Fixup);
611
612   case Constant:
613     Res = MCValue::get(cast<MCConstantExpr>(this)->getValue());
614     return true;
615
616   case SymbolRef: {
617     const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(this);
618     const MCSymbol &Sym = SRE->getSymbol();
619
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();
626
627         if (SRE->hasSubsectionsViaSymbols()) {
628           // FIXME: This is small hack. Given
629           // a = b + 4
630           // .long a
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
633           // possible.
634           if (!A && !B)
635             return true;
636         } else {
637           if (ForceVarExpansion)
638             return true;
639           bool IsSymbol = A && A->getSymbol().isDefined();
640           if (!IsSymbol)
641             return true;
642         }
643       }
644     }
645
646     Res = MCValue::get(SRE, nullptr, 0);
647     return true;
648   }
649
650   case Unary: {
651     const MCUnaryExpr *AUE = cast<MCUnaryExpr>(this);
652     MCValue Value;
653
654     if (!AUE->getSubExpr()->EvaluateAsRelocatableImpl(Value, Asm, Layout,
655                                                       Fixup, Addrs, InSet,
656                                                       ForceVarExpansion))
657       return false;
658
659     switch (AUE->getOpcode()) {
660     case MCUnaryExpr::LNot:
661       if (!Value.isAbsolute())
662         return false;
663       Res = MCValue::get(!Value.getConstant());
664       break;
665     case MCUnaryExpr::Minus:
666       /// -(a - b + const) ==> (b - a - const)
667       if (Value.getSymA() && !Value.getSymB())
668         return false;
669       Res = MCValue::get(Value.getSymB(), Value.getSymA(),
670                          -Value.getConstant());
671       break;
672     case MCUnaryExpr::Not:
673       if (!Value.isAbsolute())
674         return false;
675       Res = MCValue::get(~Value.getConstant());
676       break;
677     case MCUnaryExpr::Plus:
678       Res = Value;
679       break;
680     }
681
682     return true;
683   }
684
685   case Binary: {
686     const MCBinaryExpr *ABE = cast<MCBinaryExpr>(this);
687     MCValue LHSValue, RHSValue;
688
689     if (!ABE->getLHS()->EvaluateAsRelocatableImpl(LHSValue, Asm, Layout,
690                                                   Fixup, Addrs, InSet,
691                                                   ForceVarExpansion) ||
692         !ABE->getRHS()->EvaluateAsRelocatableImpl(RHSValue, Asm, Layout,
693                                                   Fixup, Addrs, InSet,
694                                                   ForceVarExpansion))
695       return false;
696
697     // We only support a few operations on non-constant expressions, handle
698     // those first.
699     if (!LHSValue.isAbsolute() || !RHSValue.isAbsolute()) {
700       switch (ABE->getOpcode()) {
701       default:
702         return false;
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(),
708                                    Res);
709
710       case MCBinaryExpr::Add:
711         return EvaluateSymbolicAdd(Asm, Layout, Addrs, InSet, LHSValue,
712                                    RHSValue.getSymA(), RHSValue.getSymB(),
713                                    RHSValue.getConstant(),
714                                    Res);
715       }
716     }
717
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();
722     int64_t Result = 0;
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;
742     }
743
744     Res = MCValue::get(Result);
745     return true;
746   }
747   }
748
749   llvm_unreachable("Invalid assembly expression kind!");
750 }
751
752 const MCSection *MCExpr::FindAssociatedSection() const {
753   switch (getKind()) {
754   case Target:
755     // We never look through target specific expressions.
756     return cast<MCTargetExpr>(this)->FindAssociatedSection();
757
758   case Constant:
759     return MCSymbol::AbsolutePseudoSection;
760
761   case SymbolRef: {
762     const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(this);
763     const MCSymbol &Sym = SRE->getSymbol();
764
765     if (Sym.isDefined())
766       return &Sym.getSection();
767
768     return nullptr;
769   }
770
771   case Unary:
772     return cast<MCUnaryExpr>(this)->getSubExpr()->FindAssociatedSection();
773
774   case Binary: {
775     const MCBinaryExpr *BE = cast<MCBinaryExpr>(this);
776     const MCSection *LHS_S = BE->getLHS()->FindAssociatedSection();
777     const MCSection *RHS_S = BE->getRHS()->FindAssociatedSection();
778
779     // If either section is absolute, return the other.
780     if (LHS_S == MCSymbol::AbsolutePseudoSection)
781       return RHS_S;
782     if (RHS_S == MCSymbol::AbsolutePseudoSection)
783       return LHS_S;
784
785     // Otherwise, return the first non-null section.
786     return LHS_S ? LHS_S : RHS_S;
787   }
788   }
789
790   llvm_unreachable("Invalid assembly expression kind!");
791 }