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