PageRenderTime 65ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/external/bsd/llvm/dist/llvm/lib/Support/Triple.cpp

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