xref: /freebsd/contrib/llvm-project/clang/lib/Basic/Targets.cpp (revision 79ac3c12a714bcd3f2354c52d948aed9575c46d6)
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::ppcle:
338     switch (os) {
339     case llvm::Triple::Linux:
340       return new LinuxTargetInfo<PPC32TargetInfo>(Triple, Opts);
341     case llvm::Triple::FreeBSD:
342       return new FreeBSDTargetInfo<PPC32TargetInfo>(Triple, Opts);
343     default:
344       return new PPC32TargetInfo(Triple, Opts);
345     }
346 
347   case llvm::Triple::ppc64:
348     if (Triple.isOSDarwin())
349       return new DarwinPPC64TargetInfo(Triple, Opts);
350     switch (os) {
351     case llvm::Triple::Linux:
352       return new LinuxTargetInfo<PPC64TargetInfo>(Triple, Opts);
353     case llvm::Triple::Lv2:
354       return new PS3PPUTargetInfo<PPC64TargetInfo>(Triple, Opts);
355     case llvm::Triple::FreeBSD:
356       return new FreeBSDTargetInfo<PPC64TargetInfo>(Triple, Opts);
357     case llvm::Triple::NetBSD:
358       return new NetBSDTargetInfo<PPC64TargetInfo>(Triple, Opts);
359     case llvm::Triple::OpenBSD:
360       return new OpenBSDTargetInfo<PPC64TargetInfo>(Triple, Opts);
361     case llvm::Triple::AIX:
362       return new AIXPPC64TargetInfo(Triple, Opts);
363     default:
364       return new PPC64TargetInfo(Triple, Opts);
365     }
366 
367   case llvm::Triple::ppc64le:
368     switch (os) {
369     case llvm::Triple::Linux:
370       return new LinuxTargetInfo<PPC64TargetInfo>(Triple, Opts);
371     case llvm::Triple::FreeBSD:
372       return new FreeBSDTargetInfo<PPC64TargetInfo>(Triple, Opts);
373     case llvm::Triple::NetBSD:
374       return new NetBSDTargetInfo<PPC64TargetInfo>(Triple, Opts);
375     case llvm::Triple::OpenBSD:
376       return new OpenBSDTargetInfo<PPC64TargetInfo>(Triple, Opts);
377     default:
378       return new PPC64TargetInfo(Triple, Opts);
379     }
380 
381   case llvm::Triple::nvptx:
382     return new NVPTXTargetInfo(Triple, Opts, /*TargetPointerWidth=*/32);
383   case llvm::Triple::nvptx64:
384     return new NVPTXTargetInfo(Triple, Opts, /*TargetPointerWidth=*/64);
385 
386   case llvm::Triple::amdgcn:
387   case llvm::Triple::r600:
388     return new AMDGPUTargetInfo(Triple, Opts);
389 
390   case llvm::Triple::riscv32:
391     // TODO: add cases for NetBSD, RTEMS once tested.
392     switch (os) {
393     case llvm::Triple::FreeBSD:
394       return new FreeBSDTargetInfo<RISCV32TargetInfo>(Triple, Opts);
395     case llvm::Triple::Linux:
396       return new LinuxTargetInfo<RISCV32TargetInfo>(Triple, Opts);
397     default:
398       return new RISCV32TargetInfo(Triple, Opts);
399     }
400 
401   case llvm::Triple::riscv64:
402     // TODO: add cases for NetBSD, RTEMS once tested.
403     switch (os) {
404     case llvm::Triple::FreeBSD:
405       return new FreeBSDTargetInfo<RISCV64TargetInfo>(Triple, Opts);
406     case llvm::Triple::OpenBSD:
407       return new OpenBSDTargetInfo<RISCV64TargetInfo>(Triple, Opts);
408     case llvm::Triple::Fuchsia:
409       return new FuchsiaTargetInfo<RISCV64TargetInfo>(Triple, Opts);
410     case llvm::Triple::Linux:
411       return new LinuxTargetInfo<RISCV64TargetInfo>(Triple, Opts);
412     default:
413       return new RISCV64TargetInfo(Triple, Opts);
414     }
415 
416   case llvm::Triple::sparc:
417     switch (os) {
418     case llvm::Triple::Linux:
419       return new LinuxTargetInfo<SparcV8TargetInfo>(Triple, Opts);
420     case llvm::Triple::Solaris:
421       return new SolarisTargetInfo<SparcV8TargetInfo>(Triple, Opts);
422     case llvm::Triple::NetBSD:
423       return new NetBSDTargetInfo<SparcV8TargetInfo>(Triple, Opts);
424     case llvm::Triple::RTEMS:
425       return new RTEMSTargetInfo<SparcV8TargetInfo>(Triple, Opts);
426     default:
427       return new SparcV8TargetInfo(Triple, Opts);
428     }
429 
430   // The 'sparcel' architecture copies all the above cases except for Solaris.
431   case llvm::Triple::sparcel:
432     switch (os) {
433     case llvm::Triple::Linux:
434       return new LinuxTargetInfo<SparcV8elTargetInfo>(Triple, Opts);
435     case llvm::Triple::NetBSD:
436       return new NetBSDTargetInfo<SparcV8elTargetInfo>(Triple, Opts);
437     case llvm::Triple::RTEMS:
438       return new RTEMSTargetInfo<SparcV8elTargetInfo>(Triple, Opts);
439     default:
440       return new SparcV8elTargetInfo(Triple, Opts);
441     }
442 
443   case llvm::Triple::sparcv9:
444     switch (os) {
445     case llvm::Triple::Linux:
446       return new LinuxTargetInfo<SparcV9TargetInfo>(Triple, Opts);
447     case llvm::Triple::Solaris:
448       return new SolarisTargetInfo<SparcV9TargetInfo>(Triple, Opts);
449     case llvm::Triple::NetBSD:
450       return new NetBSDTargetInfo<SparcV9TargetInfo>(Triple, Opts);
451     case llvm::Triple::OpenBSD:
452       return new OpenBSDTargetInfo<SparcV9TargetInfo>(Triple, Opts);
453     case llvm::Triple::FreeBSD:
454       return new FreeBSDTargetInfo<SparcV9TargetInfo>(Triple, Opts);
455     default:
456       return new SparcV9TargetInfo(Triple, Opts);
457     }
458 
459   case llvm::Triple::systemz:
460     switch (os) {
461     case llvm::Triple::Linux:
462       return new LinuxTargetInfo<SystemZTargetInfo>(Triple, Opts);
463     case llvm::Triple::ZOS:
464       return new ZOSTargetInfo<SystemZTargetInfo>(Triple, Opts);
465     default:
466       return new SystemZTargetInfo(Triple, Opts);
467     }
468 
469   case llvm::Triple::tce:
470     return new TCETargetInfo(Triple, Opts);
471 
472   case llvm::Triple::tcele:
473     return new TCELETargetInfo(Triple, Opts);
474 
475   case llvm::Triple::x86:
476     if (Triple.isOSDarwin())
477       return new DarwinI386TargetInfo(Triple, Opts);
478 
479     switch (os) {
480     case llvm::Triple::Ananas:
481       return new AnanasTargetInfo<X86_32TargetInfo>(Triple, Opts);
482     case llvm::Triple::CloudABI:
483       return new CloudABITargetInfo<X86_32TargetInfo>(Triple, Opts);
484     case llvm::Triple::Linux: {
485       switch (Triple.getEnvironment()) {
486       default:
487         return new LinuxTargetInfo<X86_32TargetInfo>(Triple, Opts);
488       case llvm::Triple::Android:
489         return new AndroidX86_32TargetInfo(Triple, Opts);
490       }
491     }
492     case llvm::Triple::DragonFly:
493       return new DragonFlyBSDTargetInfo<X86_32TargetInfo>(Triple, Opts);
494     case llvm::Triple::NetBSD:
495       return new NetBSDI386TargetInfo(Triple, Opts);
496     case llvm::Triple::OpenBSD:
497       return new OpenBSDI386TargetInfo(Triple, Opts);
498     case llvm::Triple::FreeBSD:
499       return new FreeBSDTargetInfo<X86_32TargetInfo>(Triple, Opts);
500     case llvm::Triple::Fuchsia:
501       return new FuchsiaTargetInfo<X86_32TargetInfo>(Triple, Opts);
502     case llvm::Triple::KFreeBSD:
503       return new KFreeBSDTargetInfo<X86_32TargetInfo>(Triple, Opts);
504     case llvm::Triple::Minix:
505       return new MinixTargetInfo<X86_32TargetInfo>(Triple, Opts);
506     case llvm::Triple::Solaris:
507       return new SolarisTargetInfo<X86_32TargetInfo>(Triple, Opts);
508     case llvm::Triple::Win32: {
509       switch (Triple.getEnvironment()) {
510       case llvm::Triple::Cygnus:
511         return new CygwinX86_32TargetInfo(Triple, Opts);
512       case llvm::Triple::GNU:
513         return new MinGWX86_32TargetInfo(Triple, Opts);
514       case llvm::Triple::Itanium:
515       case llvm::Triple::MSVC:
516       default: // Assume MSVC for unknown environments
517         return new MicrosoftX86_32TargetInfo(Triple, Opts);
518       }
519     }
520     case llvm::Triple::Haiku:
521       return new HaikuX86_32TargetInfo(Triple, Opts);
522     case llvm::Triple::RTEMS:
523       return new RTEMSX86_32TargetInfo(Triple, Opts);
524     case llvm::Triple::NaCl:
525       return new NaClTargetInfo<X86_32TargetInfo>(Triple, Opts);
526     case llvm::Triple::ELFIAMCU:
527       return new MCUX86_32TargetInfo(Triple, Opts);
528     case llvm::Triple::Hurd:
529       return new HurdTargetInfo<X86_32TargetInfo>(Triple, Opts);
530     default:
531       return new X86_32TargetInfo(Triple, Opts);
532     }
533 
534   case llvm::Triple::x86_64:
535     if (Triple.isOSDarwin() || Triple.isOSBinFormatMachO())
536       return new DarwinX86_64TargetInfo(Triple, Opts);
537 
538     switch (os) {
539     case llvm::Triple::Ananas:
540       return new AnanasTargetInfo<X86_64TargetInfo>(Triple, Opts);
541     case llvm::Triple::CloudABI:
542       return new CloudABITargetInfo<X86_64TargetInfo>(Triple, Opts);
543     case llvm::Triple::Linux: {
544       switch (Triple.getEnvironment()) {
545       default:
546         return new LinuxTargetInfo<X86_64TargetInfo>(Triple, Opts);
547       case llvm::Triple::Android:
548         return new AndroidX86_64TargetInfo(Triple, Opts);
549       }
550     }
551     case llvm::Triple::DragonFly:
552       return new DragonFlyBSDTargetInfo<X86_64TargetInfo>(Triple, Opts);
553     case llvm::Triple::NetBSD:
554       return new NetBSDTargetInfo<X86_64TargetInfo>(Triple, Opts);
555     case llvm::Triple::OpenBSD:
556       return new OpenBSDX86_64TargetInfo(Triple, Opts);
557     case llvm::Triple::FreeBSD:
558       return new FreeBSDTargetInfo<X86_64TargetInfo>(Triple, Opts);
559     case llvm::Triple::Fuchsia:
560       return new FuchsiaTargetInfo<X86_64TargetInfo>(Triple, Opts);
561     case llvm::Triple::KFreeBSD:
562       return new KFreeBSDTargetInfo<X86_64TargetInfo>(Triple, Opts);
563     case llvm::Triple::Solaris:
564       return new SolarisTargetInfo<X86_64TargetInfo>(Triple, Opts);
565     case llvm::Triple::Win32: {
566       switch (Triple.getEnvironment()) {
567       case llvm::Triple::Cygnus:
568         return new CygwinX86_64TargetInfo(Triple, Opts);
569       case llvm::Triple::GNU:
570         return new MinGWX86_64TargetInfo(Triple, Opts);
571       case llvm::Triple::MSVC:
572       default: // Assume MSVC for unknown environments
573         return new MicrosoftX86_64TargetInfo(Triple, Opts);
574       }
575     }
576     case llvm::Triple::Haiku:
577       return new HaikuTargetInfo<X86_64TargetInfo>(Triple, Opts);
578     case llvm::Triple::NaCl:
579       return new NaClTargetInfo<X86_64TargetInfo>(Triple, Opts);
580     case llvm::Triple::PS4:
581       return new PS4OSTargetInfo<X86_64TargetInfo>(Triple, Opts);
582     default:
583       return new X86_64TargetInfo(Triple, Opts);
584     }
585 
586   case llvm::Triple::spir: {
587     if (Triple.getOS() != llvm::Triple::UnknownOS ||
588         Triple.getEnvironment() != llvm::Triple::UnknownEnvironment)
589       return nullptr;
590     return new SPIR32TargetInfo(Triple, Opts);
591   }
592   case llvm::Triple::spir64: {
593     if (Triple.getOS() != llvm::Triple::UnknownOS ||
594         Triple.getEnvironment() != llvm::Triple::UnknownEnvironment)
595       return nullptr;
596     return new SPIR64TargetInfo(Triple, Opts);
597   }
598   case llvm::Triple::wasm32:
599     if (Triple.getSubArch() != llvm::Triple::NoSubArch ||
600         Triple.getVendor() != llvm::Triple::UnknownVendor ||
601         !Triple.isOSBinFormatWasm())
602       return nullptr;
603     switch (Triple.getOS()) {
604       case llvm::Triple::WASI:
605         return new WASITargetInfo<WebAssembly32TargetInfo>(Triple, Opts);
606       case llvm::Triple::Emscripten:
607         return new EmscriptenTargetInfo<WebAssembly32TargetInfo>(Triple, Opts);
608       case llvm::Triple::UnknownOS:
609         return new WebAssemblyOSTargetInfo<WebAssembly32TargetInfo>(Triple, Opts);
610       default:
611         return nullptr;
612     }
613   case llvm::Triple::wasm64:
614     if (Triple.getSubArch() != llvm::Triple::NoSubArch ||
615         Triple.getVendor() != llvm::Triple::UnknownVendor ||
616         !Triple.isOSBinFormatWasm())
617       return nullptr;
618     switch (Triple.getOS()) {
619       case llvm::Triple::WASI:
620         return new WASITargetInfo<WebAssembly64TargetInfo>(Triple, Opts);
621       case llvm::Triple::Emscripten:
622         return new EmscriptenTargetInfo<WebAssembly64TargetInfo>(Triple, Opts);
623       case llvm::Triple::UnknownOS:
624         return new WebAssemblyOSTargetInfo<WebAssembly64TargetInfo>(Triple, Opts);
625       default:
626         return nullptr;
627     }
628 
629   case llvm::Triple::renderscript32:
630     return new LinuxTargetInfo<RenderScript32TargetInfo>(Triple, Opts);
631   case llvm::Triple::renderscript64:
632     return new LinuxTargetInfo<RenderScript64TargetInfo>(Triple, Opts);
633 
634   case llvm::Triple::ve:
635     return new LinuxTargetInfo<VETargetInfo>(Triple, Opts);
636   }
637 }
638 } // namespace targets
639 } // namespace clang
640 
641 using namespace clang::targets;
642 /// CreateTargetInfo - Return the target info object for the specified target
643 /// options.
644 TargetInfo *
645 TargetInfo::CreateTargetInfo(DiagnosticsEngine &Diags,
646                              const std::shared_ptr<TargetOptions> &Opts) {
647   llvm::Triple Triple(Opts->Triple);
648 
649   // Construct the target
650   std::unique_ptr<TargetInfo> Target(AllocateTarget(Triple, *Opts));
651   if (!Target) {
652     Diags.Report(diag::err_target_unknown_triple) << Triple.str();
653     return nullptr;
654   }
655   Target->TargetOpts = Opts;
656 
657   // Set the target CPU if specified.
658   if (!Opts->CPU.empty() && !Target->setCPU(Opts->CPU)) {
659     Diags.Report(diag::err_target_unknown_cpu) << Opts->CPU;
660     SmallVector<StringRef, 32> ValidList;
661     Target->fillValidCPUList(ValidList);
662     if (!ValidList.empty())
663       Diags.Report(diag::note_valid_options) << llvm::join(ValidList, ", ");
664     return nullptr;
665   }
666 
667   // Check the TuneCPU name if specified.
668   if (!Opts->TuneCPU.empty() &&
669       !Target->isValidTuneCPUName(Opts->TuneCPU)) {
670     Diags.Report(diag::err_target_unknown_cpu) << Opts->TuneCPU;
671     SmallVector<StringRef, 32> ValidList;
672     Target->fillValidTuneCPUList(ValidList);
673     if (!ValidList.empty())
674       Diags.Report(diag::note_valid_options) << llvm::join(ValidList, ", ");
675     return nullptr;
676   }
677 
678   // Set the target ABI if specified.
679   if (!Opts->ABI.empty() && !Target->setABI(Opts->ABI)) {
680     Diags.Report(diag::err_target_unknown_abi) << Opts->ABI;
681     return nullptr;
682   }
683 
684   // Set the fp math unit.
685   if (!Opts->FPMath.empty() && !Target->setFPMath(Opts->FPMath)) {
686     Diags.Report(diag::err_target_unknown_fpmath) << Opts->FPMath;
687     return nullptr;
688   }
689 
690   // Compute the default target features, we need the target to handle this
691   // because features may have dependencies on one another.
692   if (!Target->initFeatureMap(Opts->FeatureMap, Diags, Opts->CPU,
693                               Opts->FeaturesAsWritten))
694     return nullptr;
695 
696   // Add the features to the compile options.
697   Opts->Features.clear();
698   for (const auto &F : Opts->FeatureMap)
699     Opts->Features.push_back((F.getValue() ? "+" : "-") + F.getKey().str());
700   // Sort here, so we handle the features in a predictable order. (This matters
701   // when we're dealing with features that overlap.)
702   llvm::sort(Opts->Features);
703 
704   if (!Target->handleTargetFeatures(Opts->Features, Diags))
705     return nullptr;
706 
707   Target->setSupportedOpenCLOpts();
708   Target->setCommandLineOpenCLOpts();
709   Target->setMaxAtomicWidth();
710 
711   if (!Target->validateTarget(Diags))
712     return nullptr;
713 
714   Target->CheckFixedPointBits();
715 
716   return Target.release();
717 }
718 
719 /// getOpenCLFeatureDefines - Define OpenCL macros based on target settings
720 /// and language version
721 void TargetInfo::getOpenCLFeatureDefines(const LangOptions &Opts,
722                                          MacroBuilder &Builder) const {
723 
724   auto defineOpenCLExtMacro = [&](llvm::StringRef Name, unsigned AvailVer,
725                                   unsigned CoreVersions,
726                                   unsigned OptionalVersions) {
727     // Check if extension is supported by target and is available in this
728     // OpenCL version
729     auto It = getTargetOpts().OpenCLFeaturesMap.find(Name);
730     if ((It != getTargetOpts().OpenCLFeaturesMap.end()) && It->getValue() &&
731         OpenCLOptions::OpenCLOptionInfo(AvailVer, CoreVersions,
732                                         OptionalVersions)
733             .isAvailableIn(Opts))
734       Builder.defineMacro(Name);
735   };
736 #define OPENCL_GENERIC_EXTENSION(Ext, Avail, Core, Opt)                        \
737   defineOpenCLExtMacro(#Ext, Avail, Core, Opt);
738 #include "clang/Basic/OpenCLExtensions.def"
739 
740   // FIXME: OpenCL options which affect language semantics/syntax
741   // should be moved into LangOptions, thus macro definitions of
742   // such options is better to be done in clang::InitializePreprocessor
743 }
744