blob: 8c49486b0dc3574490fc229bb6d973316760a1d3 [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;
Fariborz Jahanian93a49942012-04-16 21:03:30 +000061 UseSignedCharForObjCBool = true;
Daniel Dunbarb6830d62010-04-15 15:06:18 +000062 UseBitFieldTypeAlignment = true;
Chad Rosier61a62212011-08-04 01:21:14 +000063 UseZeroLengthBitfieldAlignment = false;
64 ZeroLengthBitfieldBoundary = 0;
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +000065 HalfFormat = &llvm::APFloat::IEEEhalf;
Chris Lattnercd4fc422008-03-08 08:59:43 +000066 FloatFormat = &llvm::APFloat::IEEEsingle;
67 DoubleFormat = &llvm::APFloat::IEEEdouble;
68 LongDoubleFormat = &llvm::APFloat::IEEEdouble;
Eli Friedmaned855cb2008-08-21 00:13:15 +000069 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 +000070 "i64:64:64-f32:32:32-f64:64:64-n32";
Chris Lattner3fdf4672008-10-05 19:22:37 +000071 UserLabelPrefix = "_";
Roman Divackybe4c8702011-02-10 16:52:03 +000072 MCountName = "mcount";
Abramo Bagnara62f940b2011-09-08 14:20:25 +000073 RegParmMax = 0;
74 SSERegParmMax = 0;
Daniel Dunbar613fd672010-05-27 00:35:16 +000075 HasAlignMac68kSupport = false;
Daniel Dunbardacf9dd2010-07-14 23:39:36 +000076
77 // Default to no types using fpret.
78 RealTypeUsesObjCFPRet = 0;
John McCallee79a4c2010-08-21 22:46:04 +000079
Anders Carlssoneea64802011-10-31 16:27:11 +000080 // Default to not using fp2ret for __Complex long double
81 ComplexLongDoubleUsesFP2Ret = false;
82
John McCallee79a4c2010-08-21 22:46:04 +000083 // Default to using the Itanium ABI.
84 CXXABI = CXXABI_Itanium;
Peter Collingbourne207f4d82011-03-18 22:38:29 +000085
86 // Default to an empty address space map.
87 AddrSpaceMap = &DefaultAddrSpaceMap;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +000088
89 // Default to an unknown platform name.
90 PlatformName = "unknown";
91 PlatformMinVersion = VersionTuple();
Chris Lattnercd4fc422008-03-08 08:59:43 +000092}
93
Chris Lattner0eaed122008-03-08 08:24:01 +000094// Out of line virtual dtor for TargetInfo.
95TargetInfo::~TargetInfo() {}
Reid Spencer5f016e22007-07-11 17:01:13 +000096
Chris Lattner2b5abf52009-02-06 05:04:11 +000097/// getTypeName - Return the user string for the specified integer type enum.
98/// For example, SignedShort -> "short".
99const char *TargetInfo::getTypeName(IntType T) {
100 switch (T) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000101 default: llvm_unreachable("not an integer!");
Chris Lattner2b5abf52009-02-06 05:04:11 +0000102 case SignedShort: return "short";
103 case UnsignedShort: return "unsigned short";
104 case SignedInt: return "int";
105 case UnsignedInt: return "unsigned int";
106 case SignedLong: return "long int";
107 case UnsignedLong: return "long unsigned int";
108 case SignedLongLong: return "long long int";
109 case UnsignedLongLong: return "long long unsigned int";
110 }
111}
112
Chris Lattnerb304f772009-10-21 06:24:21 +0000113/// getTypeConstantSuffix - Return the constant suffix for the specified
114/// integer type enum. For example, SignedLong -> "L".
115const char *TargetInfo::getTypeConstantSuffix(IntType T) {
116 switch (T) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000117 default: llvm_unreachable("not an integer!");
Chris Lattnerb304f772009-10-21 06:24:21 +0000118 case SignedShort:
119 case SignedInt: return "";
120 case SignedLong: return "L";
121 case SignedLongLong: return "LL";
122 case UnsignedShort:
123 case UnsignedInt: return "U";
124 case UnsignedLong: return "UL";
125 case UnsignedLongLong: return "ULL";
126 }
127}
128
Michael J. Spencer237cf582010-10-18 07:10:59 +0000129/// getTypeWidth - Return the width (in bits) of the specified integer type
Chris Lattnerb304f772009-10-21 06:24:21 +0000130/// enum. For example, SignedInt -> getIntWidth().
131unsigned TargetInfo::getTypeWidth(IntType T) const {
132 switch (T) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000133 default: llvm_unreachable("not an integer!");
Chris Lattner9099e7b2009-11-05 21:21:32 +0000134 case SignedShort:
Chris Lattnerb304f772009-10-21 06:24:21 +0000135 case UnsignedShort: return getShortWidth();
Chris Lattner9099e7b2009-11-05 21:21:32 +0000136 case SignedInt:
Chris Lattnerb304f772009-10-21 06:24:21 +0000137 case UnsignedInt: return getIntWidth();
Chris Lattner9099e7b2009-11-05 21:21:32 +0000138 case SignedLong:
Chris Lattnerb304f772009-10-21 06:24:21 +0000139 case UnsignedLong: return getLongWidth();
Chris Lattner9099e7b2009-11-05 21:21:32 +0000140 case SignedLongLong:
Chris Lattnerb304f772009-10-21 06:24:21 +0000141 case UnsignedLongLong: return getLongLongWidth();
142 };
143}
144
Michael J. Spencer237cf582010-10-18 07:10:59 +0000145/// getTypeAlign - Return the alignment (in bits) of the specified integer type
Chris Lattner9099e7b2009-11-05 21:21:32 +0000146/// enum. For example, SignedInt -> getIntAlign().
147unsigned TargetInfo::getTypeAlign(IntType T) const {
148 switch (T) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000149 default: llvm_unreachable("not an integer!");
Chris Lattner9099e7b2009-11-05 21:21:32 +0000150 case SignedShort:
151 case UnsignedShort: return getShortAlign();
152 case SignedInt:
153 case UnsignedInt: return getIntAlign();
154 case SignedLong:
155 case UnsignedLong: return getLongAlign();
156 case SignedLongLong:
157 case UnsignedLongLong: return getLongLongAlign();
158 };
159}
160
Chris Lattner961f0702009-10-25 22:49:18 +0000161/// isTypeSigned - Return whether an integer types is signed. Returns true if
Chris Lattnerb304f772009-10-21 06:24:21 +0000162/// the type is signed; false otherwise.
Chris Lattner3f59c972010-12-25 23:25:43 +0000163bool TargetInfo::isTypeSigned(IntType T) {
Chris Lattnerb304f772009-10-21 06:24:21 +0000164 switch (T) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000165 default: llvm_unreachable("not an integer!");
Chris Lattnerb304f772009-10-21 06:24:21 +0000166 case SignedShort:
167 case SignedInt:
168 case SignedLong:
Michael J. Spencer237cf582010-10-18 07:10:59 +0000169 case SignedLongLong:
Chris Lattnerb304f772009-10-21 06:24:21 +0000170 return true;
171 case UnsignedShort:
172 case UnsignedInt:
173 case UnsignedLong:
Michael J. Spencer237cf582010-10-18 07:10:59 +0000174 case UnsignedLongLong:
Chris Lattnerb304f772009-10-21 06:24:21 +0000175 return false;
176 };
177}
178
John Thompsona6fda122009-11-05 20:14:16 +0000179/// setForcedLangOptions - Set forced language options.
180/// Apply changes to the target information with respect to certain
181/// language options which change the target configuration.
182void TargetInfo::setForcedLangOptions(LangOptions &Opts) {
Daniel Dunbarfb937b82010-04-15 15:06:22 +0000183 if (Opts.NoBitFieldTypeAlign)
184 UseBitFieldTypeAlignment = false;
185 if (Opts.ShortWChar)
John Thompsona6fda122009-11-05 20:14:16 +0000186 WCharType = UnsignedShort;
John Thompsona6fda122009-11-05 20:14:16 +0000187}
Chris Lattnerb304f772009-10-21 06:24:21 +0000188
Chris Lattner525a0502007-09-22 18:29:59 +0000189//===----------------------------------------------------------------------===//
Chris Lattner525a0502007-09-22 18:29:59 +0000190
Reid Spencer5f016e22007-07-11 17:01:13 +0000191
Chris Lattner5f9e2722011-07-23 10:55:15 +0000192static StringRef removeGCCRegisterPrefix(StringRef Name) {
Anders Carlssonea041752008-02-06 00:11:32 +0000193 if (Name[0] == '%' || Name[0] == '#')
Anders Carlsson83c021c2010-01-30 19:12:25 +0000194 Name = Name.substr(1);
Michael J. Spencer237cf582010-10-18 07:10:59 +0000195
Anders Carlsson83c021c2010-01-30 19:12:25 +0000196 return Name;
Anders Carlssonea041752008-02-06 00:11:32 +0000197}
198
Eric Christopherde31fd72011-06-28 18:20:53 +0000199/// isValidClobber - Returns whether the passed in string is
200/// a valid clobber in an inline asm statement. This is used by
201/// Sema.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000202bool TargetInfo::isValidClobber(StringRef Name) const {
Eric Christopherde31fd72011-06-28 18:20:53 +0000203 return (isValidGCCRegisterName(Name) ||
204 Name == "memory" || Name == "cc");
205}
206
Anders Carlsson3346ae62007-11-24 23:38:12 +0000207/// isValidGCCRegisterName - Returns whether the passed in string
208/// is a valid register name according to GCC. This is used by Sema for
209/// inline asm statements.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000210bool TargetInfo::isValidGCCRegisterName(StringRef Name) const {
Anders Carlsson83c021c2010-01-30 19:12:25 +0000211 if (Name.empty())
212 return false;
Michael J. Spencer237cf582010-10-18 07:10:59 +0000213
Anders Carlsson6fa90862007-11-25 00:25:21 +0000214 const char * const *Names;
215 unsigned NumNames;
Mike Stump1eb44332009-09-09 15:08:12 +0000216
Anders Carlsson6fa90862007-11-25 00:25:21 +0000217 // Get rid of any register prefix.
Anders Carlsson83c021c2010-01-30 19:12:25 +0000218 Name = removeGCCRegisterPrefix(Name);
Anders Carlssonea041752008-02-06 00:11:32 +0000219
Chris Lattner0eaed122008-03-08 08:24:01 +0000220 getGCCRegNames(Names, NumNames);
Mike Stump1eb44332009-09-09 15:08:12 +0000221
Anders Carlsson6fa90862007-11-25 00:25:21 +0000222 // If we have a number it maps to an entry in the register name array.
223 if (isdigit(Name[0])) {
Anders Carlsson83c021c2010-01-30 19:12:25 +0000224 int n;
225 if (!Name.getAsInteger(0, n))
Anders Carlsson6fa90862007-11-25 00:25:21 +0000226 return n >= 0 && (unsigned)n < NumNames;
227 }
228
229 // Check register names.
230 for (unsigned i = 0; i < NumNames; i++) {
Anders Carlsson83c021c2010-01-30 19:12:25 +0000231 if (Name == Names[i])
Anders Carlsson6fa90862007-11-25 00:25:21 +0000232 return true;
233 }
Mike Stump1eb44332009-09-09 15:08:12 +0000234
Eric Christophercfd323d2011-06-21 00:05:20 +0000235 // Check any additional names that we have.
236 const AddlRegName *AddlNames;
237 unsigned NumAddlNames;
238 getGCCAddlRegNames(AddlNames, NumAddlNames);
239 for (unsigned i = 0; i < NumAddlNames; i++)
240 for (unsigned j = 0; j < llvm::array_lengthof(AddlNames[i].Names); j++) {
241 if (!AddlNames[i].Names[j])
242 break;
243 // Make sure the register that the additional name is for is within
244 // the bounds of the register names from above.
245 if (AddlNames[i].Names[j] == Name && AddlNames[i].RegNum < NumNames)
246 return true;
247 }
248
Anders Carlsson6fa90862007-11-25 00:25:21 +0000249 // Now check aliases.
Chris Lattner0eaed122008-03-08 08:24:01 +0000250 const GCCRegAlias *Aliases;
Anders Carlsson6fa90862007-11-25 00:25:21 +0000251 unsigned NumAliases;
Mike Stump1eb44332009-09-09 15:08:12 +0000252
Chris Lattner0eaed122008-03-08 08:24:01 +0000253 getGCCRegAliases(Aliases, NumAliases);
Anders Carlsson6fa90862007-11-25 00:25:21 +0000254 for (unsigned i = 0; i < NumAliases; i++) {
255 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
256 if (!Aliases[i].Aliases[j])
257 break;
Anders Carlsson83c021c2010-01-30 19:12:25 +0000258 if (Aliases[i].Aliases[j] == Name)
Anders Carlsson6fa90862007-11-25 00:25:21 +0000259 return true;
260 }
261 }
Mike Stump1eb44332009-09-09 15:08:12 +0000262
Anders Carlsson3346ae62007-11-24 23:38:12 +0000263 return false;
264}
Anders Carlssond04c6e22007-11-27 04:11:28 +0000265
Chris Lattner5f9e2722011-07-23 10:55:15 +0000266StringRef
267TargetInfo::getNormalizedGCCRegisterName(StringRef Name) const {
Anders Carlssond04c6e22007-11-27 04:11:28 +0000268 assert(isValidGCCRegisterName(Name) && "Invalid register passed in");
Mike Stump1eb44332009-09-09 15:08:12 +0000269
Anders Carlsson83c021c2010-01-30 19:12:25 +0000270 // Get rid of any register prefix.
271 Name = removeGCCRegisterPrefix(Name);
Mike Stump1eb44332009-09-09 15:08:12 +0000272
Anders Carlssond04c6e22007-11-27 04:11:28 +0000273 const char * const *Names;
274 unsigned NumNames;
275
Chris Lattner0eaed122008-03-08 08:24:01 +0000276 getGCCRegNames(Names, NumNames);
Anders Carlssond04c6e22007-11-27 04:11:28 +0000277
278 // First, check if we have a number.
279 if (isdigit(Name[0])) {
Anders Carlsson83c021c2010-01-30 19:12:25 +0000280 int n;
281 if (!Name.getAsInteger(0, n)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000282 assert(n >= 0 && (unsigned)n < NumNames &&
Anders Carlssond04c6e22007-11-27 04:11:28 +0000283 "Out of bounds register number!");
284 return Names[n];
285 }
286 }
Mike Stump1eb44332009-09-09 15:08:12 +0000287
Eric Christophercfd323d2011-06-21 00:05:20 +0000288 // Check any additional names that we have.
289 const AddlRegName *AddlNames;
290 unsigned NumAddlNames;
291 getGCCAddlRegNames(AddlNames, NumAddlNames);
292 for (unsigned i = 0; i < NumAddlNames; i++)
293 for (unsigned j = 0; j < llvm::array_lengthof(AddlNames[i].Names); j++) {
294 if (!AddlNames[i].Names[j])
295 break;
296 // Make sure the register that the additional name is for is within
297 // the bounds of the register names from above.
298 if (AddlNames[i].Names[j] == Name && AddlNames[i].RegNum < NumNames)
299 return Name;
300 }
301
Anders Carlssond04c6e22007-11-27 04:11:28 +0000302 // Now check aliases.
Chris Lattner0eaed122008-03-08 08:24:01 +0000303 const GCCRegAlias *Aliases;
Anders Carlssond04c6e22007-11-27 04:11:28 +0000304 unsigned NumAliases;
Mike Stump1eb44332009-09-09 15:08:12 +0000305
Chris Lattner0eaed122008-03-08 08:24:01 +0000306 getGCCRegAliases(Aliases, NumAliases);
Anders Carlssond04c6e22007-11-27 04:11:28 +0000307 for (unsigned i = 0; i < NumAliases; i++) {
308 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
309 if (!Aliases[i].Aliases[j])
310 break;
Anders Carlsson83c021c2010-01-30 19:12:25 +0000311 if (Aliases[i].Aliases[j] == Name)
Anders Carlssond04c6e22007-11-27 04:11:28 +0000312 return Aliases[i].Register;
313 }
314 }
Mike Stump1eb44332009-09-09 15:08:12 +0000315
Anders Carlssond04c6e22007-11-27 04:11:28 +0000316 return Name;
317}
318
Chris Lattner432c8692009-04-26 17:19:08 +0000319bool TargetInfo::validateOutputConstraint(ConstraintInfo &Info) const {
320 const char *Name = Info.getConstraintStr().c_str();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000321 // An output constraint must start with '=' or '+'
322 if (*Name != '=' && *Name != '+')
323 return false;
324
325 if (*Name == '+')
Chris Lattner44def072009-04-26 07:16:29 +0000326 Info.setIsReadWrite();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000327
328 Name++;
329 while (*Name) {
330 switch (*Name) {
331 default:
Chris Lattner44def072009-04-26 07:16:29 +0000332 if (!validateAsmConstraint(Name, Info)) {
Ted Kremenek5ab201a2008-04-24 16:36:38 +0000333 // FIXME: We temporarily return false
Anders Carlssond04c6e22007-11-27 04:11:28 +0000334 // so we can add more constraints as we hit it.
335 // Eventually, an unknown constraint should just be treated as 'g'.
Ted Kremenek5ab201a2008-04-24 16:36:38 +0000336 return false;
Anders Carlssond04c6e22007-11-27 04:11:28 +0000337 }
338 case '&': // early clobber.
339 break;
Chris Lattner40539832009-10-13 04:32:07 +0000340 case '%': // commutative.
341 // FIXME: Check that there is a another register after this one.
342 break;
Anders Carlssond04c6e22007-11-27 04:11:28 +0000343 case 'r': // general register.
Chris Lattner44def072009-04-26 07:16:29 +0000344 Info.setAllowsRegister();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000345 break;
346 case 'm': // memory operand.
Dale Johannesen0f048a42010-09-07 18:40:41 +0000347 case 'o': // offsetable memory operand.
348 case 'V': // non-offsetable memory operand.
349 case '<': // autodecrement memory operand.
350 case '>': // autoincrement memory operand.
Chris Lattner44def072009-04-26 07:16:29 +0000351 Info.setAllowsMemory();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000352 break;
353 case 'g': // general register, memory operand or immediate integer.
Anders Carlsson775841f2009-01-18 02:12:04 +0000354 case 'X': // any operand.
Chris Lattner44def072009-04-26 07:16:29 +0000355 Info.setAllowsRegister();
356 Info.setAllowsMemory();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000357 break;
John Thompsonef44e112010-08-10 19:20:14 +0000358 case ',': // multiple alternative constraint. Pass it.
John Thompson6f158032010-08-11 00:58:20 +0000359 // Handle additional optional '=' or '+' modifiers.
John Thompson2f474ea2010-09-18 01:15:13 +0000360 if (Name[1] == '=' || Name[1] == '+')
John Thompson6f158032010-08-11 00:58:20 +0000361 Name++;
John Thompsonef44e112010-08-10 19:20:14 +0000362 break;
363 case '?': // Disparage slightly code.
Dale Johannesen0f048a42010-09-07 18:40:41 +0000364 case '!': // Disparage severely.
John Thompsonef44e112010-08-10 19:20:14 +0000365 break; // Pass them.
Anders Carlssond04c6e22007-11-27 04:11:28 +0000366 }
Mike Stump1eb44332009-09-09 15:08:12 +0000367
Anders Carlssond04c6e22007-11-27 04:11:28 +0000368 Name++;
369 }
Mike Stump1eb44332009-09-09 15:08:12 +0000370
Anders Carlssond04c6e22007-11-27 04:11:28 +0000371 return true;
372}
373
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000374bool TargetInfo::resolveSymbolicName(const char *&Name,
Chris Lattner2819fa82009-04-26 17:57:12 +0000375 ConstraintInfo *OutputConstraints,
376 unsigned NumOutputs,
Chris Lattner44def072009-04-26 07:16:29 +0000377 unsigned &Index) const {
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000378 assert(*Name == '[' && "Symbolic name did not start with '['");
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000379 Name++;
380 const char *Start = Name;
381 while (*Name && *Name != ']')
382 Name++;
Mike Stump1eb44332009-09-09 15:08:12 +0000383
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000384 if (!*Name) {
385 // Missing ']'
386 return false;
387 }
Mike Stump1eb44332009-09-09 15:08:12 +0000388
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000389 std::string SymbolicName(Start, Name - Start);
Mike Stump1eb44332009-09-09 15:08:12 +0000390
Chris Lattner2819fa82009-04-26 17:57:12 +0000391 for (Index = 0; Index != NumOutputs; ++Index)
392 if (SymbolicName == OutputConstraints[Index].getName())
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000393 return true;
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000394
395 return false;
396}
397
Chris Lattner2819fa82009-04-26 17:57:12 +0000398bool TargetInfo::validateInputConstraint(ConstraintInfo *OutputConstraints,
399 unsigned NumOutputs,
Chris Lattner44def072009-04-26 07:16:29 +0000400 ConstraintInfo &Info) const {
Chris Lattner432c8692009-04-26 17:19:08 +0000401 const char *Name = Info.ConstraintStr.c_str();
402
Anders Carlssond04c6e22007-11-27 04:11:28 +0000403 while (*Name) {
404 switch (*Name) {
405 default:
406 // Check if we have a matching constraint
407 if (*Name >= '0' && *Name <= '9') {
408 unsigned i = *Name - '0';
Mike Stump1eb44332009-09-09 15:08:12 +0000409
Anders Carlssond04c6e22007-11-27 04:11:28 +0000410 // Check if matching constraint is out of bounds.
411 if (i >= NumOutputs)
412 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000413
Anders Carlsson86eda392010-11-03 02:22:29 +0000414 // A number must refer to an output only operand.
415 if (OutputConstraints[i].isReadWrite())
416 return false;
417
Anders Carlsson79ca1ee2010-11-03 02:54:51 +0000418 // If the constraint is already tied, it must be tied to the
419 // same operand referenced to by the number.
420 if (Info.hasTiedOperand() && Info.getTiedOperand() != i)
421 return false;
422
Mike Stump1eb44332009-09-09 15:08:12 +0000423 // The constraint should have the same info as the respective
Anders Carlsson03eb5432009-01-27 20:38:24 +0000424 // output constraint.
Chris Lattnerd6887612009-04-26 18:05:25 +0000425 Info.setTiedOperand(i, OutputConstraints[i]);
Chris Lattner44def072009-04-26 07:16:29 +0000426 } else if (!validateAsmConstraint(Name, Info)) {
Daniel Dunbar302684c2008-08-25 09:46:27 +0000427 // FIXME: This error return is in place temporarily so we can
428 // add more constraints as we hit it. Eventually, an unknown
429 // constraint should just be treated as 'g'.
430 return false;
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000431 }
432 break;
433 case '[': {
434 unsigned Index = 0;
Chris Lattner2819fa82009-04-26 17:57:12 +0000435 if (!resolveSymbolicName(Name, OutputConstraints, NumOutputs, Index))
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000436 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000437
Anders Carlsson79ca1ee2010-11-03 02:54:51 +0000438 // If the constraint is already tied, it must be tied to the
439 // same operand referenced to by the number.
440 if (Info.hasTiedOperand() && Info.getTiedOperand() != Index)
441 return false;
442
Eli Friedman0eff4e72010-08-11 23:03:37 +0000443 Info.setTiedOperand(Index, OutputConstraints[Index]);
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000444 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000445 }
Anders Carlsson44fe49c2007-12-08 19:32:57 +0000446 case '%': // commutative
447 // FIXME: Fail if % is used with the last operand.
448 break;
Anders Carlssond04c6e22007-11-27 04:11:28 +0000449 case 'i': // immediate integer.
Anders Carlssonefdfa772008-03-09 06:02:02 +0000450 case 'n': // immediate integer with a known value.
Anders Carlssond04c6e22007-11-27 04:11:28 +0000451 break;
Chris Lattner1d138792009-05-06 04:33:31 +0000452 case 'I': // Various constant constraints with target-specific meanings.
453 case 'J':
454 case 'K':
455 case 'L':
456 case 'M':
457 case 'N':
458 case 'O':
459 case 'P':
460 break;
Anders Carlssond04c6e22007-11-27 04:11:28 +0000461 case 'r': // general register.
Chris Lattner44def072009-04-26 07:16:29 +0000462 Info.setAllowsRegister();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000463 break;
464 case 'm': // memory operand.
Dale Johannesen0f048a42010-09-07 18:40:41 +0000465 case 'o': // offsettable memory operand.
466 case 'V': // non-offsettable memory operand.
467 case '<': // autodecrement memory operand.
468 case '>': // autoincrement memory operand.
Chris Lattner44def072009-04-26 07:16:29 +0000469 Info.setAllowsMemory();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000470 break;
471 case 'g': // general register, memory operand or immediate integer.
Anders Carlsson775841f2009-01-18 02:12:04 +0000472 case 'X': // any operand.
Chris Lattner44def072009-04-26 07:16:29 +0000473 Info.setAllowsRegister();
474 Info.setAllowsMemory();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000475 break;
John Thompson7ccc58f2010-09-21 22:04:54 +0000476 case 'E': // immediate floating point.
477 case 'F': // immediate floating point.
478 case 'p': // address operand.
479 break;
John Thompsonef44e112010-08-10 19:20:14 +0000480 case ',': // multiple alternative constraint. Ignore comma.
481 break;
482 case '?': // Disparage slightly code.
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000483 case '!': // Disparage severely.
John Thompsonef44e112010-08-10 19:20:14 +0000484 break; // Pass them.
Anders Carlssond04c6e22007-11-27 04:11:28 +0000485 }
Mike Stump1eb44332009-09-09 15:08:12 +0000486
Anders Carlssond04c6e22007-11-27 04:11:28 +0000487 Name++;
488 }
Mike Stump1eb44332009-09-09 15:08:12 +0000489
Anders Carlssond04c6e22007-11-27 04:11:28 +0000490 return true;
491}