blob: b259facbb7c28df3f49efae0138ce9560d15905c [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) {
Daniel Dunbar3d9289c2010-04-15 06:18:39 +000023 // Set defaults. Defaults are set for a 32-bit RISC platform, like PPC or
24 // SPARC. 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 Lattnerb781dc792008-05-08 05:58:21 +000027 IntWidth = IntAlign = 32;
Chris Lattner5a9aaaf2008-05-09 05:50:02 +000028 LongWidth = LongAlign = 32;
29 LongLongWidth = LongLongAlign = 64;
Eli Friedmanb5366062008-05-20 14:21:01 +000030 FloatWidth = 32;
31 FloatAlign = 32;
Nate Begeman65bea232008-04-18 17:17:24 +000032 DoubleWidth = 64;
Eli Friedmanb5366062008-05-20 14:21:01 +000033 DoubleAlign = 64;
34 LongDoubleWidth = 64;
35 LongDoubleAlign = 64;
Anders Carlsson2a79a902008-10-31 16:05:19 +000036 SizeType = UnsignedLong;
Eli Friedmand50881c2008-11-02 02:43:55 +000037 PtrDiffType = SignedLong;
Sanjiv Guptad7959242008-10-31 09:52:39 +000038 IntMaxType = SignedLongLong;
39 UIntMaxType = UnsignedLongLong;
Chris Lattner7e4c81c2009-02-13 22:28:55 +000040 IntPtrType = SignedLong;
Eli Friedmand50881c2008-11-02 02:43:55 +000041 WCharType = SignedInt;
Chris Lattner67204922009-10-21 04:59:34 +000042 WIntType = SignedInt;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +000043 Char16Type = UnsignedShort;
44 Char32Type = UnsignedInt;
Eli Friedman2857ccb2009-07-01 03:36:11 +000045 Int64Type = SignedLongLong;
Edward O'Callaghan847f2a12009-11-21 00:49:54 +000046 SigAtomicType = SignedInt;
Daniel Dunbar1da65112010-04-15 15:06:18 +000047 UseBitFieldTypeAlignment = true;
Chris Lattner1df27862008-03-08 08:59:43 +000048 FloatFormat = &llvm::APFloat::IEEEsingle;
49 DoubleFormat = &llvm::APFloat::IEEEdouble;
50 LongDoubleFormat = &llvm::APFloat::IEEEdouble;
Eli Friedman873f65a2008-08-21 00:13:15 +000051 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 +000052 "i64:64:64-f32:32:32-f64:64:64-n32";
Chris Lattnerf37bafc2008-10-05 19:22:37 +000053 UserLabelPrefix = "_";
Chris Lattner1df27862008-03-08 08:59:43 +000054}
55
Chris Lattnerc3a669b2008-03-08 08:24:01 +000056// Out of line virtual dtor for TargetInfo.
57TargetInfo::~TargetInfo() {}
Chris Lattner2194ddc2006-10-14 18:32:26 +000058
Chris Lattnera91c30f2009-02-06 05:04:11 +000059/// getTypeName - Return the user string for the specified integer type enum.
60/// For example, SignedShort -> "short".
61const char *TargetInfo::getTypeName(IntType T) {
62 switch (T) {
63 default: assert(0 && "not an integer!");
64 case SignedShort: return "short";
65 case UnsignedShort: return "unsigned short";
66 case SignedInt: return "int";
67 case UnsignedInt: return "unsigned int";
68 case SignedLong: return "long int";
69 case UnsignedLong: return "long unsigned int";
70 case SignedLongLong: return "long long int";
71 case UnsignedLongLong: return "long long unsigned int";
72 }
73}
74
Chris Lattner72cdcac2009-10-21 06:24:21 +000075/// getTypeConstantSuffix - Return the constant suffix for the specified
76/// integer type enum. For example, SignedLong -> "L".
77const char *TargetInfo::getTypeConstantSuffix(IntType T) {
78 switch (T) {
79 default: assert(0 && "not an integer!");
80 case SignedShort:
81 case SignedInt: return "";
82 case SignedLong: return "L";
83 case SignedLongLong: return "LL";
84 case UnsignedShort:
85 case UnsignedInt: return "U";
86 case UnsignedLong: return "UL";
87 case UnsignedLongLong: return "ULL";
88 }
89}
90
91/// getTypeWidth - Return the width (in bits) of the specified integer type
92/// enum. For example, SignedInt -> getIntWidth().
93unsigned TargetInfo::getTypeWidth(IntType T) const {
94 switch (T) {
95 default: assert(0 && "not an integer!");
Chris Lattnere4a8c642009-11-05 21:21:32 +000096 case SignedShort:
Chris Lattner72cdcac2009-10-21 06:24:21 +000097 case UnsignedShort: return getShortWidth();
Chris Lattnere4a8c642009-11-05 21:21:32 +000098 case SignedInt:
Chris Lattner72cdcac2009-10-21 06:24:21 +000099 case UnsignedInt: return getIntWidth();
Chris Lattnere4a8c642009-11-05 21:21:32 +0000100 case SignedLong:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000101 case UnsignedLong: return getLongWidth();
Chris Lattnere4a8c642009-11-05 21:21:32 +0000102 case SignedLongLong:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000103 case UnsignedLongLong: return getLongLongWidth();
104 };
105}
106
Chris Lattnere4a8c642009-11-05 21:21:32 +0000107/// getTypeAlign - Return the alignment (in bits) of the specified integer type
108/// enum. For example, SignedInt -> getIntAlign().
109unsigned TargetInfo::getTypeAlign(IntType T) const {
110 switch (T) {
111 default: assert(0 && "not an integer!");
112 case SignedShort:
113 case UnsignedShort: return getShortAlign();
114 case SignedInt:
115 case UnsignedInt: return getIntAlign();
116 case SignedLong:
117 case UnsignedLong: return getLongAlign();
118 case SignedLongLong:
119 case UnsignedLongLong: return getLongLongAlign();
120 };
121}
122
Chris Lattnerc0c04392009-10-25 22:49:18 +0000123/// isTypeSigned - Return whether an integer types is signed. Returns true if
Chris Lattner72cdcac2009-10-21 06:24:21 +0000124/// the type is signed; false otherwise.
Chris Lattnerc0c04392009-10-25 22:49:18 +0000125bool TargetInfo::isTypeSigned(IntType T) const {
Chris Lattner72cdcac2009-10-21 06:24:21 +0000126 switch (T) {
127 default: assert(0 && "not an integer!");
128 case SignedShort:
129 case SignedInt:
130 case SignedLong:
131 case SignedLongLong:
132 return true;
133 case UnsignedShort:
134 case UnsignedInt:
135 case UnsignedLong:
136 case UnsignedLongLong:
137 return false;
138 };
139}
140
John Thompsoned4e2952009-11-05 20:14:16 +0000141/// setForcedLangOptions - Set forced language options.
142/// Apply changes to the target information with respect to certain
143/// language options which change the target configuration.
144void TargetInfo::setForcedLangOptions(LangOptions &Opts) {
Daniel Dunbar9302f602010-04-15 15:06:22 +0000145 if (Opts.NoBitFieldTypeAlign)
146 UseBitFieldTypeAlignment = false;
147 if (Opts.ShortWChar)
John Thompsoned4e2952009-11-05 20:14:16 +0000148 WCharType = UnsignedShort;
John Thompsoned4e2952009-11-05 20:14:16 +0000149}
Chris Lattner72cdcac2009-10-21 06:24:21 +0000150
Chris Lattnerec0a6d92007-09-22 18:29:59 +0000151//===----------------------------------------------------------------------===//
Chris Lattnerec0a6d92007-09-22 18:29:59 +0000152
Chris Lattner10a5b382007-01-29 05:24:35 +0000153
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000154static llvm::StringRef removeGCCRegisterPrefix(llvm::StringRef Name) {
Anders Carlsson3ed4aea2008-02-06 00:11:32 +0000155 if (Name[0] == '%' || Name[0] == '#')
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000156 Name = Name.substr(1);
157
158 return Name;
Anders Carlsson3ed4aea2008-02-06 00:11:32 +0000159}
160
Anders Carlsson5fa3f342007-11-24 23:38:12 +0000161/// isValidGCCRegisterName - Returns whether the passed in string
162/// is a valid register name according to GCC. This is used by Sema for
163/// inline asm statements.
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000164bool TargetInfo::isValidGCCRegisterName(llvm::StringRef Name) const {
165 if (Name.empty())
166 return false;
167
Anders Carlsson290aa852007-11-25 00:25:21 +0000168 const char * const *Names;
169 unsigned NumNames;
Mike Stump11289f42009-09-09 15:08:12 +0000170
Anders Carlsson290aa852007-11-25 00:25:21 +0000171 // Get rid of any register prefix.
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000172 Name = removeGCCRegisterPrefix(Name);
Anders Carlsson3ed4aea2008-02-06 00:11:32 +0000173
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000174 if (Name == "memory" || Name == "cc")
Anders Carlsson290aa852007-11-25 00:25:21 +0000175 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000176
Chris Lattnerc3a669b2008-03-08 08:24:01 +0000177 getGCCRegNames(Names, NumNames);
Mike Stump11289f42009-09-09 15:08:12 +0000178
Anders Carlsson290aa852007-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])) {
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000181 int n;
182 if (!Name.getAsInteger(0, n))
Anders Carlsson290aa852007-11-25 00:25:21 +0000183 return n >= 0 && (unsigned)n < NumNames;
184 }
185
186 // Check register names.
187 for (unsigned i = 0; i < NumNames; i++) {
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000188 if (Name == Names[i])
Anders Carlsson290aa852007-11-25 00:25:21 +0000189 return true;
190 }
Mike Stump11289f42009-09-09 15:08:12 +0000191
Anders Carlsson290aa852007-11-25 00:25:21 +0000192 // Now check aliases.
Chris Lattnerc3a669b2008-03-08 08:24:01 +0000193 const GCCRegAlias *Aliases;
Anders Carlsson290aa852007-11-25 00:25:21 +0000194 unsigned NumAliases;
Mike Stump11289f42009-09-09 15:08:12 +0000195
Chris Lattnerc3a669b2008-03-08 08:24:01 +0000196 getGCCRegAliases(Aliases, NumAliases);
Anders Carlsson290aa852007-11-25 00:25:21 +0000197 for (unsigned i = 0; i < NumAliases; i++) {
198 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
199 if (!Aliases[i].Aliases[j])
200 break;
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000201 if (Aliases[i].Aliases[j] == Name)
Anders Carlsson290aa852007-11-25 00:25:21 +0000202 return true;
203 }
204 }
Mike Stump11289f42009-09-09 15:08:12 +0000205
Anders Carlsson5fa3f342007-11-24 23:38:12 +0000206 return false;
207}
Anders Carlssonf511f642007-11-27 04:11:28 +0000208
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000209llvm::StringRef
210TargetInfo::getNormalizedGCCRegisterName(llvm::StringRef Name) const {
Anders Carlssonf511f642007-11-27 04:11:28 +0000211 assert(isValidGCCRegisterName(Name) && "Invalid register passed in");
Mike Stump11289f42009-09-09 15:08:12 +0000212
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000213 // Get rid of any register prefix.
214 Name = removeGCCRegisterPrefix(Name);
Mike Stump11289f42009-09-09 15:08:12 +0000215
Anders Carlssonf511f642007-11-27 04:11:28 +0000216 const char * const *Names;
217 unsigned NumNames;
218
Chris Lattnerc3a669b2008-03-08 08:24:01 +0000219 getGCCRegNames(Names, NumNames);
Anders Carlssonf511f642007-11-27 04:11:28 +0000220
221 // First, check if we have a number.
222 if (isdigit(Name[0])) {
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000223 int n;
224 if (!Name.getAsInteger(0, n)) {
Mike Stump11289f42009-09-09 15:08:12 +0000225 assert(n >= 0 && (unsigned)n < NumNames &&
Anders Carlssonf511f642007-11-27 04:11:28 +0000226 "Out of bounds register number!");
227 return Names[n];
228 }
229 }
Mike Stump11289f42009-09-09 15:08:12 +0000230
Anders Carlssonf511f642007-11-27 04:11:28 +0000231 // Now check aliases.
Chris Lattnerc3a669b2008-03-08 08:24:01 +0000232 const GCCRegAlias *Aliases;
Anders Carlssonf511f642007-11-27 04:11:28 +0000233 unsigned NumAliases;
Mike Stump11289f42009-09-09 15:08:12 +0000234
Chris Lattnerc3a669b2008-03-08 08:24:01 +0000235 getGCCRegAliases(Aliases, NumAliases);
Anders Carlssonf511f642007-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;
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000240 if (Aliases[i].Aliases[j] == Name)
Anders Carlssonf511f642007-11-27 04:11:28 +0000241 return Aliases[i].Register;
242 }
243 }
Mike Stump11289f42009-09-09 15:08:12 +0000244
Anders Carlssonf511f642007-11-27 04:11:28 +0000245 return Name;
246}
247
Chris Lattnerc3f4c7b2009-04-26 17:19:08 +0000248bool TargetInfo::validateOutputConstraint(ConstraintInfo &Info) const {
249 const char *Name = Info.getConstraintStr().c_str();
Anders Carlssonf511f642007-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 Lattnerd9725f72009-04-26 07:16:29 +0000255 Info.setIsReadWrite();
Anders Carlssonf511f642007-11-27 04:11:28 +0000256
257 Name++;
258 while (*Name) {
259 switch (*Name) {
260 default:
Chris Lattnerd9725f72009-04-26 07:16:29 +0000261 if (!validateAsmConstraint(Name, Info)) {
Ted Kremenekb44485b2008-04-24 16:36:38 +0000262 // FIXME: We temporarily return false
Anders Carlssonf511f642007-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 Kremenekb44485b2008-04-24 16:36:38 +0000265 return false;
Anders Carlssonf511f642007-11-27 04:11:28 +0000266 }
267 case '&': // early clobber.
268 break;
Chris Lattnerf3154712009-10-13 04:32:07 +0000269 case '%': // commutative.
270 // FIXME: Check that there is a another register after this one.
271 break;
Anders Carlssonf511f642007-11-27 04:11:28 +0000272 case 'r': // general register.
Chris Lattnerd9725f72009-04-26 07:16:29 +0000273 Info.setAllowsRegister();
Anders Carlssonf511f642007-11-27 04:11:28 +0000274 break;
275 case 'm': // memory operand.
Chris Lattnerd9725f72009-04-26 07:16:29 +0000276 Info.setAllowsMemory();
Anders Carlssonf511f642007-11-27 04:11:28 +0000277 break;
278 case 'g': // general register, memory operand or immediate integer.
Anders Carlssone70cde12009-01-18 02:12:04 +0000279 case 'X': // any operand.
Chris Lattnerd9725f72009-04-26 07:16:29 +0000280 Info.setAllowsRegister();
281 Info.setAllowsMemory();
Anders Carlssonf511f642007-11-27 04:11:28 +0000282 break;
283 }
Mike Stump11289f42009-09-09 15:08:12 +0000284
Anders Carlssonf511f642007-11-27 04:11:28 +0000285 Name++;
286 }
Mike Stump11289f42009-09-09 15:08:12 +0000287
Anders Carlssonf511f642007-11-27 04:11:28 +0000288 return true;
289}
290
Anders Carlssona79203b2009-01-18 01:56:57 +0000291bool TargetInfo::resolveSymbolicName(const char *&Name,
Chris Lattnerc16d4762009-04-26 17:57:12 +0000292 ConstraintInfo *OutputConstraints,
293 unsigned NumOutputs,
Chris Lattnerd9725f72009-04-26 07:16:29 +0000294 unsigned &Index) const {
Anders Carlssona79203b2009-01-18 01:56:57 +0000295 assert(*Name == '[' && "Symbolic name did not start with '['");
Anders Carlssona79203b2009-01-18 01:56:57 +0000296 Name++;
297 const char *Start = Name;
298 while (*Name && *Name != ']')
299 Name++;
Mike Stump11289f42009-09-09 15:08:12 +0000300
Anders Carlssona79203b2009-01-18 01:56:57 +0000301 if (!*Name) {
302 // Missing ']'
303 return false;
304 }
Mike Stump11289f42009-09-09 15:08:12 +0000305
Anders Carlssona79203b2009-01-18 01:56:57 +0000306 std::string SymbolicName(Start, Name - Start);
Mike Stump11289f42009-09-09 15:08:12 +0000307
Chris Lattnerc16d4762009-04-26 17:57:12 +0000308 for (Index = 0; Index != NumOutputs; ++Index)
309 if (SymbolicName == OutputConstraints[Index].getName())
Anders Carlssona79203b2009-01-18 01:56:57 +0000310 return true;
Anders Carlssona79203b2009-01-18 01:56:57 +0000311
312 return false;
313}
314
Chris Lattnerc16d4762009-04-26 17:57:12 +0000315bool TargetInfo::validateInputConstraint(ConstraintInfo *OutputConstraints,
316 unsigned NumOutputs,
Chris Lattnerd9725f72009-04-26 07:16:29 +0000317 ConstraintInfo &Info) const {
Chris Lattnerc3f4c7b2009-04-26 17:19:08 +0000318 const char *Name = Info.ConstraintStr.c_str();
319
Anders Carlssonf511f642007-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 Stump11289f42009-09-09 15:08:12 +0000326
Anders Carlssonf511f642007-11-27 04:11:28 +0000327 // Check if matching constraint is out of bounds.
328 if (i >= NumOutputs)
329 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000330
331 // The constraint should have the same info as the respective
Anders Carlsson570c3572009-01-27 20:38:24 +0000332 // output constraint.
Chris Lattner4c92b512009-04-26 18:05:25 +0000333 Info.setTiedOperand(i, OutputConstraints[i]);
Chris Lattnerd9725f72009-04-26 07:16:29 +0000334 } else if (!validateAsmConstraint(Name, Info)) {
Daniel Dunbar81128e02008-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 Carlssona79203b2009-01-18 01:56:57 +0000339 }
340 break;
341 case '[': {
342 unsigned Index = 0;
Chris Lattnerc16d4762009-04-26 17:57:12 +0000343 if (!resolveSymbolicName(Name, OutputConstraints, NumOutputs, Index))
Anders Carlssona79203b2009-01-18 01:56:57 +0000344 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000345
Anders Carlssona79203b2009-01-18 01:56:57 +0000346 break;
Mike Stump11289f42009-09-09 15:08:12 +0000347 }
Anders Carlsson050f4942007-12-08 19:32:57 +0000348 case '%': // commutative
349 // FIXME: Fail if % is used with the last operand.
350 break;
Anders Carlssonf511f642007-11-27 04:11:28 +0000351 case 'i': // immediate integer.
Anders Carlssona3a96af2008-03-09 06:02:02 +0000352 case 'n': // immediate integer with a known value.
Anders Carlssonf511f642007-11-27 04:11:28 +0000353 break;
Chris Lattnerdbcc5ca2009-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 Carlssonf511f642007-11-27 04:11:28 +0000363 case 'r': // general register.
Chris Lattnerd9725f72009-04-26 07:16:29 +0000364 Info.setAllowsRegister();
Anders Carlssonf511f642007-11-27 04:11:28 +0000365 break;
366 case 'm': // memory operand.
Nuno Lopes2af2af22009-12-16 14:28:21 +0000367 case 'o': // offsettable memory operand
368 case 'V': // non-offsettable memory operand
Chris Lattnerd9725f72009-04-26 07:16:29 +0000369 Info.setAllowsMemory();
Anders Carlssonf511f642007-11-27 04:11:28 +0000370 break;
371 case 'g': // general register, memory operand or immediate integer.
Anders Carlssone70cde12009-01-18 02:12:04 +0000372 case 'X': // any operand.
Chris Lattnerd9725f72009-04-26 07:16:29 +0000373 Info.setAllowsRegister();
374 Info.setAllowsMemory();
Anders Carlssonf511f642007-11-27 04:11:28 +0000375 break;
376 }
Mike Stump11289f42009-09-09 15:08:12 +0000377
Anders Carlssonf511f642007-11-27 04:11:28 +0000378 Name++;
379 }
Mike Stump11289f42009-09-09 15:08:12 +0000380
Anders Carlssonf511f642007-11-27 04:11:28 +0000381 return true;
382}