Add support for small-model PIC for PowerPC.
[oota-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 MCSection;
24 class MCSectionData;
25 class MCStreamer;
26 class MCSymbol;
27 class MCValue;
28 class raw_ostream;
29 class StringRef;
30 typedef DenseMap<const MCSectionData*, uint64_t> SectionAddrMap;
31
32 /// MCExpr - 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&) LLVM_DELETED_FUNCTION;
48   void operator=(const MCExpr&) LLVM_DELETED_FUNCTION;
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,
65                                  bool ForceVarExpansion) const;
66
67 public:
68   /// @name Accessors
69   /// @{
70
71   ExprKind getKind() const { return Kind; }
72
73   /// @}
74   /// @name Utility Methods
75   /// @{
76
77   void print(raw_ostream &OS) const;
78   void dump() const;
79
80   /// @}
81   /// @name Expression Evaluation
82   /// @{
83
84   /// EvaluateAsAbsolute - Try to evaluate the expression to an absolute value.
85   ///
86   /// @param Res - The absolute value, if evaluation succeeds.
87   /// @param Layout - The assembler layout object to use for evaluating symbol
88   /// values. If not given, then only non-symbolic expressions will be
89   /// evaluated.
90   /// @result - True on success.
91   bool EvaluateAsAbsolute(int64_t &Res, const MCAsmLayout &Layout,
92                           const SectionAddrMap &Addrs) const;
93   bool EvaluateAsAbsolute(int64_t &Res) const;
94   bool EvaluateAsAbsolute(int64_t &Res, const MCAssembler &Asm) const;
95   bool EvaluateAsAbsolute(int64_t &Res, const MCAsmLayout &Layout) const;
96
97   int64_t evaluateKnownAbsolute(const MCAsmLayout &Layout) const;
98
99   /// EvaluateAsRelocatable - Try to evaluate the expression to a relocatable
100   /// value, i.e. an expression of the fixed form (a - b + constant).
101   ///
102   /// @param Res - The relocatable value, if evaluation succeeds.
103   /// @param Layout - The assembler layout object to use for evaluating values.
104   /// @param Fixup - The Fixup object if available.
105   /// @result - True on success.
106   bool EvaluateAsRelocatable(MCValue &Res, const MCAsmLayout *Layout,
107                              const MCFixup *Fixup) const;
108
109   /// \brief Try to evaluate the expression to the form (a - b + constant) where
110   /// neither a nor b are variables.
111   ///
112   /// This is a more aggressive variant of EvaluateAsRelocatable. The intended
113   /// use is for when relocations are not available, like the symbol value in
114   /// the symbol table.
115   bool EvaluateAsValue(MCValue &Res, const MCAsmLayout *Layout,
116                        const MCFixup *Fixup) const;
117
118   /// FindAssociatedSection - Find the "associated section" for this expression,
119   /// which is currently defined as the absolute section for constants, or
120   /// otherwise the section associated with the first defined symbol in the
121   /// expression.
122   const MCSection *FindAssociatedSection() const;
123
124   /// @}
125 };
126
127 inline raw_ostream &operator<<(raw_ostream &OS, const MCExpr &E) {
128   E.print(OS);
129   return OS;
130 }
131
132 //// MCConstantExpr - Represent a constant integer expression.
133 class MCConstantExpr : public MCExpr {
134   int64_t Value;
135
136   explicit MCConstantExpr(int64_t _Value)
137     : MCExpr(MCExpr::Constant), Value(_Value) {}
138
139 public:
140   /// @name Construction
141   /// @{
142
143   static const MCConstantExpr *Create(int64_t Value, MCContext &Ctx);
144
145   /// @}
146   /// @name Accessors
147   /// @{
148
149   int64_t getValue() const { return Value; }
150
151   /// @}
152
153   static bool classof(const MCExpr *E) {
154     return E->getKind() == MCExpr::Constant;
155   }
156 };
157
158 /// MCSymbolRefExpr - Represent a reference to a symbol from inside an
159 /// 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 {
167     VK_None,
168     VK_Invalid,
169
170     VK_GOT,
171     VK_GOTOFF,
172     VK_GOTPCREL,
173     VK_GOTTPOFF,
174     VK_INDNTPOFF,
175     VK_NTPOFF,
176     VK_GOTNTPOFF,
177     VK_PLT,
178     VK_TLSGD,
179     VK_TLSLD,
180     VK_TLSLDM,
181     VK_TPOFF,
182     VK_DTPOFF,
183     VK_TLVP,      // Mach-O thread local variable relocations
184     VK_TLVPPAGE,
185     VK_TLVPPAGEOFF,
186     VK_PAGE,
187     VK_PAGEOFF,
188     VK_GOTPAGE,
189     VK_GOTPAGEOFF,
190     VK_SECREL,
191     VK_WEAKREF,   // The link between the symbols in .weakref foo, bar
192
193     VK_ARM_NONE,
194     VK_ARM_TARGET1,
195     VK_ARM_TARGET2,
196     VK_ARM_PREL31,
197     VK_ARM_TLSLDO,         // symbol(tlsldo)
198     VK_ARM_TLSCALL,        // symbol(tlscall)
199     VK_ARM_TLSDESC,        // symbol(tlsdesc)
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,          // symbol@tprel
219     VK_PPC_TPREL_LO,       // symbol@tprel@l
220     VK_PPC_TPREL_HI,       // symbol@tprel@h
221     VK_PPC_TPREL_HA,       // symbol@tprel@ha
222     VK_PPC_TPREL_HIGHER,   // symbol@tprel@higher
223     VK_PPC_TPREL_HIGHERA,  // symbol@tprel@highera
224     VK_PPC_TPREL_HIGHEST,  // symbol@tprel@highest
225     VK_PPC_TPREL_HIGHESTA, // symbol@tprel@highesta
226     VK_PPC_DTPREL,         // symbol@dtprel
227     VK_PPC_DTPREL_LO,      // symbol@dtprel@l
228     VK_PPC_DTPREL_HI,      // symbol@dtprel@h
229     VK_PPC_DTPREL_HA,      // symbol@dtprel@ha
230     VK_PPC_DTPREL_HIGHER,  // symbol@dtprel@higher
231     VK_PPC_DTPREL_HIGHERA, // symbol@dtprel@highera
232     VK_PPC_DTPREL_HIGHEST, // symbol@dtprel@highest
233     VK_PPC_DTPREL_HIGHESTA,// symbol@dtprel@highesta
234     VK_PPC_GOT_TPREL,      // symbol@got@tprel
235     VK_PPC_GOT_TPREL_LO,   // symbol@got@tprel@l
236     VK_PPC_GOT_TPREL_HI,   // symbol@got@tprel@h
237     VK_PPC_GOT_TPREL_HA,   // symbol@got@tprel@ha
238     VK_PPC_GOT_DTPREL,     // symbol@got@dtprel
239     VK_PPC_GOT_DTPREL_LO,  // symbol@got@dtprel@l
240     VK_PPC_GOT_DTPREL_HI,  // symbol@got@dtprel@h
241     VK_PPC_GOT_DTPREL_HA,  // symbol@got@dtprel@ha
242     VK_PPC_TLS,            // symbol@tls
243     VK_PPC_GOT_TLSGD,      // symbol@got@tlsgd
244     VK_PPC_GOT_TLSGD_LO,   // symbol@got@tlsgd@l
245     VK_PPC_GOT_TLSGD_HI,   // symbol@got@tlsgd@h
246     VK_PPC_GOT_TLSGD_HA,   // symbol@got@tlsgd@ha
247     VK_PPC_TLSGD,          // symbol@tlsgd
248     VK_PPC_GOT_TLSLD,      // symbol@got@tlsld
249     VK_PPC_GOT_TLSLD_LO,   // symbol@got@tlsld@l
250     VK_PPC_GOT_TLSLD_HI,   // symbol@got@tlsld@h
251     VK_PPC_GOT_TLSLD_HA,   // symbol@got@tlsld@ha
252     VK_PPC_TLSLD,          // symbol@tlsld
253     VK_PPC_LOCAL,          // symbol@local
254
255     VK_Mips_GPREL,
256     VK_Mips_GOT_CALL,
257     VK_Mips_GOT16,
258     VK_Mips_GOT,
259     VK_Mips_ABS_HI,
260     VK_Mips_ABS_LO,
261     VK_Mips_TLSGD,
262     VK_Mips_TLSLDM,
263     VK_Mips_DTPREL_HI,
264     VK_Mips_DTPREL_LO,
265     VK_Mips_GOTTPREL,
266     VK_Mips_TPREL_HI,
267     VK_Mips_TPREL_LO,
268     VK_Mips_GPOFF_HI,
269     VK_Mips_GPOFF_LO,
270     VK_Mips_GOT_DISP,
271     VK_Mips_GOT_PAGE,
272     VK_Mips_GOT_OFST,
273     VK_Mips_HIGHER,
274     VK_Mips_HIGHEST,
275     VK_Mips_GOT_HI16,
276     VK_Mips_GOT_LO16,
277     VK_Mips_CALL_HI16,
278     VK_Mips_CALL_LO16,
279     VK_Mips_PCREL_HI16,
280     VK_Mips_PCREL_LO16,
281
282     VK_COFF_IMGREL32 // symbol@imgrel (image-relative)
283   };
284
285 private:
286   /// The symbol reference modifier.
287   const unsigned Kind : 16;
288
289   /// Specifies how the variant kind should be printed.
290   const unsigned UseParensForSymbolVariant : 1;
291
292   // FIXME: Remove this bit.
293   const unsigned HasSubsectionsViaSymbols : 1;
294
295   /// The symbol being referenced.
296   const MCSymbol *Symbol;
297
298   explicit MCSymbolRefExpr(const MCSymbol *Symbol, VariantKind Kind,
299                            const MCAsmInfo *MAI);
300
301 public:
302   /// @name Construction
303   /// @{
304
305   static const MCSymbolRefExpr *Create(const MCSymbol *Symbol, MCContext &Ctx) {
306     return MCSymbolRefExpr::Create(Symbol, VK_None, Ctx);
307   }
308
309   static const MCSymbolRefExpr *Create(const MCSymbol *Symbol, VariantKind Kind,
310                                        MCContext &Ctx);
311   static const MCSymbolRefExpr *Create(StringRef Name, VariantKind Kind,
312                                        MCContext &Ctx);
313
314   /// @}
315   /// @name Accessors
316   /// @{
317
318   const MCSymbol &getSymbol() const { return *Symbol; }
319
320   VariantKind getKind() const { return static_cast<VariantKind>(Kind); }
321
322   void printVariantKind(raw_ostream &OS) const;
323
324   bool hasSubsectionsViaSymbols() const { return HasSubsectionsViaSymbols; }
325
326   /// @}
327   /// @name Static Utility Functions
328   /// @{
329
330   static StringRef getVariantKindName(VariantKind Kind);
331
332   static VariantKind getVariantKindForName(StringRef Name);
333
334   /// @}
335
336   static bool classof(const MCExpr *E) {
337     return E->getKind() == MCExpr::SymbolRef;
338   }
339 };
340
341 /// MCUnaryExpr - Unary assembler expressions.
342 class MCUnaryExpr : public MCExpr {
343 public:
344   enum Opcode {
345     LNot,  ///< Logical negation.
346     Minus, ///< Unary minus.
347     Not,   ///< Bitwise negation.
348     Plus   ///< Unary plus.
349   };
350
351 private:
352   Opcode Op;
353   const MCExpr *Expr;
354
355   MCUnaryExpr(Opcode _Op, const MCExpr *_Expr)
356     : MCExpr(MCExpr::Unary), Op(_Op), Expr(_Expr) {}
357
358 public:
359   /// @name Construction
360   /// @{
361
362   static const MCUnaryExpr *Create(Opcode Op, const MCExpr *Expr,
363                                    MCContext &Ctx);
364   static const MCUnaryExpr *CreateLNot(const MCExpr *Expr, MCContext &Ctx) {
365     return Create(LNot, Expr, Ctx);
366   }
367   static const MCUnaryExpr *CreateMinus(const MCExpr *Expr, MCContext &Ctx) {
368     return Create(Minus, Expr, Ctx);
369   }
370   static const MCUnaryExpr *CreateNot(const MCExpr *Expr, MCContext &Ctx) {
371     return Create(Not, Expr, Ctx);
372   }
373   static const MCUnaryExpr *CreatePlus(const MCExpr *Expr, MCContext &Ctx) {
374     return Create(Plus, Expr, Ctx);
375   }
376
377   /// @}
378   /// @name Accessors
379   /// @{
380
381   /// getOpcode - Get the kind of this unary expression.
382   Opcode getOpcode() const { return Op; }
383
384   /// getSubExpr - Get the child of this unary expression.
385   const MCExpr *getSubExpr() const { return Expr; }
386
387   /// @}
388
389   static bool classof(const MCExpr *E) {
390     return E->getKind() == MCExpr::Unary;
391   }
392 };
393
394 /// MCBinaryExpr - Binary assembler expressions.
395 class MCBinaryExpr : public MCExpr {
396 public:
397   enum Opcode {
398     Add,  ///< Addition.
399     And,  ///< Bitwise and.
400     Div,  ///< Signed division.
401     EQ,   ///< Equality comparison.
402     GT,   ///< Signed greater than comparison (result is either 0 or some
403           ///< target-specific non-zero value)
404     GTE,  ///< Signed greater than or equal comparison (result is either 0 or
405           ///< some target-specific non-zero value).
406     LAnd, ///< Logical and.
407     LOr,  ///< Logical or.
408     LT,   ///< Signed less than comparison (result is either 0 or
409           ///< some target-specific non-zero value).
410     LTE,  ///< Signed less than or equal comparison (result is either 0 or
411           ///< some target-specific non-zero value).
412     Mod,  ///< Signed remainder.
413     Mul,  ///< Multiplication.
414     NE,   ///< Inequality comparison.
415     Or,   ///< Bitwise or.
416     Shl,  ///< Shift left.
417     Shr,  ///< Shift right (arithmetic or logical, depending on target)
418     Sub,  ///< Subtraction.
419     Xor   ///< Bitwise exclusive or.
420   };
421
422 private:
423   Opcode Op;
424   const MCExpr *LHS, *RHS;
425
426   MCBinaryExpr(Opcode _Op, const MCExpr *_LHS, const MCExpr *_RHS)
427     : MCExpr(MCExpr::Binary), 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   static const MCBinaryExpr *CreateAdd(const MCExpr *LHS, const MCExpr *RHS,
436                                        MCContext &Ctx) {
437     return Create(Add, LHS, RHS, Ctx);
438   }
439   static const MCBinaryExpr *CreateAnd(const MCExpr *LHS, const MCExpr *RHS,
440                                        MCContext &Ctx) {
441     return Create(And, LHS, RHS, Ctx);
442   }
443   static const MCBinaryExpr *CreateDiv(const MCExpr *LHS, const MCExpr *RHS,
444                                        MCContext &Ctx) {
445     return Create(Div, LHS, RHS, Ctx);
446   }
447   static const MCBinaryExpr *CreateEQ(const MCExpr *LHS, const MCExpr *RHS,
448                                       MCContext &Ctx) {
449     return Create(EQ, LHS, RHS, Ctx);
450   }
451   static const MCBinaryExpr *CreateGT(const MCExpr *LHS, const MCExpr *RHS,
452                                       MCContext &Ctx) {
453     return Create(GT, LHS, RHS, Ctx);
454   }
455   static const MCBinaryExpr *CreateGTE(const MCExpr *LHS, const MCExpr *RHS,
456                                        MCContext &Ctx) {
457     return Create(GTE, LHS, RHS, Ctx);
458   }
459   static const MCBinaryExpr *CreateLAnd(const MCExpr *LHS, const MCExpr *RHS,
460                                         MCContext &Ctx) {
461     return Create(LAnd, LHS, RHS, Ctx);
462   }
463   static const MCBinaryExpr *CreateLOr(const MCExpr *LHS, const MCExpr *RHS,
464                                        MCContext &Ctx) {
465     return Create(LOr, LHS, RHS, Ctx);
466   }
467   static const MCBinaryExpr *CreateLT(const MCExpr *LHS, const MCExpr *RHS,
468                                       MCContext &Ctx) {
469     return Create(LT, LHS, RHS, Ctx);
470   }
471   static const MCBinaryExpr *CreateLTE(const MCExpr *LHS, const MCExpr *RHS,
472                                        MCContext &Ctx) {
473     return Create(LTE, LHS, RHS, Ctx);
474   }
475   static const MCBinaryExpr *CreateMod(const MCExpr *LHS, const MCExpr *RHS,
476                                        MCContext &Ctx) {
477     return Create(Mod, LHS, RHS, Ctx);
478   }
479   static const MCBinaryExpr *CreateMul(const MCExpr *LHS, const MCExpr *RHS,
480                                        MCContext &Ctx) {
481     return Create(Mul, LHS, RHS, Ctx);
482   }
483   static const MCBinaryExpr *CreateNE(const MCExpr *LHS, const MCExpr *RHS,
484                                       MCContext &Ctx) {
485     return Create(NE, LHS, RHS, Ctx);
486   }
487   static const MCBinaryExpr *CreateOr(const MCExpr *LHS, const MCExpr *RHS,
488                                       MCContext &Ctx) {
489     return Create(Or, LHS, RHS, Ctx);
490   }
491   static const MCBinaryExpr *CreateShl(const MCExpr *LHS, const MCExpr *RHS,
492                                        MCContext &Ctx) {
493     return Create(Shl, LHS, RHS, Ctx);
494   }
495   static const MCBinaryExpr *CreateShr(const MCExpr *LHS, const MCExpr *RHS,
496                                        MCContext &Ctx) {
497     return Create(Shr, LHS, RHS, Ctx);
498   }
499   static const MCBinaryExpr *CreateSub(const MCExpr *LHS, const MCExpr *RHS,
500                                        MCContext &Ctx) {
501     return Create(Sub, LHS, RHS, Ctx);
502   }
503   static const MCBinaryExpr *CreateXor(const MCExpr *LHS, const MCExpr *RHS,
504                                        MCContext &Ctx) {
505     return Create(Xor, LHS, RHS, Ctx);
506   }
507
508   /// @}
509   /// @name Accessors
510   /// @{
511
512   /// getOpcode - Get the kind of this binary expression.
513   Opcode getOpcode() const { return Op; }
514
515   /// getLHS - Get the left-hand side expression of the binary operator.
516   const MCExpr *getLHS() const { return LHS; }
517
518   /// getRHS - Get the right-hand side expression of the binary operator.
519   const MCExpr *getRHS() const { return RHS; }
520
521   /// @}
522
523   static bool classof(const MCExpr *E) {
524     return E->getKind() == MCExpr::Binary;
525   }
526 };
527
528 /// MCTargetExpr - This is an extension point for target-specific MCExpr
529 /// subclasses to implement.
530 ///
531 /// NOTE: All subclasses are required to have trivial destructors because
532 /// MCExprs are bump pointer allocated and not destructed.
533 class MCTargetExpr : public MCExpr {
534   virtual void anchor();
535 protected:
536   MCTargetExpr() : MCExpr(Target) {}
537   virtual ~MCTargetExpr() {}
538 public:
539
540   virtual void PrintImpl(raw_ostream &OS) const = 0;
541   virtual bool EvaluateAsRelocatableImpl(MCValue &Res,
542                                          const MCAsmLayout *Layout,
543                                          const MCFixup *Fixup) const = 0;
544   virtual void visitUsedExpr(MCStreamer& Streamer) const = 0;
545   virtual const MCSection *FindAssociatedSection() const = 0;
546
547   virtual void fixELFSymbolsInTLSFixups(MCAssembler &) const = 0;
548
549   static bool classof(const MCExpr *E) {
550     return E->getKind() == MCExpr::Target;
551   }
552 };
553
554 } // end namespace llvm
555
556 #endif