PageRenderTime 138ms CodeModel.GetById 2ms app.highlight 119ms RepoModel.GetById 1ms app.codeStats 1ms

/lib/Support/Triple.cpp

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