/3rd_party/llvm/include/llvm/ADT/Triple.h

https://code.google.com/p/softart/ · C++ Header · 464 lines · 202 code · 85 blank · 177 comment · 30 complexity · 1c89338e9e8c6f480eff844f8ad71d5c MD5 · raw file

  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: arm, armv.*, xscale
  44. aarch64, // AArch64: aarch64
  45. hexagon, // Hexagon: hexagon
  46. mips, // MIPS: mips, mipsallegrex
  47. mipsel, // MIPSEL: mipsel, mipsallegrexel
  48. mips64, // MIPS64: mips64
  49. mips64el,// MIPS64EL: mips64el
  50. msp430, // MSP430: msp430
  51. ppc, // PPC: powerpc
  52. ppc64, // PPC64: powerpc64, ppu
  53. ppc64le, // PPC64LE: powerpc64le
  54. r600, // R600: AMD GPUs HD2XXX - HD6XXX
  55. sparc, // Sparc: sparc
  56. sparcv9, // Sparcv9: Sparcv9
  57. systemz, // SystemZ: s390x
  58. tce, // TCE (http://tce.cs.tut.fi/): tce
  59. thumb, // Thumb: thumb, thumbv.*
  60. x86, // X86: i[3-9]86
  61. x86_64, // X86-64: amd64, x86_64
  62. xcore, // XCore: xcore
  63. nvptx, // NVPTX: 32-bit
  64. nvptx64, // NVPTX: 64-bit
  65. le32, // le32: generic little-endian 32-bit CPU (PNaCl / Emscripten)
  66. amdil, // amdil: amd IL
  67. spir, // SPIR: standard portable IR for OpenCL 32-bit version
  68. spir64 // SPIR: standard portable IR for OpenCL 64-bit version
  69. };
  70. enum VendorType {
  71. UnknownVendor,
  72. Apple,
  73. PC,
  74. SCEI,
  75. BGP,
  76. BGQ,
  77. Freescale,
  78. IBM,
  79. NVIDIA
  80. };
  81. enum OSType {
  82. UnknownOS,
  83. AuroraUX,
  84. Cygwin,
  85. Darwin,
  86. DragonFly,
  87. FreeBSD,
  88. IOS,
  89. KFreeBSD,
  90. Linux,
  91. Lv2, // PS3
  92. MacOSX,
  93. MinGW32, // i*86-pc-mingw32, *-w64-mingw32
  94. NetBSD,
  95. OpenBSD,
  96. Solaris,
  97. Win32,
  98. Haiku,
  99. Minix,
  100. RTEMS,
  101. NaCl, // Native Client
  102. CNK, // BG/P Compute-Node Kernel
  103. Bitrig,
  104. AIX,
  105. CUDA, // NVIDIA CUDA
  106. NVCL // NVIDIA OpenCL
  107. };
  108. enum EnvironmentType {
  109. UnknownEnvironment,
  110. GNU,
  111. GNUEABI,
  112. GNUEABIHF,
  113. GNUX32,
  114. EABI,
  115. MachO,
  116. Android,
  117. ELF
  118. };
  119. private:
  120. std::string Data;
  121. /// The parsed arch type.
  122. ArchType Arch;
  123. /// The parsed vendor type.
  124. VendorType Vendor;
  125. /// The parsed OS type.
  126. OSType OS;
  127. /// The parsed Environment type.
  128. EnvironmentType Environment;
  129. public:
  130. /// @name Constructors
  131. /// @{
  132. /// \brief Default constructor is the same as an empty string and leaves all
  133. /// triple fields unknown.
  134. Triple() : Data(), Arch(), Vendor(), OS(), Environment() {}
  135. explicit Triple(const Twine &Str);
  136. Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr);
  137. Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
  138. const Twine &EnvironmentStr);
  139. /// @}
  140. /// @name Normalization
  141. /// @{
  142. /// normalize - Turn an arbitrary machine specification into the canonical
  143. /// triple form (or something sensible that the Triple class understands if
  144. /// nothing better can reasonably be done). In particular, it handles the
  145. /// common case in which otherwise valid components are in the wrong order.
  146. static std::string normalize(StringRef Str);
  147. /// @}
  148. /// @name Typed Component Access
  149. /// @{
  150. /// getArch - Get the parsed architecture type of this triple.
  151. ArchType getArch() const { return Arch; }
  152. /// getVendor - Get the parsed vendor type of this triple.
  153. VendorType getVendor() const { return Vendor; }
  154. /// getOS - Get the parsed operating system type of this triple.
  155. OSType getOS() const { return OS; }
  156. /// hasEnvironment - Does this triple have the optional environment
  157. /// (fourth) component?
  158. bool hasEnvironment() const {
  159. return getEnvironmentName() != "";
  160. }
  161. /// getEnvironment - Get the parsed environment type of this triple.
  162. EnvironmentType getEnvironment() const { return Environment; }
  163. /// getOSVersion - Parse the version number from the OS name component of the
  164. /// triple, if present.
  165. ///
  166. /// For example, "fooos1.2.3" would return (1, 2, 3).
  167. ///
  168. /// If an entry is not defined, it will be returned as 0.
  169. void getOSVersion(unsigned &Major, unsigned &Minor, unsigned &Micro) const;
  170. /// getOSMajorVersion - Return just the major version number, this is
  171. /// specialized because it is a common query.
  172. unsigned getOSMajorVersion() const {
  173. unsigned Maj, Min, Micro;
  174. getOSVersion(Maj, Min, Micro);
  175. return Maj;
  176. }
  177. /// getMacOSXVersion - Parse the version number as with getOSVersion and then
  178. /// translate generic "darwin" versions to the corresponding OS X versions.
  179. /// This may also be called with IOS triples but the OS X version number is
  180. /// just set to a constant 10.4.0 in that case. Returns true if successful.
  181. bool getMacOSXVersion(unsigned &Major, unsigned &Minor,
  182. unsigned &Micro) const;
  183. /// getiOSVersion - Parse the version number as with getOSVersion. This should
  184. /// only be called with IOS triples.
  185. void getiOSVersion(unsigned &Major, unsigned &Minor,
  186. unsigned &Micro) const;
  187. /// @}
  188. /// @name Direct Component Access
  189. /// @{
  190. const std::string &str() const { return Data; }
  191. const std::string &getTriple() const { return Data; }
  192. /// getArchName - Get the architecture (first) component of the
  193. /// triple.
  194. StringRef getArchName() const;
  195. /// getVendorName - Get the vendor (second) component of the triple.
  196. StringRef getVendorName() const;
  197. /// getOSName - Get the operating system (third) component of the
  198. /// triple.
  199. StringRef getOSName() const;
  200. /// getEnvironmentName - Get the optional environment (fourth)
  201. /// component of the triple, or "" if empty.
  202. StringRef getEnvironmentName() const;
  203. /// getOSAndEnvironmentName - Get the operating system and optional
  204. /// environment components as a single string (separated by a '-'
  205. /// if the environment component is present).
  206. StringRef getOSAndEnvironmentName() const;
  207. /// @}
  208. /// @name Convenience Predicates
  209. /// @{
  210. /// \brief Test whether the architecture is 64-bit
  211. ///
  212. /// Note that this tests for 64-bit pointer width, and nothing else. Note
  213. /// that we intentionally expose only three predicates, 64-bit, 32-bit, and
  214. /// 16-bit. The inner details of pointer width for particular architectures
  215. /// is not summed up in the triple, and so only a coarse grained predicate
  216. /// system is provided.
  217. bool isArch64Bit() const;
  218. /// \brief Test whether the architecture is 32-bit
  219. ///
  220. /// Note that this tests for 32-bit pointer width, and nothing else.
  221. bool isArch32Bit() const;
  222. /// \brief Test whether the architecture is 16-bit
  223. ///
  224. /// Note that this tests for 16-bit pointer width, and nothing else.
  225. bool isArch16Bit() const;
  226. /// isOSVersionLT - Helper function for doing comparisons against version
  227. /// numbers included in the target triple.
  228. bool isOSVersionLT(unsigned Major, unsigned Minor = 0,
  229. unsigned Micro = 0) const {
  230. unsigned LHS[3];
  231. getOSVersion(LHS[0], LHS[1], LHS[2]);
  232. if (LHS[0] != Major)
  233. return LHS[0] < Major;
  234. if (LHS[1] != Minor)
  235. return LHS[1] < Minor;
  236. if (LHS[2] != Micro)
  237. return LHS[1] < Micro;
  238. return false;
  239. }
  240. /// isMacOSXVersionLT - Comparison function for checking OS X version
  241. /// compatibility, which handles supporting skewed version numbering schemes
  242. /// used by the "darwin" triples.
  243. unsigned isMacOSXVersionLT(unsigned Major, unsigned Minor = 0,
  244. unsigned Micro = 0) const {
  245. assert(isMacOSX() && "Not an OS X triple!");
  246. // If this is OS X, expect a sane version number.
  247. if (getOS() == Triple::MacOSX)
  248. return isOSVersionLT(Major, Minor, Micro);
  249. // Otherwise, compare to the "Darwin" number.
  250. assert(Major == 10 && "Unexpected major version");
  251. return isOSVersionLT(Minor + 4, Micro, 0);
  252. }
  253. /// isMacOSX - Is this a Mac OS X triple. For legacy reasons, we support both
  254. /// "darwin" and "osx" as OS X triples.
  255. bool isMacOSX() const {
  256. return getOS() == Triple::Darwin || getOS() == Triple::MacOSX;
  257. }
  258. /// Is this an iOS triple.
  259. bool isiOS() const {
  260. return getOS() == Triple::IOS;
  261. }
  262. /// isOSDarwin - Is this a "Darwin" OS (OS X or iOS).
  263. bool isOSDarwin() const {
  264. return isMacOSX() || isiOS();
  265. }
  266. /// \brief Tests for either Cygwin or MinGW OS
  267. bool isOSCygMing() const {
  268. return getOS() == Triple::Cygwin || getOS() == Triple::MinGW32;
  269. }
  270. /// \brief Is this a "Windows" OS targeting a "MSVCRT.dll" environment.
  271. bool isOSMSVCRT() const {
  272. return getOS() == Triple::Win32 || getOS() == Triple::MinGW32;
  273. }
  274. /// \brief Tests whether the OS is Windows.
  275. bool isOSWindows() const {
  276. return getOS() == Triple::Win32 || isOSCygMing();
  277. }
  278. /// \brief Tests whether the OS is NaCl (Native Client)
  279. bool isOSNaCl() const {
  280. return getOS() == Triple::NaCl;
  281. }
  282. /// \brief Tests whether the OS is Linux.
  283. bool isOSLinux() const {
  284. return getOS() == Triple::Linux;
  285. }
  286. /// \brief Tests whether the OS uses the ELF binary format.
  287. bool isOSBinFormatELF() const {
  288. return !isOSDarwin() && !isOSWindows();
  289. }
  290. /// \brief Tests whether the OS uses the COFF binary format.
  291. bool isOSBinFormatCOFF() const {
  292. return isOSWindows();
  293. }
  294. /// \brief Tests whether the environment is MachO.
  295. // FIXME: Should this be an OSBinFormat predicate?
  296. bool isEnvironmentMachO() const {
  297. return getEnvironment() == Triple::MachO || isOSDarwin();
  298. }
  299. /// @}
  300. /// @name Mutators
  301. /// @{
  302. /// setArch - Set the architecture (first) component of the triple
  303. /// to a known type.
  304. void setArch(ArchType Kind);
  305. /// setVendor - Set the vendor (second) component of the triple to a
  306. /// known type.
  307. void setVendor(VendorType Kind);
  308. /// setOS - Set the operating system (third) component of the triple
  309. /// to a known type.
  310. void setOS(OSType Kind);
  311. /// setEnvironment - Set the environment (fourth) component of the triple
  312. /// to a known type.
  313. void setEnvironment(EnvironmentType Kind);
  314. /// setTriple - Set all components to the new triple \p Str.
  315. void setTriple(const Twine &Str);
  316. /// setArchName - Set the architecture (first) component of the
  317. /// triple by name.
  318. void setArchName(StringRef Str);
  319. /// setVendorName - Set the vendor (second) component of the triple
  320. /// by name.
  321. void setVendorName(StringRef Str);
  322. /// setOSName - Set the operating system (third) component of the
  323. /// triple by name.
  324. void setOSName(StringRef Str);
  325. /// setEnvironmentName - Set the optional environment (fourth)
  326. /// component of the triple by name.
  327. void setEnvironmentName(StringRef Str);
  328. /// setOSAndEnvironmentName - Set the operating system and optional
  329. /// environment components with a single string.
  330. void setOSAndEnvironmentName(StringRef Str);
  331. /// getArchNameForAssembler - Get an architecture name that is understood by
  332. /// the target assembler.
  333. const char *getArchNameForAssembler();
  334. /// @}
  335. /// @name Helpers to build variants of a particular triple.
  336. /// @{
  337. /// \brief Form a triple with a 32-bit variant of the current architecture.
  338. ///
  339. /// This can be used to move across "families" of architectures where useful.
  340. ///
  341. /// \returns A new triple with a 32-bit architecture or an unknown
  342. /// architecture if no such variant can be found.
  343. llvm::Triple get32BitArchVariant() const;
  344. /// \brief Form a triple with a 64-bit variant of the current architecture.
  345. ///
  346. /// This can be used to move across "families" of architectures where useful.
  347. ///
  348. /// \returns A new triple with a 64-bit architecture or an unknown
  349. /// architecture if no such variant can be found.
  350. llvm::Triple get64BitArchVariant() const;
  351. /// @}
  352. /// @name Static helpers for IDs.
  353. /// @{
  354. /// getArchTypeName - Get the canonical name for the \p Kind architecture.
  355. static const char *getArchTypeName(ArchType Kind);
  356. /// getArchTypePrefix - Get the "prefix" canonical name for the \p Kind
  357. /// architecture. This is the prefix used by the architecture specific
  358. /// builtins, and is suitable for passing to \see
  359. /// Intrinsic::getIntrinsicForGCCBuiltin().
  360. ///
  361. /// \return - The architecture prefix, or 0 if none is defined.
  362. static const char *getArchTypePrefix(ArchType Kind);
  363. /// getVendorTypeName - Get the canonical name for the \p Kind vendor.
  364. static const char *getVendorTypeName(VendorType Kind);
  365. /// getOSTypeName - Get the canonical name for the \p Kind operating system.
  366. static const char *getOSTypeName(OSType Kind);
  367. /// getEnvironmentTypeName - Get the canonical name for the \p Kind
  368. /// environment.
  369. static const char *getEnvironmentTypeName(EnvironmentType Kind);
  370. /// @}
  371. /// @name Static helpers for converting alternate architecture names.
  372. /// @{
  373. /// getArchTypeForLLVMName - The canonical type for the given LLVM
  374. /// architecture name (e.g., "x86").
  375. static ArchType getArchTypeForLLVMName(StringRef Str);
  376. /// @}
  377. };
  378. } // End llvm namespace
  379. #endif