blob: 4d0664857d638fbfa1402b47eed06a86e95737cb [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;
Stephen Hines6bcf27b2014-05-29 04:14:42 -070072 DescriptionString = nullptr;
Chris Lattner3fdf4672008-10-05 19:22:37 +000073 UserLabelPrefix = "_";
Roman Divackybe4c8702011-02-10 16:52:03 +000074 MCountName = "mcount";
Abramo Bagnara62f940b2011-09-08 14:20:25 +000075 RegParmMax = 0;
76 SSERegParmMax = 0;
Daniel Dunbar613fd672010-05-27 00:35:16 +000077 HasAlignMac68kSupport = false;
Daniel Dunbardacf9dd2010-07-14 23:39:36 +000078
79 // Default to no types using fpret.
80 RealTypeUsesObjCFPRet = 0;
John McCallee79a4c2010-08-21 22:46:04 +000081
Anders Carlssoneea64802011-10-31 16:27:11 +000082 // Default to not using fp2ret for __Complex long double
83 ComplexLongDoubleUsesFP2Ret = false;
84
Stephen Hines651f13c2014-04-23 16:59:28 -070085 // Set the C++ ABI based on the triple.
86 TheCXXABI.set(Triple.isKnownWindowsMSVCEnvironment()
87 ? TargetCXXABI::Microsoft
88 : TargetCXXABI::GenericItanium);
Peter Collingbourne207f4d82011-03-18 22:38:29 +000089
90 // Default to an empty address space map.
91 AddrSpaceMap = &DefaultAddrSpaceMap;
David Tweed1eef8522013-09-13 12:04:22 +000092 UseAddrSpaceMapMangling = false;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +000093
94 // Default to an unknown platform name.
95 PlatformName = "unknown";
96 PlatformMinVersion = VersionTuple();
Chris Lattnercd4fc422008-03-08 08:59:43 +000097}
98
Chris Lattner0eaed122008-03-08 08:24:01 +000099// Out of line virtual dtor for TargetInfo.
100TargetInfo::~TargetInfo() {}
Reid Spencer5f016e22007-07-11 17:01:13 +0000101
Chris Lattner2b5abf52009-02-06 05:04:11 +0000102/// getTypeName - Return the user string for the specified integer type enum.
103/// For example, SignedShort -> "short".
104const char *TargetInfo::getTypeName(IntType T) {
105 switch (T) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000106 default: llvm_unreachable("not an integer!");
Stepan Dyatkovskiy7b7bef12013-09-05 11:23:21 +0000107 case SignedChar: return "char";
108 case UnsignedChar: return "unsigned char";
Chris Lattner2b5abf52009-02-06 05:04:11 +0000109 case SignedShort: return "short";
110 case UnsignedShort: return "unsigned short";
111 case SignedInt: return "int";
112 case UnsignedInt: return "unsigned int";
113 case SignedLong: return "long int";
114 case UnsignedLong: return "long unsigned int";
115 case SignedLongLong: return "long long int";
116 case UnsignedLongLong: return "long long unsigned int";
117 }
118}
119
Chris Lattnerb304f772009-10-21 06:24:21 +0000120/// getTypeConstantSuffix - Return the constant suffix for the specified
121/// integer type enum. For example, SignedLong -> "L".
122const char *TargetInfo::getTypeConstantSuffix(IntType T) {
123 switch (T) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000124 default: llvm_unreachable("not an integer!");
Stepan Dyatkovskiy7b7bef12013-09-05 11:23:21 +0000125 case SignedChar:
Chris Lattnerb304f772009-10-21 06:24:21 +0000126 case SignedShort:
127 case SignedInt: return "";
128 case SignedLong: return "L";
129 case SignedLongLong: return "LL";
Stepan Dyatkovskiy7b7bef12013-09-05 11:23:21 +0000130 case UnsignedChar:
Chris Lattnerb304f772009-10-21 06:24:21 +0000131 case UnsignedShort:
132 case UnsignedInt: return "U";
133 case UnsignedLong: return "UL";
134 case UnsignedLongLong: return "ULL";
135 }
136}
137
Michael J. Spencer237cf582010-10-18 07:10:59 +0000138/// getTypeWidth - Return the width (in bits) of the specified integer type
Chris Lattnerb304f772009-10-21 06:24:21 +0000139/// enum. For example, SignedInt -> getIntWidth().
140unsigned TargetInfo::getTypeWidth(IntType T) const {
141 switch (T) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000142 default: llvm_unreachable("not an integer!");
Stepan Dyatkovskiy7b7bef12013-09-05 11:23:21 +0000143 case SignedChar:
144 case UnsignedChar: return getCharWidth();
Chris Lattner9099e7b2009-11-05 21:21:32 +0000145 case SignedShort:
Chris Lattnerb304f772009-10-21 06:24:21 +0000146 case UnsignedShort: return getShortWidth();
Chris Lattner9099e7b2009-11-05 21:21:32 +0000147 case SignedInt:
Chris Lattnerb304f772009-10-21 06:24:21 +0000148 case UnsignedInt: return getIntWidth();
Chris Lattner9099e7b2009-11-05 21:21:32 +0000149 case SignedLong:
Chris Lattnerb304f772009-10-21 06:24:21 +0000150 case UnsignedLong: return getLongWidth();
Chris Lattner9099e7b2009-11-05 21:21:32 +0000151 case SignedLongLong:
Chris Lattnerb304f772009-10-21 06:24:21 +0000152 case UnsignedLongLong: return getLongLongWidth();
153 };
154}
155
Stepan Dyatkovskiy7b7bef12013-09-05 11:23:21 +0000156TargetInfo::IntType TargetInfo::getIntTypeByWidth(
157 unsigned BitWidth, bool IsSigned) const {
158 if (getCharWidth() == BitWidth)
159 return IsSigned ? SignedChar : UnsignedChar;
160 if (getShortWidth() == BitWidth)
161 return IsSigned ? SignedShort : UnsignedShort;
162 if (getIntWidth() == BitWidth)
163 return IsSigned ? SignedInt : UnsignedInt;
164 if (getLongWidth() == BitWidth)
165 return IsSigned ? SignedLong : UnsignedLong;
166 if (getLongLongWidth() == BitWidth)
167 return IsSigned ? SignedLongLong : UnsignedLongLong;
168 return NoInt;
169}
170
171TargetInfo::RealType TargetInfo::getRealTypeByWidth(unsigned BitWidth) const {
172 if (getFloatWidth() == BitWidth)
173 return Float;
174 if (getDoubleWidth() == BitWidth)
175 return Double;
176
177 switch (BitWidth) {
178 case 96:
179 if (&getLongDoubleFormat() == &llvm::APFloat::x87DoubleExtended)
180 return LongDouble;
181 break;
182 case 128:
Stepan Dyatkovskiy35451d92013-09-09 07:46:54 +0000183 if (&getLongDoubleFormat() == &llvm::APFloat::PPCDoubleDouble ||
184 &getLongDoubleFormat() == &llvm::APFloat::IEEEquad)
Stepan Dyatkovskiy7b7bef12013-09-05 11:23:21 +0000185 return LongDouble;
186 break;
187 }
188
189 return NoFloat;
190}
191
Michael J. Spencer237cf582010-10-18 07:10:59 +0000192/// getTypeAlign - Return the alignment (in bits) of the specified integer type
Chris Lattner9099e7b2009-11-05 21:21:32 +0000193/// enum. For example, SignedInt -> getIntAlign().
194unsigned TargetInfo::getTypeAlign(IntType T) const {
195 switch (T) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000196 default: llvm_unreachable("not an integer!");
Stepan Dyatkovskiy7b7bef12013-09-05 11:23:21 +0000197 case SignedChar:
198 case UnsignedChar: return getCharAlign();
Chris Lattner9099e7b2009-11-05 21:21:32 +0000199 case SignedShort:
200 case UnsignedShort: return getShortAlign();
201 case SignedInt:
202 case UnsignedInt: return getIntAlign();
203 case SignedLong:
204 case UnsignedLong: return getLongAlign();
205 case SignedLongLong:
206 case UnsignedLongLong: return getLongLongAlign();
207 };
208}
209
Chris Lattner961f0702009-10-25 22:49:18 +0000210/// isTypeSigned - Return whether an integer types is signed. Returns true if
Chris Lattnerb304f772009-10-21 06:24:21 +0000211/// the type is signed; false otherwise.
Chris Lattner3f59c972010-12-25 23:25:43 +0000212bool TargetInfo::isTypeSigned(IntType T) {
Chris Lattnerb304f772009-10-21 06:24:21 +0000213 switch (T) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000214 default: llvm_unreachable("not an integer!");
Stepan Dyatkovskiy7b7bef12013-09-05 11:23:21 +0000215 case SignedChar:
Chris Lattnerb304f772009-10-21 06:24:21 +0000216 case SignedShort:
217 case SignedInt:
218 case SignedLong:
Michael J. Spencer237cf582010-10-18 07:10:59 +0000219 case SignedLongLong:
Chris Lattnerb304f772009-10-21 06:24:21 +0000220 return true;
Stepan Dyatkovskiy7b7bef12013-09-05 11:23:21 +0000221 case UnsignedChar:
Chris Lattnerb304f772009-10-21 06:24:21 +0000222 case UnsignedShort:
223 case UnsignedInt:
224 case UnsignedLong:
Michael J. Spencer237cf582010-10-18 07:10:59 +0000225 case UnsignedLongLong:
Chris Lattnerb304f772009-10-21 06:24:21 +0000226 return false;
227 };
228}
229
John Thompsona6fda122009-11-05 20:14:16 +0000230/// setForcedLangOptions - Set forced language options.
231/// Apply changes to the target information with respect to certain
232/// language options which change the target configuration.
233void TargetInfo::setForcedLangOptions(LangOptions &Opts) {
Daniel Dunbarfb937b82010-04-15 15:06:22 +0000234 if (Opts.NoBitFieldTypeAlign)
235 UseBitFieldTypeAlignment = false;
236 if (Opts.ShortWChar)
John Thompsona6fda122009-11-05 20:14:16 +0000237 WCharType = UnsignedShort;
David Tweed8bc56022013-09-09 09:17:24 +0000238
239 if (Opts.OpenCL) {
240 // OpenCL C requires specific widths for types, irrespective of
241 // what these normally are for the target.
242 // We also define long long and long double here, although the
243 // OpenCL standard only mentions these as "reserved".
244 IntWidth = IntAlign = 32;
245 LongWidth = LongAlign = 64;
246 LongLongWidth = LongLongAlign = 128;
247 HalfWidth = HalfAlign = 16;
248 FloatWidth = FloatAlign = 32;
Stephen Hines651f13c2014-04-23 16:59:28 -0700249
250 // Embedded 32-bit targets (OpenCL EP) might have double C type
251 // defined as float. Let's not override this as it might lead
252 // to generating illegal code that uses 64bit doubles.
253 if (DoubleWidth != FloatWidth) {
254 DoubleWidth = DoubleAlign = 64;
255 DoubleFormat = &llvm::APFloat::IEEEdouble;
256 }
David Tweed8bc56022013-09-09 09:17:24 +0000257 LongDoubleWidth = LongDoubleAlign = 128;
258
259 assert(PointerWidth == 32 || PointerWidth == 64);
David Tweed1e244632013-09-10 08:00:34 +0000260 bool Is32BitArch = PointerWidth == 32;
261 SizeType = Is32BitArch ? UnsignedInt : UnsignedLong;
262 PtrDiffType = Is32BitArch ? SignedInt : SignedLong;
263 IntPtrType = Is32BitArch ? SignedInt : SignedLong;
David Tweed8bc56022013-09-09 09:17:24 +0000264
265 IntMaxType = SignedLongLong;
266 UIntMaxType = UnsignedLongLong;
267 Int64Type = SignedLong;
268
269 HalfFormat = &llvm::APFloat::IEEEhalf;
270 FloatFormat = &llvm::APFloat::IEEEsingle;
David Tweed8bc56022013-09-09 09:17:24 +0000271 LongDoubleFormat = &llvm::APFloat::IEEEquad;
272 }
John Thompsona6fda122009-11-05 20:14:16 +0000273}
Chris Lattnerb304f772009-10-21 06:24:21 +0000274
Chris Lattner525a0502007-09-22 18:29:59 +0000275//===----------------------------------------------------------------------===//
Chris Lattner525a0502007-09-22 18:29:59 +0000276
Reid Spencer5f016e22007-07-11 17:01:13 +0000277
Chris Lattner5f9e2722011-07-23 10:55:15 +0000278static StringRef removeGCCRegisterPrefix(StringRef Name) {
Anders Carlssonea041752008-02-06 00:11:32 +0000279 if (Name[0] == '%' || Name[0] == '#')
Anders Carlsson83c021c2010-01-30 19:12:25 +0000280 Name = Name.substr(1);
Michael J. Spencer237cf582010-10-18 07:10:59 +0000281
Anders Carlsson83c021c2010-01-30 19:12:25 +0000282 return Name;
Anders Carlssonea041752008-02-06 00:11:32 +0000283}
284
Eric Christopherde31fd72011-06-28 18:20:53 +0000285/// isValidClobber - Returns whether the passed in string is
286/// a valid clobber in an inline asm statement. This is used by
287/// Sema.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000288bool TargetInfo::isValidClobber(StringRef Name) const {
Eric Christopherde31fd72011-06-28 18:20:53 +0000289 return (isValidGCCRegisterName(Name) ||
290 Name == "memory" || Name == "cc");
291}
292
Anders Carlsson3346ae62007-11-24 23:38:12 +0000293/// isValidGCCRegisterName - Returns whether the passed in string
294/// is a valid register name according to GCC. This is used by Sema for
295/// inline asm statements.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000296bool TargetInfo::isValidGCCRegisterName(StringRef Name) const {
Anders Carlsson83c021c2010-01-30 19:12:25 +0000297 if (Name.empty())
298 return false;
Michael J. Spencer237cf582010-10-18 07:10:59 +0000299
Anders Carlsson6fa90862007-11-25 00:25:21 +0000300 const char * const *Names;
301 unsigned NumNames;
Mike Stump1eb44332009-09-09 15:08:12 +0000302
Anders Carlsson6fa90862007-11-25 00:25:21 +0000303 // Get rid of any register prefix.
Anders Carlsson83c021c2010-01-30 19:12:25 +0000304 Name = removeGCCRegisterPrefix(Name);
Anders Carlssonea041752008-02-06 00:11:32 +0000305
Chris Lattner0eaed122008-03-08 08:24:01 +0000306 getGCCRegNames(Names, NumNames);
Mike Stump1eb44332009-09-09 15:08:12 +0000307
Anders Carlsson6fa90862007-11-25 00:25:21 +0000308 // If we have a number it maps to an entry in the register name array.
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000309 if (isDigit(Name[0])) {
Anders Carlsson83c021c2010-01-30 19:12:25 +0000310 int n;
311 if (!Name.getAsInteger(0, n))
Anders Carlsson6fa90862007-11-25 00:25:21 +0000312 return n >= 0 && (unsigned)n < NumNames;
313 }
314
315 // Check register names.
316 for (unsigned i = 0; i < NumNames; i++) {
Anders Carlsson83c021c2010-01-30 19:12:25 +0000317 if (Name == Names[i])
Anders Carlsson6fa90862007-11-25 00:25:21 +0000318 return true;
319 }
Mike Stump1eb44332009-09-09 15:08:12 +0000320
Eric Christophercfd323d2011-06-21 00:05:20 +0000321 // Check any additional names that we have.
322 const AddlRegName *AddlNames;
323 unsigned NumAddlNames;
324 getGCCAddlRegNames(AddlNames, NumAddlNames);
325 for (unsigned i = 0; i < NumAddlNames; i++)
326 for (unsigned j = 0; j < llvm::array_lengthof(AddlNames[i].Names); j++) {
327 if (!AddlNames[i].Names[j])
328 break;
329 // Make sure the register that the additional name is for is within
330 // the bounds of the register names from above.
331 if (AddlNames[i].Names[j] == Name && AddlNames[i].RegNum < NumNames)
332 return true;
333 }
334
Anders Carlsson6fa90862007-11-25 00:25:21 +0000335 // Now check aliases.
Chris Lattner0eaed122008-03-08 08:24:01 +0000336 const GCCRegAlias *Aliases;
Anders Carlsson6fa90862007-11-25 00:25:21 +0000337 unsigned NumAliases;
Mike Stump1eb44332009-09-09 15:08:12 +0000338
Chris Lattner0eaed122008-03-08 08:24:01 +0000339 getGCCRegAliases(Aliases, NumAliases);
Anders Carlsson6fa90862007-11-25 00:25:21 +0000340 for (unsigned i = 0; i < NumAliases; i++) {
341 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
342 if (!Aliases[i].Aliases[j])
343 break;
Anders Carlsson83c021c2010-01-30 19:12:25 +0000344 if (Aliases[i].Aliases[j] == Name)
Anders Carlsson6fa90862007-11-25 00:25:21 +0000345 return true;
346 }
347 }
Mike Stump1eb44332009-09-09 15:08:12 +0000348
Anders Carlsson3346ae62007-11-24 23:38:12 +0000349 return false;
350}
Anders Carlssond04c6e22007-11-27 04:11:28 +0000351
Chris Lattner5f9e2722011-07-23 10:55:15 +0000352StringRef
353TargetInfo::getNormalizedGCCRegisterName(StringRef Name) const {
Anders Carlssond04c6e22007-11-27 04:11:28 +0000354 assert(isValidGCCRegisterName(Name) && "Invalid register passed in");
Mike Stump1eb44332009-09-09 15:08:12 +0000355
Anders Carlsson83c021c2010-01-30 19:12:25 +0000356 // Get rid of any register prefix.
357 Name = removeGCCRegisterPrefix(Name);
Mike Stump1eb44332009-09-09 15:08:12 +0000358
Anders Carlssond04c6e22007-11-27 04:11:28 +0000359 const char * const *Names;
360 unsigned NumNames;
361
Chris Lattner0eaed122008-03-08 08:24:01 +0000362 getGCCRegNames(Names, NumNames);
Anders Carlssond04c6e22007-11-27 04:11:28 +0000363
364 // First, check if we have a number.
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000365 if (isDigit(Name[0])) {
Anders Carlsson83c021c2010-01-30 19:12:25 +0000366 int n;
367 if (!Name.getAsInteger(0, n)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000368 assert(n >= 0 && (unsigned)n < NumNames &&
Anders Carlssond04c6e22007-11-27 04:11:28 +0000369 "Out of bounds register number!");
370 return Names[n];
371 }
372 }
Mike Stump1eb44332009-09-09 15:08:12 +0000373
Eric Christophercfd323d2011-06-21 00:05:20 +0000374 // Check any additional names that we have.
375 const AddlRegName *AddlNames;
376 unsigned NumAddlNames;
377 getGCCAddlRegNames(AddlNames, NumAddlNames);
378 for (unsigned i = 0; i < NumAddlNames; i++)
379 for (unsigned j = 0; j < llvm::array_lengthof(AddlNames[i].Names); j++) {
380 if (!AddlNames[i].Names[j])
381 break;
382 // Make sure the register that the additional name is for is within
383 // the bounds of the register names from above.
384 if (AddlNames[i].Names[j] == Name && AddlNames[i].RegNum < NumNames)
385 return Name;
386 }
387
Anders Carlssond04c6e22007-11-27 04:11:28 +0000388 // Now check aliases.
Chris Lattner0eaed122008-03-08 08:24:01 +0000389 const GCCRegAlias *Aliases;
Anders Carlssond04c6e22007-11-27 04:11:28 +0000390 unsigned NumAliases;
Mike Stump1eb44332009-09-09 15:08:12 +0000391
Chris Lattner0eaed122008-03-08 08:24:01 +0000392 getGCCRegAliases(Aliases, NumAliases);
Anders Carlssond04c6e22007-11-27 04:11:28 +0000393 for (unsigned i = 0; i < NumAliases; i++) {
394 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
395 if (!Aliases[i].Aliases[j])
396 break;
Anders Carlsson83c021c2010-01-30 19:12:25 +0000397 if (Aliases[i].Aliases[j] == Name)
Anders Carlssond04c6e22007-11-27 04:11:28 +0000398 return Aliases[i].Register;
399 }
400 }
Mike Stump1eb44332009-09-09 15:08:12 +0000401
Anders Carlssond04c6e22007-11-27 04:11:28 +0000402 return Name;
403}
404
Chris Lattner432c8692009-04-26 17:19:08 +0000405bool TargetInfo::validateOutputConstraint(ConstraintInfo &Info) const {
406 const char *Name = Info.getConstraintStr().c_str();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000407 // An output constraint must start with '=' or '+'
408 if (*Name != '=' && *Name != '+')
409 return false;
410
411 if (*Name == '+')
Chris Lattner44def072009-04-26 07:16:29 +0000412 Info.setIsReadWrite();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000413
414 Name++;
415 while (*Name) {
416 switch (*Name) {
417 default:
Chris Lattner44def072009-04-26 07:16:29 +0000418 if (!validateAsmConstraint(Name, Info)) {
Ted Kremenek5ab201a2008-04-24 16:36:38 +0000419 // FIXME: We temporarily return false
Anders Carlssond04c6e22007-11-27 04:11:28 +0000420 // so we can add more constraints as we hit it.
421 // Eventually, an unknown constraint should just be treated as 'g'.
Ted Kremenek5ab201a2008-04-24 16:36:38 +0000422 return false;
Anders Carlssond04c6e22007-11-27 04:11:28 +0000423 }
424 case '&': // early clobber.
425 break;
Chris Lattner40539832009-10-13 04:32:07 +0000426 case '%': // commutative.
427 // FIXME: Check that there is a another register after this one.
428 break;
Anders Carlssond04c6e22007-11-27 04:11:28 +0000429 case 'r': // general register.
Chris Lattner44def072009-04-26 07:16:29 +0000430 Info.setAllowsRegister();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000431 break;
432 case 'm': // memory operand.
Dale Johannesen0f048a42010-09-07 18:40:41 +0000433 case 'o': // offsetable memory operand.
434 case 'V': // non-offsetable memory operand.
435 case '<': // autodecrement memory operand.
436 case '>': // autoincrement memory operand.
Chris Lattner44def072009-04-26 07:16:29 +0000437 Info.setAllowsMemory();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000438 break;
439 case 'g': // general register, memory operand or immediate integer.
Anders Carlsson775841f2009-01-18 02:12:04 +0000440 case 'X': // any operand.
Chris Lattner44def072009-04-26 07:16:29 +0000441 Info.setAllowsRegister();
442 Info.setAllowsMemory();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000443 break;
John Thompsonef44e112010-08-10 19:20:14 +0000444 case ',': // multiple alternative constraint. Pass it.
John Thompson6f158032010-08-11 00:58:20 +0000445 // Handle additional optional '=' or '+' modifiers.
John Thompson2f474ea2010-09-18 01:15:13 +0000446 if (Name[1] == '=' || Name[1] == '+')
John Thompson6f158032010-08-11 00:58:20 +0000447 Name++;
John Thompsonef44e112010-08-10 19:20:14 +0000448 break;
449 case '?': // Disparage slightly code.
Dale Johannesen0f048a42010-09-07 18:40:41 +0000450 case '!': // Disparage severely.
Ulrich Weigande6b3dba2012-10-29 12:20:54 +0000451 case '#': // Ignore as constraint.
452 case '*': // Ignore for choosing register preferences.
John Thompsonef44e112010-08-10 19:20:14 +0000453 break; // Pass them.
Anders Carlssond04c6e22007-11-27 04:11:28 +0000454 }
Mike Stump1eb44332009-09-09 15:08:12 +0000455
Anders Carlssond04c6e22007-11-27 04:11:28 +0000456 Name++;
457 }
Mike Stump1eb44332009-09-09 15:08:12 +0000458
Benjamin Kramer6576a0d2013-04-18 22:49:48 +0000459 // If a constraint allows neither memory nor register operands it contains
460 // only modifiers. Reject it.
Benjamin Kramer5ddf70f2013-04-18 13:23:23 +0000461 return Info.allowsMemory() || Info.allowsRegister();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000462}
463
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000464bool TargetInfo::resolveSymbolicName(const char *&Name,
Chris Lattner2819fa82009-04-26 17:57:12 +0000465 ConstraintInfo *OutputConstraints,
466 unsigned NumOutputs,
Chris Lattner44def072009-04-26 07:16:29 +0000467 unsigned &Index) const {
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000468 assert(*Name == '[' && "Symbolic name did not start with '['");
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000469 Name++;
470 const char *Start = Name;
471 while (*Name && *Name != ']')
472 Name++;
Mike Stump1eb44332009-09-09 15:08:12 +0000473
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000474 if (!*Name) {
475 // Missing ']'
476 return false;
477 }
Mike Stump1eb44332009-09-09 15:08:12 +0000478
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000479 std::string SymbolicName(Start, Name - Start);
Mike Stump1eb44332009-09-09 15:08:12 +0000480
Chris Lattner2819fa82009-04-26 17:57:12 +0000481 for (Index = 0; Index != NumOutputs; ++Index)
482 if (SymbolicName == OutputConstraints[Index].getName())
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000483 return true;
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000484
485 return false;
486}
487
Chris Lattner2819fa82009-04-26 17:57:12 +0000488bool TargetInfo::validateInputConstraint(ConstraintInfo *OutputConstraints,
489 unsigned NumOutputs,
Chris Lattner44def072009-04-26 07:16:29 +0000490 ConstraintInfo &Info) const {
Chris Lattner432c8692009-04-26 17:19:08 +0000491 const char *Name = Info.ConstraintStr.c_str();
492
Stephen Hines651f13c2014-04-23 16:59:28 -0700493 if (!*Name)
494 return false;
495
Anders Carlssond04c6e22007-11-27 04:11:28 +0000496 while (*Name) {
497 switch (*Name) {
498 default:
499 // Check if we have a matching constraint
500 if (*Name >= '0' && *Name <= '9') {
501 unsigned i = *Name - '0';
Mike Stump1eb44332009-09-09 15:08:12 +0000502
Anders Carlssond04c6e22007-11-27 04:11:28 +0000503 // Check if matching constraint is out of bounds.
504 if (i >= NumOutputs)
505 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000506
Anders Carlsson86eda392010-11-03 02:22:29 +0000507 // A number must refer to an output only operand.
508 if (OutputConstraints[i].isReadWrite())
509 return false;
510
Anders Carlsson79ca1ee2010-11-03 02:54:51 +0000511 // If the constraint is already tied, it must be tied to the
512 // same operand referenced to by the number.
513 if (Info.hasTiedOperand() && Info.getTiedOperand() != i)
514 return false;
515
Mike Stump1eb44332009-09-09 15:08:12 +0000516 // The constraint should have the same info as the respective
Anders Carlsson03eb5432009-01-27 20:38:24 +0000517 // output constraint.
Chris Lattnerd6887612009-04-26 18:05:25 +0000518 Info.setTiedOperand(i, OutputConstraints[i]);
Chris Lattner44def072009-04-26 07:16:29 +0000519 } else if (!validateAsmConstraint(Name, Info)) {
Daniel Dunbar302684c2008-08-25 09:46:27 +0000520 // FIXME: This error return is in place temporarily so we can
521 // add more constraints as we hit it. Eventually, an unknown
522 // constraint should just be treated as 'g'.
523 return false;
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000524 }
525 break;
526 case '[': {
527 unsigned Index = 0;
Chris Lattner2819fa82009-04-26 17:57:12 +0000528 if (!resolveSymbolicName(Name, OutputConstraints, NumOutputs, Index))
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000529 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000530
Anders Carlsson79ca1ee2010-11-03 02:54:51 +0000531 // If the constraint is already tied, it must be tied to the
532 // same operand referenced to by the number.
533 if (Info.hasTiedOperand() && Info.getTiedOperand() != Index)
534 return false;
535
Eli Friedman0eff4e72010-08-11 23:03:37 +0000536 Info.setTiedOperand(Index, OutputConstraints[Index]);
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000537 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000538 }
Anders Carlsson44fe49c2007-12-08 19:32:57 +0000539 case '%': // commutative
540 // FIXME: Fail if % is used with the last operand.
541 break;
Anders Carlssond04c6e22007-11-27 04:11:28 +0000542 case 'i': // immediate integer.
Anders Carlssonefdfa772008-03-09 06:02:02 +0000543 case 'n': // immediate integer with a known value.
Anders Carlssond04c6e22007-11-27 04:11:28 +0000544 break;
Chris Lattner1d138792009-05-06 04:33:31 +0000545 case 'I': // Various constant constraints with target-specific meanings.
546 case 'J':
547 case 'K':
548 case 'L':
549 case 'M':
550 case 'N':
551 case 'O':
552 case 'P':
553 break;
Anders Carlssond04c6e22007-11-27 04:11:28 +0000554 case 'r': // general register.
Chris Lattner44def072009-04-26 07:16:29 +0000555 Info.setAllowsRegister();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000556 break;
557 case 'm': // memory operand.
Dale Johannesen0f048a42010-09-07 18:40:41 +0000558 case 'o': // offsettable memory operand.
559 case 'V': // non-offsettable memory operand.
560 case '<': // autodecrement memory operand.
561 case '>': // autoincrement memory operand.
Chris Lattner44def072009-04-26 07:16:29 +0000562 Info.setAllowsMemory();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000563 break;
564 case 'g': // general register, memory operand or immediate integer.
Anders Carlsson775841f2009-01-18 02:12:04 +0000565 case 'X': // any operand.
Chris Lattner44def072009-04-26 07:16:29 +0000566 Info.setAllowsRegister();
567 Info.setAllowsMemory();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000568 break;
John Thompson7ccc58f2010-09-21 22:04:54 +0000569 case 'E': // immediate floating point.
570 case 'F': // immediate floating point.
571 case 'p': // address operand.
572 break;
John Thompsonef44e112010-08-10 19:20:14 +0000573 case ',': // multiple alternative constraint. Ignore comma.
574 break;
575 case '?': // Disparage slightly code.
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000576 case '!': // Disparage severely.
Ulrich Weigande6b3dba2012-10-29 12:20:54 +0000577 case '#': // Ignore as constraint.
578 case '*': // Ignore for choosing register preferences.
John Thompsonef44e112010-08-10 19:20:14 +0000579 break; // Pass them.
Anders Carlssond04c6e22007-11-27 04:11:28 +0000580 }
Mike Stump1eb44332009-09-09 15:08:12 +0000581
Anders Carlssond04c6e22007-11-27 04:11:28 +0000582 Name++;
583 }
Mike Stump1eb44332009-09-09 15:08:12 +0000584
Anders Carlssond04c6e22007-11-27 04:11:28 +0000585 return true;
586}
John McCallb8b2c9d2013-01-25 22:30:49 +0000587
588bool TargetCXXABI::tryParse(llvm::StringRef name) {
589 const Kind unknown = static_cast<Kind>(-1);
590 Kind kind = llvm::StringSwitch<Kind>(name)
591 .Case("arm", GenericARM)
592 .Case("ios", iOS)
593 .Case("itanium", GenericItanium)
594 .Case("microsoft", Microsoft)
595 .Default(unknown);
596 if (kind == unknown) return false;
597
598 set(kind);
599 return true;
600}