Coverage Report

Created: 2024-01-17 10:31

/src/llvm-project/clang/lib/Basic/Targets/Mips.h
Line
Count
Source (jump to first uncovered line)
1
//===--- Mips.h - Declare Mips target feature support -----------*- C++ -*-===//
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 declares Mips TargetInfo objects.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#ifndef LLVM_CLANG_LIB_BASIC_TARGETS_MIPS_H
14
#define LLVM_CLANG_LIB_BASIC_TARGETS_MIPS_H
15
16
#include "clang/Basic/TargetInfo.h"
17
#include "clang/Basic/TargetOptions.h"
18
#include "llvm/Support/Compiler.h"
19
#include "llvm/TargetParser/Triple.h"
20
21
namespace clang {
22
namespace targets {
23
24
class LLVM_LIBRARY_VISIBILITY MipsTargetInfo : public TargetInfo {
25
0
  void setDataLayout() {
26
0
    StringRef Layout;
27
28
0
    if (ABI == "o32")
29
0
      Layout = "m:m-p:32:32-i8:8:32-i16:16:32-i64:64-n32-S64";
30
0
    else if (ABI == "n32")
31
0
      Layout = "m:e-p:32:32-i8:8:32-i16:16:32-i64:64-n32:64-S128";
32
0
    else if (ABI == "n64")
33
0
      Layout = "m:e-i8:8:32-i16:16:32-i64:64-n32:64-S128";
34
0
    else
35
0
      llvm_unreachable("Invalid ABI");
36
37
0
    if (BigEndian)
38
0
      resetDataLayout(("E-" + Layout).str());
39
0
    else
40
0
      resetDataLayout(("e-" + Layout).str());
41
0
  }
42
43
  std::string CPU;
44
  bool IsMips16;
45
  bool IsMicromips;
46
  bool IsNan2008;
47
  bool IsAbs2008;
48
  bool IsSingleFloat;
49
  bool IsNoABICalls;
50
  bool CanUseBSDABICalls;
51
  enum MipsFloatABI { HardFloat, SoftFloat } FloatABI;
52
  enum DspRevEnum { NoDSP, DSP1, DSP2 } DspRev;
53
  bool HasMSA;
54
  bool DisableMadd4;
55
  bool UseIndirectJumpHazard;
56
  bool NoOddSpreg;
57
58
protected:
59
  enum FPModeEnum { FPXX, FP32, FP64 } FPMode;
60
  std::string ABI;
61
62
public:
63
  MipsTargetInfo(const llvm::Triple &Triple, const TargetOptions &)
64
      : TargetInfo(Triple), IsMips16(false), IsMicromips(false),
65
        IsNan2008(false), IsAbs2008(false), IsSingleFloat(false),
66
        IsNoABICalls(false), CanUseBSDABICalls(false), FloatABI(HardFloat),
67
        DspRev(NoDSP), HasMSA(false), DisableMadd4(false),
68
0
        UseIndirectJumpHazard(false), FPMode(FPXX) {
69
0
    TheCXXABI.set(TargetCXXABI::GenericMIPS);
70
71
0
    if (Triple.isMIPS32())
72
0
      setABI("o32");
73
0
    else if (Triple.getEnvironment() == llvm::Triple::GNUABIN32)
74
0
      setABI("n32");
75
0
    else
76
0
      setABI("n64");
77
78
0
    CPU = ABI == "o32" ? "mips32r2" : "mips64r2";
79
80
0
    CanUseBSDABICalls = Triple.isOSFreeBSD() ||
81
0
                        Triple.isOSOpenBSD();
82
0
  }
83
84
0
  bool isIEEE754_2008Default() const {
85
0
    return CPU == "mips32r6" || CPU == "mips64r6";
86
0
  }
87
88
0
  bool isFP64Default() const {
89
0
    return CPU == "mips32r6" || ABI == "n32" || ABI == "n64" || ABI == "64";
90
0
  }
91
92
0
  bool isNan2008() const override { return IsNan2008; }
93
94
  bool processorSupportsGPR64() const;
95
96
0
  StringRef getABI() const override { return ABI; }
97
98
0
  bool setABI(const std::string &Name) override {
99
0
    if (Name == "o32") {
100
0
      setO32ABITypes();
101
0
      ABI = Name;
102
0
      return true;
103
0
    }
104
105
0
    if (Name == "n32") {
106
0
      setN32ABITypes();
107
0
      ABI = Name;
108
0
      return true;
109
0
    }
110
0
    if (Name == "n64") {
111
0
      setN64ABITypes();
112
0
      ABI = Name;
113
0
      return true;
114
0
    }
115
0
    return false;
116
0
  }
117
118
0
  void setO32ABITypes() {
119
0
    Int64Type = SignedLongLong;
120
0
    IntMaxType = Int64Type;
121
0
    LongDoubleFormat = &llvm::APFloat::IEEEdouble();
122
0
    LongDoubleWidth = LongDoubleAlign = 64;
123
0
    LongWidth = LongAlign = 32;
124
0
    MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 32;
125
0
    PointerWidth = PointerAlign = 32;
126
0
    PtrDiffType = SignedInt;
127
0
    SizeType = UnsignedInt;
128
0
    SuitableAlign = 64;
129
0
  }
130
131
0
  void setN32N64ABITypes() {
132
0
    LongDoubleWidth = LongDoubleAlign = 128;
133
0
    LongDoubleFormat = &llvm::APFloat::IEEEquad();
134
0
    if (getTriple().isOSFreeBSD()) {
135
0
      LongDoubleWidth = LongDoubleAlign = 64;
136
0
      LongDoubleFormat = &llvm::APFloat::IEEEdouble();
137
0
    }
138
0
    MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 64;
139
0
    SuitableAlign = 128;
140
0
  }
141
142
0
  void setN64ABITypes() {
143
0
    setN32N64ABITypes();
144
0
    if (getTriple().isOSOpenBSD()) {
145
0
      Int64Type = SignedLongLong;
146
0
    } else {
147
0
      Int64Type = SignedLong;
148
0
    }
149
0
    IntMaxType = Int64Type;
150
0
    LongWidth = LongAlign = 64;
151
0
    PointerWidth = PointerAlign = 64;
152
0
    PtrDiffType = SignedLong;
153
0
    SizeType = UnsignedLong;
154
0
  }
155
156
0
  void setN32ABITypes() {
157
0
    setN32N64ABITypes();
158
0
    Int64Type = SignedLongLong;
159
0
    IntMaxType = Int64Type;
160
0
    LongWidth = LongAlign = 32;
161
0
    PointerWidth = PointerAlign = 32;
162
0
    PtrDiffType = SignedInt;
163
0
    SizeType = UnsignedInt;
164
0
  }
165
166
  bool isValidCPUName(StringRef Name) const override;
167
  void fillValidCPUList(SmallVectorImpl<StringRef> &Values) const override;
168
169
0
  bool setCPU(const std::string &Name) override {
170
0
    CPU = Name;
171
0
    return isValidCPUName(Name);
172
0
  }
173
174
0
  const std::string &getCPU() const { return CPU; }
175
  bool
176
  initFeatureMap(llvm::StringMap<bool> &Features, DiagnosticsEngine &Diags,
177
                 StringRef CPU,
178
0
                 const std::vector<std::string> &FeaturesVec) const override {
179
0
    if (CPU.empty())
180
0
      CPU = getCPU();
181
0
    if (CPU == "octeon")
182
0
      Features["mips64r2"] = Features["cnmips"] = true;
183
0
    else if (CPU == "octeon+")
184
0
      Features["mips64r2"] = Features["cnmips"] = Features["cnmipsp"] = true;
185
0
    else
186
0
      Features[CPU] = true;
187
0
    return TargetInfo::initFeatureMap(Features, Diags, CPU, FeaturesVec);
188
0
  }
189
190
  unsigned getISARev() const;
191
192
  void getTargetDefines(const LangOptions &Opts,
193
                        MacroBuilder &Builder) const override;
194
195
  ArrayRef<Builtin::Info> getTargetBuiltins() const override;
196
197
  bool hasFeature(StringRef Feature) const override;
198
199
0
  BuiltinVaListKind getBuiltinVaListKind() const override {
200
0
    return TargetInfo::VoidPtrBuiltinVaList;
201
0
  }
202
203
0
  ArrayRef<const char *> getGCCRegNames() const override {
204
0
    static const char *const GCCRegNames[] = {
205
        // CPU register names
206
        // Must match second column of GCCRegAliases
207
0
        "$0", "$1", "$2", "$3", "$4", "$5", "$6", "$7", "$8", "$9", "$10",
208
0
        "$11", "$12", "$13", "$14", "$15", "$16", "$17", "$18", "$19", "$20",
209
0
        "$21", "$22", "$23", "$24", "$25", "$26", "$27", "$28", "$29", "$30",
210
0
        "$31",
211
        // Floating point register names
212
0
        "$f0", "$f1", "$f2", "$f3", "$f4", "$f5", "$f6", "$f7", "$f8", "$f9",
213
0
        "$f10", "$f11", "$f12", "$f13", "$f14", "$f15", "$f16", "$f17", "$f18",
214
0
        "$f19", "$f20", "$f21", "$f22", "$f23", "$f24", "$f25", "$f26", "$f27",
215
0
        "$f28", "$f29", "$f30", "$f31",
216
        // Hi/lo and condition register names
217
0
        "hi", "lo", "", "$fcc0", "$fcc1", "$fcc2", "$fcc3", "$fcc4", "$fcc5",
218
0
        "$fcc6", "$fcc7", "$ac1hi", "$ac1lo", "$ac2hi", "$ac2lo", "$ac3hi",
219
0
        "$ac3lo",
220
        // MSA register names
221
0
        "$w0", "$w1", "$w2", "$w3", "$w4", "$w5", "$w6", "$w7", "$w8", "$w9",
222
0
        "$w10", "$w11", "$w12", "$w13", "$w14", "$w15", "$w16", "$w17", "$w18",
223
0
        "$w19", "$w20", "$w21", "$w22", "$w23", "$w24", "$w25", "$w26", "$w27",
224
0
        "$w28", "$w29", "$w30", "$w31",
225
        // MSA control register names
226
0
        "$msair", "$msacsr", "$msaaccess", "$msasave", "$msamodify",
227
0
        "$msarequest", "$msamap", "$msaunmap"
228
0
    };
229
0
    return llvm::ArrayRef(GCCRegNames);
230
0
  }
231
232
  bool validateAsmConstraint(const char *&Name,
233
0
                             TargetInfo::ConstraintInfo &Info) const override {
234
0
    switch (*Name) {
235
0
    default:
236
0
      return false;
237
0
    case 'r': // CPU registers.
238
0
    case 'd': // Equivalent to "r" unless generating MIPS16 code.
239
0
    case 'y': // Equivalent to "r", backward compatibility only.
240
0
    case 'f': // floating-point registers.
241
0
    case 'c': // $25 for indirect jumps
242
0
    case 'l': // lo register
243
0
    case 'x': // hilo register pair
244
0
      Info.setAllowsRegister();
245
0
      return true;
246
0
    case 'I': // Signed 16-bit constant
247
0
    case 'J': // Integer 0
248
0
    case 'K': // Unsigned 16-bit constant
249
0
    case 'L': // Signed 32-bit constant, lower 16-bit zeros (for lui)
250
0
    case 'M': // Constants not loadable via lui, addiu, or ori
251
0
    case 'N': // Constant -1 to -65535
252
0
    case 'O': // A signed 15-bit constant
253
0
    case 'P': // A constant between 1 go 65535
254
0
      return true;
255
0
    case 'R': // An address that can be used in a non-macro load or store
256
0
      Info.setAllowsMemory();
257
0
      return true;
258
0
    case 'Z':
259
0
      if (Name[1] == 'C') { // An address usable by ll, and sc.
260
0
        Info.setAllowsMemory();
261
0
        Name++; // Skip over 'Z'.
262
0
        return true;
263
0
      }
264
0
      return false;
265
0
    }
266
0
  }
267
268
0
  std::string convertConstraint(const char *&Constraint) const override {
269
0
    std::string R;
270
0
    switch (*Constraint) {
271
0
    case 'Z': // Two-character constraint; add "^" hint for later parsing.
272
0
      if (Constraint[1] == 'C') {
273
0
        R = std::string("^") + std::string(Constraint, 2);
274
0
        Constraint++;
275
0
        return R;
276
0
      }
277
0
      break;
278
0
    }
279
0
    return TargetInfo::convertConstraint(Constraint);
280
0
  }
281
282
0
  std::string_view getClobbers() const override {
283
    // In GCC, $1 is not widely used in generated code (it's used only in a few
284
    // specific situations), so there is no real need for users to add it to
285
    // the clobbers list if they want to use it in their inline assembly code.
286
    //
287
    // In LLVM, $1 is treated as a normal GPR and is always allocatable during
288
    // code generation, so using it in inline assembly without adding it to the
289
    // clobbers list can cause conflicts between the inline assembly code and
290
    // the surrounding generated code.
291
    //
292
    // Another problem is that LLVM is allowed to choose $1 for inline assembly
293
    // operands, which will conflict with the ".set at" assembler option (which
294
    // we use only for inline assembly, in order to maintain compatibility with
295
    // GCC) and will also conflict with the user's usage of $1.
296
    //
297
    // The easiest way to avoid these conflicts and keep $1 as an allocatable
298
    // register for generated code is to automatically clobber $1 for all inline
299
    // assembly code.
300
    //
301
    // FIXME: We should automatically clobber $1 only for inline assembly code
302
    // which actually uses it. This would allow LLVM to use $1 for inline
303
    // assembly operands if the user's assembly code doesn't use it.
304
0
    return "~{$1}";
305
0
  }
306
307
  bool handleTargetFeatures(std::vector<std::string> &Features,
308
0
                            DiagnosticsEngine &Diags) override {
309
0
    IsMips16 = false;
310
0
    IsMicromips = false;
311
0
    IsNan2008 = isIEEE754_2008Default();
312
0
    IsAbs2008 = isIEEE754_2008Default();
313
0
    IsSingleFloat = false;
314
0
    FloatABI = HardFloat;
315
0
    DspRev = NoDSP;
316
0
    FPMode = isFP64Default() ? FP64 : FPXX;
317
0
    NoOddSpreg = false;
318
0
    bool OddSpregGiven = false;
319
320
0
    for (const auto &Feature : Features) {
321
0
      if (Feature == "+single-float")
322
0
        IsSingleFloat = true;
323
0
      else if (Feature == "+soft-float")
324
0
        FloatABI = SoftFloat;
325
0
      else if (Feature == "+mips16")
326
0
        IsMips16 = true;
327
0
      else if (Feature == "+micromips")
328
0
        IsMicromips = true;
329
0
      else if (Feature == "+dsp")
330
0
        DspRev = std::max(DspRev, DSP1);
331
0
      else if (Feature == "+dspr2")
332
0
        DspRev = std::max(DspRev, DSP2);
333
0
      else if (Feature == "+msa")
334
0
        HasMSA = true;
335
0
      else if (Feature == "+nomadd4")
336
0
        DisableMadd4 = true;
337
0
      else if (Feature == "+fp64")
338
0
        FPMode = FP64;
339
0
      else if (Feature == "-fp64")
340
0
        FPMode = FP32;
341
0
      else if (Feature == "+fpxx")
342
0
        FPMode = FPXX;
343
0
      else if (Feature == "+nan2008")
344
0
        IsNan2008 = true;
345
0
      else if (Feature == "-nan2008")
346
0
        IsNan2008 = false;
347
0
      else if (Feature == "+abs2008")
348
0
        IsAbs2008 = true;
349
0
      else if (Feature == "-abs2008")
350
0
        IsAbs2008 = false;
351
0
      else if (Feature == "+noabicalls")
352
0
        IsNoABICalls = true;
353
0
      else if (Feature == "+use-indirect-jump-hazard")
354
0
        UseIndirectJumpHazard = true;
355
0
      else if (Feature == "+nooddspreg") {
356
0
        NoOddSpreg = true;
357
0
        OddSpregGiven = false;
358
0
      } else if (Feature == "-nooddspreg") {
359
0
        NoOddSpreg = false;
360
0
        OddSpregGiven = true;
361
0
      }
362
0
    }
363
364
0
    if (FPMode == FPXX && !OddSpregGiven)
365
0
      NoOddSpreg = true;
366
367
0
    setDataLayout();
368
369
0
    return true;
370
0
  }
371
372
0
  int getEHDataRegisterNumber(unsigned RegNo) const override {
373
0
    if (RegNo == 0)
374
0
      return 4;
375
0
    if (RegNo == 1)
376
0
      return 5;
377
0
    return -1;
378
0
  }
379
380
0
  bool isCLZForZeroUndef() const override { return false; }
381
382
0
  ArrayRef<TargetInfo::GCCRegAlias> getGCCRegAliases() const override {
383
0
    static const TargetInfo::GCCRegAlias O32RegAliases[] = {
384
0
        {{"at"}, "$1"},  {{"v0"}, "$2"},         {{"v1"}, "$3"},
385
0
        {{"a0"}, "$4"},  {{"a1"}, "$5"},         {{"a2"}, "$6"},
386
0
        {{"a3"}, "$7"},  {{"t0"}, "$8"},         {{"t1"}, "$9"},
387
0
        {{"t2"}, "$10"}, {{"t3"}, "$11"},        {{"t4"}, "$12"},
388
0
        {{"t5"}, "$13"}, {{"t6"}, "$14"},        {{"t7"}, "$15"},
389
0
        {{"s0"}, "$16"}, {{"s1"}, "$17"},        {{"s2"}, "$18"},
390
0
        {{"s3"}, "$19"}, {{"s4"}, "$20"},        {{"s5"}, "$21"},
391
0
        {{"s6"}, "$22"}, {{"s7"}, "$23"},        {{"t8"}, "$24"},
392
0
        {{"t9"}, "$25"}, {{"k0"}, "$26"},        {{"k1"}, "$27"},
393
0
        {{"gp"}, "$28"}, {{"sp", "$sp"}, "$29"}, {{"fp", "$fp"}, "$30"},
394
0
        {{"ra"}, "$31"}
395
0
    };
396
0
    static const TargetInfo::GCCRegAlias NewABIRegAliases[] = {
397
0
        {{"at"}, "$1"},  {{"v0"}, "$2"},         {{"v1"}, "$3"},
398
0
        {{"a0"}, "$4"},  {{"a1"}, "$5"},         {{"a2"}, "$6"},
399
0
        {{"a3"}, "$7"},  {{"a4"}, "$8"},         {{"a5"}, "$9"},
400
0
        {{"a6"}, "$10"}, {{"a7"}, "$11"},        {{"t0"}, "$12"},
401
0
        {{"t1"}, "$13"}, {{"t2"}, "$14"},        {{"t3"}, "$15"},
402
0
        {{"s0"}, "$16"}, {{"s1"}, "$17"},        {{"s2"}, "$18"},
403
0
        {{"s3"}, "$19"}, {{"s4"}, "$20"},        {{"s5"}, "$21"},
404
0
        {{"s6"}, "$22"}, {{"s7"}, "$23"},        {{"t8"}, "$24"},
405
0
        {{"t9"}, "$25"}, {{"k0"}, "$26"},        {{"k1"}, "$27"},
406
0
        {{"gp"}, "$28"}, {{"sp", "$sp"}, "$29"}, {{"fp", "$fp"}, "$30"},
407
0
        {{"ra"}, "$31"}
408
0
    };
409
0
    if (ABI == "o32")
410
0
      return llvm::ArrayRef(O32RegAliases);
411
0
    return llvm::ArrayRef(NewABIRegAliases);
412
0
  }
413
414
0
  bool hasInt128Type() const override {
415
0
    return (ABI == "n32" || ABI == "n64") || getTargetOpts().ForceEnableInt128;
416
0
  }
417
418
  unsigned getUnwindWordWidth() const override;
419
420
  bool validateTarget(DiagnosticsEngine &Diags) const override;
421
0
  bool hasBitIntType() const override { return true; }
422
};
423
} // namespace targets
424
} // namespace clang
425
426
#endif // LLVM_CLANG_LIB_BASIC_TARGETS_MIPS_H