aboutsummaryrefslogtreecommitdiff
path: root/contrib/llvm-project/llvm/lib/TargetParser/CSKYTargetParser.cpp
blob: 006d2bb342acc86b322201fbf382f8a3104b214e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
//===-- TargetParser - Parser for target features ---------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file implements a target parser to recognise CSKY hardware features
// such as CPU/ARCH names.
//
//===----------------------------------------------------------------------===//

#include "llvm/TargetParser/CSKYTargetParser.h"
#include "llvm/ADT/StringSwitch.h"

using namespace llvm;

bool CSKY::getFPUFeatures(CSKYFPUKind CSKYFPUKind,
                          std::vector<StringRef> &Features) {

  if (CSKYFPUKind >= FK_LAST || CSKYFPUKind == FK_INVALID)
    return false;

  switch (CSKYFPUKind) {
  case FK_AUTO:
    Features.push_back("+fpuv2_sf");
    Features.push_back("+fpuv2_df");
    Features.push_back("+fdivdu");
    break;
  case FK_FPV2:
    Features.push_back("+fpuv2_sf");
    Features.push_back("+fpuv2_df");
    break;
  case FK_FPV2_DIVD:
    Features.push_back("+fpuv2_sf");
    Features.push_back("+fpuv2_df");
    Features.push_back("+fdivdu");
    break;
  case FK_FPV2_SF:
    Features.push_back("+fpuv2_sf");
    break;
  case FK_FPV3:
    Features.push_back("+fpuv3_hf");
    Features.push_back("+fpuv3_hi");
    Features.push_back("+fpuv3_sf");
    Features.push_back("+fpuv3_df");
    break;
  case FK_FPV3_HF:
    Features.push_back("+fpuv3_hf");
    Features.push_back("+fpuv3_hi");
    break;
  case FK_FPV3_HSF:
    Features.push_back("+fpuv3_hf");
    Features.push_back("+fpuv3_hi");
    Features.push_back("+fpuv3_sf");
    break;
  case FK_FPV3_SDF:
    Features.push_back("+fpuv3_sf");
    Features.push_back("+fpuv3_df");
    break;
  default:
    llvm_unreachable("Unknown FPU Kind");
    return false;
  }

  return true;
}

// ======================================================= //
// Information by ID
// ======================================================= //

StringRef CSKY::getArchName(ArchKind AK) {
  return ARCHNames[static_cast<unsigned>(AK)].getName();
}

// The default cpu's name is same as arch name.
StringRef CSKY::getDefaultCPU(StringRef Arch) {
  ArchKind AK = parseArch(Arch);
  if (AK == CSKY::ArchKind::INVALID)
    return StringRef();

  return Arch;
}

// ======================================================= //
// Parsers
// ======================================================= //
CSKY::ArchKind CSKY::parseArch(StringRef Arch) {
  for (const auto A : ARCHNames) {
    if (A.getName() == Arch)
      return A.ID;
  }

  return CSKY::ArchKind::INVALID;
}

CSKY::ArchKind CSKY::parseCPUArch(StringRef CPU) {
  for (const auto C : CPUNames) {
    if (CPU == C.getName())
      return C.ArchID;
  }

  return CSKY::ArchKind::INVALID;
}

uint64_t CSKY::parseArchExt(StringRef ArchExt) {
  for (const auto &A : CSKYARCHExtNames) {
    if (ArchExt == A.getName())
      return A.ID;
  }
  return AEK_INVALID;
}

void CSKY::fillValidCPUArchList(SmallVectorImpl<StringRef> &Values) {
  for (const CpuNames<CSKY::ArchKind> &Arch : CPUNames) {
    if (Arch.ArchID != CSKY::ArchKind::INVALID)
      Values.push_back(Arch.getName());
  }
}

StringRef CSKY::getFPUName(unsigned FPUKind) {
  if (FPUKind >= FK_LAST)
    return StringRef();
  return FPUNames[FPUKind].getName();
}

CSKY::FPUVersion CSKY::getFPUVersion(unsigned FPUKind) {
  if (FPUKind >= FK_LAST)
    return FPUVersion::NONE;
  return FPUNames[FPUKind].FPUVer;
}

uint64_t CSKY::getDefaultExtensions(StringRef CPU) {
  return StringSwitch<uint64_t>(CPU)
#define CSKY_CPU_NAME(NAME, ID, DEFAULT_EXT)                                   \
  .Case(NAME, ARCHNames[static_cast<unsigned>(ArchKind::ID)].archBaseExt |     \
                  DEFAULT_EXT)
#include "llvm/TargetParser/CSKYTargetParser.def"
      .Default(CSKY::AEK_INVALID);
}

StringRef CSKY::getArchExtName(uint64_t ArchExtKind) {
  for (const auto &AE : CSKYARCHExtNames)
    if (ArchExtKind == AE.ID)
      return AE.getName();
  return StringRef();
}

static bool stripNegationPrefix(StringRef &Name) {
  if (Name.starts_with("no")) {
    Name = Name.substr(2);
    return true;
  }
  return false;
}

StringRef CSKY::getArchExtFeature(StringRef ArchExt) {
  bool Negated = stripNegationPrefix(ArchExt);
  for (const auto &AE : CSKYARCHExtNames) {
    if (AE.Feature && ArchExt == AE.getName())
      return StringRef(Negated ? AE.NegFeature : AE.Feature);
  }

  return StringRef();
}

bool CSKY::getExtensionFeatures(uint64_t Extensions,
                                std::vector<StringRef> &Features) {
  if (Extensions == CSKY::AEK_INVALID)
    return false;

  for (const auto &AE : CSKYARCHExtNames) {
    if ((Extensions & AE.ID) == AE.ID && AE.Feature)
      Features.push_back(AE.Feature);
  }

  return true;
}