Triple.h 30 KB

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