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