Triple.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  1. //===-- llvm/ADT/Triple.h - Target triple helper class ----------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef LLVM_ADT_TRIPLE_H
  10. #define LLVM_ADT_TRIPLE_H
  11. #include "llvm/ADT/Twine.h"
  12. // Some system headers or GCC predefined macros conflict with identifiers in
  13. // this file. Undefine them here.
  14. #undef NetBSD
  15. #undef mips
  16. #undef sparc
  17. namespace llvm {
  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. bpfel, // eBPF or extended BPF or 64-bit BPF (little endian)
  48. bpfeb, // eBPF or extended BPF or 64-bit BPF (big endian)
  49. hexagon, // Hexagon: hexagon
  50. mips, // MIPS: mips, mipsallegrex
  51. mipsel, // MIPSEL: mipsel, mipsallegrexel
  52. mips64, // MIPS64: mips64
  53. mips64el, // MIPS64EL: mips64el
  54. msp430, // MSP430: msp430
  55. ppc, // PPC: powerpc
  56. ppc64, // PPC64: powerpc64, ppu
  57. ppc64le, // PPC64LE: powerpc64le
  58. r600, // R600: AMD GPUs HD2XXX - HD6XXX
  59. amdgcn, // AMDGCN: AMD GCN GPUs
  60. sparc, // Sparc: sparc
  61. sparcv9, // Sparcv9: Sparcv9
  62. sparcel, // Sparc: (endianness = little). NB: 'Sparcle' is a CPU variant
  63. systemz, // SystemZ: s390x
  64. tce, // TCE (http://tce.cs.tut.fi/): tce
  65. thumb, // Thumb (little endian): thumb, thumbv.*
  66. thumbeb, // Thumb (big endian): thumbeb
  67. x86, // X86: i[3-9]86
  68. x86_64, // X86-64: amd64, x86_64
  69. xcore, // XCore: xcore
  70. nvptx, // NVPTX: 32-bit
  71. nvptx64, // NVPTX: 64-bit
  72. le32, // le32: generic little-endian 32-bit CPU (PNaCl / Emscripten)
  73. le64, // le64: generic little-endian 64-bit CPU (PNaCl / Emscripten)
  74. amdil, // AMDIL
  75. amdil64, // AMDIL with 64-bit pointers
  76. hsail, // AMD HSAIL
  77. hsail64, // AMD HSAIL with 64-bit pointers
  78. spir, // SPIR: standard portable IR for OpenCL 32-bit version
  79. spir64, // SPIR: standard portable IR for OpenCL 64-bit version
  80. // HLSL Change Begins
  81. dxil, // DXIL: DirectX Intermediate Language 32-bit
  82. dxil64, // DXIL: DirectX Intermediate Language 64-bit
  83. // HLSL Change Ends
  84. kalimba, // Kalimba: generic kalimba
  85. shave, // SHAVE: Movidius vector VLIW processors
  86. wasm32, // WebAssembly with 32-bit pointers
  87. wasm64, // WebAssembly with 64-bit pointers
  88. LastArchType = wasm64
  89. };
  90. enum SubArchType {
  91. NoSubArch,
  92. ARMSubArch_v8_1a,
  93. ARMSubArch_v8,
  94. ARMSubArch_v7,
  95. ARMSubArch_v7em,
  96. ARMSubArch_v7m,
  97. ARMSubArch_v7s,
  98. ARMSubArch_v6,
  99. ARMSubArch_v6m,
  100. ARMSubArch_v6k,
  101. ARMSubArch_v6t2,
  102. ARMSubArch_v5,
  103. ARMSubArch_v5te,
  104. ARMSubArch_v4t,
  105. KalimbaSubArch_v3,
  106. KalimbaSubArch_v4,
  107. KalimbaSubArch_v5
  108. };
  109. enum VendorType {
  110. UnknownVendor,
  111. Apple,
  112. PC,
  113. SCEI,
  114. BGP,
  115. BGQ,
  116. Freescale,
  117. IBM,
  118. ImaginationTechnologies,
  119. MipsTechnologies,
  120. Microsoft, // HLSL Change
  121. NVIDIA,
  122. CSR,
  123. LastVendorType = CSR
  124. };
  125. enum OSType {
  126. UnknownOS,
  127. CloudABI,
  128. Darwin,
  129. DragonFly,
  130. FreeBSD,
  131. IOS,
  132. KFreeBSD,
  133. Linux,
  134. Lv2, // PS3
  135. MacOSX,
  136. NetBSD,
  137. OpenBSD,
  138. Solaris,
  139. Win32,
  140. Haiku,
  141. Minix,
  142. RTEMS,
  143. NaCl, // Native Client
  144. CNK, // BG/P Compute-Node Kernel
  145. Bitrig,
  146. AIX,
  147. CUDA, // NVIDIA CUDA
  148. NVCL, // NVIDIA OpenCL
  149. AMDHSA, // AMD HSA Runtime
  150. DirectX, // HLSL Change
  151. PS4,
  152. LastOSType = PS4
  153. };
  154. enum EnvironmentType {
  155. UnknownEnvironment,
  156. GNU,
  157. GNUEABI,
  158. GNUEABIHF,
  159. GNUX32,
  160. CODE16,
  161. EABI,
  162. EABIHF,
  163. Android,
  164. MSVC,
  165. Itanium,
  166. Cygnus,
  167. LastEnvironmentType = Cygnus
  168. };
  169. enum ObjectFormatType {
  170. UnknownObjectFormat,
  171. COFF,
  172. ELF,
  173. MachO,
  174. };
  175. private:
  176. std::string Data;
  177. /// The parsed arch type.
  178. ArchType Arch;
  179. /// The parsed subarchitecture type.
  180. SubArchType SubArch;
  181. /// The parsed vendor type.
  182. VendorType Vendor;
  183. /// The parsed OS type.
  184. OSType OS;
  185. /// The parsed Environment type.
  186. EnvironmentType Environment;
  187. /// The object format type.
  188. ObjectFormatType ObjectFormat;
  189. public:
  190. /// @name Constructors
  191. /// @{
  192. /// \brief Default constructor is the same as an empty string and leaves all
  193. /// triple fields unknown.
  194. Triple() : Data(), Arch(), Vendor(), OS(), Environment(), ObjectFormat() {}
  195. explicit Triple(const Twine &Str);
  196. Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr);
  197. Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
  198. const Twine &EnvironmentStr);
  199. bool operator==(const Triple &Other) const {
  200. return Arch == Other.Arch && SubArch == Other.SubArch &&
  201. Vendor == Other.Vendor && OS == Other.OS &&
  202. Environment == Other.Environment &&
  203. ObjectFormat == Other.ObjectFormat;
  204. }
  205. /// @}
  206. /// @name Normalization
  207. /// @{
  208. /// normalize - Turn an arbitrary machine specification into the canonical
  209. /// triple form (or something sensible that the Triple class understands if
  210. /// nothing better can reasonably be done). In particular, it handles the
  211. /// common case in which otherwise valid components are in the wrong order.
  212. static std::string normalize(StringRef Str);
  213. /// \brief Return the normalized form of this triple's string.
  214. std::string normalize() const { return normalize(Data); }
  215. /// @}
  216. /// @name Typed Component Access
  217. /// @{
  218. /// getArch - Get the parsed architecture type of this triple.
  219. ArchType getArch() const { return Arch; }
  220. /// getSubArch - get the parsed subarchitecture type for this triple.
  221. SubArchType getSubArch() const { return SubArch; }
  222. /// getVendor - Get the parsed vendor type of this triple.
  223. VendorType getVendor() const { return Vendor; }
  224. /// getOS - Get the parsed operating system type of this triple.
  225. OSType getOS() const { return OS; }
  226. /// hasEnvironment - Does this triple have the optional environment
  227. /// (fourth) component?
  228. bool hasEnvironment() const {
  229. return getEnvironmentName() != "";
  230. }
  231. /// getEnvironment - Get the parsed environment type of this triple.
  232. EnvironmentType getEnvironment() const { return Environment; }
  233. /// \brief Parse the version number from the OS name component of the
  234. /// triple, if present.
  235. ///
  236. /// For example, "fooos1.2.3" would return (1, 2, 3).
  237. ///
  238. /// If an entry is not defined, it will be returned as 0.
  239. void getEnvironmentVersion(unsigned &Major, unsigned &Minor,
  240. unsigned &Micro) const;
  241. /// getFormat - Get the object format for this triple.
  242. ObjectFormatType getObjectFormat() const { return ObjectFormat; }
  243. /// getOSVersion - Parse the version number from the OS name component of the
  244. /// triple, if present.
  245. ///
  246. /// For example, "fooos1.2.3" would return (1, 2, 3).
  247. ///
  248. /// If an entry is not defined, it will be returned as 0.
  249. void getOSVersion(unsigned &Major, unsigned &Minor, unsigned &Micro) const;
  250. /// getOSMajorVersion - Return just the major version number, this is
  251. /// specialized because it is a common query.
  252. unsigned getOSMajorVersion() const {
  253. unsigned Maj, Min, Micro;
  254. getOSVersion(Maj, Min, Micro);
  255. return Maj;
  256. }
  257. /// getMacOSXVersion - Parse the version number as with getOSVersion and then
  258. /// translate generic "darwin" versions to the corresponding OS X versions.
  259. /// This may also be called with IOS triples but the OS X version number is
  260. /// just set to a constant 10.4.0 in that case. Returns true if successful.
  261. bool getMacOSXVersion(unsigned &Major, unsigned &Minor,
  262. unsigned &Micro) const;
  263. /// getiOSVersion - Parse the version number as with getOSVersion. This should
  264. /// only be called with IOS triples.
  265. void getiOSVersion(unsigned &Major, unsigned &Minor,
  266. unsigned &Micro) const;
  267. /// @}
  268. /// @name Direct Component Access
  269. /// @{
  270. const std::string &str() const { return Data; }
  271. const std::string &getTriple() const { return Data; }
  272. /// getArchName - Get the architecture (first) component of the
  273. /// triple.
  274. StringRef getArchName() const;
  275. /// getVendorName - Get the vendor (second) component of the triple.
  276. StringRef getVendorName() const;
  277. /// getOSName - Get the operating system (third) component of the
  278. /// triple.
  279. StringRef getOSName() const;
  280. /// getEnvironmentName - Get the optional environment (fourth)
  281. /// component of the triple, or "" if empty.
  282. StringRef getEnvironmentName() const;
  283. /// getOSAndEnvironmentName - Get the operating system and optional
  284. /// environment components as a single string (separated by a '-'
  285. /// if the environment component is present).
  286. StringRef getOSAndEnvironmentName() const;
  287. /// @}
  288. /// @name Convenience Predicates
  289. /// @{
  290. /// \brief Test whether the architecture is 64-bit
  291. ///
  292. /// Note that this tests for 64-bit pointer width, and nothing else. Note
  293. /// that we intentionally expose only three predicates, 64-bit, 32-bit, and
  294. /// 16-bit. The inner details of pointer width for particular architectures
  295. /// is not summed up in the triple, and so only a coarse grained predicate
  296. /// system is provided.
  297. bool isArch64Bit() const;
  298. /// \brief Test whether the architecture is 32-bit
  299. ///
  300. /// Note that this tests for 32-bit pointer width, and nothing else.
  301. bool isArch32Bit() const;
  302. /// \brief Test whether the architecture is 16-bit
  303. ///
  304. /// Note that this tests for 16-bit pointer width, and nothing else.
  305. bool isArch16Bit() const;
  306. /// isOSVersionLT - Helper function for doing comparisons against version
  307. /// numbers included in the target triple.
  308. bool isOSVersionLT(unsigned Major, unsigned Minor = 0,
  309. unsigned Micro = 0) const {
  310. unsigned LHS[3];
  311. getOSVersion(LHS[0], LHS[1], LHS[2]);
  312. if (LHS[0] != Major)
  313. return LHS[0] < Major;
  314. if (LHS[1] != Minor)
  315. return LHS[1] < Minor;
  316. if (LHS[2] != Micro)
  317. return LHS[1] < Micro;
  318. return false;
  319. }
  320. bool isOSVersionLT(const Triple &Other) const {
  321. unsigned RHS[3];
  322. Other.getOSVersion(RHS[0], RHS[1], RHS[2]);
  323. return isOSVersionLT(RHS[0], RHS[1], RHS[2]);
  324. }
  325. /// isMacOSXVersionLT - Comparison function for checking OS X version
  326. /// compatibility, which handles supporting skewed version numbering schemes
  327. /// used by the "darwin" triples.
  328. unsigned isMacOSXVersionLT(unsigned Major, unsigned Minor = 0,
  329. unsigned Micro = 0) const {
  330. assert(isMacOSX() && "Not an OS X triple!");
  331. // If this is OS X, expect a sane version number.
  332. if (getOS() == Triple::MacOSX)
  333. return isOSVersionLT(Major, Minor, Micro);
  334. // Otherwise, compare to the "Darwin" number.
  335. assert(Major == 10 && "Unexpected major version");
  336. return isOSVersionLT(Minor + 4, Micro, 0);
  337. }
  338. /// isMacOSX - Is this a Mac OS X triple. For legacy reasons, we support both
  339. /// "darwin" and "osx" as OS X triples.
  340. bool isMacOSX() const {
  341. return getOS() == Triple::Darwin || getOS() == Triple::MacOSX;
  342. }
  343. /// Is this an iOS triple.
  344. bool isiOS() const {
  345. return getOS() == Triple::IOS;
  346. }
  347. /// isOSDarwin - Is this a "Darwin" OS (OS X or iOS).
  348. bool isOSDarwin() const {
  349. return isMacOSX() || isiOS();
  350. }
  351. bool isOSNetBSD() const {
  352. return getOS() == Triple::NetBSD;
  353. }
  354. bool isOSOpenBSD() const {
  355. return getOS() == Triple::OpenBSD;
  356. }
  357. bool isOSFreeBSD() const {
  358. return getOS() == Triple::FreeBSD;
  359. }
  360. bool isOSDragonFly() const { return getOS() == Triple::DragonFly; }
  361. bool isOSSolaris() const {
  362. return getOS() == Triple::Solaris;
  363. }
  364. bool isOSBitrig() const {
  365. return getOS() == Triple::Bitrig;
  366. }
  367. bool isWindowsMSVCEnvironment() const {
  368. return getOS() == Triple::Win32 &&
  369. (getEnvironment() == Triple::UnknownEnvironment ||
  370. getEnvironment() == Triple::MSVC);
  371. }
  372. bool isKnownWindowsMSVCEnvironment() const {
  373. return getOS() == Triple::Win32 && getEnvironment() == Triple::MSVC;
  374. }
  375. bool isWindowsItaniumEnvironment() const {
  376. return getOS() == Triple::Win32 && getEnvironment() == Triple::Itanium;
  377. }
  378. bool isWindowsCygwinEnvironment() const {
  379. return getOS() == Triple::Win32 && getEnvironment() == Triple::Cygnus;
  380. }
  381. bool isWindowsGNUEnvironment() const {
  382. return getOS() == Triple::Win32 && getEnvironment() == Triple::GNU;
  383. }
  384. /// \brief Tests for either Cygwin or MinGW OS
  385. bool isOSCygMing() const {
  386. return isWindowsCygwinEnvironment() || isWindowsGNUEnvironment();
  387. }
  388. /// \brief Is this a "Windows" OS targeting a "MSVCRT.dll" environment.
  389. bool isOSMSVCRT() const {
  390. return isWindowsMSVCEnvironment() || isWindowsGNUEnvironment() ||
  391. isWindowsItaniumEnvironment();
  392. }
  393. /// \brief Tests whether the OS is Windows.
  394. bool isOSWindows() const {
  395. return getOS() == Triple::Win32;
  396. }
  397. /// \brief Tests whether the OS is NaCl (Native Client)
  398. bool isOSNaCl() const {
  399. return getOS() == Triple::NaCl;
  400. }
  401. /// \brief Tests whether the OS is Linux.
  402. bool isOSLinux() const {
  403. return getOS() == Triple::Linux;
  404. }
  405. /// \brief Tests whether the OS uses the ELF binary format.
  406. bool isOSBinFormatELF() const {
  407. return getObjectFormat() == Triple::ELF;
  408. }
  409. /// \brief Tests whether the OS uses the COFF binary format.
  410. bool isOSBinFormatCOFF() const {
  411. return getObjectFormat() == Triple::COFF;
  412. }
  413. /// \brief Tests whether the environment is MachO.
  414. bool isOSBinFormatMachO() const {
  415. return getObjectFormat() == Triple::MachO;
  416. }
  417. /// \brief Tests whether the target is the PS4 CPU
  418. bool isPS4CPU() const {
  419. return getArch() == Triple::x86_64 &&
  420. getVendor() == Triple::SCEI &&
  421. getOS() == Triple::PS4;
  422. }
  423. /// \brief Tests whether the target is the PS4 platform
  424. bool isPS4() const {
  425. return getVendor() == Triple::SCEI &&
  426. getOS() == Triple::PS4;
  427. }
  428. /// @}
  429. /// @name Mutators
  430. /// @{
  431. /// setArch - Set the architecture (first) component of the triple
  432. /// to a known type.
  433. void setArch(ArchType Kind);
  434. /// setVendor - Set the vendor (second) component of the triple to a
  435. /// known type.
  436. void setVendor(VendorType Kind);
  437. /// setOS - Set the operating system (third) component of the triple
  438. /// to a known type.
  439. void setOS(OSType Kind);
  440. /// setEnvironment - Set the environment (fourth) component of the triple
  441. /// to a known type.
  442. void setEnvironment(EnvironmentType Kind);
  443. /// setObjectFormat - Set the object file format
  444. void setObjectFormat(ObjectFormatType Kind);
  445. /// setTriple - Set all components to the new triple \p Str.
  446. void setTriple(const Twine &Str);
  447. /// setArchName - Set the architecture (first) component of the
  448. /// triple by name.
  449. void setArchName(StringRef Str);
  450. /// setVendorName - Set the vendor (second) component of the triple
  451. /// by name.
  452. void setVendorName(StringRef Str);
  453. /// setOSName - Set the operating system (third) component of the
  454. /// triple by name.
  455. void setOSName(StringRef Str);
  456. /// setEnvironmentName - Set the optional environment (fourth)
  457. /// component of the triple by name.
  458. void setEnvironmentName(StringRef Str);
  459. /// setOSAndEnvironmentName - Set the operating system and optional
  460. /// environment components with a single string.
  461. void setOSAndEnvironmentName(StringRef Str);
  462. /// @}
  463. /// @name Helpers to build variants of a particular triple.
  464. /// @{
  465. /// \brief Form a triple with a 32-bit variant of the current architecture.
  466. ///
  467. /// This can be used to move across "families" of architectures where useful.
  468. ///
  469. /// \returns A new triple with a 32-bit architecture or an unknown
  470. /// architecture if no such variant can be found.
  471. llvm::Triple get32BitArchVariant() const;
  472. /// \brief Form a triple with a 64-bit variant of the current architecture.
  473. ///
  474. /// This can be used to move across "families" of architectures where useful.
  475. ///
  476. /// \returns A new triple with a 64-bit architecture or an unknown
  477. /// architecture if no such variant can be found.
  478. llvm::Triple get64BitArchVariant() const;
  479. /// Form a triple with a big endian variant of the current architecture.
  480. ///
  481. /// This can be used to move across "families" of architectures where useful.
  482. ///
  483. /// \returns A new triple with a big endian architecture or an unknown
  484. /// architecture if no such variant can be found.
  485. llvm::Triple getBigEndianArchVariant() const;
  486. /// Form a triple with a little endian variant of the current architecture.
  487. ///
  488. /// This can be used to move across "families" of architectures where useful.
  489. ///
  490. /// \returns A new triple with a little endian architecture or an unknown
  491. /// architecture if no such variant can be found.
  492. llvm::Triple getLittleEndianArchVariant() const;
  493. /// Get the (LLVM) name of the minimum ARM CPU for the arch we are targeting.
  494. ///
  495. /// \param Arch the architecture name (e.g., "armv7s"). If it is an empty
  496. /// string then the triple's arch name is used.
  497. const char* getARMCPUForArch(StringRef Arch = StringRef()) const;
  498. /// @}
  499. /// @name Static helpers for IDs.
  500. /// @{
  501. /// getArchTypeName - Get the canonical name for the \p Kind architecture.
  502. static const char *getArchTypeName(ArchType Kind);
  503. /// getArchTypePrefix - Get the "prefix" canonical name for the \p Kind
  504. /// architecture. This is the prefix used by the architecture specific
  505. /// builtins, and is suitable for passing to \see
  506. /// Intrinsic::getIntrinsicForGCCBuiltin().
  507. ///
  508. /// \return - The architecture prefix, or 0 if none is defined.
  509. static const char *getArchTypePrefix(ArchType Kind);
  510. /// getVendorTypeName - Get the canonical name for the \p Kind vendor.
  511. static const char *getVendorTypeName(VendorType Kind);
  512. /// getOSTypeName - Get the canonical name for the \p Kind operating system.
  513. static const char *getOSTypeName(OSType Kind);
  514. /// getEnvironmentTypeName - Get the canonical name for the \p Kind
  515. /// environment.
  516. static const char *getEnvironmentTypeName(EnvironmentType Kind);
  517. /// @}
  518. /// @name Static helpers for converting alternate architecture names.
  519. /// @{
  520. /// getArchTypeForLLVMName - The canonical type for the given LLVM
  521. /// architecture name (e.g., "x86").
  522. static ArchType getArchTypeForLLVMName(StringRef Str);
  523. /// @}
  524. };
  525. } // End llvm namespace
  526. #endif