xref: /freebsd/contrib/llvm-project/llvm/lib/TextAPI/Architecture.cpp (revision 5f4c09dd85bff675e0ca63c55ea3c517e0fddfcc)
1 //===- Architecture.cpp ---------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Implements the architecture helper functions.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/TextAPI/Architecture.h"
14 #include "llvm/ADT/StringSwitch.h"
15 #include "llvm/ADT/Triple.h"
16 #include "llvm/BinaryFormat/MachO.h"
17 #include "llvm/Support/raw_ostream.h"
18 #include "llvm/Support/ErrorHandling.h"
19 
20 namespace llvm {
21 namespace MachO {
22 
23 Architecture getArchitectureFromCpuType(uint32_t CPUType, uint32_t CPUSubType) {
24 #define ARCHINFO(Arch, Type, Subtype, NumBits)                                 \
25   if (CPUType == (Type) &&                                                     \
26       (CPUSubType & ~MachO::CPU_SUBTYPE_MASK) == (Subtype))                    \
27     return AK_##Arch;
28 #include "llvm/TextAPI/Architecture.def"
29 #undef ARCHINFO
30 
31   return AK_unknown;
32 }
33 
34 Architecture getArchitectureFromName(StringRef Name) {
35   return StringSwitch<Architecture>(Name)
36 #define ARCHINFO(Arch, Type, Subtype, NumBits) .Case(#Arch, AK_##Arch)
37 #include "llvm/TextAPI/Architecture.def"
38 #undef ARCHINFO
39       .Default(AK_unknown);
40 }
41 
42 StringRef getArchitectureName(Architecture Arch) {
43   switch (Arch) {
44 #define ARCHINFO(Arch, Type, Subtype, NumBits)                                 \
45   case AK_##Arch:                                                              \
46     return #Arch;
47 #include "llvm/TextAPI/Architecture.def"
48 #undef ARCHINFO
49   case AK_unknown:
50     return "unknown";
51   }
52 
53   // Appease some compilers that cannot figure out that this is a fully covered
54   // switch statement.
55   return "unknown";
56 }
57 
58 std::pair<uint32_t, uint32_t> getCPUTypeFromArchitecture(Architecture Arch) {
59   switch (Arch) {
60 #define ARCHINFO(Arch, Type, Subtype, NumBits)                                 \
61   case AK_##Arch:                                                              \
62     return std::make_pair(Type, Subtype);
63 #include "llvm/TextAPI/Architecture.def"
64 #undef ARCHINFO
65   case AK_unknown:
66     return std::make_pair(0, 0);
67   }
68 
69   // Appease some compilers that cannot figure out that this is a fully covered
70   // switch statement.
71   return std::make_pair(0, 0);
72 }
73 
74 Architecture mapToArchitecture(const Triple &Target) {
75   return getArchitectureFromName(Target.getArchName());
76 }
77 
78 bool is64Bit(Architecture Arch) {
79   switch (Arch) {
80 #define ARCHINFO(Arch, Type, Subtype, NumBits)                                 \
81   case AK_##Arch:                                                              \
82     return NumBits == 64;
83 #include "llvm/TextAPI/Architecture.def"
84 #undef ARCHINFO
85   case AK_unknown:
86     return false;
87   }
88 
89   llvm_unreachable("Fully handled switch case above.");
90 }
91 
92 raw_ostream &operator<<(raw_ostream &OS, Architecture Arch) {
93   OS << getArchitectureName(Arch);
94   return OS;
95 }
96 
97 } // end namespace MachO.
98 } // end namespace llvm.
99