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