blob: 5b2ffb7d165c79dcf8cbcfdea86e8b780043a888 [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"
Chris Lattner525a0502007-09-22 18:29:59 +000015#include "llvm/ADT/APFloat.h"
Anders Carlsson6fa90862007-11-25 00:25:21 +000016#include "llvm/ADT/STLExtras.h"
Chris Lattner5483bdf2008-04-06 04:02:29 +000017#include <cstdlib>
Reid Spencer5f016e22007-07-11 17:01:13 +000018using namespace clang;
19
Chris Lattnercd4fc422008-03-08 08:59:43 +000020// TargetInfo Constructor.
21TargetInfo::TargetInfo(const std::string &T) : Triple(T) {
Eli Friedman61538a72008-05-20 14:21:01 +000022 // Set defaults. Defaults are set for a 32-bit RISC platform,
23 // like PPC or SPARC.
24 // These should be overridden by concrete targets as needed.
Eli Friedmanb030f022009-04-19 21:38:35 +000025 TLSSupported = true;
Chris Lattner927686f2008-05-09 06:08:39 +000026 PointerWidth = PointerAlign = 32;
Chris Lattnercd4fc422008-03-08 08:59:43 +000027 WCharWidth = WCharAlign = 32;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +000028 Char16Width = Char16Align = 16;
29 Char32Width = Char32Align = 32;
Chris Lattner2621fd12008-05-08 05:58:21 +000030 IntWidth = IntAlign = 32;
Chris Lattnerec10f582008-05-09 05:50:02 +000031 LongWidth = LongAlign = 32;
32 LongLongWidth = LongLongAlign = 64;
Eli Friedman61538a72008-05-20 14:21:01 +000033 FloatWidth = 32;
34 FloatAlign = 32;
Nate Begeman3d292062008-04-18 17:17:24 +000035 DoubleWidth = 64;
Eli Friedman61538a72008-05-20 14:21:01 +000036 DoubleAlign = 64;
37 LongDoubleWidth = 64;
38 LongDoubleAlign = 64;
Nate Begeman22b9d5a2009-01-17 23:56:13 +000039 IntMaxTWidth = 64;
Anders Carlsson6a3615c2008-10-31 16:05:19 +000040 SizeType = UnsignedLong;
Eli Friedmanf509d732008-11-02 02:43:55 +000041 PtrDiffType = SignedLong;
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +000042 IntMaxType = SignedLongLong;
43 UIntMaxType = UnsignedLongLong;
Chris Lattner6ad474f2009-02-13 22:28:55 +000044 IntPtrType = SignedLong;
Eli Friedmanf509d732008-11-02 02:43:55 +000045 WCharType = SignedInt;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +000046 Char16Type = UnsignedShort;
47 Char32Type = UnsignedInt;
Eli Friedman3c7b6e42009-07-01 03:36:11 +000048 Int64Type = SignedLongLong;
Chris Lattnercd4fc422008-03-08 08:59:43 +000049 FloatFormat = &llvm::APFloat::IEEEsingle;
50 DoubleFormat = &llvm::APFloat::IEEEdouble;
51 LongDoubleFormat = &llvm::APFloat::IEEEdouble;
Eli Friedmaned855cb2008-08-21 00:13:15 +000052 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";
Chris Lattner3fdf4672008-10-05 19:22:37 +000054 UserLabelPrefix = "_";
Chris Lattnercd4fc422008-03-08 08:59:43 +000055}
56
Chris Lattner0eaed122008-03-08 08:24:01 +000057// Out of line virtual dtor for TargetInfo.
58TargetInfo::~TargetInfo() {}
Reid Spencer5f016e22007-07-11 17:01:13 +000059
Chris Lattner2b5abf52009-02-06 05:04:11 +000060/// getTypeName - Return the user string for the specified integer type enum.
61/// For example, SignedShort -> "short".
62const char *TargetInfo::getTypeName(IntType T) {
63 switch (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";
73 }
74}
75
Chris Lattner525a0502007-09-22 18:29:59 +000076//===----------------------------------------------------------------------===//
Chris Lattner525a0502007-09-22 18:29:59 +000077
Reid Spencer5f016e22007-07-11 17:01:13 +000078
Chris Lattner42e67372008-03-05 01:18:20 +000079static void removeGCCRegisterPrefix(const char *&Name) {
Anders Carlssonea041752008-02-06 00:11:32 +000080 if (Name[0] == '%' || Name[0] == '#')
81 Name++;
82}
83
Anders Carlsson3346ae62007-11-24 23:38:12 +000084/// isValidGCCRegisterName - Returns whether the passed in string
85/// is a valid register name according to GCC. This is used by Sema for
86/// inline asm statements.
87bool TargetInfo::isValidGCCRegisterName(const char *Name) const {
Anders Carlsson6fa90862007-11-25 00:25:21 +000088 const char * const *Names;
89 unsigned NumNames;
90
91 // Get rid of any register prefix.
Anders Carlssonea041752008-02-06 00:11:32 +000092 removeGCCRegisterPrefix(Name);
93
Anders Carlsson6fa90862007-11-25 00:25:21 +000094
95 if (strcmp(Name, "memory") == 0 ||
96 strcmp(Name, "cc") == 0)
97 return true;
98
Chris Lattner0eaed122008-03-08 08:24:01 +000099 getGCCRegNames(Names, NumNames);
Anders Carlsson6fa90862007-11-25 00:25:21 +0000100
101 // If we have a number it maps to an entry in the register name array.
102 if (isdigit(Name[0])) {
103 char *End;
104 int n = (int)strtol(Name, &End, 0);
105 if (*End == 0)
106 return n >= 0 && (unsigned)n < NumNames;
107 }
108
109 // Check register names.
110 for (unsigned i = 0; i < NumNames; i++) {
111 if (strcmp(Name, Names[i]) == 0)
112 return true;
113 }
114
115 // Now check aliases.
Chris Lattner0eaed122008-03-08 08:24:01 +0000116 const GCCRegAlias *Aliases;
Anders Carlsson6fa90862007-11-25 00:25:21 +0000117 unsigned NumAliases;
118
Chris Lattner0eaed122008-03-08 08:24:01 +0000119 getGCCRegAliases(Aliases, NumAliases);
Anders Carlsson6fa90862007-11-25 00:25:21 +0000120 for (unsigned i = 0; i < NumAliases; i++) {
121 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
122 if (!Aliases[i].Aliases[j])
123 break;
124 if (strcmp(Aliases[i].Aliases[j], Name) == 0)
125 return true;
126 }
127 }
128
Anders Carlsson3346ae62007-11-24 23:38:12 +0000129 return false;
130}
Anders Carlssond04c6e22007-11-27 04:11:28 +0000131
Chris Lattner0eaed122008-03-08 08:24:01 +0000132const char *TargetInfo::getNormalizedGCCRegisterName(const char *Name) const {
Anders Carlssond04c6e22007-11-27 04:11:28 +0000133 assert(isValidGCCRegisterName(Name) && "Invalid register passed in");
134
Anders Carlssonea041752008-02-06 00:11:32 +0000135 removeGCCRegisterPrefix(Name);
Anders Carlssonef3577d2008-02-05 23:30:20 +0000136
Anders Carlssond04c6e22007-11-27 04:11:28 +0000137 const char * const *Names;
138 unsigned NumNames;
139
Chris Lattner0eaed122008-03-08 08:24:01 +0000140 getGCCRegNames(Names, NumNames);
Anders Carlssond04c6e22007-11-27 04:11:28 +0000141
142 // First, check if we have a number.
143 if (isdigit(Name[0])) {
144 char *End;
145 int n = (int)strtol(Name, &End, 0);
146 if (*End == 0) {
147 assert(n >= 0 && (unsigned)n < NumNames &&
148 "Out of bounds register number!");
149 return Names[n];
150 }
151 }
152
153 // Now check aliases.
Chris Lattner0eaed122008-03-08 08:24:01 +0000154 const GCCRegAlias *Aliases;
Anders Carlssond04c6e22007-11-27 04:11:28 +0000155 unsigned NumAliases;
156
Chris Lattner0eaed122008-03-08 08:24:01 +0000157 getGCCRegAliases(Aliases, NumAliases);
Anders Carlssond04c6e22007-11-27 04:11:28 +0000158 for (unsigned i = 0; i < NumAliases; i++) {
159 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
160 if (!Aliases[i].Aliases[j])
161 break;
162 if (strcmp(Aliases[i].Aliases[j], Name) == 0)
163 return Aliases[i].Register;
164 }
165 }
166
167 return Name;
168}
169
Chris Lattner432c8692009-04-26 17:19:08 +0000170bool TargetInfo::validateOutputConstraint(ConstraintInfo &Info) const {
171 const char *Name = Info.getConstraintStr().c_str();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000172 // An output constraint must start with '=' or '+'
173 if (*Name != '=' && *Name != '+')
174 return false;
175
176 if (*Name == '+')
Chris Lattner44def072009-04-26 07:16:29 +0000177 Info.setIsReadWrite();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000178
179 Name++;
180 while (*Name) {
181 switch (*Name) {
182 default:
Chris Lattner44def072009-04-26 07:16:29 +0000183 if (!validateAsmConstraint(Name, Info)) {
Ted Kremenek5ab201a2008-04-24 16:36:38 +0000184 // FIXME: We temporarily return false
Anders Carlssond04c6e22007-11-27 04:11:28 +0000185 // so we can add more constraints as we hit it.
186 // Eventually, an unknown constraint should just be treated as 'g'.
Ted Kremenek5ab201a2008-04-24 16:36:38 +0000187 return false;
Anders Carlssond04c6e22007-11-27 04:11:28 +0000188 }
189 case '&': // early clobber.
190 break;
191 case 'r': // general register.
Chris Lattner44def072009-04-26 07:16:29 +0000192 Info.setAllowsRegister();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000193 break;
194 case 'm': // memory operand.
Chris Lattner44def072009-04-26 07:16:29 +0000195 Info.setAllowsMemory();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000196 break;
197 case 'g': // general register, memory operand or immediate integer.
Anders Carlsson775841f2009-01-18 02:12:04 +0000198 case 'X': // any operand.
Chris Lattner44def072009-04-26 07:16:29 +0000199 Info.setAllowsRegister();
200 Info.setAllowsMemory();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000201 break;
202 }
203
204 Name++;
205 }
206
207 return true;
208}
209
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000210bool TargetInfo::resolveSymbolicName(const char *&Name,
Chris Lattner2819fa82009-04-26 17:57:12 +0000211 ConstraintInfo *OutputConstraints,
212 unsigned NumOutputs,
Chris Lattner44def072009-04-26 07:16:29 +0000213 unsigned &Index) const {
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000214 assert(*Name == '[' && "Symbolic name did not start with '['");
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000215 Name++;
216 const char *Start = Name;
217 while (*Name && *Name != ']')
218 Name++;
219
220 if (!*Name) {
221 // Missing ']'
222 return false;
223 }
224
225 std::string SymbolicName(Start, Name - Start);
226
Chris Lattner2819fa82009-04-26 17:57:12 +0000227 for (Index = 0; Index != NumOutputs; ++Index)
228 if (SymbolicName == OutputConstraints[Index].getName())
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000229 return true;
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000230
231 return false;
232}
233
Chris Lattner2819fa82009-04-26 17:57:12 +0000234bool TargetInfo::validateInputConstraint(ConstraintInfo *OutputConstraints,
235 unsigned NumOutputs,
Chris Lattner44def072009-04-26 07:16:29 +0000236 ConstraintInfo &Info) const {
Chris Lattner432c8692009-04-26 17:19:08 +0000237 const char *Name = Info.ConstraintStr.c_str();
238
Anders Carlssond04c6e22007-11-27 04:11:28 +0000239 while (*Name) {
240 switch (*Name) {
241 default:
242 // Check if we have a matching constraint
243 if (*Name >= '0' && *Name <= '9') {
244 unsigned i = *Name - '0';
Anders Carlsson45b050e2009-01-17 23:36:15 +0000245
Anders Carlssond04c6e22007-11-27 04:11:28 +0000246 // Check if matching constraint is out of bounds.
247 if (i >= NumOutputs)
248 return false;
Anders Carlsson03eb5432009-01-27 20:38:24 +0000249
250 // The constraint should have the same info as the respective
251 // output constraint.
Chris Lattnerd6887612009-04-26 18:05:25 +0000252 Info.setTiedOperand(i, OutputConstraints[i]);
Chris Lattner44def072009-04-26 07:16:29 +0000253 } else if (!validateAsmConstraint(Name, Info)) {
Daniel Dunbar302684c2008-08-25 09:46:27 +0000254 // FIXME: This error return is in place temporarily so we can
255 // add more constraints as we hit it. Eventually, an unknown
256 // constraint should just be treated as 'g'.
257 return false;
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000258 }
259 break;
260 case '[': {
261 unsigned Index = 0;
Chris Lattner2819fa82009-04-26 17:57:12 +0000262 if (!resolveSymbolicName(Name, OutputConstraints, NumOutputs, Index))
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000263 return false;
264
265 break;
266 }
Anders Carlsson44fe49c2007-12-08 19:32:57 +0000267 case '%': // commutative
268 // FIXME: Fail if % is used with the last operand.
269 break;
Anders Carlssond04c6e22007-11-27 04:11:28 +0000270 case 'i': // immediate integer.
Anders Carlssonefdfa772008-03-09 06:02:02 +0000271 case 'n': // immediate integer with a known value.
Anders Carlssond04c6e22007-11-27 04:11:28 +0000272 break;
Chris Lattner1d138792009-05-06 04:33:31 +0000273 case 'I': // Various constant constraints with target-specific meanings.
274 case 'J':
275 case 'K':
276 case 'L':
277 case 'M':
278 case 'N':
279 case 'O':
280 case 'P':
281 break;
Anders Carlssond04c6e22007-11-27 04:11:28 +0000282 case 'r': // general register.
Chris Lattner44def072009-04-26 07:16:29 +0000283 Info.setAllowsRegister();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000284 break;
285 case 'm': // memory operand.
Chris Lattner44def072009-04-26 07:16:29 +0000286 Info.setAllowsMemory();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000287 break;
288 case 'g': // general register, memory operand or immediate integer.
Anders Carlsson775841f2009-01-18 02:12:04 +0000289 case 'X': // any operand.
Chris Lattner44def072009-04-26 07:16:29 +0000290 Info.setAllowsRegister();
291 Info.setAllowsMemory();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000292 break;
293 }
294
295 Name++;
296 }
297
298 return true;
299}