blob: a9285e6e2dbd641376ba11a15f92ce99f5751db3 [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;
Eli Friedman61538a72008-05-20 14:21:01 +000037 FloatWidth = 32;
38 FloatAlign = 32;
Nate Begeman3d292062008-04-18 17:17:24 +000039 DoubleWidth = 64;
Eli Friedman61538a72008-05-20 14:21:01 +000040 DoubleAlign = 64;
41 LongDoubleWidth = 64;
42 LongDoubleAlign = 64;
Rafael Espindola6deecb02010-06-04 23:15:27 +000043 LargeArrayMinWidth = 0;
44 LargeArrayAlign = 0;
Eli Friedman2be46072011-10-14 20:59:01 +000045 MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 0;
Anders Carlsson6a3615c2008-10-31 16:05:19 +000046 SizeType = UnsignedLong;
Eli Friedmanf509d732008-11-02 02:43:55 +000047 PtrDiffType = SignedLong;
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +000048 IntMaxType = SignedLongLong;
49 UIntMaxType = UnsignedLongLong;
Chris Lattner6ad474f2009-02-13 22:28:55 +000050 IntPtrType = SignedLong;
Eli Friedmanf509d732008-11-02 02:43:55 +000051 WCharType = SignedInt;
Chris Lattnere64ef802009-10-21 04:59:34 +000052 WIntType = SignedInt;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +000053 Char16Type = UnsignedShort;
54 Char32Type = UnsignedInt;
Eli Friedman3c7b6e42009-07-01 03:36:11 +000055 Int64Type = SignedLongLong;
Edward O'Callaghan9cf910e2009-11-21 00:49:54 +000056 SigAtomicType = SignedInt;
Daniel Dunbarb6830d62010-04-15 15:06:18 +000057 UseBitFieldTypeAlignment = true;
Chad Rosier61a62212011-08-04 01:21:14 +000058 UseZeroLengthBitfieldAlignment = false;
59 ZeroLengthBitfieldBoundary = 0;
Chris Lattnercd4fc422008-03-08 08:59:43 +000060 FloatFormat = &llvm::APFloat::IEEEsingle;
61 DoubleFormat = &llvm::APFloat::IEEEdouble;
62 LongDoubleFormat = &llvm::APFloat::IEEEdouble;
Eli Friedmaned855cb2008-08-21 00:13:15 +000063 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 +000064 "i64:64:64-f32:32:32-f64:64:64-n32";
Chris Lattner3fdf4672008-10-05 19:22:37 +000065 UserLabelPrefix = "_";
Roman Divackybe4c8702011-02-10 16:52:03 +000066 MCountName = "mcount";
Abramo Bagnara62f940b2011-09-08 14:20:25 +000067 RegParmMax = 0;
68 SSERegParmMax = 0;
Daniel Dunbar613fd672010-05-27 00:35:16 +000069 HasAlignMac68kSupport = false;
Daniel Dunbardacf9dd2010-07-14 23:39:36 +000070
71 // Default to no types using fpret.
72 RealTypeUsesObjCFPRet = 0;
John McCallee79a4c2010-08-21 22:46:04 +000073
74 // Default to using the Itanium ABI.
75 CXXABI = CXXABI_Itanium;
Peter Collingbourne207f4d82011-03-18 22:38:29 +000076
77 // Default to an empty address space map.
78 AddrSpaceMap = &DefaultAddrSpaceMap;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +000079
80 // Default to an unknown platform name.
81 PlatformName = "unknown";
82 PlatformMinVersion = VersionTuple();
Chris Lattnercd4fc422008-03-08 08:59:43 +000083}
84
Chris Lattner0eaed122008-03-08 08:24:01 +000085// Out of line virtual dtor for TargetInfo.
86TargetInfo::~TargetInfo() {}
Reid Spencer5f016e22007-07-11 17:01:13 +000087
Chris Lattner2b5abf52009-02-06 05:04:11 +000088/// getTypeName - Return the user string for the specified integer type enum.
89/// For example, SignedShort -> "short".
90const char *TargetInfo::getTypeName(IntType T) {
91 switch (T) {
David Blaikieb219cfc2011-09-23 05:06:16 +000092 default: llvm_unreachable("not an integer!");
Chris Lattner2b5abf52009-02-06 05:04:11 +000093 case SignedShort: return "short";
94 case UnsignedShort: return "unsigned short";
95 case SignedInt: return "int";
96 case UnsignedInt: return "unsigned int";
97 case SignedLong: return "long int";
98 case UnsignedLong: return "long unsigned int";
99 case SignedLongLong: return "long long int";
100 case UnsignedLongLong: return "long long unsigned int";
101 }
102}
103
Chris Lattnerb304f772009-10-21 06:24:21 +0000104/// getTypeConstantSuffix - Return the constant suffix for the specified
105/// integer type enum. For example, SignedLong -> "L".
106const char *TargetInfo::getTypeConstantSuffix(IntType T) {
107 switch (T) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000108 default: llvm_unreachable("not an integer!");
Chris Lattnerb304f772009-10-21 06:24:21 +0000109 case SignedShort:
110 case SignedInt: return "";
111 case SignedLong: return "L";
112 case SignedLongLong: return "LL";
113 case UnsignedShort:
114 case UnsignedInt: return "U";
115 case UnsignedLong: return "UL";
116 case UnsignedLongLong: return "ULL";
117 }
118}
119
Michael J. Spencer237cf582010-10-18 07:10:59 +0000120/// getTypeWidth - Return the width (in bits) of the specified integer type
Chris Lattnerb304f772009-10-21 06:24:21 +0000121/// enum. For example, SignedInt -> getIntWidth().
122unsigned TargetInfo::getTypeWidth(IntType T) const {
123 switch (T) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000124 default: llvm_unreachable("not an integer!");
Chris Lattner9099e7b2009-11-05 21:21:32 +0000125 case SignedShort:
Chris Lattnerb304f772009-10-21 06:24:21 +0000126 case UnsignedShort: return getShortWidth();
Chris Lattner9099e7b2009-11-05 21:21:32 +0000127 case SignedInt:
Chris Lattnerb304f772009-10-21 06:24:21 +0000128 case UnsignedInt: return getIntWidth();
Chris Lattner9099e7b2009-11-05 21:21:32 +0000129 case SignedLong:
Chris Lattnerb304f772009-10-21 06:24:21 +0000130 case UnsignedLong: return getLongWidth();
Chris Lattner9099e7b2009-11-05 21:21:32 +0000131 case SignedLongLong:
Chris Lattnerb304f772009-10-21 06:24:21 +0000132 case UnsignedLongLong: return getLongLongWidth();
133 };
134}
135
Michael J. Spencer237cf582010-10-18 07:10:59 +0000136/// getTypeAlign - Return the alignment (in bits) of the specified integer type
Chris Lattner9099e7b2009-11-05 21:21:32 +0000137/// enum. For example, SignedInt -> getIntAlign().
138unsigned TargetInfo::getTypeAlign(IntType T) const {
139 switch (T) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000140 default: llvm_unreachable("not an integer!");
Chris Lattner9099e7b2009-11-05 21:21:32 +0000141 case SignedShort:
142 case UnsignedShort: return getShortAlign();
143 case SignedInt:
144 case UnsignedInt: return getIntAlign();
145 case SignedLong:
146 case UnsignedLong: return getLongAlign();
147 case SignedLongLong:
148 case UnsignedLongLong: return getLongLongAlign();
149 };
150}
151
Chris Lattner961f0702009-10-25 22:49:18 +0000152/// isTypeSigned - Return whether an integer types is signed. Returns true if
Chris Lattnerb304f772009-10-21 06:24:21 +0000153/// the type is signed; false otherwise.
Chris Lattner3f59c972010-12-25 23:25:43 +0000154bool TargetInfo::isTypeSigned(IntType T) {
Chris Lattnerb304f772009-10-21 06:24:21 +0000155 switch (T) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000156 default: llvm_unreachable("not an integer!");
Chris Lattnerb304f772009-10-21 06:24:21 +0000157 case SignedShort:
158 case SignedInt:
159 case SignedLong:
Michael J. Spencer237cf582010-10-18 07:10:59 +0000160 case SignedLongLong:
Chris Lattnerb304f772009-10-21 06:24:21 +0000161 return true;
162 case UnsignedShort:
163 case UnsignedInt:
164 case UnsignedLong:
Michael J. Spencer237cf582010-10-18 07:10:59 +0000165 case UnsignedLongLong:
Chris Lattnerb304f772009-10-21 06:24:21 +0000166 return false;
167 };
168}
169
John Thompsona6fda122009-11-05 20:14:16 +0000170/// setForcedLangOptions - Set forced language options.
171/// Apply changes to the target information with respect to certain
172/// language options which change the target configuration.
173void TargetInfo::setForcedLangOptions(LangOptions &Opts) {
Daniel Dunbarfb937b82010-04-15 15:06:22 +0000174 if (Opts.NoBitFieldTypeAlign)
175 UseBitFieldTypeAlignment = false;
176 if (Opts.ShortWChar)
John Thompsona6fda122009-11-05 20:14:16 +0000177 WCharType = UnsignedShort;
John Thompsona6fda122009-11-05 20:14:16 +0000178}
Chris Lattnerb304f772009-10-21 06:24:21 +0000179
Chris Lattner525a0502007-09-22 18:29:59 +0000180//===----------------------------------------------------------------------===//
Chris Lattner525a0502007-09-22 18:29:59 +0000181
Reid Spencer5f016e22007-07-11 17:01:13 +0000182
Chris Lattner5f9e2722011-07-23 10:55:15 +0000183static StringRef removeGCCRegisterPrefix(StringRef Name) {
Anders Carlssonea041752008-02-06 00:11:32 +0000184 if (Name[0] == '%' || Name[0] == '#')
Anders Carlsson83c021c2010-01-30 19:12:25 +0000185 Name = Name.substr(1);
Michael J. Spencer237cf582010-10-18 07:10:59 +0000186
Anders Carlsson83c021c2010-01-30 19:12:25 +0000187 return Name;
Anders Carlssonea041752008-02-06 00:11:32 +0000188}
189
Eric Christopherde31fd72011-06-28 18:20:53 +0000190/// isValidClobber - Returns whether the passed in string is
191/// a valid clobber in an inline asm statement. This is used by
192/// Sema.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000193bool TargetInfo::isValidClobber(StringRef Name) const {
Eric Christopherde31fd72011-06-28 18:20:53 +0000194 return (isValidGCCRegisterName(Name) ||
195 Name == "memory" || Name == "cc");
196}
197
Anders Carlsson3346ae62007-11-24 23:38:12 +0000198/// isValidGCCRegisterName - Returns whether the passed in string
199/// is a valid register name according to GCC. This is used by Sema for
200/// inline asm statements.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000201bool TargetInfo::isValidGCCRegisterName(StringRef Name) const {
Anders Carlsson83c021c2010-01-30 19:12:25 +0000202 if (Name.empty())
203 return false;
Michael J. Spencer237cf582010-10-18 07:10:59 +0000204
Anders Carlsson6fa90862007-11-25 00:25:21 +0000205 const char * const *Names;
206 unsigned NumNames;
Mike Stump1eb44332009-09-09 15:08:12 +0000207
Anders Carlsson6fa90862007-11-25 00:25:21 +0000208 // Get rid of any register prefix.
Anders Carlsson83c021c2010-01-30 19:12:25 +0000209 Name = removeGCCRegisterPrefix(Name);
Anders Carlssonea041752008-02-06 00:11:32 +0000210
Chris Lattner0eaed122008-03-08 08:24:01 +0000211 getGCCRegNames(Names, NumNames);
Mike Stump1eb44332009-09-09 15:08:12 +0000212
Anders Carlsson6fa90862007-11-25 00:25:21 +0000213 // If we have a number it maps to an entry in the register name array.
214 if (isdigit(Name[0])) {
Anders Carlsson83c021c2010-01-30 19:12:25 +0000215 int n;
216 if (!Name.getAsInteger(0, n))
Anders Carlsson6fa90862007-11-25 00:25:21 +0000217 return n >= 0 && (unsigned)n < NumNames;
218 }
219
220 // Check register names.
221 for (unsigned i = 0; i < NumNames; i++) {
Anders Carlsson83c021c2010-01-30 19:12:25 +0000222 if (Name == Names[i])
Anders Carlsson6fa90862007-11-25 00:25:21 +0000223 return true;
224 }
Mike Stump1eb44332009-09-09 15:08:12 +0000225
Eric Christophercfd323d2011-06-21 00:05:20 +0000226 // Check any additional names that we have.
227 const AddlRegName *AddlNames;
228 unsigned NumAddlNames;
229 getGCCAddlRegNames(AddlNames, NumAddlNames);
230 for (unsigned i = 0; i < NumAddlNames; i++)
231 for (unsigned j = 0; j < llvm::array_lengthof(AddlNames[i].Names); j++) {
232 if (!AddlNames[i].Names[j])
233 break;
234 // Make sure the register that the additional name is for is within
235 // the bounds of the register names from above.
236 if (AddlNames[i].Names[j] == Name && AddlNames[i].RegNum < NumNames)
237 return true;
238 }
239
Anders Carlsson6fa90862007-11-25 00:25:21 +0000240 // Now check aliases.
Chris Lattner0eaed122008-03-08 08:24:01 +0000241 const GCCRegAlias *Aliases;
Anders Carlsson6fa90862007-11-25 00:25:21 +0000242 unsigned NumAliases;
Mike Stump1eb44332009-09-09 15:08:12 +0000243
Chris Lattner0eaed122008-03-08 08:24:01 +0000244 getGCCRegAliases(Aliases, NumAliases);
Anders Carlsson6fa90862007-11-25 00:25:21 +0000245 for (unsigned i = 0; i < NumAliases; i++) {
246 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
247 if (!Aliases[i].Aliases[j])
248 break;
Anders Carlsson83c021c2010-01-30 19:12:25 +0000249 if (Aliases[i].Aliases[j] == Name)
Anders Carlsson6fa90862007-11-25 00:25:21 +0000250 return true;
251 }
252 }
Mike Stump1eb44332009-09-09 15:08:12 +0000253
Anders Carlsson3346ae62007-11-24 23:38:12 +0000254 return false;
255}
Anders Carlssond04c6e22007-11-27 04:11:28 +0000256
Chris Lattner5f9e2722011-07-23 10:55:15 +0000257StringRef
258TargetInfo::getNormalizedGCCRegisterName(StringRef Name) const {
Anders Carlssond04c6e22007-11-27 04:11:28 +0000259 assert(isValidGCCRegisterName(Name) && "Invalid register passed in");
Mike Stump1eb44332009-09-09 15:08:12 +0000260
Anders Carlsson83c021c2010-01-30 19:12:25 +0000261 // Get rid of any register prefix.
262 Name = removeGCCRegisterPrefix(Name);
Mike Stump1eb44332009-09-09 15:08:12 +0000263
Anders Carlssond04c6e22007-11-27 04:11:28 +0000264 const char * const *Names;
265 unsigned NumNames;
266
Chris Lattner0eaed122008-03-08 08:24:01 +0000267 getGCCRegNames(Names, NumNames);
Anders Carlssond04c6e22007-11-27 04:11:28 +0000268
269 // First, check if we have a number.
270 if (isdigit(Name[0])) {
Anders Carlsson83c021c2010-01-30 19:12:25 +0000271 int n;
272 if (!Name.getAsInteger(0, n)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000273 assert(n >= 0 && (unsigned)n < NumNames &&
Anders Carlssond04c6e22007-11-27 04:11:28 +0000274 "Out of bounds register number!");
275 return Names[n];
276 }
277 }
Mike Stump1eb44332009-09-09 15:08:12 +0000278
Eric Christophercfd323d2011-06-21 00:05:20 +0000279 // Check any additional names that we have.
280 const AddlRegName *AddlNames;
281 unsigned NumAddlNames;
282 getGCCAddlRegNames(AddlNames, NumAddlNames);
283 for (unsigned i = 0; i < NumAddlNames; i++)
284 for (unsigned j = 0; j < llvm::array_lengthof(AddlNames[i].Names); j++) {
285 if (!AddlNames[i].Names[j])
286 break;
287 // Make sure the register that the additional name is for is within
288 // the bounds of the register names from above.
289 if (AddlNames[i].Names[j] == Name && AddlNames[i].RegNum < NumNames)
290 return Name;
291 }
292
Anders Carlssond04c6e22007-11-27 04:11:28 +0000293 // Now check aliases.
Chris Lattner0eaed122008-03-08 08:24:01 +0000294 const GCCRegAlias *Aliases;
Anders Carlssond04c6e22007-11-27 04:11:28 +0000295 unsigned NumAliases;
Mike Stump1eb44332009-09-09 15:08:12 +0000296
Chris Lattner0eaed122008-03-08 08:24:01 +0000297 getGCCRegAliases(Aliases, NumAliases);
Anders Carlssond04c6e22007-11-27 04:11:28 +0000298 for (unsigned i = 0; i < NumAliases; i++) {
299 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
300 if (!Aliases[i].Aliases[j])
301 break;
Anders Carlsson83c021c2010-01-30 19:12:25 +0000302 if (Aliases[i].Aliases[j] == Name)
Anders Carlssond04c6e22007-11-27 04:11:28 +0000303 return Aliases[i].Register;
304 }
305 }
Mike Stump1eb44332009-09-09 15:08:12 +0000306
Anders Carlssond04c6e22007-11-27 04:11:28 +0000307 return Name;
308}
309
Chris Lattner432c8692009-04-26 17:19:08 +0000310bool TargetInfo::validateOutputConstraint(ConstraintInfo &Info) const {
311 const char *Name = Info.getConstraintStr().c_str();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000312 // An output constraint must start with '=' or '+'
313 if (*Name != '=' && *Name != '+')
314 return false;
315
316 if (*Name == '+')
Chris Lattner44def072009-04-26 07:16:29 +0000317 Info.setIsReadWrite();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000318
319 Name++;
320 while (*Name) {
321 switch (*Name) {
322 default:
Chris Lattner44def072009-04-26 07:16:29 +0000323 if (!validateAsmConstraint(Name, Info)) {
Ted Kremenek5ab201a2008-04-24 16:36:38 +0000324 // FIXME: We temporarily return false
Anders Carlssond04c6e22007-11-27 04:11:28 +0000325 // so we can add more constraints as we hit it.
326 // Eventually, an unknown constraint should just be treated as 'g'.
Ted Kremenek5ab201a2008-04-24 16:36:38 +0000327 return false;
Anders Carlssond04c6e22007-11-27 04:11:28 +0000328 }
329 case '&': // early clobber.
330 break;
Chris Lattner40539832009-10-13 04:32:07 +0000331 case '%': // commutative.
332 // FIXME: Check that there is a another register after this one.
333 break;
Anders Carlssond04c6e22007-11-27 04:11:28 +0000334 case 'r': // general register.
Chris Lattner44def072009-04-26 07:16:29 +0000335 Info.setAllowsRegister();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000336 break;
337 case 'm': // memory operand.
Dale Johannesen0f048a42010-09-07 18:40:41 +0000338 case 'o': // offsetable memory operand.
339 case 'V': // non-offsetable memory operand.
340 case '<': // autodecrement memory operand.
341 case '>': // autoincrement memory operand.
Chris Lattner44def072009-04-26 07:16:29 +0000342 Info.setAllowsMemory();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000343 break;
344 case 'g': // general register, memory operand or immediate integer.
Anders Carlsson775841f2009-01-18 02:12:04 +0000345 case 'X': // any operand.
Chris Lattner44def072009-04-26 07:16:29 +0000346 Info.setAllowsRegister();
347 Info.setAllowsMemory();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000348 break;
John Thompsonef44e112010-08-10 19:20:14 +0000349 case ',': // multiple alternative constraint. Pass it.
John Thompson6f158032010-08-11 00:58:20 +0000350 // Handle additional optional '=' or '+' modifiers.
John Thompson2f474ea2010-09-18 01:15:13 +0000351 if (Name[1] == '=' || Name[1] == '+')
John Thompson6f158032010-08-11 00:58:20 +0000352 Name++;
John Thompsonef44e112010-08-10 19:20:14 +0000353 break;
354 case '?': // Disparage slightly code.
Dale Johannesen0f048a42010-09-07 18:40:41 +0000355 case '!': // Disparage severely.
John Thompsonef44e112010-08-10 19:20:14 +0000356 break; // Pass them.
Anders Carlssond04c6e22007-11-27 04:11:28 +0000357 }
Mike Stump1eb44332009-09-09 15:08:12 +0000358
Anders Carlssond04c6e22007-11-27 04:11:28 +0000359 Name++;
360 }
Mike Stump1eb44332009-09-09 15:08:12 +0000361
Anders Carlssond04c6e22007-11-27 04:11:28 +0000362 return true;
363}
364
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000365bool TargetInfo::resolveSymbolicName(const char *&Name,
Chris Lattner2819fa82009-04-26 17:57:12 +0000366 ConstraintInfo *OutputConstraints,
367 unsigned NumOutputs,
Chris Lattner44def072009-04-26 07:16:29 +0000368 unsigned &Index) const {
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000369 assert(*Name == '[' && "Symbolic name did not start with '['");
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000370 Name++;
371 const char *Start = Name;
372 while (*Name && *Name != ']')
373 Name++;
Mike Stump1eb44332009-09-09 15:08:12 +0000374
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000375 if (!*Name) {
376 // Missing ']'
377 return false;
378 }
Mike Stump1eb44332009-09-09 15:08:12 +0000379
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000380 std::string SymbolicName(Start, Name - Start);
Mike Stump1eb44332009-09-09 15:08:12 +0000381
Chris Lattner2819fa82009-04-26 17:57:12 +0000382 for (Index = 0; Index != NumOutputs; ++Index)
383 if (SymbolicName == OutputConstraints[Index].getName())
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000384 return true;
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000385
386 return false;
387}
388
Chris Lattner2819fa82009-04-26 17:57:12 +0000389bool TargetInfo::validateInputConstraint(ConstraintInfo *OutputConstraints,
390 unsigned NumOutputs,
Chris Lattner44def072009-04-26 07:16:29 +0000391 ConstraintInfo &Info) const {
Chris Lattner432c8692009-04-26 17:19:08 +0000392 const char *Name = Info.ConstraintStr.c_str();
393
Anders Carlssond04c6e22007-11-27 04:11:28 +0000394 while (*Name) {
395 switch (*Name) {
396 default:
397 // Check if we have a matching constraint
398 if (*Name >= '0' && *Name <= '9') {
399 unsigned i = *Name - '0';
Mike Stump1eb44332009-09-09 15:08:12 +0000400
Anders Carlssond04c6e22007-11-27 04:11:28 +0000401 // Check if matching constraint is out of bounds.
402 if (i >= NumOutputs)
403 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000404
Anders Carlsson86eda392010-11-03 02:22:29 +0000405 // A number must refer to an output only operand.
406 if (OutputConstraints[i].isReadWrite())
407 return false;
408
Anders Carlsson79ca1ee2010-11-03 02:54:51 +0000409 // If the constraint is already tied, it must be tied to the
410 // same operand referenced to by the number.
411 if (Info.hasTiedOperand() && Info.getTiedOperand() != i)
412 return false;
413
Mike Stump1eb44332009-09-09 15:08:12 +0000414 // The constraint should have the same info as the respective
Anders Carlsson03eb5432009-01-27 20:38:24 +0000415 // output constraint.
Chris Lattnerd6887612009-04-26 18:05:25 +0000416 Info.setTiedOperand(i, OutputConstraints[i]);
Chris Lattner44def072009-04-26 07:16:29 +0000417 } else if (!validateAsmConstraint(Name, Info)) {
Daniel Dunbar302684c2008-08-25 09:46:27 +0000418 // FIXME: This error return is in place temporarily so we can
419 // add more constraints as we hit it. Eventually, an unknown
420 // constraint should just be treated as 'g'.
421 return false;
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000422 }
423 break;
424 case '[': {
425 unsigned Index = 0;
Chris Lattner2819fa82009-04-26 17:57:12 +0000426 if (!resolveSymbolicName(Name, OutputConstraints, NumOutputs, Index))
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000427 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000428
Anders Carlsson79ca1ee2010-11-03 02:54:51 +0000429 // If the constraint is already tied, it must be tied to the
430 // same operand referenced to by the number.
431 if (Info.hasTiedOperand() && Info.getTiedOperand() != Index)
432 return false;
433
Eli Friedman0eff4e72010-08-11 23:03:37 +0000434 Info.setTiedOperand(Index, OutputConstraints[Index]);
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000435 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000436 }
Anders Carlsson44fe49c2007-12-08 19:32:57 +0000437 case '%': // commutative
438 // FIXME: Fail if % is used with the last operand.
439 break;
Anders Carlssond04c6e22007-11-27 04:11:28 +0000440 case 'i': // immediate integer.
Anders Carlssonefdfa772008-03-09 06:02:02 +0000441 case 'n': // immediate integer with a known value.
Anders Carlssond04c6e22007-11-27 04:11:28 +0000442 break;
Chris Lattner1d138792009-05-06 04:33:31 +0000443 case 'I': // Various constant constraints with target-specific meanings.
444 case 'J':
445 case 'K':
446 case 'L':
447 case 'M':
448 case 'N':
449 case 'O':
450 case 'P':
451 break;
Anders Carlssond04c6e22007-11-27 04:11:28 +0000452 case 'r': // general register.
Chris Lattner44def072009-04-26 07:16:29 +0000453 Info.setAllowsRegister();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000454 break;
455 case 'm': // memory operand.
Dale Johannesen0f048a42010-09-07 18:40:41 +0000456 case 'o': // offsettable memory operand.
457 case 'V': // non-offsettable memory operand.
458 case '<': // autodecrement memory operand.
459 case '>': // autoincrement memory operand.
Chris Lattner44def072009-04-26 07:16:29 +0000460 Info.setAllowsMemory();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000461 break;
462 case 'g': // general register, memory operand or immediate integer.
Anders Carlsson775841f2009-01-18 02:12:04 +0000463 case 'X': // any operand.
Chris Lattner44def072009-04-26 07:16:29 +0000464 Info.setAllowsRegister();
465 Info.setAllowsMemory();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000466 break;
John Thompson7ccc58f2010-09-21 22:04:54 +0000467 case 'E': // immediate floating point.
468 case 'F': // immediate floating point.
469 case 'p': // address operand.
470 break;
John Thompsonef44e112010-08-10 19:20:14 +0000471 case ',': // multiple alternative constraint. Ignore comma.
472 break;
473 case '?': // Disparage slightly code.
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000474 case '!': // Disparage severely.
John Thompsonef44e112010-08-10 19:20:14 +0000475 break; // Pass them.
Anders Carlssond04c6e22007-11-27 04:11:28 +0000476 }
Mike Stump1eb44332009-09-09 15:08:12 +0000477
Anders Carlssond04c6e22007-11-27 04:11:28 +0000478 Name++;
479 }
Mike Stump1eb44332009-09-09 15:08:12 +0000480
Anders Carlssond04c6e22007-11-27 04:11:28 +0000481 return true;
482}