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