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