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