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 SizeType = UnsignedLong;
38 PtrDiffType = SignedLong;
39 IntMaxType = SignedLongLong;
40 UIntMaxType = UnsignedLongLong;
41 IntPtrType = SignedLong;
42 WCharType = SignedInt;
44 Char16Type = UnsignedShort;
45 Char32Type = UnsignedInt;
46 Int64Type = SignedLongLong;
47 SigAtomicType = SignedInt;
48 UseBitFieldTypeAlignment = true;
49 FloatFormat = &llvm::APFloat::IEEEsingle;
50 DoubleFormat = &llvm::APFloat::IEEEdouble;
51 LongDoubleFormat = &llvm::APFloat::IEEEdouble;
52 DescriptionString = "E-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
53 "i64:64:64-f32:32:32-f64:64:64-n32";
54 UserLabelPrefix = "_";
55 HasAlignMac68kSupport = false;
58 // Out of line virtual dtor for TargetInfo.
59 TargetInfo::~TargetInfo() {}
61 /// getTypeName - Return the user string for the specified integer type enum.
62 /// For example, SignedShort -> "short".
63 const char *TargetInfo::getTypeName(IntType T) {
65 default: assert(0 && "not an integer!");
66 case SignedShort: return "short";
67 case UnsignedShort: return "unsigned short";
68 case SignedInt: return "int";
69 case UnsignedInt: return "unsigned int";
70 case SignedLong: return "long int";
71 case UnsignedLong: return "long unsigned int";
72 case SignedLongLong: return "long long int";
73 case UnsignedLongLong: return "long long unsigned int";
77 /// getTypeConstantSuffix - Return the constant suffix for the specified
78 /// integer type enum. For example, SignedLong -> "L".
79 const char *TargetInfo::getTypeConstantSuffix(IntType T) {
81 default: assert(0 && "not an integer!");
83 case SignedInt: return "";
84 case SignedLong: return "L";
85 case SignedLongLong: return "LL";
87 case UnsignedInt: return "U";
88 case UnsignedLong: return "UL";
89 case UnsignedLongLong: return "ULL";
93 /// getTypeWidth - Return the width (in bits) of the specified integer type
94 /// enum. For example, SignedInt -> getIntWidth().
95 unsigned TargetInfo::getTypeWidth(IntType T) const {
97 default: assert(0 && "not an integer!");
99 case UnsignedShort: return getShortWidth();
101 case UnsignedInt: return getIntWidth();
103 case UnsignedLong: return getLongWidth();
105 case UnsignedLongLong: return getLongLongWidth();
109 /// getTypeAlign - Return the alignment (in bits) of the specified integer type
110 /// enum. For example, SignedInt -> getIntAlign().
111 unsigned TargetInfo::getTypeAlign(IntType T) const {
113 default: assert(0 && "not an integer!");
115 case UnsignedShort: return getShortAlign();
117 case UnsignedInt: return getIntAlign();
119 case UnsignedLong: return getLongAlign();
121 case UnsignedLongLong: return getLongLongAlign();
125 /// isTypeSigned - Return whether an integer types is signed. Returns true if
126 /// the type is signed; false otherwise.
127 bool TargetInfo::isTypeSigned(IntType T) const {
129 default: assert(0 && "not an integer!");
138 case UnsignedLongLong:
143 /// setForcedLangOptions - Set forced language options.
144 /// Apply changes to the target information with respect to certain
145 /// language options which change the target configuration.
146 void TargetInfo::setForcedLangOptions(LangOptions &Opts) {
147 if (Opts.NoBitFieldTypeAlign)
148 UseBitFieldTypeAlignment = false;
150 WCharType = UnsignedShort;
153 //===----------------------------------------------------------------------===//
156 static llvm::StringRef removeGCCRegisterPrefix(llvm::StringRef Name) {
157 if (Name[0] == '%' || Name[0] == '#')
158 Name = Name.substr(1);
163 /// isValidGCCRegisterName - Returns whether the passed in string
164 /// is a valid register name according to GCC. This is used by Sema for
165 /// inline asm statements.
166 bool TargetInfo::isValidGCCRegisterName(llvm::StringRef Name) const {
170 const char * const *Names;
173 // Get rid of any register prefix.
174 Name = removeGCCRegisterPrefix(Name);
176 if (Name == "memory" || Name == "cc")
179 getGCCRegNames(Names, NumNames);
181 // If we have a number it maps to an entry in the register name array.
182 if (isdigit(Name[0])) {
184 if (!Name.getAsInteger(0, n))
185 return n >= 0 && (unsigned)n < NumNames;
188 // Check register names.
189 for (unsigned i = 0; i < NumNames; i++) {
190 if (Name == Names[i])
194 // Now check aliases.
195 const GCCRegAlias *Aliases;
198 getGCCRegAliases(Aliases, NumAliases);
199 for (unsigned i = 0; i < NumAliases; i++) {
200 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
201 if (!Aliases[i].Aliases[j])
203 if (Aliases[i].Aliases[j] == Name)
212 TargetInfo::getNormalizedGCCRegisterName(llvm::StringRef Name) const {
213 assert(isValidGCCRegisterName(Name) && "Invalid register passed in");
215 // Get rid of any register prefix.
216 Name = removeGCCRegisterPrefix(Name);
218 const char * const *Names;
221 getGCCRegNames(Names, NumNames);
223 // First, check if we have a number.
224 if (isdigit(Name[0])) {
226 if (!Name.getAsInteger(0, n)) {
227 assert(n >= 0 && (unsigned)n < NumNames &&
228 "Out of bounds register number!");
233 // Now check aliases.
234 const GCCRegAlias *Aliases;
237 getGCCRegAliases(Aliases, NumAliases);
238 for (unsigned i = 0; i < NumAliases; i++) {
239 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
240 if (!Aliases[i].Aliases[j])
242 if (Aliases[i].Aliases[j] == Name)
243 return Aliases[i].Register;
250 bool TargetInfo::validateOutputConstraint(ConstraintInfo &Info) const {
251 const char *Name = Info.getConstraintStr().c_str();
252 // An output constraint must start with '=' or '+'
253 if (*Name != '=' && *Name != '+')
257 Info.setIsReadWrite();
263 if (!validateAsmConstraint(Name, Info)) {
264 // FIXME: We temporarily return false
265 // so we can add more constraints as we hit it.
266 // Eventually, an unknown constraint should just be treated as 'g'.
269 case '&': // early clobber.
271 case '%': // commutative.
272 // FIXME: Check that there is a another register after this one.
274 case 'r': // general register.
275 Info.setAllowsRegister();
277 case 'm': // memory operand.
278 Info.setAllowsMemory();
280 case 'g': // general register, memory operand or immediate integer.
281 case 'X': // any operand.
282 Info.setAllowsRegister();
283 Info.setAllowsMemory();
293 bool TargetInfo::resolveSymbolicName(const char *&Name,
294 ConstraintInfo *OutputConstraints,
296 unsigned &Index) const {
297 assert(*Name == '[' && "Symbolic name did not start with '['");
299 const char *Start = Name;
300 while (*Name && *Name != ']')
308 std::string SymbolicName(Start, Name - Start);
310 for (Index = 0; Index != NumOutputs; ++Index)
311 if (SymbolicName == OutputConstraints[Index].getName())
317 bool TargetInfo::validateInputConstraint(ConstraintInfo *OutputConstraints,
319 ConstraintInfo &Info) const {
320 const char *Name = Info.ConstraintStr.c_str();
325 // Check if we have a matching constraint
326 if (*Name >= '0' && *Name <= '9') {
327 unsigned i = *Name - '0';
329 // Check if matching constraint is out of bounds.
333 // The constraint should have the same info as the respective
334 // output constraint.
335 Info.setTiedOperand(i, OutputConstraints[i]);
336 } else if (!validateAsmConstraint(Name, Info)) {
337 // FIXME: This error return is in place temporarily so we can
338 // add more constraints as we hit it. Eventually, an unknown
339 // constraint should just be treated as 'g'.
345 if (!resolveSymbolicName(Name, OutputConstraints, NumOutputs, Index))
350 case '%': // commutative
351 // FIXME: Fail if % is used with the last operand.
353 case 'i': // immediate integer.
354 case 'n': // immediate integer with a known value.
356 case 'I': // Various constant constraints with target-specific meanings.
365 case 'r': // general register.
366 Info.setAllowsRegister();
368 case 'm': // memory operand.
369 case 'o': // offsettable memory operand
370 case 'V': // non-offsettable memory operand
371 Info.setAllowsMemory();
373 case 'g': // general register, memory operand or immediate integer.
374 case 'X': // any operand.
375 Info.setAllowsRegister();
376 Info.setAllowsMemory();