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.
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;
51 LargeArrayMinWidth = 0;
53 MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 0;
57 SizeType = UnsignedLong;
58 PtrDiffType = SignedLong;
59 IntMaxType = SignedLongLong;
60 IntPtrType = SignedLong;
61 WCharType = SignedInt;
63 Char16Type = UnsignedShort;
64 Char32Type = UnsignedInt;
65 Int64Type = SignedLongLong;
66 SigAtomicType = SignedInt;
67 ProcessIDType = SignedInt;
68 UseSignedCharForObjCBool = true;
69 UseBitFieldTypeAlignment = true;
70 UseZeroLengthBitfieldAlignment = false;
71 UseExplicitBitFieldAlignment = true;
72 ZeroLengthBitfieldBoundary = 0;
73 HalfFormat = &llvm::APFloat::IEEEhalf;
74 FloatFormat = &llvm::APFloat::IEEEsingle;
75 DoubleFormat = &llvm::APFloat::IEEEdouble;
76 LongDoubleFormat = &llvm::APFloat::IEEEdouble;
77 Float128Format = &llvm::APFloat::IEEEquad;
78 MCountName = "mcount";
81 HasAlignMac68kSupport = false;
82 HasBuiltinMSVaList = false;
84 // Default to no types using fpret.
85 RealTypeUsesObjCFPRet = 0;
87 // Default to not using fp2ret for __Complex long double
88 ComplexLongDoubleUsesFP2Ret = false;
90 // Set the C++ ABI based on the triple.
91 TheCXXABI.set(Triple.isKnownWindowsMSVCEnvironment()
92 ? TargetCXXABI::Microsoft
93 : TargetCXXABI::GenericItanium);
95 // Default to an empty address space map.
96 AddrSpaceMap = &DefaultAddrSpaceMap;
97 UseAddrSpaceMapMangling = false;
99 // Default to an unknown platform name.
100 PlatformName = "unknown";
101 PlatformMinVersion = VersionTuple();
104 // Out of line virtual dtor for TargetInfo.
105 TargetInfo::~TargetInfo() {}
107 /// getTypeName - Return the user string for the specified integer type enum.
108 /// For example, SignedShort -> "short".
109 const char *TargetInfo::getTypeName(IntType T) {
111 default: llvm_unreachable("not an integer!");
112 case SignedChar: return "signed char";
113 case UnsignedChar: return "unsigned char";
114 case SignedShort: return "short";
115 case UnsignedShort: return "unsigned short";
116 case SignedInt: return "int";
117 case UnsignedInt: return "unsigned int";
118 case SignedLong: return "long int";
119 case UnsignedLong: return "long unsigned int";
120 case SignedLongLong: return "long long int";
121 case UnsignedLongLong: return "long long unsigned int";
125 /// getTypeConstantSuffix - Return the constant suffix for the specified
126 /// integer type enum. For example, SignedLong -> "L".
127 const char *TargetInfo::getTypeConstantSuffix(IntType T) const {
129 default: llvm_unreachable("not an integer!");
132 case SignedInt: return "";
133 case SignedLong: return "L";
134 case SignedLongLong: return "LL";
136 if (getCharWidth() < getIntWidth())
139 if (getShortWidth() < getIntWidth())
141 case UnsignedInt: return "U";
142 case UnsignedLong: return "UL";
143 case UnsignedLongLong: return "ULL";
147 /// getTypeFormatModifier - Return the printf format modifier for the
148 /// specified integer type enum. For example, SignedLong -> "l".
150 const char *TargetInfo::getTypeFormatModifier(IntType T) {
152 default: llvm_unreachable("not an integer!");
154 case UnsignedChar: return "hh";
156 case UnsignedShort: return "h";
158 case UnsignedInt: return "";
160 case UnsignedLong: return "l";
162 case UnsignedLongLong: return "ll";
166 /// getTypeWidth - Return the width (in bits) of the specified integer type
167 /// enum. For example, SignedInt -> getIntWidth().
168 unsigned TargetInfo::getTypeWidth(IntType T) const {
170 default: llvm_unreachable("not an integer!");
172 case UnsignedChar: return getCharWidth();
174 case UnsignedShort: return getShortWidth();
176 case UnsignedInt: return getIntWidth();
178 case UnsignedLong: return getLongWidth();
180 case UnsignedLongLong: return getLongLongWidth();
184 TargetInfo::IntType TargetInfo::getIntTypeByWidth(
185 unsigned BitWidth, bool IsSigned) const {
186 if (getCharWidth() == BitWidth)
187 return IsSigned ? SignedChar : UnsignedChar;
188 if (getShortWidth() == BitWidth)
189 return IsSigned ? SignedShort : UnsignedShort;
190 if (getIntWidth() == BitWidth)
191 return IsSigned ? SignedInt : UnsignedInt;
192 if (getLongWidth() == BitWidth)
193 return IsSigned ? SignedLong : UnsignedLong;
194 if (getLongLongWidth() == BitWidth)
195 return IsSigned ? SignedLongLong : UnsignedLongLong;
199 TargetInfo::IntType TargetInfo::getLeastIntTypeByWidth(unsigned BitWidth,
200 bool IsSigned) const {
201 if (getCharWidth() >= BitWidth)
202 return IsSigned ? SignedChar : UnsignedChar;
203 if (getShortWidth() >= BitWidth)
204 return IsSigned ? SignedShort : UnsignedShort;
205 if (getIntWidth() >= BitWidth)
206 return IsSigned ? SignedInt : UnsignedInt;
207 if (getLongWidth() >= BitWidth)
208 return IsSigned ? SignedLong : UnsignedLong;
209 if (getLongLongWidth() >= BitWidth)
210 return IsSigned ? SignedLongLong : UnsignedLongLong;
214 TargetInfo::RealType TargetInfo::getRealTypeByWidth(unsigned BitWidth) const {
215 if (getFloatWidth() == BitWidth)
217 if (getDoubleWidth() == BitWidth)
222 if (&getLongDoubleFormat() == &llvm::APFloat::x87DoubleExtended)
226 if (&getLongDoubleFormat() == &llvm::APFloat::PPCDoubleDouble ||
227 &getLongDoubleFormat() == &llvm::APFloat::IEEEquad)
229 if (hasFloat128Type())
237 /// getTypeAlign - Return the alignment (in bits) of the specified integer type
238 /// enum. For example, SignedInt -> getIntAlign().
239 unsigned TargetInfo::getTypeAlign(IntType T) const {
241 default: llvm_unreachable("not an integer!");
243 case UnsignedChar: return getCharAlign();
245 case UnsignedShort: return getShortAlign();
247 case UnsignedInt: return getIntAlign();
249 case UnsignedLong: return getLongAlign();
251 case UnsignedLongLong: return getLongLongAlign();
255 /// isTypeSigned - Return whether an integer types is signed. Returns true if
256 /// the type is signed; false otherwise.
257 bool TargetInfo::isTypeSigned(IntType T) {
259 default: llvm_unreachable("not an integer!");
270 case UnsignedLongLong:
275 /// adjust - Set forced language options.
276 /// Apply changes to the target information with respect to certain
277 /// language options which change the target configuration.
278 void TargetInfo::adjust(const LangOptions &Opts) {
279 if (Opts.NoBitFieldTypeAlign)
280 UseBitFieldTypeAlignment = false;
282 WCharType = UnsignedShort;
283 if (Opts.AlignDouble) {
284 DoubleAlign = LongLongAlign = 64;
285 LongDoubleAlign = 64;
289 // OpenCL C requires specific widths for types, irrespective of
290 // what these normally are for the target.
291 // We also define long long and long double here, although the
292 // OpenCL standard only mentions these as "reserved".
293 IntWidth = IntAlign = 32;
294 LongWidth = LongAlign = 64;
295 LongLongWidth = LongLongAlign = 128;
296 HalfWidth = HalfAlign = 16;
297 FloatWidth = FloatAlign = 32;
299 // Embedded 32-bit targets (OpenCL EP) might have double C type
300 // defined as float. Let's not override this as it might lead
301 // to generating illegal code that uses 64bit doubles.
302 if (DoubleWidth != FloatWidth) {
303 DoubleWidth = DoubleAlign = 64;
304 DoubleFormat = &llvm::APFloat::IEEEdouble;
306 LongDoubleWidth = LongDoubleAlign = 128;
308 assert(PointerWidth == 32 || PointerWidth == 64);
309 bool Is32BitArch = PointerWidth == 32;
310 SizeType = Is32BitArch ? UnsignedInt : UnsignedLong;
311 PtrDiffType = Is32BitArch ? SignedInt : SignedLong;
312 IntPtrType = Is32BitArch ? SignedInt : SignedLong;
314 IntMaxType = SignedLongLong;
315 Int64Type = SignedLong;
317 HalfFormat = &llvm::APFloat::IEEEhalf;
318 FloatFormat = &llvm::APFloat::IEEEsingle;
319 LongDoubleFormat = &llvm::APFloat::IEEEquad;
323 bool TargetInfo::initFeatureMap(
324 llvm::StringMap<bool> &Features, DiagnosticsEngine &Diags, StringRef CPU,
325 const std::vector<std::string> &FeatureVec) const {
326 for (const auto &F : FeatureVec) {
328 // Apply the feature via the target.
329 bool Enabled = Name[0] == '+';
330 setFeatureEnabled(Features, Name.substr(1), Enabled);
335 //===----------------------------------------------------------------------===//
338 static StringRef removeGCCRegisterPrefix(StringRef Name) {
339 if (Name[0] == '%' || Name[0] == '#')
340 Name = Name.substr(1);
345 /// isValidClobber - Returns whether the passed in string is
346 /// a valid clobber in an inline asm statement. This is used by
348 bool TargetInfo::isValidClobber(StringRef Name) const {
349 return (isValidGCCRegisterName(Name) ||
350 Name == "memory" || Name == "cc");
353 /// isValidGCCRegisterName - Returns whether the passed in string
354 /// is a valid register name according to GCC. This is used by Sema for
355 /// inline asm statements.
356 bool TargetInfo::isValidGCCRegisterName(StringRef Name) const {
360 // Get rid of any register prefix.
361 Name = removeGCCRegisterPrefix(Name);
365 ArrayRef<const char *> Names = getGCCRegNames();
367 // If we have a number it maps to an entry in the register name array.
368 if (isDigit(Name[0])) {
370 if (!Name.getAsInteger(0, n))
371 return n < Names.size();
374 // Check register names.
375 if (std::find(Names.begin(), Names.end(), Name) != Names.end())
378 // Check any additional names that we have.
379 for (const AddlRegName &ARN : getGCCAddlRegNames())
380 for (const char *AN : ARN.Names) {
383 // Make sure the register that the additional name is for is within
384 // the bounds of the register names from above.
385 if (AN == Name && ARN.RegNum < Names.size())
389 // Now check aliases.
390 for (const GCCRegAlias &GRA : getGCCRegAliases())
391 for (const char *A : GRA.Aliases) {
402 TargetInfo::getNormalizedGCCRegisterName(StringRef Name) const {
403 assert(isValidGCCRegisterName(Name) && "Invalid register passed in");
405 // Get rid of any register prefix.
406 Name = removeGCCRegisterPrefix(Name);
408 ArrayRef<const char *> Names = getGCCRegNames();
410 // First, check if we have a number.
411 if (isDigit(Name[0])) {
413 if (!Name.getAsInteger(0, n)) {
414 assert(n < Names.size() && "Out of bounds register number!");
419 // Check any additional names that we have.
420 for (const AddlRegName &ARN : getGCCAddlRegNames())
421 for (const char *AN : ARN.Names) {
424 // Make sure the register that the additional name is for is within
425 // the bounds of the register names from above.
426 if (AN == Name && ARN.RegNum < Names.size())
430 // Now check aliases.
431 for (const GCCRegAlias &RA : getGCCRegAliases())
432 for (const char *A : RA.Aliases) {
442 bool TargetInfo::validateOutputConstraint(ConstraintInfo &Info) const {
443 const char *Name = Info.getConstraintStr().c_str();
444 // An output constraint must start with '=' or '+'
445 if (*Name != '=' && *Name != '+')
449 Info.setIsReadWrite();
455 if (!validateAsmConstraint(Name, Info)) {
456 // FIXME: We temporarily return false
457 // so we can add more constraints as we hit it.
458 // Eventually, an unknown constraint should just be treated as 'g'.
462 case '&': // early clobber.
463 Info.setEarlyClobber();
465 case '%': // commutative.
466 // FIXME: Check that there is a another register after this one.
468 case 'r': // general register.
469 Info.setAllowsRegister();
471 case 'm': // memory operand.
472 case 'o': // offsetable memory operand.
473 case 'V': // non-offsetable memory operand.
474 case '<': // autodecrement memory operand.
475 case '>': // autoincrement memory operand.
476 Info.setAllowsMemory();
478 case 'g': // general register, memory operand or immediate integer.
479 case 'X': // any operand.
480 Info.setAllowsRegister();
481 Info.setAllowsMemory();
483 case ',': // multiple alternative constraint. Pass it.
484 // Handle additional optional '=' or '+' modifiers.
485 if (Name[1] == '=' || Name[1] == '+')
488 case '#': // Ignore as constraint.
489 while (Name[1] && Name[1] != ',')
492 case '?': // Disparage slightly code.
493 case '!': // Disparage severely.
494 case '*': // Ignore for choosing register preferences.
501 // Early clobber with a read-write constraint which doesn't permit registers
503 if (Info.earlyClobber() && Info.isReadWrite() && !Info.allowsRegister())
506 // If a constraint allows neither memory nor register operands it contains
507 // only modifiers. Reject it.
508 return Info.allowsMemory() || Info.allowsRegister();
511 bool TargetInfo::resolveSymbolicName(const char *&Name,
512 ArrayRef<ConstraintInfo> OutputConstraints,
513 unsigned &Index) const {
514 assert(*Name == '[' && "Symbolic name did not start with '['");
516 const char *Start = Name;
517 while (*Name && *Name != ']')
525 std::string SymbolicName(Start, Name - Start);
527 for (Index = 0; Index != OutputConstraints.size(); ++Index)
528 if (SymbolicName == OutputConstraints[Index].getName())
534 bool TargetInfo::validateInputConstraint(
535 MutableArrayRef<ConstraintInfo> OutputConstraints,
536 ConstraintInfo &Info) const {
537 const char *Name = Info.ConstraintStr.c_str();
545 // Check if we have a matching constraint
546 if (*Name >= '0' && *Name <= '9') {
547 const char *DigitStart = Name;
548 while (Name[1] >= '0' && Name[1] <= '9')
550 const char *DigitEnd = Name;
552 if (StringRef(DigitStart, DigitEnd - DigitStart + 1)
553 .getAsInteger(10, i))
556 // Check if matching constraint is out of bounds.
557 if (i >= OutputConstraints.size()) return false;
559 // A number must refer to an output only operand.
560 if (OutputConstraints[i].isReadWrite())
563 // If the constraint is already tied, it must be tied to the
564 // same operand referenced to by the number.
565 if (Info.hasTiedOperand() && Info.getTiedOperand() != i)
568 // The constraint should have the same info as the respective
569 // output constraint.
570 Info.setTiedOperand(i, OutputConstraints[i]);
571 } else if (!validateAsmConstraint(Name, Info)) {
572 // FIXME: This error return is in place temporarily so we can
573 // add more constraints as we hit it. Eventually, an unknown
574 // constraint should just be treated as 'g'.
580 if (!resolveSymbolicName(Name, OutputConstraints, Index))
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() != Index)
588 // A number must refer to an output only operand.
589 if (OutputConstraints[Index].isReadWrite())
592 Info.setTiedOperand(Index, OutputConstraints[Index]);
595 case '%': // commutative
596 // FIXME: Fail if % is used with the last operand.
598 case 'i': // immediate integer.
599 case 'n': // immediate integer with a known value.
601 case 'I': // Various constant constraints with target-specific meanings.
609 if (!validateAsmConstraint(Name, Info))
612 case 'r': // general register.
613 Info.setAllowsRegister();
615 case 'm': // memory operand.
616 case 'o': // offsettable memory operand.
617 case 'V': // non-offsettable memory operand.
618 case '<': // autodecrement memory operand.
619 case '>': // autoincrement memory operand.
620 Info.setAllowsMemory();
622 case 'g': // general register, memory operand or immediate integer.
623 case 'X': // any operand.
624 Info.setAllowsRegister();
625 Info.setAllowsMemory();
627 case 'E': // immediate floating point.
628 case 'F': // immediate floating point.
629 case 'p': // address operand.
631 case ',': // multiple alternative constraint. Ignore comma.
633 case '#': // Ignore as constraint.
634 while (Name[1] && Name[1] != ',')
637 case '?': // Disparage slightly code.
638 case '!': // Disparage severely.
639 case '*': // Ignore for choosing register preferences.