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 return nullptr;
678 return std::make_unique<SPIRV64TargetInfo>(args: Triple, args: Opts);
679 }
680 case llvm::Triple::wasm32:
681 if (Triple.getSubArch() != llvm::Triple::NoSubArch ||
682 Triple.getVendor() != llvm::Triple::UnknownVendor ||
683 !Triple.isOSBinFormatWasm())
684 return nullptr;
685 switch (os) {
686 case llvm::Triple::WASI:
687 return std::make_unique<WASITargetInfo<WebAssembly32TargetInfo>>(args: Triple,
688 args: Opts);
689 case llvm::Triple::Emscripten:
690 return std::make_unique<EmscriptenTargetInfo<WebAssembly32TargetInfo>>(
691 args: Triple, args: Opts);
692 case llvm::Triple::UnknownOS:
693 return std::make_unique<WebAssemblyOSTargetInfo<WebAssembly32TargetInfo>>(
694 args: Triple, args: Opts);
695 default:
696 return nullptr;
697 }
698 case llvm::Triple::wasm64:
699 if (Triple.getSubArch() != llvm::Triple::NoSubArch ||
700 Triple.getVendor() != llvm::Triple::UnknownVendor ||
701 !Triple.isOSBinFormatWasm())
702 return nullptr;
703 switch (os) {
704 case llvm::Triple::WASI:
705 return std::make_unique<WASITargetInfo<WebAssembly64TargetInfo>>(args: Triple,
706 args: Opts);
707 case llvm::Triple::Emscripten:
708 return std::make_unique<EmscriptenTargetInfo<WebAssembly64TargetInfo>>(
709 args: Triple, args: Opts);
710 case llvm::Triple::UnknownOS:
711 return std::make_unique<WebAssemblyOSTargetInfo<WebAssembly64TargetInfo>>(
712 args: Triple, args: Opts);
713 default:
714 return nullptr;
715 }
716
717 case llvm::Triple::dxil:
718 return std::make_unique<DirectXTargetInfo>(args: Triple, args: Opts);
719 case llvm::Triple::renderscript32:
720 return std::make_unique<LinuxTargetInfo<RenderScript32TargetInfo>>(args: Triple,
721 args: Opts);
722 case llvm::Triple::renderscript64:
723 return std::make_unique<LinuxTargetInfo<RenderScript64TargetInfo>>(args: Triple,
724 args: Opts);
725
726 case llvm::Triple::ve:
727 return std::make_unique<LinuxTargetInfo<VETargetInfo>>(args: Triple, args: Opts);
728
729 case llvm::Triple::csky:
730 switch (os) {
731 case llvm::Triple::Linux:
732 return std::make_unique<LinuxTargetInfo<CSKYTargetInfo>>(args: Triple, args: Opts);
733 default:
734 return std::make_unique<CSKYTargetInfo>(args: Triple, args: Opts);
735 }
736 case llvm::Triple::loongarch32:
737 switch (os) {
738 case llvm::Triple::Linux:
739 return std::make_unique<LinuxTargetInfo<LoongArch32TargetInfo>>(args: Triple,
740 args: Opts);
741 default:
742 return std::make_unique<LoongArch32TargetInfo>(args: Triple, args: Opts);
743 }
744 case llvm::Triple::loongarch64:
745 switch (os) {
746 case llvm::Triple::Linux:
747 return std::make_unique<LinuxTargetInfo<LoongArch64TargetInfo>>(args: Triple,
748 args: Opts);
749 default:
750 return std::make_unique<LoongArch64TargetInfo>(args: Triple, args: Opts);
751 }
752 }
753}
754} // namespace targets
755} // namespace clang
756
757using namespace clang::targets;
758/// CreateTargetInfo - Return the target info object for the specified target
759/// options.
760TargetInfo *
761TargetInfo::CreateTargetInfo(DiagnosticsEngine &Diags,
762 const std::shared_ptr<TargetOptions> &Opts) {
763 llvm::Triple Triple(Opts->Triple);
764
765 // Construct the target
766 std::unique_ptr<TargetInfo> Target = AllocateTarget(Triple, Opts: *Opts);
767 if (!Target) {
768 Diags.Report(diag::err_target_unknown_triple) << Triple.str();
769 return nullptr;
770 }
771 Target->TargetOpts = Opts;
772
773 // Set the target CPU if specified.
774 if (!Opts->CPU.empty() && !Target->setCPU(Opts->CPU)) {
775 Diags.Report(diag::err_target_unknown_cpu) << Opts->CPU;
776 SmallVector<StringRef, 32> ValidList;
777 Target->fillValidCPUList(Values&: ValidList);
778 if (!ValidList.empty())
779 Diags.Report(diag::note_valid_options) << llvm::join(R&: ValidList, Separator: ", ");
780 return nullptr;
781 }
782
783 // Check the TuneCPU name if specified.
784 if (!Opts->TuneCPU.empty() &&
785 !Target->isValidTuneCPUName(Name: Opts->TuneCPU)) {
786 Diags.Report(diag::err_target_unknown_cpu) << Opts->TuneCPU;
787 SmallVector<StringRef, 32> ValidList;
788 Target->fillValidTuneCPUList(Values&: ValidList);
789 if (!ValidList.empty())
790 Diags.Report(diag::note_valid_options) << llvm::join(R&: ValidList, Separator: ", ");
791 return nullptr;
792 }
793
794 // Set the target ABI if specified.
795 if (!Opts->ABI.empty() && !Target->setABI(Opts->ABI)) {
796 Diags.Report(diag::err_target_unknown_abi) << Opts->ABI;
797 return nullptr;
798 }
799
800 // Set the fp math unit.
801 if (!Opts->FPMath.empty() && !Target->setFPMath(Opts->FPMath)) {
802 Diags.Report(diag::err_target_unknown_fpmath) << Opts->FPMath;
803 return nullptr;
804 }
805
806 // Compute the default target features, we need the target to handle this
807 // because features may have dependencies on one another.
808 llvm::erase_if(C&: Opts->FeaturesAsWritten, P: [&](StringRef Name) {
809 if (Target->isReadOnlyFeature(Feature: Name.substr(Start: 1))) {
810 Diags.Report(diag::warn_fe_backend_readonly_feature_flag) << Name;
811 return true;
812 }
813 return false;
814 });
815 if (!Target->initFeatureMap(Features&: Opts->FeatureMap, Diags, CPU: Opts->CPU,
816 FeatureVec: Opts->FeaturesAsWritten))
817 return nullptr;
818
819 // Add the features to the compile options.
820 Opts->Features.clear();
821 for (const auto &F : Opts->FeatureMap)
822 Opts->Features.push_back(x: (F.getValue() ? "+" : "-") + F.getKey().str());
823 // Sort here, so we handle the features in a predictable order. (This matters
824 // when we're dealing with features that overlap.)
825 llvm::sort(C&: Opts->Features);
826
827 if (!Target->handleTargetFeatures(Features&: Opts->Features, Diags))
828 return nullptr;
829
830 Target->setSupportedOpenCLOpts();
831 Target->setCommandLineOpenCLOpts();
832 Target->setMaxAtomicWidth();
833
834 if (!Opts->DarwinTargetVariantTriple.empty())
835 Target->DarwinTargetVariantTriple =
836 llvm::Triple(Opts->DarwinTargetVariantTriple);
837
838 if (!Target->validateTarget(Diags))
839 return nullptr;
840
841 Target->CheckFixedPointBits();
842
843 return Target.release();
844}
845/// validateOpenCLTarget - Check that OpenCL target has valid
846/// options setting based on OpenCL version.
847bool TargetInfo::validateOpenCLTarget(const LangOptions &Opts,
848 DiagnosticsEngine &Diags) const {
849 const llvm::StringMap<bool> &OpenCLFeaturesMap = getSupportedOpenCLOpts();
850
851 auto diagnoseNotSupportedCore = [&](llvm::StringRef Name, auto... OptArgs) {
852 if (OpenCLOptions::isOpenCLOptionCoreIn(Opts, OptArgs...) &&
853 !hasFeatureEnabled(Features: OpenCLFeaturesMap, Name))
854 Diags.Report(diag::warn_opencl_unsupported_core_feature)
855 << Name << Opts.OpenCLCPlusPlus
856 << Opts.getOpenCLVersionTuple().getAsString();
857 };
858#define OPENCL_GENERIC_EXTENSION(Ext, ...) \
859 diagnoseNotSupportedCore(#Ext, __VA_ARGS__);
860#include "clang/Basic/OpenCLExtensions.def"
861
862 // Validate that feature macros are set properly for OpenCL C 3.0.
863 // In other cases assume that target is always valid.
864 if (Opts.getOpenCLCompatibleVersion() < 300)
865 return true;
866
867 return OpenCLOptions::diagnoseUnsupportedFeatureDependencies(TI: *this, Diags) &&
868 OpenCLOptions::diagnoseFeatureExtensionDifferences(TI: *this, Diags);
869}
870

source code of clang/lib/Basic/Targets.cpp