Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

447 lines
14 KiB

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