blob: 20ec599960df0373785bc1439486ced670e98925 [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
14#include "clang/Basic/TargetInfo.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000015#include "clang/Basic/AddressSpaces.h"
Jordan Rose3f6f51e2013-02-08 22:30:41 +000016#include "clang/Basic/CharInfo.h"
John Thompsona6fda122009-11-05 20:14:16 +000017#include "clang/Basic/LangOptions.h"
Chris Lattner525a0502007-09-22 18:29:59 +000018#include "llvm/ADT/APFloat.h"
Anders Carlsson6fa90862007-11-25 00:25:21 +000019#include "llvm/ADT/STLExtras.h"
David Blaikie9fe8c742011-09-23 05:35:21 +000020#include "llvm/Support/ErrorHandling.h"
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.
Benjamin Kramer9df08232013-06-29 16:37:14 +000027TargetInfo::TargetInfo(const llvm::Triple &T) : TargetOpts(), 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;
Ulrich Weigand6b203512013-05-06 16:23:57 +000039 MinGlobalAlign = 0;
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +000040 HalfWidth = 16;
41 HalfAlign = 16;
Eli Friedman61538a72008-05-20 14:21:01 +000042 FloatWidth = 32;
43 FloatAlign = 32;
Nate Begeman3d292062008-04-18 17:17:24 +000044 DoubleWidth = 64;
Eli Friedman61538a72008-05-20 14:21:01 +000045 DoubleAlign = 64;
46 LongDoubleWidth = 64;
47 LongDoubleAlign = 64;
Rafael Espindola6deecb02010-06-04 23:15:27 +000048 LargeArrayMinWidth = 0;
49 LargeArrayAlign = 0;
Eli Friedman2be46072011-10-14 20:59:01 +000050 MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 0;
Chad Rosierf9e9af72012-07-13 23:57:43 +000051 MaxVectorAlign = 0;
Anders Carlsson6a3615c2008-10-31 16:05:19 +000052 SizeType = UnsignedLong;
Eli Friedmanf509d732008-11-02 02:43:55 +000053 PtrDiffType = SignedLong;
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +000054 IntMaxType = SignedLongLong;
55 UIntMaxType = UnsignedLongLong;
Chris Lattner6ad474f2009-02-13 22:28:55 +000056 IntPtrType = SignedLong;
Eli Friedmanf509d732008-11-02 02:43:55 +000057 WCharType = SignedInt;
Chris Lattnere64ef802009-10-21 04:59:34 +000058 WIntType = SignedInt;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +000059 Char16Type = UnsignedShort;
60 Char32Type = UnsignedInt;
Eli Friedman3c7b6e42009-07-01 03:36:11 +000061 Int64Type = SignedLongLong;
Edward O'Callaghan9cf910e2009-11-21 00:49:54 +000062 SigAtomicType = SignedInt;
Eli Friedman6902e412012-11-27 02:58:24 +000063 ProcessIDType = SignedInt;
Fariborz Jahanian93a49942012-04-16 21:03:30 +000064 UseSignedCharForObjCBool = true;
Daniel Dunbarb6830d62010-04-15 15:06:18 +000065 UseBitFieldTypeAlignment = true;
Chad Rosier61a62212011-08-04 01:21:14 +000066 UseZeroLengthBitfieldAlignment = false;
67 ZeroLengthBitfieldBoundary = 0;
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +000068 HalfFormat = &llvm::APFloat::IEEEhalf;
Chris Lattnercd4fc422008-03-08 08:59:43 +000069 FloatFormat = &llvm::APFloat::IEEEsingle;
70 DoubleFormat = &llvm::APFloat::IEEEdouble;
71 LongDoubleFormat = &llvm::APFloat::IEEEdouble;
Eli Friedmaned855cb2008-08-21 00:13:15 +000072 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 +000073 "i64:64:64-f32:32:32-f64:64:64-n32";
Chris Lattner3fdf4672008-10-05 19:22:37 +000074 UserLabelPrefix = "_";
Roman Divackybe4c8702011-02-10 16:52:03 +000075 MCountName = "mcount";
Abramo Bagnara62f940b2011-09-08 14:20:25 +000076 RegParmMax = 0;
77 SSERegParmMax = 0;
Daniel Dunbar613fd672010-05-27 00:35:16 +000078 HasAlignMac68kSupport = false;
Daniel Dunbardacf9dd2010-07-14 23:39:36 +000079
80 // Default to no types using fpret.
81 RealTypeUsesObjCFPRet = 0;
John McCallee79a4c2010-08-21 22:46:04 +000082
Anders Carlssoneea64802011-10-31 16:27:11 +000083 // Default to not using fp2ret for __Complex long double
84 ComplexLongDoubleUsesFP2Ret = false;
85
John McCallee79a4c2010-08-21 22:46:04 +000086 // Default to using the Itanium ABI.
John McCallb8b2c9d2013-01-25 22:30:49 +000087 TheCXXABI.set(TargetCXXABI::GenericItanium);
Peter Collingbourne207f4d82011-03-18 22:38:29 +000088
89 // Default to an empty address space map.
90 AddrSpaceMap = &DefaultAddrSpaceMap;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +000091
92 // Default to an unknown platform name.
93 PlatformName = "unknown";
94 PlatformMinVersion = VersionTuple();
Chris Lattnercd4fc422008-03-08 08:59:43 +000095}
96
Chris Lattner0eaed122008-03-08 08:24:01 +000097// Out of line virtual dtor for TargetInfo.
98TargetInfo::~TargetInfo() {}
Reid Spencer5f016e22007-07-11 17:01:13 +000099
Chris Lattner2b5abf52009-02-06 05:04:11 +0000100/// getTypeName - Return the user string for the specified integer type enum.
101/// For example, SignedShort -> "short".
102const char *TargetInfo::getTypeName(IntType T) {
103 switch (T) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000104 default: llvm_unreachable("not an integer!");
Stepan Dyatkovskiy7b7bef12013-09-05 11:23:21 +0000105 case SignedChar: return "char";
106 case UnsignedChar: return "unsigned char";
Chris Lattner2b5abf52009-02-06 05:04:11 +0000107 case SignedShort: return "short";
108 case UnsignedShort: return "unsigned short";
109 case SignedInt: return "int";
110 case UnsignedInt: return "unsigned int";
111 case SignedLong: return "long int";
112 case UnsignedLong: return "long unsigned int";
113 case SignedLongLong: return "long long int";
114 case UnsignedLongLong: return "long long unsigned int";
115 }
116}
117
Chris Lattnerb304f772009-10-21 06:24:21 +0000118/// getTypeConstantSuffix - Return the constant suffix for the specified
119/// integer type enum. For example, SignedLong -> "L".
120const char *TargetInfo::getTypeConstantSuffix(IntType T) {
121 switch (T) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000122 default: llvm_unreachable("not an integer!");
Stepan Dyatkovskiy7b7bef12013-09-05 11:23:21 +0000123 case SignedChar:
Chris Lattnerb304f772009-10-21 06:24:21 +0000124 case SignedShort:
125 case SignedInt: return "";
126 case SignedLong: return "L";
127 case SignedLongLong: return "LL";
Stepan Dyatkovskiy7b7bef12013-09-05 11:23:21 +0000128 case UnsignedChar:
Chris Lattnerb304f772009-10-21 06:24:21 +0000129 case UnsignedShort:
130 case UnsignedInt: return "U";
131 case UnsignedLong: return "UL";
132 case UnsignedLongLong: return "ULL";
133 }
134}
135
Michael J. Spencer237cf582010-10-18 07:10:59 +0000136/// getTypeWidth - Return the width (in bits) of the specified integer type
Chris Lattnerb304f772009-10-21 06:24:21 +0000137/// enum. For example, SignedInt -> getIntWidth().
138unsigned TargetInfo::getTypeWidth(IntType T) const {
139 switch (T) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000140 default: llvm_unreachable("not an integer!");
Stepan Dyatkovskiy7b7bef12013-09-05 11:23:21 +0000141 case SignedChar:
142 case UnsignedChar: return getCharWidth();
Chris Lattner9099e7b2009-11-05 21:21:32 +0000143 case SignedShort:
Chris Lattnerb304f772009-10-21 06:24:21 +0000144 case UnsignedShort: return getShortWidth();
Chris Lattner9099e7b2009-11-05 21:21:32 +0000145 case SignedInt:
Chris Lattnerb304f772009-10-21 06:24:21 +0000146 case UnsignedInt: return getIntWidth();
Chris Lattner9099e7b2009-11-05 21:21:32 +0000147 case SignedLong:
Chris Lattnerb304f772009-10-21 06:24:21 +0000148 case UnsignedLong: return getLongWidth();
Chris Lattner9099e7b2009-11-05 21:21:32 +0000149 case SignedLongLong:
Chris Lattnerb304f772009-10-21 06:24:21 +0000150 case UnsignedLongLong: return getLongLongWidth();
151 };
152}
153
Stepan Dyatkovskiy7b7bef12013-09-05 11:23:21 +0000154TargetInfo::IntType TargetInfo::getIntTypeByWidth(
155 unsigned BitWidth, bool IsSigned) const {
156 if (getCharWidth() == BitWidth)
157 return IsSigned ? SignedChar : UnsignedChar;
158 if (getShortWidth() == BitWidth)
159 return IsSigned ? SignedShort : UnsignedShort;
160 if (getIntWidth() == BitWidth)
161 return IsSigned ? SignedInt : UnsignedInt;
162 if (getLongWidth() == BitWidth)
163 return IsSigned ? SignedLong : UnsignedLong;
164 if (getLongLongWidth() == BitWidth)
165 return IsSigned ? SignedLongLong : UnsignedLongLong;
166 return NoInt;
167}
168
169TargetInfo::RealType TargetInfo::getRealTypeByWidth(unsigned BitWidth) const {
170 if (getFloatWidth() == BitWidth)
171 return Float;
172 if (getDoubleWidth() == BitWidth)
173 return Double;
174
175 switch (BitWidth) {
176 case 96:
177 if (&getLongDoubleFormat() == &llvm::APFloat::x87DoubleExtended)
178 return LongDouble;
179 break;
180 case 128:
Stepan Dyatkovskiy35451d92013-09-09 07:46:54 +0000181 if (&getLongDoubleFormat() == &llvm::APFloat::PPCDoubleDouble ||
182 &getLongDoubleFormat() == &llvm::APFloat::IEEEquad)
Stepan Dyatkovskiy7b7bef12013-09-05 11:23:21 +0000183 return LongDouble;
184 break;
185 }
186
187 return NoFloat;
188}
189
Michael J. Spencer237cf582010-10-18 07:10:59 +0000190/// getTypeAlign - Return the alignment (in bits) of the specified integer type
Chris Lattner9099e7b2009-11-05 21:21:32 +0000191/// enum. For example, SignedInt -> getIntAlign().
192unsigned TargetInfo::getTypeAlign(IntType T) const {
193 switch (T) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000194 default: llvm_unreachable("not an integer!");
Stepan Dyatkovskiy7b7bef12013-09-05 11:23:21 +0000195 case SignedChar:
196 case UnsignedChar: return getCharAlign();
Chris Lattner9099e7b2009-11-05 21:21:32 +0000197 case SignedShort:
198 case UnsignedShort: return getShortAlign();
199 case SignedInt:
200 case UnsignedInt: return getIntAlign();
201 case SignedLong:
202 case UnsignedLong: return getLongAlign();
203 case SignedLongLong:
204 case UnsignedLongLong: return getLongLongAlign();
205 };
206}
207
Chris Lattner961f0702009-10-25 22:49:18 +0000208/// isTypeSigned - Return whether an integer types is signed. Returns true if
Chris Lattnerb304f772009-10-21 06:24:21 +0000209/// the type is signed; false otherwise.
Chris Lattner3f59c972010-12-25 23:25:43 +0000210bool TargetInfo::isTypeSigned(IntType T) {
Chris Lattnerb304f772009-10-21 06:24:21 +0000211 switch (T) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000212 default: llvm_unreachable("not an integer!");
Stepan Dyatkovskiy7b7bef12013-09-05 11:23:21 +0000213 case SignedChar:
Chris Lattnerb304f772009-10-21 06:24:21 +0000214 case SignedShort:
215 case SignedInt:
216 case SignedLong:
Michael J. Spencer237cf582010-10-18 07:10:59 +0000217 case SignedLongLong:
Chris Lattnerb304f772009-10-21 06:24:21 +0000218 return true;
Stepan Dyatkovskiy7b7bef12013-09-05 11:23:21 +0000219 case UnsignedChar:
Chris Lattnerb304f772009-10-21 06:24:21 +0000220 case UnsignedShort:
221 case UnsignedInt:
222 case UnsignedLong:
Michael J. Spencer237cf582010-10-18 07:10:59 +0000223 case UnsignedLongLong:
Chris Lattnerb304f772009-10-21 06:24:21 +0000224 return false;
225 };
226}
227
John Thompsona6fda122009-11-05 20:14:16 +0000228/// setForcedLangOptions - Set forced language options.
229/// Apply changes to the target information with respect to certain
230/// language options which change the target configuration.
231void TargetInfo::setForcedLangOptions(LangOptions &Opts) {
Daniel Dunbarfb937b82010-04-15 15:06:22 +0000232 if (Opts.NoBitFieldTypeAlign)
233 UseBitFieldTypeAlignment = false;
234 if (Opts.ShortWChar)
John Thompsona6fda122009-11-05 20:14:16 +0000235 WCharType = UnsignedShort;
John Thompsona6fda122009-11-05 20:14:16 +0000236}
Chris Lattnerb304f772009-10-21 06:24:21 +0000237
Chris Lattner525a0502007-09-22 18:29:59 +0000238//===----------------------------------------------------------------------===//
Chris Lattner525a0502007-09-22 18:29:59 +0000239
Reid Spencer5f016e22007-07-11 17:01:13 +0000240
Chris Lattner5f9e2722011-07-23 10:55:15 +0000241static StringRef removeGCCRegisterPrefix(StringRef Name) {
Anders Carlssonea041752008-02-06 00:11:32 +0000242 if (Name[0] == '%' || Name[0] == '#')
Anders Carlsson83c021c2010-01-30 19:12:25 +0000243 Name = Name.substr(1);
Michael J. Spencer237cf582010-10-18 07:10:59 +0000244
Anders Carlsson83c021c2010-01-30 19:12:25 +0000245 return Name;
Anders Carlssonea041752008-02-06 00:11:32 +0000246}
247
Eric Christopherde31fd72011-06-28 18:20:53 +0000248/// isValidClobber - Returns whether the passed in string is
249/// a valid clobber in an inline asm statement. This is used by
250/// Sema.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000251bool TargetInfo::isValidClobber(StringRef Name) const {
Eric Christopherde31fd72011-06-28 18:20:53 +0000252 return (isValidGCCRegisterName(Name) ||
253 Name == "memory" || Name == "cc");
254}
255
Anders Carlsson3346ae62007-11-24 23:38:12 +0000256/// isValidGCCRegisterName - Returns whether the passed in string
257/// is a valid register name according to GCC. This is used by Sema for
258/// inline asm statements.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000259bool TargetInfo::isValidGCCRegisterName(StringRef Name) const {
Anders Carlsson83c021c2010-01-30 19:12:25 +0000260 if (Name.empty())
261 return false;
Michael J. Spencer237cf582010-10-18 07:10:59 +0000262
Anders Carlsson6fa90862007-11-25 00:25:21 +0000263 const char * const *Names;
264 unsigned NumNames;
Mike Stump1eb44332009-09-09 15:08:12 +0000265
Anders Carlsson6fa90862007-11-25 00:25:21 +0000266 // Get rid of any register prefix.
Anders Carlsson83c021c2010-01-30 19:12:25 +0000267 Name = removeGCCRegisterPrefix(Name);
Anders Carlssonea041752008-02-06 00:11:32 +0000268
Chris Lattner0eaed122008-03-08 08:24:01 +0000269 getGCCRegNames(Names, NumNames);
Mike Stump1eb44332009-09-09 15:08:12 +0000270
Anders Carlsson6fa90862007-11-25 00:25:21 +0000271 // If we have a number it maps to an entry in the register name array.
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000272 if (isDigit(Name[0])) {
Anders Carlsson83c021c2010-01-30 19:12:25 +0000273 int n;
274 if (!Name.getAsInteger(0, n))
Anders Carlsson6fa90862007-11-25 00:25:21 +0000275 return n >= 0 && (unsigned)n < NumNames;
276 }
277
278 // Check register names.
279 for (unsigned i = 0; i < NumNames; i++) {
Anders Carlsson83c021c2010-01-30 19:12:25 +0000280 if (Name == Names[i])
Anders Carlsson6fa90862007-11-25 00:25:21 +0000281 return true;
282 }
Mike Stump1eb44332009-09-09 15:08:12 +0000283
Eric Christophercfd323d2011-06-21 00:05:20 +0000284 // Check any additional names that we have.
285 const AddlRegName *AddlNames;
286 unsigned NumAddlNames;
287 getGCCAddlRegNames(AddlNames, NumAddlNames);
288 for (unsigned i = 0; i < NumAddlNames; i++)
289 for (unsigned j = 0; j < llvm::array_lengthof(AddlNames[i].Names); j++) {
290 if (!AddlNames[i].Names[j])
291 break;
292 // Make sure the register that the additional name is for is within
293 // the bounds of the register names from above.
294 if (AddlNames[i].Names[j] == Name && AddlNames[i].RegNum < NumNames)
295 return true;
296 }
297
Anders Carlsson6fa90862007-11-25 00:25:21 +0000298 // Now check aliases.
Chris Lattner0eaed122008-03-08 08:24:01 +0000299 const GCCRegAlias *Aliases;
Anders Carlsson6fa90862007-11-25 00:25:21 +0000300 unsigned NumAliases;
Mike Stump1eb44332009-09-09 15:08:12 +0000301
Chris Lattner0eaed122008-03-08 08:24:01 +0000302 getGCCRegAliases(Aliases, NumAliases);
Anders Carlsson6fa90862007-11-25 00:25:21 +0000303 for (unsigned i = 0; i < NumAliases; i++) {
304 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
305 if (!Aliases[i].Aliases[j])
306 break;
Anders Carlsson83c021c2010-01-30 19:12:25 +0000307 if (Aliases[i].Aliases[j] == Name)
Anders Carlsson6fa90862007-11-25 00:25:21 +0000308 return true;
309 }
310 }
Mike Stump1eb44332009-09-09 15:08:12 +0000311
Anders Carlsson3346ae62007-11-24 23:38:12 +0000312 return false;
313}
Anders Carlssond04c6e22007-11-27 04:11:28 +0000314
Chris Lattner5f9e2722011-07-23 10:55:15 +0000315StringRef
316TargetInfo::getNormalizedGCCRegisterName(StringRef Name) const {
Anders Carlssond04c6e22007-11-27 04:11:28 +0000317 assert(isValidGCCRegisterName(Name) && "Invalid register passed in");
Mike Stump1eb44332009-09-09 15:08:12 +0000318
Anders Carlsson83c021c2010-01-30 19:12:25 +0000319 // Get rid of any register prefix.
320 Name = removeGCCRegisterPrefix(Name);
Mike Stump1eb44332009-09-09 15:08:12 +0000321
Anders Carlssond04c6e22007-11-27 04:11:28 +0000322 const char * const *Names;
323 unsigned NumNames;
324
Chris Lattner0eaed122008-03-08 08:24:01 +0000325 getGCCRegNames(Names, NumNames);
Anders Carlssond04c6e22007-11-27 04:11:28 +0000326
327 // First, check if we have a number.
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000328 if (isDigit(Name[0])) {
Anders Carlsson83c021c2010-01-30 19:12:25 +0000329 int n;
330 if (!Name.getAsInteger(0, n)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000331 assert(n >= 0 && (unsigned)n < NumNames &&
Anders Carlssond04c6e22007-11-27 04:11:28 +0000332 "Out of bounds register number!");
333 return Names[n];
334 }
335 }
Mike Stump1eb44332009-09-09 15:08:12 +0000336
Eric Christophercfd323d2011-06-21 00:05:20 +0000337 // Check any additional names that we have.
338 const AddlRegName *AddlNames;
339 unsigned NumAddlNames;
340 getGCCAddlRegNames(AddlNames, NumAddlNames);
341 for (unsigned i = 0; i < NumAddlNames; i++)
342 for (unsigned j = 0; j < llvm::array_lengthof(AddlNames[i].Names); j++) {
343 if (!AddlNames[i].Names[j])
344 break;
345 // Make sure the register that the additional name is for is within
346 // the bounds of the register names from above.
347 if (AddlNames[i].Names[j] == Name && AddlNames[i].RegNum < NumNames)
348 return Name;
349 }
350
Anders Carlssond04c6e22007-11-27 04:11:28 +0000351 // Now check aliases.
Chris Lattner0eaed122008-03-08 08:24:01 +0000352 const GCCRegAlias *Aliases;
Anders Carlssond04c6e22007-11-27 04:11:28 +0000353 unsigned NumAliases;
Mike Stump1eb44332009-09-09 15:08:12 +0000354
Chris Lattner0eaed122008-03-08 08:24:01 +0000355 getGCCRegAliases(Aliases, NumAliases);
Anders Carlssond04c6e22007-11-27 04:11:28 +0000356 for (unsigned i = 0; i < NumAliases; i++) {
357 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
358 if (!Aliases[i].Aliases[j])
359 break;
Anders Carlsson83c021c2010-01-30 19:12:25 +0000360 if (Aliases[i].Aliases[j] == Name)
Anders Carlssond04c6e22007-11-27 04:11:28 +0000361 return Aliases[i].Register;
362 }
363 }
Mike Stump1eb44332009-09-09 15:08:12 +0000364
Anders Carlssond04c6e22007-11-27 04:11:28 +0000365 return Name;
366}
367
Chris Lattner432c8692009-04-26 17:19:08 +0000368bool TargetInfo::validateOutputConstraint(ConstraintInfo &Info) const {
369 const char *Name = Info.getConstraintStr().c_str();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000370 // An output constraint must start with '=' or '+'
371 if (*Name != '=' && *Name != '+')
372 return false;
373
374 if (*Name == '+')
Chris Lattner44def072009-04-26 07:16:29 +0000375 Info.setIsReadWrite();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000376
377 Name++;
378 while (*Name) {
379 switch (*Name) {
380 default:
Chris Lattner44def072009-04-26 07:16:29 +0000381 if (!validateAsmConstraint(Name, Info)) {
Ted Kremenek5ab201a2008-04-24 16:36:38 +0000382 // FIXME: We temporarily return false
Anders Carlssond04c6e22007-11-27 04:11:28 +0000383 // so we can add more constraints as we hit it.
384 // Eventually, an unknown constraint should just be treated as 'g'.
Ted Kremenek5ab201a2008-04-24 16:36:38 +0000385 return false;
Anders Carlssond04c6e22007-11-27 04:11:28 +0000386 }
387 case '&': // early clobber.
388 break;
Chris Lattner40539832009-10-13 04:32:07 +0000389 case '%': // commutative.
390 // FIXME: Check that there is a another register after this one.
391 break;
Anders Carlssond04c6e22007-11-27 04:11:28 +0000392 case 'r': // general register.
Chris Lattner44def072009-04-26 07:16:29 +0000393 Info.setAllowsRegister();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000394 break;
395 case 'm': // memory operand.
Dale Johannesen0f048a42010-09-07 18:40:41 +0000396 case 'o': // offsetable memory operand.
397 case 'V': // non-offsetable memory operand.
398 case '<': // autodecrement memory operand.
399 case '>': // autoincrement memory operand.
Chris Lattner44def072009-04-26 07:16:29 +0000400 Info.setAllowsMemory();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000401 break;
402 case 'g': // general register, memory operand or immediate integer.
Anders Carlsson775841f2009-01-18 02:12:04 +0000403 case 'X': // any operand.
Chris Lattner44def072009-04-26 07:16:29 +0000404 Info.setAllowsRegister();
405 Info.setAllowsMemory();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000406 break;
John Thompsonef44e112010-08-10 19:20:14 +0000407 case ',': // multiple alternative constraint. Pass it.
John Thompson6f158032010-08-11 00:58:20 +0000408 // Handle additional optional '=' or '+' modifiers.
John Thompson2f474ea2010-09-18 01:15:13 +0000409 if (Name[1] == '=' || Name[1] == '+')
John Thompson6f158032010-08-11 00:58:20 +0000410 Name++;
John Thompsonef44e112010-08-10 19:20:14 +0000411 break;
412 case '?': // Disparage slightly code.
Dale Johannesen0f048a42010-09-07 18:40:41 +0000413 case '!': // Disparage severely.
Ulrich Weigande6b3dba2012-10-29 12:20:54 +0000414 case '#': // Ignore as constraint.
415 case '*': // Ignore for choosing register preferences.
John Thompsonef44e112010-08-10 19:20:14 +0000416 break; // Pass them.
Anders Carlssond04c6e22007-11-27 04:11:28 +0000417 }
Mike Stump1eb44332009-09-09 15:08:12 +0000418
Anders Carlssond04c6e22007-11-27 04:11:28 +0000419 Name++;
420 }
Mike Stump1eb44332009-09-09 15:08:12 +0000421
Benjamin Kramer6576a0d2013-04-18 22:49:48 +0000422 // If a constraint allows neither memory nor register operands it contains
423 // only modifiers. Reject it.
Benjamin Kramer5ddf70f2013-04-18 13:23:23 +0000424 return Info.allowsMemory() || Info.allowsRegister();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000425}
426
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000427bool TargetInfo::resolveSymbolicName(const char *&Name,
Chris Lattner2819fa82009-04-26 17:57:12 +0000428 ConstraintInfo *OutputConstraints,
429 unsigned NumOutputs,
Chris Lattner44def072009-04-26 07:16:29 +0000430 unsigned &Index) const {
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000431 assert(*Name == '[' && "Symbolic name did not start with '['");
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000432 Name++;
433 const char *Start = Name;
434 while (*Name && *Name != ']')
435 Name++;
Mike Stump1eb44332009-09-09 15:08:12 +0000436
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000437 if (!*Name) {
438 // Missing ']'
439 return false;
440 }
Mike Stump1eb44332009-09-09 15:08:12 +0000441
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000442 std::string SymbolicName(Start, Name - Start);
Mike Stump1eb44332009-09-09 15:08:12 +0000443
Chris Lattner2819fa82009-04-26 17:57:12 +0000444 for (Index = 0; Index != NumOutputs; ++Index)
445 if (SymbolicName == OutputConstraints[Index].getName())
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000446 return true;
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000447
448 return false;
449}
450
Chris Lattner2819fa82009-04-26 17:57:12 +0000451bool TargetInfo::validateInputConstraint(ConstraintInfo *OutputConstraints,
452 unsigned NumOutputs,
Chris Lattner44def072009-04-26 07:16:29 +0000453 ConstraintInfo &Info) const {
Chris Lattner432c8692009-04-26 17:19:08 +0000454 const char *Name = Info.ConstraintStr.c_str();
455
Anders Carlssond04c6e22007-11-27 04:11:28 +0000456 while (*Name) {
457 switch (*Name) {
458 default:
459 // Check if we have a matching constraint
460 if (*Name >= '0' && *Name <= '9') {
461 unsigned i = *Name - '0';
Mike Stump1eb44332009-09-09 15:08:12 +0000462
Anders Carlssond04c6e22007-11-27 04:11:28 +0000463 // Check if matching constraint is out of bounds.
464 if (i >= NumOutputs)
465 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000466
Anders Carlsson86eda392010-11-03 02:22:29 +0000467 // A number must refer to an output only operand.
468 if (OutputConstraints[i].isReadWrite())
469 return false;
470
Anders Carlsson79ca1ee2010-11-03 02:54:51 +0000471 // If the constraint is already tied, it must be tied to the
472 // same operand referenced to by the number.
473 if (Info.hasTiedOperand() && Info.getTiedOperand() != i)
474 return false;
475
Mike Stump1eb44332009-09-09 15:08:12 +0000476 // The constraint should have the same info as the respective
Anders Carlsson03eb5432009-01-27 20:38:24 +0000477 // output constraint.
Chris Lattnerd6887612009-04-26 18:05:25 +0000478 Info.setTiedOperand(i, OutputConstraints[i]);
Chris Lattner44def072009-04-26 07:16:29 +0000479 } else if (!validateAsmConstraint(Name, Info)) {
Daniel Dunbar302684c2008-08-25 09:46:27 +0000480 // FIXME: This error return is in place temporarily so we can
481 // add more constraints as we hit it. Eventually, an unknown
482 // constraint should just be treated as 'g'.
483 return false;
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000484 }
485 break;
486 case '[': {
487 unsigned Index = 0;
Chris Lattner2819fa82009-04-26 17:57:12 +0000488 if (!resolveSymbolicName(Name, OutputConstraints, NumOutputs, Index))
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000489 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000490
Anders Carlsson79ca1ee2010-11-03 02:54:51 +0000491 // If the constraint is already tied, it must be tied to the
492 // same operand referenced to by the number.
493 if (Info.hasTiedOperand() && Info.getTiedOperand() != Index)
494 return false;
495
Eli Friedman0eff4e72010-08-11 23:03:37 +0000496 Info.setTiedOperand(Index, OutputConstraints[Index]);
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000497 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000498 }
Anders Carlsson44fe49c2007-12-08 19:32:57 +0000499 case '%': // commutative
500 // FIXME: Fail if % is used with the last operand.
501 break;
Anders Carlssond04c6e22007-11-27 04:11:28 +0000502 case 'i': // immediate integer.
Anders Carlssonefdfa772008-03-09 06:02:02 +0000503 case 'n': // immediate integer with a known value.
Anders Carlssond04c6e22007-11-27 04:11:28 +0000504 break;
Chris Lattner1d138792009-05-06 04:33:31 +0000505 case 'I': // Various constant constraints with target-specific meanings.
506 case 'J':
507 case 'K':
508 case 'L':
509 case 'M':
510 case 'N':
511 case 'O':
512 case 'P':
513 break;
Anders Carlssond04c6e22007-11-27 04:11:28 +0000514 case 'r': // general register.
Chris Lattner44def072009-04-26 07:16:29 +0000515 Info.setAllowsRegister();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000516 break;
517 case 'm': // memory operand.
Dale Johannesen0f048a42010-09-07 18:40:41 +0000518 case 'o': // offsettable memory operand.
519 case 'V': // non-offsettable memory operand.
520 case '<': // autodecrement memory operand.
521 case '>': // autoincrement memory operand.
Chris Lattner44def072009-04-26 07:16:29 +0000522 Info.setAllowsMemory();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000523 break;
524 case 'g': // general register, memory operand or immediate integer.
Anders Carlsson775841f2009-01-18 02:12:04 +0000525 case 'X': // any operand.
Chris Lattner44def072009-04-26 07:16:29 +0000526 Info.setAllowsRegister();
527 Info.setAllowsMemory();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000528 break;
John Thompson7ccc58f2010-09-21 22:04:54 +0000529 case 'E': // immediate floating point.
530 case 'F': // immediate floating point.
531 case 'p': // address operand.
532 break;
John Thompsonef44e112010-08-10 19:20:14 +0000533 case ',': // multiple alternative constraint. Ignore comma.
534 break;
535 case '?': // Disparage slightly code.
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000536 case '!': // Disparage severely.
Ulrich Weigande6b3dba2012-10-29 12:20:54 +0000537 case '#': // Ignore as constraint.
538 case '*': // Ignore for choosing register preferences.
John Thompsonef44e112010-08-10 19:20:14 +0000539 break; // Pass them.
Anders Carlssond04c6e22007-11-27 04:11:28 +0000540 }
Mike Stump1eb44332009-09-09 15:08:12 +0000541
Anders Carlssond04c6e22007-11-27 04:11:28 +0000542 Name++;
543 }
Mike Stump1eb44332009-09-09 15:08:12 +0000544
Anders Carlssond04c6e22007-11-27 04:11:28 +0000545 return true;
546}
John McCallb8b2c9d2013-01-25 22:30:49 +0000547
548bool TargetCXXABI::tryParse(llvm::StringRef name) {
549 const Kind unknown = static_cast<Kind>(-1);
550 Kind kind = llvm::StringSwitch<Kind>(name)
551 .Case("arm", GenericARM)
552 .Case("ios", iOS)
553 .Case("itanium", GenericItanium)
554 .Case("microsoft", Microsoft)
555 .Default(unknown);
556 if (kind == unknown) return false;
557
558 set(kind);
559 return true;
560}