blob: f6f0155773517c77a3037f5a64ab235c8b59a710 [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===-- llvm/ADT/Triple.h - Target triple helper class ----------*- C++ -*-===//
2//
Andrew Walbran16937d02019-10-22 13:54:20 +01003// 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
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01006//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLVM_ADT_TRIPLE_H
10#define LLVM_ADT_TRIPLE_H
11
12#include "llvm/ADT/Twine.h"
13
14// Some system headers or GCC predefined macros conflict with identifiers in
15// this file. Undefine them here.
16#undef NetBSD
17#undef mips
18#undef sparc
19
20namespace llvm {
21
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020022class VersionTuple;
23
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010024/// Triple - Helper class for working with autoconf configuration names. For
25/// historical reasons, we also call these 'triples' (they used to contain
26/// exactly three fields).
27///
28/// Configuration names are strings in the canonical form:
29/// ARCHITECTURE-VENDOR-OPERATING_SYSTEM
30/// or
31/// ARCHITECTURE-VENDOR-OPERATING_SYSTEM-ENVIRONMENT
32///
33/// This class is used for clients which want to support arbitrary
34/// configuration names, but also want to implement certain special
35/// behavior for particular configurations. This class isolates the mapping
36/// from the components of the configuration name to well known IDs.
37///
38/// At its core the Triple class is designed to be a wrapper for a triple
39/// string; the constructor does not change or normalize the triple string.
40/// Clients that need to handle the non-canonical triples that users often
41/// specify should use the normalize method.
42///
43/// See autoconf/config.guess for a glimpse into what configuration names
44/// look like in practice.
45class Triple {
46public:
47 enum ArchType {
48 UnknownArch,
49
50 arm, // ARM (little endian): arm, armv.*, xscale
51 armeb, // ARM (big endian): armeb
52 aarch64, // AArch64 (little endian): aarch64
53 aarch64_be, // AArch64 (big endian): aarch64_be
Andrew Walbran3d2c1972020-04-07 12:24:26 +010054 aarch64_32, // AArch64 (little endian) ILP32: aarch64_32
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010055 arc, // ARC: Synopsys ARC
56 avr, // AVR: Atmel AVR microcontroller
57 bpfel, // eBPF or extended BPF or 64-bit BPF (little endian)
58 bpfeb, // eBPF or extended BPF or 64-bit BPF (big endian)
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020059 csky, // CSKY: csky
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010060 hexagon, // Hexagon: hexagon
Andrew Scull0372a572018-11-16 15:47:06 +000061 mips, // MIPS: mips, mipsallegrex, mipsr6
62 mipsel, // MIPSEL: mipsel, mipsallegrexe, mipsr6el
63 mips64, // MIPS64: mips64, mips64r6, mipsn32, mipsn32r6
64 mips64el, // MIPS64EL: mips64el, mips64r6el, mipsn32el, mipsn32r6el
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010065 msp430, // MSP430: msp430
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010066 ppc, // PPC: powerpc
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020067 ppcle, // PPCLE: powerpc (little endian)
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010068 ppc64, // PPC64: powerpc64, ppu
69 ppc64le, // PPC64LE: powerpc64le
70 r600, // R600: AMD GPUs HD2XXX - HD6XXX
71 amdgcn, // AMDGCN: AMD GCN GPUs
72 riscv32, // RISC-V (32-bit): riscv32
73 riscv64, // RISC-V (64-bit): riscv64
74 sparc, // Sparc: sparc
75 sparcv9, // Sparcv9: Sparcv9
76 sparcel, // Sparc: (endianness = little). NB: 'Sparcle' is a CPU variant
77 systemz, // SystemZ: s390x
78 tce, // TCE (http://tce.cs.tut.fi/): tce
79 tcele, // TCE little endian (http://tce.cs.tut.fi/): tcele
80 thumb, // Thumb (little endian): thumb, thumbv.*
81 thumbeb, // Thumb (big endian): thumbeb
82 x86, // X86: i[3-9]86
83 x86_64, // X86-64: amd64, x86_64
84 xcore, // XCore: xcore
85 nvptx, // NVPTX: 32-bit
86 nvptx64, // NVPTX: 64-bit
87 le32, // le32: generic little-endian 32-bit CPU (PNaCl)
88 le64, // le64: generic little-endian 64-bit CPU (PNaCl)
89 amdil, // AMDIL
90 amdil64, // AMDIL with 64-bit pointers
91 hsail, // AMD HSAIL
92 hsail64, // AMD HSAIL with 64-bit pointers
93 spir, // SPIR: standard portable IR for OpenCL 32-bit version
94 spir64, // SPIR: standard portable IR for OpenCL 64-bit version
95 kalimba, // Kalimba: generic kalimba
96 shave, // SHAVE: Movidius vector VLIW processors
97 lanai, // Lanai: Lanai 32-bit
98 wasm32, // WebAssembly with 32-bit pointers
99 wasm64, // WebAssembly with 64-bit pointers
100 renderscript32, // 32-bit RenderScript
101 renderscript64, // 64-bit RenderScript
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200102 ve, // NEC SX-Aurora Vector Engine
103 LastArchType = ve
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100104 };
105 enum SubArchType {
106 NoSubArch,
107
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200108 ARMSubArch_v8_7a,
109 ARMSubArch_v8_6a,
Andrew Scull0372a572018-11-16 15:47:06 +0000110 ARMSubArch_v8_5a,
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100111 ARMSubArch_v8_4a,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100112 ARMSubArch_v8_3a,
113 ARMSubArch_v8_2a,
114 ARMSubArch_v8_1a,
115 ARMSubArch_v8,
116 ARMSubArch_v8r,
117 ARMSubArch_v8m_baseline,
118 ARMSubArch_v8m_mainline,
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100119 ARMSubArch_v8_1m_mainline,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100120 ARMSubArch_v7,
121 ARMSubArch_v7em,
122 ARMSubArch_v7m,
123 ARMSubArch_v7s,
124 ARMSubArch_v7k,
125 ARMSubArch_v7ve,
126 ARMSubArch_v6,
127 ARMSubArch_v6m,
128 ARMSubArch_v6k,
129 ARMSubArch_v6t2,
130 ARMSubArch_v5,
131 ARMSubArch_v5te,
132 ARMSubArch_v4t,
133
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200134 AArch64SubArch_arm64e,
135
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100136 KalimbaSubArch_v3,
137 KalimbaSubArch_v4,
Andrew Scull0372a572018-11-16 15:47:06 +0000138 KalimbaSubArch_v5,
139
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200140 MipsSubArch_r6,
141
142 PPCSubArch_spe
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100143 };
144 enum VendorType {
145 UnknownVendor,
146
147 Apple,
148 PC,
149 SCEI,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100150 Freescale,
151 IBM,
152 ImaginationTechnologies,
153 MipsTechnologies,
154 NVIDIA,
155 CSR,
156 Myriad,
157 AMD,
158 Mesa,
159 SUSE,
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100160 OpenEmbedded,
161 LastVendorType = OpenEmbedded
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100162 };
163 enum OSType {
164 UnknownOS,
165
166 Ananas,
167 CloudABI,
168 Darwin,
169 DragonFly,
170 FreeBSD,
171 Fuchsia,
172 IOS,
173 KFreeBSD,
174 Linux,
175 Lv2, // PS3
176 MacOSX,
177 NetBSD,
178 OpenBSD,
179 Solaris,
180 Win32,
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200181 ZOS,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100182 Haiku,
183 Minix,
184 RTEMS,
185 NaCl, // Native Client
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100186 AIX,
187 CUDA, // NVIDIA CUDA
188 NVCL, // NVIDIA OpenCL
189 AMDHSA, // AMD HSA Runtime
190 PS4,
191 ELFIAMCU,
192 TvOS, // Apple tvOS
193 WatchOS, // Apple watchOS
194 Mesa3D,
195 Contiki,
196 AMDPAL, // AMD PAL Runtime
Andrew Scull0372a572018-11-16 15:47:06 +0000197 HermitCore, // HermitCore Unikernel/Multikernel
Andrew Walbran16937d02019-10-22 13:54:20 +0100198 Hurd, // GNU/Hurd
199 WASI, // Experimental WebAssembly OS
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100200 Emscripten,
201 LastOSType = Emscripten
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100202 };
203 enum EnvironmentType {
204 UnknownEnvironment,
205
206 GNU,
207 GNUABIN32,
208 GNUABI64,
209 GNUEABI,
210 GNUEABIHF,
211 GNUX32,
212 CODE16,
213 EABI,
214 EABIHF,
215 Android,
216 Musl,
217 MuslEABI,
218 MuslEABIHF,
219
220 MSVC,
221 Itanium,
222 Cygnus,
223 CoreCLR,
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100224 Simulator, // Simulator variants of other systems, e.g., Apple's iOS
225 MacABI, // Mac Catalyst variant of Apple's iOS deployment target.
226 LastEnvironmentType = MacABI
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100227 };
228 enum ObjectFormatType {
229 UnknownObjectFormat,
230
231 COFF,
232 ELF,
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200233 GOFF,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100234 MachO,
235 Wasm,
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100236 XCOFF,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100237 };
238
239private:
240 std::string Data;
241
242 /// The parsed arch type.
243 ArchType Arch;
244
245 /// The parsed subarchitecture type.
246 SubArchType SubArch;
247
248 /// The parsed vendor type.
249 VendorType Vendor;
250
251 /// The parsed OS type.
252 OSType OS;
253
254 /// The parsed Environment type.
255 EnvironmentType Environment;
256
257 /// The object format type.
258 ObjectFormatType ObjectFormat;
259
260public:
261 /// @name Constructors
262 /// @{
263
264 /// Default constructor is the same as an empty string and leaves all
265 /// triple fields unknown.
266 Triple()
267 : Data(), Arch(), SubArch(), Vendor(), OS(), Environment(),
268 ObjectFormat() {}
269
270 explicit Triple(const Twine &Str);
271 Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr);
272 Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
273 const Twine &EnvironmentStr);
274
275 bool operator==(const Triple &Other) const {
276 return Arch == Other.Arch && SubArch == Other.SubArch &&
277 Vendor == Other.Vendor && OS == Other.OS &&
278 Environment == Other.Environment &&
279 ObjectFormat == Other.ObjectFormat;
280 }
281
282 bool operator!=(const Triple &Other) const {
283 return !(*this == Other);
284 }
285
286 /// @}
287 /// @name Normalization
288 /// @{
289
290 /// normalize - Turn an arbitrary machine specification into the canonical
291 /// triple form (or something sensible that the Triple class understands if
292 /// nothing better can reasonably be done). In particular, it handles the
293 /// common case in which otherwise valid components are in the wrong order.
294 static std::string normalize(StringRef Str);
295
296 /// Return the normalized form of this triple's string.
297 std::string normalize() const { return normalize(Data); }
298
299 /// @}
300 /// @name Typed Component Access
301 /// @{
302
303 /// getArch - Get the parsed architecture type of this triple.
304 ArchType getArch() const { return Arch; }
305
306 /// getSubArch - get the parsed subarchitecture type for this triple.
307 SubArchType getSubArch() const { return SubArch; }
308
309 /// getVendor - Get the parsed vendor type of this triple.
310 VendorType getVendor() const { return Vendor; }
311
312 /// getOS - Get the parsed operating system type of this triple.
313 OSType getOS() const { return OS; }
314
315 /// hasEnvironment - Does this triple have the optional environment
316 /// (fourth) component?
317 bool hasEnvironment() const {
318 return getEnvironmentName() != "";
319 }
320
321 /// getEnvironment - Get the parsed environment type of this triple.
322 EnvironmentType getEnvironment() const { return Environment; }
323
324 /// Parse the version number from the OS name component of the
325 /// triple, if present.
326 ///
327 /// For example, "fooos1.2.3" would return (1, 2, 3).
328 ///
329 /// If an entry is not defined, it will be returned as 0.
330 void getEnvironmentVersion(unsigned &Major, unsigned &Minor,
331 unsigned &Micro) const;
332
333 /// getFormat - Get the object format for this triple.
334 ObjectFormatType getObjectFormat() const { return ObjectFormat; }
335
336 /// getOSVersion - Parse the version number from the OS name component of the
337 /// triple, if present.
338 ///
339 /// For example, "fooos1.2.3" would return (1, 2, 3).
340 ///
341 /// If an entry is not defined, it will be returned as 0.
342 void getOSVersion(unsigned &Major, unsigned &Minor, unsigned &Micro) const;
343
344 /// getOSMajorVersion - Return just the major version number, this is
345 /// specialized because it is a common query.
346 unsigned getOSMajorVersion() const {
347 unsigned Maj, Min, Micro;
348 getOSVersion(Maj, Min, Micro);
349 return Maj;
350 }
351
352 /// getMacOSXVersion - Parse the version number as with getOSVersion and then
353 /// translate generic "darwin" versions to the corresponding OS X versions.
354 /// This may also be called with IOS triples but the OS X version number is
355 /// just set to a constant 10.4.0 in that case. Returns true if successful.
356 bool getMacOSXVersion(unsigned &Major, unsigned &Minor,
357 unsigned &Micro) const;
358
359 /// getiOSVersion - Parse the version number as with getOSVersion. This should
360 /// only be called with IOS or generic triples.
361 void getiOSVersion(unsigned &Major, unsigned &Minor,
362 unsigned &Micro) const;
363
364 /// getWatchOSVersion - Parse the version number as with getOSVersion. This
365 /// should only be called with WatchOS or generic triples.
366 void getWatchOSVersion(unsigned &Major, unsigned &Minor,
367 unsigned &Micro) const;
368
369 /// @}
370 /// @name Direct Component Access
371 /// @{
372
373 const std::string &str() const { return Data; }
374
375 const std::string &getTriple() const { return Data; }
376
377 /// getArchName - Get the architecture (first) component of the
378 /// triple.
379 StringRef getArchName() const;
380
381 /// getVendorName - Get the vendor (second) component of the triple.
382 StringRef getVendorName() const;
383
384 /// getOSName - Get the operating system (third) component of the
385 /// triple.
386 StringRef getOSName() const;
387
388 /// getEnvironmentName - Get the optional environment (fourth)
389 /// component of the triple, or "" if empty.
390 StringRef getEnvironmentName() const;
391
392 /// getOSAndEnvironmentName - Get the operating system and optional
393 /// environment components as a single string (separated by a '-'
394 /// if the environment component is present).
395 StringRef getOSAndEnvironmentName() const;
396
397 /// @}
398 /// @name Convenience Predicates
399 /// @{
400
401 /// Test whether the architecture is 64-bit
402 ///
403 /// Note that this tests for 64-bit pointer width, and nothing else. Note
404 /// that we intentionally expose only three predicates, 64-bit, 32-bit, and
405 /// 16-bit. The inner details of pointer width for particular architectures
406 /// is not summed up in the triple, and so only a coarse grained predicate
407 /// system is provided.
408 bool isArch64Bit() const;
409
410 /// Test whether the architecture is 32-bit
411 ///
412 /// Note that this tests for 32-bit pointer width, and nothing else.
413 bool isArch32Bit() const;
414
415 /// Test whether the architecture is 16-bit
416 ///
417 /// Note that this tests for 16-bit pointer width, and nothing else.
418 bool isArch16Bit() const;
419
420 /// isOSVersionLT - Helper function for doing comparisons against version
421 /// numbers included in the target triple.
422 bool isOSVersionLT(unsigned Major, unsigned Minor = 0,
423 unsigned Micro = 0) const {
424 unsigned LHS[3];
425 getOSVersion(LHS[0], LHS[1], LHS[2]);
426
427 if (LHS[0] != Major)
428 return LHS[0] < Major;
429 if (LHS[1] != Minor)
430 return LHS[1] < Minor;
431 if (LHS[2] != Micro)
Andrew Walbran16937d02019-10-22 13:54:20 +0100432 return LHS[2] < Micro;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100433
434 return false;
435 }
436
437 bool isOSVersionLT(const Triple &Other) const {
438 unsigned RHS[3];
439 Other.getOSVersion(RHS[0], RHS[1], RHS[2]);
440 return isOSVersionLT(RHS[0], RHS[1], RHS[2]);
441 }
442
443 /// isMacOSXVersionLT - Comparison function for checking OS X version
444 /// compatibility, which handles supporting skewed version numbering schemes
445 /// used by the "darwin" triples.
446 bool isMacOSXVersionLT(unsigned Major, unsigned Minor = 0,
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200447 unsigned Micro = 0) const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100448
449 /// isMacOSX - Is this a Mac OS X triple. For legacy reasons, we support both
450 /// "darwin" and "osx" as OS X triples.
451 bool isMacOSX() const {
452 return getOS() == Triple::Darwin || getOS() == Triple::MacOSX;
453 }
454
455 /// Is this an iOS triple.
456 /// Note: This identifies tvOS as a variant of iOS. If that ever
457 /// changes, i.e., if the two operating systems diverge or their version
458 /// numbers get out of sync, that will need to be changed.
459 /// watchOS has completely different version numbers so it is not included.
460 bool isiOS() const {
461 return getOS() == Triple::IOS || isTvOS();
462 }
463
464 /// Is this an Apple tvOS triple.
465 bool isTvOS() const {
466 return getOS() == Triple::TvOS;
467 }
468
469 /// Is this an Apple watchOS triple.
470 bool isWatchOS() const {
471 return getOS() == Triple::WatchOS;
472 }
473
474 bool isWatchABI() const {
475 return getSubArch() == Triple::ARMSubArch_v7k;
476 }
477
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200478 bool isOSzOS() const { return getOS() == Triple::ZOS; }
479
480 /// isOSDarwin - Is this a "Darwin" OS (macOS, iOS, tvOS or watchOS).
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100481 bool isOSDarwin() const {
482 return isMacOSX() || isiOS() || isWatchOS();
483 }
484
485 bool isSimulatorEnvironment() const {
486 return getEnvironment() == Triple::Simulator;
487 }
488
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100489 bool isMacCatalystEnvironment() const {
490 return getEnvironment() == Triple::MacABI;
491 }
492
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200493 /// Returns true for targets that run on a macOS machine.
494 bool isTargetMachineMac() const {
495 return isMacOSX() || (isOSDarwin() && (isSimulatorEnvironment() ||
496 isMacCatalystEnvironment()));
497 }
498
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100499 bool isOSNetBSD() const {
500 return getOS() == Triple::NetBSD;
501 }
502
503 bool isOSOpenBSD() const {
504 return getOS() == Triple::OpenBSD;
505 }
506
507 bool isOSFreeBSD() const {
508 return getOS() == Triple::FreeBSD;
509 }
510
511 bool isOSFuchsia() const {
512 return getOS() == Triple::Fuchsia;
513 }
514
515 bool isOSDragonFly() const { return getOS() == Triple::DragonFly; }
516
517 bool isOSSolaris() const {
518 return getOS() == Triple::Solaris;
519 }
520
521 bool isOSIAMCU() const {
522 return getOS() == Triple::ELFIAMCU;
523 }
524
525 bool isOSUnknown() const { return getOS() == Triple::UnknownOS; }
526
527 bool isGNUEnvironment() const {
528 EnvironmentType Env = getEnvironment();
529 return Env == Triple::GNU || Env == Triple::GNUABIN32 ||
530 Env == Triple::GNUABI64 || Env == Triple::GNUEABI ||
531 Env == Triple::GNUEABIHF || Env == Triple::GNUX32;
532 }
533
534 bool isOSContiki() const {
535 return getOS() == Triple::Contiki;
536 }
537
538 /// Tests whether the OS is Haiku.
539 bool isOSHaiku() const {
540 return getOS() == Triple::Haiku;
541 }
542
Andrew Walbran16937d02019-10-22 13:54:20 +0100543 /// Tests whether the OS is Windows.
544 bool isOSWindows() const {
545 return getOS() == Triple::Win32;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100546 }
547
548 /// Checks if the environment is MSVC.
549 bool isKnownWindowsMSVCEnvironment() const {
Andrew Walbran16937d02019-10-22 13:54:20 +0100550 return isOSWindows() && getEnvironment() == Triple::MSVC;
551 }
552
553 /// Checks if the environment could be MSVC.
554 bool isWindowsMSVCEnvironment() const {
555 return isKnownWindowsMSVCEnvironment() ||
556 (isOSWindows() && getEnvironment() == Triple::UnknownEnvironment);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100557 }
558
559 bool isWindowsCoreCLREnvironment() const {
Andrew Walbran16937d02019-10-22 13:54:20 +0100560 return isOSWindows() && getEnvironment() == Triple::CoreCLR;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100561 }
562
563 bool isWindowsItaniumEnvironment() const {
Andrew Walbran16937d02019-10-22 13:54:20 +0100564 return isOSWindows() && getEnvironment() == Triple::Itanium;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100565 }
566
567 bool isWindowsCygwinEnvironment() const {
Andrew Walbran16937d02019-10-22 13:54:20 +0100568 return isOSWindows() && getEnvironment() == Triple::Cygnus;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100569 }
570
571 bool isWindowsGNUEnvironment() const {
Andrew Walbran16937d02019-10-22 13:54:20 +0100572 return isOSWindows() && getEnvironment() == Triple::GNU;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100573 }
574
575 /// Tests for either Cygwin or MinGW OS
576 bool isOSCygMing() const {
577 return isWindowsCygwinEnvironment() || isWindowsGNUEnvironment();
578 }
579
580 /// Is this a "Windows" OS targeting a "MSVCRT.dll" environment.
581 bool isOSMSVCRT() const {
582 return isWindowsMSVCEnvironment() || isWindowsGNUEnvironment() ||
583 isWindowsItaniumEnvironment();
584 }
585
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100586 /// Tests whether the OS is NaCl (Native Client)
587 bool isOSNaCl() const {
588 return getOS() == Triple::NaCl;
589 }
590
591 /// Tests whether the OS is Linux.
592 bool isOSLinux() const {
593 return getOS() == Triple::Linux;
594 }
595
596 /// Tests whether the OS is kFreeBSD.
597 bool isOSKFreeBSD() const {
598 return getOS() == Triple::KFreeBSD;
599 }
600
Andrew Walbran16937d02019-10-22 13:54:20 +0100601 /// Tests whether the OS is Hurd.
602 bool isOSHurd() const {
603 return getOS() == Triple::Hurd;
604 }
605
606 /// Tests whether the OS is WASI.
607 bool isOSWASI() const {
608 return getOS() == Triple::WASI;
609 }
610
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100611 /// Tests whether the OS is Emscripten.
612 bool isOSEmscripten() const {
613 return getOS() == Triple::Emscripten;
614 }
615
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100616 /// Tests whether the OS uses glibc.
617 bool isOSGlibc() const {
Andrew Walbran16937d02019-10-22 13:54:20 +0100618 return (getOS() == Triple::Linux || getOS() == Triple::KFreeBSD ||
619 getOS() == Triple::Hurd) &&
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100620 !isAndroid();
621 }
622
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100623 /// Tests whether the OS is AIX.
624 bool isOSAIX() const {
625 return getOS() == Triple::AIX;
626 }
627
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100628 /// Tests whether the OS uses the ELF binary format.
629 bool isOSBinFormatELF() const {
630 return getObjectFormat() == Triple::ELF;
631 }
632
633 /// Tests whether the OS uses the COFF binary format.
634 bool isOSBinFormatCOFF() const {
635 return getObjectFormat() == Triple::COFF;
636 }
637
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200638 /// Tests whether the OS uses the GOFF binary format.
639 bool isOSBinFormatGOFF() const { return getObjectFormat() == Triple::GOFF; }
640
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100641 /// Tests whether the environment is MachO.
642 bool isOSBinFormatMachO() const {
643 return getObjectFormat() == Triple::MachO;
644 }
645
646 /// Tests whether the OS uses the Wasm binary format.
647 bool isOSBinFormatWasm() const {
648 return getObjectFormat() == Triple::Wasm;
649 }
650
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100651 /// Tests whether the OS uses the XCOFF binary format.
652 bool isOSBinFormatXCOFF() const {
653 return getObjectFormat() == Triple::XCOFF;
654 }
655
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100656 /// Tests whether the target is the PS4 CPU
657 bool isPS4CPU() const {
658 return getArch() == Triple::x86_64 &&
659 getVendor() == Triple::SCEI &&
660 getOS() == Triple::PS4;
661 }
662
663 /// Tests whether the target is the PS4 platform
664 bool isPS4() const {
665 return getVendor() == Triple::SCEI &&
666 getOS() == Triple::PS4;
667 }
668
669 /// Tests whether the target is Android
670 bool isAndroid() const { return getEnvironment() == Triple::Android; }
671
672 bool isAndroidVersionLT(unsigned Major) const {
673 assert(isAndroid() && "Not an Android triple!");
674
675 unsigned Env[3];
676 getEnvironmentVersion(Env[0], Env[1], Env[2]);
677
678 // 64-bit targets did not exist before API level 21 (Lollipop).
679 if (isArch64Bit() && Env[0] < 21)
680 Env[0] = 21;
681
682 return Env[0] < Major;
683 }
684
685 /// Tests whether the environment is musl-libc
686 bool isMusl() const {
687 return getEnvironment() == Triple::Musl ||
688 getEnvironment() == Triple::MuslEABI ||
689 getEnvironment() == Triple::MuslEABIHF;
690 }
691
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100692 /// Tests whether the target is SPIR (32- or 64-bit).
693 bool isSPIR() const {
694 return getArch() == Triple::spir || getArch() == Triple::spir64;
695 }
696
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100697 /// Tests whether the target is NVPTX (32- or 64-bit).
698 bool isNVPTX() const {
699 return getArch() == Triple::nvptx || getArch() == Triple::nvptx64;
700 }
701
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200702 /// Tests whether the target is AMDGCN
703 bool isAMDGCN() const { return getArch() == Triple::amdgcn; }
704
705 bool isAMDGPU() const {
706 return getArch() == Triple::r600 || getArch() == Triple::amdgcn;
707 }
708
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100709 /// Tests whether the target is Thumb (little and big endian).
710 bool isThumb() const {
711 return getArch() == Triple::thumb || getArch() == Triple::thumbeb;
712 }
713
714 /// Tests whether the target is ARM (little and big endian).
715 bool isARM() const {
716 return getArch() == Triple::arm || getArch() == Triple::armeb;
717 }
718
719 /// Tests whether the target is AArch64 (little and big endian).
720 bool isAArch64() const {
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200721 return getArch() == Triple::aarch64 || getArch() == Triple::aarch64_be ||
722 getArch() == Triple::aarch64_32;
723 }
724
725 /// Tests whether the target is AArch64 and pointers are the size specified by
726 /// \p PointerWidth.
727 bool isAArch64(int PointerWidth) const {
728 assert(PointerWidth == 64 || PointerWidth == 32);
729 if (!isAArch64())
730 return false;
731 return isArch64Bit() ? PointerWidth == 64 : PointerWidth == 32;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100732 }
733
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100734 /// Tests whether the target is MIPS 32-bit (little and big endian).
735 bool isMIPS32() const {
736 return getArch() == Triple::mips || getArch() == Triple::mipsel;
737 }
738
739 /// Tests whether the target is MIPS 64-bit (little and big endian).
740 bool isMIPS64() const {
741 return getArch() == Triple::mips64 || getArch() == Triple::mips64el;
742 }
743
744 /// Tests whether the target is MIPS (little and big endian, 32- or 64-bit).
745 bool isMIPS() const {
746 return isMIPS32() || isMIPS64();
747 }
748
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200749 /// Tests whether the target is PowerPC (32- or 64-bit LE or BE).
750 bool isPPC() const {
751 return getArch() == Triple::ppc || getArch() == Triple::ppc64 ||
752 getArch() == Triple::ppcle || getArch() == Triple::ppc64le;
753 }
754
755 /// Tests whether the target is 32-bit PowerPC (little and big endian).
756 bool isPPC32() const {
757 return getArch() == Triple::ppc || getArch() == Triple::ppcle;
758 }
759
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100760 /// Tests whether the target is 64-bit PowerPC (little and big endian).
761 bool isPPC64() const {
762 return getArch() == Triple::ppc64 || getArch() == Triple::ppc64le;
763 }
764
765 /// Tests whether the target is RISC-V (32- and 64-bit).
766 bool isRISCV() const {
767 return getArch() == Triple::riscv32 || getArch() == Triple::riscv64;
768 }
769
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200770 /// Tests whether the target is SystemZ.
771 bool isSystemZ() const {
772 return getArch() == Triple::systemz;
773 }
774
775 /// Tests whether the target is x86 (32- or 64-bit).
776 bool isX86() const {
777 return getArch() == Triple::x86 || getArch() == Triple::x86_64;
778 }
779
780 /// Tests whether the target is VE
781 bool isVE() const {
782 return getArch() == Triple::ve;
783 }
784
785 /// Tests whether the target is wasm (32- and 64-bit).
786 bool isWasm() const {
787 return getArch() == Triple::wasm32 || getArch() == Triple::wasm64;
788 }
789
790 // Tests whether the target is CSKY
791 bool isCSKY() const {
792 return getArch() == Triple::csky;
793 }
794
795 /// Tests whether the target is the Apple "arm64e" AArch64 subarch.
796 bool isArm64e() const {
797 return getArch() == Triple::aarch64 &&
798 getSubArch() == Triple::AArch64SubArch_arm64e;
799 }
800
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100801 /// Tests whether the target supports comdat
802 bool supportsCOMDAT() const {
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200803 return !(isOSBinFormatMachO() || isOSBinFormatXCOFF());
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100804 }
805
806 /// Tests whether the target uses emulated TLS as default.
807 bool hasDefaultEmulatedTLS() const {
808 return isAndroid() || isOSOpenBSD() || isWindowsCygwinEnvironment();
809 }
810
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200811 /// Tests whether the target uses -data-sections as default.
812 bool hasDefaultDataSections() const {
813 return isOSBinFormatXCOFF() || isWasm();
814 }
815
816 /// Tests if the environment supports dllimport/export annotations.
817 bool hasDLLImportExport() const { return isOSWindows() || isPS4CPU(); }
818
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100819 /// @}
820 /// @name Mutators
821 /// @{
822
823 /// setArch - Set the architecture (first) component of the triple
824 /// to a known type.
825 void setArch(ArchType Kind);
826
827 /// setVendor - Set the vendor (second) component of the triple to a
828 /// known type.
829 void setVendor(VendorType Kind);
830
831 /// setOS - Set the operating system (third) component of the triple
832 /// to a known type.
833 void setOS(OSType Kind);
834
835 /// setEnvironment - Set the environment (fourth) component of the triple
836 /// to a known type.
837 void setEnvironment(EnvironmentType Kind);
838
839 /// setObjectFormat - Set the object file format
840 void setObjectFormat(ObjectFormatType Kind);
841
842 /// setTriple - Set all components to the new triple \p Str.
843 void setTriple(const Twine &Str);
844
845 /// setArchName - Set the architecture (first) component of the
846 /// triple by name.
847 void setArchName(StringRef Str);
848
849 /// setVendorName - Set the vendor (second) component of the triple
850 /// by name.
851 void setVendorName(StringRef Str);
852
853 /// setOSName - Set the operating system (third) component of the
854 /// triple by name.
855 void setOSName(StringRef Str);
856
857 /// setEnvironmentName - Set the optional environment (fourth)
858 /// component of the triple by name.
859 void setEnvironmentName(StringRef Str);
860
861 /// setOSAndEnvironmentName - Set the operating system and optional
862 /// environment components with a single string.
863 void setOSAndEnvironmentName(StringRef Str);
864
865 /// @}
866 /// @name Helpers to build variants of a particular triple.
867 /// @{
868
869 /// Form a triple with a 32-bit variant of the current architecture.
870 ///
871 /// This can be used to move across "families" of architectures where useful.
872 ///
873 /// \returns A new triple with a 32-bit architecture or an unknown
874 /// architecture if no such variant can be found.
875 llvm::Triple get32BitArchVariant() const;
876
877 /// Form a triple with a 64-bit variant of the current architecture.
878 ///
879 /// This can be used to move across "families" of architectures where useful.
880 ///
881 /// \returns A new triple with a 64-bit architecture or an unknown
882 /// architecture if no such variant can be found.
883 llvm::Triple get64BitArchVariant() const;
884
885 /// Form a triple with a big endian variant of the current architecture.
886 ///
887 /// This can be used to move across "families" of architectures where useful.
888 ///
889 /// \returns A new triple with a big endian architecture or an unknown
890 /// architecture if no such variant can be found.
891 llvm::Triple getBigEndianArchVariant() const;
892
893 /// Form a triple with a little endian variant of the current architecture.
894 ///
895 /// This can be used to move across "families" of architectures where useful.
896 ///
897 /// \returns A new triple with a little endian architecture or an unknown
898 /// architecture if no such variant can be found.
899 llvm::Triple getLittleEndianArchVariant() const;
900
901 /// Get the (LLVM) name of the minimum ARM CPU for the arch we are targeting.
902 ///
903 /// \param Arch the architecture name (e.g., "armv7s"). If it is an empty
904 /// string then the triple's arch name is used.
905 StringRef getARMCPUForArch(StringRef Arch = StringRef()) const;
906
907 /// Tests whether the target triple is little endian.
908 ///
909 /// \returns true if the triple is little endian, false otherwise.
910 bool isLittleEndian() const;
911
912 /// Test whether target triples are compatible.
913 bool isCompatibleWith(const Triple &Other) const;
914
915 /// Merge target triples.
916 std::string merge(const Triple &Other) const;
917
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200918 /// Some platforms have different minimum supported OS versions that
919 /// varies by the architecture specified in the triple. This function
920 /// returns the minimum supported OS version for this triple if one an exists,
921 /// or an invalid version tuple if this triple doesn't have one.
922 VersionTuple getMinimumSupportedOSVersion() const;
923
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100924 /// @}
925 /// @name Static helpers for IDs.
926 /// @{
927
928 /// getArchTypeName - Get the canonical name for the \p Kind architecture.
929 static StringRef getArchTypeName(ArchType Kind);
930
931 /// getArchTypePrefix - Get the "prefix" canonical name for the \p Kind
932 /// architecture. This is the prefix used by the architecture specific
933 /// builtins, and is suitable for passing to \see
934 /// Intrinsic::getIntrinsicForGCCBuiltin().
935 ///
936 /// \return - The architecture prefix, or 0 if none is defined.
937 static StringRef getArchTypePrefix(ArchType Kind);
938
939 /// getVendorTypeName - Get the canonical name for the \p Kind vendor.
940 static StringRef getVendorTypeName(VendorType Kind);
941
942 /// getOSTypeName - Get the canonical name for the \p Kind operating system.
943 static StringRef getOSTypeName(OSType Kind);
944
945 /// getEnvironmentTypeName - Get the canonical name for the \p Kind
946 /// environment.
947 static StringRef getEnvironmentTypeName(EnvironmentType Kind);
948
949 /// @}
950 /// @name Static helpers for converting alternate architecture names.
951 /// @{
952
953 /// getArchTypeForLLVMName - The canonical type for the given LLVM
954 /// architecture name (e.g., "x86").
955 static ArchType getArchTypeForLLVMName(StringRef Str);
956
957 /// @}
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200958
959 /// Returns a canonicalized OS version number for the specified OS.
960 static VersionTuple getCanonicalVersionForOS(OSType OSKind,
961 const VersionTuple &Version);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100962};
963
964} // End llvm namespace
965
966
967#endif