PageRenderTime 58ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/Support/Triple.cpp

https://github.com/chyyuu/llvm
C++ | 1027 lines | 837 code | 98 blank | 92 comment | 134 complexity | 28a36a470220d051579530b01460dfbd MD5 | raw file
  1. //===--- Triple.cpp - Target triple helper class --------------------------===//
  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. #include "llvm/ADT/Triple.h"
  10. #include "llvm/ADT/STLExtras.h"
  11. #include "llvm/ADT/SmallString.h"
  12. #include "llvm/ADT/StringSwitch.h"
  13. #include "llvm/Support/ErrorHandling.h"
  14. #include <cstring>
  15. using namespace llvm;
  16. const char *Triple::getArchTypeName(ArchType Kind) {
  17. switch (Kind) {
  18. case UnknownArch: return "unknown";
  19. case aarch64: return "aarch64";
  20. case aarch64_be: return "aarch64_be";
  21. case arm: return "arm";
  22. case armeb: return "armeb";
  23. case hexagon: return "hexagon";
  24. case mips: return "mips";
  25. case mipsel: return "mipsel";
  26. case mips64: return "mips64";
  27. case mips64el: return "mips64el";
  28. case msp430: return "msp430";
  29. case ppc64: return "powerpc64";
  30. case ppc64le: return "powerpc64le";
  31. case ppc: return "powerpc";
  32. case r600: return "r600";
  33. case sparc: return "sparc";
  34. case sparcv9: return "sparcv9";
  35. case systemz: return "s390x";
  36. case tce: return "tce";
  37. case thumb: return "thumb";
  38. case thumbeb: return "thumbeb";
  39. case x86: return "i386";
  40. case x86_64: return "x86_64";
  41. case xcore: return "xcore";
  42. case nvptx: return "nvptx";
  43. case nvptx64: return "nvptx64";
  44. case le32: return "le32";
  45. case amdil: return "amdil";
  46. case spir: return "spir";
  47. case spir64: return "spir64";
  48. case kalimba: return "kalimba";
  49. }
  50. llvm_unreachable("Invalid ArchType!");
  51. }
  52. const char *Triple::getArchTypePrefix(ArchType Kind) {
  53. switch (Kind) {
  54. default:
  55. return nullptr;
  56. case aarch64:
  57. case aarch64_be: return "aarch64";
  58. case arm:
  59. case armeb:
  60. case thumb:
  61. case thumbeb: return "arm";
  62. case ppc64:
  63. case ppc64le:
  64. case ppc: return "ppc";
  65. case mips:
  66. case mipsel:
  67. case mips64:
  68. case mips64el: return "mips";
  69. case hexagon: return "hexagon";
  70. case r600: return "r600";
  71. case sparcv9:
  72. case sparc: return "sparc";
  73. case systemz: return "systemz";
  74. case x86:
  75. case x86_64: return "x86";
  76. case xcore: return "xcore";
  77. case nvptx: return "nvptx";
  78. case nvptx64: return "nvptx";
  79. case le32: return "le32";
  80. case amdil: return "amdil";
  81. case spir: return "spir";
  82. case spir64: return "spir";
  83. case kalimba: return "kalimba";
  84. }
  85. }
  86. const char *Triple::getVendorTypeName(VendorType Kind) {
  87. switch (Kind) {
  88. case UnknownVendor: return "unknown";
  89. case Apple: return "apple";
  90. case PC: return "pc";
  91. case SCEI: return "scei";
  92. case BGP: return "bgp";
  93. case BGQ: return "bgq";
  94. case Freescale: return "fsl";
  95. case IBM: return "ibm";
  96. case ImaginationTechnologies: return "img";
  97. case MipsTechnologies: return "mti";
  98. case NVIDIA: return "nvidia";
  99. case CSR: return "csr";
  100. }
  101. llvm_unreachable("Invalid VendorType!");
  102. }
  103. const char *Triple::getOSTypeName(OSType Kind) {
  104. switch (Kind) {
  105. case UnknownOS: return "unknown";
  106. case AuroraUX: return "auroraux";
  107. case Cygwin: return "cygwin";
  108. case Darwin: return "darwin";
  109. case DragonFly: return "dragonfly";
  110. case FreeBSD: return "freebsd";
  111. case IOS: return "ios";
  112. case KFreeBSD: return "kfreebsd";
  113. case Linux: return "linux";
  114. case Lv2: return "lv2";
  115. case MacOSX: return "macosx";
  116. case MinGW32: return "mingw32";
  117. case NetBSD: return "netbsd";
  118. case OpenBSD: return "openbsd";
  119. case Solaris: return "solaris";
  120. case Win32: return "windows";
  121. case Haiku: return "haiku";
  122. case Minix: return "minix";
  123. case RTEMS: return "rtems";
  124. case NaCl: return "nacl";
  125. case CNK: return "cnk";
  126. case Bitrig: return "bitrig";
  127. case AIX: return "aix";
  128. case CUDA: return "cuda";
  129. case NVCL: return "nvcl";
  130. }
  131. llvm_unreachable("Invalid OSType");
  132. }
  133. const char *Triple::getEnvironmentTypeName(EnvironmentType Kind) {
  134. switch (Kind) {
  135. case UnknownEnvironment: return "unknown";
  136. case GNU: return "gnu";
  137. case GNUEABIHF: return "gnueabihf";
  138. case GNUEABI: return "gnueabi";
  139. case GNUX32: return "gnux32";
  140. case CODE16: return "code16";
  141. case EABI: return "eabi";
  142. case EABIHF: return "eabihf";
  143. case Android: return "android";
  144. case MSVC: return "msvc";
  145. case Itanium: return "itanium";
  146. case Cygnus: return "cygnus";
  147. }
  148. llvm_unreachable("Invalid EnvironmentType!");
  149. }
  150. Triple::ArchType Triple::getArchTypeForLLVMName(StringRef Name) {
  151. return StringSwitch<Triple::ArchType>(Name)
  152. .Case("aarch64", aarch64)
  153. .Case("aarch64_be", aarch64_be)
  154. .Case("arm64", aarch64) // "arm64" is an alias for "aarch64"
  155. .Case("arm", arm)
  156. .Case("armeb", armeb)
  157. .Case("mips", mips)
  158. .Case("mipsel", mipsel)
  159. .Case("mips64", mips64)
  160. .Case("mips64el", mips64el)
  161. .Case("msp430", msp430)
  162. .Case("ppc64", ppc64)
  163. .Case("ppc32", ppc)
  164. .Case("ppc", ppc)
  165. .Case("ppc64le", ppc64le)
  166. .Case("r600", r600)
  167. .Case("hexagon", hexagon)
  168. .Case("sparc", sparc)
  169. .Case("sparcv9", sparcv9)
  170. .Case("systemz", systemz)
  171. .Case("tce", tce)
  172. .Case("thumb", thumb)
  173. .Case("thumbeb", thumbeb)
  174. .Case("x86", x86)
  175. .Case("x86-64", x86_64)
  176. .Case("xcore", xcore)
  177. .Case("nvptx", nvptx)
  178. .Case("nvptx64", nvptx64)
  179. .Case("le32", le32)
  180. .Case("amdil", amdil)
  181. .Case("spir", spir)
  182. .Case("spir64", spir64)
  183. .Case("kalimba", kalimba)
  184. .Default(UnknownArch);
  185. }
  186. // Returns architecture name that is understood by the target assembler.
  187. const char *Triple::getArchNameForAssembler() {
  188. if (!isOSDarwin() && getVendor() != Triple::Apple)
  189. return nullptr;
  190. return StringSwitch<const char*>(getArchName())
  191. .Case("i386", "i386")
  192. .Case("x86_64", "x86_64")
  193. .Case("powerpc", "ppc")
  194. .Case("powerpc64", "ppc64")
  195. .Case("powerpc64le", "ppc64le")
  196. .Case("arm", "arm")
  197. .Cases("armv4t", "thumbv4t", "armv4t")
  198. .Cases("armv5", "armv5e", "thumbv5", "thumbv5e", "armv5")
  199. .Cases("armv6", "thumbv6", "armv6")
  200. .Cases("armv7", "thumbv7", "armv7")
  201. .Case("armeb", "armeb")
  202. .Case("arm64", "arm64")
  203. .Case("r600", "r600")
  204. .Case("nvptx", "nvptx")
  205. .Case("nvptx64", "nvptx64")
  206. .Case("le32", "le32")
  207. .Case("amdil", "amdil")
  208. .Case("spir", "spir")
  209. .Case("spir64", "spir64")
  210. .Default(nullptr);
  211. }
  212. static Triple::ArchType parseArch(StringRef ArchName) {
  213. return StringSwitch<Triple::ArchType>(ArchName)
  214. .Cases("i386", "i486", "i586", "i686", Triple::x86)
  215. // FIXME: Do we need to support these?
  216. .Cases("i786", "i886", "i986", Triple::x86)
  217. .Cases("amd64", "x86_64", "x86_64h", Triple::x86_64)
  218. .Case("powerpc", Triple::ppc)
  219. .Cases("powerpc64", "ppu", Triple::ppc64)
  220. .Case("powerpc64le", Triple::ppc64le)
  221. .Case("aarch64", Triple::aarch64)
  222. .Case("aarch64_be", Triple::aarch64_be)
  223. .Case("arm64", Triple::aarch64)
  224. .Cases("arm", "xscale", Triple::arm)
  225. // FIXME: It would be good to replace these with explicit names for all the
  226. // various suffixes supported.
  227. .StartsWith("armv", Triple::arm)
  228. .Case("armeb", Triple::armeb)
  229. .StartsWith("armebv", Triple::armeb)
  230. .Case("thumb", Triple::thumb)
  231. .StartsWith("thumbv", Triple::thumb)
  232. .Case("thumbeb", Triple::thumbeb)
  233. .StartsWith("thumbebv", Triple::thumbeb)
  234. .Case("msp430", Triple::msp430)
  235. .Cases("mips", "mipseb", "mipsallegrex", Triple::mips)
  236. .Cases("mipsel", "mipsallegrexel", Triple::mipsel)
  237. .Cases("mips64", "mips64eb", Triple::mips64)
  238. .Case("mips64el", Triple::mips64el)
  239. .Case("r600", Triple::r600)
  240. .Case("hexagon", Triple::hexagon)
  241. .Case("s390x", Triple::systemz)
  242. .Case("sparc", Triple::sparc)
  243. .Cases("sparcv9", "sparc64", Triple::sparcv9)
  244. .Case("tce", Triple::tce)
  245. .Case("xcore", Triple::xcore)
  246. .Case("nvptx", Triple::nvptx)
  247. .Case("nvptx64", Triple::nvptx64)
  248. .Case("le32", Triple::le32)
  249. .Case("amdil", Triple::amdil)
  250. .Case("spir", Triple::spir)
  251. .Case("spir64", Triple::spir64)
  252. .Case("kalimba", Triple::kalimba)
  253. .Default(Triple::UnknownArch);
  254. }
  255. static Triple::VendorType parseVendor(StringRef VendorName) {
  256. return StringSwitch<Triple::VendorType>(VendorName)
  257. .Case("apple", Triple::Apple)
  258. .Case("pc", Triple::PC)
  259. .Case("scei", Triple::SCEI)
  260. .Case("bgp", Triple::BGP)
  261. .Case("bgq", Triple::BGQ)
  262. .Case("fsl", Triple::Freescale)
  263. .Case("ibm", Triple::IBM)
  264. .Case("img", Triple::ImaginationTechnologies)
  265. .Case("mti", Triple::MipsTechnologies)
  266. .Case("nvidia", Triple::NVIDIA)
  267. .Case("csr", Triple::CSR)
  268. .Default(Triple::UnknownVendor);
  269. }
  270. static Triple::OSType parseOS(StringRef OSName) {
  271. return StringSwitch<Triple::OSType>(OSName)
  272. .StartsWith("auroraux", Triple::AuroraUX)
  273. .StartsWith("cygwin", Triple::Cygwin)
  274. .StartsWith("darwin", Triple::Darwin)
  275. .StartsWith("dragonfly", Triple::DragonFly)
  276. .StartsWith("freebsd", Triple::FreeBSD)
  277. .StartsWith("ios", Triple::IOS)
  278. .StartsWith("kfreebsd", Triple::KFreeBSD)
  279. .StartsWith("linux", Triple::Linux)
  280. .StartsWith("lv2", Triple::Lv2)
  281. .StartsWith("macosx", Triple::MacOSX)
  282. .StartsWith("mingw32", Triple::MinGW32)
  283. .StartsWith("netbsd", Triple::NetBSD)
  284. .StartsWith("openbsd", Triple::OpenBSD)
  285. .StartsWith("solaris", Triple::Solaris)
  286. .StartsWith("win32", Triple::Win32)
  287. .StartsWith("windows", Triple::Win32)
  288. .StartsWith("haiku", Triple::Haiku)
  289. .StartsWith("minix", Triple::Minix)
  290. .StartsWith("rtems", Triple::RTEMS)
  291. .StartsWith("nacl", Triple::NaCl)
  292. .StartsWith("cnk", Triple::CNK)
  293. .StartsWith("bitrig", Triple::Bitrig)
  294. .StartsWith("aix", Triple::AIX)
  295. .StartsWith("cuda", Triple::CUDA)
  296. .StartsWith("nvcl", Triple::NVCL)
  297. .Default(Triple::UnknownOS);
  298. }
  299. static Triple::EnvironmentType parseEnvironment(StringRef EnvironmentName) {
  300. return StringSwitch<Triple::EnvironmentType>(EnvironmentName)
  301. .StartsWith("eabihf", Triple::EABIHF)
  302. .StartsWith("eabi", Triple::EABI)
  303. .StartsWith("gnueabihf", Triple::GNUEABIHF)
  304. .StartsWith("gnueabi", Triple::GNUEABI)
  305. .StartsWith("gnux32", Triple::GNUX32)
  306. .StartsWith("code16", Triple::CODE16)
  307. .StartsWith("gnu", Triple::GNU)
  308. .StartsWith("android", Triple::Android)
  309. .StartsWith("msvc", Triple::MSVC)
  310. .StartsWith("itanium", Triple::Itanium)
  311. .StartsWith("cygnus", Triple::Cygnus)
  312. .Default(Triple::UnknownEnvironment);
  313. }
  314. static Triple::ObjectFormatType parseFormat(StringRef EnvironmentName) {
  315. return StringSwitch<Triple::ObjectFormatType>(EnvironmentName)
  316. .EndsWith("coff", Triple::COFF)
  317. .EndsWith("elf", Triple::ELF)
  318. .EndsWith("macho", Triple::MachO)
  319. .Default(Triple::UnknownObjectFormat);
  320. }
  321. static Triple::SubArchType parseSubArch(StringRef SubArchName) {
  322. return StringSwitch<Triple::SubArchType>(SubArchName)
  323. .EndsWith("v8", Triple::ARMSubArch_v8)
  324. .EndsWith("v8a", Triple::ARMSubArch_v8)
  325. .EndsWith("v7", Triple::ARMSubArch_v7)
  326. .EndsWith("v7a", Triple::ARMSubArch_v7)
  327. .EndsWith("v7em", Triple::ARMSubArch_v7em)
  328. .EndsWith("v7l", Triple::ARMSubArch_v7)
  329. .EndsWith("v7m", Triple::ARMSubArch_v7m)
  330. .EndsWith("v7r", Triple::ARMSubArch_v7)
  331. .EndsWith("v7s", Triple::ARMSubArch_v7s)
  332. .EndsWith("v6", Triple::ARMSubArch_v6)
  333. .EndsWith("v6m", Triple::ARMSubArch_v6m)
  334. .EndsWith("v6t2", Triple::ARMSubArch_v6t2)
  335. .EndsWith("v5", Triple::ARMSubArch_v5)
  336. .EndsWith("v5e", Triple::ARMSubArch_v5)
  337. .EndsWith("v5t", Triple::ARMSubArch_v5)
  338. .EndsWith("v5te", Triple::ARMSubArch_v5te)
  339. .EndsWith("v4t", Triple::ARMSubArch_v4t)
  340. .Default(Triple::NoSubArch);
  341. }
  342. static const char *getObjectFormatTypeName(Triple::ObjectFormatType Kind) {
  343. switch (Kind) {
  344. case Triple::UnknownObjectFormat: return "";
  345. case Triple::COFF: return "coff";
  346. case Triple::ELF: return "elf";
  347. case Triple::MachO: return "macho";
  348. }
  349. llvm_unreachable("unknown object format type");
  350. }
  351. static Triple::ObjectFormatType getDefaultFormat(const Triple &T) {
  352. if (T.isOSDarwin())
  353. return Triple::MachO;
  354. else if (T.isOSWindows())
  355. return Triple::COFF;
  356. return Triple::ELF;
  357. }
  358. /// \brief Construct a triple from the string representation provided.
  359. ///
  360. /// This stores the string representation and parses the various pieces into
  361. /// enum members.
  362. Triple::Triple(const Twine &Str)
  363. : Data(Str.str()),
  364. Arch(parseArch(getArchName())),
  365. SubArch(parseSubArch(getArchName())),
  366. Vendor(parseVendor(getVendorName())),
  367. OS(parseOS(getOSName())),
  368. Environment(parseEnvironment(getEnvironmentName())),
  369. ObjectFormat(parseFormat(getEnvironmentName())) {
  370. if (ObjectFormat == Triple::UnknownObjectFormat)
  371. ObjectFormat = getDefaultFormat(*this);
  372. }
  373. /// \brief Construct a triple from string representations of the architecture,
  374. /// vendor, and OS.
  375. ///
  376. /// This joins each argument into a canonical string representation and parses
  377. /// them into enum members. It leaves the environment unknown and omits it from
  378. /// the string representation.
  379. Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr)
  380. : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr).str()),
  381. Arch(parseArch(ArchStr.str())),
  382. SubArch(parseSubArch(ArchStr.str())),
  383. Vendor(parseVendor(VendorStr.str())),
  384. OS(parseOS(OSStr.str())),
  385. Environment(), ObjectFormat(Triple::UnknownObjectFormat) {
  386. ObjectFormat = getDefaultFormat(*this);
  387. }
  388. /// \brief Construct a triple from string representations of the architecture,
  389. /// vendor, OS, and environment.
  390. ///
  391. /// This joins each argument into a canonical string representation and parses
  392. /// them into enum members.
  393. Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
  394. const Twine &EnvironmentStr)
  395. : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr + Twine('-') +
  396. EnvironmentStr).str()),
  397. Arch(parseArch(ArchStr.str())),
  398. SubArch(parseSubArch(ArchStr.str())),
  399. Vendor(parseVendor(VendorStr.str())),
  400. OS(parseOS(OSStr.str())),
  401. Environment(parseEnvironment(EnvironmentStr.str())),
  402. ObjectFormat(parseFormat(EnvironmentStr.str())) {
  403. if (ObjectFormat == Triple::UnknownObjectFormat)
  404. ObjectFormat = getDefaultFormat(*this);
  405. }
  406. std::string Triple::normalize(StringRef Str) {
  407. // Parse into components.
  408. SmallVector<StringRef, 4> Components;
  409. Str.split(Components, "-");
  410. // If the first component corresponds to a known architecture, preferentially
  411. // use it for the architecture. If the second component corresponds to a
  412. // known vendor, preferentially use it for the vendor, etc. This avoids silly
  413. // component movement when a component parses as (eg) both a valid arch and a
  414. // valid os.
  415. ArchType Arch = UnknownArch;
  416. if (Components.size() > 0)
  417. Arch = parseArch(Components[0]);
  418. VendorType Vendor = UnknownVendor;
  419. if (Components.size() > 1)
  420. Vendor = parseVendor(Components[1]);
  421. OSType OS = UnknownOS;
  422. if (Components.size() > 2)
  423. OS = parseOS(Components[2]);
  424. EnvironmentType Environment = UnknownEnvironment;
  425. if (Components.size() > 3)
  426. Environment = parseEnvironment(Components[3]);
  427. ObjectFormatType ObjectFormat = UnknownObjectFormat;
  428. if (Components.size() > 4)
  429. ObjectFormat = parseFormat(Components[4]);
  430. // Note which components are already in their final position. These will not
  431. // be moved.
  432. bool Found[4];
  433. Found[0] = Arch != UnknownArch;
  434. Found[1] = Vendor != UnknownVendor;
  435. Found[2] = OS != UnknownOS;
  436. Found[3] = Environment != UnknownEnvironment;
  437. // If they are not there already, permute the components into their canonical
  438. // positions by seeing if they parse as a valid architecture, and if so moving
  439. // the component to the architecture position etc.
  440. for (unsigned Pos = 0; Pos != array_lengthof(Found); ++Pos) {
  441. if (Found[Pos])
  442. continue; // Already in the canonical position.
  443. for (unsigned Idx = 0; Idx != Components.size(); ++Idx) {
  444. // Do not reparse any components that already matched.
  445. if (Idx < array_lengthof(Found) && Found[Idx])
  446. continue;
  447. // Does this component parse as valid for the target position?
  448. bool Valid = false;
  449. StringRef Comp = Components[Idx];
  450. switch (Pos) {
  451. default: llvm_unreachable("unexpected component type!");
  452. case 0:
  453. Arch = parseArch(Comp);
  454. Valid = Arch != UnknownArch;
  455. break;
  456. case 1:
  457. Vendor = parseVendor(Comp);
  458. Valid = Vendor != UnknownVendor;
  459. break;
  460. case 2:
  461. OS = parseOS(Comp);
  462. Valid = OS != UnknownOS;
  463. break;
  464. case 3:
  465. Environment = parseEnvironment(Comp);
  466. Valid = Environment != UnknownEnvironment;
  467. if (!Valid) {
  468. ObjectFormat = parseFormat(Comp);
  469. Valid = ObjectFormat != UnknownObjectFormat;
  470. }
  471. break;
  472. }
  473. if (!Valid)
  474. continue; // Nope, try the next component.
  475. // Move the component to the target position, pushing any non-fixed
  476. // components that are in the way to the right. This tends to give
  477. // good results in the common cases of a forgotten vendor component
  478. // or a wrongly positioned environment.
  479. if (Pos < Idx) {
  480. // Insert left, pushing the existing components to the right. For
  481. // example, a-b-i386 -> i386-a-b when moving i386 to the front.
  482. StringRef CurrentComponent(""); // The empty component.
  483. // Replace the component we are moving with an empty component.
  484. std::swap(CurrentComponent, Components[Idx]);
  485. // Insert the component being moved at Pos, displacing any existing
  486. // components to the right.
  487. for (unsigned i = Pos; !CurrentComponent.empty(); ++i) {
  488. // Skip over any fixed components.
  489. while (i < array_lengthof(Found) && Found[i])
  490. ++i;
  491. // Place the component at the new position, getting the component
  492. // that was at this position - it will be moved right.
  493. std::swap(CurrentComponent, Components[i]);
  494. }
  495. } else if (Pos > Idx) {
  496. // Push right by inserting empty components until the component at Idx
  497. // reaches the target position Pos. For example, pc-a -> -pc-a when
  498. // moving pc to the second position.
  499. do {
  500. // Insert one empty component at Idx.
  501. StringRef CurrentComponent(""); // The empty component.
  502. for (unsigned i = Idx; i < Components.size();) {
  503. // Place the component at the new position, getting the component
  504. // that was at this position - it will be moved right.
  505. std::swap(CurrentComponent, Components[i]);
  506. // If it was placed on top of an empty component then we are done.
  507. if (CurrentComponent.empty())
  508. break;
  509. // Advance to the next component, skipping any fixed components.
  510. while (++i < array_lengthof(Found) && Found[i])
  511. ;
  512. }
  513. // The last component was pushed off the end - append it.
  514. if (!CurrentComponent.empty())
  515. Components.push_back(CurrentComponent);
  516. // Advance Idx to the component's new position.
  517. while (++Idx < array_lengthof(Found) && Found[Idx])
  518. ;
  519. } while (Idx < Pos); // Add more until the final position is reached.
  520. }
  521. assert(Pos < Components.size() && Components[Pos] == Comp &&
  522. "Component moved wrong!");
  523. Found[Pos] = true;
  524. break;
  525. }
  526. }
  527. // Special case logic goes here. At this point Arch, Vendor and OS have the
  528. // correct values for the computed components.
  529. if (OS == Triple::Win32) {
  530. Components.resize(4);
  531. Components[2] = "windows";
  532. if (Environment == UnknownEnvironment) {
  533. if (ObjectFormat == UnknownObjectFormat || ObjectFormat == Triple::COFF)
  534. Components[3] = "msvc";
  535. else
  536. Components[3] = getObjectFormatTypeName(ObjectFormat);
  537. }
  538. } else if (OS == Triple::MinGW32) {
  539. Components.resize(4);
  540. Components[2] = "windows";
  541. Components[3] = "gnu";
  542. } else if (OS == Triple::Cygwin) {
  543. Components.resize(4);
  544. Components[2] = "windows";
  545. Components[3] = "cygnus";
  546. }
  547. if (OS == Triple::MinGW32 || OS == Triple::Cygwin ||
  548. (OS == Triple::Win32 && Environment != UnknownEnvironment)) {
  549. if (ObjectFormat != UnknownObjectFormat && ObjectFormat != Triple::COFF) {
  550. Components.resize(5);
  551. Components[4] = getObjectFormatTypeName(ObjectFormat);
  552. }
  553. }
  554. // Stick the corrected components back together to form the normalized string.
  555. std::string Normalized;
  556. for (unsigned i = 0, e = Components.size(); i != e; ++i) {
  557. if (i) Normalized += '-';
  558. Normalized += Components[i];
  559. }
  560. return Normalized;
  561. }
  562. StringRef Triple::getArchName() const {
  563. return StringRef(Data).split('-').first; // Isolate first component
  564. }
  565. StringRef Triple::getVendorName() const {
  566. StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
  567. return Tmp.split('-').first; // Isolate second component
  568. }
  569. StringRef Triple::getOSName() const {
  570. StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
  571. Tmp = Tmp.split('-').second; // Strip second component
  572. return Tmp.split('-').first; // Isolate third component
  573. }
  574. StringRef Triple::getEnvironmentName() const {
  575. StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
  576. Tmp = Tmp.split('-').second; // Strip second component
  577. return Tmp.split('-').second; // Strip third component
  578. }
  579. StringRef Triple::getOSAndEnvironmentName() const {
  580. StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
  581. return Tmp.split('-').second; // Strip second component
  582. }
  583. static unsigned EatNumber(StringRef &Str) {
  584. assert(!Str.empty() && Str[0] >= '0' && Str[0] <= '9' && "Not a number");
  585. unsigned Result = 0;
  586. do {
  587. // Consume the leading digit.
  588. Result = Result*10 + (Str[0] - '0');
  589. // Eat the digit.
  590. Str = Str.substr(1);
  591. } while (!Str.empty() && Str[0] >= '0' && Str[0] <= '9');
  592. return Result;
  593. }
  594. void Triple::getOSVersion(unsigned &Major, unsigned &Minor,
  595. unsigned &Micro) const {
  596. StringRef OSName = getOSName();
  597. // Assume that the OS portion of the triple starts with the canonical name.
  598. StringRef OSTypeName = getOSTypeName(getOS());
  599. if (OSName.startswith(OSTypeName))
  600. OSName = OSName.substr(OSTypeName.size());
  601. // Any unset version defaults to 0.
  602. Major = Minor = Micro = 0;
  603. // Parse up to three components.
  604. unsigned *Components[3] = { &Major, &Minor, &Micro };
  605. for (unsigned i = 0; i != 3; ++i) {
  606. if (OSName.empty() || OSName[0] < '0' || OSName[0] > '9')
  607. break;
  608. // Consume the leading number.
  609. *Components[i] = EatNumber(OSName);
  610. // Consume the separator, if present.
  611. if (OSName.startswith("."))
  612. OSName = OSName.substr(1);
  613. }
  614. }
  615. bool Triple::getMacOSXVersion(unsigned &Major, unsigned &Minor,
  616. unsigned &Micro) const {
  617. getOSVersion(Major, Minor, Micro);
  618. switch (getOS()) {
  619. default: llvm_unreachable("unexpected OS for Darwin triple");
  620. case Darwin:
  621. // Default to darwin8, i.e., MacOSX 10.4.
  622. if (Major == 0)
  623. Major = 8;
  624. // Darwin version numbers are skewed from OS X versions.
  625. if (Major < 4)
  626. return false;
  627. Micro = 0;
  628. Minor = Major - 4;
  629. Major = 10;
  630. break;
  631. case MacOSX:
  632. // Default to 10.4.
  633. if (Major == 0) {
  634. Major = 10;
  635. Minor = 4;
  636. }
  637. if (Major != 10)
  638. return false;
  639. break;
  640. case IOS:
  641. // Ignore the version from the triple. This is only handled because the
  642. // the clang driver combines OS X and IOS support into a common Darwin
  643. // toolchain that wants to know the OS X version number even when targeting
  644. // IOS.
  645. Major = 10;
  646. Minor = 4;
  647. Micro = 0;
  648. break;
  649. }
  650. return true;
  651. }
  652. void Triple::getiOSVersion(unsigned &Major, unsigned &Minor,
  653. unsigned &Micro) const {
  654. switch (getOS()) {
  655. default: llvm_unreachable("unexpected OS for Darwin triple");
  656. case Darwin:
  657. case MacOSX:
  658. // Ignore the version from the triple. This is only handled because the
  659. // the clang driver combines OS X and IOS support into a common Darwin
  660. // toolchain that wants to know the iOS version number even when targeting
  661. // OS X.
  662. Major = 5;
  663. Minor = 0;
  664. Micro = 0;
  665. break;
  666. case IOS:
  667. getOSVersion(Major, Minor, Micro);
  668. // Default to 5.0 (or 7.0 for arm64).
  669. if (Major == 0)
  670. Major = (getArch() == aarch64) ? 7 : 5;
  671. break;
  672. }
  673. }
  674. void Triple::setTriple(const Twine &Str) {
  675. *this = Triple(Str);
  676. }
  677. void Triple::setArch(ArchType Kind) {
  678. setArchName(getArchTypeName(Kind));
  679. }
  680. void Triple::setVendor(VendorType Kind) {
  681. setVendorName(getVendorTypeName(Kind));
  682. }
  683. void Triple::setOS(OSType Kind) {
  684. setOSName(getOSTypeName(Kind));
  685. }
  686. void Triple::setEnvironment(EnvironmentType Kind) {
  687. setEnvironmentName(getEnvironmentTypeName(Kind));
  688. }
  689. void Triple::setObjectFormat(ObjectFormatType Kind) {
  690. if (Environment == UnknownEnvironment)
  691. return setEnvironmentName(getObjectFormatTypeName(Kind));
  692. setEnvironmentName((getEnvironmentTypeName(Environment) + Twine("-") +
  693. getObjectFormatTypeName(Kind)).str());
  694. }
  695. void Triple::setArchName(StringRef Str) {
  696. // Work around a miscompilation bug for Twines in gcc 4.0.3.
  697. SmallString<64> Triple;
  698. Triple += Str;
  699. Triple += "-";
  700. Triple += getVendorName();
  701. Triple += "-";
  702. Triple += getOSAndEnvironmentName();
  703. setTriple(Triple.str());
  704. }
  705. void Triple::setVendorName(StringRef Str) {
  706. setTriple(getArchName() + "-" + Str + "-" + getOSAndEnvironmentName());
  707. }
  708. void Triple::setOSName(StringRef Str) {
  709. if (hasEnvironment())
  710. setTriple(getArchName() + "-" + getVendorName() + "-" + Str +
  711. "-" + getEnvironmentName());
  712. else
  713. setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
  714. }
  715. void Triple::setEnvironmentName(StringRef Str) {
  716. setTriple(getArchName() + "-" + getVendorName() + "-" + getOSName() +
  717. "-" + Str);
  718. }
  719. void Triple::setOSAndEnvironmentName(StringRef Str) {
  720. setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
  721. }
  722. static unsigned getArchPointerBitWidth(llvm::Triple::ArchType Arch) {
  723. switch (Arch) {
  724. case llvm::Triple::UnknownArch:
  725. return 0;
  726. case llvm::Triple::msp430:
  727. return 16;
  728. case llvm::Triple::amdil:
  729. case llvm::Triple::arm:
  730. case llvm::Triple::armeb:
  731. case llvm::Triple::hexagon:
  732. case llvm::Triple::le32:
  733. case llvm::Triple::mips:
  734. case llvm::Triple::mipsel:
  735. case llvm::Triple::nvptx:
  736. case llvm::Triple::ppc:
  737. case llvm::Triple::r600:
  738. case llvm::Triple::sparc:
  739. case llvm::Triple::tce:
  740. case llvm::Triple::thumb:
  741. case llvm::Triple::thumbeb:
  742. case llvm::Triple::x86:
  743. case llvm::Triple::xcore:
  744. case llvm::Triple::spir:
  745. case llvm::Triple::kalimba:
  746. return 32;
  747. case llvm::Triple::aarch64:
  748. case llvm::Triple::aarch64_be:
  749. case llvm::Triple::mips64:
  750. case llvm::Triple::mips64el:
  751. case llvm::Triple::nvptx64:
  752. case llvm::Triple::ppc64:
  753. case llvm::Triple::ppc64le:
  754. case llvm::Triple::sparcv9:
  755. case llvm::Triple::systemz:
  756. case llvm::Triple::x86_64:
  757. case llvm::Triple::spir64:
  758. return 64;
  759. }
  760. llvm_unreachable("Invalid architecture value");
  761. }
  762. bool Triple::isArch64Bit() const {
  763. return getArchPointerBitWidth(getArch()) == 64;
  764. }
  765. bool Triple::isArch32Bit() const {
  766. return getArchPointerBitWidth(getArch()) == 32;
  767. }
  768. bool Triple::isArch16Bit() const {
  769. return getArchPointerBitWidth(getArch()) == 16;
  770. }
  771. Triple Triple::get32BitArchVariant() const {
  772. Triple T(*this);
  773. switch (getArch()) {
  774. case Triple::UnknownArch:
  775. case Triple::aarch64:
  776. case Triple::aarch64_be:
  777. case Triple::msp430:
  778. case Triple::systemz:
  779. case Triple::ppc64le:
  780. T.setArch(UnknownArch);
  781. break;
  782. case Triple::amdil:
  783. case Triple::spir:
  784. case Triple::arm:
  785. case Triple::armeb:
  786. case Triple::hexagon:
  787. case Triple::kalimba:
  788. case Triple::le32:
  789. case Triple::mips:
  790. case Triple::mipsel:
  791. case Triple::nvptx:
  792. case Triple::ppc:
  793. case Triple::r600:
  794. case Triple::sparc:
  795. case Triple::tce:
  796. case Triple::thumb:
  797. case Triple::thumbeb:
  798. case Triple::x86:
  799. case Triple::xcore:
  800. // Already 32-bit.
  801. break;
  802. case Triple::mips64: T.setArch(Triple::mips); break;
  803. case Triple::mips64el: T.setArch(Triple::mipsel); break;
  804. case Triple::nvptx64: T.setArch(Triple::nvptx); break;
  805. case Triple::ppc64: T.setArch(Triple::ppc); break;
  806. case Triple::sparcv9: T.setArch(Triple::sparc); break;
  807. case Triple::x86_64: T.setArch(Triple::x86); break;
  808. case Triple::spir64: T.setArch(Triple::spir); break;
  809. }
  810. return T;
  811. }
  812. Triple Triple::get64BitArchVariant() const {
  813. Triple T(*this);
  814. switch (getArch()) {
  815. case Triple::UnknownArch:
  816. case Triple::amdil:
  817. case Triple::arm:
  818. case Triple::armeb:
  819. case Triple::hexagon:
  820. case Triple::kalimba:
  821. case Triple::le32:
  822. case Triple::msp430:
  823. case Triple::r600:
  824. case Triple::tce:
  825. case Triple::thumb:
  826. case Triple::thumbeb:
  827. case Triple::xcore:
  828. T.setArch(UnknownArch);
  829. break;
  830. case Triple::aarch64:
  831. case Triple::aarch64_be:
  832. case Triple::spir64:
  833. case Triple::mips64:
  834. case Triple::mips64el:
  835. case Triple::nvptx64:
  836. case Triple::ppc64:
  837. case Triple::ppc64le:
  838. case Triple::sparcv9:
  839. case Triple::systemz:
  840. case Triple::x86_64:
  841. // Already 64-bit.
  842. break;
  843. case Triple::mips: T.setArch(Triple::mips64); break;
  844. case Triple::mipsel: T.setArch(Triple::mips64el); break;
  845. case Triple::nvptx: T.setArch(Triple::nvptx64); break;
  846. case Triple::ppc: T.setArch(Triple::ppc64); break;
  847. case Triple::sparc: T.setArch(Triple::sparcv9); break;
  848. case Triple::x86: T.setArch(Triple::x86_64); break;
  849. case Triple::spir: T.setArch(Triple::spir64); break;
  850. }
  851. return T;
  852. }
  853. // FIXME: tblgen this.
  854. const char *Triple::getARMCPUForArch(StringRef MArch) const {
  855. if (MArch.empty())
  856. MArch = getArchName();
  857. switch (getOS()) {
  858. case llvm::Triple::NetBSD:
  859. if (MArch == "armv6")
  860. return "arm1176jzf-s";
  861. break;
  862. case llvm::Triple::Win32:
  863. // FIXME: this is invalid for WindowsCE
  864. return "cortex-a9";
  865. default:
  866. break;
  867. }
  868. const char *result = nullptr;
  869. size_t offset = StringRef::npos;
  870. if (MArch.startswith("arm"))
  871. offset = 3;
  872. if (MArch.startswith("thumb"))
  873. offset = 5;
  874. if (offset != StringRef::npos && MArch.substr(offset, 2) == "eb")
  875. offset += 2;
  876. if (offset != StringRef::npos)
  877. result = llvm::StringSwitch<const char *>(MArch.substr(offset))
  878. .Cases("v2", "v2a", "arm2")
  879. .Case("v3", "arm6")
  880. .Case("v3m", "arm7m")
  881. .Case("v4", "strongarm")
  882. .Case("v4t", "arm7tdmi")
  883. .Cases("v5", "v5t", "arm10tdmi")
  884. .Cases("v5e", "v5te", "arm1022e")
  885. .Case("v5tej", "arm926ej-s")
  886. .Cases("v6", "v6k", "arm1136jf-s")
  887. .Case("v6j", "arm1136j-s")
  888. .Cases("v6z", "v6zk", "arm1176jzf-s")
  889. .Case("v6t2", "arm1156t2-s")
  890. .Cases("v6m", "v6-m", "cortex-m0")
  891. .Cases("v7", "v7a", "v7-a", "v7l", "v7-l", "cortex-a8")
  892. .Cases("v7s", "v7-s", "swift")
  893. .Cases("v7r", "v7-r", "cortex-r4")
  894. .Cases("v7m", "v7-m", "cortex-m3")
  895. .Cases("v7em", "v7e-m", "cortex-m4")
  896. .Cases("v8", "v8a", "v8-a", "cortex-a53")
  897. .Default(nullptr);
  898. else
  899. result = llvm::StringSwitch<const char *>(MArch)
  900. .Case("ep9312", "ep9312")
  901. .Case("iwmmxt", "iwmmxt")
  902. .Case("xscale", "xscale")
  903. .Default(nullptr);
  904. if (result)
  905. return result;
  906. // If all else failed, return the most base CPU with thumb interworking
  907. // supported by LLVM.
  908. // FIXME: Should warn once that we're falling back.
  909. switch (getOS()) {
  910. case llvm::Triple::NetBSD:
  911. switch (getEnvironment()) {
  912. case llvm::Triple::GNUEABIHF:
  913. case llvm::Triple::GNUEABI:
  914. case llvm::Triple::EABIHF:
  915. case llvm::Triple::EABI:
  916. return "arm926ej-s";
  917. default:
  918. return "strongarm";
  919. }
  920. default:
  921. switch (getEnvironment()) {
  922. case llvm::Triple::EABIHF:
  923. case llvm::Triple::GNUEABIHF:
  924. return "arm1176jzf-s";
  925. default:
  926. return "arm7tdmi";
  927. }
  928. }
  929. }