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/LangOptions.h"
16 #include "llvm/ADT/APFloat.h"
17 #include "llvm/ADT/STLExtras.h"
20 using namespace clang;
22 // TargetInfo Constructor.
23 TargetInfo::TargetInfo(const std::string &T) : Triple(T) {
24 // Set defaults. Defaults are set for a 32-bit RISC platform, like PPC or
25 // SPARC. These should be overridden by concrete targets as needed.
27 NoAsmVariants = false;
28 PointerWidth = PointerAlign = 32;
29 BoolWidth = BoolAlign = 8;
30 IntWidth = IntAlign = 32;
31 LongWidth = LongAlign = 32;
32 LongLongWidth = LongLongAlign = 64;
39 LargeArrayMinWidth = 0;
41 SizeType = UnsignedLong;
42 PtrDiffType = SignedLong;
43 IntMaxType = SignedLongLong;
44 UIntMaxType = UnsignedLongLong;
45 IntPtrType = SignedLong;
46 WCharType = SignedInt;
48 Char16Type = UnsignedShort;
49 Char32Type = UnsignedInt;
50 Int64Type = SignedLongLong;
51 SigAtomicType = SignedInt;
52 UseBitFieldTypeAlignment = true;
53 FloatFormat = &llvm::APFloat::IEEEsingle;
54 DoubleFormat = &llvm::APFloat::IEEEdouble;
55 LongDoubleFormat = &llvm::APFloat::IEEEdouble;
56 DescriptionString = "E-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
57 "i64:64:64-f32:32:32-f64:64:64-n32";
58 UserLabelPrefix = "_";
59 MCountName = "mcount";
60 HasAlignMac68kSupport = false;
62 // Default to no types using fpret.
63 RealTypeUsesObjCFPRet = 0;
65 // Default to using the Itanium ABI.
66 CXXABI = CXXABI_Itanium;
69 // Out of line virtual dtor for TargetInfo.
70 TargetInfo::~TargetInfo() {}
72 /// getTypeName - Return the user string for the specified integer type enum.
73 /// For example, SignedShort -> "short".
74 const char *TargetInfo::getTypeName(IntType T) {
76 default: assert(0 && "not an integer!");
77 case SignedShort: return "short";
78 case UnsignedShort: return "unsigned short";
79 case SignedInt: return "int";
80 case UnsignedInt: return "unsigned int";
81 case SignedLong: return "long int";
82 case UnsignedLong: return "long unsigned int";
83 case SignedLongLong: return "long long int";
84 case UnsignedLongLong: return "long long unsigned int";
88 /// getTypeConstantSuffix - Return the constant suffix for the specified
89 /// integer type enum. For example, SignedLong -> "L".
90 const char *TargetInfo::getTypeConstantSuffix(IntType T) {
92 default: assert(0 && "not an integer!");
94 case SignedInt: return "";
95 case SignedLong: return "L";
96 case SignedLongLong: return "LL";
98 case UnsignedInt: return "U";
99 case UnsignedLong: return "UL";
100 case UnsignedLongLong: return "ULL";
104 /// getTypeWidth - Return the width (in bits) of the specified integer type
105 /// enum. For example, SignedInt -> getIntWidth().
106 unsigned TargetInfo::getTypeWidth(IntType T) const {
108 default: assert(0 && "not an integer!");
110 case UnsignedShort: return getShortWidth();
112 case UnsignedInt: return getIntWidth();
114 case UnsignedLong: return getLongWidth();
116 case UnsignedLongLong: return getLongLongWidth();
120 /// getTypeAlign - Return the alignment (in bits) of the specified integer type
121 /// enum. For example, SignedInt -> getIntAlign().
122 unsigned TargetInfo::getTypeAlign(IntType T) const {
124 default: assert(0 && "not an integer!");
126 case UnsignedShort: return getShortAlign();
128 case UnsignedInt: return getIntAlign();
130 case UnsignedLong: return getLongAlign();
132 case UnsignedLongLong: return getLongLongAlign();
136 /// isTypeSigned - Return whether an integer types is signed. Returns true if
137 /// the type is signed; false otherwise.
138 bool TargetInfo::isTypeSigned(IntType T) {
140 default: assert(0 && "not an integer!");
149 case UnsignedLongLong:
154 /// setForcedLangOptions - Set forced language options.
155 /// Apply changes to the target information with respect to certain
156 /// language options which change the target configuration.
157 void TargetInfo::setForcedLangOptions(LangOptions &Opts) {
158 if (Opts.NoBitFieldTypeAlign)
159 UseBitFieldTypeAlignment = false;
161 WCharType = UnsignedShort;
164 //===----------------------------------------------------------------------===//
167 static llvm::StringRef removeGCCRegisterPrefix(llvm::StringRef Name) {
168 if (Name[0] == '%' || Name[0] == '#')
169 Name = Name.substr(1);
174 /// isValidGCCRegisterName - Returns whether the passed in string
175 /// is a valid register name according to GCC. This is used by Sema for
176 /// inline asm statements.
177 bool TargetInfo::isValidGCCRegisterName(llvm::StringRef Name) const {
181 const char * const *Names;
184 // Get rid of any register prefix.
185 Name = removeGCCRegisterPrefix(Name);
187 if (Name == "memory" || Name == "cc")
190 getGCCRegNames(Names, NumNames);
192 // If we have a number it maps to an entry in the register name array.
193 if (isdigit(Name[0])) {
195 if (!Name.getAsInteger(0, n))
196 return n >= 0 && (unsigned)n < NumNames;
199 // Check register names.
200 for (unsigned i = 0; i < NumNames; i++) {
201 if (Name == Names[i])
205 // Now check aliases.
206 const GCCRegAlias *Aliases;
209 getGCCRegAliases(Aliases, NumAliases);
210 for (unsigned i = 0; i < NumAliases; i++) {
211 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
212 if (!Aliases[i].Aliases[j])
214 if (Aliases[i].Aliases[j] == Name)
223 TargetInfo::getNormalizedGCCRegisterName(llvm::StringRef Name) const {
224 assert(isValidGCCRegisterName(Name) && "Invalid register passed in");
226 // Get rid of any register prefix.
227 Name = removeGCCRegisterPrefix(Name);
229 const char * const *Names;
232 getGCCRegNames(Names, NumNames);
234 // First, check if we have a number.
235 if (isdigit(Name[0])) {
237 if (!Name.getAsInteger(0, n)) {
238 assert(n >= 0 && (unsigned)n < NumNames &&
239 "Out of bounds register number!");
244 // Now check aliases.
245 const GCCRegAlias *Aliases;
248 getGCCRegAliases(Aliases, NumAliases);
249 for (unsigned i = 0; i < NumAliases; i++) {
250 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
251 if (!Aliases[i].Aliases[j])
253 if (Aliases[i].Aliases[j] == Name)
254 return Aliases[i].Register;
261 bool TargetInfo::validateOutputConstraint(ConstraintInfo &Info) const {
262 const char *Name = Info.getConstraintStr().c_str();
263 // An output constraint must start with '=' or '+'
264 if (*Name != '=' && *Name != '+')
268 Info.setIsReadWrite();
274 if (!validateAsmConstraint(Name, Info)) {
275 // FIXME: We temporarily return false
276 // so we can add more constraints as we hit it.
277 // Eventually, an unknown constraint should just be treated as 'g'.
280 case '&': // early clobber.
282 case '%': // commutative.
283 // FIXME: Check that there is a another register after this one.
285 case 'r': // general register.
286 Info.setAllowsRegister();
288 case 'm': // memory operand.
289 case 'o': // offsetable memory operand.
290 case 'V': // non-offsetable memory operand.
291 case '<': // autodecrement memory operand.
292 case '>': // autoincrement memory operand.
293 Info.setAllowsMemory();
295 case 'g': // general register, memory operand or immediate integer.
296 case 'X': // any operand.
297 Info.setAllowsRegister();
298 Info.setAllowsMemory();
300 case ',': // multiple alternative constraint. Pass it.
301 // Handle additional optional '=' or '+' modifiers.
302 if (Name[1] == '=' || Name[1] == '+')
305 case '?': // Disparage slightly code.
306 case '!': // Disparage severely.
316 bool TargetInfo::resolveSymbolicName(const char *&Name,
317 ConstraintInfo *OutputConstraints,
319 unsigned &Index) const {
320 assert(*Name == '[' && "Symbolic name did not start with '['");
322 const char *Start = Name;
323 while (*Name && *Name != ']')
331 std::string SymbolicName(Start, Name - Start);
333 for (Index = 0; Index != NumOutputs; ++Index)
334 if (SymbolicName == OutputConstraints[Index].getName())
340 bool TargetInfo::validateInputConstraint(ConstraintInfo *OutputConstraints,
342 ConstraintInfo &Info) const {
343 const char *Name = Info.ConstraintStr.c_str();
348 // Check if we have a matching constraint
349 if (*Name >= '0' && *Name <= '9') {
350 unsigned i = *Name - '0';
352 // Check if matching constraint is out of bounds.
356 // A number must refer to an output only operand.
357 if (OutputConstraints[i].isReadWrite())
360 // If the constraint is already tied, it must be tied to the
361 // same operand referenced to by the number.
362 if (Info.hasTiedOperand() && Info.getTiedOperand() != i)
365 // The constraint should have the same info as the respective
366 // output constraint.
367 Info.setTiedOperand(i, OutputConstraints[i]);
368 } else if (!validateAsmConstraint(Name, Info)) {
369 // FIXME: This error return is in place temporarily so we can
370 // add more constraints as we hit it. Eventually, an unknown
371 // constraint should just be treated as 'g'.
377 if (!resolveSymbolicName(Name, OutputConstraints, NumOutputs, Index))
380 // If the constraint is already tied, it must be tied to the
381 // same operand referenced to by the number.
382 if (Info.hasTiedOperand() && Info.getTiedOperand() != Index)
385 Info.setTiedOperand(Index, OutputConstraints[Index]);
388 case '%': // commutative
389 // FIXME: Fail if % is used with the last operand.
391 case 'i': // immediate integer.
392 case 'n': // immediate integer with a known value.
394 case 'I': // Various constant constraints with target-specific meanings.
403 case 'r': // general register.
404 Info.setAllowsRegister();
406 case 'm': // memory operand.
407 case 'o': // offsettable memory operand.
408 case 'V': // non-offsettable memory operand.
409 case '<': // autodecrement memory operand.
410 case '>': // autoincrement memory operand.
411 Info.setAllowsMemory();
413 case 'g': // general register, memory operand or immediate integer.
414 case 'X': // any operand.
415 Info.setAllowsRegister();
416 Info.setAllowsMemory();
418 case 'E': // immediate floating point.
419 case 'F': // immediate floating point.
420 case 'p': // address operand.
422 case ',': // multiple alternative constraint. Ignore comma.
424 case '?': // Disparage slightly code.
425 case '!': // Disparage severly.