Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/clang/lib/Basic/Targets/Mips.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- Mips.cpp - Implement Mips target feature support -----------------===//
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
// This file implements Mips TargetInfo objects.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "Mips.h"
14
#include "Targets.h"
15
#include "clang/Basic/Diagnostic.h"
16
#include "clang/Basic/MacroBuilder.h"
17
#include "clang/Basic/TargetBuiltins.h"
18
#include "llvm/ADT/StringSwitch.h"
19
20
using namespace clang;
21
using namespace clang::targets;
22
23
const Builtin::Info MipsTargetInfo::BuiltinInfo[] = {
24
#define BUILTIN(ID, TYPE, ATTRS)                                               \
25
  {#ID, TYPE, ATTRS, nullptr, ALL_LANGUAGES, nullptr},
26
#define LIBBUILTIN(ID, TYPE, ATTRS, HEADER)                                    \
27
  {#ID, TYPE, ATTRS, HEADER, ALL_LANGUAGES, nullptr},
28
#include "clang/Basic/BuiltinsMips.def"
29
};
30
31
493
bool MipsTargetInfo::processorSupportsGPR64() const {
32
493
  return llvm::StringSwitch<bool>(CPU)
33
493
      .Case("mips3", true)
34
493
      .Case("mips4", true)
35
493
      .Case("mips5", true)
36
493
      .Case("mips64", true)
37
493
      .Case("mips64r2", true)
38
493
      .Case("mips64r3", true)
39
493
      .Case("mips64r5", true)
40
493
      .Case("mips64r6", true)
41
493
      .Case("octeon", true)
42
493
      .Default(false);
43
493
  
return false0
;
44
493
}
45
46
static constexpr llvm::StringLiteral ValidCPUNames[] = {
47
    {"mips1"},  {"mips2"},    {"mips3"},    {"mips4"},    {"mips5"},
48
    {"mips32"}, {"mips32r2"}, {"mips32r3"}, {"mips32r5"}, {"mips32r6"},
49
    {"mips64"}, {"mips64r2"}, {"mips64r3"}, {"mips64r5"}, {"mips64r6"},
50
    {"octeon"}, {"p5600"}};
51
52
62
bool MipsTargetInfo::isValidCPUName(StringRef Name) const {
53
62
  return llvm::find(ValidCPUNames, Name) != std::end(ValidCPUNames);
54
62
}
55
56
void MipsTargetInfo::fillValidCPUList(
57
2
    SmallVectorImpl<StringRef> &Values) const {
58
2
  Values.append(std::begin(ValidCPUNames), std::end(ValidCPUNames));
59
2
}
60
61
367
unsigned MipsTargetInfo::getISARev() const {
62
367
  return llvm::StringSwitch<unsigned>(getCPU())
63
367
             .Cases("mips32", "mips64", 1)
64
367
             .Cases("mips32r2", "mips64r2", 2)
65
367
             .Cases("mips32r3", "mips64r3", 3)
66
367
             .Cases("mips32r5", "mips64r5", 5)
67
367
             .Cases("mips32r6", "mips64r6", 6)
68
367
             .Default(0);
69
367
}
70
71
void MipsTargetInfo::getTargetDefines(const LangOptions &Opts,
72
240
                                      MacroBuilder &Builder) const {
73
240
  if (BigEndian) {
74
156
    DefineStd(Builder, "MIPSEB", Opts);
75
156
    Builder.defineMacro("_MIPSEB");
76
156
  } else {
77
84
    DefineStd(Builder, "MIPSEL", Opts);
78
84
    Builder.defineMacro("_MIPSEL");
79
84
  }
80
240
81
240
  Builder.defineMacro("__mips__");
82
240
  Builder.defineMacro("_mips");
83
240
  if (Opts.GNUMode)
84
212
    Builder.defineMacro("mips");
85
240
86
240
  if (ABI == "o32") {
87
121
    Builder.defineMacro("__mips", "32");
88
121
    Builder.defineMacro("_MIPS_ISA", "_MIPS_ISA_MIPS32");
89
121
  } else {
90
119
    Builder.defineMacro("__mips", "64");
91
119
    Builder.defineMacro("__mips64");
92
119
    Builder.defineMacro("__mips64__");
93
119
    Builder.defineMacro("_MIPS_ISA", "_MIPS_ISA_MIPS64");
94
119
  }
95
240
96
240
  const std::string ISARev = std::to_string(getISARev());
97
240
98
240
  if (!ISARev.empty())
99
240
    Builder.defineMacro("__mips_isa_rev", ISARev);
100
240
101
240
  if (ABI == "o32") {
102
121
    Builder.defineMacro("__mips_o32");
103
121
    Builder.defineMacro("_ABIO32", "1");
104
121
    Builder.defineMacro("_MIPS_SIM", "_ABIO32");
105
121
  } else 
if (119
ABI == "n32"119
) {
106
31
    Builder.defineMacro("__mips_n32");
107
31
    Builder.defineMacro("_ABIN32", "2");
108
31
    Builder.defineMacro("_MIPS_SIM", "_ABIN32");
109
88
  } else if (ABI == "n64") {
110
88
    Builder.defineMacro("__mips_n64");
111
88
    Builder.defineMacro("_ABI64", "3");
112
88
    Builder.defineMacro("_MIPS_SIM", "_ABI64");
113
88
  } else
114
88
    
llvm_unreachable0
("Invalid ABI.");
115
240
116
240
  if (!IsNoABICalls) {
117
240
    Builder.defineMacro("__mips_abicalls");
118
240
    if (CanUseBSDABICalls)
119
8
      Builder.defineMacro("__ABICALLS__");
120
240
  }
121
240
122
240
  Builder.defineMacro("__REGISTER_PREFIX__", "");
123
240
124
240
  switch (FloatABI) {
125
240
  case HardFloat:
126
237
    Builder.defineMacro("__mips_hard_float", Twine(1));
127
237
    break;
128
240
  case SoftFloat:
129
3
    Builder.defineMacro("__mips_soft_float", Twine(1));
130
3
    break;
131
240
  }
132
240
133
240
  if (IsSingleFloat)
134
4
    Builder.defineMacro("__mips_single_float", Twine(1));
135
240
136
240
  switch (FPMode) {
137
240
  case FPXX:
138
112
    Builder.defineMacro("__mips_fpr", Twine(0));
139
112
    break;
140
240
  case FP32:
141
2
    Builder.defineMacro("__mips_fpr", Twine(32));
142
2
    break;
143
240
  case FP64:
144
126
    Builder.defineMacro("__mips_fpr", Twine(64));
145
126
    break;
146
240
}
147
240
148
240
  if (FPMode == FP64 || 
IsSingleFloat114
)
149
130
    Builder.defineMacro("_MIPS_FPSET", Twine(32));
150
110
  else
151
110
    Builder.defineMacro("_MIPS_FPSET", Twine(16));
152
240
153
240
  if (IsMips16)
154
1
    Builder.defineMacro("__mips16", Twine(1));
155
240
156
240
  if (IsMicromips)
157
1
    Builder.defineMacro("__mips_micromips", Twine(1));
158
240
159
240
  if (IsNan2008)
160
12
    Builder.defineMacro("__mips_nan2008", Twine(1));
161
240
162
240
  if (IsAbs2008)
163
9
    Builder.defineMacro("__mips_abs2008", Twine(1));
164
240
165
240
  switch (DspRev) {
166
240
  default:
167
238
    break;
168
240
  case DSP1:
169
1
    Builder.defineMacro("__mips_dsp_rev", Twine(1));
170
1
    Builder.defineMacro("__mips_dsp", Twine(1));
171
1
    break;
172
240
  case DSP2:
173
1
    Builder.defineMacro("__mips_dsp_rev", Twine(2));
174
1
    Builder.defineMacro("__mips_dspr2", Twine(1));
175
1
    Builder.defineMacro("__mips_dsp", Twine(1));
176
1
    break;
177
240
  }
178
240
179
240
  if (HasMSA)
180
3
    Builder.defineMacro("__mips_msa", Twine(1));
181
240
182
240
  if (DisableMadd4)
183
3
    Builder.defineMacro("__mips_no_madd4", Twine(1));
184
240
185
240
  Builder.defineMacro("_MIPS_SZPTR", Twine(getPointerWidth(0)));
186
240
  Builder.defineMacro("_MIPS_SZINT", Twine(getIntWidth()));
187
240
  Builder.defineMacro("_MIPS_SZLONG", Twine(getLongWidth()));
188
240
189
240
  Builder.defineMacro("_MIPS_ARCH", "\"" + CPU + "\"");
190
240
  Builder.defineMacro("_MIPS_ARCH_" + StringRef(CPU).upper());
191
240
192
240
  // These shouldn't be defined for MIPS-I but there's no need to check
193
240
  // for that since MIPS-I isn't supported.
194
240
  Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_1");
195
240
  Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_2");
196
240
  Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4");
197
240
198
240
  // 32-bit MIPS processors don't have the necessary lld/scd instructions
199
240
  // found in 64-bit processors. In the case of O32 on a 64-bit processor,
200
240
  // the instructions exist but using them violates the ABI since they
201
240
  // require 64-bit GPRs and O32 only supports 32-bit GPRs.
202
240
  if (ABI == "n32" || 
ABI == "n64"209
)
203
119
    Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_8");
204
240
}
205
206
8
bool MipsTargetInfo::hasFeature(StringRef Feature) const {
207
8
  return llvm::StringSwitch<bool>(Feature)
208
8
      .Case("mips", true)
209
8
      .Case("fp64", FPMode == FP64)
210
8
      .Default(false);
211
8
}
212
213
241
ArrayRef<Builtin::Info> MipsTargetInfo::getTargetBuiltins() const {
214
241
  return llvm::makeArrayRef(BuiltinInfo, clang::Mips::LastTSBuiltin -
215
241
                                             Builtin::FirstTSBuiltin);
216
241
}
217
218
10
unsigned MipsTargetInfo::getUnwindWordWidth() const {
219
10
  return llvm::StringSwitch<unsigned>(ABI)
220
10
      .Case("o32", 32)
221
10
      .Case("n32", 64)
222
10
      .Case("n64", 64)
223
10
      .Default(getPointerWidth(0));
224
10
}
225
226
247
bool MipsTargetInfo::validateTarget(DiagnosticsEngine &Diags) const {
227
247
  // microMIPS64R6 backend was removed.
228
247
  if (getTriple().isMIPS64() && 
IsMicromips125
&&
(0
ABI == "n32"0
||
ABI == "n64"0
)) {
229
0
    Diags.Report(diag::err_target_unsupported_cpu_for_micromips) << CPU;
230
0
    return false;
231
0
  }
232
247
  // FIXME: It's valid to use O32 on a 64-bit CPU but the backend can't handle
233
247
  //        this yet. It's better to fail here than on the backend assertion.
234
247
  if (processorSupportsGPR64() && 
ABI == "o32"124
) {
235
1
    Diags.Report(diag::err_target_unsupported_abi) << ABI << CPU;
236
1
    return false;
237
1
  }
238
246
239
246
  // 64-bit ABI's require 64-bit CPU's.
240
246
  if (!processorSupportsGPR64() && 
(123
ABI == "n32"123
||
ABI == "n64"123
)) {
241
2
    Diags.Report(diag::err_target_unsupported_abi) << ABI << CPU;
242
2
    return false;
243
2
  }
244
244
245
244
  // FIXME: It's valid to use O32 on a mips64/mips64el triple but the backend
246
244
  //        can't handle this yet. It's better to fail here than on the
247
244
  //        backend assertion.
248
244
  if (getTriple().isMIPS64() && 
ABI == "o32"123
) {
249
0
    Diags.Report(diag::err_target_unsupported_abi_for_triple)
250
0
        << ABI << getTriple().str();
251
0
    return false;
252
0
  }
253
244
254
244
  // FIXME: It's valid to use N32/N64 on a mips/mipsel triple but the backend
255
244
  //        can't handle this yet. It's better to fail here than on the
256
244
  //        backend assertion.
257
244
  if (getTriple().isMIPS32() && 
(121
ABI == "n32"121
||
ABI == "n64"121
)) {
258
0
    Diags.Report(diag::err_target_unsupported_abi_for_triple)
259
0
        << ABI << getTriple().str();
260
0
    return false;
261
0
  }
262
244
263
244
  // -fpxx is valid only for the o32 ABI
264
244
  if (FPMode == FPXX && 
(114
ABI == "n32"114
||
ABI == "n64"114
)) {
265
2
    Diags.Report(diag::err_unsupported_abi_for_opt) << "-mfpxx" << "o32";
266
2
    return false;
267
2
  }
268
242
269
242
  // -mfp32 and n32/n64 ABIs are incompatible
270
242
  if (FPMode != FP64 && 
FPMode != FPXX115
&&
!IsSingleFloat3
&&
271
242
      
(2
ABI == "n32"2
||
ABI == "n64"2
)) {
272
1
    Diags.Report(diag::err_opt_not_valid_with_opt) << "-mfpxx" << CPU;
273
1
    return false;
274
1
  }
275
241
  // Mips revision 6 and -mfp32 are incompatible
276
241
  if (FPMode != FP64 && 
FPMode != FPXX114
&&
(2
CPU == "mips32r6"2
||
277
2
      CPU == "mips64r6")) {
278
0
    Diags.Report(diag::err_opt_not_valid_with_opt) << "-mfp32" << CPU;
279
0
    return false;
280
0
  }
281
241
  // Option -mfp64 permitted on Mips32 iff revision 2 or higher is present
282
241
  if (FPMode == FP64 && 
(127
CPU == "mips1"127
||
CPU == "mips2"127
||
283
127
      getISARev() < 2) && 
ABI == "o32"8
) {
284
0
    Diags.Report(diag::err_mips_fp64_req) << "-mfp64";
285
0
    return false;
286
0
  }
287
241
288
241
  return true;
289
241
}