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