1 //===- MCExpr.cpp - Assembly Level Expression Implementation --------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #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"
23 using namespace llvm;
24
25 namespace {
26 namespace stats {
27 STATISTIC(MCExprEvaluate, "Number of MCExpr evaluations");
28 }
29 }
30
print(raw_ostream & OS) const31 void MCExpr::print(raw_ostream &OS) const {
32 switch (getKind()) {
33 case MCExpr::Target:
34 return cast<MCTargetExpr>(this)->PrintImpl(OS);
35 case MCExpr::Constant:
36 OS << cast<MCConstantExpr>(*this).getValue();
37 return;
38
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
43 // absolute names.
44 bool UseParens = Sym.getName()[0] == '$';
45 if (UseParens)
46 OS << '(' << Sym << ')';
47 else
48 OS << Sym;
49
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());
63
64 return;
65 }
66
67 case MCExpr::Unary: {
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;
74 }
75 OS << *UE.getSubExpr();
76 return;
77 }
78
79 case MCExpr::Binary: {
80 const MCBinaryExpr &BE = cast<MCBinaryExpr>(*this);
81
82 // Only print parens around the LHS if it is non-trivial.
83 if (isa<MCConstantExpr>(BE.getLHS()) || isa<MCSymbolRefExpr>(BE.getLHS())) {
84 OS << *BE.getLHS();
85 } else {
86 OS << '(' << *BE.getLHS() << ')';
87 }
88
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();
95 return;
96 }
97 }
98
99 OS << '+';
100 break;
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;
118 }
119
120 // Only print parens around the LHS if it is non-trivial.
121 if (isa<MCConstantExpr>(BE.getRHS()) || isa<MCSymbolRefExpr>(BE.getRHS())) {
122 OS << *BE.getRHS();
123 } else {
124 OS << '(' << *BE.getRHS() << ')';
125 }
126 return;
127 }
128 }
129
130 llvm_unreachable("Invalid expression kind!");
131 }
132
133 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
dump() const134 void MCExpr::dump() const {
135 print(dbgs());
136 dbgs() << '\n';
137 }
138 #endif
139
140 /* *** */
141
Create(Opcode Opc,const MCExpr * LHS,const MCExpr * RHS,MCContext & Ctx)142 const MCBinaryExpr *MCBinaryExpr::Create(Opcode Opc, const MCExpr *LHS,
143 const MCExpr *RHS, MCContext &Ctx) {
144 return new (Ctx) MCBinaryExpr(Opc, LHS, RHS);
145 }
146
Create(Opcode Opc,const MCExpr * Expr,MCContext & Ctx)147 const MCUnaryExpr *MCUnaryExpr::Create(Opcode Opc, const MCExpr *Expr,
148 MCContext &Ctx) {
149 return new (Ctx) MCUnaryExpr(Opc, Expr);
150 }
151
Create(int64_t Value,MCContext & Ctx)152 const MCConstantExpr *MCConstantExpr::Create(int64_t Value, MCContext &Ctx) {
153 return new (Ctx) MCConstantExpr(Value);
154 }
155
156 /* *** */
157
Create(const MCSymbol * Sym,VariantKind Kind,MCContext & Ctx)158 const MCSymbolRefExpr *MCSymbolRefExpr::Create(const MCSymbol *Sym,
159 VariantKind Kind,
160 MCContext &Ctx) {
161 return new (Ctx) MCSymbolRefExpr(Sym, Kind);
162 }
163
Create(StringRef Name,VariantKind Kind,MCContext & Ctx)164 const MCSymbolRefExpr *MCSymbolRefExpr::Create(StringRef Name, VariantKind Kind,
165 MCContext &Ctx) {
166 return Create(Ctx.GetOrCreateSymbol(Name), Kind, Ctx);
167 }
168
getVariantKindName(VariantKind Kind)169 StringRef MCSymbolRefExpr::getVariantKindName(VariantKind Kind) {
170 switch (Kind) {
171 case VK_Invalid: return "<<invalid>>";
172 case VK_None: return "<<none>>";
173
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";
275 }
276 llvm_unreachable("Invalid variant kind");
277 }
278
279 MCSymbolRefExpr::VariantKind
getVariantKindForName(StringRef Name)280 MCSymbolRefExpr::getVariantKindForName(StringRef Name) {
281 return StringSwitch<VariantKind>(Name)
282 .Case("GOT", VK_GOT)
283 .Case("got", VK_GOT)
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)
296 .Case("PLT", VK_PLT)
297 .Case("plt", VK_PLT)
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);
413 }
414
415 /* *** */
416
anchor()417 void MCTargetExpr::anchor() {}
418
419 /* *** */
420
EvaluateAsAbsolute(int64_t & Res) const421 bool MCExpr::EvaluateAsAbsolute(int64_t &Res) const {
422 return EvaluateAsAbsolute(Res, 0, 0, 0);
423 }
424
EvaluateAsAbsolute(int64_t & Res,const MCAsmLayout & Layout) const425 bool MCExpr::EvaluateAsAbsolute(int64_t &Res,
426 const MCAsmLayout &Layout) const {
427 return EvaluateAsAbsolute(Res, &Layout.getAssembler(), &Layout, 0);
428 }
429
EvaluateAsAbsolute(int64_t & Res,const MCAsmLayout & Layout,const SectionAddrMap & Addrs) const430 bool MCExpr::EvaluateAsAbsolute(int64_t &Res,
431 const MCAsmLayout &Layout,
432 const SectionAddrMap &Addrs) const {
433 return EvaluateAsAbsolute(Res, &Layout.getAssembler(), &Layout, &Addrs);
434 }
435
EvaluateAsAbsolute(int64_t & Res,const MCAssembler & Asm) const436 bool MCExpr::EvaluateAsAbsolute(int64_t &Res, const MCAssembler &Asm) const {
437 return EvaluateAsAbsolute(Res, &Asm, 0, 0);
438 }
439
EvaluateAsAbsolute(int64_t & Res,const MCAssembler * Asm,const MCAsmLayout * Layout,const SectionAddrMap * Addrs) const440 bool MCExpr::EvaluateAsAbsolute(int64_t &Res, const MCAssembler *Asm,
441 const MCAsmLayout *Layout,
442 const SectionAddrMap *Addrs) const {
443 MCValue Value;
444
445 // Fast path constants.
446 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(this)) {
447 Res = CE->getValue();
448 return true;
449 }
450
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
453 // uses Addrs for.
454 bool IsRelocatable =
455 EvaluateAsRelocatableImpl(Value, Asm, Layout, Addrs, /*InSet*/ Addrs);
456
457 // Record the current value.
458 Res = Value.getConstant();
459
460 return IsRelocatable && Value.isAbsolute();
461 }
462
463 /// \brief Helper method for \see EvaluateSymbolAdd().
AttemptToFoldSymbolOffsetDifference(const MCAssembler * Asm,const MCAsmLayout * Layout,const SectionAddrMap * Addrs,bool InSet,const MCSymbolRefExpr * & A,const MCSymbolRefExpr * & B,int64_t & Addend)464 static void AttemptToFoldSymbolOffsetDifference(const MCAssembler *Asm,
465 const MCAsmLayout *Layout,
466 const SectionAddrMap *Addrs,
467 bool InSet,
468 const MCSymbolRefExpr *&A,
469 const MCSymbolRefExpr *&B,
470 int64_t &Addend) {
471 if (!A || !B)
472 return;
473
474 const MCSymbol &SA = A->getSymbol();
475 const MCSymbol &SB = B->getSymbol();
476
477 if (SA.isUndefined() || SB.isUndefined())
478 return;
479
480 if (!Asm->getWriter().IsSymbolRefDifferenceFullyResolved(*Asm, A, B, InSet))
481 return;
482
483 MCSymbolData &AD = Asm->getSymbolData(SA);
484 MCSymbolData &BD = Asm->getSymbolData(SB);
485
486 if (AD.getFragment() == BD.getFragment()) {
487 Addend += (AD.getOffset() - BD.getOffset());
488
489 // Pointers to Thumb symbols need to have their low-bit set to allow
490 // for interworking.
491 if (Asm->isThumbFunc(&SA))
492 Addend |= 1;
493
494 // Clear the symbol expr pointers to indicate we have folded these
495 // operands.
496 A = B = 0;
497 return;
498 }
499
500 if (!Layout)
501 return;
502
503 const MCSectionData &SecA = *AD.getFragment()->getParent();
504 const MCSectionData &SecB = *BD.getFragment()->getParent();
505
506 if ((&SecA != &SecB) && !Addrs)
507 return;
508
509 // Eagerly evaluate.
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));
514
515 // Pointers to Thumb symbols need to have their low-bit set to allow
516 // for interworking.
517 if (Asm->isThumbFunc(&SA))
518 Addend |= 1;
519
520 // Clear the symbol expr pointers to indicate we have folded these
521 // operands.
522 A = B = 0;
523 }
524
525 /// \brief Evaluate the result of an add between (conceptually) two MCValues.
526 ///
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
531 /// and
532 /// Result = (LHS_A - LHS_B + LHS_Cst) + (RHS_A - RHS_B + RHS_Cst).
533 ///
534 /// This routine attempts to aggresively fold the operands such that the result
535 /// is representable in an MCValue, but may not always succeed.
536 ///
537 /// \returns True on success, false if the result is not representable in an
538 /// MCValue.
539
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.
EvaluateSymbolicAdd(const MCAssembler * Asm,const MCAsmLayout * Layout,const SectionAddrMap * Addrs,bool InSet,const MCValue & LHS,const MCSymbolRefExpr * RHS_A,const MCSymbolRefExpr * RHS_B,int64_t RHS_Cst,MCValue & Res)544 static bool EvaluateSymbolicAdd(const MCAssembler *Asm,
545 const MCAsmLayout *Layout,
546 const SectionAddrMap *Addrs,
547 bool InSet,
548 const MCValue &LHS,const MCSymbolRefExpr *RHS_A,
549 const MCSymbolRefExpr *RHS_B, int64_t RHS_Cst,
550 MCValue &Res) {
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();
556
557 // Fold the result constant immediately.
558 int64_t Result_Cst = LHS_Cst + RHS_Cst;
559
560 assert((!Layout || Asm) &&
561 "Must have an assembler object if layout is given!");
562
563 // If we have a layout, we can fold resolved differences.
564 if (Asm) {
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:
569 // (LHS_A - LHS_B),
570 // (LHS_A - RHS_B),
571 // (RHS_A - LHS_B),
572 // (RHS_A - RHS_B).
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,
576 Result_Cst);
577 AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, LHS_A, RHS_B,
578 Result_Cst);
579 AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, RHS_A, LHS_B,
580 Result_Cst);
581 AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, RHS_A, RHS_B,
582 Result_Cst);
583 }
584
585 // We can't represent the addition or subtraction of two symbols.
586 if ((LHS_A && RHS_A) || (LHS_B && RHS_B))
587 return false;
588
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;
593
594 // If we have a negated symbol, then we must have also have a non-negated
595 // symbol in order to encode the expression.
596 if (B && !A)
597 return false;
598
599 Res = MCValue::get(A, B, Result_Cst);
600 return true;
601 }
602
EvaluateAsRelocatable(MCValue & Res,const MCAsmLayout & Layout) const603 bool MCExpr::EvaluateAsRelocatable(MCValue &Res,
604 const MCAsmLayout &Layout) const {
605 return EvaluateAsRelocatableImpl(Res, &Layout.getAssembler(), &Layout,
606 0, false);
607 }
608
EvaluateAsRelocatableImpl(MCValue & Res,const MCAssembler * Asm,const MCAsmLayout * Layout,const SectionAddrMap * Addrs,bool InSet) const609 bool MCExpr::EvaluateAsRelocatableImpl(MCValue &Res,
610 const MCAssembler *Asm,
611 const MCAsmLayout *Layout,
612 const SectionAddrMap *Addrs,
613 bool InSet) const {
614 ++stats::MCExprEvaluate;
615
616 switch (getKind()) {
617 case Target:
618 return cast<MCTargetExpr>(this)->EvaluateAsRelocatableImpl(Res, Layout);
619
620 case Constant:
621 Res = MCValue::get(cast<MCConstantExpr>(this)->getValue());
622 return true;
623
624 case SymbolRef: {
625 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(this);
626 const MCSymbol &Sym = SRE->getSymbol();
627
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,
631 Layout,
632 Addrs,
633 true);
634 // If we failed to simplify this to a constant, let the target
635 // handle it.
636 if (Ret && !Res.getSymA() && !Res.getSymB())
637 return true;
638 }
639
640 Res = MCValue::get(SRE, 0, 0);
641 return true;
642 }
643
644 case Unary: {
645 const MCUnaryExpr *AUE = cast<MCUnaryExpr>(this);
646 MCValue Value;
647
648 if (!AUE->getSubExpr()->EvaluateAsRelocatableImpl(Value, Asm, Layout,
649 Addrs, InSet))
650 return false;
651
652 switch (AUE->getOpcode()) {
653 case MCUnaryExpr::LNot:
654 if (!Value.isAbsolute())
655 return false;
656 Res = MCValue::get(!Value.getConstant());
657 break;
658 case MCUnaryExpr::Minus:
659 /// -(a - b + const) ==> (b - a - const)
660 if (Value.getSymA() && !Value.getSymB())
661 return false;
662 Res = MCValue::get(Value.getSymB(), Value.getSymA(),
663 -Value.getConstant());
664 break;
665 case MCUnaryExpr::Not:
666 if (!Value.isAbsolute())
667 return false;
668 Res = MCValue::get(~Value.getConstant());
669 break;
670 case MCUnaryExpr::Plus:
671 Res = Value;
672 break;
673 }
674
675 return true;
676 }
677
678 case Binary: {
679 const MCBinaryExpr *ABE = cast<MCBinaryExpr>(this);
680 MCValue LHSValue, RHSValue;
681
682 if (!ABE->getLHS()->EvaluateAsRelocatableImpl(LHSValue, Asm, Layout,
683 Addrs, InSet) ||
684 !ABE->getRHS()->EvaluateAsRelocatableImpl(RHSValue, Asm, Layout,
685 Addrs, InSet))
686 return false;
687
688 // We only support a few operations on non-constant expressions, handle
689 // those first.
690 if (!LHSValue.isAbsolute() || !RHSValue.isAbsolute()) {
691 switch (ABE->getOpcode()) {
692 default:
693 return false;
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(),
699 Res);
700
701 case MCBinaryExpr::Add:
702 return EvaluateSymbolicAdd(Asm, Layout, Addrs, InSet, LHSValue,
703 RHSValue.getSymA(), RHSValue.getSymB(),
704 RHSValue.getConstant(),
705 Res);
706 }
707 }
708
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();
713 int64_t Result = 0;
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;
733 }
734
735 Res = MCValue::get(Result);
736 return true;
737 }
738 }
739
740 llvm_unreachable("Invalid assembly expression kind!");
741 }
742
FindAssociatedSection() const743 const MCSection *MCExpr::FindAssociatedSection() const {
744 switch (getKind()) {
745 case Target:
746 // We never look through target specific expressions.
747 return cast<MCTargetExpr>(this)->FindAssociatedSection();
748
749 case Constant:
750 return MCSymbol::AbsolutePseudoSection;
751
752 case SymbolRef: {
753 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(this);
754 const MCSymbol &Sym = SRE->getSymbol();
755
756 if (Sym.isDefined())
757 return &Sym.getSection();
758
759 return 0;
760 }
761
762 case Unary:
763 return cast<MCUnaryExpr>(this)->getSubExpr()->FindAssociatedSection();
764
765 case Binary: {
766 const MCBinaryExpr *BE = cast<MCBinaryExpr>(this);
767 const MCSection *LHS_S = BE->getLHS()->FindAssociatedSection();
768 const MCSection *RHS_S = BE->getRHS()->FindAssociatedSection();
769
770 // If either section is absolute, return the other.
771 if (LHS_S == MCSymbol::AbsolutePseudoSection)
772 return RHS_S;
773 if (RHS_S == MCSymbol::AbsolutePseudoSection)
774 return LHS_S;
775
776 // Otherwise, return the first non-null section.
777 return LHS_S ? LHS_S : RHS_S;
778 }
779 }
780
781 llvm_unreachable("Invalid assembly expression kind!");
782 }
783