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())
148 if (getShortWidth() < getIntWidth())
151 case UnsignedInt: return "U";
152 case UnsignedLong: return "UL";
153 case UnsignedLongLong: return "ULL";
157 /// getTypeFormatModifier - Return the printf format modifier for the
158 /// specified integer type enum. For example, SignedLong -> "l".
160 const char *TargetInfo::getTypeFormatModifier(IntType T) {
162 default: llvm_unreachable("not an integer!");
164 case UnsignedChar: return "hh";
166 case UnsignedShort: return "h";
168 case UnsignedInt: return "";
170 case UnsignedLong: return "l";
172 case UnsignedLongLong: return "ll";
176 /// getTypeWidth - Return the width (in bits) of the specified integer type
177 /// enum. For example, SignedInt -> getIntWidth().
178 unsigned TargetInfo::getTypeWidth(IntType T) const {
180 default: llvm_unreachable("not an integer!");
182 case UnsignedChar: return getCharWidth();
184 case UnsignedShort: return getShortWidth();
186 case UnsignedInt: return getIntWidth();
188 case UnsignedLong: return getLongWidth();
190 case UnsignedLongLong: return getLongLongWidth();
194 TargetInfo::IntType TargetInfo::getIntTypeByWidth(
195 unsigned BitWidth, bool IsSigned) const {
196 if (getCharWidth() == BitWidth)
197 return IsSigned ? SignedChar : UnsignedChar;
198 if (getShortWidth() == BitWidth)
199 return IsSigned ? SignedShort : UnsignedShort;
200 if (getIntWidth() == BitWidth)
201 return IsSigned ? SignedInt : UnsignedInt;
202 if (getLongWidth() == BitWidth)
203 return IsSigned ? SignedLong : UnsignedLong;
204 if (getLongLongWidth() == BitWidth)
205 return IsSigned ? SignedLongLong : UnsignedLongLong;
209 TargetInfo::IntType TargetInfo::getLeastIntTypeByWidth(unsigned BitWidth,
210 bool IsSigned) const {
211 if (getCharWidth() >= BitWidth)
212 return IsSigned ? SignedChar : UnsignedChar;
213 if (getShortWidth() >= BitWidth)
214 return IsSigned ? SignedShort : UnsignedShort;
215 if (getIntWidth() >= BitWidth)
216 return IsSigned ? SignedInt : UnsignedInt;
217 if (getLongWidth() >= BitWidth)
218 return IsSigned ? SignedLong : UnsignedLong;
219 if (getLongLongWidth() >= BitWidth)
220 return IsSigned ? SignedLongLong : UnsignedLongLong;
224 TargetInfo::RealType TargetInfo::getRealTypeByWidth(unsigned BitWidth) const {
225 if (getFloatWidth() == BitWidth)
227 if (getDoubleWidth() == BitWidth)
232 if (&getLongDoubleFormat() == &llvm::APFloat::x87DoubleExtended())
236 if (&getLongDoubleFormat() == &llvm::APFloat::PPCDoubleDouble() ||
237 &getLongDoubleFormat() == &llvm::APFloat::IEEEquad())
239 if (hasFloat128Type())
247 /// getTypeAlign - Return the alignment (in bits) of the specified integer type
248 /// enum. For example, SignedInt -> getIntAlign().
249 unsigned TargetInfo::getTypeAlign(IntType T) const {
251 default: llvm_unreachable("not an integer!");
253 case UnsignedChar: return getCharAlign();
255 case UnsignedShort: return getShortAlign();
257 case UnsignedInt: return getIntAlign();
259 case UnsignedLong: return getLongAlign();
261 case UnsignedLongLong: return getLongLongAlign();
265 /// isTypeSigned - Return whether an integer types is signed. Returns true if
266 /// the type is signed; false otherwise.
267 bool TargetInfo::isTypeSigned(IntType T) {
269 default: llvm_unreachable("not an integer!");
280 case UnsignedLongLong:
285 /// adjust - Set forced language options.
286 /// Apply changes to the target information with respect to certain
287 /// language options which change the target configuration and adjust
288 /// the language based on the target options where applicable.
289 void TargetInfo::adjust(LangOptions &Opts) {
290 if (Opts.NoBitFieldTypeAlign)
291 UseBitFieldTypeAlignment = false;
293 WCharType = UnsignedShort;
294 if (Opts.AlignDouble) {
295 DoubleAlign = LongLongAlign = 64;
296 LongDoubleAlign = 64;
300 // OpenCL C requires specific widths for types, irrespective of
301 // what these normally are for the target.
302 // We also define long long and long double here, although the
303 // OpenCL standard only mentions these as "reserved".
304 IntWidth = IntAlign = 32;
305 LongWidth = LongAlign = 64;
306 LongLongWidth = LongLongAlign = 128;
307 HalfWidth = HalfAlign = 16;
308 FloatWidth = FloatAlign = 32;
310 // Embedded 32-bit targets (OpenCL EP) might have double C type
311 // defined as float. Let's not override this as it might lead
312 // to generating illegal code that uses 64bit doubles.
313 if (DoubleWidth != FloatWidth) {
314 DoubleWidth = DoubleAlign = 64;
315 DoubleFormat = &llvm::APFloat::IEEEdouble();
317 LongDoubleWidth = LongDoubleAlign = 128;
319 unsigned MaxPointerWidth = getMaxPointerWidth();
320 assert(MaxPointerWidth == 32 || MaxPointerWidth == 64);
321 bool Is32BitArch = MaxPointerWidth == 32;
322 SizeType = Is32BitArch ? UnsignedInt : UnsignedLong;
323 PtrDiffType = Is32BitArch ? SignedInt : SignedLong;
324 IntPtrType = Is32BitArch ? SignedInt : SignedLong;
326 IntMaxType = SignedLongLong;
327 Int64Type = SignedLong;
329 HalfFormat = &llvm::APFloat::IEEEhalf();
330 FloatFormat = &llvm::APFloat::IEEEsingle();
331 LongDoubleFormat = &llvm::APFloat::IEEEquad();
334 if (Opts.NewAlignOverride)
335 NewAlign = Opts.NewAlignOverride * getCharWidth();
338 bool TargetInfo::initFeatureMap(
339 llvm::StringMap<bool> &Features, DiagnosticsEngine &Diags, StringRef CPU,
340 const std::vector<std::string> &FeatureVec) const {
341 for (const auto &F : FeatureVec) {
343 // Apply the feature via the target.
344 bool Enabled = Name[0] == '+';
345 setFeatureEnabled(Features, Name.substr(1), Enabled);
350 //===----------------------------------------------------------------------===//
353 static StringRef removeGCCRegisterPrefix(StringRef Name) {
354 if (Name[0] == '%' || Name[0] == '#')
355 Name = Name.substr(1);
360 /// isValidClobber - Returns whether the passed in string is
361 /// a valid clobber in an inline asm statement. This is used by
363 bool TargetInfo::isValidClobber(StringRef Name) const {
364 return (isValidGCCRegisterName(Name) ||
365 Name == "memory" || Name == "cc");
368 /// isValidGCCRegisterName - Returns whether the passed in string
369 /// is a valid register name according to GCC. This is used by Sema for
370 /// inline asm statements.
371 bool TargetInfo::isValidGCCRegisterName(StringRef Name) const {
375 // Get rid of any register prefix.
376 Name = removeGCCRegisterPrefix(Name);
380 ArrayRef<const char *> Names = getGCCRegNames();
382 // If we have a number it maps to an entry in the register name array.
383 if (isDigit(Name[0])) {
385 if (!Name.getAsInteger(0, n))
386 return n < Names.size();
389 // Check register names.
390 if (std::find(Names.begin(), Names.end(), Name) != Names.end())
393 // Check any additional names that we have.
394 for (const AddlRegName &ARN : getGCCAddlRegNames())
395 for (const char *AN : ARN.Names) {
398 // Make sure the register that the additional name is for is within
399 // the bounds of the register names from above.
400 if (AN == Name && ARN.RegNum < Names.size())
404 // Now check aliases.
405 for (const GCCRegAlias &GRA : getGCCRegAliases())
406 for (const char *A : GRA.Aliases) {
416 StringRef TargetInfo::getNormalizedGCCRegisterName(StringRef Name,
417 bool ReturnCanonical) const {
418 assert(isValidGCCRegisterName(Name) && "Invalid register passed in");
420 // Get rid of any register prefix.
421 Name = removeGCCRegisterPrefix(Name);
423 ArrayRef<const char *> Names = getGCCRegNames();
425 // First, check if we have a number.
426 if (isDigit(Name[0])) {
428 if (!Name.getAsInteger(0, n)) {
429 assert(n < Names.size() && "Out of bounds register number!");
434 // Check any additional names that we have.
435 for (const AddlRegName &ARN : getGCCAddlRegNames())
436 for (const char *AN : ARN.Names) {
439 // Make sure the register that the additional name is for is within
440 // the bounds of the register names from above.
441 if (AN == Name && ARN.RegNum < Names.size())
442 return ReturnCanonical ? Names[ARN.RegNum] : Name;
445 // Now check aliases.
446 for (const GCCRegAlias &RA : getGCCRegAliases())
447 for (const char *A : RA.Aliases) {
457 bool TargetInfo::validateOutputConstraint(ConstraintInfo &Info) const {
458 const char *Name = Info.getConstraintStr().c_str();
459 // An output constraint must start with '=' or '+'
460 if (*Name != '=' && *Name != '+')
464 Info.setIsReadWrite();
470 if (!validateAsmConstraint(Name, Info)) {
471 // FIXME: We temporarily return false
472 // so we can add more constraints as we hit it.
473 // Eventually, an unknown constraint should just be treated as 'g'.
477 case '&': // early clobber.
478 Info.setEarlyClobber();
480 case '%': // commutative.
481 // FIXME: Check that there is a another register after this one.
483 case 'r': // general register.
484 Info.setAllowsRegister();
486 case 'm': // memory operand.
487 case 'o': // offsetable memory operand.
488 case 'V': // non-offsetable memory operand.
489 case '<': // autodecrement memory operand.
490 case '>': // autoincrement memory operand.
491 Info.setAllowsMemory();
493 case 'g': // general register, memory operand or immediate integer.
494 case 'X': // any operand.
495 Info.setAllowsRegister();
496 Info.setAllowsMemory();
498 case ',': // multiple alternative constraint. Pass it.
499 // Handle additional optional '=' or '+' modifiers.
500 if (Name[1] == '=' || Name[1] == '+')
503 case '#': // Ignore as constraint.
504 while (Name[1] && Name[1] != ',')
507 case '?': // Disparage slightly code.
508 case '!': // Disparage severely.
509 case '*': // Ignore for choosing register preferences.
510 case 'i': // Ignore i,n,E,F as output constraints (match from the other
521 // Early clobber with a read-write constraint which doesn't permit registers
523 if (Info.earlyClobber() && Info.isReadWrite() && !Info.allowsRegister())
526 // If a constraint allows neither memory nor register operands it contains
527 // only modifiers. Reject it.
528 return Info.allowsMemory() || Info.allowsRegister();
531 bool TargetInfo::resolveSymbolicName(const char *&Name,
532 ArrayRef<ConstraintInfo> OutputConstraints,
533 unsigned &Index) const {
534 assert(*Name == '[' && "Symbolic name did not start with '['");
536 const char *Start = Name;
537 while (*Name && *Name != ']')
545 std::string SymbolicName(Start, Name - Start);
547 for (Index = 0; Index != OutputConstraints.size(); ++Index)
548 if (SymbolicName == OutputConstraints[Index].getName())
554 bool TargetInfo::validateInputConstraint(
555 MutableArrayRef<ConstraintInfo> OutputConstraints,
556 ConstraintInfo &Info) const {
557 const char *Name = Info.ConstraintStr.c_str();
565 // Check if we have a matching constraint
566 if (*Name >= '0' && *Name <= '9') {
567 const char *DigitStart = Name;
568 while (Name[1] >= '0' && Name[1] <= '9')
570 const char *DigitEnd = Name;
572 if (StringRef(DigitStart, DigitEnd - DigitStart + 1)
573 .getAsInteger(10, i))
576 // Check if matching constraint is out of bounds.
577 if (i >= OutputConstraints.size()) return false;
579 // A number must refer to an output only operand.
580 if (OutputConstraints[i].isReadWrite())
583 // If the constraint is already tied, it must be tied to the
584 // same operand referenced to by the number.
585 if (Info.hasTiedOperand() && Info.getTiedOperand() != i)
588 // The constraint should have the same info as the respective
589 // output constraint.
590 Info.setTiedOperand(i, OutputConstraints[i]);
591 } else if (!validateAsmConstraint(Name, Info)) {
592 // FIXME: This error return is in place temporarily so we can
593 // add more constraints as we hit it. Eventually, an unknown
594 // constraint should just be treated as 'g'.
600 if (!resolveSymbolicName(Name, OutputConstraints, Index))
603 // If the constraint is already tied, it must be tied to the
604 // same operand referenced to by the number.
605 if (Info.hasTiedOperand() && Info.getTiedOperand() != Index)
608 // A number must refer to an output only operand.
609 if (OutputConstraints[Index].isReadWrite())
612 Info.setTiedOperand(Index, OutputConstraints[Index]);
615 case '%': // commutative
616 // FIXME: Fail if % is used with the last operand.
618 case 'i': // immediate integer.
619 case 'n': // immediate integer with a known value.
621 case 'I': // Various constant constraints with target-specific meanings.
629 if (!validateAsmConstraint(Name, Info))
632 case 'r': // general register.
633 Info.setAllowsRegister();
635 case 'm': // memory operand.
636 case 'o': // offsettable memory operand.
637 case 'V': // non-offsettable memory operand.
638 case '<': // autodecrement memory operand.
639 case '>': // autoincrement memory operand.
640 Info.setAllowsMemory();
642 case 'g': // general register, memory operand or immediate integer.
643 case 'X': // any operand.
644 Info.setAllowsRegister();
645 Info.setAllowsMemory();
647 case 'E': // immediate floating point.
648 case 'F': // immediate floating point.
649 case 'p': // address operand.
651 case ',': // multiple alternative constraint. Ignore comma.
653 case '#': // Ignore as constraint.
654 while (Name[1] && Name[1] != ',')
657 case '?': // Disparage slightly code.
658 case '!': // Disparage severely.
659 case '*': // Ignore for choosing register preferences.