blob: e993055cc881900f87126dc0603e7cdaba3b73cf [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;
David Tweed1eef8522013-09-13 12:04:22 +000091 UseAddrSpaceMapMangling = false;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +000092
93 // Default to an unknown platform name.
94 PlatformName = "unknown";
95 PlatformMinVersion = VersionTuple();
Chris Lattnercd4fc422008-03-08 08:59:43 +000096}
97
Chris Lattner0eaed122008-03-08 08:24:01 +000098// Out of line virtual dtor for TargetInfo.
99TargetInfo::~TargetInfo() {}
Reid Spencer5f016e22007-07-11 17:01:13 +0000100
Chris Lattner2b5abf52009-02-06 05:04:11 +0000101/// getTypeName - Return the user string for the specified integer type enum.
102/// For example, SignedShort -> "short".
103const char *TargetInfo::getTypeName(IntType T) {
104 switch (T) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000105 default: llvm_unreachable("not an integer!");
Stepan Dyatkovskiy7b7bef12013-09-05 11:23:21 +0000106 case SignedChar: return "char";
107 case UnsignedChar: return "unsigned char";
Chris Lattner2b5abf52009-02-06 05:04:11 +0000108 case SignedShort: return "short";
109 case UnsignedShort: return "unsigned short";
110 case SignedInt: return "int";
111 case UnsignedInt: return "unsigned int";
112 case SignedLong: return "long int";
113 case UnsignedLong: return "long unsigned int";
114 case SignedLongLong: return "long long int";
115 case UnsignedLongLong: return "long long unsigned int";
116 }
117}
118
Chris Lattnerb304f772009-10-21 06:24:21 +0000119/// getTypeConstantSuffix - Return the constant suffix for the specified
120/// integer type enum. For example, SignedLong -> "L".
121const char *TargetInfo::getTypeConstantSuffix(IntType T) {
122 switch (T) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000123 default: llvm_unreachable("not an integer!");
Stepan Dyatkovskiy7b7bef12013-09-05 11:23:21 +0000124 case SignedChar:
Chris Lattnerb304f772009-10-21 06:24:21 +0000125 case SignedShort:
126 case SignedInt: return "";
127 case SignedLong: return "L";
128 case SignedLongLong: return "LL";
Stepan Dyatkovskiy7b7bef12013-09-05 11:23:21 +0000129 case UnsignedChar:
Chris Lattnerb304f772009-10-21 06:24:21 +0000130 case UnsignedShort:
131 case UnsignedInt: return "U";
132 case UnsignedLong: return "UL";
133 case UnsignedLongLong: return "ULL";
134 }
135}
136
Michael J. Spencer237cf582010-10-18 07:10:59 +0000137/// getTypeWidth - Return the width (in bits) of the specified integer type
Chris Lattnerb304f772009-10-21 06:24:21 +0000138/// enum. For example, SignedInt -> getIntWidth().
139unsigned TargetInfo::getTypeWidth(IntType T) const {
140 switch (T) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000141 default: llvm_unreachable("not an integer!");
Stepan Dyatkovskiy7b7bef12013-09-05 11:23:21 +0000142 case SignedChar:
143 case UnsignedChar: return getCharWidth();
Chris Lattner9099e7b2009-11-05 21:21:32 +0000144 case SignedShort:
Chris Lattnerb304f772009-10-21 06:24:21 +0000145 case UnsignedShort: return getShortWidth();
Chris Lattner9099e7b2009-11-05 21:21:32 +0000146 case SignedInt:
Chris Lattnerb304f772009-10-21 06:24:21 +0000147 case UnsignedInt: return getIntWidth();
Chris Lattner9099e7b2009-11-05 21:21:32 +0000148 case SignedLong:
Chris Lattnerb304f772009-10-21 06:24:21 +0000149 case UnsignedLong: return getLongWidth();
Chris Lattner9099e7b2009-11-05 21:21:32 +0000150 case SignedLongLong:
Chris Lattnerb304f772009-10-21 06:24:21 +0000151 case UnsignedLongLong: return getLongLongWidth();
152 };
153}
154
Stepan Dyatkovskiy7b7bef12013-09-05 11:23:21 +0000155TargetInfo::IntType TargetInfo::getIntTypeByWidth(
156 unsigned BitWidth, bool IsSigned) const {
157 if (getCharWidth() == BitWidth)
158 return IsSigned ? SignedChar : UnsignedChar;
159 if (getShortWidth() == BitWidth)
160 return IsSigned ? SignedShort : UnsignedShort;
161 if (getIntWidth() == BitWidth)
162 return IsSigned ? SignedInt : UnsignedInt;
163 if (getLongWidth() == BitWidth)
164 return IsSigned ? SignedLong : UnsignedLong;
165 if (getLongLongWidth() == BitWidth)
166 return IsSigned ? SignedLongLong : UnsignedLongLong;
167 return NoInt;
168}
169
170TargetInfo::RealType TargetInfo::getRealTypeByWidth(unsigned BitWidth) const {
171 if (getFloatWidth() == BitWidth)
172 return Float;
173 if (getDoubleWidth() == BitWidth)
174 return Double;
175
176 switch (BitWidth) {
177 case 96:
178 if (&getLongDoubleFormat() == &llvm::APFloat::x87DoubleExtended)
179 return LongDouble;
180 break;
181 case 128:
Stepan Dyatkovskiy35451d92013-09-09 07:46:54 +0000182 if (&getLongDoubleFormat() == &llvm::APFloat::PPCDoubleDouble ||
183 &getLongDoubleFormat() == &llvm::APFloat::IEEEquad)
Stepan Dyatkovskiy7b7bef12013-09-05 11:23:21 +0000184 return LongDouble;
185 break;
186 }
187
188 return NoFloat;
189}
190
Michael J. Spencer237cf582010-10-18 07:10:59 +0000191/// getTypeAlign - Return the alignment (in bits) of the specified integer type
Chris Lattner9099e7b2009-11-05 21:21:32 +0000192/// enum. For example, SignedInt -> getIntAlign().
193unsigned TargetInfo::getTypeAlign(IntType T) const {
194 switch (T) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000195 default: llvm_unreachable("not an integer!");
Stepan Dyatkovskiy7b7bef12013-09-05 11:23:21 +0000196 case SignedChar:
197 case UnsignedChar: return getCharAlign();
Chris Lattner9099e7b2009-11-05 21:21:32 +0000198 case SignedShort:
199 case UnsignedShort: return getShortAlign();
200 case SignedInt:
201 case UnsignedInt: return getIntAlign();
202 case SignedLong:
203 case UnsignedLong: return getLongAlign();
204 case SignedLongLong:
205 case UnsignedLongLong: return getLongLongAlign();
206 };
207}
208
Chris Lattner961f0702009-10-25 22:49:18 +0000209/// isTypeSigned - Return whether an integer types is signed. Returns true if
Chris Lattnerb304f772009-10-21 06:24:21 +0000210/// the type is signed; false otherwise.
Chris Lattner3f59c972010-12-25 23:25:43 +0000211bool TargetInfo::isTypeSigned(IntType T) {
Chris Lattnerb304f772009-10-21 06:24:21 +0000212 switch (T) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000213 default: llvm_unreachable("not an integer!");
Stepan Dyatkovskiy7b7bef12013-09-05 11:23:21 +0000214 case SignedChar:
Chris Lattnerb304f772009-10-21 06:24:21 +0000215 case SignedShort:
216 case SignedInt:
217 case SignedLong:
Michael J. Spencer237cf582010-10-18 07:10:59 +0000218 case SignedLongLong:
Chris Lattnerb304f772009-10-21 06:24:21 +0000219 return true;
Stepan Dyatkovskiy7b7bef12013-09-05 11:23:21 +0000220 case UnsignedChar:
Chris Lattnerb304f772009-10-21 06:24:21 +0000221 case UnsignedShort:
222 case UnsignedInt:
223 case UnsignedLong:
Michael J. Spencer237cf582010-10-18 07:10:59 +0000224 case UnsignedLongLong:
Chris Lattnerb304f772009-10-21 06:24:21 +0000225 return false;
226 };
227}
228
John Thompsona6fda122009-11-05 20:14:16 +0000229/// setForcedLangOptions - Set forced language options.
230/// Apply changes to the target information with respect to certain
231/// language options which change the target configuration.
232void TargetInfo::setForcedLangOptions(LangOptions &Opts) {
Daniel Dunbarfb937b82010-04-15 15:06:22 +0000233 if (Opts.NoBitFieldTypeAlign)
234 UseBitFieldTypeAlignment = false;
235 if (Opts.ShortWChar)
John Thompsona6fda122009-11-05 20:14:16 +0000236 WCharType = UnsignedShort;
David Tweed8bc56022013-09-09 09:17:24 +0000237
238 if (Opts.OpenCL) {
239 // OpenCL C requires specific widths for types, irrespective of
240 // what these normally are for the target.
241 // We also define long long and long double here, although the
242 // OpenCL standard only mentions these as "reserved".
243 IntWidth = IntAlign = 32;
244 LongWidth = LongAlign = 64;
245 LongLongWidth = LongLongAlign = 128;
246 HalfWidth = HalfAlign = 16;
247 FloatWidth = FloatAlign = 32;
248 DoubleWidth = DoubleAlign = 64;
249 LongDoubleWidth = LongDoubleAlign = 128;
250
251 assert(PointerWidth == 32 || PointerWidth == 64);
David Tweed1e244632013-09-10 08:00:34 +0000252 bool Is32BitArch = PointerWidth == 32;
253 SizeType = Is32BitArch ? UnsignedInt : UnsignedLong;
254 PtrDiffType = Is32BitArch ? SignedInt : SignedLong;
255 IntPtrType = Is32BitArch ? SignedInt : SignedLong;
David Tweed8bc56022013-09-09 09:17:24 +0000256
257 IntMaxType = SignedLongLong;
258 UIntMaxType = UnsignedLongLong;
259 Int64Type = SignedLong;
260
261 HalfFormat = &llvm::APFloat::IEEEhalf;
262 FloatFormat = &llvm::APFloat::IEEEsingle;
263 DoubleFormat = &llvm::APFloat::IEEEdouble;
264 LongDoubleFormat = &llvm::APFloat::IEEEquad;
265 }
John Thompsona6fda122009-11-05 20:14:16 +0000266}
Chris Lattnerb304f772009-10-21 06:24:21 +0000267
Chris Lattner525a0502007-09-22 18:29:59 +0000268//===----------------------------------------------------------------------===//
Chris Lattner525a0502007-09-22 18:29:59 +0000269
Reid Spencer5f016e22007-07-11 17:01:13 +0000270
Chris Lattner5f9e2722011-07-23 10:55:15 +0000271static StringRef removeGCCRegisterPrefix(StringRef Name) {
Anders Carlssonea041752008-02-06 00:11:32 +0000272 if (Name[0] == '%' || Name[0] == '#')
Anders Carlsson83c021c2010-01-30 19:12:25 +0000273 Name = Name.substr(1);
Michael J. Spencer237cf582010-10-18 07:10:59 +0000274
Anders Carlsson83c021c2010-01-30 19:12:25 +0000275 return Name;
Anders Carlssonea041752008-02-06 00:11:32 +0000276}
277
Eric Christopherde31fd72011-06-28 18:20:53 +0000278/// isValidClobber - Returns whether the passed in string is
279/// a valid clobber in an inline asm statement. This is used by
280/// Sema.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000281bool TargetInfo::isValidClobber(StringRef Name) const {
Eric Christopherde31fd72011-06-28 18:20:53 +0000282 return (isValidGCCRegisterName(Name) ||
283 Name == "memory" || Name == "cc");
284}
285
Anders Carlsson3346ae62007-11-24 23:38:12 +0000286/// isValidGCCRegisterName - Returns whether the passed in string
287/// is a valid register name according to GCC. This is used by Sema for
288/// inline asm statements.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000289bool TargetInfo::isValidGCCRegisterName(StringRef Name) const {
Anders Carlsson83c021c2010-01-30 19:12:25 +0000290 if (Name.empty())
291 return false;
Michael J. Spencer237cf582010-10-18 07:10:59 +0000292
Anders Carlsson6fa90862007-11-25 00:25:21 +0000293 const char * const *Names;
294 unsigned NumNames;
Mike Stump1eb44332009-09-09 15:08:12 +0000295
Anders Carlsson6fa90862007-11-25 00:25:21 +0000296 // Get rid of any register prefix.
Anders Carlsson83c021c2010-01-30 19:12:25 +0000297 Name = removeGCCRegisterPrefix(Name);
Anders Carlssonea041752008-02-06 00:11:32 +0000298
Chris Lattner0eaed122008-03-08 08:24:01 +0000299 getGCCRegNames(Names, NumNames);
Mike Stump1eb44332009-09-09 15:08:12 +0000300
Anders Carlsson6fa90862007-11-25 00:25:21 +0000301 // If we have a number it maps to an entry in the register name array.
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000302 if (isDigit(Name[0])) {
Anders Carlsson83c021c2010-01-30 19:12:25 +0000303 int n;
304 if (!Name.getAsInteger(0, n))
Anders Carlsson6fa90862007-11-25 00:25:21 +0000305 return n >= 0 && (unsigned)n < NumNames;
306 }
307
308 // Check register names.
309 for (unsigned i = 0; i < NumNames; i++) {
Anders Carlsson83c021c2010-01-30 19:12:25 +0000310 if (Name == Names[i])
Anders Carlsson6fa90862007-11-25 00:25:21 +0000311 return true;
312 }
Mike Stump1eb44332009-09-09 15:08:12 +0000313
Eric Christophercfd323d2011-06-21 00:05:20 +0000314 // Check any additional names that we have.
315 const AddlRegName *AddlNames;
316 unsigned NumAddlNames;
317 getGCCAddlRegNames(AddlNames, NumAddlNames);
318 for (unsigned i = 0; i < NumAddlNames; i++)
319 for (unsigned j = 0; j < llvm::array_lengthof(AddlNames[i].Names); j++) {
320 if (!AddlNames[i].Names[j])
321 break;
322 // Make sure the register that the additional name is for is within
323 // the bounds of the register names from above.
324 if (AddlNames[i].Names[j] == Name && AddlNames[i].RegNum < NumNames)
325 return true;
326 }
327
Anders Carlsson6fa90862007-11-25 00:25:21 +0000328 // Now check aliases.
Chris Lattner0eaed122008-03-08 08:24:01 +0000329 const GCCRegAlias *Aliases;
Anders Carlsson6fa90862007-11-25 00:25:21 +0000330 unsigned NumAliases;
Mike Stump1eb44332009-09-09 15:08:12 +0000331
Chris Lattner0eaed122008-03-08 08:24:01 +0000332 getGCCRegAliases(Aliases, NumAliases);
Anders Carlsson6fa90862007-11-25 00:25:21 +0000333 for (unsigned i = 0; i < NumAliases; i++) {
334 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
335 if (!Aliases[i].Aliases[j])
336 break;
Anders Carlsson83c021c2010-01-30 19:12:25 +0000337 if (Aliases[i].Aliases[j] == Name)
Anders Carlsson6fa90862007-11-25 00:25:21 +0000338 return true;
339 }
340 }
Mike Stump1eb44332009-09-09 15:08:12 +0000341
Anders Carlsson3346ae62007-11-24 23:38:12 +0000342 return false;
343}
Anders Carlssond04c6e22007-11-27 04:11:28 +0000344
Chris Lattner5f9e2722011-07-23 10:55:15 +0000345StringRef
346TargetInfo::getNormalizedGCCRegisterName(StringRef Name) const {
Anders Carlssond04c6e22007-11-27 04:11:28 +0000347 assert(isValidGCCRegisterName(Name) && "Invalid register passed in");
Mike Stump1eb44332009-09-09 15:08:12 +0000348
Anders Carlsson83c021c2010-01-30 19:12:25 +0000349 // Get rid of any register prefix.
350 Name = removeGCCRegisterPrefix(Name);
Mike Stump1eb44332009-09-09 15:08:12 +0000351
Anders Carlssond04c6e22007-11-27 04:11:28 +0000352 const char * const *Names;
353 unsigned NumNames;
354
Chris Lattner0eaed122008-03-08 08:24:01 +0000355 getGCCRegNames(Names, NumNames);
Anders Carlssond04c6e22007-11-27 04:11:28 +0000356
357 // First, check if we have a number.
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000358 if (isDigit(Name[0])) {
Anders Carlsson83c021c2010-01-30 19:12:25 +0000359 int n;
360 if (!Name.getAsInteger(0, n)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000361 assert(n >= 0 && (unsigned)n < NumNames &&
Anders Carlssond04c6e22007-11-27 04:11:28 +0000362 "Out of bounds register number!");
363 return Names[n];
364 }
365 }
Mike Stump1eb44332009-09-09 15:08:12 +0000366
Eric Christophercfd323d2011-06-21 00:05:20 +0000367 // Check any additional names that we have.
368 const AddlRegName *AddlNames;
369 unsigned NumAddlNames;
370 getGCCAddlRegNames(AddlNames, NumAddlNames);
371 for (unsigned i = 0; i < NumAddlNames; i++)
372 for (unsigned j = 0; j < llvm::array_lengthof(AddlNames[i].Names); j++) {
373 if (!AddlNames[i].Names[j])
374 break;
375 // Make sure the register that the additional name is for is within
376 // the bounds of the register names from above.
377 if (AddlNames[i].Names[j] == Name && AddlNames[i].RegNum < NumNames)
378 return Name;
379 }
380
Anders Carlssond04c6e22007-11-27 04:11:28 +0000381 // Now check aliases.
Chris Lattner0eaed122008-03-08 08:24:01 +0000382 const GCCRegAlias *Aliases;
Anders Carlssond04c6e22007-11-27 04:11:28 +0000383 unsigned NumAliases;
Mike Stump1eb44332009-09-09 15:08:12 +0000384
Chris Lattner0eaed122008-03-08 08:24:01 +0000385 getGCCRegAliases(Aliases, NumAliases);
Anders Carlssond04c6e22007-11-27 04:11:28 +0000386 for (unsigned i = 0; i < NumAliases; i++) {
387 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
388 if (!Aliases[i].Aliases[j])
389 break;
Anders Carlsson83c021c2010-01-30 19:12:25 +0000390 if (Aliases[i].Aliases[j] == Name)
Anders Carlssond04c6e22007-11-27 04:11:28 +0000391 return Aliases[i].Register;
392 }
393 }
Mike Stump1eb44332009-09-09 15:08:12 +0000394
Anders Carlssond04c6e22007-11-27 04:11:28 +0000395 return Name;
396}
397
Chris Lattner432c8692009-04-26 17:19:08 +0000398bool TargetInfo::validateOutputConstraint(ConstraintInfo &Info) const {
399 const char *Name = Info.getConstraintStr().c_str();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000400 // An output constraint must start with '=' or '+'
401 if (*Name != '=' && *Name != '+')
402 return false;
403
404 if (*Name == '+')
Chris Lattner44def072009-04-26 07:16:29 +0000405 Info.setIsReadWrite();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000406
407 Name++;
408 while (*Name) {
409 switch (*Name) {
410 default:
Chris Lattner44def072009-04-26 07:16:29 +0000411 if (!validateAsmConstraint(Name, Info)) {
Ted Kremenek5ab201a2008-04-24 16:36:38 +0000412 // FIXME: We temporarily return false
Anders Carlssond04c6e22007-11-27 04:11:28 +0000413 // so we can add more constraints as we hit it.
414 // Eventually, an unknown constraint should just be treated as 'g'.
Ted Kremenek5ab201a2008-04-24 16:36:38 +0000415 return false;
Anders Carlssond04c6e22007-11-27 04:11:28 +0000416 }
417 case '&': // early clobber.
418 break;
Chris Lattner40539832009-10-13 04:32:07 +0000419 case '%': // commutative.
420 // FIXME: Check that there is a another register after this one.
421 break;
Anders Carlssond04c6e22007-11-27 04:11:28 +0000422 case 'r': // general register.
Chris Lattner44def072009-04-26 07:16:29 +0000423 Info.setAllowsRegister();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000424 break;
425 case 'm': // memory operand.
Dale Johannesen0f048a42010-09-07 18:40:41 +0000426 case 'o': // offsetable memory operand.
427 case 'V': // non-offsetable memory operand.
428 case '<': // autodecrement memory operand.
429 case '>': // autoincrement memory operand.
Chris Lattner44def072009-04-26 07:16:29 +0000430 Info.setAllowsMemory();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000431 break;
432 case 'g': // general register, memory operand or immediate integer.
Anders Carlsson775841f2009-01-18 02:12:04 +0000433 case 'X': // any operand.
Chris Lattner44def072009-04-26 07:16:29 +0000434 Info.setAllowsRegister();
435 Info.setAllowsMemory();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000436 break;
John Thompsonef44e112010-08-10 19:20:14 +0000437 case ',': // multiple alternative constraint. Pass it.
John Thompson6f158032010-08-11 00:58:20 +0000438 // Handle additional optional '=' or '+' modifiers.
John Thompson2f474ea2010-09-18 01:15:13 +0000439 if (Name[1] == '=' || Name[1] == '+')
John Thompson6f158032010-08-11 00:58:20 +0000440 Name++;
John Thompsonef44e112010-08-10 19:20:14 +0000441 break;
442 case '?': // Disparage slightly code.
Dale Johannesen0f048a42010-09-07 18:40:41 +0000443 case '!': // Disparage severely.
Ulrich Weigande6b3dba2012-10-29 12:20:54 +0000444 case '#': // Ignore as constraint.
445 case '*': // Ignore for choosing register preferences.
John Thompsonef44e112010-08-10 19:20:14 +0000446 break; // Pass them.
Anders Carlssond04c6e22007-11-27 04:11:28 +0000447 }
Mike Stump1eb44332009-09-09 15:08:12 +0000448
Anders Carlssond04c6e22007-11-27 04:11:28 +0000449 Name++;
450 }
Mike Stump1eb44332009-09-09 15:08:12 +0000451
Benjamin Kramer6576a0d2013-04-18 22:49:48 +0000452 // If a constraint allows neither memory nor register operands it contains
453 // only modifiers. Reject it.
Benjamin Kramer5ddf70f2013-04-18 13:23:23 +0000454 return Info.allowsMemory() || Info.allowsRegister();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000455}
456
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000457bool TargetInfo::resolveSymbolicName(const char *&Name,
Chris Lattner2819fa82009-04-26 17:57:12 +0000458 ConstraintInfo *OutputConstraints,
459 unsigned NumOutputs,
Chris Lattner44def072009-04-26 07:16:29 +0000460 unsigned &Index) const {
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000461 assert(*Name == '[' && "Symbolic name did not start with '['");
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000462 Name++;
463 const char *Start = Name;
464 while (*Name && *Name != ']')
465 Name++;
Mike Stump1eb44332009-09-09 15:08:12 +0000466
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000467 if (!*Name) {
468 // Missing ']'
469 return false;
470 }
Mike Stump1eb44332009-09-09 15:08:12 +0000471
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000472 std::string SymbolicName(Start, Name - Start);
Mike Stump1eb44332009-09-09 15:08:12 +0000473
Chris Lattner2819fa82009-04-26 17:57:12 +0000474 for (Index = 0; Index != NumOutputs; ++Index)
475 if (SymbolicName == OutputConstraints[Index].getName())
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000476 return true;
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000477
478 return false;
479}
480
Chris Lattner2819fa82009-04-26 17:57:12 +0000481bool TargetInfo::validateInputConstraint(ConstraintInfo *OutputConstraints,
482 unsigned NumOutputs,
Chris Lattner44def072009-04-26 07:16:29 +0000483 ConstraintInfo &Info) const {
Chris Lattner432c8692009-04-26 17:19:08 +0000484 const char *Name = Info.ConstraintStr.c_str();
485
Anders Carlssond04c6e22007-11-27 04:11:28 +0000486 while (*Name) {
487 switch (*Name) {
488 default:
489 // Check if we have a matching constraint
490 if (*Name >= '0' && *Name <= '9') {
491 unsigned i = *Name - '0';
Mike Stump1eb44332009-09-09 15:08:12 +0000492
Anders Carlssond04c6e22007-11-27 04:11:28 +0000493 // Check if matching constraint is out of bounds.
494 if (i >= NumOutputs)
495 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000496
Anders Carlsson86eda392010-11-03 02:22:29 +0000497 // A number must refer to an output only operand.
498 if (OutputConstraints[i].isReadWrite())
499 return false;
500
Anders Carlsson79ca1ee2010-11-03 02:54:51 +0000501 // If the constraint is already tied, it must be tied to the
502 // same operand referenced to by the number.
503 if (Info.hasTiedOperand() && Info.getTiedOperand() != i)
504 return false;
505
Mike Stump1eb44332009-09-09 15:08:12 +0000506 // The constraint should have the same info as the respective
Anders Carlsson03eb5432009-01-27 20:38:24 +0000507 // output constraint.
Chris Lattnerd6887612009-04-26 18:05:25 +0000508 Info.setTiedOperand(i, OutputConstraints[i]);
Chris Lattner44def072009-04-26 07:16:29 +0000509 } else if (!validateAsmConstraint(Name, Info)) {
Daniel Dunbar302684c2008-08-25 09:46:27 +0000510 // FIXME: This error return is in place temporarily so we can
511 // add more constraints as we hit it. Eventually, an unknown
512 // constraint should just be treated as 'g'.
513 return false;
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000514 }
515 break;
516 case '[': {
517 unsigned Index = 0;
Chris Lattner2819fa82009-04-26 17:57:12 +0000518 if (!resolveSymbolicName(Name, OutputConstraints, NumOutputs, Index))
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000519 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000520
Anders Carlsson79ca1ee2010-11-03 02:54:51 +0000521 // If the constraint is already tied, it must be tied to the
522 // same operand referenced to by the number.
523 if (Info.hasTiedOperand() && Info.getTiedOperand() != Index)
524 return false;
525
Eli Friedman0eff4e72010-08-11 23:03:37 +0000526 Info.setTiedOperand(Index, OutputConstraints[Index]);
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000527 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000528 }
Anders Carlsson44fe49c2007-12-08 19:32:57 +0000529 case '%': // commutative
530 // FIXME: Fail if % is used with the last operand.
531 break;
Anders Carlssond04c6e22007-11-27 04:11:28 +0000532 case 'i': // immediate integer.
Anders Carlssonefdfa772008-03-09 06:02:02 +0000533 case 'n': // immediate integer with a known value.
Anders Carlssond04c6e22007-11-27 04:11:28 +0000534 break;
Chris Lattner1d138792009-05-06 04:33:31 +0000535 case 'I': // Various constant constraints with target-specific meanings.
536 case 'J':
537 case 'K':
538 case 'L':
539 case 'M':
540 case 'N':
541 case 'O':
542 case 'P':
543 break;
Anders Carlssond04c6e22007-11-27 04:11:28 +0000544 case 'r': // general register.
Chris Lattner44def072009-04-26 07:16:29 +0000545 Info.setAllowsRegister();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000546 break;
547 case 'm': // memory operand.
Dale Johannesen0f048a42010-09-07 18:40:41 +0000548 case 'o': // offsettable memory operand.
549 case 'V': // non-offsettable memory operand.
550 case '<': // autodecrement memory operand.
551 case '>': // autoincrement memory operand.
Chris Lattner44def072009-04-26 07:16:29 +0000552 Info.setAllowsMemory();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000553 break;
554 case 'g': // general register, memory operand or immediate integer.
Anders Carlsson775841f2009-01-18 02:12:04 +0000555 case 'X': // any operand.
Chris Lattner44def072009-04-26 07:16:29 +0000556 Info.setAllowsRegister();
557 Info.setAllowsMemory();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000558 break;
John Thompson7ccc58f2010-09-21 22:04:54 +0000559 case 'E': // immediate floating point.
560 case 'F': // immediate floating point.
561 case 'p': // address operand.
562 break;
John Thompsonef44e112010-08-10 19:20:14 +0000563 case ',': // multiple alternative constraint. Ignore comma.
564 break;
565 case '?': // Disparage slightly code.
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000566 case '!': // Disparage severely.
Ulrich Weigande6b3dba2012-10-29 12:20:54 +0000567 case '#': // Ignore as constraint.
568 case '*': // Ignore for choosing register preferences.
John Thompsonef44e112010-08-10 19:20:14 +0000569 break; // Pass them.
Anders Carlssond04c6e22007-11-27 04:11:28 +0000570 }
Mike Stump1eb44332009-09-09 15:08:12 +0000571
Anders Carlssond04c6e22007-11-27 04:11:28 +0000572 Name++;
573 }
Mike Stump1eb44332009-09-09 15:08:12 +0000574
Anders Carlssond04c6e22007-11-27 04:11:28 +0000575 return true;
576}
John McCallb8b2c9d2013-01-25 22:30:49 +0000577
578bool TargetCXXABI::tryParse(llvm::StringRef name) {
579 const Kind unknown = static_cast<Kind>(-1);
580 Kind kind = llvm::StringSwitch<Kind>(name)
581 .Case("arm", GenericARM)
582 .Case("ios", iOS)
583 .Case("itanium", GenericItanium)
584 .Case("microsoft", Microsoft)
585 .Default(unknown);
586 if (kind == unknown) return false;
587
588 set(kind);
589 return true;
590}