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/MCAsmInfo.h"
15 #include "llvm/MC/MCAsmLayout.h"
16 #include "llvm/MC/MCAssembler.h"
17 #include "llvm/MC/MCContext.h"
18 #include "llvm/MC/MCObjectWriter.h"
19 #include "llvm/MC/MCSymbol.h"
20 #include "llvm/MC/MCValue.h"
21 #include "llvm/Support/Debug.h"
22 #include "llvm/Support/ErrorHandling.h"
23 #include "llvm/Support/raw_ostream.h"
28 STATISTIC(MCExprEvaluate, "Number of MCExpr evaluations");
32 void MCExpr::print(raw_ostream &OS) const {
35 return cast<MCTargetExpr>(this)->PrintImpl(OS);
36 case MCExpr::Constant:
37 OS << cast<MCConstantExpr>(*this).getValue();
40 case MCExpr::SymbolRef: {
41 const MCSymbolRefExpr &SRE = cast<MCSymbolRefExpr>(*this);
42 const MCSymbol &Sym = SRE.getSymbol();
43 // Parenthesize names that start with $ so that they don't look like
45 bool UseParens = Sym.getName()[0] == '$';
47 OS << '(' << Sym << ')';
51 if (SRE.getKind() != MCSymbolRefExpr::VK_None) {
52 if (SRE.getMCAsmInfo().useParensForSymbolVariant())
53 OS << '(' << MCSymbolRefExpr::getVariantKindName(SRE.getKind()) << ')';
55 OS << '@' << MCSymbolRefExpr::getVariantKindName(SRE.getKind());
62 const MCUnaryExpr &UE = cast<MCUnaryExpr>(*this);
63 switch (UE.getOpcode()) {
64 case MCUnaryExpr::LNot: OS << '!'; break;
65 case MCUnaryExpr::Minus: OS << '-'; break;
66 case MCUnaryExpr::Not: OS << '~'; break;
67 case MCUnaryExpr::Plus: OS << '+'; break;
69 OS << *UE.getSubExpr();
73 case MCExpr::Binary: {
74 const MCBinaryExpr &BE = cast<MCBinaryExpr>(*this);
76 // Only print parens around the LHS if it is non-trivial.
77 if (isa<MCConstantExpr>(BE.getLHS()) || isa<MCSymbolRefExpr>(BE.getLHS())) {
80 OS << '(' << *BE.getLHS() << ')';
83 switch (BE.getOpcode()) {
84 case MCBinaryExpr::Add:
85 // Print "X-42" instead of "X+-42".
86 if (const MCConstantExpr *RHSC = dyn_cast<MCConstantExpr>(BE.getRHS())) {
87 if (RHSC->getValue() < 0) {
88 OS << RHSC->getValue();
95 case MCBinaryExpr::And: OS << '&'; break;
96 case MCBinaryExpr::Div: OS << '/'; break;
97 case MCBinaryExpr::EQ: OS << "=="; break;
98 case MCBinaryExpr::GT: OS << '>'; break;
99 case MCBinaryExpr::GTE: OS << ">="; break;
100 case MCBinaryExpr::LAnd: OS << "&&"; break;
101 case MCBinaryExpr::LOr: OS << "||"; break;
102 case MCBinaryExpr::LT: OS << '<'; break;
103 case MCBinaryExpr::LTE: OS << "<="; break;
104 case MCBinaryExpr::Mod: OS << '%'; break;
105 case MCBinaryExpr::Mul: OS << '*'; break;
106 case MCBinaryExpr::NE: OS << "!="; break;
107 case MCBinaryExpr::Or: OS << '|'; break;
108 case MCBinaryExpr::Shl: OS << "<<"; break;
109 case MCBinaryExpr::Shr: OS << ">>"; break;
110 case MCBinaryExpr::Sub: OS << '-'; break;
111 case MCBinaryExpr::Xor: OS << '^'; break;
114 // Only print parens around the LHS if it is non-trivial.
115 if (isa<MCConstantExpr>(BE.getRHS()) || isa<MCSymbolRefExpr>(BE.getRHS())) {
118 OS << '(' << *BE.getRHS() << ')';
124 llvm_unreachable("Invalid expression kind!");
127 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
128 void MCExpr::dump() const {
136 const MCBinaryExpr *MCBinaryExpr::Create(Opcode Opc, const MCExpr *LHS,
137 const MCExpr *RHS, MCContext &Ctx) {
138 return new (Ctx) MCBinaryExpr(Opc, LHS, RHS);
141 const MCUnaryExpr *MCUnaryExpr::Create(Opcode Opc, const MCExpr *Expr,
143 return new (Ctx) MCUnaryExpr(Opc, Expr);
146 const MCConstantExpr *MCConstantExpr::Create(int64_t Value, MCContext &Ctx) {
147 return new (Ctx) MCConstantExpr(Value);
152 const MCSymbolRefExpr *MCSymbolRefExpr::Create(const MCSymbol *Sym,
155 return new (Ctx) MCSymbolRefExpr(Sym, Kind, Ctx.getAsmInfo());
158 const MCSymbolRefExpr *MCSymbolRefExpr::Create(StringRef Name, VariantKind Kind,
160 return Create(Ctx.GetOrCreateSymbol(Name), Kind, Ctx);
163 StringRef MCSymbolRefExpr::getVariantKindName(VariantKind Kind) {
165 case VK_Invalid: return "<<invalid>>";
166 case VK_None: return "<<none>>";
168 case VK_GOT: return "GOT";
169 case VK_GOTOFF: return "GOTOFF";
170 case VK_GOTPCREL: return "GOTPCREL";
171 case VK_GOTTPOFF: return "GOTTPOFF";
172 case VK_INDNTPOFF: return "INDNTPOFF";
173 case VK_NTPOFF: return "NTPOFF";
174 case VK_GOTNTPOFF: return "GOTNTPOFF";
175 case VK_PLT: return "PLT";
176 case VK_TLSGD: return "TLSGD";
177 case VK_TLSLD: return "TLSLD";
178 case VK_TLSLDM: return "TLSLDM";
179 case VK_TPOFF: return "TPOFF";
180 case VK_DTPOFF: return "DTPOFF";
181 case VK_TLVP: return "TLVP";
182 case VK_TLVPPAGE: return "TLVPPAGE";
183 case VK_TLVPPAGEOFF: return "TLVPPAGEOFF";
184 case VK_PAGE: return "PAGE";
185 case VK_PAGEOFF: return "PAGEOFF";
186 case VK_GOTPAGE: return "GOTPAGE";
187 case VK_GOTPAGEOFF: return "GOTPAGEOFF";
188 case VK_SECREL: return "SECREL32";
189 case VK_WEAKREF: return "WEAKREF";
190 case VK_ARM_NONE: return "none";
191 case VK_ARM_TARGET1: return "target1";
192 case VK_ARM_TARGET2: return "target2";
193 case VK_ARM_PREL31: return "prel31";
194 case VK_ARM_TLSLDO: return "tlsldo";
195 case VK_ARM_TLSCALL: return "tlscall";
196 case VK_ARM_TLSDESC: return "tlsdesc";
197 case VK_ARM_TLSDESCSEQ: return "tlsdescseq";
198 case VK_PPC_LO: return "l";
199 case VK_PPC_HI: return "h";
200 case VK_PPC_HA: return "ha";
201 case VK_PPC_HIGHER: return "higher";
202 case VK_PPC_HIGHERA: return "highera";
203 case VK_PPC_HIGHEST: return "highest";
204 case VK_PPC_HIGHESTA: return "highesta";
205 case VK_PPC_GOT_LO: return "got@l";
206 case VK_PPC_GOT_HI: return "got@h";
207 case VK_PPC_GOT_HA: return "got@ha";
208 case VK_PPC_TOCBASE: return "tocbase";
209 case VK_PPC_TOC: return "toc";
210 case VK_PPC_TOC_LO: return "toc@l";
211 case VK_PPC_TOC_HI: return "toc@h";
212 case VK_PPC_TOC_HA: return "toc@ha";
213 case VK_PPC_DTPMOD: return "dtpmod";
214 case VK_PPC_TPREL: return "tprel";
215 case VK_PPC_TPREL_LO: return "tprel@l";
216 case VK_PPC_TPREL_HI: return "tprel@h";
217 case VK_PPC_TPREL_HA: return "tprel@ha";
218 case VK_PPC_TPREL_HIGHER: return "tprel@higher";
219 case VK_PPC_TPREL_HIGHERA: return "tprel@highera";
220 case VK_PPC_TPREL_HIGHEST: return "tprel@highest";
221 case VK_PPC_TPREL_HIGHESTA: return "tprel@highesta";
222 case VK_PPC_DTPREL: return "dtprel";
223 case VK_PPC_DTPREL_LO: return "dtprel@l";
224 case VK_PPC_DTPREL_HI: return "dtprel@h";
225 case VK_PPC_DTPREL_HA: return "dtprel@ha";
226 case VK_PPC_DTPREL_HIGHER: return "dtprel@higher";
227 case VK_PPC_DTPREL_HIGHERA: return "dtprel@highera";
228 case VK_PPC_DTPREL_HIGHEST: return "dtprel@highest";
229 case VK_PPC_DTPREL_HIGHESTA: return "dtprel@highesta";
230 case VK_PPC_GOT_TPREL: return "got@tprel";
231 case VK_PPC_GOT_TPREL_LO: return "got@tprel@l";
232 case VK_PPC_GOT_TPREL_HI: return "got@tprel@h";
233 case VK_PPC_GOT_TPREL_HA: return "got@tprel@ha";
234 case VK_PPC_GOT_DTPREL: return "got@dtprel";
235 case VK_PPC_GOT_DTPREL_LO: return "got@dtprel@l";
236 case VK_PPC_GOT_DTPREL_HI: return "got@dtprel@h";
237 case VK_PPC_GOT_DTPREL_HA: return "got@dtprel@ha";
238 case VK_PPC_TLS: return "tls";
239 case VK_PPC_GOT_TLSGD: return "got@tlsgd";
240 case VK_PPC_GOT_TLSGD_LO: return "got@tlsgd@l";
241 case VK_PPC_GOT_TLSGD_HI: return "got@tlsgd@h";
242 case VK_PPC_GOT_TLSGD_HA: return "got@tlsgd@ha";
243 case VK_PPC_TLSGD: return "tlsgd";
244 case VK_PPC_GOT_TLSLD: return "got@tlsld";
245 case VK_PPC_GOT_TLSLD_LO: return "got@tlsld@l";
246 case VK_PPC_GOT_TLSLD_HI: return "got@tlsld@h";
247 case VK_PPC_GOT_TLSLD_HA: return "got@tlsld@ha";
248 case VK_PPC_TLSLD: return "tlsld";
249 case VK_Mips_GPREL: return "GPREL";
250 case VK_Mips_GOT_CALL: return "GOT_CALL";
251 case VK_Mips_GOT16: return "GOT16";
252 case VK_Mips_GOT: return "GOT";
253 case VK_Mips_ABS_HI: return "ABS_HI";
254 case VK_Mips_ABS_LO: return "ABS_LO";
255 case VK_Mips_TLSGD: return "TLSGD";
256 case VK_Mips_TLSLDM: return "TLSLDM";
257 case VK_Mips_DTPREL_HI: return "DTPREL_HI";
258 case VK_Mips_DTPREL_LO: return "DTPREL_LO";
259 case VK_Mips_GOTTPREL: return "GOTTPREL";
260 case VK_Mips_TPREL_HI: return "TPREL_HI";
261 case VK_Mips_TPREL_LO: return "TPREL_LO";
262 case VK_Mips_GPOFF_HI: return "GPOFF_HI";
263 case VK_Mips_GPOFF_LO: return "GPOFF_LO";
264 case VK_Mips_GOT_DISP: return "GOT_DISP";
265 case VK_Mips_GOT_PAGE: return "GOT_PAGE";
266 case VK_Mips_GOT_OFST: return "GOT_OFST";
267 case VK_Mips_HIGHER: return "HIGHER";
268 case VK_Mips_HIGHEST: return "HIGHEST";
269 case VK_Mips_GOT_HI16: return "GOT_HI16";
270 case VK_Mips_GOT_LO16: return "GOT_LO16";
271 case VK_Mips_CALL_HI16: return "CALL_HI16";
272 case VK_Mips_CALL_LO16: return "CALL_LO16";
273 case VK_COFF_IMGREL32: return "IMGREL32";
275 llvm_unreachable("Invalid variant kind");
278 MCSymbolRefExpr::VariantKind
279 MCSymbolRefExpr::getVariantKindForName(StringRef Name) {
280 return StringSwitch<VariantKind>(Name)
283 .Case("GOTOFF", VK_GOTOFF)
284 .Case("gotoff", VK_GOTOFF)
285 .Case("GOTPCREL", VK_GOTPCREL)
286 .Case("gotpcrel", VK_GOTPCREL)
287 .Case("GOTTPOFF", VK_GOTTPOFF)
288 .Case("gottpoff", VK_GOTTPOFF)
289 .Case("INDNTPOFF", VK_INDNTPOFF)
290 .Case("indntpoff", VK_INDNTPOFF)
291 .Case("NTPOFF", VK_NTPOFF)
292 .Case("ntpoff", VK_NTPOFF)
293 .Case("GOTNTPOFF", VK_GOTNTPOFF)
294 .Case("gotntpoff", VK_GOTNTPOFF)
297 .Case("TLSGD", VK_TLSGD)
298 .Case("tlsgd", VK_TLSGD)
299 .Case("TLSLD", VK_TLSLD)
300 .Case("tlsld", VK_TLSLD)
301 .Case("TLSLDM", VK_TLSLDM)
302 .Case("tlsldm", VK_TLSLDM)
303 .Case("TPOFF", VK_TPOFF)
304 .Case("tpoff", VK_TPOFF)
305 .Case("DTPOFF", VK_DTPOFF)
306 .Case("dtpoff", VK_DTPOFF)
307 .Case("TLVP", VK_TLVP)
308 .Case("tlvp", VK_TLVP)
309 .Case("TLVPPAGE", VK_TLVPPAGE)
310 .Case("tlvppage", VK_TLVPPAGE)
311 .Case("TLVPPAGEOFF", VK_TLVPPAGEOFF)
312 .Case("tlvppageoff", VK_TLVPPAGEOFF)
313 .Case("PAGE", VK_PAGE)
314 .Case("page", VK_PAGE)
315 .Case("PAGEOFF", VK_PAGEOFF)
316 .Case("pageoff", VK_PAGEOFF)
317 .Case("GOTPAGE", VK_GOTPAGE)
318 .Case("gotpage", VK_GOTPAGE)
319 .Case("GOTPAGEOFF", VK_GOTPAGEOFF)
320 .Case("gotpageoff", VK_GOTPAGEOFF)
321 .Case("IMGREL", VK_COFF_IMGREL32)
322 .Case("imgrel", VK_COFF_IMGREL32)
323 .Case("SECREL32", VK_SECREL)
324 .Case("secrel32", VK_SECREL)
325 .Case("L", VK_PPC_LO)
326 .Case("l", VK_PPC_LO)
327 .Case("H", VK_PPC_HI)
328 .Case("h", VK_PPC_HI)
329 .Case("HA", VK_PPC_HA)
330 .Case("ha", VK_PPC_HA)
331 .Case("HIGHER", VK_PPC_HIGHER)
332 .Case("higher", VK_PPC_HIGHER)
333 .Case("HIGHERA", VK_PPC_HIGHERA)
334 .Case("highera", VK_PPC_HIGHERA)
335 .Case("HIGHEST", VK_PPC_HIGHEST)
336 .Case("highest", VK_PPC_HIGHEST)
337 .Case("HIGHESTA", VK_PPC_HIGHESTA)
338 .Case("highesta", VK_PPC_HIGHESTA)
339 .Case("GOT@L", VK_PPC_GOT_LO)
340 .Case("got@l", VK_PPC_GOT_LO)
341 .Case("GOT@H", VK_PPC_GOT_HI)
342 .Case("got@h", VK_PPC_GOT_HI)
343 .Case("GOT@HA", VK_PPC_GOT_HA)
344 .Case("got@ha", VK_PPC_GOT_HA)
345 .Case("TOCBASE", VK_PPC_TOCBASE)
346 .Case("tocbase", VK_PPC_TOCBASE)
347 .Case("TOC", VK_PPC_TOC)
348 .Case("toc", VK_PPC_TOC)
349 .Case("TOC@L", VK_PPC_TOC_LO)
350 .Case("toc@l", VK_PPC_TOC_LO)
351 .Case("TOC@H", VK_PPC_TOC_HI)
352 .Case("toc@h", VK_PPC_TOC_HI)
353 .Case("TOC@HA", VK_PPC_TOC_HA)
354 .Case("toc@ha", VK_PPC_TOC_HA)
355 .Case("TLS", VK_PPC_TLS)
356 .Case("tls", VK_PPC_TLS)
357 .Case("DTPMOD", VK_PPC_DTPMOD)
358 .Case("dtpmod", VK_PPC_DTPMOD)
359 .Case("TPREL", VK_PPC_TPREL)
360 .Case("tprel", VK_PPC_TPREL)
361 .Case("TPREL@L", VK_PPC_TPREL_LO)
362 .Case("tprel@l", VK_PPC_TPREL_LO)
363 .Case("TPREL@H", VK_PPC_TPREL_HI)
364 .Case("tprel@h", VK_PPC_TPREL_HI)
365 .Case("TPREL@HA", VK_PPC_TPREL_HA)
366 .Case("tprel@ha", VK_PPC_TPREL_HA)
367 .Case("TPREL@HIGHER", VK_PPC_TPREL_HIGHER)
368 .Case("tprel@higher", VK_PPC_TPREL_HIGHER)
369 .Case("TPREL@HIGHERA", VK_PPC_TPREL_HIGHERA)
370 .Case("tprel@highera", VK_PPC_TPREL_HIGHERA)
371 .Case("TPREL@HIGHEST", VK_PPC_TPREL_HIGHEST)
372 .Case("tprel@highest", VK_PPC_TPREL_HIGHEST)
373 .Case("TPREL@HIGHESTA", VK_PPC_TPREL_HIGHESTA)
374 .Case("tprel@highesta", VK_PPC_TPREL_HIGHESTA)
375 .Case("DTPREL", VK_PPC_DTPREL)
376 .Case("dtprel", VK_PPC_DTPREL)
377 .Case("DTPREL@L", VK_PPC_DTPREL_LO)
378 .Case("dtprel@l", VK_PPC_DTPREL_LO)
379 .Case("DTPREL@H", VK_PPC_DTPREL_HI)
380 .Case("dtprel@h", VK_PPC_DTPREL_HI)
381 .Case("DTPREL@HA", VK_PPC_DTPREL_HA)
382 .Case("dtprel@ha", VK_PPC_DTPREL_HA)
383 .Case("DTPREL@HIGHER", VK_PPC_DTPREL_HIGHER)
384 .Case("dtprel@higher", VK_PPC_DTPREL_HIGHER)
385 .Case("DTPREL@HIGHERA", VK_PPC_DTPREL_HIGHERA)
386 .Case("dtprel@highera", VK_PPC_DTPREL_HIGHERA)
387 .Case("DTPREL@HIGHEST", VK_PPC_DTPREL_HIGHEST)
388 .Case("dtprel@highest", VK_PPC_DTPREL_HIGHEST)
389 .Case("DTPREL@HIGHESTA", VK_PPC_DTPREL_HIGHESTA)
390 .Case("dtprel@highesta", VK_PPC_DTPREL_HIGHESTA)
391 .Case("GOT@TPREL", VK_PPC_GOT_TPREL)
392 .Case("got@tprel", VK_PPC_GOT_TPREL)
393 .Case("GOT@TPREL@L", VK_PPC_GOT_TPREL_LO)
394 .Case("got@tprel@l", VK_PPC_GOT_TPREL_LO)
395 .Case("GOT@TPREL@H", VK_PPC_GOT_TPREL_HI)
396 .Case("got@tprel@h", VK_PPC_GOT_TPREL_HI)
397 .Case("GOT@TPREL@HA", VK_PPC_GOT_TPREL_HA)
398 .Case("got@tprel@ha", VK_PPC_GOT_TPREL_HA)
399 .Case("GOT@DTPREL", VK_PPC_GOT_DTPREL)
400 .Case("got@dtprel", VK_PPC_GOT_DTPREL)
401 .Case("GOT@DTPREL@L", VK_PPC_GOT_DTPREL_LO)
402 .Case("got@dtprel@l", VK_PPC_GOT_DTPREL_LO)
403 .Case("GOT@DTPREL@H", VK_PPC_GOT_DTPREL_HI)
404 .Case("got@dtprel@h", VK_PPC_GOT_DTPREL_HI)
405 .Case("GOT@DTPREL@HA", VK_PPC_GOT_DTPREL_HA)
406 .Case("got@dtprel@ha", VK_PPC_GOT_DTPREL_HA)
407 .Case("GOT@TLSGD", VK_PPC_GOT_TLSGD)
408 .Case("got@tlsgd", VK_PPC_GOT_TLSGD)
409 .Case("GOT@TLSGD@L", VK_PPC_GOT_TLSGD_LO)
410 .Case("got@tlsgd@l", VK_PPC_GOT_TLSGD_LO)
411 .Case("GOT@TLSGD@H", VK_PPC_GOT_TLSGD_HI)
412 .Case("got@tlsgd@h", VK_PPC_GOT_TLSGD_HI)
413 .Case("GOT@TLSGD@HA", VK_PPC_GOT_TLSGD_HA)
414 .Case("got@tlsgd@ha", VK_PPC_GOT_TLSGD_HA)
415 .Case("GOT@TLSLD", VK_PPC_GOT_TLSLD)
416 .Case("got@tlsld", VK_PPC_GOT_TLSLD)
417 .Case("GOT@TLSLD@L", VK_PPC_GOT_TLSLD_LO)
418 .Case("got@tlsld@l", VK_PPC_GOT_TLSLD_LO)
419 .Case("GOT@TLSLD@H", VK_PPC_GOT_TLSLD_HI)
420 .Case("got@tlsld@h", VK_PPC_GOT_TLSLD_HI)
421 .Case("GOT@TLSLD@HA", VK_PPC_GOT_TLSLD_HA)
422 .Case("got@tlsld@ha", VK_PPC_GOT_TLSLD_HA)
423 .Case("NONE", VK_ARM_NONE)
424 .Case("none", VK_ARM_NONE)
425 .Case("TARGET1", VK_ARM_TARGET1)
426 .Case("target1", VK_ARM_TARGET1)
427 .Case("TARGET2", VK_ARM_TARGET2)
428 .Case("target2", VK_ARM_TARGET2)
429 .Case("PREL31", VK_ARM_PREL31)
430 .Case("prel31", VK_ARM_PREL31)
431 .Case("TLSLDO", VK_ARM_TLSLDO)
432 .Case("tlsldo", VK_ARM_TLSLDO)
433 .Case("TLSCALL", VK_ARM_TLSCALL)
434 .Case("tlscall", VK_ARM_TLSCALL)
435 .Case("TLSDESC", VK_ARM_TLSDESC)
436 .Case("tlsdesc", VK_ARM_TLSDESC)
437 .Default(VK_Invalid);
442 void MCTargetExpr::anchor() {}
446 bool MCExpr::EvaluateAsAbsolute(int64_t &Res) const {
447 return EvaluateAsAbsolute(Res, 0, 0, 0);
450 bool MCExpr::EvaluateAsAbsolute(int64_t &Res,
451 const MCAsmLayout &Layout) const {
452 return EvaluateAsAbsolute(Res, &Layout.getAssembler(), &Layout, 0);
455 bool MCExpr::EvaluateAsAbsolute(int64_t &Res,
456 const MCAsmLayout &Layout,
457 const SectionAddrMap &Addrs) const {
458 return EvaluateAsAbsolute(Res, &Layout.getAssembler(), &Layout, &Addrs);
461 bool MCExpr::EvaluateAsAbsolute(int64_t &Res, const MCAssembler &Asm) const {
462 return EvaluateAsAbsolute(Res, &Asm, 0, 0);
465 bool MCExpr::EvaluateAsAbsolute(int64_t &Res, const MCAssembler *Asm,
466 const MCAsmLayout *Layout,
467 const SectionAddrMap *Addrs) const {
470 // Fast path constants.
471 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(this)) {
472 Res = CE->getValue();
476 // FIXME: The use if InSet = Addrs is a hack. Setting InSet causes us
477 // absolutize differences across sections and that is what the MachO writer
480 EvaluateAsRelocatableImpl(Value, Asm, Layout, Addrs, /*InSet*/ Addrs);
482 // Record the current value.
483 Res = Value.getConstant();
485 return IsRelocatable && Value.isAbsolute();
488 /// \brief Helper method for \see EvaluateSymbolAdd().
489 static void AttemptToFoldSymbolOffsetDifference(const MCAssembler *Asm,
490 const MCAsmLayout *Layout,
491 const SectionAddrMap *Addrs,
493 const MCSymbolRefExpr *&A,
494 const MCSymbolRefExpr *&B,
499 const MCSymbol &SA = A->getSymbol();
500 const MCSymbol &SB = B->getSymbol();
502 if (SA.isUndefined() || SB.isUndefined())
505 if (!Asm->getWriter().IsSymbolRefDifferenceFullyResolved(*Asm, A, B, InSet))
508 MCSymbolData &AD = Asm->getSymbolData(SA);
509 MCSymbolData &BD = Asm->getSymbolData(SB);
511 if (AD.getFragment() == BD.getFragment()) {
512 Addend += (AD.getOffset() - BD.getOffset());
514 // Pointers to Thumb symbols need to have their low-bit set to allow
516 if (Asm->isThumbFunc(&SA))
519 // Clear the symbol expr pointers to indicate we have folded these
528 const MCSectionData &SecA = *AD.getFragment()->getParent();
529 const MCSectionData &SecB = *BD.getFragment()->getParent();
531 if ((&SecA != &SecB) && !Addrs)
535 Addend += (Layout->getSymbolOffset(&Asm->getSymbolData(A->getSymbol())) -
536 Layout->getSymbolOffset(&Asm->getSymbolData(B->getSymbol())));
537 if (Addrs && (&SecA != &SecB))
538 Addend += (Addrs->lookup(&SecA) - Addrs->lookup(&SecB));
540 // Pointers to Thumb symbols need to have their low-bit set to allow
542 if (Asm->isThumbFunc(&SA))
545 // Clear the symbol expr pointers to indicate we have folded these
550 /// \brief Evaluate the result of an add between (conceptually) two MCValues.
552 /// This routine conceptually attempts to construct an MCValue:
553 /// Result = (Result_A - Result_B + Result_Cst)
554 /// from two MCValue's LHS and RHS where
555 /// Result = LHS + RHS
557 /// Result = (LHS_A - LHS_B + LHS_Cst) + (RHS_A - RHS_B + RHS_Cst).
559 /// This routine attempts to aggresively fold the operands such that the result
560 /// is representable in an MCValue, but may not always succeed.
562 /// \returns True on success, false if the result is not representable in an
565 /// NOTE: It is really important to have both the Asm and Layout arguments.
566 /// They might look redundant, but this function can be used before layout
567 /// is done (see the object streamer for example) and having the Asm argument
568 /// lets us avoid relaxations early.
569 static bool EvaluateSymbolicAdd(const MCAssembler *Asm,
570 const MCAsmLayout *Layout,
571 const SectionAddrMap *Addrs,
573 const MCValue &LHS,const MCSymbolRefExpr *RHS_A,
574 const MCSymbolRefExpr *RHS_B, int64_t RHS_Cst,
576 // FIXME: This routine (and other evaluation parts) are *incredibly* sloppy
577 // about dealing with modifiers. This will ultimately bite us, one day.
578 const MCSymbolRefExpr *LHS_A = LHS.getSymA();
579 const MCSymbolRefExpr *LHS_B = LHS.getSymB();
580 int64_t LHS_Cst = LHS.getConstant();
582 // Fold the result constant immediately.
583 int64_t Result_Cst = LHS_Cst + RHS_Cst;
585 assert((!Layout || Asm) &&
586 "Must have an assembler object if layout is given!");
588 // If we have a layout, we can fold resolved differences.
590 // First, fold out any differences which are fully resolved. By
591 // reassociating terms in
592 // Result = (LHS_A - LHS_B + LHS_Cst) + (RHS_A - RHS_B + RHS_Cst).
593 // we have the four possible differences:
598 // Since we are attempting to be as aggressive as possible about folding, we
599 // attempt to evaluate each possible alternative.
600 AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, LHS_A, LHS_B,
602 AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, LHS_A, RHS_B,
604 AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, RHS_A, LHS_B,
606 AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, RHS_A, RHS_B,
610 // We can't represent the addition or subtraction of two symbols.
611 if ((LHS_A && RHS_A) || (LHS_B && RHS_B))
614 // At this point, we have at most one additive symbol and one subtractive
615 // symbol -- find them.
616 const MCSymbolRefExpr *A = LHS_A ? LHS_A : RHS_A;
617 const MCSymbolRefExpr *B = LHS_B ? LHS_B : RHS_B;
619 // If we have a negated symbol, then we must have also have a non-negated
620 // symbol in order to encode the expression.
624 Res = MCValue::get(A, B, Result_Cst);
628 bool MCExpr::EvaluateAsRelocatable(MCValue &Res,
629 const MCAsmLayout *Layout) const {
630 MCAssembler *Assembler = Layout ? &Layout->getAssembler() : 0;
631 return EvaluateAsRelocatableImpl(Res, Assembler, Layout, 0, false);
634 bool MCExpr::EvaluateAsRelocatableImpl(MCValue &Res,
635 const MCAssembler *Asm,
636 const MCAsmLayout *Layout,
637 const SectionAddrMap *Addrs,
639 ++stats::MCExprEvaluate;
643 return cast<MCTargetExpr>(this)->EvaluateAsRelocatableImpl(Res, Layout);
646 Res = MCValue::get(cast<MCConstantExpr>(this)->getValue());
650 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(this);
651 const MCSymbol &Sym = SRE->getSymbol();
652 const MCAsmInfo &MCAsmInfo = SRE->getMCAsmInfo();
654 // Evaluate recursively if this is a variable.
655 if (Sym.isVariable()) {
656 if (Sym.getVariableValue()->EvaluateAsRelocatableImpl(Res, Asm, Layout,
658 const MCSymbolRefExpr *A = Res.getSymA();
659 const MCSymbolRefExpr *B = Res.getSymB();
661 if (MCAsmInfo.hasSubsectionsViaSymbols()) {
662 // FIXME: This is small hack. Given
665 // the OS X assembler will completely drop the 4. We should probably
666 // include it in the relocation or produce an error if that is not
671 bool IsSymbol = A && A->getSymbol().isDefined();
672 bool IsWeakRef = SRE->getKind() == MCSymbolRefExpr::VK_WEAKREF;
673 if (!IsSymbol && !IsWeakRef)
679 Res = MCValue::get(SRE, 0, 0);
684 const MCUnaryExpr *AUE = cast<MCUnaryExpr>(this);
687 if (!AUE->getSubExpr()->EvaluateAsRelocatableImpl(Value, Asm, Layout,
691 switch (AUE->getOpcode()) {
692 case MCUnaryExpr::LNot:
693 if (!Value.isAbsolute())
695 Res = MCValue::get(!Value.getConstant());
697 case MCUnaryExpr::Minus:
698 /// -(a - b + const) ==> (b - a - const)
699 if (Value.getSymA() && !Value.getSymB())
701 Res = MCValue::get(Value.getSymB(), Value.getSymA(),
702 -Value.getConstant());
704 case MCUnaryExpr::Not:
705 if (!Value.isAbsolute())
707 Res = MCValue::get(~Value.getConstant());
709 case MCUnaryExpr::Plus:
718 const MCBinaryExpr *ABE = cast<MCBinaryExpr>(this);
719 MCValue LHSValue, RHSValue;
721 if (!ABE->getLHS()->EvaluateAsRelocatableImpl(LHSValue, Asm, Layout,
723 !ABE->getRHS()->EvaluateAsRelocatableImpl(RHSValue, Asm, Layout,
727 // We only support a few operations on non-constant expressions, handle
729 if (!LHSValue.isAbsolute() || !RHSValue.isAbsolute()) {
730 switch (ABE->getOpcode()) {
733 case MCBinaryExpr::Sub:
734 // Negate RHS and add.
735 return EvaluateSymbolicAdd(Asm, Layout, Addrs, InSet, LHSValue,
736 RHSValue.getSymB(), RHSValue.getSymA(),
737 -RHSValue.getConstant(),
740 case MCBinaryExpr::Add:
741 return EvaluateSymbolicAdd(Asm, Layout, Addrs, InSet, LHSValue,
742 RHSValue.getSymA(), RHSValue.getSymB(),
743 RHSValue.getConstant(),
748 // FIXME: We need target hooks for the evaluation. It may be limited in
749 // width, and gas defines the result of comparisons and right shifts
750 // differently from Apple as.
751 int64_t LHS = LHSValue.getConstant(), RHS = RHSValue.getConstant();
753 switch (ABE->getOpcode()) {
754 case MCBinaryExpr::Add: Result = LHS + RHS; break;
755 case MCBinaryExpr::And: Result = LHS & RHS; break;
756 case MCBinaryExpr::Div: Result = LHS / RHS; break;
757 case MCBinaryExpr::EQ: Result = LHS == RHS; break;
758 case MCBinaryExpr::GT: Result = LHS > RHS; break;
759 case MCBinaryExpr::GTE: Result = LHS >= RHS; break;
760 case MCBinaryExpr::LAnd: Result = LHS && RHS; break;
761 case MCBinaryExpr::LOr: Result = LHS || RHS; break;
762 case MCBinaryExpr::LT: Result = LHS < RHS; break;
763 case MCBinaryExpr::LTE: Result = LHS <= RHS; break;
764 case MCBinaryExpr::Mod: Result = LHS % RHS; break;
765 case MCBinaryExpr::Mul: Result = LHS * RHS; break;
766 case MCBinaryExpr::NE: Result = LHS != RHS; break;
767 case MCBinaryExpr::Or: Result = LHS | RHS; break;
768 case MCBinaryExpr::Shl: Result = LHS << RHS; break;
769 case MCBinaryExpr::Shr: Result = LHS >> RHS; break;
770 case MCBinaryExpr::Sub: Result = LHS - RHS; break;
771 case MCBinaryExpr::Xor: Result = LHS ^ RHS; break;
774 Res = MCValue::get(Result);
779 llvm_unreachable("Invalid assembly expression kind!");
782 const MCSection *MCExpr::FindAssociatedSection() const {
785 // We never look through target specific expressions.
786 return cast<MCTargetExpr>(this)->FindAssociatedSection();
789 return MCSymbol::AbsolutePseudoSection;
792 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(this);
793 const MCSymbol &Sym = SRE->getSymbol();
796 return &Sym.getSection();
802 return cast<MCUnaryExpr>(this)->getSubExpr()->FindAssociatedSection();
805 const MCBinaryExpr *BE = cast<MCBinaryExpr>(this);
806 const MCSection *LHS_S = BE->getLHS()->FindAssociatedSection();
807 const MCSection *RHS_S = BE->getRHS()->FindAssociatedSection();
809 // If either section is absolute, return the other.
810 if (LHS_S == MCSymbol::AbsolutePseudoSection)
812 if (RHS_S == MCSymbol::AbsolutePseudoSection)
815 // Otherwise, return the first non-null section.
816 return LHS_S ? LHS_S : RHS_S;
820 llvm_unreachable("Invalid assembly expression kind!");