blob: e965b9aec369f18f15d03324dbc03edb9873b826 [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!");
99 case SignedShort: return getShortWidth();
100 case UnsignedShort: return getShortWidth();
101 case SignedInt: return getIntWidth();
102 case UnsignedInt: return getIntWidth();
103 case SignedLong: return getLongWidth();
104 case UnsignedLong: return getLongWidth();
105 case SignedLongLong: return getLongLongWidth();
106 case UnsignedLongLong: return getLongLongWidth();
107 };
108}
109
Chris Lattner961f0702009-10-25 22:49:18 +0000110/// isTypeSigned - Return whether an integer types is signed. Returns true if
Chris Lattnerb304f772009-10-21 06:24:21 +0000111/// the type is signed; false otherwise.
Chris Lattner961f0702009-10-25 22:49:18 +0000112bool TargetInfo::isTypeSigned(IntType T) const {
Chris Lattnerb304f772009-10-21 06:24:21 +0000113 switch (T) {
114 default: assert(0 && "not an integer!");
115 case SignedShort:
116 case SignedInt:
117 case SignedLong:
118 case SignedLongLong:
119 return true;
120 case UnsignedShort:
121 case UnsignedInt:
122 case UnsignedLong:
123 case UnsignedLongLong:
124 return false;
125 };
126}
127
John Thompsona6fda122009-11-05 20:14:16 +0000128/// setForcedLangOptions - Set forced language options.
129/// Apply changes to the target information with respect to certain
130/// language options which change the target configuration.
131void TargetInfo::setForcedLangOptions(LangOptions &Opts) {
132 if (Opts.ShortWChar) {
133 WCharType = UnsignedShort;
134 WCharWidth = WCharAlign = 16;
135 }
136}
Chris Lattnerb304f772009-10-21 06:24:21 +0000137
Chris Lattner525a0502007-09-22 18:29:59 +0000138//===----------------------------------------------------------------------===//
Chris Lattner525a0502007-09-22 18:29:59 +0000139
Reid Spencer5f016e22007-07-11 17:01:13 +0000140
Chris Lattner42e67372008-03-05 01:18:20 +0000141static void removeGCCRegisterPrefix(const char *&Name) {
Anders Carlssonea041752008-02-06 00:11:32 +0000142 if (Name[0] == '%' || Name[0] == '#')
143 Name++;
144}
145
Anders Carlsson3346ae62007-11-24 23:38:12 +0000146/// isValidGCCRegisterName - Returns whether the passed in string
147/// is a valid register name according to GCC. This is used by Sema for
148/// inline asm statements.
149bool TargetInfo::isValidGCCRegisterName(const char *Name) const {
Anders Carlsson6fa90862007-11-25 00:25:21 +0000150 const char * const *Names;
151 unsigned NumNames;
Mike Stump1eb44332009-09-09 15:08:12 +0000152
Anders Carlsson6fa90862007-11-25 00:25:21 +0000153 // Get rid of any register prefix.
Anders Carlssonea041752008-02-06 00:11:32 +0000154 removeGCCRegisterPrefix(Name);
155
Mike Stump1eb44332009-09-09 15:08:12 +0000156
Anders Carlsson6fa90862007-11-25 00:25:21 +0000157 if (strcmp(Name, "memory") == 0 ||
158 strcmp(Name, "cc") == 0)
159 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000160
Chris Lattner0eaed122008-03-08 08:24:01 +0000161 getGCCRegNames(Names, NumNames);
Mike Stump1eb44332009-09-09 15:08:12 +0000162
Anders Carlsson6fa90862007-11-25 00:25:21 +0000163 // If we have a number it maps to an entry in the register name array.
164 if (isdigit(Name[0])) {
165 char *End;
166 int n = (int)strtol(Name, &End, 0);
167 if (*End == 0)
168 return n >= 0 && (unsigned)n < NumNames;
169 }
170
171 // Check register names.
172 for (unsigned i = 0; i < NumNames; i++) {
173 if (strcmp(Name, Names[i]) == 0)
174 return true;
175 }
Mike Stump1eb44332009-09-09 15:08:12 +0000176
Anders Carlsson6fa90862007-11-25 00:25:21 +0000177 // Now check aliases.
Chris Lattner0eaed122008-03-08 08:24:01 +0000178 const GCCRegAlias *Aliases;
Anders Carlsson6fa90862007-11-25 00:25:21 +0000179 unsigned NumAliases;
Mike Stump1eb44332009-09-09 15:08:12 +0000180
Chris Lattner0eaed122008-03-08 08:24:01 +0000181 getGCCRegAliases(Aliases, NumAliases);
Anders Carlsson6fa90862007-11-25 00:25:21 +0000182 for (unsigned i = 0; i < NumAliases; i++) {
183 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
184 if (!Aliases[i].Aliases[j])
185 break;
186 if (strcmp(Aliases[i].Aliases[j], Name) == 0)
187 return true;
188 }
189 }
Mike Stump1eb44332009-09-09 15:08:12 +0000190
Anders Carlsson3346ae62007-11-24 23:38:12 +0000191 return false;
192}
Anders Carlssond04c6e22007-11-27 04:11:28 +0000193
Chris Lattner0eaed122008-03-08 08:24:01 +0000194const char *TargetInfo::getNormalizedGCCRegisterName(const char *Name) const {
Anders Carlssond04c6e22007-11-27 04:11:28 +0000195 assert(isValidGCCRegisterName(Name) && "Invalid register passed in");
Mike Stump1eb44332009-09-09 15:08:12 +0000196
Anders Carlssonea041752008-02-06 00:11:32 +0000197 removeGCCRegisterPrefix(Name);
Mike Stump1eb44332009-09-09 15:08:12 +0000198
Anders Carlssond04c6e22007-11-27 04:11:28 +0000199 const char * const *Names;
200 unsigned NumNames;
201
Chris Lattner0eaed122008-03-08 08:24:01 +0000202 getGCCRegNames(Names, NumNames);
Anders Carlssond04c6e22007-11-27 04:11:28 +0000203
204 // First, check if we have a number.
205 if (isdigit(Name[0])) {
206 char *End;
207 int n = (int)strtol(Name, &End, 0);
208 if (*End == 0) {
Mike Stump1eb44332009-09-09 15:08:12 +0000209 assert(n >= 0 && (unsigned)n < NumNames &&
Anders Carlssond04c6e22007-11-27 04:11:28 +0000210 "Out of bounds register number!");
211 return Names[n];
212 }
213 }
Mike Stump1eb44332009-09-09 15:08:12 +0000214
Anders Carlssond04c6e22007-11-27 04:11:28 +0000215 // Now check aliases.
Chris Lattner0eaed122008-03-08 08:24:01 +0000216 const GCCRegAlias *Aliases;
Anders Carlssond04c6e22007-11-27 04:11:28 +0000217 unsigned NumAliases;
Mike Stump1eb44332009-09-09 15:08:12 +0000218
Chris Lattner0eaed122008-03-08 08:24:01 +0000219 getGCCRegAliases(Aliases, NumAliases);
Anders Carlssond04c6e22007-11-27 04:11:28 +0000220 for (unsigned i = 0; i < NumAliases; i++) {
221 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
222 if (!Aliases[i].Aliases[j])
223 break;
224 if (strcmp(Aliases[i].Aliases[j], Name) == 0)
225 return Aliases[i].Register;
226 }
227 }
Mike Stump1eb44332009-09-09 15:08:12 +0000228
Anders Carlssond04c6e22007-11-27 04:11:28 +0000229 return Name;
230}
231
Chris Lattner432c8692009-04-26 17:19:08 +0000232bool TargetInfo::validateOutputConstraint(ConstraintInfo &Info) const {
233 const char *Name = Info.getConstraintStr().c_str();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000234 // An output constraint must start with '=' or '+'
235 if (*Name != '=' && *Name != '+')
236 return false;
237
238 if (*Name == '+')
Chris Lattner44def072009-04-26 07:16:29 +0000239 Info.setIsReadWrite();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000240
241 Name++;
242 while (*Name) {
243 switch (*Name) {
244 default:
Chris Lattner44def072009-04-26 07:16:29 +0000245 if (!validateAsmConstraint(Name, Info)) {
Ted Kremenek5ab201a2008-04-24 16:36:38 +0000246 // FIXME: We temporarily return false
Anders Carlssond04c6e22007-11-27 04:11:28 +0000247 // so we can add more constraints as we hit it.
248 // Eventually, an unknown constraint should just be treated as 'g'.
Ted Kremenek5ab201a2008-04-24 16:36:38 +0000249 return false;
Anders Carlssond04c6e22007-11-27 04:11:28 +0000250 }
251 case '&': // early clobber.
252 break;
Chris Lattner40539832009-10-13 04:32:07 +0000253 case '%': // commutative.
254 // FIXME: Check that there is a another register after this one.
255 break;
Anders Carlssond04c6e22007-11-27 04:11:28 +0000256 case 'r': // general register.
Chris Lattner44def072009-04-26 07:16:29 +0000257 Info.setAllowsRegister();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000258 break;
259 case 'm': // memory operand.
Chris Lattner44def072009-04-26 07:16:29 +0000260 Info.setAllowsMemory();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000261 break;
262 case 'g': // general register, memory operand or immediate integer.
Anders Carlsson775841f2009-01-18 02:12:04 +0000263 case 'X': // any operand.
Chris Lattner44def072009-04-26 07:16:29 +0000264 Info.setAllowsRegister();
265 Info.setAllowsMemory();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000266 break;
267 }
Mike Stump1eb44332009-09-09 15:08:12 +0000268
Anders Carlssond04c6e22007-11-27 04:11:28 +0000269 Name++;
270 }
Mike Stump1eb44332009-09-09 15:08:12 +0000271
Anders Carlssond04c6e22007-11-27 04:11:28 +0000272 return true;
273}
274
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000275bool TargetInfo::resolveSymbolicName(const char *&Name,
Chris Lattner2819fa82009-04-26 17:57:12 +0000276 ConstraintInfo *OutputConstraints,
277 unsigned NumOutputs,
Chris Lattner44def072009-04-26 07:16:29 +0000278 unsigned &Index) const {
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000279 assert(*Name == '[' && "Symbolic name did not start with '['");
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000280 Name++;
281 const char *Start = Name;
282 while (*Name && *Name != ']')
283 Name++;
Mike Stump1eb44332009-09-09 15:08:12 +0000284
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000285 if (!*Name) {
286 // Missing ']'
287 return false;
288 }
Mike Stump1eb44332009-09-09 15:08:12 +0000289
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000290 std::string SymbolicName(Start, Name - Start);
Mike Stump1eb44332009-09-09 15:08:12 +0000291
Chris Lattner2819fa82009-04-26 17:57:12 +0000292 for (Index = 0; Index != NumOutputs; ++Index)
293 if (SymbolicName == OutputConstraints[Index].getName())
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000294 return true;
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000295
296 return false;
297}
298
Chris Lattner2819fa82009-04-26 17:57:12 +0000299bool TargetInfo::validateInputConstraint(ConstraintInfo *OutputConstraints,
300 unsigned NumOutputs,
Chris Lattner44def072009-04-26 07:16:29 +0000301 ConstraintInfo &Info) const {
Chris Lattner432c8692009-04-26 17:19:08 +0000302 const char *Name = Info.ConstraintStr.c_str();
303
Anders Carlssond04c6e22007-11-27 04:11:28 +0000304 while (*Name) {
305 switch (*Name) {
306 default:
307 // Check if we have a matching constraint
308 if (*Name >= '0' && *Name <= '9') {
309 unsigned i = *Name - '0';
Mike Stump1eb44332009-09-09 15:08:12 +0000310
Anders Carlssond04c6e22007-11-27 04:11:28 +0000311 // Check if matching constraint is out of bounds.
312 if (i >= NumOutputs)
313 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000314
315 // The constraint should have the same info as the respective
Anders Carlsson03eb5432009-01-27 20:38:24 +0000316 // output constraint.
Chris Lattnerd6887612009-04-26 18:05:25 +0000317 Info.setTiedOperand(i, OutputConstraints[i]);
Chris Lattner44def072009-04-26 07:16:29 +0000318 } else if (!validateAsmConstraint(Name, Info)) {
Daniel Dunbar302684c2008-08-25 09:46:27 +0000319 // FIXME: This error return is in place temporarily so we can
320 // add more constraints as we hit it. Eventually, an unknown
321 // constraint should just be treated as 'g'.
322 return false;
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000323 }
324 break;
325 case '[': {
326 unsigned Index = 0;
Chris Lattner2819fa82009-04-26 17:57:12 +0000327 if (!resolveSymbolicName(Name, OutputConstraints, NumOutputs, Index))
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000328 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000329
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000330 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000331 }
Anders Carlsson44fe49c2007-12-08 19:32:57 +0000332 case '%': // commutative
333 // FIXME: Fail if % is used with the last operand.
334 break;
Anders Carlssond04c6e22007-11-27 04:11:28 +0000335 case 'i': // immediate integer.
Anders Carlssonefdfa772008-03-09 06:02:02 +0000336 case 'n': // immediate integer with a known value.
Anders Carlssond04c6e22007-11-27 04:11:28 +0000337 break;
Chris Lattner1d138792009-05-06 04:33:31 +0000338 case 'I': // Various constant constraints with target-specific meanings.
339 case 'J':
340 case 'K':
341 case 'L':
342 case 'M':
343 case 'N':
344 case 'O':
345 case 'P':
346 break;
Anders Carlssond04c6e22007-11-27 04:11:28 +0000347 case 'r': // general register.
Chris Lattner44def072009-04-26 07:16:29 +0000348 Info.setAllowsRegister();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000349 break;
350 case 'm': // memory operand.
Chris Lattner44def072009-04-26 07:16:29 +0000351 Info.setAllowsMemory();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000352 break;
353 case 'g': // general register, memory operand or immediate integer.
Anders Carlsson775841f2009-01-18 02:12:04 +0000354 case 'X': // any operand.
Chris Lattner44def072009-04-26 07:16:29 +0000355 Info.setAllowsRegister();
356 Info.setAllowsMemory();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000357 break;
358 }
Mike Stump1eb44332009-09-09 15:08:12 +0000359
Anders Carlssond04c6e22007-11-27 04:11:28 +0000360 Name++;
361 }
Mike Stump1eb44332009-09-09 15:08:12 +0000362
Anders Carlssond04c6e22007-11-27 04:11:28 +0000363 return true;
364}