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
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(Name: MacroName);
68
69 // Define __unix.
70 Builder.defineMacro(Name: "__" + MacroName);
71
72 // Define __unix__.
73 Builder.defineMacro(Name: "__" + MacroName + "__");
74}
75
76void defineCPUMacros(MacroBuilder &Builder, StringRef CPUName, bool Tuning) {
77 Builder.defineMacro(Name: "__" + CPUName);
78 Builder.defineMacro(Name: "__" + CPUName + "__");
79 if (Tuning)
80 Builder.defineMacro(Name: "__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(Name: "__declspec", Value: "__declspec");
90 else
91 Builder.defineMacro(Name: "__declspec(a)", Value: "__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(Name: Twine("_") + CC, Value: GCCSpelling);
103 Builder.defineMacro(Name: Twine("__") + CC, Value: 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>(args: Triple, args: Opts);
122
123 case llvm::Triple::xcore:
124 return std::make_unique<XCoreTargetInfo>(args: Triple, args: 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>>(args: Triple, args: Opts);
130 return std::make_unique<HexagonTargetInfo>(args: Triple, args: Opts);
131
132 case llvm::Triple::lanai:
133 return std::make_unique<LanaiTargetInfo>(args: Triple, args: Opts);
134
135 case llvm::Triple::aarch64_32:
136 if (Triple.isOSDarwin())
137 return std::make_unique<DarwinAArch64TargetInfo>(args: Triple, args: Opts);
138
139 return nullptr;
140 case llvm::Triple::aarch64:
141 if (Triple.isOSDarwin())
142 return std::make_unique<DarwinAArch64TargetInfo>(args: Triple, args: Opts);
143
144 switch (os) {
145 case llvm::Triple::FreeBSD:
146 return std::make_unique<FreeBSDTargetInfo<AArch64leTargetInfo>>(args: Triple,
147 args: Opts);
148 case llvm::Triple::Fuchsia:
149 return std::make_unique<FuchsiaTargetInfo<AArch64leTargetInfo>>(args: Triple,
150 args: Opts);
151 case llvm::Triple::Haiku:
152 return std::make_unique<HaikuTargetInfo<AArch64leTargetInfo>>(args: Triple,
153 args: Opts);
154 case llvm::Triple::Linux:
155 switch (Triple.getEnvironment()) {
156 default:
157 return std::make_unique<LinuxTargetInfo<AArch64leTargetInfo>>(args: Triple,
158 args: Opts);
159 case llvm::Triple::OpenHOS:
160 return std::make_unique<OHOSTargetInfo<AArch64leTargetInfo>>(args: Triple,
161 args: Opts);
162 }
163 case llvm::Triple::NetBSD:
164 return std::make_unique<NetBSDTargetInfo<AArch64leTargetInfo>>(args: Triple,
165 args: Opts);
166 case llvm::Triple::OpenBSD:
167 return std::make_unique<OpenBSDTargetInfo<AArch64leTargetInfo>>(args: Triple,
168 args: Opts);
169 case llvm::Triple::Win32:
170 switch (Triple.getEnvironment()) {
171 case llvm::Triple::GNU:
172 return std::make_unique<MinGWARM64TargetInfo>(args: Triple, args: Opts);
173 case llvm::Triple::MSVC:
174 default: // Assume MSVC for unknown environments
175 return std::make_unique<MicrosoftARM64TargetInfo>(args: Triple, args: Opts);
176 }
177 default:
178 return std::make_unique<AArch64leTargetInfo>(args: Triple, args: Opts);
179 }
180
181 case llvm::Triple::aarch64_be:
182 switch (os) {
183 case llvm::Triple::FreeBSD:
184 return std::make_unique<FreeBSDTargetInfo<AArch64beTargetInfo>>(args: Triple,
185 args: Opts);
186 case llvm::Triple::Fuchsia:
187 return std::make_unique<FuchsiaTargetInfo<AArch64beTargetInfo>>(args: Triple,
188 args: Opts);
189 case llvm::Triple::Linux:
190 return std::make_unique<LinuxTargetInfo<AArch64beTargetInfo>>(args: Triple,
191 args: Opts);
192 case llvm::Triple::NetBSD:
193 return std::make_unique<NetBSDTargetInfo<AArch64beTargetInfo>>(args: Triple,
194 args: Opts);
195 default:
196 return std::make_unique<AArch64beTargetInfo>(args: Triple, args: Opts);
197 }
198
199 case llvm::Triple::arm:
200 case llvm::Triple::thumb:
201 if (Triple.isOSBinFormatMachO())
202 return std::make_unique<DarwinARMTargetInfo>(args: Triple, args: Opts);
203
204 switch (os) {
205 case llvm::Triple::Linux:
206 switch (Triple.getEnvironment()) {
207 default:
208 return std::make_unique<LinuxTargetInfo<ARMleTargetInfo>>(args: Triple, args: Opts);
209 case llvm::Triple::OpenHOS:
210 return std::make_unique<OHOSTargetInfo<ARMleTargetInfo>>(args: Triple, args: Opts);
211 }
212 case llvm::Triple::LiteOS:
213 return std::make_unique<OHOSTargetInfo<ARMleTargetInfo>>(args: Triple, args: Opts);
214 case llvm::Triple::FreeBSD:
215 return std::make_unique<FreeBSDTargetInfo<ARMleTargetInfo>>(args: Triple, args: Opts);
216 case llvm::Triple::NetBSD:
217 return std::make_unique<NetBSDTargetInfo<ARMleTargetInfo>>(args: Triple, args: Opts);
218 case llvm::Triple::OpenBSD:
219 return std::make_unique<OpenBSDTargetInfo<ARMleTargetInfo>>(args: Triple, args: Opts);
220 case llvm::Triple::RTEMS:
221 return std::make_unique<RTEMSTargetInfo<ARMleTargetInfo>>(args: Triple, args: Opts);
222 case llvm::Triple::Haiku:
223 return std::make_unique<HaikuTargetInfo<ARMleTargetInfo>>(args: Triple, args: Opts);
224 case llvm::Triple::NaCl:
225 return std::make_unique<NaClTargetInfo<ARMleTargetInfo>>(args: Triple, args: Opts);
226 case llvm::Triple::Win32:
227 switch (Triple.getEnvironment()) {
228 case llvm::Triple::Cygnus:
229 return std::make_unique<CygwinARMTargetInfo>(args: Triple, args: Opts);
230 case llvm::Triple::GNU:
231 return std::make_unique<MinGWARMTargetInfo>(args: Triple, args: Opts);
232 case llvm::Triple::Itanium:
233 return std::make_unique<ItaniumWindowsARMleTargetInfo>(args: Triple, args: Opts);
234 case llvm::Triple::MSVC:
235 default: // Assume MSVC for unknown environments
236 return std::make_unique<MicrosoftARMleTargetInfo>(args: Triple, args: Opts);
237 }
238 default:
239 return std::make_unique<ARMleTargetInfo>(args: Triple, args: Opts);
240 }
241
242 case llvm::Triple::armeb:
243 case llvm::Triple::thumbeb:
244 if (Triple.isOSDarwin())
245 return std::make_unique<DarwinARMTargetInfo>(args: Triple, args: Opts);
246
247 switch (os) {
248 case llvm::Triple::Linux:
249 return std::make_unique<LinuxTargetInfo<ARMbeTargetInfo>>(args: Triple, args: Opts);
250 case llvm::Triple::NetBSD:
251 return std::make_unique<NetBSDTargetInfo<ARMbeTargetInfo>>(args: Triple, args: Opts);
252 case llvm::Triple::RTEMS:
253 return std::make_unique<RTEMSTargetInfo<ARMbeTargetInfo>>(args: Triple, args: Opts);
254 case llvm::Triple::NaCl:
255 return std::make_unique<NaClTargetInfo<ARMbeTargetInfo>>(args: Triple, args: Opts);
256 default:
257 return std::make_unique<ARMbeTargetInfo>(args: Triple, args: Opts);
258 }
259
260 case llvm::Triple::avr:
261 return std::make_unique<AVRTargetInfo>(args: Triple, args: Opts);
262 case llvm::Triple::bpfeb:
263 case llvm::Triple::bpfel:
264 return std::make_unique<BPFTargetInfo>(args: Triple, args: Opts);
265
266 case llvm::Triple::msp430:
267 return std::make_unique<MSP430TargetInfo>(args: Triple, args: Opts);
268
269 case llvm::Triple::mips:
270 switch (os) {
271 case llvm::Triple::Linux:
272 return std::make_unique<LinuxTargetInfo<MipsTargetInfo>>(args: Triple, args: Opts);
273 case llvm::Triple::RTEMS:
274 return std::make_unique<RTEMSTargetInfo<MipsTargetInfo>>(args: Triple, args: Opts);
275 case llvm::Triple::FreeBSD:
276 return std::make_unique<FreeBSDTargetInfo<MipsTargetInfo>>(args: Triple, args: Opts);
277 case llvm::Triple::NetBSD:
278 return std::make_unique<NetBSDTargetInfo<MipsTargetInfo>>(args: Triple, args: Opts);
279 default:
280 return std::make_unique<MipsTargetInfo>(args: Triple, args: 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>>(args: Triple, args: Opts);
289 case llvm::Triple::OpenHOS:
290 return std::make_unique<OHOSTargetInfo<MipsTargetInfo>>(args: Triple, args: Opts);
291 }
292 case llvm::Triple::RTEMS:
293 return std::make_unique<RTEMSTargetInfo<MipsTargetInfo>>(args: Triple, args: Opts);
294 case llvm::Triple::FreeBSD:
295 return std::make_unique<FreeBSDTargetInfo<MipsTargetInfo>>(args: Triple, args: Opts);
296 case llvm::Triple::NetBSD:
297 return std::make_unique<NetBSDTargetInfo<MipsTargetInfo>>(args: Triple, args: Opts);
298 case llvm::Triple::NaCl:
299 return std::make_unique<NaClTargetInfo<NaClMips32TargetInfo>>(args: Triple,
300 args: Opts);
301 default:
302 return std::make_unique<MipsTargetInfo>(args: Triple, args: Opts);
303 }
304
305 case llvm::Triple::mips64:
306 switch (os) {
307 case llvm::Triple::Linux:
308 return std::make_unique<LinuxTargetInfo<MipsTargetInfo>>(args: Triple, args: Opts);
309 case llvm::Triple::RTEMS:
310 return std::make_unique<RTEMSTargetInfo<MipsTargetInfo>>(args: Triple, args: Opts);
311 case llvm::Triple::FreeBSD:
312 return std::make_unique<FreeBSDTargetInfo<MipsTargetInfo>>(args: Triple, args: Opts);
313 case llvm::Triple::NetBSD:
314 return std::make_unique<NetBSDTargetInfo<MipsTargetInfo>>(args: Triple, args: Opts);
315 case llvm::Triple::OpenBSD:
316 return std::make_unique<OpenBSDTargetInfo<MipsTargetInfo>>(args: Triple, args: Opts);
317 default:
318 return std::make_unique<MipsTargetInfo>(args: Triple, args: Opts);
319 }
320
321 case llvm::Triple::mips64el:
322 switch (os) {
323 case llvm::Triple::Linux:
324 return std::make_unique<LinuxTargetInfo<MipsTargetInfo>>(args: Triple, args: Opts);
325 case llvm::Triple::RTEMS:
326 return std::make_unique<RTEMSTargetInfo<MipsTargetInfo>>(args: Triple, args: Opts);
327 case llvm::Triple::FreeBSD:
328 return std::make_unique<FreeBSDTargetInfo<MipsTargetInfo>>(args: Triple, args: Opts);
329 case llvm::Triple::NetBSD:
330 return std::make_unique<NetBSDTargetInfo<MipsTargetInfo>>(args: Triple, args: Opts);
331 case llvm::Triple::OpenBSD:
332 return std::make_unique<OpenBSDTargetInfo<MipsTargetInfo>>(args: Triple, args: Opts);
333 default:
334 return std::make_unique<MipsTargetInfo>(args: Triple, args: Opts);
335 }
336
337 case llvm::Triple::m68k:
338 switch (os) {
339 case llvm::Triple::Linux:
340 return std::make_unique<LinuxTargetInfo<M68kTargetInfo>>(args: Triple, args: Opts);
341 case llvm::Triple::NetBSD:
342 return std::make_unique<NetBSDTargetInfo<M68kTargetInfo>>(args: Triple, args: Opts);
343 default:
344 return std::make_unique<M68kTargetInfo>(args: Triple, args: Opts);
345 }
346
347 case llvm::Triple::le32:
348 switch (os) {
349 case llvm::Triple::NaCl:
350 return std::make_unique<NaClTargetInfo<PNaClTargetInfo>>(args: Triple, args: Opts);
351 default:
352 return nullptr;
353 }
354
355 case llvm::Triple::le64:
356 return std::make_unique<Le64TargetInfo>(args: Triple, args: Opts);
357
358 case llvm::Triple::ppc:
359 switch (os) {
360 case llvm::Triple::Linux:
361 return std::make_unique<LinuxTargetInfo<PPC32TargetInfo>>(args: Triple, args: Opts);
362 case llvm::Triple::FreeBSD:
363 return std::make_unique<FreeBSDTargetInfo<PPC32TargetInfo>>(args: Triple, args: Opts);
364 case llvm::Triple::NetBSD:
365 return std::make_unique<NetBSDTargetInfo<PPC32TargetInfo>>(args: Triple, args: Opts);
366 case llvm::Triple::OpenBSD:
367 return std::make_unique<OpenBSDTargetInfo<PPC32TargetInfo>>(args: Triple, args: Opts);
368 case llvm::Triple::RTEMS:
369 return std::make_unique<RTEMSTargetInfo<PPC32TargetInfo>>(args: Triple, args: Opts);
370 case llvm::Triple::AIX:
371 return std::make_unique<AIXPPC32TargetInfo>(args: Triple, args: Opts);
372 default:
373 return std::make_unique<PPC32TargetInfo>(args: Triple, args: Opts);
374 }
375
376 case llvm::Triple::ppcle:
377 switch (os) {
378 case llvm::Triple::Linux:
379 return std::make_unique<LinuxTargetInfo<PPC32TargetInfo>>(args: Triple, args: Opts);
380 case llvm::Triple::FreeBSD:
381 return std::make_unique<FreeBSDTargetInfo<PPC32TargetInfo>>(args: Triple, args: Opts);
382 default:
383 return std::make_unique<PPC32TargetInfo>(args: Triple, args: Opts);
384 }
385
386 case llvm::Triple::ppc64:
387 switch (os) {
388 case llvm::Triple::Linux:
389 return std::make_unique<LinuxTargetInfo<PPC64TargetInfo>>(args: Triple, args: Opts);
390 case llvm::Triple::Lv2:
391 return std::make_unique<PS3PPUTargetInfo<PPC64TargetInfo>>(args: Triple, args: Opts);
392 case llvm::Triple::FreeBSD:
393 return std::make_unique<FreeBSDTargetInfo<PPC64TargetInfo>>(args: Triple, args: Opts);
394 case llvm::Triple::NetBSD:
395 return std::make_unique<NetBSDTargetInfo<PPC64TargetInfo>>(args: Triple, args: Opts);
396 case llvm::Triple::OpenBSD:
397 return std::make_unique<OpenBSDTargetInfo<PPC64TargetInfo>>(args: Triple, args: Opts);
398 case llvm::Triple::AIX:
399 return std::make_unique<AIXPPC64TargetInfo>(args: Triple, args: Opts);
400 default:
401 return std::make_unique<PPC64TargetInfo>(args: Triple, args: Opts);
402 }
403
404 case llvm::Triple::ppc64le:
405 switch (os) {
406 case llvm::Triple::Linux:
407 return std::make_unique<LinuxTargetInfo<PPC64TargetInfo>>(args: Triple, args: Opts);
408 case llvm::Triple::FreeBSD:
409 return std::make_unique<FreeBSDTargetInfo<PPC64TargetInfo>>(args: Triple, args: Opts);
410 case llvm::Triple::NetBSD:
411 return std::make_unique<NetBSDTargetInfo<PPC64TargetInfo>>(args: Triple, args: Opts);
412 case llvm::Triple::OpenBSD:
413 return std::make_unique<OpenBSDTargetInfo<PPC64TargetInfo>>(args: Triple, args: Opts);
414 default:
415 return std::make_unique<PPC64TargetInfo>(args: Triple, args: Opts);
416 }
417
418 case llvm::Triple::nvptx:
419 return std::make_unique<NVPTXTargetInfo>(args: Triple, args: Opts,
420 /*TargetPointerWidth=*/args: 32);
421 case llvm::Triple::nvptx64:
422 return std::make_unique<NVPTXTargetInfo>(args: Triple, args: Opts,
423 /*TargetPointerWidth=*/args: 64);
424
425 case llvm::Triple::amdgcn:
426 case llvm::Triple::r600:
427 return std::make_unique<AMDGPUTargetInfo>(args: Triple, args: Opts);
428
429 case llvm::Triple::riscv32:
430 switch (os) {
431 case llvm::Triple::NetBSD:
432 return std::make_unique<NetBSDTargetInfo<RISCV32TargetInfo>>(args: Triple,
433 args: Opts);
434 case llvm::Triple::Linux:
435 return std::make_unique<LinuxTargetInfo<RISCV32TargetInfo>>(args: Triple, args: Opts);
436 default:
437 return std::make_unique<RISCV32TargetInfo>(args: Triple, args: Opts);
438 }
439
440 case llvm::Triple::riscv64:
441 switch (os) {
442 case llvm::Triple::FreeBSD:
443 return std::make_unique<FreeBSDTargetInfo<RISCV64TargetInfo>>(args: Triple,
444 args: Opts);
445 case llvm::Triple::NetBSD:
446 return std::make_unique<NetBSDTargetInfo<RISCV64TargetInfo>>(args: Triple,
447 args: Opts);
448 case llvm::Triple::OpenBSD:
449 return std::make_unique<OpenBSDTargetInfo<RISCV64TargetInfo>>(args: Triple,
450 args: Opts);
451 case llvm::Triple::Fuchsia:
452 return std::make_unique<FuchsiaTargetInfo<RISCV64TargetInfo>>(args: Triple,
453 args: Opts);
454 case llvm::Triple::Haiku:
455 return std::make_unique<HaikuTargetInfo<RISCV64TargetInfo>>(args: Triple,
456 args: Opts);
457 case llvm::Triple::Linux:
458 switch (Triple.getEnvironment()) {
459 default:
460 return std::make_unique<LinuxTargetInfo<RISCV64TargetInfo>>(args: Triple,
461 args: Opts);
462 case llvm::Triple::OpenHOS:
463 return std::make_unique<OHOSTargetInfo<RISCV64TargetInfo>>(args: Triple,
464 args: Opts);
465 }
466 default:
467 return std::make_unique<RISCV64TargetInfo>(args: Triple, args: Opts);
468 }
469
470 case llvm::Triple::sparc:
471 switch (os) {
472 case llvm::Triple::Linux:
473 return std::make_unique<LinuxTargetInfo<SparcV8TargetInfo>>(args: Triple, args: Opts);
474 case llvm::Triple::Solaris:
475 return std::make_unique<SolarisTargetInfo<SparcV8TargetInfo>>(args: Triple,
476 args: Opts);
477 case llvm::Triple::NetBSD:
478 return std::make_unique<NetBSDTargetInfo<SparcV8TargetInfo>>(args: Triple,
479 args: Opts);
480 case llvm::Triple::RTEMS:
481 return std::make_unique<RTEMSTargetInfo<SparcV8TargetInfo>>(args: Triple, args: Opts);
482 default:
483 return std::make_unique<SparcV8TargetInfo>(args: Triple, args: Opts);
484 }
485
486 case llvm::Triple::sparcel:
487 switch (os) {
488 case llvm::Triple::Linux:
489 return std::make_unique<LinuxTargetInfo<SparcV8elTargetInfo>>(args: Triple,
490 args: Opts);
491 case llvm::Triple::RTEMS:
492 return std::make_unique<RTEMSTargetInfo<SparcV8elTargetInfo>>(args: Triple,
493 args: Opts);
494 default:
495 return std::make_unique<SparcV8elTargetInfo>(args: Triple, args: Opts);
496 }
497
498 case llvm::Triple::sparcv9:
499 switch (os) {
500 case llvm::Triple::Linux:
501 return std::make_unique<LinuxTargetInfo<SparcV9TargetInfo>>(args: Triple, args: Opts);
502 case llvm::Triple::Solaris:
503 return std::make_unique<SolarisTargetInfo<SparcV9TargetInfo>>(args: Triple,
504 args: Opts);
505 case llvm::Triple::NetBSD:
506 return std::make_unique<NetBSDTargetInfo<SparcV9TargetInfo>>(args: Triple,
507 args: Opts);
508 case llvm::Triple::OpenBSD:
509 return std::make_unique<OpenBSDTargetInfo<SparcV9TargetInfo>>(args: Triple,
510 args: Opts);
511 case llvm::Triple::FreeBSD:
512 return std::make_unique<FreeBSDTargetInfo<SparcV9TargetInfo>>(args: Triple,
513 args: Opts);
514 default:
515 return std::make_unique<SparcV9TargetInfo>(args: Triple, args: Opts);
516 }
517
518 case llvm::Triple::systemz:
519 switch (os) {
520 case llvm::Triple::Linux:
521 return std::make_unique<LinuxTargetInfo<SystemZTargetInfo>>(args: Triple, args: Opts);
522 case llvm::Triple::ZOS:
523 return std::make_unique<ZOSTargetInfo<SystemZTargetInfo>>(args: Triple, args: Opts);
524 default:
525 return std::make_unique<SystemZTargetInfo>(args: Triple, args: Opts);
526 }
527
528 case llvm::Triple::tce:
529 return std::make_unique<TCETargetInfo>(args: Triple, args: Opts);
530
531 case llvm::Triple::tcele:
532 return std::make_unique<TCELETargetInfo>(args: Triple, args: Opts);
533
534 case llvm::Triple::x86:
535 if (Triple.isOSDarwin())
536 return std::make_unique<DarwinI386TargetInfo>(args: Triple, args: Opts);
537
538 switch (os) {
539 case llvm::Triple::Linux: {
540 switch (Triple.getEnvironment()) {
541 default:
542 return std::make_unique<LinuxTargetInfo<X86_32TargetInfo>>(args: Triple,
543 args: Opts);
544 case llvm::Triple::Android:
545 return std::make_unique<AndroidX86_32TargetInfo>(args: Triple, args: Opts);
546 }
547 }
548 case llvm::Triple::DragonFly:
549 return std::make_unique<DragonFlyBSDTargetInfo<X86_32TargetInfo>>(args: Triple,
550 args: Opts);
551 case llvm::Triple::NetBSD:
552 return std::make_unique<NetBSDI386TargetInfo>(args: Triple, args: Opts);
553 case llvm::Triple::OpenBSD:
554 return std::make_unique<OpenBSDI386TargetInfo>(args: Triple, args: Opts);
555 case llvm::Triple::FreeBSD:
556 return std::make_unique<FreeBSDTargetInfo<X86_32TargetInfo>>(args: Triple,
557 args: Opts);
558 case llvm::Triple::Fuchsia:
559 return std::make_unique<FuchsiaTargetInfo<X86_32TargetInfo>>(args: Triple,
560 args: Opts);
561 case llvm::Triple::KFreeBSD:
562 return std::make_unique<KFreeBSDTargetInfo<X86_32TargetInfo>>(args: Triple,
563 args: Opts);
564 case llvm::Triple::Solaris:
565 return std::make_unique<SolarisTargetInfo<X86_32TargetInfo>>(args: Triple,
566 args: Opts);
567 case llvm::Triple::Win32: {
568 switch (Triple.getEnvironment()) {
569 case llvm::Triple::Cygnus:
570 return std::make_unique<CygwinX86_32TargetInfo>(args: Triple, args: Opts);
571 case llvm::Triple::GNU:
572 return std::make_unique<MinGWX86_32TargetInfo>(args: Triple, args: 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>(args: Triple, args: Opts);
577 }
578 }
579 case llvm::Triple::Haiku:
580 return std::make_unique<HaikuX86_32TargetInfo>(args: Triple, args: Opts);
581 case llvm::Triple::RTEMS:
582 return std::make_unique<RTEMSX86_32TargetInfo>(args: Triple, args: Opts);
583 case llvm::Triple::NaCl:
584 return std::make_unique<NaClTargetInfo<X86_32TargetInfo>>(args: Triple, args: Opts);
585 case llvm::Triple::ELFIAMCU:
586 return std::make_unique<MCUX86_32TargetInfo>(args: Triple, args: Opts);
587 case llvm::Triple::Hurd:
588 return std::make_unique<HurdTargetInfo<X86_32TargetInfo>>(args: Triple, args: Opts);
589 default:
590 return std::make_unique<X86_32TargetInfo>(args: Triple, args: Opts);
591 }
592
593 case llvm::Triple::x86_64:
594 if (Triple.isOSDarwin() || Triple.isOSBinFormatMachO())
595 return std::make_unique<DarwinX86_64TargetInfo>(args: Triple, args: Opts);
596
597 switch (os) {
598 case llvm::Triple::Linux: {
599 switch (Triple.getEnvironment()) {
600 default:
601 return std::make_unique<LinuxTargetInfo<X86_64TargetInfo>>(args: Triple,
602 args: Opts);
603 case llvm::Triple::Android:
604 return std::make_unique<AndroidX86_64TargetInfo>(args: Triple, args: Opts);
605 case llvm::Triple::OpenHOS:
606 return std::make_unique<OHOSX86_64TargetInfo>(args: Triple, args: Opts);
607 }
608 }
609 case llvm::Triple::DragonFly:
610 return std::make_unique<DragonFlyBSDTargetInfo<X86_64TargetInfo>>(args: Triple,
611 args: Opts);
612 case llvm::Triple::NetBSD:
613 return std::make_unique<NetBSDTargetInfo<X86_64TargetInfo>>(args: Triple, args: Opts);
614 case llvm::Triple::OpenBSD:
615 return std::make_unique<OpenBSDX86_64TargetInfo>(args: Triple, args: Opts);
616 case llvm::Triple::FreeBSD:
617 return std::make_unique<FreeBSDTargetInfo<X86_64TargetInfo>>(args: Triple,
618 args: Opts);
619 case llvm::Triple::Fuchsia:
620 return std::make_unique<FuchsiaTargetInfo<X86_64TargetInfo>>(args: Triple,
621 args: Opts);
622 case llvm::Triple::KFreeBSD:
623 return std::make_unique<KFreeBSDTargetInfo<X86_64TargetInfo>>(args: Triple,
624 args: Opts);
625 case llvm::Triple::Solaris:
626 return std::make_unique<SolarisTargetInfo<X86_64TargetInfo>>(args: Triple,
627 args: Opts);
628 case llvm::Triple::Win32: {
629 switch (Triple.getEnvironment()) {
630 case llvm::Triple::Cygnus:
631 return std::make_unique<CygwinX86_64TargetInfo>(args: Triple, args: Opts);
632 case llvm::Triple::GNU:
633 return std::make_unique<MinGWX86_64TargetInfo>(args: Triple, args: Opts);
634 case llvm::Triple::MSVC:
635 default: // Assume MSVC for unknown environments
636 return std::make_unique<MicrosoftX86_64TargetInfo>(args: Triple, args: Opts);
637 }
638 }
639 case llvm::Triple::Haiku:
640 return std::make_unique<HaikuTargetInfo<X86_64TargetInfo>>(args: Triple, args: Opts);
641 case llvm::Triple::NaCl:
642 return std::make_unique<NaClTargetInfo<X86_64TargetInfo>>(args: Triple, args: Opts);
643 case llvm::Triple::PS4:
644 return std::make_unique<PS4OSTargetInfo<X86_64TargetInfo>>(args: Triple, args: Opts);
645 case llvm::Triple::PS5:
646 return std::make_unique<PS5OSTargetInfo<X86_64TargetInfo>>(args: Triple, args: Opts);
647 case llvm::Triple::Hurd:
648 return std::make_unique<HurdTargetInfo<X86_64TargetInfo>>(args: Triple, args: Opts);
649 default:
650 return std::make_unique<X86_64TargetInfo>(args: Triple, args: 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>(args: Triple, args: 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>(args: Triple, args: Opts);
664 }
665 case llvm::Triple::spirv: {
666 return std::make_unique<SPIRVTargetInfo>(args: Triple, args: 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>(args: Triple, args: 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>(args: Triple, args: Opts);
679 return nullptr;
680 }
681 return std::make_unique<SPIRV64TargetInfo>(args: Triple, args: 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>>(args: Triple,
691 args: Opts);
692 case llvm::Triple::Emscripten:
693 return std::make_unique<EmscriptenTargetInfo<WebAssembly32TargetInfo>>(
694 args: Triple, args: Opts);
695 case llvm::Triple::UnknownOS:
696 return std::make_unique<WebAssemblyOSTargetInfo<WebAssembly32TargetInfo>>(
697 args: Triple, args: 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>>(args: Triple,
709 args: Opts);
710 case llvm::Triple::Emscripten:
711 return std::make_unique<EmscriptenTargetInfo<WebAssembly64TargetInfo>>(
712 args: Triple, args: Opts);
713 case llvm::Triple::UnknownOS:
714 return std::make_unique<WebAssemblyOSTargetInfo<WebAssembly64TargetInfo>>(
715 args: Triple, args: Opts);
716 default:
717 return nullptr;
718 }
719
720 case llvm::Triple::dxil:
721 return std::make_unique<DirectXTargetInfo>(args: Triple, args: Opts);
722 case llvm::Triple::renderscript32:
723 return std::make_unique<LinuxTargetInfo<RenderScript32TargetInfo>>(args: Triple,
724 args: Opts);
725 case llvm::Triple::renderscript64:
726 return std::make_unique<LinuxTargetInfo<RenderScript64TargetInfo>>(args: Triple,
727 args: Opts);
728
729 case llvm::Triple::ve:
730 return std::make_unique<LinuxTargetInfo<VETargetInfo>>(args: Triple, args: Opts);
731
732 case llvm::Triple::csky:
733 switch (os) {
734 case llvm::Triple::Linux:
735 return std::make_unique<LinuxTargetInfo<CSKYTargetInfo>>(args: Triple, args: Opts);
736 default:
737 return std::make_unique<CSKYTargetInfo>(args: Triple, args: Opts);
738 }
739 case llvm::Triple::loongarch32:
740 switch (os) {
741 case llvm::Triple::Linux:
742 return std::make_unique<LinuxTargetInfo<LoongArch32TargetInfo>>(args: Triple,
743 args: Opts);
744 default:
745 return std::make_unique<LoongArch32TargetInfo>(args: Triple, args: Opts);
746 }
747 case llvm::Triple::loongarch64:
748 switch (os) {
749 case llvm::Triple::Linux:
750 return std::make_unique<LinuxTargetInfo<LoongArch64TargetInfo>>(args: Triple,
751 args: Opts);
752 default:
753 return std::make_unique<LoongArch64TargetInfo>(args: Triple, args: Opts);
754 }
755 }
756}
757} // namespace targets
758} // namespace clang
759
760using namespace clang::targets;
761/// CreateTargetInfo - Return the target info object for the specified target
762/// options.
763TargetInfo *
764TargetInfo::CreateTargetInfo(DiagnosticsEngine &Diags,
765 const std::shared_ptr<TargetOptions> &Opts) {
766 llvm::Triple Triple(llvm::Triple::normalize(Str: Opts->Triple));
767
768 // Construct the target
769 std::unique_ptr<TargetInfo> Target = AllocateTarget(Triple, Opts: *Opts);
770 if (!Target) {
771 Diags.Report(DiagID: 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(DiagID: diag::err_target_unknown_cpu) << Opts->CPU;
779 SmallVector<StringRef, 32> ValidList;
780 Target->fillValidCPUList(Values&: ValidList);
781 if (!ValidList.empty())
782 Diags.Report(DiagID: diag::note_valid_options) << llvm::join(R&: ValidList, Separator: ", ");
783 return nullptr;
784 }
785
786 // Check the TuneCPU name if specified.
787 if (!Opts->TuneCPU.empty() &&
788 !Target->isValidTuneCPUName(Name: Opts->TuneCPU)) {
789 Diags.Report(DiagID: diag::err_target_unknown_cpu) << Opts->TuneCPU;
790 SmallVector<StringRef, 32> ValidList;
791 Target->fillValidTuneCPUList(Values&: ValidList);
792 if (!ValidList.empty())
793 Diags.Report(DiagID: diag::note_valid_options) << llvm::join(R&: ValidList, Separator: ", ");
794 return nullptr;
795 }
796
797 // Set the target ABI if specified.
798 if (!Opts->ABI.empty() && !Target->setABI(Opts->ABI)) {
799 Diags.Report(DiagID: 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(DiagID: 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(C&: Opts->FeaturesAsWritten, P: [&](StringRef Name) {
812 if (Target->isReadOnlyFeature(Feature: Name.substr(Start: 1))) {
813 Diags.Report(DiagID: diag::warn_fe_backend_readonly_feature_flag) << Name;
814 return true;
815 }
816 return false;
817 });
818 if (!Target->initFeatureMap(Features&: Opts->FeatureMap, Diags, CPU: Opts->CPU,
819 FeatureVec: 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(x: (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(C&: Opts->Features);
829
830 if (!Target->handleTargetFeatures(Features&: 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.
850bool 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(Features: OpenCLFeaturesMap, Name))
857 Diags.Report(DiagID: 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(TI: *this, Diags) &&
871 OpenCLOptions::diagnoseFeatureExtensionDifferences(TI: *this, Diags);
872}
873