llvm-project
98 строк · 3.1 Кб
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/BinaryFormat/MachO.h"16#include "llvm/Support/ErrorHandling.h"17#include "llvm/Support/raw_ostream.h"18#include "llvm/TargetParser/Triple.h"19
20namespace llvm {21namespace MachO {22
23Architecture getArchitectureFromCpuType(uint32_t CPUType, uint32_t CPUSubType) {24#define ARCHINFO(Arch, Type, Subtype, NumBits) \25if (CPUType == (Type) && \26(CPUSubType & ~MachO::CPU_SUBTYPE_MASK) == (Subtype)) \27return AK_##Arch;28#include "llvm/TextAPI/Architecture.def"29#undef ARCHINFO30
31return AK_unknown;32}
33
34Architecture getArchitectureFromName(StringRef Name) {35return StringSwitch<Architecture>(Name)36#define ARCHINFO(Arch, Type, Subtype, NumBits) .Case(#Arch, AK_##Arch)37#include "llvm/TextAPI/Architecture.def"38#undef ARCHINFO39.Default(AK_unknown);40}
41
42StringRef getArchitectureName(Architecture Arch) {43switch (Arch) {44#define ARCHINFO(Arch, Type, Subtype, NumBits) \45case AK_##Arch: \46return #Arch;47#include "llvm/TextAPI/Architecture.def"48#undef ARCHINFO49case AK_unknown:50return "unknown";51}52
53// Appease some compilers that cannot figure out that this is a fully covered54// switch statement.55return "unknown";56}
57
58std::pair<uint32_t, uint32_t> getCPUTypeFromArchitecture(Architecture Arch) {59switch (Arch) {60#define ARCHINFO(Arch, Type, Subtype, NumBits) \61case AK_##Arch: \62return std::make_pair(Type, Subtype);63#include "llvm/TextAPI/Architecture.def"64#undef ARCHINFO65case AK_unknown:66return std::make_pair(0, 0);67}68
69// Appease some compilers that cannot figure out that this is a fully covered70// switch statement.71return std::make_pair(0, 0);72}
73
74Architecture mapToArchitecture(const Triple &Target) {75return getArchitectureFromName(Target.getArchName());76}
77
78bool is64Bit(Architecture Arch) {79switch (Arch) {80#define ARCHINFO(Arch, Type, Subtype, NumBits) \81case AK_##Arch: \82return NumBits == 64;83#include "llvm/TextAPI/Architecture.def"84#undef ARCHINFO85case AK_unknown:86return false;87}88
89llvm_unreachable("Fully handled switch case above.");90}
91
92raw_ostream &operator<<(raw_ostream &OS, Architecture Arch) {93OS << getArchitectureName(Arch);94return OS;95}
96
97} // end namespace MachO.98} // end namespace llvm.99