]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/lib/Basic/TargetInfo.cpp
Update clang to release_39 branch r276489, and resolve conflicts.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / clang / lib / Basic / TargetInfo.cpp
1 //===--- TargetInfo.cpp - Information about Target machine ----------------===//
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 implements the TargetInfo and TargetInfoImpl interfaces.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "clang/Basic/TargetInfo.h"
15 #include "clang/Basic/AddressSpaces.h"
16 #include "clang/Basic/CharInfo.h"
17 #include "clang/Basic/LangOptions.h"
18 #include "llvm/ADT/APFloat.h"
19 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/Support/ErrorHandling.h"
21 #include <cstdlib>
22 using namespace clang;
23
24 static const LangAS::Map DefaultAddrSpaceMap = { 0 };
25
26 // TargetInfo Constructor.
27 TargetInfo::TargetInfo(const llvm::Triple &T) : TargetOpts(), Triple(T) {
28   // Set defaults.  Defaults are set for a 32-bit RISC platform, like PPC or
29   // SPARC.  These should be overridden by concrete targets as needed.
30   BigEndian = true;
31   TLSSupported = true;
32   NoAsmVariants = false;
33   HasFloat128 = false;
34   PointerWidth = PointerAlign = 32;
35   BoolWidth = BoolAlign = 8;
36   IntWidth = IntAlign = 32;
37   LongWidth = LongAlign = 32;
38   LongLongWidth = LongLongAlign = 64;
39   SuitableAlign = 64;
40   DefaultAlignForAttributeAligned = 128;
41   MinGlobalAlign = 0;
42   HalfWidth = 16;
43   HalfAlign = 16;
44   FloatWidth = 32;
45   FloatAlign = 32;
46   DoubleWidth = 64;
47   DoubleAlign = 64;
48   LongDoubleWidth = 64;
49   LongDoubleAlign = 64;
50   Float128Align = 128;
51   LargeArrayMinWidth = 0;
52   LargeArrayAlign = 0;
53   MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 0;
54   MaxVectorAlign = 0;
55   MaxTLSAlign = 0;
56   SimdDefaultAlign = 0;
57   SizeType = UnsignedLong;
58   PtrDiffType = SignedLong;
59   IntMaxType = SignedLongLong;
60   IntPtrType = SignedLong;
61   WCharType = SignedInt;
62   WIntType = SignedInt;
63   Char16Type = UnsignedShort;
64   Char32Type = UnsignedInt;
65   Int64Type = SignedLongLong;
66   SigAtomicType = SignedInt;
67   ProcessIDType = SignedInt;
68   UseSignedCharForObjCBool = true;
69   UseBitFieldTypeAlignment = true;
70   UseZeroLengthBitfieldAlignment = false;
71   UseExplicitBitFieldAlignment = true;
72   ZeroLengthBitfieldBoundary = 0;
73   HalfFormat = &llvm::APFloat::IEEEhalf;
74   FloatFormat = &llvm::APFloat::IEEEsingle;
75   DoubleFormat = &llvm::APFloat::IEEEdouble;
76   LongDoubleFormat = &llvm::APFloat::IEEEdouble;
77   Float128Format = &llvm::APFloat::IEEEquad;
78   MCountName = "mcount";
79   RegParmMax = 0;
80   SSERegParmMax = 0;
81   HasAlignMac68kSupport = false;
82   HasBuiltinMSVaList = false;
83
84   // Default to no types using fpret.
85   RealTypeUsesObjCFPRet = 0;
86
87   // Default to not using fp2ret for __Complex long double
88   ComplexLongDoubleUsesFP2Ret = false;
89
90   // Set the C++ ABI based on the triple.
91   TheCXXABI.set(Triple.isKnownWindowsMSVCEnvironment()
92                     ? TargetCXXABI::Microsoft
93                     : TargetCXXABI::GenericItanium);
94
95   // Default to an empty address space map.
96   AddrSpaceMap = &DefaultAddrSpaceMap;
97   UseAddrSpaceMapMangling = false;
98
99   // Default to an unknown platform name.
100   PlatformName = "unknown";
101   PlatformMinVersion = VersionTuple();
102 }
103
104 // Out of line virtual dtor for TargetInfo.
105 TargetInfo::~TargetInfo() {}
106
107 /// getTypeName - Return the user string for the specified integer type enum.
108 /// For example, SignedShort -> "short".
109 const char *TargetInfo::getTypeName(IntType T) {
110   switch (T) {
111   default: llvm_unreachable("not an integer!");
112   case SignedChar:       return "signed char";
113   case UnsignedChar:     return "unsigned char";
114   case SignedShort:      return "short";
115   case UnsignedShort:    return "unsigned short";
116   case SignedInt:        return "int";
117   case UnsignedInt:      return "unsigned int";
118   case SignedLong:       return "long int";
119   case UnsignedLong:     return "long unsigned int";
120   case SignedLongLong:   return "long long int";
121   case UnsignedLongLong: return "long long unsigned int";
122   }
123 }
124
125 /// getTypeConstantSuffix - Return the constant suffix for the specified
126 /// integer type enum. For example, SignedLong -> "L".
127 const char *TargetInfo::getTypeConstantSuffix(IntType T) const {
128   switch (T) {
129   default: llvm_unreachable("not an integer!");
130   case SignedChar:
131   case SignedShort:
132   case SignedInt:        return "";
133   case SignedLong:       return "L";
134   case SignedLongLong:   return "LL";
135   case UnsignedChar:
136     if (getCharWidth() < getIntWidth())
137       return "";
138   case UnsignedShort:
139     if (getShortWidth() < getIntWidth())
140       return "";
141   case UnsignedInt:      return "U";
142   case UnsignedLong:     return "UL";
143   case UnsignedLongLong: return "ULL";
144   }
145 }
146
147 /// getTypeFormatModifier - Return the printf format modifier for the
148 /// specified integer type enum. For example, SignedLong -> "l".
149
150 const char *TargetInfo::getTypeFormatModifier(IntType T) {
151   switch (T) {
152   default: llvm_unreachable("not an integer!");
153   case SignedChar:
154   case UnsignedChar:     return "hh";
155   case SignedShort:
156   case UnsignedShort:    return "h";
157   case SignedInt:
158   case UnsignedInt:      return "";
159   case SignedLong:
160   case UnsignedLong:     return "l";
161   case SignedLongLong:
162   case UnsignedLongLong: return "ll";
163   }
164 }
165
166 /// getTypeWidth - Return the width (in bits) of the specified integer type
167 /// enum. For example, SignedInt -> getIntWidth().
168 unsigned TargetInfo::getTypeWidth(IntType T) const {
169   switch (T) {
170   default: llvm_unreachable("not an integer!");
171   case SignedChar:
172   case UnsignedChar:     return getCharWidth();
173   case SignedShort:
174   case UnsignedShort:    return getShortWidth();
175   case SignedInt:
176   case UnsignedInt:      return getIntWidth();
177   case SignedLong:
178   case UnsignedLong:     return getLongWidth();
179   case SignedLongLong:
180   case UnsignedLongLong: return getLongLongWidth();
181   };
182 }
183
184 TargetInfo::IntType TargetInfo::getIntTypeByWidth(
185     unsigned BitWidth, bool IsSigned) const {
186   if (getCharWidth() == BitWidth)
187     return IsSigned ? SignedChar : UnsignedChar;
188   if (getShortWidth() == BitWidth)
189     return IsSigned ? SignedShort : UnsignedShort;
190   if (getIntWidth() == BitWidth)
191     return IsSigned ? SignedInt : UnsignedInt;
192   if (getLongWidth() == BitWidth)
193     return IsSigned ? SignedLong : UnsignedLong;
194   if (getLongLongWidth() == BitWidth)
195     return IsSigned ? SignedLongLong : UnsignedLongLong;
196   return NoInt;
197 }
198
199 TargetInfo::IntType TargetInfo::getLeastIntTypeByWidth(unsigned BitWidth,
200                                                        bool IsSigned) const {
201   if (getCharWidth() >= BitWidth)
202     return IsSigned ? SignedChar : UnsignedChar;
203   if (getShortWidth() >= BitWidth)
204     return IsSigned ? SignedShort : UnsignedShort;
205   if (getIntWidth() >= BitWidth)
206     return IsSigned ? SignedInt : UnsignedInt;
207   if (getLongWidth() >= BitWidth)
208     return IsSigned ? SignedLong : UnsignedLong;
209   if (getLongLongWidth() >= BitWidth)
210     return IsSigned ? SignedLongLong : UnsignedLongLong;
211   return NoInt;
212 }
213
214 TargetInfo::RealType TargetInfo::getRealTypeByWidth(unsigned BitWidth) const {
215   if (getFloatWidth() == BitWidth)
216     return Float;
217   if (getDoubleWidth() == BitWidth)
218     return Double;
219
220   switch (BitWidth) {
221   case 96:
222     if (&getLongDoubleFormat() == &llvm::APFloat::x87DoubleExtended)
223       return LongDouble;
224     break;
225   case 128:
226     if (&getLongDoubleFormat() == &llvm::APFloat::PPCDoubleDouble ||
227         &getLongDoubleFormat() == &llvm::APFloat::IEEEquad)
228       return LongDouble;
229     if (hasFloat128Type())
230       return Float128;
231     break;
232   }
233
234   return NoFloat;
235 }
236
237 /// getTypeAlign - Return the alignment (in bits) of the specified integer type
238 /// enum. For example, SignedInt -> getIntAlign().
239 unsigned TargetInfo::getTypeAlign(IntType T) const {
240   switch (T) {
241   default: llvm_unreachable("not an integer!");
242   case SignedChar:
243   case UnsignedChar:     return getCharAlign();
244   case SignedShort:
245   case UnsignedShort:    return getShortAlign();
246   case SignedInt:
247   case UnsignedInt:      return getIntAlign();
248   case SignedLong:
249   case UnsignedLong:     return getLongAlign();
250   case SignedLongLong:
251   case UnsignedLongLong: return getLongLongAlign();
252   };
253 }
254
255 /// isTypeSigned - Return whether an integer types is signed. Returns true if
256 /// the type is signed; false otherwise.
257 bool TargetInfo::isTypeSigned(IntType T) {
258   switch (T) {
259   default: llvm_unreachable("not an integer!");
260   case SignedChar:
261   case SignedShort:
262   case SignedInt:
263   case SignedLong:
264   case SignedLongLong:
265     return true;
266   case UnsignedChar:
267   case UnsignedShort:
268   case UnsignedInt:
269   case UnsignedLong:
270   case UnsignedLongLong:
271     return false;
272   };
273 }
274
275 /// adjust - Set forced language options.
276 /// Apply changes to the target information with respect to certain
277 /// language options which change the target configuration.
278 void TargetInfo::adjust(const LangOptions &Opts) {
279   if (Opts.NoBitFieldTypeAlign)
280     UseBitFieldTypeAlignment = false;
281   if (Opts.ShortWChar)
282     WCharType = UnsignedShort;
283   if (Opts.AlignDouble) {
284     DoubleAlign = LongLongAlign = 64;
285     LongDoubleAlign = 64;
286   }
287
288   if (Opts.OpenCL) {
289     // OpenCL C requires specific widths for types, irrespective of
290     // what these normally are for the target.
291     // We also define long long and long double here, although the
292     // OpenCL standard only mentions these as "reserved".
293     IntWidth = IntAlign = 32;
294     LongWidth = LongAlign = 64;
295     LongLongWidth = LongLongAlign = 128;
296     HalfWidth = HalfAlign = 16;
297     FloatWidth = FloatAlign = 32;
298
299     // Embedded 32-bit targets (OpenCL EP) might have double C type
300     // defined as float. Let's not override this as it might lead
301     // to generating illegal code that uses 64bit doubles.
302     if (DoubleWidth != FloatWidth) {
303       DoubleWidth = DoubleAlign = 64;
304       DoubleFormat = &llvm::APFloat::IEEEdouble;
305     }
306     LongDoubleWidth = LongDoubleAlign = 128;
307
308     assert(PointerWidth == 32 || PointerWidth == 64);
309     bool Is32BitArch = PointerWidth == 32;
310     SizeType = Is32BitArch ? UnsignedInt : UnsignedLong;
311     PtrDiffType = Is32BitArch ? SignedInt : SignedLong;
312     IntPtrType = Is32BitArch ? SignedInt : SignedLong;
313
314     IntMaxType = SignedLongLong;
315     Int64Type = SignedLong;
316
317     HalfFormat = &llvm::APFloat::IEEEhalf;
318     FloatFormat = &llvm::APFloat::IEEEsingle;
319     LongDoubleFormat = &llvm::APFloat::IEEEquad;
320   }
321 }
322
323 bool TargetInfo::initFeatureMap(
324     llvm::StringMap<bool> &Features, DiagnosticsEngine &Diags, StringRef CPU,
325     const std::vector<std::string> &FeatureVec) const {
326   for (const auto &F : FeatureVec) {
327     StringRef Name = F;
328     // Apply the feature via the target.
329     bool Enabled = Name[0] == '+';
330     setFeatureEnabled(Features, Name.substr(1), Enabled);
331   }
332   return true;
333 }
334
335 //===----------------------------------------------------------------------===//
336
337
338 static StringRef removeGCCRegisterPrefix(StringRef Name) {
339   if (Name[0] == '%' || Name[0] == '#')
340     Name = Name.substr(1);
341
342   return Name;
343 }
344
345 /// isValidClobber - Returns whether the passed in string is
346 /// a valid clobber in an inline asm statement. This is used by
347 /// Sema.
348 bool TargetInfo::isValidClobber(StringRef Name) const {
349   return (isValidGCCRegisterName(Name) ||
350           Name == "memory" || Name == "cc");
351 }
352
353 /// isValidGCCRegisterName - Returns whether the passed in string
354 /// is a valid register name according to GCC. This is used by Sema for
355 /// inline asm statements.
356 bool TargetInfo::isValidGCCRegisterName(StringRef Name) const {
357   if (Name.empty())
358     return false;
359
360   // Get rid of any register prefix.
361   Name = removeGCCRegisterPrefix(Name);
362   if (Name.empty())
363     return false;
364
365   ArrayRef<const char *> Names = getGCCRegNames();
366
367   // If we have a number it maps to an entry in the register name array.
368   if (isDigit(Name[0])) {
369     unsigned n;
370     if (!Name.getAsInteger(0, n))
371       return n < Names.size();
372   }
373
374   // Check register names.
375   if (std::find(Names.begin(), Names.end(), Name) != Names.end())
376     return true;
377
378   // Check any additional names that we have.
379   for (const AddlRegName &ARN : getGCCAddlRegNames())
380     for (const char *AN : ARN.Names) {
381       if (!AN)
382         break;
383       // Make sure the register that the additional name is for is within
384       // the bounds of the register names from above.
385       if (AN == Name && ARN.RegNum < Names.size())
386         return true;
387     }
388
389   // Now check aliases.
390   for (const GCCRegAlias &GRA : getGCCRegAliases())
391     for (const char *A : GRA.Aliases) {
392       if (!A)
393         break;
394       if (A == Name)
395         return true;
396     }
397
398   return false;
399 }
400
401 StringRef
402 TargetInfo::getNormalizedGCCRegisterName(StringRef Name) const {
403   assert(isValidGCCRegisterName(Name) && "Invalid register passed in");
404
405   // Get rid of any register prefix.
406   Name = removeGCCRegisterPrefix(Name);
407
408   ArrayRef<const char *> Names = getGCCRegNames();
409
410   // First, check if we have a number.
411   if (isDigit(Name[0])) {
412     unsigned n;
413     if (!Name.getAsInteger(0, n)) {
414       assert(n < Names.size() && "Out of bounds register number!");
415       return Names[n];
416     }
417   }
418
419   // Check any additional names that we have.
420   for (const AddlRegName &ARN : getGCCAddlRegNames())
421     for (const char *AN : ARN.Names) {
422       if (!AN)
423         break;
424       // Make sure the register that the additional name is for is within
425       // the bounds of the register names from above.
426       if (AN == Name && ARN.RegNum < Names.size())
427         return Name;
428     }
429
430   // Now check aliases.
431   for (const GCCRegAlias &RA : getGCCRegAliases())
432     for (const char *A : RA.Aliases) {
433       if (!A)
434         break;
435       if (A == Name)
436         return RA.Register;
437     }
438
439   return Name;
440 }
441
442 bool TargetInfo::validateOutputConstraint(ConstraintInfo &Info) const {
443   const char *Name = Info.getConstraintStr().c_str();
444   // An output constraint must start with '=' or '+'
445   if (*Name != '=' && *Name != '+')
446     return false;
447
448   if (*Name == '+')
449     Info.setIsReadWrite();
450
451   Name++;
452   while (*Name) {
453     switch (*Name) {
454     default:
455       if (!validateAsmConstraint(Name, Info)) {
456         // FIXME: We temporarily return false
457         // so we can add more constraints as we hit it.
458         // Eventually, an unknown constraint should just be treated as 'g'.
459         return false;
460       }
461       break;
462     case '&': // early clobber.
463       Info.setEarlyClobber();
464       break;
465     case '%': // commutative.
466       // FIXME: Check that there is a another register after this one.
467       break;
468     case 'r': // general register.
469       Info.setAllowsRegister();
470       break;
471     case 'm': // memory operand.
472     case 'o': // offsetable memory operand.
473     case 'V': // non-offsetable memory operand.
474     case '<': // autodecrement memory operand.
475     case '>': // autoincrement memory operand.
476       Info.setAllowsMemory();
477       break;
478     case 'g': // general register, memory operand or immediate integer.
479     case 'X': // any operand.
480       Info.setAllowsRegister();
481       Info.setAllowsMemory();
482       break;
483     case ',': // multiple alternative constraint.  Pass it.
484       // Handle additional optional '=' or '+' modifiers.
485       if (Name[1] == '=' || Name[1] == '+')
486         Name++;
487       break;
488     case '#': // Ignore as constraint.
489       while (Name[1] && Name[1] != ',')
490         Name++;
491       break;
492     case '?': // Disparage slightly code.
493     case '!': // Disparage severely.
494     case '*': // Ignore for choosing register preferences.
495       break;  // Pass them.
496     }
497
498     Name++;
499   }
500
501   // Early clobber with a read-write constraint which doesn't permit registers
502   // is invalid.
503   if (Info.earlyClobber() && Info.isReadWrite() && !Info.allowsRegister())
504     return false;
505
506   // If a constraint allows neither memory nor register operands it contains
507   // only modifiers. Reject it.
508   return Info.allowsMemory() || Info.allowsRegister();
509 }
510
511 bool TargetInfo::resolveSymbolicName(const char *&Name,
512                                      ArrayRef<ConstraintInfo> OutputConstraints,
513                                      unsigned &Index) const {
514   assert(*Name == '[' && "Symbolic name did not start with '['");
515   Name++;
516   const char *Start = Name;
517   while (*Name && *Name != ']')
518     Name++;
519
520   if (!*Name) {
521     // Missing ']'
522     return false;
523   }
524
525   std::string SymbolicName(Start, Name - Start);
526
527   for (Index = 0; Index != OutputConstraints.size(); ++Index)
528     if (SymbolicName == OutputConstraints[Index].getName())
529       return true;
530
531   return false;
532 }
533
534 bool TargetInfo::validateInputConstraint(
535                               MutableArrayRef<ConstraintInfo> OutputConstraints,
536                               ConstraintInfo &Info) const {
537   const char *Name = Info.ConstraintStr.c_str();
538
539   if (!*Name)
540     return false;
541
542   while (*Name) {
543     switch (*Name) {
544     default:
545       // Check if we have a matching constraint
546       if (*Name >= '0' && *Name <= '9') {
547         const char *DigitStart = Name;
548         while (Name[1] >= '0' && Name[1] <= '9')
549           Name++;
550         const char *DigitEnd = Name;
551         unsigned i;
552         if (StringRef(DigitStart, DigitEnd - DigitStart + 1)
553                 .getAsInteger(10, i))
554           return false;
555
556         // Check if matching constraint is out of bounds.
557         if (i >= OutputConstraints.size()) return false;
558
559         // A number must refer to an output only operand.
560         if (OutputConstraints[i].isReadWrite())
561           return false;
562
563         // If the constraint is already tied, it must be tied to the
564         // same operand referenced to by the number.
565         if (Info.hasTiedOperand() && Info.getTiedOperand() != i)
566           return false;
567
568         // The constraint should have the same info as the respective
569         // output constraint.
570         Info.setTiedOperand(i, OutputConstraints[i]);
571       } else if (!validateAsmConstraint(Name, Info)) {
572         // FIXME: This error return is in place temporarily so we can
573         // add more constraints as we hit it.  Eventually, an unknown
574         // constraint should just be treated as 'g'.
575         return false;
576       }
577       break;
578     case '[': {
579       unsigned Index = 0;
580       if (!resolveSymbolicName(Name, OutputConstraints, Index))
581         return false;
582
583       // If the constraint is already tied, it must be tied to the
584       // same operand referenced to by the number.
585       if (Info.hasTiedOperand() && Info.getTiedOperand() != Index)
586         return false;
587
588       // A number must refer to an output only operand.
589       if (OutputConstraints[Index].isReadWrite())
590         return false;
591
592       Info.setTiedOperand(Index, OutputConstraints[Index]);
593       break;
594     }
595     case '%': // commutative
596       // FIXME: Fail if % is used with the last operand.
597       break;
598     case 'i': // immediate integer.
599     case 'n': // immediate integer with a known value.
600       break;
601     case 'I':  // Various constant constraints with target-specific meanings.
602     case 'J':
603     case 'K':
604     case 'L':
605     case 'M':
606     case 'N':
607     case 'O':
608     case 'P':
609       if (!validateAsmConstraint(Name, Info))
610         return false;
611       break;
612     case 'r': // general register.
613       Info.setAllowsRegister();
614       break;
615     case 'm': // memory operand.
616     case 'o': // offsettable memory operand.
617     case 'V': // non-offsettable memory operand.
618     case '<': // autodecrement memory operand.
619     case '>': // autoincrement memory operand.
620       Info.setAllowsMemory();
621       break;
622     case 'g': // general register, memory operand or immediate integer.
623     case 'X': // any operand.
624       Info.setAllowsRegister();
625       Info.setAllowsMemory();
626       break;
627     case 'E': // immediate floating point.
628     case 'F': // immediate floating point.
629     case 'p': // address operand.
630       break;
631     case ',': // multiple alternative constraint.  Ignore comma.
632       break;
633     case '#': // Ignore as constraint.
634       while (Name[1] && Name[1] != ',')
635         Name++;
636       break;
637     case '?': // Disparage slightly code.
638     case '!': // Disparage severely.
639     case '*': // Ignore for choosing register preferences.
640       break;  // Pass them.
641     }
642
643     Name++;
644   }
645
646   return true;
647 }