Coverage Report

Created: 2024-09-08 06:53

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