blob: d5dcf066754bb72bbe178fd62a12783b92cd39ad [file] [log] [blame]
Chris Lattner1e27fe12006-10-14 07:06:20 +00001//===--- TargetInfo.cpp - Information about Target machine ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner1e27fe12006-10-14 07:06:20 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the TargetInfo and TargetInfoImpl interfaces.
11//
12//===----------------------------------------------------------------------===//
13
Peter Collingbourne599cb8e2011-03-18 22:38:29 +000014#include "clang/Basic/AddressSpaces.h"
Chris Lattner1e27fe12006-10-14 07:06:20 +000015#include "clang/Basic/TargetInfo.h"
John Thompsoned4e2952009-11-05 20:14:16 +000016#include "clang/Basic/LangOptions.h"
Chris Lattnerec0a6d92007-09-22 18:29:59 +000017#include "llvm/ADT/APFloat.h"
Anders Carlsson290aa852007-11-25 00:25:21 +000018#include "llvm/ADT/STLExtras.h"
David Blaikie76bd3c82011-09-23 05:35:21 +000019#include "llvm/Support/ErrorHandling.h"
Nick Lewycky2e998b72010-12-19 20:49:25 +000020#include <cctype>
Chris Lattnerc4f02b62008-04-06 04:02:29 +000021#include <cstdlib>
Chris Lattner1e27fe12006-10-14 07:06:20 +000022using namespace clang;
23
Peter Collingbourne599cb8e2011-03-18 22:38:29 +000024static const LangAS::Map DefaultAddrSpaceMap = { 0 };
25
Chris Lattner1df27862008-03-08 08:59:43 +000026// TargetInfo Constructor.
27TargetInfo::TargetInfo(const std::string &T) : Triple(T) {
Daniel Dunbar3d9289c2010-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 Friedmand88c8a12009-04-19 21:38:35 +000030 TLSSupported = true;
Chris Lattner1a8f3942010-04-23 16:29:58 +000031 NoAsmVariants = false;
Chris Lattner5e2ef0c2008-05-09 06:08:39 +000032 PointerWidth = PointerAlign = 32;
Roman Divacky965b0b72011-01-06 08:27:10 +000033 BoolWidth = BoolAlign = 8;
Chris Lattnerb781dc792008-05-08 05:58:21 +000034 IntWidth = IntAlign = 32;
Chris Lattner5a9aaaf2008-05-09 05:50:02 +000035 LongWidth = LongAlign = 32;
36 LongLongWidth = LongLongAlign = 64;
Eli Friedmanb5366062008-05-20 14:21:01 +000037 FloatWidth = 32;
38 FloatAlign = 32;
Nate Begeman65bea232008-04-18 17:17:24 +000039 DoubleWidth = 64;
Eli Friedmanb5366062008-05-20 14:21:01 +000040 DoubleAlign = 64;
41 LongDoubleWidth = 64;
42 LongDoubleAlign = 64;
Rafael Espindolae971b9a2010-06-04 23:15:27 +000043 LargeArrayMinWidth = 0;
44 LargeArrayAlign = 0;
Anders Carlsson2a79a902008-10-31 16:05:19 +000045 SizeType = UnsignedLong;
Eli Friedmand50881c2008-11-02 02:43:55 +000046 PtrDiffType = SignedLong;
Sanjiv Guptad7959242008-10-31 09:52:39 +000047 IntMaxType = SignedLongLong;
48 UIntMaxType = UnsignedLongLong;
Chris Lattner7e4c81c2009-02-13 22:28:55 +000049 IntPtrType = SignedLong;
Eli Friedmand50881c2008-11-02 02:43:55 +000050 WCharType = SignedInt;
Chris Lattner67204922009-10-21 04:59:34 +000051 WIntType = SignedInt;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +000052 Char16Type = UnsignedShort;
53 Char32Type = UnsignedInt;
Eli Friedman2857ccb2009-07-01 03:36:11 +000054 Int64Type = SignedLongLong;
Edward O'Callaghan847f2a12009-11-21 00:49:54 +000055 SigAtomicType = SignedInt;
Daniel Dunbar1da65112010-04-15 15:06:18 +000056 UseBitFieldTypeAlignment = true;
Chad Rosier18903ee2011-08-04 01:21:14 +000057 UseZeroLengthBitfieldAlignment = false;
58 ZeroLengthBitfieldBoundary = 0;
Chris Lattner1df27862008-03-08 08:59:43 +000059 FloatFormat = &llvm::APFloat::IEEEsingle;
60 DoubleFormat = &llvm::APFloat::IEEEdouble;
61 LongDoubleFormat = &llvm::APFloat::IEEEdouble;
Eli Friedman873f65a2008-08-21 00:13:15 +000062 DescriptionString = "E-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
Chris Lattner5c67237f2009-11-07 18:59:41 +000063 "i64:64:64-f32:32:32-f64:64:64-n32";
Chris Lattnerf37bafc2008-10-05 19:22:37 +000064 UserLabelPrefix = "_";
Roman Divacky178e01602011-02-10 16:52:03 +000065 MCountName = "mcount";
Abramo Bagnara41bfb662011-09-08 14:20:25 +000066 RegParmMax = 0;
67 SSERegParmMax = 0;
Daniel Dunbarbd606522010-05-27 00:35:16 +000068 HasAlignMac68kSupport = false;
Daniel Dunbar6f2e8392010-07-14 23:39:36 +000069
70 // Default to no types using fpret.
71 RealTypeUsesObjCFPRet = 0;
John McCall86353412010-08-21 22:46:04 +000072
73 // Default to using the Itanium ABI.
74 CXXABI = CXXABI_Itanium;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +000075
76 // Default to an empty address space map.
77 AddrSpaceMap = &DefaultAddrSpaceMap;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +000078
79 // Default to an unknown platform name.
80 PlatformName = "unknown";
81 PlatformMinVersion = VersionTuple();
Chris Lattner1df27862008-03-08 08:59:43 +000082}
83
Chris Lattnerc3a669b2008-03-08 08:24:01 +000084// Out of line virtual dtor for TargetInfo.
85TargetInfo::~TargetInfo() {}
Chris Lattner2194ddc2006-10-14 18:32:26 +000086
Chris Lattnera91c30f2009-02-06 05:04:11 +000087/// getTypeName - Return the user string for the specified integer type enum.
88/// For example, SignedShort -> "short".
89const char *TargetInfo::getTypeName(IntType T) {
90 switch (T) {
David Blaikie83d382b2011-09-23 05:06:16 +000091 default: llvm_unreachable("not an integer!");
Chris Lattnera91c30f2009-02-06 05:04:11 +000092 case SignedShort: return "short";
93 case UnsignedShort: return "unsigned short";
94 case SignedInt: return "int";
95 case UnsignedInt: return "unsigned int";
96 case SignedLong: return "long int";
97 case UnsignedLong: return "long unsigned int";
98 case SignedLongLong: return "long long int";
99 case UnsignedLongLong: return "long long unsigned int";
100 }
101}
102
Chris Lattner72cdcac2009-10-21 06:24:21 +0000103/// getTypeConstantSuffix - Return the constant suffix for the specified
104/// integer type enum. For example, SignedLong -> "L".
105const char *TargetInfo::getTypeConstantSuffix(IntType T) {
106 switch (T) {
David Blaikie83d382b2011-09-23 05:06:16 +0000107 default: llvm_unreachable("not an integer!");
Chris Lattner72cdcac2009-10-21 06:24:21 +0000108 case SignedShort:
109 case SignedInt: return "";
110 case SignedLong: return "L";
111 case SignedLongLong: return "LL";
112 case UnsignedShort:
113 case UnsignedInt: return "U";
114 case UnsignedLong: return "UL";
115 case UnsignedLongLong: return "ULL";
116 }
117}
118
Michael J. Spencerfeb799c2010-10-18 07:10:59 +0000119/// getTypeWidth - Return the width (in bits) of the specified integer type
Chris Lattner72cdcac2009-10-21 06:24:21 +0000120/// enum. For example, SignedInt -> getIntWidth().
121unsigned TargetInfo::getTypeWidth(IntType T) const {
122 switch (T) {
David Blaikie83d382b2011-09-23 05:06:16 +0000123 default: llvm_unreachable("not an integer!");
Chris Lattnere4a8c642009-11-05 21:21:32 +0000124 case SignedShort:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000125 case UnsignedShort: return getShortWidth();
Chris Lattnere4a8c642009-11-05 21:21:32 +0000126 case SignedInt:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000127 case UnsignedInt: return getIntWidth();
Chris Lattnere4a8c642009-11-05 21:21:32 +0000128 case SignedLong:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000129 case UnsignedLong: return getLongWidth();
Chris Lattnere4a8c642009-11-05 21:21:32 +0000130 case SignedLongLong:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000131 case UnsignedLongLong: return getLongLongWidth();
132 };
133}
134
Michael J. Spencerfeb799c2010-10-18 07:10:59 +0000135/// getTypeAlign - Return the alignment (in bits) of the specified integer type
Chris Lattnere4a8c642009-11-05 21:21:32 +0000136/// enum. For example, SignedInt -> getIntAlign().
137unsigned TargetInfo::getTypeAlign(IntType T) const {
138 switch (T) {
David Blaikie83d382b2011-09-23 05:06:16 +0000139 default: llvm_unreachable("not an integer!");
Chris Lattnere4a8c642009-11-05 21:21:32 +0000140 case SignedShort:
141 case UnsignedShort: return getShortAlign();
142 case SignedInt:
143 case UnsignedInt: return getIntAlign();
144 case SignedLong:
145 case UnsignedLong: return getLongAlign();
146 case SignedLongLong:
147 case UnsignedLongLong: return getLongLongAlign();
148 };
149}
150
Chris Lattnerc0c04392009-10-25 22:49:18 +0000151/// isTypeSigned - Return whether an integer types is signed. Returns true if
Chris Lattner72cdcac2009-10-21 06:24:21 +0000152/// the type is signed; false otherwise.
Chris Lattnerad3467e2010-12-25 23:25:43 +0000153bool TargetInfo::isTypeSigned(IntType T) {
Chris Lattner72cdcac2009-10-21 06:24:21 +0000154 switch (T) {
David Blaikie83d382b2011-09-23 05:06:16 +0000155 default: llvm_unreachable("not an integer!");
Chris Lattner72cdcac2009-10-21 06:24:21 +0000156 case SignedShort:
157 case SignedInt:
158 case SignedLong:
Michael J. Spencerfeb799c2010-10-18 07:10:59 +0000159 case SignedLongLong:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000160 return true;
161 case UnsignedShort:
162 case UnsignedInt:
163 case UnsignedLong:
Michael J. Spencerfeb799c2010-10-18 07:10:59 +0000164 case UnsignedLongLong:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000165 return false;
166 };
167}
168
John Thompsoned4e2952009-11-05 20:14:16 +0000169/// setForcedLangOptions - Set forced language options.
170/// Apply changes to the target information with respect to certain
171/// language options which change the target configuration.
172void TargetInfo::setForcedLangOptions(LangOptions &Opts) {
Daniel Dunbar9302f602010-04-15 15:06:22 +0000173 if (Opts.NoBitFieldTypeAlign)
174 UseBitFieldTypeAlignment = false;
175 if (Opts.ShortWChar)
John Thompsoned4e2952009-11-05 20:14:16 +0000176 WCharType = UnsignedShort;
John Thompsoned4e2952009-11-05 20:14:16 +0000177}
Chris Lattner72cdcac2009-10-21 06:24:21 +0000178
Chris Lattnerec0a6d92007-09-22 18:29:59 +0000179//===----------------------------------------------------------------------===//
Chris Lattnerec0a6d92007-09-22 18:29:59 +0000180
Chris Lattner10a5b382007-01-29 05:24:35 +0000181
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000182static StringRef removeGCCRegisterPrefix(StringRef Name) {
Anders Carlsson3ed4aea2008-02-06 00:11:32 +0000183 if (Name[0] == '%' || Name[0] == '#')
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000184 Name = Name.substr(1);
Michael J. Spencerfeb799c2010-10-18 07:10:59 +0000185
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000186 return Name;
Anders Carlsson3ed4aea2008-02-06 00:11:32 +0000187}
188
Eric Christopherfd9a5f42011-06-28 18:20:53 +0000189/// isValidClobber - Returns whether the passed in string is
190/// a valid clobber in an inline asm statement. This is used by
191/// Sema.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000192bool TargetInfo::isValidClobber(StringRef Name) const {
Eric Christopherfd9a5f42011-06-28 18:20:53 +0000193 return (isValidGCCRegisterName(Name) ||
194 Name == "memory" || Name == "cc");
195}
196
Anders Carlsson5fa3f342007-11-24 23:38:12 +0000197/// isValidGCCRegisterName - Returns whether the passed in string
198/// is a valid register name according to GCC. This is used by Sema for
199/// inline asm statements.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000200bool TargetInfo::isValidGCCRegisterName(StringRef Name) const {
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000201 if (Name.empty())
202 return false;
Michael J. Spencerfeb799c2010-10-18 07:10:59 +0000203
Anders Carlsson290aa852007-11-25 00:25:21 +0000204 const char * const *Names;
205 unsigned NumNames;
Mike Stump11289f42009-09-09 15:08:12 +0000206
Anders Carlsson290aa852007-11-25 00:25:21 +0000207 // Get rid of any register prefix.
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000208 Name = removeGCCRegisterPrefix(Name);
Anders Carlsson3ed4aea2008-02-06 00:11:32 +0000209
Chris Lattnerc3a669b2008-03-08 08:24:01 +0000210 getGCCRegNames(Names, NumNames);
Mike Stump11289f42009-09-09 15:08:12 +0000211
Anders Carlsson290aa852007-11-25 00:25:21 +0000212 // If we have a number it maps to an entry in the register name array.
213 if (isdigit(Name[0])) {
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000214 int n;
215 if (!Name.getAsInteger(0, n))
Anders Carlsson290aa852007-11-25 00:25:21 +0000216 return n >= 0 && (unsigned)n < NumNames;
217 }
218
219 // Check register names.
220 for (unsigned i = 0; i < NumNames; i++) {
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000221 if (Name == Names[i])
Anders Carlsson290aa852007-11-25 00:25:21 +0000222 return true;
223 }
Mike Stump11289f42009-09-09 15:08:12 +0000224
Eric Christophercdd36352011-06-21 00:05:20 +0000225 // Check any additional names that we have.
226 const AddlRegName *AddlNames;
227 unsigned NumAddlNames;
228 getGCCAddlRegNames(AddlNames, NumAddlNames);
229 for (unsigned i = 0; i < NumAddlNames; i++)
230 for (unsigned j = 0; j < llvm::array_lengthof(AddlNames[i].Names); j++) {
231 if (!AddlNames[i].Names[j])
232 break;
233 // Make sure the register that the additional name is for is within
234 // the bounds of the register names from above.
235 if (AddlNames[i].Names[j] == Name && AddlNames[i].RegNum < NumNames)
236 return true;
237 }
238
Anders Carlsson290aa852007-11-25 00:25:21 +0000239 // Now check aliases.
Chris Lattnerc3a669b2008-03-08 08:24:01 +0000240 const GCCRegAlias *Aliases;
Anders Carlsson290aa852007-11-25 00:25:21 +0000241 unsigned NumAliases;
Mike Stump11289f42009-09-09 15:08:12 +0000242
Chris Lattnerc3a669b2008-03-08 08:24:01 +0000243 getGCCRegAliases(Aliases, NumAliases);
Anders Carlsson290aa852007-11-25 00:25:21 +0000244 for (unsigned i = 0; i < NumAliases; i++) {
245 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
246 if (!Aliases[i].Aliases[j])
247 break;
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000248 if (Aliases[i].Aliases[j] == Name)
Anders Carlsson290aa852007-11-25 00:25:21 +0000249 return true;
250 }
251 }
Mike Stump11289f42009-09-09 15:08:12 +0000252
Anders Carlsson5fa3f342007-11-24 23:38:12 +0000253 return false;
254}
Anders Carlssonf511f642007-11-27 04:11:28 +0000255
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000256StringRef
257TargetInfo::getNormalizedGCCRegisterName(StringRef Name) const {
Anders Carlssonf511f642007-11-27 04:11:28 +0000258 assert(isValidGCCRegisterName(Name) && "Invalid register passed in");
Mike Stump11289f42009-09-09 15:08:12 +0000259
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000260 // Get rid of any register prefix.
261 Name = removeGCCRegisterPrefix(Name);
Mike Stump11289f42009-09-09 15:08:12 +0000262
Anders Carlssonf511f642007-11-27 04:11:28 +0000263 const char * const *Names;
264 unsigned NumNames;
265
Chris Lattnerc3a669b2008-03-08 08:24:01 +0000266 getGCCRegNames(Names, NumNames);
Anders Carlssonf511f642007-11-27 04:11:28 +0000267
268 // First, check if we have a number.
269 if (isdigit(Name[0])) {
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000270 int n;
271 if (!Name.getAsInteger(0, n)) {
Mike Stump11289f42009-09-09 15:08:12 +0000272 assert(n >= 0 && (unsigned)n < NumNames &&
Anders Carlssonf511f642007-11-27 04:11:28 +0000273 "Out of bounds register number!");
274 return Names[n];
275 }
276 }
Mike Stump11289f42009-09-09 15:08:12 +0000277
Eric Christophercdd36352011-06-21 00:05:20 +0000278 // Check any additional names that we have.
279 const AddlRegName *AddlNames;
280 unsigned NumAddlNames;
281 getGCCAddlRegNames(AddlNames, NumAddlNames);
282 for (unsigned i = 0; i < NumAddlNames; i++)
283 for (unsigned j = 0; j < llvm::array_lengthof(AddlNames[i].Names); j++) {
284 if (!AddlNames[i].Names[j])
285 break;
286 // Make sure the register that the additional name is for is within
287 // the bounds of the register names from above.
288 if (AddlNames[i].Names[j] == Name && AddlNames[i].RegNum < NumNames)
289 return Name;
290 }
291
Anders Carlssonf511f642007-11-27 04:11:28 +0000292 // Now check aliases.
Chris Lattnerc3a669b2008-03-08 08:24:01 +0000293 const GCCRegAlias *Aliases;
Anders Carlssonf511f642007-11-27 04:11:28 +0000294 unsigned NumAliases;
Mike Stump11289f42009-09-09 15:08:12 +0000295
Chris Lattnerc3a669b2008-03-08 08:24:01 +0000296 getGCCRegAliases(Aliases, NumAliases);
Anders Carlssonf511f642007-11-27 04:11:28 +0000297 for (unsigned i = 0; i < NumAliases; i++) {
298 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
299 if (!Aliases[i].Aliases[j])
300 break;
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000301 if (Aliases[i].Aliases[j] == Name)
Anders Carlssonf511f642007-11-27 04:11:28 +0000302 return Aliases[i].Register;
303 }
304 }
Mike Stump11289f42009-09-09 15:08:12 +0000305
Anders Carlssonf511f642007-11-27 04:11:28 +0000306 return Name;
307}
308
Chris Lattnerc3f4c7b2009-04-26 17:19:08 +0000309bool TargetInfo::validateOutputConstraint(ConstraintInfo &Info) const {
310 const char *Name = Info.getConstraintStr().c_str();
Anders Carlssonf511f642007-11-27 04:11:28 +0000311 // An output constraint must start with '=' or '+'
312 if (*Name != '=' && *Name != '+')
313 return false;
314
315 if (*Name == '+')
Chris Lattnerd9725f72009-04-26 07:16:29 +0000316 Info.setIsReadWrite();
Anders Carlssonf511f642007-11-27 04:11:28 +0000317
318 Name++;
319 while (*Name) {
320 switch (*Name) {
321 default:
Chris Lattnerd9725f72009-04-26 07:16:29 +0000322 if (!validateAsmConstraint(Name, Info)) {
Ted Kremenekb44485b2008-04-24 16:36:38 +0000323 // FIXME: We temporarily return false
Anders Carlssonf511f642007-11-27 04:11:28 +0000324 // so we can add more constraints as we hit it.
325 // Eventually, an unknown constraint should just be treated as 'g'.
Ted Kremenekb44485b2008-04-24 16:36:38 +0000326 return false;
Anders Carlssonf511f642007-11-27 04:11:28 +0000327 }
328 case '&': // early clobber.
329 break;
Chris Lattnerf3154712009-10-13 04:32:07 +0000330 case '%': // commutative.
331 // FIXME: Check that there is a another register after this one.
332 break;
Anders Carlssonf511f642007-11-27 04:11:28 +0000333 case 'r': // general register.
Chris Lattnerd9725f72009-04-26 07:16:29 +0000334 Info.setAllowsRegister();
Anders Carlssonf511f642007-11-27 04:11:28 +0000335 break;
336 case 'm': // memory operand.
Dale Johannesen8499f472010-09-07 18:40:41 +0000337 case 'o': // offsetable memory operand.
338 case 'V': // non-offsetable memory operand.
339 case '<': // autodecrement memory operand.
340 case '>': // autoincrement memory operand.
Chris Lattnerd9725f72009-04-26 07:16:29 +0000341 Info.setAllowsMemory();
Anders Carlssonf511f642007-11-27 04:11:28 +0000342 break;
343 case 'g': // general register, memory operand or immediate integer.
Anders Carlssone70cde12009-01-18 02:12:04 +0000344 case 'X': // any operand.
Chris Lattnerd9725f72009-04-26 07:16:29 +0000345 Info.setAllowsRegister();
346 Info.setAllowsMemory();
Anders Carlssonf511f642007-11-27 04:11:28 +0000347 break;
John Thompsona5c7d702010-08-10 19:20:14 +0000348 case ',': // multiple alternative constraint. Pass it.
John Thompson46667af2010-08-11 00:58:20 +0000349 // Handle additional optional '=' or '+' modifiers.
John Thompson12240612010-09-18 01:15:13 +0000350 if (Name[1] == '=' || Name[1] == '+')
John Thompson46667af2010-08-11 00:58:20 +0000351 Name++;
John Thompsona5c7d702010-08-10 19:20:14 +0000352 break;
353 case '?': // Disparage slightly code.
Dale Johannesen8499f472010-09-07 18:40:41 +0000354 case '!': // Disparage severely.
John Thompsona5c7d702010-08-10 19:20:14 +0000355 break; // Pass them.
Anders Carlssonf511f642007-11-27 04:11:28 +0000356 }
Mike Stump11289f42009-09-09 15:08:12 +0000357
Anders Carlssonf511f642007-11-27 04:11:28 +0000358 Name++;
359 }
Mike Stump11289f42009-09-09 15:08:12 +0000360
Anders Carlssonf511f642007-11-27 04:11:28 +0000361 return true;
362}
363
Anders Carlssona79203b2009-01-18 01:56:57 +0000364bool TargetInfo::resolveSymbolicName(const char *&Name,
Chris Lattnerc16d4762009-04-26 17:57:12 +0000365 ConstraintInfo *OutputConstraints,
366 unsigned NumOutputs,
Chris Lattnerd9725f72009-04-26 07:16:29 +0000367 unsigned &Index) const {
Anders Carlssona79203b2009-01-18 01:56:57 +0000368 assert(*Name == '[' && "Symbolic name did not start with '['");
Anders Carlssona79203b2009-01-18 01:56:57 +0000369 Name++;
370 const char *Start = Name;
371 while (*Name && *Name != ']')
372 Name++;
Mike Stump11289f42009-09-09 15:08:12 +0000373
Anders Carlssona79203b2009-01-18 01:56:57 +0000374 if (!*Name) {
375 // Missing ']'
376 return false;
377 }
Mike Stump11289f42009-09-09 15:08:12 +0000378
Anders Carlssona79203b2009-01-18 01:56:57 +0000379 std::string SymbolicName(Start, Name - Start);
Mike Stump11289f42009-09-09 15:08:12 +0000380
Chris Lattnerc16d4762009-04-26 17:57:12 +0000381 for (Index = 0; Index != NumOutputs; ++Index)
382 if (SymbolicName == OutputConstraints[Index].getName())
Anders Carlssona79203b2009-01-18 01:56:57 +0000383 return true;
Anders Carlssona79203b2009-01-18 01:56:57 +0000384
385 return false;
386}
387
Chris Lattnerc16d4762009-04-26 17:57:12 +0000388bool TargetInfo::validateInputConstraint(ConstraintInfo *OutputConstraints,
389 unsigned NumOutputs,
Chris Lattnerd9725f72009-04-26 07:16:29 +0000390 ConstraintInfo &Info) const {
Chris Lattnerc3f4c7b2009-04-26 17:19:08 +0000391 const char *Name = Info.ConstraintStr.c_str();
392
Anders Carlssonf511f642007-11-27 04:11:28 +0000393 while (*Name) {
394 switch (*Name) {
395 default:
396 // Check if we have a matching constraint
397 if (*Name >= '0' && *Name <= '9') {
398 unsigned i = *Name - '0';
Mike Stump11289f42009-09-09 15:08:12 +0000399
Anders Carlssonf511f642007-11-27 04:11:28 +0000400 // Check if matching constraint is out of bounds.
401 if (i >= NumOutputs)
402 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000403
Anders Carlssonda1f5fc2010-11-03 02:22:29 +0000404 // A number must refer to an output only operand.
405 if (OutputConstraints[i].isReadWrite())
406 return false;
407
Anders Carlsson2d5f8b42010-11-03 02:54:51 +0000408 // If the constraint is already tied, it must be tied to the
409 // same operand referenced to by the number.
410 if (Info.hasTiedOperand() && Info.getTiedOperand() != i)
411 return false;
412
Mike Stump11289f42009-09-09 15:08:12 +0000413 // The constraint should have the same info as the respective
Anders Carlsson570c3572009-01-27 20:38:24 +0000414 // output constraint.
Chris Lattner4c92b512009-04-26 18:05:25 +0000415 Info.setTiedOperand(i, OutputConstraints[i]);
Chris Lattnerd9725f72009-04-26 07:16:29 +0000416 } else if (!validateAsmConstraint(Name, Info)) {
Daniel Dunbar81128e02008-08-25 09:46:27 +0000417 // FIXME: This error return is in place temporarily so we can
418 // add more constraints as we hit it. Eventually, an unknown
419 // constraint should just be treated as 'g'.
420 return false;
Anders Carlssona79203b2009-01-18 01:56:57 +0000421 }
422 break;
423 case '[': {
424 unsigned Index = 0;
Chris Lattnerc16d4762009-04-26 17:57:12 +0000425 if (!resolveSymbolicName(Name, OutputConstraints, NumOutputs, Index))
Anders Carlssona79203b2009-01-18 01:56:57 +0000426 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000427
Anders Carlsson2d5f8b42010-11-03 02:54:51 +0000428 // If the constraint is already tied, it must be tied to the
429 // same operand referenced to by the number.
430 if (Info.hasTiedOperand() && Info.getTiedOperand() != Index)
431 return false;
432
Eli Friedman854f1102010-08-11 23:03:37 +0000433 Info.setTiedOperand(Index, OutputConstraints[Index]);
Anders Carlssona79203b2009-01-18 01:56:57 +0000434 break;
Mike Stump11289f42009-09-09 15:08:12 +0000435 }
Anders Carlsson050f4942007-12-08 19:32:57 +0000436 case '%': // commutative
437 // FIXME: Fail if % is used with the last operand.
438 break;
Anders Carlssonf511f642007-11-27 04:11:28 +0000439 case 'i': // immediate integer.
Anders Carlssona3a96af2008-03-09 06:02:02 +0000440 case 'n': // immediate integer with a known value.
Anders Carlssonf511f642007-11-27 04:11:28 +0000441 break;
Chris Lattnerdbcc5ca2009-05-06 04:33:31 +0000442 case 'I': // Various constant constraints with target-specific meanings.
443 case 'J':
444 case 'K':
445 case 'L':
446 case 'M':
447 case 'N':
448 case 'O':
449 case 'P':
450 break;
Anders Carlssonf511f642007-11-27 04:11:28 +0000451 case 'r': // general register.
Chris Lattnerd9725f72009-04-26 07:16:29 +0000452 Info.setAllowsRegister();
Anders Carlssonf511f642007-11-27 04:11:28 +0000453 break;
454 case 'm': // memory operand.
Dale Johannesen8499f472010-09-07 18:40:41 +0000455 case 'o': // offsettable memory operand.
456 case 'V': // non-offsettable memory operand.
457 case '<': // autodecrement memory operand.
458 case '>': // autoincrement memory operand.
Chris Lattnerd9725f72009-04-26 07:16:29 +0000459 Info.setAllowsMemory();
Anders Carlssonf511f642007-11-27 04:11:28 +0000460 break;
461 case 'g': // general register, memory operand or immediate integer.
Anders Carlssone70cde12009-01-18 02:12:04 +0000462 case 'X': // any operand.
Chris Lattnerd9725f72009-04-26 07:16:29 +0000463 Info.setAllowsRegister();
464 Info.setAllowsMemory();
Anders Carlssonf511f642007-11-27 04:11:28 +0000465 break;
John Thompsonc467aa22010-09-21 22:04:54 +0000466 case 'E': // immediate floating point.
467 case 'F': // immediate floating point.
468 case 'p': // address operand.
469 break;
John Thompsona5c7d702010-08-10 19:20:14 +0000470 case ',': // multiple alternative constraint. Ignore comma.
471 break;
472 case '?': // Disparage slightly code.
Chris Lattner57540c52011-04-15 05:22:18 +0000473 case '!': // Disparage severely.
John Thompsona5c7d702010-08-10 19:20:14 +0000474 break; // Pass them.
Anders Carlssonf511f642007-11-27 04:11:28 +0000475 }
Mike Stump11289f42009-09-09 15:08:12 +0000476
Anders Carlssonf511f642007-11-27 04:11:28 +0000477 Name++;
478 }
Mike Stump11289f42009-09-09 15:08:12 +0000479
Anders Carlssonf511f642007-11-27 04:11:28 +0000480 return true;
481}