[clang][modules] Don't prevent translation of FW_Private includes when explicitly...
[llvm-project.git] / clang / lib / Basic / Targets.cpp
blobea002bb464fcc587c0de6dcfec2bd0616e21757a
1 //===--- Targets.cpp - Implement target feature support -------------------===//
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 implements construction of a TargetInfo object from a
10 // target triple.
12 //===----------------------------------------------------------------------===//
14 #include "Targets.h"
16 #include "Targets/AArch64.h"
17 #include "Targets/AMDGPU.h"
18 #include "Targets/ARC.h"
19 #include "Targets/ARM.h"
20 #include "Targets/AVR.h"
21 #include "Targets/BPF.h"
22 #include "Targets/CSKY.h"
23 #include "Targets/DirectX.h"
24 #include "Targets/Hexagon.h"
25 #include "Targets/Lanai.h"
26 #include "Targets/Le64.h"
27 #include "Targets/LoongArch.h"
28 #include "Targets/M68k.h"
29 #include "Targets/MSP430.h"
30 #include "Targets/Mips.h"
31 #include "Targets/NVPTX.h"
32 #include "Targets/OSTargets.h"
33 #include "Targets/PNaCl.h"
34 #include "Targets/PPC.h"
35 #include "Targets/RISCV.h"
36 #include "Targets/SPIR.h"
37 #include "Targets/Sparc.h"
38 #include "Targets/SystemZ.h"
39 #include "Targets/TCE.h"
40 #include "Targets/VE.h"
41 #include "Targets/WebAssembly.h"
42 #include "Targets/X86.h"
43 #include "Targets/XCore.h"
44 #include "clang/Basic/Diagnostic.h"
45 #include "clang/Basic/DiagnosticFrontend.h"
46 #include "llvm/ADT/StringExtras.h"
47 #include "llvm/TargetParser/Triple.h"
49 using namespace clang;
51 namespace clang {
52 namespace targets {
53 //===----------------------------------------------------------------------===//
54 // Common code shared among targets.
55 //===----------------------------------------------------------------------===//
57 /// DefineStd - Define a macro name and standard variants. For example if
58 /// MacroName is "unix", then this will define "__unix", "__unix__", and "unix"
59 /// when in GNU mode.
60 void DefineStd(MacroBuilder &Builder, StringRef MacroName,
61 const LangOptions &Opts) {
62 assert(MacroName[0] != '_' && "Identifier should be in the user's namespace");
64 // If in GNU mode (e.g. -std=gnu99 but not -std=c99) define the raw identifier
65 // in the user's namespace.
66 if (Opts.GNUMode)
67 Builder.defineMacro(MacroName);
69 // Define __unix.
70 Builder.defineMacro("__" + MacroName);
72 // Define __unix__.
73 Builder.defineMacro("__" + MacroName + "__");
76 void defineCPUMacros(MacroBuilder &Builder, StringRef CPUName, bool Tuning) {
77 Builder.defineMacro("__" + CPUName);
78 Builder.defineMacro("__" + CPUName + "__");
79 if (Tuning)
80 Builder.defineMacro("__tune_" + CPUName + "__");
83 void addCygMingDefines(const LangOptions &Opts, MacroBuilder &Builder) {
84 // Mingw and cygwin define __declspec(a) to __attribute__((a)). Clang
85 // supports __declspec natively under -fdeclspec (also enabled with
86 // -fms-extensions), but we define a no-op __declspec macro anyway for
87 // pre-processor compatibility.
88 if (Opts.DeclSpecKeyword)
89 Builder.defineMacro("__declspec", "__declspec");
90 else
91 Builder.defineMacro("__declspec(a)", "__attribute__((a))");
93 if (!Opts.MicrosoftExt) {
94 // Provide macros for all the calling convention keywords. Provide both
95 // single and double underscore prefixed variants. These are available on
96 // x64 as well as x86, even though they have no effect.
97 const char *CCs[] = {"cdecl", "stdcall", "fastcall", "thiscall", "pascal"};
98 for (const char *CC : CCs) {
99 std::string GCCSpelling = "__attribute__((__";
100 GCCSpelling += CC;
101 GCCSpelling += "__))";
102 Builder.defineMacro(Twine("_") + CC, GCCSpelling);
103 Builder.defineMacro(Twine("__") + CC, GCCSpelling);
108 //===----------------------------------------------------------------------===//
109 // Driver code
110 //===----------------------------------------------------------------------===//
112 std::unique_ptr<TargetInfo> AllocateTarget(const llvm::Triple &Triple,
113 const TargetOptions &Opts) {
114 llvm::Triple::OSType os = Triple.getOS();
116 switch (Triple.getArch()) {
117 default:
118 return nullptr;
120 case llvm::Triple::arc:
121 return std::make_unique<ARCTargetInfo>(Triple, Opts);
123 case llvm::Triple::xcore:
124 return std::make_unique<XCoreTargetInfo>(Triple, Opts);
126 case llvm::Triple::hexagon:
127 if (os == llvm::Triple::Linux &&
128 Triple.getEnvironment() == llvm::Triple::Musl)
129 return std::make_unique<LinuxTargetInfo<HexagonTargetInfo>>(Triple, Opts);
130 return std::make_unique<HexagonTargetInfo>(Triple, Opts);
132 case llvm::Triple::lanai:
133 return std::make_unique<LanaiTargetInfo>(Triple, Opts);
135 case llvm::Triple::aarch64_32:
136 if (Triple.isOSDarwin())
137 return std::make_unique<DarwinAArch64TargetInfo>(Triple, Opts);
139 return nullptr;
140 case llvm::Triple::aarch64:
141 if (Triple.isOSDarwin())
142 return std::make_unique<DarwinAArch64TargetInfo>(Triple, Opts);
144 switch (os) {
145 case llvm::Triple::FreeBSD:
146 return std::make_unique<FreeBSDTargetInfo<AArch64leTargetInfo>>(Triple,
147 Opts);
148 case llvm::Triple::Fuchsia:
149 return std::make_unique<FuchsiaTargetInfo<AArch64leTargetInfo>>(Triple,
150 Opts);
151 case llvm::Triple::Haiku:
152 return std::make_unique<HaikuTargetInfo<AArch64leTargetInfo>>(Triple,
153 Opts);
154 case llvm::Triple::Linux:
155 switch (Triple.getEnvironment()) {
156 default:
157 return std::make_unique<LinuxTargetInfo<AArch64leTargetInfo>>(Triple,
158 Opts);
159 case llvm::Triple::OpenHOS:
160 return std::make_unique<OHOSTargetInfo<AArch64leTargetInfo>>(Triple,
161 Opts);
163 case llvm::Triple::NetBSD:
164 return std::make_unique<NetBSDTargetInfo<AArch64leTargetInfo>>(Triple,
165 Opts);
166 case llvm::Triple::OpenBSD:
167 return std::make_unique<OpenBSDTargetInfo<AArch64leTargetInfo>>(Triple,
168 Opts);
169 case llvm::Triple::Win32:
170 switch (Triple.getEnvironment()) {
171 case llvm::Triple::GNU:
172 return std::make_unique<MinGWARM64TargetInfo>(Triple, Opts);
173 case llvm::Triple::MSVC:
174 default: // Assume MSVC for unknown environments
175 return std::make_unique<MicrosoftARM64TargetInfo>(Triple, Opts);
177 default:
178 return std::make_unique<AArch64leTargetInfo>(Triple, Opts);
181 case llvm::Triple::aarch64_be:
182 switch (os) {
183 case llvm::Triple::FreeBSD:
184 return std::make_unique<FreeBSDTargetInfo<AArch64beTargetInfo>>(Triple,
185 Opts);
186 case llvm::Triple::Fuchsia:
187 return std::make_unique<FuchsiaTargetInfo<AArch64beTargetInfo>>(Triple,
188 Opts);
189 case llvm::Triple::Linux:
190 return std::make_unique<LinuxTargetInfo<AArch64beTargetInfo>>(Triple,
191 Opts);
192 case llvm::Triple::NetBSD:
193 return std::make_unique<NetBSDTargetInfo<AArch64beTargetInfo>>(Triple,
194 Opts);
195 default:
196 return std::make_unique<AArch64beTargetInfo>(Triple, Opts);
199 case llvm::Triple::arm:
200 case llvm::Triple::thumb:
201 if (Triple.isOSBinFormatMachO())
202 return std::make_unique<DarwinARMTargetInfo>(Triple, Opts);
204 switch (os) {
205 case llvm::Triple::Linux:
206 switch (Triple.getEnvironment()) {
207 default:
208 return std::make_unique<LinuxTargetInfo<ARMleTargetInfo>>(Triple, Opts);
209 case llvm::Triple::OpenHOS:
210 return std::make_unique<OHOSTargetInfo<ARMleTargetInfo>>(Triple, Opts);
212 case llvm::Triple::LiteOS:
213 return std::make_unique<OHOSTargetInfo<ARMleTargetInfo>>(Triple, Opts);
214 case llvm::Triple::FreeBSD:
215 return std::make_unique<FreeBSDTargetInfo<ARMleTargetInfo>>(Triple, Opts);
216 case llvm::Triple::NetBSD:
217 return std::make_unique<NetBSDTargetInfo<ARMleTargetInfo>>(Triple, Opts);
218 case llvm::Triple::OpenBSD:
219 return std::make_unique<OpenBSDTargetInfo<ARMleTargetInfo>>(Triple, Opts);
220 case llvm::Triple::RTEMS:
221 return std::make_unique<RTEMSTargetInfo<ARMleTargetInfo>>(Triple, Opts);
222 case llvm::Triple::Haiku:
223 return std::make_unique<HaikuTargetInfo<ARMleTargetInfo>>(Triple, Opts);
224 case llvm::Triple::NaCl:
225 return std::make_unique<NaClTargetInfo<ARMleTargetInfo>>(Triple, Opts);
226 case llvm::Triple::Win32:
227 switch (Triple.getEnvironment()) {
228 case llvm::Triple::Cygnus:
229 return std::make_unique<CygwinARMTargetInfo>(Triple, Opts);
230 case llvm::Triple::GNU:
231 return std::make_unique<MinGWARMTargetInfo>(Triple, Opts);
232 case llvm::Triple::Itanium:
233 return std::make_unique<ItaniumWindowsARMleTargetInfo>(Triple, Opts);
234 case llvm::Triple::MSVC:
235 default: // Assume MSVC for unknown environments
236 return std::make_unique<MicrosoftARMleTargetInfo>(Triple, Opts);
238 default:
239 return std::make_unique<ARMleTargetInfo>(Triple, Opts);
242 case llvm::Triple::armeb:
243 case llvm::Triple::thumbeb:
244 if (Triple.isOSDarwin())
245 return std::make_unique<DarwinARMTargetInfo>(Triple, Opts);
247 switch (os) {
248 case llvm::Triple::Linux:
249 return std::make_unique<LinuxTargetInfo<ARMbeTargetInfo>>(Triple, Opts);
250 case llvm::Triple::NetBSD:
251 return std::make_unique<NetBSDTargetInfo<ARMbeTargetInfo>>(Triple, Opts);
252 case llvm::Triple::RTEMS:
253 return std::make_unique<RTEMSTargetInfo<ARMbeTargetInfo>>(Triple, Opts);
254 case llvm::Triple::NaCl:
255 return std::make_unique<NaClTargetInfo<ARMbeTargetInfo>>(Triple, Opts);
256 default:
257 return std::make_unique<ARMbeTargetInfo>(Triple, Opts);
260 case llvm::Triple::avr:
261 return std::make_unique<AVRTargetInfo>(Triple, Opts);
262 case llvm::Triple::bpfeb:
263 case llvm::Triple::bpfel:
264 return std::make_unique<BPFTargetInfo>(Triple, Opts);
266 case llvm::Triple::msp430:
267 return std::make_unique<MSP430TargetInfo>(Triple, Opts);
269 case llvm::Triple::mips:
270 switch (os) {
271 case llvm::Triple::Linux:
272 return std::make_unique<LinuxTargetInfo<MipsTargetInfo>>(Triple, Opts);
273 case llvm::Triple::RTEMS:
274 return std::make_unique<RTEMSTargetInfo<MipsTargetInfo>>(Triple, Opts);
275 case llvm::Triple::FreeBSD:
276 return std::make_unique<FreeBSDTargetInfo<MipsTargetInfo>>(Triple, Opts);
277 case llvm::Triple::NetBSD:
278 return std::make_unique<NetBSDTargetInfo<MipsTargetInfo>>(Triple, Opts);
279 default:
280 return std::make_unique<MipsTargetInfo>(Triple, Opts);
283 case llvm::Triple::mipsel:
284 switch (os) {
285 case llvm::Triple::Linux:
286 switch (Triple.getEnvironment()) {
287 default:
288 return std::make_unique<LinuxTargetInfo<MipsTargetInfo>>(Triple, Opts);
289 case llvm::Triple::OpenHOS:
290 return std::make_unique<OHOSTargetInfo<MipsTargetInfo>>(Triple, Opts);
292 case llvm::Triple::RTEMS:
293 return std::make_unique<RTEMSTargetInfo<MipsTargetInfo>>(Triple, Opts);
294 case llvm::Triple::FreeBSD:
295 return std::make_unique<FreeBSDTargetInfo<MipsTargetInfo>>(Triple, Opts);
296 case llvm::Triple::NetBSD:
297 return std::make_unique<NetBSDTargetInfo<MipsTargetInfo>>(Triple, Opts);
298 case llvm::Triple::NaCl:
299 return std::make_unique<NaClTargetInfo<NaClMips32TargetInfo>>(Triple,
300 Opts);
301 default:
302 return std::make_unique<MipsTargetInfo>(Triple, Opts);
305 case llvm::Triple::mips64:
306 switch (os) {
307 case llvm::Triple::Linux:
308 return std::make_unique<LinuxTargetInfo<MipsTargetInfo>>(Triple, Opts);
309 case llvm::Triple::RTEMS:
310 return std::make_unique<RTEMSTargetInfo<MipsTargetInfo>>(Triple, Opts);
311 case llvm::Triple::FreeBSD:
312 return std::make_unique<FreeBSDTargetInfo<MipsTargetInfo>>(Triple, Opts);
313 case llvm::Triple::NetBSD:
314 return std::make_unique<NetBSDTargetInfo<MipsTargetInfo>>(Triple, Opts);
315 case llvm::Triple::OpenBSD:
316 return std::make_unique<OpenBSDTargetInfo<MipsTargetInfo>>(Triple, Opts);
317 default:
318 return std::make_unique<MipsTargetInfo>(Triple, Opts);
321 case llvm::Triple::mips64el:
322 switch (os) {
323 case llvm::Triple::Linux:
324 return std::make_unique<LinuxTargetInfo<MipsTargetInfo>>(Triple, Opts);
325 case llvm::Triple::RTEMS:
326 return std::make_unique<RTEMSTargetInfo<MipsTargetInfo>>(Triple, Opts);
327 case llvm::Triple::FreeBSD:
328 return std::make_unique<FreeBSDTargetInfo<MipsTargetInfo>>(Triple, Opts);
329 case llvm::Triple::NetBSD:
330 return std::make_unique<NetBSDTargetInfo<MipsTargetInfo>>(Triple, Opts);
331 case llvm::Triple::OpenBSD:
332 return std::make_unique<OpenBSDTargetInfo<MipsTargetInfo>>(Triple, Opts);
333 default:
334 return std::make_unique<MipsTargetInfo>(Triple, Opts);
337 case llvm::Triple::m68k:
338 switch (os) {
339 case llvm::Triple::Linux:
340 return std::make_unique<LinuxTargetInfo<M68kTargetInfo>>(Triple, Opts);
341 case llvm::Triple::NetBSD:
342 return std::make_unique<NetBSDTargetInfo<M68kTargetInfo>>(Triple, Opts);
343 default:
344 return std::make_unique<M68kTargetInfo>(Triple, Opts);
347 case llvm::Triple::le32:
348 switch (os) {
349 case llvm::Triple::NaCl:
350 return std::make_unique<NaClTargetInfo<PNaClTargetInfo>>(Triple, Opts);
351 default:
352 return nullptr;
355 case llvm::Triple::le64:
356 return std::make_unique<Le64TargetInfo>(Triple, Opts);
358 case llvm::Triple::ppc:
359 switch (os) {
360 case llvm::Triple::Linux:
361 return std::make_unique<LinuxTargetInfo<PPC32TargetInfo>>(Triple, Opts);
362 case llvm::Triple::FreeBSD:
363 return std::make_unique<FreeBSDTargetInfo<PPC32TargetInfo>>(Triple, Opts);
364 case llvm::Triple::NetBSD:
365 return std::make_unique<NetBSDTargetInfo<PPC32TargetInfo>>(Triple, Opts);
366 case llvm::Triple::OpenBSD:
367 return std::make_unique<OpenBSDTargetInfo<PPC32TargetInfo>>(Triple, Opts);
368 case llvm::Triple::RTEMS:
369 return std::make_unique<RTEMSTargetInfo<PPC32TargetInfo>>(Triple, Opts);
370 case llvm::Triple::AIX:
371 return std::make_unique<AIXPPC32TargetInfo>(Triple, Opts);
372 default:
373 return std::make_unique<PPC32TargetInfo>(Triple, Opts);
376 case llvm::Triple::ppcle:
377 switch (os) {
378 case llvm::Triple::Linux:
379 return std::make_unique<LinuxTargetInfo<PPC32TargetInfo>>(Triple, Opts);
380 case llvm::Triple::FreeBSD:
381 return std::make_unique<FreeBSDTargetInfo<PPC32TargetInfo>>(Triple, Opts);
382 default:
383 return std::make_unique<PPC32TargetInfo>(Triple, Opts);
386 case llvm::Triple::ppc64:
387 switch (os) {
388 case llvm::Triple::Linux:
389 return std::make_unique<LinuxTargetInfo<PPC64TargetInfo>>(Triple, Opts);
390 case llvm::Triple::Lv2:
391 return std::make_unique<PS3PPUTargetInfo<PPC64TargetInfo>>(Triple, Opts);
392 case llvm::Triple::FreeBSD:
393 return std::make_unique<FreeBSDTargetInfo<PPC64TargetInfo>>(Triple, Opts);
394 case llvm::Triple::NetBSD:
395 return std::make_unique<NetBSDTargetInfo<PPC64TargetInfo>>(Triple, Opts);
396 case llvm::Triple::OpenBSD:
397 return std::make_unique<OpenBSDTargetInfo<PPC64TargetInfo>>(Triple, Opts);
398 case llvm::Triple::AIX:
399 return std::make_unique<AIXPPC64TargetInfo>(Triple, Opts);
400 default:
401 return std::make_unique<PPC64TargetInfo>(Triple, Opts);
404 case llvm::Triple::ppc64le:
405 switch (os) {
406 case llvm::Triple::Linux:
407 return std::make_unique<LinuxTargetInfo<PPC64TargetInfo>>(Triple, Opts);
408 case llvm::Triple::FreeBSD:
409 return std::make_unique<FreeBSDTargetInfo<PPC64TargetInfo>>(Triple, Opts);
410 case llvm::Triple::NetBSD:
411 return std::make_unique<NetBSDTargetInfo<PPC64TargetInfo>>(Triple, Opts);
412 case llvm::Triple::OpenBSD:
413 return std::make_unique<OpenBSDTargetInfo<PPC64TargetInfo>>(Triple, Opts);
414 default:
415 return std::make_unique<PPC64TargetInfo>(Triple, Opts);
418 case llvm::Triple::nvptx:
419 return std::make_unique<NVPTXTargetInfo>(Triple, Opts,
420 /*TargetPointerWidth=*/32);
421 case llvm::Triple::nvptx64:
422 return std::make_unique<NVPTXTargetInfo>(Triple, Opts,
423 /*TargetPointerWidth=*/64);
425 case llvm::Triple::amdgcn:
426 case llvm::Triple::r600:
427 return std::make_unique<AMDGPUTargetInfo>(Triple, Opts);
429 case llvm::Triple::riscv32:
430 switch (os) {
431 case llvm::Triple::NetBSD:
432 return std::make_unique<NetBSDTargetInfo<RISCV32TargetInfo>>(Triple,
433 Opts);
434 case llvm::Triple::Linux:
435 return std::make_unique<LinuxTargetInfo<RISCV32TargetInfo>>(Triple, Opts);
436 default:
437 return std::make_unique<RISCV32TargetInfo>(Triple, Opts);
440 case llvm::Triple::riscv64:
441 switch (os) {
442 case llvm::Triple::FreeBSD:
443 return std::make_unique<FreeBSDTargetInfo<RISCV64TargetInfo>>(Triple,
444 Opts);
445 case llvm::Triple::NetBSD:
446 return std::make_unique<NetBSDTargetInfo<RISCV64TargetInfo>>(Triple,
447 Opts);
448 case llvm::Triple::OpenBSD:
449 return std::make_unique<OpenBSDTargetInfo<RISCV64TargetInfo>>(Triple,
450 Opts);
451 case llvm::Triple::Fuchsia:
452 return std::make_unique<FuchsiaTargetInfo<RISCV64TargetInfo>>(Triple,
453 Opts);
454 case llvm::Triple::Haiku:
455 return std::make_unique<HaikuTargetInfo<RISCV64TargetInfo>>(Triple,
456 Opts);
457 case llvm::Triple::Linux:
458 switch (Triple.getEnvironment()) {
459 default:
460 return std::make_unique<LinuxTargetInfo<RISCV64TargetInfo>>(Triple,
461 Opts);
462 case llvm::Triple::OpenHOS:
463 return std::make_unique<OHOSTargetInfo<RISCV64TargetInfo>>(Triple,
464 Opts);
466 default:
467 return std::make_unique<RISCV64TargetInfo>(Triple, Opts);
470 case llvm::Triple::sparc:
471 switch (os) {
472 case llvm::Triple::Linux:
473 return std::make_unique<LinuxTargetInfo<SparcV8TargetInfo>>(Triple, Opts);
474 case llvm::Triple::Solaris:
475 return std::make_unique<SolarisTargetInfo<SparcV8TargetInfo>>(Triple,
476 Opts);
477 case llvm::Triple::NetBSD:
478 return std::make_unique<NetBSDTargetInfo<SparcV8TargetInfo>>(Triple,
479 Opts);
480 case llvm::Triple::RTEMS:
481 return std::make_unique<RTEMSTargetInfo<SparcV8TargetInfo>>(Triple, Opts);
482 default:
483 return std::make_unique<SparcV8TargetInfo>(Triple, Opts);
486 case llvm::Triple::sparcel:
487 switch (os) {
488 case llvm::Triple::Linux:
489 return std::make_unique<LinuxTargetInfo<SparcV8elTargetInfo>>(Triple,
490 Opts);
491 case llvm::Triple::RTEMS:
492 return std::make_unique<RTEMSTargetInfo<SparcV8elTargetInfo>>(Triple,
493 Opts);
494 default:
495 return std::make_unique<SparcV8elTargetInfo>(Triple, Opts);
498 case llvm::Triple::sparcv9:
499 switch (os) {
500 case llvm::Triple::Linux:
501 return std::make_unique<LinuxTargetInfo<SparcV9TargetInfo>>(Triple, Opts);
502 case llvm::Triple::Solaris:
503 return std::make_unique<SolarisTargetInfo<SparcV9TargetInfo>>(Triple,
504 Opts);
505 case llvm::Triple::NetBSD:
506 return std::make_unique<NetBSDTargetInfo<SparcV9TargetInfo>>(Triple,
507 Opts);
508 case llvm::Triple::OpenBSD:
509 return std::make_unique<OpenBSDTargetInfo<SparcV9TargetInfo>>(Triple,
510 Opts);
511 case llvm::Triple::FreeBSD:
512 return std::make_unique<FreeBSDTargetInfo<SparcV9TargetInfo>>(Triple,
513 Opts);
514 default:
515 return std::make_unique<SparcV9TargetInfo>(Triple, Opts);
518 case llvm::Triple::systemz:
519 switch (os) {
520 case llvm::Triple::Linux:
521 return std::make_unique<LinuxTargetInfo<SystemZTargetInfo>>(Triple, Opts);
522 case llvm::Triple::ZOS:
523 return std::make_unique<ZOSTargetInfo<SystemZTargetInfo>>(Triple, Opts);
524 default:
525 return std::make_unique<SystemZTargetInfo>(Triple, Opts);
528 case llvm::Triple::tce:
529 return std::make_unique<TCETargetInfo>(Triple, Opts);
531 case llvm::Triple::tcele:
532 return std::make_unique<TCELETargetInfo>(Triple, Opts);
534 case llvm::Triple::x86:
535 if (Triple.isOSDarwin())
536 return std::make_unique<DarwinI386TargetInfo>(Triple, Opts);
538 switch (os) {
539 case llvm::Triple::Linux: {
540 switch (Triple.getEnvironment()) {
541 default:
542 return std::make_unique<LinuxTargetInfo<X86_32TargetInfo>>(Triple,
543 Opts);
544 case llvm::Triple::Android:
545 return std::make_unique<AndroidX86_32TargetInfo>(Triple, Opts);
548 case llvm::Triple::DragonFly:
549 return std::make_unique<DragonFlyBSDTargetInfo<X86_32TargetInfo>>(Triple,
550 Opts);
551 case llvm::Triple::NetBSD:
552 return std::make_unique<NetBSDI386TargetInfo>(Triple, Opts);
553 case llvm::Triple::OpenBSD:
554 return std::make_unique<OpenBSDI386TargetInfo>(Triple, Opts);
555 case llvm::Triple::FreeBSD:
556 return std::make_unique<FreeBSDTargetInfo<X86_32TargetInfo>>(Triple,
557 Opts);
558 case llvm::Triple::Fuchsia:
559 return std::make_unique<FuchsiaTargetInfo<X86_32TargetInfo>>(Triple,
560 Opts);
561 case llvm::Triple::KFreeBSD:
562 return std::make_unique<KFreeBSDTargetInfo<X86_32TargetInfo>>(Triple,
563 Opts);
564 case llvm::Triple::Solaris:
565 return std::make_unique<SolarisTargetInfo<X86_32TargetInfo>>(Triple,
566 Opts);
567 case llvm::Triple::Win32: {
568 switch (Triple.getEnvironment()) {
569 case llvm::Triple::Cygnus:
570 return std::make_unique<CygwinX86_32TargetInfo>(Triple, Opts);
571 case llvm::Triple::GNU:
572 return std::make_unique<MinGWX86_32TargetInfo>(Triple, Opts);
573 case llvm::Triple::Itanium:
574 case llvm::Triple::MSVC:
575 default: // Assume MSVC for unknown environments
576 return std::make_unique<MicrosoftX86_32TargetInfo>(Triple, Opts);
579 case llvm::Triple::Haiku:
580 return std::make_unique<HaikuX86_32TargetInfo>(Triple, Opts);
581 case llvm::Triple::RTEMS:
582 return std::make_unique<RTEMSX86_32TargetInfo>(Triple, Opts);
583 case llvm::Triple::NaCl:
584 return std::make_unique<NaClTargetInfo<X86_32TargetInfo>>(Triple, Opts);
585 case llvm::Triple::ELFIAMCU:
586 return std::make_unique<MCUX86_32TargetInfo>(Triple, Opts);
587 case llvm::Triple::Hurd:
588 return std::make_unique<HurdTargetInfo<X86_32TargetInfo>>(Triple, Opts);
589 default:
590 return std::make_unique<X86_32TargetInfo>(Triple, Opts);
593 case llvm::Triple::x86_64:
594 if (Triple.isOSDarwin() || Triple.isOSBinFormatMachO())
595 return std::make_unique<DarwinX86_64TargetInfo>(Triple, Opts);
597 switch (os) {
598 case llvm::Triple::Linux: {
599 switch (Triple.getEnvironment()) {
600 default:
601 return std::make_unique<LinuxTargetInfo<X86_64TargetInfo>>(Triple,
602 Opts);
603 case llvm::Triple::Android:
604 return std::make_unique<AndroidX86_64TargetInfo>(Triple, Opts);
605 case llvm::Triple::OpenHOS:
606 return std::make_unique<OHOSX86_64TargetInfo>(Triple, Opts);
609 case llvm::Triple::DragonFly:
610 return std::make_unique<DragonFlyBSDTargetInfo<X86_64TargetInfo>>(Triple,
611 Opts);
612 case llvm::Triple::NetBSD:
613 return std::make_unique<NetBSDTargetInfo<X86_64TargetInfo>>(Triple, Opts);
614 case llvm::Triple::OpenBSD:
615 return std::make_unique<OpenBSDX86_64TargetInfo>(Triple, Opts);
616 case llvm::Triple::FreeBSD:
617 return std::make_unique<FreeBSDTargetInfo<X86_64TargetInfo>>(Triple,
618 Opts);
619 case llvm::Triple::Fuchsia:
620 return std::make_unique<FuchsiaTargetInfo<X86_64TargetInfo>>(Triple,
621 Opts);
622 case llvm::Triple::KFreeBSD:
623 return std::make_unique<KFreeBSDTargetInfo<X86_64TargetInfo>>(Triple,
624 Opts);
625 case llvm::Triple::Solaris:
626 return std::make_unique<SolarisTargetInfo<X86_64TargetInfo>>(Triple,
627 Opts);
628 case llvm::Triple::Win32: {
629 switch (Triple.getEnvironment()) {
630 case llvm::Triple::Cygnus:
631 return std::make_unique<CygwinX86_64TargetInfo>(Triple, Opts);
632 case llvm::Triple::GNU:
633 return std::make_unique<MinGWX86_64TargetInfo>(Triple, Opts);
634 case llvm::Triple::MSVC:
635 default: // Assume MSVC for unknown environments
636 return std::make_unique<MicrosoftX86_64TargetInfo>(Triple, Opts);
639 case llvm::Triple::Haiku:
640 return std::make_unique<HaikuTargetInfo<X86_64TargetInfo>>(Triple, Opts);
641 case llvm::Triple::NaCl:
642 return std::make_unique<NaClTargetInfo<X86_64TargetInfo>>(Triple, Opts);
643 case llvm::Triple::PS4:
644 return std::make_unique<PS4OSTargetInfo<X86_64TargetInfo>>(Triple, Opts);
645 case llvm::Triple::PS5:
646 return std::make_unique<PS5OSTargetInfo<X86_64TargetInfo>>(Triple, Opts);
647 default:
648 return std::make_unique<X86_64TargetInfo>(Triple, Opts);
651 case llvm::Triple::spir: {
652 if (os != llvm::Triple::UnknownOS ||
653 Triple.getEnvironment() != llvm::Triple::UnknownEnvironment)
654 return nullptr;
655 return std::make_unique<SPIR32TargetInfo>(Triple, Opts);
657 case llvm::Triple::spir64: {
658 if (os != llvm::Triple::UnknownOS ||
659 Triple.getEnvironment() != llvm::Triple::UnknownEnvironment)
660 return nullptr;
661 return std::make_unique<SPIR64TargetInfo>(Triple, Opts);
663 case llvm::Triple::spirv: {
664 return std::make_unique<SPIRVTargetInfo>(Triple, Opts);
666 case llvm::Triple::spirv32: {
667 if (os != llvm::Triple::UnknownOS ||
668 Triple.getEnvironment() != llvm::Triple::UnknownEnvironment)
669 return nullptr;
670 return std::make_unique<SPIRV32TargetInfo>(Triple, Opts);
672 case llvm::Triple::spirv64: {
673 if (os != llvm::Triple::UnknownOS ||
674 Triple.getEnvironment() != llvm::Triple::UnknownEnvironment)
675 return nullptr;
676 return std::make_unique<SPIRV64TargetInfo>(Triple, Opts);
678 case llvm::Triple::wasm32:
679 if (Triple.getSubArch() != llvm::Triple::NoSubArch ||
680 Triple.getVendor() != llvm::Triple::UnknownVendor ||
681 !Triple.isOSBinFormatWasm())
682 return nullptr;
683 switch (os) {
684 case llvm::Triple::WASI:
685 return std::make_unique<WASITargetInfo<WebAssembly32TargetInfo>>(Triple,
686 Opts);
687 case llvm::Triple::Emscripten:
688 return std::make_unique<EmscriptenTargetInfo<WebAssembly32TargetInfo>>(
689 Triple, Opts);
690 case llvm::Triple::UnknownOS:
691 return std::make_unique<WebAssemblyOSTargetInfo<WebAssembly32TargetInfo>>(
692 Triple, Opts);
693 default:
694 return nullptr;
696 case llvm::Triple::wasm64:
697 if (Triple.getSubArch() != llvm::Triple::NoSubArch ||
698 Triple.getVendor() != llvm::Triple::UnknownVendor ||
699 !Triple.isOSBinFormatWasm())
700 return nullptr;
701 switch (os) {
702 case llvm::Triple::WASI:
703 return std::make_unique<WASITargetInfo<WebAssembly64TargetInfo>>(Triple,
704 Opts);
705 case llvm::Triple::Emscripten:
706 return std::make_unique<EmscriptenTargetInfo<WebAssembly64TargetInfo>>(
707 Triple, Opts);
708 case llvm::Triple::UnknownOS:
709 return std::make_unique<WebAssemblyOSTargetInfo<WebAssembly64TargetInfo>>(
710 Triple, Opts);
711 default:
712 return nullptr;
715 case llvm::Triple::dxil:
716 return std::make_unique<DirectXTargetInfo>(Triple, Opts);
717 case llvm::Triple::renderscript32:
718 return std::make_unique<LinuxTargetInfo<RenderScript32TargetInfo>>(Triple,
719 Opts);
720 case llvm::Triple::renderscript64:
721 return std::make_unique<LinuxTargetInfo<RenderScript64TargetInfo>>(Triple,
722 Opts);
724 case llvm::Triple::ve:
725 return std::make_unique<LinuxTargetInfo<VETargetInfo>>(Triple, Opts);
727 case llvm::Triple::csky:
728 switch (os) {
729 case llvm::Triple::Linux:
730 return std::make_unique<LinuxTargetInfo<CSKYTargetInfo>>(Triple, Opts);
731 default:
732 return std::make_unique<CSKYTargetInfo>(Triple, Opts);
734 case llvm::Triple::loongarch32:
735 switch (os) {
736 case llvm::Triple::Linux:
737 return std::make_unique<LinuxTargetInfo<LoongArch32TargetInfo>>(Triple,
738 Opts);
739 default:
740 return std::make_unique<LoongArch32TargetInfo>(Triple, Opts);
742 case llvm::Triple::loongarch64:
743 switch (os) {
744 case llvm::Triple::Linux:
745 return std::make_unique<LinuxTargetInfo<LoongArch64TargetInfo>>(Triple,
746 Opts);
747 default:
748 return std::make_unique<LoongArch64TargetInfo>(Triple, Opts);
752 } // namespace targets
753 } // namespace clang
755 using namespace clang::targets;
756 /// CreateTargetInfo - Return the target info object for the specified target
757 /// options.
758 TargetInfo *
759 TargetInfo::CreateTargetInfo(DiagnosticsEngine &Diags,
760 const std::shared_ptr<TargetOptions> &Opts) {
761 llvm::Triple Triple(Opts->Triple);
763 // Construct the target
764 std::unique_ptr<TargetInfo> Target = AllocateTarget(Triple, *Opts);
765 if (!Target) {
766 Diags.Report(diag::err_target_unknown_triple) << Triple.str();
767 return nullptr;
769 Target->TargetOpts = Opts;
771 // Set the target CPU if specified.
772 if (!Opts->CPU.empty() && !Target->setCPU(Opts->CPU)) {
773 Diags.Report(diag::err_target_unknown_cpu) << Opts->CPU;
774 SmallVector<StringRef, 32> ValidList;
775 Target->fillValidCPUList(ValidList);
776 if (!ValidList.empty())
777 Diags.Report(diag::note_valid_options) << llvm::join(ValidList, ", ");
778 return nullptr;
781 // Check the TuneCPU name if specified.
782 if (!Opts->TuneCPU.empty() &&
783 !Target->isValidTuneCPUName(Opts->TuneCPU)) {
784 Diags.Report(diag::err_target_unknown_cpu) << Opts->TuneCPU;
785 SmallVector<StringRef, 32> ValidList;
786 Target->fillValidTuneCPUList(ValidList);
787 if (!ValidList.empty())
788 Diags.Report(diag::note_valid_options) << llvm::join(ValidList, ", ");
789 return nullptr;
792 // Set the target ABI if specified.
793 if (!Opts->ABI.empty() && !Target->setABI(Opts->ABI)) {
794 Diags.Report(diag::err_target_unknown_abi) << Opts->ABI;
795 return nullptr;
798 // Set the fp math unit.
799 if (!Opts->FPMath.empty() && !Target->setFPMath(Opts->FPMath)) {
800 Diags.Report(diag::err_target_unknown_fpmath) << Opts->FPMath;
801 return nullptr;
804 // Compute the default target features, we need the target to handle this
805 // because features may have dependencies on one another.
806 llvm::erase_if(Opts->FeaturesAsWritten, [&](StringRef Name) {
807 if (Target->isReadOnlyFeature(Name.substr(1))) {
808 Diags.Report(diag::warn_fe_backend_readonly_feature_flag) << Name;
809 return true;
811 return false;
813 if (!Target->initFeatureMap(Opts->FeatureMap, Diags, Opts->CPU,
814 Opts->FeaturesAsWritten))
815 return nullptr;
817 // Add the features to the compile options.
818 Opts->Features.clear();
819 for (const auto &F : Opts->FeatureMap)
820 Opts->Features.push_back((F.getValue() ? "+" : "-") + F.getKey().str());
821 // Sort here, so we handle the features in a predictable order. (This matters
822 // when we're dealing with features that overlap.)
823 llvm::sort(Opts->Features);
825 if (!Target->handleTargetFeatures(Opts->Features, Diags))
826 return nullptr;
828 Target->setSupportedOpenCLOpts();
829 Target->setCommandLineOpenCLOpts();
830 Target->setMaxAtomicWidth();
832 if (!Opts->DarwinTargetVariantTriple.empty())
833 Target->DarwinTargetVariantTriple =
834 llvm::Triple(Opts->DarwinTargetVariantTriple);
836 if (!Target->validateTarget(Diags))
837 return nullptr;
839 Target->CheckFixedPointBits();
841 return Target.release();
843 /// validateOpenCLTarget - Check that OpenCL target has valid
844 /// options setting based on OpenCL version.
845 bool TargetInfo::validateOpenCLTarget(const LangOptions &Opts,
846 DiagnosticsEngine &Diags) const {
847 const llvm::StringMap<bool> &OpenCLFeaturesMap = getSupportedOpenCLOpts();
849 auto diagnoseNotSupportedCore = [&](llvm::StringRef Name, auto... OptArgs) {
850 if (OpenCLOptions::isOpenCLOptionCoreIn(Opts, OptArgs...) &&
851 !hasFeatureEnabled(OpenCLFeaturesMap, Name))
852 Diags.Report(diag::warn_opencl_unsupported_core_feature)
853 << Name << Opts.OpenCLCPlusPlus
854 << Opts.getOpenCLVersionTuple().getAsString();
856 #define OPENCL_GENERIC_EXTENSION(Ext, ...) \
857 diagnoseNotSupportedCore(#Ext, __VA_ARGS__);
858 #include "clang/Basic/OpenCLExtensions.def"
860 // Validate that feature macros are set properly for OpenCL C 3.0.
861 // In other cases assume that target is always valid.
862 if (Opts.getOpenCLCompatibleVersion() < 300)
863 return true;
865 return OpenCLOptions::diagnoseUnsupportedFeatureDependencies(*this, Diags) &&
866 OpenCLOptions::diagnoseFeatureExtensionDifferences(*this, Diags);