Triple.h 31 KB

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