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"
19 using namespace clang;
21 // TargetInfo Constructor.
22 TargetInfo::TargetInfo(const std::string &T) : Triple(T) {
23 // Set defaults. Defaults are set for a 32-bit RISC platform, like PPC or
24 // SPARC. These should be overridden by concrete targets as needed.
26 NoAsmVariants = false;
27 PointerWidth = PointerAlign = 32;
28 IntWidth = IntAlign = 32;
29 LongWidth = LongAlign = 32;
30 LongLongWidth = LongLongAlign = 64;
37 LargeArrayMinWidth = 0;
39 SizeType = UnsignedLong;
40 PtrDiffType = SignedLong;
41 IntMaxType = SignedLongLong;
42 UIntMaxType = UnsignedLongLong;
43 IntPtrType = SignedLong;
44 WCharType = SignedInt;
46 Char16Type = UnsignedShort;
47 Char32Type = UnsignedInt;
48 Int64Type = SignedLongLong;
49 SigAtomicType = SignedInt;
50 UseBitFieldTypeAlignment = true;
51 FloatFormat = &llvm::APFloat::IEEEsingle;
52 DoubleFormat = &llvm::APFloat::IEEEdouble;
53 LongDoubleFormat = &llvm::APFloat::IEEEdouble;
54 DescriptionString = "E-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
55 "i64:64:64-f32:32:32-f64:64:64-n32";
56 UserLabelPrefix = "_";
57 HasAlignMac68kSupport = false;
59 // Default to no types using fpret.
60 RealTypeUsesObjCFPRet = 0;
62 // Default to using the Itanium ABI.
63 CXXABI = CXXABI_Itanium;
66 // Out of line virtual dtor for TargetInfo.
67 TargetInfo::~TargetInfo() {}
69 /// getTypeName - Return the user string for the specified integer type enum.
70 /// For example, SignedShort -> "short".
71 const char *TargetInfo::getTypeName(IntType T) {
73 default: assert(0 && "not an integer!");
74 case SignedShort: return "short";
75 case UnsignedShort: return "unsigned short";
76 case SignedInt: return "int";
77 case UnsignedInt: return "unsigned int";
78 case SignedLong: return "long int";
79 case UnsignedLong: return "long unsigned int";
80 case SignedLongLong: return "long long int";
81 case UnsignedLongLong: return "long long unsigned int";
85 /// getTypeConstantSuffix - Return the constant suffix for the specified
86 /// integer type enum. For example, SignedLong -> "L".
87 const char *TargetInfo::getTypeConstantSuffix(IntType T) {
89 default: assert(0 && "not an integer!");
91 case SignedInt: return "";
92 case SignedLong: return "L";
93 case SignedLongLong: return "LL";
95 case UnsignedInt: return "U";
96 case UnsignedLong: return "UL";
97 case UnsignedLongLong: return "ULL";
101 /// getTypeWidth - Return the width (in bits) of the specified integer type
102 /// enum. For example, SignedInt -> getIntWidth().
103 unsigned TargetInfo::getTypeWidth(IntType T) const {
105 default: assert(0 && "not an integer!");
107 case UnsignedShort: return getShortWidth();
109 case UnsignedInt: return getIntWidth();
111 case UnsignedLong: return getLongWidth();
113 case UnsignedLongLong: return getLongLongWidth();
117 /// getTypeAlign - Return the alignment (in bits) of the specified integer type
118 /// enum. For example, SignedInt -> getIntAlign().
119 unsigned TargetInfo::getTypeAlign(IntType T) const {
121 default: assert(0 && "not an integer!");
123 case UnsignedShort: return getShortAlign();
125 case UnsignedInt: return getIntAlign();
127 case UnsignedLong: return getLongAlign();
129 case UnsignedLongLong: return getLongLongAlign();
133 /// isTypeSigned - Return whether an integer types is signed. Returns true if
134 /// the type is signed; false otherwise.
135 bool TargetInfo::isTypeSigned(IntType T) const {
137 default: assert(0 && "not an integer!");
146 case UnsignedLongLong:
151 /// setForcedLangOptions - Set forced language options.
152 /// Apply changes to the target information with respect to certain
153 /// language options which change the target configuration.
154 void TargetInfo::setForcedLangOptions(LangOptions &Opts) {
155 if (Opts.NoBitFieldTypeAlign)
156 UseBitFieldTypeAlignment = false;
158 WCharType = UnsignedShort;
161 //===----------------------------------------------------------------------===//
164 static llvm::StringRef removeGCCRegisterPrefix(llvm::StringRef Name) {
165 if (Name[0] == '%' || Name[0] == '#')
166 Name = Name.substr(1);
171 /// isValidGCCRegisterName - Returns whether the passed in string
172 /// is a valid register name according to GCC. This is used by Sema for
173 /// inline asm statements.
174 bool TargetInfo::isValidGCCRegisterName(llvm::StringRef Name) const {
178 const char * const *Names;
181 // Get rid of any register prefix.
182 Name = removeGCCRegisterPrefix(Name);
184 if (Name == "memory" || Name == "cc")
187 getGCCRegNames(Names, NumNames);
189 // If we have a number it maps to an entry in the register name array.
190 if (isdigit(Name[0])) {
192 if (!Name.getAsInteger(0, n))
193 return n >= 0 && (unsigned)n < NumNames;
196 // Check register names.
197 for (unsigned i = 0; i < NumNames; i++) {
198 if (Name == Names[i])
202 // Now check aliases.
203 const GCCRegAlias *Aliases;
206 getGCCRegAliases(Aliases, NumAliases);
207 for (unsigned i = 0; i < NumAliases; i++) {
208 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
209 if (!Aliases[i].Aliases[j])
211 if (Aliases[i].Aliases[j] == Name)
220 TargetInfo::getNormalizedGCCRegisterName(llvm::StringRef Name) const {
221 assert(isValidGCCRegisterName(Name) && "Invalid register passed in");
223 // Get rid of any register prefix.
224 Name = removeGCCRegisterPrefix(Name);
226 const char * const *Names;
229 getGCCRegNames(Names, NumNames);
231 // First, check if we have a number.
232 if (isdigit(Name[0])) {
234 if (!Name.getAsInteger(0, n)) {
235 assert(n >= 0 && (unsigned)n < NumNames &&
236 "Out of bounds register number!");
241 // Now check aliases.
242 const GCCRegAlias *Aliases;
245 getGCCRegAliases(Aliases, NumAliases);
246 for (unsigned i = 0; i < NumAliases; i++) {
247 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
248 if (!Aliases[i].Aliases[j])
250 if (Aliases[i].Aliases[j] == Name)
251 return Aliases[i].Register;
258 bool TargetInfo::validateOutputConstraint(ConstraintInfo &Info) const {
259 const char *Name = Info.getConstraintStr().c_str();
260 // An output constraint must start with '=' or '+'
261 if (*Name != '=' && *Name != '+')
265 Info.setIsReadWrite();
271 if (!validateAsmConstraint(Name, Info)) {
272 // FIXME: We temporarily return false
273 // so we can add more constraints as we hit it.
274 // Eventually, an unknown constraint should just be treated as 'g'.
277 case '&': // early clobber.
279 case '%': // commutative.
280 // FIXME: Check that there is a another register after this one.
282 case 'r': // general register.
283 Info.setAllowsRegister();
285 case 'm': // memory operand.
286 Info.setAllowsMemory();
288 case 'g': // general register, memory operand or immediate integer.
289 case 'X': // any operand.
290 Info.setAllowsRegister();
291 Info.setAllowsMemory();
293 case ',': // multiple alternative constraint. Pass it.
295 // Handle additional optional '=' or '+' modifiers.
296 if (*Name == '=' || *Name == '+')
299 case '?': // Disparage slightly code.
300 case '!': // Disparage severly.
310 bool TargetInfo::resolveSymbolicName(const char *&Name,
311 ConstraintInfo *OutputConstraints,
313 unsigned &Index) const {
314 assert(*Name == '[' && "Symbolic name did not start with '['");
316 const char *Start = Name;
317 while (*Name && *Name != ']')
325 std::string SymbolicName(Start, Name - Start);
327 for (Index = 0; Index != NumOutputs; ++Index)
328 if (SymbolicName == OutputConstraints[Index].getName())
334 bool TargetInfo::validateInputConstraint(ConstraintInfo *OutputConstraints,
336 ConstraintInfo &Info) const {
337 const char *Name = Info.ConstraintStr.c_str();
342 // Check if we have a matching constraint
343 if (*Name >= '0' && *Name <= '9') {
344 unsigned i = *Name - '0';
346 // Check if matching constraint is out of bounds.
350 // The constraint should have the same info as the respective
351 // output constraint.
352 Info.setTiedOperand(i, OutputConstraints[i]);
353 } else if (!validateAsmConstraint(Name, Info)) {
354 // FIXME: This error return is in place temporarily so we can
355 // add more constraints as we hit it. Eventually, an unknown
356 // constraint should just be treated as 'g'.
362 if (!resolveSymbolicName(Name, OutputConstraints, NumOutputs, Index))
365 Info.setTiedOperand(Index, OutputConstraints[Index]);
368 case '%': // commutative
369 // FIXME: Fail if % is used with the last operand.
371 case 'i': // immediate integer.
372 case 'n': // immediate integer with a known value.
374 case 'I': // Various constant constraints with target-specific meanings.
383 case 'r': // general register.
384 Info.setAllowsRegister();
386 case 'm': // memory operand.
387 case 'o': // offsettable memory operand
388 case 'V': // non-offsettable memory operand
389 Info.setAllowsMemory();
391 case 'g': // general register, memory operand or immediate integer.
392 case 'X': // any operand.
393 Info.setAllowsRegister();
394 Info.setAllowsMemory();
396 case ',': // multiple alternative constraint. Ignore comma.
398 case '?': // Disparage slightly code.
399 case '!': // Disparage severly.