CSKYTargetParser.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. //===-- TargetParser - Parser for target features ---------------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file implements a target parser to recognise CSKY hardware features
  11. // such as CPU/ARCH names.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "llvm/TargetParser/CSKYTargetParser.h"
  15. #include "llvm/ADT/StringSwitch.h"
  16. using namespace llvm;
  17. bool CSKY::getFPUFeatures(CSKYFPUKind CSKYFPUKind,
  18. std::vector<StringRef> &Features) {
  19. if (CSKYFPUKind >= FK_LAST || CSKYFPUKind == FK_INVALID)
  20. return false;
  21. switch (CSKYFPUKind) {
  22. case FK_AUTO:
  23. Features.push_back("+fpuv2_sf");
  24. Features.push_back("+fpuv2_df");
  25. Features.push_back("+fdivdu");
  26. break;
  27. case FK_FPV2:
  28. Features.push_back("+fpuv2_sf");
  29. Features.push_back("+fpuv2_df");
  30. break;
  31. case FK_FPV2_DIVD:
  32. Features.push_back("+fpuv2_sf");
  33. Features.push_back("+fpuv2_df");
  34. Features.push_back("+fdivdu");
  35. break;
  36. case FK_FPV2_SF:
  37. Features.push_back("+fpuv2_sf");
  38. break;
  39. case FK_FPV3:
  40. Features.push_back("+fpuv3_hf");
  41. Features.push_back("+fpuv3_hi");
  42. Features.push_back("+fpuv3_sf");
  43. Features.push_back("+fpuv3_df");
  44. break;
  45. case FK_FPV3_HF:
  46. Features.push_back("+fpuv3_hf");
  47. Features.push_back("+fpuv3_hi");
  48. break;
  49. case FK_FPV3_HSF:
  50. Features.push_back("+fpuv3_hf");
  51. Features.push_back("+fpuv3_hi");
  52. Features.push_back("+fpuv3_sf");
  53. break;
  54. case FK_FPV3_SDF:
  55. Features.push_back("+fpuv3_sf");
  56. Features.push_back("+fpuv3_df");
  57. break;
  58. default:
  59. llvm_unreachable("Unknown FPU Kind");
  60. return false;
  61. }
  62. return true;
  63. }
  64. // ======================================================= //
  65. // Information by ID
  66. // ======================================================= //
  67. StringRef CSKY::getArchName(ArchKind AK) {
  68. return ARCHNames[static_cast<unsigned>(AK)].getName();
  69. }
  70. // The default cpu's name is same as arch name.
  71. StringRef CSKY::getDefaultCPU(StringRef Arch) {
  72. ArchKind AK = parseArch(Arch);
  73. if (AK == CSKY::ArchKind::INVALID)
  74. return StringRef();
  75. return Arch;
  76. }
  77. // ======================================================= //
  78. // Parsers
  79. // ======================================================= //
  80. CSKY::ArchKind CSKY::parseArch(StringRef Arch) {
  81. for (const auto A : ARCHNames) {
  82. if (A.getName() == Arch)
  83. return A.ID;
  84. }
  85. return CSKY::ArchKind::INVALID;
  86. }
  87. CSKY::ArchKind CSKY::parseCPUArch(StringRef CPU) {
  88. for (const auto C : CPUNames) {
  89. if (CPU == C.getName())
  90. return C.ArchID;
  91. }
  92. return CSKY::ArchKind::INVALID;
  93. }
  94. uint64_t CSKY::parseArchExt(StringRef ArchExt) {
  95. for (const auto &A : CSKYARCHExtNames) {
  96. if (ArchExt == A.getName())
  97. return A.ID;
  98. }
  99. return AEK_INVALID;
  100. }
  101. void CSKY::fillValidCPUArchList(SmallVectorImpl<StringRef> &Values) {
  102. for (const CpuNames<CSKY::ArchKind> &Arch : CPUNames) {
  103. if (Arch.ArchID != CSKY::ArchKind::INVALID)
  104. Values.push_back(Arch.getName());
  105. }
  106. }
  107. StringRef CSKY::getFPUName(unsigned FPUKind) {
  108. if (FPUKind >= FK_LAST)
  109. return StringRef();
  110. return FPUNames[FPUKind].getName();
  111. }
  112. CSKY::FPUVersion CSKY::getFPUVersion(unsigned FPUKind) {
  113. if (FPUKind >= FK_LAST)
  114. return FPUVersion::NONE;
  115. return FPUNames[FPUKind].FPUVer;
  116. }
  117. uint64_t CSKY::getDefaultExtensions(StringRef CPU) {
  118. return StringSwitch<uint64_t>(CPU)
  119. #define CSKY_CPU_NAME(NAME, ID, DEFAULT_EXT) \
  120. .Case(NAME, ARCHNames[static_cast<unsigned>(ArchKind::ID)].archBaseExt | \
  121. DEFAULT_EXT)
  122. #include "llvm/TargetParser/CSKYTargetParser.def"
  123. .Default(CSKY::AEK_INVALID);
  124. }
  125. StringRef CSKY::getArchExtName(uint64_t ArchExtKind) {
  126. for (const auto &AE : CSKYARCHExtNames)
  127. if (ArchExtKind == AE.ID)
  128. return AE.getName();
  129. return StringRef();
  130. }
  131. static bool stripNegationPrefix(StringRef &Name) {
  132. if (Name.startswith("no")) {
  133. Name = Name.substr(2);
  134. return true;
  135. }
  136. return false;
  137. }
  138. StringRef CSKY::getArchExtFeature(StringRef ArchExt) {
  139. bool Negated = stripNegationPrefix(ArchExt);
  140. for (const auto &AE : CSKYARCHExtNames) {
  141. if (AE.Feature && ArchExt == AE.getName())
  142. return StringRef(Negated ? AE.NegFeature : AE.Feature);
  143. }
  144. return StringRef();
  145. }
  146. bool CSKY::getExtensionFeatures(uint64_t Extensions,
  147. std::vector<StringRef> &Features) {
  148. if (Extensions == CSKY::AEK_INVALID)
  149. return false;
  150. for (const auto &AE : CSKYARCHExtNames) {
  151. if ((Extensions & AE.ID) == AE.ID && AE.Feature)
  152. Features.push_back(AE.Feature);
  153. }
  154. return true;
  155. }