blob: 5b2ffb7d165c79dcf8cbcfdea86e8b780043a888 [file] [log] [blame]
Chris Lattner1e27fe12006-10-14 07:06:20 +00001//===--- TargetInfo.cpp - Information about Target machine ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner1e27fe12006-10-14 07:06:20 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the TargetInfo and TargetInfoImpl interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Basic/TargetInfo.h"
Chris Lattnerec0a6d92007-09-22 18:29:59 +000015#include "llvm/ADT/APFloat.h"
Anders Carlsson290aa852007-11-25 00:25:21 +000016#include "llvm/ADT/STLExtras.h"
Chris Lattnerc4f02b62008-04-06 04:02:29 +000017#include <cstdlib>
Chris Lattner1e27fe12006-10-14 07:06:20 +000018using namespace clang;
19
Chris Lattner1df27862008-03-08 08:59:43 +000020// TargetInfo Constructor.
21TargetInfo::TargetInfo(const std::string &T) : Triple(T) {
Eli Friedmanb5366062008-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 Friedmand88c8a12009-04-19 21:38:35 +000025 TLSSupported = true;
Chris Lattner5e2ef0c2008-05-09 06:08:39 +000026 PointerWidth = PointerAlign = 32;
Chris Lattner1df27862008-03-08 08:59:43 +000027 WCharWidth = WCharAlign = 32;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +000028 Char16Width = Char16Align = 16;
29 Char32Width = Char32Align = 32;
Chris Lattnerb781dc792008-05-08 05:58:21 +000030 IntWidth = IntAlign = 32;
Chris Lattner5a9aaaf2008-05-09 05:50:02 +000031 LongWidth = LongAlign = 32;
32 LongLongWidth = LongLongAlign = 64;
Eli Friedmanb5366062008-05-20 14:21:01 +000033 FloatWidth = 32;
34 FloatAlign = 32;
Nate Begeman65bea232008-04-18 17:17:24 +000035 DoubleWidth = 64;
Eli Friedmanb5366062008-05-20 14:21:01 +000036 DoubleAlign = 64;
37 LongDoubleWidth = 64;
38 LongDoubleAlign = 64;
Nate Begemana45707c2009-01-17 23:56:13 +000039 IntMaxTWidth = 64;
Anders Carlsson2a79a902008-10-31 16:05:19 +000040 SizeType = UnsignedLong;
Eli Friedmand50881c2008-11-02 02:43:55 +000041 PtrDiffType = SignedLong;
Sanjiv Guptad7959242008-10-31 09:52:39 +000042 IntMaxType = SignedLongLong;
43 UIntMaxType = UnsignedLongLong;
Chris Lattner7e4c81c2009-02-13 22:28:55 +000044 IntPtrType = SignedLong;
Eli Friedmand50881c2008-11-02 02:43:55 +000045 WCharType = SignedInt;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +000046 Char16Type = UnsignedShort;
47 Char32Type = UnsignedInt;
Eli Friedman2857ccb2009-07-01 03:36:11 +000048 Int64Type = SignedLongLong;
Chris Lattner1df27862008-03-08 08:59:43 +000049 FloatFormat = &llvm::APFloat::IEEEsingle;
50 DoubleFormat = &llvm::APFloat::IEEEdouble;
51 LongDoubleFormat = &llvm::APFloat::IEEEdouble;
Eli Friedman873f65a2008-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 Lattnerf37bafc2008-10-05 19:22:37 +000054 UserLabelPrefix = "_";
Chris Lattner1df27862008-03-08 08:59:43 +000055}
56
Chris Lattnerc3a669b2008-03-08 08:24:01 +000057// Out of line virtual dtor for TargetInfo.
58TargetInfo::~TargetInfo() {}
Chris Lattner2194ddc2006-10-14 18:32:26 +000059
Chris Lattnera91c30f2009-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 Lattnerec0a6d92007-09-22 18:29:59 +000076//===----------------------------------------------------------------------===//
Chris Lattnerec0a6d92007-09-22 18:29:59 +000077
Chris Lattner10a5b382007-01-29 05:24:35 +000078
Chris Lattner855d0242008-03-05 01:18:20 +000079static void removeGCCRegisterPrefix(const char *&Name) {
Anders Carlsson3ed4aea2008-02-06 00:11:32 +000080 if (Name[0] == '%' || Name[0] == '#')
81 Name++;
82}
83
Anders Carlsson5fa3f342007-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 Carlsson290aa852007-11-25 00:25:21 +000088 const char * const *Names;
89 unsigned NumNames;
90
91 // Get rid of any register prefix.
Anders Carlsson3ed4aea2008-02-06 00:11:32 +000092 removeGCCRegisterPrefix(Name);
93
Anders Carlsson290aa852007-11-25 00:25:21 +000094
95 if (strcmp(Name, "memory") == 0 ||
96 strcmp(Name, "cc") == 0)
97 return true;
98
Chris Lattnerc3a669b2008-03-08 08:24:01 +000099 getGCCRegNames(Names, NumNames);
Anders Carlsson290aa852007-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 Lattnerc3a669b2008-03-08 08:24:01 +0000116 const GCCRegAlias *Aliases;
Anders Carlsson290aa852007-11-25 00:25:21 +0000117 unsigned NumAliases;
118
Chris Lattnerc3a669b2008-03-08 08:24:01 +0000119 getGCCRegAliases(Aliases, NumAliases);
Anders Carlsson290aa852007-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 Carlsson5fa3f342007-11-24 23:38:12 +0000129 return false;
130}
Anders Carlssonf511f642007-11-27 04:11:28 +0000131
Chris Lattnerc3a669b2008-03-08 08:24:01 +0000132const char *TargetInfo::getNormalizedGCCRegisterName(const char *Name) const {
Anders Carlssonf511f642007-11-27 04:11:28 +0000133 assert(isValidGCCRegisterName(Name) && "Invalid register passed in");
134
Anders Carlsson3ed4aea2008-02-06 00:11:32 +0000135 removeGCCRegisterPrefix(Name);
Anders Carlsson487e5272008-02-05 23:30:20 +0000136
Anders Carlssonf511f642007-11-27 04:11:28 +0000137 const char * const *Names;
138 unsigned NumNames;
139
Chris Lattnerc3a669b2008-03-08 08:24:01 +0000140 getGCCRegNames(Names, NumNames);
Anders Carlssonf511f642007-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 Lattnerc3a669b2008-03-08 08:24:01 +0000154 const GCCRegAlias *Aliases;
Anders Carlssonf511f642007-11-27 04:11:28 +0000155 unsigned NumAliases;
156
Chris Lattnerc3a669b2008-03-08 08:24:01 +0000157 getGCCRegAliases(Aliases, NumAliases);
Anders Carlssonf511f642007-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 Lattnerc3f4c7b2009-04-26 17:19:08 +0000170bool TargetInfo::validateOutputConstraint(ConstraintInfo &Info) const {
171 const char *Name = Info.getConstraintStr().c_str();
Anders Carlssonf511f642007-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 Lattnerd9725f72009-04-26 07:16:29 +0000177 Info.setIsReadWrite();
Anders Carlssonf511f642007-11-27 04:11:28 +0000178
179 Name++;
180 while (*Name) {
181 switch (*Name) {
182 default:
Chris Lattnerd9725f72009-04-26 07:16:29 +0000183 if (!validateAsmConstraint(Name, Info)) {
Ted Kremenekb44485b2008-04-24 16:36:38 +0000184 // FIXME: We temporarily return false
Anders Carlssonf511f642007-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 Kremenekb44485b2008-04-24 16:36:38 +0000187 return false;
Anders Carlssonf511f642007-11-27 04:11:28 +0000188 }
189 case '&': // early clobber.
190 break;
191 case 'r': // general register.
Chris Lattnerd9725f72009-04-26 07:16:29 +0000192 Info.setAllowsRegister();
Anders Carlssonf511f642007-11-27 04:11:28 +0000193 break;
194 case 'm': // memory operand.
Chris Lattnerd9725f72009-04-26 07:16:29 +0000195 Info.setAllowsMemory();
Anders Carlssonf511f642007-11-27 04:11:28 +0000196 break;
197 case 'g': // general register, memory operand or immediate integer.
Anders Carlssone70cde12009-01-18 02:12:04 +0000198 case 'X': // any operand.
Chris Lattnerd9725f72009-04-26 07:16:29 +0000199 Info.setAllowsRegister();
200 Info.setAllowsMemory();
Anders Carlssonf511f642007-11-27 04:11:28 +0000201 break;
202 }
203
204 Name++;
205 }
206
207 return true;
208}
209
Anders Carlssona79203b2009-01-18 01:56:57 +0000210bool TargetInfo::resolveSymbolicName(const char *&Name,
Chris Lattnerc16d4762009-04-26 17:57:12 +0000211 ConstraintInfo *OutputConstraints,
212 unsigned NumOutputs,
Chris Lattnerd9725f72009-04-26 07:16:29 +0000213 unsigned &Index) const {
Anders Carlssona79203b2009-01-18 01:56:57 +0000214 assert(*Name == '[' && "Symbolic name did not start with '['");
Anders Carlssona79203b2009-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 Lattnerc16d4762009-04-26 17:57:12 +0000227 for (Index = 0; Index != NumOutputs; ++Index)
228 if (SymbolicName == OutputConstraints[Index].getName())
Anders Carlssona79203b2009-01-18 01:56:57 +0000229 return true;
Anders Carlssona79203b2009-01-18 01:56:57 +0000230
231 return false;
232}
233
Chris Lattnerc16d4762009-04-26 17:57:12 +0000234bool TargetInfo::validateInputConstraint(ConstraintInfo *OutputConstraints,
235 unsigned NumOutputs,
Chris Lattnerd9725f72009-04-26 07:16:29 +0000236 ConstraintInfo &Info) const {
Chris Lattnerc3f4c7b2009-04-26 17:19:08 +0000237 const char *Name = Info.ConstraintStr.c_str();
238
Anders Carlssonf511f642007-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 Carlsson19aa04d2009-01-17 23:36:15 +0000245
Anders Carlssonf511f642007-11-27 04:11:28 +0000246 // Check if matching constraint is out of bounds.
247 if (i >= NumOutputs)
248 return false;
Anders Carlsson570c3572009-01-27 20:38:24 +0000249
250 // The constraint should have the same info as the respective
251 // output constraint.
Chris Lattner4c92b512009-04-26 18:05:25 +0000252 Info.setTiedOperand(i, OutputConstraints[i]);
Chris Lattnerd9725f72009-04-26 07:16:29 +0000253 } else if (!validateAsmConstraint(Name, Info)) {
Daniel Dunbar81128e02008-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 Carlssona79203b2009-01-18 01:56:57 +0000258 }
259 break;
260 case '[': {
261 unsigned Index = 0;
Chris Lattnerc16d4762009-04-26 17:57:12 +0000262 if (!resolveSymbolicName(Name, OutputConstraints, NumOutputs, Index))
Anders Carlssona79203b2009-01-18 01:56:57 +0000263 return false;
264
265 break;
266 }
Anders Carlsson050f4942007-12-08 19:32:57 +0000267 case '%': // commutative
268 // FIXME: Fail if % is used with the last operand.
269 break;
Anders Carlssonf511f642007-11-27 04:11:28 +0000270 case 'i': // immediate integer.
Anders Carlssona3a96af2008-03-09 06:02:02 +0000271 case 'n': // immediate integer with a known value.
Anders Carlssonf511f642007-11-27 04:11:28 +0000272 break;
Chris Lattnerdbcc5ca2009-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 Carlssonf511f642007-11-27 04:11:28 +0000282 case 'r': // general register.
Chris Lattnerd9725f72009-04-26 07:16:29 +0000283 Info.setAllowsRegister();
Anders Carlssonf511f642007-11-27 04:11:28 +0000284 break;
285 case 'm': // memory operand.
Chris Lattnerd9725f72009-04-26 07:16:29 +0000286 Info.setAllowsMemory();
Anders Carlssonf511f642007-11-27 04:11:28 +0000287 break;
288 case 'g': // general register, memory operand or immediate integer.
Anders Carlssone70cde12009-01-18 02:12:04 +0000289 case 'X': // any operand.
Chris Lattnerd9725f72009-04-26 07:16:29 +0000290 Info.setAllowsRegister();
291 Info.setAllowsMemory();
Anders Carlssonf511f642007-11-27 04:11:28 +0000292 break;
293 }
294
295 Name++;
296 }
297
298 return true;
299}