blob: 13d78e998e36a215f2ed6560bfa38f679c85a3cf [file] [log] [blame]
Chris Lattner1e27fe12006-10-14 07:06:20 +00001//===--- TargetInfo.cpp - Information about Target machine ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner1e27fe12006-10-14 07:06:20 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the TargetInfo and TargetInfoImpl interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Basic/TargetInfo.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000015#include "clang/Basic/AddressSpaces.h"
Jordan Rosea7d03842013-02-08 22:30:41 +000016#include "clang/Basic/CharInfo.h"
John Thompsoned4e2952009-11-05 20:14:16 +000017#include "clang/Basic/LangOptions.h"
Chris Lattnerec0a6d92007-09-22 18:29:59 +000018#include "llvm/ADT/APFloat.h"
Anders Carlsson290aa852007-11-25 00:25:21 +000019#include "llvm/ADT/STLExtras.h"
David Blaikie76bd3c82011-09-23 05:35:21 +000020#include "llvm/Support/ErrorHandling.h"
Chris Lattnerc4f02b62008-04-06 04:02:29 +000021#include <cstdlib>
Chris Lattner1e27fe12006-10-14 07:06:20 +000022using namespace clang;
23
Peter Collingbourne599cb8e2011-03-18 22:38:29 +000024static const LangAS::Map DefaultAddrSpaceMap = { 0 };
25
Chris Lattner1df27862008-03-08 08:59:43 +000026// TargetInfo Constructor.
Benjamin Kramerb1276b42013-06-29 16:37:14 +000027TargetInfo::TargetInfo(const llvm::Triple &T) : TargetOpts(), Triple(T) {
Daniel Dunbar3d9289c2010-04-15 06:18:39 +000028 // Set defaults. Defaults are set for a 32-bit RISC platform, like PPC or
29 // SPARC. These should be overridden by concrete targets as needed.
Eli Friedman803acb32011-12-22 03:51:45 +000030 BigEndian = true;
Eli Friedmand88c8a12009-04-19 21:38:35 +000031 TLSSupported = true;
Chris Lattner1a8f3942010-04-23 16:29:58 +000032 NoAsmVariants = false;
Chris Lattner5e2ef0c2008-05-09 06:08:39 +000033 PointerWidth = PointerAlign = 32;
Roman Divacky965b0b72011-01-06 08:27:10 +000034 BoolWidth = BoolAlign = 8;
Chris Lattnerb781dc792008-05-08 05:58:21 +000035 IntWidth = IntAlign = 32;
Chris Lattner5a9aaaf2008-05-09 05:50:02 +000036 LongWidth = LongAlign = 32;
37 LongLongWidth = LongLongAlign = 64;
Nick Lewyckyf59e1292011-12-16 22:34:14 +000038 SuitableAlign = 64;
Ulrich Weigandfa806422013-05-06 16:23:57 +000039 MinGlobalAlign = 0;
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +000040 HalfWidth = 16;
41 HalfAlign = 16;
Eli Friedmanb5366062008-05-20 14:21:01 +000042 FloatWidth = 32;
43 FloatAlign = 32;
Nate Begeman65bea232008-04-18 17:17:24 +000044 DoubleWidth = 64;
Eli Friedmanb5366062008-05-20 14:21:01 +000045 DoubleAlign = 64;
46 LongDoubleWidth = 64;
47 LongDoubleAlign = 64;
Rafael Espindolae971b9a2010-06-04 23:15:27 +000048 LargeArrayMinWidth = 0;
49 LargeArrayAlign = 0;
Eli Friedman4b72fdd2011-10-14 20:59:01 +000050 MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 0;
Chad Rosiercc40ea72012-07-13 23:57:43 +000051 MaxVectorAlign = 0;
Anders Carlsson2a79a902008-10-31 16:05:19 +000052 SizeType = UnsignedLong;
Eli Friedmand50881c2008-11-02 02:43:55 +000053 PtrDiffType = SignedLong;
Sanjiv Guptad7959242008-10-31 09:52:39 +000054 IntMaxType = SignedLongLong;
Chris Lattner7e4c81c2009-02-13 22:28:55 +000055 IntPtrType = SignedLong;
Eli Friedmand50881c2008-11-02 02:43:55 +000056 WCharType = SignedInt;
Chris Lattner67204922009-10-21 04:59:34 +000057 WIntType = SignedInt;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +000058 Char16Type = UnsignedShort;
59 Char32Type = UnsignedInt;
Eli Friedman2857ccb2009-07-01 03:36:11 +000060 Int64Type = SignedLongLong;
Edward O'Callaghan847f2a12009-11-21 00:49:54 +000061 SigAtomicType = SignedInt;
Eli Friedman4e91899e2012-11-27 02:58:24 +000062 ProcessIDType = SignedInt;
Fariborz Jahanian29898f42012-04-16 21:03:30 +000063 UseSignedCharForObjCBool = true;
Daniel Dunbar1da65112010-04-15 15:06:18 +000064 UseBitFieldTypeAlignment = true;
Chad Rosier18903ee2011-08-04 01:21:14 +000065 UseZeroLengthBitfieldAlignment = false;
66 ZeroLengthBitfieldBoundary = 0;
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +000067 HalfFormat = &llvm::APFloat::IEEEhalf;
Chris Lattner1df27862008-03-08 08:59:43 +000068 FloatFormat = &llvm::APFloat::IEEEsingle;
69 DoubleFormat = &llvm::APFloat::IEEEdouble;
70 LongDoubleFormat = &llvm::APFloat::IEEEdouble;
Craig Topperf1186c52014-05-08 06:41:40 +000071 DescriptionString = nullptr;
Chris Lattnerf37bafc2008-10-05 19:22:37 +000072 UserLabelPrefix = "_";
Roman Divacky178e01602011-02-10 16:52:03 +000073 MCountName = "mcount";
Abramo Bagnara41bfb662011-09-08 14:20:25 +000074 RegParmMax = 0;
75 SSERegParmMax = 0;
Daniel Dunbarbd606522010-05-27 00:35:16 +000076 HasAlignMac68kSupport = false;
Daniel Dunbar6f2e8392010-07-14 23:39:36 +000077
78 // Default to no types using fpret.
79 RealTypeUsesObjCFPRet = 0;
John McCall86353412010-08-21 22:46:04 +000080
Anders Carlsson2f1a6c32011-10-31 16:27:11 +000081 // Default to not using fp2ret for __Complex long double
82 ComplexLongDoubleUsesFP2Ret = false;
83
Hans Wennborgc9bd88e2014-01-14 19:35:09 +000084 // Set the C++ ABI based on the triple.
Saleem Abdulrasool377066a2014-03-27 22:50:18 +000085 TheCXXABI.set(Triple.isKnownWindowsMSVCEnvironment()
Hans Wennborgc9bd88e2014-01-14 19:35:09 +000086 ? TargetCXXABI::Microsoft
87 : TargetCXXABI::GenericItanium);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +000088
89 // Default to an empty address space map.
90 AddrSpaceMap = &DefaultAddrSpaceMap;
David Tweed31d09b02013-09-13 12:04:22 +000091 UseAddrSpaceMapMangling = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +000092
93 // Default to an unknown platform name.
94 PlatformName = "unknown";
95 PlatformMinVersion = VersionTuple();
Chris Lattner1df27862008-03-08 08:59:43 +000096}
97
Chris Lattnerc3a669b2008-03-08 08:24:01 +000098// Out of line virtual dtor for TargetInfo.
99TargetInfo::~TargetInfo() {}
Chris Lattner2194ddc2006-10-14 18:32:26 +0000100
Chris Lattnera91c30f2009-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 Blaikie83d382b2011-09-23 05:06:16 +0000105 default: llvm_unreachable("not an integer!");
Stepan Dyatkovskiy5a637922013-09-05 11:23:21 +0000106 case SignedChar: return "char";
107 case UnsignedChar: return "unsigned char";
Chris Lattnera91c30f2009-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 Lattner72cdcac2009-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 Blaikie83d382b2011-09-23 05:06:16 +0000123 default: llvm_unreachable("not an integer!");
Stepan Dyatkovskiy5a637922013-09-05 11:23:21 +0000124 case SignedChar:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000125 case SignedShort:
126 case SignedInt: return "";
127 case SignedLong: return "L";
128 case SignedLongLong: return "LL";
Stepan Dyatkovskiy5a637922013-09-05 11:23:21 +0000129 case UnsignedChar:
Chris Lattner72cdcac2009-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. Spencerfeb799c2010-10-18 07:10:59 +0000137/// getTypeWidth - Return the width (in bits) of the specified integer type
Chris Lattner72cdcac2009-10-21 06:24:21 +0000138/// enum. For example, SignedInt -> getIntWidth().
139unsigned TargetInfo::getTypeWidth(IntType T) const {
140 switch (T) {
David Blaikie83d382b2011-09-23 05:06:16 +0000141 default: llvm_unreachable("not an integer!");
Stepan Dyatkovskiy5a637922013-09-05 11:23:21 +0000142 case SignedChar:
143 case UnsignedChar: return getCharWidth();
Chris Lattnere4a8c642009-11-05 21:21:32 +0000144 case SignedShort:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000145 case UnsignedShort: return getShortWidth();
Chris Lattnere4a8c642009-11-05 21:21:32 +0000146 case SignedInt:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000147 case UnsignedInt: return getIntWidth();
Chris Lattnere4a8c642009-11-05 21:21:32 +0000148 case SignedLong:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000149 case UnsignedLong: return getLongWidth();
Chris Lattnere4a8c642009-11-05 21:21:32 +0000150 case SignedLongLong:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000151 case UnsignedLongLong: return getLongLongWidth();
152 };
153}
154
Stepan Dyatkovskiy5a637922013-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
JF Bastienab8d0a02014-06-25 01:31:33 +0000170TargetInfo::IntType TargetInfo::getLeastIntTypeByWidth(unsigned BitWidth,
171 bool IsSigned) const {
172 if (getCharWidth() >= BitWidth)
173 return IsSigned ? SignedChar : UnsignedChar;
174 if (getShortWidth() >= BitWidth)
175 return IsSigned ? SignedShort : UnsignedShort;
176 if (getIntWidth() >= BitWidth)
177 return IsSigned ? SignedInt : UnsignedInt;
178 if (getLongWidth() >= BitWidth)
179 return IsSigned ? SignedLong : UnsignedLong;
180 if (getLongLongWidth() >= BitWidth)
181 return IsSigned ? SignedLongLong : UnsignedLongLong;
182 return NoInt;
183}
184
Stepan Dyatkovskiy5a637922013-09-05 11:23:21 +0000185TargetInfo::RealType TargetInfo::getRealTypeByWidth(unsigned BitWidth) const {
186 if (getFloatWidth() == BitWidth)
187 return Float;
188 if (getDoubleWidth() == BitWidth)
189 return Double;
190
191 switch (BitWidth) {
192 case 96:
193 if (&getLongDoubleFormat() == &llvm::APFloat::x87DoubleExtended)
194 return LongDouble;
195 break;
196 case 128:
Stepan Dyatkovskiya4a59d72013-09-09 07:46:54 +0000197 if (&getLongDoubleFormat() == &llvm::APFloat::PPCDoubleDouble ||
198 &getLongDoubleFormat() == &llvm::APFloat::IEEEquad)
Stepan Dyatkovskiy5a637922013-09-05 11:23:21 +0000199 return LongDouble;
200 break;
201 }
202
203 return NoFloat;
204}
205
Michael J. Spencerfeb799c2010-10-18 07:10:59 +0000206/// getTypeAlign - Return the alignment (in bits) of the specified integer type
Chris Lattnere4a8c642009-11-05 21:21:32 +0000207/// enum. For example, SignedInt -> getIntAlign().
208unsigned TargetInfo::getTypeAlign(IntType T) const {
209 switch (T) {
David Blaikie83d382b2011-09-23 05:06:16 +0000210 default: llvm_unreachable("not an integer!");
Stepan Dyatkovskiy5a637922013-09-05 11:23:21 +0000211 case SignedChar:
212 case UnsignedChar: return getCharAlign();
Chris Lattnere4a8c642009-11-05 21:21:32 +0000213 case SignedShort:
214 case UnsignedShort: return getShortAlign();
215 case SignedInt:
216 case UnsignedInt: return getIntAlign();
217 case SignedLong:
218 case UnsignedLong: return getLongAlign();
219 case SignedLongLong:
220 case UnsignedLongLong: return getLongLongAlign();
221 };
222}
223
Chris Lattnerc0c04392009-10-25 22:49:18 +0000224/// isTypeSigned - Return whether an integer types is signed. Returns true if
Chris Lattner72cdcac2009-10-21 06:24:21 +0000225/// the type is signed; false otherwise.
Chris Lattnerad3467e2010-12-25 23:25:43 +0000226bool TargetInfo::isTypeSigned(IntType T) {
Chris Lattner72cdcac2009-10-21 06:24:21 +0000227 switch (T) {
David Blaikie83d382b2011-09-23 05:06:16 +0000228 default: llvm_unreachable("not an integer!");
Stepan Dyatkovskiy5a637922013-09-05 11:23:21 +0000229 case SignedChar:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000230 case SignedShort:
231 case SignedInt:
232 case SignedLong:
Michael J. Spencerfeb799c2010-10-18 07:10:59 +0000233 case SignedLongLong:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000234 return true;
Stepan Dyatkovskiy5a637922013-09-05 11:23:21 +0000235 case UnsignedChar:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000236 case UnsignedShort:
237 case UnsignedInt:
238 case UnsignedLong:
Michael J. Spencerfeb799c2010-10-18 07:10:59 +0000239 case UnsignedLongLong:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000240 return false;
241 };
242}
243
Alp Toker74437972014-07-06 05:14:24 +0000244/// adjust - Set forced language options.
John Thompsoned4e2952009-11-05 20:14:16 +0000245/// Apply changes to the target information with respect to certain
246/// language options which change the target configuration.
Alp Toker74437972014-07-06 05:14:24 +0000247void TargetInfo::adjust(const LangOptions &Opts) {
Daniel Dunbar9302f602010-04-15 15:06:22 +0000248 if (Opts.NoBitFieldTypeAlign)
249 UseBitFieldTypeAlignment = false;
250 if (Opts.ShortWChar)
John Thompsoned4e2952009-11-05 20:14:16 +0000251 WCharType = UnsignedShort;
David Tweed2da64382013-09-09 09:17:24 +0000252
253 if (Opts.OpenCL) {
254 // OpenCL C requires specific widths for types, irrespective of
255 // what these normally are for the target.
256 // We also define long long and long double here, although the
257 // OpenCL standard only mentions these as "reserved".
258 IntWidth = IntAlign = 32;
259 LongWidth = LongAlign = 64;
260 LongLongWidth = LongLongAlign = 128;
261 HalfWidth = HalfAlign = 16;
262 FloatWidth = FloatAlign = 32;
Pekka Jaaskelainen43c39d52013-12-18 18:15:03 +0000263
264 // Embedded 32-bit targets (OpenCL EP) might have double C type
265 // defined as float. Let's not override this as it might lead
266 // to generating illegal code that uses 64bit doubles.
267 if (DoubleWidth != FloatWidth) {
268 DoubleWidth = DoubleAlign = 64;
269 DoubleFormat = &llvm::APFloat::IEEEdouble;
270 }
David Tweed2da64382013-09-09 09:17:24 +0000271 LongDoubleWidth = LongDoubleAlign = 128;
272
273 assert(PointerWidth == 32 || PointerWidth == 64);
David Tweed6c2149b2013-09-10 08:00:34 +0000274 bool Is32BitArch = PointerWidth == 32;
275 SizeType = Is32BitArch ? UnsignedInt : UnsignedLong;
276 PtrDiffType = Is32BitArch ? SignedInt : SignedLong;
277 IntPtrType = Is32BitArch ? SignedInt : SignedLong;
David Tweed2da64382013-09-09 09:17:24 +0000278
279 IntMaxType = SignedLongLong;
David Tweed2da64382013-09-09 09:17:24 +0000280 Int64Type = SignedLong;
281
282 HalfFormat = &llvm::APFloat::IEEEhalf;
283 FloatFormat = &llvm::APFloat::IEEEsingle;
David Tweed2da64382013-09-09 09:17:24 +0000284 LongDoubleFormat = &llvm::APFloat::IEEEquad;
285 }
John Thompsoned4e2952009-11-05 20:14:16 +0000286}
Chris Lattner72cdcac2009-10-21 06:24:21 +0000287
Chris Lattnerec0a6d92007-09-22 18:29:59 +0000288//===----------------------------------------------------------------------===//
Chris Lattnerec0a6d92007-09-22 18:29:59 +0000289
Chris Lattner10a5b382007-01-29 05:24:35 +0000290
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000291static StringRef removeGCCRegisterPrefix(StringRef Name) {
Anders Carlsson3ed4aea2008-02-06 00:11:32 +0000292 if (Name[0] == '%' || Name[0] == '#')
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000293 Name = Name.substr(1);
Michael J. Spencerfeb799c2010-10-18 07:10:59 +0000294
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000295 return Name;
Anders Carlsson3ed4aea2008-02-06 00:11:32 +0000296}
297
Eric Christopherfd9a5f42011-06-28 18:20:53 +0000298/// isValidClobber - Returns whether the passed in string is
299/// a valid clobber in an inline asm statement. This is used by
300/// Sema.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000301bool TargetInfo::isValidClobber(StringRef Name) const {
Eric Christopherfd9a5f42011-06-28 18:20:53 +0000302 return (isValidGCCRegisterName(Name) ||
303 Name == "memory" || Name == "cc");
304}
305
Anders Carlsson5fa3f342007-11-24 23:38:12 +0000306/// isValidGCCRegisterName - Returns whether the passed in string
307/// is a valid register name according to GCC. This is used by Sema for
308/// inline asm statements.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000309bool TargetInfo::isValidGCCRegisterName(StringRef Name) const {
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000310 if (Name.empty())
311 return false;
Michael J. Spencerfeb799c2010-10-18 07:10:59 +0000312
Anders Carlsson290aa852007-11-25 00:25:21 +0000313 const char * const *Names;
314 unsigned NumNames;
Mike Stump11289f42009-09-09 15:08:12 +0000315
Anders Carlsson290aa852007-11-25 00:25:21 +0000316 // Get rid of any register prefix.
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000317 Name = removeGCCRegisterPrefix(Name);
Anders Carlsson3ed4aea2008-02-06 00:11:32 +0000318
Chris Lattnerc3a669b2008-03-08 08:24:01 +0000319 getGCCRegNames(Names, NumNames);
Mike Stump11289f42009-09-09 15:08:12 +0000320
Anders Carlsson290aa852007-11-25 00:25:21 +0000321 // If we have a number it maps to an entry in the register name array.
Jordan Rosea7d03842013-02-08 22:30:41 +0000322 if (isDigit(Name[0])) {
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000323 int n;
324 if (!Name.getAsInteger(0, n))
Anders Carlsson290aa852007-11-25 00:25:21 +0000325 return n >= 0 && (unsigned)n < NumNames;
326 }
327
328 // Check register names.
329 for (unsigned i = 0; i < NumNames; i++) {
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000330 if (Name == Names[i])
Anders Carlsson290aa852007-11-25 00:25:21 +0000331 return true;
332 }
Mike Stump11289f42009-09-09 15:08:12 +0000333
Eric Christophercdd36352011-06-21 00:05:20 +0000334 // Check any additional names that we have.
335 const AddlRegName *AddlNames;
336 unsigned NumAddlNames;
337 getGCCAddlRegNames(AddlNames, NumAddlNames);
338 for (unsigned i = 0; i < NumAddlNames; i++)
339 for (unsigned j = 0; j < llvm::array_lengthof(AddlNames[i].Names); j++) {
340 if (!AddlNames[i].Names[j])
341 break;
342 // Make sure the register that the additional name is for is within
343 // the bounds of the register names from above.
344 if (AddlNames[i].Names[j] == Name && AddlNames[i].RegNum < NumNames)
345 return true;
346 }
347
Anders Carlsson290aa852007-11-25 00:25:21 +0000348 // Now check aliases.
Chris Lattnerc3a669b2008-03-08 08:24:01 +0000349 const GCCRegAlias *Aliases;
Anders Carlsson290aa852007-11-25 00:25:21 +0000350 unsigned NumAliases;
Mike Stump11289f42009-09-09 15:08:12 +0000351
Chris Lattnerc3a669b2008-03-08 08:24:01 +0000352 getGCCRegAliases(Aliases, NumAliases);
Anders Carlsson290aa852007-11-25 00:25:21 +0000353 for (unsigned i = 0; i < NumAliases; i++) {
354 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
355 if (!Aliases[i].Aliases[j])
356 break;
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000357 if (Aliases[i].Aliases[j] == Name)
Anders Carlsson290aa852007-11-25 00:25:21 +0000358 return true;
359 }
360 }
Mike Stump11289f42009-09-09 15:08:12 +0000361
Anders Carlsson5fa3f342007-11-24 23:38:12 +0000362 return false;
363}
Anders Carlssonf511f642007-11-27 04:11:28 +0000364
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000365StringRef
366TargetInfo::getNormalizedGCCRegisterName(StringRef Name) const {
Anders Carlssonf511f642007-11-27 04:11:28 +0000367 assert(isValidGCCRegisterName(Name) && "Invalid register passed in");
Mike Stump11289f42009-09-09 15:08:12 +0000368
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000369 // Get rid of any register prefix.
370 Name = removeGCCRegisterPrefix(Name);
Mike Stump11289f42009-09-09 15:08:12 +0000371
Anders Carlssonf511f642007-11-27 04:11:28 +0000372 const char * const *Names;
373 unsigned NumNames;
374
Chris Lattnerc3a669b2008-03-08 08:24:01 +0000375 getGCCRegNames(Names, NumNames);
Anders Carlssonf511f642007-11-27 04:11:28 +0000376
377 // First, check if we have a number.
Jordan Rosea7d03842013-02-08 22:30:41 +0000378 if (isDigit(Name[0])) {
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000379 int n;
380 if (!Name.getAsInteger(0, n)) {
Mike Stump11289f42009-09-09 15:08:12 +0000381 assert(n >= 0 && (unsigned)n < NumNames &&
Anders Carlssonf511f642007-11-27 04:11:28 +0000382 "Out of bounds register number!");
383 return Names[n];
384 }
385 }
Mike Stump11289f42009-09-09 15:08:12 +0000386
Eric Christophercdd36352011-06-21 00:05:20 +0000387 // Check any additional names that we have.
388 const AddlRegName *AddlNames;
389 unsigned NumAddlNames;
390 getGCCAddlRegNames(AddlNames, NumAddlNames);
391 for (unsigned i = 0; i < NumAddlNames; i++)
392 for (unsigned j = 0; j < llvm::array_lengthof(AddlNames[i].Names); j++) {
393 if (!AddlNames[i].Names[j])
394 break;
395 // Make sure the register that the additional name is for is within
396 // the bounds of the register names from above.
397 if (AddlNames[i].Names[j] == Name && AddlNames[i].RegNum < NumNames)
398 return Name;
399 }
400
Anders Carlssonf511f642007-11-27 04:11:28 +0000401 // Now check aliases.
Chris Lattnerc3a669b2008-03-08 08:24:01 +0000402 const GCCRegAlias *Aliases;
Anders Carlssonf511f642007-11-27 04:11:28 +0000403 unsigned NumAliases;
Mike Stump11289f42009-09-09 15:08:12 +0000404
Chris Lattnerc3a669b2008-03-08 08:24:01 +0000405 getGCCRegAliases(Aliases, NumAliases);
Anders Carlssonf511f642007-11-27 04:11:28 +0000406 for (unsigned i = 0; i < NumAliases; i++) {
407 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
408 if (!Aliases[i].Aliases[j])
409 break;
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000410 if (Aliases[i].Aliases[j] == Name)
Anders Carlssonf511f642007-11-27 04:11:28 +0000411 return Aliases[i].Register;
412 }
413 }
Mike Stump11289f42009-09-09 15:08:12 +0000414
Anders Carlssonf511f642007-11-27 04:11:28 +0000415 return Name;
416}
417
Chris Lattnerc3f4c7b2009-04-26 17:19:08 +0000418bool TargetInfo::validateOutputConstraint(ConstraintInfo &Info) const {
419 const char *Name = Info.getConstraintStr().c_str();
Anders Carlssonf511f642007-11-27 04:11:28 +0000420 // An output constraint must start with '=' or '+'
421 if (*Name != '=' && *Name != '+')
422 return false;
423
424 if (*Name == '+')
Chris Lattnerd9725f72009-04-26 07:16:29 +0000425 Info.setIsReadWrite();
Anders Carlssonf511f642007-11-27 04:11:28 +0000426
427 Name++;
428 while (*Name) {
429 switch (*Name) {
430 default:
Chris Lattnerd9725f72009-04-26 07:16:29 +0000431 if (!validateAsmConstraint(Name, Info)) {
Ted Kremenekb44485b2008-04-24 16:36:38 +0000432 // FIXME: We temporarily return false
Anders Carlssonf511f642007-11-27 04:11:28 +0000433 // so we can add more constraints as we hit it.
434 // Eventually, an unknown constraint should just be treated as 'g'.
Ted Kremenekb44485b2008-04-24 16:36:38 +0000435 return false;
Anders Carlssonf511f642007-11-27 04:11:28 +0000436 }
437 case '&': // early clobber.
438 break;
Chris Lattnerf3154712009-10-13 04:32:07 +0000439 case '%': // commutative.
440 // FIXME: Check that there is a another register after this one.
441 break;
Anders Carlssonf511f642007-11-27 04:11:28 +0000442 case 'r': // general register.
Chris Lattnerd9725f72009-04-26 07:16:29 +0000443 Info.setAllowsRegister();
Anders Carlssonf511f642007-11-27 04:11:28 +0000444 break;
445 case 'm': // memory operand.
Dale Johannesen8499f472010-09-07 18:40:41 +0000446 case 'o': // offsetable memory operand.
447 case 'V': // non-offsetable memory operand.
448 case '<': // autodecrement memory operand.
449 case '>': // autoincrement memory operand.
Chris Lattnerd9725f72009-04-26 07:16:29 +0000450 Info.setAllowsMemory();
Anders Carlssonf511f642007-11-27 04:11:28 +0000451 break;
452 case 'g': // general register, memory operand or immediate integer.
Anders Carlssone70cde12009-01-18 02:12:04 +0000453 case 'X': // any operand.
Chris Lattnerd9725f72009-04-26 07:16:29 +0000454 Info.setAllowsRegister();
455 Info.setAllowsMemory();
Anders Carlssonf511f642007-11-27 04:11:28 +0000456 break;
John Thompsona5c7d702010-08-10 19:20:14 +0000457 case ',': // multiple alternative constraint. Pass it.
John Thompson46667af2010-08-11 00:58:20 +0000458 // Handle additional optional '=' or '+' modifiers.
John Thompson12240612010-09-18 01:15:13 +0000459 if (Name[1] == '=' || Name[1] == '+')
John Thompson46667af2010-08-11 00:58:20 +0000460 Name++;
John Thompsona5c7d702010-08-10 19:20:14 +0000461 break;
462 case '?': // Disparage slightly code.
Dale Johannesen8499f472010-09-07 18:40:41 +0000463 case '!': // Disparage severely.
Ulrich Weigand7bcc7ec2012-10-29 12:20:54 +0000464 case '#': // Ignore as constraint.
465 case '*': // Ignore for choosing register preferences.
John Thompsona5c7d702010-08-10 19:20:14 +0000466 break; // Pass them.
Anders Carlssonf511f642007-11-27 04:11:28 +0000467 }
Mike Stump11289f42009-09-09 15:08:12 +0000468
Anders Carlssonf511f642007-11-27 04:11:28 +0000469 Name++;
470 }
Mike Stump11289f42009-09-09 15:08:12 +0000471
Benjamin Kramer1e4a8862013-04-18 22:49:48 +0000472 // If a constraint allows neither memory nor register operands it contains
473 // only modifiers. Reject it.
Benjamin Kramer7ee3b9c2013-04-18 13:23:23 +0000474 return Info.allowsMemory() || Info.allowsRegister();
Anders Carlssonf511f642007-11-27 04:11:28 +0000475}
476
Anders Carlssona79203b2009-01-18 01:56:57 +0000477bool TargetInfo::resolveSymbolicName(const char *&Name,
Chris Lattnerc16d4762009-04-26 17:57:12 +0000478 ConstraintInfo *OutputConstraints,
479 unsigned NumOutputs,
Chris Lattnerd9725f72009-04-26 07:16:29 +0000480 unsigned &Index) const {
Anders Carlssona79203b2009-01-18 01:56:57 +0000481 assert(*Name == '[' && "Symbolic name did not start with '['");
Anders Carlssona79203b2009-01-18 01:56:57 +0000482 Name++;
483 const char *Start = Name;
484 while (*Name && *Name != ']')
485 Name++;
Mike Stump11289f42009-09-09 15:08:12 +0000486
Anders Carlssona79203b2009-01-18 01:56:57 +0000487 if (!*Name) {
488 // Missing ']'
489 return false;
490 }
Mike Stump11289f42009-09-09 15:08:12 +0000491
Anders Carlssona79203b2009-01-18 01:56:57 +0000492 std::string SymbolicName(Start, Name - Start);
Mike Stump11289f42009-09-09 15:08:12 +0000493
Chris Lattnerc16d4762009-04-26 17:57:12 +0000494 for (Index = 0; Index != NumOutputs; ++Index)
495 if (SymbolicName == OutputConstraints[Index].getName())
Anders Carlssona79203b2009-01-18 01:56:57 +0000496 return true;
Anders Carlssona79203b2009-01-18 01:56:57 +0000497
498 return false;
499}
500
Chris Lattnerc16d4762009-04-26 17:57:12 +0000501bool TargetInfo::validateInputConstraint(ConstraintInfo *OutputConstraints,
502 unsigned NumOutputs,
Chris Lattnerd9725f72009-04-26 07:16:29 +0000503 ConstraintInfo &Info) const {
Chris Lattnerc3f4c7b2009-04-26 17:19:08 +0000504 const char *Name = Info.ConstraintStr.c_str();
505
Duncan P. N. Exon Smithd68c7aa2013-12-16 03:20:06 +0000506 if (!*Name)
507 return false;
508
Anders Carlssonf511f642007-11-27 04:11:28 +0000509 while (*Name) {
510 switch (*Name) {
511 default:
512 // Check if we have a matching constraint
513 if (*Name >= '0' && *Name <= '9') {
514 unsigned i = *Name - '0';
Mike Stump11289f42009-09-09 15:08:12 +0000515
Anders Carlssonf511f642007-11-27 04:11:28 +0000516 // Check if matching constraint is out of bounds.
517 if (i >= NumOutputs)
518 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000519
Anders Carlssonda1f5fc2010-11-03 02:22:29 +0000520 // A number must refer to an output only operand.
521 if (OutputConstraints[i].isReadWrite())
522 return false;
523
Anders Carlsson2d5f8b42010-11-03 02:54:51 +0000524 // If the constraint is already tied, it must be tied to the
525 // same operand referenced to by the number.
526 if (Info.hasTiedOperand() && Info.getTiedOperand() != i)
527 return false;
528
Mike Stump11289f42009-09-09 15:08:12 +0000529 // The constraint should have the same info as the respective
Anders Carlsson570c3572009-01-27 20:38:24 +0000530 // output constraint.
Chris Lattner4c92b512009-04-26 18:05:25 +0000531 Info.setTiedOperand(i, OutputConstraints[i]);
Chris Lattnerd9725f72009-04-26 07:16:29 +0000532 } else if (!validateAsmConstraint(Name, Info)) {
Daniel Dunbar81128e02008-08-25 09:46:27 +0000533 // FIXME: This error return is in place temporarily so we can
534 // add more constraints as we hit it. Eventually, an unknown
535 // constraint should just be treated as 'g'.
536 return false;
Anders Carlssona79203b2009-01-18 01:56:57 +0000537 }
538 break;
539 case '[': {
540 unsigned Index = 0;
Chris Lattnerc16d4762009-04-26 17:57:12 +0000541 if (!resolveSymbolicName(Name, OutputConstraints, NumOutputs, Index))
Anders Carlssona79203b2009-01-18 01:56:57 +0000542 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000543
Anders Carlsson2d5f8b42010-11-03 02:54:51 +0000544 // If the constraint is already tied, it must be tied to the
545 // same operand referenced to by the number.
546 if (Info.hasTiedOperand() && Info.getTiedOperand() != Index)
547 return false;
548
Eli Friedman854f1102010-08-11 23:03:37 +0000549 Info.setTiedOperand(Index, OutputConstraints[Index]);
Anders Carlssona79203b2009-01-18 01:56:57 +0000550 break;
Mike Stump11289f42009-09-09 15:08:12 +0000551 }
Anders Carlsson050f4942007-12-08 19:32:57 +0000552 case '%': // commutative
553 // FIXME: Fail if % is used with the last operand.
554 break;
Anders Carlssonf511f642007-11-27 04:11:28 +0000555 case 'i': // immediate integer.
Anders Carlssona3a96af2008-03-09 06:02:02 +0000556 case 'n': // immediate integer with a known value.
Anders Carlssonf511f642007-11-27 04:11:28 +0000557 break;
Chris Lattnerdbcc5ca2009-05-06 04:33:31 +0000558 case 'I': // Various constant constraints with target-specific meanings.
559 case 'J':
560 case 'K':
561 case 'L':
562 case 'M':
563 case 'N':
564 case 'O':
565 case 'P':
566 break;
Anders Carlssonf511f642007-11-27 04:11:28 +0000567 case 'r': // general register.
Chris Lattnerd9725f72009-04-26 07:16:29 +0000568 Info.setAllowsRegister();
Anders Carlssonf511f642007-11-27 04:11:28 +0000569 break;
570 case 'm': // memory operand.
Dale Johannesen8499f472010-09-07 18:40:41 +0000571 case 'o': // offsettable memory operand.
572 case 'V': // non-offsettable memory operand.
573 case '<': // autodecrement memory operand.
574 case '>': // autoincrement memory operand.
Chris Lattnerd9725f72009-04-26 07:16:29 +0000575 Info.setAllowsMemory();
Anders Carlssonf511f642007-11-27 04:11:28 +0000576 break;
577 case 'g': // general register, memory operand or immediate integer.
Anders Carlssone70cde12009-01-18 02:12:04 +0000578 case 'X': // any operand.
Chris Lattnerd9725f72009-04-26 07:16:29 +0000579 Info.setAllowsRegister();
580 Info.setAllowsMemory();
Anders Carlssonf511f642007-11-27 04:11:28 +0000581 break;
John Thompsonc467aa22010-09-21 22:04:54 +0000582 case 'E': // immediate floating point.
583 case 'F': // immediate floating point.
584 case 'p': // address operand.
585 break;
John Thompsona5c7d702010-08-10 19:20:14 +0000586 case ',': // multiple alternative constraint. Ignore comma.
587 break;
588 case '?': // Disparage slightly code.
Chris Lattner57540c52011-04-15 05:22:18 +0000589 case '!': // Disparage severely.
Ulrich Weigand7bcc7ec2012-10-29 12:20:54 +0000590 case '#': // Ignore as constraint.
591 case '*': // Ignore for choosing register preferences.
John Thompsona5c7d702010-08-10 19:20:14 +0000592 break; // Pass them.
Anders Carlssonf511f642007-11-27 04:11:28 +0000593 }
Mike Stump11289f42009-09-09 15:08:12 +0000594
Anders Carlssonf511f642007-11-27 04:11:28 +0000595 Name++;
596 }
Mike Stump11289f42009-09-09 15:08:12 +0000597
Anders Carlssonf511f642007-11-27 04:11:28 +0000598 return true;
599}
John McCall359b8852013-01-25 22:30:49 +0000600
601bool TargetCXXABI::tryParse(llvm::StringRef name) {
602 const Kind unknown = static_cast<Kind>(-1);
603 Kind kind = llvm::StringSwitch<Kind>(name)
604 .Case("arm", GenericARM)
605 .Case("ios", iOS)
606 .Case("itanium", GenericItanium)
607 .Case("microsoft", Microsoft)
608 .Default(unknown);
609 if (kind == unknown) return false;
610
611 set(kind);
612 return true;
613}