Architecture.cpp 3.1 KB

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