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 = "_";
57 // Out of line virtual dtor for TargetInfo.
58 TargetInfo::~TargetInfo() {}
60 /// getTypeName - Return the user string for the specified integer type enum.
61 /// For example, SignedShort -> "short".
62 const char *TargetInfo::getTypeName(IntType T) {
64 default: assert(0 && "not an integer!");
65 case SignedShort: return "short";
66 case UnsignedShort: return "unsigned short";
67 case SignedInt: return "int";
68 case UnsignedInt: return "unsigned int";
69 case SignedLong: return "long int";
70 case UnsignedLong: return "long unsigned int";
71 case SignedLongLong: return "long long int";
72 case UnsignedLongLong: return "long long unsigned int";
76 /// getTypeConstantSuffix - Return the constant suffix for the specified
77 /// integer type enum. For example, SignedLong -> "L".
78 const char *TargetInfo::getTypeConstantSuffix(IntType T) {
80 default: assert(0 && "not an integer!");
82 case SignedInt: return "";
83 case SignedLong: return "L";
84 case SignedLongLong: return "LL";
86 case UnsignedInt: return "U";
87 case UnsignedLong: return "UL";
88 case UnsignedLongLong: return "ULL";
92 /// getTypeWidth - Return the width (in bits) of the specified integer type
93 /// enum. For example, SignedInt -> getIntWidth().
94 unsigned TargetInfo::getTypeWidth(IntType T) const {
96 default: assert(0 && "not an integer!");
98 case UnsignedShort: return getShortWidth();
100 case UnsignedInt: return getIntWidth();
102 case UnsignedLong: return getLongWidth();
104 case UnsignedLongLong: return getLongLongWidth();
108 /// getTypeAlign - Return the alignment (in bits) of the specified integer type
109 /// enum. For example, SignedInt -> getIntAlign().
110 unsigned TargetInfo::getTypeAlign(IntType T) const {
112 default: assert(0 && "not an integer!");
114 case UnsignedShort: return getShortAlign();
116 case UnsignedInt: return getIntAlign();
118 case UnsignedLong: return getLongAlign();
120 case UnsignedLongLong: return getLongLongAlign();
124 /// isTypeSigned - Return whether an integer types is signed. Returns true if
125 /// the type is signed; false otherwise.
126 bool TargetInfo::isTypeSigned(IntType T) const {
128 default: assert(0 && "not an integer!");
137 case UnsignedLongLong:
142 /// setForcedLangOptions - Set forced language options.
143 /// Apply changes to the target information with respect to certain
144 /// language options which change the target configuration.
145 void TargetInfo::setForcedLangOptions(LangOptions &Opts) {
146 if (Opts.NoBitFieldTypeAlign)
147 UseBitFieldTypeAlignment = false;
149 WCharType = UnsignedShort;
152 //===----------------------------------------------------------------------===//
155 static llvm::StringRef removeGCCRegisterPrefix(llvm::StringRef Name) {
156 if (Name[0] == '%' || Name[0] == '#')
157 Name = Name.substr(1);
162 /// isValidGCCRegisterName - Returns whether the passed in string
163 /// is a valid register name according to GCC. This is used by Sema for
164 /// inline asm statements.
165 bool TargetInfo::isValidGCCRegisterName(llvm::StringRef Name) const {
169 const char * const *Names;
172 // Get rid of any register prefix.
173 Name = removeGCCRegisterPrefix(Name);
175 if (Name == "memory" || Name == "cc")
178 getGCCRegNames(Names, NumNames);
180 // If we have a number it maps to an entry in the register name array.
181 if (isdigit(Name[0])) {
183 if (!Name.getAsInteger(0, n))
184 return n >= 0 && (unsigned)n < NumNames;
187 // Check register names.
188 for (unsigned i = 0; i < NumNames; i++) {
189 if (Name == Names[i])
193 // Now check aliases.
194 const GCCRegAlias *Aliases;
197 getGCCRegAliases(Aliases, NumAliases);
198 for (unsigned i = 0; i < NumAliases; i++) {
199 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
200 if (!Aliases[i].Aliases[j])
202 if (Aliases[i].Aliases[j] == Name)
211 TargetInfo::getNormalizedGCCRegisterName(llvm::StringRef Name) const {
212 assert(isValidGCCRegisterName(Name) && "Invalid register passed in");
214 // Get rid of any register prefix.
215 Name = removeGCCRegisterPrefix(Name);
217 const char * const *Names;
220 getGCCRegNames(Names, NumNames);
222 // First, check if we have a number.
223 if (isdigit(Name[0])) {
225 if (!Name.getAsInteger(0, n)) {
226 assert(n >= 0 && (unsigned)n < NumNames &&
227 "Out of bounds register number!");
232 // Now check aliases.
233 const GCCRegAlias *Aliases;
236 getGCCRegAliases(Aliases, NumAliases);
237 for (unsigned i = 0; i < NumAliases; i++) {
238 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
239 if (!Aliases[i].Aliases[j])
241 if (Aliases[i].Aliases[j] == Name)
242 return Aliases[i].Register;
249 bool TargetInfo::validateOutputConstraint(ConstraintInfo &Info) const {
250 const char *Name = Info.getConstraintStr().c_str();
251 // An output constraint must start with '=' or '+'
252 if (*Name != '=' && *Name != '+')
256 Info.setIsReadWrite();
262 if (!validateAsmConstraint(Name, Info)) {
263 // FIXME: We temporarily return false
264 // so we can add more constraints as we hit it.
265 // Eventually, an unknown constraint should just be treated as 'g'.
268 case '&': // early clobber.
270 case '%': // commutative.
271 // FIXME: Check that there is a another register after this one.
273 case 'r': // general register.
274 Info.setAllowsRegister();
276 case 'm': // memory operand.
277 Info.setAllowsMemory();
279 case 'g': // general register, memory operand or immediate integer.
280 case 'X': // any operand.
281 Info.setAllowsRegister();
282 Info.setAllowsMemory();
292 bool TargetInfo::resolveSymbolicName(const char *&Name,
293 ConstraintInfo *OutputConstraints,
295 unsigned &Index) const {
296 assert(*Name == '[' && "Symbolic name did not start with '['");
298 const char *Start = Name;
299 while (*Name && *Name != ']')
307 std::string SymbolicName(Start, Name - Start);
309 for (Index = 0; Index != NumOutputs; ++Index)
310 if (SymbolicName == OutputConstraints[Index].getName())
316 bool TargetInfo::validateInputConstraint(ConstraintInfo *OutputConstraints,
318 ConstraintInfo &Info) const {
319 const char *Name = Info.ConstraintStr.c_str();
324 // Check if we have a matching constraint
325 if (*Name >= '0' && *Name <= '9') {
326 unsigned i = *Name - '0';
328 // Check if matching constraint is out of bounds.
332 // The constraint should have the same info as the respective
333 // output constraint.
334 Info.setTiedOperand(i, OutputConstraints[i]);
335 } else if (!validateAsmConstraint(Name, Info)) {
336 // FIXME: This error return is in place temporarily so we can
337 // add more constraints as we hit it. Eventually, an unknown
338 // constraint should just be treated as 'g'.
344 if (!resolveSymbolicName(Name, OutputConstraints, NumOutputs, Index))
349 case '%': // commutative
350 // FIXME: Fail if % is used with the last operand.
352 case 'i': // immediate integer.
353 case 'n': // immediate integer with a known value.
355 case 'I': // Various constant constraints with target-specific meanings.
364 case 'r': // general register.
365 Info.setAllowsRegister();
367 case 'm': // memory operand.
368 case 'o': // offsettable memory operand
369 case 'V': // non-offsettable memory operand
370 Info.setAllowsMemory();
372 case 'g': // general register, memory operand or immediate integer.
373 case 'X': // any operand.
374 Info.setAllowsRegister();
375 Info.setAllowsMemory();