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