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