1//===-- X86MCAsmInfo.cpp - X86 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 X86MCAsmInfo properties.
10//
11//===----------------------------------------------------------------------===//
12
13#include "X86MCAsmInfo.h"
14#include "llvm/MC/MCExpr.h"
15#include "llvm/MC/MCStreamer.h"
16#include "llvm/Support/CommandLine.h"
17#include "llvm/TargetParser/Triple.h"
18using namespace llvm;
19
20enum AsmWriterFlavorTy {
21 // Note: This numbering has to match the GCC assembler dialects for inline
22 // asm alternatives to work right.
23 ATT = 0, Intel = 1
24};
25
26static cl::opt<AsmWriterFlavorTy> X86AsmSyntax(
27 "x86-asm-syntax", cl::init(Val: ATT), cl::Hidden,
28 cl::desc("Select the assembly style for input"),
29 cl::values(clEnumValN(ATT, "att", "Emit AT&T-style assembly"),
30 clEnumValN(Intel, "intel", "Emit Intel-style assembly")));
31
32static cl::opt<bool>
33MarkedJTDataRegions("mark-data-regions", cl::init(Val: true),
34 cl::desc("Mark code section jump table data regions."),
35 cl::Hidden);
36
37const MCAsmInfo::AtSpecifier atSpecifiers[] = {
38 {.Kind: X86::S_ABS8, .Name: "ABS8"},
39 {.Kind: X86::S_DTPOFF, .Name: "DTPOFF"},
40 {.Kind: X86::S_DTPREL, .Name: "DTPREL"},
41 {.Kind: X86::S_GOT, .Name: "GOT"},
42 {.Kind: X86::S_GOTENT, .Name: "GOTENT"},
43 {.Kind: X86::S_GOTNTPOFF, .Name: "GOTNTPOFF"},
44 {.Kind: X86::S_GOTOFF, .Name: "GOTOFF"},
45 {.Kind: X86::S_GOTPCREL, .Name: "GOTPCREL"},
46 {.Kind: X86::S_GOTPCREL_NORELAX, .Name: "GOTPCREL_NORELAX"},
47 {.Kind: X86::S_GOTREL, .Name: "GOTREL"},
48 {.Kind: X86::S_GOTTPOFF, .Name: "GOTTPOFF"},
49 {.Kind: X86::S_INDNTPOFF, .Name: "INDNTPOFF"},
50 {.Kind: MCSymbolRefExpr::VK_COFF_IMGREL32, .Name: "IMGREL"},
51 {.Kind: X86::S_NTPOFF, .Name: "NTPOFF"},
52 {.Kind: X86::S_PCREL, .Name: "PCREL"},
53 {.Kind: X86::S_PLT, .Name: "PLT"},
54 {.Kind: X86::S_PLTOFF, .Name: "PLTOFF"},
55 {.Kind: X86::S_COFF_SECREL, .Name: "SECREL32"},
56 {.Kind: X86::S_SIZE, .Name: "SIZE"},
57 {.Kind: X86::S_TLSCALL, .Name: "tlscall"},
58 {.Kind: X86::S_TLSDESC, .Name: "tlsdesc"},
59 {.Kind: X86::S_TLSGD, .Name: "TLSGD"},
60 {.Kind: X86::S_TLSLD, .Name: "TLSLD"},
61 {.Kind: X86::S_TLSLDM, .Name: "TLSLDM"},
62 {.Kind: X86::S_TLVP, .Name: "TLVP"},
63 {.Kind: X86::S_TLVPPAGE, .Name: "TLVPPAGE"},
64 {.Kind: X86::S_TLVPPAGEOFF, .Name: "TLVPPAGEOFF"},
65 {.Kind: X86::S_TPOFF, .Name: "TPOFF"},
66};
67
68void X86MCAsmInfoDarwin::anchor() { }
69
70X86MCAsmInfoDarwin::X86MCAsmInfoDarwin(const Triple &T,
71 const MCTargetOptions &Options)
72 : MCAsmInfoDarwin(Options) {
73 bool is64Bit = T.isX86_64();
74 if (is64Bit)
75 CodePointerSize = CalleeSaveStackSlotSize = 8;
76
77 AssemblerDialect = X86AsmSyntax;
78
79 if (!is64Bit)
80 Data64bitsDirective = nullptr; // we can't emit a 64-bit unit
81
82 // Use ## as a comment string so that .s files generated by llvm can go
83 // through the GCC preprocessor without causing an error. This is needed
84 // because "clang foo.s" runs the C preprocessor, which is usually reserved
85 // for .S files on other systems. Perhaps this is because the file system
86 // wasn't always case preserving or something.
87 CommentString = "##";
88
89 SupportsDebugInformation = true;
90 UseDataRegionDirectives = MarkedJTDataRegions;
91
92 // Exceptions handling
93 ExceptionsType = ExceptionHandling::DwarfCFI;
94
95 // old assembler lacks some directives
96 // FIXME: this should really be a check on the assembler characteristics
97 // rather than OS version
98 if (T.isMacOSX() && T.isMacOSXVersionLT(Major: 10, Minor: 6))
99 HasWeakDefCanBeHiddenDirective = false;
100
101 // Assume ld64 is new enough that the abs-ified FDE relocs may be used
102 // (actually, must, since otherwise the non-extern relocations we produce
103 // overwhelm ld64's tiny little mind and it fails).
104 DwarfFDESymbolsUseAbsDiff = true;
105
106 initializeAtSpecifiers(atSpecifiers);
107}
108
109X86_64MCAsmInfoDarwin::X86_64MCAsmInfoDarwin(const Triple &Triple,
110 const MCTargetOptions &Options)
111 : X86MCAsmInfoDarwin(Triple, Options) {}
112
113void X86ELFMCAsmInfo::anchor() { }
114
115X86ELFMCAsmInfo::X86ELFMCAsmInfo(const Triple &T,
116 const MCTargetOptions &Options)
117 : MCAsmInfoELF(Options) {
118 bool is64Bit = T.isX86_64();
119 bool isX32 = T.isX32();
120
121 // For ELF, x86-64 pointer size depends on the ABI.
122 // For x86-64 without the x32 ABI, pointer size is 8. For x86 and for x86-64
123 // with the x32 ABI, pointer size remains the default 4.
124 CodePointerSize = (is64Bit && !isX32) ? 8 : 4;
125
126 // OTOH, stack slot size is always 8 for x86-64, even with the x32 ABI.
127 CalleeSaveStackSlotSize = is64Bit ? 8 : 4;
128
129 AssemblerDialect = X86AsmSyntax;
130
131 // Debug Information
132 SupportsDebugInformation = true;
133
134 // Exceptions handling
135 ExceptionsType = ExceptionHandling::DwarfCFI;
136
137 initializeAtSpecifiers(atSpecifiers);
138}
139
140const MCExpr *
141X86_64MCAsmInfoDarwin::getExprForPersonalitySymbol(const MCSymbol *Sym,
142 unsigned Encoding,
143 MCStreamer &Streamer) const {
144 MCContext &Context = Streamer.getContext();
145 const MCExpr *Res = MCSymbolRefExpr::create(Symbol: Sym, specifier: X86::S_GOTPCREL, Ctx&: Context);
146 const MCExpr *Four = MCConstantExpr::create(Value: 4, Ctx&: Context);
147 return MCBinaryExpr::createAdd(LHS: Res, RHS: Four, Ctx&: Context);
148}
149
150void X86MCAsmInfoMicrosoft::anchor() { }
151
152X86MCAsmInfoMicrosoft::X86MCAsmInfoMicrosoft(const Triple &Triple,
153 const MCTargetOptions &Options)
154 : MCAsmInfoMicrosoft(Options) {
155 if (Triple.isX86_64()) {
156 InternalSymbolPrefix = ".L";
157 CodePointerSize = 8;
158 WinEHEncodingType = WinEH::EncodingType::Itanium;
159 } else {
160 // 32-bit X86 doesn't use CFI, so this isn't a real encoding type. It's just
161 // a place holder that the Windows EHStreamer looks for to suppress CFI
162 // output. In particular, usesWindowsCFI() returns false.
163 WinEHEncodingType = WinEH::EncodingType::X86;
164 }
165
166 ExceptionsType = ExceptionHandling::WinEH;
167
168 AssemblerDialect = X86AsmSyntax;
169
170 AllowAtInName = true;
171
172 initializeAtSpecifiers(atSpecifiers);
173}
174
175void X86MCAsmInfoMicrosoftMASM::anchor() { }
176
177X86MCAsmInfoMicrosoftMASM::X86MCAsmInfoMicrosoftMASM(
178 const Triple &Triple, const MCTargetOptions &Options)
179 : X86MCAsmInfoMicrosoft(Triple, Options) {
180 DollarIsPC = true;
181 SeparatorString = "\n";
182 CommentString = ";";
183 AllowAdditionalComments = false;
184 AllowQuestionAtStartOfIdentifier = true;
185 AllowDollarAtStartOfIdentifier = true;
186 AllowAtAtStartOfIdentifier = true;
187}
188
189static bool isValidX86UnquotedName(const MCAsmInfo &MAI, StringRef Name) {
190 if (!MAI.MCAsmInfo::isValidUnquotedName(Name))
191 return false;
192 // Only Intel-syntax output needs to avoid register/keyword collisions; AT&T
193 // disambiguates registers with '%' and doesn't treat `byte`, `ptr`, etc. as
194 // keywords.
195 if (MAI.getOutputAssemblerDialect() == 0)
196 return true;
197 return !MAI.getReservedIdentifiers().contains(
198 V: CachedHashStringRef(Name.lower()));
199}
200
201bool X86MCAsmInfoDarwin::isValidUnquotedName(StringRef Name) const {
202 return isValidX86UnquotedName(MAI: *this, Name);
203}
204
205bool X86ELFMCAsmInfo::isValidUnquotedName(StringRef Name) const {
206 return isValidX86UnquotedName(MAI: *this, Name);
207}
208
209bool X86MCAsmInfoMicrosoft::isValidUnquotedName(StringRef Name) const {
210 return isValidX86UnquotedName(MAI: *this, Name);
211}
212
213bool X86MCAsmInfoGNUCOFF::isValidUnquotedName(StringRef Name) const {
214 return isValidX86UnquotedName(MAI: *this, Name);
215}
216
217void X86MCAsmInfoGNUCOFF::anchor() { }
218
219X86MCAsmInfoGNUCOFF::X86MCAsmInfoGNUCOFF(const Triple &Triple,
220 const MCTargetOptions &Options)
221 : MCAsmInfoGNUCOFF(Options) {
222 assert((Triple.isOSWindows() || Triple.isUEFI()) &&
223 "Windows and UEFI are the only supported COFF targets");
224 if (Triple.isX86_64()) {
225 InternalSymbolPrefix = ".L";
226 CodePointerSize = 8;
227 WinEHEncodingType = WinEH::EncodingType::Itanium;
228 ExceptionsType = ExceptionHandling::WinEH;
229 } else {
230 ExceptionsType = ExceptionHandling::DwarfCFI;
231 }
232
233 AssemblerDialect = X86AsmSyntax;
234
235 AllowAtInName = true;
236
237 initializeAtSpecifiers(atSpecifiers);
238}
239