blob: 3257526289e027487466d8c6c5c33e2e91b8f282 [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.
Douglas Gregor9a022bb2012-10-15 16:45:32 +000027TargetInfo::TargetInfo(const std::string &T) : TargetOpts(), Triple(T)
28{
Daniel Dunbarb6a16932010-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 Friedmane6a24e82011-12-22 03:51:45 +000031 BigEndian = true;
Eli Friedmanb030f022009-04-19 21:38:35 +000032 TLSSupported = true;
Chris Lattner9bffb072010-04-23 16:29:58 +000033 NoAsmVariants = false;
Chris Lattner927686f2008-05-09 06:08:39 +000034 PointerWidth = PointerAlign = 32;
Roman Divackyc81f2a22011-01-06 08:27:10 +000035 BoolWidth = BoolAlign = 8;
Chris Lattner2621fd12008-05-08 05:58:21 +000036 IntWidth = IntAlign = 32;
Chris Lattnerec10f582008-05-09 05:50:02 +000037 LongWidth = LongAlign = 32;
38 LongLongWidth = LongLongAlign = 64;
Nick Lewycky7ec59c72011-12-16 22:34:14 +000039 SuitableAlign = 64;
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +000040 HalfWidth = 16;
41 HalfAlign = 16;
Eli Friedman61538a72008-05-20 14:21:01 +000042 FloatWidth = 32;
43 FloatAlign = 32;
Nate Begeman3d292062008-04-18 17:17:24 +000044 DoubleWidth = 64;
Eli Friedman61538a72008-05-20 14:21:01 +000045 DoubleAlign = 64;
46 LongDoubleWidth = 64;
47 LongDoubleAlign = 64;
Rafael Espindola6deecb02010-06-04 23:15:27 +000048 LargeArrayMinWidth = 0;
49 LargeArrayAlign = 0;
Eli Friedman2be46072011-10-14 20:59:01 +000050 MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 0;
Chad Rosierf9e9af72012-07-13 23:57:43 +000051 MaxVectorAlign = 0;
Anders Carlsson6a3615c2008-10-31 16:05:19 +000052 SizeType = UnsignedLong;
Eli Friedmanf509d732008-11-02 02:43:55 +000053 PtrDiffType = SignedLong;
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +000054 IntMaxType = SignedLongLong;
55 UIntMaxType = UnsignedLongLong;
Chris Lattner6ad474f2009-02-13 22:28:55 +000056 IntPtrType = SignedLong;
Eli Friedmanf509d732008-11-02 02:43:55 +000057 WCharType = SignedInt;
Chris Lattnere64ef802009-10-21 04:59:34 +000058 WIntType = SignedInt;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +000059 Char16Type = UnsignedShort;
60 Char32Type = UnsignedInt;
Eli Friedman3c7b6e42009-07-01 03:36:11 +000061 Int64Type = SignedLongLong;
Edward O'Callaghan9cf910e2009-11-21 00:49:54 +000062 SigAtomicType = SignedInt;
Fariborz Jahanian93a49942012-04-16 21:03:30 +000063 UseSignedCharForObjCBool = true;
Daniel Dunbarb6830d62010-04-15 15:06:18 +000064 UseBitFieldTypeAlignment = true;
Chad Rosier61a62212011-08-04 01:21:14 +000065 UseZeroLengthBitfieldAlignment = false;
66 ZeroLengthBitfieldBoundary = 0;
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +000067 HalfFormat = &llvm::APFloat::IEEEhalf;
Chris Lattnercd4fc422008-03-08 08:59:43 +000068 FloatFormat = &llvm::APFloat::IEEEsingle;
69 DoubleFormat = &llvm::APFloat::IEEEdouble;
70 LongDoubleFormat = &llvm::APFloat::IEEEdouble;
Eli Friedmaned855cb2008-08-21 00:13:15 +000071 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 +000072 "i64:64:64-f32:32:32-f64:64:64-n32";
Chris Lattner3fdf4672008-10-05 19:22:37 +000073 UserLabelPrefix = "_";
Roman Divackybe4c8702011-02-10 16:52:03 +000074 MCountName = "mcount";
Abramo Bagnara62f940b2011-09-08 14:20:25 +000075 RegParmMax = 0;
76 SSERegParmMax = 0;
Daniel Dunbar613fd672010-05-27 00:35:16 +000077 HasAlignMac68kSupport = false;
Daniel Dunbardacf9dd2010-07-14 23:39:36 +000078
79 // Default to no types using fpret.
80 RealTypeUsesObjCFPRet = 0;
John McCallee79a4c2010-08-21 22:46:04 +000081
Anders Carlssoneea64802011-10-31 16:27:11 +000082 // Default to not using fp2ret for __Complex long double
83 ComplexLongDoubleUsesFP2Ret = false;
84
John McCallee79a4c2010-08-21 22:46:04 +000085 // Default to using the Itanium ABI.
86 CXXABI = CXXABI_Itanium;
Peter Collingbourne207f4d82011-03-18 22:38:29 +000087
88 // Default to an empty address space map.
89 AddrSpaceMap = &DefaultAddrSpaceMap;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +000090
91 // Default to an unknown platform name.
92 PlatformName = "unknown";
93 PlatformMinVersion = VersionTuple();
Chris Lattnercd4fc422008-03-08 08:59:43 +000094}
95
Chris Lattner0eaed122008-03-08 08:24:01 +000096// Out of line virtual dtor for TargetInfo.
97TargetInfo::~TargetInfo() {}
Reid Spencer5f016e22007-07-11 17:01:13 +000098
Chris Lattner2b5abf52009-02-06 05:04:11 +000099/// getTypeName - Return the user string for the specified integer type enum.
100/// For example, SignedShort -> "short".
101const char *TargetInfo::getTypeName(IntType T) {
102 switch (T) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000103 default: llvm_unreachable("not an integer!");
Chris Lattner2b5abf52009-02-06 05:04:11 +0000104 case SignedShort: return "short";
105 case UnsignedShort: return "unsigned short";
106 case SignedInt: return "int";
107 case UnsignedInt: return "unsigned int";
108 case SignedLong: return "long int";
109 case UnsignedLong: return "long unsigned int";
110 case SignedLongLong: return "long long int";
111 case UnsignedLongLong: return "long long unsigned int";
112 }
113}
114
Chris Lattnerb304f772009-10-21 06:24:21 +0000115/// getTypeConstantSuffix - Return the constant suffix for the specified
116/// integer type enum. For example, SignedLong -> "L".
117const char *TargetInfo::getTypeConstantSuffix(IntType T) {
118 switch (T) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000119 default: llvm_unreachable("not an integer!");
Chris Lattnerb304f772009-10-21 06:24:21 +0000120 case SignedShort:
121 case SignedInt: return "";
122 case SignedLong: return "L";
123 case SignedLongLong: return "LL";
124 case UnsignedShort:
125 case UnsignedInt: return "U";
126 case UnsignedLong: return "UL";
127 case UnsignedLongLong: return "ULL";
128 }
129}
130
Michael J. Spencer237cf582010-10-18 07:10:59 +0000131/// getTypeWidth - Return the width (in bits) of the specified integer type
Chris Lattnerb304f772009-10-21 06:24:21 +0000132/// enum. For example, SignedInt -> getIntWidth().
133unsigned TargetInfo::getTypeWidth(IntType T) const {
134 switch (T) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000135 default: llvm_unreachable("not an integer!");
Chris Lattner9099e7b2009-11-05 21:21:32 +0000136 case SignedShort:
Chris Lattnerb304f772009-10-21 06:24:21 +0000137 case UnsignedShort: return getShortWidth();
Chris Lattner9099e7b2009-11-05 21:21:32 +0000138 case SignedInt:
Chris Lattnerb304f772009-10-21 06:24:21 +0000139 case UnsignedInt: return getIntWidth();
Chris Lattner9099e7b2009-11-05 21:21:32 +0000140 case SignedLong:
Chris Lattnerb304f772009-10-21 06:24:21 +0000141 case UnsignedLong: return getLongWidth();
Chris Lattner9099e7b2009-11-05 21:21:32 +0000142 case SignedLongLong:
Chris Lattnerb304f772009-10-21 06:24:21 +0000143 case UnsignedLongLong: return getLongLongWidth();
144 };
145}
146
Michael J. Spencer237cf582010-10-18 07:10:59 +0000147/// getTypeAlign - Return the alignment (in bits) of the specified integer type
Chris Lattner9099e7b2009-11-05 21:21:32 +0000148/// enum. For example, SignedInt -> getIntAlign().
149unsigned TargetInfo::getTypeAlign(IntType T) const {
150 switch (T) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000151 default: llvm_unreachable("not an integer!");
Chris Lattner9099e7b2009-11-05 21:21:32 +0000152 case SignedShort:
153 case UnsignedShort: return getShortAlign();
154 case SignedInt:
155 case UnsignedInt: return getIntAlign();
156 case SignedLong:
157 case UnsignedLong: return getLongAlign();
158 case SignedLongLong:
159 case UnsignedLongLong: return getLongLongAlign();
160 };
161}
162
Chris Lattner961f0702009-10-25 22:49:18 +0000163/// isTypeSigned - Return whether an integer types is signed. Returns true if
Chris Lattnerb304f772009-10-21 06:24:21 +0000164/// the type is signed; false otherwise.
Chris Lattner3f59c972010-12-25 23:25:43 +0000165bool TargetInfo::isTypeSigned(IntType T) {
Chris Lattnerb304f772009-10-21 06:24:21 +0000166 switch (T) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000167 default: llvm_unreachable("not an integer!");
Chris Lattnerb304f772009-10-21 06:24:21 +0000168 case SignedShort:
169 case SignedInt:
170 case SignedLong:
Michael J. Spencer237cf582010-10-18 07:10:59 +0000171 case SignedLongLong:
Chris Lattnerb304f772009-10-21 06:24:21 +0000172 return true;
173 case UnsignedShort:
174 case UnsignedInt:
175 case UnsignedLong:
Michael J. Spencer237cf582010-10-18 07:10:59 +0000176 case UnsignedLongLong:
Chris Lattnerb304f772009-10-21 06:24:21 +0000177 return false;
178 };
179}
180
John Thompsona6fda122009-11-05 20:14:16 +0000181/// setForcedLangOptions - Set forced language options.
182/// Apply changes to the target information with respect to certain
183/// language options which change the target configuration.
184void TargetInfo::setForcedLangOptions(LangOptions &Opts) {
Daniel Dunbarfb937b82010-04-15 15:06:22 +0000185 if (Opts.NoBitFieldTypeAlign)
186 UseBitFieldTypeAlignment = false;
187 if (Opts.ShortWChar)
John Thompsona6fda122009-11-05 20:14:16 +0000188 WCharType = UnsignedShort;
John Thompsona6fda122009-11-05 20:14:16 +0000189}
Chris Lattnerb304f772009-10-21 06:24:21 +0000190
Chris Lattner525a0502007-09-22 18:29:59 +0000191//===----------------------------------------------------------------------===//
Chris Lattner525a0502007-09-22 18:29:59 +0000192
Reid Spencer5f016e22007-07-11 17:01:13 +0000193
Chris Lattner5f9e2722011-07-23 10:55:15 +0000194static StringRef removeGCCRegisterPrefix(StringRef Name) {
Anders Carlssonea041752008-02-06 00:11:32 +0000195 if (Name[0] == '%' || Name[0] == '#')
Anders Carlsson83c021c2010-01-30 19:12:25 +0000196 Name = Name.substr(1);
Michael J. Spencer237cf582010-10-18 07:10:59 +0000197
Anders Carlsson83c021c2010-01-30 19:12:25 +0000198 return Name;
Anders Carlssonea041752008-02-06 00:11:32 +0000199}
200
Eric Christopherde31fd72011-06-28 18:20:53 +0000201/// isValidClobber - Returns whether the passed in string is
202/// a valid clobber in an inline asm statement. This is used by
203/// Sema.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000204bool TargetInfo::isValidClobber(StringRef Name) const {
Eric Christopherde31fd72011-06-28 18:20:53 +0000205 return (isValidGCCRegisterName(Name) ||
206 Name == "memory" || Name == "cc");
207}
208
Anders Carlsson3346ae62007-11-24 23:38:12 +0000209/// isValidGCCRegisterName - Returns whether the passed in string
210/// is a valid register name according to GCC. This is used by Sema for
211/// inline asm statements.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000212bool TargetInfo::isValidGCCRegisterName(StringRef Name) const {
Anders Carlsson83c021c2010-01-30 19:12:25 +0000213 if (Name.empty())
214 return false;
Michael J. Spencer237cf582010-10-18 07:10:59 +0000215
Anders Carlsson6fa90862007-11-25 00:25:21 +0000216 const char * const *Names;
217 unsigned NumNames;
Mike Stump1eb44332009-09-09 15:08:12 +0000218
Anders Carlsson6fa90862007-11-25 00:25:21 +0000219 // Get rid of any register prefix.
Anders Carlsson83c021c2010-01-30 19:12:25 +0000220 Name = removeGCCRegisterPrefix(Name);
Anders Carlssonea041752008-02-06 00:11:32 +0000221
Chris Lattner0eaed122008-03-08 08:24:01 +0000222 getGCCRegNames(Names, NumNames);
Mike Stump1eb44332009-09-09 15:08:12 +0000223
Anders Carlsson6fa90862007-11-25 00:25:21 +0000224 // If we have a number it maps to an entry in the register name array.
225 if (isdigit(Name[0])) {
Anders Carlsson83c021c2010-01-30 19:12:25 +0000226 int n;
227 if (!Name.getAsInteger(0, n))
Anders Carlsson6fa90862007-11-25 00:25:21 +0000228 return n >= 0 && (unsigned)n < NumNames;
229 }
230
231 // Check register names.
232 for (unsigned i = 0; i < NumNames; i++) {
Anders Carlsson83c021c2010-01-30 19:12:25 +0000233 if (Name == Names[i])
Anders Carlsson6fa90862007-11-25 00:25:21 +0000234 return true;
235 }
Mike Stump1eb44332009-09-09 15:08:12 +0000236
Eric Christophercfd323d2011-06-21 00:05:20 +0000237 // Check any additional names that we have.
238 const AddlRegName *AddlNames;
239 unsigned NumAddlNames;
240 getGCCAddlRegNames(AddlNames, NumAddlNames);
241 for (unsigned i = 0; i < NumAddlNames; i++)
242 for (unsigned j = 0; j < llvm::array_lengthof(AddlNames[i].Names); j++) {
243 if (!AddlNames[i].Names[j])
244 break;
245 // Make sure the register that the additional name is for is within
246 // the bounds of the register names from above.
247 if (AddlNames[i].Names[j] == Name && AddlNames[i].RegNum < NumNames)
248 return true;
249 }
250
Anders Carlsson6fa90862007-11-25 00:25:21 +0000251 // Now check aliases.
Chris Lattner0eaed122008-03-08 08:24:01 +0000252 const GCCRegAlias *Aliases;
Anders Carlsson6fa90862007-11-25 00:25:21 +0000253 unsigned NumAliases;
Mike Stump1eb44332009-09-09 15:08:12 +0000254
Chris Lattner0eaed122008-03-08 08:24:01 +0000255 getGCCRegAliases(Aliases, NumAliases);
Anders Carlsson6fa90862007-11-25 00:25:21 +0000256 for (unsigned i = 0; i < NumAliases; i++) {
257 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
258 if (!Aliases[i].Aliases[j])
259 break;
Anders Carlsson83c021c2010-01-30 19:12:25 +0000260 if (Aliases[i].Aliases[j] == Name)
Anders Carlsson6fa90862007-11-25 00:25:21 +0000261 return true;
262 }
263 }
Mike Stump1eb44332009-09-09 15:08:12 +0000264
Anders Carlsson3346ae62007-11-24 23:38:12 +0000265 return false;
266}
Anders Carlssond04c6e22007-11-27 04:11:28 +0000267
Chris Lattner5f9e2722011-07-23 10:55:15 +0000268StringRef
269TargetInfo::getNormalizedGCCRegisterName(StringRef Name) const {
Anders Carlssond04c6e22007-11-27 04:11:28 +0000270 assert(isValidGCCRegisterName(Name) && "Invalid register passed in");
Mike Stump1eb44332009-09-09 15:08:12 +0000271
Anders Carlsson83c021c2010-01-30 19:12:25 +0000272 // Get rid of any register prefix.
273 Name = removeGCCRegisterPrefix(Name);
Mike Stump1eb44332009-09-09 15:08:12 +0000274
Anders Carlssond04c6e22007-11-27 04:11:28 +0000275 const char * const *Names;
276 unsigned NumNames;
277
Chris Lattner0eaed122008-03-08 08:24:01 +0000278 getGCCRegNames(Names, NumNames);
Anders Carlssond04c6e22007-11-27 04:11:28 +0000279
280 // First, check if we have a number.
281 if (isdigit(Name[0])) {
Anders Carlsson83c021c2010-01-30 19:12:25 +0000282 int n;
283 if (!Name.getAsInteger(0, n)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000284 assert(n >= 0 && (unsigned)n < NumNames &&
Anders Carlssond04c6e22007-11-27 04:11:28 +0000285 "Out of bounds register number!");
286 return Names[n];
287 }
288 }
Mike Stump1eb44332009-09-09 15:08:12 +0000289
Eric Christophercfd323d2011-06-21 00:05:20 +0000290 // Check any additional names that we have.
291 const AddlRegName *AddlNames;
292 unsigned NumAddlNames;
293 getGCCAddlRegNames(AddlNames, NumAddlNames);
294 for (unsigned i = 0; i < NumAddlNames; i++)
295 for (unsigned j = 0; j < llvm::array_lengthof(AddlNames[i].Names); j++) {
296 if (!AddlNames[i].Names[j])
297 break;
298 // Make sure the register that the additional name is for is within
299 // the bounds of the register names from above.
300 if (AddlNames[i].Names[j] == Name && AddlNames[i].RegNum < NumNames)
301 return Name;
302 }
303
Anders Carlssond04c6e22007-11-27 04:11:28 +0000304 // Now check aliases.
Chris Lattner0eaed122008-03-08 08:24:01 +0000305 const GCCRegAlias *Aliases;
Anders Carlssond04c6e22007-11-27 04:11:28 +0000306 unsigned NumAliases;
Mike Stump1eb44332009-09-09 15:08:12 +0000307
Chris Lattner0eaed122008-03-08 08:24:01 +0000308 getGCCRegAliases(Aliases, NumAliases);
Anders Carlssond04c6e22007-11-27 04:11:28 +0000309 for (unsigned i = 0; i < NumAliases; i++) {
310 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
311 if (!Aliases[i].Aliases[j])
312 break;
Anders Carlsson83c021c2010-01-30 19:12:25 +0000313 if (Aliases[i].Aliases[j] == Name)
Anders Carlssond04c6e22007-11-27 04:11:28 +0000314 return Aliases[i].Register;
315 }
316 }
Mike Stump1eb44332009-09-09 15:08:12 +0000317
Anders Carlssond04c6e22007-11-27 04:11:28 +0000318 return Name;
319}
320
Chris Lattner432c8692009-04-26 17:19:08 +0000321bool TargetInfo::validateOutputConstraint(ConstraintInfo &Info) const {
322 const char *Name = Info.getConstraintStr().c_str();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000323 // An output constraint must start with '=' or '+'
324 if (*Name != '=' && *Name != '+')
325 return false;
326
327 if (*Name == '+')
Chris Lattner44def072009-04-26 07:16:29 +0000328 Info.setIsReadWrite();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000329
330 Name++;
331 while (*Name) {
332 switch (*Name) {
333 default:
Chris Lattner44def072009-04-26 07:16:29 +0000334 if (!validateAsmConstraint(Name, Info)) {
Ted Kremenek5ab201a2008-04-24 16:36:38 +0000335 // FIXME: We temporarily return false
Anders Carlssond04c6e22007-11-27 04:11:28 +0000336 // so we can add more constraints as we hit it.
337 // Eventually, an unknown constraint should just be treated as 'g'.
Ted Kremenek5ab201a2008-04-24 16:36:38 +0000338 return false;
Anders Carlssond04c6e22007-11-27 04:11:28 +0000339 }
340 case '&': // early clobber.
341 break;
Chris Lattner40539832009-10-13 04:32:07 +0000342 case '%': // commutative.
343 // FIXME: Check that there is a another register after this one.
344 break;
Anders Carlssond04c6e22007-11-27 04:11:28 +0000345 case 'r': // general register.
Chris Lattner44def072009-04-26 07:16:29 +0000346 Info.setAllowsRegister();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000347 break;
348 case 'm': // memory operand.
Dale Johannesen0f048a42010-09-07 18:40:41 +0000349 case 'o': // offsetable memory operand.
350 case 'V': // non-offsetable memory operand.
351 case '<': // autodecrement memory operand.
352 case '>': // autoincrement memory operand.
Chris Lattner44def072009-04-26 07:16:29 +0000353 Info.setAllowsMemory();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000354 break;
355 case 'g': // general register, memory operand or immediate integer.
Anders Carlsson775841f2009-01-18 02:12:04 +0000356 case 'X': // any operand.
Chris Lattner44def072009-04-26 07:16:29 +0000357 Info.setAllowsRegister();
358 Info.setAllowsMemory();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000359 break;
John Thompsonef44e112010-08-10 19:20:14 +0000360 case ',': // multiple alternative constraint. Pass it.
John Thompson6f158032010-08-11 00:58:20 +0000361 // Handle additional optional '=' or '+' modifiers.
John Thompson2f474ea2010-09-18 01:15:13 +0000362 if (Name[1] == '=' || Name[1] == '+')
John Thompson6f158032010-08-11 00:58:20 +0000363 Name++;
John Thompsonef44e112010-08-10 19:20:14 +0000364 break;
365 case '?': // Disparage slightly code.
Dale Johannesen0f048a42010-09-07 18:40:41 +0000366 case '!': // Disparage severely.
Ulrich Weigande6b3dba2012-10-29 12:20:54 +0000367 case '#': // Ignore as constraint.
368 case '*': // Ignore for choosing register preferences.
John Thompsonef44e112010-08-10 19:20:14 +0000369 break; // Pass them.
Anders Carlssond04c6e22007-11-27 04:11:28 +0000370 }
Mike Stump1eb44332009-09-09 15:08:12 +0000371
Anders Carlssond04c6e22007-11-27 04:11:28 +0000372 Name++;
373 }
Mike Stump1eb44332009-09-09 15:08:12 +0000374
Anders Carlssond04c6e22007-11-27 04:11:28 +0000375 return true;
376}
377
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000378bool TargetInfo::resolveSymbolicName(const char *&Name,
Chris Lattner2819fa82009-04-26 17:57:12 +0000379 ConstraintInfo *OutputConstraints,
380 unsigned NumOutputs,
Chris Lattner44def072009-04-26 07:16:29 +0000381 unsigned &Index) const {
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000382 assert(*Name == '[' && "Symbolic name did not start with '['");
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000383 Name++;
384 const char *Start = Name;
385 while (*Name && *Name != ']')
386 Name++;
Mike Stump1eb44332009-09-09 15:08:12 +0000387
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000388 if (!*Name) {
389 // Missing ']'
390 return false;
391 }
Mike Stump1eb44332009-09-09 15:08:12 +0000392
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000393 std::string SymbolicName(Start, Name - Start);
Mike Stump1eb44332009-09-09 15:08:12 +0000394
Chris Lattner2819fa82009-04-26 17:57:12 +0000395 for (Index = 0; Index != NumOutputs; ++Index)
396 if (SymbolicName == OutputConstraints[Index].getName())
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000397 return true;
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000398
399 return false;
400}
401
Chris Lattner2819fa82009-04-26 17:57:12 +0000402bool TargetInfo::validateInputConstraint(ConstraintInfo *OutputConstraints,
403 unsigned NumOutputs,
Chris Lattner44def072009-04-26 07:16:29 +0000404 ConstraintInfo &Info) const {
Chris Lattner432c8692009-04-26 17:19:08 +0000405 const char *Name = Info.ConstraintStr.c_str();
406
Anders Carlssond04c6e22007-11-27 04:11:28 +0000407 while (*Name) {
408 switch (*Name) {
409 default:
410 // Check if we have a matching constraint
411 if (*Name >= '0' && *Name <= '9') {
412 unsigned i = *Name - '0';
Mike Stump1eb44332009-09-09 15:08:12 +0000413
Anders Carlssond04c6e22007-11-27 04:11:28 +0000414 // Check if matching constraint is out of bounds.
415 if (i >= NumOutputs)
416 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000417
Anders Carlsson86eda392010-11-03 02:22:29 +0000418 // A number must refer to an output only operand.
419 if (OutputConstraints[i].isReadWrite())
420 return false;
421
Anders Carlsson79ca1ee2010-11-03 02:54:51 +0000422 // If the constraint is already tied, it must be tied to the
423 // same operand referenced to by the number.
424 if (Info.hasTiedOperand() && Info.getTiedOperand() != i)
425 return false;
426
Mike Stump1eb44332009-09-09 15:08:12 +0000427 // The constraint should have the same info as the respective
Anders Carlsson03eb5432009-01-27 20:38:24 +0000428 // output constraint.
Chris Lattnerd6887612009-04-26 18:05:25 +0000429 Info.setTiedOperand(i, OutputConstraints[i]);
Chris Lattner44def072009-04-26 07:16:29 +0000430 } else if (!validateAsmConstraint(Name, Info)) {
Daniel Dunbar302684c2008-08-25 09:46:27 +0000431 // FIXME: This error return is in place temporarily so we can
432 // add more constraints as we hit it. Eventually, an unknown
433 // constraint should just be treated as 'g'.
434 return false;
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000435 }
436 break;
437 case '[': {
438 unsigned Index = 0;
Chris Lattner2819fa82009-04-26 17:57:12 +0000439 if (!resolveSymbolicName(Name, OutputConstraints, NumOutputs, Index))
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000440 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000441
Anders Carlsson79ca1ee2010-11-03 02:54:51 +0000442 // If the constraint is already tied, it must be tied to the
443 // same operand referenced to by the number.
444 if (Info.hasTiedOperand() && Info.getTiedOperand() != Index)
445 return false;
446
Eli Friedman0eff4e72010-08-11 23:03:37 +0000447 Info.setTiedOperand(Index, OutputConstraints[Index]);
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000448 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000449 }
Anders Carlsson44fe49c2007-12-08 19:32:57 +0000450 case '%': // commutative
451 // FIXME: Fail if % is used with the last operand.
452 break;
Anders Carlssond04c6e22007-11-27 04:11:28 +0000453 case 'i': // immediate integer.
Anders Carlssonefdfa772008-03-09 06:02:02 +0000454 case 'n': // immediate integer with a known value.
Anders Carlssond04c6e22007-11-27 04:11:28 +0000455 break;
Chris Lattner1d138792009-05-06 04:33:31 +0000456 case 'I': // Various constant constraints with target-specific meanings.
457 case 'J':
458 case 'K':
459 case 'L':
460 case 'M':
461 case 'N':
462 case 'O':
463 case 'P':
464 break;
Anders Carlssond04c6e22007-11-27 04:11:28 +0000465 case 'r': // general register.
Chris Lattner44def072009-04-26 07:16:29 +0000466 Info.setAllowsRegister();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000467 break;
468 case 'm': // memory operand.
Dale Johannesen0f048a42010-09-07 18:40:41 +0000469 case 'o': // offsettable memory operand.
470 case 'V': // non-offsettable memory operand.
471 case '<': // autodecrement memory operand.
472 case '>': // autoincrement memory operand.
Chris Lattner44def072009-04-26 07:16:29 +0000473 Info.setAllowsMemory();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000474 break;
475 case 'g': // general register, memory operand or immediate integer.
Anders Carlsson775841f2009-01-18 02:12:04 +0000476 case 'X': // any operand.
Chris Lattner44def072009-04-26 07:16:29 +0000477 Info.setAllowsRegister();
478 Info.setAllowsMemory();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000479 break;
John Thompson7ccc58f2010-09-21 22:04:54 +0000480 case 'E': // immediate floating point.
481 case 'F': // immediate floating point.
482 case 'p': // address operand.
483 break;
John Thompsonef44e112010-08-10 19:20:14 +0000484 case ',': // multiple alternative constraint. Ignore comma.
485 break;
486 case '?': // Disparage slightly code.
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000487 case '!': // Disparage severely.
Ulrich Weigande6b3dba2012-10-29 12:20:54 +0000488 case '#': // Ignore as constraint.
489 case '*': // Ignore for choosing register preferences.
John Thompsonef44e112010-08-10 19:20:14 +0000490 break; // Pass them.
Anders Carlssond04c6e22007-11-27 04:11:28 +0000491 }
Mike Stump1eb44332009-09-09 15:08:12 +0000492
Anders Carlssond04c6e22007-11-27 04:11:28 +0000493 Name++;
494 }
Mike Stump1eb44332009-09-09 15:08:12 +0000495
Anders Carlssond04c6e22007-11-27 04:11:28 +0000496 return true;
497}