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