1 //===--- Targets.cpp - Implement target feature support -------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements construction of a TargetInfo object from a
10 // target triple.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "Targets.h"
15
16 #include "Targets/AArch64.h"
17 #include "Targets/AMDGPU.h"
18 #include "Targets/ARC.h"
19 #include "Targets/ARM.h"
20 #include "Targets/AVR.h"
21 #include "Targets/BPF.h"
22 #include "Targets/CSKY.h"
23 #include "Targets/DirectX.h"
24 #include "Targets/Hexagon.h"
25 #include "Targets/Lanai.h"
26 #include "Targets/Le64.h"
27 #include "Targets/LoongArch.h"
28 #include "Targets/M68k.h"
29 #include "Targets/MSP430.h"
30 #include "Targets/Mips.h"
31 #include "Targets/NVPTX.h"
32 #include "Targets/OSTargets.h"
33 #include "Targets/PNaCl.h"
34 #include "Targets/PPC.h"
35 #include "Targets/RISCV.h"
36 #include "Targets/SPIR.h"
37 #include "Targets/Sparc.h"
38 #include "Targets/SystemZ.h"
39 #include "Targets/TCE.h"
40 #include "Targets/VE.h"
41 #include "Targets/WebAssembly.h"
42 #include "Targets/X86.h"
43 #include "Targets/XCore.h"
44 #include "clang/Basic/Diagnostic.h"
45 #include "clang/Basic/DiagnosticFrontend.h"
46 #include "llvm/ADT/StringExtras.h"
47 #include "llvm/TargetParser/Triple.h"
48
49 using namespace clang;
50
51 namespace clang {
52 namespace targets {
53 //===----------------------------------------------------------------------===//
54 // Common code shared among targets.
55 //===----------------------------------------------------------------------===//
56
57 /// DefineStd - Define a macro name and standard variants. For example if
58 /// MacroName is "unix", then this will define "__unix", "__unix__", and "unix"
59 /// when in GNU mode.
DefineStd(MacroBuilder & Builder,StringRef MacroName,const LangOptions & Opts)60 void DefineStd(MacroBuilder &Builder, StringRef MacroName,
61 const LangOptions &Opts) {
62 assert(MacroName[0] != '_' && "Identifier should be in the user's namespace");
63
64 // If in GNU mode (e.g. -std=gnu99 but not -std=c99) define the raw identifier
65 // in the user's namespace.
66 if (Opts.GNUMode)
67 Builder.defineMacro(MacroName);
68
69 // Define __unix.
70 Builder.defineMacro("__" + MacroName);
71
72 // Define __unix__.
73 Builder.defineMacro("__" + MacroName + "__");
74 }
75
defineCPUMacros(MacroBuilder & Builder,StringRef CPUName,bool Tuning)76 void defineCPUMacros(MacroBuilder &Builder, StringRef CPUName, bool Tuning) {
77 Builder.defineMacro("__" + CPUName);
78 Builder.defineMacro("__" + CPUName + "__");
79 if (Tuning)
80 Builder.defineMacro("__tune_" + CPUName + "__");
81 }
82
addCygMingDefines(const LangOptions & Opts,MacroBuilder & Builder)83 void addCygMingDefines(const LangOptions &Opts, MacroBuilder &Builder) {
84 // Mingw and cygwin define __declspec(a) to __attribute__((a)). Clang
85 // supports __declspec natively under -fdeclspec (also enabled with
86 // -fms-extensions), but we define a no-op __declspec macro anyway for
87 // pre-processor compatibility.
88 if (Opts.DeclSpecKeyword)
89 Builder.defineMacro("__declspec", "__declspec");
90 else
91 Builder.defineMacro("__declspec(a)", "__attribute__((a))");
92
93 if (!Opts.MicrosoftExt) {
94 // Provide macros for all the calling convention keywords. Provide both
95 // single and double underscore prefixed variants. These are available on
96 // x64 as well as x86, even though they have no effect.
97 const char *CCs[] = {"cdecl", "stdcall", "fastcall", "thiscall", "pascal"};
98 for (const char *CC : CCs) {
99 std::string GCCSpelling = "__attribute__((__";
100 GCCSpelling += CC;
101 GCCSpelling += "__))";
102 Builder.defineMacro(Twine("_") + CC, GCCSpelling);
103 Builder.defineMacro(Twine("__") + CC, GCCSpelling);
104 }
105 }
106 }
107
108 //===----------------------------------------------------------------------===//
109 // Driver code
110 //===----------------------------------------------------------------------===//
111
AllocateTarget(const llvm::Triple & Triple,const TargetOptions & Opts)112 std::unique_ptr<TargetInfo> AllocateTarget(const llvm::Triple &Triple,
113 const TargetOptions &Opts) {
114 llvm::Triple::OSType os = Triple.getOS();
115
116 switch (Triple.getArch()) {
117 default:
118 return nullptr;
119
120 case llvm::Triple::arc:
121 return std::make_unique<ARCTargetInfo>(Triple, Opts);
122
123 case llvm::Triple::xcore:
124 return std::make_unique<XCoreTargetInfo>(Triple, Opts);
125
126 case llvm::Triple::hexagon:
127 if (os == llvm::Triple::Linux &&
128 Triple.getEnvironment() == llvm::Triple::Musl)
129 return std::make_unique<LinuxTargetInfo<HexagonTargetInfo>>(Triple, Opts);
130 return std::make_unique<HexagonTargetInfo>(Triple, Opts);
131
132 case llvm::Triple::lanai:
133 return std::make_unique<LanaiTargetInfo>(Triple, Opts);
134
135 case llvm::Triple::aarch64_32:
136 if (Triple.isOSDarwin())
137 return std::make_unique<DarwinAArch64TargetInfo>(Triple, Opts);
138
139 return nullptr;
140 case llvm::Triple::aarch64:
141 if (Triple.isOSDarwin())
142 return std::make_unique<DarwinAArch64TargetInfo>(Triple, Opts);
143
144 switch (os) {
145 case llvm::Triple::FreeBSD:
146 return std::make_unique<FreeBSDTargetInfo<AArch64leTargetInfo>>(Triple,
147 Opts);
148 case llvm::Triple::Fuchsia:
149 return std::make_unique<FuchsiaTargetInfo<AArch64leTargetInfo>>(Triple,
150 Opts);
151 case llvm::Triple::Haiku:
152 return std::make_unique<HaikuTargetInfo<AArch64leTargetInfo>>(Triple,
153 Opts);
154 case llvm::Triple::Linux:
155 switch (Triple.getEnvironment()) {
156 default:
157 return std::make_unique<LinuxTargetInfo<AArch64leTargetInfo>>(Triple,
158 Opts);
159 case llvm::Triple::OpenHOS:
160 return std::make_unique<OHOSTargetInfo<AArch64leTargetInfo>>(Triple,
161 Opts);
162 }
163 case llvm::Triple::NetBSD:
164 return std::make_unique<NetBSDTargetInfo<AArch64leTargetInfo>>(Triple,
165 Opts);
166 case llvm::Triple::OpenBSD:
167 return std::make_unique<OpenBSDTargetInfo<AArch64leTargetInfo>>(Triple,
168 Opts);
169 case llvm::Triple::Win32:
170 switch (Triple.getEnvironment()) {
171 case llvm::Triple::GNU:
172 return std::make_unique<MinGWARM64TargetInfo>(Triple, Opts);
173 case llvm::Triple::MSVC:
174 default: // Assume MSVC for unknown environments
175 return std::make_unique<MicrosoftARM64TargetInfo>(Triple, Opts);
176 }
177 default:
178 return std::make_unique<AArch64leTargetInfo>(Triple, Opts);
179 }
180
181 case llvm::Triple::aarch64_be:
182 switch (os) {
183 case llvm::Triple::FreeBSD:
184 return std::make_unique<FreeBSDTargetInfo<AArch64beTargetInfo>>(Triple,
185 Opts);
186 case llvm::Triple::Fuchsia:
187 return std::make_unique<FuchsiaTargetInfo<AArch64beTargetInfo>>(Triple,
188 Opts);
189 case llvm::Triple::Linux:
190 return std::make_unique<LinuxTargetInfo<AArch64beTargetInfo>>(Triple,
191 Opts);
192 case llvm::Triple::NetBSD:
193 return std::make_unique<NetBSDTargetInfo<AArch64beTargetInfo>>(Triple,
194 Opts);
195 default:
196 return std::make_unique<AArch64beTargetInfo>(Triple, Opts);
197 }
198
199 case llvm::Triple::arm:
200 case llvm::Triple::thumb:
201 if (Triple.isOSBinFormatMachO())
202 return std::make_unique<DarwinARMTargetInfo>(Triple, Opts);
203
204 switch (os) {
205 case llvm::Triple::Linux:
206 switch (Triple.getEnvironment()) {
207 default:
208 return std::make_unique<LinuxTargetInfo<ARMleTargetInfo>>(Triple, Opts);
209 case llvm::Triple::OpenHOS:
210 return std::make_unique<OHOSTargetInfo<ARMleTargetInfo>>(Triple, Opts);
211 }
212 case llvm::Triple::LiteOS:
213 return std::make_unique<OHOSTargetInfo<ARMleTargetInfo>>(Triple, Opts);
214 case llvm::Triple::FreeBSD:
215 return std::make_unique<FreeBSDTargetInfo<ARMleTargetInfo>>(Triple, Opts);
216 case llvm::Triple::NetBSD:
217 return std::make_unique<NetBSDTargetInfo<ARMleTargetInfo>>(Triple, Opts);
218 case llvm::Triple::OpenBSD:
219 return std::make_unique<OpenBSDTargetInfo<ARMleTargetInfo>>(Triple, Opts);
220 case llvm::Triple::RTEMS:
221 return std::make_unique<RTEMSTargetInfo<ARMleTargetInfo>>(Triple, Opts);
222 case llvm::Triple::Haiku:
223 return std::make_unique<HaikuTargetInfo<ARMleTargetInfo>>(Triple, Opts);
224 case llvm::Triple::NaCl:
225 return std::make_unique<NaClTargetInfo<ARMleTargetInfo>>(Triple, Opts);
226 case llvm::Triple::Win32:
227 switch (Triple.getEnvironment()) {
228 case llvm::Triple::Cygnus:
229 return std::make_unique<CygwinARMTargetInfo>(Triple, Opts);
230 case llvm::Triple::GNU:
231 return std::make_unique<MinGWARMTargetInfo>(Triple, Opts);
232 case llvm::Triple::Itanium:
233 return std::make_unique<ItaniumWindowsARMleTargetInfo>(Triple, Opts);
234 case llvm::Triple::MSVC:
235 default: // Assume MSVC for unknown environments
236 return std::make_unique<MicrosoftARMleTargetInfo>(Triple, Opts);
237 }
238 default:
239 return std::make_unique<ARMleTargetInfo>(Triple, Opts);
240 }
241
242 case llvm::Triple::armeb:
243 case llvm::Triple::thumbeb:
244 if (Triple.isOSDarwin())
245 return std::make_unique<DarwinARMTargetInfo>(Triple, Opts);
246
247 switch (os) {
248 case llvm::Triple::Linux:
249 return std::make_unique<LinuxTargetInfo<ARMbeTargetInfo>>(Triple, Opts);
250 case llvm::Triple::NetBSD:
251 return std::make_unique<NetBSDTargetInfo<ARMbeTargetInfo>>(Triple, Opts);
252 case llvm::Triple::RTEMS:
253 return std::make_unique<RTEMSTargetInfo<ARMbeTargetInfo>>(Triple, Opts);
254 case llvm::Triple::NaCl:
255 return std::make_unique<NaClTargetInfo<ARMbeTargetInfo>>(Triple, Opts);
256 default:
257 return std::make_unique<ARMbeTargetInfo>(Triple, Opts);
258 }
259
260 case llvm::Triple::avr:
261 return std::make_unique<AVRTargetInfo>(Triple, Opts);
262 case llvm::Triple::bpfeb:
263 case llvm::Triple::bpfel:
264 return std::make_unique<BPFTargetInfo>(Triple, Opts);
265
266 case llvm::Triple::msp430:
267 return std::make_unique<MSP430TargetInfo>(Triple, Opts);
268
269 case llvm::Triple::mips:
270 switch (os) {
271 case llvm::Triple::Linux:
272 return std::make_unique<LinuxTargetInfo<MipsTargetInfo>>(Triple, Opts);
273 case llvm::Triple::RTEMS:
274 return std::make_unique<RTEMSTargetInfo<MipsTargetInfo>>(Triple, Opts);
275 case llvm::Triple::FreeBSD:
276 return std::make_unique<FreeBSDTargetInfo<MipsTargetInfo>>(Triple, Opts);
277 case llvm::Triple::NetBSD:
278 return std::make_unique<NetBSDTargetInfo<MipsTargetInfo>>(Triple, Opts);
279 default:
280 return std::make_unique<MipsTargetInfo>(Triple, Opts);
281 }
282
283 case llvm::Triple::mipsel:
284 switch (os) {
285 case llvm::Triple::Linux:
286 switch (Triple.getEnvironment()) {
287 default:
288 return std::make_unique<LinuxTargetInfo<MipsTargetInfo>>(Triple, Opts);
289 case llvm::Triple::OpenHOS:
290 return std::make_unique<OHOSTargetInfo<MipsTargetInfo>>(Triple, Opts);
291 }
292 case llvm::Triple::RTEMS:
293 return std::make_unique<RTEMSTargetInfo<MipsTargetInfo>>(Triple, Opts);
294 case llvm::Triple::FreeBSD:
295 return std::make_unique<FreeBSDTargetInfo<MipsTargetInfo>>(Triple, Opts);
296 case llvm::Triple::NetBSD:
297 return std::make_unique<NetBSDTargetInfo<MipsTargetInfo>>(Triple, Opts);
298 case llvm::Triple::NaCl:
299 return std::make_unique<NaClTargetInfo<NaClMips32TargetInfo>>(Triple,
300 Opts);
301 default:
302 return std::make_unique<MipsTargetInfo>(Triple, Opts);
303 }
304
305 case llvm::Triple::mips64:
306 switch (os) {
307 case llvm::Triple::Linux:
308 return std::make_unique<LinuxTargetInfo<MipsTargetInfo>>(Triple, Opts);
309 case llvm::Triple::RTEMS:
310 return std::make_unique<RTEMSTargetInfo<MipsTargetInfo>>(Triple, Opts);
311 case llvm::Triple::FreeBSD:
312 return std::make_unique<FreeBSDTargetInfo<MipsTargetInfo>>(Triple, Opts);
313 case llvm::Triple::NetBSD:
314 return std::make_unique<NetBSDTargetInfo<MipsTargetInfo>>(Triple, Opts);
315 case llvm::Triple::OpenBSD:
316 return std::make_unique<OpenBSDTargetInfo<MipsTargetInfo>>(Triple, Opts);
317 default:
318 return std::make_unique<MipsTargetInfo>(Triple, Opts);
319 }
320
321 case llvm::Triple::mips64el:
322 switch (os) {
323 case llvm::Triple::Linux:
324 return std::make_unique<LinuxTargetInfo<MipsTargetInfo>>(Triple, Opts);
325 case llvm::Triple::RTEMS:
326 return std::make_unique<RTEMSTargetInfo<MipsTargetInfo>>(Triple, Opts);
327 case llvm::Triple::FreeBSD:
328 return std::make_unique<FreeBSDTargetInfo<MipsTargetInfo>>(Triple, Opts);
329 case llvm::Triple::NetBSD:
330 return std::make_unique<NetBSDTargetInfo<MipsTargetInfo>>(Triple, Opts);
331 case llvm::Triple::OpenBSD:
332 return std::make_unique<OpenBSDTargetInfo<MipsTargetInfo>>(Triple, Opts);
333 default:
334 return std::make_unique<MipsTargetInfo>(Triple, Opts);
335 }
336
337 case llvm::Triple::m68k:
338 switch (os) {
339 case llvm::Triple::Linux:
340 return std::make_unique<LinuxTargetInfo<M68kTargetInfo>>(Triple, Opts);
341 case llvm::Triple::NetBSD:
342 return std::make_unique<NetBSDTargetInfo<M68kTargetInfo>>(Triple, Opts);
343 default:
344 return std::make_unique<M68kTargetInfo>(Triple, Opts);
345 }
346
347 case llvm::Triple::le32:
348 switch (os) {
349 case llvm::Triple::NaCl:
350 return std::make_unique<NaClTargetInfo<PNaClTargetInfo>>(Triple, Opts);
351 default:
352 return nullptr;
353 }
354
355 case llvm::Triple::le64:
356 return std::make_unique<Le64TargetInfo>(Triple, Opts);
357
358 case llvm::Triple::ppc:
359 switch (os) {
360 case llvm::Triple::Linux:
361 return std::make_unique<LinuxTargetInfo<PPC32TargetInfo>>(Triple, Opts);
362 case llvm::Triple::FreeBSD:
363 return std::make_unique<FreeBSDTargetInfo<PPC32TargetInfo>>(Triple, Opts);
364 case llvm::Triple::NetBSD:
365 return std::make_unique<NetBSDTargetInfo<PPC32TargetInfo>>(Triple, Opts);
366 case llvm::Triple::OpenBSD:
367 return std::make_unique<OpenBSDTargetInfo<PPC32TargetInfo>>(Triple, Opts);
368 case llvm::Triple::RTEMS:
369 return std::make_unique<RTEMSTargetInfo<PPC32TargetInfo>>(Triple, Opts);
370 case llvm::Triple::AIX:
371 return std::make_unique<AIXPPC32TargetInfo>(Triple, Opts);
372 default:
373 return std::make_unique<PPC32TargetInfo>(Triple, Opts);
374 }
375
376 case llvm::Triple::ppcle:
377 switch (os) {
378 case llvm::Triple::Linux:
379 return std::make_unique<LinuxTargetInfo<PPC32TargetInfo>>(Triple, Opts);
380 case llvm::Triple::FreeBSD:
381 return std::make_unique<FreeBSDTargetInfo<PPC32TargetInfo>>(Triple, Opts);
382 default:
383 return std::make_unique<PPC32TargetInfo>(Triple, Opts);
384 }
385
386 case llvm::Triple::ppc64:
387 switch (os) {
388 case llvm::Triple::Linux:
389 return std::make_unique<LinuxTargetInfo<PPC64TargetInfo>>(Triple, Opts);
390 case llvm::Triple::Lv2:
391 return std::make_unique<PS3PPUTargetInfo<PPC64TargetInfo>>(Triple, Opts);
392 case llvm::Triple::FreeBSD:
393 return std::make_unique<FreeBSDTargetInfo<PPC64TargetInfo>>(Triple, Opts);
394 case llvm::Triple::NetBSD:
395 return std::make_unique<NetBSDTargetInfo<PPC64TargetInfo>>(Triple, Opts);
396 case llvm::Triple::OpenBSD:
397 return std::make_unique<OpenBSDTargetInfo<PPC64TargetInfo>>(Triple, Opts);
398 case llvm::Triple::AIX:
399 return std::make_unique<AIXPPC64TargetInfo>(Triple, Opts);
400 default:
401 return std::make_unique<PPC64TargetInfo>(Triple, Opts);
402 }
403
404 case llvm::Triple::ppc64le:
405 switch (os) {
406 case llvm::Triple::Linux:
407 return std::make_unique<LinuxTargetInfo<PPC64TargetInfo>>(Triple, Opts);
408 case llvm::Triple::FreeBSD:
409 return std::make_unique<FreeBSDTargetInfo<PPC64TargetInfo>>(Triple, Opts);
410 case llvm::Triple::NetBSD:
411 return std::make_unique<NetBSDTargetInfo<PPC64TargetInfo>>(Triple, Opts);
412 case llvm::Triple::OpenBSD:
413 return std::make_unique<OpenBSDTargetInfo<PPC64TargetInfo>>(Triple, Opts);
414 default:
415 return std::make_unique<PPC64TargetInfo>(Triple, Opts);
416 }
417
418 case llvm::Triple::nvptx:
419 return std::make_unique<NVPTXTargetInfo>(Triple, Opts,
420 /*TargetPointerWidth=*/32);
421 case llvm::Triple::nvptx64:
422 return std::make_unique<NVPTXTargetInfo>(Triple, Opts,
423 /*TargetPointerWidth=*/64);
424
425 case llvm::Triple::amdgcn:
426 case llvm::Triple::r600:
427 return std::make_unique<AMDGPUTargetInfo>(Triple, Opts);
428
429 case llvm::Triple::riscv32:
430 switch (os) {
431 case llvm::Triple::NetBSD:
432 return std::make_unique<NetBSDTargetInfo<RISCV32TargetInfo>>(Triple,
433 Opts);
434 case llvm::Triple::Linux:
435 return std::make_unique<LinuxTargetInfo<RISCV32TargetInfo>>(Triple, Opts);
436 default:
437 return std::make_unique<RISCV32TargetInfo>(Triple, Opts);
438 }
439
440 case llvm::Triple::riscv64:
441 switch (os) {
442 case llvm::Triple::FreeBSD:
443 return std::make_unique<FreeBSDTargetInfo<RISCV64TargetInfo>>(Triple,
444 Opts);
445 case llvm::Triple::NetBSD:
446 return std::make_unique<NetBSDTargetInfo<RISCV64TargetInfo>>(Triple,
447 Opts);
448 case llvm::Triple::OpenBSD:
449 return std::make_unique<OpenBSDTargetInfo<RISCV64TargetInfo>>(Triple,
450 Opts);
451 case llvm::Triple::Fuchsia:
452 return std::make_unique<FuchsiaTargetInfo<RISCV64TargetInfo>>(Triple,
453 Opts);
454 case llvm::Triple::Haiku:
455 return std::make_unique<HaikuTargetInfo<RISCV64TargetInfo>>(Triple,
456 Opts);
457 case llvm::Triple::Linux:
458 switch (Triple.getEnvironment()) {
459 default:
460 return std::make_unique<LinuxTargetInfo<RISCV64TargetInfo>>(Triple,
461 Opts);
462 case llvm::Triple::OpenHOS:
463 return std::make_unique<OHOSTargetInfo<RISCV64TargetInfo>>(Triple,
464 Opts);
465 }
466 default:
467 return std::make_unique<RISCV64TargetInfo>(Triple, Opts);
468 }
469
470 case llvm::Triple::sparc:
471 switch (os) {
472 case llvm::Triple::Linux:
473 return std::make_unique<LinuxTargetInfo<SparcV8TargetInfo>>(Triple, Opts);
474 case llvm::Triple::Solaris:
475 return std::make_unique<SolarisTargetInfo<SparcV8TargetInfo>>(Triple,
476 Opts);
477 case llvm::Triple::NetBSD:
478 return std::make_unique<NetBSDTargetInfo<SparcV8TargetInfo>>(Triple,
479 Opts);
480 case llvm::Triple::RTEMS:
481 return std::make_unique<RTEMSTargetInfo<SparcV8TargetInfo>>(Triple, Opts);
482 default:
483 return std::make_unique<SparcV8TargetInfo>(Triple, Opts);
484 }
485
486 case llvm::Triple::sparcel:
487 switch (os) {
488 case llvm::Triple::Linux:
489 return std::make_unique<LinuxTargetInfo<SparcV8elTargetInfo>>(Triple,
490 Opts);
491 case llvm::Triple::RTEMS:
492 return std::make_unique<RTEMSTargetInfo<SparcV8elTargetInfo>>(Triple,
493 Opts);
494 default:
495 return std::make_unique<SparcV8elTargetInfo>(Triple, Opts);
496 }
497
498 case llvm::Triple::sparcv9:
499 switch (os) {
500 case llvm::Triple::Linux:
501 return std::make_unique<LinuxTargetInfo<SparcV9TargetInfo>>(Triple, Opts);
502 case llvm::Triple::Solaris:
503 return std::make_unique<SolarisTargetInfo<SparcV9TargetInfo>>(Triple,
504 Opts);
505 case llvm::Triple::NetBSD:
506 return std::make_unique<NetBSDTargetInfo<SparcV9TargetInfo>>(Triple,
507 Opts);
508 case llvm::Triple::OpenBSD:
509 return std::make_unique<OpenBSDTargetInfo<SparcV9TargetInfo>>(Triple,
510 Opts);
511 case llvm::Triple::FreeBSD:
512 return std::make_unique<FreeBSDTargetInfo<SparcV9TargetInfo>>(Triple,
513 Opts);
514 default:
515 return std::make_unique<SparcV9TargetInfo>(Triple, Opts);
516 }
517
518 case llvm::Triple::systemz:
519 switch (os) {
520 case llvm::Triple::Linux:
521 return std::make_unique<LinuxTargetInfo<SystemZTargetInfo>>(Triple, Opts);
522 case llvm::Triple::ZOS:
523 return std::make_unique<ZOSTargetInfo<SystemZTargetInfo>>(Triple, Opts);
524 default:
525 return std::make_unique<SystemZTargetInfo>(Triple, Opts);
526 }
527
528 case llvm::Triple::tce:
529 return std::make_unique<TCETargetInfo>(Triple, Opts);
530
531 case llvm::Triple::tcele:
532 return std::make_unique<TCELETargetInfo>(Triple, Opts);
533
534 case llvm::Triple::x86:
535 if (Triple.isOSDarwin())
536 return std::make_unique<DarwinI386TargetInfo>(Triple, Opts);
537
538 switch (os) {
539 case llvm::Triple::Linux: {
540 switch (Triple.getEnvironment()) {
541 default:
542 return std::make_unique<LinuxTargetInfo<X86_32TargetInfo>>(Triple,
543 Opts);
544 case llvm::Triple::Android:
545 return std::make_unique<AndroidX86_32TargetInfo>(Triple, Opts);
546 }
547 }
548 case llvm::Triple::DragonFly:
549 return std::make_unique<DragonFlyBSDTargetInfo<X86_32TargetInfo>>(Triple,
550 Opts);
551 case llvm::Triple::NetBSD:
552 return std::make_unique<NetBSDI386TargetInfo>(Triple, Opts);
553 case llvm::Triple::OpenBSD:
554 return std::make_unique<OpenBSDI386TargetInfo>(Triple, Opts);
555 case llvm::Triple::FreeBSD:
556 return std::make_unique<FreeBSDTargetInfo<X86_32TargetInfo>>(Triple,
557 Opts);
558 case llvm::Triple::Fuchsia:
559 return std::make_unique<FuchsiaTargetInfo<X86_32TargetInfo>>(Triple,
560 Opts);
561 case llvm::Triple::KFreeBSD:
562 return std::make_unique<KFreeBSDTargetInfo<X86_32TargetInfo>>(Triple,
563 Opts);
564 case llvm::Triple::Solaris:
565 return std::make_unique<SolarisTargetInfo<X86_32TargetInfo>>(Triple,
566 Opts);
567 case llvm::Triple::Win32: {
568 switch (Triple.getEnvironment()) {
569 case llvm::Triple::Cygnus:
570 return std::make_unique<CygwinX86_32TargetInfo>(Triple, Opts);
571 case llvm::Triple::GNU:
572 return std::make_unique<MinGWX86_32TargetInfo>(Triple, Opts);
573 case llvm::Triple::Itanium:
574 case llvm::Triple::MSVC:
575 default: // Assume MSVC for unknown environments
576 return std::make_unique<MicrosoftX86_32TargetInfo>(Triple, Opts);
577 }
578 }
579 case llvm::Triple::Haiku:
580 return std::make_unique<HaikuX86_32TargetInfo>(Triple, Opts);
581 case llvm::Triple::RTEMS:
582 return std::make_unique<RTEMSX86_32TargetInfo>(Triple, Opts);
583 case llvm::Triple::NaCl:
584 return std::make_unique<NaClTargetInfo<X86_32TargetInfo>>(Triple, Opts);
585 case llvm::Triple::ELFIAMCU:
586 return std::make_unique<MCUX86_32TargetInfo>(Triple, Opts);
587 case llvm::Triple::Hurd:
588 return std::make_unique<HurdTargetInfo<X86_32TargetInfo>>(Triple, Opts);
589 default:
590 return std::make_unique<X86_32TargetInfo>(Triple, Opts);
591 }
592
593 case llvm::Triple::x86_64:
594 if (Triple.isOSDarwin() || Triple.isOSBinFormatMachO())
595 return std::make_unique<DarwinX86_64TargetInfo>(Triple, Opts);
596
597 switch (os) {
598 case llvm::Triple::Linux: {
599 switch (Triple.getEnvironment()) {
600 default:
601 return std::make_unique<LinuxTargetInfo<X86_64TargetInfo>>(Triple,
602 Opts);
603 case llvm::Triple::Android:
604 return std::make_unique<AndroidX86_64TargetInfo>(Triple, Opts);
605 case llvm::Triple::OpenHOS:
606 return std::make_unique<OHOSX86_64TargetInfo>(Triple, Opts);
607 }
608 }
609 case llvm::Triple::DragonFly:
610 return std::make_unique<DragonFlyBSDTargetInfo<X86_64TargetInfo>>(Triple,
611 Opts);
612 case llvm::Triple::NetBSD:
613 return std::make_unique<NetBSDTargetInfo<X86_64TargetInfo>>(Triple, Opts);
614 case llvm::Triple::OpenBSD:
615 return std::make_unique<OpenBSDX86_64TargetInfo>(Triple, Opts);
616 case llvm::Triple::FreeBSD:
617 return std::make_unique<FreeBSDTargetInfo<X86_64TargetInfo>>(Triple,
618 Opts);
619 case llvm::Triple::Fuchsia:
620 return std::make_unique<FuchsiaTargetInfo<X86_64TargetInfo>>(Triple,
621 Opts);
622 case llvm::Triple::KFreeBSD:
623 return std::make_unique<KFreeBSDTargetInfo<X86_64TargetInfo>>(Triple,
624 Opts);
625 case llvm::Triple::Solaris:
626 return std::make_unique<SolarisTargetInfo<X86_64TargetInfo>>(Triple,
627 Opts);
628 case llvm::Triple::Win32: {
629 switch (Triple.getEnvironment()) {
630 case llvm::Triple::Cygnus:
631 return std::make_unique<CygwinX86_64TargetInfo>(Triple, Opts);
632 case llvm::Triple::GNU:
633 return std::make_unique<MinGWX86_64TargetInfo>(Triple, Opts);
634 case llvm::Triple::MSVC:
635 default: // Assume MSVC for unknown environments
636 return std::make_unique<MicrosoftX86_64TargetInfo>(Triple, Opts);
637 }
638 }
639 case llvm::Triple::Haiku:
640 return std::make_unique<HaikuTargetInfo<X86_64TargetInfo>>(Triple, Opts);
641 case llvm::Triple::NaCl:
642 return std::make_unique<NaClTargetInfo<X86_64TargetInfo>>(Triple, Opts);
643 case llvm::Triple::PS4:
644 return std::make_unique<PS4OSTargetInfo<X86_64TargetInfo>>(Triple, Opts);
645 case llvm::Triple::PS5:
646 return std::make_unique<PS5OSTargetInfo<X86_64TargetInfo>>(Triple, Opts);
647 case llvm::Triple::Hurd:
648 return std::make_unique<HurdTargetInfo<X86_64TargetInfo>>(Triple, Opts);
649 default:
650 return std::make_unique<X86_64TargetInfo>(Triple, Opts);
651 }
652
653 case llvm::Triple::spir: {
654 if (os != llvm::Triple::UnknownOS ||
655 Triple.getEnvironment() != llvm::Triple::UnknownEnvironment)
656 return nullptr;
657 return std::make_unique<SPIR32TargetInfo>(Triple, Opts);
658 }
659 case llvm::Triple::spir64: {
660 if (os != llvm::Triple::UnknownOS ||
661 Triple.getEnvironment() != llvm::Triple::UnknownEnvironment)
662 return nullptr;
663 return std::make_unique<SPIR64TargetInfo>(Triple, Opts);
664 }
665 case llvm::Triple::spirv: {
666 return std::make_unique<SPIRVTargetInfo>(Triple, Opts);
667 }
668 case llvm::Triple::spirv32: {
669 if (os != llvm::Triple::UnknownOS ||
670 Triple.getEnvironment() != llvm::Triple::UnknownEnvironment)
671 return nullptr;
672 return std::make_unique<SPIRV32TargetInfo>(Triple, Opts);
673 }
674 case llvm::Triple::spirv64: {
675 if (os != llvm::Triple::UnknownOS ||
676 Triple.getEnvironment() != llvm::Triple::UnknownEnvironment) {
677 if (os == llvm::Triple::OSType::AMDHSA)
678 return std::make_unique<SPIRV64AMDGCNTargetInfo>(Triple, Opts);
679 return nullptr;
680 }
681 return std::make_unique<SPIRV64TargetInfo>(Triple, Opts);
682 }
683 case llvm::Triple::wasm32:
684 if (Triple.getSubArch() != llvm::Triple::NoSubArch ||
685 Triple.getVendor() != llvm::Triple::UnknownVendor ||
686 !Triple.isOSBinFormatWasm())
687 return nullptr;
688 switch (os) {
689 case llvm::Triple::WASI:
690 return std::make_unique<WASITargetInfo<WebAssembly32TargetInfo>>(Triple,
691 Opts);
692 case llvm::Triple::Emscripten:
693 return std::make_unique<EmscriptenTargetInfo<WebAssembly32TargetInfo>>(
694 Triple, Opts);
695 case llvm::Triple::UnknownOS:
696 return std::make_unique<WebAssemblyOSTargetInfo<WebAssembly32TargetInfo>>(
697 Triple, Opts);
698 default:
699 return nullptr;
700 }
701 case llvm::Triple::wasm64:
702 if (Triple.getSubArch() != llvm::Triple::NoSubArch ||
703 Triple.getVendor() != llvm::Triple::UnknownVendor ||
704 !Triple.isOSBinFormatWasm())
705 return nullptr;
706 switch (os) {
707 case llvm::Triple::WASI:
708 return std::make_unique<WASITargetInfo<WebAssembly64TargetInfo>>(Triple,
709 Opts);
710 case llvm::Triple::Emscripten:
711 return std::make_unique<EmscriptenTargetInfo<WebAssembly64TargetInfo>>(
712 Triple, Opts);
713 case llvm::Triple::UnknownOS:
714 return std::make_unique<WebAssemblyOSTargetInfo<WebAssembly64TargetInfo>>(
715 Triple, Opts);
716 default:
717 return nullptr;
718 }
719
720 case llvm::Triple::dxil:
721 return std::make_unique<DirectXTargetInfo>(Triple, Opts);
722 case llvm::Triple::renderscript32:
723 return std::make_unique<LinuxTargetInfo<RenderScript32TargetInfo>>(Triple,
724 Opts);
725 case llvm::Triple::renderscript64:
726 return std::make_unique<LinuxTargetInfo<RenderScript64TargetInfo>>(Triple,
727 Opts);
728
729 case llvm::Triple::ve:
730 return std::make_unique<LinuxTargetInfo<VETargetInfo>>(Triple, Opts);
731
732 case llvm::Triple::csky:
733 switch (os) {
734 case llvm::Triple::Linux:
735 return std::make_unique<LinuxTargetInfo<CSKYTargetInfo>>(Triple, Opts);
736 default:
737 return std::make_unique<CSKYTargetInfo>(Triple, Opts);
738 }
739 case llvm::Triple::loongarch32:
740 switch (os) {
741 case llvm::Triple::Linux:
742 return std::make_unique<LinuxTargetInfo<LoongArch32TargetInfo>>(Triple,
743 Opts);
744 default:
745 return std::make_unique<LoongArch32TargetInfo>(Triple, Opts);
746 }
747 case llvm::Triple::loongarch64:
748 switch (os) {
749 case llvm::Triple::Linux:
750 return std::make_unique<LinuxTargetInfo<LoongArch64TargetInfo>>(Triple,
751 Opts);
752 default:
753 return std::make_unique<LoongArch64TargetInfo>(Triple, Opts);
754 }
755 }
756 }
757 } // namespace targets
758 } // namespace clang
759
760 using namespace clang::targets;
761 /// CreateTargetInfo - Return the target info object for the specified target
762 /// options.
763 TargetInfo *
CreateTargetInfo(DiagnosticsEngine & Diags,const std::shared_ptr<TargetOptions> & Opts)764 TargetInfo::CreateTargetInfo(DiagnosticsEngine &Diags,
765 const std::shared_ptr<TargetOptions> &Opts) {
766 llvm::Triple Triple(llvm::Triple::normalize(Opts->Triple));
767
768 // Construct the target
769 std::unique_ptr<TargetInfo> Target = AllocateTarget(Triple, *Opts);
770 if (!Target) {
771 Diags.Report(diag::err_target_unknown_triple) << Triple.str();
772 return nullptr;
773 }
774 Target->TargetOpts = Opts;
775
776 // Set the target CPU if specified.
777 if (!Opts->CPU.empty() && !Target->setCPU(Opts->CPU)) {
778 Diags.Report(diag::err_target_unknown_cpu) << Opts->CPU;
779 SmallVector<StringRef, 32> ValidList;
780 Target->fillValidCPUList(ValidList);
781 if (!ValidList.empty())
782 Diags.Report(diag::note_valid_options) << llvm::join(ValidList, ", ");
783 return nullptr;
784 }
785
786 // Check the TuneCPU name if specified.
787 if (!Opts->TuneCPU.empty() &&
788 !Target->isValidTuneCPUName(Opts->TuneCPU)) {
789 Diags.Report(diag::err_target_unknown_cpu) << Opts->TuneCPU;
790 SmallVector<StringRef, 32> ValidList;
791 Target->fillValidTuneCPUList(ValidList);
792 if (!ValidList.empty())
793 Diags.Report(diag::note_valid_options) << llvm::join(ValidList, ", ");
794 return nullptr;
795 }
796
797 // Set the target ABI if specified.
798 if (!Opts->ABI.empty() && !Target->setABI(Opts->ABI)) {
799 Diags.Report(diag::err_target_unknown_abi) << Opts->ABI;
800 return nullptr;
801 }
802
803 // Set the fp math unit.
804 if (!Opts->FPMath.empty() && !Target->setFPMath(Opts->FPMath)) {
805 Diags.Report(diag::err_target_unknown_fpmath) << Opts->FPMath;
806 return nullptr;
807 }
808
809 // Compute the default target features, we need the target to handle this
810 // because features may have dependencies on one another.
811 llvm::erase_if(Opts->FeaturesAsWritten, [&](StringRef Name) {
812 if (Target->isReadOnlyFeature(Name.substr(1))) {
813 Diags.Report(diag::warn_fe_backend_readonly_feature_flag) << Name;
814 return true;
815 }
816 return false;
817 });
818 if (!Target->initFeatureMap(Opts->FeatureMap, Diags, Opts->CPU,
819 Opts->FeaturesAsWritten))
820 return nullptr;
821
822 // Add the features to the compile options.
823 Opts->Features.clear();
824 for (const auto &F : Opts->FeatureMap)
825 Opts->Features.push_back((F.getValue() ? "+" : "-") + F.getKey().str());
826 // Sort here, so we handle the features in a predictable order. (This matters
827 // when we're dealing with features that overlap.)
828 llvm::sort(Opts->Features);
829
830 if (!Target->handleTargetFeatures(Opts->Features, Diags))
831 return nullptr;
832
833 Target->setSupportedOpenCLOpts();
834 Target->setCommandLineOpenCLOpts();
835 Target->setMaxAtomicWidth();
836
837 if (!Opts->DarwinTargetVariantTriple.empty())
838 Target->DarwinTargetVariantTriple =
839 llvm::Triple(Opts->DarwinTargetVariantTriple);
840
841 if (!Target->validateTarget(Diags))
842 return nullptr;
843
844 Target->CheckFixedPointBits();
845
846 return Target.release();
847 }
848 /// validateOpenCLTarget - Check that OpenCL target has valid
849 /// options setting based on OpenCL version.
validateOpenCLTarget(const LangOptions & Opts,DiagnosticsEngine & Diags) const850 bool TargetInfo::validateOpenCLTarget(const LangOptions &Opts,
851 DiagnosticsEngine &Diags) const {
852 const llvm::StringMap<bool> &OpenCLFeaturesMap = getSupportedOpenCLOpts();
853
854 auto diagnoseNotSupportedCore = [&](llvm::StringRef Name, auto... OptArgs) {
855 if (OpenCLOptions::isOpenCLOptionCoreIn(Opts, OptArgs...) &&
856 !hasFeatureEnabled(OpenCLFeaturesMap, Name))
857 Diags.Report(diag::warn_opencl_unsupported_core_feature)
858 << Name << Opts.OpenCLCPlusPlus
859 << Opts.getOpenCLVersionTuple().getAsString();
860 };
861 #define OPENCL_GENERIC_EXTENSION(Ext, ...) \
862 diagnoseNotSupportedCore(#Ext, __VA_ARGS__);
863 #include "clang/Basic/OpenCLExtensions.def"
864
865 // Validate that feature macros are set properly for OpenCL C 3.0.
866 // In other cases assume that target is always valid.
867 if (Opts.getOpenCLCompatibleVersion() < 300)
868 return true;
869
870 return OpenCLOptions::diagnoseUnsupportedFeatureDependencies(*this, Diags) &&
871 OpenCLOptions::diagnoseFeatureExtensionDifferences(*this, Diags);
872 }
873