clang 18.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/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"
46#include "llvm/ADT/StringExtras.h"
47#include "llvm/TargetParser/Triple.h"
48
49using namespace clang;
50
51namespace clang {
52namespace 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.
60void 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
76void 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
83void 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
112std::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::NaCl:
223 return std::make_unique<NaClTargetInfo<ARMleTargetInfo>>(Triple, Opts);
224 case llvm::Triple::Win32:
225 switch (Triple.getEnvironment()) {
226 case llvm::Triple::Cygnus:
227 return std::make_unique<CygwinARMTargetInfo>(Triple, Opts);
228 case llvm::Triple::GNU:
229 return std::make_unique<MinGWARMTargetInfo>(Triple, Opts);
230 case llvm::Triple::Itanium:
231 return std::make_unique<ItaniumWindowsARMleTargetInfo>(Triple, Opts);
232 case llvm::Triple::MSVC:
233 default: // Assume MSVC for unknown environments
234 return std::make_unique<MicrosoftARMleTargetInfo>(Triple, Opts);
235 }
236 default:
237 return std::make_unique<ARMleTargetInfo>(Triple, Opts);
238 }
239
240 case llvm::Triple::armeb:
241 case llvm::Triple::thumbeb:
242 if (Triple.isOSDarwin())
243 return std::make_unique<DarwinARMTargetInfo>(Triple, Opts);
244
245 switch (os) {
246 case llvm::Triple::Linux:
247 return std::make_unique<LinuxTargetInfo<ARMbeTargetInfo>>(Triple, Opts);
248 case llvm::Triple::FreeBSD:
249 return std::make_unique<FreeBSDTargetInfo<ARMbeTargetInfo>>(Triple, Opts);
250 case llvm::Triple::NetBSD:
251 return std::make_unique<NetBSDTargetInfo<ARMbeTargetInfo>>(Triple, Opts);
252 case llvm::Triple::OpenBSD:
253 return std::make_unique<OpenBSDTargetInfo<ARMbeTargetInfo>>(Triple, Opts);
254 case llvm::Triple::RTEMS:
255 return std::make_unique<RTEMSTargetInfo<ARMbeTargetInfo>>(Triple, Opts);
256 case llvm::Triple::NaCl:
257 return std::make_unique<NaClTargetInfo<ARMbeTargetInfo>>(Triple, Opts);
258 default:
259 return std::make_unique<ARMbeTargetInfo>(Triple, Opts);
260 }
261
262 case llvm::Triple::avr:
263 return std::make_unique<AVRTargetInfo>(Triple, Opts);
264 case llvm::Triple::bpfeb:
265 case llvm::Triple::bpfel:
266 return std::make_unique<BPFTargetInfo>(Triple, Opts);
267
268 case llvm::Triple::msp430:
269 return std::make_unique<MSP430TargetInfo>(Triple, Opts);
270
271 case llvm::Triple::mips:
272 switch (os) {
273 case llvm::Triple::Linux:
274 return std::make_unique<LinuxTargetInfo<MipsTargetInfo>>(Triple, Opts);
275 case llvm::Triple::RTEMS:
276 return std::make_unique<RTEMSTargetInfo<MipsTargetInfo>>(Triple, Opts);
277 case llvm::Triple::FreeBSD:
278 return std::make_unique<FreeBSDTargetInfo<MipsTargetInfo>>(Triple, Opts);
279 case llvm::Triple::NetBSD:
280 return std::make_unique<NetBSDTargetInfo<MipsTargetInfo>>(Triple, Opts);
281 default:
282 return std::make_unique<MipsTargetInfo>(Triple, Opts);
283 }
284
285 case llvm::Triple::mipsel:
286 switch (os) {
287 case llvm::Triple::Linux:
288 switch (Triple.getEnvironment()) {
289 default:
290 return std::make_unique<LinuxTargetInfo<MipsTargetInfo>>(Triple, Opts);
291 case llvm::Triple::OpenHOS:
292 return std::make_unique<OHOSTargetInfo<MipsTargetInfo>>(Triple, Opts);
293 }
294 case llvm::Triple::RTEMS:
295 return std::make_unique<RTEMSTargetInfo<MipsTargetInfo>>(Triple, Opts);
296 case llvm::Triple::FreeBSD:
297 return std::make_unique<FreeBSDTargetInfo<MipsTargetInfo>>(Triple, Opts);
298 case llvm::Triple::NetBSD:
299 return std::make_unique<NetBSDTargetInfo<MipsTargetInfo>>(Triple, Opts);
300 case llvm::Triple::NaCl:
301 return std::make_unique<NaClTargetInfo<NaClMips32TargetInfo>>(Triple,
302 Opts);
303 default:
304 return std::make_unique<MipsTargetInfo>(Triple, Opts);
305 }
306
307 case llvm::Triple::mips64:
308 switch (os) {
309 case llvm::Triple::Linux:
310 return std::make_unique<LinuxTargetInfo<MipsTargetInfo>>(Triple, Opts);
311 case llvm::Triple::RTEMS:
312 return std::make_unique<RTEMSTargetInfo<MipsTargetInfo>>(Triple, Opts);
313 case llvm::Triple::FreeBSD:
314 return std::make_unique<FreeBSDTargetInfo<MipsTargetInfo>>(Triple, Opts);
315 case llvm::Triple::NetBSD:
316 return std::make_unique<NetBSDTargetInfo<MipsTargetInfo>>(Triple, Opts);
317 case llvm::Triple::OpenBSD:
318 return std::make_unique<OpenBSDTargetInfo<MipsTargetInfo>>(Triple, Opts);
319 default:
320 return std::make_unique<MipsTargetInfo>(Triple, Opts);
321 }
322
323 case llvm::Triple::mips64el:
324 switch (os) {
325 case llvm::Triple::Linux:
326 return std::make_unique<LinuxTargetInfo<MipsTargetInfo>>(Triple, Opts);
327 case llvm::Triple::RTEMS:
328 return std::make_unique<RTEMSTargetInfo<MipsTargetInfo>>(Triple, Opts);
329 case llvm::Triple::FreeBSD:
330 return std::make_unique<FreeBSDTargetInfo<MipsTargetInfo>>(Triple, Opts);
331 case llvm::Triple::NetBSD:
332 return std::make_unique<NetBSDTargetInfo<MipsTargetInfo>>(Triple, Opts);
333 case llvm::Triple::OpenBSD:
334 return std::make_unique<OpenBSDTargetInfo<MipsTargetInfo>>(Triple, Opts);
335 default:
336 return std::make_unique<MipsTargetInfo>(Triple, Opts);
337 }
338
339 case llvm::Triple::m68k:
340 switch (os) {
341 case llvm::Triple::Linux:
342 return std::make_unique<LinuxTargetInfo<M68kTargetInfo>>(Triple, Opts);
343 case llvm::Triple::NetBSD:
344 return std::make_unique<NetBSDTargetInfo<M68kTargetInfo>>(Triple, Opts);
345 default:
346 return std::make_unique<M68kTargetInfo>(Triple, Opts);
347 }
348
349 case llvm::Triple::le32:
350 switch (os) {
351 case llvm::Triple::NaCl:
352 return std::make_unique<NaClTargetInfo<PNaClTargetInfo>>(Triple, Opts);
353 default:
354 return nullptr;
355 }
356
357 case llvm::Triple::le64:
358 return std::make_unique<Le64TargetInfo>(Triple, Opts);
359
360 case llvm::Triple::ppc:
361 switch (os) {
362 case llvm::Triple::Linux:
363 return std::make_unique<LinuxTargetInfo<PPC32TargetInfo>>(Triple, Opts);
364 case llvm::Triple::FreeBSD:
365 return std::make_unique<FreeBSDTargetInfo<PPC32TargetInfo>>(Triple, Opts);
366 case llvm::Triple::NetBSD:
367 return std::make_unique<NetBSDTargetInfo<PPC32TargetInfo>>(Triple, Opts);
368 case llvm::Triple::OpenBSD:
369 return std::make_unique<OpenBSDTargetInfo<PPC32TargetInfo>>(Triple, Opts);
370 case llvm::Triple::RTEMS:
371 return std::make_unique<RTEMSTargetInfo<PPC32TargetInfo>>(Triple, Opts);
372 case llvm::Triple::AIX:
373 return std::make_unique<AIXPPC32TargetInfo>(Triple, Opts);
374 default:
375 return std::make_unique<PPC32TargetInfo>(Triple, Opts);
376 }
377
378 case llvm::Triple::ppcle:
379 switch (os) {
380 case llvm::Triple::Linux:
381 return std::make_unique<LinuxTargetInfo<PPC32TargetInfo>>(Triple, Opts);
382 case llvm::Triple::FreeBSD:
383 return std::make_unique<FreeBSDTargetInfo<PPC32TargetInfo>>(Triple, Opts);
384 default:
385 return std::make_unique<PPC32TargetInfo>(Triple, Opts);
386 }
387
388 case llvm::Triple::ppc64:
389 switch (os) {
390 case llvm::Triple::Linux:
391 return std::make_unique<LinuxTargetInfo<PPC64TargetInfo>>(Triple, Opts);
392 case llvm::Triple::Lv2:
393 return std::make_unique<PS3PPUTargetInfo<PPC64TargetInfo>>(Triple, Opts);
394 case llvm::Triple::FreeBSD:
395 return std::make_unique<FreeBSDTargetInfo<PPC64TargetInfo>>(Triple, Opts);
396 case llvm::Triple::NetBSD:
397 return std::make_unique<NetBSDTargetInfo<PPC64TargetInfo>>(Triple, Opts);
398 case llvm::Triple::OpenBSD:
399 return std::make_unique<OpenBSDTargetInfo<PPC64TargetInfo>>(Triple, Opts);
400 case llvm::Triple::AIX:
401 return std::make_unique<AIXPPC64TargetInfo>(Triple, Opts);
402 default:
403 return std::make_unique<PPC64TargetInfo>(Triple, Opts);
404 }
405
406 case llvm::Triple::ppc64le:
407 switch (os) {
408 case llvm::Triple::Linux:
409 return std::make_unique<LinuxTargetInfo<PPC64TargetInfo>>(Triple, Opts);
410 case llvm::Triple::FreeBSD:
411 return std::make_unique<FreeBSDTargetInfo<PPC64TargetInfo>>(Triple, Opts);
412 case llvm::Triple::NetBSD:
413 return std::make_unique<NetBSDTargetInfo<PPC64TargetInfo>>(Triple, Opts);
414 case llvm::Triple::OpenBSD:
415 return std::make_unique<OpenBSDTargetInfo<PPC64TargetInfo>>(Triple, Opts);
416 default:
417 return std::make_unique<PPC64TargetInfo>(Triple, Opts);
418 }
419
420 case llvm::Triple::nvptx:
421 return std::make_unique<NVPTXTargetInfo>(Triple, Opts,
422 /*TargetPointerWidth=*/32);
423 case llvm::Triple::nvptx64:
424 return std::make_unique<NVPTXTargetInfo>(Triple, Opts,
425 /*TargetPointerWidth=*/64);
426
427 case llvm::Triple::amdgcn:
428 case llvm::Triple::r600:
429 return std::make_unique<AMDGPUTargetInfo>(Triple, Opts);
430
431 case llvm::Triple::riscv32:
432 switch (os) {
433 case llvm::Triple::NetBSD:
434 return std::make_unique<NetBSDTargetInfo<RISCV32TargetInfo>>(Triple,
435 Opts);
436 case llvm::Triple::Linux:
437 return std::make_unique<LinuxTargetInfo<RISCV32TargetInfo>>(Triple, Opts);
438 default:
439 return std::make_unique<RISCV32TargetInfo>(Triple, Opts);
440 }
441
442 case llvm::Triple::riscv64:
443 switch (os) {
444 case llvm::Triple::FreeBSD:
445 return std::make_unique<FreeBSDTargetInfo<RISCV64TargetInfo>>(Triple,
446 Opts);
447 case llvm::Triple::NetBSD:
448 return std::make_unique<NetBSDTargetInfo<RISCV64TargetInfo>>(Triple,
449 Opts);
450 case llvm::Triple::OpenBSD:
451 return std::make_unique<OpenBSDTargetInfo<RISCV64TargetInfo>>(Triple,
452 Opts);
453 case llvm::Triple::Fuchsia:
454 return std::make_unique<FuchsiaTargetInfo<RISCV64TargetInfo>>(Triple,
455 Opts);
456 case llvm::Triple::Haiku:
457 return std::make_unique<HaikuTargetInfo<RISCV64TargetInfo>>(Triple,
458 Opts);
459 case llvm::Triple::Linux:
460 switch (Triple.getEnvironment()) {
461 default:
462 return std::make_unique<LinuxTargetInfo<RISCV64TargetInfo>>(Triple,
463 Opts);
464 case llvm::Triple::OpenHOS:
465 return std::make_unique<OHOSTargetInfo<RISCV64TargetInfo>>(Triple,
466 Opts);
467 }
468 default:
469 return std::make_unique<RISCV64TargetInfo>(Triple, Opts);
470 }
471
472 case llvm::Triple::sparc:
473 switch (os) {
474 case llvm::Triple::Linux:
475 return std::make_unique<LinuxTargetInfo<SparcV8TargetInfo>>(Triple, Opts);
476 case llvm::Triple::Solaris:
477 return std::make_unique<SolarisTargetInfo<SparcV8TargetInfo>>(Triple,
478 Opts);
479 case llvm::Triple::NetBSD:
480 return std::make_unique<NetBSDTargetInfo<SparcV8TargetInfo>>(Triple,
481 Opts);
482 case llvm::Triple::RTEMS:
483 return std::make_unique<RTEMSTargetInfo<SparcV8TargetInfo>>(Triple, Opts);
484 default:
485 return std::make_unique<SparcV8TargetInfo>(Triple, Opts);
486 }
487
488 // The 'sparcel' architecture copies all the above cases except for Solaris.
489 case llvm::Triple::sparcel:
490 switch (os) {
491 case llvm::Triple::Linux:
492 return std::make_unique<LinuxTargetInfo<SparcV8elTargetInfo>>(Triple,
493 Opts);
494 case llvm::Triple::NetBSD:
495 return std::make_unique<NetBSDTargetInfo<SparcV8elTargetInfo>>(Triple,
496 Opts);
497 case llvm::Triple::RTEMS:
498 return std::make_unique<RTEMSTargetInfo<SparcV8elTargetInfo>>(Triple,
499 Opts);
500 default:
501 return std::make_unique<SparcV8elTargetInfo>(Triple, Opts);
502 }
503
504 case llvm::Triple::sparcv9:
505 switch (os) {
506 case llvm::Triple::Linux:
507 return std::make_unique<LinuxTargetInfo<SparcV9TargetInfo>>(Triple, Opts);
508 case llvm::Triple::Solaris:
509 return std::make_unique<SolarisTargetInfo<SparcV9TargetInfo>>(Triple,
510 Opts);
511 case llvm::Triple::NetBSD:
512 return std::make_unique<NetBSDTargetInfo<SparcV9TargetInfo>>(Triple,
513 Opts);
514 case llvm::Triple::OpenBSD:
515 return std::make_unique<OpenBSDTargetInfo<SparcV9TargetInfo>>(Triple,
516 Opts);
517 case llvm::Triple::FreeBSD:
518 return std::make_unique<FreeBSDTargetInfo<SparcV9TargetInfo>>(Triple,
519 Opts);
520 default:
521 return std::make_unique<SparcV9TargetInfo>(Triple, Opts);
522 }
523
524 case llvm::Triple::systemz:
525 switch (os) {
526 case llvm::Triple::Linux:
527 return std::make_unique<LinuxTargetInfo<SystemZTargetInfo>>(Triple, Opts);
528 case llvm::Triple::ZOS:
529 return std::make_unique<ZOSTargetInfo<SystemZTargetInfo>>(Triple, Opts);
530 default:
531 return std::make_unique<SystemZTargetInfo>(Triple, Opts);
532 }
533
534 case llvm::Triple::tce:
535 return std::make_unique<TCETargetInfo>(Triple, Opts);
536
537 case llvm::Triple::tcele:
538 return std::make_unique<TCELETargetInfo>(Triple, Opts);
539
540 case llvm::Triple::x86:
541 if (Triple.isOSDarwin())
542 return std::make_unique<DarwinI386TargetInfo>(Triple, Opts);
543
544 switch (os) {
545 case llvm::Triple::Linux: {
546 switch (Triple.getEnvironment()) {
547 default:
548 return std::make_unique<LinuxTargetInfo<X86_32TargetInfo>>(Triple,
549 Opts);
550 case llvm::Triple::Android:
551 return std::make_unique<AndroidX86_32TargetInfo>(Triple, Opts);
552 }
553 }
554 case llvm::Triple::DragonFly:
555 return std::make_unique<DragonFlyBSDTargetInfo<X86_32TargetInfo>>(Triple,
556 Opts);
557 case llvm::Triple::NetBSD:
558 return std::make_unique<NetBSDI386TargetInfo>(Triple, Opts);
559 case llvm::Triple::OpenBSD:
560 return std::make_unique<OpenBSDI386TargetInfo>(Triple, Opts);
561 case llvm::Triple::FreeBSD:
562 return std::make_unique<FreeBSDTargetInfo<X86_32TargetInfo>>(Triple,
563 Opts);
564 case llvm::Triple::Fuchsia:
565 return std::make_unique<FuchsiaTargetInfo<X86_32TargetInfo>>(Triple,
566 Opts);
567 case llvm::Triple::KFreeBSD:
568 return std::make_unique<KFreeBSDTargetInfo<X86_32TargetInfo>>(Triple,
569 Opts);
570 case llvm::Triple::Solaris:
571 return std::make_unique<SolarisTargetInfo<X86_32TargetInfo>>(Triple,
572 Opts);
573 case llvm::Triple::Win32: {
574 switch (Triple.getEnvironment()) {
575 case llvm::Triple::Cygnus:
576 return std::make_unique<CygwinX86_32TargetInfo>(Triple, Opts);
577 case llvm::Triple::GNU:
578 return std::make_unique<MinGWX86_32TargetInfo>(Triple, Opts);
579 case llvm::Triple::Itanium:
580 case llvm::Triple::MSVC:
581 default: // Assume MSVC for unknown environments
582 return std::make_unique<MicrosoftX86_32TargetInfo>(Triple, Opts);
583 }
584 }
585 case llvm::Triple::Haiku:
586 return std::make_unique<HaikuX86_32TargetInfo>(Triple, Opts);
587 case llvm::Triple::RTEMS:
588 return std::make_unique<RTEMSX86_32TargetInfo>(Triple, Opts);
589 case llvm::Triple::NaCl:
590 return std::make_unique<NaClTargetInfo<X86_32TargetInfo>>(Triple, Opts);
591 case llvm::Triple::ELFIAMCU:
592 return std::make_unique<MCUX86_32TargetInfo>(Triple, Opts);
593 case llvm::Triple::Hurd:
594 return std::make_unique<HurdTargetInfo<X86_32TargetInfo>>(Triple, Opts);
595 default:
596 return std::make_unique<X86_32TargetInfo>(Triple, Opts);
597 }
598
599 case llvm::Triple::x86_64:
600 if (Triple.isOSDarwin() || Triple.isOSBinFormatMachO())
601 return std::make_unique<DarwinX86_64TargetInfo>(Triple, Opts);
602
603 switch (os) {
604 case llvm::Triple::Linux: {
605 switch (Triple.getEnvironment()) {
606 default:
607 return std::make_unique<LinuxTargetInfo<X86_64TargetInfo>>(Triple,
608 Opts);
609 case llvm::Triple::Android:
610 return std::make_unique<AndroidX86_64TargetInfo>(Triple, Opts);
611 case llvm::Triple::OpenHOS:
612 return std::make_unique<OHOSX86_64TargetInfo>(Triple, Opts);
613 }
614 }
615 case llvm::Triple::DragonFly:
616 return std::make_unique<DragonFlyBSDTargetInfo<X86_64TargetInfo>>(Triple,
617 Opts);
618 case llvm::Triple::NetBSD:
619 return std::make_unique<NetBSDTargetInfo<X86_64TargetInfo>>(Triple, Opts);
620 case llvm::Triple::OpenBSD:
621 return std::make_unique<OpenBSDX86_64TargetInfo>(Triple, Opts);
622 case llvm::Triple::FreeBSD:
623 return std::make_unique<FreeBSDTargetInfo<X86_64TargetInfo>>(Triple,
624 Opts);
625 case llvm::Triple::Fuchsia:
626 return std::make_unique<FuchsiaTargetInfo<X86_64TargetInfo>>(Triple,
627 Opts);
628 case llvm::Triple::KFreeBSD:
629 return std::make_unique<KFreeBSDTargetInfo<X86_64TargetInfo>>(Triple,
630 Opts);
631 case llvm::Triple::Solaris:
632 return std::make_unique<SolarisTargetInfo<X86_64TargetInfo>>(Triple,
633 Opts);
634 case llvm::Triple::Win32: {
635 switch (Triple.getEnvironment()) {
636 case llvm::Triple::Cygnus:
637 return std::make_unique<CygwinX86_64TargetInfo>(Triple, Opts);
638 case llvm::Triple::GNU:
639 return std::make_unique<MinGWX86_64TargetInfo>(Triple, Opts);
640 case llvm::Triple::MSVC:
641 default: // Assume MSVC for unknown environments
642 return std::make_unique<MicrosoftX86_64TargetInfo>(Triple, Opts);
643 }
644 }
645 case llvm::Triple::Haiku:
646 return std::make_unique<HaikuTargetInfo<X86_64TargetInfo>>(Triple, Opts);
647 case llvm::Triple::NaCl:
648 return std::make_unique<NaClTargetInfo<X86_64TargetInfo>>(Triple, Opts);
649 case llvm::Triple::PS4:
650 return std::make_unique<PS4OSTargetInfo<X86_64TargetInfo>>(Triple, Opts);
651 case llvm::Triple::PS5:
652 return std::make_unique<PS5OSTargetInfo<X86_64TargetInfo>>(Triple, Opts);
653 default:
654 return std::make_unique<X86_64TargetInfo>(Triple, Opts);
655 }
656
657 case llvm::Triple::spir: {
658 if (os != llvm::Triple::UnknownOS ||
659 Triple.getEnvironment() != llvm::Triple::UnknownEnvironment)
660 return nullptr;
661 return std::make_unique<SPIR32TargetInfo>(Triple, Opts);
662 }
663 case llvm::Triple::spir64: {
664 if (os != llvm::Triple::UnknownOS ||
665 Triple.getEnvironment() != llvm::Triple::UnknownEnvironment)
666 return nullptr;
667 return std::make_unique<SPIR64TargetInfo>(Triple, Opts);
668 }
669 case llvm::Triple::spirv: {
670 return std::make_unique<SPIRVTargetInfo>(Triple, Opts);
671 }
672 case llvm::Triple::spirv32: {
673 if (os != llvm::Triple::UnknownOS ||
674 Triple.getEnvironment() != llvm::Triple::UnknownEnvironment)
675 return nullptr;
676 return std::make_unique<SPIRV32TargetInfo>(Triple, Opts);
677 }
678 case llvm::Triple::spirv64: {
679 if (os != llvm::Triple::UnknownOS ||
680 Triple.getEnvironment() != llvm::Triple::UnknownEnvironment)
681 return nullptr;
682 return std::make_unique<SPIRV64TargetInfo>(Triple, Opts);
683 }
684 case llvm::Triple::wasm32:
685 if (Triple.getSubArch() != llvm::Triple::NoSubArch ||
686 Triple.getVendor() != llvm::Triple::UnknownVendor ||
687 !Triple.isOSBinFormatWasm())
688 return nullptr;
689 switch (os) {
690 case llvm::Triple::WASI:
691 return std::make_unique<WASITargetInfo<WebAssembly32TargetInfo>>(Triple,
692 Opts);
693 case llvm::Triple::Emscripten:
694 return std::make_unique<EmscriptenTargetInfo<WebAssembly32TargetInfo>>(
695 Triple, Opts);
696 case llvm::Triple::UnknownOS:
697 return std::make_unique<WebAssemblyOSTargetInfo<WebAssembly32TargetInfo>>(
698 Triple, Opts);
699 default:
700 return nullptr;
701 }
702 case llvm::Triple::wasm64:
703 if (Triple.getSubArch() != llvm::Triple::NoSubArch ||
704 Triple.getVendor() != llvm::Triple::UnknownVendor ||
705 !Triple.isOSBinFormatWasm())
706 return nullptr;
707 switch (os) {
708 case llvm::Triple::WASI:
709 return std::make_unique<WASITargetInfo<WebAssembly64TargetInfo>>(Triple,
710 Opts);
711 case llvm::Triple::Emscripten:
712 return std::make_unique<EmscriptenTargetInfo<WebAssembly64TargetInfo>>(
713 Triple, Opts);
714 case llvm::Triple::UnknownOS:
715 return std::make_unique<WebAssemblyOSTargetInfo<WebAssembly64TargetInfo>>(
716 Triple, Opts);
717 default:
718 return nullptr;
719 }
720
721 case llvm::Triple::dxil:
722 return std::make_unique<DirectXTargetInfo>(Triple, Opts);
723 case llvm::Triple::renderscript32:
724 return std::make_unique<LinuxTargetInfo<RenderScript32TargetInfo>>(Triple,
725 Opts);
726 case llvm::Triple::renderscript64:
727 return std::make_unique<LinuxTargetInfo<RenderScript64TargetInfo>>(Triple,
728 Opts);
729
730 case llvm::Triple::ve:
731 return std::make_unique<LinuxTargetInfo<VETargetInfo>>(Triple, Opts);
732
733 case llvm::Triple::csky:
734 switch (os) {
735 case llvm::Triple::Linux:
736 return std::make_unique<LinuxTargetInfo<CSKYTargetInfo>>(Triple, Opts);
737 default:
738 return std::make_unique<CSKYTargetInfo>(Triple, Opts);
739 }
740 case llvm::Triple::loongarch32:
741 switch (os) {
742 case llvm::Triple::Linux:
743 return std::make_unique<LinuxTargetInfo<LoongArch32TargetInfo>>(Triple,
744 Opts);
745 default:
746 return std::make_unique<LoongArch32TargetInfo>(Triple, Opts);
747 }
748 case llvm::Triple::loongarch64:
749 switch (os) {
750 case llvm::Triple::Linux:
751 return std::make_unique<LinuxTargetInfo<LoongArch64TargetInfo>>(Triple,
752 Opts);
753 default:
754 return std::make_unique<LoongArch64TargetInfo>(Triple, Opts);
755 }
756 }
757}
758} // namespace targets
759} // namespace clang
760
761using namespace clang::targets;
762/// CreateTargetInfo - Return the target info object for the specified target
763/// options.
766 const std::shared_ptr<TargetOptions> &Opts) {
767 llvm::Triple Triple(Opts->Triple);
768
769 // Construct the target
770 std::unique_ptr<TargetInfo> Target = AllocateTarget(Triple, *Opts);
771 if (!Target) {
772 Diags.Report(diag::err_target_unknown_triple) << Triple.str();
773 return nullptr;
774 }
775 Target->TargetOpts = Opts;
776
777 // Set the target CPU if specified.
778 if (!Opts->CPU.empty() && !Target->setCPU(Opts->CPU)) {
779 Diags.Report(diag::err_target_unknown_cpu) << Opts->CPU;
781 Target->fillValidCPUList(ValidList);
782 if (!ValidList.empty())
783 Diags.Report(diag::note_valid_options) << llvm::join(ValidList, ", ");
784 return nullptr;
785 }
786
787 // Check the TuneCPU name if specified.
788 if (!Opts->TuneCPU.empty() &&
789 !Target->isValidTuneCPUName(Opts->TuneCPU)) {
790 Diags.Report(diag::err_target_unknown_cpu) << Opts->TuneCPU;
792 Target->fillValidTuneCPUList(ValidList);
793 if (!ValidList.empty())
794 Diags.Report(diag::note_valid_options) << llvm::join(ValidList, ", ");
795 return nullptr;
796 }
797
798 // Set the target ABI if specified.
799 if (!Opts->ABI.empty() && !Target->setABI(Opts->ABI)) {
800 Diags.Report(diag::err_target_unknown_abi) << Opts->ABI;
801 return nullptr;
802 }
803
804 // Set the fp math unit.
805 if (!Opts->FPMath.empty() && !Target->setFPMath(Opts->FPMath)) {
806 Diags.Report(diag::err_target_unknown_fpmath) << Opts->FPMath;
807 return nullptr;
808 }
809
810 // Compute the default target features, we need the target to handle this
811 // because features may have dependencies on one another.
812 llvm::erase_if(Opts->FeaturesAsWritten, [&](StringRef Name) {
813 if (Target->isReadOnlyFeature(Name.substr(1))) {
814 Diags.Report(diag::warn_fe_backend_readonly_feature_flag) << Name;
815 return true;
816 }
817 return false;
818 });
819 if (!Target->initFeatureMap(Opts->FeatureMap, Diags, Opts->CPU,
820 Opts->FeaturesAsWritten))
821 return nullptr;
822
823 // Add the features to the compile options.
824 Opts->Features.clear();
825 for (const auto &F : Opts->FeatureMap)
826 Opts->Features.push_back((F.getValue() ? "+" : "-") + F.getKey().str());
827 // Sort here, so we handle the features in a predictable order. (This matters
828 // when we're dealing with features that overlap.)
829 llvm::sort(Opts->Features);
830
831 if (!Target->handleTargetFeatures(Opts->Features, Diags))
832 return nullptr;
833
834 Target->setSupportedOpenCLOpts();
835 Target->setCommandLineOpenCLOpts();
836 Target->setMaxAtomicWidth();
837
838 if (!Opts->DarwinTargetVariantTriple.empty())
839 Target->DarwinTargetVariantTriple =
840 llvm::Triple(Opts->DarwinTargetVariantTriple);
841
842 if (!Target->validateTarget(Diags))
843 return nullptr;
844
845 Target->CheckFixedPointBits();
846
847 return Target.release();
848}
849/// validateOpenCLTarget - Check that OpenCL target has valid
850/// options setting based on OpenCL version.
852 DiagnosticsEngine &Diags) const {
853 const llvm::StringMap<bool> &OpenCLFeaturesMap = getSupportedOpenCLOpts();
854
855 auto diagnoseNotSupportedCore = [&](llvm::StringRef Name, auto... OptArgs) {
856 if (OpenCLOptions::isOpenCLOptionCoreIn(Opts, OptArgs...) &&
857 !hasFeatureEnabled(OpenCLFeaturesMap, Name))
858 Diags.Report(diag::warn_opencl_unsupported_core_feature)
859 << Name << Opts.OpenCLCPlusPlus
860 << Opts.getOpenCLVersionTuple().getAsString();
861 };
862#define OPENCL_GENERIC_EXTENSION(Ext, ...) \
863 diagnoseNotSupportedCore(#Ext, __VA_ARGS__);
864#include "clang/Basic/OpenCLExtensions.def"
865
866 // Validate that feature macros are set properly for OpenCL C 3.0.
867 // In other cases assume that target is always valid.
868 if (Opts.getOpenCLCompatibleVersion() < 300)
869 return true;
870
873}
Defines the Diagnostic-related interfaces.
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:1542
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:83
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:207
virtual bool hasFeatureEnabled(const llvm::StringMap< bool > &Features, StringRef Name) const
Check if target has a given feature enabled.
Definition: TargetInfo.h:1327
static TargetInfo * CreateTargetInfo(DiagnosticsEngine &Diags, const std::shared_ptr< TargetOptions > &Opts)
Construct a target for the given options.
Definition: Targets.cpp:765
virtual bool validateOpenCLTarget(const LangOptions &Opts, DiagnosticsEngine &Diags) const
Check that OpenCL target has valid options setting based on OpenCL version.
Definition: Targets.cpp:851
llvm::StringMap< bool > & getSupportedOpenCLOpts()
Get supported OpenCL extensions and optional core features.
Definition: TargetInfo.h:1661
Options for controlling the target.
Definition: TargetOptions.h:26
void defineCPUMacros(MacroBuilder &Builder, StringRef CPUName, bool Tuning)
Definition: Targets.cpp:76
void DefineStd(MacroBuilder &Builder, StringRef MacroName, const LangOptions &Opts)
DefineStd - Define a macro name and standard variants.
Definition: Targets.cpp:60
std::unique_ptr< TargetInfo > AllocateTarget(const llvm::Triple &Triple, const TargetOptions &Opts)
Definition: Targets.cpp:112
void addCygMingDefines(const LangOptions &Opts, MacroBuilder &Builder)
Definition: Targets.cpp:83