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