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