blob: db5941a5d921191f39de03a723b777456ffe8111 [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
Peter Collingbourne599cb8e2011-03-18 22:38:29 +000014#include "clang/Basic/AddressSpaces.h"
Chris Lattner1e27fe12006-10-14 07:06:20 +000015#include "clang/Basic/TargetInfo.h"
John Thompsoned4e2952009-11-05 20:14:16 +000016#include "clang/Basic/LangOptions.h"
Chris Lattnerec0a6d92007-09-22 18:29:59 +000017#include "llvm/ADT/APFloat.h"
Anders Carlsson290aa852007-11-25 00:25:21 +000018#include "llvm/ADT/STLExtras.h"
David Blaikie76bd3c82011-09-23 05:35:21 +000019#include "llvm/Support/ErrorHandling.h"
Nick Lewycky2e998b72010-12-19 20:49:25 +000020#include <cctype>
Chris Lattnerc4f02b62008-04-06 04:02:29 +000021#include <cstdlib>
Chris Lattner1e27fe12006-10-14 07:06:20 +000022using namespace clang;
23
Peter Collingbourne599cb8e2011-03-18 22:38:29 +000024static const LangAS::Map DefaultAddrSpaceMap = { 0 };
25
Chris Lattner1df27862008-03-08 08:59:43 +000026// TargetInfo Constructor.
27TargetInfo::TargetInfo(const std::string &T) : Triple(T) {
Daniel Dunbar3d9289c2010-04-15 06:18:39 +000028 // Set defaults. Defaults are set for a 32-bit RISC platform, like PPC or
29 // SPARC. These should be overridden by concrete targets as needed.
Eli Friedman803acb32011-12-22 03:51:45 +000030 BigEndian = true;
Eli Friedmand88c8a12009-04-19 21:38:35 +000031 TLSSupported = true;
Chris Lattner1a8f3942010-04-23 16:29:58 +000032 NoAsmVariants = false;
Chris Lattner5e2ef0c2008-05-09 06:08:39 +000033 PointerWidth = PointerAlign = 32;
Roman Divacky965b0b72011-01-06 08:27:10 +000034 BoolWidth = BoolAlign = 8;
Chris Lattnerb781dc792008-05-08 05:58:21 +000035 IntWidth = IntAlign = 32;
Chris Lattner5a9aaaf2008-05-09 05:50:02 +000036 LongWidth = LongAlign = 32;
37 LongLongWidth = LongLongAlign = 64;
Nick Lewyckyf59e1292011-12-16 22:34:14 +000038 SuitableAlign = 64;
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +000039 HalfWidth = 16;
40 HalfAlign = 16;
Eli Friedmanb5366062008-05-20 14:21:01 +000041 FloatWidth = 32;
42 FloatAlign = 32;
Nate Begeman65bea232008-04-18 17:17:24 +000043 DoubleWidth = 64;
Eli Friedmanb5366062008-05-20 14:21:01 +000044 DoubleAlign = 64;
45 LongDoubleWidth = 64;
46 LongDoubleAlign = 64;
Rafael Espindolae971b9a2010-06-04 23:15:27 +000047 LargeArrayMinWidth = 0;
48 LargeArrayAlign = 0;
Eli Friedman4b72fdd2011-10-14 20:59:01 +000049 MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 0;
Chad Rosiercc40ea72012-07-13 23:57:43 +000050 MaxVectorAlign = 0;
Anders Carlsson2a79a902008-10-31 16:05:19 +000051 SizeType = UnsignedLong;
Eli Friedmand50881c2008-11-02 02:43:55 +000052 PtrDiffType = SignedLong;
Sanjiv Guptad7959242008-10-31 09:52:39 +000053 IntMaxType = SignedLongLong;
54 UIntMaxType = UnsignedLongLong;
Chris Lattner7e4c81c2009-02-13 22:28:55 +000055 IntPtrType = SignedLong;
Eli Friedmand50881c2008-11-02 02:43:55 +000056 WCharType = SignedInt;
Chris Lattner67204922009-10-21 04:59:34 +000057 WIntType = SignedInt;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +000058 Char16Type = UnsignedShort;
59 Char32Type = UnsignedInt;
Eli Friedman2857ccb2009-07-01 03:36:11 +000060 Int64Type = SignedLongLong;
Edward O'Callaghan847f2a12009-11-21 00:49:54 +000061 SigAtomicType = SignedInt;
Fariborz Jahanian29898f42012-04-16 21:03:30 +000062 UseSignedCharForObjCBool = true;
Daniel Dunbar1da65112010-04-15 15:06:18 +000063 UseBitFieldTypeAlignment = true;
Chad Rosier18903ee2011-08-04 01:21:14 +000064 UseZeroLengthBitfieldAlignment = false;
65 ZeroLengthBitfieldBoundary = 0;
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +000066 HalfFormat = &llvm::APFloat::IEEEhalf;
Chris Lattner1df27862008-03-08 08:59:43 +000067 FloatFormat = &llvm::APFloat::IEEEsingle;
68 DoubleFormat = &llvm::APFloat::IEEEdouble;
69 LongDoubleFormat = &llvm::APFloat::IEEEdouble;
Eli Friedman873f65a2008-08-21 00:13:15 +000070 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 +000071 "i64:64:64-f32:32:32-f64:64:64-n32";
Chris Lattnerf37bafc2008-10-05 19:22:37 +000072 UserLabelPrefix = "_";
Roman Divacky178e01602011-02-10 16:52:03 +000073 MCountName = "mcount";
Abramo Bagnara41bfb662011-09-08 14:20:25 +000074 RegParmMax = 0;
75 SSERegParmMax = 0;
Daniel Dunbarbd606522010-05-27 00:35:16 +000076 HasAlignMac68kSupport = false;
Daniel Dunbar6f2e8392010-07-14 23:39:36 +000077
78 // Default to no types using fpret.
79 RealTypeUsesObjCFPRet = 0;
John McCall86353412010-08-21 22:46:04 +000080
Anders Carlsson2f1a6c32011-10-31 16:27:11 +000081 // Default to not using fp2ret for __Complex long double
82 ComplexLongDoubleUsesFP2Ret = false;
83
John McCall86353412010-08-21 22:46:04 +000084 // Default to using the Itanium ABI.
85 CXXABI = CXXABI_Itanium;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +000086
87 // Default to an empty address space map.
88 AddrSpaceMap = &DefaultAddrSpaceMap;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +000089
90 // Default to an unknown platform name.
91 PlatformName = "unknown";
92 PlatformMinVersion = VersionTuple();
Chris Lattner1df27862008-03-08 08:59:43 +000093}
94
Chris Lattnerc3a669b2008-03-08 08:24:01 +000095// Out of line virtual dtor for TargetInfo.
96TargetInfo::~TargetInfo() {}
Chris Lattner2194ddc2006-10-14 18:32:26 +000097
Chris Lattnera91c30f2009-02-06 05:04:11 +000098/// getTypeName - Return the user string for the specified integer type enum.
99/// For example, SignedShort -> "short".
100const char *TargetInfo::getTypeName(IntType T) {
101 switch (T) {
David Blaikie83d382b2011-09-23 05:06:16 +0000102 default: llvm_unreachable("not an integer!");
Chris Lattnera91c30f2009-02-06 05:04:11 +0000103 case SignedShort: return "short";
104 case UnsignedShort: return "unsigned short";
105 case SignedInt: return "int";
106 case UnsignedInt: return "unsigned int";
107 case SignedLong: return "long int";
108 case UnsignedLong: return "long unsigned int";
109 case SignedLongLong: return "long long int";
110 case UnsignedLongLong: return "long long unsigned int";
111 }
112}
113
Chris Lattner72cdcac2009-10-21 06:24:21 +0000114/// getTypeConstantSuffix - Return the constant suffix for the specified
115/// integer type enum. For example, SignedLong -> "L".
116const char *TargetInfo::getTypeConstantSuffix(IntType T) {
117 switch (T) {
David Blaikie83d382b2011-09-23 05:06:16 +0000118 default: llvm_unreachable("not an integer!");
Chris Lattner72cdcac2009-10-21 06:24:21 +0000119 case SignedShort:
120 case SignedInt: return "";
121 case SignedLong: return "L";
122 case SignedLongLong: return "LL";
123 case UnsignedShort:
124 case UnsignedInt: return "U";
125 case UnsignedLong: return "UL";
126 case UnsignedLongLong: return "ULL";
127 }
128}
129
Michael J. Spencerfeb799c2010-10-18 07:10:59 +0000130/// getTypeWidth - Return the width (in bits) of the specified integer type
Chris Lattner72cdcac2009-10-21 06:24:21 +0000131/// enum. For example, SignedInt -> getIntWidth().
132unsigned TargetInfo::getTypeWidth(IntType T) const {
133 switch (T) {
David Blaikie83d382b2011-09-23 05:06:16 +0000134 default: llvm_unreachable("not an integer!");
Chris Lattnere4a8c642009-11-05 21:21:32 +0000135 case SignedShort:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000136 case UnsignedShort: return getShortWidth();
Chris Lattnere4a8c642009-11-05 21:21:32 +0000137 case SignedInt:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000138 case UnsignedInt: return getIntWidth();
Chris Lattnere4a8c642009-11-05 21:21:32 +0000139 case SignedLong:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000140 case UnsignedLong: return getLongWidth();
Chris Lattnere4a8c642009-11-05 21:21:32 +0000141 case SignedLongLong:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000142 case UnsignedLongLong: return getLongLongWidth();
143 };
144}
145
Michael J. Spencerfeb799c2010-10-18 07:10:59 +0000146/// getTypeAlign - Return the alignment (in bits) of the specified integer type
Chris Lattnere4a8c642009-11-05 21:21:32 +0000147/// enum. For example, SignedInt -> getIntAlign().
148unsigned TargetInfo::getTypeAlign(IntType T) const {
149 switch (T) {
David Blaikie83d382b2011-09-23 05:06:16 +0000150 default: llvm_unreachable("not an integer!");
Chris Lattnere4a8c642009-11-05 21:21:32 +0000151 case SignedShort:
152 case UnsignedShort: return getShortAlign();
153 case SignedInt:
154 case UnsignedInt: return getIntAlign();
155 case SignedLong:
156 case UnsignedLong: return getLongAlign();
157 case SignedLongLong:
158 case UnsignedLongLong: return getLongLongAlign();
159 };
160}
161
Chris Lattnerc0c04392009-10-25 22:49:18 +0000162/// isTypeSigned - Return whether an integer types is signed. Returns true if
Chris Lattner72cdcac2009-10-21 06:24:21 +0000163/// the type is signed; false otherwise.
Chris Lattnerad3467e2010-12-25 23:25:43 +0000164bool TargetInfo::isTypeSigned(IntType T) {
Chris Lattner72cdcac2009-10-21 06:24:21 +0000165 switch (T) {
David Blaikie83d382b2011-09-23 05:06:16 +0000166 default: llvm_unreachable("not an integer!");
Chris Lattner72cdcac2009-10-21 06:24:21 +0000167 case SignedShort:
168 case SignedInt:
169 case SignedLong:
Michael J. Spencerfeb799c2010-10-18 07:10:59 +0000170 case SignedLongLong:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000171 return true;
172 case UnsignedShort:
173 case UnsignedInt:
174 case UnsignedLong:
Michael J. Spencerfeb799c2010-10-18 07:10:59 +0000175 case UnsignedLongLong:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000176 return false;
177 };
178}
179
John Thompsoned4e2952009-11-05 20:14:16 +0000180/// setForcedLangOptions - Set forced language options.
181/// Apply changes to the target information with respect to certain
182/// language options which change the target configuration.
183void TargetInfo::setForcedLangOptions(LangOptions &Opts) {
Daniel Dunbar9302f602010-04-15 15:06:22 +0000184 if (Opts.NoBitFieldTypeAlign)
185 UseBitFieldTypeAlignment = false;
186 if (Opts.ShortWChar)
John Thompsoned4e2952009-11-05 20:14:16 +0000187 WCharType = UnsignedShort;
John Thompsoned4e2952009-11-05 20:14:16 +0000188}
Chris Lattner72cdcac2009-10-21 06:24:21 +0000189
Chris Lattnerec0a6d92007-09-22 18:29:59 +0000190//===----------------------------------------------------------------------===//
Chris Lattnerec0a6d92007-09-22 18:29:59 +0000191
Chris Lattner10a5b382007-01-29 05:24:35 +0000192
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000193static StringRef removeGCCRegisterPrefix(StringRef Name) {
Anders Carlsson3ed4aea2008-02-06 00:11:32 +0000194 if (Name[0] == '%' || Name[0] == '#')
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000195 Name = Name.substr(1);
Michael J. Spencerfeb799c2010-10-18 07:10:59 +0000196
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000197 return Name;
Anders Carlsson3ed4aea2008-02-06 00:11:32 +0000198}
199
Eric Christopherfd9a5f42011-06-28 18:20:53 +0000200/// isValidClobber - Returns whether the passed in string is
201/// a valid clobber in an inline asm statement. This is used by
202/// Sema.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000203bool TargetInfo::isValidClobber(StringRef Name) const {
Eric Christopherfd9a5f42011-06-28 18:20:53 +0000204 return (isValidGCCRegisterName(Name) ||
205 Name == "memory" || Name == "cc");
206}
207
Anders Carlsson5fa3f342007-11-24 23:38:12 +0000208/// isValidGCCRegisterName - Returns whether the passed in string
209/// is a valid register name according to GCC. This is used by Sema for
210/// inline asm statements.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000211bool TargetInfo::isValidGCCRegisterName(StringRef Name) const {
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000212 if (Name.empty())
213 return false;
Michael J. Spencerfeb799c2010-10-18 07:10:59 +0000214
Anders Carlsson290aa852007-11-25 00:25:21 +0000215 const char * const *Names;
216 unsigned NumNames;
Mike Stump11289f42009-09-09 15:08:12 +0000217
Anders Carlsson290aa852007-11-25 00:25:21 +0000218 // Get rid of any register prefix.
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000219 Name = removeGCCRegisterPrefix(Name);
Anders Carlsson3ed4aea2008-02-06 00:11:32 +0000220
Chris Lattnerc3a669b2008-03-08 08:24:01 +0000221 getGCCRegNames(Names, NumNames);
Mike Stump11289f42009-09-09 15:08:12 +0000222
Anders Carlsson290aa852007-11-25 00:25:21 +0000223 // If we have a number it maps to an entry in the register name array.
224 if (isdigit(Name[0])) {
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000225 int n;
226 if (!Name.getAsInteger(0, n))
Anders Carlsson290aa852007-11-25 00:25:21 +0000227 return n >= 0 && (unsigned)n < NumNames;
228 }
229
230 // Check register names.
231 for (unsigned i = 0; i < NumNames; i++) {
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000232 if (Name == Names[i])
Anders Carlsson290aa852007-11-25 00:25:21 +0000233 return true;
234 }
Mike Stump11289f42009-09-09 15:08:12 +0000235
Eric Christophercdd36352011-06-21 00:05:20 +0000236 // Check any additional names that we have.
237 const AddlRegName *AddlNames;
238 unsigned NumAddlNames;
239 getGCCAddlRegNames(AddlNames, NumAddlNames);
240 for (unsigned i = 0; i < NumAddlNames; i++)
241 for (unsigned j = 0; j < llvm::array_lengthof(AddlNames[i].Names); j++) {
242 if (!AddlNames[i].Names[j])
243 break;
244 // Make sure the register that the additional name is for is within
245 // the bounds of the register names from above.
246 if (AddlNames[i].Names[j] == Name && AddlNames[i].RegNum < NumNames)
247 return true;
248 }
249
Anders Carlsson290aa852007-11-25 00:25:21 +0000250 // Now check aliases.
Chris Lattnerc3a669b2008-03-08 08:24:01 +0000251 const GCCRegAlias *Aliases;
Anders Carlsson290aa852007-11-25 00:25:21 +0000252 unsigned NumAliases;
Mike Stump11289f42009-09-09 15:08:12 +0000253
Chris Lattnerc3a669b2008-03-08 08:24:01 +0000254 getGCCRegAliases(Aliases, NumAliases);
Anders Carlsson290aa852007-11-25 00:25:21 +0000255 for (unsigned i = 0; i < NumAliases; i++) {
256 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
257 if (!Aliases[i].Aliases[j])
258 break;
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000259 if (Aliases[i].Aliases[j] == Name)
Anders Carlsson290aa852007-11-25 00:25:21 +0000260 return true;
261 }
262 }
Mike Stump11289f42009-09-09 15:08:12 +0000263
Anders Carlsson5fa3f342007-11-24 23:38:12 +0000264 return false;
265}
Anders Carlssonf511f642007-11-27 04:11:28 +0000266
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000267StringRef
268TargetInfo::getNormalizedGCCRegisterName(StringRef Name) const {
Anders Carlssonf511f642007-11-27 04:11:28 +0000269 assert(isValidGCCRegisterName(Name) && "Invalid register passed in");
Mike Stump11289f42009-09-09 15:08:12 +0000270
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000271 // Get rid of any register prefix.
272 Name = removeGCCRegisterPrefix(Name);
Mike Stump11289f42009-09-09 15:08:12 +0000273
Anders Carlssonf511f642007-11-27 04:11:28 +0000274 const char * const *Names;
275 unsigned NumNames;
276
Chris Lattnerc3a669b2008-03-08 08:24:01 +0000277 getGCCRegNames(Names, NumNames);
Anders Carlssonf511f642007-11-27 04:11:28 +0000278
279 // First, check if we have a number.
280 if (isdigit(Name[0])) {
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000281 int n;
282 if (!Name.getAsInteger(0, n)) {
Mike Stump11289f42009-09-09 15:08:12 +0000283 assert(n >= 0 && (unsigned)n < NumNames &&
Anders Carlssonf511f642007-11-27 04:11:28 +0000284 "Out of bounds register number!");
285 return Names[n];
286 }
287 }
Mike Stump11289f42009-09-09 15:08:12 +0000288
Eric Christophercdd36352011-06-21 00:05:20 +0000289 // Check any additional names that we have.
290 const AddlRegName *AddlNames;
291 unsigned NumAddlNames;
292 getGCCAddlRegNames(AddlNames, NumAddlNames);
293 for (unsigned i = 0; i < NumAddlNames; i++)
294 for (unsigned j = 0; j < llvm::array_lengthof(AddlNames[i].Names); j++) {
295 if (!AddlNames[i].Names[j])
296 break;
297 // Make sure the register that the additional name is for is within
298 // the bounds of the register names from above.
299 if (AddlNames[i].Names[j] == Name && AddlNames[i].RegNum < NumNames)
300 return Name;
301 }
302
Anders Carlssonf511f642007-11-27 04:11:28 +0000303 // Now check aliases.
Chris Lattnerc3a669b2008-03-08 08:24:01 +0000304 const GCCRegAlias *Aliases;
Anders Carlssonf511f642007-11-27 04:11:28 +0000305 unsigned NumAliases;
Mike Stump11289f42009-09-09 15:08:12 +0000306
Chris Lattnerc3a669b2008-03-08 08:24:01 +0000307 getGCCRegAliases(Aliases, NumAliases);
Anders Carlssonf511f642007-11-27 04:11:28 +0000308 for (unsigned i = 0; i < NumAliases; i++) {
309 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
310 if (!Aliases[i].Aliases[j])
311 break;
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000312 if (Aliases[i].Aliases[j] == Name)
Anders Carlssonf511f642007-11-27 04:11:28 +0000313 return Aliases[i].Register;
314 }
315 }
Mike Stump11289f42009-09-09 15:08:12 +0000316
Anders Carlssonf511f642007-11-27 04:11:28 +0000317 return Name;
318}
319
Chris Lattnerc3f4c7b2009-04-26 17:19:08 +0000320bool TargetInfo::validateOutputConstraint(ConstraintInfo &Info) const {
321 const char *Name = Info.getConstraintStr().c_str();
Anders Carlssonf511f642007-11-27 04:11:28 +0000322 // An output constraint must start with '=' or '+'
323 if (*Name != '=' && *Name != '+')
324 return false;
325
326 if (*Name == '+')
Chris Lattnerd9725f72009-04-26 07:16:29 +0000327 Info.setIsReadWrite();
Anders Carlssonf511f642007-11-27 04:11:28 +0000328
329 Name++;
330 while (*Name) {
331 switch (*Name) {
332 default:
Chris Lattnerd9725f72009-04-26 07:16:29 +0000333 if (!validateAsmConstraint(Name, Info)) {
Ted Kremenekb44485b2008-04-24 16:36:38 +0000334 // FIXME: We temporarily return false
Anders Carlssonf511f642007-11-27 04:11:28 +0000335 // so we can add more constraints as we hit it.
336 // Eventually, an unknown constraint should just be treated as 'g'.
Ted Kremenekb44485b2008-04-24 16:36:38 +0000337 return false;
Anders Carlssonf511f642007-11-27 04:11:28 +0000338 }
339 case '&': // early clobber.
340 break;
Chris Lattnerf3154712009-10-13 04:32:07 +0000341 case '%': // commutative.
342 // FIXME: Check that there is a another register after this one.
343 break;
Anders Carlssonf511f642007-11-27 04:11:28 +0000344 case 'r': // general register.
Chris Lattnerd9725f72009-04-26 07:16:29 +0000345 Info.setAllowsRegister();
Anders Carlssonf511f642007-11-27 04:11:28 +0000346 break;
347 case 'm': // memory operand.
Dale Johannesen8499f472010-09-07 18:40:41 +0000348 case 'o': // offsetable memory operand.
349 case 'V': // non-offsetable memory operand.
350 case '<': // autodecrement memory operand.
351 case '>': // autoincrement memory operand.
Chris Lattnerd9725f72009-04-26 07:16:29 +0000352 Info.setAllowsMemory();
Anders Carlssonf511f642007-11-27 04:11:28 +0000353 break;
354 case 'g': // general register, memory operand or immediate integer.
Anders Carlssone70cde12009-01-18 02:12:04 +0000355 case 'X': // any operand.
Chris Lattnerd9725f72009-04-26 07:16:29 +0000356 Info.setAllowsRegister();
357 Info.setAllowsMemory();
Anders Carlssonf511f642007-11-27 04:11:28 +0000358 break;
John Thompsona5c7d702010-08-10 19:20:14 +0000359 case ',': // multiple alternative constraint. Pass it.
John Thompson46667af2010-08-11 00:58:20 +0000360 // Handle additional optional '=' or '+' modifiers.
John Thompson12240612010-09-18 01:15:13 +0000361 if (Name[1] == '=' || Name[1] == '+')
John Thompson46667af2010-08-11 00:58:20 +0000362 Name++;
John Thompsona5c7d702010-08-10 19:20:14 +0000363 break;
364 case '?': // Disparage slightly code.
Dale Johannesen8499f472010-09-07 18:40:41 +0000365 case '!': // Disparage severely.
John Thompsona5c7d702010-08-10 19:20:14 +0000366 break; // Pass them.
Anders Carlssonf511f642007-11-27 04:11:28 +0000367 }
Mike Stump11289f42009-09-09 15:08:12 +0000368
Anders Carlssonf511f642007-11-27 04:11:28 +0000369 Name++;
370 }
Mike Stump11289f42009-09-09 15:08:12 +0000371
Anders Carlssonf511f642007-11-27 04:11:28 +0000372 return true;
373}
374
Anders Carlssona79203b2009-01-18 01:56:57 +0000375bool TargetInfo::resolveSymbolicName(const char *&Name,
Chris Lattnerc16d4762009-04-26 17:57:12 +0000376 ConstraintInfo *OutputConstraints,
377 unsigned NumOutputs,
Chris Lattnerd9725f72009-04-26 07:16:29 +0000378 unsigned &Index) const {
Anders Carlssona79203b2009-01-18 01:56:57 +0000379 assert(*Name == '[' && "Symbolic name did not start with '['");
Anders Carlssona79203b2009-01-18 01:56:57 +0000380 Name++;
381 const char *Start = Name;
382 while (*Name && *Name != ']')
383 Name++;
Mike Stump11289f42009-09-09 15:08:12 +0000384
Anders Carlssona79203b2009-01-18 01:56:57 +0000385 if (!*Name) {
386 // Missing ']'
387 return false;
388 }
Mike Stump11289f42009-09-09 15:08:12 +0000389
Anders Carlssona79203b2009-01-18 01:56:57 +0000390 std::string SymbolicName(Start, Name - Start);
Mike Stump11289f42009-09-09 15:08:12 +0000391
Chris Lattnerc16d4762009-04-26 17:57:12 +0000392 for (Index = 0; Index != NumOutputs; ++Index)
393 if (SymbolicName == OutputConstraints[Index].getName())
Anders Carlssona79203b2009-01-18 01:56:57 +0000394 return true;
Anders Carlssona79203b2009-01-18 01:56:57 +0000395
396 return false;
397}
398
Chris Lattnerc16d4762009-04-26 17:57:12 +0000399bool TargetInfo::validateInputConstraint(ConstraintInfo *OutputConstraints,
400 unsigned NumOutputs,
Chris Lattnerd9725f72009-04-26 07:16:29 +0000401 ConstraintInfo &Info) const {
Chris Lattnerc3f4c7b2009-04-26 17:19:08 +0000402 const char *Name = Info.ConstraintStr.c_str();
403
Anders Carlssonf511f642007-11-27 04:11:28 +0000404 while (*Name) {
405 switch (*Name) {
406 default:
407 // Check if we have a matching constraint
408 if (*Name >= '0' && *Name <= '9') {
409 unsigned i = *Name - '0';
Mike Stump11289f42009-09-09 15:08:12 +0000410
Anders Carlssonf511f642007-11-27 04:11:28 +0000411 // Check if matching constraint is out of bounds.
412 if (i >= NumOutputs)
413 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000414
Anders Carlssonda1f5fc2010-11-03 02:22:29 +0000415 // A number must refer to an output only operand.
416 if (OutputConstraints[i].isReadWrite())
417 return false;
418
Anders Carlsson2d5f8b42010-11-03 02:54:51 +0000419 // If the constraint is already tied, it must be tied to the
420 // same operand referenced to by the number.
421 if (Info.hasTiedOperand() && Info.getTiedOperand() != i)
422 return false;
423
Mike Stump11289f42009-09-09 15:08:12 +0000424 // The constraint should have the same info as the respective
Anders Carlsson570c3572009-01-27 20:38:24 +0000425 // output constraint.
Chris Lattner4c92b512009-04-26 18:05:25 +0000426 Info.setTiedOperand(i, OutputConstraints[i]);
Chris Lattnerd9725f72009-04-26 07:16:29 +0000427 } else if (!validateAsmConstraint(Name, Info)) {
Daniel Dunbar81128e02008-08-25 09:46:27 +0000428 // FIXME: This error return is in place temporarily so we can
429 // add more constraints as we hit it. Eventually, an unknown
430 // constraint should just be treated as 'g'.
431 return false;
Anders Carlssona79203b2009-01-18 01:56:57 +0000432 }
433 break;
434 case '[': {
435 unsigned Index = 0;
Chris Lattnerc16d4762009-04-26 17:57:12 +0000436 if (!resolveSymbolicName(Name, OutputConstraints, NumOutputs, Index))
Anders Carlssona79203b2009-01-18 01:56:57 +0000437 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000438
Anders Carlsson2d5f8b42010-11-03 02:54:51 +0000439 // If the constraint is already tied, it must be tied to the
440 // same operand referenced to by the number.
441 if (Info.hasTiedOperand() && Info.getTiedOperand() != Index)
442 return false;
443
Eli Friedman854f1102010-08-11 23:03:37 +0000444 Info.setTiedOperand(Index, OutputConstraints[Index]);
Anders Carlssona79203b2009-01-18 01:56:57 +0000445 break;
Mike Stump11289f42009-09-09 15:08:12 +0000446 }
Anders Carlsson050f4942007-12-08 19:32:57 +0000447 case '%': // commutative
448 // FIXME: Fail if % is used with the last operand.
449 break;
Anders Carlssonf511f642007-11-27 04:11:28 +0000450 case 'i': // immediate integer.
Anders Carlssona3a96af2008-03-09 06:02:02 +0000451 case 'n': // immediate integer with a known value.
Anders Carlssonf511f642007-11-27 04:11:28 +0000452 break;
Chris Lattnerdbcc5ca2009-05-06 04:33:31 +0000453 case 'I': // Various constant constraints with target-specific meanings.
454 case 'J':
455 case 'K':
456 case 'L':
457 case 'M':
458 case 'N':
459 case 'O':
460 case 'P':
461 break;
Anders Carlssonf511f642007-11-27 04:11:28 +0000462 case 'r': // general register.
Chris Lattnerd9725f72009-04-26 07:16:29 +0000463 Info.setAllowsRegister();
Anders Carlssonf511f642007-11-27 04:11:28 +0000464 break;
465 case 'm': // memory operand.
Dale Johannesen8499f472010-09-07 18:40:41 +0000466 case 'o': // offsettable memory operand.
467 case 'V': // non-offsettable memory operand.
468 case '<': // autodecrement memory operand.
469 case '>': // autoincrement memory operand.
Chris Lattnerd9725f72009-04-26 07:16:29 +0000470 Info.setAllowsMemory();
Anders Carlssonf511f642007-11-27 04:11:28 +0000471 break;
472 case 'g': // general register, memory operand or immediate integer.
Anders Carlssone70cde12009-01-18 02:12:04 +0000473 case 'X': // any operand.
Chris Lattnerd9725f72009-04-26 07:16:29 +0000474 Info.setAllowsRegister();
475 Info.setAllowsMemory();
Anders Carlssonf511f642007-11-27 04:11:28 +0000476 break;
John Thompsonc467aa22010-09-21 22:04:54 +0000477 case 'E': // immediate floating point.
478 case 'F': // immediate floating point.
479 case 'p': // address operand.
480 break;
John Thompsona5c7d702010-08-10 19:20:14 +0000481 case ',': // multiple alternative constraint. Ignore comma.
482 break;
483 case '?': // Disparage slightly code.
Chris Lattner57540c52011-04-15 05:22:18 +0000484 case '!': // Disparage severely.
John Thompsona5c7d702010-08-10 19:20:14 +0000485 break; // Pass them.
Anders Carlssonf511f642007-11-27 04:11:28 +0000486 }
Mike Stump11289f42009-09-09 15:08:12 +0000487
Anders Carlssonf511f642007-11-27 04:11:28 +0000488 Name++;
489 }
Mike Stump11289f42009-09-09 15:08:12 +0000490
Anders Carlssonf511f642007-11-27 04:11:28 +0000491 return true;
492}