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