blob: fcbbe650d26f3f150da7bc6eacc927eb1fad3b8c [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===- MCExpr.h - Assembly Level Expressions --------------------*- C++ -*-===//
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#ifndef LLVM_MC_MCEXPR_H
11#define LLVM_MC_MCEXPR_H
12
13#include "llvm/ADT/DenseMap.h"
14#include "llvm/Support/SMLoc.h"
15#include <cstdint>
16
17namespace llvm {
18
19class MCAsmInfo;
20class MCAsmLayout;
21class MCAssembler;
22class MCContext;
23class MCFixup;
24class MCFragment;
25class MCSection;
26class MCStreamer;
27class MCSymbol;
28class MCValue;
29class raw_ostream;
30class StringRef;
31
32using SectionAddrMap = DenseMap<const MCSection *, uint64_t>;
33
34/// \brief Base class for the full range of assembler expressions which are
35/// needed for parsing.
36class MCExpr {
37public:
38 enum ExprKind {
39 Binary, ///< Binary expressions.
40 Constant, ///< Constant expressions.
41 SymbolRef, ///< References to labels and assigned expressions.
42 Unary, ///< Unary expressions.
43 Target ///< Target specific expression.
44 };
45
46private:
47 ExprKind Kind;
48 SMLoc Loc;
49
50 bool evaluateAsAbsolute(int64_t &Res, const MCAssembler *Asm,
51 const MCAsmLayout *Layout,
52 const SectionAddrMap *Addrs) const;
53
54 bool evaluateAsAbsolute(int64_t &Res, const MCAssembler *Asm,
55 const MCAsmLayout *Layout,
56 const SectionAddrMap *Addrs, bool InSet) const;
57
58protected:
59 explicit MCExpr(ExprKind Kind, SMLoc Loc) : Kind(Kind), Loc(Loc) {}
60
61 bool evaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm,
62 const MCAsmLayout *Layout,
63 const MCFixup *Fixup,
64 const SectionAddrMap *Addrs, bool InSet) const;
65
66public:
67 MCExpr(const MCExpr &) = delete;
68 MCExpr &operator=(const MCExpr &) = delete;
69
70 /// \name Accessors
71 /// @{
72
73 ExprKind getKind() const { return Kind; }
74 SMLoc getLoc() const { return Loc; }
75
76 /// @}
77 /// \name Utility Methods
78 /// @{
79
80 void print(raw_ostream &OS, const MCAsmInfo *MAI,
81 bool InParens = false) const;
82 void dump() const;
83
84 /// @}
85 /// \name Expression Evaluation
86 /// @{
87
88 /// \brief Try to evaluate the expression to an absolute value.
89 ///
90 /// \param Res - The absolute value, if evaluation succeeds.
91 /// \param Layout - The assembler layout object to use for evaluating symbol
92 /// values. If not given, then only non-symbolic expressions will be
93 /// evaluated.
94 /// \return - True on success.
95 bool evaluateAsAbsolute(int64_t &Res, const MCAsmLayout &Layout,
96 const SectionAddrMap &Addrs) const;
97 bool evaluateAsAbsolute(int64_t &Res) const;
98 bool evaluateAsAbsolute(int64_t &Res, const MCAssembler &Asm) const;
99 bool evaluateAsAbsolute(int64_t &Res, const MCAsmLayout &Layout) const;
100
101 bool evaluateKnownAbsolute(int64_t &Res, const MCAsmLayout &Layout) const;
102
103 /// \brief Try to evaluate the expression to a relocatable value, i.e. an
104 /// expression of the fixed form (a - b + constant).
105 ///
106 /// \param Res - The relocatable value, if evaluation succeeds.
107 /// \param Layout - The assembler layout object to use for evaluating values.
108 /// \param Fixup - The Fixup object if available.
109 /// \return - True on success.
110 bool evaluateAsRelocatable(MCValue &Res, const MCAsmLayout *Layout,
111 const MCFixup *Fixup) const;
112
113 /// \brief Try to evaluate the expression to the form (a - b + constant) where
114 /// neither a nor b are variables.
115 ///
116 /// This is a more aggressive variant of evaluateAsRelocatable. The intended
117 /// use is for when relocations are not available, like the .size directive.
118 bool evaluateAsValue(MCValue &Res, const MCAsmLayout &Layout) const;
119
120 /// \brief Find the "associated section" for this expression, which is
121 /// currently defined as the absolute section for constants, or
122 /// otherwise the section associated with the first defined symbol in the
123 /// expression.
124 MCFragment *findAssociatedFragment() const;
125
126 /// @}
127};
128
129inline raw_ostream &operator<<(raw_ostream &OS, const MCExpr &E) {
130 E.print(OS, nullptr);
131 return OS;
132}
133
134//// \brief Represent a constant integer expression.
135class MCConstantExpr : public MCExpr {
136 int64_t Value;
137
138 explicit MCConstantExpr(int64_t Value)
139 : MCExpr(MCExpr::Constant, SMLoc()), Value(Value) {}
140
141public:
142 /// \name Construction
143 /// @{
144
145 static const MCConstantExpr *create(int64_t Value, MCContext &Ctx);
146
147 /// @}
148 /// \name Accessors
149 /// @{
150
151 int64_t getValue() const { return Value; }
152
153 /// @}
154
155 static bool classof(const MCExpr *E) {
156 return E->getKind() == MCExpr::Constant;
157 }
158};
159
160/// \brief Represent a reference to a symbol from inside an expression.
161///
162/// A symbol reference in an expression may be a use of a label, a use of an
163/// assembler variable (defined constant), or constitute an implicit definition
164/// of the symbol as external.
165class MCSymbolRefExpr : public MCExpr {
166public:
167 enum VariantKind : uint16_t {
168 VK_None,
169 VK_Invalid,
170
171 VK_GOT,
172 VK_GOTOFF,
173 VK_GOTREL,
174 VK_GOTPCREL,
175 VK_GOTTPOFF,
176 VK_INDNTPOFF,
177 VK_NTPOFF,
178 VK_GOTNTPOFF,
179 VK_PLT,
180 VK_TLSGD,
181 VK_TLSLD,
182 VK_TLSLDM,
183 VK_TPOFF,
184 VK_DTPOFF,
185 VK_TLSCALL, // symbol(tlscall)
186 VK_TLSDESC, // symbol(tlsdesc)
187 VK_TLVP, // Mach-O thread local variable relocations
188 VK_TLVPPAGE,
189 VK_TLVPPAGEOFF,
190 VK_PAGE,
191 VK_PAGEOFF,
192 VK_GOTPAGE,
193 VK_GOTPAGEOFF,
194 VK_SECREL,
195 VK_SIZE, // symbol@SIZE
196 VK_WEAKREF, // The link between the symbols in .weakref foo, bar
197
198 VK_X86_ABS8,
199
200 VK_ARM_NONE,
201 VK_ARM_GOT_PREL,
202 VK_ARM_TARGET1,
203 VK_ARM_TARGET2,
204 VK_ARM_PREL31,
205 VK_ARM_SBREL, // symbol(sbrel)
206 VK_ARM_TLSLDO, // symbol(tlsldo)
207 VK_ARM_TLSDESCSEQ,
208
209 VK_AVR_NONE,
210 VK_AVR_LO8,
211 VK_AVR_HI8,
212 VK_AVR_HLO8,
213 VK_AVR_DIFF8,
214 VK_AVR_DIFF16,
215 VK_AVR_DIFF32,
216
217 VK_PPC_LO, // symbol@l
218 VK_PPC_HI, // symbol@h
219 VK_PPC_HA, // symbol@ha
220 VK_PPC_HIGHER, // symbol@higher
221 VK_PPC_HIGHERA, // symbol@highera
222 VK_PPC_HIGHEST, // symbol@highest
223 VK_PPC_HIGHESTA, // symbol@highesta
224 VK_PPC_GOT_LO, // symbol@got@l
225 VK_PPC_GOT_HI, // symbol@got@h
226 VK_PPC_GOT_HA, // symbol@got@ha
227 VK_PPC_TOCBASE, // symbol@tocbase
228 VK_PPC_TOC, // symbol@toc
229 VK_PPC_TOC_LO, // symbol@toc@l
230 VK_PPC_TOC_HI, // symbol@toc@h
231 VK_PPC_TOC_HA, // symbol@toc@ha
232 VK_PPC_DTPMOD, // symbol@dtpmod
233 VK_PPC_TPREL_LO, // symbol@tprel@l
234 VK_PPC_TPREL_HI, // symbol@tprel@h
235 VK_PPC_TPREL_HA, // symbol@tprel@ha
236 VK_PPC_TPREL_HIGHER, // symbol@tprel@higher
237 VK_PPC_TPREL_HIGHERA, // symbol@tprel@highera
238 VK_PPC_TPREL_HIGHEST, // symbol@tprel@highest
239 VK_PPC_TPREL_HIGHESTA, // symbol@tprel@highesta
240 VK_PPC_DTPREL_LO, // symbol@dtprel@l
241 VK_PPC_DTPREL_HI, // symbol@dtprel@h
242 VK_PPC_DTPREL_HA, // symbol@dtprel@ha
243 VK_PPC_DTPREL_HIGHER, // symbol@dtprel@higher
244 VK_PPC_DTPREL_HIGHERA, // symbol@dtprel@highera
245 VK_PPC_DTPREL_HIGHEST, // symbol@dtprel@highest
246 VK_PPC_DTPREL_HIGHESTA,// symbol@dtprel@highesta
247 VK_PPC_GOT_TPREL, // symbol@got@tprel
248 VK_PPC_GOT_TPREL_LO, // symbol@got@tprel@l
249 VK_PPC_GOT_TPREL_HI, // symbol@got@tprel@h
250 VK_PPC_GOT_TPREL_HA, // symbol@got@tprel@ha
251 VK_PPC_GOT_DTPREL, // symbol@got@dtprel
252 VK_PPC_GOT_DTPREL_LO, // symbol@got@dtprel@l
253 VK_PPC_GOT_DTPREL_HI, // symbol@got@dtprel@h
254 VK_PPC_GOT_DTPREL_HA, // symbol@got@dtprel@ha
255 VK_PPC_TLS, // symbol@tls
256 VK_PPC_GOT_TLSGD, // symbol@got@tlsgd
257 VK_PPC_GOT_TLSGD_LO, // symbol@got@tlsgd@l
258 VK_PPC_GOT_TLSGD_HI, // symbol@got@tlsgd@h
259 VK_PPC_GOT_TLSGD_HA, // symbol@got@tlsgd@ha
260 VK_PPC_TLSGD, // symbol@tlsgd
261 VK_PPC_GOT_TLSLD, // symbol@got@tlsld
262 VK_PPC_GOT_TLSLD_LO, // symbol@got@tlsld@l
263 VK_PPC_GOT_TLSLD_HI, // symbol@got@tlsld@h
264 VK_PPC_GOT_TLSLD_HA, // symbol@got@tlsld@ha
265 VK_PPC_TLSLD, // symbol@tlsld
266 VK_PPC_LOCAL, // symbol@local
267
268 VK_COFF_IMGREL32, // symbol@imgrel (image-relative)
269
270 VK_Hexagon_PCREL,
271 VK_Hexagon_LO16,
272 VK_Hexagon_HI16,
273 VK_Hexagon_GPREL,
274 VK_Hexagon_GD_GOT,
275 VK_Hexagon_LD_GOT,
276 VK_Hexagon_GD_PLT,
277 VK_Hexagon_LD_PLT,
278 VK_Hexagon_IE,
279 VK_Hexagon_IE_GOT,
280
281 VK_WebAssembly_FUNCTION, // Function table index, rather than virtual addr
282 VK_WebAssembly_TYPEINDEX,// Type table index
283
284 VK_AMDGPU_GOTPCREL32_LO, // symbol@gotpcrel32@lo
285 VK_AMDGPU_GOTPCREL32_HI, // symbol@gotpcrel32@hi
286 VK_AMDGPU_REL32_LO, // symbol@rel32@lo
287 VK_AMDGPU_REL32_HI, // symbol@rel32@hi
288
289 VK_TPREL,
290 VK_DTPREL
291 };
292
293private:
294 /// The symbol reference modifier.
295 const VariantKind Kind;
296
297 /// Specifies how the variant kind should be printed.
298 const unsigned UseParensForSymbolVariant : 1;
299
300 // FIXME: Remove this bit.
301 const unsigned HasSubsectionsViaSymbols : 1;
302
303 /// The symbol being referenced.
304 const MCSymbol *Symbol;
305
306 explicit MCSymbolRefExpr(const MCSymbol *Symbol, VariantKind Kind,
307 const MCAsmInfo *MAI, SMLoc Loc = SMLoc());
308
309public:
310 /// \name Construction
311 /// @{
312
313 static const MCSymbolRefExpr *create(const MCSymbol *Symbol, MCContext &Ctx) {
314 return MCSymbolRefExpr::create(Symbol, VK_None, Ctx);
315 }
316
317 static const MCSymbolRefExpr *create(const MCSymbol *Symbol, VariantKind Kind,
318 MCContext &Ctx, SMLoc Loc = SMLoc());
319 static const MCSymbolRefExpr *create(StringRef Name, VariantKind Kind,
320 MCContext &Ctx);
321
322 /// @}
323 /// \name Accessors
324 /// @{
325
326 const MCSymbol &getSymbol() const { return *Symbol; }
327
328 VariantKind getKind() const { return Kind; }
329
330 void printVariantKind(raw_ostream &OS) const;
331
332 bool hasSubsectionsViaSymbols() const { return HasSubsectionsViaSymbols; }
333
334 /// @}
335 /// \name Static Utility Functions
336 /// @{
337
338 static StringRef getVariantKindName(VariantKind Kind);
339
340 static VariantKind getVariantKindForName(StringRef Name);
341
342 /// @}
343
344 static bool classof(const MCExpr *E) {
345 return E->getKind() == MCExpr::SymbolRef;
346 }
347};
348
349/// \brief Unary assembler expressions.
350class MCUnaryExpr : public MCExpr {
351public:
352 enum Opcode {
353 LNot, ///< Logical negation.
354 Minus, ///< Unary minus.
355 Not, ///< Bitwise negation.
356 Plus ///< Unary plus.
357 };
358
359private:
360 Opcode Op;
361 const MCExpr *Expr;
362
363 MCUnaryExpr(Opcode Op, const MCExpr *Expr, SMLoc Loc)
364 : MCExpr(MCExpr::Unary, Loc), Op(Op), Expr(Expr) {}
365
366public:
367 /// \name Construction
368 /// @{
369
370 static const MCUnaryExpr *create(Opcode Op, const MCExpr *Expr,
371 MCContext &Ctx, SMLoc Loc = SMLoc());
372
373 static const MCUnaryExpr *createLNot(const MCExpr *Expr, MCContext &Ctx, SMLoc Loc = SMLoc()) {
374 return create(LNot, Expr, Ctx, Loc);
375 }
376
377 static const MCUnaryExpr *createMinus(const MCExpr *Expr, MCContext &Ctx, SMLoc Loc = SMLoc()) {
378 return create(Minus, Expr, Ctx, Loc);
379 }
380
381 static const MCUnaryExpr *createNot(const MCExpr *Expr, MCContext &Ctx, SMLoc Loc = SMLoc()) {
382 return create(Not, Expr, Ctx, Loc);
383 }
384
385 static const MCUnaryExpr *createPlus(const MCExpr *Expr, MCContext &Ctx, SMLoc Loc = SMLoc()) {
386 return create(Plus, Expr, Ctx, Loc);
387 }
388
389 /// @}
390 /// \name Accessors
391 /// @{
392
393 /// \brief Get the kind of this unary expression.
394 Opcode getOpcode() const { return Op; }
395
396 /// \brief Get the child of this unary expression.
397 const MCExpr *getSubExpr() const { return Expr; }
398
399 /// @}
400
401 static bool classof(const MCExpr *E) {
402 return E->getKind() == MCExpr::Unary;
403 }
404};
405
406/// \brief Binary assembler expressions.
407class MCBinaryExpr : public MCExpr {
408public:
409 enum Opcode {
410 Add, ///< Addition.
411 And, ///< Bitwise and.
412 Div, ///< Signed division.
413 EQ, ///< Equality comparison.
414 GT, ///< Signed greater than comparison (result is either 0 or some
415 ///< target-specific non-zero value)
416 GTE, ///< Signed greater than or equal comparison (result is either 0 or
417 ///< some target-specific non-zero value).
418 LAnd, ///< Logical and.
419 LOr, ///< Logical or.
420 LT, ///< Signed less than comparison (result is either 0 or
421 ///< some target-specific non-zero value).
422 LTE, ///< Signed less than or equal comparison (result is either 0 or
423 ///< some target-specific non-zero value).
424 Mod, ///< Signed remainder.
425 Mul, ///< Multiplication.
426 NE, ///< Inequality comparison.
427 Or, ///< Bitwise or.
428 Shl, ///< Shift left.
429 AShr, ///< Arithmetic shift right.
430 LShr, ///< Logical shift right.
431 Sub, ///< Subtraction.
432 Xor ///< Bitwise exclusive or.
433 };
434
435private:
436 Opcode Op;
437 const MCExpr *LHS, *RHS;
438
439 MCBinaryExpr(Opcode Op, const MCExpr *LHS, const MCExpr *RHS,
440 SMLoc Loc = SMLoc())
441 : MCExpr(MCExpr::Binary, Loc), Op(Op), LHS(LHS), RHS(RHS) {}
442
443public:
444 /// \name Construction
445 /// @{
446
447 static const MCBinaryExpr *create(Opcode Op, const MCExpr *LHS,
448 const MCExpr *RHS, MCContext &Ctx,
449 SMLoc Loc = SMLoc());
450
451 static const MCBinaryExpr *createAdd(const MCExpr *LHS, const MCExpr *RHS,
452 MCContext &Ctx) {
453 return create(Add, LHS, RHS, Ctx);
454 }
455
456 static const MCBinaryExpr *createAnd(const MCExpr *LHS, const MCExpr *RHS,
457 MCContext &Ctx) {
458 return create(And, LHS, RHS, Ctx);
459 }
460
461 static const MCBinaryExpr *createDiv(const MCExpr *LHS, const MCExpr *RHS,
462 MCContext &Ctx) {
463 return create(Div, LHS, RHS, Ctx);
464 }
465
466 static const MCBinaryExpr *createEQ(const MCExpr *LHS, const MCExpr *RHS,
467 MCContext &Ctx) {
468 return create(EQ, LHS, RHS, Ctx);
469 }
470
471 static const MCBinaryExpr *createGT(const MCExpr *LHS, const MCExpr *RHS,
472 MCContext &Ctx) {
473 return create(GT, LHS, RHS, Ctx);
474 }
475
476 static const MCBinaryExpr *createGTE(const MCExpr *LHS, const MCExpr *RHS,
477 MCContext &Ctx) {
478 return create(GTE, LHS, RHS, Ctx);
479 }
480
481 static const MCBinaryExpr *createLAnd(const MCExpr *LHS, const MCExpr *RHS,
482 MCContext &Ctx) {
483 return create(LAnd, LHS, RHS, Ctx);
484 }
485
486 static const MCBinaryExpr *createLOr(const MCExpr *LHS, const MCExpr *RHS,
487 MCContext &Ctx) {
488 return create(LOr, LHS, RHS, Ctx);
489 }
490
491 static const MCBinaryExpr *createLT(const MCExpr *LHS, const MCExpr *RHS,
492 MCContext &Ctx) {
493 return create(LT, LHS, RHS, Ctx);
494 }
495
496 static const MCBinaryExpr *createLTE(const MCExpr *LHS, const MCExpr *RHS,
497 MCContext &Ctx) {
498 return create(LTE, LHS, RHS, Ctx);
499 }
500
501 static const MCBinaryExpr *createMod(const MCExpr *LHS, const MCExpr *RHS,
502 MCContext &Ctx) {
503 return create(Mod, LHS, RHS, Ctx);
504 }
505
506 static const MCBinaryExpr *createMul(const MCExpr *LHS, const MCExpr *RHS,
507 MCContext &Ctx) {
508 return create(Mul, LHS, RHS, Ctx);
509 }
510
511 static const MCBinaryExpr *createNE(const MCExpr *LHS, const MCExpr *RHS,
512 MCContext &Ctx) {
513 return create(NE, LHS, RHS, Ctx);
514 }
515
516 static const MCBinaryExpr *createOr(const MCExpr *LHS, const MCExpr *RHS,
517 MCContext &Ctx) {
518 return create(Or, LHS, RHS, Ctx);
519 }
520
521 static const MCBinaryExpr *createShl(const MCExpr *LHS, const MCExpr *RHS,
522 MCContext &Ctx) {
523 return create(Shl, LHS, RHS, Ctx);
524 }
525
526 static const MCBinaryExpr *createAShr(const MCExpr *LHS, const MCExpr *RHS,
527 MCContext &Ctx) {
528 return create(AShr, LHS, RHS, Ctx);
529 }
530
531 static const MCBinaryExpr *createLShr(const MCExpr *LHS, const MCExpr *RHS,
532 MCContext &Ctx) {
533 return create(LShr, LHS, RHS, Ctx);
534 }
535
536 static const MCBinaryExpr *createSub(const MCExpr *LHS, const MCExpr *RHS,
537 MCContext &Ctx) {
538 return create(Sub, LHS, RHS, Ctx);
539 }
540
541 static const MCBinaryExpr *createXor(const MCExpr *LHS, const MCExpr *RHS,
542 MCContext &Ctx) {
543 return create(Xor, LHS, RHS, Ctx);
544 }
545
546 /// @}
547 /// \name Accessors
548 /// @{
549
550 /// \brief Get the kind of this binary expression.
551 Opcode getOpcode() const { return Op; }
552
553 /// \brief Get the left-hand side expression of the binary operator.
554 const MCExpr *getLHS() const { return LHS; }
555
556 /// \brief Get the right-hand side expression of the binary operator.
557 const MCExpr *getRHS() const { return RHS; }
558
559 /// @}
560
561 static bool classof(const MCExpr *E) {
562 return E->getKind() == MCExpr::Binary;
563 }
564};
565
566/// \brief This is an extension point for target-specific MCExpr subclasses to
567/// implement.
568///
569/// NOTE: All subclasses are required to have trivial destructors because
570/// MCExprs are bump pointer allocated and not destructed.
571class MCTargetExpr : public MCExpr {
572 virtual void anchor();
573
574protected:
575 MCTargetExpr() : MCExpr(Target, SMLoc()) {}
576 virtual ~MCTargetExpr() = default;
577
578public:
579 virtual void printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const = 0;
580 virtual bool evaluateAsRelocatableImpl(MCValue &Res,
581 const MCAsmLayout *Layout,
582 const MCFixup *Fixup) const = 0;
583 virtual void visitUsedExpr(MCStreamer& Streamer) const = 0;
584 virtual MCFragment *findAssociatedFragment() const = 0;
585
586 virtual void fixELFSymbolsInTLSFixups(MCAssembler &) const = 0;
587
588 static bool classof(const MCExpr *E) {
589 return E->getKind() == MCExpr::Target;
590 }
591};
592
593} // end namespace llvm
594
595#endif // LLVM_MC_MCEXPR_H