xref: /freebsd/contrib/llvm-project/clang/lib/Basic/Targets.cpp (revision 59c8e88e72633afbc47a4ace0d2170d00d51f7dc)
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.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "Targets.h"
15 
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"
48 
49 using namespace clang;
50 
51 namespace clang {
52 namespace targets {
53 //===----------------------------------------------------------------------===//
54 //  Common code shared among targets.
55 //===----------------------------------------------------------------------===//
56 
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");
63 
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);
68 
69   // Define __unix.
70   Builder.defineMacro("__" + MacroName);
71 
72   // Define __unix__.
73   Builder.defineMacro("__" + MacroName + "__");
74 }
75 
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 + "__");
81 }
82 
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))");
92 
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);
104     }
105   }
106 }
107 
108 //===----------------------------------------------------------------------===//
109 // Driver code
110 //===----------------------------------------------------------------------===//
111 
112 std::unique_ptr<TargetInfo> AllocateTarget(const llvm::Triple &Triple,
113                                            const TargetOptions &Opts) {
114   llvm::Triple::OSType os = Triple.getOS();
115 
116   switch (Triple.getArch()) {
117   default:
118     return nullptr;
119 
120   case llvm::Triple::arc:
121     return std::make_unique<ARCTargetInfo>(Triple, Opts);
122 
123   case llvm::Triple::xcore:
124     return std::make_unique<XCoreTargetInfo>(Triple, Opts);
125 
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);
131 
132   case llvm::Triple::lanai:
133     return std::make_unique<LanaiTargetInfo>(Triple, Opts);
134 
135   case llvm::Triple::aarch64_32:
136     if (Triple.isOSDarwin())
137       return std::make_unique<DarwinAArch64TargetInfo>(Triple, Opts);
138 
139     return nullptr;
140   case llvm::Triple::aarch64:
141     if (Triple.isOSDarwin())
142       return std::make_unique<DarwinAArch64TargetInfo>(Triple, Opts);
143 
144     switch (os) {
145     case llvm::Triple::CloudABI:
146       return std::make_unique<CloudABITargetInfo<AArch64leTargetInfo>>(Triple,
147                                                                        Opts);
148     case llvm::Triple::FreeBSD:
149       return std::make_unique<FreeBSDTargetInfo<AArch64leTargetInfo>>(Triple,
150                                                                       Opts);
151     case llvm::Triple::Fuchsia:
152       return std::make_unique<FuchsiaTargetInfo<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);
162       }
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);
176       }
177     default:
178       return std::make_unique<AArch64leTargetInfo>(Triple, Opts);
179     }
180 
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);
197     }
198 
199   case llvm::Triple::arm:
200   case llvm::Triple::thumb:
201     if (Triple.isOSBinFormatMachO())
202       return std::make_unique<DarwinARMTargetInfo>(Triple, Opts);
203 
204     switch (os) {
205     case llvm::Triple::CloudABI:
206       return std::make_unique<CloudABITargetInfo<ARMleTargetInfo>>(Triple,
207                                                                    Opts);
208     case llvm::Triple::Linux:
209       switch (Triple.getEnvironment()) {
210       default:
211         return std::make_unique<LinuxTargetInfo<ARMleTargetInfo>>(Triple, Opts);
212       case llvm::Triple::OpenHOS:
213         return std::make_unique<OHOSTargetInfo<ARMleTargetInfo>>(Triple, Opts);
214       }
215     case llvm::Triple::LiteOS:
216       return std::make_unique<OHOSTargetInfo<ARMleTargetInfo>>(Triple, Opts);
217     case llvm::Triple::FreeBSD:
218       return std::make_unique<FreeBSDTargetInfo<ARMleTargetInfo>>(Triple, Opts);
219     case llvm::Triple::NetBSD:
220       return std::make_unique<NetBSDTargetInfo<ARMleTargetInfo>>(Triple, Opts);
221     case llvm::Triple::OpenBSD:
222       return std::make_unique<OpenBSDTargetInfo<ARMleTargetInfo>>(Triple, Opts);
223     case llvm::Triple::RTEMS:
224       return std::make_unique<RTEMSTargetInfo<ARMleTargetInfo>>(Triple, Opts);
225     case llvm::Triple::NaCl:
226       return std::make_unique<NaClTargetInfo<ARMleTargetInfo>>(Triple, Opts);
227     case llvm::Triple::Win32:
228       switch (Triple.getEnvironment()) {
229       case llvm::Triple::Cygnus:
230         return std::make_unique<CygwinARMTargetInfo>(Triple, Opts);
231       case llvm::Triple::GNU:
232         return std::make_unique<MinGWARMTargetInfo>(Triple, Opts);
233       case llvm::Triple::Itanium:
234         return std::make_unique<ItaniumWindowsARMleTargetInfo>(Triple, Opts);
235       case llvm::Triple::MSVC:
236       default: // Assume MSVC for unknown environments
237         return std::make_unique<MicrosoftARMleTargetInfo>(Triple, Opts);
238       }
239     default:
240       return std::make_unique<ARMleTargetInfo>(Triple, Opts);
241     }
242 
243   case llvm::Triple::armeb:
244   case llvm::Triple::thumbeb:
245     if (Triple.isOSDarwin())
246       return std::make_unique<DarwinARMTargetInfo>(Triple, Opts);
247 
248     switch (os) {
249     case llvm::Triple::Linux:
250       return std::make_unique<LinuxTargetInfo<ARMbeTargetInfo>>(Triple, Opts);
251     case llvm::Triple::FreeBSD:
252       return std::make_unique<FreeBSDTargetInfo<ARMbeTargetInfo>>(Triple, Opts);
253     case llvm::Triple::NetBSD:
254       return std::make_unique<NetBSDTargetInfo<ARMbeTargetInfo>>(Triple, Opts);
255     case llvm::Triple::OpenBSD:
256       return std::make_unique<OpenBSDTargetInfo<ARMbeTargetInfo>>(Triple, Opts);
257     case llvm::Triple::RTEMS:
258       return std::make_unique<RTEMSTargetInfo<ARMbeTargetInfo>>(Triple, Opts);
259     case llvm::Triple::NaCl:
260       return std::make_unique<NaClTargetInfo<ARMbeTargetInfo>>(Triple, Opts);
261     default:
262       return std::make_unique<ARMbeTargetInfo>(Triple, Opts);
263     }
264 
265   case llvm::Triple::avr:
266     return std::make_unique<AVRTargetInfo>(Triple, Opts);
267   case llvm::Triple::bpfeb:
268   case llvm::Triple::bpfel:
269     return std::make_unique<BPFTargetInfo>(Triple, Opts);
270 
271   case llvm::Triple::msp430:
272     return std::make_unique<MSP430TargetInfo>(Triple, Opts);
273 
274   case llvm::Triple::mips:
275     switch (os) {
276     case llvm::Triple::Linux:
277       return std::make_unique<LinuxTargetInfo<MipsTargetInfo>>(Triple, Opts);
278     case llvm::Triple::RTEMS:
279       return std::make_unique<RTEMSTargetInfo<MipsTargetInfo>>(Triple, Opts);
280     case llvm::Triple::FreeBSD:
281       return std::make_unique<FreeBSDTargetInfo<MipsTargetInfo>>(Triple, Opts);
282     case llvm::Triple::NetBSD:
283       return std::make_unique<NetBSDTargetInfo<MipsTargetInfo>>(Triple, Opts);
284     default:
285       return std::make_unique<MipsTargetInfo>(Triple, Opts);
286     }
287 
288   case llvm::Triple::mipsel:
289     switch (os) {
290     case llvm::Triple::Linux:
291       switch (Triple.getEnvironment()) {
292       default:
293         return std::make_unique<LinuxTargetInfo<MipsTargetInfo>>(Triple, Opts);
294       case llvm::Triple::OpenHOS:
295         return std::make_unique<OHOSTargetInfo<MipsTargetInfo>>(Triple, Opts);
296       }
297     case llvm::Triple::RTEMS:
298       return std::make_unique<RTEMSTargetInfo<MipsTargetInfo>>(Triple, Opts);
299     case llvm::Triple::FreeBSD:
300       return std::make_unique<FreeBSDTargetInfo<MipsTargetInfo>>(Triple, Opts);
301     case llvm::Triple::NetBSD:
302       return std::make_unique<NetBSDTargetInfo<MipsTargetInfo>>(Triple, Opts);
303     case llvm::Triple::NaCl:
304       return std::make_unique<NaClTargetInfo<NaClMips32TargetInfo>>(Triple,
305                                                                     Opts);
306     default:
307       return std::make_unique<MipsTargetInfo>(Triple, Opts);
308     }
309 
310   case llvm::Triple::mips64:
311     switch (os) {
312     case llvm::Triple::Linux:
313       return std::make_unique<LinuxTargetInfo<MipsTargetInfo>>(Triple, Opts);
314     case llvm::Triple::RTEMS:
315       return std::make_unique<RTEMSTargetInfo<MipsTargetInfo>>(Triple, Opts);
316     case llvm::Triple::FreeBSD:
317       return std::make_unique<FreeBSDTargetInfo<MipsTargetInfo>>(Triple, Opts);
318     case llvm::Triple::NetBSD:
319       return std::make_unique<NetBSDTargetInfo<MipsTargetInfo>>(Triple, Opts);
320     case llvm::Triple::OpenBSD:
321       return std::make_unique<OpenBSDTargetInfo<MipsTargetInfo>>(Triple, Opts);
322     default:
323       return std::make_unique<MipsTargetInfo>(Triple, Opts);
324     }
325 
326   case llvm::Triple::mips64el:
327     switch (os) {
328     case llvm::Triple::Linux:
329       return std::make_unique<LinuxTargetInfo<MipsTargetInfo>>(Triple, Opts);
330     case llvm::Triple::RTEMS:
331       return std::make_unique<RTEMSTargetInfo<MipsTargetInfo>>(Triple, Opts);
332     case llvm::Triple::FreeBSD:
333       return std::make_unique<FreeBSDTargetInfo<MipsTargetInfo>>(Triple, Opts);
334     case llvm::Triple::NetBSD:
335       return std::make_unique<NetBSDTargetInfo<MipsTargetInfo>>(Triple, Opts);
336     case llvm::Triple::OpenBSD:
337       return std::make_unique<OpenBSDTargetInfo<MipsTargetInfo>>(Triple, Opts);
338     default:
339       return std::make_unique<MipsTargetInfo>(Triple, Opts);
340     }
341 
342   case llvm::Triple::m68k:
343     switch (os) {
344     case llvm::Triple::Linux:
345       return std::make_unique<LinuxTargetInfo<M68kTargetInfo>>(Triple, Opts);
346     case llvm::Triple::NetBSD:
347       return std::make_unique<NetBSDTargetInfo<M68kTargetInfo>>(Triple, Opts);
348     default:
349       return std::make_unique<M68kTargetInfo>(Triple, Opts);
350     }
351 
352   case llvm::Triple::le32:
353     switch (os) {
354     case llvm::Triple::NaCl:
355       return std::make_unique<NaClTargetInfo<PNaClTargetInfo>>(Triple, Opts);
356     default:
357       return nullptr;
358     }
359 
360   case llvm::Triple::le64:
361     return std::make_unique<Le64TargetInfo>(Triple, Opts);
362 
363   case llvm::Triple::ppc:
364     switch (os) {
365     case llvm::Triple::Linux:
366       return std::make_unique<LinuxTargetInfo<PPC32TargetInfo>>(Triple, Opts);
367     case llvm::Triple::FreeBSD:
368       return std::make_unique<FreeBSDTargetInfo<PPC32TargetInfo>>(Triple, Opts);
369     case llvm::Triple::NetBSD:
370       return std::make_unique<NetBSDTargetInfo<PPC32TargetInfo>>(Triple, Opts);
371     case llvm::Triple::OpenBSD:
372       return std::make_unique<OpenBSDTargetInfo<PPC32TargetInfo>>(Triple, Opts);
373     case llvm::Triple::RTEMS:
374       return std::make_unique<RTEMSTargetInfo<PPC32TargetInfo>>(Triple, Opts);
375     case llvm::Triple::AIX:
376       return std::make_unique<AIXPPC32TargetInfo>(Triple, Opts);
377     default:
378       return std::make_unique<PPC32TargetInfo>(Triple, Opts);
379     }
380 
381   case llvm::Triple::ppcle:
382     switch (os) {
383     case llvm::Triple::Linux:
384       return std::make_unique<LinuxTargetInfo<PPC32TargetInfo>>(Triple, Opts);
385     case llvm::Triple::FreeBSD:
386       return std::make_unique<FreeBSDTargetInfo<PPC32TargetInfo>>(Triple, Opts);
387     default:
388       return std::make_unique<PPC32TargetInfo>(Triple, Opts);
389     }
390 
391   case llvm::Triple::ppc64:
392     switch (os) {
393     case llvm::Triple::Linux:
394       return std::make_unique<LinuxTargetInfo<PPC64TargetInfo>>(Triple, Opts);
395     case llvm::Triple::Lv2:
396       return std::make_unique<PS3PPUTargetInfo<PPC64TargetInfo>>(Triple, Opts);
397     case llvm::Triple::FreeBSD:
398       return std::make_unique<FreeBSDTargetInfo<PPC64TargetInfo>>(Triple, Opts);
399     case llvm::Triple::NetBSD:
400       return std::make_unique<NetBSDTargetInfo<PPC64TargetInfo>>(Triple, Opts);
401     case llvm::Triple::OpenBSD:
402       return std::make_unique<OpenBSDTargetInfo<PPC64TargetInfo>>(Triple, Opts);
403     case llvm::Triple::AIX:
404       return std::make_unique<AIXPPC64TargetInfo>(Triple, Opts);
405     default:
406       return std::make_unique<PPC64TargetInfo>(Triple, Opts);
407     }
408 
409   case llvm::Triple::ppc64le:
410     switch (os) {
411     case llvm::Triple::Linux:
412       return std::make_unique<LinuxTargetInfo<PPC64TargetInfo>>(Triple, Opts);
413     case llvm::Triple::FreeBSD:
414       return std::make_unique<FreeBSDTargetInfo<PPC64TargetInfo>>(Triple, Opts);
415     case llvm::Triple::NetBSD:
416       return std::make_unique<NetBSDTargetInfo<PPC64TargetInfo>>(Triple, Opts);
417     case llvm::Triple::OpenBSD:
418       return std::make_unique<OpenBSDTargetInfo<PPC64TargetInfo>>(Triple, Opts);
419     default:
420       return std::make_unique<PPC64TargetInfo>(Triple, Opts);
421     }
422 
423   case llvm::Triple::nvptx:
424     return std::make_unique<NVPTXTargetInfo>(Triple, Opts,
425                                              /*TargetPointerWidth=*/32);
426   case llvm::Triple::nvptx64:
427     return std::make_unique<NVPTXTargetInfo>(Triple, Opts,
428                                              /*TargetPointerWidth=*/64);
429 
430   case llvm::Triple::amdgcn:
431   case llvm::Triple::r600:
432     return std::make_unique<AMDGPUTargetInfo>(Triple, Opts);
433 
434   case llvm::Triple::riscv32:
435     // TODO: add cases for NetBSD, RTEMS once tested.
436     switch (os) {
437     case llvm::Triple::FreeBSD:
438       return std::make_unique<FreeBSDTargetInfo<RISCV32TargetInfo>>(Triple,
439                                                                     Opts);
440     case llvm::Triple::Linux:
441       return std::make_unique<LinuxTargetInfo<RISCV32TargetInfo>>(Triple, Opts);
442     default:
443       return std::make_unique<RISCV32TargetInfo>(Triple, Opts);
444     }
445 
446   case llvm::Triple::riscv64:
447     // TODO: add cases for NetBSD, RTEMS once tested.
448     switch (os) {
449     case llvm::Triple::FreeBSD:
450       return std::make_unique<FreeBSDTargetInfo<RISCV64TargetInfo>>(Triple,
451                                                                     Opts);
452     case llvm::Triple::OpenBSD:
453       return std::make_unique<OpenBSDTargetInfo<RISCV64TargetInfo>>(Triple,
454                                                                     Opts);
455     case llvm::Triple::Fuchsia:
456       return std::make_unique<FuchsiaTargetInfo<RISCV64TargetInfo>>(Triple,
457                                                                     Opts);
458     case llvm::Triple::Linux:
459       switch (Triple.getEnvironment()) {
460       default:
461         return std::make_unique<LinuxTargetInfo<RISCV64TargetInfo>>(Triple,
462                                                                     Opts);
463       case llvm::Triple::OpenHOS:
464         return std::make_unique<OHOSTargetInfo<RISCV64TargetInfo>>(Triple,
465                                                                    Opts);
466       }
467     default:
468       return std::make_unique<RISCV64TargetInfo>(Triple, Opts);
469     }
470 
471   case llvm::Triple::sparc:
472     switch (os) {
473     case llvm::Triple::Linux:
474       return std::make_unique<LinuxTargetInfo<SparcV8TargetInfo>>(Triple, Opts);
475     case llvm::Triple::Solaris:
476       return std::make_unique<SolarisTargetInfo<SparcV8TargetInfo>>(Triple,
477                                                                     Opts);
478     case llvm::Triple::NetBSD:
479       return std::make_unique<NetBSDTargetInfo<SparcV8TargetInfo>>(Triple,
480                                                                    Opts);
481     case llvm::Triple::RTEMS:
482       return std::make_unique<RTEMSTargetInfo<SparcV8TargetInfo>>(Triple, Opts);
483     default:
484       return std::make_unique<SparcV8TargetInfo>(Triple, Opts);
485     }
486 
487   // The 'sparcel' architecture copies all the above cases except for Solaris.
488   case llvm::Triple::sparcel:
489     switch (os) {
490     case llvm::Triple::Linux:
491       return std::make_unique<LinuxTargetInfo<SparcV8elTargetInfo>>(Triple,
492                                                                     Opts);
493     case llvm::Triple::NetBSD:
494       return std::make_unique<NetBSDTargetInfo<SparcV8elTargetInfo>>(Triple,
495                                                                      Opts);
496     case llvm::Triple::RTEMS:
497       return std::make_unique<RTEMSTargetInfo<SparcV8elTargetInfo>>(Triple,
498                                                                     Opts);
499     default:
500       return std::make_unique<SparcV8elTargetInfo>(Triple, Opts);
501     }
502 
503   case llvm::Triple::sparcv9:
504     switch (os) {
505     case llvm::Triple::Linux:
506       return std::make_unique<LinuxTargetInfo<SparcV9TargetInfo>>(Triple, Opts);
507     case llvm::Triple::Solaris:
508       return std::make_unique<SolarisTargetInfo<SparcV9TargetInfo>>(Triple,
509                                                                     Opts);
510     case llvm::Triple::NetBSD:
511       return std::make_unique<NetBSDTargetInfo<SparcV9TargetInfo>>(Triple,
512                                                                    Opts);
513     case llvm::Triple::OpenBSD:
514       return std::make_unique<OpenBSDTargetInfo<SparcV9TargetInfo>>(Triple,
515                                                                     Opts);
516     case llvm::Triple::FreeBSD:
517       return std::make_unique<FreeBSDTargetInfo<SparcV9TargetInfo>>(Triple,
518                                                                     Opts);
519     default:
520       return std::make_unique<SparcV9TargetInfo>(Triple, Opts);
521     }
522 
523   case llvm::Triple::systemz:
524     switch (os) {
525     case llvm::Triple::Linux:
526       return std::make_unique<LinuxTargetInfo<SystemZTargetInfo>>(Triple, Opts);
527     case llvm::Triple::ZOS:
528       return std::make_unique<ZOSTargetInfo<SystemZTargetInfo>>(Triple, Opts);
529     default:
530       return std::make_unique<SystemZTargetInfo>(Triple, Opts);
531     }
532 
533   case llvm::Triple::tce:
534     return std::make_unique<TCETargetInfo>(Triple, Opts);
535 
536   case llvm::Triple::tcele:
537     return std::make_unique<TCELETargetInfo>(Triple, Opts);
538 
539   case llvm::Triple::x86:
540     if (Triple.isOSDarwin())
541       return std::make_unique<DarwinI386TargetInfo>(Triple, Opts);
542 
543     switch (os) {
544     case llvm::Triple::Ananas:
545       return std::make_unique<AnanasTargetInfo<X86_32TargetInfo>>(Triple, Opts);
546     case llvm::Triple::CloudABI:
547       return std::make_unique<CloudABITargetInfo<X86_32TargetInfo>>(Triple,
548                                                                     Opts);
549     case llvm::Triple::Linux: {
550       switch (Triple.getEnvironment()) {
551       default:
552         return std::make_unique<LinuxTargetInfo<X86_32TargetInfo>>(Triple,
553                                                                    Opts);
554       case llvm::Triple::Android:
555         return std::make_unique<AndroidX86_32TargetInfo>(Triple, Opts);
556       }
557     }
558     case llvm::Triple::DragonFly:
559       return std::make_unique<DragonFlyBSDTargetInfo<X86_32TargetInfo>>(Triple,
560                                                                         Opts);
561     case llvm::Triple::NetBSD:
562       return std::make_unique<NetBSDI386TargetInfo>(Triple, Opts);
563     case llvm::Triple::OpenBSD:
564       return std::make_unique<OpenBSDI386TargetInfo>(Triple, Opts);
565     case llvm::Triple::FreeBSD:
566       return std::make_unique<FreeBSDTargetInfo<X86_32TargetInfo>>(Triple,
567                                                                    Opts);
568     case llvm::Triple::Fuchsia:
569       return std::make_unique<FuchsiaTargetInfo<X86_32TargetInfo>>(Triple,
570                                                                    Opts);
571     case llvm::Triple::KFreeBSD:
572       return std::make_unique<KFreeBSDTargetInfo<X86_32TargetInfo>>(Triple,
573                                                                     Opts);
574     case llvm::Triple::Minix:
575       return std::make_unique<MinixTargetInfo<X86_32TargetInfo>>(Triple, Opts);
576     case llvm::Triple::Solaris:
577       return std::make_unique<SolarisTargetInfo<X86_32TargetInfo>>(Triple,
578                                                                    Opts);
579     case llvm::Triple::Win32: {
580       switch (Triple.getEnvironment()) {
581       case llvm::Triple::Cygnus:
582         return std::make_unique<CygwinX86_32TargetInfo>(Triple, Opts);
583       case llvm::Triple::GNU:
584         return std::make_unique<MinGWX86_32TargetInfo>(Triple, Opts);
585       case llvm::Triple::Itanium:
586       case llvm::Triple::MSVC:
587       default: // Assume MSVC for unknown environments
588         return std::make_unique<MicrosoftX86_32TargetInfo>(Triple, Opts);
589       }
590     }
591     case llvm::Triple::Haiku:
592       return std::make_unique<HaikuX86_32TargetInfo>(Triple, Opts);
593     case llvm::Triple::RTEMS:
594       return std::make_unique<RTEMSX86_32TargetInfo>(Triple, Opts);
595     case llvm::Triple::NaCl:
596       return std::make_unique<NaClTargetInfo<X86_32TargetInfo>>(Triple, Opts);
597     case llvm::Triple::ELFIAMCU:
598       return std::make_unique<MCUX86_32TargetInfo>(Triple, Opts);
599     case llvm::Triple::Hurd:
600       return std::make_unique<HurdTargetInfo<X86_32TargetInfo>>(Triple, Opts);
601     default:
602       return std::make_unique<X86_32TargetInfo>(Triple, Opts);
603     }
604 
605   case llvm::Triple::x86_64:
606     if (Triple.isOSDarwin() || Triple.isOSBinFormatMachO())
607       return std::make_unique<DarwinX86_64TargetInfo>(Triple, Opts);
608 
609     switch (os) {
610     case llvm::Triple::Ananas:
611       return std::make_unique<AnanasTargetInfo<X86_64TargetInfo>>(Triple, Opts);
612     case llvm::Triple::CloudABI:
613       return std::make_unique<CloudABITargetInfo<X86_64TargetInfo>>(Triple,
614                                                                     Opts);
615     case llvm::Triple::Linux: {
616       switch (Triple.getEnvironment()) {
617       default:
618         return std::make_unique<LinuxTargetInfo<X86_64TargetInfo>>(Triple,
619                                                                    Opts);
620       case llvm::Triple::Android:
621         return std::make_unique<AndroidX86_64TargetInfo>(Triple, Opts);
622       case llvm::Triple::OpenHOS:
623         return std::make_unique<OHOSX86_64TargetInfo>(Triple, Opts);
624       }
625     }
626     case llvm::Triple::DragonFly:
627       return std::make_unique<DragonFlyBSDTargetInfo<X86_64TargetInfo>>(Triple,
628                                                                         Opts);
629     case llvm::Triple::NetBSD:
630       return std::make_unique<NetBSDTargetInfo<X86_64TargetInfo>>(Triple, Opts);
631     case llvm::Triple::OpenBSD:
632       return std::make_unique<OpenBSDX86_64TargetInfo>(Triple, Opts);
633     case llvm::Triple::FreeBSD:
634       return std::make_unique<FreeBSDTargetInfo<X86_64TargetInfo>>(Triple,
635                                                                    Opts);
636     case llvm::Triple::Fuchsia:
637       return std::make_unique<FuchsiaTargetInfo<X86_64TargetInfo>>(Triple,
638                                                                    Opts);
639     case llvm::Triple::KFreeBSD:
640       return std::make_unique<KFreeBSDTargetInfo<X86_64TargetInfo>>(Triple,
641                                                                     Opts);
642     case llvm::Triple::Solaris:
643       return std::make_unique<SolarisTargetInfo<X86_64TargetInfo>>(Triple,
644                                                                    Opts);
645     case llvm::Triple::Win32: {
646       switch (Triple.getEnvironment()) {
647       case llvm::Triple::Cygnus:
648         return std::make_unique<CygwinX86_64TargetInfo>(Triple, Opts);
649       case llvm::Triple::GNU:
650         return std::make_unique<MinGWX86_64TargetInfo>(Triple, Opts);
651       case llvm::Triple::MSVC:
652       default: // Assume MSVC for unknown environments
653         return std::make_unique<MicrosoftX86_64TargetInfo>(Triple, Opts);
654       }
655     }
656     case llvm::Triple::Haiku:
657       return std::make_unique<HaikuTargetInfo<X86_64TargetInfo>>(Triple, Opts);
658     case llvm::Triple::NaCl:
659       return std::make_unique<NaClTargetInfo<X86_64TargetInfo>>(Triple, Opts);
660     case llvm::Triple::PS4:
661       return std::make_unique<PS4OSTargetInfo<X86_64TargetInfo>>(Triple, Opts);
662     case llvm::Triple::PS5:
663       return std::make_unique<PS5OSTargetInfo<X86_64TargetInfo>>(Triple, Opts);
664     default:
665       return std::make_unique<X86_64TargetInfo>(Triple, Opts);
666     }
667 
668   case llvm::Triple::spir: {
669     if (os != llvm::Triple::UnknownOS ||
670         Triple.getEnvironment() != llvm::Triple::UnknownEnvironment)
671       return nullptr;
672     return std::make_unique<SPIR32TargetInfo>(Triple, Opts);
673   }
674   case llvm::Triple::spir64: {
675     if (os != llvm::Triple::UnknownOS ||
676         Triple.getEnvironment() != llvm::Triple::UnknownEnvironment)
677       return nullptr;
678     return std::make_unique<SPIR64TargetInfo>(Triple, Opts);
679   }
680   case llvm::Triple::spirv32: {
681     if (os != llvm::Triple::UnknownOS ||
682         Triple.getEnvironment() != llvm::Triple::UnknownEnvironment)
683       return nullptr;
684     return std::make_unique<SPIRV32TargetInfo>(Triple, Opts);
685   }
686   case llvm::Triple::spirv64: {
687     if (os != llvm::Triple::UnknownOS ||
688         Triple.getEnvironment() != llvm::Triple::UnknownEnvironment)
689       return nullptr;
690     return std::make_unique<SPIRV64TargetInfo>(Triple, Opts);
691   }
692   case llvm::Triple::wasm32:
693     if (Triple.getSubArch() != llvm::Triple::NoSubArch ||
694         Triple.getVendor() != llvm::Triple::UnknownVendor ||
695         !Triple.isOSBinFormatWasm())
696       return nullptr;
697     switch (os) {
698       case llvm::Triple::WASI:
699       return std::make_unique<WASITargetInfo<WebAssembly32TargetInfo>>(Triple,
700                                                                        Opts);
701       case llvm::Triple::Emscripten:
702       return std::make_unique<EmscriptenTargetInfo<WebAssembly32TargetInfo>>(
703           Triple, Opts);
704       case llvm::Triple::UnknownOS:
705       return std::make_unique<WebAssemblyOSTargetInfo<WebAssembly32TargetInfo>>(
706           Triple, Opts);
707       default:
708         return nullptr;
709     }
710   case llvm::Triple::wasm64:
711     if (Triple.getSubArch() != llvm::Triple::NoSubArch ||
712         Triple.getVendor() != llvm::Triple::UnknownVendor ||
713         !Triple.isOSBinFormatWasm())
714       return nullptr;
715     switch (os) {
716       case llvm::Triple::WASI:
717       return std::make_unique<WASITargetInfo<WebAssembly64TargetInfo>>(Triple,
718                                                                        Opts);
719       case llvm::Triple::Emscripten:
720       return std::make_unique<EmscriptenTargetInfo<WebAssembly64TargetInfo>>(
721           Triple, Opts);
722       case llvm::Triple::UnknownOS:
723       return std::make_unique<WebAssemblyOSTargetInfo<WebAssembly64TargetInfo>>(
724           Triple, Opts);
725       default:
726         return nullptr;
727     }
728 
729   case llvm::Triple::dxil:
730     return std::make_unique<DirectXTargetInfo>(Triple, Opts);
731   case llvm::Triple::renderscript32:
732     return std::make_unique<LinuxTargetInfo<RenderScript32TargetInfo>>(Triple,
733                                                                        Opts);
734   case llvm::Triple::renderscript64:
735     return std::make_unique<LinuxTargetInfo<RenderScript64TargetInfo>>(Triple,
736                                                                        Opts);
737 
738   case llvm::Triple::ve:
739     return std::make_unique<LinuxTargetInfo<VETargetInfo>>(Triple, Opts);
740 
741   case llvm::Triple::csky:
742     switch (os) {
743     case llvm::Triple::Linux:
744         return std::make_unique<LinuxTargetInfo<CSKYTargetInfo>>(Triple, Opts);
745     default:
746         return std::make_unique<CSKYTargetInfo>(Triple, Opts);
747     }
748   case llvm::Triple::loongarch32:
749     switch (os) {
750     case llvm::Triple::Linux:
751         return std::make_unique<LinuxTargetInfo<LoongArch32TargetInfo>>(Triple,
752                                                                         Opts);
753     default:
754         return std::make_unique<LoongArch32TargetInfo>(Triple, Opts);
755     }
756   case llvm::Triple::loongarch64:
757     switch (os) {
758     case llvm::Triple::Linux:
759         return std::make_unique<LinuxTargetInfo<LoongArch64TargetInfo>>(Triple,
760                                                                         Opts);
761     default:
762         return std::make_unique<LoongArch64TargetInfo>(Triple, Opts);
763     }
764   }
765 }
766 } // namespace targets
767 } // namespace clang
768 
769 using namespace clang::targets;
770 /// CreateTargetInfo - Return the target info object for the specified target
771 /// options.
772 TargetInfo *
773 TargetInfo::CreateTargetInfo(DiagnosticsEngine &Diags,
774                              const std::shared_ptr<TargetOptions> &Opts) {
775   llvm::Triple Triple(Opts->Triple);
776 
777   // Construct the target
778   std::unique_ptr<TargetInfo> Target = AllocateTarget(Triple, *Opts);
779   if (!Target) {
780     Diags.Report(diag::err_target_unknown_triple) << Triple.str();
781     return nullptr;
782   }
783   Target->TargetOpts = Opts;
784 
785   // Set the target CPU if specified.
786   if (!Opts->CPU.empty() && !Target->setCPU(Opts->CPU)) {
787     Diags.Report(diag::err_target_unknown_cpu) << Opts->CPU;
788     SmallVector<StringRef, 32> ValidList;
789     Target->fillValidCPUList(ValidList);
790     if (!ValidList.empty())
791       Diags.Report(diag::note_valid_options) << llvm::join(ValidList, ", ");
792     return nullptr;
793   }
794 
795   // Check the TuneCPU name if specified.
796   if (!Opts->TuneCPU.empty() &&
797       !Target->isValidTuneCPUName(Opts->TuneCPU)) {
798     Diags.Report(diag::err_target_unknown_cpu) << Opts->TuneCPU;
799     SmallVector<StringRef, 32> ValidList;
800     Target->fillValidTuneCPUList(ValidList);
801     if (!ValidList.empty())
802       Diags.Report(diag::note_valid_options) << llvm::join(ValidList, ", ");
803     return nullptr;
804   }
805 
806   // Set the target ABI if specified.
807   if (!Opts->ABI.empty() && !Target->setABI(Opts->ABI)) {
808     Diags.Report(diag::err_target_unknown_abi) << Opts->ABI;
809     return nullptr;
810   }
811 
812   // Set the fp math unit.
813   if (!Opts->FPMath.empty() && !Target->setFPMath(Opts->FPMath)) {
814     Diags.Report(diag::err_target_unknown_fpmath) << Opts->FPMath;
815     return nullptr;
816   }
817 
818   // Compute the default target features, we need the target to handle this
819   // because features may have dependencies on one another.
820   llvm::erase_if(Opts->FeaturesAsWritten, [&](StringRef Name) {
821     if (Target->isReadOnlyFeature(Name.substr(1))) {
822       Diags.Report(diag::warn_fe_backend_readonly_feature_flag) << Name;
823       return true;
824     }
825     return false;
826   });
827   if (!Target->initFeatureMap(Opts->FeatureMap, Diags, Opts->CPU,
828                               Opts->FeaturesAsWritten))
829     return nullptr;
830 
831   // Add the features to the compile options.
832   Opts->Features.clear();
833   for (const auto &F : Opts->FeatureMap)
834     Opts->Features.push_back((F.getValue() ? "+" : "-") + F.getKey().str());
835   // Sort here, so we handle the features in a predictable order. (This matters
836   // when we're dealing with features that overlap.)
837   llvm::sort(Opts->Features);
838 
839   if (!Target->handleTargetFeatures(Opts->Features, Diags))
840     return nullptr;
841 
842   Target->setSupportedOpenCLOpts();
843   Target->setCommandLineOpenCLOpts();
844   Target->setMaxAtomicWidth();
845 
846   if (!Opts->DarwinTargetVariantTriple.empty())
847     Target->DarwinTargetVariantTriple =
848         llvm::Triple(Opts->DarwinTargetVariantTriple);
849 
850   if (!Target->validateTarget(Diags))
851     return nullptr;
852 
853   Target->CheckFixedPointBits();
854 
855   return Target.release();
856 }
857 /// validateOpenCLTarget  - Check that OpenCL target has valid
858 /// options setting based on OpenCL version.
859 bool TargetInfo::validateOpenCLTarget(const LangOptions &Opts,
860                                       DiagnosticsEngine &Diags) const {
861   const llvm::StringMap<bool> &OpenCLFeaturesMap = getSupportedOpenCLOpts();
862 
863   auto diagnoseNotSupportedCore = [&](llvm::StringRef Name, auto... OptArgs) {
864     if (OpenCLOptions::isOpenCLOptionCoreIn(Opts, OptArgs...) &&
865         !hasFeatureEnabled(OpenCLFeaturesMap, Name))
866       Diags.Report(diag::warn_opencl_unsupported_core_feature)
867           << Name << Opts.OpenCLCPlusPlus
868           << Opts.getOpenCLVersionTuple().getAsString();
869   };
870 #define OPENCL_GENERIC_EXTENSION(Ext, ...)                                     \
871   diagnoseNotSupportedCore(#Ext, __VA_ARGS__);
872 #include "clang/Basic/OpenCLExtensions.def"
873 
874   // Validate that feature macros are set properly for OpenCL C 3.0.
875   // In other cases assume that target is always valid.
876   if (Opts.getOpenCLCompatibleVersion() < 300)
877     return true;
878 
879   return OpenCLOptions::diagnoseUnsupportedFeatureDependencies(*this, Diags) &&
880          OpenCLOptions::diagnoseFeatureExtensionDifferences(*this, Diags);
881 }
882