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