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;
39 DefaultAlignForAttributeAligned = 128;
49 LargeArrayMinWidth = 0;
51 MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 0;
55 SizeType = UnsignedLong;
56 PtrDiffType = SignedLong;
57 IntMaxType = SignedLongLong;
58 IntPtrType = SignedLong;
59 WCharType = SignedInt;
61 Char16Type = UnsignedShort;
62 Char32Type = UnsignedInt;
63 Int64Type = SignedLongLong;
64 SigAtomicType = SignedInt;
65 ProcessIDType = SignedInt;
66 UseSignedCharForObjCBool = true;
67 UseBitFieldTypeAlignment = true;
68 UseZeroLengthBitfieldAlignment = false;
69 ZeroLengthBitfieldBoundary = 0;
70 HalfFormat = &llvm::APFloat::IEEEhalf;
71 FloatFormat = &llvm::APFloat::IEEEsingle;
72 DoubleFormat = &llvm::APFloat::IEEEdouble;
73 LongDoubleFormat = &llvm::APFloat::IEEEdouble;
74 DescriptionString = nullptr;
75 UserLabelPrefix = "_";
76 MCountName = "mcount";
79 HasAlignMac68kSupport = false;
81 // Default to no types using fpret.
82 RealTypeUsesObjCFPRet = 0;
84 // Default to not using fp2ret for __Complex long double
85 ComplexLongDoubleUsesFP2Ret = false;
87 // Set the C++ ABI based on the triple.
88 TheCXXABI.set(Triple.isKnownWindowsMSVCEnvironment()
89 ? TargetCXXABI::Microsoft
90 : TargetCXXABI::GenericItanium);
92 // Default to an empty address space map.
93 AddrSpaceMap = &DefaultAddrSpaceMap;
94 UseAddrSpaceMapMangling = false;
96 // Default to an unknown platform name.
97 PlatformName = "unknown";
98 PlatformMinVersion = VersionTuple();
101 // Out of line virtual dtor for TargetInfo.
102 TargetInfo::~TargetInfo() {}
104 /// getTypeName - Return the user string for the specified integer type enum.
105 /// For example, SignedShort -> "short".
106 const char *TargetInfo::getTypeName(IntType T) {
108 default: llvm_unreachable("not an integer!");
109 case SignedChar: return "signed char";
110 case UnsignedChar: return "unsigned char";
111 case SignedShort: return "short";
112 case UnsignedShort: return "unsigned short";
113 case SignedInt: return "int";
114 case UnsignedInt: return "unsigned int";
115 case SignedLong: return "long int";
116 case UnsignedLong: return "long unsigned int";
117 case SignedLongLong: return "long long int";
118 case UnsignedLongLong: return "long long unsigned int";
122 /// getTypeConstantSuffix - Return the constant suffix for the specified
123 /// integer type enum. For example, SignedLong -> "L".
124 const char *TargetInfo::getTypeConstantSuffix(IntType T) const {
126 default: llvm_unreachable("not an integer!");
129 case SignedInt: return "";
130 case SignedLong: return "L";
131 case SignedLongLong: return "LL";
133 if (getCharWidth() < getIntWidth())
136 if (getShortWidth() < getIntWidth())
138 case UnsignedInt: return "U";
139 case UnsignedLong: return "UL";
140 case UnsignedLongLong: return "ULL";
144 /// getTypeFormatModifier - Return the printf format modifier for the
145 /// specified integer type enum. For example, SignedLong -> "l".
147 const char *TargetInfo::getTypeFormatModifier(IntType T) {
149 default: llvm_unreachable("not an integer!");
151 case UnsignedChar: return "hh";
153 case UnsignedShort: return "h";
155 case UnsignedInt: return "";
157 case UnsignedLong: return "l";
159 case UnsignedLongLong: return "ll";
163 /// getTypeWidth - Return the width (in bits) of the specified integer type
164 /// enum. For example, SignedInt -> getIntWidth().
165 unsigned TargetInfo::getTypeWidth(IntType T) const {
167 default: llvm_unreachable("not an integer!");
169 case UnsignedChar: return getCharWidth();
171 case UnsignedShort: return getShortWidth();
173 case UnsignedInt: return getIntWidth();
175 case UnsignedLong: return getLongWidth();
177 case UnsignedLongLong: return getLongLongWidth();
181 TargetInfo::IntType TargetInfo::getIntTypeByWidth(
182 unsigned BitWidth, bool IsSigned) const {
183 if (getCharWidth() == BitWidth)
184 return IsSigned ? SignedChar : UnsignedChar;
185 if (getShortWidth() == BitWidth)
186 return IsSigned ? SignedShort : UnsignedShort;
187 if (getIntWidth() == BitWidth)
188 return IsSigned ? SignedInt : UnsignedInt;
189 if (getLongWidth() == BitWidth)
190 return IsSigned ? SignedLong : UnsignedLong;
191 if (getLongLongWidth() == BitWidth)
192 return IsSigned ? SignedLongLong : UnsignedLongLong;
196 TargetInfo::IntType TargetInfo::getLeastIntTypeByWidth(unsigned BitWidth,
197 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::RealType TargetInfo::getRealTypeByWidth(unsigned BitWidth) const {
212 if (getFloatWidth() == BitWidth)
214 if (getDoubleWidth() == BitWidth)
219 if (&getLongDoubleFormat() == &llvm::APFloat::x87DoubleExtended)
223 if (&getLongDoubleFormat() == &llvm::APFloat::PPCDoubleDouble ||
224 &getLongDoubleFormat() == &llvm::APFloat::IEEEquad)
232 /// getTypeAlign - Return the alignment (in bits) of the specified integer type
233 /// enum. For example, SignedInt -> getIntAlign().
234 unsigned TargetInfo::getTypeAlign(IntType T) const {
236 default: llvm_unreachable("not an integer!");
238 case UnsignedChar: return getCharAlign();
240 case UnsignedShort: return getShortAlign();
242 case UnsignedInt: return getIntAlign();
244 case UnsignedLong: return getLongAlign();
246 case UnsignedLongLong: return getLongLongAlign();
250 /// isTypeSigned - Return whether an integer types is signed. Returns true if
251 /// the type is signed; false otherwise.
252 bool TargetInfo::isTypeSigned(IntType T) {
254 default: llvm_unreachable("not an integer!");
265 case UnsignedLongLong:
270 /// adjust - Set forced language options.
271 /// Apply changes to the target information with respect to certain
272 /// language options which change the target configuration.
273 void TargetInfo::adjust(const LangOptions &Opts) {
274 if (Opts.NoBitFieldTypeAlign)
275 UseBitFieldTypeAlignment = false;
277 WCharType = UnsignedShort;
280 // OpenCL C requires specific widths for types, irrespective of
281 // what these normally are for the target.
282 // We also define long long and long double here, although the
283 // OpenCL standard only mentions these as "reserved".
284 IntWidth = IntAlign = 32;
285 LongWidth = LongAlign = 64;
286 LongLongWidth = LongLongAlign = 128;
287 HalfWidth = HalfAlign = 16;
288 FloatWidth = FloatAlign = 32;
290 // Embedded 32-bit targets (OpenCL EP) might have double C type
291 // defined as float. Let's not override this as it might lead
292 // to generating illegal code that uses 64bit doubles.
293 if (DoubleWidth != FloatWidth) {
294 DoubleWidth = DoubleAlign = 64;
295 DoubleFormat = &llvm::APFloat::IEEEdouble;
297 LongDoubleWidth = LongDoubleAlign = 128;
299 assert(PointerWidth == 32 || PointerWidth == 64);
300 bool Is32BitArch = PointerWidth == 32;
301 SizeType = Is32BitArch ? UnsignedInt : UnsignedLong;
302 PtrDiffType = Is32BitArch ? SignedInt : SignedLong;
303 IntPtrType = Is32BitArch ? SignedInt : SignedLong;
305 IntMaxType = SignedLongLong;
306 Int64Type = SignedLong;
308 HalfFormat = &llvm::APFloat::IEEEhalf;
309 FloatFormat = &llvm::APFloat::IEEEsingle;
310 LongDoubleFormat = &llvm::APFloat::IEEEquad;
314 //===----------------------------------------------------------------------===//
317 static StringRef removeGCCRegisterPrefix(StringRef Name) {
318 if (Name[0] == '%' || Name[0] == '#')
319 Name = Name.substr(1);
324 /// isValidClobber - Returns whether the passed in string is
325 /// a valid clobber in an inline asm statement. This is used by
327 bool TargetInfo::isValidClobber(StringRef Name) const {
328 return (isValidGCCRegisterName(Name) ||
329 Name == "memory" || Name == "cc");
332 /// isValidGCCRegisterName - Returns whether the passed in string
333 /// is a valid register name according to GCC. This is used by Sema for
334 /// inline asm statements.
335 bool TargetInfo::isValidGCCRegisterName(StringRef Name) const {
339 const char * const *Names;
342 // Get rid of any register prefix.
343 Name = removeGCCRegisterPrefix(Name);
347 getGCCRegNames(Names, NumNames);
349 // If we have a number it maps to an entry in the register name array.
350 if (isDigit(Name[0])) {
352 if (!Name.getAsInteger(0, n))
353 return n >= 0 && (unsigned)n < NumNames;
356 // Check register names.
357 for (unsigned i = 0; i < NumNames; i++) {
358 if (Name == Names[i])
362 // Check any additional names that we have.
363 const AddlRegName *AddlNames;
364 unsigned NumAddlNames;
365 getGCCAddlRegNames(AddlNames, NumAddlNames);
366 for (unsigned i = 0; i < NumAddlNames; i++)
367 for (unsigned j = 0; j < llvm::array_lengthof(AddlNames[i].Names); j++) {
368 if (!AddlNames[i].Names[j])
370 // Make sure the register that the additional name is for is within
371 // the bounds of the register names from above.
372 if (AddlNames[i].Names[j] == Name && AddlNames[i].RegNum < NumNames)
376 // Now check aliases.
377 const GCCRegAlias *Aliases;
380 getGCCRegAliases(Aliases, NumAliases);
381 for (unsigned i = 0; i < NumAliases; i++) {
382 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
383 if (!Aliases[i].Aliases[j])
385 if (Aliases[i].Aliases[j] == Name)
394 TargetInfo::getNormalizedGCCRegisterName(StringRef Name) const {
395 assert(isValidGCCRegisterName(Name) && "Invalid register passed in");
397 // Get rid of any register prefix.
398 Name = removeGCCRegisterPrefix(Name);
400 const char * const *Names;
403 getGCCRegNames(Names, NumNames);
405 // First, check if we have a number.
406 if (isDigit(Name[0])) {
408 if (!Name.getAsInteger(0, n)) {
409 assert(n >= 0 && (unsigned)n < NumNames &&
410 "Out of bounds register number!");
415 // Check any additional names that we have.
416 const AddlRegName *AddlNames;
417 unsigned NumAddlNames;
418 getGCCAddlRegNames(AddlNames, NumAddlNames);
419 for (unsigned i = 0; i < NumAddlNames; i++)
420 for (unsigned j = 0; j < llvm::array_lengthof(AddlNames[i].Names); j++) {
421 if (!AddlNames[i].Names[j])
423 // Make sure the register that the additional name is for is within
424 // the bounds of the register names from above.
425 if (AddlNames[i].Names[j] == Name && AddlNames[i].RegNum < NumNames)
429 // Now check aliases.
430 const GCCRegAlias *Aliases;
433 getGCCRegAliases(Aliases, NumAliases);
434 for (unsigned i = 0; i < NumAliases; i++) {
435 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
436 if (!Aliases[i].Aliases[j])
438 if (Aliases[i].Aliases[j] == Name)
439 return Aliases[i].Register;
446 bool TargetInfo::validateOutputConstraint(ConstraintInfo &Info) const {
447 const char *Name = Info.getConstraintStr().c_str();
448 // An output constraint must start with '=' or '+'
449 if (*Name != '=' && *Name != '+')
453 Info.setIsReadWrite();
459 if (!validateAsmConstraint(Name, Info)) {
460 // FIXME: We temporarily return false
461 // so we can add more constraints as we hit it.
462 // Eventually, an unknown constraint should just be treated as 'g'.
466 case '&': // early clobber.
467 Info.setEarlyClobber();
469 case '%': // commutative.
470 // FIXME: Check that there is a another register after this one.
472 case 'r': // general register.
473 Info.setAllowsRegister();
475 case 'm': // memory operand.
476 case 'o': // offsetable memory operand.
477 case 'V': // non-offsetable memory operand.
478 case '<': // autodecrement memory operand.
479 case '>': // autoincrement memory operand.
480 Info.setAllowsMemory();
482 case 'g': // general register, memory operand or immediate integer.
483 case 'X': // any operand.
484 Info.setAllowsRegister();
485 Info.setAllowsMemory();
487 case ',': // multiple alternative constraint. Pass it.
488 // Handle additional optional '=' or '+' modifiers.
489 if (Name[1] == '=' || Name[1] == '+')
492 case '#': // Ignore as constraint.
493 while (Name[1] && Name[1] != ',')
496 case '?': // Disparage slightly code.
497 case '!': // Disparage severely.
498 case '*': // Ignore for choosing register preferences.
505 // Early clobber with a read-write constraint which doesn't permit registers
507 if (Info.earlyClobber() && Info.isReadWrite() && !Info.allowsRegister())
510 // If a constraint allows neither memory nor register operands it contains
511 // only modifiers. Reject it.
512 return Info.allowsMemory() || Info.allowsRegister();
515 bool TargetInfo::resolveSymbolicName(const char *&Name,
516 ConstraintInfo *OutputConstraints,
518 unsigned &Index) const {
519 assert(*Name == '[' && "Symbolic name did not start with '['");
521 const char *Start = Name;
522 while (*Name && *Name != ']')
530 std::string SymbolicName(Start, Name - Start);
532 for (Index = 0; Index != NumOutputs; ++Index)
533 if (SymbolicName == OutputConstraints[Index].getName())
539 bool TargetInfo::validateInputConstraint(ConstraintInfo *OutputConstraints,
541 ConstraintInfo &Info) const {
542 const char *Name = Info.ConstraintStr.c_str();
550 // Check if we have a matching constraint
551 if (*Name >= '0' && *Name <= '9') {
552 const char *DigitStart = Name;
553 while (Name[1] >= '0' && Name[1] <= '9')
555 const char *DigitEnd = Name;
557 if (StringRef(DigitStart, DigitEnd - DigitStart + 1)
558 .getAsInteger(10, i))
561 // Check if matching constraint is out of bounds.
562 if (i >= NumOutputs) return false;
564 // A number must refer to an output only operand.
565 if (OutputConstraints[i].isReadWrite())
568 // If the constraint is already tied, it must be tied to the
569 // same operand referenced to by the number.
570 if (Info.hasTiedOperand() && Info.getTiedOperand() != i)
573 // The constraint should have the same info as the respective
574 // output constraint.
575 Info.setTiedOperand(i, OutputConstraints[i]);
576 } else if (!validateAsmConstraint(Name, Info)) {
577 // FIXME: This error return is in place temporarily so we can
578 // add more constraints as we hit it. Eventually, an unknown
579 // constraint should just be treated as 'g'.
585 if (!resolveSymbolicName(Name, OutputConstraints, NumOutputs, Index))
588 // If the constraint is already tied, it must be tied to the
589 // same operand referenced to by the number.
590 if (Info.hasTiedOperand() && Info.getTiedOperand() != Index)
593 // A number must refer to an output only operand.
594 if (OutputConstraints[Index].isReadWrite())
597 Info.setTiedOperand(Index, OutputConstraints[Index]);
600 case '%': // commutative
601 // FIXME: Fail if % is used with the last operand.
603 case 'i': // immediate integer.
604 case 'n': // immediate integer with a known value.
606 case 'I': // Various constant constraints with target-specific meanings.
614 if (!validateAsmConstraint(Name, Info))
617 case 'r': // general register.
618 Info.setAllowsRegister();
620 case 'm': // memory operand.
621 case 'o': // offsettable memory operand.
622 case 'V': // non-offsettable memory operand.
623 case '<': // autodecrement memory operand.
624 case '>': // autoincrement memory operand.
625 Info.setAllowsMemory();
627 case 'g': // general register, memory operand or immediate integer.
628 case 'X': // any operand.
629 Info.setAllowsRegister();
630 Info.setAllowsMemory();
632 case 'E': // immediate floating point.
633 case 'F': // immediate floating point.
634 case 'p': // address operand.
636 case ',': // multiple alternative constraint. Ignore comma.
638 case '#': // Ignore as constraint.
639 while (Name[1] && Name[1] != ',')
642 case '?': // Disparage slightly code.
643 case '!': // Disparage severely.
644 case '*': // Ignore for choosing register preferences.
654 bool TargetCXXABI::tryParse(llvm::StringRef name) {
655 const Kind unknown = static_cast<Kind>(-1);
656 Kind kind = llvm::StringSwitch<Kind>(name)
657 .Case("arm", GenericARM)
659 .Case("itanium", GenericItanium)
660 .Case("microsoft", Microsoft)
661 .Case("mips", GenericMIPS)
663 if (kind == unknown) return false;