xref: /freebsd/contrib/llvm-project/clang/lib/Basic/Targets.cpp (revision ec4deee4e4f2aef1b97d9424f25d04e91fd7dc10)
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/Hexagon.h"
23 #include "Targets/Lanai.h"
24 #include "Targets/Le64.h"
25 #include "Targets/MSP430.h"
26 #include "Targets/Mips.h"
27 #include "Targets/NVPTX.h"
28 #include "Targets/OSTargets.h"
29 #include "Targets/PNaCl.h"
30 #include "Targets/PPC.h"
31 #include "Targets/RISCV.h"
32 #include "Targets/SPIR.h"
33 #include "Targets/Sparc.h"
34 #include "Targets/SystemZ.h"
35 #include "Targets/TCE.h"
36 #include "Targets/WebAssembly.h"
37 #include "Targets/X86.h"
38 #include "Targets/XCore.h"
39 #include "clang/Basic/Diagnostic.h"
40 #include "llvm/ADT/StringExtras.h"
41 #include "llvm/ADT/Triple.h"
42 
43 using namespace clang;
44 
45 namespace clang {
46 namespace targets {
47 //===----------------------------------------------------------------------===//
48 //  Common code shared among targets.
49 //===----------------------------------------------------------------------===//
50 
51 /// DefineStd - Define a macro name and standard variants.  For example if
52 /// MacroName is "unix", then this will define "__unix", "__unix__", and "unix"
53 /// when in GNU mode.
54 void DefineStd(MacroBuilder &Builder, StringRef MacroName,
55                const LangOptions &Opts) {
56   assert(MacroName[0] != '_' && "Identifier should be in the user's namespace");
57 
58   // If in GNU mode (e.g. -std=gnu99 but not -std=c99) define the raw identifier
59   // in the user's namespace.
60   if (Opts.GNUMode)
61     Builder.defineMacro(MacroName);
62 
63   // Define __unix.
64   Builder.defineMacro("__" + MacroName);
65 
66   // Define __unix__.
67   Builder.defineMacro("__" + MacroName + "__");
68 }
69 
70 void defineCPUMacros(MacroBuilder &Builder, StringRef CPUName, bool Tuning) {
71   Builder.defineMacro("__" + CPUName);
72   Builder.defineMacro("__" + CPUName + "__");
73   if (Tuning)
74     Builder.defineMacro("__tune_" + CPUName + "__");
75 }
76 
77 void addCygMingDefines(const LangOptions &Opts, MacroBuilder &Builder) {
78   // Mingw and cygwin define __declspec(a) to __attribute__((a)).  Clang
79   // supports __declspec natively under -fms-extensions, but we define a no-op
80   // __declspec macro anyway for pre-processor compatibility.
81   if (Opts.MicrosoftExt)
82     Builder.defineMacro("__declspec", "__declspec");
83   else
84     Builder.defineMacro("__declspec(a)", "__attribute__((a))");
85 
86   if (!Opts.MicrosoftExt) {
87     // Provide macros for all the calling convention keywords.  Provide both
88     // single and double underscore prefixed variants.  These are available on
89     // x64 as well as x86, even though they have no effect.
90     const char *CCs[] = {"cdecl", "stdcall", "fastcall", "thiscall", "pascal"};
91     for (const char *CC : CCs) {
92       std::string GCCSpelling = "__attribute__((__";
93       GCCSpelling += CC;
94       GCCSpelling += "__))";
95       Builder.defineMacro(Twine("_") + CC, GCCSpelling);
96       Builder.defineMacro(Twine("__") + CC, GCCSpelling);
97     }
98   }
99 }
100 
101 //===----------------------------------------------------------------------===//
102 // Driver code
103 //===----------------------------------------------------------------------===//
104 
105 TargetInfo *AllocateTarget(const llvm::Triple &Triple,
106                            const TargetOptions &Opts) {
107   llvm::Triple::OSType os = Triple.getOS();
108 
109   switch (Triple.getArch()) {
110   default:
111     return nullptr;
112 
113   case llvm::Triple::arc:
114     return new ARCTargetInfo(Triple, Opts);
115 
116   case llvm::Triple::xcore:
117     return new XCoreTargetInfo(Triple, Opts);
118 
119   case llvm::Triple::hexagon:
120     return new HexagonTargetInfo(Triple, Opts);
121 
122   case llvm::Triple::lanai:
123     return new LanaiTargetInfo(Triple, Opts);
124 
125   case llvm::Triple::aarch64_32:
126     if (Triple.isOSDarwin())
127       return new DarwinAArch64TargetInfo(Triple, Opts);
128 
129     return nullptr;
130   case llvm::Triple::aarch64:
131     if (Triple.isOSDarwin())
132       return new DarwinAArch64TargetInfo(Triple, Opts);
133 
134     switch (os) {
135     case llvm::Triple::CloudABI:
136       return new CloudABITargetInfo<AArch64leTargetInfo>(Triple, Opts);
137     case llvm::Triple::FreeBSD:
138       return new FreeBSDTargetInfo<AArch64leTargetInfo>(Triple, Opts);
139     case llvm::Triple::Fuchsia:
140       return new FuchsiaTargetInfo<AArch64leTargetInfo>(Triple, Opts);
141     case llvm::Triple::Linux:
142       return new LinuxTargetInfo<AArch64leTargetInfo>(Triple, Opts);
143     case llvm::Triple::NetBSD:
144       return new NetBSDTargetInfo<AArch64leTargetInfo>(Triple, Opts);
145     case llvm::Triple::OpenBSD:
146       return new OpenBSDTargetInfo<AArch64leTargetInfo>(Triple, Opts);
147     case llvm::Triple::Win32:
148       switch (Triple.getEnvironment()) {
149       case llvm::Triple::GNU:
150         return new MinGWARM64TargetInfo(Triple, Opts);
151       case llvm::Triple::MSVC:
152       default: // Assume MSVC for unknown environments
153         return new MicrosoftARM64TargetInfo(Triple, Opts);
154       }
155     default:
156       return new AArch64leTargetInfo(Triple, Opts);
157     }
158 
159   case llvm::Triple::aarch64_be:
160     switch (os) {
161     case llvm::Triple::FreeBSD:
162       return new FreeBSDTargetInfo<AArch64beTargetInfo>(Triple, Opts);
163     case llvm::Triple::Fuchsia:
164       return new FuchsiaTargetInfo<AArch64beTargetInfo>(Triple, Opts);
165     case llvm::Triple::Linux:
166       return new LinuxTargetInfo<AArch64beTargetInfo>(Triple, Opts);
167     case llvm::Triple::NetBSD:
168       return new NetBSDTargetInfo<AArch64beTargetInfo>(Triple, Opts);
169     default:
170       return new AArch64beTargetInfo(Triple, Opts);
171     }
172 
173   case llvm::Triple::arm:
174   case llvm::Triple::thumb:
175     if (Triple.isOSBinFormatMachO())
176       return new DarwinARMTargetInfo(Triple, Opts);
177 
178     switch (os) {
179     case llvm::Triple::CloudABI:
180       return new CloudABITargetInfo<ARMleTargetInfo>(Triple, Opts);
181     case llvm::Triple::Linux:
182       return new LinuxTargetInfo<ARMleTargetInfo>(Triple, Opts);
183     case llvm::Triple::FreeBSD:
184       return new FreeBSDTargetInfo<ARMleTargetInfo>(Triple, Opts);
185     case llvm::Triple::NetBSD:
186       return new NetBSDTargetInfo<ARMleTargetInfo>(Triple, Opts);
187     case llvm::Triple::OpenBSD:
188       return new OpenBSDTargetInfo<ARMleTargetInfo>(Triple, Opts);
189     case llvm::Triple::RTEMS:
190       return new RTEMSTargetInfo<ARMleTargetInfo>(Triple, Opts);
191     case llvm::Triple::NaCl:
192       return new NaClTargetInfo<ARMleTargetInfo>(Triple, Opts);
193     case llvm::Triple::Win32:
194       switch (Triple.getEnvironment()) {
195       case llvm::Triple::Cygnus:
196         return new CygwinARMTargetInfo(Triple, Opts);
197       case llvm::Triple::GNU:
198         return new MinGWARMTargetInfo(Triple, Opts);
199       case llvm::Triple::Itanium:
200         return new ItaniumWindowsARMleTargetInfo(Triple, Opts);
201       case llvm::Triple::MSVC:
202       default: // Assume MSVC for unknown environments
203         return new MicrosoftARMleTargetInfo(Triple, Opts);
204       }
205     default:
206       return new ARMleTargetInfo(Triple, Opts);
207     }
208 
209   case llvm::Triple::armeb:
210   case llvm::Triple::thumbeb:
211     if (Triple.isOSDarwin())
212       return new DarwinARMTargetInfo(Triple, Opts);
213 
214     switch (os) {
215     case llvm::Triple::Linux:
216       return new LinuxTargetInfo<ARMbeTargetInfo>(Triple, Opts);
217     case llvm::Triple::FreeBSD:
218       return new FreeBSDTargetInfo<ARMbeTargetInfo>(Triple, Opts);
219     case llvm::Triple::NetBSD:
220       return new NetBSDTargetInfo<ARMbeTargetInfo>(Triple, Opts);
221     case llvm::Triple::OpenBSD:
222       return new OpenBSDTargetInfo<ARMbeTargetInfo>(Triple, Opts);
223     case llvm::Triple::RTEMS:
224       return new RTEMSTargetInfo<ARMbeTargetInfo>(Triple, Opts);
225     case llvm::Triple::NaCl:
226       return new NaClTargetInfo<ARMbeTargetInfo>(Triple, Opts);
227     default:
228       return new ARMbeTargetInfo(Triple, Opts);
229     }
230 
231   case llvm::Triple::avr:
232     return new AVRTargetInfo(Triple, Opts);
233   case llvm::Triple::bpfeb:
234   case llvm::Triple::bpfel:
235     return new BPFTargetInfo(Triple, Opts);
236 
237   case llvm::Triple::msp430:
238     return new MSP430TargetInfo(Triple, Opts);
239 
240   case llvm::Triple::mips:
241     switch (os) {
242     case llvm::Triple::Linux:
243       return new LinuxTargetInfo<MipsTargetInfo>(Triple, Opts);
244     case llvm::Triple::RTEMS:
245       return new RTEMSTargetInfo<MipsTargetInfo>(Triple, Opts);
246     case llvm::Triple::FreeBSD:
247       return new FreeBSDTargetInfo<MipsTargetInfo>(Triple, Opts);
248     case llvm::Triple::NetBSD:
249       return new NetBSDTargetInfo<MipsTargetInfo>(Triple, Opts);
250     default:
251       return new MipsTargetInfo(Triple, Opts);
252     }
253 
254   case llvm::Triple::mipsel:
255     switch (os) {
256     case llvm::Triple::Linux:
257       return new LinuxTargetInfo<MipsTargetInfo>(Triple, Opts);
258     case llvm::Triple::RTEMS:
259       return new RTEMSTargetInfo<MipsTargetInfo>(Triple, Opts);
260     case llvm::Triple::FreeBSD:
261       return new FreeBSDTargetInfo<MipsTargetInfo>(Triple, Opts);
262     case llvm::Triple::NetBSD:
263       return new NetBSDTargetInfo<MipsTargetInfo>(Triple, Opts);
264     case llvm::Triple::NaCl:
265       return new NaClTargetInfo<NaClMips32TargetInfo>(Triple, Opts);
266     default:
267       return new MipsTargetInfo(Triple, Opts);
268     }
269 
270   case llvm::Triple::mips64:
271     switch (os) {
272     case llvm::Triple::Linux:
273       return new LinuxTargetInfo<MipsTargetInfo>(Triple, Opts);
274     case llvm::Triple::RTEMS:
275       return new RTEMSTargetInfo<MipsTargetInfo>(Triple, Opts);
276     case llvm::Triple::FreeBSD:
277       return new FreeBSDTargetInfo<MipsTargetInfo>(Triple, Opts);
278     case llvm::Triple::NetBSD:
279       return new NetBSDTargetInfo<MipsTargetInfo>(Triple, Opts);
280     case llvm::Triple::OpenBSD:
281       return new OpenBSDTargetInfo<MipsTargetInfo>(Triple, Opts);
282     default:
283       return new MipsTargetInfo(Triple, Opts);
284     }
285 
286   case llvm::Triple::mips64el:
287     switch (os) {
288     case llvm::Triple::Linux:
289       return new LinuxTargetInfo<MipsTargetInfo>(Triple, Opts);
290     case llvm::Triple::RTEMS:
291       return new RTEMSTargetInfo<MipsTargetInfo>(Triple, Opts);
292     case llvm::Triple::FreeBSD:
293       return new FreeBSDTargetInfo<MipsTargetInfo>(Triple, Opts);
294     case llvm::Triple::NetBSD:
295       return new NetBSDTargetInfo<MipsTargetInfo>(Triple, Opts);
296     case llvm::Triple::OpenBSD:
297       return new OpenBSDTargetInfo<MipsTargetInfo>(Triple, Opts);
298     default:
299       return new MipsTargetInfo(Triple, Opts);
300     }
301 
302   case llvm::Triple::le32:
303     switch (os) {
304     case llvm::Triple::NaCl:
305       return new NaClTargetInfo<PNaClTargetInfo>(Triple, Opts);
306     default:
307       return nullptr;
308     }
309 
310   case llvm::Triple::le64:
311     return new Le64TargetInfo(Triple, Opts);
312 
313   case llvm::Triple::ppc:
314     if (Triple.isOSDarwin())
315       return new DarwinPPC32TargetInfo(Triple, Opts);
316     switch (os) {
317     case llvm::Triple::Linux:
318       return new LinuxTargetInfo<PPC32TargetInfo>(Triple, Opts);
319     case llvm::Triple::FreeBSD:
320       return new FreeBSDTargetInfo<PPC32TargetInfo>(Triple, Opts);
321     case llvm::Triple::NetBSD:
322       return new NetBSDTargetInfo<PPC32TargetInfo>(Triple, Opts);
323     case llvm::Triple::OpenBSD:
324       return new OpenBSDTargetInfo<PPC32TargetInfo>(Triple, Opts);
325     case llvm::Triple::RTEMS:
326       return new RTEMSTargetInfo<PPC32TargetInfo>(Triple, Opts);
327     case llvm::Triple::AIX:
328       return new AIXPPC32TargetInfo(Triple, Opts);
329     default:
330       return new PPC32TargetInfo(Triple, Opts);
331     }
332 
333   case llvm::Triple::ppc64:
334     if (Triple.isOSDarwin())
335       return new DarwinPPC64TargetInfo(Triple, Opts);
336     switch (os) {
337     case llvm::Triple::Linux:
338       return new LinuxTargetInfo<PPC64TargetInfo>(Triple, Opts);
339     case llvm::Triple::Lv2:
340       return new PS3PPUTargetInfo<PPC64TargetInfo>(Triple, Opts);
341     case llvm::Triple::FreeBSD:
342       return new FreeBSDTargetInfo<PPC64TargetInfo>(Triple, Opts);
343     case llvm::Triple::NetBSD:
344       return new NetBSDTargetInfo<PPC64TargetInfo>(Triple, Opts);
345     case llvm::Triple::AIX:
346       return new AIXPPC64TargetInfo(Triple, Opts);
347     default:
348       return new PPC64TargetInfo(Triple, Opts);
349     }
350 
351   case llvm::Triple::ppc64le:
352     switch (os) {
353     case llvm::Triple::Linux:
354       return new LinuxTargetInfo<PPC64TargetInfo>(Triple, Opts);
355     case llvm::Triple::NetBSD:
356       return new NetBSDTargetInfo<PPC64TargetInfo>(Triple, Opts);
357     default:
358       return new PPC64TargetInfo(Triple, Opts);
359     }
360 
361   case llvm::Triple::nvptx:
362     return new NVPTXTargetInfo(Triple, Opts, /*TargetPointerWidth=*/32);
363   case llvm::Triple::nvptx64:
364     return new NVPTXTargetInfo(Triple, Opts, /*TargetPointerWidth=*/64);
365 
366   case llvm::Triple::amdgcn:
367   case llvm::Triple::r600:
368     return new AMDGPUTargetInfo(Triple, Opts);
369 
370   case llvm::Triple::riscv32:
371     // TODO: add cases for NetBSD, RTEMS once tested.
372     switch (os) {
373     case llvm::Triple::FreeBSD:
374       return new FreeBSDTargetInfo<RISCV32TargetInfo>(Triple, Opts);
375     case llvm::Triple::Linux:
376       return new LinuxTargetInfo<RISCV32TargetInfo>(Triple, Opts);
377     default:
378       return new RISCV32TargetInfo(Triple, Opts);
379     }
380 
381   case llvm::Triple::riscv64:
382     // TODO: add cases for NetBSD, RTEMS once tested.
383     switch (os) {
384     case llvm::Triple::FreeBSD:
385       return new FreeBSDTargetInfo<RISCV64TargetInfo>(Triple, Opts);
386     case llvm::Triple::Fuchsia:
387       return new FuchsiaTargetInfo<RISCV64TargetInfo>(Triple, Opts);
388     case llvm::Triple::Linux:
389       return new LinuxTargetInfo<RISCV64TargetInfo>(Triple, Opts);
390     default:
391       return new RISCV64TargetInfo(Triple, Opts);
392     }
393 
394   case llvm::Triple::sparc:
395     switch (os) {
396     case llvm::Triple::Linux:
397       return new LinuxTargetInfo<SparcV8TargetInfo>(Triple, Opts);
398     case llvm::Triple::Solaris:
399       return new SolarisTargetInfo<SparcV8TargetInfo>(Triple, Opts);
400     case llvm::Triple::NetBSD:
401       return new NetBSDTargetInfo<SparcV8TargetInfo>(Triple, Opts);
402     case llvm::Triple::OpenBSD:
403       return new OpenBSDTargetInfo<SparcV8TargetInfo>(Triple, Opts);
404     case llvm::Triple::RTEMS:
405       return new RTEMSTargetInfo<SparcV8TargetInfo>(Triple, Opts);
406     default:
407       return new SparcV8TargetInfo(Triple, Opts);
408     }
409 
410   // The 'sparcel' architecture copies all the above cases except for Solaris.
411   case llvm::Triple::sparcel:
412     switch (os) {
413     case llvm::Triple::Linux:
414       return new LinuxTargetInfo<SparcV8elTargetInfo>(Triple, Opts);
415     case llvm::Triple::NetBSD:
416       return new NetBSDTargetInfo<SparcV8elTargetInfo>(Triple, Opts);
417     case llvm::Triple::OpenBSD:
418       return new OpenBSDTargetInfo<SparcV8elTargetInfo>(Triple, Opts);
419     case llvm::Triple::RTEMS:
420       return new RTEMSTargetInfo<SparcV8elTargetInfo>(Triple, Opts);
421     default:
422       return new SparcV8elTargetInfo(Triple, Opts);
423     }
424 
425   case llvm::Triple::sparcv9:
426     switch (os) {
427     case llvm::Triple::Linux:
428       return new LinuxTargetInfo<SparcV9TargetInfo>(Triple, Opts);
429     case llvm::Triple::Solaris:
430       return new SolarisTargetInfo<SparcV9TargetInfo>(Triple, Opts);
431     case llvm::Triple::NetBSD:
432       return new NetBSDTargetInfo<SparcV9TargetInfo>(Triple, Opts);
433     case llvm::Triple::OpenBSD:
434       return new OpenBSDTargetInfo<SparcV9TargetInfo>(Triple, Opts);
435     case llvm::Triple::FreeBSD:
436       return new FreeBSDTargetInfo<SparcV9TargetInfo>(Triple, Opts);
437     default:
438       return new SparcV9TargetInfo(Triple, Opts);
439     }
440 
441   case llvm::Triple::systemz:
442     switch (os) {
443     case llvm::Triple::Linux:
444       return new LinuxTargetInfo<SystemZTargetInfo>(Triple, Opts);
445     default:
446       return new SystemZTargetInfo(Triple, Opts);
447     }
448 
449   case llvm::Triple::tce:
450     return new TCETargetInfo(Triple, Opts);
451 
452   case llvm::Triple::tcele:
453     return new TCELETargetInfo(Triple, Opts);
454 
455   case llvm::Triple::x86:
456     if (Triple.isOSDarwin())
457       return new DarwinI386TargetInfo(Triple, Opts);
458 
459     switch (os) {
460     case llvm::Triple::Ananas:
461       return new AnanasTargetInfo<X86_32TargetInfo>(Triple, Opts);
462     case llvm::Triple::CloudABI:
463       return new CloudABITargetInfo<X86_32TargetInfo>(Triple, Opts);
464     case llvm::Triple::Linux: {
465       switch (Triple.getEnvironment()) {
466       default:
467         return new LinuxTargetInfo<X86_32TargetInfo>(Triple, Opts);
468       case llvm::Triple::Android:
469         return new AndroidX86_32TargetInfo(Triple, Opts);
470       }
471     }
472     case llvm::Triple::DragonFly:
473       return new DragonFlyBSDTargetInfo<X86_32TargetInfo>(Triple, Opts);
474     case llvm::Triple::NetBSD:
475       return new NetBSDI386TargetInfo(Triple, Opts);
476     case llvm::Triple::OpenBSD:
477       return new OpenBSDI386TargetInfo(Triple, Opts);
478     case llvm::Triple::FreeBSD:
479       return new FreeBSDTargetInfo<X86_32TargetInfo>(Triple, Opts);
480     case llvm::Triple::KFreeBSD:
481       return new KFreeBSDTargetInfo<X86_32TargetInfo>(Triple, Opts);
482     case llvm::Triple::Minix:
483       return new MinixTargetInfo<X86_32TargetInfo>(Triple, Opts);
484     case llvm::Triple::Solaris:
485       return new SolarisTargetInfo<X86_32TargetInfo>(Triple, Opts);
486     case llvm::Triple::Win32: {
487       switch (Triple.getEnvironment()) {
488       case llvm::Triple::Cygnus:
489         return new CygwinX86_32TargetInfo(Triple, Opts);
490       case llvm::Triple::GNU:
491         return new MinGWX86_32TargetInfo(Triple, Opts);
492       case llvm::Triple::Itanium:
493       case llvm::Triple::MSVC:
494       default: // Assume MSVC for unknown environments
495         return new MicrosoftX86_32TargetInfo(Triple, Opts);
496       }
497     }
498     case llvm::Triple::Haiku:
499       return new HaikuX86_32TargetInfo(Triple, Opts);
500     case llvm::Triple::RTEMS:
501       return new RTEMSX86_32TargetInfo(Triple, Opts);
502     case llvm::Triple::NaCl:
503       return new NaClTargetInfo<X86_32TargetInfo>(Triple, Opts);
504     case llvm::Triple::ELFIAMCU:
505       return new MCUX86_32TargetInfo(Triple, Opts);
506     case llvm::Triple::Hurd:
507       return new HurdTargetInfo<X86_32TargetInfo>(Triple, Opts);
508     default:
509       return new X86_32TargetInfo(Triple, Opts);
510     }
511 
512   case llvm::Triple::x86_64:
513     if (Triple.isOSDarwin() || Triple.isOSBinFormatMachO())
514       return new DarwinX86_64TargetInfo(Triple, Opts);
515 
516     switch (os) {
517     case llvm::Triple::Ananas:
518       return new AnanasTargetInfo<X86_64TargetInfo>(Triple, Opts);
519     case llvm::Triple::CloudABI:
520       return new CloudABITargetInfo<X86_64TargetInfo>(Triple, Opts);
521     case llvm::Triple::Linux: {
522       switch (Triple.getEnvironment()) {
523       default:
524         return new LinuxTargetInfo<X86_64TargetInfo>(Triple, Opts);
525       case llvm::Triple::Android:
526         return new AndroidX86_64TargetInfo(Triple, Opts);
527       }
528     }
529     case llvm::Triple::DragonFly:
530       return new DragonFlyBSDTargetInfo<X86_64TargetInfo>(Triple, Opts);
531     case llvm::Triple::NetBSD:
532       return new NetBSDTargetInfo<X86_64TargetInfo>(Triple, Opts);
533     case llvm::Triple::OpenBSD:
534       return new OpenBSDX86_64TargetInfo(Triple, Opts);
535     case llvm::Triple::FreeBSD:
536       return new FreeBSDTargetInfo<X86_64TargetInfo>(Triple, Opts);
537     case llvm::Triple::Fuchsia:
538       return new FuchsiaTargetInfo<X86_64TargetInfo>(Triple, Opts);
539     case llvm::Triple::KFreeBSD:
540       return new KFreeBSDTargetInfo<X86_64TargetInfo>(Triple, Opts);
541     case llvm::Triple::Solaris:
542       return new SolarisTargetInfo<X86_64TargetInfo>(Triple, Opts);
543     case llvm::Triple::Win32: {
544       switch (Triple.getEnvironment()) {
545       case llvm::Triple::Cygnus:
546         return new CygwinX86_64TargetInfo(Triple, Opts);
547       case llvm::Triple::GNU:
548         return new MinGWX86_64TargetInfo(Triple, Opts);
549       case llvm::Triple::MSVC:
550       default: // Assume MSVC for unknown environments
551         return new MicrosoftX86_64TargetInfo(Triple, Opts);
552       }
553     }
554     case llvm::Triple::Haiku:
555       return new HaikuTargetInfo<X86_64TargetInfo>(Triple, Opts);
556     case llvm::Triple::NaCl:
557       return new NaClTargetInfo<X86_64TargetInfo>(Triple, Opts);
558     case llvm::Triple::PS4:
559       return new PS4OSTargetInfo<X86_64TargetInfo>(Triple, Opts);
560     default:
561       return new X86_64TargetInfo(Triple, Opts);
562     }
563 
564   case llvm::Triple::spir: {
565     if (Triple.getOS() != llvm::Triple::UnknownOS ||
566         Triple.getEnvironment() != llvm::Triple::UnknownEnvironment)
567       return nullptr;
568     return new SPIR32TargetInfo(Triple, Opts);
569   }
570   case llvm::Triple::spir64: {
571     if (Triple.getOS() != llvm::Triple::UnknownOS ||
572         Triple.getEnvironment() != llvm::Triple::UnknownEnvironment)
573       return nullptr;
574     return new SPIR64TargetInfo(Triple, Opts);
575   }
576   case llvm::Triple::wasm32:
577     if (Triple.getSubArch() != llvm::Triple::NoSubArch ||
578         Triple.getVendor() != llvm::Triple::UnknownVendor ||
579         !Triple.isOSBinFormatWasm())
580       return nullptr;
581     switch (Triple.getOS()) {
582       case llvm::Triple::WASI:
583         return new WASITargetInfo<WebAssembly32TargetInfo>(Triple, Opts);
584       case llvm::Triple::Emscripten:
585         return new EmscriptenTargetInfo<WebAssembly32TargetInfo>(Triple, Opts);
586       case llvm::Triple::UnknownOS:
587         return new WebAssemblyOSTargetInfo<WebAssembly32TargetInfo>(Triple, Opts);
588       default:
589         return nullptr;
590     }
591   case llvm::Triple::wasm64:
592     if (Triple.getSubArch() != llvm::Triple::NoSubArch ||
593         Triple.getVendor() != llvm::Triple::UnknownVendor ||
594         !Triple.isOSBinFormatWasm())
595       return nullptr;
596     switch (Triple.getOS()) {
597       case llvm::Triple::WASI:
598         return new WASITargetInfo<WebAssembly64TargetInfo>(Triple, Opts);
599       case llvm::Triple::Emscripten:
600         return new EmscriptenTargetInfo<WebAssembly64TargetInfo>(Triple, Opts);
601       case llvm::Triple::UnknownOS:
602         return new WebAssemblyOSTargetInfo<WebAssembly64TargetInfo>(Triple, Opts);
603       default:
604         return nullptr;
605     }
606 
607   case llvm::Triple::renderscript32:
608     return new LinuxTargetInfo<RenderScript32TargetInfo>(Triple, Opts);
609   case llvm::Triple::renderscript64:
610     return new LinuxTargetInfo<RenderScript64TargetInfo>(Triple, Opts);
611   }
612 }
613 } // namespace targets
614 } // namespace clang
615 
616 using namespace clang::targets;
617 /// CreateTargetInfo - Return the target info object for the specified target
618 /// options.
619 TargetInfo *
620 TargetInfo::CreateTargetInfo(DiagnosticsEngine &Diags,
621                              const std::shared_ptr<TargetOptions> &Opts) {
622   llvm::Triple Triple(Opts->Triple);
623 
624   // Construct the target
625   std::unique_ptr<TargetInfo> Target(AllocateTarget(Triple, *Opts));
626   if (!Target) {
627     Diags.Report(diag::err_target_unknown_triple) << Triple.str();
628     return nullptr;
629   }
630   Target->TargetOpts = Opts;
631 
632   // Set the target CPU if specified.
633   if (!Opts->CPU.empty() && !Target->setCPU(Opts->CPU)) {
634     Diags.Report(diag::err_target_unknown_cpu) << Opts->CPU;
635     SmallVector<StringRef, 32> ValidList;
636     Target->fillValidCPUList(ValidList);
637     if (!ValidList.empty())
638       Diags.Report(diag::note_valid_options) << llvm::join(ValidList, ", ");
639     return nullptr;
640   }
641 
642   // Set the target ABI if specified.
643   if (!Opts->ABI.empty() && !Target->setABI(Opts->ABI)) {
644     Diags.Report(diag::err_target_unknown_abi) << Opts->ABI;
645     return nullptr;
646   }
647 
648   // Set the fp math unit.
649   if (!Opts->FPMath.empty() && !Target->setFPMath(Opts->FPMath)) {
650     Diags.Report(diag::err_target_unknown_fpmath) << Opts->FPMath;
651     return nullptr;
652   }
653 
654   // Compute the default target features, we need the target to handle this
655   // because features may have dependencies on one another.
656   llvm::StringMap<bool> Features;
657   if (!Target->initFeatureMap(Features, Diags, Opts->CPU,
658                               Opts->FeaturesAsWritten))
659     return nullptr;
660 
661   // Add the features to the compile options.
662   Opts->Features.clear();
663   for (const auto &F : Features)
664     Opts->Features.push_back((F.getValue() ? "+" : "-") + F.getKey().str());
665   // Sort here, so we handle the features in a predictable order. (This matters
666   // when we're dealing with features that overlap.)
667   llvm::sort(Opts->Features);
668 
669   if (!Target->handleTargetFeatures(Opts->Features, Diags))
670     return nullptr;
671 
672   Target->setSupportedOpenCLOpts();
673   Target->setOpenCLExtensionOpts();
674   Target->setMaxAtomicWidth();
675 
676   if (!Target->validateTarget(Diags))
677     return nullptr;
678 
679   Target->CheckFixedPointBits();
680 
681   return Target.release();
682 }
683