blob: 6692e641f2a420d4073bb88a461180aea93d0ce3 [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 Lattner1a8f3942010-04-23 16:29:58 +000026 NoAsmVariants = false;
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;
Edward O'Callaghan847f2a12009-11-21 00:49:54 +000047 SigAtomicType = SignedInt;
Daniel Dunbar1da65112010-04-15 15:06:18 +000048 UseBitFieldTypeAlignment = true;
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-"
Chris Lattner5c67237f2009-11-07 18:59:41 +000053 "i64:64:64-f32:32:32-f64:64:64-n32";
Chris Lattnerf37bafc2008-10-05 19:22:37 +000054 UserLabelPrefix = "_";
Daniel Dunbarbd606522010-05-27 00:35:16 +000055 HasAlignMac68kSupport = false;
Chris Lattner1df27862008-03-08 08:59:43 +000056}
57
Chris Lattnerc3a669b2008-03-08 08:24:01 +000058// Out of line virtual dtor for TargetInfo.
59TargetInfo::~TargetInfo() {}
Chris Lattner2194ddc2006-10-14 18:32:26 +000060
Chris Lattnera91c30f2009-02-06 05:04:11 +000061/// getTypeName - Return the user string for the specified integer type enum.
62/// For example, SignedShort -> "short".
63const char *TargetInfo::getTypeName(IntType T) {
64 switch (T) {
65 default: assert(0 && "not an integer!");
66 case SignedShort: return "short";
67 case UnsignedShort: return "unsigned short";
68 case SignedInt: return "int";
69 case UnsignedInt: return "unsigned int";
70 case SignedLong: return "long int";
71 case UnsignedLong: return "long unsigned int";
72 case SignedLongLong: return "long long int";
73 case UnsignedLongLong: return "long long unsigned int";
74 }
75}
76
Chris Lattner72cdcac2009-10-21 06:24:21 +000077/// getTypeConstantSuffix - Return the constant suffix for the specified
78/// integer type enum. For example, SignedLong -> "L".
79const char *TargetInfo::getTypeConstantSuffix(IntType T) {
80 switch (T) {
81 default: assert(0 && "not an integer!");
82 case SignedShort:
83 case SignedInt: return "";
84 case SignedLong: return "L";
85 case SignedLongLong: return "LL";
86 case UnsignedShort:
87 case UnsignedInt: return "U";
88 case UnsignedLong: return "UL";
89 case UnsignedLongLong: return "ULL";
90 }
91}
92
93/// getTypeWidth - Return the width (in bits) of the specified integer type
94/// enum. For example, SignedInt -> getIntWidth().
95unsigned TargetInfo::getTypeWidth(IntType T) const {
96 switch (T) {
97 default: assert(0 && "not an integer!");
Chris Lattnere4a8c642009-11-05 21:21:32 +000098 case SignedShort:
Chris Lattner72cdcac2009-10-21 06:24:21 +000099 case UnsignedShort: return getShortWidth();
Chris Lattnere4a8c642009-11-05 21:21:32 +0000100 case SignedInt:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000101 case UnsignedInt: return getIntWidth();
Chris Lattnere4a8c642009-11-05 21:21:32 +0000102 case SignedLong:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000103 case UnsignedLong: return getLongWidth();
Chris Lattnere4a8c642009-11-05 21:21:32 +0000104 case SignedLongLong:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000105 case UnsignedLongLong: return getLongLongWidth();
106 };
107}
108
Chris Lattnere4a8c642009-11-05 21:21:32 +0000109/// getTypeAlign - Return the alignment (in bits) of the specified integer type
110/// enum. For example, SignedInt -> getIntAlign().
111unsigned TargetInfo::getTypeAlign(IntType T) const {
112 switch (T) {
113 default: assert(0 && "not an integer!");
114 case SignedShort:
115 case UnsignedShort: return getShortAlign();
116 case SignedInt:
117 case UnsignedInt: return getIntAlign();
118 case SignedLong:
119 case UnsignedLong: return getLongAlign();
120 case SignedLongLong:
121 case UnsignedLongLong: return getLongLongAlign();
122 };
123}
124
Chris Lattnerc0c04392009-10-25 22:49:18 +0000125/// isTypeSigned - Return whether an integer types is signed. Returns true if
Chris Lattner72cdcac2009-10-21 06:24:21 +0000126/// the type is signed; false otherwise.
Chris Lattnerc0c04392009-10-25 22:49:18 +0000127bool TargetInfo::isTypeSigned(IntType T) const {
Chris Lattner72cdcac2009-10-21 06:24:21 +0000128 switch (T) {
129 default: assert(0 && "not an integer!");
130 case SignedShort:
131 case SignedInt:
132 case SignedLong:
133 case SignedLongLong:
134 return true;
135 case UnsignedShort:
136 case UnsignedInt:
137 case UnsignedLong:
138 case UnsignedLongLong:
139 return false;
140 };
141}
142
John Thompsoned4e2952009-11-05 20:14:16 +0000143/// setForcedLangOptions - Set forced language options.
144/// Apply changes to the target information with respect to certain
145/// language options which change the target configuration.
146void TargetInfo::setForcedLangOptions(LangOptions &Opts) {
Daniel Dunbar9302f602010-04-15 15:06:22 +0000147 if (Opts.NoBitFieldTypeAlign)
148 UseBitFieldTypeAlignment = false;
149 if (Opts.ShortWChar)
John Thompsoned4e2952009-11-05 20:14:16 +0000150 WCharType = UnsignedShort;
John Thompsoned4e2952009-11-05 20:14:16 +0000151}
Chris Lattner72cdcac2009-10-21 06:24:21 +0000152
Chris Lattnerec0a6d92007-09-22 18:29:59 +0000153//===----------------------------------------------------------------------===//
Chris Lattnerec0a6d92007-09-22 18:29:59 +0000154
Chris Lattner10a5b382007-01-29 05:24:35 +0000155
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000156static llvm::StringRef removeGCCRegisterPrefix(llvm::StringRef Name) {
Anders Carlsson3ed4aea2008-02-06 00:11:32 +0000157 if (Name[0] == '%' || Name[0] == '#')
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000158 Name = Name.substr(1);
159
160 return Name;
Anders Carlsson3ed4aea2008-02-06 00:11:32 +0000161}
162
Anders Carlsson5fa3f342007-11-24 23:38:12 +0000163/// isValidGCCRegisterName - Returns whether the passed in string
164/// is a valid register name according to GCC. This is used by Sema for
165/// inline asm statements.
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000166bool TargetInfo::isValidGCCRegisterName(llvm::StringRef Name) const {
167 if (Name.empty())
168 return false;
169
Anders Carlsson290aa852007-11-25 00:25:21 +0000170 const char * const *Names;
171 unsigned NumNames;
Mike Stump11289f42009-09-09 15:08:12 +0000172
Anders Carlsson290aa852007-11-25 00:25:21 +0000173 // Get rid of any register prefix.
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000174 Name = removeGCCRegisterPrefix(Name);
Anders Carlsson3ed4aea2008-02-06 00:11:32 +0000175
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000176 if (Name == "memory" || Name == "cc")
Anders Carlsson290aa852007-11-25 00:25:21 +0000177 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000178
Chris Lattnerc3a669b2008-03-08 08:24:01 +0000179 getGCCRegNames(Names, NumNames);
Mike Stump11289f42009-09-09 15:08:12 +0000180
Anders Carlsson290aa852007-11-25 00:25:21 +0000181 // If we have a number it maps to an entry in the register name array.
182 if (isdigit(Name[0])) {
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000183 int n;
184 if (!Name.getAsInteger(0, n))
Anders Carlsson290aa852007-11-25 00:25:21 +0000185 return n >= 0 && (unsigned)n < NumNames;
186 }
187
188 // Check register names.
189 for (unsigned i = 0; i < NumNames; i++) {
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000190 if (Name == Names[i])
Anders Carlsson290aa852007-11-25 00:25:21 +0000191 return true;
192 }
Mike Stump11289f42009-09-09 15:08:12 +0000193
Anders Carlsson290aa852007-11-25 00:25:21 +0000194 // Now check aliases.
Chris Lattnerc3a669b2008-03-08 08:24:01 +0000195 const GCCRegAlias *Aliases;
Anders Carlsson290aa852007-11-25 00:25:21 +0000196 unsigned NumAliases;
Mike Stump11289f42009-09-09 15:08:12 +0000197
Chris Lattnerc3a669b2008-03-08 08:24:01 +0000198 getGCCRegAliases(Aliases, NumAliases);
Anders Carlsson290aa852007-11-25 00:25:21 +0000199 for (unsigned i = 0; i < NumAliases; i++) {
200 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
201 if (!Aliases[i].Aliases[j])
202 break;
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000203 if (Aliases[i].Aliases[j] == Name)
Anders Carlsson290aa852007-11-25 00:25:21 +0000204 return true;
205 }
206 }
Mike Stump11289f42009-09-09 15:08:12 +0000207
Anders Carlsson5fa3f342007-11-24 23:38:12 +0000208 return false;
209}
Anders Carlssonf511f642007-11-27 04:11:28 +0000210
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000211llvm::StringRef
212TargetInfo::getNormalizedGCCRegisterName(llvm::StringRef Name) const {
Anders Carlssonf511f642007-11-27 04:11:28 +0000213 assert(isValidGCCRegisterName(Name) && "Invalid register passed in");
Mike Stump11289f42009-09-09 15:08:12 +0000214
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000215 // Get rid of any register prefix.
216 Name = removeGCCRegisterPrefix(Name);
Mike Stump11289f42009-09-09 15:08:12 +0000217
Anders Carlssonf511f642007-11-27 04:11:28 +0000218 const char * const *Names;
219 unsigned NumNames;
220
Chris Lattnerc3a669b2008-03-08 08:24:01 +0000221 getGCCRegNames(Names, NumNames);
Anders Carlssonf511f642007-11-27 04:11:28 +0000222
223 // First, check if we have a number.
224 if (isdigit(Name[0])) {
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000225 int n;
226 if (!Name.getAsInteger(0, n)) {
Mike Stump11289f42009-09-09 15:08:12 +0000227 assert(n >= 0 && (unsigned)n < NumNames &&
Anders Carlssonf511f642007-11-27 04:11:28 +0000228 "Out of bounds register number!");
229 return Names[n];
230 }
231 }
Mike Stump11289f42009-09-09 15:08:12 +0000232
Anders Carlssonf511f642007-11-27 04:11:28 +0000233 // Now check aliases.
Chris Lattnerc3a669b2008-03-08 08:24:01 +0000234 const GCCRegAlias *Aliases;
Anders Carlssonf511f642007-11-27 04:11:28 +0000235 unsigned NumAliases;
Mike Stump11289f42009-09-09 15:08:12 +0000236
Chris Lattnerc3a669b2008-03-08 08:24:01 +0000237 getGCCRegAliases(Aliases, NumAliases);
Anders Carlssonf511f642007-11-27 04:11:28 +0000238 for (unsigned i = 0; i < NumAliases; i++) {
239 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
240 if (!Aliases[i].Aliases[j])
241 break;
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000242 if (Aliases[i].Aliases[j] == Name)
Anders Carlssonf511f642007-11-27 04:11:28 +0000243 return Aliases[i].Register;
244 }
245 }
Mike Stump11289f42009-09-09 15:08:12 +0000246
Anders Carlssonf511f642007-11-27 04:11:28 +0000247 return Name;
248}
249
Chris Lattnerc3f4c7b2009-04-26 17:19:08 +0000250bool TargetInfo::validateOutputConstraint(ConstraintInfo &Info) const {
251 const char *Name = Info.getConstraintStr().c_str();
Anders Carlssonf511f642007-11-27 04:11:28 +0000252 // An output constraint must start with '=' or '+'
253 if (*Name != '=' && *Name != '+')
254 return false;
255
256 if (*Name == '+')
Chris Lattnerd9725f72009-04-26 07:16:29 +0000257 Info.setIsReadWrite();
Anders Carlssonf511f642007-11-27 04:11:28 +0000258
259 Name++;
260 while (*Name) {
261 switch (*Name) {
262 default:
Chris Lattnerd9725f72009-04-26 07:16:29 +0000263 if (!validateAsmConstraint(Name, Info)) {
Ted Kremenekb44485b2008-04-24 16:36:38 +0000264 // FIXME: We temporarily return false
Anders Carlssonf511f642007-11-27 04:11:28 +0000265 // so we can add more constraints as we hit it.
266 // Eventually, an unknown constraint should just be treated as 'g'.
Ted Kremenekb44485b2008-04-24 16:36:38 +0000267 return false;
Anders Carlssonf511f642007-11-27 04:11:28 +0000268 }
269 case '&': // early clobber.
270 break;
Chris Lattnerf3154712009-10-13 04:32:07 +0000271 case '%': // commutative.
272 // FIXME: Check that there is a another register after this one.
273 break;
Anders Carlssonf511f642007-11-27 04:11:28 +0000274 case 'r': // general register.
Chris Lattnerd9725f72009-04-26 07:16:29 +0000275 Info.setAllowsRegister();
Anders Carlssonf511f642007-11-27 04:11:28 +0000276 break;
277 case 'm': // memory operand.
Chris Lattnerd9725f72009-04-26 07:16:29 +0000278 Info.setAllowsMemory();
Anders Carlssonf511f642007-11-27 04:11:28 +0000279 break;
280 case 'g': // general register, memory operand or immediate integer.
Anders Carlssone70cde12009-01-18 02:12:04 +0000281 case 'X': // any operand.
Chris Lattnerd9725f72009-04-26 07:16:29 +0000282 Info.setAllowsRegister();
283 Info.setAllowsMemory();
Anders Carlssonf511f642007-11-27 04:11:28 +0000284 break;
285 }
Mike Stump11289f42009-09-09 15:08:12 +0000286
Anders Carlssonf511f642007-11-27 04:11:28 +0000287 Name++;
288 }
Mike Stump11289f42009-09-09 15:08:12 +0000289
Anders Carlssonf511f642007-11-27 04:11:28 +0000290 return true;
291}
292
Anders Carlssona79203b2009-01-18 01:56:57 +0000293bool TargetInfo::resolveSymbolicName(const char *&Name,
Chris Lattnerc16d4762009-04-26 17:57:12 +0000294 ConstraintInfo *OutputConstraints,
295 unsigned NumOutputs,
Chris Lattnerd9725f72009-04-26 07:16:29 +0000296 unsigned &Index) const {
Anders Carlssona79203b2009-01-18 01:56:57 +0000297 assert(*Name == '[' && "Symbolic name did not start with '['");
Anders Carlssona79203b2009-01-18 01:56:57 +0000298 Name++;
299 const char *Start = Name;
300 while (*Name && *Name != ']')
301 Name++;
Mike Stump11289f42009-09-09 15:08:12 +0000302
Anders Carlssona79203b2009-01-18 01:56:57 +0000303 if (!*Name) {
304 // Missing ']'
305 return false;
306 }
Mike Stump11289f42009-09-09 15:08:12 +0000307
Anders Carlssona79203b2009-01-18 01:56:57 +0000308 std::string SymbolicName(Start, Name - Start);
Mike Stump11289f42009-09-09 15:08:12 +0000309
Chris Lattnerc16d4762009-04-26 17:57:12 +0000310 for (Index = 0; Index != NumOutputs; ++Index)
311 if (SymbolicName == OutputConstraints[Index].getName())
Anders Carlssona79203b2009-01-18 01:56:57 +0000312 return true;
Anders Carlssona79203b2009-01-18 01:56:57 +0000313
314 return false;
315}
316
Chris Lattnerc16d4762009-04-26 17:57:12 +0000317bool TargetInfo::validateInputConstraint(ConstraintInfo *OutputConstraints,
318 unsigned NumOutputs,
Chris Lattnerd9725f72009-04-26 07:16:29 +0000319 ConstraintInfo &Info) const {
Chris Lattnerc3f4c7b2009-04-26 17:19:08 +0000320 const char *Name = Info.ConstraintStr.c_str();
321
Anders Carlssonf511f642007-11-27 04:11:28 +0000322 while (*Name) {
323 switch (*Name) {
324 default:
325 // Check if we have a matching constraint
326 if (*Name >= '0' && *Name <= '9') {
327 unsigned i = *Name - '0';
Mike Stump11289f42009-09-09 15:08:12 +0000328
Anders Carlssonf511f642007-11-27 04:11:28 +0000329 // Check if matching constraint is out of bounds.
330 if (i >= NumOutputs)
331 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000332
333 // The constraint should have the same info as the respective
Anders Carlsson570c3572009-01-27 20:38:24 +0000334 // output constraint.
Chris Lattner4c92b512009-04-26 18:05:25 +0000335 Info.setTiedOperand(i, OutputConstraints[i]);
Chris Lattnerd9725f72009-04-26 07:16:29 +0000336 } else if (!validateAsmConstraint(Name, Info)) {
Daniel Dunbar81128e02008-08-25 09:46:27 +0000337 // FIXME: This error return is in place temporarily so we can
338 // add more constraints as we hit it. Eventually, an unknown
339 // constraint should just be treated as 'g'.
340 return false;
Anders Carlssona79203b2009-01-18 01:56:57 +0000341 }
342 break;
343 case '[': {
344 unsigned Index = 0;
Chris Lattnerc16d4762009-04-26 17:57:12 +0000345 if (!resolveSymbolicName(Name, OutputConstraints, NumOutputs, Index))
Anders Carlssona79203b2009-01-18 01:56:57 +0000346 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000347
Anders Carlssona79203b2009-01-18 01:56:57 +0000348 break;
Mike Stump11289f42009-09-09 15:08:12 +0000349 }
Anders Carlsson050f4942007-12-08 19:32:57 +0000350 case '%': // commutative
351 // FIXME: Fail if % is used with the last operand.
352 break;
Anders Carlssonf511f642007-11-27 04:11:28 +0000353 case 'i': // immediate integer.
Anders Carlssona3a96af2008-03-09 06:02:02 +0000354 case 'n': // immediate integer with a known value.
Anders Carlssonf511f642007-11-27 04:11:28 +0000355 break;
Chris Lattnerdbcc5ca2009-05-06 04:33:31 +0000356 case 'I': // Various constant constraints with target-specific meanings.
357 case 'J':
358 case 'K':
359 case 'L':
360 case 'M':
361 case 'N':
362 case 'O':
363 case 'P':
364 break;
Anders Carlssonf511f642007-11-27 04:11:28 +0000365 case 'r': // general register.
Chris Lattnerd9725f72009-04-26 07:16:29 +0000366 Info.setAllowsRegister();
Anders Carlssonf511f642007-11-27 04:11:28 +0000367 break;
368 case 'm': // memory operand.
Nuno Lopes2af2af22009-12-16 14:28:21 +0000369 case 'o': // offsettable memory operand
370 case 'V': // non-offsettable memory operand
Chris Lattnerd9725f72009-04-26 07:16:29 +0000371 Info.setAllowsMemory();
Anders Carlssonf511f642007-11-27 04:11:28 +0000372 break;
373 case 'g': // general register, memory operand or immediate integer.
Anders Carlssone70cde12009-01-18 02:12:04 +0000374 case 'X': // any operand.
Chris Lattnerd9725f72009-04-26 07:16:29 +0000375 Info.setAllowsRegister();
376 Info.setAllowsMemory();
Anders Carlssonf511f642007-11-27 04:11:28 +0000377 break;
378 }
Mike Stump11289f42009-09-09 15:08:12 +0000379
Anders Carlssonf511f642007-11-27 04:11:28 +0000380 Name++;
381 }
Mike Stump11289f42009-09-09 15:08:12 +0000382
Anders Carlssonf511f642007-11-27 04:11:28 +0000383 return true;
384}