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 bool is64Bit = T.isX86_64();
72 if (is64Bit)
73 CodePointerSize = CalleeSaveStackSlotSize = 8;
74
75 AssemblerDialect = X86AsmSyntax;
76
77 if (!is64Bit)
78 Data64bitsDirective = nullptr; // we can't emit a 64-bit unit
79
80 // Use ## as a comment string so that .s files generated by llvm can go
81 // through the GCC preprocessor without causing an error. This is needed
82 // because "clang foo.s" runs the C preprocessor, which is usually reserved
83 // for .S files on other systems. Perhaps this is because the file system
84 // wasn't always case preserving or something.
85 CommentString = "##";
86
87 AllowDollarAtStartOfIdentifier = false;
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 : X86MCAsmInfoDarwin(Triple) {
111}
112
113void X86ELFMCAsmInfo::anchor() { }
114
115X86ELFMCAsmInfo::X86ELFMCAsmInfo(const Triple &T) {
116 bool is64Bit = T.isX86_64();
117 bool isX32 = T.isX32();
118
119 // For ELF, x86-64 pointer size depends on the ABI.
120 // For x86-64 without the x32 ABI, pointer size is 8. For x86 and for x86-64
121 // with the x32 ABI, pointer size remains the default 4.
122 CodePointerSize = (is64Bit && !isX32) ? 8 : 4;
123
124 // OTOH, stack slot size is always 8 for x86-64, even with the x32 ABI.
125 CalleeSaveStackSlotSize = is64Bit ? 8 : 4;
126
127 AssemblerDialect = X86AsmSyntax;
128 AllowDollarAtStartOfIdentifier = false;
129
130 // Debug Information
131 SupportsDebugInformation = true;
132
133 // Exceptions handling
134 ExceptionsType = ExceptionHandling::DwarfCFI;
135
136 initializeAtSpecifiers(atSpecifiers);
137}
138
139const MCExpr *
140X86_64MCAsmInfoDarwin::getExprForPersonalitySymbol(const MCSymbol *Sym,
141 unsigned Encoding,
142 MCStreamer &Streamer) const {
143 MCContext &Context = Streamer.getContext();
144 const MCExpr *Res = MCSymbolRefExpr::create(Symbol: Sym, specifier: X86::S_GOTPCREL, Ctx&: Context);
145 const MCExpr *Four = MCConstantExpr::create(Value: 4, Ctx&: Context);
146 return MCBinaryExpr::createAdd(LHS: Res, RHS: Four, Ctx&: Context);
147}
148
149void X86MCAsmInfoMicrosoft::anchor() { }
150
151X86MCAsmInfoMicrosoft::X86MCAsmInfoMicrosoft(const Triple &Triple) {
152 if (Triple.isX86_64()) {
153 PrivateGlobalPrefix = ".L";
154 PrivateLabelPrefix = ".L";
155 CodePointerSize = 8;
156 WinEHEncodingType = WinEH::EncodingType::Itanium;
157 } else {
158 // 32-bit X86 doesn't use CFI, so this isn't a real encoding type. It's just
159 // a place holder that the Windows EHStreamer looks for to suppress CFI
160 // output. In particular, usesWindowsCFI() returns false.
161 WinEHEncodingType = WinEH::EncodingType::X86;
162 }
163
164 ExceptionsType = ExceptionHandling::WinEH;
165
166 AssemblerDialect = X86AsmSyntax;
167 AllowDollarAtStartOfIdentifier = false;
168
169 AllowAtInName = true;
170
171 initializeAtSpecifiers(atSpecifiers);
172}
173
174void X86MCAsmInfoMicrosoftMASM::anchor() { }
175
176X86MCAsmInfoMicrosoftMASM::X86MCAsmInfoMicrosoftMASM(const Triple &Triple)
177 : X86MCAsmInfoMicrosoft(Triple) {
178 DollarIsPC = true;
179 SeparatorString = "\n";
180 CommentString = ";";
181 AllowAdditionalComments = false;
182 AllowQuestionAtStartOfIdentifier = true;
183 AllowDollarAtStartOfIdentifier = true;
184 AllowAtAtStartOfIdentifier = true;
185}
186
187void X86MCAsmInfoGNUCOFF::anchor() { }
188
189X86MCAsmInfoGNUCOFF::X86MCAsmInfoGNUCOFF(const Triple &Triple) {
190 assert((Triple.isOSWindows() || Triple.isUEFI()) &&
191 "Windows and UEFI are the only supported COFF targets");
192 if (Triple.isX86_64()) {
193 PrivateGlobalPrefix = ".L";
194 PrivateLabelPrefix = ".L";
195 CodePointerSize = 8;
196 WinEHEncodingType = WinEH::EncodingType::Itanium;
197 ExceptionsType = ExceptionHandling::WinEH;
198 } else {
199 ExceptionsType = ExceptionHandling::DwarfCFI;
200 }
201
202 AssemblerDialect = X86AsmSyntax;
203
204 AllowAtInName = true;
205 AllowDollarAtStartOfIdentifier = false;
206
207 initializeAtSpecifiers(atSpecifiers);
208}
209