blob: 694121545ad399921505a6894e817ca4f79614e0 [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;
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +000037 HalfWidth = 16;
38 HalfAlign = 16;
Eli Friedman61538a72008-05-20 14:21:01 +000039 FloatWidth = 32;
40 FloatAlign = 32;
Nate Begeman3d292062008-04-18 17:17:24 +000041 DoubleWidth = 64;
Eli Friedman61538a72008-05-20 14:21:01 +000042 DoubleAlign = 64;
43 LongDoubleWidth = 64;
44 LongDoubleAlign = 64;
Rafael Espindola6deecb02010-06-04 23:15:27 +000045 LargeArrayMinWidth = 0;
46 LargeArrayAlign = 0;
Eli Friedman2be46072011-10-14 20:59:01 +000047 MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 0;
Anders Carlsson6a3615c2008-10-31 16:05:19 +000048 SizeType = UnsignedLong;
Eli Friedmanf509d732008-11-02 02:43:55 +000049 PtrDiffType = SignedLong;
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +000050 IntMaxType = SignedLongLong;
51 UIntMaxType = UnsignedLongLong;
Chris Lattner6ad474f2009-02-13 22:28:55 +000052 IntPtrType = SignedLong;
Eli Friedmanf509d732008-11-02 02:43:55 +000053 WCharType = SignedInt;
Chris Lattnere64ef802009-10-21 04:59:34 +000054 WIntType = SignedInt;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +000055 Char16Type = UnsignedShort;
56 Char32Type = UnsignedInt;
Eli Friedman3c7b6e42009-07-01 03:36:11 +000057 Int64Type = SignedLongLong;
Edward O'Callaghan9cf910e2009-11-21 00:49:54 +000058 SigAtomicType = SignedInt;
Daniel Dunbarb6830d62010-04-15 15:06:18 +000059 UseBitFieldTypeAlignment = true;
Chad Rosier61a62212011-08-04 01:21:14 +000060 UseZeroLengthBitfieldAlignment = false;
61 ZeroLengthBitfieldBoundary = 0;
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +000062 HalfFormat = &llvm::APFloat::IEEEhalf;
Chris Lattnercd4fc422008-03-08 08:59:43 +000063 FloatFormat = &llvm::APFloat::IEEEsingle;
64 DoubleFormat = &llvm::APFloat::IEEEdouble;
65 LongDoubleFormat = &llvm::APFloat::IEEEdouble;
Eli Friedmaned855cb2008-08-21 00:13:15 +000066 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 +000067 "i64:64:64-f32:32:32-f64:64:64-n32";
Chris Lattner3fdf4672008-10-05 19:22:37 +000068 UserLabelPrefix = "_";
Roman Divackybe4c8702011-02-10 16:52:03 +000069 MCountName = "mcount";
Abramo Bagnara62f940b2011-09-08 14:20:25 +000070 RegParmMax = 0;
71 SSERegParmMax = 0;
Daniel Dunbar613fd672010-05-27 00:35:16 +000072 HasAlignMac68kSupport = false;
Daniel Dunbardacf9dd2010-07-14 23:39:36 +000073
74 // Default to no types using fpret.
75 RealTypeUsesObjCFPRet = 0;
John McCallee79a4c2010-08-21 22:46:04 +000076
Anders Carlssoneea64802011-10-31 16:27:11 +000077 // Default to not using fp2ret for __Complex long double
78 ComplexLongDoubleUsesFP2Ret = false;
79
John McCallee79a4c2010-08-21 22:46:04 +000080 // Default to using the Itanium ABI.
81 CXXABI = CXXABI_Itanium;
Peter Collingbourne207f4d82011-03-18 22:38:29 +000082
83 // Default to an empty address space map.
84 AddrSpaceMap = &DefaultAddrSpaceMap;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +000085
86 // Default to an unknown platform name.
87 PlatformName = "unknown";
88 PlatformMinVersion = VersionTuple();
Chris Lattnercd4fc422008-03-08 08:59:43 +000089}
90
Chris Lattner0eaed122008-03-08 08:24:01 +000091// Out of line virtual dtor for TargetInfo.
92TargetInfo::~TargetInfo() {}
Reid Spencer5f016e22007-07-11 17:01:13 +000093
Chris Lattner2b5abf52009-02-06 05:04:11 +000094/// getTypeName - Return the user string for the specified integer type enum.
95/// For example, SignedShort -> "short".
96const char *TargetInfo::getTypeName(IntType T) {
97 switch (T) {
David Blaikieb219cfc2011-09-23 05:06:16 +000098 default: llvm_unreachable("not an integer!");
Chris Lattner2b5abf52009-02-06 05:04:11 +000099 case SignedShort: return "short";
100 case UnsignedShort: return "unsigned short";
101 case SignedInt: return "int";
102 case UnsignedInt: return "unsigned int";
103 case SignedLong: return "long int";
104 case UnsignedLong: return "long unsigned int";
105 case SignedLongLong: return "long long int";
106 case UnsignedLongLong: return "long long unsigned int";
107 }
108}
109
Chris Lattnerb304f772009-10-21 06:24:21 +0000110/// getTypeConstantSuffix - Return the constant suffix for the specified
111/// integer type enum. For example, SignedLong -> "L".
112const char *TargetInfo::getTypeConstantSuffix(IntType T) {
113 switch (T) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000114 default: llvm_unreachable("not an integer!");
Chris Lattnerb304f772009-10-21 06:24:21 +0000115 case SignedShort:
116 case SignedInt: return "";
117 case SignedLong: return "L";
118 case SignedLongLong: return "LL";
119 case UnsignedShort:
120 case UnsignedInt: return "U";
121 case UnsignedLong: return "UL";
122 case UnsignedLongLong: return "ULL";
123 }
124}
125
Michael J. Spencer237cf582010-10-18 07:10:59 +0000126/// getTypeWidth - Return the width (in bits) of the specified integer type
Chris Lattnerb304f772009-10-21 06:24:21 +0000127/// enum. For example, SignedInt -> getIntWidth().
128unsigned TargetInfo::getTypeWidth(IntType T) const {
129 switch (T) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000130 default: llvm_unreachable("not an integer!");
Chris Lattner9099e7b2009-11-05 21:21:32 +0000131 case SignedShort:
Chris Lattnerb304f772009-10-21 06:24:21 +0000132 case UnsignedShort: return getShortWidth();
Chris Lattner9099e7b2009-11-05 21:21:32 +0000133 case SignedInt:
Chris Lattnerb304f772009-10-21 06:24:21 +0000134 case UnsignedInt: return getIntWidth();
Chris Lattner9099e7b2009-11-05 21:21:32 +0000135 case SignedLong:
Chris Lattnerb304f772009-10-21 06:24:21 +0000136 case UnsignedLong: return getLongWidth();
Chris Lattner9099e7b2009-11-05 21:21:32 +0000137 case SignedLongLong:
Chris Lattnerb304f772009-10-21 06:24:21 +0000138 case UnsignedLongLong: return getLongLongWidth();
139 };
140}
141
Michael J. Spencer237cf582010-10-18 07:10:59 +0000142/// getTypeAlign - Return the alignment (in bits) of the specified integer type
Chris Lattner9099e7b2009-11-05 21:21:32 +0000143/// enum. For example, SignedInt -> getIntAlign().
144unsigned TargetInfo::getTypeAlign(IntType T) const {
145 switch (T) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000146 default: llvm_unreachable("not an integer!");
Chris Lattner9099e7b2009-11-05 21:21:32 +0000147 case SignedShort:
148 case UnsignedShort: return getShortAlign();
149 case SignedInt:
150 case UnsignedInt: return getIntAlign();
151 case SignedLong:
152 case UnsignedLong: return getLongAlign();
153 case SignedLongLong:
154 case UnsignedLongLong: return getLongLongAlign();
155 };
156}
157
Chris Lattner961f0702009-10-25 22:49:18 +0000158/// isTypeSigned - Return whether an integer types is signed. Returns true if
Chris Lattnerb304f772009-10-21 06:24:21 +0000159/// the type is signed; false otherwise.
Chris Lattner3f59c972010-12-25 23:25:43 +0000160bool TargetInfo::isTypeSigned(IntType T) {
Chris Lattnerb304f772009-10-21 06:24:21 +0000161 switch (T) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000162 default: llvm_unreachable("not an integer!");
Chris Lattnerb304f772009-10-21 06:24:21 +0000163 case SignedShort:
164 case SignedInt:
165 case SignedLong:
Michael J. Spencer237cf582010-10-18 07:10:59 +0000166 case SignedLongLong:
Chris Lattnerb304f772009-10-21 06:24:21 +0000167 return true;
168 case UnsignedShort:
169 case UnsignedInt:
170 case UnsignedLong:
Michael J. Spencer237cf582010-10-18 07:10:59 +0000171 case UnsignedLongLong:
Chris Lattnerb304f772009-10-21 06:24:21 +0000172 return false;
173 };
174}
175
John Thompsona6fda122009-11-05 20:14:16 +0000176/// setForcedLangOptions - Set forced language options.
177/// Apply changes to the target information with respect to certain
178/// language options which change the target configuration.
179void TargetInfo::setForcedLangOptions(LangOptions &Opts) {
Daniel Dunbarfb937b82010-04-15 15:06:22 +0000180 if (Opts.NoBitFieldTypeAlign)
181 UseBitFieldTypeAlignment = false;
182 if (Opts.ShortWChar)
John Thompsona6fda122009-11-05 20:14:16 +0000183 WCharType = UnsignedShort;
John Thompsona6fda122009-11-05 20:14:16 +0000184}
Chris Lattnerb304f772009-10-21 06:24:21 +0000185
Chris Lattner525a0502007-09-22 18:29:59 +0000186//===----------------------------------------------------------------------===//
Chris Lattner525a0502007-09-22 18:29:59 +0000187
Reid Spencer5f016e22007-07-11 17:01:13 +0000188
Chris Lattner5f9e2722011-07-23 10:55:15 +0000189static StringRef removeGCCRegisterPrefix(StringRef Name) {
Anders Carlssonea041752008-02-06 00:11:32 +0000190 if (Name[0] == '%' || Name[0] == '#')
Anders Carlsson83c021c2010-01-30 19:12:25 +0000191 Name = Name.substr(1);
Michael J. Spencer237cf582010-10-18 07:10:59 +0000192
Anders Carlsson83c021c2010-01-30 19:12:25 +0000193 return Name;
Anders Carlssonea041752008-02-06 00:11:32 +0000194}
195
Eric Christopherde31fd72011-06-28 18:20:53 +0000196/// isValidClobber - Returns whether the passed in string is
197/// a valid clobber in an inline asm statement. This is used by
198/// Sema.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000199bool TargetInfo::isValidClobber(StringRef Name) const {
Eric Christopherde31fd72011-06-28 18:20:53 +0000200 return (isValidGCCRegisterName(Name) ||
201 Name == "memory" || Name == "cc");
202}
203
Anders Carlsson3346ae62007-11-24 23:38:12 +0000204/// isValidGCCRegisterName - Returns whether the passed in string
205/// is a valid register name according to GCC. This is used by Sema for
206/// inline asm statements.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000207bool TargetInfo::isValidGCCRegisterName(StringRef Name) const {
Anders Carlsson83c021c2010-01-30 19:12:25 +0000208 if (Name.empty())
209 return false;
Michael J. Spencer237cf582010-10-18 07:10:59 +0000210
Anders Carlsson6fa90862007-11-25 00:25:21 +0000211 const char * const *Names;
212 unsigned NumNames;
Mike Stump1eb44332009-09-09 15:08:12 +0000213
Anders Carlsson6fa90862007-11-25 00:25:21 +0000214 // Get rid of any register prefix.
Anders Carlsson83c021c2010-01-30 19:12:25 +0000215 Name = removeGCCRegisterPrefix(Name);
Anders Carlssonea041752008-02-06 00:11:32 +0000216
Chris Lattner0eaed122008-03-08 08:24:01 +0000217 getGCCRegNames(Names, NumNames);
Mike Stump1eb44332009-09-09 15:08:12 +0000218
Anders Carlsson6fa90862007-11-25 00:25:21 +0000219 // If we have a number it maps to an entry in the register name array.
220 if (isdigit(Name[0])) {
Anders Carlsson83c021c2010-01-30 19:12:25 +0000221 int n;
222 if (!Name.getAsInteger(0, n))
Anders Carlsson6fa90862007-11-25 00:25:21 +0000223 return n >= 0 && (unsigned)n < NumNames;
224 }
225
226 // Check register names.
227 for (unsigned i = 0; i < NumNames; i++) {
Anders Carlsson83c021c2010-01-30 19:12:25 +0000228 if (Name == Names[i])
Anders Carlsson6fa90862007-11-25 00:25:21 +0000229 return true;
230 }
Mike Stump1eb44332009-09-09 15:08:12 +0000231
Eric Christophercfd323d2011-06-21 00:05:20 +0000232 // Check any additional names that we have.
233 const AddlRegName *AddlNames;
234 unsigned NumAddlNames;
235 getGCCAddlRegNames(AddlNames, NumAddlNames);
236 for (unsigned i = 0; i < NumAddlNames; i++)
237 for (unsigned j = 0; j < llvm::array_lengthof(AddlNames[i].Names); j++) {
238 if (!AddlNames[i].Names[j])
239 break;
240 // Make sure the register that the additional name is for is within
241 // the bounds of the register names from above.
242 if (AddlNames[i].Names[j] == Name && AddlNames[i].RegNum < NumNames)
243 return true;
244 }
245
Anders Carlsson6fa90862007-11-25 00:25:21 +0000246 // Now check aliases.
Chris Lattner0eaed122008-03-08 08:24:01 +0000247 const GCCRegAlias *Aliases;
Anders Carlsson6fa90862007-11-25 00:25:21 +0000248 unsigned NumAliases;
Mike Stump1eb44332009-09-09 15:08:12 +0000249
Chris Lattner0eaed122008-03-08 08:24:01 +0000250 getGCCRegAliases(Aliases, NumAliases);
Anders Carlsson6fa90862007-11-25 00:25:21 +0000251 for (unsigned i = 0; i < NumAliases; i++) {
252 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
253 if (!Aliases[i].Aliases[j])
254 break;
Anders Carlsson83c021c2010-01-30 19:12:25 +0000255 if (Aliases[i].Aliases[j] == Name)
Anders Carlsson6fa90862007-11-25 00:25:21 +0000256 return true;
257 }
258 }
Mike Stump1eb44332009-09-09 15:08:12 +0000259
Anders Carlsson3346ae62007-11-24 23:38:12 +0000260 return false;
261}
Anders Carlssond04c6e22007-11-27 04:11:28 +0000262
Chris Lattner5f9e2722011-07-23 10:55:15 +0000263StringRef
264TargetInfo::getNormalizedGCCRegisterName(StringRef Name) const {
Anders Carlssond04c6e22007-11-27 04:11:28 +0000265 assert(isValidGCCRegisterName(Name) && "Invalid register passed in");
Mike Stump1eb44332009-09-09 15:08:12 +0000266
Anders Carlsson83c021c2010-01-30 19:12:25 +0000267 // Get rid of any register prefix.
268 Name = removeGCCRegisterPrefix(Name);
Mike Stump1eb44332009-09-09 15:08:12 +0000269
Anders Carlssond04c6e22007-11-27 04:11:28 +0000270 const char * const *Names;
271 unsigned NumNames;
272
Chris Lattner0eaed122008-03-08 08:24:01 +0000273 getGCCRegNames(Names, NumNames);
Anders Carlssond04c6e22007-11-27 04:11:28 +0000274
275 // First, check if we have a number.
276 if (isdigit(Name[0])) {
Anders Carlsson83c021c2010-01-30 19:12:25 +0000277 int n;
278 if (!Name.getAsInteger(0, n)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000279 assert(n >= 0 && (unsigned)n < NumNames &&
Anders Carlssond04c6e22007-11-27 04:11:28 +0000280 "Out of bounds register number!");
281 return Names[n];
282 }
283 }
Mike Stump1eb44332009-09-09 15:08:12 +0000284
Eric Christophercfd323d2011-06-21 00:05:20 +0000285 // Check any additional names that we have.
286 const AddlRegName *AddlNames;
287 unsigned NumAddlNames;
288 getGCCAddlRegNames(AddlNames, NumAddlNames);
289 for (unsigned i = 0; i < NumAddlNames; i++)
290 for (unsigned j = 0; j < llvm::array_lengthof(AddlNames[i].Names); j++) {
291 if (!AddlNames[i].Names[j])
292 break;
293 // Make sure the register that the additional name is for is within
294 // the bounds of the register names from above.
295 if (AddlNames[i].Names[j] == Name && AddlNames[i].RegNum < NumNames)
296 return Name;
297 }
298
Anders Carlssond04c6e22007-11-27 04:11:28 +0000299 // Now check aliases.
Chris Lattner0eaed122008-03-08 08:24:01 +0000300 const GCCRegAlias *Aliases;
Anders Carlssond04c6e22007-11-27 04:11:28 +0000301 unsigned NumAliases;
Mike Stump1eb44332009-09-09 15:08:12 +0000302
Chris Lattner0eaed122008-03-08 08:24:01 +0000303 getGCCRegAliases(Aliases, NumAliases);
Anders Carlssond04c6e22007-11-27 04:11:28 +0000304 for (unsigned i = 0; i < NumAliases; i++) {
305 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
306 if (!Aliases[i].Aliases[j])
307 break;
Anders Carlsson83c021c2010-01-30 19:12:25 +0000308 if (Aliases[i].Aliases[j] == Name)
Anders Carlssond04c6e22007-11-27 04:11:28 +0000309 return Aliases[i].Register;
310 }
311 }
Mike Stump1eb44332009-09-09 15:08:12 +0000312
Anders Carlssond04c6e22007-11-27 04:11:28 +0000313 return Name;
314}
315
Chris Lattner432c8692009-04-26 17:19:08 +0000316bool TargetInfo::validateOutputConstraint(ConstraintInfo &Info) const {
317 const char *Name = Info.getConstraintStr().c_str();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000318 // An output constraint must start with '=' or '+'
319 if (*Name != '=' && *Name != '+')
320 return false;
321
322 if (*Name == '+')
Chris Lattner44def072009-04-26 07:16:29 +0000323 Info.setIsReadWrite();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000324
325 Name++;
326 while (*Name) {
327 switch (*Name) {
328 default:
Chris Lattner44def072009-04-26 07:16:29 +0000329 if (!validateAsmConstraint(Name, Info)) {
Ted Kremenek5ab201a2008-04-24 16:36:38 +0000330 // FIXME: We temporarily return false
Anders Carlssond04c6e22007-11-27 04:11:28 +0000331 // so we can add more constraints as we hit it.
332 // Eventually, an unknown constraint should just be treated as 'g'.
Ted Kremenek5ab201a2008-04-24 16:36:38 +0000333 return false;
Anders Carlssond04c6e22007-11-27 04:11:28 +0000334 }
335 case '&': // early clobber.
336 break;
Chris Lattner40539832009-10-13 04:32:07 +0000337 case '%': // commutative.
338 // FIXME: Check that there is a another register after this one.
339 break;
Anders Carlssond04c6e22007-11-27 04:11:28 +0000340 case 'r': // general register.
Chris Lattner44def072009-04-26 07:16:29 +0000341 Info.setAllowsRegister();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000342 break;
343 case 'm': // memory operand.
Dale Johannesen0f048a42010-09-07 18:40:41 +0000344 case 'o': // offsetable memory operand.
345 case 'V': // non-offsetable memory operand.
346 case '<': // autodecrement memory operand.
347 case '>': // autoincrement memory operand.
Chris Lattner44def072009-04-26 07:16:29 +0000348 Info.setAllowsMemory();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000349 break;
350 case 'g': // general register, memory operand or immediate integer.
Anders Carlsson775841f2009-01-18 02:12:04 +0000351 case 'X': // any operand.
Chris Lattner44def072009-04-26 07:16:29 +0000352 Info.setAllowsRegister();
353 Info.setAllowsMemory();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000354 break;
John Thompsonef44e112010-08-10 19:20:14 +0000355 case ',': // multiple alternative constraint. Pass it.
John Thompson6f158032010-08-11 00:58:20 +0000356 // Handle additional optional '=' or '+' modifiers.
John Thompson2f474ea2010-09-18 01:15:13 +0000357 if (Name[1] == '=' || Name[1] == '+')
John Thompson6f158032010-08-11 00:58:20 +0000358 Name++;
John Thompsonef44e112010-08-10 19:20:14 +0000359 break;
360 case '?': // Disparage slightly code.
Dale Johannesen0f048a42010-09-07 18:40:41 +0000361 case '!': // Disparage severely.
John Thompsonef44e112010-08-10 19:20:14 +0000362 break; // Pass them.
Anders Carlssond04c6e22007-11-27 04:11:28 +0000363 }
Mike Stump1eb44332009-09-09 15:08:12 +0000364
Anders Carlssond04c6e22007-11-27 04:11:28 +0000365 Name++;
366 }
Mike Stump1eb44332009-09-09 15:08:12 +0000367
Anders Carlssond04c6e22007-11-27 04:11:28 +0000368 return true;
369}
370
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000371bool TargetInfo::resolveSymbolicName(const char *&Name,
Chris Lattner2819fa82009-04-26 17:57:12 +0000372 ConstraintInfo *OutputConstraints,
373 unsigned NumOutputs,
Chris Lattner44def072009-04-26 07:16:29 +0000374 unsigned &Index) const {
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000375 assert(*Name == '[' && "Symbolic name did not start with '['");
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000376 Name++;
377 const char *Start = Name;
378 while (*Name && *Name != ']')
379 Name++;
Mike Stump1eb44332009-09-09 15:08:12 +0000380
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000381 if (!*Name) {
382 // Missing ']'
383 return false;
384 }
Mike Stump1eb44332009-09-09 15:08:12 +0000385
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000386 std::string SymbolicName(Start, Name - Start);
Mike Stump1eb44332009-09-09 15:08:12 +0000387
Chris Lattner2819fa82009-04-26 17:57:12 +0000388 for (Index = 0; Index != NumOutputs; ++Index)
389 if (SymbolicName == OutputConstraints[Index].getName())
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000390 return true;
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000391
392 return false;
393}
394
Chris Lattner2819fa82009-04-26 17:57:12 +0000395bool TargetInfo::validateInputConstraint(ConstraintInfo *OutputConstraints,
396 unsigned NumOutputs,
Chris Lattner44def072009-04-26 07:16:29 +0000397 ConstraintInfo &Info) const {
Chris Lattner432c8692009-04-26 17:19:08 +0000398 const char *Name = Info.ConstraintStr.c_str();
399
Anders Carlssond04c6e22007-11-27 04:11:28 +0000400 while (*Name) {
401 switch (*Name) {
402 default:
403 // Check if we have a matching constraint
404 if (*Name >= '0' && *Name <= '9') {
405 unsigned i = *Name - '0';
Mike Stump1eb44332009-09-09 15:08:12 +0000406
Anders Carlssond04c6e22007-11-27 04:11:28 +0000407 // Check if matching constraint is out of bounds.
408 if (i >= NumOutputs)
409 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000410
Anders Carlsson86eda392010-11-03 02:22:29 +0000411 // A number must refer to an output only operand.
412 if (OutputConstraints[i].isReadWrite())
413 return false;
414
Anders Carlsson79ca1ee2010-11-03 02:54:51 +0000415 // If the constraint is already tied, it must be tied to the
416 // same operand referenced to by the number.
417 if (Info.hasTiedOperand() && Info.getTiedOperand() != i)
418 return false;
419
Mike Stump1eb44332009-09-09 15:08:12 +0000420 // The constraint should have the same info as the respective
Anders Carlsson03eb5432009-01-27 20:38:24 +0000421 // output constraint.
Chris Lattnerd6887612009-04-26 18:05:25 +0000422 Info.setTiedOperand(i, OutputConstraints[i]);
Chris Lattner44def072009-04-26 07:16:29 +0000423 } else if (!validateAsmConstraint(Name, Info)) {
Daniel Dunbar302684c2008-08-25 09:46:27 +0000424 // FIXME: This error return is in place temporarily so we can
425 // add more constraints as we hit it. Eventually, an unknown
426 // constraint should just be treated as 'g'.
427 return false;
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000428 }
429 break;
430 case '[': {
431 unsigned Index = 0;
Chris Lattner2819fa82009-04-26 17:57:12 +0000432 if (!resolveSymbolicName(Name, OutputConstraints, NumOutputs, Index))
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000433 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000434
Anders Carlsson79ca1ee2010-11-03 02:54:51 +0000435 // If the constraint is already tied, it must be tied to the
436 // same operand referenced to by the number.
437 if (Info.hasTiedOperand() && Info.getTiedOperand() != Index)
438 return false;
439
Eli Friedman0eff4e72010-08-11 23:03:37 +0000440 Info.setTiedOperand(Index, OutputConstraints[Index]);
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000441 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000442 }
Anders Carlsson44fe49c2007-12-08 19:32:57 +0000443 case '%': // commutative
444 // FIXME: Fail if % is used with the last operand.
445 break;
Anders Carlssond04c6e22007-11-27 04:11:28 +0000446 case 'i': // immediate integer.
Anders Carlssonefdfa772008-03-09 06:02:02 +0000447 case 'n': // immediate integer with a known value.
Anders Carlssond04c6e22007-11-27 04:11:28 +0000448 break;
Chris Lattner1d138792009-05-06 04:33:31 +0000449 case 'I': // Various constant constraints with target-specific meanings.
450 case 'J':
451 case 'K':
452 case 'L':
453 case 'M':
454 case 'N':
455 case 'O':
456 case 'P':
457 break;
Anders Carlssond04c6e22007-11-27 04:11:28 +0000458 case 'r': // general register.
Chris Lattner44def072009-04-26 07:16:29 +0000459 Info.setAllowsRegister();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000460 break;
461 case 'm': // memory operand.
Dale Johannesen0f048a42010-09-07 18:40:41 +0000462 case 'o': // offsettable memory operand.
463 case 'V': // non-offsettable memory operand.
464 case '<': // autodecrement memory operand.
465 case '>': // autoincrement memory operand.
Chris Lattner44def072009-04-26 07:16:29 +0000466 Info.setAllowsMemory();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000467 break;
468 case 'g': // general register, memory operand or immediate integer.
Anders Carlsson775841f2009-01-18 02:12:04 +0000469 case 'X': // any operand.
Chris Lattner44def072009-04-26 07:16:29 +0000470 Info.setAllowsRegister();
471 Info.setAllowsMemory();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000472 break;
John Thompson7ccc58f2010-09-21 22:04:54 +0000473 case 'E': // immediate floating point.
474 case 'F': // immediate floating point.
475 case 'p': // address operand.
476 break;
John Thompsonef44e112010-08-10 19:20:14 +0000477 case ',': // multiple alternative constraint. Ignore comma.
478 break;
479 case '?': // Disparage slightly code.
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000480 case '!': // Disparage severely.
John Thompsonef44e112010-08-10 19:20:14 +0000481 break; // Pass them.
Anders Carlssond04c6e22007-11-27 04:11:28 +0000482 }
Mike Stump1eb44332009-09-09 15:08:12 +0000483
Anders Carlssond04c6e22007-11-27 04:11:28 +0000484 Name++;
485 }
Mike Stump1eb44332009-09-09 15:08:12 +0000486
Anders Carlssond04c6e22007-11-27 04:11:28 +0000487 return true;
488}