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/StringSwitch.h"
12 #include "llvm/MC/MCAsmInfo.h"
13 #include "llvm/MC/MCAsmLayout.h"
14 #include "llvm/MC/MCAssembler.h"
15 #include "llvm/MC/MCContext.h"
16 #include "llvm/MC/MCObjectWriter.h"
17 #include "llvm/MC/MCSymbol.h"
18 #include "llvm/MC/MCValue.h"
19 #include "llvm/Support/Debug.h"
20 #include "llvm/Support/ErrorHandling.h"
21 #include "llvm/Support/raw_ostream.h"
22 using namespace llvm;
23 
24 #define DEBUG_TYPE "mcexpr"
25 
26 
print(raw_ostream & OS,const MCAsmInfo * MAI) const27 void MCExpr::print(raw_ostream &OS, const MCAsmInfo *MAI) const {
28   switch (getKind()) {
29   case MCExpr::Target:
30     return cast<MCTargetExpr>(this)->printImpl(OS, MAI);
31   case MCExpr::Constant:
32     OS << cast<MCConstantExpr>(*this).getValue();
33     return;
34 
35   case MCExpr::SymbolRef: {
36     const MCSymbolRefExpr &SRE = cast<MCSymbolRefExpr>(*this);
37     const MCSymbol &Sym = SRE.getSymbol();
38     // Parenthesize names that start with $ so that they don't look like
39     // absolute names.
40     bool UseParens = Sym.getName().size() && Sym.getName()[0] == '$';
41     if (UseParens) {
42       OS << '(';
43       Sym.print(OS, MAI);
44       OS << ')';
45     } else
46       Sym.print(OS, MAI);
47 
48     if (SRE.getKind() != MCSymbolRefExpr::VK_None)
49       SRE.printVariantKind(OS);
50 
51     return;
52   }
53 
54   case MCExpr::Unary: {
55     const MCUnaryExpr &UE = cast<MCUnaryExpr>(*this);
56     switch (UE.getOpcode()) {
57     case MCUnaryExpr::LNot:  OS << '!'; break;
58     case MCUnaryExpr::Minus: OS << '-'; break;
59     case MCUnaryExpr::Not:   OS << '~'; break;
60     case MCUnaryExpr::Plus:  OS << '+'; break;
61     }
62     UE.getSubExpr()->print(OS, MAI);
63     return;
64   }
65 
66   case MCExpr::Binary: {
67     const MCBinaryExpr &BE = cast<MCBinaryExpr>(*this);
68 
69     // Only print parens around the LHS if it is non-trivial.
70     if (isa<MCConstantExpr>(BE.getLHS()) || isa<MCSymbolRefExpr>(BE.getLHS())) {
71       BE.getLHS()->print(OS, MAI);
72     } else {
73       OS << '(';
74       BE.getLHS()->print(OS, MAI);
75       OS << ')';
76     }
77 
78     switch (BE.getOpcode()) {
79     case MCBinaryExpr::Add:
80       // Print "X-42" instead of "X+-42".
81       if (const MCConstantExpr *RHSC = dyn_cast<MCConstantExpr>(BE.getRHS())) {
82         if (RHSC->getValue() < 0) {
83           OS << RHSC->getValue();
84           return;
85         }
86       }
87 
88       OS <<  '+';
89       break;
90     case MCBinaryExpr::AShr: OS << ">>"; break;
91     case MCBinaryExpr::And:  OS <<  '&'; break;
92     case MCBinaryExpr::Div:  OS <<  '/'; break;
93     case MCBinaryExpr::EQ:   OS << "=="; break;
94     case MCBinaryExpr::GT:   OS <<  '>'; break;
95     case MCBinaryExpr::GTE:  OS << ">="; break;
96     case MCBinaryExpr::LAnd: OS << "&&"; break;
97     case MCBinaryExpr::LOr:  OS << "||"; break;
98     case MCBinaryExpr::LShr: 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::Sub:  OS <<  '-'; break;
107     case MCBinaryExpr::Xor:  OS <<  '^'; break;
108     }
109 
110     // Only print parens around the LHS if it is non-trivial.
111     if (isa<MCConstantExpr>(BE.getRHS()) || isa<MCSymbolRefExpr>(BE.getRHS())) {
112       BE.getRHS()->print(OS, MAI);
113     } else {
114       OS << '(';
115       BE.getRHS()->print(OS, MAI);
116       OS << ')';
117     }
118     return;
119   }
120   }
121 
122   llvm_unreachable("Invalid expression kind!");
123 }
124 
125 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
dump() const126 LLVM_DUMP_METHOD void MCExpr::dump() const {
127 }
128 #endif
129 
130 /* *** */
131 
create(Opcode Opc,const MCExpr * LHS,const MCExpr * RHS,MCContext & Ctx)132 const MCBinaryExpr *MCBinaryExpr::create(Opcode Opc, const MCExpr *LHS,
133                                          const MCExpr *RHS, MCContext &Ctx) {
134   return new (Ctx) MCBinaryExpr(Opc, LHS, RHS);
135 }
136 
create(Opcode Opc,const MCExpr * Expr,MCContext & Ctx)137 const MCUnaryExpr *MCUnaryExpr::create(Opcode Opc, const MCExpr *Expr,
138                                        MCContext &Ctx) {
139   return new (Ctx) MCUnaryExpr(Opc, Expr);
140 }
141 
create(int64_t Value,MCContext & Ctx)142 const MCConstantExpr *MCConstantExpr::create(int64_t Value, MCContext &Ctx) {
143   return new (Ctx) MCConstantExpr(Value);
144 }
145 
146 /* *** */
147 
MCSymbolRefExpr(const MCSymbol * Symbol,VariantKind Kind,const MCAsmInfo * MAI)148 MCSymbolRefExpr::MCSymbolRefExpr(const MCSymbol *Symbol, VariantKind Kind,
149                                  const MCAsmInfo *MAI)
150     : MCExpr(MCExpr::SymbolRef), Kind(Kind),
151       UseParensForSymbolVariant(MAI->useParensForSymbolVariant()),
152       HasSubsectionsViaSymbols(MAI->hasSubsectionsViaSymbols()),
153       Symbol(Symbol) {
154   assert(Symbol);
155 }
156 
create(const MCSymbol * Sym,VariantKind Kind,MCContext & Ctx)157 const MCSymbolRefExpr *MCSymbolRefExpr::create(const MCSymbol *Sym,
158                                                VariantKind Kind,
159                                                MCContext &Ctx) {
160   return new (Ctx) MCSymbolRefExpr(Sym, Kind, Ctx.getAsmInfo());
161 }
162 
create(StringRef Name,VariantKind Kind,MCContext & Ctx)163 const MCSymbolRefExpr *MCSymbolRefExpr::create(StringRef Name, VariantKind Kind,
164                                                MCContext &Ctx) {
165   return create(Ctx.getOrCreateSymbol(Name), Kind, Ctx);
166 }
167 
getVariantKindName(VariantKind Kind)168 StringRef MCSymbolRefExpr::getVariantKindName(VariantKind Kind) {
169   switch (Kind) {
170   case VK_Invalid: return "<<invalid>>";
171   case VK_None: return "<<none>>";
172 
173   case VK_GOT: return "GOT";
174   case VK_GOTOFF: return "GOTOFF";
175   case VK_GOTPCREL: return "GOTPCREL";
176   case VK_GOTTPOFF: return "GOTTPOFF";
177   case VK_INDNTPOFF: return "INDNTPOFF";
178   case VK_NTPOFF: return "NTPOFF";
179   case VK_GOTNTPOFF: return "GOTNTPOFF";
180   case VK_PLT: return "PLT";
181   case VK_TLSGD: return "TLSGD";
182   case VK_TLSLD: return "TLSLD";
183   case VK_TLSLDM: return "TLSLDM";
184   case VK_TPOFF: return "TPOFF";
185   case VK_DTPOFF: return "DTPOFF";
186   case VK_TLVP: return "TLVP";
187   case VK_TLVPPAGE: return "TLVPPAGE";
188   case VK_TLVPPAGEOFF: return "TLVPPAGEOFF";
189   case VK_PAGE: return "PAGE";
190   case VK_PAGEOFF: return "PAGEOFF";
191   case VK_GOTPAGE: return "GOTPAGE";
192   case VK_GOTPAGEOFF: return "GOTPAGEOFF";
193   case VK_SECREL: return "SECREL32";
194   case VK_SIZE: return "SIZE";
195   case VK_WEAKREF: return "WEAKREF";
196   case VK_ARM_NONE: return "none";
197   case VK_ARM_GOT_PREL: return "GOT_PREL";
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   case VK_Hexagon_PCREL: return "PCREL";
286   case VK_Hexagon_LO16: return "LO16";
287   case VK_Hexagon_HI16: return "HI16";
288   case VK_Hexagon_GPREL: return "GPREL";
289   case VK_Hexagon_GD_GOT: return "GDGOT";
290   case VK_Hexagon_LD_GOT: return "LDGOT";
291   case VK_Hexagon_GD_PLT: return "GDPLT";
292   case VK_Hexagon_LD_PLT: return "LDPLT";
293   case VK_Hexagon_IE: return "IE";
294   case VK_Hexagon_IE_GOT: return "IEGOT";
295   case VK_WebAssembly_FUNCTION: return "FUNCTION";
296   case VK_TPREL: return "tprel";
297   case VK_DTPREL: return "dtprel";
298   }
299   llvm_unreachable("Invalid variant kind");
300 }
301 
302 MCSymbolRefExpr::VariantKind
getVariantKindForName(StringRef Name)303 MCSymbolRefExpr::getVariantKindForName(StringRef Name) {
304   return StringSwitch<VariantKind>(Name.lower())
305     .Case("got", VK_GOT)
306     .Case("gotoff", VK_GOTOFF)
307     .Case("gotpcrel", VK_GOTPCREL)
308     .Case("gottpoff", VK_GOTTPOFF)
309     .Case("indntpoff", VK_INDNTPOFF)
310     .Case("ntpoff", VK_NTPOFF)
311     .Case("gotntpoff", VK_GOTNTPOFF)
312     .Case("plt", VK_PLT)
313     .Case("tlsgd", VK_TLSGD)
314     .Case("tlsld", VK_TLSLD)
315     .Case("tlsldm", VK_TLSLDM)
316     .Case("tpoff", VK_TPOFF)
317     .Case("dtpoff", VK_DTPOFF)
318     .Case("tlvp", VK_TLVP)
319     .Case("tlvppage", VK_TLVPPAGE)
320     .Case("tlvppageoff", VK_TLVPPAGEOFF)
321     .Case("page", VK_PAGE)
322     .Case("pageoff", VK_PAGEOFF)
323     .Case("gotpage", VK_GOTPAGE)
324     .Case("gotpageoff", VK_GOTPAGEOFF)
325     .Case("imgrel", VK_COFF_IMGREL32)
326     .Case("secrel32", VK_SECREL)
327     .Case("size", VK_SIZE)
328     .Case("l", VK_PPC_LO)
329     .Case("h", VK_PPC_HI)
330     .Case("ha", VK_PPC_HA)
331     .Case("higher", VK_PPC_HIGHER)
332     .Case("highera", VK_PPC_HIGHERA)
333     .Case("highest", VK_PPC_HIGHEST)
334     .Case("highesta", VK_PPC_HIGHESTA)
335     .Case("got@l", VK_PPC_GOT_LO)
336     .Case("got@h", VK_PPC_GOT_HI)
337     .Case("got@ha", VK_PPC_GOT_HA)
338     .Case("local", VK_PPC_LOCAL)
339     .Case("tocbase", VK_PPC_TOCBASE)
340     .Case("toc", VK_PPC_TOC)
341     .Case("toc@l", VK_PPC_TOC_LO)
342     .Case("toc@h", VK_PPC_TOC_HI)
343     .Case("toc@ha", VK_PPC_TOC_HA)
344     .Case("tls", VK_PPC_TLS)
345     .Case("dtpmod", VK_PPC_DTPMOD)
346     .Case("tprel", VK_PPC_TPREL)
347     .Case("tprel@l", VK_PPC_TPREL_LO)
348     .Case("tprel@h", VK_PPC_TPREL_HI)
349     .Case("tprel@ha", VK_PPC_TPREL_HA)
350     .Case("tprel@higher", VK_PPC_TPREL_HIGHER)
351     .Case("tprel@highera", VK_PPC_TPREL_HIGHERA)
352     .Case("tprel@highest", VK_PPC_TPREL_HIGHEST)
353     .Case("tprel@highesta", VK_PPC_TPREL_HIGHESTA)
354     .Case("dtprel", VK_PPC_DTPREL)
355     .Case("dtprel@l", VK_PPC_DTPREL_LO)
356     .Case("dtprel@h", VK_PPC_DTPREL_HI)
357     .Case("dtprel@ha", VK_PPC_DTPREL_HA)
358     .Case("dtprel@higher", VK_PPC_DTPREL_HIGHER)
359     .Case("dtprel@highera", VK_PPC_DTPREL_HIGHERA)
360     .Case("dtprel@highest", VK_PPC_DTPREL_HIGHEST)
361     .Case("dtprel@highesta", VK_PPC_DTPREL_HIGHESTA)
362     .Case("got@tprel", VK_PPC_GOT_TPREL)
363     .Case("got@tprel@l", VK_PPC_GOT_TPREL_LO)
364     .Case("got@tprel@h", VK_PPC_GOT_TPREL_HI)
365     .Case("got@tprel@ha", VK_PPC_GOT_TPREL_HA)
366     .Case("got@dtprel", VK_PPC_GOT_DTPREL)
367     .Case("got@dtprel@l", VK_PPC_GOT_DTPREL_LO)
368     .Case("got@dtprel@h", VK_PPC_GOT_DTPREL_HI)
369     .Case("got@dtprel@ha", VK_PPC_GOT_DTPREL_HA)
370     .Case("got@tlsgd", VK_PPC_GOT_TLSGD)
371     .Case("got@tlsgd@l", VK_PPC_GOT_TLSGD_LO)
372     .Case("got@tlsgd@h", VK_PPC_GOT_TLSGD_HI)
373     .Case("got@tlsgd@ha", VK_PPC_GOT_TLSGD_HA)
374     .Case("got@tlsld", VK_PPC_GOT_TLSLD)
375     .Case("got@tlsld@l", VK_PPC_GOT_TLSLD_LO)
376     .Case("got@tlsld@h", VK_PPC_GOT_TLSLD_HI)
377     .Case("got@tlsld@ha", VK_PPC_GOT_TLSLD_HA)
378     .Case("gdgot", VK_Hexagon_GD_GOT)
379     .Case("gdplt", VK_Hexagon_GD_PLT)
380     .Case("iegot", VK_Hexagon_IE_GOT)
381     .Case("ie", VK_Hexagon_IE)
382     .Case("ldgot", VK_Hexagon_LD_GOT)
383     .Case("ldplt", VK_Hexagon_LD_PLT)
384     .Case("pcrel", VK_Hexagon_PCREL)
385     .Case("none", VK_ARM_NONE)
386     .Case("got_prel", VK_ARM_GOT_PREL)
387     .Case("target1", VK_ARM_TARGET1)
388     .Case("target2", VK_ARM_TARGET2)
389     .Case("prel31", VK_ARM_PREL31)
390     .Case("sbrel", VK_ARM_SBREL)
391     .Case("tlsldo", VK_ARM_TLSLDO)
392     .Case("tlscall", VK_ARM_TLSCALL)
393     .Case("tlsdesc", VK_ARM_TLSDESC)
394     .Default(VK_Invalid);
395 }
396 
printVariantKind(raw_ostream & OS) const397 void MCSymbolRefExpr::printVariantKind(raw_ostream &OS) const {
398   if (UseParensForSymbolVariant)
399     OS << '(' << MCSymbolRefExpr::getVariantKindName(getKind()) << ')';
400   else
401     OS << '@' << MCSymbolRefExpr::getVariantKindName(getKind());
402 }
403 
404 /* *** */
405 
evaluateAsAbsolute(int64_t & Res) const406 bool MCExpr::evaluateAsAbsolute(int64_t &Res) const {
407   return evaluateAsAbsolute(Res, nullptr, nullptr, nullptr);
408 }
409 
evaluateAsAbsolute(int64_t & Res,const MCAsmLayout & Layout) const410 bool MCExpr::evaluateAsAbsolute(int64_t &Res,
411                                 const MCAsmLayout &Layout) const {
412   return evaluateAsAbsolute(Res, &Layout.getAssembler(), &Layout, nullptr);
413 }
414 
evaluateAsAbsolute(int64_t & Res,const MCAsmLayout & Layout,const SectionAddrMap & Addrs) const415 bool MCExpr::evaluateAsAbsolute(int64_t &Res,
416                                 const MCAsmLayout &Layout,
417                                 const SectionAddrMap &Addrs) const {
418   return evaluateAsAbsolute(Res, &Layout.getAssembler(), &Layout, &Addrs);
419 }
420 
evaluateAsAbsolute(int64_t & Res,const MCAssembler & Asm) const421 bool MCExpr::evaluateAsAbsolute(int64_t &Res, const MCAssembler &Asm) const {
422   return evaluateAsAbsolute(Res, &Asm, nullptr, nullptr);
423 }
424 
evaluateKnownAbsolute(int64_t & Res,const MCAsmLayout & Layout) const425 bool MCExpr::evaluateKnownAbsolute(int64_t &Res,
426                                    const MCAsmLayout &Layout) const {
427   return evaluateAsAbsolute(Res, &Layout.getAssembler(), &Layout, nullptr,
428                             true);
429 }
430 
evaluateAsAbsolute(int64_t & Res,const MCAssembler * Asm,const MCAsmLayout * Layout,const SectionAddrMap * Addrs) const431 bool MCExpr::evaluateAsAbsolute(int64_t &Res, const MCAssembler *Asm,
432                                 const MCAsmLayout *Layout,
433                                 const SectionAddrMap *Addrs) const {
434   // FIXME: The use if InSet = Addrs is a hack. Setting InSet causes us
435   // absolutize differences across sections and that is what the MachO writer
436   // uses Addrs for.
437   return evaluateAsAbsolute(Res, Asm, Layout, Addrs, Addrs);
438 }
439 
evaluateAsAbsolute(int64_t & Res,const MCAssembler * Asm,const MCAsmLayout * Layout,const SectionAddrMap * Addrs,bool InSet) const440 bool MCExpr::evaluateAsAbsolute(int64_t &Res, const MCAssembler *Asm,
441                                 const MCAsmLayout *Layout,
442                                 const SectionAddrMap *Addrs, bool InSet) const
443 {
444   MCValue Value;
445 
446   // Fast path constants.
447   if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(this)) {
448     Res = CE->getValue();
449     return true;
450   }
451 
452   bool valid;
453   bool IsRelocatable =
454       evaluateAsRelocatableImpl(Value, Asm, Layout, nullptr, Addrs, InSet, valid);
455 
456   // Record the current value.
457   Res = Value.getConstant();
458 
459   return IsRelocatable && Value.isAbsolute();
460 }
461 
462 /// \brief Helper method for \see EvaluateSymbolAdd().
AttemptToFoldSymbolOffsetDifference(const MCAssembler * Asm,const MCAsmLayout * Layout,const SectionAddrMap * Addrs,bool InSet,const MCSymbolRefExpr * & A,const MCSymbolRefExpr * & B,int64_t & Addend,bool & valid)463 static void AttemptToFoldSymbolOffsetDifference(
464     const MCAssembler *Asm, const MCAsmLayout *Layout,
465     const SectionAddrMap *Addrs, bool InSet, const MCSymbolRefExpr *&A,
466     const MCSymbolRefExpr *&B, int64_t &Addend, bool &valid) {
467   valid = true;
468   if (!A || !B)
469     return;
470 
471   const MCSymbol &SA = A->getSymbol();
472   const MCSymbol &SB = B->getSymbol();
473 
474   if (SA.isUndefined() || SB.isUndefined())
475     return;
476 
477   if (!Asm->getWriter().isSymbolRefDifferenceFullyResolved(*Asm, A, B, InSet, valid))
478     return;
479   if (!valid)
480       return;
481 
482   if (SA.getFragment() == SB.getFragment() && !SA.isVariable() &&
483       !SB.isVariable()) {
484     Addend += (SA.getOffset() - SB.getOffset());
485 
486     // Pointers to Thumb symbols need to have their low-bit set to allow
487     // for interworking.
488     if (Asm->isThumbFunc(&SA))
489       Addend |= 1;
490 
491     // Clear the symbol expr pointers to indicate we have folded these
492     // operands.
493     A = B = nullptr;
494     return;
495   }
496 
497   if (!Layout)
498     return;
499 
500   const MCSection &SecA = *SA.getFragment()->getParent();
501   const MCSection &SecB = *SB.getFragment()->getParent();
502 
503   if ((&SecA != &SecB) && !Addrs)
504     return;
505 
506   // Eagerly evaluate.
507   bool valid1, valid2;
508   Addend += Layout->getSymbolOffset(A->getSymbol(), valid1) -
509             Layout->getSymbolOffset(B->getSymbol(), valid2);
510   if (Addrs && (&SecA != &SecB))
511     Addend += (Addrs->lookup(&SecA) - Addrs->lookup(&SecB));
512 
513   // Pointers to Thumb symbols need to have their low-bit set to allow
514   // for interworking.
515   if (Asm->isThumbFunc(&SA))
516     Addend |= 1;
517 
518   // Clear the symbol expr pointers to indicate we have folded these
519   // operands.
520   A = B = nullptr;
521 }
522 
523 /// \brief Evaluate the result of an add between (conceptually) two MCValues.
524 ///
525 /// This routine conceptually attempts to construct an MCValue:
526 ///   Result = (Result_A - Result_B + Result_Cst)
527 /// from two MCValue's LHS and RHS where
528 ///   Result = LHS + RHS
529 /// and
530 ///   Result = (LHS_A - LHS_B + LHS_Cst) + (RHS_A - RHS_B + RHS_Cst).
531 ///
532 /// This routine attempts to aggresively fold the operands such that the result
533 /// is representable in an MCValue, but may not always succeed.
534 ///
535 /// \returns True on success, false if the result is not representable in an
536 /// MCValue.
537 
538 /// NOTE: It is really important to have both the Asm and Layout arguments.
539 /// They might look redundant, but this function can be used before layout
540 /// is done (see the object streamer for example) and having the Asm argument
541 /// lets us avoid relaxations early.
542 static bool
EvaluateSymbolicAdd(const MCAssembler * Asm,const MCAsmLayout * Layout,const SectionAddrMap * Addrs,bool InSet,const MCValue & LHS,const MCSymbolRefExpr * RHS_A,const MCSymbolRefExpr * RHS_B,int64_t RHS_Cst,MCValue & Res,bool & valid)543 EvaluateSymbolicAdd(const MCAssembler *Asm, const MCAsmLayout *Layout,
544                     const SectionAddrMap *Addrs, bool InSet, const MCValue &LHS,
545                     const MCSymbolRefExpr *RHS_A, const MCSymbolRefExpr *RHS_B,
546                     int64_t RHS_Cst, MCValue &Res, bool &valid)
547 {
548   // FIXME: This routine (and other evaluation parts) are *incredibly* sloppy
549   // about dealing with modifiers. This will ultimately bite us, one day.
550   const MCSymbolRefExpr *LHS_A = LHS.getSymA();
551   const MCSymbolRefExpr *LHS_B = LHS.getSymB();
552   int64_t LHS_Cst = LHS.getConstant();
553 
554   // Fold the result constant immediately.
555   int64_t Result_Cst = LHS_Cst + RHS_Cst;
556 
557   assert((!Layout || Asm) &&
558          "Must have an assembler object if layout is given!");
559 
560   // If we have a layout, we can fold resolved differences.
561   if (Asm) {
562     // First, fold out any differences which are fully resolved. By
563     // reassociating terms in
564     //   Result = (LHS_A - LHS_B + LHS_Cst) + (RHS_A - RHS_B + RHS_Cst).
565     // we have the four possible differences:
566     //   (LHS_A - LHS_B),
567     //   (LHS_A - RHS_B),
568     //   (RHS_A - LHS_B),
569     //   (RHS_A - RHS_B).
570     // Since we are attempting to be as aggressive as possible about folding, we
571     // attempt to evaluate each possible alternative.
572     AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, LHS_A, LHS_B,
573                                         Result_Cst, valid);
574     if (!valid)
575         return false;
576     AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, LHS_A, RHS_B,
577                                         Result_Cst, valid);
578     if (!valid)
579         return false;
580     AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, RHS_A, LHS_B,
581                                         Result_Cst, valid);
582     if (!valid)
583         return false;
584     AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, RHS_A, RHS_B,
585                                         Result_Cst, valid);
586     if (!valid)
587         return false;
588   }
589 
590   // We can't represent the addition or subtraction of two symbols.
591   if ((LHS_A && RHS_A) || (LHS_B && RHS_B))
592     return false;
593 
594   // At this point, we have at most one additive symbol and one subtractive
595   // symbol -- find them.
596   const MCSymbolRefExpr *A = LHS_A ? LHS_A : RHS_A;
597   const MCSymbolRefExpr *B = LHS_B ? LHS_B : RHS_B;
598 
599   Res = MCValue::get(A, B, Result_Cst);
600   return true;
601 }
602 
evaluateAsRelocatable(MCValue & Res,const MCAsmLayout * Layout,const MCFixup * Fixup) const603 bool MCExpr::evaluateAsRelocatable(MCValue &Res,
604                                    const MCAsmLayout *Layout,
605                                    const MCFixup *Fixup) const
606 {
607   MCAssembler *Assembler = Layout ? &Layout->getAssembler() : nullptr;
608   bool valid;
609   return evaluateAsRelocatableImpl(Res, Assembler, Layout, Fixup, nullptr,
610                                    false, valid);
611 }
612 
evaluateAsValue(MCValue & Res,const MCAsmLayout & Layout) const613 bool MCExpr::evaluateAsValue(MCValue &Res, const MCAsmLayout &Layout) const
614 {
615   MCAssembler *Assembler = &Layout.getAssembler();
616   bool valid;
617   return evaluateAsRelocatableImpl(Res, Assembler, &Layout, nullptr, nullptr,
618                                    true, valid);
619 }
620 
canExpand(const MCSymbol & Sym,bool InSet)621 static bool canExpand(const MCSymbol &Sym, bool InSet) {
622   const MCExpr *Expr = Sym.getVariableValue();
623   const auto *Inner = dyn_cast<MCSymbolRefExpr>(Expr);
624   if (Inner) {
625     if (Inner->getKind() == MCSymbolRefExpr::VK_WEAKREF)
626       return false;
627   }
628 
629   if (InSet)
630     return true;
631   return !Sym.isInSection();
632 }
633 
evaluateAsRelocatableImpl(MCValue & Res,const MCAssembler * Asm,const MCAsmLayout * Layout,const MCFixup * Fixup,const SectionAddrMap * Addrs,bool InSet,bool & valid) const634 bool MCExpr::evaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm,
635                                        const MCAsmLayout *Layout,
636                                        const MCFixup *Fixup,
637                                        const SectionAddrMap *Addrs,
638                                        bool InSet, bool &valid) const
639 {
640   switch (getKind()) {
641   case Target:
642     return cast<MCTargetExpr>(this)->evaluateAsRelocatableImpl(Res, Layout,
643                                                                Fixup);
644 
645   case Constant:
646     Res = MCValue::get(cast<MCConstantExpr>(this)->getValue());
647     return true;
648 
649   case SymbolRef: {
650     const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(this);
651     const MCSymbol &Sym = SRE->getSymbol();
652 
653     // Evaluate recursively if this is a variable.
654     if (Sym.isVariable() && SRE->getKind() == MCSymbolRefExpr::VK_None &&
655         canExpand(Sym, InSet)) {
656       bool IsMachO = SRE->hasSubsectionsViaSymbols();
657       bool valid;
658       if (Sym.getVariableValue()->evaluateAsRelocatableImpl(
659               Res, Asm, Layout, Fixup, Addrs, InSet || IsMachO, valid)) {
660         if (!IsMachO)
661           return true;
662 
663         const MCSymbolRefExpr *A = Res.getSymA();
664         const MCSymbolRefExpr *B = Res.getSymB();
665         // FIXME: This is small hack. Given
666         // a = b + 4
667         // .long a
668         // the OS X assembler will completely drop the 4. We should probably
669         // include it in the relocation or produce an error if that is not
670         // possible.
671         if (!A && !B)
672           return true;
673       }
674     }
675 
676     Res = MCValue::get(SRE, nullptr, 0);
677     return true;
678   }
679 
680   case Unary: {
681     const MCUnaryExpr *AUE = cast<MCUnaryExpr>(this);
682     MCValue Value;
683 
684     bool valid;
685     if (!AUE->getSubExpr()->evaluateAsRelocatableImpl(Value, Asm, Layout, Fixup,
686                                                       Addrs, InSet, valid))
687       return false;
688 
689     switch (AUE->getOpcode()) {
690     case MCUnaryExpr::LNot:
691       if (!Value.isAbsolute())
692         return false;
693       Res = MCValue::get(!Value.getConstant());
694       break;
695     case MCUnaryExpr::Minus:
696       /// -(a - b + const) ==> (b - a - const)
697       if (Value.getSymA() && !Value.getSymB())
698         return false;
699       Res = MCValue::get(Value.getSymB(), Value.getSymA(),
700                          -Value.getConstant());
701       break;
702     case MCUnaryExpr::Not:
703       if (!Value.isAbsolute())
704         return false;
705       Res = MCValue::get(~Value.getConstant());
706       break;
707     case MCUnaryExpr::Plus:
708       Res = Value;
709       break;
710     }
711 
712     return true;
713   }
714 
715   case Binary: {
716     const MCBinaryExpr *ABE = cast<MCBinaryExpr>(this);
717     MCValue LHSValue, RHSValue;
718     bool valid;
719 
720     if (!ABE->getLHS()->evaluateAsRelocatableImpl(LHSValue, Asm, Layout, Fixup,
721                                                   Addrs, InSet, valid) ||
722         !ABE->getRHS()->evaluateAsRelocatableImpl(RHSValue, Asm, Layout, Fixup,
723                                                   Addrs, InSet, valid))
724       return false;
725 
726     // We only support a few operations on non-constant expressions, handle
727     // those first.
728     if (!LHSValue.isAbsolute() || !RHSValue.isAbsolute()) {
729       switch (ABE->getOpcode()) {
730       default:
731         return false;
732       case MCBinaryExpr::Sub:
733         // Negate RHS and add.
734         return EvaluateSymbolicAdd(Asm, Layout, Addrs, InSet, LHSValue,
735                                    RHSValue.getSymB(), RHSValue.getSymA(),
736                                    -RHSValue.getConstant(), Res, valid);
737 
738       case MCBinaryExpr::Add:
739         return EvaluateSymbolicAdd(Asm, Layout, Addrs, InSet, LHSValue,
740                                    RHSValue.getSymA(), RHSValue.getSymB(),
741                                    RHSValue.getConstant(), Res, valid);
742       }
743     }
744 
745     // FIXME: We need target hooks for the evaluation. It may be limited in
746     // width, and gas defines the result of comparisons differently from
747     // Apple as.
748     int64_t LHS = LHSValue.getConstant(), RHS = RHSValue.getConstant();
749     int64_t Result = 0;
750     switch (ABE->getOpcode()) {
751     case MCBinaryExpr::AShr: Result = LHS >> RHS; break;
752     case MCBinaryExpr::Add:  Result = LHS + RHS; break;
753     case MCBinaryExpr::And:  Result = LHS & RHS; break;
754     case MCBinaryExpr::Div:
755       // Handle division by zero. gas just emits a warning and keeps going,
756       // we try to be stricter.
757       // FIXME: Currently the caller of this function has no way to understand
758       // we're bailing out because of 'division by zero'. Therefore, it will
759       // emit a 'expected relocatable expression' error. It would be nice to
760       // change this code to emit a better diagnostic.
761       if (RHS == 0)
762         return false;
763       Result = LHS / RHS;
764       break;
765     case MCBinaryExpr::EQ:   Result = LHS == RHS; break;
766     case MCBinaryExpr::GT:   Result = LHS > RHS; break;
767     case MCBinaryExpr::GTE:  Result = LHS >= RHS; break;
768     case MCBinaryExpr::LAnd: Result = LHS && RHS; break;
769     case MCBinaryExpr::LOr:  Result = LHS || RHS; break;
770     case MCBinaryExpr::LShr: Result = uint64_t(LHS) >> uint64_t(RHS); break;
771     case MCBinaryExpr::LT:   Result = LHS < RHS; break;
772     case MCBinaryExpr::LTE:  Result = LHS <= RHS; break;
773     case MCBinaryExpr::Mod:
774       // Handle division by zero. gas just emits a warning and keeps going,
775       // we try to be stricter.
776       // FIXME: Currently the caller of this function has no way to understand
777       // we're bailing out because of 'division by zero'. Therefore, it will
778       // emit a 'expected relocatable expression' error. It would be nice to
779       // change this code to emit a better diagnostic.
780       if (RHS == 0)
781         return false;
782       Result = LHS % RHS;
783       break;
784     case MCBinaryExpr::Mul:  Result = LHS * RHS; break;
785     case MCBinaryExpr::NE:   Result = LHS != RHS; break;
786     case MCBinaryExpr::Or:   Result = LHS | RHS; break;
787     case MCBinaryExpr::Shl:  Result = uint64_t(LHS) << uint64_t(RHS); break;
788     case MCBinaryExpr::Sub:  Result = LHS - RHS; break;
789     case MCBinaryExpr::Xor:  Result = LHS ^ RHS; break;
790     }
791 
792     Res = MCValue::get(Result);
793     return true;
794   }
795   }
796 
797   llvm_unreachable("Invalid assembly expression kind!");
798 }
799 
findAssociatedFragment() const800 MCFragment *MCExpr::findAssociatedFragment() const {
801   switch (getKind()) {
802   case Target:
803     // We never look through target specific expressions.
804     return cast<MCTargetExpr>(this)->findAssociatedFragment();
805 
806   case Constant:
807     return MCSymbol::AbsolutePseudoFragment;
808 
809   case SymbolRef: {
810     const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(this);
811     const MCSymbol &Sym = SRE->getSymbol();
812     return Sym.getFragment();
813   }
814 
815   case Unary:
816     return cast<MCUnaryExpr>(this)->getSubExpr()->findAssociatedFragment();
817 
818   case Binary: {
819     const MCBinaryExpr *BE = cast<MCBinaryExpr>(this);
820     MCFragment *LHS_F = BE->getLHS()->findAssociatedFragment();
821     MCFragment *RHS_F = BE->getRHS()->findAssociatedFragment();
822 
823     // If either is absolute, return the other.
824     if (LHS_F == MCSymbol::AbsolutePseudoFragment)
825       return RHS_F;
826     if (RHS_F == MCSymbol::AbsolutePseudoFragment)
827       return LHS_F;
828 
829     // Not always correct, but probably the best we can do without more context.
830     if (BE->getOpcode() == MCBinaryExpr::Sub)
831       return MCSymbol::AbsolutePseudoFragment;
832 
833     // Otherwise, return the first non-null fragment.
834     return LHS_F ? LHS_F : RHS_F;
835   }
836   }
837 
838   llvm_unreachable("Invalid assembly expression kind!");
839 }
840