blob: aa7385eb005da543bde5720da9c6779ef20de898 [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 Weigandca3cb7f2015-04-21 17:29:35 +000039 DefaultAlignForAttributeAligned = 128;
Ulrich Weigandfa806422013-05-06 16:23:57 +000040 MinGlobalAlign = 0;
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +000041 HalfWidth = 16;
42 HalfAlign = 16;
Eli Friedmanb5366062008-05-20 14:21:01 +000043 FloatWidth = 32;
44 FloatAlign = 32;
Nate Begeman65bea232008-04-18 17:17:24 +000045 DoubleWidth = 64;
Eli Friedmanb5366062008-05-20 14:21:01 +000046 DoubleAlign = 64;
47 LongDoubleWidth = 64;
48 LongDoubleAlign = 64;
Rafael Espindolae971b9a2010-06-04 23:15:27 +000049 LargeArrayMinWidth = 0;
50 LargeArrayAlign = 0;
Eli Friedman4b72fdd2011-10-14 20:59:01 +000051 MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 0;
Chad Rosiercc40ea72012-07-13 23:57:43 +000052 MaxVectorAlign = 0;
Paul Robinsond30e2ee2015-07-14 20:52:32 +000053 MaxTLSAlign = 0;
Alexey Bataev00396512015-07-02 03:40:19 +000054 SimdDefaultAlign = 0;
Anders Carlsson2a79a902008-10-31 16:05:19 +000055 SizeType = UnsignedLong;
Eli Friedmand50881c2008-11-02 02:43:55 +000056 PtrDiffType = SignedLong;
Sanjiv Guptad7959242008-10-31 09:52:39 +000057 IntMaxType = SignedLongLong;
Chris Lattner7e4c81c2009-02-13 22:28:55 +000058 IntPtrType = SignedLong;
Eli Friedmand50881c2008-11-02 02:43:55 +000059 WCharType = SignedInt;
Chris Lattner67204922009-10-21 04:59:34 +000060 WIntType = SignedInt;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +000061 Char16Type = UnsignedShort;
62 Char32Type = UnsignedInt;
Eli Friedman2857ccb2009-07-01 03:36:11 +000063 Int64Type = SignedLongLong;
Edward O'Callaghan847f2a12009-11-21 00:49:54 +000064 SigAtomicType = SignedInt;
Eli Friedman4e91899e2012-11-27 02:58:24 +000065 ProcessIDType = SignedInt;
Fariborz Jahanian29898f42012-04-16 21:03:30 +000066 UseSignedCharForObjCBool = true;
Daniel Dunbar1da65112010-04-15 15:06:18 +000067 UseBitFieldTypeAlignment = true;
Chad Rosier18903ee2011-08-04 01:21:14 +000068 UseZeroLengthBitfieldAlignment = false;
69 ZeroLengthBitfieldBoundary = 0;
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +000070 HalfFormat = &llvm::APFloat::IEEEhalf;
Chris Lattner1df27862008-03-08 08:59:43 +000071 FloatFormat = &llvm::APFloat::IEEEsingle;
72 DoubleFormat = &llvm::APFloat::IEEEdouble;
73 LongDoubleFormat = &llvm::APFloat::IEEEdouble;
Eric Christopher964a5f32015-08-05 23:48:05 +000074 DataLayoutString = nullptr;
Chris Lattnerf37bafc2008-10-05 19:22:37 +000075 UserLabelPrefix = "_";
Roman Divacky178e01602011-02-10 16:52:03 +000076 MCountName = "mcount";
Abramo Bagnara41bfb662011-09-08 14:20:25 +000077 RegParmMax = 0;
78 SSERegParmMax = 0;
Daniel Dunbarbd606522010-05-27 00:35:16 +000079 HasAlignMac68kSupport = false;
Daniel Dunbar6f2e8392010-07-14 23:39:36 +000080
81 // Default to no types using fpret.
82 RealTypeUsesObjCFPRet = 0;
John McCall86353412010-08-21 22:46:04 +000083
Anders Carlsson2f1a6c32011-10-31 16:27:11 +000084 // Default to not using fp2ret for __Complex long double
85 ComplexLongDoubleUsesFP2Ret = false;
86
Hans Wennborgc9bd88e2014-01-14 19:35:09 +000087 // Set the C++ ABI based on the triple.
Saleem Abdulrasool377066a2014-03-27 22:50:18 +000088 TheCXXABI.set(Triple.isKnownWindowsMSVCEnvironment()
Hans Wennborgc9bd88e2014-01-14 19:35:09 +000089 ? TargetCXXABI::Microsoft
90 : TargetCXXABI::GenericItanium);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +000091
92 // Default to an empty address space map.
93 AddrSpaceMap = &DefaultAddrSpaceMap;
David Tweed31d09b02013-09-13 12:04:22 +000094 UseAddrSpaceMapMangling = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +000095
96 // Default to an unknown platform name.
97 PlatformName = "unknown";
98 PlatformMinVersion = VersionTuple();
Chris Lattner1df27862008-03-08 08:59:43 +000099}
100
Chris Lattnerc3a669b2008-03-08 08:24:01 +0000101// Out of line virtual dtor for TargetInfo.
102TargetInfo::~TargetInfo() {}
Chris Lattner2194ddc2006-10-14 18:32:26 +0000103
Chris Lattnera91c30f2009-02-06 05:04:11 +0000104/// getTypeName - Return the user string for the specified integer type enum.
105/// For example, SignedShort -> "short".
106const char *TargetInfo::getTypeName(IntType T) {
107 switch (T) {
David Blaikie83d382b2011-09-23 05:06:16 +0000108 default: llvm_unreachable("not an integer!");
Joerg Sonnenberger3d9478c2014-07-28 21:06:22 +0000109 case SignedChar: return "signed char";
Stepan Dyatkovskiy5a637922013-09-05 11:23:21 +0000110 case UnsignedChar: return "unsigned char";
Chris Lattnera91c30f2009-02-06 05:04:11 +0000111 case SignedShort: return "short";
112 case UnsignedShort: return "unsigned short";
113 case SignedInt: return "int";
114 case UnsignedInt: return "unsigned int";
115 case SignedLong: return "long int";
116 case UnsignedLong: return "long unsigned int";
117 case SignedLongLong: return "long long int";
118 case UnsignedLongLong: return "long long unsigned int";
119 }
120}
121
Chris Lattner72cdcac2009-10-21 06:24:21 +0000122/// getTypeConstantSuffix - Return the constant suffix for the specified
123/// integer type enum. For example, SignedLong -> "L".
Joerg Sonnenberger587deea2014-07-17 20:12:32 +0000124const char *TargetInfo::getTypeConstantSuffix(IntType T) const {
Chris Lattner72cdcac2009-10-21 06:24:21 +0000125 switch (T) {
David Blaikie83d382b2011-09-23 05:06:16 +0000126 default: llvm_unreachable("not an integer!");
Stepan Dyatkovskiy5a637922013-09-05 11:23:21 +0000127 case SignedChar:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000128 case SignedShort:
129 case SignedInt: return "";
130 case SignedLong: return "L";
131 case SignedLongLong: return "LL";
Stepan Dyatkovskiy5a637922013-09-05 11:23:21 +0000132 case UnsignedChar:
Joerg Sonnenberger587deea2014-07-17 20:12:32 +0000133 if (getCharWidth() < getIntWidth())
134 return "";
Chris Lattner72cdcac2009-10-21 06:24:21 +0000135 case UnsignedShort:
Joerg Sonnenberger587deea2014-07-17 20:12:32 +0000136 if (getShortWidth() < getIntWidth())
137 return "";
Chris Lattner72cdcac2009-10-21 06:24:21 +0000138 case UnsignedInt: return "U";
139 case UnsignedLong: return "UL";
140 case UnsignedLongLong: return "ULL";
141 }
142}
143
Joerg Sonnenbergerbe324f92014-07-15 11:30:00 +0000144/// getTypeFormatModifier - Return the printf format modifier for the
145/// specified integer type enum. For example, SignedLong -> "l".
146
147const char *TargetInfo::getTypeFormatModifier(IntType T) {
148 switch (T) {
149 default: llvm_unreachable("not an integer!");
150 case SignedChar:
151 case UnsignedChar: return "hh";
152 case SignedShort:
153 case UnsignedShort: return "h";
154 case SignedInt:
155 case UnsignedInt: return "";
156 case SignedLong:
157 case UnsignedLong: return "l";
158 case SignedLongLong:
159 case UnsignedLongLong: return "ll";
160 }
161}
162
Michael J. Spencerfeb799c2010-10-18 07:10:59 +0000163/// getTypeWidth - Return the width (in bits) of the specified integer type
Chris Lattner72cdcac2009-10-21 06:24:21 +0000164/// enum. For example, SignedInt -> getIntWidth().
165unsigned TargetInfo::getTypeWidth(IntType T) const {
166 switch (T) {
David Blaikie83d382b2011-09-23 05:06:16 +0000167 default: llvm_unreachable("not an integer!");
Stepan Dyatkovskiy5a637922013-09-05 11:23:21 +0000168 case SignedChar:
169 case UnsignedChar: return getCharWidth();
Chris Lattnere4a8c642009-11-05 21:21:32 +0000170 case SignedShort:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000171 case UnsignedShort: return getShortWidth();
Chris Lattnere4a8c642009-11-05 21:21:32 +0000172 case SignedInt:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000173 case UnsignedInt: return getIntWidth();
Chris Lattnere4a8c642009-11-05 21:21:32 +0000174 case SignedLong:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000175 case UnsignedLong: return getLongWidth();
Chris Lattnere4a8c642009-11-05 21:21:32 +0000176 case SignedLongLong:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000177 case UnsignedLongLong: return getLongLongWidth();
178 };
179}
180
Stepan Dyatkovskiy5a637922013-09-05 11:23:21 +0000181TargetInfo::IntType TargetInfo::getIntTypeByWidth(
182 unsigned BitWidth, bool IsSigned) const {
183 if (getCharWidth() == BitWidth)
184 return IsSigned ? SignedChar : UnsignedChar;
185 if (getShortWidth() == BitWidth)
186 return IsSigned ? SignedShort : UnsignedShort;
187 if (getIntWidth() == BitWidth)
188 return IsSigned ? SignedInt : UnsignedInt;
189 if (getLongWidth() == BitWidth)
190 return IsSigned ? SignedLong : UnsignedLong;
191 if (getLongLongWidth() == BitWidth)
192 return IsSigned ? SignedLongLong : UnsignedLongLong;
193 return NoInt;
194}
195
JF Bastienab8d0a02014-06-25 01:31:33 +0000196TargetInfo::IntType TargetInfo::getLeastIntTypeByWidth(unsigned BitWidth,
197 bool IsSigned) const {
198 if (getCharWidth() >= BitWidth)
199 return IsSigned ? SignedChar : UnsignedChar;
200 if (getShortWidth() >= BitWidth)
201 return IsSigned ? SignedShort : UnsignedShort;
202 if (getIntWidth() >= BitWidth)
203 return IsSigned ? SignedInt : UnsignedInt;
204 if (getLongWidth() >= BitWidth)
205 return IsSigned ? SignedLong : UnsignedLong;
206 if (getLongLongWidth() >= BitWidth)
207 return IsSigned ? SignedLongLong : UnsignedLongLong;
208 return NoInt;
209}
210
Stepan Dyatkovskiy5a637922013-09-05 11:23:21 +0000211TargetInfo::RealType TargetInfo::getRealTypeByWidth(unsigned BitWidth) const {
212 if (getFloatWidth() == BitWidth)
213 return Float;
214 if (getDoubleWidth() == BitWidth)
215 return Double;
216
217 switch (BitWidth) {
218 case 96:
219 if (&getLongDoubleFormat() == &llvm::APFloat::x87DoubleExtended)
220 return LongDouble;
221 break;
222 case 128:
Stepan Dyatkovskiya4a59d72013-09-09 07:46:54 +0000223 if (&getLongDoubleFormat() == &llvm::APFloat::PPCDoubleDouble ||
224 &getLongDoubleFormat() == &llvm::APFloat::IEEEquad)
Stepan Dyatkovskiy5a637922013-09-05 11:23:21 +0000225 return LongDouble;
226 break;
227 }
228
229 return NoFloat;
230}
231
Michael J. Spencerfeb799c2010-10-18 07:10:59 +0000232/// getTypeAlign - Return the alignment (in bits) of the specified integer type
Chris Lattnere4a8c642009-11-05 21:21:32 +0000233/// enum. For example, SignedInt -> getIntAlign().
234unsigned TargetInfo::getTypeAlign(IntType T) const {
235 switch (T) {
David Blaikie83d382b2011-09-23 05:06:16 +0000236 default: llvm_unreachable("not an integer!");
Stepan Dyatkovskiy5a637922013-09-05 11:23:21 +0000237 case SignedChar:
238 case UnsignedChar: return getCharAlign();
Chris Lattnere4a8c642009-11-05 21:21:32 +0000239 case SignedShort:
240 case UnsignedShort: return getShortAlign();
241 case SignedInt:
242 case UnsignedInt: return getIntAlign();
243 case SignedLong:
244 case UnsignedLong: return getLongAlign();
245 case SignedLongLong:
246 case UnsignedLongLong: return getLongLongAlign();
247 };
248}
249
Chris Lattnerc0c04392009-10-25 22:49:18 +0000250/// isTypeSigned - Return whether an integer types is signed. Returns true if
Chris Lattner72cdcac2009-10-21 06:24:21 +0000251/// the type is signed; false otherwise.
Chris Lattnerad3467e2010-12-25 23:25:43 +0000252bool TargetInfo::isTypeSigned(IntType T) {
Chris Lattner72cdcac2009-10-21 06:24:21 +0000253 switch (T) {
David Blaikie83d382b2011-09-23 05:06:16 +0000254 default: llvm_unreachable("not an integer!");
Stepan Dyatkovskiy5a637922013-09-05 11:23:21 +0000255 case SignedChar:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000256 case SignedShort:
257 case SignedInt:
258 case SignedLong:
Michael J. Spencerfeb799c2010-10-18 07:10:59 +0000259 case SignedLongLong:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000260 return true;
Stepan Dyatkovskiy5a637922013-09-05 11:23:21 +0000261 case UnsignedChar:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000262 case UnsignedShort:
263 case UnsignedInt:
264 case UnsignedLong:
Michael J. Spencerfeb799c2010-10-18 07:10:59 +0000265 case UnsignedLongLong:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000266 return false;
267 };
268}
269
Alp Toker74437972014-07-06 05:14:24 +0000270/// adjust - Set forced language options.
John Thompsoned4e2952009-11-05 20:14:16 +0000271/// Apply changes to the target information with respect to certain
272/// language options which change the target configuration.
Alp Toker74437972014-07-06 05:14:24 +0000273void TargetInfo::adjust(const LangOptions &Opts) {
Daniel Dunbar9302f602010-04-15 15:06:22 +0000274 if (Opts.NoBitFieldTypeAlign)
275 UseBitFieldTypeAlignment = false;
276 if (Opts.ShortWChar)
John Thompsoned4e2952009-11-05 20:14:16 +0000277 WCharType = UnsignedShort;
David Tweed2da64382013-09-09 09:17:24 +0000278
279 if (Opts.OpenCL) {
280 // OpenCL C requires specific widths for types, irrespective of
281 // what these normally are for the target.
282 // We also define long long and long double here, although the
283 // OpenCL standard only mentions these as "reserved".
284 IntWidth = IntAlign = 32;
285 LongWidth = LongAlign = 64;
286 LongLongWidth = LongLongAlign = 128;
287 HalfWidth = HalfAlign = 16;
288 FloatWidth = FloatAlign = 32;
Pekka Jaaskelainen43c39d52013-12-18 18:15:03 +0000289
290 // Embedded 32-bit targets (OpenCL EP) might have double C type
291 // defined as float. Let's not override this as it might lead
292 // to generating illegal code that uses 64bit doubles.
293 if (DoubleWidth != FloatWidth) {
294 DoubleWidth = DoubleAlign = 64;
295 DoubleFormat = &llvm::APFloat::IEEEdouble;
296 }
David Tweed2da64382013-09-09 09:17:24 +0000297 LongDoubleWidth = LongDoubleAlign = 128;
298
299 assert(PointerWidth == 32 || PointerWidth == 64);
David Tweed6c2149b2013-09-10 08:00:34 +0000300 bool Is32BitArch = PointerWidth == 32;
301 SizeType = Is32BitArch ? UnsignedInt : UnsignedLong;
302 PtrDiffType = Is32BitArch ? SignedInt : SignedLong;
303 IntPtrType = Is32BitArch ? SignedInt : SignedLong;
David Tweed2da64382013-09-09 09:17:24 +0000304
305 IntMaxType = SignedLongLong;
David Tweed2da64382013-09-09 09:17:24 +0000306 Int64Type = SignedLong;
307
308 HalfFormat = &llvm::APFloat::IEEEhalf;
309 FloatFormat = &llvm::APFloat::IEEEsingle;
David Tweed2da64382013-09-09 09:17:24 +0000310 LongDoubleFormat = &llvm::APFloat::IEEEquad;
311 }
John Thompsoned4e2952009-11-05 20:14:16 +0000312}
Chris Lattner72cdcac2009-10-21 06:24:21 +0000313
Eric Christophere9cdcae2015-09-01 18:13:20 +0000314bool TargetInfo::initFeatureMap(llvm::StringMap<bool> &Features,
315 DiagnosticsEngine &Diags, StringRef CPU,
316 std::vector<std::string> &FeatureVec) const {
317 for (const auto &F : FeatureVec) {
318 const char *Name = F.c_str();
319 // Apply the feature via the target.
320 bool Enabled = Name[0] == '+';
321 setFeatureEnabled(Features, Name + 1, Enabled);
322 }
323 return true;
324}
325
Chris Lattnerec0a6d92007-09-22 18:29:59 +0000326//===----------------------------------------------------------------------===//
Chris Lattnerec0a6d92007-09-22 18:29:59 +0000327
Chris Lattner10a5b382007-01-29 05:24:35 +0000328
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000329static StringRef removeGCCRegisterPrefix(StringRef Name) {
Anders Carlsson3ed4aea2008-02-06 00:11:32 +0000330 if (Name[0] == '%' || Name[0] == '#')
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000331 Name = Name.substr(1);
Michael J. Spencerfeb799c2010-10-18 07:10:59 +0000332
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000333 return Name;
Anders Carlsson3ed4aea2008-02-06 00:11:32 +0000334}
335
Eric Christopherfd9a5f42011-06-28 18:20:53 +0000336/// isValidClobber - Returns whether the passed in string is
337/// a valid clobber in an inline asm statement. This is used by
338/// Sema.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000339bool TargetInfo::isValidClobber(StringRef Name) const {
Eric Christopherfd9a5f42011-06-28 18:20:53 +0000340 return (isValidGCCRegisterName(Name) ||
341 Name == "memory" || Name == "cc");
342}
343
Anders Carlsson5fa3f342007-11-24 23:38:12 +0000344/// isValidGCCRegisterName - Returns whether the passed in string
345/// is a valid register name according to GCC. This is used by Sema for
346/// inline asm statements.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000347bool TargetInfo::isValidGCCRegisterName(StringRef Name) const {
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000348 if (Name.empty())
349 return false;
Michael J. Spencerfeb799c2010-10-18 07:10:59 +0000350
Anders Carlsson290aa852007-11-25 00:25:21 +0000351 const char * const *Names;
352 unsigned NumNames;
Mike Stump11289f42009-09-09 15:08:12 +0000353
Anders Carlsson290aa852007-11-25 00:25:21 +0000354 // Get rid of any register prefix.
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000355 Name = removeGCCRegisterPrefix(Name);
Olivier Goffartfc8f8932014-08-17 13:19:48 +0000356 if (Name.empty())
357 return false;
Anders Carlsson3ed4aea2008-02-06 00:11:32 +0000358
Chris Lattnerc3a669b2008-03-08 08:24:01 +0000359 getGCCRegNames(Names, NumNames);
Mike Stump11289f42009-09-09 15:08:12 +0000360
Anders Carlsson290aa852007-11-25 00:25:21 +0000361 // If we have a number it maps to an entry in the register name array.
Jordan Rosea7d03842013-02-08 22:30:41 +0000362 if (isDigit(Name[0])) {
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000363 int n;
364 if (!Name.getAsInteger(0, n))
Anders Carlsson290aa852007-11-25 00:25:21 +0000365 return n >= 0 && (unsigned)n < NumNames;
366 }
367
368 // Check register names.
369 for (unsigned i = 0; i < NumNames; i++) {
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000370 if (Name == Names[i])
Anders Carlsson290aa852007-11-25 00:25:21 +0000371 return true;
372 }
Mike Stump11289f42009-09-09 15:08:12 +0000373
Eric Christophercdd36352011-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 true;
386 }
387
Anders Carlsson290aa852007-11-25 00:25:21 +0000388 // Now check aliases.
Chris Lattnerc3a669b2008-03-08 08:24:01 +0000389 const GCCRegAlias *Aliases;
Anders Carlsson290aa852007-11-25 00:25:21 +0000390 unsigned NumAliases;
Mike Stump11289f42009-09-09 15:08:12 +0000391
Chris Lattnerc3a669b2008-03-08 08:24:01 +0000392 getGCCRegAliases(Aliases, NumAliases);
Anders Carlsson290aa852007-11-25 00:25:21 +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 Carlssonc7c5baa2010-01-30 19:12:25 +0000397 if (Aliases[i].Aliases[j] == Name)
Anders Carlsson290aa852007-11-25 00:25:21 +0000398 return true;
399 }
400 }
Mike Stump11289f42009-09-09 15:08:12 +0000401
Anders Carlsson5fa3f342007-11-24 23:38:12 +0000402 return false;
403}
Anders Carlssonf511f642007-11-27 04:11:28 +0000404
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000405StringRef
406TargetInfo::getNormalizedGCCRegisterName(StringRef Name) const {
Anders Carlssonf511f642007-11-27 04:11:28 +0000407 assert(isValidGCCRegisterName(Name) && "Invalid register passed in");
Mike Stump11289f42009-09-09 15:08:12 +0000408
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000409 // Get rid of any register prefix.
410 Name = removeGCCRegisterPrefix(Name);
Mike Stump11289f42009-09-09 15:08:12 +0000411
Anders Carlssonf511f642007-11-27 04:11:28 +0000412 const char * const *Names;
413 unsigned NumNames;
414
Chris Lattnerc3a669b2008-03-08 08:24:01 +0000415 getGCCRegNames(Names, NumNames);
Anders Carlssonf511f642007-11-27 04:11:28 +0000416
417 // First, check if we have a number.
Jordan Rosea7d03842013-02-08 22:30:41 +0000418 if (isDigit(Name[0])) {
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000419 int n;
420 if (!Name.getAsInteger(0, n)) {
Mike Stump11289f42009-09-09 15:08:12 +0000421 assert(n >= 0 && (unsigned)n < NumNames &&
Anders Carlssonf511f642007-11-27 04:11:28 +0000422 "Out of bounds register number!");
423 return Names[n];
424 }
425 }
Mike Stump11289f42009-09-09 15:08:12 +0000426
Eric Christophercdd36352011-06-21 00:05:20 +0000427 // Check any additional names that we have.
428 const AddlRegName *AddlNames;
429 unsigned NumAddlNames;
430 getGCCAddlRegNames(AddlNames, NumAddlNames);
431 for (unsigned i = 0; i < NumAddlNames; i++)
432 for (unsigned j = 0; j < llvm::array_lengthof(AddlNames[i].Names); j++) {
433 if (!AddlNames[i].Names[j])
434 break;
435 // Make sure the register that the additional name is for is within
436 // the bounds of the register names from above.
437 if (AddlNames[i].Names[j] == Name && AddlNames[i].RegNum < NumNames)
438 return Name;
439 }
440
Anders Carlssonf511f642007-11-27 04:11:28 +0000441 // Now check aliases.
Chris Lattnerc3a669b2008-03-08 08:24:01 +0000442 const GCCRegAlias *Aliases;
Anders Carlssonf511f642007-11-27 04:11:28 +0000443 unsigned NumAliases;
Mike Stump11289f42009-09-09 15:08:12 +0000444
Chris Lattnerc3a669b2008-03-08 08:24:01 +0000445 getGCCRegAliases(Aliases, NumAliases);
Anders Carlssonf511f642007-11-27 04:11:28 +0000446 for (unsigned i = 0; i < NumAliases; i++) {
447 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
448 if (!Aliases[i].Aliases[j])
449 break;
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000450 if (Aliases[i].Aliases[j] == Name)
Anders Carlssonf511f642007-11-27 04:11:28 +0000451 return Aliases[i].Register;
452 }
453 }
Mike Stump11289f42009-09-09 15:08:12 +0000454
Anders Carlssonf511f642007-11-27 04:11:28 +0000455 return Name;
456}
457
Chris Lattnerc3f4c7b2009-04-26 17:19:08 +0000458bool TargetInfo::validateOutputConstraint(ConstraintInfo &Info) const {
459 const char *Name = Info.getConstraintStr().c_str();
Anders Carlssonf511f642007-11-27 04:11:28 +0000460 // An output constraint must start with '=' or '+'
461 if (*Name != '=' && *Name != '+')
462 return false;
463
464 if (*Name == '+')
Chris Lattnerd9725f72009-04-26 07:16:29 +0000465 Info.setIsReadWrite();
Anders Carlssonf511f642007-11-27 04:11:28 +0000466
467 Name++;
468 while (*Name) {
469 switch (*Name) {
470 default:
Chris Lattnerd9725f72009-04-26 07:16:29 +0000471 if (!validateAsmConstraint(Name, Info)) {
Ted Kremenekb44485b2008-04-24 16:36:38 +0000472 // FIXME: We temporarily return false
Anders Carlssonf511f642007-11-27 04:11:28 +0000473 // so we can add more constraints as we hit it.
474 // Eventually, an unknown constraint should just be treated as 'g'.
Ted Kremenekb44485b2008-04-24 16:36:38 +0000475 return false;
Anders Carlssonf511f642007-11-27 04:11:28 +0000476 }
David Majnemera0040df2015-01-10 10:43:19 +0000477 break;
Anders Carlssonf511f642007-11-27 04:11:28 +0000478 case '&': // early clobber.
David Majnemera0040df2015-01-10 10:43:19 +0000479 Info.setEarlyClobber();
Anders Carlssonf511f642007-11-27 04:11:28 +0000480 break;
Chris Lattnerf3154712009-10-13 04:32:07 +0000481 case '%': // commutative.
482 // FIXME: Check that there is a another register after this one.
483 break;
Anders Carlssonf511f642007-11-27 04:11:28 +0000484 case 'r': // general register.
Chris Lattnerd9725f72009-04-26 07:16:29 +0000485 Info.setAllowsRegister();
Anders Carlssonf511f642007-11-27 04:11:28 +0000486 break;
487 case 'm': // memory operand.
Dale Johannesen8499f472010-09-07 18:40:41 +0000488 case 'o': // offsetable memory operand.
489 case 'V': // non-offsetable memory operand.
490 case '<': // autodecrement memory operand.
491 case '>': // autoincrement memory operand.
Chris Lattnerd9725f72009-04-26 07:16:29 +0000492 Info.setAllowsMemory();
Anders Carlssonf511f642007-11-27 04:11:28 +0000493 break;
494 case 'g': // general register, memory operand or immediate integer.
Anders Carlssone70cde12009-01-18 02:12:04 +0000495 case 'X': // any operand.
Chris Lattnerd9725f72009-04-26 07:16:29 +0000496 Info.setAllowsRegister();
497 Info.setAllowsMemory();
Anders Carlssonf511f642007-11-27 04:11:28 +0000498 break;
John Thompsona5c7d702010-08-10 19:20:14 +0000499 case ',': // multiple alternative constraint. Pass it.
John Thompson46667af2010-08-11 00:58:20 +0000500 // Handle additional optional '=' or '+' modifiers.
John Thompson12240612010-09-18 01:15:13 +0000501 if (Name[1] == '=' || Name[1] == '+')
John Thompson46667af2010-08-11 00:58:20 +0000502 Name++;
John Thompsona5c7d702010-08-10 19:20:14 +0000503 break;
David Majnemer50cb0552015-01-11 08:52:38 +0000504 case '#': // Ignore as constraint.
505 while (Name[1] && Name[1] != ',')
506 Name++;
David Majnemerc71a5662015-01-11 09:39:03 +0000507 break;
John Thompsona5c7d702010-08-10 19:20:14 +0000508 case '?': // Disparage slightly code.
Dale Johannesen8499f472010-09-07 18:40:41 +0000509 case '!': // Disparage severely.
Ulrich Weigand7bcc7ec2012-10-29 12:20:54 +0000510 case '*': // Ignore for choosing register preferences.
John Thompsona5c7d702010-08-10 19:20:14 +0000511 break; // Pass them.
Anders Carlssonf511f642007-11-27 04:11:28 +0000512 }
Mike Stump11289f42009-09-09 15:08:12 +0000513
Anders Carlssonf511f642007-11-27 04:11:28 +0000514 Name++;
515 }
Mike Stump11289f42009-09-09 15:08:12 +0000516
David Majnemera0040df2015-01-10 10:43:19 +0000517 // Early clobber with a read-write constraint which doesn't permit registers
518 // is invalid.
519 if (Info.earlyClobber() && Info.isReadWrite() && !Info.allowsRegister())
520 return false;
521
Benjamin Kramer1e4a8862013-04-18 22:49:48 +0000522 // If a constraint allows neither memory nor register operands it contains
523 // only modifiers. Reject it.
Benjamin Kramer7ee3b9c2013-04-18 13:23:23 +0000524 return Info.allowsMemory() || Info.allowsRegister();
Anders Carlssonf511f642007-11-27 04:11:28 +0000525}
526
Anders Carlssona79203b2009-01-18 01:56:57 +0000527bool TargetInfo::resolveSymbolicName(const char *&Name,
Chris Lattnerc16d4762009-04-26 17:57:12 +0000528 ConstraintInfo *OutputConstraints,
529 unsigned NumOutputs,
Chris Lattnerd9725f72009-04-26 07:16:29 +0000530 unsigned &Index) const {
Anders Carlssona79203b2009-01-18 01:56:57 +0000531 assert(*Name == '[' && "Symbolic name did not start with '['");
Anders Carlssona79203b2009-01-18 01:56:57 +0000532 Name++;
533 const char *Start = Name;
534 while (*Name && *Name != ']')
535 Name++;
Mike Stump11289f42009-09-09 15:08:12 +0000536
Anders Carlssona79203b2009-01-18 01:56:57 +0000537 if (!*Name) {
538 // Missing ']'
539 return false;
540 }
Mike Stump11289f42009-09-09 15:08:12 +0000541
Anders Carlssona79203b2009-01-18 01:56:57 +0000542 std::string SymbolicName(Start, Name - Start);
Mike Stump11289f42009-09-09 15:08:12 +0000543
Chris Lattnerc16d4762009-04-26 17:57:12 +0000544 for (Index = 0; Index != NumOutputs; ++Index)
545 if (SymbolicName == OutputConstraints[Index].getName())
Anders Carlssona79203b2009-01-18 01:56:57 +0000546 return true;
Anders Carlssona79203b2009-01-18 01:56:57 +0000547
548 return false;
549}
550
Chris Lattnerc16d4762009-04-26 17:57:12 +0000551bool TargetInfo::validateInputConstraint(ConstraintInfo *OutputConstraints,
552 unsigned NumOutputs,
Chris Lattnerd9725f72009-04-26 07:16:29 +0000553 ConstraintInfo &Info) const {
Chris Lattnerc3f4c7b2009-04-26 17:19:08 +0000554 const char *Name = Info.ConstraintStr.c_str();
555
Duncan P. N. Exon Smithd68c7aa2013-12-16 03:20:06 +0000556 if (!*Name)
557 return false;
558
Anders Carlssonf511f642007-11-27 04:11:28 +0000559 while (*Name) {
560 switch (*Name) {
561 default:
562 // Check if we have a matching constraint
563 if (*Name >= '0' && *Name <= '9') {
David Majnemerb6b56432015-01-11 10:22:41 +0000564 const char *DigitStart = Name;
565 while (Name[1] >= '0' && Name[1] <= '9')
566 Name++;
567 const char *DigitEnd = Name;
568 unsigned i;
569 if (StringRef(DigitStart, DigitEnd - DigitStart + 1)
570 .getAsInteger(10, i))
571 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000572
Anders Carlssonf511f642007-11-27 04:11:28 +0000573 // Check if matching constraint is out of bounds.
David Majnemerb6b56432015-01-11 10:22:41 +0000574 if (i >= NumOutputs) return false;
Mike Stump11289f42009-09-09 15:08:12 +0000575
Anders Carlssonda1f5fc2010-11-03 02:22:29 +0000576 // A number must refer to an output only operand.
577 if (OutputConstraints[i].isReadWrite())
578 return false;
579
Anders Carlsson2d5f8b42010-11-03 02:54:51 +0000580 // If the constraint is already tied, it must be tied to the
581 // same operand referenced to by the number.
582 if (Info.hasTiedOperand() && Info.getTiedOperand() != i)
583 return false;
584
Mike Stump11289f42009-09-09 15:08:12 +0000585 // The constraint should have the same info as the respective
Anders Carlsson570c3572009-01-27 20:38:24 +0000586 // output constraint.
Chris Lattner4c92b512009-04-26 18:05:25 +0000587 Info.setTiedOperand(i, OutputConstraints[i]);
Chris Lattnerd9725f72009-04-26 07:16:29 +0000588 } else if (!validateAsmConstraint(Name, Info)) {
Daniel Dunbar81128e02008-08-25 09:46:27 +0000589 // FIXME: This error return is in place temporarily so we can
590 // add more constraints as we hit it. Eventually, an unknown
591 // constraint should just be treated as 'g'.
592 return false;
Anders Carlssona79203b2009-01-18 01:56:57 +0000593 }
594 break;
595 case '[': {
596 unsigned Index = 0;
Chris Lattnerc16d4762009-04-26 17:57:12 +0000597 if (!resolveSymbolicName(Name, OutputConstraints, NumOutputs, Index))
Anders Carlssona79203b2009-01-18 01:56:57 +0000598 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000599
Anders Carlsson2d5f8b42010-11-03 02:54:51 +0000600 // If the constraint is already tied, it must be tied to the
601 // same operand referenced to by the number.
602 if (Info.hasTiedOperand() && Info.getTiedOperand() != Index)
603 return false;
604
David Majnemer55164f92015-01-11 09:57:13 +0000605 // A number must refer to an output only operand.
606 if (OutputConstraints[Index].isReadWrite())
607 return false;
608
Eli Friedman854f1102010-08-11 23:03:37 +0000609 Info.setTiedOperand(Index, OutputConstraints[Index]);
Anders Carlssona79203b2009-01-18 01:56:57 +0000610 break;
Mike Stump11289f42009-09-09 15:08:12 +0000611 }
Anders Carlsson050f4942007-12-08 19:32:57 +0000612 case '%': // commutative
613 // FIXME: Fail if % is used with the last operand.
614 break;
Anders Carlssonf511f642007-11-27 04:11:28 +0000615 case 'i': // immediate integer.
Anders Carlssona3a96af2008-03-09 06:02:02 +0000616 case 'n': // immediate integer with a known value.
Anders Carlssonf511f642007-11-27 04:11:28 +0000617 break;
Chris Lattnerdbcc5ca2009-05-06 04:33:31 +0000618 case 'I': // Various constant constraints with target-specific meanings.
619 case 'J':
620 case 'K':
621 case 'L':
622 case 'M':
623 case 'N':
624 case 'O':
625 case 'P':
Saleem Abdulrasoola2823572015-01-06 04:26:34 +0000626 if (!validateAsmConstraint(Name, Info))
627 return false;
Chris Lattnerdbcc5ca2009-05-06 04:33:31 +0000628 break;
Anders Carlssonf511f642007-11-27 04:11:28 +0000629 case 'r': // general register.
Chris Lattnerd9725f72009-04-26 07:16:29 +0000630 Info.setAllowsRegister();
Anders Carlssonf511f642007-11-27 04:11:28 +0000631 break;
632 case 'm': // memory operand.
Dale Johannesen8499f472010-09-07 18:40:41 +0000633 case 'o': // offsettable memory operand.
634 case 'V': // non-offsettable memory operand.
635 case '<': // autodecrement memory operand.
636 case '>': // autoincrement memory operand.
Chris Lattnerd9725f72009-04-26 07:16:29 +0000637 Info.setAllowsMemory();
Anders Carlssonf511f642007-11-27 04:11:28 +0000638 break;
639 case 'g': // general register, memory operand or immediate integer.
Anders Carlssone70cde12009-01-18 02:12:04 +0000640 case 'X': // any operand.
Chris Lattnerd9725f72009-04-26 07:16:29 +0000641 Info.setAllowsRegister();
642 Info.setAllowsMemory();
Anders Carlssonf511f642007-11-27 04:11:28 +0000643 break;
John Thompsonc467aa22010-09-21 22:04:54 +0000644 case 'E': // immediate floating point.
645 case 'F': // immediate floating point.
646 case 'p': // address operand.
647 break;
John Thompsona5c7d702010-08-10 19:20:14 +0000648 case ',': // multiple alternative constraint. Ignore comma.
649 break;
David Majnemerc71a5662015-01-11 09:39:03 +0000650 case '#': // Ignore as constraint.
651 while (Name[1] && Name[1] != ',')
652 Name++;
David Majnemerc71a5662015-01-11 09:39:03 +0000653 break;
John Thompsona5c7d702010-08-10 19:20:14 +0000654 case '?': // Disparage slightly code.
Chris Lattner57540c52011-04-15 05:22:18 +0000655 case '!': // Disparage severely.
Ulrich Weigand7bcc7ec2012-10-29 12:20:54 +0000656 case '*': // Ignore for choosing register preferences.
John Thompsona5c7d702010-08-10 19:20:14 +0000657 break; // Pass them.
Anders Carlssonf511f642007-11-27 04:11:28 +0000658 }
Mike Stump11289f42009-09-09 15:08:12 +0000659
Anders Carlssonf511f642007-11-27 04:11:28 +0000660 Name++;
661 }
Mike Stump11289f42009-09-09 15:08:12 +0000662
Anders Carlssonf511f642007-11-27 04:11:28 +0000663 return true;
664}