blob: 136089fe90c20b7b1186d4c07a953d1dd0a46992 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- TargetInfo.cpp - Information about Target machine ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the TargetInfo and TargetInfoImpl interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Basic/TargetInfo.h"
John Thompsona6fda122009-11-05 20:14:16 +000015#include "clang/Basic/LangOptions.h"
Chris Lattner525a0502007-09-22 18:29:59 +000016#include "llvm/ADT/APFloat.h"
Anders Carlsson6fa90862007-11-25 00:25:21 +000017#include "llvm/ADT/STLExtras.h"
Chris Lattner5483bdf2008-04-06 04:02:29 +000018#include <cstdlib>
Reid Spencer5f016e22007-07-11 17:01:13 +000019using namespace clang;
20
Chris Lattnercd4fc422008-03-08 08:59:43 +000021// TargetInfo Constructor.
22TargetInfo::TargetInfo(const std::string &T) : Triple(T) {
Eli Friedman61538a72008-05-20 14:21:01 +000023 // Set defaults. Defaults are set for a 32-bit RISC platform,
24 // like PPC or SPARC.
25 // These should be overridden by concrete targets as needed.
Eli Friedmanb030f022009-04-19 21:38:35 +000026 TLSSupported = true;
Chris Lattner927686f2008-05-09 06:08:39 +000027 PointerWidth = PointerAlign = 32;
Chris Lattner2621fd12008-05-08 05:58:21 +000028 IntWidth = IntAlign = 32;
Chris Lattnerec10f582008-05-09 05:50:02 +000029 LongWidth = LongAlign = 32;
30 LongLongWidth = LongLongAlign = 64;
Eli Friedman61538a72008-05-20 14:21:01 +000031 FloatWidth = 32;
32 FloatAlign = 32;
Nate Begeman3d292062008-04-18 17:17:24 +000033 DoubleWidth = 64;
Eli Friedman61538a72008-05-20 14:21:01 +000034 DoubleAlign = 64;
35 LongDoubleWidth = 64;
36 LongDoubleAlign = 64;
Anders Carlsson6a3615c2008-10-31 16:05:19 +000037 SizeType = UnsignedLong;
Eli Friedmanf509d732008-11-02 02:43:55 +000038 PtrDiffType = SignedLong;
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +000039 IntMaxType = SignedLongLong;
40 UIntMaxType = UnsignedLongLong;
Chris Lattner6ad474f2009-02-13 22:28:55 +000041 IntPtrType = SignedLong;
Eli Friedmanf509d732008-11-02 02:43:55 +000042 WCharType = SignedInt;
Chris Lattnere64ef802009-10-21 04:59:34 +000043 WIntType = SignedInt;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +000044 Char16Type = UnsignedShort;
45 Char32Type = UnsignedInt;
Eli Friedman3c7b6e42009-07-01 03:36:11 +000046 Int64Type = SignedLongLong;
Edward O'Callaghan9cf910e2009-11-21 00:49:54 +000047 SigAtomicType = SignedInt;
Chris Lattnercd4fc422008-03-08 08:59:43 +000048 FloatFormat = &llvm::APFloat::IEEEsingle;
49 DoubleFormat = &llvm::APFloat::IEEEdouble;
50 LongDoubleFormat = &llvm::APFloat::IEEEdouble;
Eli Friedmaned855cb2008-08-21 00:13:15 +000051 DescriptionString = "E-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
Chris Lattner1932e122009-11-07 18:59:41 +000052 "i64:64:64-f32:32:32-f64:64:64-n32";
Chris Lattner3fdf4672008-10-05 19:22:37 +000053 UserLabelPrefix = "_";
Chris Lattnercd4fc422008-03-08 08:59:43 +000054}
55
Chris Lattner0eaed122008-03-08 08:24:01 +000056// Out of line virtual dtor for TargetInfo.
57TargetInfo::~TargetInfo() {}
Reid Spencer5f016e22007-07-11 17:01:13 +000058
Chris Lattner2b5abf52009-02-06 05:04:11 +000059/// getTypeName - Return the user string for the specified integer type enum.
60/// For example, SignedShort -> "short".
61const char *TargetInfo::getTypeName(IntType T) {
62 switch (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";
72 }
73}
74
Chris Lattnerb304f772009-10-21 06:24:21 +000075/// getTypeConstantSuffix - Return the constant suffix for the specified
76/// integer type enum. For example, SignedLong -> "L".
77const char *TargetInfo::getTypeConstantSuffix(IntType T) {
78 switch (T) {
79 default: assert(0 && "not an integer!");
80 case SignedShort:
81 case SignedInt: return "";
82 case SignedLong: return "L";
83 case SignedLongLong: return "LL";
84 case UnsignedShort:
85 case UnsignedInt: return "U";
86 case UnsignedLong: return "UL";
87 case UnsignedLongLong: return "ULL";
88 }
89}
90
91/// getTypeWidth - Return the width (in bits) of the specified integer type
92/// enum. For example, SignedInt -> getIntWidth().
93unsigned TargetInfo::getTypeWidth(IntType T) const {
94 switch (T) {
95 default: assert(0 && "not an integer!");
Chris Lattner9099e7b2009-11-05 21:21:32 +000096 case SignedShort:
Chris Lattnerb304f772009-10-21 06:24:21 +000097 case UnsignedShort: return getShortWidth();
Chris Lattner9099e7b2009-11-05 21:21:32 +000098 case SignedInt:
Chris Lattnerb304f772009-10-21 06:24:21 +000099 case UnsignedInt: return getIntWidth();
Chris Lattner9099e7b2009-11-05 21:21:32 +0000100 case SignedLong:
Chris Lattnerb304f772009-10-21 06:24:21 +0000101 case UnsignedLong: return getLongWidth();
Chris Lattner9099e7b2009-11-05 21:21:32 +0000102 case SignedLongLong:
Chris Lattnerb304f772009-10-21 06:24:21 +0000103 case UnsignedLongLong: return getLongLongWidth();
104 };
105}
106
Chris Lattner9099e7b2009-11-05 21:21:32 +0000107/// getTypeAlign - Return the alignment (in bits) of the specified integer type
108/// enum. For example, SignedInt -> getIntAlign().
109unsigned TargetInfo::getTypeAlign(IntType T) const {
110 switch (T) {
111 default: assert(0 && "not an integer!");
112 case SignedShort:
113 case UnsignedShort: return getShortAlign();
114 case SignedInt:
115 case UnsignedInt: return getIntAlign();
116 case SignedLong:
117 case UnsignedLong: return getLongAlign();
118 case SignedLongLong:
119 case UnsignedLongLong: return getLongLongAlign();
120 };
121}
122
Chris Lattner961f0702009-10-25 22:49:18 +0000123/// isTypeSigned - Return whether an integer types is signed. Returns true if
Chris Lattnerb304f772009-10-21 06:24:21 +0000124/// the type is signed; false otherwise.
Chris Lattner961f0702009-10-25 22:49:18 +0000125bool TargetInfo::isTypeSigned(IntType T) const {
Chris Lattnerb304f772009-10-21 06:24:21 +0000126 switch (T) {
127 default: assert(0 && "not an integer!");
128 case SignedShort:
129 case SignedInt:
130 case SignedLong:
131 case SignedLongLong:
132 return true;
133 case UnsignedShort:
134 case UnsignedInt:
135 case UnsignedLong:
136 case UnsignedLongLong:
137 return false;
138 };
139}
140
John Thompsona6fda122009-11-05 20:14:16 +0000141/// setForcedLangOptions - Set forced language options.
142/// Apply changes to the target information with respect to certain
143/// language options which change the target configuration.
144void TargetInfo::setForcedLangOptions(LangOptions &Opts) {
John Thompson40d1bb62009-11-05 22:03:02 +0000145 if (Opts.ShortWChar) {
John Thompsona6fda122009-11-05 20:14:16 +0000146 WCharType = UnsignedShort;
John Thompson40d1bb62009-11-05 22:03:02 +0000147 }
John Thompsona6fda122009-11-05 20:14:16 +0000148}
Chris Lattnerb304f772009-10-21 06:24:21 +0000149
Chris Lattner525a0502007-09-22 18:29:59 +0000150//===----------------------------------------------------------------------===//
Chris Lattner525a0502007-09-22 18:29:59 +0000151
Reid Spencer5f016e22007-07-11 17:01:13 +0000152
Anders Carlsson83c021c2010-01-30 19:12:25 +0000153static llvm::StringRef removeGCCRegisterPrefix(llvm::StringRef Name) {
Anders Carlssonea041752008-02-06 00:11:32 +0000154 if (Name[0] == '%' || Name[0] == '#')
Anders Carlsson83c021c2010-01-30 19:12:25 +0000155 Name = Name.substr(1);
156
157 return Name;
Anders Carlssonea041752008-02-06 00:11:32 +0000158}
159
Anders Carlsson3346ae62007-11-24 23:38:12 +0000160/// 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.
Anders Carlsson83c021c2010-01-30 19:12:25 +0000163bool TargetInfo::isValidGCCRegisterName(llvm::StringRef Name) const {
164 if (Name.empty())
165 return false;
166
Anders Carlsson6fa90862007-11-25 00:25:21 +0000167 const char * const *Names;
168 unsigned NumNames;
Mike Stump1eb44332009-09-09 15:08:12 +0000169
Anders Carlsson6fa90862007-11-25 00:25:21 +0000170 // Get rid of any register prefix.
Anders Carlsson83c021c2010-01-30 19:12:25 +0000171 Name = removeGCCRegisterPrefix(Name);
Anders Carlssonea041752008-02-06 00:11:32 +0000172
Anders Carlsson83c021c2010-01-30 19:12:25 +0000173 if (Name == "memory" || Name == "cc")
Anders Carlsson6fa90862007-11-25 00:25:21 +0000174 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000175
Chris Lattner0eaed122008-03-08 08:24:01 +0000176 getGCCRegNames(Names, NumNames);
Mike Stump1eb44332009-09-09 15:08:12 +0000177
Anders Carlsson6fa90862007-11-25 00:25:21 +0000178 // If we have a number it maps to an entry in the register name array.
179 if (isdigit(Name[0])) {
Anders Carlsson83c021c2010-01-30 19:12:25 +0000180 int n;
181 if (!Name.getAsInteger(0, n))
Anders Carlsson6fa90862007-11-25 00:25:21 +0000182 return n >= 0 && (unsigned)n < NumNames;
183 }
184
185 // Check register names.
186 for (unsigned i = 0; i < NumNames; i++) {
Anders Carlsson83c021c2010-01-30 19:12:25 +0000187 if (Name == Names[i])
Anders Carlsson6fa90862007-11-25 00:25:21 +0000188 return true;
189 }
Mike Stump1eb44332009-09-09 15:08:12 +0000190
Anders Carlsson6fa90862007-11-25 00:25:21 +0000191 // Now check aliases.
Chris Lattner0eaed122008-03-08 08:24:01 +0000192 const GCCRegAlias *Aliases;
Anders Carlsson6fa90862007-11-25 00:25:21 +0000193 unsigned NumAliases;
Mike Stump1eb44332009-09-09 15:08:12 +0000194
Chris Lattner0eaed122008-03-08 08:24:01 +0000195 getGCCRegAliases(Aliases, NumAliases);
Anders Carlsson6fa90862007-11-25 00:25:21 +0000196 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])
199 break;
Anders Carlsson83c021c2010-01-30 19:12:25 +0000200 if (Aliases[i].Aliases[j] == Name)
Anders Carlsson6fa90862007-11-25 00:25:21 +0000201 return true;
202 }
203 }
Mike Stump1eb44332009-09-09 15:08:12 +0000204
Anders Carlsson3346ae62007-11-24 23:38:12 +0000205 return false;
206}
Anders Carlssond04c6e22007-11-27 04:11:28 +0000207
Anders Carlsson83c021c2010-01-30 19:12:25 +0000208llvm::StringRef
209TargetInfo::getNormalizedGCCRegisterName(llvm::StringRef Name) const {
Anders Carlssond04c6e22007-11-27 04:11:28 +0000210 assert(isValidGCCRegisterName(Name) && "Invalid register passed in");
Mike Stump1eb44332009-09-09 15:08:12 +0000211
Anders Carlsson83c021c2010-01-30 19:12:25 +0000212 // Get rid of any register prefix.
213 Name = removeGCCRegisterPrefix(Name);
Mike Stump1eb44332009-09-09 15:08:12 +0000214
Anders Carlssond04c6e22007-11-27 04:11:28 +0000215 const char * const *Names;
216 unsigned NumNames;
217
Chris Lattner0eaed122008-03-08 08:24:01 +0000218 getGCCRegNames(Names, NumNames);
Anders Carlssond04c6e22007-11-27 04:11:28 +0000219
220 // First, check if we have a number.
221 if (isdigit(Name[0])) {
Anders Carlsson83c021c2010-01-30 19:12:25 +0000222 int n;
223 if (!Name.getAsInteger(0, n)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000224 assert(n >= 0 && (unsigned)n < NumNames &&
Anders Carlssond04c6e22007-11-27 04:11:28 +0000225 "Out of bounds register number!");
226 return Names[n];
227 }
228 }
Mike Stump1eb44332009-09-09 15:08:12 +0000229
Anders Carlssond04c6e22007-11-27 04:11:28 +0000230 // Now check aliases.
Chris Lattner0eaed122008-03-08 08:24:01 +0000231 const GCCRegAlias *Aliases;
Anders Carlssond04c6e22007-11-27 04:11:28 +0000232 unsigned NumAliases;
Mike Stump1eb44332009-09-09 15:08:12 +0000233
Chris Lattner0eaed122008-03-08 08:24:01 +0000234 getGCCRegAliases(Aliases, NumAliases);
Anders Carlssond04c6e22007-11-27 04:11:28 +0000235 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])
238 break;
Anders Carlsson83c021c2010-01-30 19:12:25 +0000239 if (Aliases[i].Aliases[j] == Name)
Anders Carlssond04c6e22007-11-27 04:11:28 +0000240 return Aliases[i].Register;
241 }
242 }
Mike Stump1eb44332009-09-09 15:08:12 +0000243
Anders Carlssond04c6e22007-11-27 04:11:28 +0000244 return Name;
245}
246
Chris Lattner432c8692009-04-26 17:19:08 +0000247bool TargetInfo::validateOutputConstraint(ConstraintInfo &Info) const {
248 const char *Name = Info.getConstraintStr().c_str();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000249 // An output constraint must start with '=' or '+'
250 if (*Name != '=' && *Name != '+')
251 return false;
252
253 if (*Name == '+')
Chris Lattner44def072009-04-26 07:16:29 +0000254 Info.setIsReadWrite();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000255
256 Name++;
257 while (*Name) {
258 switch (*Name) {
259 default:
Chris Lattner44def072009-04-26 07:16:29 +0000260 if (!validateAsmConstraint(Name, Info)) {
Ted Kremenek5ab201a2008-04-24 16:36:38 +0000261 // FIXME: We temporarily return false
Anders Carlssond04c6e22007-11-27 04:11:28 +0000262 // so we can add more constraints as we hit it.
263 // Eventually, an unknown constraint should just be treated as 'g'.
Ted Kremenek5ab201a2008-04-24 16:36:38 +0000264 return false;
Anders Carlssond04c6e22007-11-27 04:11:28 +0000265 }
266 case '&': // early clobber.
267 break;
Chris Lattner40539832009-10-13 04:32:07 +0000268 case '%': // commutative.
269 // FIXME: Check that there is a another register after this one.
270 break;
Anders Carlssond04c6e22007-11-27 04:11:28 +0000271 case 'r': // general register.
Chris Lattner44def072009-04-26 07:16:29 +0000272 Info.setAllowsRegister();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000273 break;
274 case 'm': // memory operand.
Chris Lattner44def072009-04-26 07:16:29 +0000275 Info.setAllowsMemory();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000276 break;
277 case 'g': // general register, memory operand or immediate integer.
Anders Carlsson775841f2009-01-18 02:12:04 +0000278 case 'X': // any operand.
Chris Lattner44def072009-04-26 07:16:29 +0000279 Info.setAllowsRegister();
280 Info.setAllowsMemory();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000281 break;
282 }
Mike Stump1eb44332009-09-09 15:08:12 +0000283
Anders Carlssond04c6e22007-11-27 04:11:28 +0000284 Name++;
285 }
Mike Stump1eb44332009-09-09 15:08:12 +0000286
Anders Carlssond04c6e22007-11-27 04:11:28 +0000287 return true;
288}
289
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000290bool TargetInfo::resolveSymbolicName(const char *&Name,
Chris Lattner2819fa82009-04-26 17:57:12 +0000291 ConstraintInfo *OutputConstraints,
292 unsigned NumOutputs,
Chris Lattner44def072009-04-26 07:16:29 +0000293 unsigned &Index) const {
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000294 assert(*Name == '[' && "Symbolic name did not start with '['");
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000295 Name++;
296 const char *Start = Name;
297 while (*Name && *Name != ']')
298 Name++;
Mike Stump1eb44332009-09-09 15:08:12 +0000299
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000300 if (!*Name) {
301 // Missing ']'
302 return false;
303 }
Mike Stump1eb44332009-09-09 15:08:12 +0000304
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000305 std::string SymbolicName(Start, Name - Start);
Mike Stump1eb44332009-09-09 15:08:12 +0000306
Chris Lattner2819fa82009-04-26 17:57:12 +0000307 for (Index = 0; Index != NumOutputs; ++Index)
308 if (SymbolicName == OutputConstraints[Index].getName())
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000309 return true;
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000310
311 return false;
312}
313
Chris Lattner2819fa82009-04-26 17:57:12 +0000314bool TargetInfo::validateInputConstraint(ConstraintInfo *OutputConstraints,
315 unsigned NumOutputs,
Chris Lattner44def072009-04-26 07:16:29 +0000316 ConstraintInfo &Info) const {
Chris Lattner432c8692009-04-26 17:19:08 +0000317 const char *Name = Info.ConstraintStr.c_str();
318
Anders Carlssond04c6e22007-11-27 04:11:28 +0000319 while (*Name) {
320 switch (*Name) {
321 default:
322 // Check if we have a matching constraint
323 if (*Name >= '0' && *Name <= '9') {
324 unsigned i = *Name - '0';
Mike Stump1eb44332009-09-09 15:08:12 +0000325
Anders Carlssond04c6e22007-11-27 04:11:28 +0000326 // Check if matching constraint is out of bounds.
327 if (i >= NumOutputs)
328 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000329
330 // The constraint should have the same info as the respective
Anders Carlsson03eb5432009-01-27 20:38:24 +0000331 // output constraint.
Chris Lattnerd6887612009-04-26 18:05:25 +0000332 Info.setTiedOperand(i, OutputConstraints[i]);
Chris Lattner44def072009-04-26 07:16:29 +0000333 } else if (!validateAsmConstraint(Name, Info)) {
Daniel Dunbar302684c2008-08-25 09:46:27 +0000334 // 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'.
337 return false;
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000338 }
339 break;
340 case '[': {
341 unsigned Index = 0;
Chris Lattner2819fa82009-04-26 17:57:12 +0000342 if (!resolveSymbolicName(Name, OutputConstraints, NumOutputs, Index))
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000343 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000344
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000345 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000346 }
Anders Carlsson44fe49c2007-12-08 19:32:57 +0000347 case '%': // commutative
348 // FIXME: Fail if % is used with the last operand.
349 break;
Anders Carlssond04c6e22007-11-27 04:11:28 +0000350 case 'i': // immediate integer.
Anders Carlssonefdfa772008-03-09 06:02:02 +0000351 case 'n': // immediate integer with a known value.
Anders Carlssond04c6e22007-11-27 04:11:28 +0000352 break;
Chris Lattner1d138792009-05-06 04:33:31 +0000353 case 'I': // Various constant constraints with target-specific meanings.
354 case 'J':
355 case 'K':
356 case 'L':
357 case 'M':
358 case 'N':
359 case 'O':
360 case 'P':
361 break;
Anders Carlssond04c6e22007-11-27 04:11:28 +0000362 case 'r': // general register.
Chris Lattner44def072009-04-26 07:16:29 +0000363 Info.setAllowsRegister();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000364 break;
365 case 'm': // memory operand.
Nuno Lopesaa8e3612009-12-16 14:28:21 +0000366 case 'o': // offsettable memory operand
367 case 'V': // non-offsettable memory operand
Chris Lattner44def072009-04-26 07:16:29 +0000368 Info.setAllowsMemory();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000369 break;
370 case 'g': // general register, memory operand or immediate integer.
Anders Carlsson775841f2009-01-18 02:12:04 +0000371 case 'X': // any operand.
Chris Lattner44def072009-04-26 07:16:29 +0000372 Info.setAllowsRegister();
373 Info.setAllowsMemory();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000374 break;
375 }
Mike Stump1eb44332009-09-09 15:08:12 +0000376
Anders Carlssond04c6e22007-11-27 04:11:28 +0000377 Name++;
378 }
Mike Stump1eb44332009-09-09 15:08:12 +0000379
Anders Carlssond04c6e22007-11-27 04:11:28 +0000380 return true;
381}