Fix fixup evaluation when deciding what to relocate with.
[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_SIZE: return "SIZE";
196   case VK_WEAKREF: return "WEAKREF";
197   case VK_ARM_NONE: return "none";
198   case VK_ARM_TARGET1: return "target1";
199   case VK_ARM_TARGET2: return "target2";
200   case VK_ARM_PREL31: return "prel31";
201   case VK_ARM_SBREL: return "sbrel";
202   case VK_ARM_TLSLDO: return "tlsldo";
203   case VK_ARM_TLSCALL: return "tlscall";
204   case VK_ARM_TLSDESC: return "tlsdesc";
205   case VK_ARM_TLSDESCSEQ: return "tlsdescseq";
206   case VK_PPC_LO: return "l";
207   case VK_PPC_HI: return "h";
208   case VK_PPC_HA: return "ha";
209   case VK_PPC_HIGHER: return "higher";
210   case VK_PPC_HIGHERA: return "highera";
211   case VK_PPC_HIGHEST: return "highest";
212   case VK_PPC_HIGHESTA: return "highesta";
213   case VK_PPC_GOT_LO: return "got@l";
214   case VK_PPC_GOT_HI: return "got@h";
215   case VK_PPC_GOT_HA: return "got@ha";
216   case VK_PPC_TOCBASE: return "tocbase";
217   case VK_PPC_TOC: return "toc";
218   case VK_PPC_TOC_LO: return "toc@l";
219   case VK_PPC_TOC_HI: return "toc@h";
220   case VK_PPC_TOC_HA: return "toc@ha";
221   case VK_PPC_DTPMOD: return "dtpmod";
222   case VK_PPC_TPREL: return "tprel";
223   case VK_PPC_TPREL_LO: return "tprel@l";
224   case VK_PPC_TPREL_HI: return "tprel@h";
225   case VK_PPC_TPREL_HA: return "tprel@ha";
226   case VK_PPC_TPREL_HIGHER: return "tprel@higher";
227   case VK_PPC_TPREL_HIGHERA: return "tprel@highera";
228   case VK_PPC_TPREL_HIGHEST: return "tprel@highest";
229   case VK_PPC_TPREL_HIGHESTA: return "tprel@highesta";
230   case VK_PPC_DTPREL: return "dtprel";
231   case VK_PPC_DTPREL_LO: return "dtprel@l";
232   case VK_PPC_DTPREL_HI: return "dtprel@h";
233   case VK_PPC_DTPREL_HA: return "dtprel@ha";
234   case VK_PPC_DTPREL_HIGHER: return "dtprel@higher";
235   case VK_PPC_DTPREL_HIGHERA: return "dtprel@highera";
236   case VK_PPC_DTPREL_HIGHEST: return "dtprel@highest";
237   case VK_PPC_DTPREL_HIGHESTA: return "dtprel@highesta";
238   case VK_PPC_GOT_TPREL: return "got@tprel";
239   case VK_PPC_GOT_TPREL_LO: return "got@tprel@l";
240   case VK_PPC_GOT_TPREL_HI: return "got@tprel@h";
241   case VK_PPC_GOT_TPREL_HA: return "got@tprel@ha";
242   case VK_PPC_GOT_DTPREL: return "got@dtprel";
243   case VK_PPC_GOT_DTPREL_LO: return "got@dtprel@l";
244   case VK_PPC_GOT_DTPREL_HI: return "got@dtprel@h";
245   case VK_PPC_GOT_DTPREL_HA: return "got@dtprel@ha";
246   case VK_PPC_TLS: return "tls";
247   case VK_PPC_GOT_TLSGD: return "got@tlsgd";
248   case VK_PPC_GOT_TLSGD_LO: return "got@tlsgd@l";
249   case VK_PPC_GOT_TLSGD_HI: return "got@tlsgd@h";
250   case VK_PPC_GOT_TLSGD_HA: return "got@tlsgd@ha";
251   case VK_PPC_TLSGD: return "tlsgd";
252   case VK_PPC_GOT_TLSLD: return "got@tlsld";
253   case VK_PPC_GOT_TLSLD_LO: return "got@tlsld@l";
254   case VK_PPC_GOT_TLSLD_HI: return "got@tlsld@h";
255   case VK_PPC_GOT_TLSLD_HA: return "got@tlsld@ha";
256   case VK_PPC_TLSLD: return "tlsld";
257   case VK_PPC_LOCAL: return "local";
258   case VK_Mips_GPREL: return "GPREL";
259   case VK_Mips_GOT_CALL: return "GOT_CALL";
260   case VK_Mips_GOT16: return "GOT16";
261   case VK_Mips_GOT: return "GOT";
262   case VK_Mips_ABS_HI: return "ABS_HI";
263   case VK_Mips_ABS_LO: return "ABS_LO";
264   case VK_Mips_TLSGD: return "TLSGD";
265   case VK_Mips_TLSLDM: return "TLSLDM";
266   case VK_Mips_DTPREL_HI: return "DTPREL_HI";
267   case VK_Mips_DTPREL_LO: return "DTPREL_LO";
268   case VK_Mips_GOTTPREL: return "GOTTPREL";
269   case VK_Mips_TPREL_HI: return "TPREL_HI";
270   case VK_Mips_TPREL_LO: return "TPREL_LO";
271   case VK_Mips_GPOFF_HI: return "GPOFF_HI";
272   case VK_Mips_GPOFF_LO: return "GPOFF_LO";
273   case VK_Mips_GOT_DISP: return "GOT_DISP";
274   case VK_Mips_GOT_PAGE: return "GOT_PAGE";
275   case VK_Mips_GOT_OFST: return "GOT_OFST";
276   case VK_Mips_HIGHER:   return "HIGHER";
277   case VK_Mips_HIGHEST:  return "HIGHEST";
278   case VK_Mips_GOT_HI16: return "GOT_HI16";
279   case VK_Mips_GOT_LO16: return "GOT_LO16";
280   case VK_Mips_CALL_HI16: return "CALL_HI16";
281   case VK_Mips_CALL_LO16: return "CALL_LO16";
282   case VK_Mips_PCREL_HI16: return "PCREL_HI16";
283   case VK_Mips_PCREL_LO16: return "PCREL_LO16";
284   case VK_COFF_IMGREL32: return "IMGREL";
285   }
286   llvm_unreachable("Invalid variant kind");
287 }
288
289 MCSymbolRefExpr::VariantKind
290 MCSymbolRefExpr::getVariantKindForName(StringRef Name) {
291   return StringSwitch<VariantKind>(Name.lower())
292     .Case("got", VK_GOT)
293     .Case("gotoff", VK_GOTOFF)
294     .Case("gotpcrel", VK_GOTPCREL)
295     .Case("got_prel", VK_GOTPCREL)
296     .Case("gottpoff", VK_GOTTPOFF)
297     .Case("indntpoff", VK_INDNTPOFF)
298     .Case("ntpoff", VK_NTPOFF)
299     .Case("gotntpoff", VK_GOTNTPOFF)
300     .Case("plt", VK_PLT)
301     .Case("tlsgd", VK_TLSGD)
302     .Case("tlsld", VK_TLSLD)
303     .Case("tlsldm", VK_TLSLDM)
304     .Case("tpoff", VK_TPOFF)
305     .Case("dtpoff", VK_DTPOFF)
306     .Case("tlvp", VK_TLVP)
307     .Case("tlvppage", VK_TLVPPAGE)
308     .Case("tlvppageoff", VK_TLVPPAGEOFF)
309     .Case("page", VK_PAGE)
310     .Case("pageoff", VK_PAGEOFF)
311     .Case("gotpage", VK_GOTPAGE)
312     .Case("gotpageoff", VK_GOTPAGEOFF)
313     .Case("imgrel", VK_COFF_IMGREL32)
314     .Case("secrel32", VK_SECREL)
315     .Case("size", VK_SIZE)
316     .Case("l", VK_PPC_LO)
317     .Case("h", VK_PPC_HI)
318     .Case("ha", VK_PPC_HA)
319     .Case("higher", VK_PPC_HIGHER)
320     .Case("highera", VK_PPC_HIGHERA)
321     .Case("highest", VK_PPC_HIGHEST)
322     .Case("highesta", VK_PPC_HIGHESTA)
323     .Case("got@l", VK_PPC_GOT_LO)
324     .Case("got@h", VK_PPC_GOT_HI)
325     .Case("got@ha", VK_PPC_GOT_HA)
326     .Case("local", VK_PPC_LOCAL)
327     .Case("tocbase", VK_PPC_TOCBASE)
328     .Case("toc", VK_PPC_TOC)
329     .Case("toc@l", VK_PPC_TOC_LO)
330     .Case("toc@h", VK_PPC_TOC_HI)
331     .Case("toc@ha", VK_PPC_TOC_HA)
332     .Case("tls", VK_PPC_TLS)
333     .Case("dtpmod", VK_PPC_DTPMOD)
334     .Case("tprel", VK_PPC_TPREL)
335     .Case("tprel@l", VK_PPC_TPREL_LO)
336     .Case("tprel@h", VK_PPC_TPREL_HI)
337     .Case("tprel@ha", VK_PPC_TPREL_HA)
338     .Case("tprel@higher", VK_PPC_TPREL_HIGHER)
339     .Case("tprel@highera", VK_PPC_TPREL_HIGHERA)
340     .Case("tprel@highest", VK_PPC_TPREL_HIGHEST)
341     .Case("tprel@highesta", VK_PPC_TPREL_HIGHESTA)
342     .Case("dtprel", VK_PPC_DTPREL)
343     .Case("dtprel@l", VK_PPC_DTPREL_LO)
344     .Case("dtprel@h", VK_PPC_DTPREL_HI)
345     .Case("dtprel@ha", VK_PPC_DTPREL_HA)
346     .Case("dtprel@higher", VK_PPC_DTPREL_HIGHER)
347     .Case("dtprel@highera", VK_PPC_DTPREL_HIGHERA)
348     .Case("dtprel@highest", VK_PPC_DTPREL_HIGHEST)
349     .Case("dtprel@highesta", VK_PPC_DTPREL_HIGHESTA)
350     .Case("got@tprel", VK_PPC_GOT_TPREL)
351     .Case("got@tprel@l", VK_PPC_GOT_TPREL_LO)
352     .Case("got@tprel@h", VK_PPC_GOT_TPREL_HI)
353     .Case("got@tprel@ha", VK_PPC_GOT_TPREL_HA)
354     .Case("got@dtprel", VK_PPC_GOT_DTPREL)
355     .Case("got@dtprel@l", VK_PPC_GOT_DTPREL_LO)
356     .Case("got@dtprel@h", VK_PPC_GOT_DTPREL_HI)
357     .Case("got@dtprel@ha", VK_PPC_GOT_DTPREL_HA)
358     .Case("got@tlsgd", VK_PPC_GOT_TLSGD)
359     .Case("got@tlsgd@l", VK_PPC_GOT_TLSGD_LO)
360     .Case("got@tlsgd@h", VK_PPC_GOT_TLSGD_HI)
361     .Case("got@tlsgd@ha", VK_PPC_GOT_TLSGD_HA)
362     .Case("got@tlsld", VK_PPC_GOT_TLSLD)
363     .Case("got@tlsld@l", VK_PPC_GOT_TLSLD_LO)
364     .Case("got@tlsld@h", VK_PPC_GOT_TLSLD_HI)
365     .Case("got@tlsld@ha", VK_PPC_GOT_TLSLD_HA)
366     .Case("none", VK_ARM_NONE)
367     .Case("target1", VK_ARM_TARGET1)
368     .Case("target2", VK_ARM_TARGET2)
369     .Case("prel31", VK_ARM_PREL31)
370     .Case("sbrel", VK_ARM_SBREL)
371     .Case("tlsldo", VK_ARM_TLSLDO)
372     .Case("tlscall", VK_ARM_TLSCALL)
373     .Case("tlsdesc", VK_ARM_TLSDESC)
374     .Default(VK_Invalid);
375 }
376
377 void MCSymbolRefExpr::printVariantKind(raw_ostream &OS) const {
378   if (UseParensForSymbolVariant)
379     OS << '(' << MCSymbolRefExpr::getVariantKindName(getKind()) << ')';
380   else
381     OS << '@' << MCSymbolRefExpr::getVariantKindName(getKind());
382 }
383
384 /* *** */
385
386 void MCTargetExpr::anchor() {}
387
388 /* *** */
389
390 bool MCExpr::EvaluateAsAbsolute(int64_t &Res) const {
391   return EvaluateAsAbsolute(Res, nullptr, nullptr, nullptr);
392 }
393
394 bool MCExpr::EvaluateAsAbsolute(int64_t &Res,
395                                 const MCAsmLayout &Layout) const {
396   return EvaluateAsAbsolute(Res, &Layout.getAssembler(), &Layout, nullptr);
397 }
398
399 bool MCExpr::EvaluateAsAbsolute(int64_t &Res,
400                                 const MCAsmLayout &Layout,
401                                 const SectionAddrMap &Addrs) const {
402   return EvaluateAsAbsolute(Res, &Layout.getAssembler(), &Layout, &Addrs);
403 }
404
405 bool MCExpr::EvaluateAsAbsolute(int64_t &Res, const MCAssembler &Asm) const {
406   return EvaluateAsAbsolute(Res, &Asm, nullptr, nullptr);
407 }
408
409 bool MCExpr::evaluateKnownAbsolute(int64_t &Res,
410                                    const MCAsmLayout &Layout) const {
411   return evaluateAsAbsolute(Res, &Layout.getAssembler(), &Layout, nullptr,
412                             true);
413 }
414
415 bool MCExpr::EvaluateAsAbsolute(int64_t &Res, const MCAssembler *Asm,
416                                 const MCAsmLayout *Layout,
417                                 const SectionAddrMap *Addrs) const {
418   // FIXME: The use if InSet = Addrs is a hack. Setting InSet causes us
419   // absolutize differences across sections and that is what the MachO writer
420   // uses Addrs for.
421   return evaluateAsAbsolute(Res, Asm, Layout, Addrs, Addrs);
422 }
423
424 bool MCExpr::evaluateAsAbsolute(int64_t &Res, const MCAssembler *Asm,
425                                 const MCAsmLayout *Layout,
426                                 const SectionAddrMap *Addrs, bool InSet) const {
427   MCValue Value;
428
429   // Fast path constants.
430   if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(this)) {
431     Res = CE->getValue();
432     return true;
433   }
434
435   bool IsRelocatable =
436       EvaluateAsRelocatableImpl(Value, Asm, Layout, nullptr, Addrs, InSet);
437
438   // Record the current value.
439   Res = Value.getConstant();
440
441   return IsRelocatable && Value.isAbsolute();
442 }
443
444 /// \brief Helper method for \see EvaluateSymbolAdd().
445 static void AttemptToFoldSymbolOffsetDifference(const MCAssembler *Asm,
446                                                 const MCAsmLayout *Layout,
447                                                 const SectionAddrMap *Addrs,
448                                                 bool InSet,
449                                                 const MCSymbolRefExpr *&A,
450                                                 const MCSymbolRefExpr *&B,
451                                                 int64_t &Addend) {
452   if (!A || !B)
453     return;
454
455   const MCSymbol &SA = A->getSymbol();
456   const MCSymbol &SB = B->getSymbol();
457
458   if (SA.isUndefined() || SB.isUndefined())
459     return;
460
461   if (!Asm->getWriter().IsSymbolRefDifferenceFullyResolved(*Asm, A, B, InSet))
462     return;
463
464   const MCSymbolData &AD = Asm->getSymbolData(SA);
465   const MCSymbolData &BD = Asm->getSymbolData(SB);
466
467   if (AD.getFragment() == BD.getFragment()) {
468     Addend += (AD.getOffset() - BD.getOffset());
469
470     // Pointers to Thumb symbols need to have their low-bit set to allow
471     // for interworking.
472     if (Asm->isThumbFunc(&SA))
473       Addend |= 1;
474
475     // Clear the symbol expr pointers to indicate we have folded these
476     // operands.
477     A = B = nullptr;
478     return;
479   }
480
481   if (!Layout)
482     return;
483
484   const MCSectionData &SecA = *AD.getFragment()->getParent();
485   const MCSectionData &SecB = *BD.getFragment()->getParent();
486
487   if ((&SecA != &SecB) && !Addrs)
488     return;
489
490   // Eagerly evaluate.
491   Addend += (Layout->getSymbolOffset(&Asm->getSymbolData(A->getSymbol())) -
492              Layout->getSymbolOffset(&Asm->getSymbolData(B->getSymbol())));
493   if (Addrs && (&SecA != &SecB))
494     Addend += (Addrs->lookup(&SecA) - Addrs->lookup(&SecB));
495
496   // Pointers to Thumb symbols need to have their low-bit set to allow
497   // for interworking.
498   if (Asm->isThumbFunc(&SA))
499     Addend |= 1;
500
501   // Clear the symbol expr pointers to indicate we have folded these
502   // operands.
503   A = B = nullptr;
504 }
505
506 /// \brief Evaluate the result of an add between (conceptually) two MCValues.
507 ///
508 /// This routine conceptually attempts to construct an MCValue:
509 ///   Result = (Result_A - Result_B + Result_Cst)
510 /// from two MCValue's LHS and RHS where
511 ///   Result = LHS + RHS
512 /// and
513 ///   Result = (LHS_A - LHS_B + LHS_Cst) + (RHS_A - RHS_B + RHS_Cst).
514 ///
515 /// This routine attempts to aggresively fold the operands such that the result
516 /// is representable in an MCValue, but may not always succeed.
517 ///
518 /// \returns True on success, false if the result is not representable in an
519 /// MCValue.
520
521 /// NOTE: It is really important to have both the Asm and Layout arguments.
522 /// They might look redundant, but this function can be used before layout
523 /// is done (see the object streamer for example) and having the Asm argument
524 /// lets us avoid relaxations early.
525 static bool EvaluateSymbolicAdd(const MCAssembler *Asm,
526                                 const MCAsmLayout *Layout,
527                                 const SectionAddrMap *Addrs,
528                                 bool InSet,
529                                 const MCValue &LHS,const MCSymbolRefExpr *RHS_A,
530                                 const MCSymbolRefExpr *RHS_B, int64_t RHS_Cst,
531                                 MCValue &Res) {
532   // FIXME: This routine (and other evaluation parts) are *incredibly* sloppy
533   // about dealing with modifiers. This will ultimately bite us, one day.
534   const MCSymbolRefExpr *LHS_A = LHS.getSymA();
535   const MCSymbolRefExpr *LHS_B = LHS.getSymB();
536   int64_t LHS_Cst = LHS.getConstant();
537
538   // Fold the result constant immediately.
539   int64_t Result_Cst = LHS_Cst + RHS_Cst;
540
541   assert((!Layout || Asm) &&
542          "Must have an assembler object if layout is given!");
543
544   // If we have a layout, we can fold resolved differences.
545   if (Asm) {
546     // First, fold out any differences which are fully resolved. By
547     // reassociating terms in
548     //   Result = (LHS_A - LHS_B + LHS_Cst) + (RHS_A - RHS_B + RHS_Cst).
549     // we have the four possible differences:
550     //   (LHS_A - LHS_B),
551     //   (LHS_A - RHS_B),
552     //   (RHS_A - LHS_B),
553     //   (RHS_A - RHS_B).
554     // Since we are attempting to be as aggressive as possible about folding, we
555     // attempt to evaluate each possible alternative.
556     AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, LHS_A, LHS_B,
557                                         Result_Cst);
558     AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, LHS_A, RHS_B,
559                                         Result_Cst);
560     AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, RHS_A, LHS_B,
561                                         Result_Cst);
562     AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, RHS_A, RHS_B,
563                                         Result_Cst);
564   }
565
566   // We can't represent the addition or subtraction of two symbols.
567   if ((LHS_A && RHS_A) || (LHS_B && RHS_B))
568     return false;
569
570   // At this point, we have at most one additive symbol and one subtractive
571   // symbol -- find them.
572   const MCSymbolRefExpr *A = LHS_A ? LHS_A : RHS_A;
573   const MCSymbolRefExpr *B = LHS_B ? LHS_B : RHS_B;
574
575   // If we have a negated symbol, then we must have also have a non-negated
576   // symbol in order to encode the expression.
577   if (B && !A)
578     return false;
579
580   Res = MCValue::get(A, B, Result_Cst);
581   return true;
582 }
583
584 bool MCExpr::EvaluateAsRelocatable(MCValue &Res,
585                                    const MCAsmLayout *Layout,
586                                    const MCFixup *Fixup) const {
587   MCAssembler *Assembler = Layout ? &Layout->getAssembler() : nullptr;
588   return EvaluateAsRelocatableImpl(Res, Assembler, Layout, Fixup, nullptr,
589                                    false);
590 }
591
592 static bool canExpand(const MCSymbol &Sym, const MCAssembler *Asm) {
593   if (!Asm)
594     return false;
595   const MCSymbolData &SD = Asm->getSymbolData(Sym);
596   return !Asm->getWriter().isWeak(SD);
597 }
598
599 bool MCExpr::EvaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm,
600                                        const MCAsmLayout *Layout,
601                                        const MCFixup *Fixup,
602                                        const SectionAddrMap *Addrs,
603                                        bool InSet) const {
604   ++stats::MCExprEvaluate;
605
606   switch (getKind()) {
607   case Target:
608     return cast<MCTargetExpr>(this)->EvaluateAsRelocatableImpl(Res, Layout,
609                                                                Fixup);
610
611   case Constant:
612     Res = MCValue::get(cast<MCConstantExpr>(this)->getValue());
613     return true;
614
615   case SymbolRef: {
616     const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(this);
617     const MCSymbol &Sym = SRE->getSymbol();
618
619     // Evaluate recursively if this is a variable.
620     if (Sym.isVariable() && SRE->getKind() == MCSymbolRefExpr::VK_None &&
621         canExpand(Sym, Asm)) {
622       if (Sym.getVariableValue()->EvaluateAsRelocatableImpl(
623               Res, Asm, Layout, Fixup, Addrs, true)) {
624         if (!SRE->hasSubsectionsViaSymbols())
625           return true;
626
627         const MCSymbolRefExpr *A = Res.getSymA();
628         const MCSymbolRefExpr *B = Res.getSymB();
629         // FIXME: This is small hack. Given
630         // a = b + 4
631         // .long a
632         // the OS X assembler will completely drop the 4. We should probably
633         // include it in the relocation or produce an error if that is not
634         // possible.
635         if (!A && !B)
636           return true;
637       }
638     }
639
640     Res = MCValue::get(SRE, nullptr, 0);
641     return true;
642   }
643
644   case Unary: {
645     const MCUnaryExpr *AUE = cast<MCUnaryExpr>(this);
646     MCValue Value;
647
648     if (!AUE->getSubExpr()->EvaluateAsRelocatableImpl(Value, Asm, Layout, Fixup,
649                                                       Addrs, InSet))
650       return false;
651
652     switch (AUE->getOpcode()) {
653     case MCUnaryExpr::LNot:
654       if (!Value.isAbsolute())
655         return false;
656       Res = MCValue::get(!Value.getConstant());
657       break;
658     case MCUnaryExpr::Minus:
659       /// -(a - b + const) ==> (b - a - const)
660       if (Value.getSymA() && !Value.getSymB())
661         return false;
662       Res = MCValue::get(Value.getSymB(), Value.getSymA(),
663                          -Value.getConstant());
664       break;
665     case MCUnaryExpr::Not:
666       if (!Value.isAbsolute())
667         return false;
668       Res = MCValue::get(~Value.getConstant());
669       break;
670     case MCUnaryExpr::Plus:
671       Res = Value;
672       break;
673     }
674
675     return true;
676   }
677
678   case Binary: {
679     const MCBinaryExpr *ABE = cast<MCBinaryExpr>(this);
680     MCValue LHSValue, RHSValue;
681
682     if (!ABE->getLHS()->EvaluateAsRelocatableImpl(LHSValue, Asm, Layout, Fixup,
683                                                   Addrs, InSet) ||
684         !ABE->getRHS()->EvaluateAsRelocatableImpl(RHSValue, Asm, Layout, Fixup,
685                                                   Addrs, InSet))
686       return false;
687
688     // We only support a few operations on non-constant expressions, handle
689     // those first.
690     if (!LHSValue.isAbsolute() || !RHSValue.isAbsolute()) {
691       switch (ABE->getOpcode()) {
692       default:
693         return false;
694       case MCBinaryExpr::Sub:
695         // Negate RHS and add.
696         return EvaluateSymbolicAdd(Asm, Layout, Addrs, InSet, LHSValue,
697                                    RHSValue.getSymB(), RHSValue.getSymA(),
698                                    -RHSValue.getConstant(),
699                                    Res);
700
701       case MCBinaryExpr::Add:
702         return EvaluateSymbolicAdd(Asm, Layout, Addrs, InSet, LHSValue,
703                                    RHSValue.getSymA(), RHSValue.getSymB(),
704                                    RHSValue.getConstant(),
705                                    Res);
706       }
707     }
708
709     // FIXME: We need target hooks for the evaluation. It may be limited in
710     // width, and gas defines the result of comparisons and right shifts
711     // differently from Apple as.
712     int64_t LHS = LHSValue.getConstant(), RHS = RHSValue.getConstant();
713     int64_t Result = 0;
714     switch (ABE->getOpcode()) {
715     case MCBinaryExpr::Add:  Result = LHS + RHS; break;
716     case MCBinaryExpr::And:  Result = LHS & RHS; break;
717     case MCBinaryExpr::Div:  Result = LHS / RHS; break;
718     case MCBinaryExpr::EQ:   Result = LHS == RHS; break;
719     case MCBinaryExpr::GT:   Result = LHS > RHS; break;
720     case MCBinaryExpr::GTE:  Result = LHS >= RHS; break;
721     case MCBinaryExpr::LAnd: Result = LHS && RHS; break;
722     case MCBinaryExpr::LOr:  Result = LHS || RHS; break;
723     case MCBinaryExpr::LT:   Result = LHS < RHS; break;
724     case MCBinaryExpr::LTE:  Result = LHS <= RHS; break;
725     case MCBinaryExpr::Mod:  Result = LHS % RHS; break;
726     case MCBinaryExpr::Mul:  Result = LHS * RHS; break;
727     case MCBinaryExpr::NE:   Result = LHS != RHS; break;
728     case MCBinaryExpr::Or:   Result = LHS | RHS; break;
729     case MCBinaryExpr::Shl:  Result = LHS << RHS; break;
730     case MCBinaryExpr::Shr:  Result = LHS >> RHS; break;
731     case MCBinaryExpr::Sub:  Result = LHS - RHS; break;
732     case MCBinaryExpr::Xor:  Result = LHS ^ RHS; break;
733     }
734
735     Res = MCValue::get(Result);
736     return true;
737   }
738   }
739
740   llvm_unreachable("Invalid assembly expression kind!");
741 }
742
743 const MCSection *MCExpr::FindAssociatedSection() const {
744   switch (getKind()) {
745   case Target:
746     // We never look through target specific expressions.
747     return cast<MCTargetExpr>(this)->FindAssociatedSection();
748
749   case Constant:
750     return MCSymbol::AbsolutePseudoSection;
751
752   case SymbolRef: {
753     const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(this);
754     const MCSymbol &Sym = SRE->getSymbol();
755
756     if (Sym.isDefined())
757       return &Sym.getSection();
758
759     return nullptr;
760   }
761
762   case Unary:
763     return cast<MCUnaryExpr>(this)->getSubExpr()->FindAssociatedSection();
764
765   case Binary: {
766     const MCBinaryExpr *BE = cast<MCBinaryExpr>(this);
767     const MCSection *LHS_S = BE->getLHS()->FindAssociatedSection();
768     const MCSection *RHS_S = BE->getRHS()->FindAssociatedSection();
769
770     // If either section is absolute, return the other.
771     if (LHS_S == MCSymbol::AbsolutePseudoSection)
772       return RHS_S;
773     if (RHS_S == MCSymbol::AbsolutePseudoSection)
774       return LHS_S;
775
776     // Otherwise, return the first non-null section.
777     return LHS_S ? LHS_S : RHS_S;
778   }
779   }
780
781   llvm_unreachable("Invalid assembly expression kind!");
782 }