blob: 83d4e2bf63c97cbe98dfc9c8152b0c273fe220cd [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.
Douglas Gregorbc10b9f2012-10-15 16:45:32 +000027TargetInfo::TargetInfo(const std::string &T) : TargetOpts(), Triple(T)
28{
Daniel Dunbar3d9289c2010-04-15 06:18:39 +000029 // Set defaults. Defaults are set for a 32-bit RISC platform, like PPC or
30 // SPARC. These should be overridden by concrete targets as needed.
Eli Friedman803acb32011-12-22 03:51:45 +000031 BigEndian = true;
Eli Friedmand88c8a12009-04-19 21:38:35 +000032 TLSSupported = true;
Chris Lattner1a8f3942010-04-23 16:29:58 +000033 NoAsmVariants = false;
Chris Lattner5e2ef0c2008-05-09 06:08:39 +000034 PointerWidth = PointerAlign = 32;
Roman Divacky965b0b72011-01-06 08:27:10 +000035 BoolWidth = BoolAlign = 8;
Chris Lattnerb781dc792008-05-08 05:58:21 +000036 IntWidth = IntAlign = 32;
Chris Lattner5a9aaaf2008-05-09 05:50:02 +000037 LongWidth = LongAlign = 32;
38 LongLongWidth = LongLongAlign = 64;
Nick Lewyckyf59e1292011-12-16 22:34:14 +000039 SuitableAlign = 64;
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +000040 HalfWidth = 16;
41 HalfAlign = 16;
Eli Friedmanb5366062008-05-20 14:21:01 +000042 FloatWidth = 32;
43 FloatAlign = 32;
Nate Begeman65bea232008-04-18 17:17:24 +000044 DoubleWidth = 64;
Eli Friedmanb5366062008-05-20 14:21:01 +000045 DoubleAlign = 64;
46 LongDoubleWidth = 64;
47 LongDoubleAlign = 64;
Rafael Espindolae971b9a2010-06-04 23:15:27 +000048 LargeArrayMinWidth = 0;
49 LargeArrayAlign = 0;
Eli Friedman4b72fdd2011-10-14 20:59:01 +000050 MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 0;
Chad Rosiercc40ea72012-07-13 23:57:43 +000051 MaxVectorAlign = 0;
Anders Carlsson2a79a902008-10-31 16:05:19 +000052 SizeType = UnsignedLong;
Eli Friedmand50881c2008-11-02 02:43:55 +000053 PtrDiffType = SignedLong;
Sanjiv Guptad7959242008-10-31 09:52:39 +000054 IntMaxType = SignedLongLong;
55 UIntMaxType = UnsignedLongLong;
Chris Lattner7e4c81c2009-02-13 22:28:55 +000056 IntPtrType = SignedLong;
Eli Friedmand50881c2008-11-02 02:43:55 +000057 WCharType = SignedInt;
Chris Lattner67204922009-10-21 04:59:34 +000058 WIntType = SignedInt;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +000059 Char16Type = UnsignedShort;
60 Char32Type = UnsignedInt;
Eli Friedman2857ccb2009-07-01 03:36:11 +000061 Int64Type = SignedLongLong;
Edward O'Callaghan847f2a12009-11-21 00:49:54 +000062 SigAtomicType = SignedInt;
Eli Friedman4e91899e2012-11-27 02:58:24 +000063 ProcessIDType = SignedInt;
Fariborz Jahanian29898f42012-04-16 21:03:30 +000064 UseSignedCharForObjCBool = true;
Daniel Dunbar1da65112010-04-15 15:06:18 +000065 UseBitFieldTypeAlignment = true;
Chad Rosier18903ee2011-08-04 01:21:14 +000066 UseZeroLengthBitfieldAlignment = false;
67 ZeroLengthBitfieldBoundary = 0;
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +000068 HalfFormat = &llvm::APFloat::IEEEhalf;
Chris Lattner1df27862008-03-08 08:59:43 +000069 FloatFormat = &llvm::APFloat::IEEEsingle;
70 DoubleFormat = &llvm::APFloat::IEEEdouble;
71 LongDoubleFormat = &llvm::APFloat::IEEEdouble;
Eli Friedman873f65a2008-08-21 00:13:15 +000072 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 +000073 "i64:64:64-f32:32:32-f64:64:64-n32";
Chris Lattnerf37bafc2008-10-05 19:22:37 +000074 UserLabelPrefix = "_";
Roman Divacky178e01602011-02-10 16:52:03 +000075 MCountName = "mcount";
Abramo Bagnara41bfb662011-09-08 14:20:25 +000076 RegParmMax = 0;
77 SSERegParmMax = 0;
Daniel Dunbarbd606522010-05-27 00:35:16 +000078 HasAlignMac68kSupport = false;
Daniel Dunbar6f2e8392010-07-14 23:39:36 +000079
80 // Default to no types using fpret.
81 RealTypeUsesObjCFPRet = 0;
John McCall86353412010-08-21 22:46:04 +000082
Anders Carlsson2f1a6c32011-10-31 16:27:11 +000083 // Default to not using fp2ret for __Complex long double
84 ComplexLongDoubleUsesFP2Ret = false;
85
John McCall86353412010-08-21 22:46:04 +000086 // Default to using the Itanium ABI.
87 CXXABI = CXXABI_Itanium;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +000088
89 // Default to an empty address space map.
90 AddrSpaceMap = &DefaultAddrSpaceMap;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +000091
92 // Default to an unknown platform name.
93 PlatformName = "unknown";
94 PlatformMinVersion = VersionTuple();
Chris Lattner1df27862008-03-08 08:59:43 +000095}
96
Chris Lattnerc3a669b2008-03-08 08:24:01 +000097// Out of line virtual dtor for TargetInfo.
98TargetInfo::~TargetInfo() {}
Chris Lattner2194ddc2006-10-14 18:32:26 +000099
Chris Lattnera91c30f2009-02-06 05:04:11 +0000100/// getTypeName - Return the user string for the specified integer type enum.
101/// For example, SignedShort -> "short".
102const char *TargetInfo::getTypeName(IntType T) {
103 switch (T) {
David Blaikie83d382b2011-09-23 05:06:16 +0000104 default: llvm_unreachable("not an integer!");
Chris Lattnera91c30f2009-02-06 05:04:11 +0000105 case SignedShort: return "short";
106 case UnsignedShort: return "unsigned short";
107 case SignedInt: return "int";
108 case UnsignedInt: return "unsigned int";
109 case SignedLong: return "long int";
110 case UnsignedLong: return "long unsigned int";
111 case SignedLongLong: return "long long int";
112 case UnsignedLongLong: return "long long unsigned int";
113 }
114}
115
Chris Lattner72cdcac2009-10-21 06:24:21 +0000116/// getTypeConstantSuffix - Return the constant suffix for the specified
117/// integer type enum. For example, SignedLong -> "L".
118const char *TargetInfo::getTypeConstantSuffix(IntType T) {
119 switch (T) {
David Blaikie83d382b2011-09-23 05:06:16 +0000120 default: llvm_unreachable("not an integer!");
Chris Lattner72cdcac2009-10-21 06:24:21 +0000121 case SignedShort:
122 case SignedInt: return "";
123 case SignedLong: return "L";
124 case SignedLongLong: return "LL";
125 case UnsignedShort:
126 case UnsignedInt: return "U";
127 case UnsignedLong: return "UL";
128 case UnsignedLongLong: return "ULL";
129 }
130}
131
Michael J. Spencerfeb799c2010-10-18 07:10:59 +0000132/// getTypeWidth - Return the width (in bits) of the specified integer type
Chris Lattner72cdcac2009-10-21 06:24:21 +0000133/// enum. For example, SignedInt -> getIntWidth().
134unsigned TargetInfo::getTypeWidth(IntType T) const {
135 switch (T) {
David Blaikie83d382b2011-09-23 05:06:16 +0000136 default: llvm_unreachable("not an integer!");
Chris Lattnere4a8c642009-11-05 21:21:32 +0000137 case SignedShort:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000138 case UnsignedShort: return getShortWidth();
Chris Lattnere4a8c642009-11-05 21:21:32 +0000139 case SignedInt:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000140 case UnsignedInt: return getIntWidth();
Chris Lattnere4a8c642009-11-05 21:21:32 +0000141 case SignedLong:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000142 case UnsignedLong: return getLongWidth();
Chris Lattnere4a8c642009-11-05 21:21:32 +0000143 case SignedLongLong:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000144 case UnsignedLongLong: return getLongLongWidth();
145 };
146}
147
Michael J. Spencerfeb799c2010-10-18 07:10:59 +0000148/// getTypeAlign - Return the alignment (in bits) of the specified integer type
Chris Lattnere4a8c642009-11-05 21:21:32 +0000149/// enum. For example, SignedInt -> getIntAlign().
150unsigned TargetInfo::getTypeAlign(IntType T) const {
151 switch (T) {
David Blaikie83d382b2011-09-23 05:06:16 +0000152 default: llvm_unreachable("not an integer!");
Chris Lattnere4a8c642009-11-05 21:21:32 +0000153 case SignedShort:
154 case UnsignedShort: return getShortAlign();
155 case SignedInt:
156 case UnsignedInt: return getIntAlign();
157 case SignedLong:
158 case UnsignedLong: return getLongAlign();
159 case SignedLongLong:
160 case UnsignedLongLong: return getLongLongAlign();
161 };
162}
163
Chris Lattnerc0c04392009-10-25 22:49:18 +0000164/// isTypeSigned - Return whether an integer types is signed. Returns true if
Chris Lattner72cdcac2009-10-21 06:24:21 +0000165/// the type is signed; false otherwise.
Chris Lattnerad3467e2010-12-25 23:25:43 +0000166bool TargetInfo::isTypeSigned(IntType T) {
Chris Lattner72cdcac2009-10-21 06:24:21 +0000167 switch (T) {
David Blaikie83d382b2011-09-23 05:06:16 +0000168 default: llvm_unreachable("not an integer!");
Chris Lattner72cdcac2009-10-21 06:24:21 +0000169 case SignedShort:
170 case SignedInt:
171 case SignedLong:
Michael J. Spencerfeb799c2010-10-18 07:10:59 +0000172 case SignedLongLong:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000173 return true;
174 case UnsignedShort:
175 case UnsignedInt:
176 case UnsignedLong:
Michael J. Spencerfeb799c2010-10-18 07:10:59 +0000177 case UnsignedLongLong:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000178 return false;
179 };
180}
181
John Thompsoned4e2952009-11-05 20:14:16 +0000182/// setForcedLangOptions - Set forced language options.
183/// Apply changes to the target information with respect to certain
184/// language options which change the target configuration.
185void TargetInfo::setForcedLangOptions(LangOptions &Opts) {
Daniel Dunbar9302f602010-04-15 15:06:22 +0000186 if (Opts.NoBitFieldTypeAlign)
187 UseBitFieldTypeAlignment = false;
188 if (Opts.ShortWChar)
John Thompsoned4e2952009-11-05 20:14:16 +0000189 WCharType = UnsignedShort;
John Thompsoned4e2952009-11-05 20:14:16 +0000190}
Chris Lattner72cdcac2009-10-21 06:24:21 +0000191
Chris Lattnerec0a6d92007-09-22 18:29:59 +0000192//===----------------------------------------------------------------------===//
Chris Lattnerec0a6d92007-09-22 18:29:59 +0000193
Chris Lattner10a5b382007-01-29 05:24:35 +0000194
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000195static StringRef removeGCCRegisterPrefix(StringRef Name) {
Anders Carlsson3ed4aea2008-02-06 00:11:32 +0000196 if (Name[0] == '%' || Name[0] == '#')
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000197 Name = Name.substr(1);
Michael J. Spencerfeb799c2010-10-18 07:10:59 +0000198
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000199 return Name;
Anders Carlsson3ed4aea2008-02-06 00:11:32 +0000200}
201
Eric Christopherfd9a5f42011-06-28 18:20:53 +0000202/// isValidClobber - Returns whether the passed in string is
203/// a valid clobber in an inline asm statement. This is used by
204/// Sema.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000205bool TargetInfo::isValidClobber(StringRef Name) const {
Eric Christopherfd9a5f42011-06-28 18:20:53 +0000206 return (isValidGCCRegisterName(Name) ||
207 Name == "memory" || Name == "cc");
208}
209
Anders Carlsson5fa3f342007-11-24 23:38:12 +0000210/// isValidGCCRegisterName - Returns whether the passed in string
211/// is a valid register name according to GCC. This is used by Sema for
212/// inline asm statements.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000213bool TargetInfo::isValidGCCRegisterName(StringRef Name) const {
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000214 if (Name.empty())
215 return false;
Michael J. Spencerfeb799c2010-10-18 07:10:59 +0000216
Anders Carlsson290aa852007-11-25 00:25:21 +0000217 const char * const *Names;
218 unsigned NumNames;
Mike Stump11289f42009-09-09 15:08:12 +0000219
Anders Carlsson290aa852007-11-25 00:25:21 +0000220 // Get rid of any register prefix.
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000221 Name = removeGCCRegisterPrefix(Name);
Anders Carlsson3ed4aea2008-02-06 00:11:32 +0000222
Chris Lattnerc3a669b2008-03-08 08:24:01 +0000223 getGCCRegNames(Names, NumNames);
Mike Stump11289f42009-09-09 15:08:12 +0000224
Anders Carlsson290aa852007-11-25 00:25:21 +0000225 // If we have a number it maps to an entry in the register name array.
226 if (isdigit(Name[0])) {
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000227 int n;
228 if (!Name.getAsInteger(0, n))
Anders Carlsson290aa852007-11-25 00:25:21 +0000229 return n >= 0 && (unsigned)n < NumNames;
230 }
231
232 // Check register names.
233 for (unsigned i = 0; i < NumNames; i++) {
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000234 if (Name == Names[i])
Anders Carlsson290aa852007-11-25 00:25:21 +0000235 return true;
236 }
Mike Stump11289f42009-09-09 15:08:12 +0000237
Eric Christophercdd36352011-06-21 00:05:20 +0000238 // Check any additional names that we have.
239 const AddlRegName *AddlNames;
240 unsigned NumAddlNames;
241 getGCCAddlRegNames(AddlNames, NumAddlNames);
242 for (unsigned i = 0; i < NumAddlNames; i++)
243 for (unsigned j = 0; j < llvm::array_lengthof(AddlNames[i].Names); j++) {
244 if (!AddlNames[i].Names[j])
245 break;
246 // Make sure the register that the additional name is for is within
247 // the bounds of the register names from above.
248 if (AddlNames[i].Names[j] == Name && AddlNames[i].RegNum < NumNames)
249 return true;
250 }
251
Anders Carlsson290aa852007-11-25 00:25:21 +0000252 // Now check aliases.
Chris Lattnerc3a669b2008-03-08 08:24:01 +0000253 const GCCRegAlias *Aliases;
Anders Carlsson290aa852007-11-25 00:25:21 +0000254 unsigned NumAliases;
Mike Stump11289f42009-09-09 15:08:12 +0000255
Chris Lattnerc3a669b2008-03-08 08:24:01 +0000256 getGCCRegAliases(Aliases, NumAliases);
Anders Carlsson290aa852007-11-25 00:25:21 +0000257 for (unsigned i = 0; i < NumAliases; i++) {
258 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
259 if (!Aliases[i].Aliases[j])
260 break;
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000261 if (Aliases[i].Aliases[j] == Name)
Anders Carlsson290aa852007-11-25 00:25:21 +0000262 return true;
263 }
264 }
Mike Stump11289f42009-09-09 15:08:12 +0000265
Anders Carlsson5fa3f342007-11-24 23:38:12 +0000266 return false;
267}
Anders Carlssonf511f642007-11-27 04:11:28 +0000268
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000269StringRef
270TargetInfo::getNormalizedGCCRegisterName(StringRef Name) const {
Anders Carlssonf511f642007-11-27 04:11:28 +0000271 assert(isValidGCCRegisterName(Name) && "Invalid register passed in");
Mike Stump11289f42009-09-09 15:08:12 +0000272
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000273 // Get rid of any register prefix.
274 Name = removeGCCRegisterPrefix(Name);
Mike Stump11289f42009-09-09 15:08:12 +0000275
Anders Carlssonf511f642007-11-27 04:11:28 +0000276 const char * const *Names;
277 unsigned NumNames;
278
Chris Lattnerc3a669b2008-03-08 08:24:01 +0000279 getGCCRegNames(Names, NumNames);
Anders Carlssonf511f642007-11-27 04:11:28 +0000280
281 // First, check if we have a number.
282 if (isdigit(Name[0])) {
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000283 int n;
284 if (!Name.getAsInteger(0, n)) {
Mike Stump11289f42009-09-09 15:08:12 +0000285 assert(n >= 0 && (unsigned)n < NumNames &&
Anders Carlssonf511f642007-11-27 04:11:28 +0000286 "Out of bounds register number!");
287 return Names[n];
288 }
289 }
Mike Stump11289f42009-09-09 15:08:12 +0000290
Eric Christophercdd36352011-06-21 00:05:20 +0000291 // Check any additional names that we have.
292 const AddlRegName *AddlNames;
293 unsigned NumAddlNames;
294 getGCCAddlRegNames(AddlNames, NumAddlNames);
295 for (unsigned i = 0; i < NumAddlNames; i++)
296 for (unsigned j = 0; j < llvm::array_lengthof(AddlNames[i].Names); j++) {
297 if (!AddlNames[i].Names[j])
298 break;
299 // Make sure the register that the additional name is for is within
300 // the bounds of the register names from above.
301 if (AddlNames[i].Names[j] == Name && AddlNames[i].RegNum < NumNames)
302 return Name;
303 }
304
Anders Carlssonf511f642007-11-27 04:11:28 +0000305 // Now check aliases.
Chris Lattnerc3a669b2008-03-08 08:24:01 +0000306 const GCCRegAlias *Aliases;
Anders Carlssonf511f642007-11-27 04:11:28 +0000307 unsigned NumAliases;
Mike Stump11289f42009-09-09 15:08:12 +0000308
Chris Lattnerc3a669b2008-03-08 08:24:01 +0000309 getGCCRegAliases(Aliases, NumAliases);
Anders Carlssonf511f642007-11-27 04:11:28 +0000310 for (unsigned i = 0; i < NumAliases; i++) {
311 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
312 if (!Aliases[i].Aliases[j])
313 break;
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000314 if (Aliases[i].Aliases[j] == Name)
Anders Carlssonf511f642007-11-27 04:11:28 +0000315 return Aliases[i].Register;
316 }
317 }
Mike Stump11289f42009-09-09 15:08:12 +0000318
Anders Carlssonf511f642007-11-27 04:11:28 +0000319 return Name;
320}
321
Chris Lattnerc3f4c7b2009-04-26 17:19:08 +0000322bool TargetInfo::validateOutputConstraint(ConstraintInfo &Info) const {
323 const char *Name = Info.getConstraintStr().c_str();
Anders Carlssonf511f642007-11-27 04:11:28 +0000324 // An output constraint must start with '=' or '+'
325 if (*Name != '=' && *Name != '+')
326 return false;
327
328 if (*Name == '+')
Chris Lattnerd9725f72009-04-26 07:16:29 +0000329 Info.setIsReadWrite();
Anders Carlssonf511f642007-11-27 04:11:28 +0000330
331 Name++;
332 while (*Name) {
333 switch (*Name) {
334 default:
Chris Lattnerd9725f72009-04-26 07:16:29 +0000335 if (!validateAsmConstraint(Name, Info)) {
Ted Kremenekb44485b2008-04-24 16:36:38 +0000336 // FIXME: We temporarily return false
Anders Carlssonf511f642007-11-27 04:11:28 +0000337 // so we can add more constraints as we hit it.
338 // Eventually, an unknown constraint should just be treated as 'g'.
Ted Kremenekb44485b2008-04-24 16:36:38 +0000339 return false;
Anders Carlssonf511f642007-11-27 04:11:28 +0000340 }
341 case '&': // early clobber.
342 break;
Chris Lattnerf3154712009-10-13 04:32:07 +0000343 case '%': // commutative.
344 // FIXME: Check that there is a another register after this one.
345 break;
Anders Carlssonf511f642007-11-27 04:11:28 +0000346 case 'r': // general register.
Chris Lattnerd9725f72009-04-26 07:16:29 +0000347 Info.setAllowsRegister();
Anders Carlssonf511f642007-11-27 04:11:28 +0000348 break;
349 case 'm': // memory operand.
Dale Johannesen8499f472010-09-07 18:40:41 +0000350 case 'o': // offsetable memory operand.
351 case 'V': // non-offsetable memory operand.
352 case '<': // autodecrement memory operand.
353 case '>': // autoincrement memory operand.
Chris Lattnerd9725f72009-04-26 07:16:29 +0000354 Info.setAllowsMemory();
Anders Carlssonf511f642007-11-27 04:11:28 +0000355 break;
356 case 'g': // general register, memory operand or immediate integer.
Anders Carlssone70cde12009-01-18 02:12:04 +0000357 case 'X': // any operand.
Chris Lattnerd9725f72009-04-26 07:16:29 +0000358 Info.setAllowsRegister();
359 Info.setAllowsMemory();
Anders Carlssonf511f642007-11-27 04:11:28 +0000360 break;
John Thompsona5c7d702010-08-10 19:20:14 +0000361 case ',': // multiple alternative constraint. Pass it.
John Thompson46667af2010-08-11 00:58:20 +0000362 // Handle additional optional '=' or '+' modifiers.
John Thompson12240612010-09-18 01:15:13 +0000363 if (Name[1] == '=' || Name[1] == '+')
John Thompson46667af2010-08-11 00:58:20 +0000364 Name++;
John Thompsona5c7d702010-08-10 19:20:14 +0000365 break;
366 case '?': // Disparage slightly code.
Dale Johannesen8499f472010-09-07 18:40:41 +0000367 case '!': // Disparage severely.
Ulrich Weigand7bcc7ec2012-10-29 12:20:54 +0000368 case '#': // Ignore as constraint.
369 case '*': // Ignore for choosing register preferences.
John Thompsona5c7d702010-08-10 19:20:14 +0000370 break; // Pass them.
Anders Carlssonf511f642007-11-27 04:11:28 +0000371 }
Mike Stump11289f42009-09-09 15:08:12 +0000372
Anders Carlssonf511f642007-11-27 04:11:28 +0000373 Name++;
374 }
Mike Stump11289f42009-09-09 15:08:12 +0000375
Anders Carlssonf511f642007-11-27 04:11:28 +0000376 return true;
377}
378
Anders Carlssona79203b2009-01-18 01:56:57 +0000379bool TargetInfo::resolveSymbolicName(const char *&Name,
Chris Lattnerc16d4762009-04-26 17:57:12 +0000380 ConstraintInfo *OutputConstraints,
381 unsigned NumOutputs,
Chris Lattnerd9725f72009-04-26 07:16:29 +0000382 unsigned &Index) const {
Anders Carlssona79203b2009-01-18 01:56:57 +0000383 assert(*Name == '[' && "Symbolic name did not start with '['");
Anders Carlssona79203b2009-01-18 01:56:57 +0000384 Name++;
385 const char *Start = Name;
386 while (*Name && *Name != ']')
387 Name++;
Mike Stump11289f42009-09-09 15:08:12 +0000388
Anders Carlssona79203b2009-01-18 01:56:57 +0000389 if (!*Name) {
390 // Missing ']'
391 return false;
392 }
Mike Stump11289f42009-09-09 15:08:12 +0000393
Anders Carlssona79203b2009-01-18 01:56:57 +0000394 std::string SymbolicName(Start, Name - Start);
Mike Stump11289f42009-09-09 15:08:12 +0000395
Chris Lattnerc16d4762009-04-26 17:57:12 +0000396 for (Index = 0; Index != NumOutputs; ++Index)
397 if (SymbolicName == OutputConstraints[Index].getName())
Anders Carlssona79203b2009-01-18 01:56:57 +0000398 return true;
Anders Carlssona79203b2009-01-18 01:56:57 +0000399
400 return false;
401}
402
Chris Lattnerc16d4762009-04-26 17:57:12 +0000403bool TargetInfo::validateInputConstraint(ConstraintInfo *OutputConstraints,
404 unsigned NumOutputs,
Chris Lattnerd9725f72009-04-26 07:16:29 +0000405 ConstraintInfo &Info) const {
Chris Lattnerc3f4c7b2009-04-26 17:19:08 +0000406 const char *Name = Info.ConstraintStr.c_str();
407
Anders Carlssonf511f642007-11-27 04:11:28 +0000408 while (*Name) {
409 switch (*Name) {
410 default:
411 // Check if we have a matching constraint
412 if (*Name >= '0' && *Name <= '9') {
413 unsigned i = *Name - '0';
Mike Stump11289f42009-09-09 15:08:12 +0000414
Anders Carlssonf511f642007-11-27 04:11:28 +0000415 // Check if matching constraint is out of bounds.
416 if (i >= NumOutputs)
417 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000418
Anders Carlssonda1f5fc2010-11-03 02:22:29 +0000419 // A number must refer to an output only operand.
420 if (OutputConstraints[i].isReadWrite())
421 return false;
422
Anders Carlsson2d5f8b42010-11-03 02:54:51 +0000423 // If the constraint is already tied, it must be tied to the
424 // same operand referenced to by the number.
425 if (Info.hasTiedOperand() && Info.getTiedOperand() != i)
426 return false;
427
Mike Stump11289f42009-09-09 15:08:12 +0000428 // The constraint should have the same info as the respective
Anders Carlsson570c3572009-01-27 20:38:24 +0000429 // output constraint.
Chris Lattner4c92b512009-04-26 18:05:25 +0000430 Info.setTiedOperand(i, OutputConstraints[i]);
Chris Lattnerd9725f72009-04-26 07:16:29 +0000431 } else if (!validateAsmConstraint(Name, Info)) {
Daniel Dunbar81128e02008-08-25 09:46:27 +0000432 // FIXME: This error return is in place temporarily so we can
433 // add more constraints as we hit it. Eventually, an unknown
434 // constraint should just be treated as 'g'.
435 return false;
Anders Carlssona79203b2009-01-18 01:56:57 +0000436 }
437 break;
438 case '[': {
439 unsigned Index = 0;
Chris Lattnerc16d4762009-04-26 17:57:12 +0000440 if (!resolveSymbolicName(Name, OutputConstraints, NumOutputs, Index))
Anders Carlssona79203b2009-01-18 01:56:57 +0000441 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000442
Anders Carlsson2d5f8b42010-11-03 02:54:51 +0000443 // If the constraint is already tied, it must be tied to the
444 // same operand referenced to by the number.
445 if (Info.hasTiedOperand() && Info.getTiedOperand() != Index)
446 return false;
447
Eli Friedman854f1102010-08-11 23:03:37 +0000448 Info.setTiedOperand(Index, OutputConstraints[Index]);
Anders Carlssona79203b2009-01-18 01:56:57 +0000449 break;
Mike Stump11289f42009-09-09 15:08:12 +0000450 }
Anders Carlsson050f4942007-12-08 19:32:57 +0000451 case '%': // commutative
452 // FIXME: Fail if % is used with the last operand.
453 break;
Anders Carlssonf511f642007-11-27 04:11:28 +0000454 case 'i': // immediate integer.
Anders Carlssona3a96af2008-03-09 06:02:02 +0000455 case 'n': // immediate integer with a known value.
Anders Carlssonf511f642007-11-27 04:11:28 +0000456 break;
Chris Lattnerdbcc5ca2009-05-06 04:33:31 +0000457 case 'I': // Various constant constraints with target-specific meanings.
458 case 'J':
459 case 'K':
460 case 'L':
461 case 'M':
462 case 'N':
463 case 'O':
464 case 'P':
465 break;
Anders Carlssonf511f642007-11-27 04:11:28 +0000466 case 'r': // general register.
Chris Lattnerd9725f72009-04-26 07:16:29 +0000467 Info.setAllowsRegister();
Anders Carlssonf511f642007-11-27 04:11:28 +0000468 break;
469 case 'm': // memory operand.
Dale Johannesen8499f472010-09-07 18:40:41 +0000470 case 'o': // offsettable memory operand.
471 case 'V': // non-offsettable memory operand.
472 case '<': // autodecrement memory operand.
473 case '>': // autoincrement memory operand.
Chris Lattnerd9725f72009-04-26 07:16:29 +0000474 Info.setAllowsMemory();
Anders Carlssonf511f642007-11-27 04:11:28 +0000475 break;
476 case 'g': // general register, memory operand or immediate integer.
Anders Carlssone70cde12009-01-18 02:12:04 +0000477 case 'X': // any operand.
Chris Lattnerd9725f72009-04-26 07:16:29 +0000478 Info.setAllowsRegister();
479 Info.setAllowsMemory();
Anders Carlssonf511f642007-11-27 04:11:28 +0000480 break;
John Thompsonc467aa22010-09-21 22:04:54 +0000481 case 'E': // immediate floating point.
482 case 'F': // immediate floating point.
483 case 'p': // address operand.
484 break;
John Thompsona5c7d702010-08-10 19:20:14 +0000485 case ',': // multiple alternative constraint. Ignore comma.
486 break;
487 case '?': // Disparage slightly code.
Chris Lattner57540c52011-04-15 05:22:18 +0000488 case '!': // Disparage severely.
Ulrich Weigand7bcc7ec2012-10-29 12:20:54 +0000489 case '#': // Ignore as constraint.
490 case '*': // Ignore for choosing register preferences.
John Thompsona5c7d702010-08-10 19:20:14 +0000491 break; // Pass them.
Anders Carlssonf511f642007-11-27 04:11:28 +0000492 }
Mike Stump11289f42009-09-09 15:08:12 +0000493
Anders Carlssonf511f642007-11-27 04:11:28 +0000494 Name++;
495 }
Mike Stump11289f42009-09-09 15:08:12 +0000496
Anders Carlssonf511f642007-11-27 04:11:28 +0000497 return true;
498}