1 //===--- TargetInfo.cpp - Information about Target machine ----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the TargetInfo and TargetInfoImpl interfaces.
12 //===----------------------------------------------------------------------===//
14 #include "clang/Basic/TargetInfo.h"
15 #include "clang/Basic/AddressSpaces.h"
16 #include "clang/Basic/CharInfo.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"
22 using namespace clang;
24 static const LangAS::Map DefaultAddrSpaceMap = { 0 };
26 // TargetInfo Constructor.
27 TargetInfo::TargetInfo(const llvm::Triple &T) : TargetOpts(), Triple(T) {
28 // Set defaults. Defaults are set for a 32-bit RISC platform, like PPC or
29 // SPARC. These should be overridden by concrete targets as needed.
30 BigEndian = !T.isLittleEndian();
32 NoAsmVariants = false;
34 PointerWidth = PointerAlign = 32;
35 BoolWidth = BoolAlign = 8;
36 IntWidth = IntAlign = 32;
37 LongWidth = LongAlign = 32;
38 LongLongWidth = LongLongAlign = 64;
40 DefaultAlignForAttributeAligned = 128;
42 // From the glibc documentation, on GNU systems, malloc guarantees 16-byte
43 // alignment on 64-bit systems and 8-byte alignment on 32-bit systems. See
44 // https://www.gnu.org/software/libc/manual/html_node/Malloc-Examples.html
45 if (T.isGNUEnvironment())
46 NewAlign = Triple.isArch64Bit() ? 128 : Triple.isArch32Bit() ? 64 : 0;
48 NewAlign = 0; // Infer from basic type alignment.
58 LargeArrayMinWidth = 0;
60 MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 0;
64 SizeType = UnsignedLong;
65 PtrDiffType = SignedLong;
66 IntMaxType = SignedLongLong;
67 IntPtrType = SignedLong;
68 WCharType = SignedInt;
70 Char16Type = UnsignedShort;
71 Char32Type = UnsignedInt;
72 Int64Type = SignedLongLong;
73 SigAtomicType = SignedInt;
74 ProcessIDType = SignedInt;
75 UseSignedCharForObjCBool = true;
76 UseBitFieldTypeAlignment = true;
77 UseZeroLengthBitfieldAlignment = false;
78 UseExplicitBitFieldAlignment = true;
79 ZeroLengthBitfieldBoundary = 0;
80 HalfFormat = &llvm::APFloat::IEEEhalf();
81 FloatFormat = &llvm::APFloat::IEEEsingle();
82 DoubleFormat = &llvm::APFloat::IEEEdouble();
83 LongDoubleFormat = &llvm::APFloat::IEEEdouble();
84 Float128Format = &llvm::APFloat::IEEEquad();
85 MCountName = "mcount";
88 HasAlignMac68kSupport = false;
89 HasBuiltinMSVaList = false;
90 IsRenderScriptTarget = false;
92 // Default to no types using fpret.
93 RealTypeUsesObjCFPRet = 0;
95 // Default to not using fp2ret for __Complex long double
96 ComplexLongDoubleUsesFP2Ret = false;
98 // Set the C++ ABI based on the triple.
99 TheCXXABI.set(Triple.isKnownWindowsMSVCEnvironment()
100 ? TargetCXXABI::Microsoft
101 : TargetCXXABI::GenericItanium);
103 // Default to an empty address space map.
104 AddrSpaceMap = &DefaultAddrSpaceMap;
105 UseAddrSpaceMapMangling = false;
107 // Default to an unknown platform name.
108 PlatformName = "unknown";
109 PlatformMinVersion = VersionTuple();
112 // Out of line virtual dtor for TargetInfo.
113 TargetInfo::~TargetInfo() {}
115 /// getTypeName - Return the user string for the specified integer type enum.
116 /// For example, SignedShort -> "short".
117 const char *TargetInfo::getTypeName(IntType T) {
119 default: llvm_unreachable("not an integer!");
120 case SignedChar: return "signed char";
121 case UnsignedChar: return "unsigned char";
122 case SignedShort: return "short";
123 case UnsignedShort: return "unsigned short";
124 case SignedInt: return "int";
125 case UnsignedInt: return "unsigned int";
126 case SignedLong: return "long int";
127 case UnsignedLong: return "long unsigned int";
128 case SignedLongLong: return "long long int";
129 case UnsignedLongLong: return "long long unsigned int";
133 /// getTypeConstantSuffix - Return the constant suffix for the specified
134 /// integer type enum. For example, SignedLong -> "L".
135 const char *TargetInfo::getTypeConstantSuffix(IntType T) const {
137 default: llvm_unreachable("not an integer!");
140 case SignedInt: return "";
141 case SignedLong: return "L";
142 case SignedLongLong: return "LL";
144 if (getCharWidth() < getIntWidth())
147 if (getShortWidth() < getIntWidth())
149 case UnsignedInt: return "U";
150 case UnsignedLong: return "UL";
151 case UnsignedLongLong: return "ULL";
155 /// getTypeFormatModifier - Return the printf format modifier for the
156 /// specified integer type enum. For example, SignedLong -> "l".
158 const char *TargetInfo::getTypeFormatModifier(IntType T) {
160 default: llvm_unreachable("not an integer!");
162 case UnsignedChar: return "hh";
164 case UnsignedShort: return "h";
166 case UnsignedInt: return "";
168 case UnsignedLong: return "l";
170 case UnsignedLongLong: return "ll";
174 /// getTypeWidth - Return the width (in bits) of the specified integer type
175 /// enum. For example, SignedInt -> getIntWidth().
176 unsigned TargetInfo::getTypeWidth(IntType T) const {
178 default: llvm_unreachable("not an integer!");
180 case UnsignedChar: return getCharWidth();
182 case UnsignedShort: return getShortWidth();
184 case UnsignedInt: return getIntWidth();
186 case UnsignedLong: return getLongWidth();
188 case UnsignedLongLong: return getLongLongWidth();
192 TargetInfo::IntType TargetInfo::getIntTypeByWidth(
193 unsigned BitWidth, bool IsSigned) const {
194 if (getCharWidth() == BitWidth)
195 return IsSigned ? SignedChar : UnsignedChar;
196 if (getShortWidth() == BitWidth)
197 return IsSigned ? SignedShort : UnsignedShort;
198 if (getIntWidth() == BitWidth)
199 return IsSigned ? SignedInt : UnsignedInt;
200 if (getLongWidth() == BitWidth)
201 return IsSigned ? SignedLong : UnsignedLong;
202 if (getLongLongWidth() == BitWidth)
203 return IsSigned ? SignedLongLong : UnsignedLongLong;
207 TargetInfo::IntType TargetInfo::getLeastIntTypeByWidth(unsigned BitWidth,
208 bool IsSigned) const {
209 if (getCharWidth() >= BitWidth)
210 return IsSigned ? SignedChar : UnsignedChar;
211 if (getShortWidth() >= BitWidth)
212 return IsSigned ? SignedShort : UnsignedShort;
213 if (getIntWidth() >= BitWidth)
214 return IsSigned ? SignedInt : UnsignedInt;
215 if (getLongWidth() >= BitWidth)
216 return IsSigned ? SignedLong : UnsignedLong;
217 if (getLongLongWidth() >= BitWidth)
218 return IsSigned ? SignedLongLong : UnsignedLongLong;
222 TargetInfo::RealType TargetInfo::getRealTypeByWidth(unsigned BitWidth) const {
223 if (getFloatWidth() == BitWidth)
225 if (getDoubleWidth() == BitWidth)
230 if (&getLongDoubleFormat() == &llvm::APFloat::x87DoubleExtended())
234 if (&getLongDoubleFormat() == &llvm::APFloat::PPCDoubleDouble() ||
235 &getLongDoubleFormat() == &llvm::APFloat::IEEEquad())
237 if (hasFloat128Type())
245 /// getTypeAlign - Return the alignment (in bits) of the specified integer type
246 /// enum. For example, SignedInt -> getIntAlign().
247 unsigned TargetInfo::getTypeAlign(IntType T) const {
249 default: llvm_unreachable("not an integer!");
251 case UnsignedChar: return getCharAlign();
253 case UnsignedShort: return getShortAlign();
255 case UnsignedInt: return getIntAlign();
257 case UnsignedLong: return getLongAlign();
259 case UnsignedLongLong: return getLongLongAlign();
263 /// isTypeSigned - Return whether an integer types is signed. Returns true if
264 /// the type is signed; false otherwise.
265 bool TargetInfo::isTypeSigned(IntType T) {
267 default: llvm_unreachable("not an integer!");
278 case UnsignedLongLong:
283 /// adjust - Set forced language options.
284 /// Apply changes to the target information with respect to certain
285 /// language options which change the target configuration and adjust
286 /// the language based on the target options where applicable.
287 void TargetInfo::adjust(LangOptions &Opts) {
288 if (Opts.NoBitFieldTypeAlign)
289 UseBitFieldTypeAlignment = false;
291 WCharType = UnsignedShort;
292 if (Opts.AlignDouble) {
293 DoubleAlign = LongLongAlign = 64;
294 LongDoubleAlign = 64;
298 // OpenCL C requires specific widths for types, irrespective of
299 // what these normally are for the target.
300 // We also define long long and long double here, although the
301 // OpenCL standard only mentions these as "reserved".
302 IntWidth = IntAlign = 32;
303 LongWidth = LongAlign = 64;
304 LongLongWidth = LongLongAlign = 128;
305 HalfWidth = HalfAlign = 16;
306 FloatWidth = FloatAlign = 32;
308 // Embedded 32-bit targets (OpenCL EP) might have double C type
309 // defined as float. Let's not override this as it might lead
310 // to generating illegal code that uses 64bit doubles.
311 if (DoubleWidth != FloatWidth) {
312 DoubleWidth = DoubleAlign = 64;
313 DoubleFormat = &llvm::APFloat::IEEEdouble();
315 LongDoubleWidth = LongDoubleAlign = 128;
317 unsigned MaxPointerWidth = getMaxPointerWidth();
318 assert(MaxPointerWidth == 32 || MaxPointerWidth == 64);
319 bool Is32BitArch = MaxPointerWidth == 32;
320 SizeType = Is32BitArch ? UnsignedInt : UnsignedLong;
321 PtrDiffType = Is32BitArch ? SignedInt : SignedLong;
322 IntPtrType = Is32BitArch ? SignedInt : SignedLong;
324 IntMaxType = SignedLongLong;
325 Int64Type = SignedLong;
327 HalfFormat = &llvm::APFloat::IEEEhalf();
328 FloatFormat = &llvm::APFloat::IEEEsingle();
329 LongDoubleFormat = &llvm::APFloat::IEEEquad();
332 if (Opts.NewAlignOverride)
333 NewAlign = Opts.NewAlignOverride * getCharWidth();
336 bool TargetInfo::initFeatureMap(
337 llvm::StringMap<bool> &Features, DiagnosticsEngine &Diags, StringRef CPU,
338 const std::vector<std::string> &FeatureVec) const {
339 for (const auto &F : FeatureVec) {
341 // Apply the feature via the target.
342 bool Enabled = Name[0] == '+';
343 setFeatureEnabled(Features, Name.substr(1), Enabled);
348 //===----------------------------------------------------------------------===//
351 static StringRef removeGCCRegisterPrefix(StringRef Name) {
352 if (Name[0] == '%' || Name[0] == '#')
353 Name = Name.substr(1);
358 /// isValidClobber - Returns whether the passed in string is
359 /// a valid clobber in an inline asm statement. This is used by
361 bool TargetInfo::isValidClobber(StringRef Name) const {
362 return (isValidGCCRegisterName(Name) ||
363 Name == "memory" || Name == "cc");
366 /// isValidGCCRegisterName - Returns whether the passed in string
367 /// is a valid register name according to GCC. This is used by Sema for
368 /// inline asm statements.
369 bool TargetInfo::isValidGCCRegisterName(StringRef Name) const {
373 // Get rid of any register prefix.
374 Name = removeGCCRegisterPrefix(Name);
378 ArrayRef<const char *> Names = getGCCRegNames();
380 // If we have a number it maps to an entry in the register name array.
381 if (isDigit(Name[0])) {
383 if (!Name.getAsInteger(0, n))
384 return n < Names.size();
387 // Check register names.
388 if (std::find(Names.begin(), Names.end(), Name) != Names.end())
391 // Check any additional names that we have.
392 for (const AddlRegName &ARN : getGCCAddlRegNames())
393 for (const char *AN : ARN.Names) {
396 // Make sure the register that the additional name is for is within
397 // the bounds of the register names from above.
398 if (AN == Name && ARN.RegNum < Names.size())
402 // Now check aliases.
403 for (const GCCRegAlias &GRA : getGCCRegAliases())
404 for (const char *A : GRA.Aliases) {
414 StringRef TargetInfo::getNormalizedGCCRegisterName(StringRef Name,
415 bool ReturnCanonical) const {
416 assert(isValidGCCRegisterName(Name) && "Invalid register passed in");
418 // Get rid of any register prefix.
419 Name = removeGCCRegisterPrefix(Name);
421 ArrayRef<const char *> Names = getGCCRegNames();
423 // First, check if we have a number.
424 if (isDigit(Name[0])) {
426 if (!Name.getAsInteger(0, n)) {
427 assert(n < Names.size() && "Out of bounds register number!");
432 // Check any additional names that we have.
433 for (const AddlRegName &ARN : getGCCAddlRegNames())
434 for (const char *AN : ARN.Names) {
437 // Make sure the register that the additional name is for is within
438 // the bounds of the register names from above.
439 if (AN == Name && ARN.RegNum < Names.size())
440 return ReturnCanonical ? Names[ARN.RegNum] : Name;
443 // Now check aliases.
444 for (const GCCRegAlias &RA : getGCCRegAliases())
445 for (const char *A : RA.Aliases) {
455 bool TargetInfo::validateOutputConstraint(ConstraintInfo &Info) const {
456 const char *Name = Info.getConstraintStr().c_str();
457 // An output constraint must start with '=' or '+'
458 if (*Name != '=' && *Name != '+')
462 Info.setIsReadWrite();
468 if (!validateAsmConstraint(Name, Info)) {
469 // FIXME: We temporarily return false
470 // so we can add more constraints as we hit it.
471 // Eventually, an unknown constraint should just be treated as 'g'.
475 case '&': // early clobber.
476 Info.setEarlyClobber();
478 case '%': // commutative.
479 // FIXME: Check that there is a another register after this one.
481 case 'r': // general register.
482 Info.setAllowsRegister();
484 case 'm': // memory operand.
485 case 'o': // offsetable memory operand.
486 case 'V': // non-offsetable memory operand.
487 case '<': // autodecrement memory operand.
488 case '>': // autoincrement memory operand.
489 Info.setAllowsMemory();
491 case 'g': // general register, memory operand or immediate integer.
492 case 'X': // any operand.
493 Info.setAllowsRegister();
494 Info.setAllowsMemory();
496 case ',': // multiple alternative constraint. Pass it.
497 // Handle additional optional '=' or '+' modifiers.
498 if (Name[1] == '=' || Name[1] == '+')
501 case '#': // Ignore as constraint.
502 while (Name[1] && Name[1] != ',')
505 case '?': // Disparage slightly code.
506 case '!': // Disparage severely.
507 case '*': // Ignore for choosing register preferences.
514 // Early clobber with a read-write constraint which doesn't permit registers
516 if (Info.earlyClobber() && Info.isReadWrite() && !Info.allowsRegister())
519 // If a constraint allows neither memory nor register operands it contains
520 // only modifiers. Reject it.
521 return Info.allowsMemory() || Info.allowsRegister();
524 bool TargetInfo::resolveSymbolicName(const char *&Name,
525 ArrayRef<ConstraintInfo> OutputConstraints,
526 unsigned &Index) const {
527 assert(*Name == '[' && "Symbolic name did not start with '['");
529 const char *Start = Name;
530 while (*Name && *Name != ']')
538 std::string SymbolicName(Start, Name - Start);
540 for (Index = 0; Index != OutputConstraints.size(); ++Index)
541 if (SymbolicName == OutputConstraints[Index].getName())
547 bool TargetInfo::validateInputConstraint(
548 MutableArrayRef<ConstraintInfo> OutputConstraints,
549 ConstraintInfo &Info) const {
550 const char *Name = Info.ConstraintStr.c_str();
558 // Check if we have a matching constraint
559 if (*Name >= '0' && *Name <= '9') {
560 const char *DigitStart = Name;
561 while (Name[1] >= '0' && Name[1] <= '9')
563 const char *DigitEnd = Name;
565 if (StringRef(DigitStart, DigitEnd - DigitStart + 1)
566 .getAsInteger(10, i))
569 // Check if matching constraint is out of bounds.
570 if (i >= OutputConstraints.size()) return false;
572 // A number must refer to an output only operand.
573 if (OutputConstraints[i].isReadWrite())
576 // If the constraint is already tied, it must be tied to the
577 // same operand referenced to by the number.
578 if (Info.hasTiedOperand() && Info.getTiedOperand() != i)
581 // The constraint should have the same info as the respective
582 // output constraint.
583 Info.setTiedOperand(i, OutputConstraints[i]);
584 } else if (!validateAsmConstraint(Name, Info)) {
585 // FIXME: This error return is in place temporarily so we can
586 // add more constraints as we hit it. Eventually, an unknown
587 // constraint should just be treated as 'g'.
593 if (!resolveSymbolicName(Name, OutputConstraints, Index))
596 // If the constraint is already tied, it must be tied to the
597 // same operand referenced to by the number.
598 if (Info.hasTiedOperand() && Info.getTiedOperand() != Index)
601 // A number must refer to an output only operand.
602 if (OutputConstraints[Index].isReadWrite())
605 Info.setTiedOperand(Index, OutputConstraints[Index]);
608 case '%': // commutative
609 // FIXME: Fail if % is used with the last operand.
611 case 'i': // immediate integer.
612 case 'n': // immediate integer with a known value.
614 case 'I': // Various constant constraints with target-specific meanings.
622 if (!validateAsmConstraint(Name, Info))
625 case 'r': // general register.
626 Info.setAllowsRegister();
628 case 'm': // memory operand.
629 case 'o': // offsettable memory operand.
630 case 'V': // non-offsettable memory operand.
631 case '<': // autodecrement memory operand.
632 case '>': // autoincrement memory operand.
633 Info.setAllowsMemory();
635 case 'g': // general register, memory operand or immediate integer.
636 case 'X': // any operand.
637 Info.setAllowsRegister();
638 Info.setAllowsMemory();
640 case 'E': // immediate floating point.
641 case 'F': // immediate floating point.
642 case 'p': // address operand.
644 case ',': // multiple alternative constraint. Ignore comma.
646 case '#': // Ignore as constraint.
647 while (Name[1] && Name[1] != ',')
650 case '?': // Disparage slightly code.
651 case '!': // Disparage severely.
652 case '*': // Ignore for choosing register preferences.