OSDN Git Service

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