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     MSVC,
267     Itanium,
268     Cygnus,
269     CoreCLR,
270     Simulator, // Simulator variants of other systems, e.g., Apple's iOS
271     MacABI,    // Mac Catalyst variant of Apple's iOS deployment target.
272 
273     // Shader Stages
274     // The order of these values matters, and must be kept in sync with the
275     // language options enum in Clang. The ordering is enforced in
276     // static_asserts in Triple.cpp and in Clang.
277     Pixel,
278     Vertex,
279     Geometry,
280     Hull,
281     Domain,
282     Compute,
283     Library,
284     RayGeneration,
285     Intersection,
286     AnyHit,
287     ClosestHit,
288     Miss,
289     Callable,
290     Mesh,
291     Amplification,
292     OpenCL,
293     OpenHOS,
294 
295     PAuthTest,
296 
297     GNUT64,
298     GNUEABIT64,
299     GNUEABIHFT64,
300 
301     LastEnvironmentType = GNUEABIHFT64
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 OSVersion and 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::GNUT64 ||
613            Env == Triple::GNUABIN32 || Env == Triple::GNUABI64 ||
614            Env == Triple::GNUEABI || Env == Triple::GNUEABIT64 ||
615            Env == Triple::GNUEABIHF || Env == Triple::GNUEABIHFT64 ||
616            Env == Triple::GNUF32 || Env == Triple::GNUF64 ||
617            Env == Triple::GNUSF || Env == Triple::GNUX32;
618   }
619 
620   /// Tests whether the OS is Haiku.
isOSHaiku()621   bool isOSHaiku() const {
622     return getOS() == Triple::Haiku;
623   }
624 
625   /// Tests whether the OS is UEFI.
isUEFI()626   bool isUEFI() const {
627     return getOS() == Triple::UEFI;
628   }
629 
630   /// Tests whether the OS is Windows.
isOSWindows()631   bool isOSWindows() const {
632     return getOS() == Triple::Win32;
633   }
634 
635   /// Checks if the environment is MSVC.
isKnownWindowsMSVCEnvironment()636   bool isKnownWindowsMSVCEnvironment() const {
637     return isOSWindows() && getEnvironment() == Triple::MSVC;
638   }
639 
640   /// Checks if the environment could be MSVC.
isWindowsMSVCEnvironment()641   bool isWindowsMSVCEnvironment() const {
642     return isKnownWindowsMSVCEnvironment() ||
643            (isOSWindows() && getEnvironment() == Triple::UnknownEnvironment);
644   }
645 
646   // Checks if we're using the Windows Arm64EC ABI.
isWindowsArm64EC()647   bool isWindowsArm64EC() const {
648     return getArch() == Triple::aarch64 &&
649            getSubArch() == Triple::AArch64SubArch_arm64ec;
650   }
651 
isWindowsCoreCLREnvironment()652   bool isWindowsCoreCLREnvironment() const {
653     return isOSWindows() && getEnvironment() == Triple::CoreCLR;
654   }
655 
isWindowsItaniumEnvironment()656   bool isWindowsItaniumEnvironment() const {
657     return isOSWindows() && getEnvironment() == Triple::Itanium;
658   }
659 
isWindowsCygwinEnvironment()660   bool isWindowsCygwinEnvironment() const {
661     return isOSWindows() && getEnvironment() == Triple::Cygnus;
662   }
663 
isWindowsGNUEnvironment()664   bool isWindowsGNUEnvironment() const {
665     return isOSWindows() && getEnvironment() == Triple::GNU;
666   }
667 
668   /// Tests for either Cygwin or MinGW OS
isOSCygMing()669   bool isOSCygMing() const {
670     return isWindowsCygwinEnvironment() || isWindowsGNUEnvironment();
671   }
672 
673   /// Is this a "Windows" OS targeting a "MSVCRT.dll" environment.
isOSMSVCRT()674   bool isOSMSVCRT() const {
675     return isWindowsMSVCEnvironment() || isWindowsGNUEnvironment() ||
676            isWindowsItaniumEnvironment();
677   }
678 
679   /// Tests whether the OS is NaCl (Native Client)
isOSNaCl()680   bool isOSNaCl() const {
681     return getOS() == Triple::NaCl;
682   }
683 
684   /// Tests whether the OS is Linux.
isOSLinux()685   bool isOSLinux() const {
686     return getOS() == Triple::Linux;
687   }
688 
689   /// Tests whether the OS is kFreeBSD.
isOSKFreeBSD()690   bool isOSKFreeBSD() const {
691     return getOS() == Triple::KFreeBSD;
692   }
693 
694   /// Tests whether the OS is Hurd.
isOSHurd()695   bool isOSHurd() const {
696     return getOS() == Triple::Hurd;
697   }
698 
699   /// Tests whether the OS is WASI.
isOSWASI()700   bool isOSWASI() const {
701     return getOS() == Triple::WASI;
702   }
703 
704   /// Tests whether the OS is Emscripten.
isOSEmscripten()705   bool isOSEmscripten() const {
706     return getOS() == Triple::Emscripten;
707   }
708 
709   /// Tests whether the OS uses glibc.
isOSGlibc()710   bool isOSGlibc() const {
711     return (getOS() == Triple::Linux || getOS() == Triple::KFreeBSD ||
712             getOS() == Triple::Hurd) &&
713            !isAndroid();
714   }
715 
716   /// Tests whether the OS is AIX.
isOSAIX()717   bool isOSAIX() const {
718     return getOS() == Triple::AIX;
719   }
720 
isOSSerenity()721   bool isOSSerenity() const {
722     return getOS() == Triple::Serenity;
723   }
724 
725   /// Tests whether the OS uses the ELF binary format.
isOSBinFormatELF()726   bool isOSBinFormatELF() const {
727     return getObjectFormat() == Triple::ELF;
728   }
729 
730   /// Tests whether the OS uses the COFF binary format.
isOSBinFormatCOFF()731   bool isOSBinFormatCOFF() const {
732     return getObjectFormat() == Triple::COFF;
733   }
734 
735   /// Tests whether the OS uses the GOFF binary format.
isOSBinFormatGOFF()736   bool isOSBinFormatGOFF() const { return getObjectFormat() == Triple::GOFF; }
737 
738   /// Tests whether the environment is MachO.
isOSBinFormatMachO()739   bool isOSBinFormatMachO() const {
740     return getObjectFormat() == Triple::MachO;
741   }
742 
743   /// Tests whether the OS uses the Wasm binary format.
isOSBinFormatWasm()744   bool isOSBinFormatWasm() const {
745     return getObjectFormat() == Triple::Wasm;
746   }
747 
748   /// Tests whether the OS uses the XCOFF binary format.
isOSBinFormatXCOFF()749   bool isOSBinFormatXCOFF() const {
750     return getObjectFormat() == Triple::XCOFF;
751   }
752 
753   /// Tests whether the OS uses the DXContainer binary format.
isOSBinFormatDXContainer()754   bool isOSBinFormatDXContainer() const {
755     return getObjectFormat() == Triple::DXContainer;
756   }
757 
758   /// Tests whether the target is the PS4 platform.
isPS4()759   bool isPS4() const {
760     return getArch() == Triple::x86_64 &&
761            getVendor() == Triple::SCEI &&
762            getOS() == Triple::PS4;
763   }
764 
765   /// Tests whether the target is the PS5 platform.
isPS5()766   bool isPS5() const {
767     return getArch() == Triple::x86_64 &&
768       getVendor() == Triple::SCEI &&
769       getOS() == Triple::PS5;
770   }
771 
772   /// Tests whether the target is the PS4 or PS5 platform.
isPS()773   bool isPS() const { return isPS4() || isPS5(); }
774 
775   /// Tests whether the target is Android
isAndroid()776   bool isAndroid() const { return getEnvironment() == Triple::Android; }
777 
isAndroidVersionLT(unsigned Major)778   bool isAndroidVersionLT(unsigned Major) const {
779     assert(isAndroid() && "Not an Android triple!");
780 
781     VersionTuple Version = getEnvironmentVersion();
782 
783     // 64-bit targets did not exist before API level 21 (Lollipop).
784     if (isArch64Bit() && Version.getMajor() < 21)
785       return VersionTuple(21) < VersionTuple(Major);
786 
787     return Version < VersionTuple(Major);
788   }
789 
790   /// Tests whether the environment is musl-libc
isMusl()791   bool isMusl() const {
792     return getEnvironment() == Triple::Musl ||
793            getEnvironment() == Triple::MuslEABI ||
794            getEnvironment() == Triple::MuslEABIHF ||
795            getEnvironment() == Triple::MuslX32 ||
796            getEnvironment() == Triple::OpenHOS || isOSLiteOS();
797   }
798 
799   /// Tests whether the target is OHOS
800   /// LiteOS default enviroment is also OHOS, but omited on triple.
isOHOSFamily()801   bool isOHOSFamily() const { return isOpenHOS() || isOSLiteOS(); }
802 
isOpenHOS()803   bool isOpenHOS() const { return getEnvironment() == Triple::OpenHOS; }
804 
isOSLiteOS()805   bool isOSLiteOS() const { return getOS() == Triple::LiteOS; }
806 
807   /// Tests whether the target is DXIL.
isDXIL()808   bool isDXIL() const {
809     return getArch() == Triple::dxil;
810   }
811 
isShaderModelOS()812   bool isShaderModelOS() const {
813     return getOS() == Triple::ShaderModel;
814   }
815 
isVulkanOS()816   bool isVulkanOS() const { return getOS() == Triple::Vulkan; }
817 
isShaderStageEnvironment()818   bool isShaderStageEnvironment() const {
819     EnvironmentType Env = getEnvironment();
820     return Env == Triple::Pixel || Env == Triple::Vertex ||
821            Env == Triple::Geometry || Env == Triple::Hull ||
822            Env == Triple::Domain || Env == Triple::Compute ||
823            Env == Triple::Library || Env == Triple::RayGeneration ||
824            Env == Triple::Intersection || Env == Triple::AnyHit ||
825            Env == Triple::ClosestHit || Env == Triple::Miss ||
826            Env == Triple::Callable || Env == Triple::Mesh ||
827            Env == Triple::Amplification;
828   }
829 
830   /// Tests whether the target is SPIR (32- or 64-bit).
isSPIR()831   bool isSPIR() const {
832     return getArch() == Triple::spir || getArch() == Triple::spir64;
833   }
834 
835   /// Tests whether the target is SPIR-V (32/64-bit/Logical).
isSPIRV()836   bool isSPIRV() const {
837     return getArch() == Triple::spirv32 || getArch() == Triple::spirv64 ||
838            getArch() == Triple::spirv;
839   }
840 
841   /// Tests whether the target is SPIR-V Logical
isSPIRVLogical()842   bool isSPIRVLogical() const {
843     return getArch() == Triple::spirv;
844   }
845 
846   /// Tests whether the target is NVPTX (32- or 64-bit).
isNVPTX()847   bool isNVPTX() const {
848     return getArch() == Triple::nvptx || getArch() == Triple::nvptx64;
849   }
850 
851   /// Tests whether the target is AMDGCN
isAMDGCN()852   bool isAMDGCN() const { return getArch() == Triple::amdgcn; }
853 
isAMDGPU()854   bool isAMDGPU() const {
855     return getArch() == Triple::r600 || getArch() == Triple::amdgcn;
856   }
857 
858   /// Tests whether the target is Thumb (little and big endian).
isThumb()859   bool isThumb() const {
860     return getArch() == Triple::thumb || getArch() == Triple::thumbeb;
861   }
862 
863   /// Tests whether the target is ARM (little and big endian).
isARM()864   bool isARM() const {
865     return getArch() == Triple::arm || getArch() == Triple::armeb;
866   }
867 
868   /// Tests whether the target supports the EHABI exception
869   /// handling standard.
isTargetEHABICompatible()870   bool isTargetEHABICompatible() const {
871     return (isARM() || isThumb()) &&
872            (getEnvironment() == Triple::EABI ||
873             getEnvironment() == Triple::GNUEABI ||
874             getEnvironment() == Triple::GNUEABIT64 ||
875             getEnvironment() == Triple::MuslEABI ||
876             getEnvironment() == Triple::EABIHF ||
877             getEnvironment() == Triple::GNUEABIHF ||
878             getEnvironment() == Triple::GNUEABIHFT64 ||
879             getEnvironment() == Triple::OpenHOS ||
880             getEnvironment() == Triple::MuslEABIHF || isAndroid()) &&
881            isOSBinFormatELF();
882   }
883 
884   /// Tests whether the target is T32.
isArmT32()885   bool isArmT32() const {
886     switch (getSubArch()) {
887     case Triple::ARMSubArch_v8m_baseline:
888     case Triple::ARMSubArch_v7s:
889     case Triple::ARMSubArch_v7k:
890     case Triple::ARMSubArch_v7ve:
891     case Triple::ARMSubArch_v6:
892     case Triple::ARMSubArch_v6m:
893     case Triple::ARMSubArch_v6k:
894     case Triple::ARMSubArch_v6t2:
895     case Triple::ARMSubArch_v5:
896     case Triple::ARMSubArch_v5te:
897     case Triple::ARMSubArch_v4t:
898       return false;
899     default:
900       return true;
901     }
902   }
903 
904   /// Tests whether the target is an M-class.
isArmMClass()905   bool isArmMClass() const {
906     switch (getSubArch()) {
907     case Triple::ARMSubArch_v6m:
908     case Triple::ARMSubArch_v7m:
909     case Triple::ARMSubArch_v7em:
910     case Triple::ARMSubArch_v8m_mainline:
911     case Triple::ARMSubArch_v8m_baseline:
912     case Triple::ARMSubArch_v8_1m_mainline:
913       return true;
914     default:
915       return false;
916     }
917   }
918 
919   /// Tests whether the target is AArch64 (little and big endian).
isAArch64()920   bool isAArch64() const {
921     return getArch() == Triple::aarch64 || getArch() == Triple::aarch64_be ||
922            getArch() == Triple::aarch64_32;
923   }
924 
925   /// Tests whether the target is AArch64 and pointers are the size specified by
926   /// \p PointerWidth.
isAArch64(int PointerWidth)927   bool isAArch64(int PointerWidth) const {
928     assert(PointerWidth == 64 || PointerWidth == 32);
929     if (!isAArch64())
930       return false;
931     return getArch() == Triple::aarch64_32 ||
932                    getEnvironment() == Triple::GNUILP32
933                ? PointerWidth == 32
934                : PointerWidth == 64;
935   }
936 
937   /// Tests whether the target is 32-bit LoongArch.
isLoongArch32()938   bool isLoongArch32() const { return getArch() == Triple::loongarch32; }
939 
940   /// Tests whether the target is 64-bit LoongArch.
isLoongArch64()941   bool isLoongArch64() const { return getArch() == Triple::loongarch64; }
942 
943   /// Tests whether the target is LoongArch (32- and 64-bit).
isLoongArch()944   bool isLoongArch() const { return isLoongArch32() || isLoongArch64(); }
945 
946   /// Tests whether the target is MIPS 32-bit (little and big endian).
isMIPS32()947   bool isMIPS32() const {
948     return getArch() == Triple::mips || getArch() == Triple::mipsel;
949   }
950 
951   /// Tests whether the target is MIPS 64-bit (little and big endian).
isMIPS64()952   bool isMIPS64() const {
953     return getArch() == Triple::mips64 || getArch() == Triple::mips64el;
954   }
955 
956   /// Tests whether the target is MIPS (little and big endian, 32- or 64-bit).
isMIPS()957   bool isMIPS() const {
958     return isMIPS32() || isMIPS64();
959   }
960 
961   /// Tests whether the target is PowerPC (32- or 64-bit LE or BE).
isPPC()962   bool isPPC() const {
963     return getArch() == Triple::ppc || getArch() == Triple::ppc64 ||
964            getArch() == Triple::ppcle || getArch() == Triple::ppc64le;
965   }
966 
967   /// Tests whether the target is 32-bit PowerPC (little and big endian).
isPPC32()968   bool isPPC32() const {
969     return getArch() == Triple::ppc || getArch() == Triple::ppcle;
970   }
971 
972   /// Tests whether the target is 64-bit PowerPC (little and big endian).
isPPC64()973   bool isPPC64() const {
974     return getArch() == Triple::ppc64 || getArch() == Triple::ppc64le;
975   }
976 
977   /// Tests whether the target 64-bit PowerPC big endian ABI is ELFv2.
isPPC64ELFv2ABI()978   bool isPPC64ELFv2ABI() const {
979     return (getArch() == Triple::ppc64 &&
980             ((getOS() == Triple::FreeBSD &&
981               (getOSMajorVersion() >= 13 || getOSVersion().empty())) ||
982              getOS() == Triple::OpenBSD || isMusl()));
983   }
984 
985   /// Tests whether the target 32-bit PowerPC uses Secure PLT.
isPPC32SecurePlt()986   bool isPPC32SecurePlt() const {
987     return ((getArch() == Triple::ppc || getArch() == Triple::ppcle) &&
988             ((getOS() == Triple::FreeBSD &&
989               (getOSMajorVersion() >= 13 || getOSVersion().empty())) ||
990              getOS() == Triple::NetBSD || getOS() == Triple::OpenBSD ||
991              isMusl()));
992   }
993 
994   /// Tests whether the target is 32-bit RISC-V.
isRISCV32()995   bool isRISCV32() const { return getArch() == Triple::riscv32; }
996 
997   /// Tests whether the target is 64-bit RISC-V.
isRISCV64()998   bool isRISCV64() const { return getArch() == Triple::riscv64; }
999 
1000   /// Tests whether the target is RISC-V (32- and 64-bit).
isRISCV()1001   bool isRISCV() const { return isRISCV32() || isRISCV64(); }
1002 
1003   /// Tests whether the target is 32-bit SPARC (little and big endian).
isSPARC32()1004   bool isSPARC32() const {
1005     return getArch() == Triple::sparc || getArch() == Triple::sparcel;
1006   }
1007 
1008   /// Tests whether the target is 64-bit SPARC (big endian).
isSPARC64()1009   bool isSPARC64() const { return getArch() == Triple::sparcv9; }
1010 
1011   /// Tests whether the target is SPARC.
isSPARC()1012   bool isSPARC() const { return isSPARC32() || isSPARC64(); }
1013 
1014   /// Tests whether the target is SystemZ.
isSystemZ()1015   bool isSystemZ() const {
1016     return getArch() == Triple::systemz;
1017   }
1018 
1019   /// Tests whether the target is x86 (32- or 64-bit).
isX86()1020   bool isX86() const {
1021     return getArch() == Triple::x86 || getArch() == Triple::x86_64;
1022   }
1023 
1024   /// Tests whether the target is VE
isVE()1025   bool isVE() const {
1026     return getArch() == Triple::ve;
1027   }
1028 
1029   /// Tests whether the target is wasm (32- and 64-bit).
isWasm()1030   bool isWasm() const {
1031     return getArch() == Triple::wasm32 || getArch() == Triple::wasm64;
1032   }
1033 
1034   // Tests whether the target is CSKY
isCSKY()1035   bool isCSKY() const {
1036     return getArch() == Triple::csky;
1037   }
1038 
1039   /// Tests whether the target is the Apple "arm64e" AArch64 subarch.
isArm64e()1040   bool isArm64e() const {
1041     return getArch() == Triple::aarch64 &&
1042            getSubArch() == Triple::AArch64SubArch_arm64e;
1043   }
1044 
1045   /// Tests whether the target is X32.
isX32()1046   bool isX32() const {
1047     EnvironmentType Env = getEnvironment();
1048     return Env == Triple::GNUX32 || Env == Triple::MuslX32;
1049   }
1050 
1051   /// Tests whether the target is eBPF.
isBPF()1052   bool isBPF() const {
1053     return getArch() == Triple::bpfel || getArch() == Triple::bpfeb;
1054   }
1055 
1056   /// Tests if the target forces 64-bit time_t on a 32-bit architecture.
isTime64ABI()1057   bool isTime64ABI() const {
1058     EnvironmentType Env = getEnvironment();
1059     return Env == Triple::GNUT64 || Env == Triple::GNUEABIT64 ||
1060            Env == Triple::GNUEABIHFT64;
1061   }
1062 
1063   /// Tests if the target forces hardfloat.
isHardFloatABI()1064   bool isHardFloatABI() const {
1065     EnvironmentType Env = getEnvironment();
1066     return Env == llvm::Triple::GNUEABIHF ||
1067            Env == llvm::Triple::GNUEABIHFT64 ||
1068            Env == llvm::Triple::MuslEABIHF ||
1069            Env == llvm::Triple::EABIHF;
1070   }
1071 
1072   /// Tests whether the target supports comdat
supportsCOMDAT()1073   bool supportsCOMDAT() const {
1074     return !(isOSBinFormatMachO() || isOSBinFormatXCOFF() ||
1075              isOSBinFormatDXContainer());
1076   }
1077 
1078   /// Tests whether the target uses emulated TLS as default.
1079   ///
1080   /// Note: Android API level 29 (10) introduced ELF TLS.
hasDefaultEmulatedTLS()1081   bool hasDefaultEmulatedTLS() const {
1082     return (isAndroid() && isAndroidVersionLT(29)) || isOSOpenBSD() ||
1083            isWindowsCygwinEnvironment() || isOHOSFamily();
1084   }
1085 
1086   /// True if the target supports both general-dynamic and TLSDESC, and TLSDESC
1087   /// is enabled by default.
hasDefaultTLSDESC()1088   bool hasDefaultTLSDESC() const { return isAndroid() && isRISCV64(); }
1089 
1090   /// Tests whether the target uses -data-sections as default.
hasDefaultDataSections()1091   bool hasDefaultDataSections() const {
1092     return isOSBinFormatXCOFF() || isWasm();
1093   }
1094 
1095   /// Tests if the environment supports dllimport/export annotations.
hasDLLImportExport()1096   bool hasDLLImportExport() const { return isOSWindows() || isPS(); }
1097 
1098   /// @}
1099   /// @name Mutators
1100   /// @{
1101 
1102   /// Set the architecture (first) component of the triple to a known type.
1103   void setArch(ArchType Kind, SubArchType SubArch = NoSubArch);
1104 
1105   /// Set the vendor (second) component of the triple to a known type.
1106   void setVendor(VendorType Kind);
1107 
1108   /// Set the operating system (third) component of the triple to a known type.
1109   void setOS(OSType Kind);
1110 
1111   /// Set the environment (fourth) component of the triple to a known type.
1112   void setEnvironment(EnvironmentType Kind);
1113 
1114   /// Set the object file format.
1115   void setObjectFormat(ObjectFormatType Kind);
1116 
1117   /// Set all components to the new triple \p Str.
1118   void setTriple(const Twine &Str);
1119 
1120   /// Set the architecture (first) component of the triple by name.
1121   void setArchName(StringRef Str);
1122 
1123   /// Set the vendor (second) component of the triple by name.
1124   void setVendorName(StringRef Str);
1125 
1126   /// Set the operating system (third) component of the triple by name.
1127   void setOSName(StringRef Str);
1128 
1129   /// Set the optional environment (fourth) component of the triple by name.
1130   void setEnvironmentName(StringRef Str);
1131 
1132   /// Set the operating system and optional environment components with a single
1133   /// string.
1134   void setOSAndEnvironmentName(StringRef Str);
1135 
1136   /// @}
1137   /// @name Helpers to build variants of a particular triple.
1138   /// @{
1139 
1140   /// Form a triple with a 32-bit variant of the current architecture.
1141   ///
1142   /// This can be used to move across "families" of architectures where useful.
1143   ///
1144   /// \returns A new triple with a 32-bit architecture or an unknown
1145   ///          architecture if no such variant can be found.
1146   llvm::Triple get32BitArchVariant() const;
1147 
1148   /// Form a triple with a 64-bit variant of the current architecture.
1149   ///
1150   /// This can be used to move across "families" of architectures where useful.
1151   ///
1152   /// \returns A new triple with a 64-bit architecture or an unknown
1153   ///          architecture if no such variant can be found.
1154   llvm::Triple get64BitArchVariant() const;
1155 
1156   /// Form a triple with a big endian variant of the current architecture.
1157   ///
1158   /// This can be used to move across "families" of architectures where useful.
1159   ///
1160   /// \returns A new triple with a big endian architecture or an unknown
1161   ///          architecture if no such variant can be found.
1162   llvm::Triple getBigEndianArchVariant() const;
1163 
1164   /// Form a triple with a little endian variant of the current architecture.
1165   ///
1166   /// This can be used to move across "families" of architectures where useful.
1167   ///
1168   /// \returns A new triple with a little endian architecture or an unknown
1169   ///          architecture if no such variant can be found.
1170   llvm::Triple getLittleEndianArchVariant() const;
1171 
1172   /// Tests whether the target triple is little endian.
1173   ///
1174   /// \returns true if the triple is little endian, false otherwise.
1175   bool isLittleEndian() const;
1176 
1177   /// Test whether target triples are compatible.
1178   bool isCompatibleWith(const Triple &Other) const;
1179 
1180   /// Merge target triples.
1181   std::string merge(const Triple &Other) const;
1182 
1183   /// Some platforms have different minimum supported OS versions that
1184   /// varies by the architecture specified in the triple. This function
1185   /// returns the minimum supported OS version for this triple if one an exists,
1186   /// or an invalid version tuple if this triple doesn't have one.
1187   VersionTuple getMinimumSupportedOSVersion() const;
1188 
1189   /// @}
1190   /// @name Static helpers for IDs.
1191   /// @{
1192 
1193   /// Get the canonical name for the \p Kind architecture.
1194   static StringRef getArchTypeName(ArchType Kind);
1195 
1196   /// Get the architecture name based on \p Kind and \p SubArch.
1197   static StringRef getArchName(ArchType Kind, SubArchType SubArch = NoSubArch);
1198 
1199   /// Get the "prefix" canonical name for the \p Kind architecture. This is the
1200   /// prefix used by the architecture specific builtins, and is suitable for
1201   /// passing to \see Intrinsic::getIntrinsicForClangBuiltin().
1202   ///
1203   /// \return - The architecture prefix, or 0 if none is defined.
1204   static StringRef getArchTypePrefix(ArchType Kind);
1205 
1206   /// Get the canonical name for the \p Kind vendor.
1207   static StringRef getVendorTypeName(VendorType Kind);
1208 
1209   /// Get the canonical name for the \p Kind operating system.
1210   static StringRef getOSTypeName(OSType Kind);
1211 
1212   /// Get the canonical name for the \p Kind environment.
1213   static StringRef getEnvironmentTypeName(EnvironmentType Kind);
1214 
1215   /// Get the name for the \p Object format.
1216   static StringRef getObjectFormatTypeName(ObjectFormatType ObjectFormat);
1217 
1218   /// @}
1219   /// @name Static helpers for converting alternate architecture names.
1220   /// @{
1221 
1222   /// The canonical type for the given LLVM architecture name (e.g., "x86").
1223   static ArchType getArchTypeForLLVMName(StringRef Str);
1224 
1225   /// @}
1226 
1227   /// Returns a canonicalized OS version number for the specified OS.
1228   static VersionTuple getCanonicalVersionForOS(OSType OSKind,
1229                                                const VersionTuple &Version);
1230 };
1231 
1232 } // End llvm namespace
1233 
1234 
1235 #endif
1236