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_Mips_PCREL_HI16: return "PCREL_HI16";
275 case VK_Mips_PCREL_LO16: return "PCREL_LO16";
276 case VK_COFF_IMGREL32: return "IMGREL32";
278 llvm_unreachable("Invalid variant kind");
281 MCSymbolRefExpr::VariantKind
282 MCSymbolRefExpr::getVariantKindForName(StringRef Name) {
283 return StringSwitch<VariantKind>(Name)
286 .Case("GOTOFF", VK_GOTOFF)
287 .Case("gotoff", VK_GOTOFF)
288 .Case("GOTPCREL", VK_GOTPCREL)
289 .Case("gotpcrel", VK_GOTPCREL)
290 .Case("GOT_PREL", VK_GOTPCREL)
291 .Case("got_prel", VK_GOTPCREL)
292 .Case("GOTTPOFF", VK_GOTTPOFF)
293 .Case("gottpoff", VK_GOTTPOFF)
294 .Case("INDNTPOFF", VK_INDNTPOFF)
295 .Case("indntpoff", VK_INDNTPOFF)
296 .Case("NTPOFF", VK_NTPOFF)
297 .Case("ntpoff", VK_NTPOFF)
298 .Case("GOTNTPOFF", VK_GOTNTPOFF)
299 .Case("gotntpoff", VK_GOTNTPOFF)
302 .Case("TLSGD", VK_TLSGD)
303 .Case("tlsgd", VK_TLSGD)
304 .Case("TLSLD", VK_TLSLD)
305 .Case("tlsld", VK_TLSLD)
306 .Case("TLSLDM", VK_TLSLDM)
307 .Case("tlsldm", VK_TLSLDM)
308 .Case("TPOFF", VK_TPOFF)
309 .Case("tpoff", VK_TPOFF)
310 .Case("DTPOFF", VK_DTPOFF)
311 .Case("dtpoff", VK_DTPOFF)
312 .Case("TLVP", VK_TLVP)
313 .Case("tlvp", VK_TLVP)
314 .Case("TLVPPAGE", VK_TLVPPAGE)
315 .Case("tlvppage", VK_TLVPPAGE)
316 .Case("TLVPPAGEOFF", VK_TLVPPAGEOFF)
317 .Case("tlvppageoff", VK_TLVPPAGEOFF)
318 .Case("PAGE", VK_PAGE)
319 .Case("page", VK_PAGE)
320 .Case("PAGEOFF", VK_PAGEOFF)
321 .Case("pageoff", VK_PAGEOFF)
322 .Case("GOTPAGE", VK_GOTPAGE)
323 .Case("gotpage", VK_GOTPAGE)
324 .Case("GOTPAGEOFF", VK_GOTPAGEOFF)
325 .Case("gotpageoff", VK_GOTPAGEOFF)
326 .Case("IMGREL", VK_COFF_IMGREL32)
327 .Case("imgrel", VK_COFF_IMGREL32)
328 .Case("SECREL32", VK_SECREL)
329 .Case("secrel32", VK_SECREL)
330 .Case("L", VK_PPC_LO)
331 .Case("l", VK_PPC_LO)
332 .Case("H", VK_PPC_HI)
333 .Case("h", VK_PPC_HI)
334 .Case("HA", VK_PPC_HA)
335 .Case("ha", VK_PPC_HA)
336 .Case("HIGHER", VK_PPC_HIGHER)
337 .Case("higher", VK_PPC_HIGHER)
338 .Case("HIGHERA", VK_PPC_HIGHERA)
339 .Case("highera", VK_PPC_HIGHERA)
340 .Case("HIGHEST", VK_PPC_HIGHEST)
341 .Case("highest", VK_PPC_HIGHEST)
342 .Case("HIGHESTA", VK_PPC_HIGHESTA)
343 .Case("highesta", VK_PPC_HIGHESTA)
344 .Case("GOT@L", VK_PPC_GOT_LO)
345 .Case("got@l", VK_PPC_GOT_LO)
346 .Case("GOT@H", VK_PPC_GOT_HI)
347 .Case("got@h", VK_PPC_GOT_HI)
348 .Case("GOT@HA", VK_PPC_GOT_HA)
349 .Case("got@ha", VK_PPC_GOT_HA)
350 .Case("TOCBASE", VK_PPC_TOCBASE)
351 .Case("tocbase", VK_PPC_TOCBASE)
352 .Case("TOC", VK_PPC_TOC)
353 .Case("toc", VK_PPC_TOC)
354 .Case("TOC@L", VK_PPC_TOC_LO)
355 .Case("toc@l", VK_PPC_TOC_LO)
356 .Case("TOC@H", VK_PPC_TOC_HI)
357 .Case("toc@h", VK_PPC_TOC_HI)
358 .Case("TOC@HA", VK_PPC_TOC_HA)
359 .Case("toc@ha", VK_PPC_TOC_HA)
360 .Case("TLS", VK_PPC_TLS)
361 .Case("tls", VK_PPC_TLS)
362 .Case("DTPMOD", VK_PPC_DTPMOD)
363 .Case("dtpmod", VK_PPC_DTPMOD)
364 .Case("TPREL", VK_PPC_TPREL)
365 .Case("tprel", VK_PPC_TPREL)
366 .Case("TPREL@L", VK_PPC_TPREL_LO)
367 .Case("tprel@l", VK_PPC_TPREL_LO)
368 .Case("TPREL@H", VK_PPC_TPREL_HI)
369 .Case("tprel@h", VK_PPC_TPREL_HI)
370 .Case("TPREL@HA", VK_PPC_TPREL_HA)
371 .Case("tprel@ha", VK_PPC_TPREL_HA)
372 .Case("TPREL@HIGHER", VK_PPC_TPREL_HIGHER)
373 .Case("tprel@higher", VK_PPC_TPREL_HIGHER)
374 .Case("TPREL@HIGHERA", VK_PPC_TPREL_HIGHERA)
375 .Case("tprel@highera", VK_PPC_TPREL_HIGHERA)
376 .Case("TPREL@HIGHEST", VK_PPC_TPREL_HIGHEST)
377 .Case("tprel@highest", VK_PPC_TPREL_HIGHEST)
378 .Case("TPREL@HIGHESTA", VK_PPC_TPREL_HIGHESTA)
379 .Case("tprel@highesta", VK_PPC_TPREL_HIGHESTA)
380 .Case("DTPREL", VK_PPC_DTPREL)
381 .Case("dtprel", VK_PPC_DTPREL)
382 .Case("DTPREL@L", VK_PPC_DTPREL_LO)
383 .Case("dtprel@l", VK_PPC_DTPREL_LO)
384 .Case("DTPREL@H", VK_PPC_DTPREL_HI)
385 .Case("dtprel@h", VK_PPC_DTPREL_HI)
386 .Case("DTPREL@HA", VK_PPC_DTPREL_HA)
387 .Case("dtprel@ha", VK_PPC_DTPREL_HA)
388 .Case("DTPREL@HIGHER", VK_PPC_DTPREL_HIGHER)
389 .Case("dtprel@higher", VK_PPC_DTPREL_HIGHER)
390 .Case("DTPREL@HIGHERA", VK_PPC_DTPREL_HIGHERA)
391 .Case("dtprel@highera", VK_PPC_DTPREL_HIGHERA)
392 .Case("DTPREL@HIGHEST", VK_PPC_DTPREL_HIGHEST)
393 .Case("dtprel@highest", VK_PPC_DTPREL_HIGHEST)
394 .Case("DTPREL@HIGHESTA", VK_PPC_DTPREL_HIGHESTA)
395 .Case("dtprel@highesta", VK_PPC_DTPREL_HIGHESTA)
396 .Case("GOT@TPREL", VK_PPC_GOT_TPREL)
397 .Case("got@tprel", VK_PPC_GOT_TPREL)
398 .Case("GOT@TPREL@L", VK_PPC_GOT_TPREL_LO)
399 .Case("got@tprel@l", VK_PPC_GOT_TPREL_LO)
400 .Case("GOT@TPREL@H", VK_PPC_GOT_TPREL_HI)
401 .Case("got@tprel@h", VK_PPC_GOT_TPREL_HI)
402 .Case("GOT@TPREL@HA", VK_PPC_GOT_TPREL_HA)
403 .Case("got@tprel@ha", VK_PPC_GOT_TPREL_HA)
404 .Case("GOT@DTPREL", VK_PPC_GOT_DTPREL)
405 .Case("got@dtprel", VK_PPC_GOT_DTPREL)
406 .Case("GOT@DTPREL@L", VK_PPC_GOT_DTPREL_LO)
407 .Case("got@dtprel@l", VK_PPC_GOT_DTPREL_LO)
408 .Case("GOT@DTPREL@H", VK_PPC_GOT_DTPREL_HI)
409 .Case("got@dtprel@h", VK_PPC_GOT_DTPREL_HI)
410 .Case("GOT@DTPREL@HA", VK_PPC_GOT_DTPREL_HA)
411 .Case("got@dtprel@ha", VK_PPC_GOT_DTPREL_HA)
412 .Case("GOT@TLSGD", VK_PPC_GOT_TLSGD)
413 .Case("got@tlsgd", VK_PPC_GOT_TLSGD)
414 .Case("GOT@TLSGD@L", VK_PPC_GOT_TLSGD_LO)
415 .Case("got@tlsgd@l", VK_PPC_GOT_TLSGD_LO)
416 .Case("GOT@TLSGD@H", VK_PPC_GOT_TLSGD_HI)
417 .Case("got@tlsgd@h", VK_PPC_GOT_TLSGD_HI)
418 .Case("GOT@TLSGD@HA", VK_PPC_GOT_TLSGD_HA)
419 .Case("got@tlsgd@ha", VK_PPC_GOT_TLSGD_HA)
420 .Case("GOT@TLSLD", VK_PPC_GOT_TLSLD)
421 .Case("got@tlsld", VK_PPC_GOT_TLSLD)
422 .Case("GOT@TLSLD@L", VK_PPC_GOT_TLSLD_LO)
423 .Case("got@tlsld@l", VK_PPC_GOT_TLSLD_LO)
424 .Case("GOT@TLSLD@H", VK_PPC_GOT_TLSLD_HI)
425 .Case("got@tlsld@h", VK_PPC_GOT_TLSLD_HI)
426 .Case("GOT@TLSLD@HA", VK_PPC_GOT_TLSLD_HA)
427 .Case("got@tlsld@ha", VK_PPC_GOT_TLSLD_HA)
428 .Case("NONE", VK_ARM_NONE)
429 .Case("none", VK_ARM_NONE)
430 .Case("TARGET1", VK_ARM_TARGET1)
431 .Case("target1", VK_ARM_TARGET1)
432 .Case("TARGET2", VK_ARM_TARGET2)
433 .Case("target2", VK_ARM_TARGET2)
434 .Case("PREL31", VK_ARM_PREL31)
435 .Case("prel31", VK_ARM_PREL31)
436 .Case("TLSLDO", VK_ARM_TLSLDO)
437 .Case("tlsldo", VK_ARM_TLSLDO)
438 .Case("TLSCALL", VK_ARM_TLSCALL)
439 .Case("tlscall", VK_ARM_TLSCALL)
440 .Case("TLSDESC", VK_ARM_TLSDESC)
441 .Case("tlsdesc", VK_ARM_TLSDESC)
442 .Default(VK_Invalid);
447 void MCTargetExpr::anchor() {}
451 bool MCExpr::EvaluateAsAbsolute(int64_t &Res) const {
452 return EvaluateAsAbsolute(Res, nullptr, nullptr, nullptr);
455 bool MCExpr::EvaluateAsAbsolute(int64_t &Res,
456 const MCAsmLayout &Layout) const {
457 return EvaluateAsAbsolute(Res, &Layout.getAssembler(), &Layout, nullptr);
460 bool MCExpr::EvaluateAsAbsolute(int64_t &Res,
461 const MCAsmLayout &Layout,
462 const SectionAddrMap &Addrs) const {
463 return EvaluateAsAbsolute(Res, &Layout.getAssembler(), &Layout, &Addrs);
466 bool MCExpr::EvaluateAsAbsolute(int64_t &Res, const MCAssembler &Asm) const {
467 return EvaluateAsAbsolute(Res, &Asm, nullptr, nullptr);
470 int64_t MCExpr::evaluateKnownAbsolute(const MCAsmLayout &Layout) const {
473 evaluateAsAbsolute(Res, &Layout.getAssembler(), &Layout, nullptr, true);
475 assert(Abs && "Not actually absolute");
479 bool MCExpr::EvaluateAsAbsolute(int64_t &Res, const MCAssembler *Asm,
480 const MCAsmLayout *Layout,
481 const SectionAddrMap *Addrs) const {
482 // FIXME: The use if InSet = Addrs is a hack. Setting InSet causes us
483 // absolutize differences across sections and that is what the MachO writer
485 return evaluateAsAbsolute(Res, Asm, Layout, Addrs, Addrs);
488 bool MCExpr::evaluateAsAbsolute(int64_t &Res, const MCAssembler *Asm,
489 const MCAsmLayout *Layout,
490 const SectionAddrMap *Addrs, bool InSet) const {
493 // Fast path constants.
494 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(this)) {
495 Res = CE->getValue();
499 bool IsRelocatable = EvaluateAsRelocatableImpl(
500 Value, Asm, Layout, nullptr, Addrs, InSet, /*ForceVarExpansion*/ false);
502 // Record the current value.
503 Res = Value.getConstant();
505 return IsRelocatable && Value.isAbsolute();
508 /// \brief Helper method for \see EvaluateSymbolAdd().
509 static void AttemptToFoldSymbolOffsetDifference(const MCAssembler *Asm,
510 const MCAsmLayout *Layout,
511 const SectionAddrMap *Addrs,
513 const MCSymbolRefExpr *&A,
514 const MCSymbolRefExpr *&B,
519 const MCSymbol &SA = A->getSymbol();
520 const MCSymbol &SB = B->getSymbol();
522 if (SA.isUndefined() || SB.isUndefined())
525 if (!Asm->getWriter().IsSymbolRefDifferenceFullyResolved(*Asm, A, B, InSet))
528 const MCSymbolData &AD = Asm->getSymbolData(SA);
529 const MCSymbolData &BD = Asm->getSymbolData(SB);
531 if (AD.getFragment() == BD.getFragment()) {
532 Addend += (AD.getOffset() - BD.getOffset());
534 // Pointers to Thumb symbols need to have their low-bit set to allow
536 if (Asm->isThumbFunc(&SA))
539 // Clear the symbol expr pointers to indicate we have folded these
548 const MCSectionData &SecA = *AD.getFragment()->getParent();
549 const MCSectionData &SecB = *BD.getFragment()->getParent();
551 if ((&SecA != &SecB) && !Addrs)
555 Addend += (Layout->getSymbolOffset(&Asm->getSymbolData(A->getSymbol())) -
556 Layout->getSymbolOffset(&Asm->getSymbolData(B->getSymbol())));
557 if (Addrs && (&SecA != &SecB))
558 Addend += (Addrs->lookup(&SecA) - Addrs->lookup(&SecB));
560 // Pointers to Thumb symbols need to have their low-bit set to allow
562 if (Asm->isThumbFunc(&SA))
565 // Clear the symbol expr pointers to indicate we have folded these
570 /// \brief Evaluate the result of an add between (conceptually) two MCValues.
572 /// This routine conceptually attempts to construct an MCValue:
573 /// Result = (Result_A - Result_B + Result_Cst)
574 /// from two MCValue's LHS and RHS where
575 /// Result = LHS + RHS
577 /// Result = (LHS_A - LHS_B + LHS_Cst) + (RHS_A - RHS_B + RHS_Cst).
579 /// This routine attempts to aggresively fold the operands such that the result
580 /// is representable in an MCValue, but may not always succeed.
582 /// \returns True on success, false if the result is not representable in an
585 /// NOTE: It is really important to have both the Asm and Layout arguments.
586 /// They might look redundant, but this function can be used before layout
587 /// is done (see the object streamer for example) and having the Asm argument
588 /// lets us avoid relaxations early.
589 static bool EvaluateSymbolicAdd(const MCAssembler *Asm,
590 const MCAsmLayout *Layout,
591 const SectionAddrMap *Addrs,
593 const MCValue &LHS,const MCSymbolRefExpr *RHS_A,
594 const MCSymbolRefExpr *RHS_B, int64_t RHS_Cst,
596 // FIXME: This routine (and other evaluation parts) are *incredibly* sloppy
597 // about dealing with modifiers. This will ultimately bite us, one day.
598 const MCSymbolRefExpr *LHS_A = LHS.getSymA();
599 const MCSymbolRefExpr *LHS_B = LHS.getSymB();
600 int64_t LHS_Cst = LHS.getConstant();
602 // Fold the result constant immediately.
603 int64_t Result_Cst = LHS_Cst + RHS_Cst;
605 assert((!Layout || Asm) &&
606 "Must have an assembler object if layout is given!");
608 // If we have a layout, we can fold resolved differences.
610 // First, fold out any differences which are fully resolved. By
611 // reassociating terms in
612 // Result = (LHS_A - LHS_B + LHS_Cst) + (RHS_A - RHS_B + RHS_Cst).
613 // we have the four possible differences:
618 // Since we are attempting to be as aggressive as possible about folding, we
619 // attempt to evaluate each possible alternative.
620 AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, LHS_A, LHS_B,
622 AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, LHS_A, RHS_B,
624 AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, RHS_A, LHS_B,
626 AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, RHS_A, RHS_B,
630 // We can't represent the addition or subtraction of two symbols.
631 if ((LHS_A && RHS_A) || (LHS_B && RHS_B))
634 // At this point, we have at most one additive symbol and one subtractive
635 // symbol -- find them.
636 const MCSymbolRefExpr *A = LHS_A ? LHS_A : RHS_A;
637 const MCSymbolRefExpr *B = LHS_B ? LHS_B : RHS_B;
639 // If we have a negated symbol, then we must have also have a non-negated
640 // symbol in order to encode the expression.
644 Res = MCValue::get(A, B, Result_Cst);
648 bool MCExpr::EvaluateAsRelocatable(MCValue &Res,
649 const MCAsmLayout *Layout,
650 const MCFixup *Fixup) const {
651 MCAssembler *Assembler = Layout ? &Layout->getAssembler() : nullptr;
652 return EvaluateAsRelocatableImpl(Res, Assembler, Layout, Fixup, nullptr,
653 false, /*ForceVarExpansion*/ false);
656 bool MCExpr::EvaluateAsValue(MCValue &Res, const MCAsmLayout *Layout,
657 const MCFixup *Fixup) const {
658 MCAssembler *Assembler = Layout ? &Layout->getAssembler() : nullptr;
659 return EvaluateAsRelocatableImpl(Res, Assembler, Layout, Fixup, nullptr,
660 false, /*ForceVarExpansion*/ true);
663 bool MCExpr::EvaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm,
664 const MCAsmLayout *Layout,
665 const MCFixup *Fixup,
666 const SectionAddrMap *Addrs, bool InSet,
667 bool ForceVarExpansion) const {
668 ++stats::MCExprEvaluate;
672 return cast<MCTargetExpr>(this)->EvaluateAsRelocatableImpl(Res, Layout,
676 Res = MCValue::get(cast<MCConstantExpr>(this)->getValue());
680 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(this);
681 const MCSymbol &Sym = SRE->getSymbol();
682 const MCAsmInfo &MCAsmInfo = SRE->getMCAsmInfo();
684 // Evaluate recursively if this is a variable.
685 if (Sym.isVariable() && SRE->getKind() == MCSymbolRefExpr::VK_None) {
686 if (Sym.getVariableValue()->EvaluateAsRelocatableImpl(
687 Res, Asm, Layout, Fixup, Addrs, true, ForceVarExpansion)) {
688 const MCSymbolRefExpr *A = Res.getSymA();
689 const MCSymbolRefExpr *B = Res.getSymB();
691 if (MCAsmInfo.hasSubsectionsViaSymbols()) {
692 // FIXME: This is small hack. Given
695 // the OS X assembler will completely drop the 4. We should probably
696 // include it in the relocation or produce an error if that is not
701 if (ForceVarExpansion)
703 bool IsSymbol = A && A->getSymbol().isDefined();
710 Res = MCValue::get(SRE, nullptr, 0);
715 const MCUnaryExpr *AUE = cast<MCUnaryExpr>(this);
718 if (!AUE->getSubExpr()->EvaluateAsRelocatableImpl(Value, Asm, Layout,
723 switch (AUE->getOpcode()) {
724 case MCUnaryExpr::LNot:
725 if (!Value.isAbsolute())
727 Res = MCValue::get(!Value.getConstant());
729 case MCUnaryExpr::Minus:
730 /// -(a - b + const) ==> (b - a - const)
731 if (Value.getSymA() && !Value.getSymB())
733 Res = MCValue::get(Value.getSymB(), Value.getSymA(),
734 -Value.getConstant());
736 case MCUnaryExpr::Not:
737 if (!Value.isAbsolute())
739 Res = MCValue::get(~Value.getConstant());
741 case MCUnaryExpr::Plus:
750 const MCBinaryExpr *ABE = cast<MCBinaryExpr>(this);
751 MCValue LHSValue, RHSValue;
753 if (!ABE->getLHS()->EvaluateAsRelocatableImpl(LHSValue, Asm, Layout,
755 ForceVarExpansion) ||
756 !ABE->getRHS()->EvaluateAsRelocatableImpl(RHSValue, Asm, Layout,
761 // We only support a few operations on non-constant expressions, handle
763 if (!LHSValue.isAbsolute() || !RHSValue.isAbsolute()) {
764 switch (ABE->getOpcode()) {
767 case MCBinaryExpr::Sub:
768 // Negate RHS and add.
769 return EvaluateSymbolicAdd(Asm, Layout, Addrs, InSet, LHSValue,
770 RHSValue.getSymB(), RHSValue.getSymA(),
771 -RHSValue.getConstant(),
774 case MCBinaryExpr::Add:
775 return EvaluateSymbolicAdd(Asm, Layout, Addrs, InSet, LHSValue,
776 RHSValue.getSymA(), RHSValue.getSymB(),
777 RHSValue.getConstant(),
782 // FIXME: We need target hooks for the evaluation. It may be limited in
783 // width, and gas defines the result of comparisons and right shifts
784 // differently from Apple as.
785 int64_t LHS = LHSValue.getConstant(), RHS = RHSValue.getConstant();
787 switch (ABE->getOpcode()) {
788 case MCBinaryExpr::Add: Result = LHS + RHS; break;
789 case MCBinaryExpr::And: Result = LHS & RHS; break;
790 case MCBinaryExpr::Div: Result = LHS / RHS; break;
791 case MCBinaryExpr::EQ: Result = LHS == RHS; break;
792 case MCBinaryExpr::GT: Result = LHS > RHS; break;
793 case MCBinaryExpr::GTE: Result = LHS >= RHS; break;
794 case MCBinaryExpr::LAnd: Result = LHS && RHS; break;
795 case MCBinaryExpr::LOr: Result = LHS || RHS; break;
796 case MCBinaryExpr::LT: Result = LHS < RHS; break;
797 case MCBinaryExpr::LTE: Result = LHS <= RHS; break;
798 case MCBinaryExpr::Mod: Result = LHS % RHS; break;
799 case MCBinaryExpr::Mul: Result = LHS * RHS; break;
800 case MCBinaryExpr::NE: Result = LHS != RHS; break;
801 case MCBinaryExpr::Or: Result = LHS | RHS; break;
802 case MCBinaryExpr::Shl: Result = LHS << RHS; break;
803 case MCBinaryExpr::Shr: Result = LHS >> RHS; break;
804 case MCBinaryExpr::Sub: Result = LHS - RHS; break;
805 case MCBinaryExpr::Xor: Result = LHS ^ RHS; break;
808 Res = MCValue::get(Result);
813 llvm_unreachable("Invalid assembly expression kind!");
816 const MCSection *MCExpr::FindAssociatedSection() const {
819 // We never look through target specific expressions.
820 return cast<MCTargetExpr>(this)->FindAssociatedSection();
823 return MCSymbol::AbsolutePseudoSection;
826 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(this);
827 const MCSymbol &Sym = SRE->getSymbol();
830 return &Sym.getSection();
836 return cast<MCUnaryExpr>(this)->getSubExpr()->FindAssociatedSection();
839 const MCBinaryExpr *BE = cast<MCBinaryExpr>(this);
840 const MCSection *LHS_S = BE->getLHS()->FindAssociatedSection();
841 const MCSection *RHS_S = BE->getRHS()->FindAssociatedSection();
843 // If either section is absolute, return the other.
844 if (LHS_S == MCSymbol::AbsolutePseudoSection)
846 if (RHS_S == MCSymbol::AbsolutePseudoSection)
849 // Otherwise, return the first non-null section.
850 return LHS_S ? LHS_S : RHS_S;
854 llvm_unreachable("Invalid assembly expression kind!");