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 #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"
23 using namespace llvm;
24
25 #define DEBUG_TYPE "mcexpr"
26
27 namespace {
28 namespace stats {
29 STATISTIC(MCExprEvaluate, "Number of MCExpr evaluations");
30 }
31 }
32
print(raw_ostream & OS) const33 void MCExpr::print(raw_ostream &OS) const {
34 switch (getKind()) {
35 case MCExpr::Target:
36 return cast<MCTargetExpr>(this)->PrintImpl(OS);
37 case MCExpr::Constant:
38 OS << cast<MCConstantExpr>(*this).getValue();
39 return;
40
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
45 // absolute names.
46 bool UseParens = Sym.getName()[0] == '$';
47 if (UseParens)
48 OS << '(' << Sym << ')';
49 else
50 OS << Sym;
51
52 if (SRE.getKind() != MCSymbolRefExpr::VK_None) {
53 if (SRE.getMCAsmInfo().useParensForSymbolVariant())
54 OS << '(' << MCSymbolRefExpr::getVariantKindName(SRE.getKind()) << ')';
55 else
56 OS << '@' << MCSymbolRefExpr::getVariantKindName(SRE.getKind());
57 }
58
59 return;
60 }
61
62 case MCExpr::Unary: {
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;
69 }
70 OS << *UE.getSubExpr();
71 return;
72 }
73
74 case MCExpr::Binary: {
75 const MCBinaryExpr &BE = cast<MCBinaryExpr>(*this);
76
77 // Only print parens around the LHS if it is non-trivial.
78 if (isa<MCConstantExpr>(BE.getLHS()) || isa<MCSymbolRefExpr>(BE.getLHS())) {
79 OS << *BE.getLHS();
80 } else {
81 OS << '(' << *BE.getLHS() << ')';
82 }
83
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();
90 return;
91 }
92 }
93
94 OS << '+';
95 break;
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;
113 }
114
115 // Only print parens around the LHS if it is non-trivial.
116 if (isa<MCConstantExpr>(BE.getRHS()) || isa<MCSymbolRefExpr>(BE.getRHS())) {
117 OS << *BE.getRHS();
118 } else {
119 OS << '(' << *BE.getRHS() << ')';
120 }
121 return;
122 }
123 }
124
125 llvm_unreachable("Invalid expression kind!");
126 }
127
128 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
dump() const129 void MCExpr::dump() const {
130 print(dbgs());
131 dbgs() << '\n';
132 }
133 #endif
134
135 /* *** */
136
Create(Opcode Opc,const MCExpr * LHS,const MCExpr * RHS,MCContext & Ctx)137 const MCBinaryExpr *MCBinaryExpr::Create(Opcode Opc, const MCExpr *LHS,
138 const MCExpr *RHS, MCContext &Ctx) {
139 return new (Ctx) MCBinaryExpr(Opc, LHS, RHS);
140 }
141
Create(Opcode Opc,const MCExpr * Expr,MCContext & Ctx)142 const MCUnaryExpr *MCUnaryExpr::Create(Opcode Opc, const MCExpr *Expr,
143 MCContext &Ctx) {
144 return new (Ctx) MCUnaryExpr(Opc, Expr);
145 }
146
Create(int64_t Value,MCContext & Ctx)147 const MCConstantExpr *MCConstantExpr::Create(int64_t Value, MCContext &Ctx) {
148 return new (Ctx) MCConstantExpr(Value);
149 }
150
151 /* *** */
152
Create(const MCSymbol * Sym,VariantKind Kind,MCContext & Ctx)153 const MCSymbolRefExpr *MCSymbolRefExpr::Create(const MCSymbol *Sym,
154 VariantKind Kind,
155 MCContext &Ctx) {
156 return new (Ctx) MCSymbolRefExpr(Sym, Kind, Ctx.getAsmInfo());
157 }
158
Create(StringRef Name,VariantKind Kind,MCContext & Ctx)159 const MCSymbolRefExpr *MCSymbolRefExpr::Create(StringRef Name, VariantKind Kind,
160 MCContext &Ctx) {
161 return Create(Ctx.GetOrCreateSymbol(Name), Kind, Ctx);
162 }
163
getVariantKindName(VariantKind Kind)164 StringRef MCSymbolRefExpr::getVariantKindName(VariantKind Kind) {
165 switch (Kind) {
166 case VK_Invalid: return "<<invalid>>";
167 case VK_None: return "<<none>>";
168
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";
277 }
278 llvm_unreachable("Invalid variant kind");
279 }
280
281 MCSymbolRefExpr::VariantKind
getVariantKindForName(StringRef Name)282 MCSymbolRefExpr::getVariantKindForName(StringRef Name) {
283 return StringSwitch<VariantKind>(Name)
284 .Case("GOT", VK_GOT)
285 .Case("got", VK_GOT)
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)
300 .Case("PLT", VK_PLT)
301 .Case("plt", VK_PLT)
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);
443 }
444
445 /* *** */
446
anchor()447 void MCTargetExpr::anchor() {}
448
449 /* *** */
450
EvaluateAsAbsolute(int64_t & Res) const451 bool MCExpr::EvaluateAsAbsolute(int64_t &Res) const {
452 return EvaluateAsAbsolute(Res, nullptr, nullptr, nullptr);
453 }
454
EvaluateAsAbsolute(int64_t & Res,const MCAsmLayout & Layout) const455 bool MCExpr::EvaluateAsAbsolute(int64_t &Res,
456 const MCAsmLayout &Layout) const {
457 return EvaluateAsAbsolute(Res, &Layout.getAssembler(), &Layout, nullptr);
458 }
459
EvaluateAsAbsolute(int64_t & Res,const MCAsmLayout & Layout,const SectionAddrMap & Addrs) const460 bool MCExpr::EvaluateAsAbsolute(int64_t &Res,
461 const MCAsmLayout &Layout,
462 const SectionAddrMap &Addrs) const {
463 return EvaluateAsAbsolute(Res, &Layout.getAssembler(), &Layout, &Addrs);
464 }
465
EvaluateAsAbsolute(int64_t & Res,const MCAssembler & Asm) const466 bool MCExpr::EvaluateAsAbsolute(int64_t &Res, const MCAssembler &Asm) const {
467 return EvaluateAsAbsolute(Res, &Asm, nullptr, nullptr);
468 }
469
EvaluateAsAbsolute(int64_t & Res,const MCAssembler * Asm,const MCAsmLayout * Layout,const SectionAddrMap * Addrs) const470 bool MCExpr::EvaluateAsAbsolute(int64_t &Res, const MCAssembler *Asm,
471 const MCAsmLayout *Layout,
472 const SectionAddrMap *Addrs) const {
473 MCValue Value;
474
475 // Fast path constants.
476 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(this)) {
477 Res = CE->getValue();
478 return true;
479 }
480
481 // FIXME: The use if InSet = Addrs is a hack. Setting InSet causes us
482 // absolutize differences across sections and that is what the MachO writer
483 // uses Addrs for.
484 bool IsRelocatable =
485 EvaluateAsRelocatableImpl(Value, Asm, Layout, Addrs, /*InSet*/ Addrs,
486 /*ForceVarExpansion*/ false);
487
488 // Record the current value.
489 Res = Value.getConstant();
490
491 return IsRelocatable && Value.isAbsolute();
492 }
493
494 /// \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)495 static void AttemptToFoldSymbolOffsetDifference(const MCAssembler *Asm,
496 const MCAsmLayout *Layout,
497 const SectionAddrMap *Addrs,
498 bool InSet,
499 const MCSymbolRefExpr *&A,
500 const MCSymbolRefExpr *&B,
501 int64_t &Addend) {
502 if (!A || !B)
503 return;
504
505 const MCSymbol &SA = A->getSymbol();
506 const MCSymbol &SB = B->getSymbol();
507
508 if (SA.isUndefined() || SB.isUndefined())
509 return;
510
511 if (!Asm->getWriter().IsSymbolRefDifferenceFullyResolved(*Asm, A, B, InSet))
512 return;
513
514 const MCSymbolData &AD = Asm->getSymbolData(SA);
515 const MCSymbolData &BD = Asm->getSymbolData(SB);
516
517 if (AD.getFragment() == BD.getFragment()) {
518 Addend += (AD.getOffset() - BD.getOffset());
519
520 // Pointers to Thumb symbols need to have their low-bit set to allow
521 // for interworking.
522 if (Asm->isThumbFunc(&SA))
523 Addend |= 1;
524
525 // Clear the symbol expr pointers to indicate we have folded these
526 // operands.
527 A = B = nullptr;
528 return;
529 }
530
531 if (!Layout)
532 return;
533
534 const MCSectionData &SecA = *AD.getFragment()->getParent();
535 const MCSectionData &SecB = *BD.getFragment()->getParent();
536
537 if ((&SecA != &SecB) && !Addrs)
538 return;
539
540 // Eagerly evaluate.
541 Addend += (Layout->getSymbolOffset(&Asm->getSymbolData(A->getSymbol())) -
542 Layout->getSymbolOffset(&Asm->getSymbolData(B->getSymbol())));
543 if (Addrs && (&SecA != &SecB))
544 Addend += (Addrs->lookup(&SecA) - Addrs->lookup(&SecB));
545
546 // Pointers to Thumb symbols need to have their low-bit set to allow
547 // for interworking.
548 if (Asm->isThumbFunc(&SA))
549 Addend |= 1;
550
551 // Clear the symbol expr pointers to indicate we have folded these
552 // operands.
553 A = B = nullptr;
554 }
555
556 /// \brief Evaluate the result of an add between (conceptually) two MCValues.
557 ///
558 /// This routine conceptually attempts to construct an MCValue:
559 /// Result = (Result_A - Result_B + Result_Cst)
560 /// from two MCValue's LHS and RHS where
561 /// Result = LHS + RHS
562 /// and
563 /// Result = (LHS_A - LHS_B + LHS_Cst) + (RHS_A - RHS_B + RHS_Cst).
564 ///
565 /// This routine attempts to aggresively fold the operands such that the result
566 /// is representable in an MCValue, but may not always succeed.
567 ///
568 /// \returns True on success, false if the result is not representable in an
569 /// MCValue.
570
571 /// NOTE: It is really important to have both the Asm and Layout arguments.
572 /// They might look redundant, but this function can be used before layout
573 /// is done (see the object streamer for example) and having the Asm argument
574 /// 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)575 static bool EvaluateSymbolicAdd(const MCAssembler *Asm,
576 const MCAsmLayout *Layout,
577 const SectionAddrMap *Addrs,
578 bool InSet,
579 const MCValue &LHS,const MCSymbolRefExpr *RHS_A,
580 const MCSymbolRefExpr *RHS_B, int64_t RHS_Cst,
581 MCValue &Res) {
582 // FIXME: This routine (and other evaluation parts) are *incredibly* sloppy
583 // about dealing with modifiers. This will ultimately bite us, one day.
584 const MCSymbolRefExpr *LHS_A = LHS.getSymA();
585 const MCSymbolRefExpr *LHS_B = LHS.getSymB();
586 int64_t LHS_Cst = LHS.getConstant();
587
588 // Fold the result constant immediately.
589 int64_t Result_Cst = LHS_Cst + RHS_Cst;
590
591 assert((!Layout || Asm) &&
592 "Must have an assembler object if layout is given!");
593
594 // If we have a layout, we can fold resolved differences.
595 if (Asm) {
596 // First, fold out any differences which are fully resolved. By
597 // reassociating terms in
598 // Result = (LHS_A - LHS_B + LHS_Cst) + (RHS_A - RHS_B + RHS_Cst).
599 // we have the four possible differences:
600 // (LHS_A - LHS_B),
601 // (LHS_A - RHS_B),
602 // (RHS_A - LHS_B),
603 // (RHS_A - RHS_B).
604 // Since we are attempting to be as aggressive as possible about folding, we
605 // attempt to evaluate each possible alternative.
606 AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, LHS_A, LHS_B,
607 Result_Cst);
608 AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, LHS_A, RHS_B,
609 Result_Cst);
610 AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, RHS_A, LHS_B,
611 Result_Cst);
612 AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, RHS_A, RHS_B,
613 Result_Cst);
614 }
615
616 // We can't represent the addition or subtraction of two symbols.
617 if ((LHS_A && RHS_A) || (LHS_B && RHS_B))
618 return false;
619
620 // At this point, we have at most one additive symbol and one subtractive
621 // symbol -- find them.
622 const MCSymbolRefExpr *A = LHS_A ? LHS_A : RHS_A;
623 const MCSymbolRefExpr *B = LHS_B ? LHS_B : RHS_B;
624
625 // If we have a negated symbol, then we must have also have a non-negated
626 // symbol in order to encode the expression.
627 if (B && !A)
628 return false;
629
630 Res = MCValue::get(A, B, Result_Cst);
631 return true;
632 }
633
EvaluateAsRelocatable(MCValue & Res,const MCAsmLayout * Layout) const634 bool MCExpr::EvaluateAsRelocatable(MCValue &Res,
635 const MCAsmLayout *Layout) const {
636 MCAssembler *Assembler = Layout ? &Layout->getAssembler() : nullptr;
637 return EvaluateAsRelocatableImpl(Res, Assembler, Layout, nullptr, false,
638 /*ForceVarExpansion*/ false);
639 }
640
EvaluateAsValue(MCValue & Res,const MCAsmLayout * Layout) const641 bool MCExpr::EvaluateAsValue(MCValue &Res, const MCAsmLayout *Layout) const {
642 MCAssembler *Assembler = Layout ? &Layout->getAssembler() : nullptr;
643 return EvaluateAsRelocatableImpl(Res, Assembler, Layout, nullptr, false,
644 /*ForceVarExpansion*/ true);
645 }
646
EvaluateAsRelocatableImpl(MCValue & Res,const MCAssembler * Asm,const MCAsmLayout * Layout,const SectionAddrMap * Addrs,bool InSet,bool ForceVarExpansion) const647 bool MCExpr::EvaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm,
648 const MCAsmLayout *Layout,
649 const SectionAddrMap *Addrs, bool InSet,
650 bool ForceVarExpansion) const {
651 ++stats::MCExprEvaluate;
652
653 switch (getKind()) {
654 case Target:
655 return cast<MCTargetExpr>(this)->EvaluateAsRelocatableImpl(Res, Layout);
656
657 case Constant:
658 Res = MCValue::get(cast<MCConstantExpr>(this)->getValue());
659 return true;
660
661 case SymbolRef: {
662 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(this);
663 const MCSymbol &Sym = SRE->getSymbol();
664 const MCAsmInfo &MCAsmInfo = SRE->getMCAsmInfo();
665
666 // Evaluate recursively if this is a variable.
667 if (Sym.isVariable() && SRE->getKind() == MCSymbolRefExpr::VK_None) {
668 if (Sym.getVariableValue()->EvaluateAsRelocatableImpl(
669 Res, Asm, Layout, Addrs, true, ForceVarExpansion)) {
670 const MCSymbolRefExpr *A = Res.getSymA();
671 const MCSymbolRefExpr *B = Res.getSymB();
672
673 if (MCAsmInfo.hasSubsectionsViaSymbols()) {
674 // FIXME: This is small hack. Given
675 // a = b + 4
676 // .long a
677 // the OS X assembler will completely drop the 4. We should probably
678 // include it in the relocation or produce an error if that is not
679 // possible.
680 if (!A && !B)
681 return true;
682 } else {
683 if (ForceVarExpansion)
684 return true;
685 bool IsSymbol = A && A->getSymbol().isDefined();
686 if (!IsSymbol)
687 return true;
688 }
689 }
690 }
691
692 Res = MCValue::get(SRE, nullptr, 0);
693 return true;
694 }
695
696 case Unary: {
697 const MCUnaryExpr *AUE = cast<MCUnaryExpr>(this);
698 MCValue Value;
699
700 if (!AUE->getSubExpr()->EvaluateAsRelocatableImpl(Value, Asm, Layout, Addrs,
701 InSet, ForceVarExpansion))
702 return false;
703
704 switch (AUE->getOpcode()) {
705 case MCUnaryExpr::LNot:
706 if (!Value.isAbsolute())
707 return false;
708 Res = MCValue::get(!Value.getConstant());
709 break;
710 case MCUnaryExpr::Minus:
711 /// -(a - b + const) ==> (b - a - const)
712 if (Value.getSymA() && !Value.getSymB())
713 return false;
714 Res = MCValue::get(Value.getSymB(), Value.getSymA(),
715 -Value.getConstant());
716 break;
717 case MCUnaryExpr::Not:
718 if (!Value.isAbsolute())
719 return false;
720 Res = MCValue::get(~Value.getConstant());
721 break;
722 case MCUnaryExpr::Plus:
723 Res = Value;
724 break;
725 }
726
727 return true;
728 }
729
730 case Binary: {
731 const MCBinaryExpr *ABE = cast<MCBinaryExpr>(this);
732 MCValue LHSValue, RHSValue;
733
734 if (!ABE->getLHS()->EvaluateAsRelocatableImpl(LHSValue, Asm, Layout, Addrs,
735 InSet, ForceVarExpansion) ||
736 !ABE->getRHS()->EvaluateAsRelocatableImpl(RHSValue, Asm, Layout, Addrs,
737 InSet, ForceVarExpansion))
738 return false;
739
740 // We only support a few operations on non-constant expressions, handle
741 // those first.
742 if (!LHSValue.isAbsolute() || !RHSValue.isAbsolute()) {
743 switch (ABE->getOpcode()) {
744 default:
745 return false;
746 case MCBinaryExpr::Sub:
747 // Negate RHS and add.
748 return EvaluateSymbolicAdd(Asm, Layout, Addrs, InSet, LHSValue,
749 RHSValue.getSymB(), RHSValue.getSymA(),
750 -RHSValue.getConstant(),
751 Res);
752
753 case MCBinaryExpr::Add:
754 return EvaluateSymbolicAdd(Asm, Layout, Addrs, InSet, LHSValue,
755 RHSValue.getSymA(), RHSValue.getSymB(),
756 RHSValue.getConstant(),
757 Res);
758 }
759 }
760
761 // FIXME: We need target hooks for the evaluation. It may be limited in
762 // width, and gas defines the result of comparisons and right shifts
763 // differently from Apple as.
764 int64_t LHS = LHSValue.getConstant(), RHS = RHSValue.getConstant();
765 int64_t Result = 0;
766 switch (ABE->getOpcode()) {
767 case MCBinaryExpr::Add: Result = LHS + RHS; break;
768 case MCBinaryExpr::And: Result = LHS & RHS; break;
769 case MCBinaryExpr::Div: Result = LHS / RHS; break;
770 case MCBinaryExpr::EQ: Result = LHS == RHS; break;
771 case MCBinaryExpr::GT: Result = LHS > RHS; break;
772 case MCBinaryExpr::GTE: Result = LHS >= RHS; break;
773 case MCBinaryExpr::LAnd: Result = LHS && RHS; break;
774 case MCBinaryExpr::LOr: Result = LHS || RHS; break;
775 case MCBinaryExpr::LT: Result = LHS < RHS; break;
776 case MCBinaryExpr::LTE: Result = LHS <= RHS; break;
777 case MCBinaryExpr::Mod: Result = LHS % RHS; break;
778 case MCBinaryExpr::Mul: Result = LHS * RHS; break;
779 case MCBinaryExpr::NE: Result = LHS != RHS; break;
780 case MCBinaryExpr::Or: Result = LHS | RHS; break;
781 case MCBinaryExpr::Shl: Result = LHS << RHS; break;
782 case MCBinaryExpr::Shr: Result = LHS >> RHS; break;
783 case MCBinaryExpr::Sub: Result = LHS - RHS; break;
784 case MCBinaryExpr::Xor: Result = LHS ^ RHS; break;
785 }
786
787 Res = MCValue::get(Result);
788 return true;
789 }
790 }
791
792 llvm_unreachable("Invalid assembly expression kind!");
793 }
794
FindAssociatedSection() const795 const MCSection *MCExpr::FindAssociatedSection() const {
796 switch (getKind()) {
797 case Target:
798 // We never look through target specific expressions.
799 return cast<MCTargetExpr>(this)->FindAssociatedSection();
800
801 case Constant:
802 return MCSymbol::AbsolutePseudoSection;
803
804 case SymbolRef: {
805 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(this);
806 const MCSymbol &Sym = SRE->getSymbol();
807
808 if (Sym.isDefined())
809 return &Sym.getSection();
810
811 return nullptr;
812 }
813
814 case Unary:
815 return cast<MCUnaryExpr>(this)->getSubExpr()->FindAssociatedSection();
816
817 case Binary: {
818 const MCBinaryExpr *BE = cast<MCBinaryExpr>(this);
819 const MCSection *LHS_S = BE->getLHS()->FindAssociatedSection();
820 const MCSection *RHS_S = BE->getRHS()->FindAssociatedSection();
821
822 // If either section is absolute, return the other.
823 if (LHS_S == MCSymbol::AbsolutePseudoSection)
824 return RHS_S;
825 if (RHS_S == MCSymbol::AbsolutePseudoSection)
826 return LHS_S;
827
828 // Otherwise, return the first non-null section.
829 return LHS_S ? LHS_S : RHS_S;
830 }
831 }
832
833 llvm_unreachable("Invalid assembly expression kind!");
834 }
835