blob: b6c4df87f27256cea2acbd8a769ed2b61325ce61 [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"
John Thompsoned4e2952009-11-05 20:14:16 +000015#include "clang/Basic/LangOptions.h"
Chris Lattnerec0a6d92007-09-22 18:29:59 +000016#include "llvm/ADT/APFloat.h"
Anders Carlsson290aa852007-11-25 00:25:21 +000017#include "llvm/ADT/STLExtras.h"
Chris Lattnerc4f02b62008-04-06 04:02:29 +000018#include <cstdlib>
Chris Lattner1e27fe12006-10-14 07:06:20 +000019using namespace clang;
20
Chris Lattner1df27862008-03-08 08:59:43 +000021// TargetInfo Constructor.
22TargetInfo::TargetInfo(const std::string &T) : Triple(T) {
Eli Friedmanb5366062008-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 Friedmand88c8a12009-04-19 21:38:35 +000026 TLSSupported = true;
Chris Lattner5e2ef0c2008-05-09 06:08:39 +000027 PointerWidth = PointerAlign = 32;
Chris Lattnerb781dc792008-05-08 05:58:21 +000028 IntWidth = IntAlign = 32;
Chris Lattner5a9aaaf2008-05-09 05:50:02 +000029 LongWidth = LongAlign = 32;
30 LongLongWidth = LongLongAlign = 64;
Eli Friedmanb5366062008-05-20 14:21:01 +000031 FloatWidth = 32;
32 FloatAlign = 32;
Nate Begeman65bea232008-04-18 17:17:24 +000033 DoubleWidth = 64;
Eli Friedmanb5366062008-05-20 14:21:01 +000034 DoubleAlign = 64;
35 LongDoubleWidth = 64;
36 LongDoubleAlign = 64;
Anders Carlsson2a79a902008-10-31 16:05:19 +000037 SizeType = UnsignedLong;
Eli Friedmand50881c2008-11-02 02:43:55 +000038 PtrDiffType = SignedLong;
Sanjiv Guptad7959242008-10-31 09:52:39 +000039 IntMaxType = SignedLongLong;
40 UIntMaxType = UnsignedLongLong;
Chris Lattner7e4c81c2009-02-13 22:28:55 +000041 IntPtrType = SignedLong;
Eli Friedmand50881c2008-11-02 02:43:55 +000042 WCharType = SignedInt;
Chris Lattner67204922009-10-21 04:59:34 +000043 WIntType = SignedInt;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +000044 Char16Type = UnsignedShort;
45 Char32Type = UnsignedInt;
Eli Friedman2857ccb2009-07-01 03:36:11 +000046 Int64Type = SignedLongLong;
Chris Lattner1df27862008-03-08 08:59:43 +000047 FloatFormat = &llvm::APFloat::IEEEsingle;
48 DoubleFormat = &llvm::APFloat::IEEEdouble;
49 LongDoubleFormat = &llvm::APFloat::IEEEdouble;
Eli Friedman873f65a2008-08-21 00:13:15 +000050 DescriptionString = "E-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
Chris Lattner5c67237f2009-11-07 18:59:41 +000051 "i64:64:64-f32:32:32-f64:64:64-n32";
Chris Lattnerf37bafc2008-10-05 19:22:37 +000052 UserLabelPrefix = "_";
Chris Lattner1df27862008-03-08 08:59:43 +000053}
54
Chris Lattnerc3a669b2008-03-08 08:24:01 +000055// Out of line virtual dtor for TargetInfo.
56TargetInfo::~TargetInfo() {}
Chris Lattner2194ddc2006-10-14 18:32:26 +000057
Chris Lattnera91c30f2009-02-06 05:04:11 +000058/// getTypeName - Return the user string for the specified integer type enum.
59/// For example, SignedShort -> "short".
60const char *TargetInfo::getTypeName(IntType T) {
61 switch (T) {
62 default: assert(0 && "not an integer!");
63 case SignedShort: return "short";
64 case UnsignedShort: return "unsigned short";
65 case SignedInt: return "int";
66 case UnsignedInt: return "unsigned int";
67 case SignedLong: return "long int";
68 case UnsignedLong: return "long unsigned int";
69 case SignedLongLong: return "long long int";
70 case UnsignedLongLong: return "long long unsigned int";
71 }
72}
73
Chris Lattner72cdcac2009-10-21 06:24:21 +000074/// getTypeConstantSuffix - Return the constant suffix for the specified
75/// integer type enum. For example, SignedLong -> "L".
76const char *TargetInfo::getTypeConstantSuffix(IntType T) {
77 switch (T) {
78 default: assert(0 && "not an integer!");
79 case SignedShort:
80 case SignedInt: return "";
81 case SignedLong: return "L";
82 case SignedLongLong: return "LL";
83 case UnsignedShort:
84 case UnsignedInt: return "U";
85 case UnsignedLong: return "UL";
86 case UnsignedLongLong: return "ULL";
87 }
88}
89
90/// getTypeWidth - Return the width (in bits) of the specified integer type
91/// enum. For example, SignedInt -> getIntWidth().
92unsigned TargetInfo::getTypeWidth(IntType T) const {
93 switch (T) {
94 default: assert(0 && "not an integer!");
Chris Lattnere4a8c642009-11-05 21:21:32 +000095 case SignedShort:
Chris Lattner72cdcac2009-10-21 06:24:21 +000096 case UnsignedShort: return getShortWidth();
Chris Lattnere4a8c642009-11-05 21:21:32 +000097 case SignedInt:
Chris Lattner72cdcac2009-10-21 06:24:21 +000098 case UnsignedInt: return getIntWidth();
Chris Lattnere4a8c642009-11-05 21:21:32 +000099 case SignedLong:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000100 case UnsignedLong: return getLongWidth();
Chris Lattnere4a8c642009-11-05 21:21:32 +0000101 case SignedLongLong:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000102 case UnsignedLongLong: return getLongLongWidth();
103 };
104}
105
Chris Lattnere4a8c642009-11-05 21:21:32 +0000106/// getTypeAlign - Return the alignment (in bits) of the specified integer type
107/// enum. For example, SignedInt -> getIntAlign().
108unsigned TargetInfo::getTypeAlign(IntType T) const {
109 switch (T) {
110 default: assert(0 && "not an integer!");
111 case SignedShort:
112 case UnsignedShort: return getShortAlign();
113 case SignedInt:
114 case UnsignedInt: return getIntAlign();
115 case SignedLong:
116 case UnsignedLong: return getLongAlign();
117 case SignedLongLong:
118 case UnsignedLongLong: return getLongLongAlign();
119 };
120}
121
Chris Lattnerc0c04392009-10-25 22:49:18 +0000122/// isTypeSigned - Return whether an integer types is signed. Returns true if
Chris Lattner72cdcac2009-10-21 06:24:21 +0000123/// the type is signed; false otherwise.
Chris Lattnerc0c04392009-10-25 22:49:18 +0000124bool TargetInfo::isTypeSigned(IntType T) const {
Chris Lattner72cdcac2009-10-21 06:24:21 +0000125 switch (T) {
126 default: assert(0 && "not an integer!");
127 case SignedShort:
128 case SignedInt:
129 case SignedLong:
130 case SignedLongLong:
131 return true;
132 case UnsignedShort:
133 case UnsignedInt:
134 case UnsignedLong:
135 case UnsignedLongLong:
136 return false;
137 };
138}
139
John Thompsoned4e2952009-11-05 20:14:16 +0000140/// setForcedLangOptions - Set forced language options.
141/// Apply changes to the target information with respect to certain
142/// language options which change the target configuration.
143void TargetInfo::setForcedLangOptions(LangOptions &Opts) {
John Thompson864bf752009-11-05 22:03:02 +0000144 if (Opts.ShortWChar) {
John Thompsoned4e2952009-11-05 20:14:16 +0000145 WCharType = UnsignedShort;
John Thompson864bf752009-11-05 22:03:02 +0000146 }
John Thompsoned4e2952009-11-05 20:14:16 +0000147}
Chris Lattner72cdcac2009-10-21 06:24:21 +0000148
Chris Lattnerec0a6d92007-09-22 18:29:59 +0000149//===----------------------------------------------------------------------===//
Chris Lattnerec0a6d92007-09-22 18:29:59 +0000150
Chris Lattner10a5b382007-01-29 05:24:35 +0000151
Chris Lattner855d0242008-03-05 01:18:20 +0000152static void removeGCCRegisterPrefix(const char *&Name) {
Anders Carlsson3ed4aea2008-02-06 00:11:32 +0000153 if (Name[0] == '%' || Name[0] == '#')
154 Name++;
155}
156
Anders Carlsson5fa3f342007-11-24 23:38:12 +0000157/// isValidGCCRegisterName - Returns whether the passed in string
158/// is a valid register name according to GCC. This is used by Sema for
159/// inline asm statements.
160bool TargetInfo::isValidGCCRegisterName(const char *Name) const {
Anders Carlsson290aa852007-11-25 00:25:21 +0000161 const char * const *Names;
162 unsigned NumNames;
Mike Stump11289f42009-09-09 15:08:12 +0000163
Anders Carlsson290aa852007-11-25 00:25:21 +0000164 // Get rid of any register prefix.
Anders Carlsson3ed4aea2008-02-06 00:11:32 +0000165 removeGCCRegisterPrefix(Name);
166
Mike Stump11289f42009-09-09 15:08:12 +0000167
Anders Carlsson290aa852007-11-25 00:25:21 +0000168 if (strcmp(Name, "memory") == 0 ||
169 strcmp(Name, "cc") == 0)
170 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000171
Chris Lattnerc3a669b2008-03-08 08:24:01 +0000172 getGCCRegNames(Names, NumNames);
Mike Stump11289f42009-09-09 15:08:12 +0000173
Anders Carlsson290aa852007-11-25 00:25:21 +0000174 // If we have a number it maps to an entry in the register name array.
175 if (isdigit(Name[0])) {
176 char *End;
177 int n = (int)strtol(Name, &End, 0);
178 if (*End == 0)
179 return n >= 0 && (unsigned)n < NumNames;
180 }
181
182 // Check register names.
183 for (unsigned i = 0; i < NumNames; i++) {
184 if (strcmp(Name, Names[i]) == 0)
185 return true;
186 }
Mike Stump11289f42009-09-09 15:08:12 +0000187
Anders Carlsson290aa852007-11-25 00:25:21 +0000188 // Now check aliases.
Chris Lattnerc3a669b2008-03-08 08:24:01 +0000189 const GCCRegAlias *Aliases;
Anders Carlsson290aa852007-11-25 00:25:21 +0000190 unsigned NumAliases;
Mike Stump11289f42009-09-09 15:08:12 +0000191
Chris Lattnerc3a669b2008-03-08 08:24:01 +0000192 getGCCRegAliases(Aliases, NumAliases);
Anders Carlsson290aa852007-11-25 00:25:21 +0000193 for (unsigned i = 0; i < NumAliases; i++) {
194 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
195 if (!Aliases[i].Aliases[j])
196 break;
197 if (strcmp(Aliases[i].Aliases[j], Name) == 0)
198 return true;
199 }
200 }
Mike Stump11289f42009-09-09 15:08:12 +0000201
Anders Carlsson5fa3f342007-11-24 23:38:12 +0000202 return false;
203}
Anders Carlssonf511f642007-11-27 04:11:28 +0000204
Chris Lattnerc3a669b2008-03-08 08:24:01 +0000205const char *TargetInfo::getNormalizedGCCRegisterName(const char *Name) const {
Anders Carlssonf511f642007-11-27 04:11:28 +0000206 assert(isValidGCCRegisterName(Name) && "Invalid register passed in");
Mike Stump11289f42009-09-09 15:08:12 +0000207
Anders Carlsson3ed4aea2008-02-06 00:11:32 +0000208 removeGCCRegisterPrefix(Name);
Mike Stump11289f42009-09-09 15:08:12 +0000209
Anders Carlssonf511f642007-11-27 04:11:28 +0000210 const char * const *Names;
211 unsigned NumNames;
212
Chris Lattnerc3a669b2008-03-08 08:24:01 +0000213 getGCCRegNames(Names, NumNames);
Anders Carlssonf511f642007-11-27 04:11:28 +0000214
215 // First, check if we have a number.
216 if (isdigit(Name[0])) {
217 char *End;
218 int n = (int)strtol(Name, &End, 0);
219 if (*End == 0) {
Mike Stump11289f42009-09-09 15:08:12 +0000220 assert(n >= 0 && (unsigned)n < NumNames &&
Anders Carlssonf511f642007-11-27 04:11:28 +0000221 "Out of bounds register number!");
222 return Names[n];
223 }
224 }
Mike Stump11289f42009-09-09 15:08:12 +0000225
Anders Carlssonf511f642007-11-27 04:11:28 +0000226 // Now check aliases.
Chris Lattnerc3a669b2008-03-08 08:24:01 +0000227 const GCCRegAlias *Aliases;
Anders Carlssonf511f642007-11-27 04:11:28 +0000228 unsigned NumAliases;
Mike Stump11289f42009-09-09 15:08:12 +0000229
Chris Lattnerc3a669b2008-03-08 08:24:01 +0000230 getGCCRegAliases(Aliases, NumAliases);
Anders Carlssonf511f642007-11-27 04:11:28 +0000231 for (unsigned i = 0; i < NumAliases; i++) {
232 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
233 if (!Aliases[i].Aliases[j])
234 break;
235 if (strcmp(Aliases[i].Aliases[j], Name) == 0)
236 return Aliases[i].Register;
237 }
238 }
Mike Stump11289f42009-09-09 15:08:12 +0000239
Anders Carlssonf511f642007-11-27 04:11:28 +0000240 return Name;
241}
242
Chris Lattnerc3f4c7b2009-04-26 17:19:08 +0000243bool TargetInfo::validateOutputConstraint(ConstraintInfo &Info) const {
244 const char *Name = Info.getConstraintStr().c_str();
Anders Carlssonf511f642007-11-27 04:11:28 +0000245 // An output constraint must start with '=' or '+'
246 if (*Name != '=' && *Name != '+')
247 return false;
248
249 if (*Name == '+')
Chris Lattnerd9725f72009-04-26 07:16:29 +0000250 Info.setIsReadWrite();
Anders Carlssonf511f642007-11-27 04:11:28 +0000251
252 Name++;
253 while (*Name) {
254 switch (*Name) {
255 default:
Chris Lattnerd9725f72009-04-26 07:16:29 +0000256 if (!validateAsmConstraint(Name, Info)) {
Ted Kremenekb44485b2008-04-24 16:36:38 +0000257 // FIXME: We temporarily return false
Anders Carlssonf511f642007-11-27 04:11:28 +0000258 // so we can add more constraints as we hit it.
259 // Eventually, an unknown constraint should just be treated as 'g'.
Ted Kremenekb44485b2008-04-24 16:36:38 +0000260 return false;
Anders Carlssonf511f642007-11-27 04:11:28 +0000261 }
262 case '&': // early clobber.
263 break;
Chris Lattnerf3154712009-10-13 04:32:07 +0000264 case '%': // commutative.
265 // FIXME: Check that there is a another register after this one.
266 break;
Anders Carlssonf511f642007-11-27 04:11:28 +0000267 case 'r': // general register.
Chris Lattnerd9725f72009-04-26 07:16:29 +0000268 Info.setAllowsRegister();
Anders Carlssonf511f642007-11-27 04:11:28 +0000269 break;
270 case 'm': // memory operand.
Chris Lattnerd9725f72009-04-26 07:16:29 +0000271 Info.setAllowsMemory();
Anders Carlssonf511f642007-11-27 04:11:28 +0000272 break;
273 case 'g': // general register, memory operand or immediate integer.
Anders Carlssone70cde12009-01-18 02:12:04 +0000274 case 'X': // any operand.
Chris Lattnerd9725f72009-04-26 07:16:29 +0000275 Info.setAllowsRegister();
276 Info.setAllowsMemory();
Anders Carlssonf511f642007-11-27 04:11:28 +0000277 break;
278 }
Mike Stump11289f42009-09-09 15:08:12 +0000279
Anders Carlssonf511f642007-11-27 04:11:28 +0000280 Name++;
281 }
Mike Stump11289f42009-09-09 15:08:12 +0000282
Anders Carlssonf511f642007-11-27 04:11:28 +0000283 return true;
284}
285
Anders Carlssona79203b2009-01-18 01:56:57 +0000286bool TargetInfo::resolveSymbolicName(const char *&Name,
Chris Lattnerc16d4762009-04-26 17:57:12 +0000287 ConstraintInfo *OutputConstraints,
288 unsigned NumOutputs,
Chris Lattnerd9725f72009-04-26 07:16:29 +0000289 unsigned &Index) const {
Anders Carlssona79203b2009-01-18 01:56:57 +0000290 assert(*Name == '[' && "Symbolic name did not start with '['");
Anders Carlssona79203b2009-01-18 01:56:57 +0000291 Name++;
292 const char *Start = Name;
293 while (*Name && *Name != ']')
294 Name++;
Mike Stump11289f42009-09-09 15:08:12 +0000295
Anders Carlssona79203b2009-01-18 01:56:57 +0000296 if (!*Name) {
297 // Missing ']'
298 return false;
299 }
Mike Stump11289f42009-09-09 15:08:12 +0000300
Anders Carlssona79203b2009-01-18 01:56:57 +0000301 std::string SymbolicName(Start, Name - Start);
Mike Stump11289f42009-09-09 15:08:12 +0000302
Chris Lattnerc16d4762009-04-26 17:57:12 +0000303 for (Index = 0; Index != NumOutputs; ++Index)
304 if (SymbolicName == OutputConstraints[Index].getName())
Anders Carlssona79203b2009-01-18 01:56:57 +0000305 return true;
Anders Carlssona79203b2009-01-18 01:56:57 +0000306
307 return false;
308}
309
Chris Lattnerc16d4762009-04-26 17:57:12 +0000310bool TargetInfo::validateInputConstraint(ConstraintInfo *OutputConstraints,
311 unsigned NumOutputs,
Chris Lattnerd9725f72009-04-26 07:16:29 +0000312 ConstraintInfo &Info) const {
Chris Lattnerc3f4c7b2009-04-26 17:19:08 +0000313 const char *Name = Info.ConstraintStr.c_str();
314
Anders Carlssonf511f642007-11-27 04:11:28 +0000315 while (*Name) {
316 switch (*Name) {
317 default:
318 // Check if we have a matching constraint
319 if (*Name >= '0' && *Name <= '9') {
320 unsigned i = *Name - '0';
Mike Stump11289f42009-09-09 15:08:12 +0000321
Anders Carlssonf511f642007-11-27 04:11:28 +0000322 // Check if matching constraint is out of bounds.
323 if (i >= NumOutputs)
324 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000325
326 // The constraint should have the same info as the respective
Anders Carlsson570c3572009-01-27 20:38:24 +0000327 // output constraint.
Chris Lattner4c92b512009-04-26 18:05:25 +0000328 Info.setTiedOperand(i, OutputConstraints[i]);
Chris Lattnerd9725f72009-04-26 07:16:29 +0000329 } else if (!validateAsmConstraint(Name, Info)) {
Daniel Dunbar81128e02008-08-25 09:46:27 +0000330 // FIXME: This error return is in place temporarily so we can
331 // add more constraints as we hit it. Eventually, an unknown
332 // constraint should just be treated as 'g'.
333 return false;
Anders Carlssona79203b2009-01-18 01:56:57 +0000334 }
335 break;
336 case '[': {
337 unsigned Index = 0;
Chris Lattnerc16d4762009-04-26 17:57:12 +0000338 if (!resolveSymbolicName(Name, OutputConstraints, NumOutputs, Index))
Anders Carlssona79203b2009-01-18 01:56:57 +0000339 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000340
Anders Carlssona79203b2009-01-18 01:56:57 +0000341 break;
Mike Stump11289f42009-09-09 15:08:12 +0000342 }
Anders Carlsson050f4942007-12-08 19:32:57 +0000343 case '%': // commutative
344 // FIXME: Fail if % is used with the last operand.
345 break;
Anders Carlssonf511f642007-11-27 04:11:28 +0000346 case 'i': // immediate integer.
Anders Carlssona3a96af2008-03-09 06:02:02 +0000347 case 'n': // immediate integer with a known value.
Anders Carlssonf511f642007-11-27 04:11:28 +0000348 break;
Chris Lattnerdbcc5ca2009-05-06 04:33:31 +0000349 case 'I': // Various constant constraints with target-specific meanings.
350 case 'J':
351 case 'K':
352 case 'L':
353 case 'M':
354 case 'N':
355 case 'O':
356 case 'P':
357 break;
Anders Carlssonf511f642007-11-27 04:11:28 +0000358 case 'r': // general register.
Chris Lattnerd9725f72009-04-26 07:16:29 +0000359 Info.setAllowsRegister();
Anders Carlssonf511f642007-11-27 04:11:28 +0000360 break;
361 case 'm': // memory operand.
Chris Lattnerd9725f72009-04-26 07:16:29 +0000362 Info.setAllowsMemory();
Anders Carlssonf511f642007-11-27 04:11:28 +0000363 break;
364 case 'g': // general register, memory operand or immediate integer.
Anders Carlssone70cde12009-01-18 02:12:04 +0000365 case 'X': // any operand.
Chris Lattnerd9725f72009-04-26 07:16:29 +0000366 Info.setAllowsRegister();
367 Info.setAllowsMemory();
Anders Carlssonf511f642007-11-27 04:11:28 +0000368 break;
369 }
Mike Stump11289f42009-09-09 15:08:12 +0000370
Anders Carlssonf511f642007-11-27 04:11:28 +0000371 Name++;
372 }
Mike Stump11289f42009-09-09 15:08:12 +0000373
Anders Carlssonf511f642007-11-27 04:11:28 +0000374 return true;
375}