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/Diagnostic.h"
18 #include "clang/Basic/LangOptions.h"
19 #include "llvm/ADT/APFloat.h"
20 #include "llvm/ADT/STLExtras.h"
21 #include "llvm/Support/ErrorHandling.h"
22 #include "llvm/Support/TargetParser.h"
23 #include <cstdlib>
24 using namespace clang;
25 
26 static const LangASMap DefaultAddrSpaceMap = {0};
27 
28 // TargetInfo Constructor.
TargetInfo(const llvm::Triple & T)29 TargetInfo::TargetInfo(const llvm::Triple &T) : TargetOpts(), Triple(T) {
30   // Set defaults.  Defaults are set for a 32-bit RISC platform, like PPC or
31   // SPARC.  These should be overridden by concrete targets as needed.
32   BigEndian = !T.isLittleEndian();
33   TLSSupported = true;
34   VLASupported = true;
35   NoAsmVariants = false;
36   HasLegalHalfType = false;
37   HasFloat128 = false;
38   HasFloat16 = false;
39   PointerWidth = PointerAlign = 32;
40   BoolWidth = BoolAlign = 8;
41   IntWidth = IntAlign = 32;
42   LongWidth = LongAlign = 32;
43   LongLongWidth = LongLongAlign = 64;
44 
45   // Fixed point default bit widths
46   ShortAccumWidth = ShortAccumAlign = 16;
47   AccumWidth = AccumAlign = 32;
48   LongAccumWidth = LongAccumAlign = 64;
49   ShortFractWidth = ShortFractAlign = 8;
50   FractWidth = FractAlign = 16;
51   LongFractWidth = LongFractAlign = 32;
52 
53   // Fixed point default integral and fractional bit sizes
54   // We give the _Accum 1 fewer fractional bits than their corresponding _Fract
55   // types by default to have the same number of fractional bits between _Accum
56   // and _Fract types.
57   PaddingOnUnsignedFixedPoint = false;
58   ShortAccumScale = 7;
59   AccumScale = 15;
60   LongAccumScale = 31;
61 
62   SuitableAlign = 64;
63   DefaultAlignForAttributeAligned = 128;
64   MinGlobalAlign = 0;
65   // From the glibc documentation, on GNU systems, malloc guarantees 16-byte
66   // alignment on 64-bit systems and 8-byte alignment on 32-bit systems. See
67   // https://www.gnu.org/software/libc/manual/html_node/Malloc-Examples.html.
68   // This alignment guarantee also applies to Windows and Android.
69   if (T.isGNUEnvironment() || T.isWindowsMSVCEnvironment() || T.isAndroid())
70     NewAlign = Triple.isArch64Bit() ? 128 : Triple.isArch32Bit() ? 64 : 0;
71   else
72     NewAlign = 0; // Infer from basic type alignment.
73   HalfWidth = 16;
74   HalfAlign = 16;
75   FloatWidth = 32;
76   FloatAlign = 32;
77   DoubleWidth = 64;
78   DoubleAlign = 64;
79   LongDoubleWidth = 64;
80   LongDoubleAlign = 64;
81   Float128Align = 128;
82   LargeArrayMinWidth = 0;
83   LargeArrayAlign = 0;
84   MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 0;
85   MaxVectorAlign = 0;
86   MaxTLSAlign = 0;
87   SimdDefaultAlign = 0;
88   SizeType = UnsignedLong;
89   PtrDiffType = SignedLong;
90   IntMaxType = SignedLongLong;
91   IntPtrType = SignedLong;
92   WCharType = SignedInt;
93   WIntType = SignedInt;
94   Char16Type = UnsignedShort;
95   Char32Type = UnsignedInt;
96   Int64Type = SignedLongLong;
97   SigAtomicType = SignedInt;
98   ProcessIDType = SignedInt;
99   UseSignedCharForObjCBool = true;
100   UseBitFieldTypeAlignment = true;
101   UseZeroLengthBitfieldAlignment = false;
102   UseExplicitBitFieldAlignment = true;
103   ZeroLengthBitfieldBoundary = 0;
104   HalfFormat = &llvm::APFloat::IEEEhalf();
105   FloatFormat = &llvm::APFloat::IEEEsingle();
106   DoubleFormat = &llvm::APFloat::IEEEdouble();
107   LongDoubleFormat = &llvm::APFloat::IEEEdouble();
108   Float128Format = &llvm::APFloat::IEEEquad();
109   MCountName = "mcount";
110   RegParmMax = 0;
111   SSERegParmMax = 0;
112   HasAlignMac68kSupport = false;
113   HasBuiltinMSVaList = false;
114   IsRenderScriptTarget = false;
115 
116   // Default to no types using fpret.
117   RealTypeUsesObjCFPRet = 0;
118 
119   // Default to not using fp2ret for __Complex long double
120   ComplexLongDoubleUsesFP2Ret = false;
121 
122   // Set the C++ ABI based on the triple.
123   TheCXXABI.set(Triple.isKnownWindowsMSVCEnvironment()
124                     ? TargetCXXABI::Microsoft
125                     : TargetCXXABI::GenericItanium);
126 
127   // Default to an empty address space map.
128   AddrSpaceMap = &DefaultAddrSpaceMap;
129   UseAddrSpaceMapMangling = false;
130 
131   // Default to an unknown platform name.
132   PlatformName = "unknown";
133   PlatformMinVersion = VersionTuple();
134 }
135 
136 // Out of line virtual dtor for TargetInfo.
~TargetInfo()137 TargetInfo::~TargetInfo() {}
138 
139 bool
checkCFProtectionBranchSupported(DiagnosticsEngine & Diags) const140 TargetInfo::checkCFProtectionBranchSupported(DiagnosticsEngine &Diags) const {
141   Diags.Report(diag::err_opt_not_valid_on_target) << "cf-protection=branch";
142   return false;
143 }
144 
145 bool
checkCFProtectionReturnSupported(DiagnosticsEngine & Diags) const146 TargetInfo::checkCFProtectionReturnSupported(DiagnosticsEngine &Diags) const {
147   Diags.Report(diag::err_opt_not_valid_on_target) << "cf-protection=return";
148   return false;
149 }
150 
151 /// getTypeName - Return the user string for the specified integer type enum.
152 /// For example, SignedShort -> "short".
getTypeName(IntType T)153 const char *TargetInfo::getTypeName(IntType T) {
154   switch (T) {
155   default: llvm_unreachable("not an integer!");
156   case SignedChar:       return "signed char";
157   case UnsignedChar:     return "unsigned char";
158   case SignedShort:      return "short";
159   case UnsignedShort:    return "unsigned short";
160   case SignedInt:        return "int";
161   case UnsignedInt:      return "unsigned int";
162   case SignedLong:       return "long int";
163   case UnsignedLong:     return "long unsigned int";
164   case SignedLongLong:   return "long long int";
165   case UnsignedLongLong: return "long long unsigned int";
166   }
167 }
168 
169 /// getTypeConstantSuffix - Return the constant suffix for the specified
170 /// integer type enum. For example, SignedLong -> "L".
getTypeConstantSuffix(IntType T) const171 const char *TargetInfo::getTypeConstantSuffix(IntType T) const {
172   switch (T) {
173   default: llvm_unreachable("not an integer!");
174   case SignedChar:
175   case SignedShort:
176   case SignedInt:        return "";
177   case SignedLong:       return "L";
178   case SignedLongLong:   return "LL";
179   case UnsignedChar:
180     if (getCharWidth() < getIntWidth())
181       return "";
182     LLVM_FALLTHROUGH;
183   case UnsignedShort:
184     if (getShortWidth() < getIntWidth())
185       return "";
186     LLVM_FALLTHROUGH;
187   case UnsignedInt:      return "U";
188   case UnsignedLong:     return "UL";
189   case UnsignedLongLong: return "ULL";
190   }
191 }
192 
193 /// getTypeFormatModifier - Return the printf format modifier for the
194 /// specified integer type enum. For example, SignedLong -> "l".
195 
getTypeFormatModifier(IntType T)196 const char *TargetInfo::getTypeFormatModifier(IntType T) {
197   switch (T) {
198   default: llvm_unreachable("not an integer!");
199   case SignedChar:
200   case UnsignedChar:     return "hh";
201   case SignedShort:
202   case UnsignedShort:    return "h";
203   case SignedInt:
204   case UnsignedInt:      return "";
205   case SignedLong:
206   case UnsignedLong:     return "l";
207   case SignedLongLong:
208   case UnsignedLongLong: return "ll";
209   }
210 }
211 
212 /// getTypeWidth - Return the width (in bits) of the specified integer type
213 /// enum. For example, SignedInt -> getIntWidth().
getTypeWidth(IntType T) const214 unsigned TargetInfo::getTypeWidth(IntType T) const {
215   switch (T) {
216   default: llvm_unreachable("not an integer!");
217   case SignedChar:
218   case UnsignedChar:     return getCharWidth();
219   case SignedShort:
220   case UnsignedShort:    return getShortWidth();
221   case SignedInt:
222   case UnsignedInt:      return getIntWidth();
223   case SignedLong:
224   case UnsignedLong:     return getLongWidth();
225   case SignedLongLong:
226   case UnsignedLongLong: return getLongLongWidth();
227   };
228 }
229 
getIntTypeByWidth(unsigned BitWidth,bool IsSigned) const230 TargetInfo::IntType TargetInfo::getIntTypeByWidth(
231     unsigned BitWidth, bool IsSigned) const {
232   if (getCharWidth() == BitWidth)
233     return IsSigned ? SignedChar : UnsignedChar;
234   if (getShortWidth() == BitWidth)
235     return IsSigned ? SignedShort : UnsignedShort;
236   if (getIntWidth() == BitWidth)
237     return IsSigned ? SignedInt : UnsignedInt;
238   if (getLongWidth() == BitWidth)
239     return IsSigned ? SignedLong : UnsignedLong;
240   if (getLongLongWidth() == BitWidth)
241     return IsSigned ? SignedLongLong : UnsignedLongLong;
242   return NoInt;
243 }
244 
getLeastIntTypeByWidth(unsigned BitWidth,bool IsSigned) const245 TargetInfo::IntType TargetInfo::getLeastIntTypeByWidth(unsigned BitWidth,
246                                                        bool IsSigned) const {
247   if (getCharWidth() >= BitWidth)
248     return IsSigned ? SignedChar : UnsignedChar;
249   if (getShortWidth() >= BitWidth)
250     return IsSigned ? SignedShort : UnsignedShort;
251   if (getIntWidth() >= BitWidth)
252     return IsSigned ? SignedInt : UnsignedInt;
253   if (getLongWidth() >= BitWidth)
254     return IsSigned ? SignedLong : UnsignedLong;
255   if (getLongLongWidth() >= BitWidth)
256     return IsSigned ? SignedLongLong : UnsignedLongLong;
257   return NoInt;
258 }
259 
getRealTypeByWidth(unsigned BitWidth) const260 TargetInfo::RealType TargetInfo::getRealTypeByWidth(unsigned BitWidth) const {
261   if (getFloatWidth() == BitWidth)
262     return Float;
263   if (getDoubleWidth() == BitWidth)
264     return Double;
265 
266   switch (BitWidth) {
267   case 96:
268     if (&getLongDoubleFormat() == &llvm::APFloat::x87DoubleExtended())
269       return LongDouble;
270     break;
271   case 128:
272     if (&getLongDoubleFormat() == &llvm::APFloat::PPCDoubleDouble() ||
273         &getLongDoubleFormat() == &llvm::APFloat::IEEEquad())
274       return LongDouble;
275     if (hasFloat128Type())
276       return Float128;
277     break;
278   }
279 
280   return NoFloat;
281 }
282 
283 /// getTypeAlign - Return the alignment (in bits) of the specified integer type
284 /// enum. For example, SignedInt -> getIntAlign().
getTypeAlign(IntType T) const285 unsigned TargetInfo::getTypeAlign(IntType T) const {
286   switch (T) {
287   default: llvm_unreachable("not an integer!");
288   case SignedChar:
289   case UnsignedChar:     return getCharAlign();
290   case SignedShort:
291   case UnsignedShort:    return getShortAlign();
292   case SignedInt:
293   case UnsignedInt:      return getIntAlign();
294   case SignedLong:
295   case UnsignedLong:     return getLongAlign();
296   case SignedLongLong:
297   case UnsignedLongLong: return getLongLongAlign();
298   };
299 }
300 
301 /// isTypeSigned - Return whether an integer types is signed. Returns true if
302 /// the type is signed; false otherwise.
isTypeSigned(IntType T)303 bool TargetInfo::isTypeSigned(IntType T) {
304   switch (T) {
305   default: llvm_unreachable("not an integer!");
306   case SignedChar:
307   case SignedShort:
308   case SignedInt:
309   case SignedLong:
310   case SignedLongLong:
311     return true;
312   case UnsignedChar:
313   case UnsignedShort:
314   case UnsignedInt:
315   case UnsignedLong:
316   case UnsignedLongLong:
317     return false;
318   };
319 }
320 
321 /// adjust - Set forced language options.
322 /// Apply changes to the target information with respect to certain
323 /// language options which change the target configuration and adjust
324 /// the language based on the target options where applicable.
adjust(LangOptions & Opts)325 void TargetInfo::adjust(LangOptions &Opts) {
326   if (Opts.NoBitFieldTypeAlign)
327     UseBitFieldTypeAlignment = false;
328 
329   switch (Opts.WCharSize) {
330   default: llvm_unreachable("invalid wchar_t width");
331   case 0: break;
332   case 1: WCharType = Opts.WCharIsSigned ? SignedChar : UnsignedChar; break;
333   case 2: WCharType = Opts.WCharIsSigned ? SignedShort : UnsignedShort; break;
334   case 4: WCharType = Opts.WCharIsSigned ? SignedInt : UnsignedInt; break;
335   }
336 
337   if (Opts.AlignDouble) {
338     DoubleAlign = LongLongAlign = 64;
339     LongDoubleAlign = 64;
340   }
341 
342   if (Opts.OpenCL) {
343     // OpenCL C requires specific widths for types, irrespective of
344     // what these normally are for the target.
345     // We also define long long and long double here, although the
346     // OpenCL standard only mentions these as "reserved".
347     IntWidth = IntAlign = 32;
348     LongWidth = LongAlign = 64;
349     LongLongWidth = LongLongAlign = 128;
350     HalfWidth = HalfAlign = 16;
351     FloatWidth = FloatAlign = 32;
352 
353     // Embedded 32-bit targets (OpenCL EP) might have double C type
354     // defined as float. Let's not override this as it might lead
355     // to generating illegal code that uses 64bit doubles.
356     if (DoubleWidth != FloatWidth) {
357       DoubleWidth = DoubleAlign = 64;
358       DoubleFormat = &llvm::APFloat::IEEEdouble();
359     }
360     LongDoubleWidth = LongDoubleAlign = 128;
361 
362     unsigned MaxPointerWidth = getMaxPointerWidth();
363     assert(MaxPointerWidth == 32 || MaxPointerWidth == 64);
364     bool Is32BitArch = MaxPointerWidth == 32;
365     SizeType = Is32BitArch ? UnsignedInt : UnsignedLong;
366     PtrDiffType = Is32BitArch ? SignedInt : SignedLong;
367     IntPtrType = Is32BitArch ? SignedInt : SignedLong;
368 
369     IntMaxType = SignedLongLong;
370     Int64Type = SignedLong;
371 
372     HalfFormat = &llvm::APFloat::IEEEhalf();
373     FloatFormat = &llvm::APFloat::IEEEsingle();
374     LongDoubleFormat = &llvm::APFloat::IEEEquad();
375   }
376 
377   if (Opts.NewAlignOverride)
378     NewAlign = Opts.NewAlignOverride * getCharWidth();
379 
380   // Each unsigned fixed point type has the same number of fractional bits as
381   // its corresponding signed type.
382   PaddingOnUnsignedFixedPoint |= Opts.PaddingOnUnsignedFixedPoint;
383   CheckFixedPointBits();
384 }
385 
initFeatureMap(llvm::StringMap<bool> & Features,DiagnosticsEngine & Diags,StringRef CPU,const std::vector<std::string> & FeatureVec) const386 bool TargetInfo::initFeatureMap(
387     llvm::StringMap<bool> &Features, DiagnosticsEngine &Diags, StringRef CPU,
388     const std::vector<std::string> &FeatureVec) const {
389   for (const auto &F : FeatureVec) {
390     StringRef Name = F;
391     // Apply the feature via the target.
392     bool Enabled = Name[0] == '+';
393     setFeatureEnabled(Features, Name.substr(1), Enabled);
394   }
395   return true;
396 }
397 
398 TargetInfo::CallingConvKind
getCallingConvKind(bool ClangABICompat4) const399 TargetInfo::getCallingConvKind(bool ClangABICompat4) const {
400   if (getCXXABI() != TargetCXXABI::Microsoft &&
401       (ClangABICompat4 || getTriple().getOS() == llvm::Triple::PS4))
402     return CCK_ClangABI4OrPS4;
403   return CCK_Default;
404 }
405 
getOpenCLTypeAddrSpace(OpenCLTypeKind TK) const406 LangAS TargetInfo::getOpenCLTypeAddrSpace(OpenCLTypeKind TK) const {
407   switch (TK) {
408   case OCLTK_Image:
409   case OCLTK_Pipe:
410     return LangAS::opencl_global;
411 
412   case OCLTK_Sampler:
413     return LangAS::opencl_constant;
414 
415   default:
416     return LangAS::Default;
417   }
418 }
419 
420 //===----------------------------------------------------------------------===//
421 
422 
removeGCCRegisterPrefix(StringRef Name)423 static StringRef removeGCCRegisterPrefix(StringRef Name) {
424   if (Name[0] == '%' || Name[0] == '#')
425     Name = Name.substr(1);
426 
427   return Name;
428 }
429 
430 /// isValidClobber - Returns whether the passed in string is
431 /// a valid clobber in an inline asm statement. This is used by
432 /// Sema.
isValidClobber(StringRef Name) const433 bool TargetInfo::isValidClobber(StringRef Name) const {
434   return (isValidGCCRegisterName(Name) ||
435           Name == "memory" || Name == "cc");
436 }
437 
438 /// isValidGCCRegisterName - Returns whether the passed in string
439 /// is a valid register name according to GCC. This is used by Sema for
440 /// inline asm statements.
isValidGCCRegisterName(StringRef Name) const441 bool TargetInfo::isValidGCCRegisterName(StringRef Name) const {
442   if (Name.empty())
443     return false;
444 
445   // Get rid of any register prefix.
446   Name = removeGCCRegisterPrefix(Name);
447   if (Name.empty())
448     return false;
449 
450   ArrayRef<const char *> Names = getGCCRegNames();
451 
452   // If we have a number it maps to an entry in the register name array.
453   if (isDigit(Name[0])) {
454     unsigned n;
455     if (!Name.getAsInteger(0, n))
456       return n < Names.size();
457   }
458 
459   // Check register names.
460   if (std::find(Names.begin(), Names.end(), Name) != Names.end())
461     return true;
462 
463   // Check any additional names that we have.
464   for (const AddlRegName &ARN : getGCCAddlRegNames())
465     for (const char *AN : ARN.Names) {
466       if (!AN)
467         break;
468       // Make sure the register that the additional name is for is within
469       // the bounds of the register names from above.
470       if (AN == Name && ARN.RegNum < Names.size())
471         return true;
472     }
473 
474   // Now check aliases.
475   for (const GCCRegAlias &GRA : getGCCRegAliases())
476     for (const char *A : GRA.Aliases) {
477       if (!A)
478         break;
479       if (A == Name)
480         return true;
481     }
482 
483   return false;
484 }
485 
getNormalizedGCCRegisterName(StringRef Name,bool ReturnCanonical) const486 StringRef TargetInfo::getNormalizedGCCRegisterName(StringRef Name,
487                                                    bool ReturnCanonical) const {
488   assert(isValidGCCRegisterName(Name) && "Invalid register passed in");
489 
490   // Get rid of any register prefix.
491   Name = removeGCCRegisterPrefix(Name);
492 
493   ArrayRef<const char *> Names = getGCCRegNames();
494 
495   // First, check if we have a number.
496   if (isDigit(Name[0])) {
497     unsigned n;
498     if (!Name.getAsInteger(0, n)) {
499       assert(n < Names.size() && "Out of bounds register number!");
500       return Names[n];
501     }
502   }
503 
504   // Check any additional names that we have.
505   for (const AddlRegName &ARN : getGCCAddlRegNames())
506     for (const char *AN : ARN.Names) {
507       if (!AN)
508         break;
509       // Make sure the register that the additional name is for is within
510       // the bounds of the register names from above.
511       if (AN == Name && ARN.RegNum < Names.size())
512         return ReturnCanonical ? Names[ARN.RegNum] : Name;
513     }
514 
515   // Now check aliases.
516   for (const GCCRegAlias &RA : getGCCRegAliases())
517     for (const char *A : RA.Aliases) {
518       if (!A)
519         break;
520       if (A == Name)
521         return RA.Register;
522     }
523 
524   return Name;
525 }
526 
validateOutputConstraint(ConstraintInfo & Info) const527 bool TargetInfo::validateOutputConstraint(ConstraintInfo &Info) const {
528   const char *Name = Info.getConstraintStr().c_str();
529   // An output constraint must start with '=' or '+'
530   if (*Name != '=' && *Name != '+')
531     return false;
532 
533   if (*Name == '+')
534     Info.setIsReadWrite();
535 
536   Name++;
537   while (*Name) {
538     switch (*Name) {
539     default:
540       if (!validateAsmConstraint(Name, Info)) {
541         // FIXME: We temporarily return false
542         // so we can add more constraints as we hit it.
543         // Eventually, an unknown constraint should just be treated as 'g'.
544         return false;
545       }
546       break;
547     case '&': // early clobber.
548       Info.setEarlyClobber();
549       break;
550     case '%': // commutative.
551       // FIXME: Check that there is a another register after this one.
552       break;
553     case 'r': // general register.
554       Info.setAllowsRegister();
555       break;
556     case 'm': // memory operand.
557     case 'o': // offsetable memory operand.
558     case 'V': // non-offsetable memory operand.
559     case '<': // autodecrement memory operand.
560     case '>': // autoincrement memory operand.
561       Info.setAllowsMemory();
562       break;
563     case 'g': // general register, memory operand or immediate integer.
564     case 'X': // any operand.
565       Info.setAllowsRegister();
566       Info.setAllowsMemory();
567       break;
568     case ',': // multiple alternative constraint.  Pass it.
569       // Handle additional optional '=' or '+' modifiers.
570       if (Name[1] == '=' || Name[1] == '+')
571         Name++;
572       break;
573     case '#': // Ignore as constraint.
574       while (Name[1] && Name[1] != ',')
575         Name++;
576       break;
577     case '?': // Disparage slightly code.
578     case '!': // Disparage severely.
579     case '*': // Ignore for choosing register preferences.
580     case 'i': // Ignore i,n,E,F as output constraints (match from the other
581               // chars)
582     case 'n':
583     case 'E':
584     case 'F':
585       break;  // Pass them.
586     }
587 
588     Name++;
589   }
590 
591   // Early clobber with a read-write constraint which doesn't permit registers
592   // is invalid.
593   if (Info.earlyClobber() && Info.isReadWrite() && !Info.allowsRegister())
594     return false;
595 
596   // If a constraint allows neither memory nor register operands it contains
597   // only modifiers. Reject it.
598   return Info.allowsMemory() || Info.allowsRegister();
599 }
600 
resolveSymbolicName(const char * & Name,ArrayRef<ConstraintInfo> OutputConstraints,unsigned & Index) const601 bool TargetInfo::resolveSymbolicName(const char *&Name,
602                                      ArrayRef<ConstraintInfo> OutputConstraints,
603                                      unsigned &Index) const {
604   assert(*Name == '[' && "Symbolic name did not start with '['");
605   Name++;
606   const char *Start = Name;
607   while (*Name && *Name != ']')
608     Name++;
609 
610   if (!*Name) {
611     // Missing ']'
612     return false;
613   }
614 
615   std::string SymbolicName(Start, Name - Start);
616 
617   for (Index = 0; Index != OutputConstraints.size(); ++Index)
618     if (SymbolicName == OutputConstraints[Index].getName())
619       return true;
620 
621   return false;
622 }
623 
validateInputConstraint(MutableArrayRef<ConstraintInfo> OutputConstraints,ConstraintInfo & Info) const624 bool TargetInfo::validateInputConstraint(
625                               MutableArrayRef<ConstraintInfo> OutputConstraints,
626                               ConstraintInfo &Info) const {
627   const char *Name = Info.ConstraintStr.c_str();
628 
629   if (!*Name)
630     return false;
631 
632   while (*Name) {
633     switch (*Name) {
634     default:
635       // Check if we have a matching constraint
636       if (*Name >= '0' && *Name <= '9') {
637         const char *DigitStart = Name;
638         while (Name[1] >= '0' && Name[1] <= '9')
639           Name++;
640         const char *DigitEnd = Name;
641         unsigned i;
642         if (StringRef(DigitStart, DigitEnd - DigitStart + 1)
643                 .getAsInteger(10, i))
644           return false;
645 
646         // Check if matching constraint is out of bounds.
647         if (i >= OutputConstraints.size()) return false;
648 
649         // A number must refer to an output only operand.
650         if (OutputConstraints[i].isReadWrite())
651           return false;
652 
653         // If the constraint is already tied, it must be tied to the
654         // same operand referenced to by the number.
655         if (Info.hasTiedOperand() && Info.getTiedOperand() != i)
656           return false;
657 
658         // The constraint should have the same info as the respective
659         // output constraint.
660         Info.setTiedOperand(i, OutputConstraints[i]);
661       } else if (!validateAsmConstraint(Name, Info)) {
662         // FIXME: This error return is in place temporarily so we can
663         // add more constraints as we hit it.  Eventually, an unknown
664         // constraint should just be treated as 'g'.
665         return false;
666       }
667       break;
668     case '[': {
669       unsigned Index = 0;
670       if (!resolveSymbolicName(Name, OutputConstraints, Index))
671         return false;
672 
673       // If the constraint is already tied, it must be tied to the
674       // same operand referenced to by the number.
675       if (Info.hasTiedOperand() && Info.getTiedOperand() != Index)
676         return false;
677 
678       // A number must refer to an output only operand.
679       if (OutputConstraints[Index].isReadWrite())
680         return false;
681 
682       Info.setTiedOperand(Index, OutputConstraints[Index]);
683       break;
684     }
685     case '%': // commutative
686       // FIXME: Fail if % is used with the last operand.
687       break;
688     case 'i': // immediate integer.
689       break;
690     case 'n': // immediate integer with a known value.
691       Info.setRequiresImmediate();
692       break;
693     case 'I':  // Various constant constraints with target-specific meanings.
694     case 'J':
695     case 'K':
696     case 'L':
697     case 'M':
698     case 'N':
699     case 'O':
700     case 'P':
701       if (!validateAsmConstraint(Name, Info))
702         return false;
703       break;
704     case 'r': // general register.
705       Info.setAllowsRegister();
706       break;
707     case 'm': // memory operand.
708     case 'o': // offsettable memory operand.
709     case 'V': // non-offsettable memory operand.
710     case '<': // autodecrement memory operand.
711     case '>': // autoincrement memory operand.
712       Info.setAllowsMemory();
713       break;
714     case 'g': // general register, memory operand or immediate integer.
715     case 'X': // any operand.
716       Info.setAllowsRegister();
717       Info.setAllowsMemory();
718       break;
719     case 'E': // immediate floating point.
720     case 'F': // immediate floating point.
721     case 'p': // address operand.
722       break;
723     case ',': // multiple alternative constraint.  Ignore comma.
724       break;
725     case '#': // Ignore as constraint.
726       while (Name[1] && Name[1] != ',')
727         Name++;
728       break;
729     case '?': // Disparage slightly code.
730     case '!': // Disparage severely.
731     case '*': // Ignore for choosing register preferences.
732       break;  // Pass them.
733     }
734 
735     Name++;
736   }
737 
738   return true;
739 }
740 
CheckFixedPointBits() const741 void TargetInfo::CheckFixedPointBits() const {
742   // Check that the number of fractional and integral bits (and maybe sign) can
743   // fit into the bits given for a fixed point type.
744   assert(ShortAccumScale + getShortAccumIBits() + 1 <= ShortAccumWidth);
745   assert(AccumScale + getAccumIBits() + 1 <= AccumWidth);
746   assert(LongAccumScale + getLongAccumIBits() + 1 <= LongAccumWidth);
747   assert(getUnsignedShortAccumScale() + getUnsignedShortAccumIBits() <=
748          ShortAccumWidth);
749   assert(getUnsignedAccumScale() + getUnsignedAccumIBits() <= AccumWidth);
750   assert(getUnsignedLongAccumScale() + getUnsignedLongAccumIBits() <=
751          LongAccumWidth);
752 
753   assert(getShortFractScale() + 1 <= ShortFractWidth);
754   assert(getFractScale() + 1 <= FractWidth);
755   assert(getLongFractScale() + 1 <= LongFractWidth);
756   assert(getUnsignedShortFractScale() <= ShortFractWidth);
757   assert(getUnsignedFractScale() <= FractWidth);
758   assert(getUnsignedLongFractScale() <= LongFractWidth);
759 
760   // Each unsigned fract type has either the same number of fractional bits
761   // as, or one more fractional bit than, its corresponding signed fract type.
762   assert(getShortFractScale() == getUnsignedShortFractScale() ||
763          getShortFractScale() == getUnsignedShortFractScale() - 1);
764   assert(getFractScale() == getUnsignedFractScale() ||
765          getFractScale() == getUnsignedFractScale() - 1);
766   assert(getLongFractScale() == getUnsignedLongFractScale() ||
767          getLongFractScale() == getUnsignedLongFractScale() - 1);
768 
769   // When arranged in order of increasing rank (see 6.3.1.3a), the number of
770   // fractional bits is nondecreasing for each of the following sets of
771   // fixed-point types:
772   // - signed fract types
773   // - unsigned fract types
774   // - signed accum types
775   // - unsigned accum types.
776   assert(getLongFractScale() >= getFractScale() &&
777          getFractScale() >= getShortFractScale());
778   assert(getUnsignedLongFractScale() >= getUnsignedFractScale() &&
779          getUnsignedFractScale() >= getUnsignedShortFractScale());
780   assert(LongAccumScale >= AccumScale && AccumScale >= ShortAccumScale);
781   assert(getUnsignedLongAccumScale() >= getUnsignedAccumScale() &&
782          getUnsignedAccumScale() >= getUnsignedShortAccumScale());
783 
784   // When arranged in order of increasing rank (see 6.3.1.3a), the number of
785   // integral bits is nondecreasing for each of the following sets of
786   // fixed-point types:
787   // - signed accum types
788   // - unsigned accum types
789   assert(getLongAccumIBits() >= getAccumIBits() &&
790          getAccumIBits() >= getShortAccumIBits());
791   assert(getUnsignedLongAccumIBits() >= getUnsignedAccumIBits() &&
792          getUnsignedAccumIBits() >= getUnsignedShortAccumIBits());
793 
794   // Each signed accum type has at least as many integral bits as its
795   // corresponding unsigned accum type.
796   assert(getShortAccumIBits() >= getUnsignedShortAccumIBits());
797   assert(getAccumIBits() >= getUnsignedAccumIBits());
798   assert(getLongAccumIBits() >= getUnsignedLongAccumIBits());
799 }
800