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,
25 // These should be overridden by concrete targets as needed.
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 FloatFormat = &llvm::APFloat::IEEEsingle;
49 DoubleFormat = &llvm::APFloat::IEEEdouble;
50 LongDoubleFormat = &llvm::APFloat::IEEEdouble;
51 DescriptionString = "E-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
52 "i64:64:64-f32:32:32-f64:64:64-n32";
53 UserLabelPrefix = "_";
56 // Out of line virtual dtor for TargetInfo.
57 TargetInfo::~TargetInfo() {}
59 /// getTypeName - Return the user string for the specified integer type enum.
60 /// For example, SignedShort -> "short".
61 const char *TargetInfo::getTypeName(IntType T) {
63 default: assert(0 && "not an integer!");
64 case SignedShort: return "short";
65 case UnsignedShort: return "unsigned short";
66 case SignedInt: return "int";
67 case UnsignedInt: return "unsigned int";
68 case SignedLong: return "long int";
69 case UnsignedLong: return "long unsigned int";
70 case SignedLongLong: return "long long int";
71 case UnsignedLongLong: return "long long unsigned int";
75 /// getTypeConstantSuffix - Return the constant suffix for the specified
76 /// integer type enum. For example, SignedLong -> "L".
77 const char *TargetInfo::getTypeConstantSuffix(IntType T) {
79 default: assert(0 && "not an integer!");
81 case SignedInt: return "";
82 case SignedLong: return "L";
83 case SignedLongLong: return "LL";
85 case UnsignedInt: return "U";
86 case UnsignedLong: return "UL";
87 case UnsignedLongLong: return "ULL";
91 /// getTypeWidth - Return the width (in bits) of the specified integer type
92 /// enum. For example, SignedInt -> getIntWidth().
93 unsigned TargetInfo::getTypeWidth(IntType T) const {
95 default: assert(0 && "not an integer!");
97 case UnsignedShort: return getShortWidth();
99 case UnsignedInt: return getIntWidth();
101 case UnsignedLong: return getLongWidth();
103 case UnsignedLongLong: return getLongLongWidth();
107 /// getTypeAlign - Return the alignment (in bits) of the specified integer type
108 /// enum. For example, SignedInt -> getIntAlign().
109 unsigned TargetInfo::getTypeAlign(IntType T) const {
111 default: assert(0 && "not an integer!");
113 case UnsignedShort: return getShortAlign();
115 case UnsignedInt: return getIntAlign();
117 case UnsignedLong: return getLongAlign();
119 case UnsignedLongLong: return getLongLongAlign();
123 /// isTypeSigned - Return whether an integer types is signed. Returns true if
124 /// the type is signed; false otherwise.
125 bool TargetInfo::isTypeSigned(IntType T) const {
127 default: assert(0 && "not an integer!");
136 case UnsignedLongLong:
141 /// setForcedLangOptions - Set forced language options.
142 /// Apply changes to the target information with respect to certain
143 /// language options which change the target configuration.
144 void TargetInfo::setForcedLangOptions(LangOptions &Opts) {
145 if (Opts.ShortWChar) {
146 WCharType = UnsignedShort;
150 //===----------------------------------------------------------------------===//
153 static llvm::StringRef removeGCCRegisterPrefix(llvm::StringRef Name) {
154 if (Name[0] == '%' || Name[0] == '#')
155 Name = Name.substr(1);
160 /// isValidGCCRegisterName - Returns whether the passed in string
161 /// is a valid register name according to GCC. This is used by Sema for
162 /// inline asm statements.
163 bool TargetInfo::isValidGCCRegisterName(llvm::StringRef Name) const {
167 const char * const *Names;
170 // Get rid of any register prefix.
171 Name = removeGCCRegisterPrefix(Name);
173 if (Name == "memory" || Name == "cc")
176 getGCCRegNames(Names, NumNames);
178 // If we have a number it maps to an entry in the register name array.
179 if (isdigit(Name[0])) {
181 if (!Name.getAsInteger(0, n))
182 return n >= 0 && (unsigned)n < NumNames;
185 // Check register names.
186 for (unsigned i = 0; i < NumNames; i++) {
187 if (Name == Names[i])
191 // Now check aliases.
192 const GCCRegAlias *Aliases;
195 getGCCRegAliases(Aliases, NumAliases);
196 for (unsigned i = 0; i < NumAliases; i++) {
197 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
198 if (!Aliases[i].Aliases[j])
200 if (Aliases[i].Aliases[j] == Name)
209 TargetInfo::getNormalizedGCCRegisterName(llvm::StringRef Name) const {
210 assert(isValidGCCRegisterName(Name) && "Invalid register passed in");
212 // Get rid of any register prefix.
213 Name = removeGCCRegisterPrefix(Name);
215 const char * const *Names;
218 getGCCRegNames(Names, NumNames);
220 // First, check if we have a number.
221 if (isdigit(Name[0])) {
223 if (!Name.getAsInteger(0, n)) {
224 assert(n >= 0 && (unsigned)n < NumNames &&
225 "Out of bounds register number!");
230 // Now check aliases.
231 const GCCRegAlias *Aliases;
234 getGCCRegAliases(Aliases, NumAliases);
235 for (unsigned i = 0; i < NumAliases; i++) {
236 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
237 if (!Aliases[i].Aliases[j])
239 if (Aliases[i].Aliases[j] == Name)
240 return Aliases[i].Register;
247 bool TargetInfo::validateOutputConstraint(ConstraintInfo &Info) const {
248 const char *Name = Info.getConstraintStr().c_str();
249 // An output constraint must start with '=' or '+'
250 if (*Name != '=' && *Name != '+')
254 Info.setIsReadWrite();
260 if (!validateAsmConstraint(Name, Info)) {
261 // FIXME: We temporarily return false
262 // so we can add more constraints as we hit it.
263 // Eventually, an unknown constraint should just be treated as 'g'.
266 case '&': // early clobber.
268 case '%': // commutative.
269 // FIXME: Check that there is a another register after this one.
271 case 'r': // general register.
272 Info.setAllowsRegister();
274 case 'm': // memory operand.
275 Info.setAllowsMemory();
277 case 'g': // general register, memory operand or immediate integer.
278 case 'X': // any operand.
279 Info.setAllowsRegister();
280 Info.setAllowsMemory();
290 bool TargetInfo::resolveSymbolicName(const char *&Name,
291 ConstraintInfo *OutputConstraints,
293 unsigned &Index) const {
294 assert(*Name == '[' && "Symbolic name did not start with '['");
296 const char *Start = Name;
297 while (*Name && *Name != ']')
305 std::string SymbolicName(Start, Name - Start);
307 for (Index = 0; Index != NumOutputs; ++Index)
308 if (SymbolicName == OutputConstraints[Index].getName())
314 bool TargetInfo::validateInputConstraint(ConstraintInfo *OutputConstraints,
316 ConstraintInfo &Info) const {
317 const char *Name = Info.ConstraintStr.c_str();
322 // Check if we have a matching constraint
323 if (*Name >= '0' && *Name <= '9') {
324 unsigned i = *Name - '0';
326 // Check if matching constraint is out of bounds.
330 // The constraint should have the same info as the respective
331 // output constraint.
332 Info.setTiedOperand(i, OutputConstraints[i]);
333 } else if (!validateAsmConstraint(Name, Info)) {
334 // FIXME: This error return is in place temporarily so we can
335 // add more constraints as we hit it. Eventually, an unknown
336 // constraint should just be treated as 'g'.
342 if (!resolveSymbolicName(Name, OutputConstraints, NumOutputs, Index))
347 case '%': // commutative
348 // FIXME: Fail if % is used with the last operand.
350 case 'i': // immediate integer.
351 case 'n': // immediate integer with a known value.
353 case 'I': // Various constant constraints with target-specific meanings.
362 case 'r': // general register.
363 Info.setAllowsRegister();
365 case 'm': // memory operand.
366 case 'o': // offsettable memory operand
367 case 'V': // non-offsettable memory operand
368 Info.setAllowsMemory();
370 case 'g': // general register, memory operand or immediate integer.
371 case 'X': // any operand.
372 Info.setAllowsRegister();
373 Info.setAllowsMemory();