1 //===- MCExpr.cpp - Assembly Level Expression Implementation --------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/MC/MCExpr.h"
11 #include "llvm/ADT/Statistic.h"
12 #include "llvm/ADT/StringSwitch.h"
13 #include "llvm/MC/MCAsmInfo.h"
14 #include "llvm/MC/MCAsmLayout.h"
15 #include "llvm/MC/MCAssembler.h"
16 #include "llvm/MC/MCContext.h"
17 #include "llvm/MC/MCObjectWriter.h"
18 #include "llvm/MC/MCSymbol.h"
19 #include "llvm/MC/MCValue.h"
20 #include "llvm/Support/Debug.h"
21 #include "llvm/Support/ErrorHandling.h"
22 #include "llvm/Support/raw_ostream.h"
25 #define DEBUG_TYPE "mcexpr"
29 STATISTIC(MCExprEvaluate, "Number of MCExpr evaluations");
33 void MCExpr::print(raw_ostream &OS) const {
36 return cast<MCTargetExpr>(this)->PrintImpl(OS);
37 case MCExpr::Constant:
38 OS << cast<MCConstantExpr>(*this).getValue();
41 case MCExpr::SymbolRef: {
42 const MCSymbolRefExpr &SRE = cast<MCSymbolRefExpr>(*this);
43 const MCSymbol &Sym = SRE.getSymbol();
44 // Parenthesize names that start with $ so that they don't look like
46 bool UseParens = Sym.getName()[0] == '$';
48 OS << '(' << Sym << ')';
52 if (SRE.getKind() != MCSymbolRefExpr::VK_None) {
53 if (SRE.getMCAsmInfo().useParensForSymbolVariant())
54 OS << '(' << MCSymbolRefExpr::getVariantKindName(SRE.getKind()) << ')';
56 OS << '@' << MCSymbolRefExpr::getVariantKindName(SRE.getKind());
63 const MCUnaryExpr &UE = cast<MCUnaryExpr>(*this);
64 switch (UE.getOpcode()) {
65 case MCUnaryExpr::LNot: OS << '!'; break;
66 case MCUnaryExpr::Minus: OS << '-'; break;
67 case MCUnaryExpr::Not: OS << '~'; break;
68 case MCUnaryExpr::Plus: OS << '+'; break;
70 OS << *UE.getSubExpr();
74 case MCExpr::Binary: {
75 const MCBinaryExpr &BE = cast<MCBinaryExpr>(*this);
77 // Only print parens around the LHS if it is non-trivial.
78 if (isa<MCConstantExpr>(BE.getLHS()) || isa<MCSymbolRefExpr>(BE.getLHS())) {
81 OS << '(' << *BE.getLHS() << ')';
84 switch (BE.getOpcode()) {
85 case MCBinaryExpr::Add:
86 // Print "X-42" instead of "X+-42".
87 if (const MCConstantExpr *RHSC = dyn_cast<MCConstantExpr>(BE.getRHS())) {
88 if (RHSC->getValue() < 0) {
89 OS << RHSC->getValue();
96 case MCBinaryExpr::And: OS << '&'; break;
97 case MCBinaryExpr::Div: OS << '/'; break;
98 case MCBinaryExpr::EQ: OS << "=="; break;
99 case MCBinaryExpr::GT: OS << '>'; break;
100 case MCBinaryExpr::GTE: OS << ">="; break;
101 case MCBinaryExpr::LAnd: OS << "&&"; break;
102 case MCBinaryExpr::LOr: OS << "||"; break;
103 case MCBinaryExpr::LT: OS << '<'; break;
104 case MCBinaryExpr::LTE: OS << "<="; break;
105 case MCBinaryExpr::Mod: OS << '%'; break;
106 case MCBinaryExpr::Mul: OS << '*'; break;
107 case MCBinaryExpr::NE: OS << "!="; break;
108 case MCBinaryExpr::Or: OS << '|'; break;
109 case MCBinaryExpr::Shl: OS << "<<"; break;
110 case MCBinaryExpr::Shr: OS << ">>"; break;
111 case MCBinaryExpr::Sub: OS << '-'; break;
112 case MCBinaryExpr::Xor: OS << '^'; break;
115 // Only print parens around the LHS if it is non-trivial.
116 if (isa<MCConstantExpr>(BE.getRHS()) || isa<MCSymbolRefExpr>(BE.getRHS())) {
119 OS << '(' << *BE.getRHS() << ')';
125 llvm_unreachable("Invalid expression kind!");
128 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
129 void MCExpr::dump() const {
137 const MCBinaryExpr *MCBinaryExpr::Create(Opcode Opc, const MCExpr *LHS,
138 const MCExpr *RHS, MCContext &Ctx) {
139 return new (Ctx) MCBinaryExpr(Opc, LHS, RHS);
142 const MCUnaryExpr *MCUnaryExpr::Create(Opcode Opc, const MCExpr *Expr,
144 return new (Ctx) MCUnaryExpr(Opc, Expr);
147 const MCConstantExpr *MCConstantExpr::Create(int64_t Value, MCContext &Ctx) {
148 return new (Ctx) MCConstantExpr(Value);
153 const MCSymbolRefExpr *MCSymbolRefExpr::Create(const MCSymbol *Sym,
156 return new (Ctx) MCSymbolRefExpr(Sym, Kind, Ctx.getAsmInfo());
159 const MCSymbolRefExpr *MCSymbolRefExpr::Create(StringRef Name, VariantKind Kind,
161 return Create(Ctx.GetOrCreateSymbol(Name), Kind, Ctx);
164 StringRef MCSymbolRefExpr::getVariantKindName(VariantKind Kind) {
166 case VK_Invalid: return "<<invalid>>";
167 case VK_None: return "<<none>>";
169 case VK_GOT: return "GOT";
170 case VK_GOTOFF: return "GOTOFF";
171 case VK_GOTPCREL: return "GOTPCREL";
172 case VK_GOTTPOFF: return "GOTTPOFF";
173 case VK_INDNTPOFF: return "INDNTPOFF";
174 case VK_NTPOFF: return "NTPOFF";
175 case VK_GOTNTPOFF: return "GOTNTPOFF";
176 case VK_PLT: return "PLT";
177 case VK_TLSGD: return "TLSGD";
178 case VK_TLSLD: return "TLSLD";
179 case VK_TLSLDM: return "TLSLDM";
180 case VK_TPOFF: return "TPOFF";
181 case VK_DTPOFF: return "DTPOFF";
182 case VK_TLVP: return "TLVP";
183 case VK_TLVPPAGE: return "TLVPPAGE";
184 case VK_TLVPPAGEOFF: return "TLVPPAGEOFF";
185 case VK_PAGE: return "PAGE";
186 case VK_PAGEOFF: return "PAGEOFF";
187 case VK_GOTPAGE: return "GOTPAGE";
188 case VK_GOTPAGEOFF: return "GOTPAGEOFF";
189 case VK_SECREL: return "SECREL32";
190 case VK_WEAKREF: return "WEAKREF";
191 case VK_ARM_NONE: return "none";
192 case VK_ARM_TARGET1: return "target1";
193 case VK_ARM_TARGET2: return "target2";
194 case VK_ARM_PREL31: return "prel31";
195 case VK_ARM_TLSLDO: return "tlsldo";
196 case VK_ARM_TLSCALL: return "tlscall";
197 case VK_ARM_TLSDESC: return "tlsdesc";
198 case VK_ARM_TLSDESCSEQ: return "tlsdescseq";
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("GOT_PREL", VK_GOTPCREL)
289 .Case("got_prel", VK_GOTPCREL)
290 .Case("GOTTPOFF", VK_GOTTPOFF)
291 .Case("gottpoff", VK_GOTTPOFF)
292 .Case("INDNTPOFF", VK_INDNTPOFF)
293 .Case("indntpoff", VK_INDNTPOFF)
294 .Case("NTPOFF", VK_NTPOFF)
295 .Case("ntpoff", VK_NTPOFF)
296 .Case("GOTNTPOFF", VK_GOTNTPOFF)
297 .Case("gotntpoff", VK_GOTNTPOFF)
300 .Case("TLSGD", VK_TLSGD)
301 .Case("tlsgd", VK_TLSGD)
302 .Case("TLSLD", VK_TLSLD)
303 .Case("tlsld", VK_TLSLD)
304 .Case("TLSLDM", VK_TLSLDM)
305 .Case("tlsldm", VK_TLSLDM)
306 .Case("TPOFF", VK_TPOFF)
307 .Case("tpoff", VK_TPOFF)
308 .Case("DTPOFF", VK_DTPOFF)
309 .Case("dtpoff", VK_DTPOFF)
310 .Case("TLVP", VK_TLVP)
311 .Case("tlvp", VK_TLVP)
312 .Case("TLVPPAGE", VK_TLVPPAGE)
313 .Case("tlvppage", VK_TLVPPAGE)
314 .Case("TLVPPAGEOFF", VK_TLVPPAGEOFF)
315 .Case("tlvppageoff", VK_TLVPPAGEOFF)
316 .Case("PAGE", VK_PAGE)
317 .Case("page", VK_PAGE)
318 .Case("PAGEOFF", VK_PAGEOFF)
319 .Case("pageoff", VK_PAGEOFF)
320 .Case("GOTPAGE", VK_GOTPAGE)
321 .Case("gotpage", VK_GOTPAGE)
322 .Case("GOTPAGEOFF", VK_GOTPAGEOFF)
323 .Case("gotpageoff", VK_GOTPAGEOFF)
324 .Case("IMGREL", VK_COFF_IMGREL32)
325 .Case("imgrel", VK_COFF_IMGREL32)
326 .Case("SECREL32", VK_SECREL)
327 .Case("secrel32", VK_SECREL)
328 .Case("L", VK_PPC_LO)
329 .Case("l", VK_PPC_LO)
330 .Case("H", VK_PPC_HI)
331 .Case("h", VK_PPC_HI)
332 .Case("HA", VK_PPC_HA)
333 .Case("ha", VK_PPC_HA)
334 .Case("HIGHER", VK_PPC_HIGHER)
335 .Case("higher", VK_PPC_HIGHER)
336 .Case("HIGHERA", VK_PPC_HIGHERA)
337 .Case("highera", VK_PPC_HIGHERA)
338 .Case("HIGHEST", VK_PPC_HIGHEST)
339 .Case("highest", VK_PPC_HIGHEST)
340 .Case("HIGHESTA", VK_PPC_HIGHESTA)
341 .Case("highesta", VK_PPC_HIGHESTA)
342 .Case("GOT@L", VK_PPC_GOT_LO)
343 .Case("got@l", VK_PPC_GOT_LO)
344 .Case("GOT@H", VK_PPC_GOT_HI)
345 .Case("got@h", VK_PPC_GOT_HI)
346 .Case("GOT@HA", VK_PPC_GOT_HA)
347 .Case("got@ha", VK_PPC_GOT_HA)
348 .Case("TOCBASE", VK_PPC_TOCBASE)
349 .Case("tocbase", VK_PPC_TOCBASE)
350 .Case("TOC", VK_PPC_TOC)
351 .Case("toc", VK_PPC_TOC)
352 .Case("TOC@L", VK_PPC_TOC_LO)
353 .Case("toc@l", VK_PPC_TOC_LO)
354 .Case("TOC@H", VK_PPC_TOC_HI)
355 .Case("toc@h", VK_PPC_TOC_HI)
356 .Case("TOC@HA", VK_PPC_TOC_HA)
357 .Case("toc@ha", VK_PPC_TOC_HA)
358 .Case("TLS", VK_PPC_TLS)
359 .Case("tls", VK_PPC_TLS)
360 .Case("DTPMOD", VK_PPC_DTPMOD)
361 .Case("dtpmod", VK_PPC_DTPMOD)
362 .Case("TPREL", VK_PPC_TPREL)
363 .Case("tprel", VK_PPC_TPREL)
364 .Case("TPREL@L", VK_PPC_TPREL_LO)
365 .Case("tprel@l", VK_PPC_TPREL_LO)
366 .Case("TPREL@H", VK_PPC_TPREL_HI)
367 .Case("tprel@h", VK_PPC_TPREL_HI)
368 .Case("TPREL@HA", VK_PPC_TPREL_HA)
369 .Case("tprel@ha", VK_PPC_TPREL_HA)
370 .Case("TPREL@HIGHER", VK_PPC_TPREL_HIGHER)
371 .Case("tprel@higher", VK_PPC_TPREL_HIGHER)
372 .Case("TPREL@HIGHERA", VK_PPC_TPREL_HIGHERA)
373 .Case("tprel@highera", VK_PPC_TPREL_HIGHERA)
374 .Case("TPREL@HIGHEST", VK_PPC_TPREL_HIGHEST)
375 .Case("tprel@highest", VK_PPC_TPREL_HIGHEST)
376 .Case("TPREL@HIGHESTA", VK_PPC_TPREL_HIGHESTA)
377 .Case("tprel@highesta", VK_PPC_TPREL_HIGHESTA)
378 .Case("DTPREL", VK_PPC_DTPREL)
379 .Case("dtprel", VK_PPC_DTPREL)
380 .Case("DTPREL@L", VK_PPC_DTPREL_LO)
381 .Case("dtprel@l", VK_PPC_DTPREL_LO)
382 .Case("DTPREL@H", VK_PPC_DTPREL_HI)
383 .Case("dtprel@h", VK_PPC_DTPREL_HI)
384 .Case("DTPREL@HA", VK_PPC_DTPREL_HA)
385 .Case("dtprel@ha", VK_PPC_DTPREL_HA)
386 .Case("DTPREL@HIGHER", VK_PPC_DTPREL_HIGHER)
387 .Case("dtprel@higher", VK_PPC_DTPREL_HIGHER)
388 .Case("DTPREL@HIGHERA", VK_PPC_DTPREL_HIGHERA)
389 .Case("dtprel@highera", VK_PPC_DTPREL_HIGHERA)
390 .Case("DTPREL@HIGHEST", VK_PPC_DTPREL_HIGHEST)
391 .Case("dtprel@highest", VK_PPC_DTPREL_HIGHEST)
392 .Case("DTPREL@HIGHESTA", VK_PPC_DTPREL_HIGHESTA)
393 .Case("dtprel@highesta", VK_PPC_DTPREL_HIGHESTA)
394 .Case("GOT@TPREL", VK_PPC_GOT_TPREL)
395 .Case("got@tprel", VK_PPC_GOT_TPREL)
396 .Case("GOT@TPREL@L", VK_PPC_GOT_TPREL_LO)
397 .Case("got@tprel@l", VK_PPC_GOT_TPREL_LO)
398 .Case("GOT@TPREL@H", VK_PPC_GOT_TPREL_HI)
399 .Case("got@tprel@h", VK_PPC_GOT_TPREL_HI)
400 .Case("GOT@TPREL@HA", VK_PPC_GOT_TPREL_HA)
401 .Case("got@tprel@ha", VK_PPC_GOT_TPREL_HA)
402 .Case("GOT@DTPREL", VK_PPC_GOT_DTPREL)
403 .Case("got@dtprel", VK_PPC_GOT_DTPREL)
404 .Case("GOT@DTPREL@L", VK_PPC_GOT_DTPREL_LO)
405 .Case("got@dtprel@l", VK_PPC_GOT_DTPREL_LO)
406 .Case("GOT@DTPREL@H", VK_PPC_GOT_DTPREL_HI)
407 .Case("got@dtprel@h", VK_PPC_GOT_DTPREL_HI)
408 .Case("GOT@DTPREL@HA", VK_PPC_GOT_DTPREL_HA)
409 .Case("got@dtprel@ha", VK_PPC_GOT_DTPREL_HA)
410 .Case("GOT@TLSGD", VK_PPC_GOT_TLSGD)
411 .Case("got@tlsgd", VK_PPC_GOT_TLSGD)
412 .Case("GOT@TLSGD@L", VK_PPC_GOT_TLSGD_LO)
413 .Case("got@tlsgd@l", VK_PPC_GOT_TLSGD_LO)
414 .Case("GOT@TLSGD@H", VK_PPC_GOT_TLSGD_HI)
415 .Case("got@tlsgd@h", VK_PPC_GOT_TLSGD_HI)
416 .Case("GOT@TLSGD@HA", VK_PPC_GOT_TLSGD_HA)
417 .Case("got@tlsgd@ha", VK_PPC_GOT_TLSGD_HA)
418 .Case("GOT@TLSLD", VK_PPC_GOT_TLSLD)
419 .Case("got@tlsld", VK_PPC_GOT_TLSLD)
420 .Case("GOT@TLSLD@L", VK_PPC_GOT_TLSLD_LO)
421 .Case("got@tlsld@l", VK_PPC_GOT_TLSLD_LO)
422 .Case("GOT@TLSLD@H", VK_PPC_GOT_TLSLD_HI)
423 .Case("got@tlsld@h", VK_PPC_GOT_TLSLD_HI)
424 .Case("GOT@TLSLD@HA", VK_PPC_GOT_TLSLD_HA)
425 .Case("got@tlsld@ha", VK_PPC_GOT_TLSLD_HA)
426 .Case("NONE", VK_ARM_NONE)
427 .Case("none", VK_ARM_NONE)
428 .Case("TARGET1", VK_ARM_TARGET1)
429 .Case("target1", VK_ARM_TARGET1)
430 .Case("TARGET2", VK_ARM_TARGET2)
431 .Case("target2", VK_ARM_TARGET2)
432 .Case("PREL31", VK_ARM_PREL31)
433 .Case("prel31", VK_ARM_PREL31)
434 .Case("TLSLDO", VK_ARM_TLSLDO)
435 .Case("tlsldo", VK_ARM_TLSLDO)
436 .Case("TLSCALL", VK_ARM_TLSCALL)
437 .Case("tlscall", VK_ARM_TLSCALL)
438 .Case("TLSDESC", VK_ARM_TLSDESC)
439 .Case("tlsdesc", VK_ARM_TLSDESC)
440 .Default(VK_Invalid);
445 void MCTargetExpr::anchor() {}
449 bool MCExpr::EvaluateAsAbsolute(int64_t &Res) const {
450 return EvaluateAsAbsolute(Res, nullptr, nullptr, nullptr);
453 bool MCExpr::EvaluateAsAbsolute(int64_t &Res,
454 const MCAsmLayout &Layout) const {
455 return EvaluateAsAbsolute(Res, &Layout.getAssembler(), &Layout, nullptr);
458 bool MCExpr::EvaluateAsAbsolute(int64_t &Res,
459 const MCAsmLayout &Layout,
460 const SectionAddrMap &Addrs) const {
461 return EvaluateAsAbsolute(Res, &Layout.getAssembler(), &Layout, &Addrs);
464 bool MCExpr::EvaluateAsAbsolute(int64_t &Res, const MCAssembler &Asm) const {
465 return EvaluateAsAbsolute(Res, &Asm, nullptr, nullptr);
468 bool MCExpr::EvaluateAsAbsolute(int64_t &Res, const MCAssembler *Asm,
469 const MCAsmLayout *Layout,
470 const SectionAddrMap *Addrs) const {
473 // Fast path constants.
474 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(this)) {
475 Res = CE->getValue();
479 // FIXME: The use if InSet = Addrs is a hack. Setting InSet causes us
480 // absolutize differences across sections and that is what the MachO writer
483 EvaluateAsRelocatableImpl(Value, Asm, Layout, Addrs, /*InSet*/ Addrs,
484 /*ForceVarExpansion*/ false);
486 // Record the current value.
487 Res = Value.getConstant();
489 return IsRelocatable && Value.isAbsolute();
492 /// \brief Helper method for \see EvaluateSymbolAdd().
493 static void AttemptToFoldSymbolOffsetDifference(const MCAssembler *Asm,
494 const MCAsmLayout *Layout,
495 const SectionAddrMap *Addrs,
497 const MCSymbolRefExpr *&A,
498 const MCSymbolRefExpr *&B,
503 const MCSymbol &SA = A->getSymbol();
504 const MCSymbol &SB = B->getSymbol();
506 if (SA.isUndefined() || SB.isUndefined())
509 if (!Asm->getWriter().IsSymbolRefDifferenceFullyResolved(*Asm, A, B, InSet))
512 const MCSymbolData &AD = Asm->getSymbolData(SA);
513 const MCSymbolData &BD = Asm->getSymbolData(SB);
515 if (AD.getFragment() == BD.getFragment()) {
516 Addend += (AD.getOffset() - BD.getOffset());
518 // Pointers to Thumb symbols need to have their low-bit set to allow
520 if (Asm->isThumbFunc(&SA))
523 // Clear the symbol expr pointers to indicate we have folded these
532 const MCSectionData &SecA = *AD.getFragment()->getParent();
533 const MCSectionData &SecB = *BD.getFragment()->getParent();
535 if ((&SecA != &SecB) && !Addrs)
539 Addend += (Layout->getSymbolOffset(&Asm->getSymbolData(A->getSymbol())) -
540 Layout->getSymbolOffset(&Asm->getSymbolData(B->getSymbol())));
541 if (Addrs && (&SecA != &SecB))
542 Addend += (Addrs->lookup(&SecA) - Addrs->lookup(&SecB));
544 // Pointers to Thumb symbols need to have their low-bit set to allow
546 if (Asm->isThumbFunc(&SA))
549 // Clear the symbol expr pointers to indicate we have folded these
554 /// \brief Evaluate the result of an add between (conceptually) two MCValues.
556 /// This routine conceptually attempts to construct an MCValue:
557 /// Result = (Result_A - Result_B + Result_Cst)
558 /// from two MCValue's LHS and RHS where
559 /// Result = LHS + RHS
561 /// Result = (LHS_A - LHS_B + LHS_Cst) + (RHS_A - RHS_B + RHS_Cst).
563 /// This routine attempts to aggresively fold the operands such that the result
564 /// is representable in an MCValue, but may not always succeed.
566 /// \returns True on success, false if the result is not representable in an
569 /// NOTE: It is really important to have both the Asm and Layout arguments.
570 /// They might look redundant, but this function can be used before layout
571 /// is done (see the object streamer for example) and having the Asm argument
572 /// lets us avoid relaxations early.
573 static bool EvaluateSymbolicAdd(const MCAssembler *Asm,
574 const MCAsmLayout *Layout,
575 const SectionAddrMap *Addrs,
577 const MCValue &LHS,const MCSymbolRefExpr *RHS_A,
578 const MCSymbolRefExpr *RHS_B, int64_t RHS_Cst,
580 // FIXME: This routine (and other evaluation parts) are *incredibly* sloppy
581 // about dealing with modifiers. This will ultimately bite us, one day.
582 const MCSymbolRefExpr *LHS_A = LHS.getSymA();
583 const MCSymbolRefExpr *LHS_B = LHS.getSymB();
584 int64_t LHS_Cst = LHS.getConstant();
586 // Fold the result constant immediately.
587 int64_t Result_Cst = LHS_Cst + RHS_Cst;
589 assert((!Layout || Asm) &&
590 "Must have an assembler object if layout is given!");
592 // If we have a layout, we can fold resolved differences.
594 // First, fold out any differences which are fully resolved. By
595 // reassociating terms in
596 // Result = (LHS_A - LHS_B + LHS_Cst) + (RHS_A - RHS_B + RHS_Cst).
597 // we have the four possible differences:
602 // Since we are attempting to be as aggressive as possible about folding, we
603 // attempt to evaluate each possible alternative.
604 AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, LHS_A, LHS_B,
606 AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, LHS_A, RHS_B,
608 AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, RHS_A, LHS_B,
610 AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, RHS_A, RHS_B,
614 // We can't represent the addition or subtraction of two symbols.
615 if ((LHS_A && RHS_A) || (LHS_B && RHS_B))
618 // At this point, we have at most one additive symbol and one subtractive
619 // symbol -- find them.
620 const MCSymbolRefExpr *A = LHS_A ? LHS_A : RHS_A;
621 const MCSymbolRefExpr *B = LHS_B ? LHS_B : RHS_B;
623 // If we have a negated symbol, then we must have also have a non-negated
624 // symbol in order to encode the expression.
628 Res = MCValue::get(A, B, Result_Cst);
632 bool MCExpr::EvaluateAsRelocatable(MCValue &Res,
633 const MCAsmLayout *Layout) const {
634 MCAssembler *Assembler = Layout ? &Layout->getAssembler() : nullptr;
635 return EvaluateAsRelocatableImpl(Res, Assembler, Layout, nullptr, false,
636 /*ForceVarExpansion*/ false);
639 bool MCExpr::EvaluateAsValue(MCValue &Res, const MCAsmLayout *Layout) const {
640 MCAssembler *Assembler = Layout ? &Layout->getAssembler() : nullptr;
641 return EvaluateAsRelocatableImpl(Res, Assembler, Layout, nullptr, false,
642 /*ForceVarExpansion*/ true);
645 bool MCExpr::EvaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm,
646 const MCAsmLayout *Layout,
647 const SectionAddrMap *Addrs, bool InSet,
648 bool ForceVarExpansion) const {
649 ++stats::MCExprEvaluate;
653 return cast<MCTargetExpr>(this)->EvaluateAsRelocatableImpl(Res, Layout);
656 Res = MCValue::get(cast<MCConstantExpr>(this)->getValue());
660 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(this);
661 bool IsWeakRef = SRE->getKind() == MCSymbolRefExpr::VK_WEAKREF;
662 const MCSymbol &Sym = SRE->getSymbol();
663 const MCAsmInfo &MCAsmInfo = SRE->getMCAsmInfo();
665 // Evaluate recursively if this is a variable.
666 if (Sym.isVariable() && !IsWeakRef) {
667 if (Sym.getVariableValue()->EvaluateAsRelocatableImpl(
668 Res, Asm, Layout, Addrs, true, ForceVarExpansion)) {
669 const MCSymbolRefExpr *A = Res.getSymA();
670 const MCSymbolRefExpr *B = Res.getSymB();
672 if (MCAsmInfo.hasSubsectionsViaSymbols()) {
673 // FIXME: This is small hack. Given
676 // the OS X assembler will completely drop the 4. We should probably
677 // include it in the relocation or produce an error if that is not
682 if (ForceVarExpansion)
684 bool IsSymbol = A && A->getSymbol().isDefined();
691 Res = MCValue::get(SRE, nullptr, 0);
696 const MCUnaryExpr *AUE = cast<MCUnaryExpr>(this);
699 if (!AUE->getSubExpr()->EvaluateAsRelocatableImpl(Value, Asm, Layout, Addrs,
700 InSet, ForceVarExpansion))
703 switch (AUE->getOpcode()) {
704 case MCUnaryExpr::LNot:
705 if (!Value.isAbsolute())
707 Res = MCValue::get(!Value.getConstant());
709 case MCUnaryExpr::Minus:
710 /// -(a - b + const) ==> (b - a - const)
711 if (Value.getSymA() && !Value.getSymB())
713 Res = MCValue::get(Value.getSymB(), Value.getSymA(),
714 -Value.getConstant());
716 case MCUnaryExpr::Not:
717 if (!Value.isAbsolute())
719 Res = MCValue::get(~Value.getConstant());
721 case MCUnaryExpr::Plus:
730 const MCBinaryExpr *ABE = cast<MCBinaryExpr>(this);
731 MCValue LHSValue, RHSValue;
733 if (!ABE->getLHS()->EvaluateAsRelocatableImpl(LHSValue, Asm, Layout, Addrs,
734 InSet, ForceVarExpansion) ||
735 !ABE->getRHS()->EvaluateAsRelocatableImpl(RHSValue, Asm, Layout, Addrs,
736 InSet, ForceVarExpansion))
739 // We only support a few operations on non-constant expressions, handle
741 if (!LHSValue.isAbsolute() || !RHSValue.isAbsolute()) {
742 switch (ABE->getOpcode()) {
745 case MCBinaryExpr::Sub:
746 // Negate RHS and add.
747 return EvaluateSymbolicAdd(Asm, Layout, Addrs, InSet, LHSValue,
748 RHSValue.getSymB(), RHSValue.getSymA(),
749 -RHSValue.getConstant(),
752 case MCBinaryExpr::Add:
753 return EvaluateSymbolicAdd(Asm, Layout, Addrs, InSet, LHSValue,
754 RHSValue.getSymA(), RHSValue.getSymB(),
755 RHSValue.getConstant(),
760 // FIXME: We need target hooks for the evaluation. It may be limited in
761 // width, and gas defines the result of comparisons and right shifts
762 // differently from Apple as.
763 int64_t LHS = LHSValue.getConstant(), RHS = RHSValue.getConstant();
765 switch (ABE->getOpcode()) {
766 case MCBinaryExpr::Add: Result = LHS + RHS; break;
767 case MCBinaryExpr::And: Result = LHS & RHS; break;
768 case MCBinaryExpr::Div: Result = LHS / RHS; break;
769 case MCBinaryExpr::EQ: Result = LHS == RHS; break;
770 case MCBinaryExpr::GT: Result = LHS > RHS; break;
771 case MCBinaryExpr::GTE: Result = LHS >= RHS; break;
772 case MCBinaryExpr::LAnd: Result = LHS && RHS; break;
773 case MCBinaryExpr::LOr: Result = LHS || RHS; break;
774 case MCBinaryExpr::LT: Result = LHS < RHS; break;
775 case MCBinaryExpr::LTE: Result = LHS <= RHS; break;
776 case MCBinaryExpr::Mod: Result = LHS % RHS; break;
777 case MCBinaryExpr::Mul: Result = LHS * RHS; break;
778 case MCBinaryExpr::NE: Result = LHS != RHS; break;
779 case MCBinaryExpr::Or: Result = LHS | RHS; break;
780 case MCBinaryExpr::Shl: Result = LHS << RHS; break;
781 case MCBinaryExpr::Shr: Result = LHS >> RHS; break;
782 case MCBinaryExpr::Sub: Result = LHS - RHS; break;
783 case MCBinaryExpr::Xor: Result = LHS ^ RHS; break;
786 Res = MCValue::get(Result);
791 llvm_unreachable("Invalid assembly expression kind!");
794 const MCSection *MCExpr::FindAssociatedSection() const {
797 // We never look through target specific expressions.
798 return cast<MCTargetExpr>(this)->FindAssociatedSection();
801 return MCSymbol::AbsolutePseudoSection;
804 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(this);
805 const MCSymbol &Sym = SRE->getSymbol();
808 return &Sym.getSection();
814 return cast<MCUnaryExpr>(this)->getSubExpr()->FindAssociatedSection();
817 const MCBinaryExpr *BE = cast<MCBinaryExpr>(this);
818 const MCSection *LHS_S = BE->getLHS()->FindAssociatedSection();
819 const MCSection *RHS_S = BE->getRHS()->FindAssociatedSection();
821 // If either section is absolute, return the other.
822 if (LHS_S == MCSymbol::AbsolutePseudoSection)
824 if (RHS_S == MCSymbol::AbsolutePseudoSection)
827 // Otherwise, return the first non-null section.
828 return LHS_S ? LHS_S : RHS_S;
832 llvm_unreachable("Invalid assembly expression kind!");