Architecture.cpp 3.0 KB

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