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