1//===-- AArch64MCAsmInfo.cpp - AArch64 asm properties ---------------------===//
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 contains the declarations of the AArch64MCAsmInfo properties.
10//
11//===----------------------------------------------------------------------===//
12
13#include "AArch64MCAsmInfo.h"
14#include "AArch64MCOptions.h"
15#include "llvm/ADT/Enum.h"
16#include "llvm/MC/MCContext.h"
17#include "llvm/MC/MCExpr.h"
18#include "llvm/MC/MCStreamer.h"
19#include "llvm/MC/MCValue.h"
20#include "llvm/TargetParser/Triple.h"
21using namespace llvm;
22
23constexpr EnumStringDef<MCAsmInfo::AtSpecifierKind> COFFAtSpecifierDefs[] = {
24 {.Names: {"IMGREL"}, .Value: MCSymbolRefExpr::VK_COFF_IMGREL32},
25 {.Names: {"PAGEOFF"}, .Value: AArch64::S_MACHO_PAGEOFF},
26};
27constexpr auto COFFAtSpecifiers = BUILD_ENUM_STRINGS(COFFAtSpecifierDefs);
28
29constexpr EnumStringDef<MCAsmInfo::AtSpecifierKind> ELFAtSpecifierDefs[] = {
30 {.Names: {"GOT"}, .Value: AArch64::S_GOT},
31};
32constexpr auto ELFAtSpecifiers = BUILD_ENUM_STRINGS(ELFAtSpecifierDefs);
33
34constexpr EnumStringDef<MCAsmInfo::AtSpecifierKind> MachOAtSpecifierDefs[] = {
35 {.Names: {"GOT"}, .Value: AArch64::S_MACHO_GOT},
36 {.Names: {"GOTPAGE"}, .Value: AArch64::S_MACHO_GOTPAGE},
37 {.Names: {"GOTPAGEOFF"}, .Value: AArch64::S_MACHO_GOTPAGEOFF},
38 {.Names: {"PAGE"}, .Value: AArch64::S_MACHO_PAGE},
39 {.Names: {"PAGEOFF"}, .Value: AArch64::S_MACHO_PAGEOFF},
40 {.Names: {"TLVP"}, .Value: AArch64::S_MACHO_TLVP},
41 {.Names: {"TLVPPAGE"}, .Value: AArch64::S_MACHO_TLVPPAGE},
42 {.Names: {"TLVPPAGEOFF"}, .Value: AArch64::S_MACHO_TLVPPAGEOFF},
43};
44constexpr auto MachOAtSpecifiers = BUILD_ENUM_STRINGS(MachOAtSpecifierDefs);
45
46StringRef AArch64::getSpecifierName(AArch64::Specifier S) {
47 // clang-format off
48 switch (static_cast<uint32_t>(S)) {
49 case AArch64::S_CALL: return "";
50 case AArch64::S_LO12: return ":lo12:";
51 case AArch64::S_ABS_G3: return ":abs_g3:";
52 case AArch64::S_ABS_G2: return ":abs_g2:";
53 case AArch64::S_ABS_G2_S: return ":abs_g2_s:";
54 case AArch64::S_ABS_G2_NC: return ":abs_g2_nc:";
55 case AArch64::S_ABS_G1: return ":abs_g1:";
56 case AArch64::S_ABS_G1_S: return ":abs_g1_s:";
57 case AArch64::S_ABS_G1_NC: return ":abs_g1_nc:";
58 case AArch64::S_ABS_G0: return ":abs_g0:";
59 case AArch64::S_ABS_G0_S: return ":abs_g0_s:";
60 case AArch64::S_ABS_G0_NC: return ":abs_g0_nc:";
61 case AArch64::S_PREL_G3: return ":prel_g3:";
62 case AArch64::S_PREL_G2: return ":prel_g2:";
63 case AArch64::S_PREL_G2_NC: return ":prel_g2_nc:";
64 case AArch64::S_PREL_G1: return ":prel_g1:";
65 case AArch64::S_PREL_G1_NC: return ":prel_g1_nc:";
66 case AArch64::S_PREL_G0: return ":prel_g0:";
67 case AArch64::S_PREL_G0_NC: return ":prel_g0_nc:";
68 case AArch64::S_DTPREL_G2: return ":dtprel_g2:";
69 case AArch64::S_DTPREL_G1: return ":dtprel_g1:";
70 case AArch64::S_DTPREL_G1_NC: return ":dtprel_g1_nc:";
71 case AArch64::S_DTPREL_G0: return ":dtprel_g0:";
72 case AArch64::S_DTPREL_G0_NC: return ":dtprel_g0_nc:";
73 case AArch64::S_DTPREL_HI12: return ":dtprel_hi12:";
74 case AArch64::S_DTPREL_LO12: return ":dtprel_lo12:";
75 case AArch64::S_DTPREL_LO12_NC: return ":dtprel_lo12_nc:";
76 case AArch64::S_TPREL_G2: return ":tprel_g2:";
77 case AArch64::S_TPREL_G1: return ":tprel_g1:";
78 case AArch64::S_TPREL_G1_NC: return ":tprel_g1_nc:";
79 case AArch64::S_TPREL_G0: return ":tprel_g0:";
80 case AArch64::S_TPREL_G0_NC: return ":tprel_g0_nc:";
81 case AArch64::S_TPREL_HI12: return ":tprel_hi12:";
82 case AArch64::S_TPREL_LO12: return ":tprel_lo12:";
83 case AArch64::S_TPREL_LO12_NC: return ":tprel_lo12_nc:";
84 case AArch64::S_TLSDESC_LO12: return ":tlsdesc_lo12:";
85 case AArch64::S_TLSDESC_AUTH_LO12: return ":tlsdesc_auth_lo12:";
86 case AArch64::S_ABS_PAGE: return "";
87 case AArch64::S_ABS_PAGE_NC: return ":pg_hi21_nc:";
88 case AArch64::S_GOT: return ":got:";
89 case AArch64::S_GOT_PAGE: return ":got:";
90 case AArch64::S_GOT_PAGE_LO15: return ":gotpage_lo15:";
91 case AArch64::S_GOT_LO12: return ":got_lo12:";
92 case AArch64::S_GOTTPREL: return ":gottprel:";
93 case AArch64::S_GOTTPREL_PAGE: return ":gottprel:";
94 case AArch64::S_GOTTPREL_LO12_NC: return ":gottprel_lo12:";
95 case AArch64::S_GOTTPREL_G1: return ":gottprel_g1:";
96 case AArch64::S_GOTTPREL_G0_NC: return ":gottprel_g0_nc:";
97 case AArch64::S_TLSDESC: return "";
98 case AArch64::S_TLSDESC_PAGE: return ":tlsdesc:";
99 case AArch64::S_TLSDESC_AUTH: return "";
100 case AArch64::S_TLSDESC_AUTH_PAGE: return ":tlsdesc_auth:";
101 case AArch64::S_SECREL_LO12: return ":secrel_lo12:";
102 case AArch64::S_SECREL_HI12: return ":secrel_hi12:";
103 case AArch64::S_GOT_AUTH: return ":got_auth:";
104 case AArch64::S_GOT_AUTH_PAGE: return ":got_auth:";
105 case AArch64::S_GOT_AUTH_LO12: return ":got_auth_lo12:";
106
107 case AArch64::S_GOTPCREL: return "%gotpcrel";
108 case AArch64::S_PLT: return "%pltpcrel";
109 case AArch64::S_DTPREL: return "%dtprel";
110 case AArch64::S_FUNCINIT: return "%funcinit";
111 default:
112 llvm_unreachable("Invalid relocation specifier");
113 }
114 // clang-format on
115}
116
117AArch64::Specifier AArch64::parsePercentSpecifierName(StringRef name) {
118 return StringSwitch<AArch64::Specifier>(name)
119 .Case(S: "pltpcrel", Value: AArch64::S_PLT)
120 .Case(S: "gotpcrel", Value: AArch64::S_GOTPCREL)
121 .Case(S: "dtprel", Value: AArch64::S_DTPREL)
122 .Case(S: "funcinit", Value: AArch64::S_FUNCINIT)
123 .Default(Value: 0);
124}
125
126static bool evaluate(const MCSpecifierExpr &Expr, MCValue &Res,
127 const MCAssembler *Asm) {
128 if (!Expr.getSubExpr()->evaluateAsRelocatable(Res, Asm))
129 return false;
130 Res.setSpecifier(Expr.getSpecifier());
131 return !Res.getSubSym();
132}
133
134AArch64MCAsmInfoDarwin::AArch64MCAsmInfoDarwin(bool IsILP32,
135 const MCTargetOptions &Options)
136 : MCAsmInfoDarwin(Options) {
137 // We prefer NEON instructions to be printed in the short, Apple-specific
138 // form when targeting Darwin.
139 AssemblerDialect =
140 AArch64_MC::getCLOpts(Options).neon_syntax.value_or(u: AArch64::Apple);
141
142 InternalSymbolPrefix = "L";
143 SeparatorString = "%%";
144 CommentString = ";";
145 CalleeSaveStackSlotSize = 8;
146 CodePointerSize = IsILP32 ? 4 : 8;
147
148 AlignmentIsInBytes = false;
149 UsesELFSectionDirectiveForBSS = true;
150 SupportsDebugInformation = true;
151 UseDataRegionDirectives = true;
152
153 initializeAtSpecifiers(MachOAtSpecifiers);
154 UseAtForSpecifier = false;
155}
156
157const MCExpr *AArch64MCAsmInfoDarwin::getExprForPersonalitySymbol(
158 const MCSymbol *Sym, unsigned Encoding, MCStreamer &Streamer) const {
159 // On Darwin, we can reference dwarf symbols with foo@GOT-., which
160 // is an indirect pc-relative reference. The default implementation
161 // won't reference using the GOT, so we need this target-specific
162 // version.
163 MCContext &Context = Streamer.getContext();
164 const MCExpr *Res =
165 MCSymbolRefExpr::create(Symbol: Sym, specifier: AArch64::S_MACHO_GOT, Ctx&: Context);
166 MCSymbol *PCSym = Context.createTempSymbol();
167 Streamer.emitLabel(Symbol: PCSym);
168 const MCExpr *PC = MCSymbolRefExpr::create(Symbol: PCSym, Ctx&: Context);
169 return MCBinaryExpr::createSub(LHS: Res, RHS: PC, Ctx&: Context);
170}
171
172void AArch64AuthMCExpr::print(raw_ostream &OS, const MCAsmInfo *MAI) const {
173 bool WrapSubExprInParens = !isa<MCSymbolRefExpr>(Val: getSubExpr());
174 if (WrapSubExprInParens)
175 OS << '(';
176 MAI->printExpr(OS, *getSubExpr());
177 if (WrapSubExprInParens)
178 OS << ')';
179
180 OS << "@AUTH(" << AArch64PACKeyIDToString(KeyID: Key) << ',' << Discriminator;
181 if (hasAddressDiversity())
182 OS << ",addr";
183 OS << ')';
184}
185
186void AArch64MCAsmInfoDarwin::printSpecifierExpr(
187 raw_ostream &OS, const MCSpecifierExpr &Expr) const {
188 if (auto *AE = dyn_cast<AArch64AuthMCExpr>(Val: &Expr))
189 return AE->print(OS, MAI: this);
190 OS << AArch64::getSpecifierName(S: Expr.getSpecifier());
191 printExpr(OS, *Expr.getSubExpr());
192}
193
194bool AArch64MCAsmInfoDarwin::evaluateAsRelocatableImpl(
195 const MCSpecifierExpr &Expr, MCValue &Res, const MCAssembler *Asm) const {
196 return evaluate(Expr, Res, Asm);
197}
198
199AArch64MCAsmInfoELF::AArch64MCAsmInfoELF(const Triple &T,
200 const MCTargetOptions &Options)
201 : MCAsmInfoELF(Options) {
202 if (T.getArch() == Triple::aarch64_be)
203 IsLittleEndian = false;
204
205 // We prefer NEON instructions to be printed in the generic form when
206 // targeting ELF.
207 AssemblerDialect =
208 AArch64_MC::getCLOpts(Options).neon_syntax.value_or(u: AArch64::Generic);
209
210 CodePointerSize = T.getEnvironment() == Triple::GNUILP32 ? 4 : 8;
211
212 // ".comm align is in bytes but .align is pow-2."
213 AlignmentIsInBytes = false;
214
215 CommentString = "//";
216 InternalSymbolPrefix = ".L";
217
218 Data16bitsDirective = "\t.hword\t";
219 Data32bitsDirective = "\t.word\t";
220 Data64bitsDirective = "\t.xword\t";
221
222 UseDataRegionDirectives = false;
223
224 WeakRefDirective = "\t.weak\t";
225
226 SupportsDebugInformation = true;
227
228 HasIdentDirective = true;
229
230 initializeAtSpecifiers(ELFAtSpecifiers);
231 UseAtForSpecifier = false;
232}
233
234void AArch64MCAsmInfoELF::printSpecifierExpr(
235 raw_ostream &OS, const MCSpecifierExpr &Expr) const {
236 if (auto *AE = dyn_cast<AArch64AuthMCExpr>(Val: &Expr))
237 return AE->print(OS, MAI: this);
238 auto Str = AArch64::getSpecifierName(S: Expr.getSpecifier());
239 OS << Str;
240 if (!Str.empty() && Str[0] == '%')
241 OS << '(';
242 printExpr(OS, *Expr.getSubExpr());
243 if (!Str.empty() && Str[0] == '%')
244 OS << ')';
245}
246
247bool AArch64MCAsmInfoELF::evaluateAsRelocatableImpl(
248 const MCSpecifierExpr &Expr, MCValue &Res, const MCAssembler *Asm) const {
249 return evaluate(Expr, Res, Asm);
250}
251
252AArch64MCAsmInfoMicrosoftCOFF::AArch64MCAsmInfoMicrosoftCOFF(
253 const MCTargetOptions &Options)
254 : MCAsmInfoMicrosoft(Options) {
255 InternalSymbolPrefix = ".L";
256
257 Data16bitsDirective = "\t.hword\t";
258 Data32bitsDirective = "\t.word\t";
259 Data64bitsDirective = "\t.xword\t";
260
261 AlignmentIsInBytes = false;
262 SupportsDebugInformation = true;
263 CodePointerSize = 8;
264
265 CommentString = "//";
266 WinEHEncodingType = WinEH::EncodingType::Itanium;
267
268 initializeAtSpecifiers(COFFAtSpecifiers);
269}
270
271void AArch64MCAsmInfoMicrosoftCOFF::printSpecifierExpr(
272 raw_ostream &OS, const MCSpecifierExpr &Expr) const {
273 OS << AArch64::getSpecifierName(S: Expr.getSpecifier());
274 printExpr(OS, *Expr.getSubExpr());
275}
276
277bool AArch64MCAsmInfoMicrosoftCOFF::evaluateAsRelocatableImpl(
278 const MCSpecifierExpr &Expr, MCValue &Res, const MCAssembler *Asm) const {
279 return evaluate(Expr, Res, Asm);
280}
281
282AArch64MCAsmInfoGNUCOFF::AArch64MCAsmInfoGNUCOFF(const MCTargetOptions &Options)
283 : MCAsmInfoGNUCOFF(Options) {
284 InternalSymbolPrefix = ".L";
285
286 Data16bitsDirective = "\t.hword\t";
287 Data32bitsDirective = "\t.word\t";
288 Data64bitsDirective = "\t.xword\t";
289
290 AlignmentIsInBytes = false;
291 SupportsDebugInformation = true;
292 CodePointerSize = 8;
293
294 CommentString = "//";
295 WinEHEncodingType = WinEH::EncodingType::Itanium;
296
297 initializeAtSpecifiers(COFFAtSpecifiers);
298}
299
300void AArch64MCAsmInfoGNUCOFF::printSpecifierExpr(
301 raw_ostream &OS, const MCSpecifierExpr &Expr) const {
302 OS << AArch64::getSpecifierName(S: Expr.getSpecifier());
303 printExpr(OS, *Expr.getSubExpr());
304}
305
306bool AArch64MCAsmInfoGNUCOFF::evaluateAsRelocatableImpl(
307 const MCSpecifierExpr &Expr, MCValue &Res, const MCAssembler *Asm) const {
308 return evaluate(Expr, Res, Asm);
309}
310