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/MachO/Architecture.h"
14 #include "llvm/ADT/StringSwitch.h"
15 #include "llvm/BinaryFormat/MachO.h"
16
17 namespace llvm {
18 namespace MachO {
19
getArchitectureFromCpuType(uint32_t CPUType,uint32_t CPUSubType)20 Architecture getArchitectureFromCpuType(uint32_t CPUType, uint32_t CPUSubType) {
21 #define ARCHINFO(Arch, Type, Subtype) \
22 if (CPUType == (Type) && \
23 (CPUSubType & ~MachO::CPU_SUBTYPE_MASK) == (Subtype)) \
24 return AK_##Arch;
25 #include "llvm/TextAPI/MachO/Architecture.def"
26 #undef ARCHINFO
27
28 return AK_unknown;
29 }
30
getArchitectureFromName(StringRef Name)31 Architecture getArchitectureFromName(StringRef Name) {
32 return StringSwitch<Architecture>(Name)
33 #define ARCHINFO(Arch, Type, Subtype) .Case(#Arch, AK_##Arch)
34 #include "llvm/TextAPI/MachO/Architecture.def"
35 #undef ARCHINFO
36 .Default(AK_unknown);
37 }
38
getArchitectureName(Architecture Arch)39 StringRef getArchitectureName(Architecture Arch) {
40 switch (Arch) {
41 #define ARCHINFO(Arch, Type, Subtype) \
42 case AK_##Arch: \
43 return #Arch;
44 #include "llvm/TextAPI/MachO/Architecture.def"
45 #undef ARCHINFO
46 case AK_unknown:
47 return "unknown";
48 }
49
50 // Appease some compilers that cannot figure out that this is a fully covered
51 // switch statement.
52 return "unknown";
53 }
54
getCPUTypeFromArchitecture(Architecture Arch)55 std::pair<uint32_t, uint32_t> getCPUTypeFromArchitecture(Architecture Arch) {
56 switch (Arch) {
57 #define ARCHINFO(Arch, Type, Subtype) \
58 case AK_##Arch: \
59 return std::make_pair(Type, Subtype);
60 #include "llvm/TextAPI/MachO/Architecture.def"
61 #undef ARCHINFO
62 case AK_unknown:
63 return std::make_pair(0, 0);
64 }
65
66 // Appease some compilers that cannot figure out that this is a fully covered
67 // switch statement.
68 return std::make_pair(0, 0);
69 }
70
mapToArchitecture(const Triple & Target)71 Architecture mapToArchitecture(const Triple &Target) {
72 return getArchitectureFromName(Target.getArchName());
73 }
74
operator <<(raw_ostream & OS,Architecture Arch)75 raw_ostream &operator<<(raw_ostream &OS, Architecture Arch) {
76 OS << getArchitectureName(Arch);
77 return OS;
78 }
79
80 } // end namespace MachO.
81 } // end namespace llvm.
82