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