OSDN Git Service

MC: Introduce the ABS8 symbol modifier.
[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/Casting.h"
15 #include "llvm/Support/DataTypes.h"
16 #include "llvm/Support/SMLoc.h"
17
18 namespace llvm {
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   MCExpr(const MCExpr&) = delete;
50   void operator=(const MCExpr&) = delete;
51
52   bool evaluateAsAbsolute(int64_t &Res, const MCAssembler *Asm,
53                           const MCAsmLayout *Layout,
54                           const SectionAddrMap *Addrs) const;
55
56   bool evaluateAsAbsolute(int64_t &Res, const MCAssembler *Asm,
57                           const MCAsmLayout *Layout,
58                           const SectionAddrMap *Addrs, bool InSet) const;
59
60 protected:
61   explicit MCExpr(ExprKind Kind, SMLoc Loc) : Kind(Kind), Loc(Loc) {}
62
63   bool evaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm,
64                                  const MCAsmLayout *Layout,
65                                  const MCFixup *Fixup,
66                                  const SectionAddrMap *Addrs, bool InSet) const;
67
68 public:
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   static const MCUnaryExpr *createLNot(const MCExpr *Expr, MCContext &Ctx) {
363     return create(LNot, Expr, Ctx);
364   }
365   static const MCUnaryExpr *createMinus(const MCExpr *Expr, MCContext &Ctx) {
366     return create(Minus, Expr, Ctx);
367   }
368   static const MCUnaryExpr *createNot(const MCExpr *Expr, MCContext &Ctx) {
369     return create(Not, Expr, Ctx);
370   }
371   static const MCUnaryExpr *createPlus(const MCExpr *Expr, MCContext &Ctx) {
372     return create(Plus, Expr, Ctx);
373   }
374
375   /// @}
376   /// \name Accessors
377   /// @{
378
379   /// \brief Get the kind of this unary expression.
380   Opcode getOpcode() const { return Op; }
381
382   /// \brief Get the child of this unary expression.
383   const MCExpr *getSubExpr() const { return Expr; }
384
385   /// @}
386
387   static bool classof(const MCExpr *E) {
388     return E->getKind() == MCExpr::Unary;
389   }
390 };
391
392 /// \brief Binary assembler expressions.
393 class MCBinaryExpr : public MCExpr {
394 public:
395   enum Opcode {
396     Add,  ///< Addition.
397     And,  ///< Bitwise and.
398     Div,  ///< Signed division.
399     EQ,   ///< Equality comparison.
400     GT,   ///< Signed greater than comparison (result is either 0 or some
401           ///< target-specific non-zero value)
402     GTE,  ///< Signed greater than or equal comparison (result is either 0 or
403           ///< some target-specific non-zero value).
404     LAnd, ///< Logical and.
405     LOr,  ///< Logical or.
406     LT,   ///< Signed less than comparison (result is either 0 or
407           ///< some target-specific non-zero value).
408     LTE,  ///< Signed less than or equal comparison (result is either 0 or
409           ///< some target-specific non-zero value).
410     Mod,  ///< Signed remainder.
411     Mul,  ///< Multiplication.
412     NE,   ///< Inequality comparison.
413     Or,   ///< Bitwise or.
414     Shl,  ///< Shift left.
415     AShr, ///< Arithmetic shift right.
416     LShr, ///< Logical shift right.
417     Sub,  ///< Subtraction.
418     Xor   ///< Bitwise exclusive or.
419   };
420
421 private:
422   Opcode Op;
423   const MCExpr *LHS, *RHS;
424
425   MCBinaryExpr(Opcode Op, const MCExpr *LHS, const MCExpr *RHS,
426                SMLoc Loc = SMLoc())
427       : MCExpr(MCExpr::Binary, Loc), Op(Op), LHS(LHS), RHS(RHS) {}
428
429 public:
430   /// \name Construction
431   /// @{
432
433   static const MCBinaryExpr *create(Opcode Op, const MCExpr *LHS,
434                                     const MCExpr *RHS, MCContext &Ctx,
435                                     SMLoc Loc = SMLoc());
436   static const MCBinaryExpr *createAdd(const MCExpr *LHS, const MCExpr *RHS,
437                                        MCContext &Ctx) {
438     return create(Add, LHS, RHS, Ctx);
439   }
440   static const MCBinaryExpr *createAnd(const MCExpr *LHS, const MCExpr *RHS,
441                                        MCContext &Ctx) {
442     return create(And, LHS, RHS, Ctx);
443   }
444   static const MCBinaryExpr *createDiv(const MCExpr *LHS, const MCExpr *RHS,
445                                        MCContext &Ctx) {
446     return create(Div, LHS, RHS, Ctx);
447   }
448   static const MCBinaryExpr *createEQ(const MCExpr *LHS, const MCExpr *RHS,
449                                       MCContext &Ctx) {
450     return create(EQ, LHS, RHS, Ctx);
451   }
452   static const MCBinaryExpr *createGT(const MCExpr *LHS, const MCExpr *RHS,
453                                       MCContext &Ctx) {
454     return create(GT, LHS, RHS, Ctx);
455   }
456   static const MCBinaryExpr *createGTE(const MCExpr *LHS, const MCExpr *RHS,
457                                        MCContext &Ctx) {
458     return create(GTE, LHS, RHS, Ctx);
459   }
460   static const MCBinaryExpr *createLAnd(const MCExpr *LHS, const MCExpr *RHS,
461                                         MCContext &Ctx) {
462     return create(LAnd, LHS, RHS, Ctx);
463   }
464   static const MCBinaryExpr *createLOr(const MCExpr *LHS, const MCExpr *RHS,
465                                        MCContext &Ctx) {
466     return create(LOr, LHS, RHS, Ctx);
467   }
468   static const MCBinaryExpr *createLT(const MCExpr *LHS, const MCExpr *RHS,
469                                       MCContext &Ctx) {
470     return create(LT, LHS, RHS, Ctx);
471   }
472   static const MCBinaryExpr *createLTE(const MCExpr *LHS, const MCExpr *RHS,
473                                        MCContext &Ctx) {
474     return create(LTE, LHS, RHS, Ctx);
475   }
476   static const MCBinaryExpr *createMod(const MCExpr *LHS, const MCExpr *RHS,
477                                        MCContext &Ctx) {
478     return create(Mod, LHS, RHS, Ctx);
479   }
480   static const MCBinaryExpr *createMul(const MCExpr *LHS, const MCExpr *RHS,
481                                        MCContext &Ctx) {
482     return create(Mul, LHS, RHS, Ctx);
483   }
484   static const MCBinaryExpr *createNE(const MCExpr *LHS, const MCExpr *RHS,
485                                       MCContext &Ctx) {
486     return create(NE, LHS, RHS, Ctx);
487   }
488   static const MCBinaryExpr *createOr(const MCExpr *LHS, const MCExpr *RHS,
489                                       MCContext &Ctx) {
490     return create(Or, LHS, RHS, Ctx);
491   }
492   static const MCBinaryExpr *createShl(const MCExpr *LHS, const MCExpr *RHS,
493                                        MCContext &Ctx) {
494     return create(Shl, LHS, RHS, Ctx);
495   }
496   static const MCBinaryExpr *createAShr(const MCExpr *LHS, const MCExpr *RHS,
497                                        MCContext &Ctx) {
498     return create(AShr, LHS, RHS, Ctx);
499   }
500   static const MCBinaryExpr *createLShr(const MCExpr *LHS, const MCExpr *RHS,
501                                        MCContext &Ctx) {
502     return create(LShr, LHS, RHS, Ctx);
503   }
504   static const MCBinaryExpr *createSub(const MCExpr *LHS, const MCExpr *RHS,
505                                        MCContext &Ctx) {
506     return create(Sub, LHS, RHS, Ctx);
507   }
508   static const MCBinaryExpr *createXor(const MCExpr *LHS, const MCExpr *RHS,
509                                        MCContext &Ctx) {
510     return create(Xor, LHS, RHS, Ctx);
511   }
512
513   /// @}
514   /// \name Accessors
515   /// @{
516
517   /// \brief Get the kind of this binary expression.
518   Opcode getOpcode() const { return Op; }
519
520   /// \brief Get the left-hand side expression of the binary operator.
521   const MCExpr *getLHS() const { return LHS; }
522
523   /// \brief Get the right-hand side expression of the binary operator.
524   const MCExpr *getRHS() const { return RHS; }
525
526   /// @}
527
528   static bool classof(const MCExpr *E) {
529     return E->getKind() == MCExpr::Binary;
530   }
531 };
532
533 /// \brief This is an extension point for target-specific MCExpr subclasses to
534 /// implement.
535 ///
536 /// NOTE: All subclasses are required to have trivial destructors because
537 /// MCExprs are bump pointer allocated and not destructed.
538 class MCTargetExpr : public MCExpr {
539   virtual void anchor();
540 protected:
541   MCTargetExpr() : MCExpr(Target, SMLoc()) {}
542   virtual ~MCTargetExpr() {}
543 public:
544   virtual void printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const = 0;
545   virtual bool evaluateAsRelocatableImpl(MCValue &Res,
546                                          const MCAsmLayout *Layout,
547                                          const MCFixup *Fixup) const = 0;
548   virtual void visitUsedExpr(MCStreamer& Streamer) const = 0;
549   virtual MCFragment *findAssociatedFragment() const = 0;
550
551   virtual void fixELFSymbolsInTLSFixups(MCAssembler &) const = 0;
552
553   static bool classof(const MCExpr *E) {
554     return E->getKind() == MCExpr::Target;
555   }
556 };
557
558 } // end namespace llvm
559
560 #endif