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