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;
54 SizeType = UnsignedLong;
55 PtrDiffType = SignedLong;
56 IntMaxType = SignedLongLong;
57 IntPtrType = SignedLong;
58 WCharType = SignedInt;
60 Char16Type = UnsignedShort;
61 Char32Type = UnsignedInt;
62 Int64Type = SignedLongLong;
63 SigAtomicType = SignedInt;
64 ProcessIDType = SignedInt;
65 UseSignedCharForObjCBool = true;
66 UseBitFieldTypeAlignment = true;
67 UseZeroLengthBitfieldAlignment = false;
68 ZeroLengthBitfieldBoundary = 0;
69 HalfFormat = &llvm::APFloat::IEEEhalf;
70 FloatFormat = &llvm::APFloat::IEEEsingle;
71 DoubleFormat = &llvm::APFloat::IEEEdouble;
72 LongDoubleFormat = &llvm::APFloat::IEEEdouble;
73 DescriptionString = nullptr;
74 UserLabelPrefix = "_";
75 MCountName = "mcount";
78 HasAlignMac68kSupport = false;
80 // Default to no types using fpret.
81 RealTypeUsesObjCFPRet = 0;
83 // Default to not using fp2ret for __Complex long double
84 ComplexLongDoubleUsesFP2Ret = false;
86 // Set the C++ ABI based on the triple.
87 TheCXXABI.set(Triple.isKnownWindowsMSVCEnvironment()
88 ? TargetCXXABI::Microsoft
89 : TargetCXXABI::GenericItanium);
91 // Default to an empty address space map.
92 AddrSpaceMap = &DefaultAddrSpaceMap;
93 UseAddrSpaceMapMangling = false;
95 // Default to an unknown platform name.
96 PlatformName = "unknown";
97 PlatformMinVersion = VersionTuple();
100 // Out of line virtual dtor for TargetInfo.
101 TargetInfo::~TargetInfo() {}
103 /// getTypeName - Return the user string for the specified integer type enum.
104 /// For example, SignedShort -> "short".
105 const char *TargetInfo::getTypeName(IntType T) {
107 default: llvm_unreachable("not an integer!");
108 case SignedChar: return "signed char";
109 case UnsignedChar: return "unsigned char";
110 case SignedShort: return "short";
111 case UnsignedShort: return "unsigned short";
112 case SignedInt: return "int";
113 case UnsignedInt: return "unsigned int";
114 case SignedLong: return "long int";
115 case UnsignedLong: return "long unsigned int";
116 case SignedLongLong: return "long long int";
117 case UnsignedLongLong: return "long long unsigned int";
121 /// getTypeConstantSuffix - Return the constant suffix for the specified
122 /// integer type enum. For example, SignedLong -> "L".
123 const char *TargetInfo::getTypeConstantSuffix(IntType T) const {
125 default: llvm_unreachable("not an integer!");
128 case SignedInt: return "";
129 case SignedLong: return "L";
130 case SignedLongLong: return "LL";
132 if (getCharWidth() < getIntWidth())
135 if (getShortWidth() < getIntWidth())
137 case UnsignedInt: return "U";
138 case UnsignedLong: return "UL";
139 case UnsignedLongLong: return "ULL";
143 /// getTypeFormatModifier - Return the printf format modifier for the
144 /// specified integer type enum. For example, SignedLong -> "l".
146 const char *TargetInfo::getTypeFormatModifier(IntType T) {
148 default: llvm_unreachable("not an integer!");
150 case UnsignedChar: return "hh";
152 case UnsignedShort: return "h";
154 case UnsignedInt: return "";
156 case UnsignedLong: return "l";
158 case UnsignedLongLong: return "ll";
162 /// getTypeWidth - Return the width (in bits) of the specified integer type
163 /// enum. For example, SignedInt -> getIntWidth().
164 unsigned TargetInfo::getTypeWidth(IntType T) const {
166 default: llvm_unreachable("not an integer!");
168 case UnsignedChar: return getCharWidth();
170 case UnsignedShort: return getShortWidth();
172 case UnsignedInt: return getIntWidth();
174 case UnsignedLong: return getLongWidth();
176 case UnsignedLongLong: return getLongLongWidth();
180 TargetInfo::IntType TargetInfo::getIntTypeByWidth(
181 unsigned BitWidth, bool IsSigned) const {
182 if (getCharWidth() == BitWidth)
183 return IsSigned ? SignedChar : UnsignedChar;
184 if (getShortWidth() == BitWidth)
185 return IsSigned ? SignedShort : UnsignedShort;
186 if (getIntWidth() == BitWidth)
187 return IsSigned ? SignedInt : UnsignedInt;
188 if (getLongWidth() == BitWidth)
189 return IsSigned ? SignedLong : UnsignedLong;
190 if (getLongLongWidth() == BitWidth)
191 return IsSigned ? SignedLongLong : UnsignedLongLong;
195 TargetInfo::IntType TargetInfo::getLeastIntTypeByWidth(unsigned BitWidth,
196 bool IsSigned) const {
197 if (getCharWidth() >= BitWidth)
198 return IsSigned ? SignedChar : UnsignedChar;
199 if (getShortWidth() >= BitWidth)
200 return IsSigned ? SignedShort : UnsignedShort;
201 if (getIntWidth() >= BitWidth)
202 return IsSigned ? SignedInt : UnsignedInt;
203 if (getLongWidth() >= BitWidth)
204 return IsSigned ? SignedLong : UnsignedLong;
205 if (getLongLongWidth() >= BitWidth)
206 return IsSigned ? SignedLongLong : UnsignedLongLong;
210 TargetInfo::RealType TargetInfo::getRealTypeByWidth(unsigned BitWidth) const {
211 if (getFloatWidth() == BitWidth)
213 if (getDoubleWidth() == BitWidth)
218 if (&getLongDoubleFormat() == &llvm::APFloat::x87DoubleExtended)
222 if (&getLongDoubleFormat() == &llvm::APFloat::PPCDoubleDouble ||
223 &getLongDoubleFormat() == &llvm::APFloat::IEEEquad)
231 /// getTypeAlign - Return the alignment (in bits) of the specified integer type
232 /// enum. For example, SignedInt -> getIntAlign().
233 unsigned TargetInfo::getTypeAlign(IntType T) const {
235 default: llvm_unreachable("not an integer!");
237 case UnsignedChar: return getCharAlign();
239 case UnsignedShort: return getShortAlign();
241 case UnsignedInt: return getIntAlign();
243 case UnsignedLong: return getLongAlign();
245 case UnsignedLongLong: return getLongLongAlign();
249 /// isTypeSigned - Return whether an integer types is signed. Returns true if
250 /// the type is signed; false otherwise.
251 bool TargetInfo::isTypeSigned(IntType T) {
253 default: llvm_unreachable("not an integer!");
264 case UnsignedLongLong:
269 /// adjust - Set forced language options.
270 /// Apply changes to the target information with respect to certain
271 /// language options which change the target configuration.
272 void TargetInfo::adjust(const LangOptions &Opts) {
273 if (Opts.NoBitFieldTypeAlign)
274 UseBitFieldTypeAlignment = false;
276 WCharType = UnsignedShort;
279 // OpenCL C requires specific widths for types, irrespective of
280 // what these normally are for the target.
281 // We also define long long and long double here, although the
282 // OpenCL standard only mentions these as "reserved".
283 IntWidth = IntAlign = 32;
284 LongWidth = LongAlign = 64;
285 LongLongWidth = LongLongAlign = 128;
286 HalfWidth = HalfAlign = 16;
287 FloatWidth = FloatAlign = 32;
289 // Embedded 32-bit targets (OpenCL EP) might have double C type
290 // defined as float. Let's not override this as it might lead
291 // to generating illegal code that uses 64bit doubles.
292 if (DoubleWidth != FloatWidth) {
293 DoubleWidth = DoubleAlign = 64;
294 DoubleFormat = &llvm::APFloat::IEEEdouble;
296 LongDoubleWidth = LongDoubleAlign = 128;
298 assert(PointerWidth == 32 || PointerWidth == 64);
299 bool Is32BitArch = PointerWidth == 32;
300 SizeType = Is32BitArch ? UnsignedInt : UnsignedLong;
301 PtrDiffType = Is32BitArch ? SignedInt : SignedLong;
302 IntPtrType = Is32BitArch ? SignedInt : SignedLong;
304 IntMaxType = SignedLongLong;
305 Int64Type = SignedLong;
307 HalfFormat = &llvm::APFloat::IEEEhalf;
308 FloatFormat = &llvm::APFloat::IEEEsingle;
309 LongDoubleFormat = &llvm::APFloat::IEEEquad;
313 //===----------------------------------------------------------------------===//
316 static StringRef removeGCCRegisterPrefix(StringRef Name) {
317 if (Name[0] == '%' || Name[0] == '#')
318 Name = Name.substr(1);
323 /// isValidClobber - Returns whether the passed in string is
324 /// a valid clobber in an inline asm statement. This is used by
326 bool TargetInfo::isValidClobber(StringRef Name) const {
327 return (isValidGCCRegisterName(Name) ||
328 Name == "memory" || Name == "cc");
331 /// isValidGCCRegisterName - Returns whether the passed in string
332 /// is a valid register name according to GCC. This is used by Sema for
333 /// inline asm statements.
334 bool TargetInfo::isValidGCCRegisterName(StringRef Name) const {
338 const char * const *Names;
341 // Get rid of any register prefix.
342 Name = removeGCCRegisterPrefix(Name);
346 getGCCRegNames(Names, NumNames);
348 // If we have a number it maps to an entry in the register name array.
349 if (isDigit(Name[0])) {
351 if (!Name.getAsInteger(0, n))
352 return n >= 0 && (unsigned)n < NumNames;
355 // Check register names.
356 for (unsigned i = 0; i < NumNames; i++) {
357 if (Name == Names[i])
361 // Check any additional names that we have.
362 const AddlRegName *AddlNames;
363 unsigned NumAddlNames;
364 getGCCAddlRegNames(AddlNames, NumAddlNames);
365 for (unsigned i = 0; i < NumAddlNames; i++)
366 for (unsigned j = 0; j < llvm::array_lengthof(AddlNames[i].Names); j++) {
367 if (!AddlNames[i].Names[j])
369 // Make sure the register that the additional name is for is within
370 // the bounds of the register names from above.
371 if (AddlNames[i].Names[j] == Name && AddlNames[i].RegNum < NumNames)
375 // Now check aliases.
376 const GCCRegAlias *Aliases;
379 getGCCRegAliases(Aliases, NumAliases);
380 for (unsigned i = 0; i < NumAliases; i++) {
381 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
382 if (!Aliases[i].Aliases[j])
384 if (Aliases[i].Aliases[j] == Name)
393 TargetInfo::getNormalizedGCCRegisterName(StringRef Name) const {
394 assert(isValidGCCRegisterName(Name) && "Invalid register passed in");
396 // Get rid of any register prefix.
397 Name = removeGCCRegisterPrefix(Name);
399 const char * const *Names;
402 getGCCRegNames(Names, NumNames);
404 // First, check if we have a number.
405 if (isDigit(Name[0])) {
407 if (!Name.getAsInteger(0, n)) {
408 assert(n >= 0 && (unsigned)n < NumNames &&
409 "Out of bounds register number!");
414 // Check any additional names that we have.
415 const AddlRegName *AddlNames;
416 unsigned NumAddlNames;
417 getGCCAddlRegNames(AddlNames, NumAddlNames);
418 for (unsigned i = 0; i < NumAddlNames; i++)
419 for (unsigned j = 0; j < llvm::array_lengthof(AddlNames[i].Names); j++) {
420 if (!AddlNames[i].Names[j])
422 // Make sure the register that the additional name is for is within
423 // the bounds of the register names from above.
424 if (AddlNames[i].Names[j] == Name && AddlNames[i].RegNum < NumNames)
428 // Now check aliases.
429 const GCCRegAlias *Aliases;
432 getGCCRegAliases(Aliases, NumAliases);
433 for (unsigned i = 0; i < NumAliases; i++) {
434 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
435 if (!Aliases[i].Aliases[j])
437 if (Aliases[i].Aliases[j] == Name)
438 return Aliases[i].Register;
445 bool TargetInfo::validateOutputConstraint(ConstraintInfo &Info) const {
446 const char *Name = Info.getConstraintStr().c_str();
447 // An output constraint must start with '=' or '+'
448 if (*Name != '=' && *Name != '+')
452 Info.setIsReadWrite();
458 if (!validateAsmConstraint(Name, Info)) {
459 // FIXME: We temporarily return false
460 // so we can add more constraints as we hit it.
461 // Eventually, an unknown constraint should just be treated as 'g'.
465 case '&': // early clobber.
466 Info.setEarlyClobber();
468 case '%': // commutative.
469 // FIXME: Check that there is a another register after this one.
471 case 'r': // general register.
472 Info.setAllowsRegister();
474 case 'm': // memory operand.
475 case 'o': // offsetable memory operand.
476 case 'V': // non-offsetable memory operand.
477 case '<': // autodecrement memory operand.
478 case '>': // autoincrement memory operand.
479 Info.setAllowsMemory();
481 case 'g': // general register, memory operand or immediate integer.
482 case 'X': // any operand.
483 Info.setAllowsRegister();
484 Info.setAllowsMemory();
486 case ',': // multiple alternative constraint. Pass it.
487 // Handle additional optional '=' or '+' modifiers.
488 if (Name[1] == '=' || Name[1] == '+')
491 case '#': // Ignore as constraint.
492 while (Name[1] && Name[1] != ',')
495 case '?': // Disparage slightly code.
496 case '!': // Disparage severely.
497 case '*': // Ignore for choosing register preferences.
504 // Early clobber with a read-write constraint which doesn't permit registers
506 if (Info.earlyClobber() && Info.isReadWrite() && !Info.allowsRegister())
509 // If a constraint allows neither memory nor register operands it contains
510 // only modifiers. Reject it.
511 return Info.allowsMemory() || Info.allowsRegister();
514 bool TargetInfo::resolveSymbolicName(const char *&Name,
515 ConstraintInfo *OutputConstraints,
517 unsigned &Index) const {
518 assert(*Name == '[' && "Symbolic name did not start with '['");
520 const char *Start = Name;
521 while (*Name && *Name != ']')
529 std::string SymbolicName(Start, Name - Start);
531 for (Index = 0; Index != NumOutputs; ++Index)
532 if (SymbolicName == OutputConstraints[Index].getName())
538 bool TargetInfo::validateInputConstraint(ConstraintInfo *OutputConstraints,
540 ConstraintInfo &Info) const {
541 const char *Name = Info.ConstraintStr.c_str();
549 // Check if we have a matching constraint
550 if (*Name >= '0' && *Name <= '9') {
551 const char *DigitStart = Name;
552 while (Name[1] >= '0' && Name[1] <= '9')
554 const char *DigitEnd = Name;
556 if (StringRef(DigitStart, DigitEnd - DigitStart + 1)
557 .getAsInteger(10, i))
560 // Check if matching constraint is out of bounds.
561 if (i >= NumOutputs) return false;
563 // A number must refer to an output only operand.
564 if (OutputConstraints[i].isReadWrite())
567 // If the constraint is already tied, it must be tied to the
568 // same operand referenced to by the number.
569 if (Info.hasTiedOperand() && Info.getTiedOperand() != i)
572 // The constraint should have the same info as the respective
573 // output constraint.
574 Info.setTiedOperand(i, OutputConstraints[i]);
575 } else if (!validateAsmConstraint(Name, Info)) {
576 // FIXME: This error return is in place temporarily so we can
577 // add more constraints as we hit it. Eventually, an unknown
578 // constraint should just be treated as 'g'.
584 if (!resolveSymbolicName(Name, OutputConstraints, NumOutputs, Index))
587 // If the constraint is already tied, it must be tied to the
588 // same operand referenced to by the number.
589 if (Info.hasTiedOperand() && Info.getTiedOperand() != Index)
592 // A number must refer to an output only operand.
593 if (OutputConstraints[Index].isReadWrite())
596 Info.setTiedOperand(Index, OutputConstraints[Index]);
599 case '%': // commutative
600 // FIXME: Fail if % is used with the last operand.
602 case 'i': // immediate integer.
603 case 'n': // immediate integer with a known value.
605 case 'I': // Various constant constraints with target-specific meanings.
613 if (!validateAsmConstraint(Name, Info))
616 case 'r': // general register.
617 Info.setAllowsRegister();
619 case 'm': // memory operand.
620 case 'o': // offsettable memory operand.
621 case 'V': // non-offsettable memory operand.
622 case '<': // autodecrement memory operand.
623 case '>': // autoincrement memory operand.
624 Info.setAllowsMemory();
626 case 'g': // general register, memory operand or immediate integer.
627 case 'X': // any operand.
628 Info.setAllowsRegister();
629 Info.setAllowsMemory();
631 case 'E': // immediate floating point.
632 case 'F': // immediate floating point.
633 case 'p': // address operand.
635 case ',': // multiple alternative constraint. Ignore comma.
637 case '#': // Ignore as constraint.
638 while (Name[1] && Name[1] != ',')
641 case '?': // Disparage slightly code.
642 case '!': // Disparage severely.
643 case '*': // Ignore for choosing register preferences.
653 bool TargetCXXABI::tryParse(llvm::StringRef name) {
654 const Kind unknown = static_cast<Kind>(-1);
655 Kind kind = llvm::StringSwitch<Kind>(name)
656 .Case("arm", GenericARM)
658 .Case("itanium", GenericItanium)
659 .Case("microsoft", Microsoft)
660 .Case("mips", GenericMIPS)
662 if (kind == unknown) return false;