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