Triple.h 29 KB

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