OSDN Git Service

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