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