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 // We prefer NEON instructions to be printed in the short, Apple-specific
144 // form when targeting Darwin.
145 AssemblerDialect = AsmWriterVariant == Default ? Apple : AsmWriterVariant;
146
147 InternalSymbolPrefix = "L";
148 PrivateLabelPrefix = "L";
149 SeparatorString = "%%";
150 CommentString = ";";
151 CalleeSaveStackSlotSize = 8;
152 CodePointerSize = IsILP32 ? 4 : 8;
153
154 AlignmentIsInBytes = false;
155 UsesELFSectionDirectiveForBSS = true;
156 SupportsDebugInformation = true;
157 UseDataRegionDirectives = true;
158 ExceptionsType = ExceptionHandling::DwarfCFI;
159
160 initializeAtSpecifiers(MachOAtSpecifiers);
161 UseAtForSpecifier = false;
162}
163
164const MCExpr *AArch64MCAsmInfoDarwin::getExprForPersonalitySymbol(
165 const MCSymbol *Sym, unsigned Encoding, MCStreamer &Streamer) const {
166 // On Darwin, we can reference dwarf symbols with foo@GOT-., which
167 // is an indirect pc-relative reference. The default implementation
168 // won't reference using the GOT, so we need this target-specific
169 // version.
170 MCContext &Context = Streamer.getContext();
171 const MCExpr *Res =
172 MCSymbolRefExpr::create(Symbol: Sym, specifier: AArch64::S_MACHO_GOT, Ctx&: Context);
173 MCSymbol *PCSym = Context.createTempSymbol();
174 Streamer.emitLabel(Symbol: PCSym);
175 const MCExpr *PC = MCSymbolRefExpr::create(Symbol: PCSym, Ctx&: Context);
176 return MCBinaryExpr::createSub(LHS: Res, RHS: PC, Ctx&: Context);
177}
178
179void AArch64AuthMCExpr::print(raw_ostream &OS, const MCAsmInfo *MAI) const {
180 bool WrapSubExprInParens = !isa<MCSymbolRefExpr>(Val: getSubExpr());
181 if (WrapSubExprInParens)
182 OS << '(';
183 MAI->printExpr(OS, *getSubExpr());
184 if (WrapSubExprInParens)
185 OS << ')';
186
187 OS << "@AUTH(" << AArch64PACKeyIDToString(KeyID: Key) << ',' << Discriminator;
188 if (hasAddressDiversity())
189 OS << ",addr";
190 OS << ')';
191}
192
193void AArch64MCAsmInfoDarwin::printSpecifierExpr(
194 raw_ostream &OS, const MCSpecifierExpr &Expr) const {
195 if (auto *AE = dyn_cast<AArch64AuthMCExpr>(Val: &Expr))
196 return AE->print(OS, MAI: this);
197 OS << AArch64::getSpecifierName(S: Expr.getSpecifier());
198 printExpr(OS, *Expr.getSubExpr());
199}
200
201bool AArch64MCAsmInfoDarwin::evaluateAsRelocatableImpl(
202 const MCSpecifierExpr &Expr, MCValue &Res, const MCAssembler *Asm) const {
203 return evaluate(Expr, Res, Asm);
204}
205
206AArch64MCAsmInfoELF::AArch64MCAsmInfoELF(const Triple &T) {
207 if (T.getArch() == Triple::aarch64_be)
208 IsLittleEndian = false;
209
210 // We prefer NEON instructions to be printed in the generic form when
211 // targeting ELF.
212 AssemblerDialect = AsmWriterVariant == Default ? Generic : AsmWriterVariant;
213
214 CodePointerSize = T.getEnvironment() == Triple::GNUILP32 ? 4 : 8;
215
216 // ".comm align is in bytes but .align is pow-2."
217 AlignmentIsInBytes = false;
218
219 CommentString = "//";
220 InternalSymbolPrefix = ".L";
221 PrivateLabelPrefix = ".L";
222
223 Data16bitsDirective = "\t.hword\t";
224 Data32bitsDirective = "\t.word\t";
225 Data64bitsDirective = "\t.xword\t";
226
227 UseDataRegionDirectives = false;
228
229 WeakRefDirective = "\t.weak\t";
230
231 SupportsDebugInformation = true;
232
233 // Exceptions handling
234 ExceptionsType = ExceptionHandling::DwarfCFI;
235
236 HasIdentDirective = true;
237
238 initializeAtSpecifiers(ELFAtSpecifiers);
239 UseAtForSpecifier = false;
240}
241
242void AArch64MCAsmInfoELF::printSpecifierExpr(
243 raw_ostream &OS, const MCSpecifierExpr &Expr) const {
244 if (auto *AE = dyn_cast<AArch64AuthMCExpr>(Val: &Expr))
245 return AE->print(OS, MAI: this);
246 auto Str = AArch64::getSpecifierName(S: Expr.getSpecifier());
247 OS << Str;
248 if (!Str.empty() && Str[0] == '%')
249 OS << '(';
250 printExpr(OS, *Expr.getSubExpr());
251 if (!Str.empty() && Str[0] == '%')
252 OS << ')';
253}
254
255bool AArch64MCAsmInfoELF::evaluateAsRelocatableImpl(
256 const MCSpecifierExpr &Expr, MCValue &Res, const MCAssembler *Asm) const {
257 return evaluate(Expr, Res, Asm);
258}
259
260AArch64MCAsmInfoMicrosoftCOFF::AArch64MCAsmInfoMicrosoftCOFF() {
261 InternalSymbolPrefix = ".L";
262 PrivateLabelPrefix = ".L";
263
264 Data16bitsDirective = "\t.hword\t";
265 Data32bitsDirective = "\t.word\t";
266 Data64bitsDirective = "\t.xword\t";
267
268 AlignmentIsInBytes = false;
269 SupportsDebugInformation = true;
270 CodePointerSize = 8;
271
272 CommentString = "//";
273 ExceptionsType = ExceptionHandling::WinEH;
274 WinEHEncodingType = WinEH::EncodingType::Itanium;
275
276 initializeAtSpecifiers(COFFAtSpecifiers);
277}
278
279void AArch64MCAsmInfoMicrosoftCOFF::printSpecifierExpr(
280 raw_ostream &OS, const MCSpecifierExpr &Expr) const {
281 OS << AArch64::getSpecifierName(S: Expr.getSpecifier());
282 printExpr(OS, *Expr.getSubExpr());
283}
284
285bool AArch64MCAsmInfoMicrosoftCOFF::evaluateAsRelocatableImpl(
286 const MCSpecifierExpr &Expr, MCValue &Res, const MCAssembler *Asm) const {
287 return evaluate(Expr, Res, Asm);
288}
289
290AArch64MCAsmInfoGNUCOFF::AArch64MCAsmInfoGNUCOFF() {
291 InternalSymbolPrefix = ".L";
292 PrivateLabelPrefix = ".L";
293
294 Data16bitsDirective = "\t.hword\t";
295 Data32bitsDirective = "\t.word\t";
296 Data64bitsDirective = "\t.xword\t";
297
298 AlignmentIsInBytes = false;
299 SupportsDebugInformation = true;
300 CodePointerSize = 8;
301
302 CommentString = "//";
303 ExceptionsType = ExceptionHandling::WinEH;
304 WinEHEncodingType = WinEH::EncodingType::Itanium;
305
306 initializeAtSpecifiers(COFFAtSpecifiers);
307}
308
309void AArch64MCAsmInfoGNUCOFF::printSpecifierExpr(
310 raw_ostream &OS, const MCSpecifierExpr &Expr) const {
311 OS << AArch64::getSpecifierName(S: Expr.getSpecifier());
312 printExpr(OS, *Expr.getSubExpr());
313}
314
315bool AArch64MCAsmInfoGNUCOFF::evaluateAsRelocatableImpl(
316 const MCSpecifierExpr &Expr, MCValue &Res, const MCAssembler *Asm) const {
317 return evaluate(Expr, Res, Asm);
318}
319