1 //===- MCExpr.cpp - Assembly Level Expression Implementation --------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #define DEBUG_TYPE "mcexpr"
11 #include "llvm/MC/MCExpr.h"
12 #include "llvm/ADT/Statistic.h"
13 #include "llvm/ADT/StringSwitch.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"
27 STATISTIC(MCExprEvaluate, "Number of MCExpr evaluations");
31 void MCExpr::print(raw_ostream &OS) const {
34 return cast<MCTargetExpr>(this)->PrintImpl(OS);
35 case MCExpr::Constant:
36 OS << cast<MCConstantExpr>(*this).getValue();
39 case MCExpr::SymbolRef: {
40 const MCSymbolRefExpr &SRE = cast<MCSymbolRefExpr>(*this);
41 const MCSymbol &Sym = SRE.getSymbol();
42 // Parenthesize names that start with $ so that they don't look like
44 bool UseParens = Sym.getName()[0] == '$';
46 OS << '(' << Sym << ')';
50 if (SRE.getKind() == MCSymbolRefExpr::VK_ARM_NONE ||
51 SRE.getKind() == MCSymbolRefExpr::VK_ARM_PLT ||
52 SRE.getKind() == MCSymbolRefExpr::VK_ARM_TLSGD ||
53 SRE.getKind() == MCSymbolRefExpr::VK_ARM_GOT ||
54 SRE.getKind() == MCSymbolRefExpr::VK_ARM_GOTOFF ||
55 SRE.getKind() == MCSymbolRefExpr::VK_ARM_TPOFF ||
56 SRE.getKind() == MCSymbolRefExpr::VK_ARM_GOTTPOFF ||
57 SRE.getKind() == MCSymbolRefExpr::VK_ARM_TARGET1 ||
58 SRE.getKind() == MCSymbolRefExpr::VK_ARM_TARGET2 ||
59 SRE.getKind() == MCSymbolRefExpr::VK_ARM_PREL31)
60 OS << MCSymbolRefExpr::getVariantKindName(SRE.getKind());
61 else if (SRE.getKind() != MCSymbolRefExpr::VK_None)
62 OS << '@' << MCSymbolRefExpr::getVariantKindName(SRE.getKind());
68 const MCUnaryExpr &UE = cast<MCUnaryExpr>(*this);
69 switch (UE.getOpcode()) {
70 case MCUnaryExpr::LNot: OS << '!'; break;
71 case MCUnaryExpr::Minus: OS << '-'; break;
72 case MCUnaryExpr::Not: OS << '~'; break;
73 case MCUnaryExpr::Plus: OS << '+'; break;
75 OS << *UE.getSubExpr();
79 case MCExpr::Binary: {
80 const MCBinaryExpr &BE = cast<MCBinaryExpr>(*this);
82 // Only print parens around the LHS if it is non-trivial.
83 if (isa<MCConstantExpr>(BE.getLHS()) || isa<MCSymbolRefExpr>(BE.getLHS())) {
86 OS << '(' << *BE.getLHS() << ')';
89 switch (BE.getOpcode()) {
90 case MCBinaryExpr::Add:
91 // Print "X-42" instead of "X+-42".
92 if (const MCConstantExpr *RHSC = dyn_cast<MCConstantExpr>(BE.getRHS())) {
93 if (RHSC->getValue() < 0) {
94 OS << RHSC->getValue();
101 case MCBinaryExpr::And: OS << '&'; break;
102 case MCBinaryExpr::Div: OS << '/'; break;
103 case MCBinaryExpr::EQ: OS << "=="; break;
104 case MCBinaryExpr::GT: OS << '>'; break;
105 case MCBinaryExpr::GTE: OS << ">="; break;
106 case MCBinaryExpr::LAnd: OS << "&&"; break;
107 case MCBinaryExpr::LOr: OS << "||"; break;
108 case MCBinaryExpr::LT: OS << '<'; break;
109 case MCBinaryExpr::LTE: OS << "<="; break;
110 case MCBinaryExpr::Mod: OS << '%'; break;
111 case MCBinaryExpr::Mul: OS << '*'; break;
112 case MCBinaryExpr::NE: OS << "!="; break;
113 case MCBinaryExpr::Or: OS << '|'; break;
114 case MCBinaryExpr::Shl: OS << "<<"; break;
115 case MCBinaryExpr::Shr: OS << ">>"; break;
116 case MCBinaryExpr::Sub: OS << '-'; break;
117 case MCBinaryExpr::Xor: OS << '^'; break;
120 // Only print parens around the LHS if it is non-trivial.
121 if (isa<MCConstantExpr>(BE.getRHS()) || isa<MCSymbolRefExpr>(BE.getRHS())) {
124 OS << '(' << *BE.getRHS() << ')';
130 llvm_unreachable("Invalid expression kind!");
133 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
134 void MCExpr::dump() const {
142 const MCBinaryExpr *MCBinaryExpr::Create(Opcode Opc, const MCExpr *LHS,
143 const MCExpr *RHS, MCContext &Ctx) {
144 return new (Ctx) MCBinaryExpr(Opc, LHS, RHS);
147 const MCUnaryExpr *MCUnaryExpr::Create(Opcode Opc, const MCExpr *Expr,
149 return new (Ctx) MCUnaryExpr(Opc, Expr);
152 const MCConstantExpr *MCConstantExpr::Create(int64_t Value, MCContext &Ctx) {
153 return new (Ctx) MCConstantExpr(Value);
158 const MCSymbolRefExpr *MCSymbolRefExpr::Create(const MCSymbol *Sym,
161 return new (Ctx) MCSymbolRefExpr(Sym, Kind);
164 const MCSymbolRefExpr *MCSymbolRefExpr::Create(StringRef Name, VariantKind Kind,
166 return Create(Ctx.GetOrCreateSymbol(Name), Kind, Ctx);
169 StringRef MCSymbolRefExpr::getVariantKindName(VariantKind Kind) {
171 case VK_Invalid: return "<<invalid>>";
172 case VK_None: return "<<none>>";
174 case VK_GOT: return "GOT";
175 case VK_GOTOFF: return "GOTOFF";
176 case VK_GOTPCREL: return "GOTPCREL";
177 case VK_GOTTPOFF: return "GOTTPOFF";
178 case VK_INDNTPOFF: return "INDNTPOFF";
179 case VK_NTPOFF: return "NTPOFF";
180 case VK_GOTNTPOFF: return "GOTNTPOFF";
181 case VK_PLT: return "PLT";
182 case VK_TLSGD: return "TLSGD";
183 case VK_TLSLD: return "TLSLD";
184 case VK_TLSLDM: return "TLSLDM";
185 case VK_TPOFF: return "TPOFF";
186 case VK_DTPOFF: return "DTPOFF";
187 case VK_TLVP: return "TLVP";
188 case VK_SECREL: return "SECREL32";
189 case VK_ARM_NONE: return "(NONE)";
190 case VK_ARM_PLT: return "(PLT)";
191 case VK_ARM_GOT: return "(GOT)";
192 case VK_ARM_GOTOFF: return "(GOTOFF)";
193 case VK_ARM_TPOFF: return "(tpoff)";
194 case VK_ARM_GOTTPOFF: return "(gottpoff)";
195 case VK_ARM_TLSGD: return "(tlsgd)";
196 case VK_ARM_TARGET1: return "(target1)";
197 case VK_ARM_TARGET2: return "(target2)";
198 case VK_ARM_PREL31: return "(prel31)";
199 case VK_PPC_LO: return "l";
200 case VK_PPC_HI: return "h";
201 case VK_PPC_HA: return "ha";
202 case VK_PPC_HIGHER: return "higher";
203 case VK_PPC_HIGHERA: return "highera";
204 case VK_PPC_HIGHEST: return "highest";
205 case VK_PPC_HIGHESTA: return "highesta";
206 case VK_PPC_GOT_LO: return "got@l";
207 case VK_PPC_GOT_HI: return "got@h";
208 case VK_PPC_GOT_HA: return "got@ha";
209 case VK_PPC_TOCBASE: return "tocbase";
210 case VK_PPC_TOC: return "toc";
211 case VK_PPC_TOC_LO: return "toc@l";
212 case VK_PPC_TOC_HI: return "toc@h";
213 case VK_PPC_TOC_HA: return "toc@ha";
214 case VK_PPC_DTPMOD: return "dtpmod";
215 case VK_PPC_TPREL: return "tprel";
216 case VK_PPC_TPREL_LO: return "tprel@l";
217 case VK_PPC_TPREL_HI: return "tprel@h";
218 case VK_PPC_TPREL_HA: return "tprel@ha";
219 case VK_PPC_TPREL_HIGHER: return "tprel@higher";
220 case VK_PPC_TPREL_HIGHERA: return "tprel@highera";
221 case VK_PPC_TPREL_HIGHEST: return "tprel@highest";
222 case VK_PPC_TPREL_HIGHESTA: return "tprel@highesta";
223 case VK_PPC_DTPREL: return "dtprel";
224 case VK_PPC_DTPREL_LO: return "dtprel@l";
225 case VK_PPC_DTPREL_HI: return "dtprel@h";
226 case VK_PPC_DTPREL_HA: return "dtprel@ha";
227 case VK_PPC_DTPREL_HIGHER: return "dtprel@higher";
228 case VK_PPC_DTPREL_HIGHERA: return "dtprel@highera";
229 case VK_PPC_DTPREL_HIGHEST: return "dtprel@highest";
230 case VK_PPC_DTPREL_HIGHESTA: return "dtprel@highesta";
231 case VK_PPC_GOT_TPREL: return "got@tprel";
232 case VK_PPC_GOT_TPREL_LO: return "got@tprel@l";
233 case VK_PPC_GOT_TPREL_HI: return "got@tprel@h";
234 case VK_PPC_GOT_TPREL_HA: return "got@tprel@ha";
235 case VK_PPC_GOT_DTPREL: return "got@dtprel";
236 case VK_PPC_GOT_DTPREL_LO: return "got@dtprel@l";
237 case VK_PPC_GOT_DTPREL_HI: return "got@dtprel@h";
238 case VK_PPC_GOT_DTPREL_HA: return "got@dtprel@ha";
239 case VK_PPC_TLS: return "tls";
240 case VK_PPC_GOT_TLSGD: return "got@tlsgd";
241 case VK_PPC_GOT_TLSGD_LO: return "got@tlsgd@l";
242 case VK_PPC_GOT_TLSGD_HI: return "got@tlsgd@h";
243 case VK_PPC_GOT_TLSGD_HA: return "got@tlsgd@ha";
244 case VK_PPC_TLSGD: return "tlsgd";
245 case VK_PPC_GOT_TLSLD: return "got@tlsld";
246 case VK_PPC_GOT_TLSLD_LO: return "got@tlsld@l";
247 case VK_PPC_GOT_TLSLD_HI: return "got@tlsld@h";
248 case VK_PPC_GOT_TLSLD_HA: return "got@tlsld@ha";
249 case VK_PPC_TLSLD: return "tlsld";
250 case VK_Mips_GPREL: return "GPREL";
251 case VK_Mips_GOT_CALL: return "GOT_CALL";
252 case VK_Mips_GOT16: return "GOT16";
253 case VK_Mips_GOT: return "GOT";
254 case VK_Mips_ABS_HI: return "ABS_HI";
255 case VK_Mips_ABS_LO: return "ABS_LO";
256 case VK_Mips_TLSGD: return "TLSGD";
257 case VK_Mips_TLSLDM: return "TLSLDM";
258 case VK_Mips_DTPREL_HI: return "DTPREL_HI";
259 case VK_Mips_DTPREL_LO: return "DTPREL_LO";
260 case VK_Mips_GOTTPREL: return "GOTTPREL";
261 case VK_Mips_TPREL_HI: return "TPREL_HI";
262 case VK_Mips_TPREL_LO: return "TPREL_LO";
263 case VK_Mips_GPOFF_HI: return "GPOFF_HI";
264 case VK_Mips_GPOFF_LO: return "GPOFF_LO";
265 case VK_Mips_GOT_DISP: return "GOT_DISP";
266 case VK_Mips_GOT_PAGE: return "GOT_PAGE";
267 case VK_Mips_GOT_OFST: return "GOT_OFST";
268 case VK_Mips_HIGHER: return "HIGHER";
269 case VK_Mips_HIGHEST: return "HIGHEST";
270 case VK_Mips_GOT_HI16: return "GOT_HI16";
271 case VK_Mips_GOT_LO16: return "GOT_LO16";
272 case VK_Mips_CALL_HI16: return "CALL_HI16";
273 case VK_Mips_CALL_LO16: return "CALL_LO16";
274 case VK_COFF_IMGREL32: return "IMGREL32";
276 llvm_unreachable("Invalid variant kind");
279 MCSymbolRefExpr::VariantKind
280 MCSymbolRefExpr::getVariantKindForName(StringRef Name) {
281 return StringSwitch<VariantKind>(Name)
284 .Case("GOTOFF", VK_GOTOFF)
285 .Case("gotoff", VK_GOTOFF)
286 .Case("GOTPCREL", VK_GOTPCREL)
287 .Case("gotpcrel", VK_GOTPCREL)
288 .Case("GOTTPOFF", VK_GOTTPOFF)
289 .Case("gottpoff", VK_GOTTPOFF)
290 .Case("INDNTPOFF", VK_INDNTPOFF)
291 .Case("indntpoff", VK_INDNTPOFF)
292 .Case("NTPOFF", VK_NTPOFF)
293 .Case("ntpoff", VK_NTPOFF)
294 .Case("GOTNTPOFF", VK_GOTNTPOFF)
295 .Case("gotntpoff", VK_GOTNTPOFF)
298 .Case("TLSGD", VK_TLSGD)
299 .Case("tlsgd", VK_TLSGD)
300 .Case("TLSLD", VK_TLSLD)
301 .Case("tlsld", VK_TLSLD)
302 .Case("TLSLDM", VK_TLSLDM)
303 .Case("tlsldm", VK_TLSLDM)
304 .Case("TPOFF", VK_TPOFF)
305 .Case("tpoff", VK_TPOFF)
306 .Case("DTPOFF", VK_DTPOFF)
307 .Case("dtpoff", VK_DTPOFF)
308 .Case("TLVP", VK_TLVP)
309 .Case("tlvp", VK_TLVP)
310 .Case("IMGREL", VK_COFF_IMGREL32)
311 .Case("imgrel", VK_COFF_IMGREL32)
312 .Case("SECREL32", VK_SECREL)
313 .Case("secrel32", VK_SECREL)
314 .Case("L", VK_PPC_LO)
315 .Case("l", VK_PPC_LO)
316 .Case("H", VK_PPC_HI)
317 .Case("h", VK_PPC_HI)
318 .Case("HA", VK_PPC_HA)
319 .Case("ha", VK_PPC_HA)
320 .Case("HIGHER", VK_PPC_HIGHER)
321 .Case("higher", VK_PPC_HIGHER)
322 .Case("HIGHERA", VK_PPC_HIGHERA)
323 .Case("highera", VK_PPC_HIGHERA)
324 .Case("HIGHEST", VK_PPC_HIGHEST)
325 .Case("highest", VK_PPC_HIGHEST)
326 .Case("HIGHESTA", VK_PPC_HIGHESTA)
327 .Case("highesta", VK_PPC_HIGHESTA)
328 .Case("GOT@L", VK_PPC_GOT_LO)
329 .Case("got@l", VK_PPC_GOT_LO)
330 .Case("GOT@H", VK_PPC_GOT_HI)
331 .Case("got@h", VK_PPC_GOT_HI)
332 .Case("GOT@HA", VK_PPC_GOT_HA)
333 .Case("got@ha", VK_PPC_GOT_HA)
334 .Case("TOCBASE", VK_PPC_TOCBASE)
335 .Case("tocbase", VK_PPC_TOCBASE)
336 .Case("TOC", VK_PPC_TOC)
337 .Case("toc", VK_PPC_TOC)
338 .Case("TOC@L", VK_PPC_TOC_LO)
339 .Case("toc@l", VK_PPC_TOC_LO)
340 .Case("TOC@H", VK_PPC_TOC_HI)
341 .Case("toc@h", VK_PPC_TOC_HI)
342 .Case("TOC@HA", VK_PPC_TOC_HA)
343 .Case("toc@ha", VK_PPC_TOC_HA)
344 .Case("TLS", VK_PPC_TLS)
345 .Case("tls", VK_PPC_TLS)
346 .Case("DTPMOD", VK_PPC_DTPMOD)
347 .Case("dtpmod", VK_PPC_DTPMOD)
348 .Case("TPREL", VK_PPC_TPREL)
349 .Case("tprel", VK_PPC_TPREL)
350 .Case("TPREL@L", VK_PPC_TPREL_LO)
351 .Case("tprel@l", VK_PPC_TPREL_LO)
352 .Case("TPREL@H", VK_PPC_TPREL_HI)
353 .Case("tprel@h", VK_PPC_TPREL_HI)
354 .Case("TPREL@HA", VK_PPC_TPREL_HA)
355 .Case("tprel@ha", VK_PPC_TPREL_HA)
356 .Case("TPREL@HIGHER", VK_PPC_TPREL_HIGHER)
357 .Case("tprel@higher", VK_PPC_TPREL_HIGHER)
358 .Case("TPREL@HIGHERA", VK_PPC_TPREL_HIGHERA)
359 .Case("tprel@highera", VK_PPC_TPREL_HIGHERA)
360 .Case("TPREL@HIGHEST", VK_PPC_TPREL_HIGHEST)
361 .Case("tprel@highest", VK_PPC_TPREL_HIGHEST)
362 .Case("TPREL@HIGHESTA", VK_PPC_TPREL_HIGHESTA)
363 .Case("tprel@highesta", VK_PPC_TPREL_HIGHESTA)
364 .Case("DTPREL", VK_PPC_DTPREL)
365 .Case("dtprel", VK_PPC_DTPREL)
366 .Case("DTPREL@L", VK_PPC_DTPREL_LO)
367 .Case("dtprel@l", VK_PPC_DTPREL_LO)
368 .Case("DTPREL@H", VK_PPC_DTPREL_HI)
369 .Case("dtprel@h", VK_PPC_DTPREL_HI)
370 .Case("DTPREL@HA", VK_PPC_DTPREL_HA)
371 .Case("dtprel@ha", VK_PPC_DTPREL_HA)
372 .Case("DTPREL@HIGHER", VK_PPC_DTPREL_HIGHER)
373 .Case("dtprel@higher", VK_PPC_DTPREL_HIGHER)
374 .Case("DTPREL@HIGHERA", VK_PPC_DTPREL_HIGHERA)
375 .Case("dtprel@highera", VK_PPC_DTPREL_HIGHERA)
376 .Case("DTPREL@HIGHEST", VK_PPC_DTPREL_HIGHEST)
377 .Case("dtprel@highest", VK_PPC_DTPREL_HIGHEST)
378 .Case("DTPREL@HIGHESTA", VK_PPC_DTPREL_HIGHESTA)
379 .Case("dtprel@highesta", VK_PPC_DTPREL_HIGHESTA)
380 .Case("GOT@TPREL", VK_PPC_GOT_TPREL)
381 .Case("got@tprel", VK_PPC_GOT_TPREL)
382 .Case("GOT@TPREL@L", VK_PPC_GOT_TPREL_LO)
383 .Case("got@tprel@l", VK_PPC_GOT_TPREL_LO)
384 .Case("GOT@TPREL@H", VK_PPC_GOT_TPREL_HI)
385 .Case("got@tprel@h", VK_PPC_GOT_TPREL_HI)
386 .Case("GOT@TPREL@HA", VK_PPC_GOT_TPREL_HA)
387 .Case("got@tprel@ha", VK_PPC_GOT_TPREL_HA)
388 .Case("GOT@DTPREL", VK_PPC_GOT_DTPREL)
389 .Case("got@dtprel", VK_PPC_GOT_DTPREL)
390 .Case("GOT@DTPREL@L", VK_PPC_GOT_DTPREL_LO)
391 .Case("got@dtprel@l", VK_PPC_GOT_DTPREL_LO)
392 .Case("GOT@DTPREL@H", VK_PPC_GOT_DTPREL_HI)
393 .Case("got@dtprel@h", VK_PPC_GOT_DTPREL_HI)
394 .Case("GOT@DTPREL@HA", VK_PPC_GOT_DTPREL_HA)
395 .Case("got@dtprel@ha", VK_PPC_GOT_DTPREL_HA)
396 .Case("GOT@TLSGD", VK_PPC_GOT_TLSGD)
397 .Case("got@tlsgd", VK_PPC_GOT_TLSGD)
398 .Case("GOT@TLSGD@L", VK_PPC_GOT_TLSGD_LO)
399 .Case("got@tlsgd@l", VK_PPC_GOT_TLSGD_LO)
400 .Case("GOT@TLSGD@H", VK_PPC_GOT_TLSGD_HI)
401 .Case("got@tlsgd@h", VK_PPC_GOT_TLSGD_HI)
402 .Case("GOT@TLSGD@HA", VK_PPC_GOT_TLSGD_HA)
403 .Case("got@tlsgd@ha", VK_PPC_GOT_TLSGD_HA)
404 .Case("GOT@TLSLD", VK_PPC_GOT_TLSLD)
405 .Case("got@tlsld", VK_PPC_GOT_TLSLD)
406 .Case("GOT@TLSLD@L", VK_PPC_GOT_TLSLD_LO)
407 .Case("got@tlsld@l", VK_PPC_GOT_TLSLD_LO)
408 .Case("GOT@TLSLD@H", VK_PPC_GOT_TLSLD_HI)
409 .Case("got@tlsld@h", VK_PPC_GOT_TLSLD_HI)
410 .Case("GOT@TLSLD@HA", VK_PPC_GOT_TLSLD_HA)
411 .Case("got@tlsld@ha", VK_PPC_GOT_TLSLD_HA)
412 .Default(VK_Invalid);
417 void MCTargetExpr::anchor() {}
421 bool MCExpr::EvaluateAsAbsolute(int64_t &Res) const {
422 return EvaluateAsAbsolute(Res, 0, 0, 0);
425 bool MCExpr::EvaluateAsAbsolute(int64_t &Res,
426 const MCAsmLayout &Layout) const {
427 return EvaluateAsAbsolute(Res, &Layout.getAssembler(), &Layout, 0);
430 bool MCExpr::EvaluateAsAbsolute(int64_t &Res,
431 const MCAsmLayout &Layout,
432 const SectionAddrMap &Addrs) const {
433 return EvaluateAsAbsolute(Res, &Layout.getAssembler(), &Layout, &Addrs);
436 bool MCExpr::EvaluateAsAbsolute(int64_t &Res, const MCAssembler &Asm) const {
437 return EvaluateAsAbsolute(Res, &Asm, 0, 0);
440 bool MCExpr::EvaluateAsAbsolute(int64_t &Res, const MCAssembler *Asm,
441 const MCAsmLayout *Layout,
442 const SectionAddrMap *Addrs) const {
445 // Fast path constants.
446 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(this)) {
447 Res = CE->getValue();
451 // FIXME: The use if InSet = Addrs is a hack. Setting InSet causes us
452 // absolutize differences across sections and that is what the MachO writer
455 EvaluateAsRelocatableImpl(Value, Asm, Layout, Addrs, /*InSet*/ Addrs);
457 // Record the current value.
458 Res = Value.getConstant();
460 return IsRelocatable && Value.isAbsolute();
463 /// \brief Helper method for \see EvaluateSymbolAdd().
464 static void AttemptToFoldSymbolOffsetDifference(const MCAssembler *Asm,
465 const MCAsmLayout *Layout,
466 const SectionAddrMap *Addrs,
468 const MCSymbolRefExpr *&A,
469 const MCSymbolRefExpr *&B,
474 const MCSymbol &SA = A->getSymbol();
475 const MCSymbol &SB = B->getSymbol();
477 if (SA.isUndefined() || SB.isUndefined())
480 if (!Asm->getWriter().IsSymbolRefDifferenceFullyResolved(*Asm, A, B, InSet))
483 MCSymbolData &AD = Asm->getSymbolData(SA);
484 MCSymbolData &BD = Asm->getSymbolData(SB);
486 if (AD.getFragment() == BD.getFragment()) {
487 Addend += (AD.getOffset() - BD.getOffset());
489 // Pointers to Thumb symbols need to have their low-bit set to allow
491 if (Asm->isThumbFunc(&SA))
494 // Clear the symbol expr pointers to indicate we have folded these
503 const MCSectionData &SecA = *AD.getFragment()->getParent();
504 const MCSectionData &SecB = *BD.getFragment()->getParent();
506 if ((&SecA != &SecB) && !Addrs)
510 Addend += (Layout->getSymbolOffset(&Asm->getSymbolData(A->getSymbol())) -
511 Layout->getSymbolOffset(&Asm->getSymbolData(B->getSymbol())));
512 if (Addrs && (&SecA != &SecB))
513 Addend += (Addrs->lookup(&SecA) - Addrs->lookup(&SecB));
515 // Pointers to Thumb symbols need to have their low-bit set to allow
517 if (Asm->isThumbFunc(&SA))
520 // Clear the symbol expr pointers to indicate we have folded these
525 /// \brief Evaluate the result of an add between (conceptually) two MCValues.
527 /// This routine conceptually attempts to construct an MCValue:
528 /// Result = (Result_A - Result_B + Result_Cst)
529 /// from two MCValue's LHS and RHS where
530 /// Result = LHS + RHS
532 /// Result = (LHS_A - LHS_B + LHS_Cst) + (RHS_A - RHS_B + RHS_Cst).
534 /// This routine attempts to aggresively fold the operands such that the result
535 /// is representable in an MCValue, but may not always succeed.
537 /// \returns True on success, false if the result is not representable in an
540 /// NOTE: It is really important to have both the Asm and Layout arguments.
541 /// They might look redundant, but this function can be used before layout
542 /// is done (see the object streamer for example) and having the Asm argument
543 /// lets us avoid relaxations early.
544 static bool EvaluateSymbolicAdd(const MCAssembler *Asm,
545 const MCAsmLayout *Layout,
546 const SectionAddrMap *Addrs,
548 const MCValue &LHS,const MCSymbolRefExpr *RHS_A,
549 const MCSymbolRefExpr *RHS_B, int64_t RHS_Cst,
551 // FIXME: This routine (and other evaluation parts) are *incredibly* sloppy
552 // about dealing with modifiers. This will ultimately bite us, one day.
553 const MCSymbolRefExpr *LHS_A = LHS.getSymA();
554 const MCSymbolRefExpr *LHS_B = LHS.getSymB();
555 int64_t LHS_Cst = LHS.getConstant();
557 // Fold the result constant immediately.
558 int64_t Result_Cst = LHS_Cst + RHS_Cst;
560 assert((!Layout || Asm) &&
561 "Must have an assembler object if layout is given!");
563 // If we have a layout, we can fold resolved differences.
565 // First, fold out any differences which are fully resolved. By
566 // reassociating terms in
567 // Result = (LHS_A - LHS_B + LHS_Cst) + (RHS_A - RHS_B + RHS_Cst).
568 // we have the four possible differences:
573 // Since we are attempting to be as aggressive as possible about folding, we
574 // attempt to evaluate each possible alternative.
575 AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, LHS_A, LHS_B,
577 AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, LHS_A, RHS_B,
579 AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, RHS_A, LHS_B,
581 AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, RHS_A, RHS_B,
585 // We can't represent the addition or subtraction of two symbols.
586 if ((LHS_A && RHS_A) || (LHS_B && RHS_B))
589 // At this point, we have at most one additive symbol and one subtractive
590 // symbol -- find them.
591 const MCSymbolRefExpr *A = LHS_A ? LHS_A : RHS_A;
592 const MCSymbolRefExpr *B = LHS_B ? LHS_B : RHS_B;
594 // If we have a negated symbol, then we must have also have a non-negated
595 // symbol in order to encode the expression.
599 Res = MCValue::get(A, B, Result_Cst);
603 bool MCExpr::EvaluateAsRelocatable(MCValue &Res,
604 const MCAsmLayout &Layout) const {
605 return EvaluateAsRelocatableImpl(Res, &Layout.getAssembler(), &Layout,
609 bool MCExpr::EvaluateAsRelocatableImpl(MCValue &Res,
610 const MCAssembler *Asm,
611 const MCAsmLayout *Layout,
612 const SectionAddrMap *Addrs,
614 ++stats::MCExprEvaluate;
618 return cast<MCTargetExpr>(this)->EvaluateAsRelocatableImpl(Res, Layout);
621 Res = MCValue::get(cast<MCConstantExpr>(this)->getValue());
625 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(this);
626 const MCSymbol &Sym = SRE->getSymbol();
628 // Evaluate recursively if this is a variable.
629 if (Sym.isVariable() && SRE->getKind() == MCSymbolRefExpr::VK_None) {
630 bool Ret = Sym.getVariableValue()->EvaluateAsRelocatableImpl(Res, Asm,
634 // If we failed to simplify this to a constant, let the target
636 if (Ret && !Res.getSymA() && !Res.getSymB())
640 Res = MCValue::get(SRE, 0, 0);
645 const MCUnaryExpr *AUE = cast<MCUnaryExpr>(this);
648 if (!AUE->getSubExpr()->EvaluateAsRelocatableImpl(Value, Asm, Layout,
652 switch (AUE->getOpcode()) {
653 case MCUnaryExpr::LNot:
654 if (!Value.isAbsolute())
656 Res = MCValue::get(!Value.getConstant());
658 case MCUnaryExpr::Minus:
659 /// -(a - b + const) ==> (b - a - const)
660 if (Value.getSymA() && !Value.getSymB())
662 Res = MCValue::get(Value.getSymB(), Value.getSymA(),
663 -Value.getConstant());
665 case MCUnaryExpr::Not:
666 if (!Value.isAbsolute())
668 Res = MCValue::get(~Value.getConstant());
670 case MCUnaryExpr::Plus:
679 const MCBinaryExpr *ABE = cast<MCBinaryExpr>(this);
680 MCValue LHSValue, RHSValue;
682 if (!ABE->getLHS()->EvaluateAsRelocatableImpl(LHSValue, Asm, Layout,
684 !ABE->getRHS()->EvaluateAsRelocatableImpl(RHSValue, Asm, Layout,
688 // We only support a few operations on non-constant expressions, handle
690 if (!LHSValue.isAbsolute() || !RHSValue.isAbsolute()) {
691 switch (ABE->getOpcode()) {
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(),
701 case MCBinaryExpr::Add:
702 return EvaluateSymbolicAdd(Asm, Layout, Addrs, InSet, LHSValue,
703 RHSValue.getSymA(), RHSValue.getSymB(),
704 RHSValue.getConstant(),
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();
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;
735 Res = MCValue::get(Result);
740 llvm_unreachable("Invalid assembly expression kind!");
743 const MCSection *MCExpr::FindAssociatedSection() const {
746 // We never look through target specific expressions.
747 return cast<MCTargetExpr>(this)->FindAssociatedSection();
750 return MCSymbol::AbsolutePseudoSection;
753 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(this);
754 const MCSymbol &Sym = SRE->getSymbol();
757 return &Sym.getSection();
763 return cast<MCUnaryExpr>(this)->getSubExpr()->FindAssociatedSection();
766 const MCBinaryExpr *BE = cast<MCBinaryExpr>(this);
767 const MCSection *LHS_S = BE->getLHS()->FindAssociatedSection();
768 const MCSection *RHS_S = BE->getRHS()->FindAssociatedSection();
770 // If either section is absolute, return the other.
771 if (LHS_S == MCSymbol::AbsolutePseudoSection)
773 if (RHS_S == MCSymbol::AbsolutePseudoSection)
776 // Otherwise, return the first non-null section.
777 return LHS_S ? LHS_S : RHS_S;
781 llvm_unreachable("Invalid assembly expression kind!");