[docs] Fix build-docs.sh
[llvm-project.git] / clang / lib / Basic / Targets.cpp
blob38244800219acd35dbb7d4e5277d372818e0c13f
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 "llvm/ADT/StringExtras.h"
46 #include "llvm/ADT/Triple.h"
48 using namespace clang;
50 namespace clang {
51 namespace targets {
52 //===----------------------------------------------------------------------===//
53 // Common code shared among targets.
54 //===----------------------------------------------------------------------===//
56 /// DefineStd - Define a macro name and standard variants. For example if
57 /// MacroName is "unix", then this will define "__unix", "__unix__", and "unix"
58 /// when in GNU mode.
59 void DefineStd(MacroBuilder &Builder, StringRef MacroName,
60 const LangOptions &Opts) {
61 assert(MacroName[0] != '_' && "Identifier should be in the user's namespace");
63 // If in GNU mode (e.g. -std=gnu99 but not -std=c99) define the raw identifier
64 // in the user's namespace.
65 if (Opts.GNUMode)
66 Builder.defineMacro(MacroName);
68 // Define __unix.
69 Builder.defineMacro("__" + MacroName);
71 // Define __unix__.
72 Builder.defineMacro("__" + MacroName + "__");
75 void defineCPUMacros(MacroBuilder &Builder, StringRef CPUName, bool Tuning) {
76 Builder.defineMacro("__" + CPUName);
77 Builder.defineMacro("__" + CPUName + "__");
78 if (Tuning)
79 Builder.defineMacro("__tune_" + CPUName + "__");
82 void addCygMingDefines(const LangOptions &Opts, MacroBuilder &Builder) {
83 // Mingw and cygwin define __declspec(a) to __attribute__((a)). Clang
84 // supports __declspec natively under -fms-extensions, but we define a no-op
85 // __declspec macro anyway for pre-processor compatibility.
86 if (Opts.MicrosoftExt)
87 Builder.defineMacro("__declspec", "__declspec");
88 else
89 Builder.defineMacro("__declspec(a)", "__attribute__((a))");
91 if (!Opts.MicrosoftExt) {
92 // Provide macros for all the calling convention keywords. Provide both
93 // single and double underscore prefixed variants. These are available on
94 // x64 as well as x86, even though they have no effect.
95 const char *CCs[] = {"cdecl", "stdcall", "fastcall", "thiscall", "pascal"};
96 for (const char *CC : CCs) {
97 std::string GCCSpelling = "__attribute__((__";
98 GCCSpelling += CC;
99 GCCSpelling += "__))";
100 Builder.defineMacro(Twine("_") + CC, GCCSpelling);
101 Builder.defineMacro(Twine("__") + CC, GCCSpelling);
106 //===----------------------------------------------------------------------===//
107 // Driver code
108 //===----------------------------------------------------------------------===//
110 TargetInfo *AllocateTarget(const llvm::Triple &Triple,
111 const TargetOptions &Opts) {
112 llvm::Triple::OSType os = Triple.getOS();
114 switch (Triple.getArch()) {
115 default:
116 return nullptr;
118 case llvm::Triple::arc:
119 return new ARCTargetInfo(Triple, Opts);
121 case llvm::Triple::xcore:
122 return new XCoreTargetInfo(Triple, Opts);
124 case llvm::Triple::hexagon:
125 if (os == llvm::Triple::Linux &&
126 Triple.getEnvironment() == llvm::Triple::Musl)
127 return new LinuxTargetInfo<HexagonTargetInfo>(Triple, Opts);
128 return new HexagonTargetInfo(Triple, Opts);
130 case llvm::Triple::lanai:
131 return new LanaiTargetInfo(Triple, Opts);
133 case llvm::Triple::aarch64_32:
134 if (Triple.isOSDarwin())
135 return new DarwinAArch64TargetInfo(Triple, Opts);
137 return nullptr;
138 case llvm::Triple::aarch64:
139 if (Triple.isOSDarwin())
140 return new DarwinAArch64TargetInfo(Triple, Opts);
142 switch (os) {
143 case llvm::Triple::CloudABI:
144 return new CloudABITargetInfo<AArch64leTargetInfo>(Triple, Opts);
145 case llvm::Triple::FreeBSD:
146 return new FreeBSDTargetInfo<AArch64leTargetInfo>(Triple, Opts);
147 case llvm::Triple::Fuchsia:
148 return new FuchsiaTargetInfo<AArch64leTargetInfo>(Triple, Opts);
149 case llvm::Triple::Linux:
150 return new LinuxTargetInfo<AArch64leTargetInfo>(Triple, Opts);
151 case llvm::Triple::NetBSD:
152 return new NetBSDTargetInfo<AArch64leTargetInfo>(Triple, Opts);
153 case llvm::Triple::OpenBSD:
154 return new OpenBSDTargetInfo<AArch64leTargetInfo>(Triple, Opts);
155 case llvm::Triple::Win32:
156 switch (Triple.getEnvironment()) {
157 case llvm::Triple::GNU:
158 return new MinGWARM64TargetInfo(Triple, Opts);
159 case llvm::Triple::MSVC:
160 default: // Assume MSVC for unknown environments
161 return new MicrosoftARM64TargetInfo(Triple, Opts);
163 default:
164 return new AArch64leTargetInfo(Triple, Opts);
167 case llvm::Triple::aarch64_be:
168 switch (os) {
169 case llvm::Triple::FreeBSD:
170 return new FreeBSDTargetInfo<AArch64beTargetInfo>(Triple, Opts);
171 case llvm::Triple::Fuchsia:
172 return new FuchsiaTargetInfo<AArch64beTargetInfo>(Triple, Opts);
173 case llvm::Triple::Linux:
174 return new LinuxTargetInfo<AArch64beTargetInfo>(Triple, Opts);
175 case llvm::Triple::NetBSD:
176 return new NetBSDTargetInfo<AArch64beTargetInfo>(Triple, Opts);
177 default:
178 return new AArch64beTargetInfo(Triple, Opts);
181 case llvm::Triple::arm:
182 case llvm::Triple::thumb:
183 if (Triple.isOSBinFormatMachO())
184 return new DarwinARMTargetInfo(Triple, Opts);
186 switch (os) {
187 case llvm::Triple::CloudABI:
188 return new CloudABITargetInfo<ARMleTargetInfo>(Triple, Opts);
189 case llvm::Triple::Linux:
190 return new LinuxTargetInfo<ARMleTargetInfo>(Triple, Opts);
191 case llvm::Triple::FreeBSD:
192 return new FreeBSDTargetInfo<ARMleTargetInfo>(Triple, Opts);
193 case llvm::Triple::NetBSD:
194 return new NetBSDTargetInfo<ARMleTargetInfo>(Triple, Opts);
195 case llvm::Triple::OpenBSD:
196 return new OpenBSDTargetInfo<ARMleTargetInfo>(Triple, Opts);
197 case llvm::Triple::RTEMS:
198 return new RTEMSTargetInfo<ARMleTargetInfo>(Triple, Opts);
199 case llvm::Triple::NaCl:
200 return new NaClTargetInfo<ARMleTargetInfo>(Triple, Opts);
201 case llvm::Triple::Win32:
202 switch (Triple.getEnvironment()) {
203 case llvm::Triple::Cygnus:
204 return new CygwinARMTargetInfo(Triple, Opts);
205 case llvm::Triple::GNU:
206 return new MinGWARMTargetInfo(Triple, Opts);
207 case llvm::Triple::Itanium:
208 return new ItaniumWindowsARMleTargetInfo(Triple, Opts);
209 case llvm::Triple::MSVC:
210 default: // Assume MSVC for unknown environments
211 return new MicrosoftARMleTargetInfo(Triple, Opts);
213 default:
214 return new ARMleTargetInfo(Triple, Opts);
217 case llvm::Triple::armeb:
218 case llvm::Triple::thumbeb:
219 if (Triple.isOSDarwin())
220 return new DarwinARMTargetInfo(Triple, Opts);
222 switch (os) {
223 case llvm::Triple::Linux:
224 return new LinuxTargetInfo<ARMbeTargetInfo>(Triple, Opts);
225 case llvm::Triple::FreeBSD:
226 return new FreeBSDTargetInfo<ARMbeTargetInfo>(Triple, Opts);
227 case llvm::Triple::NetBSD:
228 return new NetBSDTargetInfo<ARMbeTargetInfo>(Triple, Opts);
229 case llvm::Triple::OpenBSD:
230 return new OpenBSDTargetInfo<ARMbeTargetInfo>(Triple, Opts);
231 case llvm::Triple::RTEMS:
232 return new RTEMSTargetInfo<ARMbeTargetInfo>(Triple, Opts);
233 case llvm::Triple::NaCl:
234 return new NaClTargetInfo<ARMbeTargetInfo>(Triple, Opts);
235 default:
236 return new ARMbeTargetInfo(Triple, Opts);
239 case llvm::Triple::avr:
240 return new AVRTargetInfo(Triple, Opts);
241 case llvm::Triple::bpfeb:
242 case llvm::Triple::bpfel:
243 return new BPFTargetInfo(Triple, Opts);
245 case llvm::Triple::msp430:
246 return new MSP430TargetInfo(Triple, Opts);
248 case llvm::Triple::mips:
249 switch (os) {
250 case llvm::Triple::Linux:
251 return new LinuxTargetInfo<MipsTargetInfo>(Triple, Opts);
252 case llvm::Triple::RTEMS:
253 return new RTEMSTargetInfo<MipsTargetInfo>(Triple, Opts);
254 case llvm::Triple::FreeBSD:
255 return new FreeBSDTargetInfo<MipsTargetInfo>(Triple, Opts);
256 case llvm::Triple::NetBSD:
257 return new NetBSDTargetInfo<MipsTargetInfo>(Triple, Opts);
258 default:
259 return new MipsTargetInfo(Triple, Opts);
262 case llvm::Triple::mipsel:
263 switch (os) {
264 case llvm::Triple::Linux:
265 return new LinuxTargetInfo<MipsTargetInfo>(Triple, Opts);
266 case llvm::Triple::RTEMS:
267 return new RTEMSTargetInfo<MipsTargetInfo>(Triple, Opts);
268 case llvm::Triple::FreeBSD:
269 return new FreeBSDTargetInfo<MipsTargetInfo>(Triple, Opts);
270 case llvm::Triple::NetBSD:
271 return new NetBSDTargetInfo<MipsTargetInfo>(Triple, Opts);
272 case llvm::Triple::NaCl:
273 return new NaClTargetInfo<NaClMips32TargetInfo>(Triple, Opts);
274 default:
275 return new MipsTargetInfo(Triple, Opts);
278 case llvm::Triple::mips64:
279 switch (os) {
280 case llvm::Triple::Linux:
281 return new LinuxTargetInfo<MipsTargetInfo>(Triple, Opts);
282 case llvm::Triple::RTEMS:
283 return new RTEMSTargetInfo<MipsTargetInfo>(Triple, Opts);
284 case llvm::Triple::FreeBSD:
285 return new FreeBSDTargetInfo<MipsTargetInfo>(Triple, Opts);
286 case llvm::Triple::NetBSD:
287 return new NetBSDTargetInfo<MipsTargetInfo>(Triple, Opts);
288 case llvm::Triple::OpenBSD:
289 return new OpenBSDTargetInfo<MipsTargetInfo>(Triple, Opts);
290 default:
291 return new MipsTargetInfo(Triple, Opts);
294 case llvm::Triple::mips64el:
295 switch (os) {
296 case llvm::Triple::Linux:
297 return new LinuxTargetInfo<MipsTargetInfo>(Triple, Opts);
298 case llvm::Triple::RTEMS:
299 return new RTEMSTargetInfo<MipsTargetInfo>(Triple, Opts);
300 case llvm::Triple::FreeBSD:
301 return new FreeBSDTargetInfo<MipsTargetInfo>(Triple, Opts);
302 case llvm::Triple::NetBSD:
303 return new NetBSDTargetInfo<MipsTargetInfo>(Triple, Opts);
304 case llvm::Triple::OpenBSD:
305 return new OpenBSDTargetInfo<MipsTargetInfo>(Triple, Opts);
306 default:
307 return new MipsTargetInfo(Triple, Opts);
310 case llvm::Triple::m68k:
311 switch (os) {
312 case llvm::Triple::Linux:
313 return new LinuxTargetInfo<M68kTargetInfo>(Triple, Opts);
314 case llvm::Triple::NetBSD:
315 return new NetBSDTargetInfo<M68kTargetInfo>(Triple, Opts);
316 default:
317 return new M68kTargetInfo(Triple, Opts);
320 case llvm::Triple::le32:
321 switch (os) {
322 case llvm::Triple::NaCl:
323 return new NaClTargetInfo<PNaClTargetInfo>(Triple, Opts);
324 default:
325 return nullptr;
328 case llvm::Triple::le64:
329 return new Le64TargetInfo(Triple, Opts);
331 case llvm::Triple::ppc:
332 if (Triple.isOSDarwin())
333 return new DarwinPPC32TargetInfo(Triple, Opts);
334 switch (os) {
335 case llvm::Triple::Linux:
336 return new LinuxTargetInfo<PPC32TargetInfo>(Triple, Opts);
337 case llvm::Triple::FreeBSD:
338 return new FreeBSDTargetInfo<PPC32TargetInfo>(Triple, Opts);
339 case llvm::Triple::NetBSD:
340 return new NetBSDTargetInfo<PPC32TargetInfo>(Triple, Opts);
341 case llvm::Triple::OpenBSD:
342 return new OpenBSDTargetInfo<PPC32TargetInfo>(Triple, Opts);
343 case llvm::Triple::RTEMS:
344 return new RTEMSTargetInfo<PPC32TargetInfo>(Triple, Opts);
345 case llvm::Triple::AIX:
346 return new AIXPPC32TargetInfo(Triple, Opts);
347 default:
348 return new PPC32TargetInfo(Triple, Opts);
351 case llvm::Triple::ppcle:
352 switch (os) {
353 case llvm::Triple::Linux:
354 return new LinuxTargetInfo<PPC32TargetInfo>(Triple, Opts);
355 case llvm::Triple::FreeBSD:
356 return new FreeBSDTargetInfo<PPC32TargetInfo>(Triple, Opts);
357 default:
358 return new PPC32TargetInfo(Triple, Opts);
361 case llvm::Triple::ppc64:
362 if (Triple.isOSDarwin())
363 return new DarwinPPC64TargetInfo(Triple, Opts);
364 switch (os) {
365 case llvm::Triple::Linux:
366 return new LinuxTargetInfo<PPC64TargetInfo>(Triple, Opts);
367 case llvm::Triple::Lv2:
368 return new PS3PPUTargetInfo<PPC64TargetInfo>(Triple, Opts);
369 case llvm::Triple::FreeBSD:
370 return new FreeBSDTargetInfo<PPC64TargetInfo>(Triple, Opts);
371 case llvm::Triple::NetBSD:
372 return new NetBSDTargetInfo<PPC64TargetInfo>(Triple, Opts);
373 case llvm::Triple::OpenBSD:
374 return new OpenBSDTargetInfo<PPC64TargetInfo>(Triple, Opts);
375 case llvm::Triple::AIX:
376 return new AIXPPC64TargetInfo(Triple, Opts);
377 default:
378 return new PPC64TargetInfo(Triple, Opts);
381 case llvm::Triple::ppc64le:
382 switch (os) {
383 case llvm::Triple::Linux:
384 return new LinuxTargetInfo<PPC64TargetInfo>(Triple, Opts);
385 case llvm::Triple::FreeBSD:
386 return new FreeBSDTargetInfo<PPC64TargetInfo>(Triple, Opts);
387 case llvm::Triple::NetBSD:
388 return new NetBSDTargetInfo<PPC64TargetInfo>(Triple, Opts);
389 case llvm::Triple::OpenBSD:
390 return new OpenBSDTargetInfo<PPC64TargetInfo>(Triple, Opts);
391 default:
392 return new PPC64TargetInfo(Triple, Opts);
395 case llvm::Triple::nvptx:
396 return new NVPTXTargetInfo(Triple, Opts, /*TargetPointerWidth=*/32);
397 case llvm::Triple::nvptx64:
398 return new NVPTXTargetInfo(Triple, Opts, /*TargetPointerWidth=*/64);
400 case llvm::Triple::amdgcn:
401 case llvm::Triple::r600:
402 return new AMDGPUTargetInfo(Triple, Opts);
404 case llvm::Triple::riscv32:
405 // TODO: add cases for NetBSD, RTEMS once tested.
406 switch (os) {
407 case llvm::Triple::FreeBSD:
408 return new FreeBSDTargetInfo<RISCV32TargetInfo>(Triple, Opts);
409 case llvm::Triple::Linux:
410 return new LinuxTargetInfo<RISCV32TargetInfo>(Triple, Opts);
411 default:
412 return new RISCV32TargetInfo(Triple, Opts);
415 case llvm::Triple::riscv64:
416 // TODO: add cases for NetBSD, RTEMS once tested.
417 switch (os) {
418 case llvm::Triple::FreeBSD:
419 return new FreeBSDTargetInfo<RISCV64TargetInfo>(Triple, Opts);
420 case llvm::Triple::OpenBSD:
421 return new OpenBSDTargetInfo<RISCV64TargetInfo>(Triple, Opts);
422 case llvm::Triple::Fuchsia:
423 return new FuchsiaTargetInfo<RISCV64TargetInfo>(Triple, Opts);
424 case llvm::Triple::Linux:
425 return new LinuxTargetInfo<RISCV64TargetInfo>(Triple, Opts);
426 default:
427 return new RISCV64TargetInfo(Triple, Opts);
430 case llvm::Triple::sparc:
431 switch (os) {
432 case llvm::Triple::Linux:
433 return new LinuxTargetInfo<SparcV8TargetInfo>(Triple, Opts);
434 case llvm::Triple::Solaris:
435 return new SolarisTargetInfo<SparcV8TargetInfo>(Triple, Opts);
436 case llvm::Triple::NetBSD:
437 return new NetBSDTargetInfo<SparcV8TargetInfo>(Triple, Opts);
438 case llvm::Triple::RTEMS:
439 return new RTEMSTargetInfo<SparcV8TargetInfo>(Triple, Opts);
440 default:
441 return new SparcV8TargetInfo(Triple, Opts);
444 // The 'sparcel' architecture copies all the above cases except for Solaris.
445 case llvm::Triple::sparcel:
446 switch (os) {
447 case llvm::Triple::Linux:
448 return new LinuxTargetInfo<SparcV8elTargetInfo>(Triple, Opts);
449 case llvm::Triple::NetBSD:
450 return new NetBSDTargetInfo<SparcV8elTargetInfo>(Triple, Opts);
451 case llvm::Triple::RTEMS:
452 return new RTEMSTargetInfo<SparcV8elTargetInfo>(Triple, Opts);
453 default:
454 return new SparcV8elTargetInfo(Triple, Opts);
457 case llvm::Triple::sparcv9:
458 switch (os) {
459 case llvm::Triple::Linux:
460 return new LinuxTargetInfo<SparcV9TargetInfo>(Triple, Opts);
461 case llvm::Triple::Solaris:
462 return new SolarisTargetInfo<SparcV9TargetInfo>(Triple, Opts);
463 case llvm::Triple::NetBSD:
464 return new NetBSDTargetInfo<SparcV9TargetInfo>(Triple, Opts);
465 case llvm::Triple::OpenBSD:
466 return new OpenBSDTargetInfo<SparcV9TargetInfo>(Triple, Opts);
467 case llvm::Triple::FreeBSD:
468 return new FreeBSDTargetInfo<SparcV9TargetInfo>(Triple, Opts);
469 default:
470 return new SparcV9TargetInfo(Triple, Opts);
473 case llvm::Triple::systemz:
474 switch (os) {
475 case llvm::Triple::Linux:
476 return new LinuxTargetInfo<SystemZTargetInfo>(Triple, Opts);
477 case llvm::Triple::ZOS:
478 return new ZOSTargetInfo<SystemZTargetInfo>(Triple, Opts);
479 default:
480 return new SystemZTargetInfo(Triple, Opts);
483 case llvm::Triple::tce:
484 return new TCETargetInfo(Triple, Opts);
486 case llvm::Triple::tcele:
487 return new TCELETargetInfo(Triple, Opts);
489 case llvm::Triple::x86:
490 if (Triple.isOSDarwin())
491 return new DarwinI386TargetInfo(Triple, Opts);
493 switch (os) {
494 case llvm::Triple::Ananas:
495 return new AnanasTargetInfo<X86_32TargetInfo>(Triple, Opts);
496 case llvm::Triple::CloudABI:
497 return new CloudABITargetInfo<X86_32TargetInfo>(Triple, Opts);
498 case llvm::Triple::Linux: {
499 switch (Triple.getEnvironment()) {
500 default:
501 return new LinuxTargetInfo<X86_32TargetInfo>(Triple, Opts);
502 case llvm::Triple::Android:
503 return new AndroidX86_32TargetInfo(Triple, Opts);
506 case llvm::Triple::DragonFly:
507 return new DragonFlyBSDTargetInfo<X86_32TargetInfo>(Triple, Opts);
508 case llvm::Triple::NetBSD:
509 return new NetBSDI386TargetInfo(Triple, Opts);
510 case llvm::Triple::OpenBSD:
511 return new OpenBSDI386TargetInfo(Triple, Opts);
512 case llvm::Triple::FreeBSD:
513 return new FreeBSDTargetInfo<X86_32TargetInfo>(Triple, Opts);
514 case llvm::Triple::Fuchsia:
515 return new FuchsiaTargetInfo<X86_32TargetInfo>(Triple, Opts);
516 case llvm::Triple::KFreeBSD:
517 return new KFreeBSDTargetInfo<X86_32TargetInfo>(Triple, Opts);
518 case llvm::Triple::Minix:
519 return new MinixTargetInfo<X86_32TargetInfo>(Triple, Opts);
520 case llvm::Triple::Solaris:
521 return new SolarisTargetInfo<X86_32TargetInfo>(Triple, Opts);
522 case llvm::Triple::Win32: {
523 switch (Triple.getEnvironment()) {
524 case llvm::Triple::Cygnus:
525 return new CygwinX86_32TargetInfo(Triple, Opts);
526 case llvm::Triple::GNU:
527 return new MinGWX86_32TargetInfo(Triple, Opts);
528 case llvm::Triple::Itanium:
529 case llvm::Triple::MSVC:
530 default: // Assume MSVC for unknown environments
531 return new MicrosoftX86_32TargetInfo(Triple, Opts);
534 case llvm::Triple::Haiku:
535 return new HaikuX86_32TargetInfo(Triple, Opts);
536 case llvm::Triple::RTEMS:
537 return new RTEMSX86_32TargetInfo(Triple, Opts);
538 case llvm::Triple::NaCl:
539 return new NaClTargetInfo<X86_32TargetInfo>(Triple, Opts);
540 case llvm::Triple::ELFIAMCU:
541 return new MCUX86_32TargetInfo(Triple, Opts);
542 case llvm::Triple::Hurd:
543 return new HurdTargetInfo<X86_32TargetInfo>(Triple, Opts);
544 default:
545 return new X86_32TargetInfo(Triple, Opts);
548 case llvm::Triple::x86_64:
549 if (Triple.isOSDarwin() || Triple.isOSBinFormatMachO())
550 return new DarwinX86_64TargetInfo(Triple, Opts);
552 switch (os) {
553 case llvm::Triple::Ananas:
554 return new AnanasTargetInfo<X86_64TargetInfo>(Triple, Opts);
555 case llvm::Triple::CloudABI:
556 return new CloudABITargetInfo<X86_64TargetInfo>(Triple, Opts);
557 case llvm::Triple::Linux: {
558 switch (Triple.getEnvironment()) {
559 default:
560 return new LinuxTargetInfo<X86_64TargetInfo>(Triple, Opts);
561 case llvm::Triple::Android:
562 return new AndroidX86_64TargetInfo(Triple, Opts);
565 case llvm::Triple::DragonFly:
566 return new DragonFlyBSDTargetInfo<X86_64TargetInfo>(Triple, Opts);
567 case llvm::Triple::NetBSD:
568 return new NetBSDTargetInfo<X86_64TargetInfo>(Triple, Opts);
569 case llvm::Triple::OpenBSD:
570 return new OpenBSDX86_64TargetInfo(Triple, Opts);
571 case llvm::Triple::FreeBSD:
572 return new FreeBSDTargetInfo<X86_64TargetInfo>(Triple, Opts);
573 case llvm::Triple::Fuchsia:
574 return new FuchsiaTargetInfo<X86_64TargetInfo>(Triple, Opts);
575 case llvm::Triple::KFreeBSD:
576 return new KFreeBSDTargetInfo<X86_64TargetInfo>(Triple, Opts);
577 case llvm::Triple::Solaris:
578 return new SolarisTargetInfo<X86_64TargetInfo>(Triple, Opts);
579 case llvm::Triple::Win32: {
580 switch (Triple.getEnvironment()) {
581 case llvm::Triple::Cygnus:
582 return new CygwinX86_64TargetInfo(Triple, Opts);
583 case llvm::Triple::GNU:
584 return new MinGWX86_64TargetInfo(Triple, Opts);
585 case llvm::Triple::MSVC:
586 default: // Assume MSVC for unknown environments
587 return new MicrosoftX86_64TargetInfo(Triple, Opts);
590 case llvm::Triple::Haiku:
591 return new HaikuTargetInfo<X86_64TargetInfo>(Triple, Opts);
592 case llvm::Triple::NaCl:
593 return new NaClTargetInfo<X86_64TargetInfo>(Triple, Opts);
594 case llvm::Triple::PS4:
595 return new PS4OSTargetInfo<X86_64TargetInfo>(Triple, Opts);
596 case llvm::Triple::PS5:
597 return new PS5OSTargetInfo<X86_64TargetInfo>(Triple, Opts);
598 default:
599 return new X86_64TargetInfo(Triple, Opts);
602 case llvm::Triple::spir: {
603 if (os != llvm::Triple::UnknownOS ||
604 Triple.getEnvironment() != llvm::Triple::UnknownEnvironment)
605 return nullptr;
606 return new SPIR32TargetInfo(Triple, Opts);
608 case llvm::Triple::spir64: {
609 if (os != llvm::Triple::UnknownOS ||
610 Triple.getEnvironment() != llvm::Triple::UnknownEnvironment)
611 return nullptr;
612 return new SPIR64TargetInfo(Triple, Opts);
614 case llvm::Triple::spirv32: {
615 if (os != llvm::Triple::UnknownOS ||
616 Triple.getEnvironment() != llvm::Triple::UnknownEnvironment)
617 return nullptr;
618 return new SPIRV32TargetInfo(Triple, Opts);
620 case llvm::Triple::spirv64: {
621 if (os != llvm::Triple::UnknownOS ||
622 Triple.getEnvironment() != llvm::Triple::UnknownEnvironment)
623 return nullptr;
624 return new SPIRV64TargetInfo(Triple, Opts);
626 case llvm::Triple::wasm32:
627 if (Triple.getSubArch() != llvm::Triple::NoSubArch ||
628 Triple.getVendor() != llvm::Triple::UnknownVendor ||
629 !Triple.isOSBinFormatWasm())
630 return nullptr;
631 switch (os) {
632 case llvm::Triple::WASI:
633 return new WASITargetInfo<WebAssembly32TargetInfo>(Triple, Opts);
634 case llvm::Triple::Emscripten:
635 return new EmscriptenTargetInfo<WebAssembly32TargetInfo>(Triple, Opts);
636 case llvm::Triple::UnknownOS:
637 return new WebAssemblyOSTargetInfo<WebAssembly32TargetInfo>(Triple, Opts);
638 default:
639 return nullptr;
641 case llvm::Triple::wasm64:
642 if (Triple.getSubArch() != llvm::Triple::NoSubArch ||
643 Triple.getVendor() != llvm::Triple::UnknownVendor ||
644 !Triple.isOSBinFormatWasm())
645 return nullptr;
646 switch (os) {
647 case llvm::Triple::WASI:
648 return new WASITargetInfo<WebAssembly64TargetInfo>(Triple, Opts);
649 case llvm::Triple::Emscripten:
650 return new EmscriptenTargetInfo<WebAssembly64TargetInfo>(Triple, Opts);
651 case llvm::Triple::UnknownOS:
652 return new WebAssemblyOSTargetInfo<WebAssembly64TargetInfo>(Triple, Opts);
653 default:
654 return nullptr;
657 case llvm::Triple::dxil:
658 return new DirectXTargetInfo(Triple,Opts);
659 case llvm::Triple::renderscript32:
660 return new LinuxTargetInfo<RenderScript32TargetInfo>(Triple, Opts);
661 case llvm::Triple::renderscript64:
662 return new LinuxTargetInfo<RenderScript64TargetInfo>(Triple, Opts);
664 case llvm::Triple::ve:
665 return new LinuxTargetInfo<VETargetInfo>(Triple, Opts);
667 case llvm::Triple::csky:
668 switch (os) {
669 case llvm::Triple::Linux:
670 return new LinuxTargetInfo<CSKYTargetInfo>(Triple, Opts);
671 default:
672 return new CSKYTargetInfo(Triple, Opts);
674 case llvm::Triple::loongarch32:
675 switch (os) {
676 case llvm::Triple::Linux:
677 return new LinuxTargetInfo<LoongArch32TargetInfo>(Triple, Opts);
678 default:
679 return new LoongArch32TargetInfo(Triple, Opts);
681 case llvm::Triple::loongarch64:
682 switch (os) {
683 case llvm::Triple::Linux:
684 return new LinuxTargetInfo<LoongArch64TargetInfo>(Triple, Opts);
685 default:
686 return new LoongArch64TargetInfo(Triple, Opts);
690 } // namespace targets
691 } // namespace clang
693 using namespace clang::targets;
694 /// CreateTargetInfo - Return the target info object for the specified target
695 /// options.
696 TargetInfo *
697 TargetInfo::CreateTargetInfo(DiagnosticsEngine &Diags,
698 const std::shared_ptr<TargetOptions> &Opts) {
699 llvm::Triple Triple(Opts->Triple);
701 // Construct the target
702 std::unique_ptr<TargetInfo> Target(AllocateTarget(Triple, *Opts));
703 if (!Target) {
704 Diags.Report(diag::err_target_unknown_triple) << Triple.str();
705 return nullptr;
707 Target->TargetOpts = Opts;
709 // Set the target CPU if specified.
710 if (!Opts->CPU.empty() && !Target->setCPU(Opts->CPU)) {
711 Diags.Report(diag::err_target_unknown_cpu) << Opts->CPU;
712 SmallVector<StringRef, 32> ValidList;
713 Target->fillValidCPUList(ValidList);
714 if (!ValidList.empty())
715 Diags.Report(diag::note_valid_options) << llvm::join(ValidList, ", ");
716 return nullptr;
719 // Check the TuneCPU name if specified.
720 if (!Opts->TuneCPU.empty() &&
721 !Target->isValidTuneCPUName(Opts->TuneCPU)) {
722 Diags.Report(diag::err_target_unknown_cpu) << Opts->TuneCPU;
723 SmallVector<StringRef, 32> ValidList;
724 Target->fillValidTuneCPUList(ValidList);
725 if (!ValidList.empty())
726 Diags.Report(diag::note_valid_options) << llvm::join(ValidList, ", ");
727 return nullptr;
730 // Set the target ABI if specified.
731 if (!Opts->ABI.empty() && !Target->setABI(Opts->ABI)) {
732 Diags.Report(diag::err_target_unknown_abi) << Opts->ABI;
733 return nullptr;
736 // Set the fp math unit.
737 if (!Opts->FPMath.empty() && !Target->setFPMath(Opts->FPMath)) {
738 Diags.Report(diag::err_target_unknown_fpmath) << Opts->FPMath;
739 return nullptr;
742 // Compute the default target features, we need the target to handle this
743 // because features may have dependencies on one another.
744 if (!Target->initFeatureMap(Opts->FeatureMap, Diags, Opts->CPU,
745 Opts->FeaturesAsWritten))
746 return nullptr;
748 // Add the features to the compile options.
749 Opts->Features.clear();
750 for (const auto &F : Opts->FeatureMap)
751 Opts->Features.push_back((F.getValue() ? "+" : "-") + F.getKey().str());
752 // Sort here, so we handle the features in a predictable order. (This matters
753 // when we're dealing with features that overlap.)
754 llvm::sort(Opts->Features);
756 if (!Target->handleTargetFeatures(Opts->Features, Diags))
757 return nullptr;
759 Target->setSupportedOpenCLOpts();
760 Target->setCommandLineOpenCLOpts();
761 Target->setMaxAtomicWidth();
763 if (!Opts->DarwinTargetVariantTriple.empty())
764 Target->DarwinTargetVariantTriple =
765 llvm::Triple(Opts->DarwinTargetVariantTriple);
767 if (!Target->validateTarget(Diags))
768 return nullptr;
770 Target->CheckFixedPointBits();
772 return Target.release();
774 /// validateOpenCLTarget - Check that OpenCL target has valid
775 /// options setting based on OpenCL version.
776 bool TargetInfo::validateOpenCLTarget(const LangOptions &Opts,
777 DiagnosticsEngine &Diags) const {
778 const llvm::StringMap<bool> &OpenCLFeaturesMap = getSupportedOpenCLOpts();
780 auto diagnoseNotSupportedCore = [&](llvm::StringRef Name, auto... OptArgs) {
781 if (OpenCLOptions::isOpenCLOptionCoreIn(Opts, OptArgs...) &&
782 !hasFeatureEnabled(OpenCLFeaturesMap, Name))
783 Diags.Report(diag::warn_opencl_unsupported_core_feature)
784 << Name << Opts.OpenCLCPlusPlus
785 << Opts.getOpenCLVersionTuple().getAsString();
787 #define OPENCL_GENERIC_EXTENSION(Ext, ...) \
788 diagnoseNotSupportedCore(#Ext, __VA_ARGS__);
789 #include "clang/Basic/OpenCLExtensions.def"
791 // Validate that feature macros are set properly for OpenCL C 3.0.
792 // In other cases assume that target is always valid.
793 if (Opts.getOpenCLCompatibleVersion() < 300)
794 return true;
796 return OpenCLOptions::diagnoseUnsupportedFeatureDependencies(*this, Diags) &&
797 OpenCLOptions::diagnoseFeatureExtensionDifferences(*this, Diags);