blob: f938b5a6045dbb971e306f01bc4daac689385957 [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
Peter Collingbourne207f4d82011-03-18 22:38:29 +000014#include "clang/Basic/AddressSpaces.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000015#include "clang/Basic/TargetInfo.h"
John Thompsona6fda122009-11-05 20:14:16 +000016#include "clang/Basic/LangOptions.h"
Chris Lattner525a0502007-09-22 18:29:59 +000017#include "llvm/ADT/APFloat.h"
Anders Carlsson6fa90862007-11-25 00:25:21 +000018#include "llvm/ADT/STLExtras.h"
David Blaikie9fe8c742011-09-23 05:35:21 +000019#include "llvm/Support/ErrorHandling.h"
Nick Lewycky403ba352010-12-19 20:49:25 +000020#include <cctype>
Chris Lattner5483bdf2008-04-06 04:02:29 +000021#include <cstdlib>
Reid Spencer5f016e22007-07-11 17:01:13 +000022using namespace clang;
23
Peter Collingbourne207f4d82011-03-18 22:38:29 +000024static const LangAS::Map DefaultAddrSpaceMap = { 0 };
25
Chris Lattnercd4fc422008-03-08 08:59:43 +000026// TargetInfo Constructor.
27TargetInfo::TargetInfo(const std::string &T) : Triple(T) {
Daniel Dunbarb6a16932010-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 Friedmane6a24e82011-12-22 03:51:45 +000030 BigEndian = true;
Eli Friedmanb030f022009-04-19 21:38:35 +000031 TLSSupported = true;
Chris Lattner9bffb072010-04-23 16:29:58 +000032 NoAsmVariants = false;
Chris Lattner927686f2008-05-09 06:08:39 +000033 PointerWidth = PointerAlign = 32;
Roman Divackyc81f2a22011-01-06 08:27:10 +000034 BoolWidth = BoolAlign = 8;
Chris Lattner2621fd12008-05-08 05:58:21 +000035 IntWidth = IntAlign = 32;
Chris Lattnerec10f582008-05-09 05:50:02 +000036 LongWidth = LongAlign = 32;
37 LongLongWidth = LongLongAlign = 64;
Nick Lewycky7ec59c72011-12-16 22:34:14 +000038 SuitableAlign = 64;
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +000039 HalfWidth = 16;
40 HalfAlign = 16;
Eli Friedman61538a72008-05-20 14:21:01 +000041 FloatWidth = 32;
42 FloatAlign = 32;
Nate Begeman3d292062008-04-18 17:17:24 +000043 DoubleWidth = 64;
Eli Friedman61538a72008-05-20 14:21:01 +000044 DoubleAlign = 64;
45 LongDoubleWidth = 64;
46 LongDoubleAlign = 64;
Rafael Espindola6deecb02010-06-04 23:15:27 +000047 LargeArrayMinWidth = 0;
48 LargeArrayAlign = 0;
Eli Friedman2be46072011-10-14 20:59:01 +000049 MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 0;
Anders Carlsson6a3615c2008-10-31 16:05:19 +000050 SizeType = UnsignedLong;
Eli Friedmanf509d732008-11-02 02:43:55 +000051 PtrDiffType = SignedLong;
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +000052 IntMaxType = SignedLongLong;
53 UIntMaxType = UnsignedLongLong;
Chris Lattner6ad474f2009-02-13 22:28:55 +000054 IntPtrType = SignedLong;
Eli Friedmanf509d732008-11-02 02:43:55 +000055 WCharType = SignedInt;
Chris Lattnere64ef802009-10-21 04:59:34 +000056 WIntType = SignedInt;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +000057 Char16Type = UnsignedShort;
58 Char32Type = UnsignedInt;
Eli Friedman3c7b6e42009-07-01 03:36:11 +000059 Int64Type = SignedLongLong;
Edward O'Callaghan9cf910e2009-11-21 00:49:54 +000060 SigAtomicType = SignedInt;
Daniel Dunbarb6830d62010-04-15 15:06:18 +000061 UseBitFieldTypeAlignment = true;
Chad Rosier61a62212011-08-04 01:21:14 +000062 UseZeroLengthBitfieldAlignment = false;
63 ZeroLengthBitfieldBoundary = 0;
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +000064 HalfFormat = &llvm::APFloat::IEEEhalf;
Chris Lattnercd4fc422008-03-08 08:59:43 +000065 FloatFormat = &llvm::APFloat::IEEEsingle;
66 DoubleFormat = &llvm::APFloat::IEEEdouble;
67 LongDoubleFormat = &llvm::APFloat::IEEEdouble;
Eli Friedmaned855cb2008-08-21 00:13:15 +000068 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 +000069 "i64:64:64-f32:32:32-f64:64:64-n32";
Chris Lattner3fdf4672008-10-05 19:22:37 +000070 UserLabelPrefix = "_";
Roman Divackybe4c8702011-02-10 16:52:03 +000071 MCountName = "mcount";
Abramo Bagnara62f940b2011-09-08 14:20:25 +000072 RegParmMax = 0;
73 SSERegParmMax = 0;
Daniel Dunbar613fd672010-05-27 00:35:16 +000074 HasAlignMac68kSupport = false;
Daniel Dunbardacf9dd2010-07-14 23:39:36 +000075
76 // Default to no types using fpret.
77 RealTypeUsesObjCFPRet = 0;
John McCallee79a4c2010-08-21 22:46:04 +000078
Anders Carlssoneea64802011-10-31 16:27:11 +000079 // Default to not using fp2ret for __Complex long double
80 ComplexLongDoubleUsesFP2Ret = false;
81
John McCallee79a4c2010-08-21 22:46:04 +000082 // Default to using the Itanium ABI.
83 CXXABI = CXXABI_Itanium;
Peter Collingbourne207f4d82011-03-18 22:38:29 +000084
85 // Default to an empty address space map.
86 AddrSpaceMap = &DefaultAddrSpaceMap;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +000087
88 // Default to an unknown platform name.
89 PlatformName = "unknown";
90 PlatformMinVersion = VersionTuple();
Chris Lattnercd4fc422008-03-08 08:59:43 +000091}
92
Chris Lattner0eaed122008-03-08 08:24:01 +000093// Out of line virtual dtor for TargetInfo.
94TargetInfo::~TargetInfo() {}
Reid Spencer5f016e22007-07-11 17:01:13 +000095
Chris Lattner2b5abf52009-02-06 05:04:11 +000096/// getTypeName - Return the user string for the specified integer type enum.
97/// For example, SignedShort -> "short".
98const char *TargetInfo::getTypeName(IntType T) {
99 switch (T) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000100 default: llvm_unreachable("not an integer!");
Chris Lattner2b5abf52009-02-06 05:04:11 +0000101 case SignedShort: return "short";
102 case UnsignedShort: return "unsigned short";
103 case SignedInt: return "int";
104 case UnsignedInt: return "unsigned int";
105 case SignedLong: return "long int";
106 case UnsignedLong: return "long unsigned int";
107 case SignedLongLong: return "long long int";
108 case UnsignedLongLong: return "long long unsigned int";
109 }
110}
111
Chris Lattnerb304f772009-10-21 06:24:21 +0000112/// getTypeConstantSuffix - Return the constant suffix for the specified
113/// integer type enum. For example, SignedLong -> "L".
114const char *TargetInfo::getTypeConstantSuffix(IntType T) {
115 switch (T) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000116 default: llvm_unreachable("not an integer!");
Chris Lattnerb304f772009-10-21 06:24:21 +0000117 case SignedShort:
118 case SignedInt: return "";
119 case SignedLong: return "L";
120 case SignedLongLong: return "LL";
121 case UnsignedShort:
122 case UnsignedInt: return "U";
123 case UnsignedLong: return "UL";
124 case UnsignedLongLong: return "ULL";
125 }
126}
127
Michael J. Spencer237cf582010-10-18 07:10:59 +0000128/// getTypeWidth - Return the width (in bits) of the specified integer type
Chris Lattnerb304f772009-10-21 06:24:21 +0000129/// enum. For example, SignedInt -> getIntWidth().
130unsigned TargetInfo::getTypeWidth(IntType T) const {
131 switch (T) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000132 default: llvm_unreachable("not an integer!");
Chris Lattner9099e7b2009-11-05 21:21:32 +0000133 case SignedShort:
Chris Lattnerb304f772009-10-21 06:24:21 +0000134 case UnsignedShort: return getShortWidth();
Chris Lattner9099e7b2009-11-05 21:21:32 +0000135 case SignedInt:
Chris Lattnerb304f772009-10-21 06:24:21 +0000136 case UnsignedInt: return getIntWidth();
Chris Lattner9099e7b2009-11-05 21:21:32 +0000137 case SignedLong:
Chris Lattnerb304f772009-10-21 06:24:21 +0000138 case UnsignedLong: return getLongWidth();
Chris Lattner9099e7b2009-11-05 21:21:32 +0000139 case SignedLongLong:
Chris Lattnerb304f772009-10-21 06:24:21 +0000140 case UnsignedLongLong: return getLongLongWidth();
141 };
142}
143
Michael J. Spencer237cf582010-10-18 07:10:59 +0000144/// getTypeAlign - Return the alignment (in bits) of the specified integer type
Chris Lattner9099e7b2009-11-05 21:21:32 +0000145/// enum. For example, SignedInt -> getIntAlign().
146unsigned TargetInfo::getTypeAlign(IntType T) const {
147 switch (T) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000148 default: llvm_unreachable("not an integer!");
Chris Lattner9099e7b2009-11-05 21:21:32 +0000149 case SignedShort:
150 case UnsignedShort: return getShortAlign();
151 case SignedInt:
152 case UnsignedInt: return getIntAlign();
153 case SignedLong:
154 case UnsignedLong: return getLongAlign();
155 case SignedLongLong:
156 case UnsignedLongLong: return getLongLongAlign();
157 };
158}
159
Chris Lattner961f0702009-10-25 22:49:18 +0000160/// isTypeSigned - Return whether an integer types is signed. Returns true if
Chris Lattnerb304f772009-10-21 06:24:21 +0000161/// the type is signed; false otherwise.
Chris Lattner3f59c972010-12-25 23:25:43 +0000162bool TargetInfo::isTypeSigned(IntType T) {
Chris Lattnerb304f772009-10-21 06:24:21 +0000163 switch (T) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000164 default: llvm_unreachable("not an integer!");
Chris Lattnerb304f772009-10-21 06:24:21 +0000165 case SignedShort:
166 case SignedInt:
167 case SignedLong:
Michael J. Spencer237cf582010-10-18 07:10:59 +0000168 case SignedLongLong:
Chris Lattnerb304f772009-10-21 06:24:21 +0000169 return true;
170 case UnsignedShort:
171 case UnsignedInt:
172 case UnsignedLong:
Michael J. Spencer237cf582010-10-18 07:10:59 +0000173 case UnsignedLongLong:
Chris Lattnerb304f772009-10-21 06:24:21 +0000174 return false;
175 };
176}
177
John Thompsona6fda122009-11-05 20:14:16 +0000178/// setForcedLangOptions - Set forced language options.
179/// Apply changes to the target information with respect to certain
180/// language options which change the target configuration.
181void TargetInfo::setForcedLangOptions(LangOptions &Opts) {
Daniel Dunbarfb937b82010-04-15 15:06:22 +0000182 if (Opts.NoBitFieldTypeAlign)
183 UseBitFieldTypeAlignment = false;
184 if (Opts.ShortWChar)
John Thompsona6fda122009-11-05 20:14:16 +0000185 WCharType = UnsignedShort;
John Thompsona6fda122009-11-05 20:14:16 +0000186}
Chris Lattnerb304f772009-10-21 06:24:21 +0000187
Chris Lattner525a0502007-09-22 18:29:59 +0000188//===----------------------------------------------------------------------===//
Chris Lattner525a0502007-09-22 18:29:59 +0000189
Reid Spencer5f016e22007-07-11 17:01:13 +0000190
Chris Lattner5f9e2722011-07-23 10:55:15 +0000191static StringRef removeGCCRegisterPrefix(StringRef Name) {
Anders Carlssonea041752008-02-06 00:11:32 +0000192 if (Name[0] == '%' || Name[0] == '#')
Anders Carlsson83c021c2010-01-30 19:12:25 +0000193 Name = Name.substr(1);
Michael J. Spencer237cf582010-10-18 07:10:59 +0000194
Anders Carlsson83c021c2010-01-30 19:12:25 +0000195 return Name;
Anders Carlssonea041752008-02-06 00:11:32 +0000196}
197
Eric Christopherde31fd72011-06-28 18:20:53 +0000198/// isValidClobber - Returns whether the passed in string is
199/// a valid clobber in an inline asm statement. This is used by
200/// Sema.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000201bool TargetInfo::isValidClobber(StringRef Name) const {
Eric Christopherde31fd72011-06-28 18:20:53 +0000202 return (isValidGCCRegisterName(Name) ||
203 Name == "memory" || Name == "cc");
204}
205
Anders Carlsson3346ae62007-11-24 23:38:12 +0000206/// isValidGCCRegisterName - Returns whether the passed in string
207/// is a valid register name according to GCC. This is used by Sema for
208/// inline asm statements.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000209bool TargetInfo::isValidGCCRegisterName(StringRef Name) const {
Anders Carlsson83c021c2010-01-30 19:12:25 +0000210 if (Name.empty())
211 return false;
Michael J. Spencer237cf582010-10-18 07:10:59 +0000212
Anders Carlsson6fa90862007-11-25 00:25:21 +0000213 const char * const *Names;
214 unsigned NumNames;
Mike Stump1eb44332009-09-09 15:08:12 +0000215
Anders Carlsson6fa90862007-11-25 00:25:21 +0000216 // Get rid of any register prefix.
Anders Carlsson83c021c2010-01-30 19:12:25 +0000217 Name = removeGCCRegisterPrefix(Name);
Anders Carlssonea041752008-02-06 00:11:32 +0000218
Chris Lattner0eaed122008-03-08 08:24:01 +0000219 getGCCRegNames(Names, NumNames);
Mike Stump1eb44332009-09-09 15:08:12 +0000220
Anders Carlsson6fa90862007-11-25 00:25:21 +0000221 // If we have a number it maps to an entry in the register name array.
222 if (isdigit(Name[0])) {
Anders Carlsson83c021c2010-01-30 19:12:25 +0000223 int n;
224 if (!Name.getAsInteger(0, n))
Anders Carlsson6fa90862007-11-25 00:25:21 +0000225 return n >= 0 && (unsigned)n < NumNames;
226 }
227
228 // Check register names.
229 for (unsigned i = 0; i < NumNames; i++) {
Anders Carlsson83c021c2010-01-30 19:12:25 +0000230 if (Name == Names[i])
Anders Carlsson6fa90862007-11-25 00:25:21 +0000231 return true;
232 }
Mike Stump1eb44332009-09-09 15:08:12 +0000233
Eric Christophercfd323d2011-06-21 00:05:20 +0000234 // Check any additional names that we have.
235 const AddlRegName *AddlNames;
236 unsigned NumAddlNames;
237 getGCCAddlRegNames(AddlNames, NumAddlNames);
238 for (unsigned i = 0; i < NumAddlNames; i++)
239 for (unsigned j = 0; j < llvm::array_lengthof(AddlNames[i].Names); j++) {
240 if (!AddlNames[i].Names[j])
241 break;
242 // Make sure the register that the additional name is for is within
243 // the bounds of the register names from above.
244 if (AddlNames[i].Names[j] == Name && AddlNames[i].RegNum < NumNames)
245 return true;
246 }
247
Anders Carlsson6fa90862007-11-25 00:25:21 +0000248 // Now check aliases.
Chris Lattner0eaed122008-03-08 08:24:01 +0000249 const GCCRegAlias *Aliases;
Anders Carlsson6fa90862007-11-25 00:25:21 +0000250 unsigned NumAliases;
Mike Stump1eb44332009-09-09 15:08:12 +0000251
Chris Lattner0eaed122008-03-08 08:24:01 +0000252 getGCCRegAliases(Aliases, NumAliases);
Anders Carlsson6fa90862007-11-25 00:25:21 +0000253 for (unsigned i = 0; i < NumAliases; i++) {
254 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
255 if (!Aliases[i].Aliases[j])
256 break;
Anders Carlsson83c021c2010-01-30 19:12:25 +0000257 if (Aliases[i].Aliases[j] == Name)
Anders Carlsson6fa90862007-11-25 00:25:21 +0000258 return true;
259 }
260 }
Mike Stump1eb44332009-09-09 15:08:12 +0000261
Anders Carlsson3346ae62007-11-24 23:38:12 +0000262 return false;
263}
Anders Carlssond04c6e22007-11-27 04:11:28 +0000264
Chris Lattner5f9e2722011-07-23 10:55:15 +0000265StringRef
266TargetInfo::getNormalizedGCCRegisterName(StringRef Name) const {
Anders Carlssond04c6e22007-11-27 04:11:28 +0000267 assert(isValidGCCRegisterName(Name) && "Invalid register passed in");
Mike Stump1eb44332009-09-09 15:08:12 +0000268
Anders Carlsson83c021c2010-01-30 19:12:25 +0000269 // Get rid of any register prefix.
270 Name = removeGCCRegisterPrefix(Name);
Mike Stump1eb44332009-09-09 15:08:12 +0000271
Anders Carlssond04c6e22007-11-27 04:11:28 +0000272 const char * const *Names;
273 unsigned NumNames;
274
Chris Lattner0eaed122008-03-08 08:24:01 +0000275 getGCCRegNames(Names, NumNames);
Anders Carlssond04c6e22007-11-27 04:11:28 +0000276
277 // First, check if we have a number.
278 if (isdigit(Name[0])) {
Anders Carlsson83c021c2010-01-30 19:12:25 +0000279 int n;
280 if (!Name.getAsInteger(0, n)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000281 assert(n >= 0 && (unsigned)n < NumNames &&
Anders Carlssond04c6e22007-11-27 04:11:28 +0000282 "Out of bounds register number!");
283 return Names[n];
284 }
285 }
Mike Stump1eb44332009-09-09 15:08:12 +0000286
Eric Christophercfd323d2011-06-21 00:05:20 +0000287 // Check any additional names that we have.
288 const AddlRegName *AddlNames;
289 unsigned NumAddlNames;
290 getGCCAddlRegNames(AddlNames, NumAddlNames);
291 for (unsigned i = 0; i < NumAddlNames; i++)
292 for (unsigned j = 0; j < llvm::array_lengthof(AddlNames[i].Names); j++) {
293 if (!AddlNames[i].Names[j])
294 break;
295 // Make sure the register that the additional name is for is within
296 // the bounds of the register names from above.
297 if (AddlNames[i].Names[j] == Name && AddlNames[i].RegNum < NumNames)
298 return Name;
299 }
300
Anders Carlssond04c6e22007-11-27 04:11:28 +0000301 // Now check aliases.
Chris Lattner0eaed122008-03-08 08:24:01 +0000302 const GCCRegAlias *Aliases;
Anders Carlssond04c6e22007-11-27 04:11:28 +0000303 unsigned NumAliases;
Mike Stump1eb44332009-09-09 15:08:12 +0000304
Chris Lattner0eaed122008-03-08 08:24:01 +0000305 getGCCRegAliases(Aliases, NumAliases);
Anders Carlssond04c6e22007-11-27 04:11:28 +0000306 for (unsigned i = 0; i < NumAliases; i++) {
307 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
308 if (!Aliases[i].Aliases[j])
309 break;
Anders Carlsson83c021c2010-01-30 19:12:25 +0000310 if (Aliases[i].Aliases[j] == Name)
Anders Carlssond04c6e22007-11-27 04:11:28 +0000311 return Aliases[i].Register;
312 }
313 }
Mike Stump1eb44332009-09-09 15:08:12 +0000314
Anders Carlssond04c6e22007-11-27 04:11:28 +0000315 return Name;
316}
317
Chris Lattner432c8692009-04-26 17:19:08 +0000318bool TargetInfo::validateOutputConstraint(ConstraintInfo &Info) const {
319 const char *Name = Info.getConstraintStr().c_str();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000320 // An output constraint must start with '=' or '+'
321 if (*Name != '=' && *Name != '+')
322 return false;
323
324 if (*Name == '+')
Chris Lattner44def072009-04-26 07:16:29 +0000325 Info.setIsReadWrite();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000326
327 Name++;
328 while (*Name) {
329 switch (*Name) {
330 default:
Chris Lattner44def072009-04-26 07:16:29 +0000331 if (!validateAsmConstraint(Name, Info)) {
Ted Kremenek5ab201a2008-04-24 16:36:38 +0000332 // FIXME: We temporarily return false
Anders Carlssond04c6e22007-11-27 04:11:28 +0000333 // so we can add more constraints as we hit it.
334 // Eventually, an unknown constraint should just be treated as 'g'.
Ted Kremenek5ab201a2008-04-24 16:36:38 +0000335 return false;
Anders Carlssond04c6e22007-11-27 04:11:28 +0000336 }
337 case '&': // early clobber.
338 break;
Chris Lattner40539832009-10-13 04:32:07 +0000339 case '%': // commutative.
340 // FIXME: Check that there is a another register after this one.
341 break;
Anders Carlssond04c6e22007-11-27 04:11:28 +0000342 case 'r': // general register.
Chris Lattner44def072009-04-26 07:16:29 +0000343 Info.setAllowsRegister();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000344 break;
345 case 'm': // memory operand.
Dale Johannesen0f048a42010-09-07 18:40:41 +0000346 case 'o': // offsetable memory operand.
347 case 'V': // non-offsetable memory operand.
348 case '<': // autodecrement memory operand.
349 case '>': // autoincrement memory operand.
Chris Lattner44def072009-04-26 07:16:29 +0000350 Info.setAllowsMemory();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000351 break;
352 case 'g': // general register, memory operand or immediate integer.
Anders Carlsson775841f2009-01-18 02:12:04 +0000353 case 'X': // any operand.
Chris Lattner44def072009-04-26 07:16:29 +0000354 Info.setAllowsRegister();
355 Info.setAllowsMemory();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000356 break;
John Thompsonef44e112010-08-10 19:20:14 +0000357 case ',': // multiple alternative constraint. Pass it.
John Thompson6f158032010-08-11 00:58:20 +0000358 // Handle additional optional '=' or '+' modifiers.
John Thompson2f474ea2010-09-18 01:15:13 +0000359 if (Name[1] == '=' || Name[1] == '+')
John Thompson6f158032010-08-11 00:58:20 +0000360 Name++;
John Thompsonef44e112010-08-10 19:20:14 +0000361 break;
362 case '?': // Disparage slightly code.
Dale Johannesen0f048a42010-09-07 18:40:41 +0000363 case '!': // Disparage severely.
John Thompsonef44e112010-08-10 19:20:14 +0000364 break; // Pass them.
Anders Carlssond04c6e22007-11-27 04:11:28 +0000365 }
Mike Stump1eb44332009-09-09 15:08:12 +0000366
Anders Carlssond04c6e22007-11-27 04:11:28 +0000367 Name++;
368 }
Mike Stump1eb44332009-09-09 15:08:12 +0000369
Anders Carlssond04c6e22007-11-27 04:11:28 +0000370 return true;
371}
372
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000373bool TargetInfo::resolveSymbolicName(const char *&Name,
Chris Lattner2819fa82009-04-26 17:57:12 +0000374 ConstraintInfo *OutputConstraints,
375 unsigned NumOutputs,
Chris Lattner44def072009-04-26 07:16:29 +0000376 unsigned &Index) const {
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000377 assert(*Name == '[' && "Symbolic name did not start with '['");
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000378 Name++;
379 const char *Start = Name;
380 while (*Name && *Name != ']')
381 Name++;
Mike Stump1eb44332009-09-09 15:08:12 +0000382
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000383 if (!*Name) {
384 // Missing ']'
385 return false;
386 }
Mike Stump1eb44332009-09-09 15:08:12 +0000387
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000388 std::string SymbolicName(Start, Name - Start);
Mike Stump1eb44332009-09-09 15:08:12 +0000389
Chris Lattner2819fa82009-04-26 17:57:12 +0000390 for (Index = 0; Index != NumOutputs; ++Index)
391 if (SymbolicName == OutputConstraints[Index].getName())
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000392 return true;
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000393
394 return false;
395}
396
Chris Lattner2819fa82009-04-26 17:57:12 +0000397bool TargetInfo::validateInputConstraint(ConstraintInfo *OutputConstraints,
398 unsigned NumOutputs,
Chris Lattner44def072009-04-26 07:16:29 +0000399 ConstraintInfo &Info) const {
Chris Lattner432c8692009-04-26 17:19:08 +0000400 const char *Name = Info.ConstraintStr.c_str();
401
Anders Carlssond04c6e22007-11-27 04:11:28 +0000402 while (*Name) {
403 switch (*Name) {
404 default:
405 // Check if we have a matching constraint
406 if (*Name >= '0' && *Name <= '9') {
407 unsigned i = *Name - '0';
Mike Stump1eb44332009-09-09 15:08:12 +0000408
Anders Carlssond04c6e22007-11-27 04:11:28 +0000409 // Check if matching constraint is out of bounds.
410 if (i >= NumOutputs)
411 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000412
Anders Carlsson86eda392010-11-03 02:22:29 +0000413 // A number must refer to an output only operand.
414 if (OutputConstraints[i].isReadWrite())
415 return false;
416
Anders Carlsson79ca1ee2010-11-03 02:54:51 +0000417 // If the constraint is already tied, it must be tied to the
418 // same operand referenced to by the number.
419 if (Info.hasTiedOperand() && Info.getTiedOperand() != i)
420 return false;
421
Mike Stump1eb44332009-09-09 15:08:12 +0000422 // The constraint should have the same info as the respective
Anders Carlsson03eb5432009-01-27 20:38:24 +0000423 // output constraint.
Chris Lattnerd6887612009-04-26 18:05:25 +0000424 Info.setTiedOperand(i, OutputConstraints[i]);
Chris Lattner44def072009-04-26 07:16:29 +0000425 } else if (!validateAsmConstraint(Name, Info)) {
Daniel Dunbar302684c2008-08-25 09:46:27 +0000426 // FIXME: This error return is in place temporarily so we can
427 // add more constraints as we hit it. Eventually, an unknown
428 // constraint should just be treated as 'g'.
429 return false;
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000430 }
431 break;
432 case '[': {
433 unsigned Index = 0;
Chris Lattner2819fa82009-04-26 17:57:12 +0000434 if (!resolveSymbolicName(Name, OutputConstraints, NumOutputs, Index))
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000435 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000436
Anders Carlsson79ca1ee2010-11-03 02:54:51 +0000437 // If the constraint is already tied, it must be tied to the
438 // same operand referenced to by the number.
439 if (Info.hasTiedOperand() && Info.getTiedOperand() != Index)
440 return false;
441
Eli Friedman0eff4e72010-08-11 23:03:37 +0000442 Info.setTiedOperand(Index, OutputConstraints[Index]);
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000443 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000444 }
Anders Carlsson44fe49c2007-12-08 19:32:57 +0000445 case '%': // commutative
446 // FIXME: Fail if % is used with the last operand.
447 break;
Anders Carlssond04c6e22007-11-27 04:11:28 +0000448 case 'i': // immediate integer.
Anders Carlssonefdfa772008-03-09 06:02:02 +0000449 case 'n': // immediate integer with a known value.
Anders Carlssond04c6e22007-11-27 04:11:28 +0000450 break;
Chris Lattner1d138792009-05-06 04:33:31 +0000451 case 'I': // Various constant constraints with target-specific meanings.
452 case 'J':
453 case 'K':
454 case 'L':
455 case 'M':
456 case 'N':
457 case 'O':
458 case 'P':
459 break;
Anders Carlssond04c6e22007-11-27 04:11:28 +0000460 case 'r': // general register.
Chris Lattner44def072009-04-26 07:16:29 +0000461 Info.setAllowsRegister();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000462 break;
463 case 'm': // memory operand.
Dale Johannesen0f048a42010-09-07 18:40:41 +0000464 case 'o': // offsettable memory operand.
465 case 'V': // non-offsettable memory operand.
466 case '<': // autodecrement memory operand.
467 case '>': // autoincrement memory operand.
Chris Lattner44def072009-04-26 07:16:29 +0000468 Info.setAllowsMemory();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000469 break;
470 case 'g': // general register, memory operand or immediate integer.
Anders Carlsson775841f2009-01-18 02:12:04 +0000471 case 'X': // any operand.
Chris Lattner44def072009-04-26 07:16:29 +0000472 Info.setAllowsRegister();
473 Info.setAllowsMemory();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000474 break;
John Thompson7ccc58f2010-09-21 22:04:54 +0000475 case 'E': // immediate floating point.
476 case 'F': // immediate floating point.
477 case 'p': // address operand.
478 break;
John Thompsonef44e112010-08-10 19:20:14 +0000479 case ',': // multiple alternative constraint. Ignore comma.
480 break;
481 case '?': // Disparage slightly code.
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000482 case '!': // Disparage severely.
John Thompsonef44e112010-08-10 19:20:14 +0000483 break; // Pass them.
Anders Carlssond04c6e22007-11-27 04:11:28 +0000484 }
Mike Stump1eb44332009-09-09 15:08:12 +0000485
Anders Carlssond04c6e22007-11-27 04:11:28 +0000486 Name++;
487 }
Mike Stump1eb44332009-09-09 15:08:12 +0000488
Anders Carlssond04c6e22007-11-27 04:11:28 +0000489 return true;
490}