blob: 34d3fb2145af81368d439dab85b9728c4a1272e4 [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 Lattnercd4fc422008-03-08 08:59:43 +000028 WCharWidth = WCharAlign = 32;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +000029 Char16Width = Char16Align = 16;
30 Char32Width = Char32Align = 32;
Chris Lattner2621fd12008-05-08 05:58:21 +000031 IntWidth = IntAlign = 32;
Chris Lattnerec10f582008-05-09 05:50:02 +000032 LongWidth = LongAlign = 32;
33 LongLongWidth = LongLongAlign = 64;
Eli Friedman61538a72008-05-20 14:21:01 +000034 FloatWidth = 32;
35 FloatAlign = 32;
Nate Begeman3d292062008-04-18 17:17:24 +000036 DoubleWidth = 64;
Eli Friedman61538a72008-05-20 14:21:01 +000037 DoubleAlign = 64;
38 LongDoubleWidth = 64;
39 LongDoubleAlign = 64;
Nate Begeman22b9d5a2009-01-17 23:56:13 +000040 IntMaxTWidth = 64;
Anders Carlsson6a3615c2008-10-31 16:05:19 +000041 SizeType = UnsignedLong;
Eli Friedmanf509d732008-11-02 02:43:55 +000042 PtrDiffType = SignedLong;
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +000043 IntMaxType = SignedLongLong;
44 UIntMaxType = UnsignedLongLong;
Chris Lattner6ad474f2009-02-13 22:28:55 +000045 IntPtrType = SignedLong;
Eli Friedmanf509d732008-11-02 02:43:55 +000046 WCharType = SignedInt;
Chris Lattnere64ef802009-10-21 04:59:34 +000047 WIntType = SignedInt;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +000048 Char16Type = UnsignedShort;
49 Char32Type = UnsignedInt;
Eli Friedman3c7b6e42009-07-01 03:36:11 +000050 Int64Type = SignedLongLong;
Chris Lattnercd4fc422008-03-08 08:59:43 +000051 FloatFormat = &llvm::APFloat::IEEEsingle;
52 DoubleFormat = &llvm::APFloat::IEEEdouble;
53 LongDoubleFormat = &llvm::APFloat::IEEEdouble;
Eli Friedmaned855cb2008-08-21 00:13:15 +000054 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";
Chris Lattner3fdf4672008-10-05 19:22:37 +000056 UserLabelPrefix = "_";
Chris Lattnercd4fc422008-03-08 08:59:43 +000057}
58
Chris Lattner0eaed122008-03-08 08:24:01 +000059// Out of line virtual dtor for TargetInfo.
60TargetInfo::~TargetInfo() {}
Reid Spencer5f016e22007-07-11 17:01:13 +000061
Chris Lattner2b5abf52009-02-06 05:04:11 +000062/// getTypeName - Return the user string for the specified integer type enum.
63/// For example, SignedShort -> "short".
64const char *TargetInfo::getTypeName(IntType T) {
65 switch (T) {
66 default: assert(0 && "not an integer!");
67 case SignedShort: return "short";
68 case UnsignedShort: return "unsigned short";
69 case SignedInt: return "int";
70 case UnsignedInt: return "unsigned int";
71 case SignedLong: return "long int";
72 case UnsignedLong: return "long unsigned int";
73 case SignedLongLong: return "long long int";
74 case UnsignedLongLong: return "long long unsigned int";
75 }
76}
77
Chris Lattnerb304f772009-10-21 06:24:21 +000078/// getTypeConstantSuffix - Return the constant suffix for the specified
79/// integer type enum. For example, SignedLong -> "L".
80const char *TargetInfo::getTypeConstantSuffix(IntType T) {
81 switch (T) {
82 default: assert(0 && "not an integer!");
83 case SignedShort:
84 case SignedInt: return "";
85 case SignedLong: return "L";
86 case SignedLongLong: return "LL";
87 case UnsignedShort:
88 case UnsignedInt: return "U";
89 case UnsignedLong: return "UL";
90 case UnsignedLongLong: return "ULL";
91 }
92}
93
94/// getTypeWidth - Return the width (in bits) of the specified integer type
95/// enum. For example, SignedInt -> getIntWidth().
96unsigned TargetInfo::getTypeWidth(IntType T) const {
97 switch (T) {
98 default: assert(0 && "not an integer!");
Chris Lattner9099e7b2009-11-05 21:21:32 +000099 case SignedShort:
Chris Lattnerb304f772009-10-21 06:24:21 +0000100 case UnsignedShort: return getShortWidth();
Chris Lattner9099e7b2009-11-05 21:21:32 +0000101 case SignedInt:
Chris Lattnerb304f772009-10-21 06:24:21 +0000102 case UnsignedInt: return getIntWidth();
Chris Lattner9099e7b2009-11-05 21:21:32 +0000103 case SignedLong:
Chris Lattnerb304f772009-10-21 06:24:21 +0000104 case UnsignedLong: return getLongWidth();
Chris Lattner9099e7b2009-11-05 21:21:32 +0000105 case SignedLongLong:
Chris Lattnerb304f772009-10-21 06:24:21 +0000106 case UnsignedLongLong: return getLongLongWidth();
107 };
108}
109
Chris Lattner9099e7b2009-11-05 21:21:32 +0000110/// getTypeAlign - Return the alignment (in bits) of the specified integer type
111/// enum. For example, SignedInt -> getIntAlign().
112unsigned TargetInfo::getTypeAlign(IntType T) const {
113 switch (T) {
114 default: assert(0 && "not an integer!");
115 case SignedShort:
116 case UnsignedShort: return getShortAlign();
117 case SignedInt:
118 case UnsignedInt: return getIntAlign();
119 case SignedLong:
120 case UnsignedLong: return getLongAlign();
121 case SignedLongLong:
122 case UnsignedLongLong: return getLongLongAlign();
123 };
124}
125
Chris Lattner961f0702009-10-25 22:49:18 +0000126/// isTypeSigned - Return whether an integer types is signed. Returns true if
Chris Lattnerb304f772009-10-21 06:24:21 +0000127/// the type is signed; false otherwise.
Chris Lattner961f0702009-10-25 22:49:18 +0000128bool TargetInfo::isTypeSigned(IntType T) const {
Chris Lattnerb304f772009-10-21 06:24:21 +0000129 switch (T) {
130 default: assert(0 && "not an integer!");
131 case SignedShort:
132 case SignedInt:
133 case SignedLong:
134 case SignedLongLong:
135 return true;
136 case UnsignedShort:
137 case UnsignedInt:
138 case UnsignedLong:
139 case UnsignedLongLong:
140 return false;
141 };
142}
143
John Thompsona6fda122009-11-05 20:14:16 +0000144/// setForcedLangOptions - Set forced language options.
145/// Apply changes to the target information with respect to certain
146/// language options which change the target configuration.
147void TargetInfo::setForcedLangOptions(LangOptions &Opts) {
John Thompson40d1bb62009-11-05 22:03:02 +0000148 if (Opts.ShortWChar) {
John Thompsona6fda122009-11-05 20:14:16 +0000149 WCharType = UnsignedShort;
150 WCharWidth = WCharAlign = 16;
John Thompson40d1bb62009-11-05 22:03:02 +0000151 }
John Thompsona6fda122009-11-05 20:14:16 +0000152}
Chris Lattnerb304f772009-10-21 06:24:21 +0000153
Chris Lattner525a0502007-09-22 18:29:59 +0000154//===----------------------------------------------------------------------===//
Chris Lattner525a0502007-09-22 18:29:59 +0000155
Reid Spencer5f016e22007-07-11 17:01:13 +0000156
Chris Lattner42e67372008-03-05 01:18:20 +0000157static void removeGCCRegisterPrefix(const char *&Name) {
Anders Carlssonea041752008-02-06 00:11:32 +0000158 if (Name[0] == '%' || Name[0] == '#')
159 Name++;
160}
161
Anders Carlsson3346ae62007-11-24 23:38:12 +0000162/// 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.
165bool TargetInfo::isValidGCCRegisterName(const char *Name) const {
Anders Carlsson6fa90862007-11-25 00:25:21 +0000166 const char * const *Names;
167 unsigned NumNames;
Mike Stump1eb44332009-09-09 15:08:12 +0000168
Anders Carlsson6fa90862007-11-25 00:25:21 +0000169 // Get rid of any register prefix.
Anders Carlssonea041752008-02-06 00:11:32 +0000170 removeGCCRegisterPrefix(Name);
171
Mike Stump1eb44332009-09-09 15:08:12 +0000172
Anders Carlsson6fa90862007-11-25 00:25:21 +0000173 if (strcmp(Name, "memory") == 0 ||
174 strcmp(Name, "cc") == 0)
175 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000176
Chris Lattner0eaed122008-03-08 08:24:01 +0000177 getGCCRegNames(Names, NumNames);
Mike Stump1eb44332009-09-09 15:08:12 +0000178
Anders Carlsson6fa90862007-11-25 00:25:21 +0000179 // If we have a number it maps to an entry in the register name array.
180 if (isdigit(Name[0])) {
181 char *End;
182 int n = (int)strtol(Name, &End, 0);
183 if (*End == 0)
184 return n >= 0 && (unsigned)n < NumNames;
185 }
186
187 // Check register names.
188 for (unsigned i = 0; i < NumNames; i++) {
189 if (strcmp(Name, Names[i]) == 0)
190 return true;
191 }
Mike Stump1eb44332009-09-09 15:08:12 +0000192
Anders Carlsson6fa90862007-11-25 00:25:21 +0000193 // Now check aliases.
Chris Lattner0eaed122008-03-08 08:24:01 +0000194 const GCCRegAlias *Aliases;
Anders Carlsson6fa90862007-11-25 00:25:21 +0000195 unsigned NumAliases;
Mike Stump1eb44332009-09-09 15:08:12 +0000196
Chris Lattner0eaed122008-03-08 08:24:01 +0000197 getGCCRegAliases(Aliases, NumAliases);
Anders Carlsson6fa90862007-11-25 00:25:21 +0000198 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])
201 break;
202 if (strcmp(Aliases[i].Aliases[j], Name) == 0)
203 return true;
204 }
205 }
Mike Stump1eb44332009-09-09 15:08:12 +0000206
Anders Carlsson3346ae62007-11-24 23:38:12 +0000207 return false;
208}
Anders Carlssond04c6e22007-11-27 04:11:28 +0000209
Chris Lattner0eaed122008-03-08 08:24:01 +0000210const char *TargetInfo::getNormalizedGCCRegisterName(const char *Name) const {
Anders Carlssond04c6e22007-11-27 04:11:28 +0000211 assert(isValidGCCRegisterName(Name) && "Invalid register passed in");
Mike Stump1eb44332009-09-09 15:08:12 +0000212
Anders Carlssonea041752008-02-06 00:11:32 +0000213 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])) {
222 char *End;
223 int n = (int)strtol(Name, &End, 0);
224 if (*End == 0) {
Mike Stump1eb44332009-09-09 15:08:12 +0000225 assert(n >= 0 && (unsigned)n < NumNames &&
Anders Carlssond04c6e22007-11-27 04:11:28 +0000226 "Out of bounds register number!");
227 return Names[n];
228 }
229 }
Mike Stump1eb44332009-09-09 15:08:12 +0000230
Anders Carlssond04c6e22007-11-27 04:11:28 +0000231 // Now check aliases.
Chris Lattner0eaed122008-03-08 08:24:01 +0000232 const GCCRegAlias *Aliases;
Anders Carlssond04c6e22007-11-27 04:11:28 +0000233 unsigned NumAliases;
Mike Stump1eb44332009-09-09 15:08:12 +0000234
Chris Lattner0eaed122008-03-08 08:24:01 +0000235 getGCCRegAliases(Aliases, NumAliases);
Anders Carlssond04c6e22007-11-27 04:11:28 +0000236 for (unsigned i = 0; i < NumAliases; i++) {
237 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
238 if (!Aliases[i].Aliases[j])
239 break;
240 if (strcmp(Aliases[i].Aliases[j], Name) == 0)
241 return Aliases[i].Register;
242 }
243 }
Mike Stump1eb44332009-09-09 15:08:12 +0000244
Anders Carlssond04c6e22007-11-27 04:11:28 +0000245 return Name;
246}
247
Chris Lattner432c8692009-04-26 17:19:08 +0000248bool TargetInfo::validateOutputConstraint(ConstraintInfo &Info) const {
249 const char *Name = Info.getConstraintStr().c_str();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000250 // An output constraint must start with '=' or '+'
251 if (*Name != '=' && *Name != '+')
252 return false;
253
254 if (*Name == '+')
Chris Lattner44def072009-04-26 07:16:29 +0000255 Info.setIsReadWrite();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000256
257 Name++;
258 while (*Name) {
259 switch (*Name) {
260 default:
Chris Lattner44def072009-04-26 07:16:29 +0000261 if (!validateAsmConstraint(Name, Info)) {
Ted Kremenek5ab201a2008-04-24 16:36:38 +0000262 // FIXME: We temporarily return false
Anders Carlssond04c6e22007-11-27 04:11:28 +0000263 // so we can add more constraints as we hit it.
264 // Eventually, an unknown constraint should just be treated as 'g'.
Ted Kremenek5ab201a2008-04-24 16:36:38 +0000265 return false;
Anders Carlssond04c6e22007-11-27 04:11:28 +0000266 }
267 case '&': // early clobber.
268 break;
Chris Lattner40539832009-10-13 04:32:07 +0000269 case '%': // commutative.
270 // FIXME: Check that there is a another register after this one.
271 break;
Anders Carlssond04c6e22007-11-27 04:11:28 +0000272 case 'r': // general register.
Chris Lattner44def072009-04-26 07:16:29 +0000273 Info.setAllowsRegister();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000274 break;
275 case 'm': // memory operand.
Chris Lattner44def072009-04-26 07:16:29 +0000276 Info.setAllowsMemory();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000277 break;
278 case 'g': // general register, memory operand or immediate integer.
Anders Carlsson775841f2009-01-18 02:12:04 +0000279 case 'X': // any operand.
Chris Lattner44def072009-04-26 07:16:29 +0000280 Info.setAllowsRegister();
281 Info.setAllowsMemory();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000282 break;
283 }
Mike Stump1eb44332009-09-09 15:08:12 +0000284
Anders Carlssond04c6e22007-11-27 04:11:28 +0000285 Name++;
286 }
Mike Stump1eb44332009-09-09 15:08:12 +0000287
Anders Carlssond04c6e22007-11-27 04:11:28 +0000288 return true;
289}
290
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000291bool TargetInfo::resolveSymbolicName(const char *&Name,
Chris Lattner2819fa82009-04-26 17:57:12 +0000292 ConstraintInfo *OutputConstraints,
293 unsigned NumOutputs,
Chris Lattner44def072009-04-26 07:16:29 +0000294 unsigned &Index) const {
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000295 assert(*Name == '[' && "Symbolic name did not start with '['");
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000296 Name++;
297 const char *Start = Name;
298 while (*Name && *Name != ']')
299 Name++;
Mike Stump1eb44332009-09-09 15:08:12 +0000300
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000301 if (!*Name) {
302 // Missing ']'
303 return false;
304 }
Mike Stump1eb44332009-09-09 15:08:12 +0000305
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000306 std::string SymbolicName(Start, Name - Start);
Mike Stump1eb44332009-09-09 15:08:12 +0000307
Chris Lattner2819fa82009-04-26 17:57:12 +0000308 for (Index = 0; Index != NumOutputs; ++Index)
309 if (SymbolicName == OutputConstraints[Index].getName())
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000310 return true;
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000311
312 return false;
313}
314
Chris Lattner2819fa82009-04-26 17:57:12 +0000315bool TargetInfo::validateInputConstraint(ConstraintInfo *OutputConstraints,
316 unsigned NumOutputs,
Chris Lattner44def072009-04-26 07:16:29 +0000317 ConstraintInfo &Info) const {
Chris Lattner432c8692009-04-26 17:19:08 +0000318 const char *Name = Info.ConstraintStr.c_str();
319
Anders Carlssond04c6e22007-11-27 04:11:28 +0000320 while (*Name) {
321 switch (*Name) {
322 default:
323 // Check if we have a matching constraint
324 if (*Name >= '0' && *Name <= '9') {
325 unsigned i = *Name - '0';
Mike Stump1eb44332009-09-09 15:08:12 +0000326
Anders Carlssond04c6e22007-11-27 04:11:28 +0000327 // Check if matching constraint is out of bounds.
328 if (i >= NumOutputs)
329 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000330
331 // The constraint should have the same info as the respective
Anders Carlsson03eb5432009-01-27 20:38:24 +0000332 // output constraint.
Chris Lattnerd6887612009-04-26 18:05:25 +0000333 Info.setTiedOperand(i, OutputConstraints[i]);
Chris Lattner44def072009-04-26 07:16:29 +0000334 } else if (!validateAsmConstraint(Name, Info)) {
Daniel Dunbar302684c2008-08-25 09:46:27 +0000335 // FIXME: This error return is in place temporarily so we can
336 // add more constraints as we hit it. Eventually, an unknown
337 // constraint should just be treated as 'g'.
338 return false;
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000339 }
340 break;
341 case '[': {
342 unsigned Index = 0;
Chris Lattner2819fa82009-04-26 17:57:12 +0000343 if (!resolveSymbolicName(Name, OutputConstraints, NumOutputs, Index))
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000344 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000345
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000346 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000347 }
Anders Carlsson44fe49c2007-12-08 19:32:57 +0000348 case '%': // commutative
349 // FIXME: Fail if % is used with the last operand.
350 break;
Anders Carlssond04c6e22007-11-27 04:11:28 +0000351 case 'i': // immediate integer.
Anders Carlssonefdfa772008-03-09 06:02:02 +0000352 case 'n': // immediate integer with a known value.
Anders Carlssond04c6e22007-11-27 04:11:28 +0000353 break;
Chris Lattner1d138792009-05-06 04:33:31 +0000354 case 'I': // Various constant constraints with target-specific meanings.
355 case 'J':
356 case 'K':
357 case 'L':
358 case 'M':
359 case 'N':
360 case 'O':
361 case 'P':
362 break;
Anders Carlssond04c6e22007-11-27 04:11:28 +0000363 case 'r': // general register.
Chris Lattner44def072009-04-26 07:16:29 +0000364 Info.setAllowsRegister();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000365 break;
366 case 'm': // memory operand.
Chris Lattner44def072009-04-26 07:16:29 +0000367 Info.setAllowsMemory();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000368 break;
369 case 'g': // general register, memory operand or immediate integer.
Anders Carlsson775841f2009-01-18 02:12:04 +0000370 case 'X': // any operand.
Chris Lattner44def072009-04-26 07:16:29 +0000371 Info.setAllowsRegister();
372 Info.setAllowsMemory();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000373 break;
374 }
Mike Stump1eb44332009-09-09 15:08:12 +0000375
Anders Carlssond04c6e22007-11-27 04:11:28 +0000376 Name++;
377 }
Mike Stump1eb44332009-09-09 15:08:12 +0000378
Anders Carlssond04c6e22007-11-27 04:11:28 +0000379 return true;
380}