Triple.cpp 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813
  1. //===--- Triple.cpp - Target triple helper class --------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "llvm/ADT/Triple.h"
  9. #include "llvm/ADT/STLArrayExtras.h"
  10. #include "llvm/ADT/SmallString.h"
  11. #include "llvm/ADT/StringExtras.h"
  12. #include "llvm/ADT/StringSwitch.h"
  13. #include "llvm/Support/ARMTargetParser.h"
  14. #include "llvm/Support/ErrorHandling.h"
  15. #include "llvm/Support/Host.h"
  16. #include "llvm/Support/SwapByteOrder.h"
  17. #include "llvm/Support/VersionTuple.h"
  18. #include <cassert>
  19. #include <cstring>
  20. using namespace llvm;
  21. StringRef Triple::getArchTypeName(ArchType Kind) {
  22. switch (Kind) {
  23. case UnknownArch: return "unknown";
  24. case aarch64: return "aarch64";
  25. case aarch64_32: return "aarch64_32";
  26. case aarch64_be: return "aarch64_be";
  27. case amdgcn: return "amdgcn";
  28. case amdil64: return "amdil64";
  29. case amdil: return "amdil";
  30. case arc: return "arc";
  31. case arm: return "arm";
  32. case armeb: return "armeb";
  33. case avr: return "avr";
  34. case bpfeb: return "bpfeb";
  35. case bpfel: return "bpfel";
  36. case csky: return "csky";
  37. case hexagon: return "hexagon";
  38. case hsail64: return "hsail64";
  39. case hsail: return "hsail";
  40. case kalimba: return "kalimba";
  41. case lanai: return "lanai";
  42. case le32: return "le32";
  43. case le64: return "le64";
  44. case m68k: return "m68k";
  45. case mips64: return "mips64";
  46. case mips64el: return "mips64el";
  47. case mips: return "mips";
  48. case mipsel: return "mipsel";
  49. case msp430: return "msp430";
  50. case nvptx64: return "nvptx64";
  51. case nvptx: return "nvptx";
  52. case ppc64: return "powerpc64";
  53. case ppc64le: return "powerpc64le";
  54. case ppc: return "powerpc";
  55. case ppcle: return "powerpcle";
  56. case r600: return "r600";
  57. case renderscript32: return "renderscript32";
  58. case renderscript64: return "renderscript64";
  59. case riscv32: return "riscv32";
  60. case riscv64: return "riscv64";
  61. case shave: return "shave";
  62. case sparc: return "sparc";
  63. case sparcel: return "sparcel";
  64. case sparcv9: return "sparcv9";
  65. case spir64: return "spir64";
  66. case spir: return "spir";
  67. case spirv32: return "spirv32";
  68. case spirv64: return "spirv64";
  69. case systemz: return "s390x";
  70. case tce: return "tce";
  71. case tcele: return "tcele";
  72. case thumb: return "thumb";
  73. case thumbeb: return "thumbeb";
  74. case ve: return "ve";
  75. case wasm32: return "wasm32";
  76. case wasm64: return "wasm64";
  77. case x86: return "i386";
  78. case x86_64: return "x86_64";
  79. case xcore: return "xcore";
  80. }
  81. llvm_unreachable("Invalid ArchType!");
  82. }
  83. StringRef Triple::getArchTypePrefix(ArchType Kind) {
  84. switch (Kind) {
  85. default:
  86. return StringRef();
  87. case aarch64:
  88. case aarch64_be:
  89. case aarch64_32: return "aarch64";
  90. case arc: return "arc";
  91. case arm:
  92. case armeb:
  93. case thumb:
  94. case thumbeb: return "arm";
  95. case avr: return "avr";
  96. case ppc64:
  97. case ppc64le:
  98. case ppc:
  99. case ppcle: return "ppc";
  100. case m68k: return "m68k";
  101. case mips:
  102. case mipsel:
  103. case mips64:
  104. case mips64el: return "mips";
  105. case hexagon: return "hexagon";
  106. case amdgcn: return "amdgcn";
  107. case r600: return "r600";
  108. case bpfel:
  109. case bpfeb: return "bpf";
  110. case sparcv9:
  111. case sparcel:
  112. case sparc: return "sparc";
  113. case systemz: return "s390";
  114. case x86:
  115. case x86_64: return "x86";
  116. case xcore: return "xcore";
  117. // NVPTX intrinsics are namespaced under nvvm.
  118. case nvptx: return "nvvm";
  119. case nvptx64: return "nvvm";
  120. case le32: return "le32";
  121. case le64: return "le64";
  122. case amdil:
  123. case amdil64: return "amdil";
  124. case hsail:
  125. case hsail64: return "hsail";
  126. case spir:
  127. case spir64: return "spir";
  128. case spirv32:
  129. case spirv64: return "spirv";
  130. case kalimba: return "kalimba";
  131. case lanai: return "lanai";
  132. case shave: return "shave";
  133. case wasm32:
  134. case wasm64: return "wasm";
  135. case riscv32:
  136. case riscv64: return "riscv";
  137. case ve: return "ve";
  138. case csky: return "csky";
  139. }
  140. }
  141. StringRef Triple::getVendorTypeName(VendorType Kind) {
  142. switch (Kind) {
  143. case UnknownVendor: return "unknown";
  144. case AMD: return "amd";
  145. case Apple: return "apple";
  146. case CSR: return "csr";
  147. case Freescale: return "fsl";
  148. case IBM: return "ibm";
  149. case ImaginationTechnologies: return "img";
  150. case Mesa: return "mesa";
  151. case MipsTechnologies: return "mti";
  152. case Myriad: return "myriad";
  153. case NVIDIA: return "nvidia";
  154. case OpenEmbedded: return "oe";
  155. case PC: return "pc";
  156. case SCEI: return "scei";
  157. case SUSE: return "suse";
  158. }
  159. llvm_unreachable("Invalid VendorType!");
  160. }
  161. StringRef Triple::getOSTypeName(OSType Kind) {
  162. switch (Kind) {
  163. case UnknownOS: return "unknown";
  164. case AIX: return "aix";
  165. case AMDHSA: return "amdhsa";
  166. case AMDPAL: return "amdpal";
  167. case Ananas: return "ananas";
  168. case CUDA: return "cuda";
  169. case CloudABI: return "cloudabi";
  170. case Contiki: return "contiki";
  171. case Darwin: return "darwin";
  172. case DragonFly: return "dragonfly";
  173. case ELFIAMCU: return "elfiamcu";
  174. case Emscripten: return "emscripten";
  175. case FreeBSD: return "freebsd";
  176. case Fuchsia: return "fuchsia";
  177. case Haiku: return "haiku";
  178. case HermitCore: return "hermit";
  179. case Hurd: return "hurd";
  180. case IOS: return "ios";
  181. case KFreeBSD: return "kfreebsd";
  182. case Linux: return "linux";
  183. case Lv2: return "lv2";
  184. case MacOSX: return "macosx";
  185. case Mesa3D: return "mesa3d";
  186. case Minix: return "minix";
  187. case NVCL: return "nvcl";
  188. case NaCl: return "nacl";
  189. case NetBSD: return "netbsd";
  190. case OpenBSD: return "openbsd";
  191. case PS4: return "ps4";
  192. case RTEMS: return "rtems";
  193. case Solaris: return "solaris";
  194. case TvOS: return "tvos";
  195. case WASI: return "wasi";
  196. case WatchOS: return "watchos";
  197. case Win32: return "windows";
  198. case ZOS: return "zos";
  199. }
  200. llvm_unreachable("Invalid OSType");
  201. }
  202. StringRef Triple::getEnvironmentTypeName(EnvironmentType Kind) {
  203. switch (Kind) {
  204. case UnknownEnvironment: return "unknown";
  205. case Android: return "android";
  206. case CODE16: return "code16";
  207. case CoreCLR: return "coreclr";
  208. case Cygnus: return "cygnus";
  209. case EABI: return "eabi";
  210. case EABIHF: return "eabihf";
  211. case GNU: return "gnu";
  212. case GNUABI64: return "gnuabi64";
  213. case GNUABIN32: return "gnuabin32";
  214. case GNUEABI: return "gnueabi";
  215. case GNUEABIHF: return "gnueabihf";
  216. case GNUX32: return "gnux32";
  217. case GNUILP32: return "gnu_ilp32";
  218. case Itanium: return "itanium";
  219. case MSVC: return "msvc";
  220. case MacABI: return "macabi";
  221. case Musl: return "musl";
  222. case MuslEABI: return "musleabi";
  223. case MuslEABIHF: return "musleabihf";
  224. case MuslX32: return "muslx32";
  225. case Simulator: return "simulator";
  226. }
  227. llvm_unreachable("Invalid EnvironmentType!");
  228. }
  229. static Triple::ArchType parseBPFArch(StringRef ArchName) {
  230. if (ArchName.equals("bpf")) {
  231. if (sys::IsLittleEndianHost)
  232. return Triple::bpfel;
  233. else
  234. return Triple::bpfeb;
  235. } else if (ArchName.equals("bpf_be") || ArchName.equals("bpfeb")) {
  236. return Triple::bpfeb;
  237. } else if (ArchName.equals("bpf_le") || ArchName.equals("bpfel")) {
  238. return Triple::bpfel;
  239. } else {
  240. return Triple::UnknownArch;
  241. }
  242. }
  243. Triple::ArchType Triple::getArchTypeForLLVMName(StringRef Name) {
  244. Triple::ArchType BPFArch(parseBPFArch(Name));
  245. return StringSwitch<Triple::ArchType>(Name)
  246. .Case("aarch64", aarch64)
  247. .Case("aarch64_be", aarch64_be)
  248. .Case("aarch64_32", aarch64_32)
  249. .Case("arc", arc)
  250. .Case("arm64", aarch64) // "arm64" is an alias for "aarch64"
  251. .Case("arm64_32", aarch64_32)
  252. .Case("arm", arm)
  253. .Case("armeb", armeb)
  254. .Case("avr", avr)
  255. .StartsWith("bpf", BPFArch)
  256. .Case("m68k", m68k)
  257. .Case("mips", mips)
  258. .Case("mipsel", mipsel)
  259. .Case("mips64", mips64)
  260. .Case("mips64el", mips64el)
  261. .Case("msp430", msp430)
  262. .Case("ppc64", ppc64)
  263. .Case("ppc32", ppc)
  264. .Case("ppc", ppc)
  265. .Case("ppc32le", ppcle)
  266. .Case("ppcle", ppcle)
  267. .Case("ppc64le", ppc64le)
  268. .Case("r600", r600)
  269. .Case("amdgcn", amdgcn)
  270. .Case("riscv32", riscv32)
  271. .Case("riscv64", riscv64)
  272. .Case("hexagon", hexagon)
  273. .Case("sparc", sparc)
  274. .Case("sparcel", sparcel)
  275. .Case("sparcv9", sparcv9)
  276. .Case("systemz", systemz)
  277. .Case("tce", tce)
  278. .Case("tcele", tcele)
  279. .Case("thumb", thumb)
  280. .Case("thumbeb", thumbeb)
  281. .Case("x86", x86)
  282. .Case("x86-64", x86_64)
  283. .Case("xcore", xcore)
  284. .Case("nvptx", nvptx)
  285. .Case("nvptx64", nvptx64)
  286. .Case("le32", le32)
  287. .Case("le64", le64)
  288. .Case("amdil", amdil)
  289. .Case("amdil64", amdil64)
  290. .Case("hsail", hsail)
  291. .Case("hsail64", hsail64)
  292. .Case("spir", spir)
  293. .Case("spir64", spir64)
  294. .Case("spirv32", spirv32)
  295. .Case("spirv64", spirv64)
  296. .Case("kalimba", kalimba)
  297. .Case("lanai", lanai)
  298. .Case("shave", shave)
  299. .Case("wasm32", wasm32)
  300. .Case("wasm64", wasm64)
  301. .Case("renderscript32", renderscript32)
  302. .Case("renderscript64", renderscript64)
  303. .Case("ve", ve)
  304. .Case("csky", csky)
  305. .Default(UnknownArch);
  306. }
  307. static Triple::ArchType parseARMArch(StringRef ArchName) {
  308. ARM::ISAKind ISA = ARM::parseArchISA(ArchName);
  309. ARM::EndianKind ENDIAN = ARM::parseArchEndian(ArchName);
  310. Triple::ArchType arch = Triple::UnknownArch;
  311. switch (ENDIAN) {
  312. case ARM::EndianKind::LITTLE: {
  313. switch (ISA) {
  314. case ARM::ISAKind::ARM:
  315. arch = Triple::arm;
  316. break;
  317. case ARM::ISAKind::THUMB:
  318. arch = Triple::thumb;
  319. break;
  320. case ARM::ISAKind::AARCH64:
  321. arch = Triple::aarch64;
  322. break;
  323. case ARM::ISAKind::INVALID:
  324. break;
  325. }
  326. break;
  327. }
  328. case ARM::EndianKind::BIG: {
  329. switch (ISA) {
  330. case ARM::ISAKind::ARM:
  331. arch = Triple::armeb;
  332. break;
  333. case ARM::ISAKind::THUMB:
  334. arch = Triple::thumbeb;
  335. break;
  336. case ARM::ISAKind::AARCH64:
  337. arch = Triple::aarch64_be;
  338. break;
  339. case ARM::ISAKind::INVALID:
  340. break;
  341. }
  342. break;
  343. }
  344. case ARM::EndianKind::INVALID: {
  345. break;
  346. }
  347. }
  348. ArchName = ARM::getCanonicalArchName(ArchName);
  349. if (ArchName.empty())
  350. return Triple::UnknownArch;
  351. // Thumb only exists in v4+
  352. if (ISA == ARM::ISAKind::THUMB &&
  353. (ArchName.startswith("v2") || ArchName.startswith("v3")))
  354. return Triple::UnknownArch;
  355. // Thumb only for v6m
  356. ARM::ProfileKind Profile = ARM::parseArchProfile(ArchName);
  357. unsigned Version = ARM::parseArchVersion(ArchName);
  358. if (Profile == ARM::ProfileKind::M && Version == 6) {
  359. if (ENDIAN == ARM::EndianKind::BIG)
  360. return Triple::thumbeb;
  361. else
  362. return Triple::thumb;
  363. }
  364. return arch;
  365. }
  366. static Triple::ArchType parseArch(StringRef ArchName) {
  367. auto AT = StringSwitch<Triple::ArchType>(ArchName)
  368. .Cases("i386", "i486", "i586", "i686", Triple::x86)
  369. // FIXME: Do we need to support these?
  370. .Cases("i786", "i886", "i986", Triple::x86)
  371. .Cases("amd64", "x86_64", "x86_64h", Triple::x86_64)
  372. .Cases("powerpc", "powerpcspe", "ppc", "ppc32", Triple::ppc)
  373. .Cases("powerpcle", "ppcle", "ppc32le", Triple::ppcle)
  374. .Cases("powerpc64", "ppu", "ppc64", Triple::ppc64)
  375. .Cases("powerpc64le", "ppc64le", Triple::ppc64le)
  376. .Case("xscale", Triple::arm)
  377. .Case("xscaleeb", Triple::armeb)
  378. .Case("aarch64", Triple::aarch64)
  379. .Case("aarch64_be", Triple::aarch64_be)
  380. .Case("aarch64_32", Triple::aarch64_32)
  381. .Case("arc", Triple::arc)
  382. .Case("arm64", Triple::aarch64)
  383. .Case("arm64_32", Triple::aarch64_32)
  384. .Case("arm64e", Triple::aarch64)
  385. .Case("arm", Triple::arm)
  386. .Case("armeb", Triple::armeb)
  387. .Case("thumb", Triple::thumb)
  388. .Case("thumbeb", Triple::thumbeb)
  389. .Case("avr", Triple::avr)
  390. .Case("m68k", Triple::m68k)
  391. .Case("msp430", Triple::msp430)
  392. .Cases("mips", "mipseb", "mipsallegrex", "mipsisa32r6",
  393. "mipsr6", Triple::mips)
  394. .Cases("mipsel", "mipsallegrexel", "mipsisa32r6el", "mipsr6el",
  395. Triple::mipsel)
  396. .Cases("mips64", "mips64eb", "mipsn32", "mipsisa64r6",
  397. "mips64r6", "mipsn32r6", Triple::mips64)
  398. .Cases("mips64el", "mipsn32el", "mipsisa64r6el", "mips64r6el",
  399. "mipsn32r6el", Triple::mips64el)
  400. .Case("r600", Triple::r600)
  401. .Case("amdgcn", Triple::amdgcn)
  402. .Case("riscv32", Triple::riscv32)
  403. .Case("riscv64", Triple::riscv64)
  404. .Case("hexagon", Triple::hexagon)
  405. .Cases("s390x", "systemz", Triple::systemz)
  406. .Case("sparc", Triple::sparc)
  407. .Case("sparcel", Triple::sparcel)
  408. .Cases("sparcv9", "sparc64", Triple::sparcv9)
  409. .Case("tce", Triple::tce)
  410. .Case("tcele", Triple::tcele)
  411. .Case("xcore", Triple::xcore)
  412. .Case("nvptx", Triple::nvptx)
  413. .Case("nvptx64", Triple::nvptx64)
  414. .Case("le32", Triple::le32)
  415. .Case("le64", Triple::le64)
  416. .Case("amdil", Triple::amdil)
  417. .Case("amdil64", Triple::amdil64)
  418. .Case("hsail", Triple::hsail)
  419. .Case("hsail64", Triple::hsail64)
  420. .Case("spir", Triple::spir)
  421. .Case("spir64", Triple::spir64)
  422. .Case("spirv32", Triple::spirv32)
  423. .Case("spirv64", Triple::spirv64)
  424. .StartsWith("kalimba", Triple::kalimba)
  425. .Case("lanai", Triple::lanai)
  426. .Case("renderscript32", Triple::renderscript32)
  427. .Case("renderscript64", Triple::renderscript64)
  428. .Case("shave", Triple::shave)
  429. .Case("ve", Triple::ve)
  430. .Case("wasm32", Triple::wasm32)
  431. .Case("wasm64", Triple::wasm64)
  432. .Case("csky", Triple::csky)
  433. .Default(Triple::UnknownArch);
  434. // Some architectures require special parsing logic just to compute the
  435. // ArchType result.
  436. if (AT == Triple::UnknownArch) {
  437. if (ArchName.startswith("arm") || ArchName.startswith("thumb") ||
  438. ArchName.startswith("aarch64"))
  439. return parseARMArch(ArchName);
  440. if (ArchName.startswith("bpf"))
  441. return parseBPFArch(ArchName);
  442. }
  443. return AT;
  444. }
  445. static Triple::VendorType parseVendor(StringRef VendorName) {
  446. return StringSwitch<Triple::VendorType>(VendorName)
  447. .Case("apple", Triple::Apple)
  448. .Case("pc", Triple::PC)
  449. .Case("scei", Triple::SCEI)
  450. .Case("sie", Triple::SCEI)
  451. .Case("fsl", Triple::Freescale)
  452. .Case("ibm", Triple::IBM)
  453. .Case("img", Triple::ImaginationTechnologies)
  454. .Case("mti", Triple::MipsTechnologies)
  455. .Case("nvidia", Triple::NVIDIA)
  456. .Case("csr", Triple::CSR)
  457. .Case("myriad", Triple::Myriad)
  458. .Case("amd", Triple::AMD)
  459. .Case("mesa", Triple::Mesa)
  460. .Case("suse", Triple::SUSE)
  461. .Case("oe", Triple::OpenEmbedded)
  462. .Default(Triple::UnknownVendor);
  463. }
  464. static Triple::OSType parseOS(StringRef OSName) {
  465. return StringSwitch<Triple::OSType>(OSName)
  466. .StartsWith("ananas", Triple::Ananas)
  467. .StartsWith("cloudabi", Triple::CloudABI)
  468. .StartsWith("darwin", Triple::Darwin)
  469. .StartsWith("dragonfly", Triple::DragonFly)
  470. .StartsWith("freebsd", Triple::FreeBSD)
  471. .StartsWith("fuchsia", Triple::Fuchsia)
  472. .StartsWith("ios", Triple::IOS)
  473. .StartsWith("kfreebsd", Triple::KFreeBSD)
  474. .StartsWith("linux", Triple::Linux)
  475. .StartsWith("lv2", Triple::Lv2)
  476. .StartsWith("macos", Triple::MacOSX)
  477. .StartsWith("netbsd", Triple::NetBSD)
  478. .StartsWith("openbsd", Triple::OpenBSD)
  479. .StartsWith("solaris", Triple::Solaris)
  480. .StartsWith("win32", Triple::Win32)
  481. .StartsWith("windows", Triple::Win32)
  482. .StartsWith("zos", Triple::ZOS)
  483. .StartsWith("haiku", Triple::Haiku)
  484. .StartsWith("minix", Triple::Minix)
  485. .StartsWith("rtems", Triple::RTEMS)
  486. .StartsWith("nacl", Triple::NaCl)
  487. .StartsWith("aix", Triple::AIX)
  488. .StartsWith("cuda", Triple::CUDA)
  489. .StartsWith("nvcl", Triple::NVCL)
  490. .StartsWith("amdhsa", Triple::AMDHSA)
  491. .StartsWith("ps4", Triple::PS4)
  492. .StartsWith("elfiamcu", Triple::ELFIAMCU)
  493. .StartsWith("tvos", Triple::TvOS)
  494. .StartsWith("watchos", Triple::WatchOS)
  495. .StartsWith("mesa3d", Triple::Mesa3D)
  496. .StartsWith("contiki", Triple::Contiki)
  497. .StartsWith("amdpal", Triple::AMDPAL)
  498. .StartsWith("hermit", Triple::HermitCore)
  499. .StartsWith("hurd", Triple::Hurd)
  500. .StartsWith("wasi", Triple::WASI)
  501. .StartsWith("emscripten", Triple::Emscripten)
  502. .Default(Triple::UnknownOS);
  503. }
  504. static Triple::EnvironmentType parseEnvironment(StringRef EnvironmentName) {
  505. return StringSwitch<Triple::EnvironmentType>(EnvironmentName)
  506. .StartsWith("eabihf", Triple::EABIHF)
  507. .StartsWith("eabi", Triple::EABI)
  508. .StartsWith("gnuabin32", Triple::GNUABIN32)
  509. .StartsWith("gnuabi64", Triple::GNUABI64)
  510. .StartsWith("gnueabihf", Triple::GNUEABIHF)
  511. .StartsWith("gnueabi", Triple::GNUEABI)
  512. .StartsWith("gnux32", Triple::GNUX32)
  513. .StartsWith("gnu_ilp32", Triple::GNUILP32)
  514. .StartsWith("code16", Triple::CODE16)
  515. .StartsWith("gnu", Triple::GNU)
  516. .StartsWith("android", Triple::Android)
  517. .StartsWith("musleabihf", Triple::MuslEABIHF)
  518. .StartsWith("musleabi", Triple::MuslEABI)
  519. .StartsWith("muslx32", Triple::MuslX32)
  520. .StartsWith("musl", Triple::Musl)
  521. .StartsWith("msvc", Triple::MSVC)
  522. .StartsWith("itanium", Triple::Itanium)
  523. .StartsWith("cygnus", Triple::Cygnus)
  524. .StartsWith("coreclr", Triple::CoreCLR)
  525. .StartsWith("simulator", Triple::Simulator)
  526. .StartsWith("macabi", Triple::MacABI)
  527. .Default(Triple::UnknownEnvironment);
  528. }
  529. static Triple::ObjectFormatType parseFormat(StringRef EnvironmentName) {
  530. return StringSwitch<Triple::ObjectFormatType>(EnvironmentName)
  531. // "xcoff" must come before "coff" because of the order-dependendent
  532. // pattern matching.
  533. .EndsWith("xcoff", Triple::XCOFF)
  534. .EndsWith("coff", Triple::COFF)
  535. .EndsWith("elf", Triple::ELF)
  536. .EndsWith("goff", Triple::GOFF)
  537. .EndsWith("macho", Triple::MachO)
  538. .EndsWith("wasm", Triple::Wasm)
  539. .Default(Triple::UnknownObjectFormat);
  540. }
  541. static Triple::SubArchType parseSubArch(StringRef SubArchName) {
  542. if (SubArchName.startswith("mips") &&
  543. (SubArchName.endswith("r6el") || SubArchName.endswith("r6")))
  544. return Triple::MipsSubArch_r6;
  545. if (SubArchName == "powerpcspe")
  546. return Triple::PPCSubArch_spe;
  547. if (SubArchName == "arm64e")
  548. return Triple::AArch64SubArch_arm64e;
  549. StringRef ARMSubArch = ARM::getCanonicalArchName(SubArchName);
  550. // For now, this is the small part. Early return.
  551. if (ARMSubArch.empty())
  552. return StringSwitch<Triple::SubArchType>(SubArchName)
  553. .EndsWith("kalimba3", Triple::KalimbaSubArch_v3)
  554. .EndsWith("kalimba4", Triple::KalimbaSubArch_v4)
  555. .EndsWith("kalimba5", Triple::KalimbaSubArch_v5)
  556. .Default(Triple::NoSubArch);
  557. // ARM sub arch.
  558. switch(ARM::parseArch(ARMSubArch)) {
  559. case ARM::ArchKind::ARMV4:
  560. return Triple::NoSubArch;
  561. case ARM::ArchKind::ARMV4T:
  562. return Triple::ARMSubArch_v4t;
  563. case ARM::ArchKind::ARMV5T:
  564. return Triple::ARMSubArch_v5;
  565. case ARM::ArchKind::ARMV5TE:
  566. case ARM::ArchKind::IWMMXT:
  567. case ARM::ArchKind::IWMMXT2:
  568. case ARM::ArchKind::XSCALE:
  569. case ARM::ArchKind::ARMV5TEJ:
  570. return Triple::ARMSubArch_v5te;
  571. case ARM::ArchKind::ARMV6:
  572. return Triple::ARMSubArch_v6;
  573. case ARM::ArchKind::ARMV6K:
  574. case ARM::ArchKind::ARMV6KZ:
  575. return Triple::ARMSubArch_v6k;
  576. case ARM::ArchKind::ARMV6T2:
  577. return Triple::ARMSubArch_v6t2;
  578. case ARM::ArchKind::ARMV6M:
  579. return Triple::ARMSubArch_v6m;
  580. case ARM::ArchKind::ARMV7A:
  581. case ARM::ArchKind::ARMV7R:
  582. return Triple::ARMSubArch_v7;
  583. case ARM::ArchKind::ARMV7VE:
  584. return Triple::ARMSubArch_v7ve;
  585. case ARM::ArchKind::ARMV7K:
  586. return Triple::ARMSubArch_v7k;
  587. case ARM::ArchKind::ARMV7M:
  588. return Triple::ARMSubArch_v7m;
  589. case ARM::ArchKind::ARMV7S:
  590. return Triple::ARMSubArch_v7s;
  591. case ARM::ArchKind::ARMV7EM:
  592. return Triple::ARMSubArch_v7em;
  593. case ARM::ArchKind::ARMV8A:
  594. return Triple::ARMSubArch_v8;
  595. case ARM::ArchKind::ARMV8_1A:
  596. return Triple::ARMSubArch_v8_1a;
  597. case ARM::ArchKind::ARMV8_2A:
  598. return Triple::ARMSubArch_v8_2a;
  599. case ARM::ArchKind::ARMV8_3A:
  600. return Triple::ARMSubArch_v8_3a;
  601. case ARM::ArchKind::ARMV8_4A:
  602. return Triple::ARMSubArch_v8_4a;
  603. case ARM::ArchKind::ARMV8_5A:
  604. return Triple::ARMSubArch_v8_5a;
  605. case ARM::ArchKind::ARMV8_6A:
  606. return Triple::ARMSubArch_v8_6a;
  607. case ARM::ArchKind::ARMV8_7A:
  608. return Triple::ARMSubArch_v8_7a;
  609. case ARM::ArchKind::ARMV8_8A:
  610. return Triple::ARMSubArch_v8_8a;
  611. case ARM::ArchKind::ARMV9A:
  612. return Triple::ARMSubArch_v9;
  613. case ARM::ArchKind::ARMV9_1A:
  614. return Triple::ARMSubArch_v9_1a;
  615. case ARM::ArchKind::ARMV9_2A:
  616. return Triple::ARMSubArch_v9_2a;
  617. case ARM::ArchKind::ARMV9_3A:
  618. return Triple::ARMSubArch_v9_3a;
  619. case ARM::ArchKind::ARMV8R:
  620. return Triple::ARMSubArch_v8r;
  621. case ARM::ArchKind::ARMV8MBaseline:
  622. return Triple::ARMSubArch_v8m_baseline;
  623. case ARM::ArchKind::ARMV8MMainline:
  624. return Triple::ARMSubArch_v8m_mainline;
  625. case ARM::ArchKind::ARMV8_1MMainline:
  626. return Triple::ARMSubArch_v8_1m_mainline;
  627. default:
  628. return Triple::NoSubArch;
  629. }
  630. }
  631. static StringRef getObjectFormatTypeName(Triple::ObjectFormatType Kind) {
  632. switch (Kind) {
  633. case Triple::UnknownObjectFormat: return "";
  634. case Triple::COFF: return "coff";
  635. case Triple::ELF: return "elf";
  636. case Triple::GOFF: return "goff";
  637. case Triple::MachO: return "macho";
  638. case Triple::Wasm: return "wasm";
  639. case Triple::XCOFF: return "xcoff";
  640. }
  641. llvm_unreachable("unknown object format type");
  642. }
  643. static Triple::ObjectFormatType getDefaultFormat(const Triple &T) {
  644. switch (T.getArch()) {
  645. case Triple::UnknownArch:
  646. case Triple::aarch64:
  647. case Triple::aarch64_32:
  648. case Triple::arm:
  649. case Triple::thumb:
  650. case Triple::x86:
  651. case Triple::x86_64:
  652. if (T.isOSDarwin())
  653. return Triple::MachO;
  654. else if (T.isOSWindows())
  655. return Triple::COFF;
  656. return Triple::ELF;
  657. case Triple::aarch64_be:
  658. case Triple::amdgcn:
  659. case Triple::amdil64:
  660. case Triple::amdil:
  661. case Triple::arc:
  662. case Triple::armeb:
  663. case Triple::avr:
  664. case Triple::bpfeb:
  665. case Triple::bpfel:
  666. case Triple::csky:
  667. case Triple::hexagon:
  668. case Triple::hsail64:
  669. case Triple::hsail:
  670. case Triple::kalimba:
  671. case Triple::lanai:
  672. case Triple::le32:
  673. case Triple::le64:
  674. case Triple::m68k:
  675. case Triple::mips64:
  676. case Triple::mips64el:
  677. case Triple::mips:
  678. case Triple::mipsel:
  679. case Triple::msp430:
  680. case Triple::nvptx64:
  681. case Triple::nvptx:
  682. case Triple::ppc64le:
  683. case Triple::ppcle:
  684. case Triple::r600:
  685. case Triple::renderscript32:
  686. case Triple::renderscript64:
  687. case Triple::riscv32:
  688. case Triple::riscv64:
  689. case Triple::shave:
  690. case Triple::sparc:
  691. case Triple::sparcel:
  692. case Triple::sparcv9:
  693. case Triple::spir64:
  694. case Triple::spir:
  695. case Triple::tce:
  696. case Triple::tcele:
  697. case Triple::thumbeb:
  698. case Triple::ve:
  699. case Triple::xcore:
  700. return Triple::ELF;
  701. case Triple::ppc64:
  702. case Triple::ppc:
  703. if (T.isOSAIX())
  704. return Triple::XCOFF;
  705. return Triple::ELF;
  706. case Triple::systemz:
  707. if (T.isOSzOS())
  708. return Triple::GOFF;
  709. return Triple::ELF;
  710. case Triple::wasm32:
  711. case Triple::wasm64:
  712. return Triple::Wasm;
  713. case Triple::spirv32:
  714. case Triple::spirv64:
  715. // TODO: In future this will be Triple::SPIRV.
  716. return Triple::UnknownObjectFormat;
  717. }
  718. llvm_unreachable("unknown architecture");
  719. }
  720. /// Construct a triple from the string representation provided.
  721. ///
  722. /// This stores the string representation and parses the various pieces into
  723. /// enum members.
  724. Triple::Triple(const Twine &Str)
  725. : Data(Str.str()), Arch(UnknownArch), SubArch(NoSubArch),
  726. Vendor(UnknownVendor), OS(UnknownOS), Environment(UnknownEnvironment),
  727. ObjectFormat(UnknownObjectFormat) {
  728. // Do minimal parsing by hand here.
  729. SmallVector<StringRef, 4> Components;
  730. StringRef(Data).split(Components, '-', /*MaxSplit*/ 3);
  731. if (Components.size() > 0) {
  732. Arch = parseArch(Components[0]);
  733. SubArch = parseSubArch(Components[0]);
  734. if (Components.size() > 1) {
  735. Vendor = parseVendor(Components[1]);
  736. if (Components.size() > 2) {
  737. OS = parseOS(Components[2]);
  738. if (Components.size() > 3) {
  739. Environment = parseEnvironment(Components[3]);
  740. ObjectFormat = parseFormat(Components[3]);
  741. }
  742. }
  743. } else {
  744. Environment =
  745. StringSwitch<Triple::EnvironmentType>(Components[0])
  746. .StartsWith("mipsn32", Triple::GNUABIN32)
  747. .StartsWith("mips64", Triple::GNUABI64)
  748. .StartsWith("mipsisa64", Triple::GNUABI64)
  749. .StartsWith("mipsisa32", Triple::GNU)
  750. .Cases("mips", "mipsel", "mipsr6", "mipsr6el", Triple::GNU)
  751. .Default(UnknownEnvironment);
  752. }
  753. }
  754. if (ObjectFormat == UnknownObjectFormat)
  755. ObjectFormat = getDefaultFormat(*this);
  756. }
  757. /// Construct a triple from string representations of the architecture,
  758. /// vendor, and OS.
  759. ///
  760. /// This joins each argument into a canonical string representation and parses
  761. /// them into enum members. It leaves the environment unknown and omits it from
  762. /// the string representation.
  763. Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr)
  764. : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr).str()),
  765. Arch(parseArch(ArchStr.str())),
  766. SubArch(parseSubArch(ArchStr.str())),
  767. Vendor(parseVendor(VendorStr.str())),
  768. OS(parseOS(OSStr.str())),
  769. Environment(), ObjectFormat(Triple::UnknownObjectFormat) {
  770. ObjectFormat = getDefaultFormat(*this);
  771. }
  772. /// Construct a triple from string representations of the architecture,
  773. /// vendor, OS, and environment.
  774. ///
  775. /// This joins each argument into a canonical string representation and parses
  776. /// them into enum members.
  777. Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
  778. const Twine &EnvironmentStr)
  779. : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr + Twine('-') +
  780. EnvironmentStr).str()),
  781. Arch(parseArch(ArchStr.str())),
  782. SubArch(parseSubArch(ArchStr.str())),
  783. Vendor(parseVendor(VendorStr.str())),
  784. OS(parseOS(OSStr.str())),
  785. Environment(parseEnvironment(EnvironmentStr.str())),
  786. ObjectFormat(parseFormat(EnvironmentStr.str())) {
  787. if (ObjectFormat == Triple::UnknownObjectFormat)
  788. ObjectFormat = getDefaultFormat(*this);
  789. }
  790. std::string Triple::normalize(StringRef Str) {
  791. bool IsMinGW32 = false;
  792. bool IsCygwin = false;
  793. // Parse into components.
  794. SmallVector<StringRef, 4> Components;
  795. Str.split(Components, '-');
  796. // If the first component corresponds to a known architecture, preferentially
  797. // use it for the architecture. If the second component corresponds to a
  798. // known vendor, preferentially use it for the vendor, etc. This avoids silly
  799. // component movement when a component parses as (eg) both a valid arch and a
  800. // valid os.
  801. ArchType Arch = UnknownArch;
  802. if (Components.size() > 0)
  803. Arch = parseArch(Components[0]);
  804. VendorType Vendor = UnknownVendor;
  805. if (Components.size() > 1)
  806. Vendor = parseVendor(Components[1]);
  807. OSType OS = UnknownOS;
  808. if (Components.size() > 2) {
  809. OS = parseOS(Components[2]);
  810. IsCygwin = Components[2].startswith("cygwin");
  811. IsMinGW32 = Components[2].startswith("mingw");
  812. }
  813. EnvironmentType Environment = UnknownEnvironment;
  814. if (Components.size() > 3)
  815. Environment = parseEnvironment(Components[3]);
  816. ObjectFormatType ObjectFormat = UnknownObjectFormat;
  817. if (Components.size() > 4)
  818. ObjectFormat = parseFormat(Components[4]);
  819. // Note which components are already in their final position. These will not
  820. // be moved.
  821. bool Found[4];
  822. Found[0] = Arch != UnknownArch;
  823. Found[1] = Vendor != UnknownVendor;
  824. Found[2] = OS != UnknownOS;
  825. Found[3] = Environment != UnknownEnvironment;
  826. // If they are not there already, permute the components into their canonical
  827. // positions by seeing if they parse as a valid architecture, and if so moving
  828. // the component to the architecture position etc.
  829. for (unsigned Pos = 0; Pos != array_lengthof(Found); ++Pos) {
  830. if (Found[Pos])
  831. continue; // Already in the canonical position.
  832. for (unsigned Idx = 0; Idx != Components.size(); ++Idx) {
  833. // Do not reparse any components that already matched.
  834. if (Idx < array_lengthof(Found) && Found[Idx])
  835. continue;
  836. // Does this component parse as valid for the target position?
  837. bool Valid = false;
  838. StringRef Comp = Components[Idx];
  839. switch (Pos) {
  840. default: llvm_unreachable("unexpected component type!");
  841. case 0:
  842. Arch = parseArch(Comp);
  843. Valid = Arch != UnknownArch;
  844. break;
  845. case 1:
  846. Vendor = parseVendor(Comp);
  847. Valid = Vendor != UnknownVendor;
  848. break;
  849. case 2:
  850. OS = parseOS(Comp);
  851. IsCygwin = Comp.startswith("cygwin");
  852. IsMinGW32 = Comp.startswith("mingw");
  853. Valid = OS != UnknownOS || IsCygwin || IsMinGW32;
  854. break;
  855. case 3:
  856. Environment = parseEnvironment(Comp);
  857. Valid = Environment != UnknownEnvironment;
  858. if (!Valid) {
  859. ObjectFormat = parseFormat(Comp);
  860. Valid = ObjectFormat != UnknownObjectFormat;
  861. }
  862. break;
  863. }
  864. if (!Valid)
  865. continue; // Nope, try the next component.
  866. // Move the component to the target position, pushing any non-fixed
  867. // components that are in the way to the right. This tends to give
  868. // good results in the common cases of a forgotten vendor component
  869. // or a wrongly positioned environment.
  870. if (Pos < Idx) {
  871. // Insert left, pushing the existing components to the right. For
  872. // example, a-b-i386 -> i386-a-b when moving i386 to the front.
  873. StringRef CurrentComponent(""); // The empty component.
  874. // Replace the component we are moving with an empty component.
  875. std::swap(CurrentComponent, Components[Idx]);
  876. // Insert the component being moved at Pos, displacing any existing
  877. // components to the right.
  878. for (unsigned i = Pos; !CurrentComponent.empty(); ++i) {
  879. // Skip over any fixed components.
  880. while (i < array_lengthof(Found) && Found[i])
  881. ++i;
  882. // Place the component at the new position, getting the component
  883. // that was at this position - it will be moved right.
  884. std::swap(CurrentComponent, Components[i]);
  885. }
  886. } else if (Pos > Idx) {
  887. // Push right by inserting empty components until the component at Idx
  888. // reaches the target position Pos. For example, pc-a -> -pc-a when
  889. // moving pc to the second position.
  890. do {
  891. // Insert one empty component at Idx.
  892. StringRef CurrentComponent(""); // The empty component.
  893. for (unsigned i = Idx; i < Components.size();) {
  894. // Place the component at the new position, getting the component
  895. // that was at this position - it will be moved right.
  896. std::swap(CurrentComponent, Components[i]);
  897. // If it was placed on top of an empty component then we are done.
  898. if (CurrentComponent.empty())
  899. break;
  900. // Advance to the next component, skipping any fixed components.
  901. while (++i < array_lengthof(Found) && Found[i])
  902. ;
  903. }
  904. // The last component was pushed off the end - append it.
  905. if (!CurrentComponent.empty())
  906. Components.push_back(CurrentComponent);
  907. // Advance Idx to the component's new position.
  908. while (++Idx < array_lengthof(Found) && Found[Idx])
  909. ;
  910. } while (Idx < Pos); // Add more until the final position is reached.
  911. }
  912. assert(Pos < Components.size() && Components[Pos] == Comp &&
  913. "Component moved wrong!");
  914. Found[Pos] = true;
  915. break;
  916. }
  917. }
  918. // Replace empty components with "unknown" value.
  919. for (StringRef &C : Components)
  920. if (C.empty())
  921. C = "unknown";
  922. // Special case logic goes here. At this point Arch, Vendor and OS have the
  923. // correct values for the computed components.
  924. std::string NormalizedEnvironment;
  925. if (Environment == Triple::Android && Components[3].startswith("androideabi")) {
  926. StringRef AndroidVersion = Components[3].drop_front(strlen("androideabi"));
  927. if (AndroidVersion.empty()) {
  928. Components[3] = "android";
  929. } else {
  930. NormalizedEnvironment = Twine("android", AndroidVersion).str();
  931. Components[3] = NormalizedEnvironment;
  932. }
  933. }
  934. // SUSE uses "gnueabi" to mean "gnueabihf"
  935. if (Vendor == Triple::SUSE && Environment == llvm::Triple::GNUEABI)
  936. Components[3] = "gnueabihf";
  937. if (OS == Triple::Win32) {
  938. Components.resize(4);
  939. Components[2] = "windows";
  940. if (Environment == UnknownEnvironment) {
  941. if (ObjectFormat == UnknownObjectFormat || ObjectFormat == Triple::COFF)
  942. Components[3] = "msvc";
  943. else
  944. Components[3] = getObjectFormatTypeName(ObjectFormat);
  945. }
  946. } else if (IsMinGW32) {
  947. Components.resize(4);
  948. Components[2] = "windows";
  949. Components[3] = "gnu";
  950. } else if (IsCygwin) {
  951. Components.resize(4);
  952. Components[2] = "windows";
  953. Components[3] = "cygnus";
  954. }
  955. if (IsMinGW32 || IsCygwin ||
  956. (OS == Triple::Win32 && Environment != UnknownEnvironment)) {
  957. if (ObjectFormat != UnknownObjectFormat && ObjectFormat != Triple::COFF) {
  958. Components.resize(5);
  959. Components[4] = getObjectFormatTypeName(ObjectFormat);
  960. }
  961. }
  962. // Stick the corrected components back together to form the normalized string.
  963. return join(Components, "-");
  964. }
  965. StringRef Triple::getArchName() const {
  966. return StringRef(Data).split('-').first; // Isolate first component
  967. }
  968. StringRef Triple::getArchName(ArchType Kind, SubArchType SubArch) const {
  969. switch (Kind) {
  970. case Triple::mips:
  971. if (SubArch == MipsSubArch_r6)
  972. return "mipsisa32r6";
  973. break;
  974. case Triple::mipsel:
  975. if (SubArch == MipsSubArch_r6)
  976. return "mipsisa32r6el";
  977. break;
  978. case Triple::mips64:
  979. if (SubArch == MipsSubArch_r6)
  980. return "mipsisa64r6";
  981. break;
  982. case Triple::mips64el:
  983. if (SubArch == MipsSubArch_r6)
  984. return "mipsisa64r6el";
  985. break;
  986. default:
  987. break;
  988. }
  989. return getArchTypeName(Kind);
  990. }
  991. StringRef Triple::getVendorName() const {
  992. StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
  993. return Tmp.split('-').first; // Isolate second component
  994. }
  995. StringRef Triple::getOSName() const {
  996. StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
  997. Tmp = Tmp.split('-').second; // Strip second component
  998. return Tmp.split('-').first; // Isolate third component
  999. }
  1000. StringRef Triple::getEnvironmentName() const {
  1001. StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
  1002. Tmp = Tmp.split('-').second; // Strip second component
  1003. return Tmp.split('-').second; // Strip third component
  1004. }
  1005. StringRef Triple::getOSAndEnvironmentName() const {
  1006. StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
  1007. return Tmp.split('-').second; // Strip second component
  1008. }
  1009. static VersionTuple parseVersionFromName(StringRef Name) {
  1010. VersionTuple Version;
  1011. Version.tryParse(Name);
  1012. return Version.withoutBuild();
  1013. }
  1014. VersionTuple Triple::getEnvironmentVersion() const {
  1015. StringRef EnvironmentName = getEnvironmentName();
  1016. StringRef EnvironmentTypeName = getEnvironmentTypeName(getEnvironment());
  1017. if (EnvironmentName.startswith(EnvironmentTypeName))
  1018. EnvironmentName = EnvironmentName.substr(EnvironmentTypeName.size());
  1019. return parseVersionFromName(EnvironmentName);
  1020. }
  1021. VersionTuple Triple::getOSVersion() const {
  1022. StringRef OSName = getOSName();
  1023. // Assume that the OS portion of the triple starts with the canonical name.
  1024. StringRef OSTypeName = getOSTypeName(getOS());
  1025. if (OSName.startswith(OSTypeName))
  1026. OSName = OSName.substr(OSTypeName.size());
  1027. else if (getOS() == MacOSX)
  1028. OSName.consume_front("macos");
  1029. return parseVersionFromName(OSName);
  1030. }
  1031. bool Triple::getMacOSXVersion(VersionTuple &Version) const {
  1032. Version = getOSVersion();
  1033. switch (getOS()) {
  1034. default: llvm_unreachable("unexpected OS for Darwin triple");
  1035. case Darwin:
  1036. // Default to darwin8, i.e., MacOSX 10.4.
  1037. if (Version.getMajor() == 0)
  1038. Version = VersionTuple(8);
  1039. // Darwin version numbers are skewed from OS X versions.
  1040. if (Version.getMajor() < 4) {
  1041. return false;
  1042. }
  1043. if (Version.getMajor() <= 19) {
  1044. Version = VersionTuple(10, Version.getMajor() - 4);
  1045. } else {
  1046. // darwin20+ corresponds to macOS 11+.
  1047. Version = VersionTuple(11 + Version.getMajor() - 20);
  1048. }
  1049. break;
  1050. case MacOSX:
  1051. // Default to 10.4.
  1052. if (Version.getMajor() == 0) {
  1053. Version = VersionTuple(10, 4);
  1054. } else if (Version.getMajor() < 10) {
  1055. return false;
  1056. }
  1057. break;
  1058. case IOS:
  1059. case TvOS:
  1060. case WatchOS:
  1061. // Ignore the version from the triple. This is only handled because the
  1062. // the clang driver combines OS X and IOS support into a common Darwin
  1063. // toolchain that wants to know the OS X version number even when targeting
  1064. // IOS.
  1065. Version = VersionTuple(10, 4);
  1066. break;
  1067. }
  1068. return true;
  1069. }
  1070. VersionTuple Triple::getiOSVersion() const {
  1071. switch (getOS()) {
  1072. default: llvm_unreachable("unexpected OS for Darwin triple");
  1073. case Darwin:
  1074. case MacOSX:
  1075. // Ignore the version from the triple. This is only handled because the
  1076. // the clang driver combines OS X and IOS support into a common Darwin
  1077. // toolchain that wants to know the iOS version number even when targeting
  1078. // OS X.
  1079. return VersionTuple(5);
  1080. case IOS:
  1081. case TvOS: {
  1082. VersionTuple Version = getOSVersion();
  1083. // Default to 5.0 (or 7.0 for arm64).
  1084. if (Version.getMajor() == 0)
  1085. return (getArch() == aarch64) ? VersionTuple(7) : VersionTuple(5);
  1086. return Version;
  1087. }
  1088. case WatchOS:
  1089. llvm_unreachable("conflicting triple info");
  1090. }
  1091. }
  1092. VersionTuple Triple::getWatchOSVersion() const {
  1093. switch (getOS()) {
  1094. default: llvm_unreachable("unexpected OS for Darwin triple");
  1095. case Darwin:
  1096. case MacOSX:
  1097. // Ignore the version from the triple. This is only handled because the
  1098. // the clang driver combines OS X and IOS support into a common Darwin
  1099. // toolchain that wants to know the iOS version number even when targeting
  1100. // OS X.
  1101. return VersionTuple(2);
  1102. case WatchOS: {
  1103. VersionTuple Version = getOSVersion();
  1104. if (Version.getMajor() == 0)
  1105. return VersionTuple(2);
  1106. return Version;
  1107. }
  1108. case IOS:
  1109. llvm_unreachable("conflicting triple info");
  1110. }
  1111. }
  1112. void Triple::setTriple(const Twine &Str) {
  1113. *this = Triple(Str);
  1114. }
  1115. void Triple::setArch(ArchType Kind, SubArchType SubArch) {
  1116. setArchName(getArchName(Kind, SubArch));
  1117. }
  1118. void Triple::setVendor(VendorType Kind) {
  1119. setVendorName(getVendorTypeName(Kind));
  1120. }
  1121. void Triple::setOS(OSType Kind) {
  1122. setOSName(getOSTypeName(Kind));
  1123. }
  1124. void Triple::setEnvironment(EnvironmentType Kind) {
  1125. if (ObjectFormat == getDefaultFormat(*this))
  1126. return setEnvironmentName(getEnvironmentTypeName(Kind));
  1127. setEnvironmentName((getEnvironmentTypeName(Kind) + Twine("-") +
  1128. getObjectFormatTypeName(ObjectFormat)).str());
  1129. }
  1130. void Triple::setObjectFormat(ObjectFormatType Kind) {
  1131. if (Environment == UnknownEnvironment)
  1132. return setEnvironmentName(getObjectFormatTypeName(Kind));
  1133. setEnvironmentName((getEnvironmentTypeName(Environment) + Twine("-") +
  1134. getObjectFormatTypeName(Kind)).str());
  1135. }
  1136. void Triple::setArchName(StringRef Str) {
  1137. // Work around a miscompilation bug for Twines in gcc 4.0.3.
  1138. SmallString<64> Triple;
  1139. Triple += Str;
  1140. Triple += "-";
  1141. Triple += getVendorName();
  1142. Triple += "-";
  1143. Triple += getOSAndEnvironmentName();
  1144. setTriple(Triple);
  1145. }
  1146. void Triple::setVendorName(StringRef Str) {
  1147. setTriple(getArchName() + "-" + Str + "-" + getOSAndEnvironmentName());
  1148. }
  1149. void Triple::setOSName(StringRef Str) {
  1150. if (hasEnvironment())
  1151. setTriple(getArchName() + "-" + getVendorName() + "-" + Str +
  1152. "-" + getEnvironmentName());
  1153. else
  1154. setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
  1155. }
  1156. void Triple::setEnvironmentName(StringRef Str) {
  1157. setTriple(getArchName() + "-" + getVendorName() + "-" + getOSName() +
  1158. "-" + Str);
  1159. }
  1160. void Triple::setOSAndEnvironmentName(StringRef Str) {
  1161. setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
  1162. }
  1163. static unsigned getArchPointerBitWidth(llvm::Triple::ArchType Arch) {
  1164. switch (Arch) {
  1165. case llvm::Triple::UnknownArch:
  1166. return 0;
  1167. case llvm::Triple::avr:
  1168. case llvm::Triple::msp430:
  1169. return 16;
  1170. case llvm::Triple::aarch64_32:
  1171. case llvm::Triple::amdil:
  1172. case llvm::Triple::arc:
  1173. case llvm::Triple::arm:
  1174. case llvm::Triple::armeb:
  1175. case llvm::Triple::csky:
  1176. case llvm::Triple::hexagon:
  1177. case llvm::Triple::hsail:
  1178. case llvm::Triple::kalimba:
  1179. case llvm::Triple::lanai:
  1180. case llvm::Triple::le32:
  1181. case llvm::Triple::m68k:
  1182. case llvm::Triple::mips:
  1183. case llvm::Triple::mipsel:
  1184. case llvm::Triple::nvptx:
  1185. case llvm::Triple::ppc:
  1186. case llvm::Triple::ppcle:
  1187. case llvm::Triple::r600:
  1188. case llvm::Triple::renderscript32:
  1189. case llvm::Triple::riscv32:
  1190. case llvm::Triple::shave:
  1191. case llvm::Triple::sparc:
  1192. case llvm::Triple::sparcel:
  1193. case llvm::Triple::spir:
  1194. case llvm::Triple::spirv32:
  1195. case llvm::Triple::tce:
  1196. case llvm::Triple::tcele:
  1197. case llvm::Triple::thumb:
  1198. case llvm::Triple::thumbeb:
  1199. case llvm::Triple::wasm32:
  1200. case llvm::Triple::x86:
  1201. case llvm::Triple::xcore:
  1202. return 32;
  1203. case llvm::Triple::aarch64:
  1204. case llvm::Triple::aarch64_be:
  1205. case llvm::Triple::amdgcn:
  1206. case llvm::Triple::amdil64:
  1207. case llvm::Triple::bpfeb:
  1208. case llvm::Triple::bpfel:
  1209. case llvm::Triple::hsail64:
  1210. case llvm::Triple::le64:
  1211. case llvm::Triple::mips64:
  1212. case llvm::Triple::mips64el:
  1213. case llvm::Triple::nvptx64:
  1214. case llvm::Triple::ppc64:
  1215. case llvm::Triple::ppc64le:
  1216. case llvm::Triple::renderscript64:
  1217. case llvm::Triple::riscv64:
  1218. case llvm::Triple::sparcv9:
  1219. case llvm::Triple::spir64:
  1220. case llvm::Triple::spirv64:
  1221. case llvm::Triple::systemz:
  1222. case llvm::Triple::ve:
  1223. case llvm::Triple::wasm64:
  1224. case llvm::Triple::x86_64:
  1225. return 64;
  1226. }
  1227. llvm_unreachable("Invalid architecture value");
  1228. }
  1229. bool Triple::isArch64Bit() const {
  1230. return getArchPointerBitWidth(getArch()) == 64;
  1231. }
  1232. bool Triple::isArch32Bit() const {
  1233. return getArchPointerBitWidth(getArch()) == 32;
  1234. }
  1235. bool Triple::isArch16Bit() const {
  1236. return getArchPointerBitWidth(getArch()) == 16;
  1237. }
  1238. Triple Triple::get32BitArchVariant() const {
  1239. Triple T(*this);
  1240. switch (getArch()) {
  1241. case Triple::UnknownArch:
  1242. case Triple::amdgcn:
  1243. case Triple::avr:
  1244. case Triple::bpfeb:
  1245. case Triple::bpfel:
  1246. case Triple::msp430:
  1247. case Triple::systemz:
  1248. case Triple::ve:
  1249. T.setArch(UnknownArch);
  1250. break;
  1251. case Triple::aarch64_32:
  1252. case Triple::amdil:
  1253. case Triple::arc:
  1254. case Triple::arm:
  1255. case Triple::armeb:
  1256. case Triple::csky:
  1257. case Triple::hexagon:
  1258. case Triple::hsail:
  1259. case Triple::kalimba:
  1260. case Triple::lanai:
  1261. case Triple::le32:
  1262. case Triple::m68k:
  1263. case Triple::mips:
  1264. case Triple::mipsel:
  1265. case Triple::nvptx:
  1266. case Triple::ppc:
  1267. case Triple::ppcle:
  1268. case Triple::r600:
  1269. case Triple::renderscript32:
  1270. case Triple::riscv32:
  1271. case Triple::shave:
  1272. case Triple::sparc:
  1273. case Triple::sparcel:
  1274. case Triple::spir:
  1275. case Triple::spirv32:
  1276. case Triple::tce:
  1277. case Triple::tcele:
  1278. case Triple::thumb:
  1279. case Triple::thumbeb:
  1280. case Triple::wasm32:
  1281. case Triple::x86:
  1282. case Triple::xcore:
  1283. // Already 32-bit.
  1284. break;
  1285. case Triple::aarch64: T.setArch(Triple::arm); break;
  1286. case Triple::aarch64_be: T.setArch(Triple::armeb); break;
  1287. case Triple::amdil64: T.setArch(Triple::amdil); break;
  1288. case Triple::hsail64: T.setArch(Triple::hsail); break;
  1289. case Triple::le64: T.setArch(Triple::le32); break;
  1290. case Triple::mips64:
  1291. T.setArch(Triple::mips, getSubArch());
  1292. break;
  1293. case Triple::mips64el:
  1294. T.setArch(Triple::mipsel, getSubArch());
  1295. break;
  1296. case Triple::nvptx64: T.setArch(Triple::nvptx); break;
  1297. case Triple::ppc64: T.setArch(Triple::ppc); break;
  1298. case Triple::ppc64le: T.setArch(Triple::ppcle); break;
  1299. case Triple::renderscript64: T.setArch(Triple::renderscript32); break;
  1300. case Triple::riscv64: T.setArch(Triple::riscv32); break;
  1301. case Triple::sparcv9: T.setArch(Triple::sparc); break;
  1302. case Triple::spir64: T.setArch(Triple::spir); break;
  1303. case Triple::spirv64: T.setArch(Triple::spirv32); break;
  1304. case Triple::wasm64: T.setArch(Triple::wasm32); break;
  1305. case Triple::x86_64: T.setArch(Triple::x86); break;
  1306. }
  1307. return T;
  1308. }
  1309. Triple Triple::get64BitArchVariant() const {
  1310. Triple T(*this);
  1311. switch (getArch()) {
  1312. case Triple::UnknownArch:
  1313. case Triple::arc:
  1314. case Triple::avr:
  1315. case Triple::csky:
  1316. case Triple::hexagon:
  1317. case Triple::kalimba:
  1318. case Triple::lanai:
  1319. case Triple::m68k:
  1320. case Triple::msp430:
  1321. case Triple::r600:
  1322. case Triple::shave:
  1323. case Triple::sparcel:
  1324. case Triple::tce:
  1325. case Triple::tcele:
  1326. case Triple::xcore:
  1327. T.setArch(UnknownArch);
  1328. break;
  1329. case Triple::aarch64:
  1330. case Triple::aarch64_be:
  1331. case Triple::amdgcn:
  1332. case Triple::amdil64:
  1333. case Triple::bpfeb:
  1334. case Triple::bpfel:
  1335. case Triple::hsail64:
  1336. case Triple::le64:
  1337. case Triple::mips64:
  1338. case Triple::mips64el:
  1339. case Triple::nvptx64:
  1340. case Triple::ppc64:
  1341. case Triple::ppc64le:
  1342. case Triple::renderscript64:
  1343. case Triple::riscv64:
  1344. case Triple::sparcv9:
  1345. case Triple::spir64:
  1346. case Triple::spirv64:
  1347. case Triple::systemz:
  1348. case Triple::ve:
  1349. case Triple::wasm64:
  1350. case Triple::x86_64:
  1351. // Already 64-bit.
  1352. break;
  1353. case Triple::aarch64_32: T.setArch(Triple::aarch64); break;
  1354. case Triple::amdil: T.setArch(Triple::amdil64); break;
  1355. case Triple::arm: T.setArch(Triple::aarch64); break;
  1356. case Triple::armeb: T.setArch(Triple::aarch64_be); break;
  1357. case Triple::hsail: T.setArch(Triple::hsail64); break;
  1358. case Triple::le32: T.setArch(Triple::le64); break;
  1359. case Triple::mips:
  1360. T.setArch(Triple::mips64, getSubArch());
  1361. break;
  1362. case Triple::mipsel:
  1363. T.setArch(Triple::mips64el, getSubArch());
  1364. break;
  1365. case Triple::nvptx: T.setArch(Triple::nvptx64); break;
  1366. case Triple::ppc: T.setArch(Triple::ppc64); break;
  1367. case Triple::ppcle: T.setArch(Triple::ppc64le); break;
  1368. case Triple::renderscript32: T.setArch(Triple::renderscript64); break;
  1369. case Triple::riscv32: T.setArch(Triple::riscv64); break;
  1370. case Triple::sparc: T.setArch(Triple::sparcv9); break;
  1371. case Triple::spir: T.setArch(Triple::spir64); break;
  1372. case Triple::spirv32: T.setArch(Triple::spirv64); break;
  1373. case Triple::thumb: T.setArch(Triple::aarch64); break;
  1374. case Triple::thumbeb: T.setArch(Triple::aarch64_be); break;
  1375. case Triple::wasm32: T.setArch(Triple::wasm64); break;
  1376. case Triple::x86: T.setArch(Triple::x86_64); break;
  1377. }
  1378. return T;
  1379. }
  1380. Triple Triple::getBigEndianArchVariant() const {
  1381. Triple T(*this);
  1382. // Already big endian.
  1383. if (!isLittleEndian())
  1384. return T;
  1385. switch (getArch()) {
  1386. case Triple::UnknownArch:
  1387. case Triple::amdgcn:
  1388. case Triple::amdil64:
  1389. case Triple::amdil:
  1390. case Triple::avr:
  1391. case Triple::hexagon:
  1392. case Triple::hsail64:
  1393. case Triple::hsail:
  1394. case Triple::kalimba:
  1395. case Triple::le32:
  1396. case Triple::le64:
  1397. case Triple::msp430:
  1398. case Triple::nvptx64:
  1399. case Triple::nvptx:
  1400. case Triple::r600:
  1401. case Triple::renderscript32:
  1402. case Triple::renderscript64:
  1403. case Triple::riscv32:
  1404. case Triple::riscv64:
  1405. case Triple::shave:
  1406. case Triple::spir64:
  1407. case Triple::spir:
  1408. case Triple::spirv32:
  1409. case Triple::spirv64:
  1410. case Triple::wasm32:
  1411. case Triple::wasm64:
  1412. case Triple::x86:
  1413. case Triple::x86_64:
  1414. case Triple::xcore:
  1415. case Triple::ve:
  1416. case Triple::csky:
  1417. // ARM is intentionally unsupported here, changing the architecture would
  1418. // drop any arch suffixes.
  1419. case Triple::arm:
  1420. case Triple::thumb:
  1421. T.setArch(UnknownArch);
  1422. break;
  1423. case Triple::aarch64: T.setArch(Triple::aarch64_be); break;
  1424. case Triple::bpfel: T.setArch(Triple::bpfeb); break;
  1425. case Triple::mips64el:
  1426. T.setArch(Triple::mips64, getSubArch());
  1427. break;
  1428. case Triple::mipsel:
  1429. T.setArch(Triple::mips, getSubArch());
  1430. break;
  1431. case Triple::ppcle: T.setArch(Triple::ppc); break;
  1432. case Triple::ppc64le: T.setArch(Triple::ppc64); break;
  1433. case Triple::sparcel: T.setArch(Triple::sparc); break;
  1434. case Triple::tcele: T.setArch(Triple::tce); break;
  1435. default:
  1436. llvm_unreachable("getBigEndianArchVariant: unknown triple.");
  1437. }
  1438. return T;
  1439. }
  1440. Triple Triple::getLittleEndianArchVariant() const {
  1441. Triple T(*this);
  1442. if (isLittleEndian())
  1443. return T;
  1444. switch (getArch()) {
  1445. case Triple::UnknownArch:
  1446. case Triple::lanai:
  1447. case Triple::sparcv9:
  1448. case Triple::systemz:
  1449. case Triple::m68k:
  1450. // ARM is intentionally unsupported here, changing the architecture would
  1451. // drop any arch suffixes.
  1452. case Triple::armeb:
  1453. case Triple::thumbeb:
  1454. T.setArch(UnknownArch);
  1455. break;
  1456. case Triple::aarch64_be: T.setArch(Triple::aarch64); break;
  1457. case Triple::bpfeb: T.setArch(Triple::bpfel); break;
  1458. case Triple::mips64:
  1459. T.setArch(Triple::mips64el, getSubArch());
  1460. break;
  1461. case Triple::mips:
  1462. T.setArch(Triple::mipsel, getSubArch());
  1463. break;
  1464. case Triple::ppc: T.setArch(Triple::ppcle); break;
  1465. case Triple::ppc64: T.setArch(Triple::ppc64le); break;
  1466. case Triple::sparc: T.setArch(Triple::sparcel); break;
  1467. case Triple::tce: T.setArch(Triple::tcele); break;
  1468. default:
  1469. llvm_unreachable("getLittleEndianArchVariant: unknown triple.");
  1470. }
  1471. return T;
  1472. }
  1473. bool Triple::isLittleEndian() const {
  1474. switch (getArch()) {
  1475. case Triple::aarch64:
  1476. case Triple::aarch64_32:
  1477. case Triple::amdgcn:
  1478. case Triple::amdil64:
  1479. case Triple::amdil:
  1480. case Triple::arm:
  1481. case Triple::avr:
  1482. case Triple::bpfel:
  1483. case Triple::csky:
  1484. case Triple::hexagon:
  1485. case Triple::hsail64:
  1486. case Triple::hsail:
  1487. case Triple::kalimba:
  1488. case Triple::le32:
  1489. case Triple::le64:
  1490. case Triple::mips64el:
  1491. case Triple::mipsel:
  1492. case Triple::msp430:
  1493. case Triple::nvptx64:
  1494. case Triple::nvptx:
  1495. case Triple::ppcle:
  1496. case Triple::ppc64le:
  1497. case Triple::r600:
  1498. case Triple::renderscript32:
  1499. case Triple::renderscript64:
  1500. case Triple::riscv32:
  1501. case Triple::riscv64:
  1502. case Triple::shave:
  1503. case Triple::sparcel:
  1504. case Triple::spir64:
  1505. case Triple::spir:
  1506. case Triple::spirv32:
  1507. case Triple::spirv64:
  1508. case Triple::tcele:
  1509. case Triple::thumb:
  1510. case Triple::ve:
  1511. case Triple::wasm32:
  1512. case Triple::wasm64:
  1513. case Triple::x86:
  1514. case Triple::x86_64:
  1515. case Triple::xcore:
  1516. return true;
  1517. default:
  1518. return false;
  1519. }
  1520. }
  1521. bool Triple::isCompatibleWith(const Triple &Other) const {
  1522. // ARM and Thumb triples are compatible, if subarch, vendor and OS match.
  1523. if ((getArch() == Triple::thumb && Other.getArch() == Triple::arm) ||
  1524. (getArch() == Triple::arm && Other.getArch() == Triple::thumb) ||
  1525. (getArch() == Triple::thumbeb && Other.getArch() == Triple::armeb) ||
  1526. (getArch() == Triple::armeb && Other.getArch() == Triple::thumbeb)) {
  1527. if (getVendor() == Triple::Apple)
  1528. return getSubArch() == Other.getSubArch() &&
  1529. getVendor() == Other.getVendor() && getOS() == Other.getOS();
  1530. else
  1531. return getSubArch() == Other.getSubArch() &&
  1532. getVendor() == Other.getVendor() && getOS() == Other.getOS() &&
  1533. getEnvironment() == Other.getEnvironment() &&
  1534. getObjectFormat() == Other.getObjectFormat();
  1535. }
  1536. // If vendor is apple, ignore the version number.
  1537. if (getVendor() == Triple::Apple)
  1538. return getArch() == Other.getArch() && getSubArch() == Other.getSubArch() &&
  1539. getVendor() == Other.getVendor() && getOS() == Other.getOS();
  1540. return *this == Other;
  1541. }
  1542. std::string Triple::merge(const Triple &Other) const {
  1543. // If vendor is apple, pick the triple with the larger version number.
  1544. if (getVendor() == Triple::Apple)
  1545. if (Other.isOSVersionLT(*this))
  1546. return str();
  1547. return Other.str();
  1548. }
  1549. bool Triple::isMacOSXVersionLT(unsigned Major, unsigned Minor,
  1550. unsigned Micro) const {
  1551. assert(isMacOSX() && "Not an OS X triple!");
  1552. // If this is OS X, expect a sane version number.
  1553. if (getOS() == Triple::MacOSX)
  1554. return isOSVersionLT(Major, Minor, Micro);
  1555. // Otherwise, compare to the "Darwin" number.
  1556. if (Major == 10) {
  1557. return isOSVersionLT(Minor + 4, Micro, 0);
  1558. } else {
  1559. assert(Major >= 11 && "Unexpected major version");
  1560. return isOSVersionLT(Major - 11 + 20, Minor, Micro);
  1561. }
  1562. }
  1563. VersionTuple Triple::getMinimumSupportedOSVersion() const {
  1564. if (getVendor() != Triple::Apple || getArch() != Triple::aarch64)
  1565. return VersionTuple();
  1566. switch (getOS()) {
  1567. case Triple::MacOSX:
  1568. // ARM64 slice is supported starting from macOS 11.0+.
  1569. return VersionTuple(11, 0, 0);
  1570. case Triple::IOS:
  1571. // ARM64 slice is supported starting from Mac Catalyst 14 (macOS 11).
  1572. // ARM64 simulators are supported for iOS 14+.
  1573. if (isMacCatalystEnvironment() || isSimulatorEnvironment())
  1574. return VersionTuple(14, 0, 0);
  1575. // ARM64e slice is supported starting from iOS 14.
  1576. if (isArm64e())
  1577. return VersionTuple(14, 0, 0);
  1578. break;
  1579. case Triple::TvOS:
  1580. // ARM64 simulators are supported for tvOS 14+.
  1581. if (isSimulatorEnvironment())
  1582. return VersionTuple(14, 0, 0);
  1583. break;
  1584. case Triple::WatchOS:
  1585. // ARM64 simulators are supported for watchOS 7+.
  1586. if (isSimulatorEnvironment())
  1587. return VersionTuple(7, 0, 0);
  1588. break;
  1589. default:
  1590. break;
  1591. }
  1592. return VersionTuple();
  1593. }
  1594. StringRef Triple::getARMCPUForArch(StringRef MArch) const {
  1595. if (MArch.empty())
  1596. MArch = getArchName();
  1597. MArch = ARM::getCanonicalArchName(MArch);
  1598. // Some defaults are forced.
  1599. switch (getOS()) {
  1600. case llvm::Triple::FreeBSD:
  1601. case llvm::Triple::NetBSD:
  1602. case llvm::Triple::OpenBSD:
  1603. if (!MArch.empty() && MArch == "v6")
  1604. return "arm1176jzf-s";
  1605. if (!MArch.empty() && MArch == "v7")
  1606. return "cortex-a8";
  1607. break;
  1608. case llvm::Triple::Win32:
  1609. // FIXME: this is invalid for WindowsCE
  1610. if (ARM::parseArchVersion(MArch) <= 7)
  1611. return "cortex-a9";
  1612. break;
  1613. case llvm::Triple::IOS:
  1614. case llvm::Triple::MacOSX:
  1615. case llvm::Triple::TvOS:
  1616. case llvm::Triple::WatchOS:
  1617. if (MArch == "v7k")
  1618. return "cortex-a7";
  1619. break;
  1620. default:
  1621. break;
  1622. }
  1623. if (MArch.empty())
  1624. return StringRef();
  1625. StringRef CPU = ARM::getDefaultCPU(MArch);
  1626. if (!CPU.empty() && !CPU.equals("invalid"))
  1627. return CPU;
  1628. // If no specific architecture version is requested, return the minimum CPU
  1629. // required by the OS and environment.
  1630. switch (getOS()) {
  1631. case llvm::Triple::NetBSD:
  1632. switch (getEnvironment()) {
  1633. case llvm::Triple::EABI:
  1634. case llvm::Triple::EABIHF:
  1635. case llvm::Triple::GNUEABI:
  1636. case llvm::Triple::GNUEABIHF:
  1637. return "arm926ej-s";
  1638. default:
  1639. return "strongarm";
  1640. }
  1641. case llvm::Triple::NaCl:
  1642. case llvm::Triple::OpenBSD:
  1643. return "cortex-a8";
  1644. default:
  1645. switch (getEnvironment()) {
  1646. case llvm::Triple::EABIHF:
  1647. case llvm::Triple::GNUEABIHF:
  1648. case llvm::Triple::MuslEABIHF:
  1649. return "arm1176jzf-s";
  1650. default:
  1651. return "arm7tdmi";
  1652. }
  1653. }
  1654. llvm_unreachable("invalid arch name");
  1655. }
  1656. VersionTuple Triple::getCanonicalVersionForOS(OSType OSKind,
  1657. const VersionTuple &Version) {
  1658. switch (OSKind) {
  1659. case MacOSX:
  1660. // macOS 10.16 is canonicalized to macOS 11.
  1661. if (Version == VersionTuple(10, 16))
  1662. return VersionTuple(11, 0);
  1663. LLVM_FALLTHROUGH;
  1664. default:
  1665. return Version;
  1666. }
  1667. }