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;
33 PointerWidth = PointerAlign = 32;
34 BoolWidth = BoolAlign = 8;
35 IntWidth = IntAlign = 32;
36 LongWidth = LongAlign = 32;
37 LongLongWidth = LongLongAlign = 64;
48 LargeArrayMinWidth = 0;
50 MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 0;
52 SizeType = UnsignedLong;
53 PtrDiffType = SignedLong;
54 IntMaxType = SignedLongLong;
55 IntPtrType = SignedLong;
56 WCharType = SignedInt;
58 Char16Type = UnsignedShort;
59 Char32Type = UnsignedInt;
60 Int64Type = SignedLongLong;
61 SigAtomicType = SignedInt;
62 ProcessIDType = SignedInt;
63 UseSignedCharForObjCBool = true;
64 UseBitFieldTypeAlignment = true;
65 UseZeroLengthBitfieldAlignment = false;
66 ZeroLengthBitfieldBoundary = 0;
67 HalfFormat = &llvm::APFloat::IEEEhalf;
68 FloatFormat = &llvm::APFloat::IEEEsingle;
69 DoubleFormat = &llvm::APFloat::IEEEdouble;
70 LongDoubleFormat = &llvm::APFloat::IEEEdouble;
71 DescriptionString = nullptr;
72 UserLabelPrefix = "_";
73 MCountName = "mcount";
76 HasAlignMac68kSupport = false;
78 // Default to no types using fpret.
79 RealTypeUsesObjCFPRet = 0;
81 // Default to not using fp2ret for __Complex long double
82 ComplexLongDoubleUsesFP2Ret = false;
84 // Set the C++ ABI based on the triple.
85 TheCXXABI.set(Triple.isKnownWindowsMSVCEnvironment()
86 ? TargetCXXABI::Microsoft
87 : TargetCXXABI::GenericItanium);
89 // Default to an empty address space map.
90 AddrSpaceMap = &DefaultAddrSpaceMap;
91 UseAddrSpaceMapMangling = false;
93 // Default to an unknown platform name.
94 PlatformName = "unknown";
95 PlatformMinVersion = VersionTuple();
98 // Out of line virtual dtor for TargetInfo.
99 TargetInfo::~TargetInfo() {}
101 /// getTypeName - Return the user string for the specified integer type enum.
102 /// For example, SignedShort -> "short".
103 const char *TargetInfo::getTypeName(IntType T) {
105 default: llvm_unreachable("not an integer!");
106 case SignedChar: return "signed char";
107 case UnsignedChar: return "unsigned char";
108 case SignedShort: return "short";
109 case UnsignedShort: return "unsigned short";
110 case SignedInt: return "int";
111 case UnsignedInt: return "unsigned int";
112 case SignedLong: return "long int";
113 case UnsignedLong: return "long unsigned int";
114 case SignedLongLong: return "long long int";
115 case UnsignedLongLong: return "long long unsigned int";
119 /// getTypeConstantSuffix - Return the constant suffix for the specified
120 /// integer type enum. For example, SignedLong -> "L".
121 const char *TargetInfo::getTypeConstantSuffix(IntType T) const {
123 default: llvm_unreachable("not an integer!");
126 case SignedInt: return "";
127 case SignedLong: return "L";
128 case SignedLongLong: return "LL";
130 if (getCharWidth() < getIntWidth())
133 if (getShortWidth() < getIntWidth())
135 case UnsignedInt: return "U";
136 case UnsignedLong: return "UL";
137 case UnsignedLongLong: return "ULL";
141 /// getTypeFormatModifier - Return the printf format modifier for the
142 /// specified integer type enum. For example, SignedLong -> "l".
144 const char *TargetInfo::getTypeFormatModifier(IntType T) {
146 default: llvm_unreachable("not an integer!");
148 case UnsignedChar: return "hh";
150 case UnsignedShort: return "h";
152 case UnsignedInt: return "";
154 case UnsignedLong: return "l";
156 case UnsignedLongLong: return "ll";
160 /// getTypeWidth - Return the width (in bits) of the specified integer type
161 /// enum. For example, SignedInt -> getIntWidth().
162 unsigned TargetInfo::getTypeWidth(IntType T) const {
164 default: llvm_unreachable("not an integer!");
166 case UnsignedChar: return getCharWidth();
168 case UnsignedShort: return getShortWidth();
170 case UnsignedInt: return getIntWidth();
172 case UnsignedLong: return getLongWidth();
174 case UnsignedLongLong: return getLongLongWidth();
178 TargetInfo::IntType TargetInfo::getIntTypeByWidth(
179 unsigned BitWidth, bool IsSigned) const {
180 if (getCharWidth() == BitWidth)
181 return IsSigned ? SignedChar : UnsignedChar;
182 if (getShortWidth() == BitWidth)
183 return IsSigned ? SignedShort : UnsignedShort;
184 if (getIntWidth() == BitWidth)
185 return IsSigned ? SignedInt : UnsignedInt;
186 if (getLongWidth() == BitWidth)
187 return IsSigned ? SignedLong : UnsignedLong;
188 if (getLongLongWidth() == BitWidth)
189 return IsSigned ? SignedLongLong : UnsignedLongLong;
193 TargetInfo::IntType TargetInfo::getLeastIntTypeByWidth(unsigned BitWidth,
194 bool IsSigned) const {
195 if (getCharWidth() >= BitWidth)
196 return IsSigned ? SignedChar : UnsignedChar;
197 if (getShortWidth() >= BitWidth)
198 return IsSigned ? SignedShort : UnsignedShort;
199 if (getIntWidth() >= BitWidth)
200 return IsSigned ? SignedInt : UnsignedInt;
201 if (getLongWidth() >= BitWidth)
202 return IsSigned ? SignedLong : UnsignedLong;
203 if (getLongLongWidth() >= BitWidth)
204 return IsSigned ? SignedLongLong : UnsignedLongLong;
208 TargetInfo::RealType TargetInfo::getRealTypeByWidth(unsigned BitWidth) const {
209 if (getFloatWidth() == BitWidth)
211 if (getDoubleWidth() == BitWidth)
216 if (&getLongDoubleFormat() == &llvm::APFloat::x87DoubleExtended)
220 if (&getLongDoubleFormat() == &llvm::APFloat::PPCDoubleDouble ||
221 &getLongDoubleFormat() == &llvm::APFloat::IEEEquad)
229 /// getTypeAlign - Return the alignment (in bits) of the specified integer type
230 /// enum. For example, SignedInt -> getIntAlign().
231 unsigned TargetInfo::getTypeAlign(IntType T) const {
233 default: llvm_unreachable("not an integer!");
235 case UnsignedChar: return getCharAlign();
237 case UnsignedShort: return getShortAlign();
239 case UnsignedInt: return getIntAlign();
241 case UnsignedLong: return getLongAlign();
243 case UnsignedLongLong: return getLongLongAlign();
247 /// isTypeSigned - Return whether an integer types is signed. Returns true if
248 /// the type is signed; false otherwise.
249 bool TargetInfo::isTypeSigned(IntType T) {
251 default: llvm_unreachable("not an integer!");
262 case UnsignedLongLong:
267 /// adjust - Set forced language options.
268 /// Apply changes to the target information with respect to certain
269 /// language options which change the target configuration.
270 void TargetInfo::adjust(const LangOptions &Opts) {
271 if (Opts.NoBitFieldTypeAlign)
272 UseBitFieldTypeAlignment = false;
274 WCharType = UnsignedShort;
277 // OpenCL C requires specific widths for types, irrespective of
278 // what these normally are for the target.
279 // We also define long long and long double here, although the
280 // OpenCL standard only mentions these as "reserved".
281 IntWidth = IntAlign = 32;
282 LongWidth = LongAlign = 64;
283 LongLongWidth = LongLongAlign = 128;
284 HalfWidth = HalfAlign = 16;
285 FloatWidth = FloatAlign = 32;
287 // Embedded 32-bit targets (OpenCL EP) might have double C type
288 // defined as float. Let's not override this as it might lead
289 // to generating illegal code that uses 64bit doubles.
290 if (DoubleWidth != FloatWidth) {
291 DoubleWidth = DoubleAlign = 64;
292 DoubleFormat = &llvm::APFloat::IEEEdouble;
294 LongDoubleWidth = LongDoubleAlign = 128;
296 assert(PointerWidth == 32 || PointerWidth == 64);
297 bool Is32BitArch = PointerWidth == 32;
298 SizeType = Is32BitArch ? UnsignedInt : UnsignedLong;
299 PtrDiffType = Is32BitArch ? SignedInt : SignedLong;
300 IntPtrType = Is32BitArch ? SignedInt : SignedLong;
302 IntMaxType = SignedLongLong;
303 Int64Type = SignedLong;
305 HalfFormat = &llvm::APFloat::IEEEhalf;
306 FloatFormat = &llvm::APFloat::IEEEsingle;
307 LongDoubleFormat = &llvm::APFloat::IEEEquad;
311 //===----------------------------------------------------------------------===//
314 static StringRef removeGCCRegisterPrefix(StringRef Name) {
315 if (Name[0] == '%' || Name[0] == '#')
316 Name = Name.substr(1);
321 /// isValidClobber - Returns whether the passed in string is
322 /// a valid clobber in an inline asm statement. This is used by
324 bool TargetInfo::isValidClobber(StringRef Name) const {
325 return (isValidGCCRegisterName(Name) ||
326 Name == "memory" || Name == "cc");
329 /// isValidGCCRegisterName - Returns whether the passed in string
330 /// is a valid register name according to GCC. This is used by Sema for
331 /// inline asm statements.
332 bool TargetInfo::isValidGCCRegisterName(StringRef Name) const {
336 const char * const *Names;
339 // Get rid of any register prefix.
340 Name = removeGCCRegisterPrefix(Name);
344 getGCCRegNames(Names, NumNames);
346 // If we have a number it maps to an entry in the register name array.
347 if (isDigit(Name[0])) {
349 if (!Name.getAsInteger(0, n))
350 return n >= 0 && (unsigned)n < NumNames;
353 // Check register names.
354 for (unsigned i = 0; i < NumNames; i++) {
355 if (Name == Names[i])
359 // Check any additional names that we have.
360 const AddlRegName *AddlNames;
361 unsigned NumAddlNames;
362 getGCCAddlRegNames(AddlNames, NumAddlNames);
363 for (unsigned i = 0; i < NumAddlNames; i++)
364 for (unsigned j = 0; j < llvm::array_lengthof(AddlNames[i].Names); j++) {
365 if (!AddlNames[i].Names[j])
367 // Make sure the register that the additional name is for is within
368 // the bounds of the register names from above.
369 if (AddlNames[i].Names[j] == Name && AddlNames[i].RegNum < NumNames)
373 // Now check aliases.
374 const GCCRegAlias *Aliases;
377 getGCCRegAliases(Aliases, NumAliases);
378 for (unsigned i = 0; i < NumAliases; i++) {
379 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
380 if (!Aliases[i].Aliases[j])
382 if (Aliases[i].Aliases[j] == Name)
391 TargetInfo::getNormalizedGCCRegisterName(StringRef Name) const {
392 assert(isValidGCCRegisterName(Name) && "Invalid register passed in");
394 // Get rid of any register prefix.
395 Name = removeGCCRegisterPrefix(Name);
397 const char * const *Names;
400 getGCCRegNames(Names, NumNames);
402 // First, check if we have a number.
403 if (isDigit(Name[0])) {
405 if (!Name.getAsInteger(0, n)) {
406 assert(n >= 0 && (unsigned)n < NumNames &&
407 "Out of bounds register number!");
412 // Check any additional names that we have.
413 const AddlRegName *AddlNames;
414 unsigned NumAddlNames;
415 getGCCAddlRegNames(AddlNames, NumAddlNames);
416 for (unsigned i = 0; i < NumAddlNames; i++)
417 for (unsigned j = 0; j < llvm::array_lengthof(AddlNames[i].Names); j++) {
418 if (!AddlNames[i].Names[j])
420 // Make sure the register that the additional name is for is within
421 // the bounds of the register names from above.
422 if (AddlNames[i].Names[j] == Name && AddlNames[i].RegNum < NumNames)
426 // Now check aliases.
427 const GCCRegAlias *Aliases;
430 getGCCRegAliases(Aliases, NumAliases);
431 for (unsigned i = 0; i < NumAliases; i++) {
432 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
433 if (!Aliases[i].Aliases[j])
435 if (Aliases[i].Aliases[j] == Name)
436 return Aliases[i].Register;
443 bool TargetInfo::validateOutputConstraint(ConstraintInfo &Info) const {
444 const char *Name = Info.getConstraintStr().c_str();
445 // An output constraint must start with '=' or '+'
446 if (*Name != '=' && *Name != '+')
450 Info.setIsReadWrite();
456 if (!validateAsmConstraint(Name, Info)) {
457 // FIXME: We temporarily return false
458 // so we can add more constraints as we hit it.
459 // Eventually, an unknown constraint should just be treated as 'g'.
463 case '&': // early clobber.
464 Info.setEarlyClobber();
466 case '%': // commutative.
467 // FIXME: Check that there is a another register after this one.
469 case 'r': // general register.
470 Info.setAllowsRegister();
472 case 'm': // memory operand.
473 case 'o': // offsetable memory operand.
474 case 'V': // non-offsetable memory operand.
475 case '<': // autodecrement memory operand.
476 case '>': // autoincrement memory operand.
477 Info.setAllowsMemory();
479 case 'g': // general register, memory operand or immediate integer.
480 case 'X': // any operand.
481 Info.setAllowsRegister();
482 Info.setAllowsMemory();
484 case ',': // multiple alternative constraint. Pass it.
485 // Handle additional optional '=' or '+' modifiers.
486 if (Name[1] == '=' || Name[1] == '+')
489 case '#': // Ignore as constraint.
490 while (Name[1] && Name[1] != ',')
493 case '?': // Disparage slightly code.
494 case '!': // Disparage severely.
495 case '*': // Ignore for choosing register preferences.
502 // Early clobber with a read-write constraint which doesn't permit registers
504 if (Info.earlyClobber() && Info.isReadWrite() && !Info.allowsRegister())
507 // If a constraint allows neither memory nor register operands it contains
508 // only modifiers. Reject it.
509 return Info.allowsMemory() || Info.allowsRegister();
512 bool TargetInfo::resolveSymbolicName(const char *&Name,
513 ConstraintInfo *OutputConstraints,
515 unsigned &Index) const {
516 assert(*Name == '[' && "Symbolic name did not start with '['");
518 const char *Start = Name;
519 while (*Name && *Name != ']')
527 std::string SymbolicName(Start, Name - Start);
529 for (Index = 0; Index != NumOutputs; ++Index)
530 if (SymbolicName == OutputConstraints[Index].getName())
536 bool TargetInfo::validateInputConstraint(ConstraintInfo *OutputConstraints,
538 ConstraintInfo &Info) const {
539 const char *Name = Info.ConstraintStr.c_str();
547 // Check if we have a matching constraint
548 if (*Name >= '0' && *Name <= '9') {
549 const char *DigitStart = Name;
550 while (Name[1] >= '0' && Name[1] <= '9')
552 const char *DigitEnd = Name;
554 if (StringRef(DigitStart, DigitEnd - DigitStart + 1)
555 .getAsInteger(10, i))
558 // Check if matching constraint is out of bounds.
559 if (i >= NumOutputs) return false;
561 // A number must refer to an output only operand.
562 if (OutputConstraints[i].isReadWrite())
565 // If the constraint is already tied, it must be tied to the
566 // same operand referenced to by the number.
567 if (Info.hasTiedOperand() && Info.getTiedOperand() != i)
570 // The constraint should have the same info as the respective
571 // output constraint.
572 Info.setTiedOperand(i, OutputConstraints[i]);
573 } else if (!validateAsmConstraint(Name, Info)) {
574 // FIXME: This error return is in place temporarily so we can
575 // add more constraints as we hit it. Eventually, an unknown
576 // constraint should just be treated as 'g'.
582 if (!resolveSymbolicName(Name, OutputConstraints, NumOutputs, Index))
585 // If the constraint is already tied, it must be tied to the
586 // same operand referenced to by the number.
587 if (Info.hasTiedOperand() && Info.getTiedOperand() != Index)
590 // A number must refer to an output only operand.
591 if (OutputConstraints[Index].isReadWrite())
594 Info.setTiedOperand(Index, OutputConstraints[Index]);
597 case '%': // commutative
598 // FIXME: Fail if % is used with the last operand.
600 case 'i': // immediate integer.
601 case 'n': // immediate integer with a known value.
603 case 'I': // Various constant constraints with target-specific meanings.
611 if (!validateAsmConstraint(Name, Info))
614 case 'r': // general register.
615 Info.setAllowsRegister();
617 case 'm': // memory operand.
618 case 'o': // offsettable memory operand.
619 case 'V': // non-offsettable memory operand.
620 case '<': // autodecrement memory operand.
621 case '>': // autoincrement memory operand.
622 Info.setAllowsMemory();
624 case 'g': // general register, memory operand or immediate integer.
625 case 'X': // any operand.
626 Info.setAllowsRegister();
627 Info.setAllowsMemory();
629 case 'E': // immediate floating point.
630 case 'F': // immediate floating point.
631 case 'p': // address operand.
633 case ',': // multiple alternative constraint. Ignore comma.
635 case '#': // Ignore as constraint.
636 while (Name[1] && Name[1] != ',')
639 case '?': // Disparage slightly code.
640 case '!': // Disparage severely.
641 case '*': // Ignore for choosing register preferences.
651 bool TargetCXXABI::tryParse(llvm::StringRef name) {
652 const Kind unknown = static_cast<Kind>(-1);
653 Kind kind = llvm::StringSwitch<Kind>(name)
654 .Case("arm", GenericARM)
656 .Case("itanium", GenericItanium)
657 .Case("microsoft", Microsoft)
659 if (kind == unknown) return false;