[Hexagon] Adding expression MC emission and removing XFAIL from test that hits this...
[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&) = 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;
77   void dump() const;
78
79   /// @}
80   /// @name Expression Evaluation
81   /// @{
82
83   /// EvaluateAsAbsolute - 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   /// @result - 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   /// EvaluateAsRelocatable - Try to evaluate the expression to a relocatable
99   /// value, i.e. an 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   /// @result - 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   /// FindAssociatedSection - Find the "associated section" for this expression,
116   /// which is currently defined as the absolute section for constants, or
117   /// otherwise the section associated with the first defined symbol in the
118   /// expression.
119   const MCSection *FindAssociatedSection() const;
120
121   /// @}
122 };
123
124 inline raw_ostream &operator<<(raw_ostream &OS, const MCExpr &E) {
125   E.print(OS);
126   return OS;
127 }
128
129 //// MCConstantExpr - 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 /// MCSymbolRefExpr - Represent a reference to a symbol from inside an
156 /// expression.
157 ///
158 /// A symbol reference in an expression may be a use of a label, a use of an
159 /// assembler variable (defined constant), or constitute an implicit definition
160 /// of the symbol as external.
161 class MCSymbolRefExpr : public MCExpr {
162 public:
163   enum VariantKind {
164     VK_None,
165     VK_Invalid,
166
167     VK_GOT,
168     VK_GOTOFF,
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_TLVP,      // Mach-O thread local variable relocations
181     VK_TLVPPAGE,
182     VK_TLVPPAGEOFF,
183     VK_PAGE,
184     VK_PAGEOFF,
185     VK_GOTPAGE,
186     VK_GOTPAGEOFF,
187     VK_SECREL,
188     VK_SIZE,      // symbol@SIZE
189     VK_WEAKREF,   // The link between the symbols in .weakref foo, bar
190
191     VK_ARM_NONE,
192     VK_ARM_TARGET1,
193     VK_ARM_TARGET2,
194     VK_ARM_PREL31,
195     VK_ARM_SBREL,          // symbol(sbrel)
196     VK_ARM_TLSLDO,         // symbol(tlsldo)
197     VK_ARM_TLSCALL,        // symbol(tlscall)
198     VK_ARM_TLSDESC,        // symbol(tlsdesc)
199     VK_ARM_TLSDESCSEQ,
200
201     VK_PPC_LO,             // symbol@l
202     VK_PPC_HI,             // symbol@h
203     VK_PPC_HA,             // symbol@ha
204     VK_PPC_HIGHER,         // symbol@higher
205     VK_PPC_HIGHERA,        // symbol@highera
206     VK_PPC_HIGHEST,        // symbol@highest
207     VK_PPC_HIGHESTA,       // symbol@highesta
208     VK_PPC_GOT_LO,         // symbol@got@l
209     VK_PPC_GOT_HI,         // symbol@got@h
210     VK_PPC_GOT_HA,         // symbol@got@ha
211     VK_PPC_TOCBASE,        // symbol@tocbase
212     VK_PPC_TOC,            // symbol@toc
213     VK_PPC_TOC_LO,         // symbol@toc@l
214     VK_PPC_TOC_HI,         // symbol@toc@h
215     VK_PPC_TOC_HA,         // symbol@toc@ha
216     VK_PPC_DTPMOD,         // symbol@dtpmod
217     VK_PPC_TPREL,          // symbol@tprel
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,         // symbol@dtprel
226     VK_PPC_DTPREL_LO,      // symbol@dtprel@l
227     VK_PPC_DTPREL_HI,      // symbol@dtprel@h
228     VK_PPC_DTPREL_HA,      // symbol@dtprel@ha
229     VK_PPC_DTPREL_HIGHER,  // symbol@dtprel@higher
230     VK_PPC_DTPREL_HIGHERA, // symbol@dtprel@highera
231     VK_PPC_DTPREL_HIGHEST, // symbol@dtprel@highest
232     VK_PPC_DTPREL_HIGHESTA,// symbol@dtprel@highesta
233     VK_PPC_GOT_TPREL,      // symbol@got@tprel
234     VK_PPC_GOT_TPREL_LO,   // symbol@got@tprel@l
235     VK_PPC_GOT_TPREL_HI,   // symbol@got@tprel@h
236     VK_PPC_GOT_TPREL_HA,   // symbol@got@tprel@ha
237     VK_PPC_GOT_DTPREL,     // symbol@got@dtprel
238     VK_PPC_GOT_DTPREL_LO,  // symbol@got@dtprel@l
239     VK_PPC_GOT_DTPREL_HI,  // symbol@got@dtprel@h
240     VK_PPC_GOT_DTPREL_HA,  // symbol@got@dtprel@ha
241     VK_PPC_TLS,            // symbol@tls
242     VK_PPC_GOT_TLSGD,      // symbol@got@tlsgd
243     VK_PPC_GOT_TLSGD_LO,   // symbol@got@tlsgd@l
244     VK_PPC_GOT_TLSGD_HI,   // symbol@got@tlsgd@h
245     VK_PPC_GOT_TLSGD_HA,   // symbol@got@tlsgd@ha
246     VK_PPC_TLSGD,          // symbol@tlsgd
247     VK_PPC_GOT_TLSLD,      // symbol@got@tlsld
248     VK_PPC_GOT_TLSLD_LO,   // symbol@got@tlsld@l
249     VK_PPC_GOT_TLSLD_HI,   // symbol@got@tlsld@h
250     VK_PPC_GOT_TLSLD_HA,   // symbol@got@tlsld@ha
251     VK_PPC_TLSLD,          // symbol@tlsld
252     VK_PPC_LOCAL,          // symbol@local
253
254     VK_Mips_GPREL,
255     VK_Mips_GOT_CALL,
256     VK_Mips_GOT16,
257     VK_Mips_GOT,
258     VK_Mips_ABS_HI,
259     VK_Mips_ABS_LO,
260     VK_Mips_TLSGD,
261     VK_Mips_TLSLDM,
262     VK_Mips_DTPREL_HI,
263     VK_Mips_DTPREL_LO,
264     VK_Mips_GOTTPREL,
265     VK_Mips_TPREL_HI,
266     VK_Mips_TPREL_LO,
267     VK_Mips_GPOFF_HI,
268     VK_Mips_GPOFF_LO,
269     VK_Mips_GOT_DISP,
270     VK_Mips_GOT_PAGE,
271     VK_Mips_GOT_OFST,
272     VK_Mips_HIGHER,
273     VK_Mips_HIGHEST,
274     VK_Mips_GOT_HI16,
275     VK_Mips_GOT_LO16,
276     VK_Mips_CALL_HI16,
277     VK_Mips_CALL_LO16,
278     VK_Mips_PCREL_HI16,
279     VK_Mips_PCREL_LO16,
280
281     VK_COFF_IMGREL32, // symbol@imgrel (image-relative)
282
283     VK_Hexagon_PCREL,
284     VK_Hexagon_LO16,
285     VK_Hexagon_HI16,
286     VK_Hexagon_GPREL,
287     VK_Hexagon_GD_GOT,
288     VK_Hexagon_LD_GOT,
289     VK_Hexagon_GD_PLT,
290     VK_Hexagon_LD_PLT,
291     VK_Hexagon_IE,
292     VK_Hexagon_IE_GOT,
293     VK_TPREL,
294     VK_DTPREL
295   };
296
297 private:
298   /// The symbol reference modifier.
299   const unsigned Kind : 16;
300
301   /// Specifies how the variant kind should be printed.
302   const unsigned UseParensForSymbolVariant : 1;
303
304   // FIXME: Remove this bit.
305   const unsigned HasSubsectionsViaSymbols : 1;
306
307   /// The symbol being referenced.
308   const MCSymbol *Symbol;
309
310   explicit MCSymbolRefExpr(const MCSymbol *Symbol, VariantKind Kind,
311                            const MCAsmInfo *MAI);
312
313 public:
314   /// @name Construction
315   /// @{
316
317   static const MCSymbolRefExpr *Create(const MCSymbol *Symbol, MCContext &Ctx) {
318     return MCSymbolRefExpr::Create(Symbol, VK_None, Ctx);
319   }
320
321   static const MCSymbolRefExpr *Create(const MCSymbol *Symbol, VariantKind Kind,
322                                        MCContext &Ctx);
323   static const MCSymbolRefExpr *Create(StringRef Name, VariantKind Kind,
324                                        MCContext &Ctx);
325
326   /// @}
327   /// @name Accessors
328   /// @{
329
330   const MCSymbol &getSymbol() const { return *Symbol; }
331
332   VariantKind getKind() const { return static_cast<VariantKind>(Kind); }
333
334   void printVariantKind(raw_ostream &OS) const;
335
336   bool hasSubsectionsViaSymbols() const { return HasSubsectionsViaSymbols; }
337
338   /// @}
339   /// @name Static Utility Functions
340   /// @{
341
342   static StringRef getVariantKindName(VariantKind Kind);
343
344   static VariantKind getVariantKindForName(StringRef Name);
345
346   /// @}
347
348   static bool classof(const MCExpr *E) {
349     return E->getKind() == MCExpr::SymbolRef;
350   }
351 };
352
353 /// MCUnaryExpr - Unary assembler expressions.
354 class MCUnaryExpr : public MCExpr {
355 public:
356   enum Opcode {
357     LNot,  ///< Logical negation.
358     Minus, ///< Unary minus.
359     Not,   ///< Bitwise negation.
360     Plus   ///< Unary plus.
361   };
362
363 private:
364   Opcode Op;
365   const MCExpr *Expr;
366
367   MCUnaryExpr(Opcode Op, const MCExpr *Expr)
368       : MCExpr(MCExpr::Unary), Op(Op), Expr(Expr) {}
369
370 public:
371   /// @name Construction
372   /// @{
373
374   static const MCUnaryExpr *Create(Opcode Op, const MCExpr *Expr,
375                                    MCContext &Ctx);
376   static const MCUnaryExpr *CreateLNot(const MCExpr *Expr, MCContext &Ctx) {
377     return Create(LNot, Expr, Ctx);
378   }
379   static const MCUnaryExpr *CreateMinus(const MCExpr *Expr, MCContext &Ctx) {
380     return Create(Minus, Expr, Ctx);
381   }
382   static const MCUnaryExpr *CreateNot(const MCExpr *Expr, MCContext &Ctx) {
383     return Create(Not, Expr, Ctx);
384   }
385   static const MCUnaryExpr *CreatePlus(const MCExpr *Expr, MCContext &Ctx) {
386     return Create(Plus, Expr, Ctx);
387   }
388
389   /// @}
390   /// @name Accessors
391   /// @{
392
393   /// getOpcode - Get the kind of this unary expression.
394   Opcode getOpcode() const { return Op; }
395
396   /// getSubExpr - 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 /// MCBinaryExpr - Binary assembler expressions.
407 class MCBinaryExpr : public MCExpr {
408 public:
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
435 private:
436   Opcode Op;
437   const MCExpr *LHS, *RHS;
438
439   MCBinaryExpr(Opcode Op, const MCExpr *LHS, const MCExpr *RHS)
440       : MCExpr(MCExpr::Binary), Op(Op), LHS(LHS), RHS(RHS) {}
441
442 public:
443   /// @name Construction
444   /// @{
445
446   static const MCBinaryExpr *Create(Opcode Op, const MCExpr *LHS,
447                                     const MCExpr *RHS, MCContext &Ctx);
448   static const MCBinaryExpr *CreateAdd(const MCExpr *LHS, const MCExpr *RHS,
449                                        MCContext &Ctx) {
450     return Create(Add, LHS, RHS, Ctx);
451   }
452   static const MCBinaryExpr *CreateAnd(const MCExpr *LHS, const MCExpr *RHS,
453                                        MCContext &Ctx) {
454     return Create(And, LHS, RHS, Ctx);
455   }
456   static const MCBinaryExpr *CreateDiv(const MCExpr *LHS, const MCExpr *RHS,
457                                        MCContext &Ctx) {
458     return Create(Div, LHS, RHS, Ctx);
459   }
460   static const MCBinaryExpr *CreateEQ(const MCExpr *LHS, const MCExpr *RHS,
461                                       MCContext &Ctx) {
462     return Create(EQ, LHS, RHS, Ctx);
463   }
464   static const MCBinaryExpr *CreateGT(const MCExpr *LHS, const MCExpr *RHS,
465                                       MCContext &Ctx) {
466     return Create(GT, LHS, RHS, Ctx);
467   }
468   static const MCBinaryExpr *CreateGTE(const MCExpr *LHS, const MCExpr *RHS,
469                                        MCContext &Ctx) {
470     return Create(GTE, LHS, RHS, Ctx);
471   }
472   static const MCBinaryExpr *CreateLAnd(const MCExpr *LHS, const MCExpr *RHS,
473                                         MCContext &Ctx) {
474     return Create(LAnd, LHS, RHS, Ctx);
475   }
476   static const MCBinaryExpr *CreateLOr(const MCExpr *LHS, const MCExpr *RHS,
477                                        MCContext &Ctx) {
478     return Create(LOr, LHS, RHS, Ctx);
479   }
480   static const MCBinaryExpr *CreateLT(const MCExpr *LHS, const MCExpr *RHS,
481                                       MCContext &Ctx) {
482     return Create(LT, LHS, RHS, Ctx);
483   }
484   static const MCBinaryExpr *CreateLTE(const MCExpr *LHS, const MCExpr *RHS,
485                                        MCContext &Ctx) {
486     return Create(LTE, LHS, RHS, Ctx);
487   }
488   static const MCBinaryExpr *CreateMod(const MCExpr *LHS, const MCExpr *RHS,
489                                        MCContext &Ctx) {
490     return Create(Mod, LHS, RHS, Ctx);
491   }
492   static const MCBinaryExpr *CreateMul(const MCExpr *LHS, const MCExpr *RHS,
493                                        MCContext &Ctx) {
494     return Create(Mul, LHS, RHS, Ctx);
495   }
496   static const MCBinaryExpr *CreateNE(const MCExpr *LHS, const MCExpr *RHS,
497                                       MCContext &Ctx) {
498     return Create(NE, LHS, RHS, Ctx);
499   }
500   static const MCBinaryExpr *CreateOr(const MCExpr *LHS, const MCExpr *RHS,
501                                       MCContext &Ctx) {
502     return Create(Or, LHS, RHS, Ctx);
503   }
504   static const MCBinaryExpr *CreateShl(const MCExpr *LHS, const MCExpr *RHS,
505                                        MCContext &Ctx) {
506     return Create(Shl, LHS, RHS, Ctx);
507   }
508   static const MCBinaryExpr *CreateAShr(const MCExpr *LHS, const MCExpr *RHS,
509                                        MCContext &Ctx) {
510     return Create(AShr, LHS, RHS, Ctx);
511   }
512   static const MCBinaryExpr *CreateLShr(const MCExpr *LHS, const MCExpr *RHS,
513                                        MCContext &Ctx) {
514     return Create(LShr, LHS, RHS, Ctx);
515   }
516   static const MCBinaryExpr *CreateSub(const MCExpr *LHS, const MCExpr *RHS,
517                                        MCContext &Ctx) {
518     return Create(Sub, LHS, RHS, Ctx);
519   }
520   static const MCBinaryExpr *CreateXor(const MCExpr *LHS, const MCExpr *RHS,
521                                        MCContext &Ctx) {
522     return Create(Xor, LHS, RHS, Ctx);
523   }
524
525   /// @}
526   /// @name Accessors
527   /// @{
528
529   /// getOpcode - Get the kind of this binary expression.
530   Opcode getOpcode() const { return Op; }
531
532   /// getLHS - Get the left-hand side expression of the binary operator.
533   const MCExpr *getLHS() const { return LHS; }
534
535   /// getRHS - Get the right-hand side expression of the binary operator.
536   const MCExpr *getRHS() const { return RHS; }
537
538   /// @}
539
540   static bool classof(const MCExpr *E) {
541     return E->getKind() == MCExpr::Binary;
542   }
543 };
544
545 /// MCTargetExpr - This is an extension point for target-specific MCExpr
546 /// subclasses to implement.
547 ///
548 /// NOTE: All subclasses are required to have trivial destructors because
549 /// MCExprs are bump pointer allocated and not destructed.
550 class MCTargetExpr : public MCExpr {
551   virtual void anchor();
552 protected:
553   MCTargetExpr() : MCExpr(Target) {}
554   virtual ~MCTargetExpr() {}
555 public:
556
557   virtual void PrintImpl(raw_ostream &OS) const = 0;
558   virtual bool EvaluateAsRelocatableImpl(MCValue &Res,
559                                          const MCAsmLayout *Layout,
560                                          const MCFixup *Fixup) const = 0;
561   virtual void visitUsedExpr(MCStreamer& Streamer) const = 0;
562   virtual const MCSection *FindAssociatedSection() const = 0;
563
564   virtual void fixELFSymbolsInTLSFixups(MCAssembler &) const = 0;
565
566   static bool classof(const MCExpr *E) {
567     return E->getKind() == MCExpr::Target;
568   }
569 };
570
571 } // end namespace llvm
572
573 #endif