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/AddressSpaces.h"
15 #include "clang/Basic/TargetInfo.h"
16 #include "clang/Basic/LangOptions.h"
17 #include "llvm/ADT/APFloat.h"
18 #include "llvm/ADT/STLExtras.h"
21 using namespace clang;
23 static const LangAS::Map DefaultAddrSpaceMap = { 0 };
25 // TargetInfo Constructor.
26 TargetInfo::TargetInfo(const std::string &T) : Triple(T) {
27 // Set defaults. Defaults are set for a 32-bit RISC platform, like PPC or
28 // SPARC. These should be overridden by concrete targets as needed.
30 NoAsmVariants = false;
31 PointerWidth = PointerAlign = 32;
32 BoolWidth = BoolAlign = 8;
33 IntWidth = IntAlign = 32;
34 LongWidth = LongAlign = 32;
35 LongLongWidth = LongLongAlign = 64;
42 LargeArrayMinWidth = 0;
44 SizeType = UnsignedLong;
45 PtrDiffType = SignedLong;
46 IntMaxType = SignedLongLong;
47 UIntMaxType = UnsignedLongLong;
48 IntPtrType = SignedLong;
49 WCharType = SignedInt;
51 Char16Type = UnsignedShort;
52 Char32Type = UnsignedInt;
53 Int64Type = SignedLongLong;
54 SigAtomicType = SignedInt;
55 UseBitFieldTypeAlignment = true;
56 FloatFormat = &llvm::APFloat::IEEEsingle;
57 DoubleFormat = &llvm::APFloat::IEEEdouble;
58 LongDoubleFormat = &llvm::APFloat::IEEEdouble;
59 DescriptionString = "E-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
60 "i64:64:64-f32:32:32-f64:64:64-n32";
61 UserLabelPrefix = "_";
62 MCountName = "mcount";
63 HasAlignMac68kSupport = false;
65 // Default to no types using fpret.
66 RealTypeUsesObjCFPRet = 0;
68 // Default to using the Itanium ABI.
69 CXXABI = CXXABI_Itanium;
71 // Default to an empty address space map.
72 AddrSpaceMap = &DefaultAddrSpaceMap;
74 // Default to an unknown platform name.
75 PlatformName = "unknown";
76 PlatformMinVersion = VersionTuple();
79 // Out of line virtual dtor for TargetInfo.
80 TargetInfo::~TargetInfo() {}
82 /// getTypeName - Return the user string for the specified integer type enum.
83 /// For example, SignedShort -> "short".
84 const char *TargetInfo::getTypeName(IntType T) {
86 default: assert(0 && "not an integer!");
87 case SignedShort: return "short";
88 case UnsignedShort: return "unsigned short";
89 case SignedInt: return "int";
90 case UnsignedInt: return "unsigned int";
91 case SignedLong: return "long int";
92 case UnsignedLong: return "long unsigned int";
93 case SignedLongLong: return "long long int";
94 case UnsignedLongLong: return "long long unsigned int";
98 /// getTypeConstantSuffix - Return the constant suffix for the specified
99 /// integer type enum. For example, SignedLong -> "L".
100 const char *TargetInfo::getTypeConstantSuffix(IntType T) {
102 default: assert(0 && "not an integer!");
104 case SignedInt: return "";
105 case SignedLong: return "L";
106 case SignedLongLong: return "LL";
108 case UnsignedInt: return "U";
109 case UnsignedLong: return "UL";
110 case UnsignedLongLong: return "ULL";
114 /// getTypeWidth - Return the width (in bits) of the specified integer type
115 /// enum. For example, SignedInt -> getIntWidth().
116 unsigned TargetInfo::getTypeWidth(IntType T) const {
118 default: assert(0 && "not an integer!");
120 case UnsignedShort: return getShortWidth();
122 case UnsignedInt: return getIntWidth();
124 case UnsignedLong: return getLongWidth();
126 case UnsignedLongLong: return getLongLongWidth();
130 /// getTypeAlign - Return the alignment (in bits) of the specified integer type
131 /// enum. For example, SignedInt -> getIntAlign().
132 unsigned TargetInfo::getTypeAlign(IntType T) const {
134 default: assert(0 && "not an integer!");
136 case UnsignedShort: return getShortAlign();
138 case UnsignedInt: return getIntAlign();
140 case UnsignedLong: return getLongAlign();
142 case UnsignedLongLong: return getLongLongAlign();
146 /// isTypeSigned - Return whether an integer types is signed. Returns true if
147 /// the type is signed; false otherwise.
148 bool TargetInfo::isTypeSigned(IntType T) {
150 default: assert(0 && "not an integer!");
159 case UnsignedLongLong:
164 /// setForcedLangOptions - Set forced language options.
165 /// Apply changes to the target information with respect to certain
166 /// language options which change the target configuration.
167 void TargetInfo::setForcedLangOptions(LangOptions &Opts) {
168 if (Opts.NoBitFieldTypeAlign)
169 UseBitFieldTypeAlignment = false;
171 WCharType = UnsignedShort;
174 //===----------------------------------------------------------------------===//
177 static llvm::StringRef removeGCCRegisterPrefix(llvm::StringRef Name) {
178 if (Name[0] == '%' || Name[0] == '#')
179 Name = Name.substr(1);
184 /// isValidGCCRegisterName - Returns whether the passed in string
185 /// is a valid register name according to GCC. This is used by Sema for
186 /// inline asm statements.
187 bool TargetInfo::isValidGCCRegisterName(llvm::StringRef Name) const {
191 const char * const *Names;
194 // Get rid of any register prefix.
195 Name = removeGCCRegisterPrefix(Name);
197 if (Name == "memory" || Name == "cc")
200 getGCCRegNames(Names, NumNames);
202 // If we have a number it maps to an entry in the register name array.
203 if (isdigit(Name[0])) {
205 if (!Name.getAsInteger(0, n))
206 return n >= 0 && (unsigned)n < NumNames;
209 // Check register names.
210 for (unsigned i = 0; i < NumNames; i++) {
211 if (Name == Names[i])
215 // Now check aliases.
216 const GCCRegAlias *Aliases;
219 getGCCRegAliases(Aliases, NumAliases);
220 for (unsigned i = 0; i < NumAliases; i++) {
221 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
222 if (!Aliases[i].Aliases[j])
224 if (Aliases[i].Aliases[j] == Name)
233 TargetInfo::getNormalizedGCCRegisterName(llvm::StringRef Name) const {
234 assert(isValidGCCRegisterName(Name) && "Invalid register passed in");
236 // Get rid of any register prefix.
237 Name = removeGCCRegisterPrefix(Name);
239 const char * const *Names;
242 getGCCRegNames(Names, NumNames);
244 // First, check if we have a number.
245 if (isdigit(Name[0])) {
247 if (!Name.getAsInteger(0, n)) {
248 assert(n >= 0 && (unsigned)n < NumNames &&
249 "Out of bounds register number!");
254 // Now check aliases.
255 const GCCRegAlias *Aliases;
258 getGCCRegAliases(Aliases, NumAliases);
259 for (unsigned i = 0; i < NumAliases; i++) {
260 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
261 if (!Aliases[i].Aliases[j])
263 if (Aliases[i].Aliases[j] == Name)
264 return Aliases[i].Register;
271 bool TargetInfo::validateOutputConstraint(ConstraintInfo &Info) const {
272 const char *Name = Info.getConstraintStr().c_str();
273 // An output constraint must start with '=' or '+'
274 if (*Name != '=' && *Name != '+')
278 Info.setIsReadWrite();
284 if (!validateAsmConstraint(Name, Info)) {
285 // FIXME: We temporarily return false
286 // so we can add more constraints as we hit it.
287 // Eventually, an unknown constraint should just be treated as 'g'.
290 case '&': // early clobber.
292 case '%': // commutative.
293 // FIXME: Check that there is a another register after this one.
295 case 'r': // general register.
296 Info.setAllowsRegister();
298 case 'm': // memory operand.
299 case 'o': // offsetable memory operand.
300 case 'V': // non-offsetable memory operand.
301 case '<': // autodecrement memory operand.
302 case '>': // autoincrement memory operand.
303 Info.setAllowsMemory();
305 case 'g': // general register, memory operand or immediate integer.
306 case 'X': // any operand.
307 Info.setAllowsRegister();
308 Info.setAllowsMemory();
310 case ',': // multiple alternative constraint. Pass it.
311 // Handle additional optional '=' or '+' modifiers.
312 if (Name[1] == '=' || Name[1] == '+')
315 case '?': // Disparage slightly code.
316 case '!': // Disparage severely.
326 bool TargetInfo::resolveSymbolicName(const char *&Name,
327 ConstraintInfo *OutputConstraints,
329 unsigned &Index) const {
330 assert(*Name == '[' && "Symbolic name did not start with '['");
332 const char *Start = Name;
333 while (*Name && *Name != ']')
341 std::string SymbolicName(Start, Name - Start);
343 for (Index = 0; Index != NumOutputs; ++Index)
344 if (SymbolicName == OutputConstraints[Index].getName())
350 bool TargetInfo::validateInputConstraint(ConstraintInfo *OutputConstraints,
352 ConstraintInfo &Info) const {
353 const char *Name = Info.ConstraintStr.c_str();
358 // Check if we have a matching constraint
359 if (*Name >= '0' && *Name <= '9') {
360 unsigned i = *Name - '0';
362 // Check if matching constraint is out of bounds.
366 // A number must refer to an output only operand.
367 if (OutputConstraints[i].isReadWrite())
370 // If the constraint is already tied, it must be tied to the
371 // same operand referenced to by the number.
372 if (Info.hasTiedOperand() && Info.getTiedOperand() != i)
375 // The constraint should have the same info as the respective
376 // output constraint.
377 Info.setTiedOperand(i, OutputConstraints[i]);
378 } else if (!validateAsmConstraint(Name, Info)) {
379 // FIXME: This error return is in place temporarily so we can
380 // add more constraints as we hit it. Eventually, an unknown
381 // constraint should just be treated as 'g'.
387 if (!resolveSymbolicName(Name, OutputConstraints, NumOutputs, Index))
390 // If the constraint is already tied, it must be tied to the
391 // same operand referenced to by the number.
392 if (Info.hasTiedOperand() && Info.getTiedOperand() != Index)
395 Info.setTiedOperand(Index, OutputConstraints[Index]);
398 case '%': // commutative
399 // FIXME: Fail if % is used with the last operand.
401 case 'i': // immediate integer.
402 case 'n': // immediate integer with a known value.
404 case 'I': // Various constant constraints with target-specific meanings.
413 case 'r': // general register.
414 Info.setAllowsRegister();
416 case 'm': // memory operand.
417 case 'o': // offsettable memory operand.
418 case 'V': // non-offsettable memory operand.
419 case '<': // autodecrement memory operand.
420 case '>': // autoincrement memory operand.
421 Info.setAllowsMemory();
423 case 'g': // general register, memory operand or immediate integer.
424 case 'X': // any operand.
425 Info.setAllowsRegister();
426 Info.setAllowsMemory();
428 case 'E': // immediate floating point.
429 case 'F': // immediate floating point.
430 case 'p': // address operand.
432 case ',': // multiple alternative constraint. Ignore comma.
434 case '?': // Disparage slightly code.
435 case '!': // Disparage severely.