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