clang  6.0.0svn
Mips.h
Go to the documentation of this file.
1 //===--- 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"
19 #include "llvm/ADT/Triple.h"
20 #include "llvm/Support/Compiler.h"
21 
22 namespace clang {
23 namespace targets {
24 
25 class 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 IsAbs2008;
50  bool IsSingleFloat;
51  bool IsNoABICalls;
52  bool CanUseBSDABICalls;
53  enum MipsFloatABI { HardFloat, SoftFloat } FloatABI;
54  enum DspRevEnum { NoDSP, DSP1, DSP2 } DspRev;
55  bool HasMSA;
56  bool DisableMadd4;
57 
58 protected:
59  bool HasFP64;
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), HasFP64(false) {
68  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 
81  bool isIEEE754_2008Default() const {
82  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 
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;
164 
165  bool setCPU(const std::string &Name) override {
166  CPU = Name;
167  return isValidCPUName(Name);
168  }
169 
170  const std::string &getCPU() const { return CPU; }
171  bool
172  initFeatureMap(llvm::StringMap<bool> &Features, DiagnosticsEngine &Diags,
173  StringRef CPU,
174  const std::vector<std::string> &FeaturesVec) const override {
175  if (CPU.empty())
176  CPU = getCPU();
177  if (CPU == "octeon")
178  Features["mips64r2"] = Features["cnmips"] = true;
179  else
180  Features[CPU] = true;
181  return TargetInfo::initFeatureMap(Features, Diags, CPU, FeaturesVec);
182  }
183 
184  void getTargetDefines(const LangOptions &Opts,
185  MacroBuilder &Builder) const override;
186 
187  ArrayRef<Builtin::Info> getTargetBuiltins() const override;
188 
189  bool hasFeature(StringRef Feature) const override;
190 
193  }
194 
196  static const char *const GCCRegNames[] = {
197  // CPU register names
198  // Must match second column of GCCRegAliases
199  "$0", "$1", "$2", "$3", "$4", "$5", "$6", "$7", "$8", "$9", "$10",
200  "$11", "$12", "$13", "$14", "$15", "$16", "$17", "$18", "$19", "$20",
201  "$21", "$22", "$23", "$24", "$25", "$26", "$27", "$28", "$29", "$30",
202  "$31",
203  // Floating point register names
204  "$f0", "$f1", "$f2", "$f3", "$f4", "$f5", "$f6", "$f7", "$f8", "$f9",
205  "$f10", "$f11", "$f12", "$f13", "$f14", "$f15", "$f16", "$f17", "$f18",
206  "$f19", "$f20", "$f21", "$f22", "$f23", "$f24", "$f25", "$f26", "$f27",
207  "$f28", "$f29", "$f30", "$f31",
208  // Hi/lo and condition register names
209  "hi", "lo", "", "$fcc0", "$fcc1", "$fcc2", "$fcc3", "$fcc4", "$fcc5",
210  "$fcc6", "$fcc7", "$ac1hi", "$ac1lo", "$ac2hi", "$ac2lo", "$ac3hi",
211  "$ac3lo",
212  // MSA register names
213  "$w0", "$w1", "$w2", "$w3", "$w4", "$w5", "$w6", "$w7", "$w8", "$w9",
214  "$w10", "$w11", "$w12", "$w13", "$w14", "$w15", "$w16", "$w17", "$w18",
215  "$w19", "$w20", "$w21", "$w22", "$w23", "$w24", "$w25", "$w26", "$w27",
216  "$w28", "$w29", "$w30", "$w31",
217  // MSA control register names
218  "$msair", "$msacsr", "$msaaccess", "$msasave", "$msamodify",
219  "$msarequest", "$msamap", "$msaunmap"
220  };
221  return llvm::makeArrayRef(GCCRegNames);
222  }
223 
224  bool validateAsmConstraint(const char *&Name,
225  TargetInfo::ConstraintInfo &Info) const override {
226  switch (*Name) {
227  default:
228  return false;
229  case 'r': // CPU registers.
230  case 'd': // Equivalent to "r" unless generating MIPS16 code.
231  case 'y': // Equivalent to "r", backward compatibility only.
232  case 'f': // floating-point registers.
233  case 'c': // $25 for indirect jumps
234  case 'l': // lo register
235  case 'x': // hilo register pair
236  Info.setAllowsRegister();
237  return true;
238  case 'I': // Signed 16-bit constant
239  case 'J': // Integer 0
240  case 'K': // Unsigned 16-bit constant
241  case 'L': // Signed 32-bit constant, lower 16-bit zeros (for lui)
242  case 'M': // Constants not loadable via lui, addiu, or ori
243  case 'N': // Constant -1 to -65535
244  case 'O': // A signed 15-bit constant
245  case 'P': // A constant between 1 go 65535
246  return true;
247  case 'R': // An address that can be used in a non-macro load or store
248  Info.setAllowsMemory();
249  return true;
250  case 'Z':
251  if (Name[1] == 'C') { // An address usable by ll, and sc.
252  Info.setAllowsMemory();
253  Name++; // Skip over 'Z'.
254  return true;
255  }
256  return false;
257  }
258  }
259 
260  std::string convertConstraint(const char *&Constraint) const override {
261  std::string R;
262  switch (*Constraint) {
263  case 'Z': // Two-character constraint; add "^" hint for later parsing.
264  if (Constraint[1] == 'C') {
265  R = std::string("^") + std::string(Constraint, 2);
266  Constraint++;
267  return R;
268  }
269  break;
270  }
271  return TargetInfo::convertConstraint(Constraint);
272  }
273 
274  const char *getClobbers() const override {
275  // In GCC, $1 is not widely used in generated code (it's used only in a few
276  // specific situations), so there is no real need for users to add it to
277  // the clobbers list if they want to use it in their inline assembly code.
278  //
279  // In LLVM, $1 is treated as a normal GPR and is always allocatable during
280  // code generation, so using it in inline assembly without adding it to the
281  // clobbers list can cause conflicts between the inline assembly code and
282  // the surrounding generated code.
283  //
284  // Another problem is that LLVM is allowed to choose $1 for inline assembly
285  // operands, which will conflict with the ".set at" assembler option (which
286  // we use only for inline assembly, in order to maintain compatibility with
287  // GCC) and will also conflict with the user's usage of $1.
288  //
289  // The easiest way to avoid these conflicts and keep $1 as an allocatable
290  // register for generated code is to automatically clobber $1 for all inline
291  // assembly code.
292  //
293  // FIXME: We should automatically clobber $1 only for inline assembly code
294  // which actually uses it. This would allow LLVM to use $1 for inline
295  // assembly operands if the user's assembly code doesn't use it.
296  return "~{$1}";
297  }
298 
299  bool handleTargetFeatures(std::vector<std::string> &Features,
300  DiagnosticsEngine &Diags) override {
301  IsMips16 = false;
302  IsMicromips = false;
303  IsNan2008 = isIEEE754_2008Default();
304  IsAbs2008 = isIEEE754_2008Default();
305  IsSingleFloat = false;
306  FloatABI = HardFloat;
307  DspRev = NoDSP;
308  HasFP64 = isFP64Default();
309 
310  for (const auto &Feature : Features) {
311  if (Feature == "+single-float")
312  IsSingleFloat = true;
313  else if (Feature == "+soft-float")
314  FloatABI = SoftFloat;
315  else if (Feature == "+mips16")
316  IsMips16 = true;
317  else if (Feature == "+micromips")
318  IsMicromips = true;
319  else if (Feature == "+dsp")
320  DspRev = std::max(DspRev, DSP1);
321  else if (Feature == "+dspr2")
322  DspRev = std::max(DspRev, DSP2);
323  else if (Feature == "+msa")
324  HasMSA = true;
325  else if (Feature == "+nomadd4")
326  DisableMadd4 = true;
327  else if (Feature == "+fp64")
328  HasFP64 = true;
329  else if (Feature == "-fp64")
330  HasFP64 = false;
331  else if (Feature == "+nan2008")
332  IsNan2008 = true;
333  else if (Feature == "-nan2008")
334  IsNan2008 = false;
335  else if (Feature == "+abs2008")
336  IsAbs2008 = true;
337  else if (Feature == "-abs2008")
338  IsAbs2008 = false;
339  else if (Feature == "+noabicalls")
340  IsNoABICalls = true;
341  }
342 
343  setDataLayout();
344 
345  return true;
346  }
347 
348  int getEHDataRegisterNumber(unsigned RegNo) const override {
349  if (RegNo == 0)
350  return 4;
351  if (RegNo == 1)
352  return 5;
353  return -1;
354  }
355 
356  bool isCLZForZeroUndef() const override { return false; }
357 
359  static const TargetInfo::GCCRegAlias O32RegAliases[] = {
360  {{"at"}, "$1"}, {{"v0"}, "$2"}, {{"v1"}, "$3"},
361  {{"a0"}, "$4"}, {{"a1"}, "$5"}, {{"a2"}, "$6"},
362  {{"a3"}, "$7"}, {{"t0"}, "$8"}, {{"t1"}, "$9"},
363  {{"t2"}, "$10"}, {{"t3"}, "$11"}, {{"t4"}, "$12"},
364  {{"t5"}, "$13"}, {{"t6"}, "$14"}, {{"t7"}, "$15"},
365  {{"s0"}, "$16"}, {{"s1"}, "$17"}, {{"s2"}, "$18"},
366  {{"s3"}, "$19"}, {{"s4"}, "$20"}, {{"s5"}, "$21"},
367  {{"s6"}, "$22"}, {{"s7"}, "$23"}, {{"t8"}, "$24"},
368  {{"t9"}, "$25"}, {{"k0"}, "$26"}, {{"k1"}, "$27"},
369  {{"gp"}, "$28"}, {{"sp", "$sp"}, "$29"}, {{"fp", "$fp"}, "$30"},
370  {{"ra"}, "$31"}
371  };
372  static const TargetInfo::GCCRegAlias NewABIRegAliases[] = {
373  {{"at"}, "$1"}, {{"v0"}, "$2"}, {{"v1"}, "$3"},
374  {{"a0"}, "$4"}, {{"a1"}, "$5"}, {{"a2"}, "$6"},
375  {{"a3"}, "$7"}, {{"a4"}, "$8"}, {{"a5"}, "$9"},
376  {{"a6"}, "$10"}, {{"a7"}, "$11"}, {{"t0"}, "$12"},
377  {{"t1"}, "$13"}, {{"t2"}, "$14"}, {{"t3"}, "$15"},
378  {{"s0"}, "$16"}, {{"s1"}, "$17"}, {{"s2"}, "$18"},
379  {{"s3"}, "$19"}, {{"s4"}, "$20"}, {{"s5"}, "$21"},
380  {{"s6"}, "$22"}, {{"s7"}, "$23"}, {{"t8"}, "$24"},
381  {{"t9"}, "$25"}, {{"k0"}, "$26"}, {{"k1"}, "$27"},
382  {{"gp"}, "$28"}, {{"sp", "$sp"}, "$29"}, {{"fp", "$fp"}, "$30"},
383  {{"ra"}, "$31"}
384  };
385  if (ABI == "o32")
386  return llvm::makeArrayRef(O32RegAliases);
387  return llvm::makeArrayRef(NewABIRegAliases);
388  }
389 
390  bool hasInt128Type() const override { return ABI == "n32" || ABI == "n64"; }
391 
392  bool validateTarget(DiagnosticsEngine &Diags) const override;
393 };
394 } // namespace targets
395 } // namespace clang
396 
397 #endif // LLVM_CLANG_LIB_BASIC_TARGETS_MIPS_H
std::string convertConstraint(const char *&Constraint) const override
Definition: Mips.h:260
ArrayRef< const char * > getGCCRegNames() const override
Definition: Mips.h:195
BuiltinVaListKind getBuiltinVaListKind() const override
Returns the kind of __builtin_va_list type that should be used with this target.
Definition: Mips.h:191
static const Builtin::Info BuiltinInfo[]
Definition: Builtins.cpp:21
static bool hasFeature(StringRef Feature, const LangOptions &LangOpts, const TargetInfo &Target)
Determine whether a translation unit built using the current language options has the given feature...
Definition: Module.cpp:73
const char * getClobbers() const override
Returns a string of target-specific clobbers, in LLVM format.
Definition: Mips.h:274
Options for controlling the target.
Definition: TargetOptions.h:26
The generic Mips ABI is a modified version of the Itanium ABI.
Definition: TargetCXXABI.h:90
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:48
int getEHDataRegisterNumber(unsigned RegNo) const override
Return the register number that __builtin_eh_return_regno would return with the specified argument...
Definition: Mips.h:348
Concrete class used by the front-end to report problems and issues.
Definition: Diagnostic.h:147
bool handleTargetFeatures(std::vector< std::string > &Features, DiagnosticsEngine &Diags) override
Perform initialization based on the user configured set of features (e.g., +sse4).
Definition: Mips.h:299
static const char *const GCCRegNames[]
Definition: X86.cpp:42
typedef void* __builtin_va_list;
Definition: TargetInfo.h:157
Exposes information about the current target.
Definition: TargetInfo.h:54
virtual bool initFeatureMap(llvm::StringMap< bool > &Features, DiagnosticsEngine &Diags, StringRef CPU, const std::vector< std::string > &FeatureVec) const
Initialize the map with the default set of target features for the CPU this should include all legal ...
Definition: TargetInfo.cpp:347
bool setCPU(const std::string &Name) override
Target the specified CPU.
Definition: Mips.h:165
bool isFP64Default() const
Definition: Mips.h:85
bool hasInt128Type() const override
Determine whether the __int128 type is supported on this target.
Definition: Mips.h:390
bool isIEEE754_2008Default() const
Definition: Mips.h:81
bool initFeatureMap(llvm::StringMap< bool > &Features, DiagnosticsEngine &Diags, StringRef CPU, const std::vector< std::string > &FeaturesVec) const override
Initialize the map with the default set of target features for the CPU this should include all legal ...
Definition: Mips.h:172
bool isCLZForZeroUndef() const override
The __builtin_clz* and __builtin_ctz* built-in functions are specified to have undefined results for ...
Definition: Mips.h:356
#define false
Definition: stdbool.h:33
Defines the clang::TargetOptions class.
StringRef getABI() const override
Get the ABI currently in use.
Definition: Mips.h:93
const std::string & getCPU() const
Definition: Mips.h:170
Dataflow Directional Tag Classes.
ArrayRef< TargetInfo::GCCRegAlias > getGCCRegAliases() const override
Definition: Mips.h:358
bool isNan2008() const override
Returns true if NaN encoding is IEEE 754-2008.
Definition: Mips.h:89
BuiltinVaListKind
The different kinds of __builtin_va_list types defined by the target implementation.
Definition: TargetInfo.h:152
char __ovld __cnfn max(char x, char y)
Returns y if x < y, otherwise it returns x.
bool setABI(const std::string &Name) override
Use the specified ABI.
Definition: Mips.h:95
Defines the clang::TargetInfo interface.
bool validateAsmConstraint(const char *&Name, TargetInfo::ConstraintInfo &Info) const override
Definition: Mips.h:224
MipsTargetInfo(const llvm::Triple &Triple, const TargetOptions &)
Definition: Mips.h:63
virtual std::string convertConstraint(const char *&Constraint) const
Definition: TargetInfo.h:773