xref: /freebsd/contrib/llvm-project/llvm/lib/Target/AArch64/MCTargetDesc/AArch64MCExpr.h (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
1 //=--- AArch64MCExpr.h - AArch64 specific MC expression classes ---*- 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 // This file describes AArch64-specific MCExprs, used for modifiers like
10 // ":lo12:" or ":gottprel_g1:".
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_LIB_TARGET_AARCH64_MCTARGETDESC_AARCH64MCEXPR_H
15 #define LLVM_LIB_TARGET_AARCH64_MCTARGETDESC_AARCH64MCEXPR_H
16 
17 #include "Utils/AArch64BaseInfo.h"
18 #include "llvm/MC/MCExpr.h"
19 #include "llvm/Support/Casting.h"
20 #include "llvm/Support/ErrorHandling.h"
21 
22 namespace llvm {
23 
24 class AArch64MCExpr : public MCTargetExpr {
25 public:
26   enum VariantKind {
27     // Symbol locations specifying (roughly speaking) what calculation should be
28     // performed to construct the final address for the relocated
29     // symbol. E.g. direct, via the GOT, ...
30     VK_ABS      = 0x001,
31     VK_SABS     = 0x002,
32     VK_PREL     = 0x003,
33     VK_GOT      = 0x004,
34     VK_DTPREL   = 0x005,
35     VK_GOTTPREL = 0x006,
36     VK_TPREL    = 0x007,
37     VK_TLSDESC  = 0x008,
38     VK_SECREL   = 0x009,
39     VK_AUTH     = 0x00a,
40     VK_AUTHADDR = 0x00b,
41     VK_SymLocBits = 0x00f,
42 
43     // Variants specifying which part of the final address calculation is
44     // used. E.g. the low 12 bits for an ADD/LDR, the middle 16 bits for a
45     // MOVZ/MOVK.
46     VK_PAGE     = 0x010,
47     VK_PAGEOFF  = 0x020,
48     VK_HI12     = 0x030,
49     VK_G0       = 0x040,
50     VK_G1       = 0x050,
51     VK_G2       = 0x060,
52     VK_G3       = 0x070,
53     VK_LO15     = 0x080,
54     VK_AddressFragBits = 0x0f0,
55 
56     // Whether the final relocation is a checked one (where a linker should
57     // perform a range-check on the final address) or not. Note that this field
58     // is unfortunately sometimes omitted from the assembly syntax. E.g. :lo12:
59     // on its own is a non-checked relocation. We side with ELF on being
60     // explicit about this!
61     VK_NC       = 0x100,
62 
63     // Convenience definitions for referring to specific textual representations
64     // of relocation specifiers. Note that this means the "_NC" is sometimes
65     // omitted in line with assembly syntax here (VK_LO12 rather than VK_LO12_NC
66     // since a user would write ":lo12:").
67     VK_CALL              = VK_ABS,
68     VK_ABS_PAGE          = VK_ABS      | VK_PAGE,
69     VK_ABS_PAGE_NC       = VK_ABS      | VK_PAGE    | VK_NC,
70     VK_ABS_G3            = VK_ABS      | VK_G3,
71     VK_ABS_G2            = VK_ABS      | VK_G2,
72     VK_ABS_G2_S          = VK_SABS     | VK_G2,
73     VK_ABS_G2_NC         = VK_ABS      | VK_G2      | VK_NC,
74     VK_ABS_G1            = VK_ABS      | VK_G1,
75     VK_ABS_G1_S          = VK_SABS     | VK_G1,
76     VK_ABS_G1_NC         = VK_ABS      | VK_G1      | VK_NC,
77     VK_ABS_G0            = VK_ABS      | VK_G0,
78     VK_ABS_G0_S          = VK_SABS     | VK_G0,
79     VK_ABS_G0_NC         = VK_ABS      | VK_G0      | VK_NC,
80     VK_LO12              = VK_ABS      | VK_PAGEOFF | VK_NC,
81     VK_PREL_G3           = VK_PREL     | VK_G3,
82     VK_PREL_G2           = VK_PREL     | VK_G2,
83     VK_PREL_G2_NC        = VK_PREL     | VK_G2      | VK_NC,
84     VK_PREL_G1           = VK_PREL     | VK_G1,
85     VK_PREL_G1_NC        = VK_PREL     | VK_G1      | VK_NC,
86     VK_PREL_G0           = VK_PREL     | VK_G0,
87     VK_PREL_G0_NC        = VK_PREL     | VK_G0      | VK_NC,
88     VK_GOT_LO12          = VK_GOT      | VK_PAGEOFF | VK_NC,
89     VK_GOT_PAGE          = VK_GOT      | VK_PAGE,
90     VK_GOT_PAGE_LO15     = VK_GOT      | VK_LO15    | VK_NC,
91     VK_DTPREL_G2         = VK_DTPREL   | VK_G2,
92     VK_DTPREL_G1         = VK_DTPREL   | VK_G1,
93     VK_DTPREL_G1_NC      = VK_DTPREL   | VK_G1      | VK_NC,
94     VK_DTPREL_G0         = VK_DTPREL   | VK_G0,
95     VK_DTPREL_G0_NC      = VK_DTPREL   | VK_G0      | VK_NC,
96     VK_DTPREL_HI12       = VK_DTPREL   | VK_HI12,
97     VK_DTPREL_LO12       = VK_DTPREL   | VK_PAGEOFF,
98     VK_DTPREL_LO12_NC    = VK_DTPREL   | VK_PAGEOFF | VK_NC,
99     VK_GOTTPREL_PAGE     = VK_GOTTPREL | VK_PAGE,
100     VK_GOTTPREL_LO12_NC  = VK_GOTTPREL | VK_PAGEOFF | VK_NC,
101     VK_GOTTPREL_G1       = VK_GOTTPREL | VK_G1,
102     VK_GOTTPREL_G0_NC    = VK_GOTTPREL | VK_G0      | VK_NC,
103     VK_TPREL_G2          = VK_TPREL    | VK_G2,
104     VK_TPREL_G1          = VK_TPREL    | VK_G1,
105     VK_TPREL_G1_NC       = VK_TPREL    | VK_G1      | VK_NC,
106     VK_TPREL_G0          = VK_TPREL    | VK_G0,
107     VK_TPREL_G0_NC       = VK_TPREL    | VK_G0      | VK_NC,
108     VK_TPREL_HI12        = VK_TPREL    | VK_HI12,
109     VK_TPREL_LO12        = VK_TPREL    | VK_PAGEOFF,
110     VK_TPREL_LO12_NC     = VK_TPREL    | VK_PAGEOFF | VK_NC,
111     VK_TLSDESC_LO12      = VK_TLSDESC  | VK_PAGEOFF,
112     VK_TLSDESC_PAGE      = VK_TLSDESC  | VK_PAGE,
113     VK_SECREL_LO12       = VK_SECREL   | VK_PAGEOFF,
114     VK_SECREL_HI12       = VK_SECREL   | VK_HI12,
115 
116     VK_INVALID  = 0xfff
117   };
118 
119 private:
120   const MCExpr *Expr;
121   const VariantKind Kind;
122 
123 protected:
AArch64MCExpr(const MCExpr * Expr,VariantKind Kind)124   explicit AArch64MCExpr(const MCExpr *Expr, VariantKind Kind)
125     : Expr(Expr), Kind(Kind) {}
126 
127 public:
128   /// @name Construction
129   /// @{
130 
131   static const AArch64MCExpr *create(const MCExpr *Expr, VariantKind Kind,
132                                    MCContext &Ctx);
133 
134   /// @}
135   /// @name Accessors
136   /// @{
137 
138   /// Get the kind of this expression.
getKind()139   VariantKind getKind() const { return Kind; }
140 
141   /// Get the expression this modifier applies to.
getSubExpr()142   const MCExpr *getSubExpr() const { return Expr; }
143 
144   /// @}
145   /// @name VariantKind information extractors.
146   /// @{
147 
getSymbolLoc(VariantKind Kind)148   static VariantKind getSymbolLoc(VariantKind Kind) {
149     return static_cast<VariantKind>(Kind & VK_SymLocBits);
150   }
151 
getAddressFrag(VariantKind Kind)152   static VariantKind getAddressFrag(VariantKind Kind) {
153     return static_cast<VariantKind>(Kind & VK_AddressFragBits);
154   }
155 
isNotChecked(VariantKind Kind)156   static bool isNotChecked(VariantKind Kind) { return Kind & VK_NC; }
157 
158   /// @}
159 
160   /// Convert the variant kind into an ELF-appropriate modifier
161   /// (e.g. ":got:", ":lo12:").
162   StringRef getVariantKindName() const;
163 
164   void printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const override;
165 
166   void visitUsedExpr(MCStreamer &Streamer) const override;
167 
168   MCFragment *findAssociatedFragment() const override;
169 
170   bool evaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm,
171                                  const MCFixup *Fixup) const override;
172 
173   void fixELFSymbolsInTLSFixups(MCAssembler &Asm) const override;
174 
classof(const MCExpr * E)175   static bool classof(const MCExpr *E) {
176     return E->getKind() == MCExpr::Target;
177   }
178 };
179 
180 class AArch64AuthMCExpr final : public AArch64MCExpr {
181   uint16_t Discriminator;
182   AArch64PACKey::ID Key;
183 
AArch64AuthMCExpr(const MCExpr * Expr,uint16_t Discriminator,AArch64PACKey::ID Key,bool HasAddressDiversity)184   explicit AArch64AuthMCExpr(const MCExpr *Expr, uint16_t Discriminator,
185                              AArch64PACKey::ID Key, bool HasAddressDiversity)
186       : AArch64MCExpr(Expr, HasAddressDiversity ? VK_AUTHADDR : VK_AUTH),
187         Discriminator(Discriminator), Key(Key) {}
188 
189 public:
190   static const AArch64AuthMCExpr *
191   create(const MCExpr *Expr, uint16_t Discriminator, AArch64PACKey::ID Key,
192          bool HasAddressDiversity, MCContext &Ctx);
193 
getKey()194   AArch64PACKey::ID getKey() const { return Key; }
getDiscriminator()195   uint16_t getDiscriminator() const { return Discriminator; }
hasAddressDiversity()196   bool hasAddressDiversity() const { return getKind() == VK_AUTHADDR; }
197 
198   void printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const override;
199 
200   void visitUsedExpr(MCStreamer &Streamer) const override;
201 
202   MCFragment *findAssociatedFragment() const override;
203 
204   bool evaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm,
205                                  const MCFixup *Fixup) const override;
206 
classof(const MCExpr * E)207   static bool classof(const MCExpr *E) {
208     return isa<AArch64MCExpr>(E) && classof(cast<AArch64MCExpr>(E));
209   }
210 
classof(const AArch64MCExpr * E)211   static bool classof(const AArch64MCExpr *E) {
212     return E->getKind() == VK_AUTH || E->getKind() == VK_AUTHADDR;
213   }
214 };
215 } // end namespace llvm
216 
217 #endif
218