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