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"
21 #include "llvm/Support/TargetParser.h"
23 using namespace clang;
25 static const LangASMap DefaultAddrSpaceMap = {0};
27 // TargetInfo Constructor.
28 TargetInfo::TargetInfo(const llvm::Triple &T) : TargetOpts(), 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();
34 NoAsmVariants = false;
36 PointerWidth = PointerAlign = 32;
37 BoolWidth = BoolAlign = 8;
38 IntWidth = IntAlign = 32;
39 LongWidth = LongAlign = 32;
40 LongLongWidth = LongLongAlign = 64;
42 DefaultAlignForAttributeAligned = 128;
44 // From the glibc documentation, on GNU systems, malloc guarantees 16-byte
45 // alignment on 64-bit systems and 8-byte alignment on 32-bit systems. See
46 // https://www.gnu.org/software/libc/manual/html_node/Malloc-Examples.html
47 if (T.isGNUEnvironment() || T.isWindowsMSVCEnvironment())
48 NewAlign = Triple.isArch64Bit() ? 128 : Triple.isArch32Bit() ? 64 : 0;
50 NewAlign = 0; // Infer from basic type alignment.
60 LargeArrayMinWidth = 0;
62 MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 0;
66 SizeType = UnsignedLong;
67 PtrDiffType = SignedLong;
68 IntMaxType = SignedLongLong;
69 IntPtrType = SignedLong;
70 WCharType = SignedInt;
72 Char16Type = UnsignedShort;
73 Char32Type = UnsignedInt;
74 Int64Type = SignedLongLong;
75 SigAtomicType = SignedInt;
76 ProcessIDType = SignedInt;
77 UseSignedCharForObjCBool = true;
78 UseBitFieldTypeAlignment = true;
79 UseZeroLengthBitfieldAlignment = false;
80 UseExplicitBitFieldAlignment = true;
81 ZeroLengthBitfieldBoundary = 0;
82 HalfFormat = &llvm::APFloat::IEEEhalf();
83 FloatFormat = &llvm::APFloat::IEEEsingle();
84 DoubleFormat = &llvm::APFloat::IEEEdouble();
85 LongDoubleFormat = &llvm::APFloat::IEEEdouble();
86 Float128Format = &llvm::APFloat::IEEEquad();
87 MCountName = "mcount";
90 HasAlignMac68kSupport = false;
91 HasBuiltinMSVaList = false;
92 IsRenderScriptTarget = false;
94 // Default to no types using fpret.
95 RealTypeUsesObjCFPRet = 0;
97 // Default to not using fp2ret for __Complex long double
98 ComplexLongDoubleUsesFP2Ret = false;
100 // Set the C++ ABI based on the triple.
101 TheCXXABI.set(Triple.isKnownWindowsMSVCEnvironment()
102 ? TargetCXXABI::Microsoft
103 : TargetCXXABI::GenericItanium);
105 // Default to an empty address space map.
106 AddrSpaceMap = &DefaultAddrSpaceMap;
107 UseAddrSpaceMapMangling = false;
109 // Default to an unknown platform name.
110 PlatformName = "unknown";
111 PlatformMinVersion = VersionTuple();
114 // Out of line virtual dtor for TargetInfo.
115 TargetInfo::~TargetInfo() {}
117 /// getTypeName - Return the user string for the specified integer type enum.
118 /// For example, SignedShort -> "short".
119 const char *TargetInfo::getTypeName(IntType T) {
121 default: llvm_unreachable("not an integer!");
122 case SignedChar: return "signed char";
123 case UnsignedChar: return "unsigned char";
124 case SignedShort: return "short";
125 case UnsignedShort: return "unsigned short";
126 case SignedInt: return "int";
127 case UnsignedInt: return "unsigned int";
128 case SignedLong: return "long int";
129 case UnsignedLong: return "long unsigned int";
130 case SignedLongLong: return "long long int";
131 case UnsignedLongLong: return "long long unsigned int";
135 /// getTypeConstantSuffix - Return the constant suffix for the specified
136 /// integer type enum. For example, SignedLong -> "L".
137 const char *TargetInfo::getTypeConstantSuffix(IntType T) const {
139 default: llvm_unreachable("not an integer!");
142 case SignedInt: return "";
143 case SignedLong: return "L";
144 case SignedLongLong: return "LL";
146 if (getCharWidth() < getIntWidth())
150 if (getShortWidth() < getIntWidth())
153 case UnsignedInt: return "U";
154 case UnsignedLong: return "UL";
155 case UnsignedLongLong: return "ULL";
159 /// getTypeFormatModifier - Return the printf format modifier for the
160 /// specified integer type enum. For example, SignedLong -> "l".
162 const char *TargetInfo::getTypeFormatModifier(IntType T) {
164 default: llvm_unreachable("not an integer!");
166 case UnsignedChar: return "hh";
168 case UnsignedShort: return "h";
170 case UnsignedInt: return "";
172 case UnsignedLong: return "l";
174 case UnsignedLongLong: return "ll";
178 /// getTypeWidth - Return the width (in bits) of the specified integer type
179 /// enum. For example, SignedInt -> getIntWidth().
180 unsigned TargetInfo::getTypeWidth(IntType T) const {
182 default: llvm_unreachable("not an integer!");
184 case UnsignedChar: return getCharWidth();
186 case UnsignedShort: return getShortWidth();
188 case UnsignedInt: return getIntWidth();
190 case UnsignedLong: return getLongWidth();
192 case UnsignedLongLong: return getLongLongWidth();
196 TargetInfo::IntType TargetInfo::getIntTypeByWidth(
197 unsigned BitWidth, bool IsSigned) const {
198 if (getCharWidth() == BitWidth)
199 return IsSigned ? SignedChar : UnsignedChar;
200 if (getShortWidth() == BitWidth)
201 return IsSigned ? SignedShort : UnsignedShort;
202 if (getIntWidth() == BitWidth)
203 return IsSigned ? SignedInt : UnsignedInt;
204 if (getLongWidth() == BitWidth)
205 return IsSigned ? SignedLong : UnsignedLong;
206 if (getLongLongWidth() == BitWidth)
207 return IsSigned ? SignedLongLong : UnsignedLongLong;
211 TargetInfo::IntType TargetInfo::getLeastIntTypeByWidth(unsigned BitWidth,
212 bool IsSigned) const {
213 if (getCharWidth() >= BitWidth)
214 return IsSigned ? SignedChar : UnsignedChar;
215 if (getShortWidth() >= BitWidth)
216 return IsSigned ? SignedShort : UnsignedShort;
217 if (getIntWidth() >= BitWidth)
218 return IsSigned ? SignedInt : UnsignedInt;
219 if (getLongWidth() >= BitWidth)
220 return IsSigned ? SignedLong : UnsignedLong;
221 if (getLongLongWidth() >= BitWidth)
222 return IsSigned ? SignedLongLong : UnsignedLongLong;
226 TargetInfo::RealType TargetInfo::getRealTypeByWidth(unsigned BitWidth) const {
227 if (getFloatWidth() == BitWidth)
229 if (getDoubleWidth() == BitWidth)
234 if (&getLongDoubleFormat() == &llvm::APFloat::x87DoubleExtended())
238 if (&getLongDoubleFormat() == &llvm::APFloat::PPCDoubleDouble() ||
239 &getLongDoubleFormat() == &llvm::APFloat::IEEEquad())
241 if (hasFloat128Type())
249 /// getTypeAlign - Return the alignment (in bits) of the specified integer type
250 /// enum. For example, SignedInt -> getIntAlign().
251 unsigned TargetInfo::getTypeAlign(IntType T) const {
253 default: llvm_unreachable("not an integer!");
255 case UnsignedChar: return getCharAlign();
257 case UnsignedShort: return getShortAlign();
259 case UnsignedInt: return getIntAlign();
261 case UnsignedLong: return getLongAlign();
263 case UnsignedLongLong: return getLongLongAlign();
267 /// isTypeSigned - Return whether an integer types is signed. Returns true if
268 /// the type is signed; false otherwise.
269 bool TargetInfo::isTypeSigned(IntType T) {
271 default: llvm_unreachable("not an integer!");
282 case UnsignedLongLong:
287 /// adjust - Set forced language options.
288 /// Apply changes to the target information with respect to certain
289 /// language options which change the target configuration and adjust
290 /// the language based on the target options where applicable.
291 void TargetInfo::adjust(LangOptions &Opts) {
292 if (Opts.NoBitFieldTypeAlign)
293 UseBitFieldTypeAlignment = false;
295 switch (Opts.WCharSize) {
296 default: llvm_unreachable("invalid wchar_t width");
298 case 1: WCharType = Opts.WCharIsSigned ? SignedChar : UnsignedChar; break;
299 case 2: WCharType = Opts.WCharIsSigned ? SignedShort : UnsignedShort; break;
300 case 4: WCharType = Opts.WCharIsSigned ? SignedInt : UnsignedInt; break;
303 if (Opts.AlignDouble) {
304 DoubleAlign = LongLongAlign = 64;
305 LongDoubleAlign = 64;
309 // OpenCL C requires specific widths for types, irrespective of
310 // what these normally are for the target.
311 // We also define long long and long double here, although the
312 // OpenCL standard only mentions these as "reserved".
313 IntWidth = IntAlign = 32;
314 LongWidth = LongAlign = 64;
315 LongLongWidth = LongLongAlign = 128;
316 HalfWidth = HalfAlign = 16;
317 FloatWidth = FloatAlign = 32;
319 // Embedded 32-bit targets (OpenCL EP) might have double C type
320 // defined as float. Let's not override this as it might lead
321 // to generating illegal code that uses 64bit doubles.
322 if (DoubleWidth != FloatWidth) {
323 DoubleWidth = DoubleAlign = 64;
324 DoubleFormat = &llvm::APFloat::IEEEdouble();
326 LongDoubleWidth = LongDoubleAlign = 128;
328 unsigned MaxPointerWidth = getMaxPointerWidth();
329 assert(MaxPointerWidth == 32 || MaxPointerWidth == 64);
330 bool Is32BitArch = MaxPointerWidth == 32;
331 SizeType = Is32BitArch ? UnsignedInt : UnsignedLong;
332 PtrDiffType = Is32BitArch ? SignedInt : SignedLong;
333 IntPtrType = Is32BitArch ? SignedInt : SignedLong;
335 IntMaxType = SignedLongLong;
336 Int64Type = SignedLong;
338 HalfFormat = &llvm::APFloat::IEEEhalf();
339 FloatFormat = &llvm::APFloat::IEEEsingle();
340 LongDoubleFormat = &llvm::APFloat::IEEEquad();
343 if (Opts.NewAlignOverride)
344 NewAlign = Opts.NewAlignOverride * getCharWidth();
347 bool TargetInfo::initFeatureMap(
348 llvm::StringMap<bool> &Features, DiagnosticsEngine &Diags, StringRef CPU,
349 const std::vector<std::string> &FeatureVec) const {
350 for (const auto &F : FeatureVec) {
352 // Apply the feature via the target.
353 bool Enabled = Name[0] == '+';
354 setFeatureEnabled(Features, Name.substr(1), Enabled);
359 LangAS TargetInfo::getOpenCLTypeAddrSpace(OpenCLTypeKind TK) const {
363 return LangAS::opencl_global;
366 return LangAS::opencl_constant;
369 return LangAS::Default;
373 //===----------------------------------------------------------------------===//
376 static StringRef removeGCCRegisterPrefix(StringRef Name) {
377 if (Name[0] == '%' || Name[0] == '#')
378 Name = Name.substr(1);
383 /// isValidClobber - Returns whether the passed in string is
384 /// a valid clobber in an inline asm statement. This is used by
386 bool TargetInfo::isValidClobber(StringRef Name) const {
387 return (isValidGCCRegisterName(Name) ||
388 Name == "memory" || Name == "cc");
391 /// isValidGCCRegisterName - Returns whether the passed in string
392 /// is a valid register name according to GCC. This is used by Sema for
393 /// inline asm statements.
394 bool TargetInfo::isValidGCCRegisterName(StringRef Name) const {
398 // Get rid of any register prefix.
399 Name = removeGCCRegisterPrefix(Name);
403 ArrayRef<const char *> Names = getGCCRegNames();
405 // If we have a number it maps to an entry in the register name array.
406 if (isDigit(Name[0])) {
408 if (!Name.getAsInteger(0, n))
409 return n < Names.size();
412 // Check register names.
413 if (std::find(Names.begin(), Names.end(), Name) != Names.end())
416 // Check any additional names that we have.
417 for (const AddlRegName &ARN : getGCCAddlRegNames())
418 for (const char *AN : ARN.Names) {
421 // Make sure the register that the additional name is for is within
422 // the bounds of the register names from above.
423 if (AN == Name && ARN.RegNum < Names.size())
427 // Now check aliases.
428 for (const GCCRegAlias &GRA : getGCCRegAliases())
429 for (const char *A : GRA.Aliases) {
439 StringRef TargetInfo::getNormalizedGCCRegisterName(StringRef Name,
440 bool ReturnCanonical) const {
441 assert(isValidGCCRegisterName(Name) && "Invalid register passed in");
443 // Get rid of any register prefix.
444 Name = removeGCCRegisterPrefix(Name);
446 ArrayRef<const char *> Names = getGCCRegNames();
448 // First, check if we have a number.
449 if (isDigit(Name[0])) {
451 if (!Name.getAsInteger(0, n)) {
452 assert(n < Names.size() && "Out of bounds register number!");
457 // Check any additional names that we have.
458 for (const AddlRegName &ARN : getGCCAddlRegNames())
459 for (const char *AN : ARN.Names) {
462 // Make sure the register that the additional name is for is within
463 // the bounds of the register names from above.
464 if (AN == Name && ARN.RegNum < Names.size())
465 return ReturnCanonical ? Names[ARN.RegNum] : Name;
468 // Now check aliases.
469 for (const GCCRegAlias &RA : getGCCRegAliases())
470 for (const char *A : RA.Aliases) {
480 bool TargetInfo::validateOutputConstraint(ConstraintInfo &Info) const {
481 const char *Name = Info.getConstraintStr().c_str();
482 // An output constraint must start with '=' or '+'
483 if (*Name != '=' && *Name != '+')
487 Info.setIsReadWrite();
493 if (!validateAsmConstraint(Name, Info)) {
494 // FIXME: We temporarily return false
495 // so we can add more constraints as we hit it.
496 // Eventually, an unknown constraint should just be treated as 'g'.
500 case '&': // early clobber.
501 Info.setEarlyClobber();
503 case '%': // commutative.
504 // FIXME: Check that there is a another register after this one.
506 case 'r': // general register.
507 Info.setAllowsRegister();
509 case 'm': // memory operand.
510 case 'o': // offsetable memory operand.
511 case 'V': // non-offsetable memory operand.
512 case '<': // autodecrement memory operand.
513 case '>': // autoincrement memory operand.
514 Info.setAllowsMemory();
516 case 'g': // general register, memory operand or immediate integer.
517 case 'X': // any operand.
518 Info.setAllowsRegister();
519 Info.setAllowsMemory();
521 case ',': // multiple alternative constraint. Pass it.
522 // Handle additional optional '=' or '+' modifiers.
523 if (Name[1] == '=' || Name[1] == '+')
526 case '#': // Ignore as constraint.
527 while (Name[1] && Name[1] != ',')
530 case '?': // Disparage slightly code.
531 case '!': // Disparage severely.
532 case '*': // Ignore for choosing register preferences.
533 case 'i': // Ignore i,n,E,F as output constraints (match from the other
544 // Early clobber with a read-write constraint which doesn't permit registers
546 if (Info.earlyClobber() && Info.isReadWrite() && !Info.allowsRegister())
549 // If a constraint allows neither memory nor register operands it contains
550 // only modifiers. Reject it.
551 return Info.allowsMemory() || Info.allowsRegister();
554 bool TargetInfo::resolveSymbolicName(const char *&Name,
555 ArrayRef<ConstraintInfo> OutputConstraints,
556 unsigned &Index) const {
557 assert(*Name == '[' && "Symbolic name did not start with '['");
559 const char *Start = Name;
560 while (*Name && *Name != ']')
568 std::string SymbolicName(Start, Name - Start);
570 for (Index = 0; Index != OutputConstraints.size(); ++Index)
571 if (SymbolicName == OutputConstraints[Index].getName())
577 bool TargetInfo::validateInputConstraint(
578 MutableArrayRef<ConstraintInfo> OutputConstraints,
579 ConstraintInfo &Info) const {
580 const char *Name = Info.ConstraintStr.c_str();
588 // Check if we have a matching constraint
589 if (*Name >= '0' && *Name <= '9') {
590 const char *DigitStart = Name;
591 while (Name[1] >= '0' && Name[1] <= '9')
593 const char *DigitEnd = Name;
595 if (StringRef(DigitStart, DigitEnd - DigitStart + 1)
596 .getAsInteger(10, i))
599 // Check if matching constraint is out of bounds.
600 if (i >= OutputConstraints.size()) return false;
602 // A number must refer to an output only operand.
603 if (OutputConstraints[i].isReadWrite())
606 // If the constraint is already tied, it must be tied to the
607 // same operand referenced to by the number.
608 if (Info.hasTiedOperand() && Info.getTiedOperand() != i)
611 // The constraint should have the same info as the respective
612 // output constraint.
613 Info.setTiedOperand(i, OutputConstraints[i]);
614 } else if (!validateAsmConstraint(Name, Info)) {
615 // FIXME: This error return is in place temporarily so we can
616 // add more constraints as we hit it. Eventually, an unknown
617 // constraint should just be treated as 'g'.
623 if (!resolveSymbolicName(Name, OutputConstraints, Index))
626 // If the constraint is already tied, it must be tied to the
627 // same operand referenced to by the number.
628 if (Info.hasTiedOperand() && Info.getTiedOperand() != Index)
631 // A number must refer to an output only operand.
632 if (OutputConstraints[Index].isReadWrite())
635 Info.setTiedOperand(Index, OutputConstraints[Index]);
638 case '%': // commutative
639 // FIXME: Fail if % is used with the last operand.
641 case 'i': // immediate integer.
642 case 'n': // immediate integer with a known value.
644 case 'I': // Various constant constraints with target-specific meanings.
652 if (!validateAsmConstraint(Name, Info))
655 case 'r': // general register.
656 Info.setAllowsRegister();
658 case 'm': // memory operand.
659 case 'o': // offsettable memory operand.
660 case 'V': // non-offsettable memory operand.
661 case '<': // autodecrement memory operand.
662 case '>': // autoincrement memory operand.
663 Info.setAllowsMemory();
665 case 'g': // general register, memory operand or immediate integer.
666 case 'X': // any operand.
667 Info.setAllowsRegister();
668 Info.setAllowsMemory();
670 case 'E': // immediate floating point.
671 case 'F': // immediate floating point.
672 case 'p': // address operand.
674 case ',': // multiple alternative constraint. Ignore comma.
676 case '#': // Ignore as constraint.
677 while (Name[1] && Name[1] != ',')
680 case '?': // Disparage slightly code.
681 case '!': // Disparage severely.
682 case '*': // Ignore for choosing register preferences.