blob: 6692e641f2a420d4073bb88a461180aea93d0ce3 [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"
John Thompsona6fda122009-11-05 20:14:16 +000015#include "clang/Basic/LangOptions.h"
Chris Lattner525a0502007-09-22 18:29:59 +000016#include "llvm/ADT/APFloat.h"
Anders Carlsson6fa90862007-11-25 00:25:21 +000017#include "llvm/ADT/STLExtras.h"
Chris Lattner5483bdf2008-04-06 04:02:29 +000018#include <cstdlib>
Reid Spencer5f016e22007-07-11 17:01:13 +000019using namespace clang;
20
Chris Lattnercd4fc422008-03-08 08:59:43 +000021// TargetInfo Constructor.
22TargetInfo::TargetInfo(const std::string &T) : Triple(T) {
Daniel Dunbarb6a16932010-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 Friedmanb030f022009-04-19 21:38:35 +000025 TLSSupported = true;
Chris Lattner9bffb072010-04-23 16:29:58 +000026 NoAsmVariants = false;
Chris Lattner927686f2008-05-09 06:08:39 +000027 PointerWidth = PointerAlign = 32;
Chris Lattner2621fd12008-05-08 05:58:21 +000028 IntWidth = IntAlign = 32;
Chris Lattnerec10f582008-05-09 05:50:02 +000029 LongWidth = LongAlign = 32;
30 LongLongWidth = LongLongAlign = 64;
Eli Friedman61538a72008-05-20 14:21:01 +000031 FloatWidth = 32;
32 FloatAlign = 32;
Nate Begeman3d292062008-04-18 17:17:24 +000033 DoubleWidth = 64;
Eli Friedman61538a72008-05-20 14:21:01 +000034 DoubleAlign = 64;
35 LongDoubleWidth = 64;
36 LongDoubleAlign = 64;
Anders Carlsson6a3615c2008-10-31 16:05:19 +000037 SizeType = UnsignedLong;
Eli Friedmanf509d732008-11-02 02:43:55 +000038 PtrDiffType = SignedLong;
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +000039 IntMaxType = SignedLongLong;
40 UIntMaxType = UnsignedLongLong;
Chris Lattner6ad474f2009-02-13 22:28:55 +000041 IntPtrType = SignedLong;
Eli Friedmanf509d732008-11-02 02:43:55 +000042 WCharType = SignedInt;
Chris Lattnere64ef802009-10-21 04:59:34 +000043 WIntType = SignedInt;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +000044 Char16Type = UnsignedShort;
45 Char32Type = UnsignedInt;
Eli Friedman3c7b6e42009-07-01 03:36:11 +000046 Int64Type = SignedLongLong;
Edward O'Callaghan9cf910e2009-11-21 00:49:54 +000047 SigAtomicType = SignedInt;
Daniel Dunbarb6830d62010-04-15 15:06:18 +000048 UseBitFieldTypeAlignment = true;
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-"
Chris Lattner1932e122009-11-07 18:59:41 +000053 "i64:64:64-f32:32:32-f64:64:64-n32";
Chris Lattner3fdf4672008-10-05 19:22:37 +000054 UserLabelPrefix = "_";
Daniel Dunbar613fd672010-05-27 00:35:16 +000055 HasAlignMac68kSupport = false;
Chris Lattnercd4fc422008-03-08 08:59:43 +000056}
57
Chris Lattner0eaed122008-03-08 08:24:01 +000058// Out of line virtual dtor for TargetInfo.
59TargetInfo::~TargetInfo() {}
Reid Spencer5f016e22007-07-11 17:01:13 +000060
Chris Lattner2b5abf52009-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 Lattnerb304f772009-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 Lattner9099e7b2009-11-05 21:21:32 +000098 case SignedShort:
Chris Lattnerb304f772009-10-21 06:24:21 +000099 case UnsignedShort: return getShortWidth();
Chris Lattner9099e7b2009-11-05 21:21:32 +0000100 case SignedInt:
Chris Lattnerb304f772009-10-21 06:24:21 +0000101 case UnsignedInt: return getIntWidth();
Chris Lattner9099e7b2009-11-05 21:21:32 +0000102 case SignedLong:
Chris Lattnerb304f772009-10-21 06:24:21 +0000103 case UnsignedLong: return getLongWidth();
Chris Lattner9099e7b2009-11-05 21:21:32 +0000104 case SignedLongLong:
Chris Lattnerb304f772009-10-21 06:24:21 +0000105 case UnsignedLongLong: return getLongLongWidth();
106 };
107}
108
Chris Lattner9099e7b2009-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 Lattner961f0702009-10-25 22:49:18 +0000125/// isTypeSigned - Return whether an integer types is signed. Returns true if
Chris Lattnerb304f772009-10-21 06:24:21 +0000126/// the type is signed; false otherwise.
Chris Lattner961f0702009-10-25 22:49:18 +0000127bool TargetInfo::isTypeSigned(IntType T) const {
Chris Lattnerb304f772009-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 Thompsona6fda122009-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 Dunbarfb937b82010-04-15 15:06:22 +0000147 if (Opts.NoBitFieldTypeAlign)
148 UseBitFieldTypeAlignment = false;
149 if (Opts.ShortWChar)
John Thompsona6fda122009-11-05 20:14:16 +0000150 WCharType = UnsignedShort;
John Thompsona6fda122009-11-05 20:14:16 +0000151}
Chris Lattnerb304f772009-10-21 06:24:21 +0000152
Chris Lattner525a0502007-09-22 18:29:59 +0000153//===----------------------------------------------------------------------===//
Chris Lattner525a0502007-09-22 18:29:59 +0000154
Reid Spencer5f016e22007-07-11 17:01:13 +0000155
Anders Carlsson83c021c2010-01-30 19:12:25 +0000156static llvm::StringRef removeGCCRegisterPrefix(llvm::StringRef Name) {
Anders Carlssonea041752008-02-06 00:11:32 +0000157 if (Name[0] == '%' || Name[0] == '#')
Anders Carlsson83c021c2010-01-30 19:12:25 +0000158 Name = Name.substr(1);
159
160 return Name;
Anders Carlssonea041752008-02-06 00:11:32 +0000161}
162
Anders Carlsson3346ae62007-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 Carlsson83c021c2010-01-30 19:12:25 +0000166bool TargetInfo::isValidGCCRegisterName(llvm::StringRef Name) const {
167 if (Name.empty())
168 return false;
169
Anders Carlsson6fa90862007-11-25 00:25:21 +0000170 const char * const *Names;
171 unsigned NumNames;
Mike Stump1eb44332009-09-09 15:08:12 +0000172
Anders Carlsson6fa90862007-11-25 00:25:21 +0000173 // Get rid of any register prefix.
Anders Carlsson83c021c2010-01-30 19:12:25 +0000174 Name = removeGCCRegisterPrefix(Name);
Anders Carlssonea041752008-02-06 00:11:32 +0000175
Anders Carlsson83c021c2010-01-30 19:12:25 +0000176 if (Name == "memory" || Name == "cc")
Anders Carlsson6fa90862007-11-25 00:25:21 +0000177 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000178
Chris Lattner0eaed122008-03-08 08:24:01 +0000179 getGCCRegNames(Names, NumNames);
Mike Stump1eb44332009-09-09 15:08:12 +0000180
Anders Carlsson6fa90862007-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 Carlsson83c021c2010-01-30 19:12:25 +0000183 int n;
184 if (!Name.getAsInteger(0, n))
Anders Carlsson6fa90862007-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 Carlsson83c021c2010-01-30 19:12:25 +0000190 if (Name == Names[i])
Anders Carlsson6fa90862007-11-25 00:25:21 +0000191 return true;
192 }
Mike Stump1eb44332009-09-09 15:08:12 +0000193
Anders Carlsson6fa90862007-11-25 00:25:21 +0000194 // Now check aliases.
Chris Lattner0eaed122008-03-08 08:24:01 +0000195 const GCCRegAlias *Aliases;
Anders Carlsson6fa90862007-11-25 00:25:21 +0000196 unsigned NumAliases;
Mike Stump1eb44332009-09-09 15:08:12 +0000197
Chris Lattner0eaed122008-03-08 08:24:01 +0000198 getGCCRegAliases(Aliases, NumAliases);
Anders Carlsson6fa90862007-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 Carlsson83c021c2010-01-30 19:12:25 +0000203 if (Aliases[i].Aliases[j] == Name)
Anders Carlsson6fa90862007-11-25 00:25:21 +0000204 return true;
205 }
206 }
Mike Stump1eb44332009-09-09 15:08:12 +0000207
Anders Carlsson3346ae62007-11-24 23:38:12 +0000208 return false;
209}
Anders Carlssond04c6e22007-11-27 04:11:28 +0000210
Anders Carlsson83c021c2010-01-30 19:12:25 +0000211llvm::StringRef
212TargetInfo::getNormalizedGCCRegisterName(llvm::StringRef Name) const {
Anders Carlssond04c6e22007-11-27 04:11:28 +0000213 assert(isValidGCCRegisterName(Name) && "Invalid register passed in");
Mike Stump1eb44332009-09-09 15:08:12 +0000214
Anders Carlsson83c021c2010-01-30 19:12:25 +0000215 // Get rid of any register prefix.
216 Name = removeGCCRegisterPrefix(Name);
Mike Stump1eb44332009-09-09 15:08:12 +0000217
Anders Carlssond04c6e22007-11-27 04:11:28 +0000218 const char * const *Names;
219 unsigned NumNames;
220
Chris Lattner0eaed122008-03-08 08:24:01 +0000221 getGCCRegNames(Names, NumNames);
Anders Carlssond04c6e22007-11-27 04:11:28 +0000222
223 // First, check if we have a number.
224 if (isdigit(Name[0])) {
Anders Carlsson83c021c2010-01-30 19:12:25 +0000225 int n;
226 if (!Name.getAsInteger(0, n)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000227 assert(n >= 0 && (unsigned)n < NumNames &&
Anders Carlssond04c6e22007-11-27 04:11:28 +0000228 "Out of bounds register number!");
229 return Names[n];
230 }
231 }
Mike Stump1eb44332009-09-09 15:08:12 +0000232
Anders Carlssond04c6e22007-11-27 04:11:28 +0000233 // Now check aliases.
Chris Lattner0eaed122008-03-08 08:24:01 +0000234 const GCCRegAlias *Aliases;
Anders Carlssond04c6e22007-11-27 04:11:28 +0000235 unsigned NumAliases;
Mike Stump1eb44332009-09-09 15:08:12 +0000236
Chris Lattner0eaed122008-03-08 08:24:01 +0000237 getGCCRegAliases(Aliases, NumAliases);
Anders Carlssond04c6e22007-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 Carlsson83c021c2010-01-30 19:12:25 +0000242 if (Aliases[i].Aliases[j] == Name)
Anders Carlssond04c6e22007-11-27 04:11:28 +0000243 return Aliases[i].Register;
244 }
245 }
Mike Stump1eb44332009-09-09 15:08:12 +0000246
Anders Carlssond04c6e22007-11-27 04:11:28 +0000247 return Name;
248}
249
Chris Lattner432c8692009-04-26 17:19:08 +0000250bool TargetInfo::validateOutputConstraint(ConstraintInfo &Info) const {
251 const char *Name = Info.getConstraintStr().c_str();
Anders Carlssond04c6e22007-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 Lattner44def072009-04-26 07:16:29 +0000257 Info.setIsReadWrite();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000258
259 Name++;
260 while (*Name) {
261 switch (*Name) {
262 default:
Chris Lattner44def072009-04-26 07:16:29 +0000263 if (!validateAsmConstraint(Name, Info)) {
Ted Kremenek5ab201a2008-04-24 16:36:38 +0000264 // FIXME: We temporarily return false
Anders Carlssond04c6e22007-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 Kremenek5ab201a2008-04-24 16:36:38 +0000267 return false;
Anders Carlssond04c6e22007-11-27 04:11:28 +0000268 }
269 case '&': // early clobber.
270 break;
Chris Lattner40539832009-10-13 04:32:07 +0000271 case '%': // commutative.
272 // FIXME: Check that there is a another register after this one.
273 break;
Anders Carlssond04c6e22007-11-27 04:11:28 +0000274 case 'r': // general register.
Chris Lattner44def072009-04-26 07:16:29 +0000275 Info.setAllowsRegister();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000276 break;
277 case 'm': // memory operand.
Chris Lattner44def072009-04-26 07:16:29 +0000278 Info.setAllowsMemory();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000279 break;
280 case 'g': // general register, memory operand or immediate integer.
Anders Carlsson775841f2009-01-18 02:12:04 +0000281 case 'X': // any operand.
Chris Lattner44def072009-04-26 07:16:29 +0000282 Info.setAllowsRegister();
283 Info.setAllowsMemory();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000284 break;
285 }
Mike Stump1eb44332009-09-09 15:08:12 +0000286
Anders Carlssond04c6e22007-11-27 04:11:28 +0000287 Name++;
288 }
Mike Stump1eb44332009-09-09 15:08:12 +0000289
Anders Carlssond04c6e22007-11-27 04:11:28 +0000290 return true;
291}
292
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000293bool TargetInfo::resolveSymbolicName(const char *&Name,
Chris Lattner2819fa82009-04-26 17:57:12 +0000294 ConstraintInfo *OutputConstraints,
295 unsigned NumOutputs,
Chris Lattner44def072009-04-26 07:16:29 +0000296 unsigned &Index) const {
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000297 assert(*Name == '[' && "Symbolic name did not start with '['");
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000298 Name++;
299 const char *Start = Name;
300 while (*Name && *Name != ']')
301 Name++;
Mike Stump1eb44332009-09-09 15:08:12 +0000302
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000303 if (!*Name) {
304 // Missing ']'
305 return false;
306 }
Mike Stump1eb44332009-09-09 15:08:12 +0000307
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000308 std::string SymbolicName(Start, Name - Start);
Mike Stump1eb44332009-09-09 15:08:12 +0000309
Chris Lattner2819fa82009-04-26 17:57:12 +0000310 for (Index = 0; Index != NumOutputs; ++Index)
311 if (SymbolicName == OutputConstraints[Index].getName())
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000312 return true;
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000313
314 return false;
315}
316
Chris Lattner2819fa82009-04-26 17:57:12 +0000317bool TargetInfo::validateInputConstraint(ConstraintInfo *OutputConstraints,
318 unsigned NumOutputs,
Chris Lattner44def072009-04-26 07:16:29 +0000319 ConstraintInfo &Info) const {
Chris Lattner432c8692009-04-26 17:19:08 +0000320 const char *Name = Info.ConstraintStr.c_str();
321
Anders Carlssond04c6e22007-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 Stump1eb44332009-09-09 15:08:12 +0000328
Anders Carlssond04c6e22007-11-27 04:11:28 +0000329 // Check if matching constraint is out of bounds.
330 if (i >= NumOutputs)
331 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000332
333 // The constraint should have the same info as the respective
Anders Carlsson03eb5432009-01-27 20:38:24 +0000334 // output constraint.
Chris Lattnerd6887612009-04-26 18:05:25 +0000335 Info.setTiedOperand(i, OutputConstraints[i]);
Chris Lattner44def072009-04-26 07:16:29 +0000336 } else if (!validateAsmConstraint(Name, Info)) {
Daniel Dunbar302684c2008-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 Carlsson42e1ee02009-01-18 01:56:57 +0000341 }
342 break;
343 case '[': {
344 unsigned Index = 0;
Chris Lattner2819fa82009-04-26 17:57:12 +0000345 if (!resolveSymbolicName(Name, OutputConstraints, NumOutputs, Index))
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000346 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000347
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000348 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000349 }
Anders Carlsson44fe49c2007-12-08 19:32:57 +0000350 case '%': // commutative
351 // FIXME: Fail if % is used with the last operand.
352 break;
Anders Carlssond04c6e22007-11-27 04:11:28 +0000353 case 'i': // immediate integer.
Anders Carlssonefdfa772008-03-09 06:02:02 +0000354 case 'n': // immediate integer with a known value.
Anders Carlssond04c6e22007-11-27 04:11:28 +0000355 break;
Chris Lattner1d138792009-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 Carlssond04c6e22007-11-27 04:11:28 +0000365 case 'r': // general register.
Chris Lattner44def072009-04-26 07:16:29 +0000366 Info.setAllowsRegister();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000367 break;
368 case 'm': // memory operand.
Nuno Lopesaa8e3612009-12-16 14:28:21 +0000369 case 'o': // offsettable memory operand
370 case 'V': // non-offsettable memory operand
Chris Lattner44def072009-04-26 07:16:29 +0000371 Info.setAllowsMemory();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000372 break;
373 case 'g': // general register, memory operand or immediate integer.
Anders Carlsson775841f2009-01-18 02:12:04 +0000374 case 'X': // any operand.
Chris Lattner44def072009-04-26 07:16:29 +0000375 Info.setAllowsRegister();
376 Info.setAllowsMemory();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000377 break;
378 }
Mike Stump1eb44332009-09-09 15:08:12 +0000379
Anders Carlssond04c6e22007-11-27 04:11:28 +0000380 Name++;
381 }
Mike Stump1eb44332009-09-09 15:08:12 +0000382
Anders Carlssond04c6e22007-11-27 04:11:28 +0000383 return true;
384}