1 //===-- llvm/TargetParser/Triple.h - Target triple helper class--*- C++ -*-===//
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 #ifndef LLVM_TARGETPARSER_TRIPLE_H
10 #define LLVM_TARGETPARSER_TRIPLE_H
11 
12 #include "llvm/ADT/Twine.h"
13 #include "llvm/Support/VersionTuple.h"
14 
15 // Some system headers or GCC predefined macros conflict with identifiers in
16 // this file.  Undefine them here.
17 #undef NetBSD
18 #undef mips
19 #undef sparc
20 
21 namespace llvm {
22 
23 /// Triple - Helper class for working with autoconf configuration names. For
24 /// historical reasons, we also call these 'triples' (they used to contain
25 /// exactly three fields).
26 ///
27 /// Configuration names are strings in the canonical form:
28 ///   ARCHITECTURE-VENDOR-OPERATING_SYSTEM
29 /// or
30 ///   ARCHITECTURE-VENDOR-OPERATING_SYSTEM-ENVIRONMENT
31 ///
32 /// This class is used for clients which want to support arbitrary
33 /// configuration names, but also want to implement certain special
34 /// behavior for particular configurations. This class isolates the mapping
35 /// from the components of the configuration name to well known IDs.
36 ///
37 /// At its core the Triple class is designed to be a wrapper for a triple
38 /// string; the constructor does not change or normalize the triple string.
39 /// Clients that need to handle the non-canonical triples that users often
40 /// specify should use the normalize method.
41 ///
42 /// See autoconf/config.guess for a glimpse into what configuration names
43 /// look like in practice.
44 class Triple {
45 public:
46   enum ArchType {
47     UnknownArch,
48 
49     arm,            // ARM (little endian): arm, armv.*, xscale
50     armeb,          // ARM (big endian): armeb
51     aarch64,        // AArch64 (little endian): aarch64
52     aarch64_be,     // AArch64 (big endian): aarch64_be
53     aarch64_32,     // AArch64 (little endian) ILP32: aarch64_32
54     arc,            // ARC: Synopsys ARC
55     avr,            // AVR: Atmel AVR microcontroller
56     bpfel,          // eBPF or extended BPF or 64-bit BPF (little endian)
57     bpfeb,          // eBPF or extended BPF or 64-bit BPF (big endian)
58     csky,           // CSKY: csky
59     dxil,           // DXIL 32-bit DirectX bytecode
60     hexagon,        // Hexagon: hexagon
61     loongarch32,    // LoongArch (32-bit): loongarch32
62     loongarch64,    // LoongArch (64-bit): loongarch64
63     m68k,           // M68k: Motorola 680x0 family
64     mips,           // MIPS: mips, mipsallegrex, mipsr6
65     mipsel,         // MIPSEL: mipsel, mipsallegrexe, mipsr6el
66     mips64,         // MIPS64: mips64, mips64r6, mipsn32, mipsn32r6
67     mips64el,       // MIPS64EL: mips64el, mips64r6el, mipsn32el, mipsn32r6el
68     msp430,         // MSP430: msp430
69     ppc,            // PPC: powerpc
70     ppcle,          // PPCLE: powerpc (little endian)
71     ppc64,          // PPC64: powerpc64, ppu
72     ppc64le,        // PPC64LE: powerpc64le
73     r600,           // R600: AMD GPUs HD2XXX - HD6XXX
74     amdgcn,         // AMDGCN: AMD GCN GPUs
75     riscv32,        // RISC-V (32-bit): riscv32
76     riscv64,        // RISC-V (64-bit): riscv64
77     sparc,          // Sparc: sparc
78     sparcv9,        // Sparcv9: Sparcv9
79     sparcel,        // Sparc: (endianness = little). NB: 'Sparcle' is a CPU variant
80     systemz,        // SystemZ: s390x
81     tce,            // TCE (http://tce.cs.tut.fi/): tce
82     tcele,          // TCE little endian (http://tce.cs.tut.fi/): tcele
83     thumb,          // Thumb (little endian): thumb, thumbv.*
84     thumbeb,        // Thumb (big endian): thumbeb
85     x86,            // X86: i[3-9]86
86     x86_64,         // X86-64: amd64, x86_64
87     xcore,          // XCore: xcore
88     xtensa,         // Tensilica: Xtensa
89     nvptx,          // NVPTX: 32-bit
90     nvptx64,        // NVPTX: 64-bit
91     le32,           // le32: generic little-endian 32-bit CPU (PNaCl)
92     le64,           // le64: generic little-endian 64-bit CPU (PNaCl)
93     amdil,          // AMDIL
94     amdil64,        // AMDIL with 64-bit pointers
95     hsail,          // AMD HSAIL
96     hsail64,        // AMD HSAIL with 64-bit pointers
97     spir,           // SPIR: standard portable IR for OpenCL 32-bit version
98     spir64,         // SPIR: standard portable IR for OpenCL 64-bit version
99     spirv,          // SPIR-V with logical memory layout.
100     spirv32,        // SPIR-V with 32-bit pointers
101     spirv64,        // SPIR-V with 64-bit pointers
102     kalimba,        // Kalimba: generic kalimba
103     shave,          // SHAVE: Movidius vector VLIW processors
104     lanai,          // Lanai: Lanai 32-bit
105     wasm32,         // WebAssembly with 32-bit pointers
106     wasm64,         // WebAssembly with 64-bit pointers
107     renderscript32, // 32-bit RenderScript
108     renderscript64, // 64-bit RenderScript
109     ve,             // NEC SX-Aurora Vector Engine
110     LastArchType = ve
111   };
112   enum SubArchType {
113     NoSubArch,
114 
115     ARMSubArch_v9_5a,
116     ARMSubArch_v9_4a,
117     ARMSubArch_v9_3a,
118     ARMSubArch_v9_2a,
119     ARMSubArch_v9_1a,
120     ARMSubArch_v9,
121     ARMSubArch_v8_9a,
122     ARMSubArch_v8_8a,
123     ARMSubArch_v8_7a,
124     ARMSubArch_v8_6a,
125     ARMSubArch_v8_5a,
126     ARMSubArch_v8_4a,
127     ARMSubArch_v8_3a,
128     ARMSubArch_v8_2a,
129     ARMSubArch_v8_1a,
130     ARMSubArch_v8,
131     ARMSubArch_v8r,
132     ARMSubArch_v8m_baseline,
133     ARMSubArch_v8m_mainline,
134     ARMSubArch_v8_1m_mainline,
135     ARMSubArch_v7,
136     ARMSubArch_v7em,
137     ARMSubArch_v7m,
138     ARMSubArch_v7s,
139     ARMSubArch_v7k,
140     ARMSubArch_v7ve,
141     ARMSubArch_v6,
142     ARMSubArch_v6m,
143     ARMSubArch_v6k,
144     ARMSubArch_v6t2,
145     ARMSubArch_v5,
146     ARMSubArch_v5te,
147     ARMSubArch_v4t,
148 
149     AArch64SubArch_arm64e,
150     AArch64SubArch_arm64ec,
151 
152     KalimbaSubArch_v3,
153     KalimbaSubArch_v4,
154     KalimbaSubArch_v5,
155 
156     MipsSubArch_r6,
157 
158     PPCSubArch_spe,
159 
160     // SPIR-V sub-arch corresponds to its version.
161     SPIRVSubArch_v10,
162     SPIRVSubArch_v11,
163     SPIRVSubArch_v12,
164     SPIRVSubArch_v13,
165     SPIRVSubArch_v14,
166     SPIRVSubArch_v15,
167   };
168   enum VendorType {
169     UnknownVendor,
170 
171     Apple,
172     PC,
173     SCEI,
174     Freescale,
175     IBM,
176     ImaginationTechnologies,
177     MipsTechnologies,
178     NVIDIA,
179     CSR,
180     AMD,
181     Mesa,
182     SUSE,
183     OpenEmbedded,
184     LastVendorType = OpenEmbedded
185   };
186   enum OSType {
187     UnknownOS,
188 
189     Darwin,
190     DragonFly,
191     FreeBSD,
192     Fuchsia,
193     IOS,
194     KFreeBSD,
195     Linux,
196     Lv2,        // PS3
197     MacOSX,
198     NetBSD,
199     OpenBSD,
200     Solaris,
201     UEFI,
202     Win32,
203     ZOS,
204     Haiku,
205     RTEMS,
206     NaCl,       // Native Client
207     AIX,
208     CUDA,       // NVIDIA CUDA
209     NVCL,       // NVIDIA OpenCL
210     AMDHSA,     // AMD HSA Runtime
211     PS4,
212     PS5,
213     ELFIAMCU,
214     TvOS,       // Apple tvOS
215     WatchOS,    // Apple watchOS
216     DriverKit,  // Apple DriverKit
217     Mesa3D,
218     AMDPAL,     // AMD PAL Runtime
219     HermitCore, // HermitCore Unikernel/Multikernel
220     Hurd,       // GNU/Hurd
221     WASI,       // Experimental WebAssembly OS
222     Emscripten,
223     ShaderModel, // DirectX ShaderModel
224     LiteOS,
225     Serenity,
226     LastOSType = Serenity
227   };
228   enum EnvironmentType {
229     UnknownEnvironment,
230 
231     GNU,
232     GNUABIN32,
233     GNUABI64,
234     GNUEABI,
235     GNUEABIHF,
236     GNUF32,
237     GNUF64,
238     GNUSF,
239     GNUX32,
240     GNUILP32,
241     CODE16,
242     EABI,
243     EABIHF,
244     Android,
245     Musl,
246     MuslEABI,
247     MuslEABIHF,
248     MuslX32,
249 
250     // As in wasm64-unknown-unknown-nativeandroid.
251     // Turns off 64->32 function pointer cast on indirect
252     // lookup and compiles varargs calls using intrinsics
253     // for varargs allocation:
254     NativeAndroid,
255 
256     MSVC,
257     Itanium,
258     Cygnus,
259     CoreCLR,
260     Simulator, // Simulator variants of other systems, e.g., Apple's iOS
261     MacABI, // Mac Catalyst variant of Apple's iOS deployment target.
262 
263     // Shader Stages
264     // The order of these values matters, and must be kept in sync with the
265     // language options enum in Clang. The ordering is enforced in
266     // static_asserts in Triple.cpp and in Clang.
267     Pixel,
268     Vertex,
269     Geometry,
270     Hull,
271     Domain,
272     Compute,
273     Library,
274     RayGeneration,
275     Intersection,
276     AnyHit,
277     ClosestHit,
278     Miss,
279     Callable,
280     Mesh,
281     Amplification,
282 
283     OpenHOS,
284 
285     LastEnvironmentType = OpenHOS
286   };
287   enum ObjectFormatType {
288     UnknownObjectFormat,
289 
290     COFF,
291     DXContainer,
292     ELF,
293     GOFF,
294     MachO,
295     SPIRV,
296     Wasm,
297     XCOFF,
298   };
299 
300 private:
301   std::string Data;
302 
303   /// The parsed arch type.
304   ArchType Arch{};
305 
306   /// The parsed subarchitecture type.
307   SubArchType SubArch{};
308 
309   /// The parsed vendor type.
310   VendorType Vendor{};
311 
312   /// The parsed OS type.
313   OSType OS{};
314 
315   /// The parsed Environment type.
316   EnvironmentType Environment{};
317 
318   /// The object format type.
319   ObjectFormatType ObjectFormat{};
320 
321 public:
322   /// @name Constructors
323   /// @{
324 
325   /// Default constructor is the same as an empty string and leaves all
326   /// triple fields unknown.
327   Triple() = default;
328 
329   explicit Triple(const Twine &Str);
330   Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr);
331   Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
332          const Twine &EnvironmentStr);
333 
334   bool operator==(const Triple &Other) const {
335     return Arch == Other.Arch && SubArch == Other.SubArch &&
336            Vendor == Other.Vendor && OS == Other.OS &&
337            Environment == Other.Environment &&
338            ObjectFormat == Other.ObjectFormat;
339   }
340 
341   bool operator!=(const Triple &Other) const {
342     return !(*this == Other);
343   }
344 
345   /// @}
346   /// @name Normalization
347   /// @{
348 
349   /// Turn an arbitrary machine specification into the canonical triple form (or
350   /// something sensible that the Triple class understands if nothing better can
351   /// reasonably be done).  In particular, it handles the common case in which
352   /// otherwise valid components are in the wrong order.
353   static std::string normalize(StringRef Str);
354 
355   /// Return the normalized form of this triple's string.
normalize()356   std::string normalize() const { return normalize(Data); }
357 
358   /// @}
359   /// @name Typed Component Access
360   /// @{
361 
362   /// Get the parsed architecture type of this triple.
getArch()363   ArchType getArch() const { return Arch; }
364 
365   /// get the parsed subarchitecture type for this triple.
getSubArch()366   SubArchType getSubArch() const { return SubArch; }
367 
368   /// Get the parsed vendor type of this triple.
getVendor()369   VendorType getVendor() const { return Vendor; }
370 
371   /// Get the parsed operating system type of this triple.
getOS()372   OSType getOS() const { return OS; }
373 
374   /// Does this triple have the optional environment (fourth) component?
hasEnvironment()375   bool hasEnvironment() const {
376     return getEnvironmentName() != "";
377   }
378 
379   /// Get the parsed environment type of this triple.
getEnvironment()380   EnvironmentType getEnvironment() const { return Environment; }
381 
382   /// Parse the version number from the OS name component of the
383   /// triple, if present.
384   ///
385   /// For example, "fooos1.2.3" would return (1, 2, 3).
386   VersionTuple getEnvironmentVersion() const;
387 
388   /// Get the object format for this triple.
getObjectFormat()389   ObjectFormatType getObjectFormat() const { return ObjectFormat; }
390 
391   /// Parse the version number from the OS name component of the triple, if
392   /// present.
393   ///
394   /// For example, "fooos1.2.3" would return (1, 2, 3).
395   VersionTuple getOSVersion() const;
396 
397   /// Return just the major version number, this is specialized because it is a
398   /// common query.
getOSMajorVersion()399   unsigned getOSMajorVersion() const { return getOSVersion().getMajor(); }
400 
401   /// Parse the version number as with getOSVersion and then translate generic
402   /// "darwin" versions to the corresponding OS X versions.  This may also be
403   /// called with IOS triples but the OS X version number is just set to a
404   /// constant 10.4.0 in that case.  Returns true if successful.
405   bool getMacOSXVersion(VersionTuple &Version) const;
406 
407   /// Parse the version number as with getOSVersion.  This should only be called
408   /// with IOS or generic triples.
409   VersionTuple getiOSVersion() const;
410 
411   /// Parse the version number as with getOSVersion.  This should only be called
412   /// with WatchOS or generic triples.
413   VersionTuple getWatchOSVersion() const;
414 
415   /// Parse the version number as with getOSVersion.
416   VersionTuple getDriverKitVersion() const;
417 
418   /// @}
419   /// @name Direct Component Access
420   /// @{
421 
str()422   const std::string &str() const { return Data; }
423 
getTriple()424   const std::string &getTriple() const { return Data; }
425 
426   /// Get the architecture (first) component of the triple.
427   StringRef getArchName() const;
428 
429   /// Get the vendor (second) component of the triple.
430   StringRef getVendorName() const;
431 
432   /// Get the operating system (third) component of the triple.
433   StringRef getOSName() const;
434 
435   /// Get the optional environment (fourth) component of the triple, or "" if
436   /// empty.
437   StringRef getEnvironmentName() const;
438 
439   /// Get the operating system and optional environment components as a single
440   /// string (separated by a '-' if the environment component is present).
441   StringRef getOSAndEnvironmentName() const;
442 
443   /// @}
444   /// @name Convenience Predicates
445   /// @{
446 
447   /// Test whether the architecture is 64-bit
448   ///
449   /// Note that this tests for 64-bit pointer width, and nothing else. Note
450   /// that we intentionally expose only three predicates, 64-bit, 32-bit, and
451   /// 16-bit. The inner details of pointer width for particular architectures
452   /// is not summed up in the triple, and so only a coarse grained predicate
453   /// system is provided.
454   bool isArch64Bit() const;
455 
456   /// Test whether the architecture is 32-bit
457   ///
458   /// Note that this tests for 32-bit pointer width, and nothing else.
459   bool isArch32Bit() const;
460 
461   /// Test whether the architecture is 16-bit
462   ///
463   /// Note that this tests for 16-bit pointer width, and nothing else.
464   bool isArch16Bit() const;
465 
466   /// Helper function for doing comparisons against version numbers included in
467   /// the target triple.
468   bool isOSVersionLT(unsigned Major, unsigned Minor = 0,
469                      unsigned Micro = 0) const {
470     if (Minor == 0) {
471       return getOSVersion() < VersionTuple(Major);
472     }
473     if (Micro == 0) {
474       return getOSVersion() < VersionTuple(Major, Minor);
475     }
476     return getOSVersion() < VersionTuple(Major, Minor, Micro);
477   }
478 
isOSVersionLT(const Triple & Other)479   bool isOSVersionLT(const Triple &Other) const {
480     return getOSVersion() < Other.getOSVersion();
481   }
482 
483   /// Comparison function for checking OS X version compatibility, which handles
484   /// supporting skewed version numbering schemes used by the "darwin" triples.
485   bool isMacOSXVersionLT(unsigned Major, unsigned Minor = 0,
486                          unsigned Micro = 0) const;
487 
488   /// Is this a Mac OS X triple. For legacy reasons, we support both "darwin"
489   /// and "osx" as OS X triples.
isMacOSX()490   bool isMacOSX() const {
491     return getOS() == Triple::Darwin || getOS() == Triple::MacOSX;
492   }
493 
494   /// Is this an iOS triple.
495   /// Note: This identifies tvOS as a variant of iOS. If that ever
496   /// changes, i.e., if the two operating systems diverge or their version
497   /// numbers get out of sync, that will need to be changed.
498   /// watchOS has completely different version numbers so it is not included.
isiOS()499   bool isiOS() const {
500     return getOS() == Triple::IOS || isTvOS();
501   }
502 
503   /// Is this an Apple tvOS triple.
isTvOS()504   bool isTvOS() const {
505     return getOS() == Triple::TvOS;
506   }
507 
508   /// Is this an Apple watchOS triple.
isWatchOS()509   bool isWatchOS() const {
510     return getOS() == Triple::WatchOS;
511   }
512 
isWatchABI()513   bool isWatchABI() const {
514     return getSubArch() == Triple::ARMSubArch_v7k;
515   }
516 
517   /// Is this an Apple DriverKit triple.
isDriverKit()518   bool isDriverKit() const { return getOS() == Triple::DriverKit; }
519 
isOSzOS()520   bool isOSzOS() const { return getOS() == Triple::ZOS; }
521 
522   /// Is this a "Darwin" OS (macOS, iOS, tvOS, watchOS, or DriverKit).
isOSDarwin()523   bool isOSDarwin() const {
524     return isMacOSX() || isiOS() || isWatchOS() || isDriverKit();
525   }
526 
isSimulatorEnvironment()527   bool isSimulatorEnvironment() const {
528     return getEnvironment() == Triple::Simulator;
529   }
530 
isMacCatalystEnvironment()531   bool isMacCatalystEnvironment() const {
532     return getEnvironment() == Triple::MacABI;
533   }
534 
535   /// Returns true for targets that run on a macOS machine.
isTargetMachineMac()536   bool isTargetMachineMac() const {
537     return isMacOSX() || (isOSDarwin() && (isSimulatorEnvironment() ||
538                                            isMacCatalystEnvironment()));
539   }
540 
isOSNetBSD()541   bool isOSNetBSD() const {
542     return getOS() == Triple::NetBSD;
543   }
544 
isOSOpenBSD()545   bool isOSOpenBSD() const {
546     return getOS() == Triple::OpenBSD;
547   }
548 
isOSFreeBSD()549   bool isOSFreeBSD() const {
550     return getOS() == Triple::FreeBSD;
551   }
552 
isOSFuchsia()553   bool isOSFuchsia() const {
554     return getOS() == Triple::Fuchsia;
555   }
556 
isOSDragonFly()557   bool isOSDragonFly() const { return getOS() == Triple::DragonFly; }
558 
isOSSolaris()559   bool isOSSolaris() const {
560     return getOS() == Triple::Solaris;
561   }
562 
isOSIAMCU()563   bool isOSIAMCU() const {
564     return getOS() == Triple::ELFIAMCU;
565   }
566 
isOSUnknown()567   bool isOSUnknown() const { return getOS() == Triple::UnknownOS; }
568 
isGNUEnvironment()569   bool isGNUEnvironment() const {
570     EnvironmentType Env = getEnvironment();
571     return Env == Triple::GNU || Env == Triple::GNUABIN32 ||
572            Env == Triple::GNUABI64 || Env == Triple::GNUEABI ||
573            Env == Triple::GNUEABIHF || Env == Triple::GNUF32 ||
574            Env == Triple::GNUF64 || Env == Triple::GNUSF ||
575            Env == Triple::GNUX32;
576   }
577 
578   /// Tests whether the OS is Haiku.
isOSHaiku()579   bool isOSHaiku() const {
580     return getOS() == Triple::Haiku;
581   }
582 
583   /// Tests whether the OS is UEFI.
isUEFI()584   bool isUEFI() const {
585     return getOS() == Triple::UEFI;
586   }
587 
588   /// Tests whether the OS is Windows.
isOSWindows()589   bool isOSWindows() const {
590     return getOS() == Triple::Win32;
591   }
592 
593   /// Checks if the environment is MSVC.
isKnownWindowsMSVCEnvironment()594   bool isKnownWindowsMSVCEnvironment() const {
595     return isOSWindows() && getEnvironment() == Triple::MSVC;
596   }
597 
598   /// Checks if the environment could be MSVC.
isWindowsMSVCEnvironment()599   bool isWindowsMSVCEnvironment() const {
600     return isKnownWindowsMSVCEnvironment() ||
601            (isOSWindows() && getEnvironment() == Triple::UnknownEnvironment);
602   }
603 
604   // Checks if we're using the Windows Arm64EC ABI.
isWindowsArm64EC()605   bool isWindowsArm64EC() const {
606     return getArch() == Triple::aarch64 &&
607            getSubArch() == Triple::AArch64SubArch_arm64ec;
608   }
609 
isWindowsCoreCLREnvironment()610   bool isWindowsCoreCLREnvironment() const {
611     return isOSWindows() && getEnvironment() == Triple::CoreCLR;
612   }
613 
isWindowsItaniumEnvironment()614   bool isWindowsItaniumEnvironment() const {
615     return isOSWindows() && getEnvironment() == Triple::Itanium;
616   }
617 
isWindowsCygwinEnvironment()618   bool isWindowsCygwinEnvironment() const {
619     return isOSWindows() && getEnvironment() == Triple::Cygnus;
620   }
621 
isWindowsGNUEnvironment()622   bool isWindowsGNUEnvironment() const {
623     return isOSWindows() && getEnvironment() == Triple::GNU;
624   }
625 
626   /// Tests for either Cygwin or MinGW OS
isOSCygMing()627   bool isOSCygMing() const {
628     return isWindowsCygwinEnvironment() || isWindowsGNUEnvironment();
629   }
630 
631   /// Is this a "Windows" OS targeting a "MSVCRT.dll" environment.
isOSMSVCRT()632   bool isOSMSVCRT() const {
633     return isWindowsMSVCEnvironment() || isWindowsGNUEnvironment() ||
634            isWindowsItaniumEnvironment();
635   }
636 
637   /// Tests whether the OS is NaCl (Native Client)
isOSNaCl()638   bool isOSNaCl() const {
639     return getOS() == Triple::NaCl;
640   }
641 
642   /// Tests whether the OS is Linux.
isOSLinux()643   bool isOSLinux() const {
644     return getOS() == Triple::Linux;
645   }
646 
647   /// Tests whether the OS is kFreeBSD.
isOSKFreeBSD()648   bool isOSKFreeBSD() const {
649     return getOS() == Triple::KFreeBSD;
650   }
651 
652   /// Tests whether the OS is Hurd.
isOSHurd()653   bool isOSHurd() const {
654     return getOS() == Triple::Hurd;
655   }
656 
657   /// Tests whether the OS is WASI.
isOSWASI()658   bool isOSWASI() const {
659     return getOS() == Triple::WASI;
660   }
661 
662   /// Tests whether the OS is Emscripten.
isOSEmscripten()663   bool isOSEmscripten() const {
664     return getOS() == Triple::Emscripten;
665   }
666 
667   /// Tests whether the OS uses glibc.
isOSGlibc()668   bool isOSGlibc() const {
669     return (getOS() == Triple::Linux || getOS() == Triple::KFreeBSD ||
670             getOS() == Triple::Hurd) &&
671            !isAndroid();
672   }
673 
674   /// Tests whether the OS is AIX.
isOSAIX()675   bool isOSAIX() const {
676     return getOS() == Triple::AIX;
677   }
678 
isOSSerenity()679   bool isOSSerenity() const {
680     return getOS() == Triple::Serenity;
681   }
682 
683   /// Tests whether the OS uses the ELF binary format.
isOSBinFormatELF()684   bool isOSBinFormatELF() const {
685     return getObjectFormat() == Triple::ELF;
686   }
687 
688   /// Tests whether the OS uses the COFF binary format.
isOSBinFormatCOFF()689   bool isOSBinFormatCOFF() const {
690     return getObjectFormat() == Triple::COFF;
691   }
692 
693   /// Tests whether the OS uses the GOFF binary format.
isOSBinFormatGOFF()694   bool isOSBinFormatGOFF() const { return getObjectFormat() == Triple::GOFF; }
695 
696   /// Tests whether the environment is MachO.
isOSBinFormatMachO()697   bool isOSBinFormatMachO() const {
698     return getObjectFormat() == Triple::MachO;
699   }
700 
701   /// Tests whether the OS uses the Wasm binary format.
isOSBinFormatWasm()702   bool isOSBinFormatWasm() const {
703     return getObjectFormat() == Triple::Wasm;
704   }
705 
706   /// Tests whether the OS uses the XCOFF binary format.
isOSBinFormatXCOFF()707   bool isOSBinFormatXCOFF() const {
708     return getObjectFormat() == Triple::XCOFF;
709   }
710 
711   /// Tests whether the OS uses the DXContainer binary format.
isOSBinFormatDXContainer()712   bool isOSBinFormatDXContainer() const {
713     return getObjectFormat() == Triple::DXContainer;
714   }
715 
716   /// Tests whether the target is the PS4 platform.
isPS4()717   bool isPS4() const {
718     return getArch() == Triple::x86_64 &&
719            getVendor() == Triple::SCEI &&
720            getOS() == Triple::PS4;
721   }
722 
723   /// Tests whether the target is the PS5 platform.
isPS5()724   bool isPS5() const {
725     return getArch() == Triple::x86_64 &&
726       getVendor() == Triple::SCEI &&
727       getOS() == Triple::PS5;
728   }
729 
730   /// Tests whether the target is the PS4 or PS5 platform.
isPS()731   bool isPS() const { return isPS4() || isPS5(); }
732 
733   /// Tests whether the target is Android
isAndroid()734   bool isAndroid() const { return getEnvironment() == Triple::Android; }
735 
isAndroidVersionLT(unsigned Major)736   bool isAndroidVersionLT(unsigned Major) const {
737     assert(isAndroid() && "Not an Android triple!");
738 
739     VersionTuple Version = getEnvironmentVersion();
740 
741     // 64-bit targets did not exist before API level 21 (Lollipop).
742     if (isArch64Bit() && Version.getMajor() < 21)
743       return VersionTuple(21) < VersionTuple(Major);
744 
745     return Version < VersionTuple(Major);
746   }
747 
748   /// Tests whether the environment is musl-libc
isMusl()749   bool isMusl() const {
750     return getEnvironment() == Triple::Musl ||
751            getEnvironment() == Triple::MuslEABI ||
752            getEnvironment() == Triple::MuslEABIHF ||
753            getEnvironment() == Triple::MuslX32 ||
754            getEnvironment() == Triple::OpenHOS || isOSLiteOS();
755   }
756 
757   /// Tests whether the target is OHOS
758   /// LiteOS default enviroment is also OHOS, but omited on triple.
isOHOSFamily()759   bool isOHOSFamily() const { return isOpenHOS() || isOSLiteOS(); }
760 
isOpenHOS()761   bool isOpenHOS() const { return getEnvironment() == Triple::OpenHOS; }
762 
isOSLiteOS()763   bool isOSLiteOS() const { return getOS() == Triple::LiteOS; }
764 
isNativeAndroid()765   bool isNativeAndroid() const { return getEnvironment() == Triple::NativeAndroid; }
766 
767   /// Tests whether the target is DXIL.
isDXIL()768   bool isDXIL() const {
769     return getArch() == Triple::dxil;
770   }
771 
isShaderModelOS()772   bool isShaderModelOS() const {
773     return getOS() == Triple::ShaderModel;
774   }
775 
isShaderStageEnvironment()776   bool isShaderStageEnvironment() const {
777     EnvironmentType Env = getEnvironment();
778     return Env == Triple::Pixel || Env == Triple::Vertex ||
779            Env == Triple::Geometry || Env == Triple::Hull ||
780            Env == Triple::Domain || Env == Triple::Compute ||
781            Env == Triple::Library || Env == Triple::RayGeneration ||
782            Env == Triple::Intersection || Env == Triple::AnyHit ||
783            Env == Triple::ClosestHit || Env == Triple::Miss ||
784            Env == Triple::Callable || Env == Triple::Mesh ||
785            Env == Triple::Amplification;
786   }
787 
788   /// Tests whether the target is SPIR (32- or 64-bit).
isSPIR()789   bool isSPIR() const {
790     return getArch() == Triple::spir || getArch() == Triple::spir64;
791   }
792 
793   /// Tests whether the target is SPIR-V (32/64-bit/Logical).
isSPIRV()794   bool isSPIRV() const {
795     return getArch() == Triple::spirv32 || getArch() == Triple::spirv64 ||
796            getArch() == Triple::spirv;
797   }
798 
799   /// Tests whether the target is SPIR-V Logical
isSPIRVLogical()800   bool isSPIRVLogical() const {
801     return getArch() == Triple::spirv;
802   }
803 
804   /// Tests whether the target is NVPTX (32- or 64-bit).
isNVPTX()805   bool isNVPTX() const {
806     return getArch() == Triple::nvptx || getArch() == Triple::nvptx64;
807   }
808 
809   /// Tests whether the target is AMDGCN
isAMDGCN()810   bool isAMDGCN() const { return getArch() == Triple::amdgcn; }
811 
isAMDGPU()812   bool isAMDGPU() const {
813     return getArch() == Triple::r600 || getArch() == Triple::amdgcn;
814   }
815 
816   /// Tests whether the target is Thumb (little and big endian).
isThumb()817   bool isThumb() const {
818     return getArch() == Triple::thumb || getArch() == Triple::thumbeb;
819   }
820 
821   /// Tests whether the target is ARM (little and big endian).
isARM()822   bool isARM() const {
823     return getArch() == Triple::arm || getArch() == Triple::armeb;
824   }
825 
826   /// Tests whether the target supports the EHABI exception
827   /// handling standard.
isTargetEHABICompatible()828   bool isTargetEHABICompatible() const {
829     return (isARM() || isThumb()) &&
830            (getEnvironment() == Triple::EABI ||
831             getEnvironment() == Triple::GNUEABI ||
832             getEnvironment() == Triple::MuslEABI ||
833             getEnvironment() == Triple::EABIHF ||
834             getEnvironment() == Triple::GNUEABIHF ||
835             getEnvironment() == Triple::OpenHOS ||
836             getEnvironment() == Triple::MuslEABIHF || isAndroid()) &&
837            isOSBinFormatELF();
838   }
839 
840   /// Tests whether the target is T32.
isArmT32()841   bool isArmT32() const {
842     switch (getSubArch()) {
843     case Triple::ARMSubArch_v8m_baseline:
844     case Triple::ARMSubArch_v7s:
845     case Triple::ARMSubArch_v7k:
846     case Triple::ARMSubArch_v7ve:
847     case Triple::ARMSubArch_v6:
848     case Triple::ARMSubArch_v6m:
849     case Triple::ARMSubArch_v6k:
850     case Triple::ARMSubArch_v6t2:
851     case Triple::ARMSubArch_v5:
852     case Triple::ARMSubArch_v5te:
853     case Triple::ARMSubArch_v4t:
854       return false;
855     default:
856       return true;
857     }
858   }
859 
860   /// Tests whether the target is an M-class.
isArmMClass()861   bool isArmMClass() const {
862     switch (getSubArch()) {
863     case Triple::ARMSubArch_v6m:
864     case Triple::ARMSubArch_v7m:
865     case Triple::ARMSubArch_v7em:
866     case Triple::ARMSubArch_v8m_mainline:
867     case Triple::ARMSubArch_v8m_baseline:
868     case Triple::ARMSubArch_v8_1m_mainline:
869       return true;
870     default:
871       return false;
872     }
873   }
874 
875   /// Tests whether the target is AArch64 (little and big endian).
isAArch64()876   bool isAArch64() const {
877     return getArch() == Triple::aarch64 || getArch() == Triple::aarch64_be ||
878            getArch() == Triple::aarch64_32;
879   }
880 
881   /// Tests whether the target is AArch64 and pointers are the size specified by
882   /// \p PointerWidth.
isAArch64(int PointerWidth)883   bool isAArch64(int PointerWidth) const {
884     assert(PointerWidth == 64 || PointerWidth == 32);
885     if (!isAArch64())
886       return false;
887     return getArch() == Triple::aarch64_32 ||
888                    getEnvironment() == Triple::GNUILP32
889                ? PointerWidth == 32
890                : PointerWidth == 64;
891   }
892 
893   /// Tests whether the target is 32-bit LoongArch.
isLoongArch32()894   bool isLoongArch32() const { return getArch() == Triple::loongarch32; }
895 
896   /// Tests whether the target is 64-bit LoongArch.
isLoongArch64()897   bool isLoongArch64() const { return getArch() == Triple::loongarch64; }
898 
899   /// Tests whether the target is LoongArch (32- and 64-bit).
isLoongArch()900   bool isLoongArch() const { return isLoongArch32() || isLoongArch64(); }
901 
902   /// Tests whether the target is MIPS 32-bit (little and big endian).
isMIPS32()903   bool isMIPS32() const {
904     return getArch() == Triple::mips || getArch() == Triple::mipsel;
905   }
906 
907   /// Tests whether the target is MIPS 64-bit (little and big endian).
isMIPS64()908   bool isMIPS64() const {
909     return getArch() == Triple::mips64 || getArch() == Triple::mips64el;
910   }
911 
912   /// Tests whether the target is MIPS (little and big endian, 32- or 64-bit).
isMIPS()913   bool isMIPS() const {
914     return isMIPS32() || isMIPS64();
915   }
916 
917   /// Tests whether the target is PowerPC (32- or 64-bit LE or BE).
isPPC()918   bool isPPC() const {
919     return getArch() == Triple::ppc || getArch() == Triple::ppc64 ||
920            getArch() == Triple::ppcle || getArch() == Triple::ppc64le;
921   }
922 
923   /// Tests whether the target is 32-bit PowerPC (little and big endian).
isPPC32()924   bool isPPC32() const {
925     return getArch() == Triple::ppc || getArch() == Triple::ppcle;
926   }
927 
928   /// Tests whether the target is 64-bit PowerPC (little and big endian).
isPPC64()929   bool isPPC64() const {
930     return getArch() == Triple::ppc64 || getArch() == Triple::ppc64le;
931   }
932 
933   /// Tests whether the target 64-bit PowerPC big endian ABI is ELFv2.
isPPC64ELFv2ABI()934   bool isPPC64ELFv2ABI() const {
935     return (getArch() == Triple::ppc64 &&
936             ((getOS() == Triple::FreeBSD &&
937               (getOSMajorVersion() >= 13 || getOSVersion().empty())) ||
938              getOS() == Triple::OpenBSD || isMusl()));
939   }
940 
941   /// Tests whether the target 32-bit PowerPC uses Secure PLT.
isPPC32SecurePlt()942   bool isPPC32SecurePlt() const {
943     return ((getArch() == Triple::ppc || getArch() == Triple::ppcle) &&
944             ((getOS() == Triple::FreeBSD &&
945               (getOSMajorVersion() >= 13 || getOSVersion().empty())) ||
946              getOS() == Triple::NetBSD || getOS() == Triple::OpenBSD ||
947              isMusl()));
948   }
949 
950   /// Tests whether the target is 32-bit RISC-V.
isRISCV32()951   bool isRISCV32() const { return getArch() == Triple::riscv32; }
952 
953   /// Tests whether the target is 64-bit RISC-V.
isRISCV64()954   bool isRISCV64() const { return getArch() == Triple::riscv64; }
955 
956   /// Tests whether the target is RISC-V (32- and 64-bit).
isRISCV()957   bool isRISCV() const { return isRISCV32() || isRISCV64(); }
958 
959   /// Tests whether the target is 32-bit SPARC (little and big endian).
isSPARC32()960   bool isSPARC32() const {
961     return getArch() == Triple::sparc || getArch() == Triple::sparcel;
962   }
963 
964   /// Tests whether the target is 64-bit SPARC (big endian).
isSPARC64()965   bool isSPARC64() const { return getArch() == Triple::sparcv9; }
966 
967   /// Tests whether the target is SPARC.
isSPARC()968   bool isSPARC() const { return isSPARC32() || isSPARC64(); }
969 
970   /// Tests whether the target is SystemZ.
isSystemZ()971   bool isSystemZ() const {
972     return getArch() == Triple::systemz;
973   }
974 
975   /// Tests whether the target is x86 (32- or 64-bit).
isX86()976   bool isX86() const {
977     return getArch() == Triple::x86 || getArch() == Triple::x86_64;
978   }
979 
980   /// Tests whether the target is VE
isVE()981   bool isVE() const {
982     return getArch() == Triple::ve;
983   }
984 
985   /// Tests whether the target is wasm (32- and 64-bit).
isWasm()986   bool isWasm() const {
987     return getArch() == Triple::wasm32 || getArch() == Triple::wasm64;
988   }
989 
990   // Tests whether the target is CSKY
isCSKY()991   bool isCSKY() const {
992     return getArch() == Triple::csky;
993   }
994 
995   /// Tests whether the target is the Apple "arm64e" AArch64 subarch.
isArm64e()996   bool isArm64e() const {
997     return getArch() == Triple::aarch64 &&
998            getSubArch() == Triple::AArch64SubArch_arm64e;
999   }
1000 
1001   /// Tests whether the target is X32.
isX32()1002   bool isX32() const {
1003     EnvironmentType Env = getEnvironment();
1004     return Env == Triple::GNUX32 || Env == Triple::MuslX32;
1005   }
1006 
1007   /// Tests whether the target is eBPF.
isBPF()1008   bool isBPF() const {
1009     return getArch() == Triple::bpfel || getArch() == Triple::bpfeb;
1010   }
1011 
1012   /// Tests whether the target supports comdat
supportsCOMDAT()1013   bool supportsCOMDAT() const {
1014     return !(isOSBinFormatMachO() || isOSBinFormatXCOFF() ||
1015              isOSBinFormatDXContainer());
1016   }
1017 
1018   /// Tests whether the target uses emulated TLS as default.
1019   ///
1020   /// Note: Android API level 29 (10) introduced ELF TLS.
hasDefaultEmulatedTLS()1021   bool hasDefaultEmulatedTLS() const {
1022     return (isAndroid() && isAndroidVersionLT(29)) || isOSOpenBSD() ||
1023            isWindowsCygwinEnvironment() || isOHOSFamily();
1024   }
1025 
1026   /// True if the target supports both general-dynamic and TLSDESC, and TLSDESC
1027   /// is enabled by default.
hasDefaultTLSDESC()1028   bool hasDefaultTLSDESC() const {
1029     // TODO: Improve check for other platforms, like Android, and RISC-V
1030     return false;
1031   }
1032 
1033   /// Tests whether the target uses -data-sections as default.
hasDefaultDataSections()1034   bool hasDefaultDataSections() const {
1035     return isOSBinFormatXCOFF() || isWasm();
1036   }
1037 
1038   /// Tests if the environment supports dllimport/export annotations.
hasDLLImportExport()1039   bool hasDLLImportExport() const { return isOSWindows() || isPS(); }
1040 
1041   /// @}
1042   /// @name Mutators
1043   /// @{
1044 
1045   /// Set the architecture (first) component of the triple to a known type.
1046   void setArch(ArchType Kind, SubArchType SubArch = NoSubArch);
1047 
1048   /// Set the vendor (second) component of the triple to a known type.
1049   void setVendor(VendorType Kind);
1050 
1051   /// Set the operating system (third) component of the triple to a known type.
1052   void setOS(OSType Kind);
1053 
1054   /// Set the environment (fourth) component of the triple to a known type.
1055   void setEnvironment(EnvironmentType Kind);
1056 
1057   /// Set the object file format.
1058   void setObjectFormat(ObjectFormatType Kind);
1059 
1060   /// Set all components to the new triple \p Str.
1061   void setTriple(const Twine &Str);
1062 
1063   /// Set the architecture (first) component of the triple by name.
1064   void setArchName(StringRef Str);
1065 
1066   /// Set the vendor (second) component of the triple by name.
1067   void setVendorName(StringRef Str);
1068 
1069   /// Set the operating system (third) component of the triple by name.
1070   void setOSName(StringRef Str);
1071 
1072   /// Set the optional environment (fourth) component of the triple by name.
1073   void setEnvironmentName(StringRef Str);
1074 
1075   /// Set the operating system and optional environment components with a single
1076   /// string.
1077   void setOSAndEnvironmentName(StringRef Str);
1078 
1079   /// @}
1080   /// @name Helpers to build variants of a particular triple.
1081   /// @{
1082 
1083   /// Form a triple with a 32-bit variant of the current architecture.
1084   ///
1085   /// This can be used to move across "families" of architectures where useful.
1086   ///
1087   /// \returns A new triple with a 32-bit architecture or an unknown
1088   ///          architecture if no such variant can be found.
1089   llvm::Triple get32BitArchVariant() const;
1090 
1091   /// Form a triple with a 64-bit variant of the current architecture.
1092   ///
1093   /// This can be used to move across "families" of architectures where useful.
1094   ///
1095   /// \returns A new triple with a 64-bit architecture or an unknown
1096   ///          architecture if no such variant can be found.
1097   llvm::Triple get64BitArchVariant() const;
1098 
1099   /// Form a triple with a big endian variant of the current architecture.
1100   ///
1101   /// This can be used to move across "families" of architectures where useful.
1102   ///
1103   /// \returns A new triple with a big endian architecture or an unknown
1104   ///          architecture if no such variant can be found.
1105   llvm::Triple getBigEndianArchVariant() const;
1106 
1107   /// Form a triple with a little endian variant of the current architecture.
1108   ///
1109   /// This can be used to move across "families" of architectures where useful.
1110   ///
1111   /// \returns A new triple with a little endian architecture or an unknown
1112   ///          architecture if no such variant can be found.
1113   llvm::Triple getLittleEndianArchVariant() const;
1114 
1115   /// Tests whether the target triple is little endian.
1116   ///
1117   /// \returns true if the triple is little endian, false otherwise.
1118   bool isLittleEndian() const;
1119 
1120   /// Test whether target triples are compatible.
1121   bool isCompatibleWith(const Triple &Other) const;
1122 
1123   /// Merge target triples.
1124   std::string merge(const Triple &Other) const;
1125 
1126   /// Some platforms have different minimum supported OS versions that
1127   /// varies by the architecture specified in the triple. This function
1128   /// returns the minimum supported OS version for this triple if one an exists,
1129   /// or an invalid version tuple if this triple doesn't have one.
1130   VersionTuple getMinimumSupportedOSVersion() const;
1131 
1132   /// @}
1133   /// @name Static helpers for IDs.
1134   /// @{
1135 
1136   /// Get the canonical name for the \p Kind architecture.
1137   static StringRef getArchTypeName(ArchType Kind);
1138 
1139   /// Get the architecture name based on \p Kind and \p SubArch.
1140   static StringRef getArchName(ArchType Kind, SubArchType SubArch = NoSubArch);
1141 
1142   /// Get the "prefix" canonical name for the \p Kind architecture. This is the
1143   /// prefix used by the architecture specific builtins, and is suitable for
1144   /// passing to \see Intrinsic::getIntrinsicForClangBuiltin().
1145   ///
1146   /// \return - The architecture prefix, or 0 if none is defined.
1147   static StringRef getArchTypePrefix(ArchType Kind);
1148 
1149   /// Get the canonical name for the \p Kind vendor.
1150   static StringRef getVendorTypeName(VendorType Kind);
1151 
1152   /// Get the canonical name for the \p Kind operating system.
1153   static StringRef getOSTypeName(OSType Kind);
1154 
1155   /// Get the canonical name for the \p Kind environment.
1156   static StringRef getEnvironmentTypeName(EnvironmentType Kind);
1157 
1158   /// Get the name for the \p Object format.
1159   static StringRef getObjectFormatTypeName(ObjectFormatType ObjectFormat);
1160 
1161   /// @}
1162   /// @name Static helpers for converting alternate architecture names.
1163   /// @{
1164 
1165   /// The canonical type for the given LLVM architecture name (e.g., "x86").
1166   static ArchType getArchTypeForLLVMName(StringRef Str);
1167 
1168   /// @}
1169 
1170   /// Returns a canonicalized OS version number for the specified OS.
1171   static VersionTuple getCanonicalVersionForOS(OSType OSKind,
1172                                                const VersionTuple &Version);
1173 };
1174 
1175 } // End llvm namespace
1176 
1177 
1178 #endif
1179