clang  11.0.0git
Targets.cpp
Go to the documentation of this file.
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::AIX:
350  return new AIXPPC64TargetInfo(Triple, Opts);
351  default:
352  return new PPC64TargetInfo(Triple, Opts);
353  }
354 
355  case llvm::Triple::ppc64le:
356  switch (os) {
357  case llvm::Triple::Linux:
358  return new LinuxTargetInfo<PPC64TargetInfo>(Triple, Opts);
359  case llvm::Triple::NetBSD:
360  return new NetBSDTargetInfo<PPC64TargetInfo>(Triple, Opts);
361  default:
362  return new PPC64TargetInfo(Triple, Opts);
363  }
364 
365  case llvm::Triple::nvptx:
366  return new NVPTXTargetInfo(Triple, Opts, /*TargetPointerWidth=*/32);
367  case llvm::Triple::nvptx64:
368  return new NVPTXTargetInfo(Triple, Opts, /*TargetPointerWidth=*/64);
369 
370  case llvm::Triple::amdgcn:
371  case llvm::Triple::r600:
372  return new AMDGPUTargetInfo(Triple, Opts);
373 
374  case llvm::Triple::riscv32:
375  // TODO: add cases for NetBSD, RTEMS once tested.
376  switch (os) {
377  case llvm::Triple::FreeBSD:
378  return new FreeBSDTargetInfo<RISCV32TargetInfo>(Triple, Opts);
379  case llvm::Triple::Linux:
380  return new LinuxTargetInfo<RISCV32TargetInfo>(Triple, Opts);
381  default:
382  return new RISCV32TargetInfo(Triple, Opts);
383  }
384 
385  case llvm::Triple::riscv64:
386  // TODO: add cases for NetBSD, RTEMS once tested.
387  switch (os) {
388  case llvm::Triple::FreeBSD:
389  return new FreeBSDTargetInfo<RISCV64TargetInfo>(Triple, Opts);
390  case llvm::Triple::Fuchsia:
391  return new FuchsiaTargetInfo<RISCV64TargetInfo>(Triple, Opts);
392  case llvm::Triple::Linux:
393  return new LinuxTargetInfo<RISCV64TargetInfo>(Triple, Opts);
394  default:
395  return new RISCV64TargetInfo(Triple, Opts);
396  }
397 
398  case llvm::Triple::sparc:
399  switch (os) {
400  case llvm::Triple::Linux:
401  return new LinuxTargetInfo<SparcV8TargetInfo>(Triple, Opts);
402  case llvm::Triple::Solaris:
403  return new SolarisTargetInfo<SparcV8TargetInfo>(Triple, Opts);
404  case llvm::Triple::NetBSD:
405  return new NetBSDTargetInfo<SparcV8TargetInfo>(Triple, Opts);
406  case llvm::Triple::OpenBSD:
407  return new OpenBSDTargetInfo<SparcV8TargetInfo>(Triple, Opts);
408  case llvm::Triple::RTEMS:
409  return new RTEMSTargetInfo<SparcV8TargetInfo>(Triple, Opts);
410  default:
411  return new SparcV8TargetInfo(Triple, Opts);
412  }
413 
414  // The 'sparcel' architecture copies all the above cases except for Solaris.
415  case llvm::Triple::sparcel:
416  switch (os) {
417  case llvm::Triple::Linux:
418  return new LinuxTargetInfo<SparcV8elTargetInfo>(Triple, Opts);
419  case llvm::Triple::NetBSD:
420  return new NetBSDTargetInfo<SparcV8elTargetInfo>(Triple, Opts);
421  case llvm::Triple::OpenBSD:
422  return new OpenBSDTargetInfo<SparcV8elTargetInfo>(Triple, Opts);
423  case llvm::Triple::RTEMS:
424  return new RTEMSTargetInfo<SparcV8elTargetInfo>(Triple, Opts);
425  default:
426  return new SparcV8elTargetInfo(Triple, Opts);
427  }
428 
429  case llvm::Triple::sparcv9:
430  switch (os) {
431  case llvm::Triple::Linux:
432  return new LinuxTargetInfo<SparcV9TargetInfo>(Triple, Opts);
433  case llvm::Triple::Solaris:
434  return new SolarisTargetInfo<SparcV9TargetInfo>(Triple, Opts);
435  case llvm::Triple::NetBSD:
436  return new NetBSDTargetInfo<SparcV9TargetInfo>(Triple, Opts);
437  case llvm::Triple::OpenBSD:
438  return new OpenBSDTargetInfo<SparcV9TargetInfo>(Triple, Opts);
439  case llvm::Triple::FreeBSD:
440  return new FreeBSDTargetInfo<SparcV9TargetInfo>(Triple, Opts);
441  default:
442  return new SparcV9TargetInfo(Triple, Opts);
443  }
444 
445  case llvm::Triple::systemz:
446  switch (os) {
447  case llvm::Triple::Linux:
448  return new LinuxTargetInfo<SystemZTargetInfo>(Triple, Opts);
449  default:
450  return new SystemZTargetInfo(Triple, Opts);
451  }
452 
453  case llvm::Triple::tce:
454  return new TCETargetInfo(Triple, Opts);
455 
456  case llvm::Triple::tcele:
457  return new TCELETargetInfo(Triple, Opts);
458 
459  case llvm::Triple::x86:
460  if (Triple.isOSDarwin())
461  return new DarwinI386TargetInfo(Triple, Opts);
462 
463  switch (os) {
464  case llvm::Triple::Ananas:
465  return new AnanasTargetInfo<X86_32TargetInfo>(Triple, Opts);
466  case llvm::Triple::CloudABI:
467  return new CloudABITargetInfo<X86_32TargetInfo>(Triple, Opts);
468  case llvm::Triple::Linux: {
469  switch (Triple.getEnvironment()) {
470  default:
471  return new LinuxTargetInfo<X86_32TargetInfo>(Triple, Opts);
472  case llvm::Triple::Android:
473  return new AndroidX86_32TargetInfo(Triple, Opts);
474  }
475  }
476  case llvm::Triple::DragonFly:
477  return new DragonFlyBSDTargetInfo<X86_32TargetInfo>(Triple, Opts);
478  case llvm::Triple::NetBSD:
479  return new NetBSDI386TargetInfo(Triple, Opts);
480  case llvm::Triple::OpenBSD:
481  return new OpenBSDI386TargetInfo(Triple, Opts);
482  case llvm::Triple::FreeBSD:
483  return new FreeBSDTargetInfo<X86_32TargetInfo>(Triple, Opts);
484  case llvm::Triple::Fuchsia:
485  return new FuchsiaTargetInfo<X86_32TargetInfo>(Triple, Opts);
486  case llvm::Triple::KFreeBSD:
487  return new KFreeBSDTargetInfo<X86_32TargetInfo>(Triple, Opts);
488  case llvm::Triple::Minix:
489  return new MinixTargetInfo<X86_32TargetInfo>(Triple, Opts);
490  case llvm::Triple::Solaris:
491  return new SolarisTargetInfo<X86_32TargetInfo>(Triple, Opts);
492  case llvm::Triple::Win32: {
493  switch (Triple.getEnvironment()) {
494  case llvm::Triple::Cygnus:
495  return new CygwinX86_32TargetInfo(Triple, Opts);
496  case llvm::Triple::GNU:
497  return new MinGWX86_32TargetInfo(Triple, Opts);
498  case llvm::Triple::Itanium:
499  case llvm::Triple::MSVC:
500  default: // Assume MSVC for unknown environments
501  return new MicrosoftX86_32TargetInfo(Triple, Opts);
502  }
503  }
504  case llvm::Triple::Haiku:
505  return new HaikuX86_32TargetInfo(Triple, Opts);
506  case llvm::Triple::RTEMS:
507  return new RTEMSX86_32TargetInfo(Triple, Opts);
508  case llvm::Triple::NaCl:
509  return new NaClTargetInfo<X86_32TargetInfo>(Triple, Opts);
510  case llvm::Triple::ELFIAMCU:
511  return new MCUX86_32TargetInfo(Triple, Opts);
512  case llvm::Triple::Hurd:
513  return new HurdTargetInfo<X86_32TargetInfo>(Triple, Opts);
514  default:
515  return new X86_32TargetInfo(Triple, Opts);
516  }
517 
518  case llvm::Triple::x86_64:
519  if (Triple.isOSDarwin() || Triple.isOSBinFormatMachO())
520  return new DarwinX86_64TargetInfo(Triple, Opts);
521 
522  switch (os) {
523  case llvm::Triple::Ananas:
524  return new AnanasTargetInfo<X86_64TargetInfo>(Triple, Opts);
525  case llvm::Triple::CloudABI:
526  return new CloudABITargetInfo<X86_64TargetInfo>(Triple, Opts);
527  case llvm::Triple::Linux: {
528  switch (Triple.getEnvironment()) {
529  default:
530  return new LinuxTargetInfo<X86_64TargetInfo>(Triple, Opts);
531  case llvm::Triple::Android:
532  return new AndroidX86_64TargetInfo(Triple, Opts);
533  }
534  }
535  case llvm::Triple::DragonFly:
536  return new DragonFlyBSDTargetInfo<X86_64TargetInfo>(Triple, Opts);
537  case llvm::Triple::NetBSD:
538  return new NetBSDTargetInfo<X86_64TargetInfo>(Triple, Opts);
539  case llvm::Triple::OpenBSD:
540  return new OpenBSDX86_64TargetInfo(Triple, Opts);
541  case llvm::Triple::FreeBSD:
542  return new FreeBSDTargetInfo<X86_64TargetInfo>(Triple, Opts);
543  case llvm::Triple::Fuchsia:
544  return new FuchsiaTargetInfo<X86_64TargetInfo>(Triple, Opts);
545  case llvm::Triple::KFreeBSD:
546  return new KFreeBSDTargetInfo<X86_64TargetInfo>(Triple, Opts);
547  case llvm::Triple::Solaris:
548  return new SolarisTargetInfo<X86_64TargetInfo>(Triple, Opts);
549  case llvm::Triple::Win32: {
550  switch (Triple.getEnvironment()) {
551  case llvm::Triple::Cygnus:
552  return new CygwinX86_64TargetInfo(Triple, Opts);
553  case llvm::Triple::GNU:
554  return new MinGWX86_64TargetInfo(Triple, Opts);
555  case llvm::Triple::MSVC:
556  default: // Assume MSVC for unknown environments
557  return new MicrosoftX86_64TargetInfo(Triple, Opts);
558  }
559  }
560  case llvm::Triple::Haiku:
561  return new HaikuTargetInfo<X86_64TargetInfo>(Triple, Opts);
562  case llvm::Triple::NaCl:
563  return new NaClTargetInfo<X86_64TargetInfo>(Triple, Opts);
564  case llvm::Triple::PS4:
565  return new PS4OSTargetInfo<X86_64TargetInfo>(Triple, Opts);
566  default:
567  return new X86_64TargetInfo(Triple, Opts);
568  }
569 
570  case llvm::Triple::spir: {
571  if (Triple.getOS() != llvm::Triple::UnknownOS ||
572  Triple.getEnvironment() != llvm::Triple::UnknownEnvironment)
573  return nullptr;
574  return new SPIR32TargetInfo(Triple, Opts);
575  }
576  case llvm::Triple::spir64: {
577  if (Triple.getOS() != llvm::Triple::UnknownOS ||
578  Triple.getEnvironment() != llvm::Triple::UnknownEnvironment)
579  return nullptr;
580  return new SPIR64TargetInfo(Triple, Opts);
581  }
582  case llvm::Triple::wasm32:
583  if (Triple.getSubArch() != llvm::Triple::NoSubArch ||
584  Triple.getVendor() != llvm::Triple::UnknownVendor ||
585  !Triple.isOSBinFormatWasm())
586  return nullptr;
587  switch (Triple.getOS()) {
588  case llvm::Triple::WASI:
589  return new WASITargetInfo<WebAssembly32TargetInfo>(Triple, Opts);
590  case llvm::Triple::Emscripten:
591  return new EmscriptenTargetInfo<WebAssembly32TargetInfo>(Triple, Opts);
592  case llvm::Triple::UnknownOS:
593  return new WebAssemblyOSTargetInfo<WebAssembly32TargetInfo>(Triple, Opts);
594  default:
595  return nullptr;
596  }
597  case llvm::Triple::wasm64:
598  if (Triple.getSubArch() != llvm::Triple::NoSubArch ||
599  Triple.getVendor() != llvm::Triple::UnknownVendor ||
600  !Triple.isOSBinFormatWasm())
601  return nullptr;
602  switch (Triple.getOS()) {
603  case llvm::Triple::WASI:
604  return new WASITargetInfo<WebAssembly64TargetInfo>(Triple, Opts);
605  case llvm::Triple::Emscripten:
606  return new EmscriptenTargetInfo<WebAssembly64TargetInfo>(Triple, Opts);
607  case llvm::Triple::UnknownOS:
608  return new WebAssemblyOSTargetInfo<WebAssembly64TargetInfo>(Triple, Opts);
609  default:
610  return nullptr;
611  }
612 
613  case llvm::Triple::renderscript32:
614  return new LinuxTargetInfo<RenderScript32TargetInfo>(Triple, Opts);
615  case llvm::Triple::renderscript64:
616  return new LinuxTargetInfo<RenderScript64TargetInfo>(Triple, Opts);
617 
618  case llvm::Triple::ve:
619  return new LinuxTargetInfo<VETargetInfo>(Triple, Opts);
620  }
621 }
622 } // namespace targets
623 } // namespace clang
624 
625 using namespace clang::targets;
626 /// CreateTargetInfo - Return the target info object for the specified target
627 /// options.
628 TargetInfo *
630  const std::shared_ptr<TargetOptions> &Opts) {
631  llvm::Triple Triple(Opts->Triple);
632 
633  // Construct the target
634  std::unique_ptr<TargetInfo> Target(AllocateTarget(Triple, *Opts));
635  if (!Target) {
636  Diags.Report(diag::err_target_unknown_triple) << Triple.str();
637  return nullptr;
638  }
639  Target->TargetOpts = Opts;
640 
641  // Set the target CPU if specified.
642  if (!Opts->CPU.empty() && !Target->setCPU(Opts->CPU)) {
643  Diags.Report(diag::err_target_unknown_cpu) << Opts->CPU;
644  SmallVector<StringRef, 32> ValidList;
645  Target->fillValidCPUList(ValidList);
646  if (!ValidList.empty())
647  Diags.Report(diag::note_valid_options) << llvm::join(ValidList, ", ");
648  return nullptr;
649  }
650 
651  // Set the target ABI if specified.
652  if (!Opts->ABI.empty() && !Target->setABI(Opts->ABI)) {
653  Diags.Report(diag::err_target_unknown_abi) << Opts->ABI;
654  return nullptr;
655  }
656 
657  // Set the fp math unit.
658  if (!Opts->FPMath.empty() && !Target->setFPMath(Opts->FPMath)) {
659  Diags.Report(diag::err_target_unknown_fpmath) << Opts->FPMath;
660  return nullptr;
661  }
662 
663  // Compute the default target features, we need the target to handle this
664  // because features may have dependencies on one another.
665  llvm::StringMap<bool> Features;
666  if (!Target->initFeatureMap(Features, Diags, Opts->CPU,
667  Opts->FeaturesAsWritten))
668  return nullptr;
669 
670  // Add the features to the compile options.
671  Opts->Features.clear();
672  for (const auto &F : Features)
673  Opts->Features.push_back((F.getValue() ? "+" : "-") + F.getKey().str());
674  // Sort here, so we handle the features in a predictable order. (This matters
675  // when we're dealing with features that overlap.)
676  llvm::sort(Opts->Features);
677 
678  if (!Target->handleTargetFeatures(Opts->Features, Diags))
679  return nullptr;
680 
681  Target->setSupportedOpenCLOpts();
682  Target->setOpenCLExtensionOpts();
683  Target->setMaxAtomicWidth();
684 
685  if (!Target->validateTarget(Diags))
686  return nullptr;
687 
688  Target->CheckFixedPointBits();
689 
690  return Target.release();
691 }
void DefineStd(MacroBuilder &Builder, StringRef MacroName, const LangOptions &Opts)
DefineStd - Define a macro name and standard variants.
Definition: Targets.cpp:55
DiagnosticBuilder Report(SourceLocation Loc, unsigned DiagID)
Issue the message to the client.
Definition: Diagnostic.h:1330
Options for controlling the target.
Definition: TargetOptions.h:26
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:54
Concrete class used by the front-end to report problems and issues.
Definition: Diagnostic.h:153
Defines the Diagnostic-related interfaces.
Exposes information about the current target.
Definition: TargetInfo.h:179
static TargetInfo * CreateTargetInfo(DiagnosticsEngine &Diags, const std::shared_ptr< TargetOptions > &Opts)
Construct a target for the given options.
Definition: Targets.cpp:629
TargetInfo * AllocateTarget(const llvm::Triple &Triple, const TargetOptions &Opts)
Definition: Targets.cpp:106
void addCygMingDefines(const LangOptions &Opts, MacroBuilder &Builder)
Definition: Targets.cpp:78
Dataflow Directional Tag Classes.
clang::driver::toolchains::AIX AIX
Definition: AIX.cpp:17
void defineCPUMacros(MacroBuilder &Builder, StringRef CPUName, bool Tuning)
Definition: Targets.cpp:71
void defineMacro(const Twine &Name, const Twine &Value="1")
Append a #define line for macro of the form "\#define Name Value\n".
Definition: MacroBuilder.h:29