blob: 66274f11fc1421f9b543a7908341381c6d1a37a2 [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 Friedmanb030f022009-04-19 21:38:35 +000030 TLSSupported = true;
Chris Lattner9bffb072010-04-23 16:29:58 +000031 NoAsmVariants = false;
Chris Lattner927686f2008-05-09 06:08:39 +000032 PointerWidth = PointerAlign = 32;
Roman Divackyc81f2a22011-01-06 08:27:10 +000033 BoolWidth = BoolAlign = 8;
Chris Lattner2621fd12008-05-08 05:58:21 +000034 IntWidth = IntAlign = 32;
Chris Lattnerec10f582008-05-09 05:50:02 +000035 LongWidth = LongAlign = 32;
36 LongLongWidth = LongLongAlign = 64;
Nick Lewycky7ec59c72011-12-16 22:34:14 +000037 SuitableAlign = 64;
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +000038 HalfWidth = 16;
39 HalfAlign = 16;
Eli Friedman61538a72008-05-20 14:21:01 +000040 FloatWidth = 32;
41 FloatAlign = 32;
Nate Begeman3d292062008-04-18 17:17:24 +000042 DoubleWidth = 64;
Eli Friedman61538a72008-05-20 14:21:01 +000043 DoubleAlign = 64;
44 LongDoubleWidth = 64;
45 LongDoubleAlign = 64;
Rafael Espindola6deecb02010-06-04 23:15:27 +000046 LargeArrayMinWidth = 0;
47 LargeArrayAlign = 0;
Eli Friedman2be46072011-10-14 20:59:01 +000048 MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 0;
Anders Carlsson6a3615c2008-10-31 16:05:19 +000049 SizeType = UnsignedLong;
Eli Friedmanf509d732008-11-02 02:43:55 +000050 PtrDiffType = SignedLong;
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +000051 IntMaxType = SignedLongLong;
52 UIntMaxType = UnsignedLongLong;
Chris Lattner6ad474f2009-02-13 22:28:55 +000053 IntPtrType = SignedLong;
Eli Friedmanf509d732008-11-02 02:43:55 +000054 WCharType = SignedInt;
Chris Lattnere64ef802009-10-21 04:59:34 +000055 WIntType = SignedInt;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +000056 Char16Type = UnsignedShort;
57 Char32Type = UnsignedInt;
Eli Friedman3c7b6e42009-07-01 03:36:11 +000058 Int64Type = SignedLongLong;
Edward O'Callaghan9cf910e2009-11-21 00:49:54 +000059 SigAtomicType = SignedInt;
Daniel Dunbarb6830d62010-04-15 15:06:18 +000060 UseBitFieldTypeAlignment = true;
Chad Rosier61a62212011-08-04 01:21:14 +000061 UseZeroLengthBitfieldAlignment = false;
62 ZeroLengthBitfieldBoundary = 0;
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +000063 HalfFormat = &llvm::APFloat::IEEEhalf;
Chris Lattnercd4fc422008-03-08 08:59:43 +000064 FloatFormat = &llvm::APFloat::IEEEsingle;
65 DoubleFormat = &llvm::APFloat::IEEEdouble;
66 LongDoubleFormat = &llvm::APFloat::IEEEdouble;
Eli Friedmaned855cb2008-08-21 00:13:15 +000067 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 +000068 "i64:64:64-f32:32:32-f64:64:64-n32";
Chris Lattner3fdf4672008-10-05 19:22:37 +000069 UserLabelPrefix = "_";
Roman Divackybe4c8702011-02-10 16:52:03 +000070 MCountName = "mcount";
Abramo Bagnara62f940b2011-09-08 14:20:25 +000071 RegParmMax = 0;
72 SSERegParmMax = 0;
Daniel Dunbar613fd672010-05-27 00:35:16 +000073 HasAlignMac68kSupport = false;
Daniel Dunbardacf9dd2010-07-14 23:39:36 +000074
75 // Default to no types using fpret.
76 RealTypeUsesObjCFPRet = 0;
John McCallee79a4c2010-08-21 22:46:04 +000077
Anders Carlssoneea64802011-10-31 16:27:11 +000078 // Default to not using fp2ret for __Complex long double
79 ComplexLongDoubleUsesFP2Ret = false;
80
John McCallee79a4c2010-08-21 22:46:04 +000081 // Default to using the Itanium ABI.
82 CXXABI = CXXABI_Itanium;
Peter Collingbourne207f4d82011-03-18 22:38:29 +000083
84 // Default to an empty address space map.
85 AddrSpaceMap = &DefaultAddrSpaceMap;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +000086
87 // Default to an unknown platform name.
88 PlatformName = "unknown";
89 PlatformMinVersion = VersionTuple();
Chris Lattnercd4fc422008-03-08 08:59:43 +000090}
91
Chris Lattner0eaed122008-03-08 08:24:01 +000092// Out of line virtual dtor for TargetInfo.
93TargetInfo::~TargetInfo() {}
Reid Spencer5f016e22007-07-11 17:01:13 +000094
Chris Lattner2b5abf52009-02-06 05:04:11 +000095/// getTypeName - Return the user string for the specified integer type enum.
96/// For example, SignedShort -> "short".
97const char *TargetInfo::getTypeName(IntType T) {
98 switch (T) {
David Blaikieb219cfc2011-09-23 05:06:16 +000099 default: llvm_unreachable("not an integer!");
Chris Lattner2b5abf52009-02-06 05:04:11 +0000100 case SignedShort: return "short";
101 case UnsignedShort: return "unsigned short";
102 case SignedInt: return "int";
103 case UnsignedInt: return "unsigned int";
104 case SignedLong: return "long int";
105 case UnsignedLong: return "long unsigned int";
106 case SignedLongLong: return "long long int";
107 case UnsignedLongLong: return "long long unsigned int";
108 }
109}
110
Chris Lattnerb304f772009-10-21 06:24:21 +0000111/// getTypeConstantSuffix - Return the constant suffix for the specified
112/// integer type enum. For example, SignedLong -> "L".
113const char *TargetInfo::getTypeConstantSuffix(IntType T) {
114 switch (T) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000115 default: llvm_unreachable("not an integer!");
Chris Lattnerb304f772009-10-21 06:24:21 +0000116 case SignedShort:
117 case SignedInt: return "";
118 case SignedLong: return "L";
119 case SignedLongLong: return "LL";
120 case UnsignedShort:
121 case UnsignedInt: return "U";
122 case UnsignedLong: return "UL";
123 case UnsignedLongLong: return "ULL";
124 }
125}
126
Michael J. Spencer237cf582010-10-18 07:10:59 +0000127/// getTypeWidth - Return the width (in bits) of the specified integer type
Chris Lattnerb304f772009-10-21 06:24:21 +0000128/// enum. For example, SignedInt -> getIntWidth().
129unsigned TargetInfo::getTypeWidth(IntType T) const {
130 switch (T) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000131 default: llvm_unreachable("not an integer!");
Chris Lattner9099e7b2009-11-05 21:21:32 +0000132 case SignedShort:
Chris Lattnerb304f772009-10-21 06:24:21 +0000133 case UnsignedShort: return getShortWidth();
Chris Lattner9099e7b2009-11-05 21:21:32 +0000134 case SignedInt:
Chris Lattnerb304f772009-10-21 06:24:21 +0000135 case UnsignedInt: return getIntWidth();
Chris Lattner9099e7b2009-11-05 21:21:32 +0000136 case SignedLong:
Chris Lattnerb304f772009-10-21 06:24:21 +0000137 case UnsignedLong: return getLongWidth();
Chris Lattner9099e7b2009-11-05 21:21:32 +0000138 case SignedLongLong:
Chris Lattnerb304f772009-10-21 06:24:21 +0000139 case UnsignedLongLong: return getLongLongWidth();
140 };
141}
142
Michael J. Spencer237cf582010-10-18 07:10:59 +0000143/// getTypeAlign - Return the alignment (in bits) of the specified integer type
Chris Lattner9099e7b2009-11-05 21:21:32 +0000144/// enum. For example, SignedInt -> getIntAlign().
145unsigned TargetInfo::getTypeAlign(IntType T) const {
146 switch (T) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000147 default: llvm_unreachable("not an integer!");
Chris Lattner9099e7b2009-11-05 21:21:32 +0000148 case SignedShort:
149 case UnsignedShort: return getShortAlign();
150 case SignedInt:
151 case UnsignedInt: return getIntAlign();
152 case SignedLong:
153 case UnsignedLong: return getLongAlign();
154 case SignedLongLong:
155 case UnsignedLongLong: return getLongLongAlign();
156 };
157}
158
Chris Lattner961f0702009-10-25 22:49:18 +0000159/// isTypeSigned - Return whether an integer types is signed. Returns true if
Chris Lattnerb304f772009-10-21 06:24:21 +0000160/// the type is signed; false otherwise.
Chris Lattner3f59c972010-12-25 23:25:43 +0000161bool TargetInfo::isTypeSigned(IntType T) {
Chris Lattnerb304f772009-10-21 06:24:21 +0000162 switch (T) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000163 default: llvm_unreachable("not an integer!");
Chris Lattnerb304f772009-10-21 06:24:21 +0000164 case SignedShort:
165 case SignedInt:
166 case SignedLong:
Michael J. Spencer237cf582010-10-18 07:10:59 +0000167 case SignedLongLong:
Chris Lattnerb304f772009-10-21 06:24:21 +0000168 return true;
169 case UnsignedShort:
170 case UnsignedInt:
171 case UnsignedLong:
Michael J. Spencer237cf582010-10-18 07:10:59 +0000172 case UnsignedLongLong:
Chris Lattnerb304f772009-10-21 06:24:21 +0000173 return false;
174 };
175}
176
John Thompsona6fda122009-11-05 20:14:16 +0000177/// setForcedLangOptions - Set forced language options.
178/// Apply changes to the target information with respect to certain
179/// language options which change the target configuration.
180void TargetInfo::setForcedLangOptions(LangOptions &Opts) {
Daniel Dunbarfb937b82010-04-15 15:06:22 +0000181 if (Opts.NoBitFieldTypeAlign)
182 UseBitFieldTypeAlignment = false;
183 if (Opts.ShortWChar)
John Thompsona6fda122009-11-05 20:14:16 +0000184 WCharType = UnsignedShort;
John Thompsona6fda122009-11-05 20:14:16 +0000185}
Chris Lattnerb304f772009-10-21 06:24:21 +0000186
Chris Lattner525a0502007-09-22 18:29:59 +0000187//===----------------------------------------------------------------------===//
Chris Lattner525a0502007-09-22 18:29:59 +0000188
Reid Spencer5f016e22007-07-11 17:01:13 +0000189
Chris Lattner5f9e2722011-07-23 10:55:15 +0000190static StringRef removeGCCRegisterPrefix(StringRef Name) {
Anders Carlssonea041752008-02-06 00:11:32 +0000191 if (Name[0] == '%' || Name[0] == '#')
Anders Carlsson83c021c2010-01-30 19:12:25 +0000192 Name = Name.substr(1);
Michael J. Spencer237cf582010-10-18 07:10:59 +0000193
Anders Carlsson83c021c2010-01-30 19:12:25 +0000194 return Name;
Anders Carlssonea041752008-02-06 00:11:32 +0000195}
196
Eric Christopherde31fd72011-06-28 18:20:53 +0000197/// isValidClobber - Returns whether the passed in string is
198/// a valid clobber in an inline asm statement. This is used by
199/// Sema.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000200bool TargetInfo::isValidClobber(StringRef Name) const {
Eric Christopherde31fd72011-06-28 18:20:53 +0000201 return (isValidGCCRegisterName(Name) ||
202 Name == "memory" || Name == "cc");
203}
204
Anders Carlsson3346ae62007-11-24 23:38:12 +0000205/// isValidGCCRegisterName - Returns whether the passed in string
206/// is a valid register name according to GCC. This is used by Sema for
207/// inline asm statements.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000208bool TargetInfo::isValidGCCRegisterName(StringRef Name) const {
Anders Carlsson83c021c2010-01-30 19:12:25 +0000209 if (Name.empty())
210 return false;
Michael J. Spencer237cf582010-10-18 07:10:59 +0000211
Anders Carlsson6fa90862007-11-25 00:25:21 +0000212 const char * const *Names;
213 unsigned NumNames;
Mike Stump1eb44332009-09-09 15:08:12 +0000214
Anders Carlsson6fa90862007-11-25 00:25:21 +0000215 // Get rid of any register prefix.
Anders Carlsson83c021c2010-01-30 19:12:25 +0000216 Name = removeGCCRegisterPrefix(Name);
Anders Carlssonea041752008-02-06 00:11:32 +0000217
Chris Lattner0eaed122008-03-08 08:24:01 +0000218 getGCCRegNames(Names, NumNames);
Mike Stump1eb44332009-09-09 15:08:12 +0000219
Anders Carlsson6fa90862007-11-25 00:25:21 +0000220 // If we have a number it maps to an entry in the register name array.
221 if (isdigit(Name[0])) {
Anders Carlsson83c021c2010-01-30 19:12:25 +0000222 int n;
223 if (!Name.getAsInteger(0, n))
Anders Carlsson6fa90862007-11-25 00:25:21 +0000224 return n >= 0 && (unsigned)n < NumNames;
225 }
226
227 // Check register names.
228 for (unsigned i = 0; i < NumNames; i++) {
Anders Carlsson83c021c2010-01-30 19:12:25 +0000229 if (Name == Names[i])
Anders Carlsson6fa90862007-11-25 00:25:21 +0000230 return true;
231 }
Mike Stump1eb44332009-09-09 15:08:12 +0000232
Eric Christophercfd323d2011-06-21 00:05:20 +0000233 // Check any additional names that we have.
234 const AddlRegName *AddlNames;
235 unsigned NumAddlNames;
236 getGCCAddlRegNames(AddlNames, NumAddlNames);
237 for (unsigned i = 0; i < NumAddlNames; i++)
238 for (unsigned j = 0; j < llvm::array_lengthof(AddlNames[i].Names); j++) {
239 if (!AddlNames[i].Names[j])
240 break;
241 // Make sure the register that the additional name is for is within
242 // the bounds of the register names from above.
243 if (AddlNames[i].Names[j] == Name && AddlNames[i].RegNum < NumNames)
244 return true;
245 }
246
Anders Carlsson6fa90862007-11-25 00:25:21 +0000247 // Now check aliases.
Chris Lattner0eaed122008-03-08 08:24:01 +0000248 const GCCRegAlias *Aliases;
Anders Carlsson6fa90862007-11-25 00:25:21 +0000249 unsigned NumAliases;
Mike Stump1eb44332009-09-09 15:08:12 +0000250
Chris Lattner0eaed122008-03-08 08:24:01 +0000251 getGCCRegAliases(Aliases, NumAliases);
Anders Carlsson6fa90862007-11-25 00:25:21 +0000252 for (unsigned i = 0; i < NumAliases; i++) {
253 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
254 if (!Aliases[i].Aliases[j])
255 break;
Anders Carlsson83c021c2010-01-30 19:12:25 +0000256 if (Aliases[i].Aliases[j] == Name)
Anders Carlsson6fa90862007-11-25 00:25:21 +0000257 return true;
258 }
259 }
Mike Stump1eb44332009-09-09 15:08:12 +0000260
Anders Carlsson3346ae62007-11-24 23:38:12 +0000261 return false;
262}
Anders Carlssond04c6e22007-11-27 04:11:28 +0000263
Chris Lattner5f9e2722011-07-23 10:55:15 +0000264StringRef
265TargetInfo::getNormalizedGCCRegisterName(StringRef Name) const {
Anders Carlssond04c6e22007-11-27 04:11:28 +0000266 assert(isValidGCCRegisterName(Name) && "Invalid register passed in");
Mike Stump1eb44332009-09-09 15:08:12 +0000267
Anders Carlsson83c021c2010-01-30 19:12:25 +0000268 // Get rid of any register prefix.
269 Name = removeGCCRegisterPrefix(Name);
Mike Stump1eb44332009-09-09 15:08:12 +0000270
Anders Carlssond04c6e22007-11-27 04:11:28 +0000271 const char * const *Names;
272 unsigned NumNames;
273
Chris Lattner0eaed122008-03-08 08:24:01 +0000274 getGCCRegNames(Names, NumNames);
Anders Carlssond04c6e22007-11-27 04:11:28 +0000275
276 // First, check if we have a number.
277 if (isdigit(Name[0])) {
Anders Carlsson83c021c2010-01-30 19:12:25 +0000278 int n;
279 if (!Name.getAsInteger(0, n)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000280 assert(n >= 0 && (unsigned)n < NumNames &&
Anders Carlssond04c6e22007-11-27 04:11:28 +0000281 "Out of bounds register number!");
282 return Names[n];
283 }
284 }
Mike Stump1eb44332009-09-09 15:08:12 +0000285
Eric Christophercfd323d2011-06-21 00:05:20 +0000286 // Check any additional names that we have.
287 const AddlRegName *AddlNames;
288 unsigned NumAddlNames;
289 getGCCAddlRegNames(AddlNames, NumAddlNames);
290 for (unsigned i = 0; i < NumAddlNames; i++)
291 for (unsigned j = 0; j < llvm::array_lengthof(AddlNames[i].Names); j++) {
292 if (!AddlNames[i].Names[j])
293 break;
294 // Make sure the register that the additional name is for is within
295 // the bounds of the register names from above.
296 if (AddlNames[i].Names[j] == Name && AddlNames[i].RegNum < NumNames)
297 return Name;
298 }
299
Anders Carlssond04c6e22007-11-27 04:11:28 +0000300 // Now check aliases.
Chris Lattner0eaed122008-03-08 08:24:01 +0000301 const GCCRegAlias *Aliases;
Anders Carlssond04c6e22007-11-27 04:11:28 +0000302 unsigned NumAliases;
Mike Stump1eb44332009-09-09 15:08:12 +0000303
Chris Lattner0eaed122008-03-08 08:24:01 +0000304 getGCCRegAliases(Aliases, NumAliases);
Anders Carlssond04c6e22007-11-27 04:11:28 +0000305 for (unsigned i = 0; i < NumAliases; i++) {
306 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
307 if (!Aliases[i].Aliases[j])
308 break;
Anders Carlsson83c021c2010-01-30 19:12:25 +0000309 if (Aliases[i].Aliases[j] == Name)
Anders Carlssond04c6e22007-11-27 04:11:28 +0000310 return Aliases[i].Register;
311 }
312 }
Mike Stump1eb44332009-09-09 15:08:12 +0000313
Anders Carlssond04c6e22007-11-27 04:11:28 +0000314 return Name;
315}
316
Chris Lattner432c8692009-04-26 17:19:08 +0000317bool TargetInfo::validateOutputConstraint(ConstraintInfo &Info) const {
318 const char *Name = Info.getConstraintStr().c_str();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000319 // An output constraint must start with '=' or '+'
320 if (*Name != '=' && *Name != '+')
321 return false;
322
323 if (*Name == '+')
Chris Lattner44def072009-04-26 07:16:29 +0000324 Info.setIsReadWrite();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000325
326 Name++;
327 while (*Name) {
328 switch (*Name) {
329 default:
Chris Lattner44def072009-04-26 07:16:29 +0000330 if (!validateAsmConstraint(Name, Info)) {
Ted Kremenek5ab201a2008-04-24 16:36:38 +0000331 // FIXME: We temporarily return false
Anders Carlssond04c6e22007-11-27 04:11:28 +0000332 // so we can add more constraints as we hit it.
333 // Eventually, an unknown constraint should just be treated as 'g'.
Ted Kremenek5ab201a2008-04-24 16:36:38 +0000334 return false;
Anders Carlssond04c6e22007-11-27 04:11:28 +0000335 }
336 case '&': // early clobber.
337 break;
Chris Lattner40539832009-10-13 04:32:07 +0000338 case '%': // commutative.
339 // FIXME: Check that there is a another register after this one.
340 break;
Anders Carlssond04c6e22007-11-27 04:11:28 +0000341 case 'r': // general register.
Chris Lattner44def072009-04-26 07:16:29 +0000342 Info.setAllowsRegister();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000343 break;
344 case 'm': // memory operand.
Dale Johannesen0f048a42010-09-07 18:40:41 +0000345 case 'o': // offsetable memory operand.
346 case 'V': // non-offsetable memory operand.
347 case '<': // autodecrement memory operand.
348 case '>': // autoincrement memory operand.
Chris Lattner44def072009-04-26 07:16:29 +0000349 Info.setAllowsMemory();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000350 break;
351 case 'g': // general register, memory operand or immediate integer.
Anders Carlsson775841f2009-01-18 02:12:04 +0000352 case 'X': // any operand.
Chris Lattner44def072009-04-26 07:16:29 +0000353 Info.setAllowsRegister();
354 Info.setAllowsMemory();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000355 break;
John Thompsonef44e112010-08-10 19:20:14 +0000356 case ',': // multiple alternative constraint. Pass it.
John Thompson6f158032010-08-11 00:58:20 +0000357 // Handle additional optional '=' or '+' modifiers.
John Thompson2f474ea2010-09-18 01:15:13 +0000358 if (Name[1] == '=' || Name[1] == '+')
John Thompson6f158032010-08-11 00:58:20 +0000359 Name++;
John Thompsonef44e112010-08-10 19:20:14 +0000360 break;
361 case '?': // Disparage slightly code.
Dale Johannesen0f048a42010-09-07 18:40:41 +0000362 case '!': // Disparage severely.
John Thompsonef44e112010-08-10 19:20:14 +0000363 break; // Pass them.
Anders Carlssond04c6e22007-11-27 04:11:28 +0000364 }
Mike Stump1eb44332009-09-09 15:08:12 +0000365
Anders Carlssond04c6e22007-11-27 04:11:28 +0000366 Name++;
367 }
Mike Stump1eb44332009-09-09 15:08:12 +0000368
Anders Carlssond04c6e22007-11-27 04:11:28 +0000369 return true;
370}
371
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000372bool TargetInfo::resolveSymbolicName(const char *&Name,
Chris Lattner2819fa82009-04-26 17:57:12 +0000373 ConstraintInfo *OutputConstraints,
374 unsigned NumOutputs,
Chris Lattner44def072009-04-26 07:16:29 +0000375 unsigned &Index) const {
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000376 assert(*Name == '[' && "Symbolic name did not start with '['");
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000377 Name++;
378 const char *Start = Name;
379 while (*Name && *Name != ']')
380 Name++;
Mike Stump1eb44332009-09-09 15:08:12 +0000381
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000382 if (!*Name) {
383 // Missing ']'
384 return false;
385 }
Mike Stump1eb44332009-09-09 15:08:12 +0000386
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000387 std::string SymbolicName(Start, Name - Start);
Mike Stump1eb44332009-09-09 15:08:12 +0000388
Chris Lattner2819fa82009-04-26 17:57:12 +0000389 for (Index = 0; Index != NumOutputs; ++Index)
390 if (SymbolicName == OutputConstraints[Index].getName())
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000391 return true;
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000392
393 return false;
394}
395
Chris Lattner2819fa82009-04-26 17:57:12 +0000396bool TargetInfo::validateInputConstraint(ConstraintInfo *OutputConstraints,
397 unsigned NumOutputs,
Chris Lattner44def072009-04-26 07:16:29 +0000398 ConstraintInfo &Info) const {
Chris Lattner432c8692009-04-26 17:19:08 +0000399 const char *Name = Info.ConstraintStr.c_str();
400
Anders Carlssond04c6e22007-11-27 04:11:28 +0000401 while (*Name) {
402 switch (*Name) {
403 default:
404 // Check if we have a matching constraint
405 if (*Name >= '0' && *Name <= '9') {
406 unsigned i = *Name - '0';
Mike Stump1eb44332009-09-09 15:08:12 +0000407
Anders Carlssond04c6e22007-11-27 04:11:28 +0000408 // Check if matching constraint is out of bounds.
409 if (i >= NumOutputs)
410 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000411
Anders Carlsson86eda392010-11-03 02:22:29 +0000412 // A number must refer to an output only operand.
413 if (OutputConstraints[i].isReadWrite())
414 return false;
415
Anders Carlsson79ca1ee2010-11-03 02:54:51 +0000416 // If the constraint is already tied, it must be tied to the
417 // same operand referenced to by the number.
418 if (Info.hasTiedOperand() && Info.getTiedOperand() != i)
419 return false;
420
Mike Stump1eb44332009-09-09 15:08:12 +0000421 // The constraint should have the same info as the respective
Anders Carlsson03eb5432009-01-27 20:38:24 +0000422 // output constraint.
Chris Lattnerd6887612009-04-26 18:05:25 +0000423 Info.setTiedOperand(i, OutputConstraints[i]);
Chris Lattner44def072009-04-26 07:16:29 +0000424 } else if (!validateAsmConstraint(Name, Info)) {
Daniel Dunbar302684c2008-08-25 09:46:27 +0000425 // FIXME: This error return is in place temporarily so we can
426 // add more constraints as we hit it. Eventually, an unknown
427 // constraint should just be treated as 'g'.
428 return false;
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000429 }
430 break;
431 case '[': {
432 unsigned Index = 0;
Chris Lattner2819fa82009-04-26 17:57:12 +0000433 if (!resolveSymbolicName(Name, OutputConstraints, NumOutputs, Index))
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000434 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000435
Anders Carlsson79ca1ee2010-11-03 02:54:51 +0000436 // If the constraint is already tied, it must be tied to the
437 // same operand referenced to by the number.
438 if (Info.hasTiedOperand() && Info.getTiedOperand() != Index)
439 return false;
440
Eli Friedman0eff4e72010-08-11 23:03:37 +0000441 Info.setTiedOperand(Index, OutputConstraints[Index]);
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000442 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000443 }
Anders Carlsson44fe49c2007-12-08 19:32:57 +0000444 case '%': // commutative
445 // FIXME: Fail if % is used with the last operand.
446 break;
Anders Carlssond04c6e22007-11-27 04:11:28 +0000447 case 'i': // immediate integer.
Anders Carlssonefdfa772008-03-09 06:02:02 +0000448 case 'n': // immediate integer with a known value.
Anders Carlssond04c6e22007-11-27 04:11:28 +0000449 break;
Chris Lattner1d138792009-05-06 04:33:31 +0000450 case 'I': // Various constant constraints with target-specific meanings.
451 case 'J':
452 case 'K':
453 case 'L':
454 case 'M':
455 case 'N':
456 case 'O':
457 case 'P':
458 break;
Anders Carlssond04c6e22007-11-27 04:11:28 +0000459 case 'r': // general register.
Chris Lattner44def072009-04-26 07:16:29 +0000460 Info.setAllowsRegister();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000461 break;
462 case 'm': // memory operand.
Dale Johannesen0f048a42010-09-07 18:40:41 +0000463 case 'o': // offsettable memory operand.
464 case 'V': // non-offsettable memory operand.
465 case '<': // autodecrement memory operand.
466 case '>': // autoincrement memory operand.
Chris Lattner44def072009-04-26 07:16:29 +0000467 Info.setAllowsMemory();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000468 break;
469 case 'g': // general register, memory operand or immediate integer.
Anders Carlsson775841f2009-01-18 02:12:04 +0000470 case 'X': // any operand.
Chris Lattner44def072009-04-26 07:16:29 +0000471 Info.setAllowsRegister();
472 Info.setAllowsMemory();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000473 break;
John Thompson7ccc58f2010-09-21 22:04:54 +0000474 case 'E': // immediate floating point.
475 case 'F': // immediate floating point.
476 case 'p': // address operand.
477 break;
John Thompsonef44e112010-08-10 19:20:14 +0000478 case ',': // multiple alternative constraint. Ignore comma.
479 break;
480 case '?': // Disparage slightly code.
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000481 case '!': // Disparage severely.
John Thompsonef44e112010-08-10 19:20:14 +0000482 break; // Pass them.
Anders Carlssond04c6e22007-11-27 04:11:28 +0000483 }
Mike Stump1eb44332009-09-09 15:08:12 +0000484
Anders Carlssond04c6e22007-11-27 04:11:28 +0000485 Name++;
486 }
Mike Stump1eb44332009-09-09 15:08:12 +0000487
Anders Carlssond04c6e22007-11-27 04:11:28 +0000488 return true;
489}