1 //===- MCExpr.h - Assembly Level Expressions --------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLVM_MC_MCEXPR_H
10 #define LLVM_MC_MCEXPR_H
11 
12 #include "llvm/ADT/DenseMap.h"
13 #include "llvm/Support/SMLoc.h"
14 #include <cstdint>
15 
16 namespace llvm {
17 
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 
31 using SectionAddrMap = DenseMap<const MCSection *, uint64_t>;
32 
33 /// Base class for the full range of assembler expressions which are
34 /// needed for parsing.
35 class MCExpr {
36 public:
37   enum ExprKind : uint8_t {
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   static const unsigned NumSubclassDataBits = 24;
47   static_assert(
48       NumSubclassDataBits == CHAR_BIT * (sizeof(unsigned) - sizeof(ExprKind)),
49       "ExprKind and SubclassData together should take up one word");
50 
51   ExprKind Kind;
52   /// Field reserved for use by MCExpr subclasses.
53   unsigned SubclassData : NumSubclassDataBits;
54   SMLoc Loc;
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, unsigned SubclassData = 0)
Kind(Kind)62       : Kind(Kind), SubclassData(SubclassData), Loc(Loc) {
63     assert(SubclassData < (1 << NumSubclassDataBits) &&
64            "Subclass data too large");
65   }
66 
67   bool evaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm,
68                                  const MCAsmLayout *Layout,
69                                  const MCFixup *Fixup,
70                                  const SectionAddrMap *Addrs, bool InSet) const;
71 
getSubclassData()72   unsigned getSubclassData() const { return SubclassData; }
73 
74 public:
75   MCExpr(const MCExpr &) = delete;
76   MCExpr &operator=(const MCExpr &) = delete;
77 
78   /// \name Accessors
79   /// @{
80 
getKind()81   ExprKind getKind() const { return Kind; }
getLoc()82   SMLoc getLoc() const { return Loc; }
83 
84   /// @}
85   /// \name Utility Methods
86   /// @{
87 
88   void print(raw_ostream &OS, const MCAsmInfo *MAI,
89              bool InParens = false) const;
90   void dump() const;
91 
92   /// @}
93   /// \name Expression Evaluation
94   /// @{
95 
96   /// Try to evaluate the expression to an absolute value.
97   ///
98   /// \param Res - The absolute value, if evaluation succeeds.
99   /// \param Layout - The assembler layout object to use for evaluating symbol
100   /// values. If not given, then only non-symbolic expressions will be
101   /// evaluated.
102   /// \return - True on success.
103   bool evaluateAsAbsolute(int64_t &Res, const MCAsmLayout &Layout,
104                           const SectionAddrMap &Addrs) const;
105   bool evaluateAsAbsolute(int64_t &Res) const;
106   bool evaluateAsAbsolute(int64_t &Res, MCStreamer &Out) const;
107   bool evaluateAsAbsolute(int64_t &Res, const MCAssembler &Asm) const;
108   bool evaluateAsAbsolute(int64_t &Res, const MCAssembler *Asm) const;
109   bool evaluateAsAbsolute(int64_t &Res, const MCAsmLayout &Layout) const;
110 
111   bool evaluateKnownAbsolute(int64_t &Res, const MCAsmLayout &Layout) const;
112 
113   /// Try to evaluate the expression to a relocatable value, i.e. an
114   /// expression of the fixed form (a - b + constant).
115   ///
116   /// \param Res - The relocatable value, if evaluation succeeds.
117   /// \param Layout - The assembler layout object to use for evaluating values.
118   /// \param Fixup - The Fixup object if available.
119   /// \return - True on success.
120   bool evaluateAsRelocatable(MCValue &Res, const MCAsmLayout *Layout,
121                              const MCFixup *Fixup) const;
122 
123   /// Try to evaluate the expression to the form (a - b + constant) where
124   /// neither a nor b are variables.
125   ///
126   /// This is a more aggressive variant of evaluateAsRelocatable. The intended
127   /// use is for when relocations are not available, like the .size directive.
128   bool evaluateAsValue(MCValue &Res, const MCAsmLayout &Layout) const;
129 
130   /// Find the "associated section" for this expression, which is
131   /// currently defined as the absolute section for constants, or
132   /// otherwise the section associated with the first defined symbol in the
133   /// expression.
134   MCFragment *findAssociatedFragment() const;
135 
136   /// @}
137 };
138 
139 inline raw_ostream &operator<<(raw_ostream &OS, const MCExpr &E) {
140   E.print(OS, nullptr);
141   return OS;
142 }
143 
144 ////  Represent a constant integer expression.
145 class MCConstantExpr : public MCExpr {
146   int64_t Value;
147 
148   // Subclass data stores SizeInBytes in bits 0..7 and PrintInHex in bit 8.
149   static const unsigned SizeInBytesBits = 8;
150   static const unsigned SizeInBytesMask = (1 << SizeInBytesBits) - 1;
151   static const unsigned PrintInHexBit = 1 << SizeInBytesBits;
152 
encodeSubclassData(bool PrintInHex,unsigned SizeInBytes)153   static unsigned encodeSubclassData(bool PrintInHex, unsigned SizeInBytes) {
154     assert(SizeInBytes <= sizeof(int64_t) && "Excessive size");
155     return SizeInBytes | (PrintInHex ? PrintInHexBit : 0);
156   }
157 
MCConstantExpr(int64_t Value,bool PrintInHex,unsigned SizeInBytes)158   MCConstantExpr(int64_t Value, bool PrintInHex, unsigned SizeInBytes)
159       : MCExpr(MCExpr::Constant, SMLoc(),
160                encodeSubclassData(PrintInHex, SizeInBytes)), Value(Value) {}
161 
162 public:
163   /// \name Construction
164   /// @{
165 
166   static const MCConstantExpr *create(int64_t Value, MCContext &Ctx,
167                                       bool PrintInHex = false,
168                                       unsigned SizeInBytes = 0);
169 
170   /// @}
171   /// \name Accessors
172   /// @{
173 
getValue()174   int64_t getValue() const { return Value; }
getSizeInBytes()175   unsigned getSizeInBytes() const {
176     return getSubclassData() & SizeInBytesMask;
177   }
178 
useHexFormat()179   bool useHexFormat() const { return (getSubclassData() & PrintInHexBit) != 0; }
180 
181   /// @}
182 
classof(const MCExpr * E)183   static bool classof(const MCExpr *E) {
184     return E->getKind() == MCExpr::Constant;
185   }
186 };
187 
188 ///  Represent a reference to a symbol from inside an expression.
189 ///
190 /// A symbol reference in an expression may be a use of a label, a use of an
191 /// assembler variable (defined constant), or constitute an implicit definition
192 /// of the symbol as external.
193 class MCSymbolRefExpr : public MCExpr {
194 public:
195   enum VariantKind : uint16_t {
196     VK_None,
197     VK_Invalid,
198 
199     VK_GOT,
200     VK_GOTOFF,
201     VK_GOTREL,
202     VK_PCREL,
203     VK_GOTPCREL,
204     VK_GOTTPOFF,
205     VK_INDNTPOFF,
206     VK_NTPOFF,
207     VK_GOTNTPOFF,
208     VK_PLT,
209     VK_TLSGD,
210     VK_TLSLD,
211     VK_TLSLDM,
212     VK_TPOFF,
213     VK_DTPOFF,
214     VK_TLSCALL, // symbol(tlscall)
215     VK_TLSDESC, // symbol(tlsdesc)
216     VK_TLVP,    // Mach-O thread local variable relocations
217     VK_TLVPPAGE,
218     VK_TLVPPAGEOFF,
219     VK_PAGE,
220     VK_PAGEOFF,
221     VK_GOTPAGE,
222     VK_GOTPAGEOFF,
223     VK_SECREL,
224     VK_SIZE,    // symbol@SIZE
225     VK_WEAKREF, // The link between the symbols in .weakref foo, bar
226 
227     VK_X86_ABS8,
228 
229     VK_ARM_NONE,
230     VK_ARM_GOT_PREL,
231     VK_ARM_TARGET1,
232     VK_ARM_TARGET2,
233     VK_ARM_PREL31,
234     VK_ARM_SBREL,  // symbol(sbrel)
235     VK_ARM_TLSLDO, // symbol(tlsldo)
236     VK_ARM_TLSDESCSEQ,
237 
238     VK_AVR_NONE,
239     VK_AVR_LO8,
240     VK_AVR_HI8,
241     VK_AVR_HLO8,
242     VK_AVR_DIFF8,
243     VK_AVR_DIFF16,
244     VK_AVR_DIFF32,
245 
246     VK_PPC_LO,              // symbol@l
247     VK_PPC_HI,              // symbol@h
248     VK_PPC_HA,              // symbol@ha
249     VK_PPC_HIGH,            // symbol@high
250     VK_PPC_HIGHA,           // symbol@higha
251     VK_PPC_HIGHER,          // symbol@higher
252     VK_PPC_HIGHERA,         // symbol@highera
253     VK_PPC_HIGHEST,         // symbol@highest
254     VK_PPC_HIGHESTA,        // symbol@highesta
255     VK_PPC_GOT_LO,          // symbol@got@l
256     VK_PPC_GOT_HI,          // symbol@got@h
257     VK_PPC_GOT_HA,          // symbol@got@ha
258     VK_PPC_TOCBASE,         // symbol@tocbase
259     VK_PPC_TOC,             // symbol@toc
260     VK_PPC_TOC_LO,          // symbol@toc@l
261     VK_PPC_TOC_HI,          // symbol@toc@h
262     VK_PPC_TOC_HA,          // symbol@toc@ha
263     VK_PPC_U,               // symbol@u
264     VK_PPC_L,               // symbol@l
265     VK_PPC_DTPMOD,          // symbol@dtpmod
266     VK_PPC_TPREL_LO,        // symbol@tprel@l
267     VK_PPC_TPREL_HI,        // symbol@tprel@h
268     VK_PPC_TPREL_HA,        // symbol@tprel@ha
269     VK_PPC_TPREL_HIGH,      // symbol@tprel@high
270     VK_PPC_TPREL_HIGHA,     // symbol@tprel@higha
271     VK_PPC_TPREL_HIGHER,    // symbol@tprel@higher
272     VK_PPC_TPREL_HIGHERA,   // symbol@tprel@highera
273     VK_PPC_TPREL_HIGHEST,   // symbol@tprel@highest
274     VK_PPC_TPREL_HIGHESTA,  // symbol@tprel@highesta
275     VK_PPC_DTPREL_LO,       // symbol@dtprel@l
276     VK_PPC_DTPREL_HI,       // symbol@dtprel@h
277     VK_PPC_DTPREL_HA,       // symbol@dtprel@ha
278     VK_PPC_DTPREL_HIGH,     // symbol@dtprel@high
279     VK_PPC_DTPREL_HIGHA,    // symbol@dtprel@higha
280     VK_PPC_DTPREL_HIGHER,   // symbol@dtprel@higher
281     VK_PPC_DTPREL_HIGHERA,  // symbol@dtprel@highera
282     VK_PPC_DTPREL_HIGHEST,  // symbol@dtprel@highest
283     VK_PPC_DTPREL_HIGHESTA, // symbol@dtprel@highesta
284     VK_PPC_GOT_TPREL,       // symbol@got@tprel
285     VK_PPC_GOT_TPREL_LO,    // symbol@got@tprel@l
286     VK_PPC_GOT_TPREL_HI,    // symbol@got@tprel@h
287     VK_PPC_GOT_TPREL_HA,    // symbol@got@tprel@ha
288     VK_PPC_GOT_DTPREL,      // symbol@got@dtprel
289     VK_PPC_GOT_DTPREL_LO,   // symbol@got@dtprel@l
290     VK_PPC_GOT_DTPREL_HI,   // symbol@got@dtprel@h
291     VK_PPC_GOT_DTPREL_HA,   // symbol@got@dtprel@ha
292     VK_PPC_TLS,             // symbol@tls
293     VK_PPC_GOT_TLSGD,       // symbol@got@tlsgd
294     VK_PPC_GOT_TLSGD_LO,    // symbol@got@tlsgd@l
295     VK_PPC_GOT_TLSGD_HI,    // symbol@got@tlsgd@h
296     VK_PPC_GOT_TLSGD_HA,    // symbol@got@tlsgd@ha
297     VK_PPC_TLSGD,           // symbol@tlsgd
298     VK_PPC_GOT_TLSLD,       // symbol@got@tlsld
299     VK_PPC_GOT_TLSLD_LO,    // symbol@got@tlsld@l
300     VK_PPC_GOT_TLSLD_HI,    // symbol@got@tlsld@h
301     VK_PPC_GOT_TLSLD_HA,    // symbol@got@tlsld@ha
302     VK_PPC_GOT_PCREL,       // symbol@got@pcrel
303     VK_PPC_TLSLD,           // symbol@tlsld
304     VK_PPC_LOCAL,           // symbol@local
305     VK_PPC_NOTOC,           // symbol@notoc
306 
307     VK_COFF_IMGREL32, // symbol@imgrel (image-relative)
308 
309     VK_Hexagon_LO16,
310     VK_Hexagon_HI16,
311     VK_Hexagon_GPREL,
312     VK_Hexagon_GD_GOT,
313     VK_Hexagon_LD_GOT,
314     VK_Hexagon_GD_PLT,
315     VK_Hexagon_LD_PLT,
316     VK_Hexagon_IE,
317     VK_Hexagon_IE_GOT,
318 
319     VK_WASM_TYPEINDEX, // Reference to a symbol's type (signature)
320     VK_WASM_MBREL,     // Memory address relative to memory base
321     VK_WASM_TBREL,     // Table index relative to table bare
322 
323     VK_AMDGPU_GOTPCREL32_LO, // symbol@gotpcrel32@lo
324     VK_AMDGPU_GOTPCREL32_HI, // symbol@gotpcrel32@hi
325     VK_AMDGPU_REL32_LO,      // symbol@rel32@lo
326     VK_AMDGPU_REL32_HI,      // symbol@rel32@hi
327     VK_AMDGPU_REL64,         // symbol@rel64
328     VK_AMDGPU_ABS32_LO,      // symbol@abs32@lo
329     VK_AMDGPU_ABS32_HI,      // symbol@abs32@hi
330 
331     VK_VE_HI32,        // symbol@hi
332     VK_VE_LO32,        // symbol@lo
333     VK_VE_PC_HI32,     // symbol@pc_hi
334     VK_VE_PC_LO32,     // symbol@pc_lo
335     VK_VE_GOT_HI32,    // symbol@got_hi
336     VK_VE_GOT_LO32,    // symbol@got_lo
337     VK_VE_GOTOFF_HI32, // symbol@gotoff_hi
338     VK_VE_GOTOFF_LO32, // symbol@gotoff_lo
339     VK_VE_PLT_HI32,    // symbol@plt_hi
340     VK_VE_PLT_LO32,    // symbol@plt_lo
341     VK_VE_TLS_GD_HI32, // symbol@tls_gd_hi
342     VK_VE_TLS_GD_LO32, // symbol@tls_gd_lo
343     VK_VE_TPOFF_HI32,  // symbol@tpoff_hi
344     VK_VE_TPOFF_LO32,  // symbol@tpoff_lo
345 
346     VK_TPREL,
347     VK_DTPREL
348   };
349 
350 private:
351   /// The symbol being referenced.
352   const MCSymbol *Symbol;
353 
354   // Subclass data stores VariantKind in bits 0..15, UseParensForSymbolVariant
355   // in bit 16 and HasSubsectionsViaSymbols in bit 17.
356   static const unsigned VariantKindBits = 16;
357   static const unsigned VariantKindMask = (1 << VariantKindBits) - 1;
358 
359   /// Specifies how the variant kind should be printed.
360   static const unsigned UseParensForSymbolVariantBit = 1 << VariantKindBits;
361 
362   // FIXME: Remove this bit.
363   static const unsigned HasSubsectionsViaSymbolsBit =
364       1 << (VariantKindBits + 1);
365 
encodeSubclassData(VariantKind Kind,bool UseParensForSymbolVariant,bool HasSubsectionsViaSymbols)366   static unsigned encodeSubclassData(VariantKind Kind,
367                               bool UseParensForSymbolVariant,
368                               bool HasSubsectionsViaSymbols) {
369     return (unsigned)Kind |
370            (UseParensForSymbolVariant ? UseParensForSymbolVariantBit : 0) |
371            (HasSubsectionsViaSymbols ? HasSubsectionsViaSymbolsBit : 0);
372   }
373 
useParensForSymbolVariant()374   bool useParensForSymbolVariant() const {
375     return (getSubclassData() & UseParensForSymbolVariantBit) != 0;
376   }
377 
378   explicit MCSymbolRefExpr(const MCSymbol *Symbol, VariantKind Kind,
379                            const MCAsmInfo *MAI, SMLoc Loc = SMLoc());
380 
381 public:
382   /// \name Construction
383   /// @{
384 
create(const MCSymbol * Symbol,MCContext & Ctx)385   static const MCSymbolRefExpr *create(const MCSymbol *Symbol, MCContext &Ctx) {
386     return MCSymbolRefExpr::create(Symbol, VK_None, Ctx);
387   }
388 
389   static const MCSymbolRefExpr *create(const MCSymbol *Symbol, VariantKind Kind,
390                                        MCContext &Ctx, SMLoc Loc = SMLoc());
391   static const MCSymbolRefExpr *create(StringRef Name, VariantKind Kind,
392                                        MCContext &Ctx);
393 
394   /// @}
395   /// \name Accessors
396   /// @{
397 
getSymbol()398   const MCSymbol &getSymbol() const { return *Symbol; }
399 
getKind()400   VariantKind getKind() const {
401     return (VariantKind)(getSubclassData() & VariantKindMask);
402   }
403 
404   void printVariantKind(raw_ostream &OS) const;
405 
hasSubsectionsViaSymbols()406   bool hasSubsectionsViaSymbols() const {
407     return (getSubclassData() & HasSubsectionsViaSymbolsBit) != 0;
408   }
409 
410   /// @}
411   /// \name Static Utility Functions
412   /// @{
413 
414   static StringRef getVariantKindName(VariantKind Kind);
415 
416   static VariantKind getVariantKindForName(StringRef Name);
417 
418   /// @}
419 
classof(const MCExpr * E)420   static bool classof(const MCExpr *E) {
421     return E->getKind() == MCExpr::SymbolRef;
422   }
423 };
424 
425 /// Unary assembler expressions.
426 class MCUnaryExpr : public MCExpr {
427 public:
428   enum Opcode {
429     LNot,  ///< Logical negation.
430     Minus, ///< Unary minus.
431     Not,   ///< Bitwise negation.
432     Plus   ///< Unary plus.
433   };
434 
435 private:
436   const MCExpr *Expr;
437 
MCUnaryExpr(Opcode Op,const MCExpr * Expr,SMLoc Loc)438   MCUnaryExpr(Opcode Op, const MCExpr *Expr, SMLoc Loc)
439       : MCExpr(MCExpr::Unary, Loc, Op), Expr(Expr) {}
440 
441 public:
442   /// \name Construction
443   /// @{
444 
445   static const MCUnaryExpr *create(Opcode Op, const MCExpr *Expr,
446                                    MCContext &Ctx, SMLoc Loc = SMLoc());
447 
448   static const MCUnaryExpr *createLNot(const MCExpr *Expr, MCContext &Ctx, SMLoc Loc = SMLoc()) {
449     return create(LNot, Expr, Ctx, Loc);
450   }
451 
452   static const MCUnaryExpr *createMinus(const MCExpr *Expr, MCContext &Ctx, SMLoc Loc = SMLoc()) {
453     return create(Minus, Expr, Ctx, Loc);
454   }
455 
456   static const MCUnaryExpr *createNot(const MCExpr *Expr, MCContext &Ctx, SMLoc Loc = SMLoc()) {
457     return create(Not, Expr, Ctx, Loc);
458   }
459 
460   static const MCUnaryExpr *createPlus(const MCExpr *Expr, MCContext &Ctx, SMLoc Loc = SMLoc()) {
461     return create(Plus, Expr, Ctx, Loc);
462   }
463 
464   /// @}
465   /// \name Accessors
466   /// @{
467 
468   /// Get the kind of this unary expression.
getOpcode()469   Opcode getOpcode() const { return (Opcode)getSubclassData(); }
470 
471   /// Get the child of this unary expression.
getSubExpr()472   const MCExpr *getSubExpr() const { return Expr; }
473 
474   /// @}
475 
classof(const MCExpr * E)476   static bool classof(const MCExpr *E) {
477     return E->getKind() == MCExpr::Unary;
478   }
479 };
480 
481 /// Binary assembler expressions.
482 class MCBinaryExpr : public MCExpr {
483 public:
484   enum Opcode {
485     Add,  ///< Addition.
486     And,  ///< Bitwise and.
487     Div,  ///< Signed division.
488     EQ,   ///< Equality comparison.
489     GT,   ///< Signed greater than comparison (result is either 0 or some
490           ///< target-specific non-zero value)
491     GTE,  ///< Signed greater than or equal comparison (result is either 0 or
492           ///< some target-specific non-zero value).
493     LAnd, ///< Logical and.
494     LOr,  ///< Logical or.
495     LT,   ///< Signed less than comparison (result is either 0 or
496           ///< some target-specific non-zero value).
497     LTE,  ///< Signed less than or equal comparison (result is either 0 or
498           ///< some target-specific non-zero value).
499     Mod,  ///< Signed remainder.
500     Mul,  ///< Multiplication.
501     NE,   ///< Inequality comparison.
502     Or,   ///< Bitwise or.
503     Shl,  ///< Shift left.
504     AShr, ///< Arithmetic shift right.
505     LShr, ///< Logical shift right.
506     Sub,  ///< Subtraction.
507     Xor   ///< Bitwise exclusive or.
508   };
509 
510 private:
511   const MCExpr *LHS, *RHS;
512 
513   MCBinaryExpr(Opcode Op, const MCExpr *LHS, const MCExpr *RHS,
514                SMLoc Loc = SMLoc())
MCExpr(MCExpr::Binary,Loc,Op)515       : MCExpr(MCExpr::Binary, Loc, Op), LHS(LHS), RHS(RHS) {}
516 
517 public:
518   /// \name Construction
519   /// @{
520 
521   static const MCBinaryExpr *create(Opcode Op, const MCExpr *LHS,
522                                     const MCExpr *RHS, MCContext &Ctx,
523                                     SMLoc Loc = SMLoc());
524 
createAdd(const MCExpr * LHS,const MCExpr * RHS,MCContext & Ctx)525   static const MCBinaryExpr *createAdd(const MCExpr *LHS, const MCExpr *RHS,
526                                        MCContext &Ctx) {
527     return create(Add, LHS, RHS, Ctx);
528   }
529 
createAnd(const MCExpr * LHS,const MCExpr * RHS,MCContext & Ctx)530   static const MCBinaryExpr *createAnd(const MCExpr *LHS, const MCExpr *RHS,
531                                        MCContext &Ctx) {
532     return create(And, LHS, RHS, Ctx);
533   }
534 
createDiv(const MCExpr * LHS,const MCExpr * RHS,MCContext & Ctx)535   static const MCBinaryExpr *createDiv(const MCExpr *LHS, const MCExpr *RHS,
536                                        MCContext &Ctx) {
537     return create(Div, LHS, RHS, Ctx);
538   }
539 
createEQ(const MCExpr * LHS,const MCExpr * RHS,MCContext & Ctx)540   static const MCBinaryExpr *createEQ(const MCExpr *LHS, const MCExpr *RHS,
541                                       MCContext &Ctx) {
542     return create(EQ, LHS, RHS, Ctx);
543   }
544 
createGT(const MCExpr * LHS,const MCExpr * RHS,MCContext & Ctx)545   static const MCBinaryExpr *createGT(const MCExpr *LHS, const MCExpr *RHS,
546                                       MCContext &Ctx) {
547     return create(GT, LHS, RHS, Ctx);
548   }
549 
createGTE(const MCExpr * LHS,const MCExpr * RHS,MCContext & Ctx)550   static const MCBinaryExpr *createGTE(const MCExpr *LHS, const MCExpr *RHS,
551                                        MCContext &Ctx) {
552     return create(GTE, LHS, RHS, Ctx);
553   }
554 
createLAnd(const MCExpr * LHS,const MCExpr * RHS,MCContext & Ctx)555   static const MCBinaryExpr *createLAnd(const MCExpr *LHS, const MCExpr *RHS,
556                                         MCContext &Ctx) {
557     return create(LAnd, LHS, RHS, Ctx);
558   }
559 
createLOr(const MCExpr * LHS,const MCExpr * RHS,MCContext & Ctx)560   static const MCBinaryExpr *createLOr(const MCExpr *LHS, const MCExpr *RHS,
561                                        MCContext &Ctx) {
562     return create(LOr, LHS, RHS, Ctx);
563   }
564 
createLT(const MCExpr * LHS,const MCExpr * RHS,MCContext & Ctx)565   static const MCBinaryExpr *createLT(const MCExpr *LHS, const MCExpr *RHS,
566                                       MCContext &Ctx) {
567     return create(LT, LHS, RHS, Ctx);
568   }
569 
createLTE(const MCExpr * LHS,const MCExpr * RHS,MCContext & Ctx)570   static const MCBinaryExpr *createLTE(const MCExpr *LHS, const MCExpr *RHS,
571                                        MCContext &Ctx) {
572     return create(LTE, LHS, RHS, Ctx);
573   }
574 
createMod(const MCExpr * LHS,const MCExpr * RHS,MCContext & Ctx)575   static const MCBinaryExpr *createMod(const MCExpr *LHS, const MCExpr *RHS,
576                                        MCContext &Ctx) {
577     return create(Mod, LHS, RHS, Ctx);
578   }
579 
createMul(const MCExpr * LHS,const MCExpr * RHS,MCContext & Ctx)580   static const MCBinaryExpr *createMul(const MCExpr *LHS, const MCExpr *RHS,
581                                        MCContext &Ctx) {
582     return create(Mul, LHS, RHS, Ctx);
583   }
584 
createNE(const MCExpr * LHS,const MCExpr * RHS,MCContext & Ctx)585   static const MCBinaryExpr *createNE(const MCExpr *LHS, const MCExpr *RHS,
586                                       MCContext &Ctx) {
587     return create(NE, LHS, RHS, Ctx);
588   }
589 
createOr(const MCExpr * LHS,const MCExpr * RHS,MCContext & Ctx)590   static const MCBinaryExpr *createOr(const MCExpr *LHS, const MCExpr *RHS,
591                                       MCContext &Ctx) {
592     return create(Or, LHS, RHS, Ctx);
593   }
594 
createShl(const MCExpr * LHS,const MCExpr * RHS,MCContext & Ctx)595   static const MCBinaryExpr *createShl(const MCExpr *LHS, const MCExpr *RHS,
596                                        MCContext &Ctx) {
597     return create(Shl, LHS, RHS, Ctx);
598   }
599 
createAShr(const MCExpr * LHS,const MCExpr * RHS,MCContext & Ctx)600   static const MCBinaryExpr *createAShr(const MCExpr *LHS, const MCExpr *RHS,
601                                        MCContext &Ctx) {
602     return create(AShr, LHS, RHS, Ctx);
603   }
604 
createLShr(const MCExpr * LHS,const MCExpr * RHS,MCContext & Ctx)605   static const MCBinaryExpr *createLShr(const MCExpr *LHS, const MCExpr *RHS,
606                                        MCContext &Ctx) {
607     return create(LShr, LHS, RHS, Ctx);
608   }
609 
createSub(const MCExpr * LHS,const MCExpr * RHS,MCContext & Ctx)610   static const MCBinaryExpr *createSub(const MCExpr *LHS, const MCExpr *RHS,
611                                        MCContext &Ctx) {
612     return create(Sub, LHS, RHS, Ctx);
613   }
614 
createXor(const MCExpr * LHS,const MCExpr * RHS,MCContext & Ctx)615   static const MCBinaryExpr *createXor(const MCExpr *LHS, const MCExpr *RHS,
616                                        MCContext &Ctx) {
617     return create(Xor, LHS, RHS, Ctx);
618   }
619 
620   /// @}
621   /// \name Accessors
622   /// @{
623 
624   /// Get the kind of this binary expression.
getOpcode()625   Opcode getOpcode() const { return (Opcode)getSubclassData(); }
626 
627   /// Get the left-hand side expression of the binary operator.
getLHS()628   const MCExpr *getLHS() const { return LHS; }
629 
630   /// Get the right-hand side expression of the binary operator.
getRHS()631   const MCExpr *getRHS() const { return RHS; }
632 
633   /// @}
634 
classof(const MCExpr * E)635   static bool classof(const MCExpr *E) {
636     return E->getKind() == MCExpr::Binary;
637   }
638 };
639 
640 /// This is an extension point for target-specific MCExpr subclasses to
641 /// implement.
642 ///
643 /// NOTE: All subclasses are required to have trivial destructors because
644 /// MCExprs are bump pointer allocated and not destructed.
645 class MCTargetExpr : public MCExpr {
646   virtual void anchor();
647 
648 protected:
MCTargetExpr()649   MCTargetExpr() : MCExpr(Target, SMLoc()) {}
650   virtual ~MCTargetExpr() = default;
651 
652 public:
653   virtual void printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const = 0;
654   virtual bool evaluateAsRelocatableImpl(MCValue &Res,
655                                          const MCAsmLayout *Layout,
656                                          const MCFixup *Fixup) const = 0;
657   // allow Target Expressions to be checked for equality
isEqualTo(const MCExpr * x)658   virtual bool isEqualTo(const MCExpr *x) const { return false; }
659   // This should be set when assigned expressions are not valid ".set"
660   // expressions, e.g. registers, and must be inlined.
inlineAssignedExpr()661   virtual bool inlineAssignedExpr() const { return false; }
662   virtual void visitUsedExpr(MCStreamer& Streamer) const = 0;
663   virtual MCFragment *findAssociatedFragment() const = 0;
664 
665   virtual void fixELFSymbolsInTLSFixups(MCAssembler &) const = 0;
666 
classof(const MCExpr * E)667   static bool classof(const MCExpr *E) {
668     return E->getKind() == MCExpr::Target;
669   }
670 };
671 
672 } // end namespace llvm
673 
674 #endif // LLVM_MC_MCEXPR_H
675