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;
53 SizeType = UnsignedLong;
54 PtrDiffType = SignedLong;
55 IntMaxType = SignedLongLong;
56 IntPtrType = SignedLong;
57 WCharType = SignedInt;
59 Char16Type = UnsignedShort;
60 Char32Type = UnsignedInt;
61 Int64Type = SignedLongLong;
62 SigAtomicType = SignedInt;
63 ProcessIDType = SignedInt;
64 UseSignedCharForObjCBool = true;
65 UseBitFieldTypeAlignment = true;
66 UseZeroLengthBitfieldAlignment = false;
67 ZeroLengthBitfieldBoundary = 0;
68 HalfFormat = &llvm::APFloat::IEEEhalf;
69 FloatFormat = &llvm::APFloat::IEEEsingle;
70 DoubleFormat = &llvm::APFloat::IEEEdouble;
71 LongDoubleFormat = &llvm::APFloat::IEEEdouble;
72 DescriptionString = nullptr;
73 UserLabelPrefix = "_";
74 MCountName = "mcount";
77 HasAlignMac68kSupport = false;
79 // Default to no types using fpret.
80 RealTypeUsesObjCFPRet = 0;
82 // Default to not using fp2ret for __Complex long double
83 ComplexLongDoubleUsesFP2Ret = false;
85 // Set the C++ ABI based on the triple.
86 TheCXXABI.set(Triple.isKnownWindowsMSVCEnvironment()
87 ? TargetCXXABI::Microsoft
88 : TargetCXXABI::GenericItanium);
90 // Default to an empty address space map.
91 AddrSpaceMap = &DefaultAddrSpaceMap;
92 UseAddrSpaceMapMangling = false;
94 // Default to an unknown platform name.
95 PlatformName = "unknown";
96 PlatformMinVersion = VersionTuple();
99 // Out of line virtual dtor for TargetInfo.
100 TargetInfo::~TargetInfo() {}
102 /// getTypeName - Return the user string for the specified integer type enum.
103 /// For example, SignedShort -> "short".
104 const char *TargetInfo::getTypeName(IntType T) {
106 default: llvm_unreachable("not an integer!");
107 case SignedChar: return "signed char";
108 case UnsignedChar: return "unsigned char";
109 case SignedShort: return "short";
110 case UnsignedShort: return "unsigned short";
111 case SignedInt: return "int";
112 case UnsignedInt: return "unsigned int";
113 case SignedLong: return "long int";
114 case UnsignedLong: return "long unsigned int";
115 case SignedLongLong: return "long long int";
116 case UnsignedLongLong: return "long long unsigned int";
120 /// getTypeConstantSuffix - Return the constant suffix for the specified
121 /// integer type enum. For example, SignedLong -> "L".
122 const char *TargetInfo::getTypeConstantSuffix(IntType T) const {
124 default: llvm_unreachable("not an integer!");
127 case SignedInt: return "";
128 case SignedLong: return "L";
129 case SignedLongLong: return "LL";
131 if (getCharWidth() < getIntWidth())
134 if (getShortWidth() < getIntWidth())
136 case UnsignedInt: return "U";
137 case UnsignedLong: return "UL";
138 case UnsignedLongLong: return "ULL";
142 /// getTypeFormatModifier - Return the printf format modifier for the
143 /// specified integer type enum. For example, SignedLong -> "l".
145 const char *TargetInfo::getTypeFormatModifier(IntType T) {
147 default: llvm_unreachable("not an integer!");
149 case UnsignedChar: return "hh";
151 case UnsignedShort: return "h";
153 case UnsignedInt: return "";
155 case UnsignedLong: return "l";
157 case UnsignedLongLong: return "ll";
161 /// getTypeWidth - Return the width (in bits) of the specified integer type
162 /// enum. For example, SignedInt -> getIntWidth().
163 unsigned TargetInfo::getTypeWidth(IntType T) const {
165 default: llvm_unreachable("not an integer!");
167 case UnsignedChar: return getCharWidth();
169 case UnsignedShort: return getShortWidth();
171 case UnsignedInt: return getIntWidth();
173 case UnsignedLong: return getLongWidth();
175 case UnsignedLongLong: return getLongLongWidth();
179 TargetInfo::IntType TargetInfo::getIntTypeByWidth(
180 unsigned BitWidth, bool IsSigned) const {
181 if (getCharWidth() == BitWidth)
182 return IsSigned ? SignedChar : UnsignedChar;
183 if (getShortWidth() == BitWidth)
184 return IsSigned ? SignedShort : UnsignedShort;
185 if (getIntWidth() == BitWidth)
186 return IsSigned ? SignedInt : UnsignedInt;
187 if (getLongWidth() == BitWidth)
188 return IsSigned ? SignedLong : UnsignedLong;
189 if (getLongLongWidth() == BitWidth)
190 return IsSigned ? SignedLongLong : UnsignedLongLong;
194 TargetInfo::IntType TargetInfo::getLeastIntTypeByWidth(unsigned BitWidth,
195 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::RealType TargetInfo::getRealTypeByWidth(unsigned BitWidth) const {
210 if (getFloatWidth() == BitWidth)
212 if (getDoubleWidth() == BitWidth)
217 if (&getLongDoubleFormat() == &llvm::APFloat::x87DoubleExtended)
221 if (&getLongDoubleFormat() == &llvm::APFloat::PPCDoubleDouble ||
222 &getLongDoubleFormat() == &llvm::APFloat::IEEEquad)
230 /// getTypeAlign - Return the alignment (in bits) of the specified integer type
231 /// enum. For example, SignedInt -> getIntAlign().
232 unsigned TargetInfo::getTypeAlign(IntType T) const {
234 default: llvm_unreachable("not an integer!");
236 case UnsignedChar: return getCharAlign();
238 case UnsignedShort: return getShortAlign();
240 case UnsignedInt: return getIntAlign();
242 case UnsignedLong: return getLongAlign();
244 case UnsignedLongLong: return getLongLongAlign();
248 /// isTypeSigned - Return whether an integer types is signed. Returns true if
249 /// the type is signed; false otherwise.
250 bool TargetInfo::isTypeSigned(IntType T) {
252 default: llvm_unreachable("not an integer!");
263 case UnsignedLongLong:
268 /// adjust - Set forced language options.
269 /// Apply changes to the target information with respect to certain
270 /// language options which change the target configuration.
271 void TargetInfo::adjust(const LangOptions &Opts) {
272 if (Opts.NoBitFieldTypeAlign)
273 UseBitFieldTypeAlignment = false;
275 WCharType = UnsignedShort;
278 // OpenCL C requires specific widths for types, irrespective of
279 // what these normally are for the target.
280 // We also define long long and long double here, although the
281 // OpenCL standard only mentions these as "reserved".
282 IntWidth = IntAlign = 32;
283 LongWidth = LongAlign = 64;
284 LongLongWidth = LongLongAlign = 128;
285 HalfWidth = HalfAlign = 16;
286 FloatWidth = FloatAlign = 32;
288 // Embedded 32-bit targets (OpenCL EP) might have double C type
289 // defined as float. Let's not override this as it might lead
290 // to generating illegal code that uses 64bit doubles.
291 if (DoubleWidth != FloatWidth) {
292 DoubleWidth = DoubleAlign = 64;
293 DoubleFormat = &llvm::APFloat::IEEEdouble;
295 LongDoubleWidth = LongDoubleAlign = 128;
297 assert(PointerWidth == 32 || PointerWidth == 64);
298 bool Is32BitArch = PointerWidth == 32;
299 SizeType = Is32BitArch ? UnsignedInt : UnsignedLong;
300 PtrDiffType = Is32BitArch ? SignedInt : SignedLong;
301 IntPtrType = Is32BitArch ? SignedInt : SignedLong;
303 IntMaxType = SignedLongLong;
304 Int64Type = SignedLong;
306 HalfFormat = &llvm::APFloat::IEEEhalf;
307 FloatFormat = &llvm::APFloat::IEEEsingle;
308 LongDoubleFormat = &llvm::APFloat::IEEEquad;
312 //===----------------------------------------------------------------------===//
315 static StringRef removeGCCRegisterPrefix(StringRef Name) {
316 if (Name[0] == '%' || Name[0] == '#')
317 Name = Name.substr(1);
322 /// isValidClobber - Returns whether the passed in string is
323 /// a valid clobber in an inline asm statement. This is used by
325 bool TargetInfo::isValidClobber(StringRef Name) const {
326 return (isValidGCCRegisterName(Name) ||
327 Name == "memory" || Name == "cc");
330 /// isValidGCCRegisterName - Returns whether the passed in string
331 /// is a valid register name according to GCC. This is used by Sema for
332 /// inline asm statements.
333 bool TargetInfo::isValidGCCRegisterName(StringRef Name) const {
337 const char * const *Names;
340 // Get rid of any register prefix.
341 Name = removeGCCRegisterPrefix(Name);
345 getGCCRegNames(Names, NumNames);
347 // If we have a number it maps to an entry in the register name array.
348 if (isDigit(Name[0])) {
350 if (!Name.getAsInteger(0, n))
351 return n >= 0 && (unsigned)n < NumNames;
354 // Check register names.
355 for (unsigned i = 0; i < NumNames; i++) {
356 if (Name == Names[i])
360 // Check any additional names that we have.
361 const AddlRegName *AddlNames;
362 unsigned NumAddlNames;
363 getGCCAddlRegNames(AddlNames, NumAddlNames);
364 for (unsigned i = 0; i < NumAddlNames; i++)
365 for (unsigned j = 0; j < llvm::array_lengthof(AddlNames[i].Names); j++) {
366 if (!AddlNames[i].Names[j])
368 // Make sure the register that the additional name is for is within
369 // the bounds of the register names from above.
370 if (AddlNames[i].Names[j] == Name && AddlNames[i].RegNum < NumNames)
374 // Now check aliases.
375 const GCCRegAlias *Aliases;
378 getGCCRegAliases(Aliases, NumAliases);
379 for (unsigned i = 0; i < NumAliases; i++) {
380 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
381 if (!Aliases[i].Aliases[j])
383 if (Aliases[i].Aliases[j] == Name)
392 TargetInfo::getNormalizedGCCRegisterName(StringRef Name) const {
393 assert(isValidGCCRegisterName(Name) && "Invalid register passed in");
395 // Get rid of any register prefix.
396 Name = removeGCCRegisterPrefix(Name);
398 const char * const *Names;
401 getGCCRegNames(Names, NumNames);
403 // First, check if we have a number.
404 if (isDigit(Name[0])) {
406 if (!Name.getAsInteger(0, n)) {
407 assert(n >= 0 && (unsigned)n < NumNames &&
408 "Out of bounds register number!");
413 // Check any additional names that we have.
414 const AddlRegName *AddlNames;
415 unsigned NumAddlNames;
416 getGCCAddlRegNames(AddlNames, NumAddlNames);
417 for (unsigned i = 0; i < NumAddlNames; i++)
418 for (unsigned j = 0; j < llvm::array_lengthof(AddlNames[i].Names); j++) {
419 if (!AddlNames[i].Names[j])
421 // Make sure the register that the additional name is for is within
422 // the bounds of the register names from above.
423 if (AddlNames[i].Names[j] == Name && AddlNames[i].RegNum < NumNames)
427 // Now check aliases.
428 const GCCRegAlias *Aliases;
431 getGCCRegAliases(Aliases, NumAliases);
432 for (unsigned i = 0; i < NumAliases; i++) {
433 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
434 if (!Aliases[i].Aliases[j])
436 if (Aliases[i].Aliases[j] == Name)
437 return Aliases[i].Register;
444 bool TargetInfo::validateOutputConstraint(ConstraintInfo &Info) const {
445 const char *Name = Info.getConstraintStr().c_str();
446 // An output constraint must start with '=' or '+'
447 if (*Name != '=' && *Name != '+')
451 Info.setIsReadWrite();
457 if (!validateAsmConstraint(Name, Info)) {
458 // FIXME: We temporarily return false
459 // so we can add more constraints as we hit it.
460 // Eventually, an unknown constraint should just be treated as 'g'.
464 case '&': // early clobber.
465 Info.setEarlyClobber();
467 case '%': // commutative.
468 // FIXME: Check that there is a another register after this one.
470 case 'r': // general register.
471 Info.setAllowsRegister();
473 case 'm': // memory operand.
474 case 'o': // offsetable memory operand.
475 case 'V': // non-offsetable memory operand.
476 case '<': // autodecrement memory operand.
477 case '>': // autoincrement memory operand.
478 Info.setAllowsMemory();
480 case 'g': // general register, memory operand or immediate integer.
481 case 'X': // any operand.
482 Info.setAllowsRegister();
483 Info.setAllowsMemory();
485 case ',': // multiple alternative constraint. Pass it.
486 // Handle additional optional '=' or '+' modifiers.
487 if (Name[1] == '=' || Name[1] == '+')
490 case '#': // Ignore as constraint.
491 while (Name[1] && Name[1] != ',')
494 case '?': // Disparage slightly code.
495 case '!': // Disparage severely.
496 case '*': // Ignore for choosing register preferences.
503 // Early clobber with a read-write constraint which doesn't permit registers
505 if (Info.earlyClobber() && Info.isReadWrite() && !Info.allowsRegister())
508 // If a constraint allows neither memory nor register operands it contains
509 // only modifiers. Reject it.
510 return Info.allowsMemory() || Info.allowsRegister();
513 bool TargetInfo::resolveSymbolicName(const char *&Name,
514 ConstraintInfo *OutputConstraints,
516 unsigned &Index) const {
517 assert(*Name == '[' && "Symbolic name did not start with '['");
519 const char *Start = Name;
520 while (*Name && *Name != ']')
528 std::string SymbolicName(Start, Name - Start);
530 for (Index = 0; Index != NumOutputs; ++Index)
531 if (SymbolicName == OutputConstraints[Index].getName())
537 bool TargetInfo::validateInputConstraint(ConstraintInfo *OutputConstraints,
539 ConstraintInfo &Info) const {
540 const char *Name = Info.ConstraintStr.c_str();
548 // Check if we have a matching constraint
549 if (*Name >= '0' && *Name <= '9') {
550 const char *DigitStart = Name;
551 while (Name[1] >= '0' && Name[1] <= '9')
553 const char *DigitEnd = Name;
555 if (StringRef(DigitStart, DigitEnd - DigitStart + 1)
556 .getAsInteger(10, i))
559 // Check if matching constraint is out of bounds.
560 if (i >= NumOutputs) return false;
562 // A number must refer to an output only operand.
563 if (OutputConstraints[i].isReadWrite())
566 // If the constraint is already tied, it must be tied to the
567 // same operand referenced to by the number.
568 if (Info.hasTiedOperand() && Info.getTiedOperand() != i)
571 // The constraint should have the same info as the respective
572 // output constraint.
573 Info.setTiedOperand(i, OutputConstraints[i]);
574 } else if (!validateAsmConstraint(Name, Info)) {
575 // FIXME: This error return is in place temporarily so we can
576 // add more constraints as we hit it. Eventually, an unknown
577 // constraint should just be treated as 'g'.
583 if (!resolveSymbolicName(Name, OutputConstraints, NumOutputs, Index))
586 // If the constraint is already tied, it must be tied to the
587 // same operand referenced to by the number.
588 if (Info.hasTiedOperand() && Info.getTiedOperand() != Index)
591 // A number must refer to an output only operand.
592 if (OutputConstraints[Index].isReadWrite())
595 Info.setTiedOperand(Index, OutputConstraints[Index]);
598 case '%': // commutative
599 // FIXME: Fail if % is used with the last operand.
601 case 'i': // immediate integer.
602 case 'n': // immediate integer with a known value.
604 case 'I': // Various constant constraints with target-specific meanings.
612 if (!validateAsmConstraint(Name, Info))
615 case 'r': // general register.
616 Info.setAllowsRegister();
618 case 'm': // memory operand.
619 case 'o': // offsettable memory operand.
620 case 'V': // non-offsettable memory operand.
621 case '<': // autodecrement memory operand.
622 case '>': // autoincrement memory operand.
623 Info.setAllowsMemory();
625 case 'g': // general register, memory operand or immediate integer.
626 case 'X': // any operand.
627 Info.setAllowsRegister();
628 Info.setAllowsMemory();
630 case 'E': // immediate floating point.
631 case 'F': // immediate floating point.
632 case 'p': // address operand.
634 case ',': // multiple alternative constraint. Ignore comma.
636 case '#': // Ignore as constraint.
637 while (Name[1] && Name[1] != ',')
640 case '?': // Disparage slightly code.
641 case '!': // Disparage severely.
642 case '*': // Ignore for choosing register preferences.
652 bool TargetCXXABI::tryParse(llvm::StringRef name) {
653 const Kind unknown = static_cast<Kind>(-1);
654 Kind kind = llvm::StringSwitch<Kind>(name)
655 .Case("arm", GenericARM)
657 .Case("itanium", GenericItanium)
658 .Case("microsoft", Microsoft)
659 .Case("mips", GenericMIPS)
661 if (kind == unknown) return false;