blob: 856ad50d3782a10dbaaac1d2bf6ea77f9789cad0 [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;
Alexey Bataev00396512015-07-02 03:40:19 +000053 SimdDefaultAlign = 0;
Anders Carlsson2a79a902008-10-31 16:05:19 +000054 SizeType = UnsignedLong;
Eli Friedmand50881c2008-11-02 02:43:55 +000055 PtrDiffType = SignedLong;
Sanjiv Guptad7959242008-10-31 09:52:39 +000056 IntMaxType = SignedLongLong;
Chris Lattner7e4c81c2009-02-13 22:28:55 +000057 IntPtrType = SignedLong;
Eli Friedmand50881c2008-11-02 02:43:55 +000058 WCharType = SignedInt;
Chris Lattner67204922009-10-21 04:59:34 +000059 WIntType = SignedInt;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +000060 Char16Type = UnsignedShort;
61 Char32Type = UnsignedInt;
Eli Friedman2857ccb2009-07-01 03:36:11 +000062 Int64Type = SignedLongLong;
Edward O'Callaghan847f2a12009-11-21 00:49:54 +000063 SigAtomicType = SignedInt;
Eli Friedman4e91899e2012-11-27 02:58:24 +000064 ProcessIDType = SignedInt;
Fariborz Jahanian29898f42012-04-16 21:03:30 +000065 UseSignedCharForObjCBool = true;
Daniel Dunbar1da65112010-04-15 15:06:18 +000066 UseBitFieldTypeAlignment = true;
Chad Rosier18903ee2011-08-04 01:21:14 +000067 UseZeroLengthBitfieldAlignment = false;
68 ZeroLengthBitfieldBoundary = 0;
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +000069 HalfFormat = &llvm::APFloat::IEEEhalf;
Chris Lattner1df27862008-03-08 08:59:43 +000070 FloatFormat = &llvm::APFloat::IEEEsingle;
71 DoubleFormat = &llvm::APFloat::IEEEdouble;
72 LongDoubleFormat = &llvm::APFloat::IEEEdouble;
Craig Topperf1186c52014-05-08 06:41:40 +000073 DescriptionString = nullptr;
Chris Lattnerf37bafc2008-10-05 19:22:37 +000074 UserLabelPrefix = "_";
Roman Divacky178e01602011-02-10 16:52:03 +000075 MCountName = "mcount";
Abramo Bagnara41bfb662011-09-08 14:20:25 +000076 RegParmMax = 0;
77 SSERegParmMax = 0;
Daniel Dunbarbd606522010-05-27 00:35:16 +000078 HasAlignMac68kSupport = false;
Daniel Dunbar6f2e8392010-07-14 23:39:36 +000079
80 // Default to no types using fpret.
81 RealTypeUsesObjCFPRet = 0;
John McCall86353412010-08-21 22:46:04 +000082
Anders Carlsson2f1a6c32011-10-31 16:27:11 +000083 // Default to not using fp2ret for __Complex long double
84 ComplexLongDoubleUsesFP2Ret = false;
85
Hans Wennborgc9bd88e2014-01-14 19:35:09 +000086 // Set the C++ ABI based on the triple.
Saleem Abdulrasool377066a2014-03-27 22:50:18 +000087 TheCXXABI.set(Triple.isKnownWindowsMSVCEnvironment()
Hans Wennborgc9bd88e2014-01-14 19:35:09 +000088 ? TargetCXXABI::Microsoft
89 : TargetCXXABI::GenericItanium);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +000090
91 // Default to an empty address space map.
92 AddrSpaceMap = &DefaultAddrSpaceMap;
David Tweed31d09b02013-09-13 12:04:22 +000093 UseAddrSpaceMapMangling = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +000094
95 // Default to an unknown platform name.
96 PlatformName = "unknown";
97 PlatformMinVersion = VersionTuple();
Chris Lattner1df27862008-03-08 08:59:43 +000098}
99
Chris Lattnerc3a669b2008-03-08 08:24:01 +0000100// Out of line virtual dtor for TargetInfo.
101TargetInfo::~TargetInfo() {}
Chris Lattner2194ddc2006-10-14 18:32:26 +0000102
Chris Lattnera91c30f2009-02-06 05:04:11 +0000103/// getTypeName - Return the user string for the specified integer type enum.
104/// For example, SignedShort -> "short".
105const char *TargetInfo::getTypeName(IntType T) {
106 switch (T) {
David Blaikie83d382b2011-09-23 05:06:16 +0000107 default: llvm_unreachable("not an integer!");
Joerg Sonnenberger3d9478c2014-07-28 21:06:22 +0000108 case SignedChar: return "signed char";
Stepan Dyatkovskiy5a637922013-09-05 11:23:21 +0000109 case UnsignedChar: return "unsigned char";
Chris Lattnera91c30f2009-02-06 05:04:11 +0000110 case SignedShort: return "short";
111 case UnsignedShort: return "unsigned short";
112 case SignedInt: return "int";
113 case UnsignedInt: return "unsigned int";
114 case SignedLong: return "long int";
115 case UnsignedLong: return "long unsigned int";
116 case SignedLongLong: return "long long int";
117 case UnsignedLongLong: return "long long unsigned int";
118 }
119}
120
Chris Lattner72cdcac2009-10-21 06:24:21 +0000121/// getTypeConstantSuffix - Return the constant suffix for the specified
122/// integer type enum. For example, SignedLong -> "L".
Joerg Sonnenberger587deea2014-07-17 20:12:32 +0000123const char *TargetInfo::getTypeConstantSuffix(IntType T) const {
Chris Lattner72cdcac2009-10-21 06:24:21 +0000124 switch (T) {
David Blaikie83d382b2011-09-23 05:06:16 +0000125 default: llvm_unreachable("not an integer!");
Stepan Dyatkovskiy5a637922013-09-05 11:23:21 +0000126 case SignedChar:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000127 case SignedShort:
128 case SignedInt: return "";
129 case SignedLong: return "L";
130 case SignedLongLong: return "LL";
Stepan Dyatkovskiy5a637922013-09-05 11:23:21 +0000131 case UnsignedChar:
Joerg Sonnenberger587deea2014-07-17 20:12:32 +0000132 if (getCharWidth() < getIntWidth())
133 return "";
Chris Lattner72cdcac2009-10-21 06:24:21 +0000134 case UnsignedShort:
Joerg Sonnenberger587deea2014-07-17 20:12:32 +0000135 if (getShortWidth() < getIntWidth())
136 return "";
Chris Lattner72cdcac2009-10-21 06:24:21 +0000137 case UnsignedInt: return "U";
138 case UnsignedLong: return "UL";
139 case UnsignedLongLong: return "ULL";
140 }
141}
142
Joerg Sonnenbergerbe324f92014-07-15 11:30:00 +0000143/// getTypeFormatModifier - Return the printf format modifier for the
144/// specified integer type enum. For example, SignedLong -> "l".
145
146const char *TargetInfo::getTypeFormatModifier(IntType T) {
147 switch (T) {
148 default: llvm_unreachable("not an integer!");
149 case SignedChar:
150 case UnsignedChar: return "hh";
151 case SignedShort:
152 case UnsignedShort: return "h";
153 case SignedInt:
154 case UnsignedInt: return "";
155 case SignedLong:
156 case UnsignedLong: return "l";
157 case SignedLongLong:
158 case UnsignedLongLong: return "ll";
159 }
160}
161
Michael J. Spencerfeb799c2010-10-18 07:10:59 +0000162/// getTypeWidth - Return the width (in bits) of the specified integer type
Chris Lattner72cdcac2009-10-21 06:24:21 +0000163/// enum. For example, SignedInt -> getIntWidth().
164unsigned TargetInfo::getTypeWidth(IntType T) const {
165 switch (T) {
David Blaikie83d382b2011-09-23 05:06:16 +0000166 default: llvm_unreachable("not an integer!");
Stepan Dyatkovskiy5a637922013-09-05 11:23:21 +0000167 case SignedChar:
168 case UnsignedChar: return getCharWidth();
Chris Lattnere4a8c642009-11-05 21:21:32 +0000169 case SignedShort:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000170 case UnsignedShort: return getShortWidth();
Chris Lattnere4a8c642009-11-05 21:21:32 +0000171 case SignedInt:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000172 case UnsignedInt: return getIntWidth();
Chris Lattnere4a8c642009-11-05 21:21:32 +0000173 case SignedLong:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000174 case UnsignedLong: return getLongWidth();
Chris Lattnere4a8c642009-11-05 21:21:32 +0000175 case SignedLongLong:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000176 case UnsignedLongLong: return getLongLongWidth();
177 };
178}
179
Stepan Dyatkovskiy5a637922013-09-05 11:23:21 +0000180TargetInfo::IntType TargetInfo::getIntTypeByWidth(
181 unsigned BitWidth, bool IsSigned) const {
182 if (getCharWidth() == BitWidth)
183 return IsSigned ? SignedChar : UnsignedChar;
184 if (getShortWidth() == BitWidth)
185 return IsSigned ? SignedShort : UnsignedShort;
186 if (getIntWidth() == BitWidth)
187 return IsSigned ? SignedInt : UnsignedInt;
188 if (getLongWidth() == BitWidth)
189 return IsSigned ? SignedLong : UnsignedLong;
190 if (getLongLongWidth() == BitWidth)
191 return IsSigned ? SignedLongLong : UnsignedLongLong;
192 return NoInt;
193}
194
JF Bastienab8d0a02014-06-25 01:31:33 +0000195TargetInfo::IntType TargetInfo::getLeastIntTypeByWidth(unsigned BitWidth,
196 bool IsSigned) const {
197 if (getCharWidth() >= BitWidth)
198 return IsSigned ? SignedChar : UnsignedChar;
199 if (getShortWidth() >= BitWidth)
200 return IsSigned ? SignedShort : UnsignedShort;
201 if (getIntWidth() >= BitWidth)
202 return IsSigned ? SignedInt : UnsignedInt;
203 if (getLongWidth() >= BitWidth)
204 return IsSigned ? SignedLong : UnsignedLong;
205 if (getLongLongWidth() >= BitWidth)
206 return IsSigned ? SignedLongLong : UnsignedLongLong;
207 return NoInt;
208}
209
Stepan Dyatkovskiy5a637922013-09-05 11:23:21 +0000210TargetInfo::RealType TargetInfo::getRealTypeByWidth(unsigned BitWidth) const {
211 if (getFloatWidth() == BitWidth)
212 return Float;
213 if (getDoubleWidth() == BitWidth)
214 return Double;
215
216 switch (BitWidth) {
217 case 96:
218 if (&getLongDoubleFormat() == &llvm::APFloat::x87DoubleExtended)
219 return LongDouble;
220 break;
221 case 128:
Stepan Dyatkovskiya4a59d72013-09-09 07:46:54 +0000222 if (&getLongDoubleFormat() == &llvm::APFloat::PPCDoubleDouble ||
223 &getLongDoubleFormat() == &llvm::APFloat::IEEEquad)
Stepan Dyatkovskiy5a637922013-09-05 11:23:21 +0000224 return LongDouble;
225 break;
226 }
227
228 return NoFloat;
229}
230
Michael J. Spencerfeb799c2010-10-18 07:10:59 +0000231/// getTypeAlign - Return the alignment (in bits) of the specified integer type
Chris Lattnere4a8c642009-11-05 21:21:32 +0000232/// enum. For example, SignedInt -> getIntAlign().
233unsigned TargetInfo::getTypeAlign(IntType T) const {
234 switch (T) {
David Blaikie83d382b2011-09-23 05:06:16 +0000235 default: llvm_unreachable("not an integer!");
Stepan Dyatkovskiy5a637922013-09-05 11:23:21 +0000236 case SignedChar:
237 case UnsignedChar: return getCharAlign();
Chris Lattnere4a8c642009-11-05 21:21:32 +0000238 case SignedShort:
239 case UnsignedShort: return getShortAlign();
240 case SignedInt:
241 case UnsignedInt: return getIntAlign();
242 case SignedLong:
243 case UnsignedLong: return getLongAlign();
244 case SignedLongLong:
245 case UnsignedLongLong: return getLongLongAlign();
246 };
247}
248
Chris Lattnerc0c04392009-10-25 22:49:18 +0000249/// isTypeSigned - Return whether an integer types is signed. Returns true if
Chris Lattner72cdcac2009-10-21 06:24:21 +0000250/// the type is signed; false otherwise.
Chris Lattnerad3467e2010-12-25 23:25:43 +0000251bool TargetInfo::isTypeSigned(IntType T) {
Chris Lattner72cdcac2009-10-21 06:24:21 +0000252 switch (T) {
David Blaikie83d382b2011-09-23 05:06:16 +0000253 default: llvm_unreachable("not an integer!");
Stepan Dyatkovskiy5a637922013-09-05 11:23:21 +0000254 case SignedChar:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000255 case SignedShort:
256 case SignedInt:
257 case SignedLong:
Michael J. Spencerfeb799c2010-10-18 07:10:59 +0000258 case SignedLongLong:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000259 return true;
Stepan Dyatkovskiy5a637922013-09-05 11:23:21 +0000260 case UnsignedChar:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000261 case UnsignedShort:
262 case UnsignedInt:
263 case UnsignedLong:
Michael J. Spencerfeb799c2010-10-18 07:10:59 +0000264 case UnsignedLongLong:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000265 return false;
266 };
267}
268
Alp Toker74437972014-07-06 05:14:24 +0000269/// adjust - Set forced language options.
John Thompsoned4e2952009-11-05 20:14:16 +0000270/// Apply changes to the target information with respect to certain
271/// language options which change the target configuration.
Alp Toker74437972014-07-06 05:14:24 +0000272void TargetInfo::adjust(const LangOptions &Opts) {
Daniel Dunbar9302f602010-04-15 15:06:22 +0000273 if (Opts.NoBitFieldTypeAlign)
274 UseBitFieldTypeAlignment = false;
275 if (Opts.ShortWChar)
John Thompsoned4e2952009-11-05 20:14:16 +0000276 WCharType = UnsignedShort;
David Tweed2da64382013-09-09 09:17:24 +0000277
278 if (Opts.OpenCL) {
279 // OpenCL C requires specific widths for types, irrespective of
280 // what these normally are for the target.
281 // We also define long long and long double here, although the
282 // OpenCL standard only mentions these as "reserved".
283 IntWidth = IntAlign = 32;
284 LongWidth = LongAlign = 64;
285 LongLongWidth = LongLongAlign = 128;
286 HalfWidth = HalfAlign = 16;
287 FloatWidth = FloatAlign = 32;
Pekka Jaaskelainen43c39d52013-12-18 18:15:03 +0000288
289 // Embedded 32-bit targets (OpenCL EP) might have double C type
290 // defined as float. Let's not override this as it might lead
291 // to generating illegal code that uses 64bit doubles.
292 if (DoubleWidth != FloatWidth) {
293 DoubleWidth = DoubleAlign = 64;
294 DoubleFormat = &llvm::APFloat::IEEEdouble;
295 }
David Tweed2da64382013-09-09 09:17:24 +0000296 LongDoubleWidth = LongDoubleAlign = 128;
297
298 assert(PointerWidth == 32 || PointerWidth == 64);
David Tweed6c2149b2013-09-10 08:00:34 +0000299 bool Is32BitArch = PointerWidth == 32;
300 SizeType = Is32BitArch ? UnsignedInt : UnsignedLong;
301 PtrDiffType = Is32BitArch ? SignedInt : SignedLong;
302 IntPtrType = Is32BitArch ? SignedInt : SignedLong;
David Tweed2da64382013-09-09 09:17:24 +0000303
304 IntMaxType = SignedLongLong;
David Tweed2da64382013-09-09 09:17:24 +0000305 Int64Type = SignedLong;
306
307 HalfFormat = &llvm::APFloat::IEEEhalf;
308 FloatFormat = &llvm::APFloat::IEEEsingle;
David Tweed2da64382013-09-09 09:17:24 +0000309 LongDoubleFormat = &llvm::APFloat::IEEEquad;
310 }
John Thompsoned4e2952009-11-05 20:14:16 +0000311}
Chris Lattner72cdcac2009-10-21 06:24:21 +0000312
Chris Lattnerec0a6d92007-09-22 18:29:59 +0000313//===----------------------------------------------------------------------===//
Chris Lattnerec0a6d92007-09-22 18:29:59 +0000314
Chris Lattner10a5b382007-01-29 05:24:35 +0000315
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000316static StringRef removeGCCRegisterPrefix(StringRef Name) {
Anders Carlsson3ed4aea2008-02-06 00:11:32 +0000317 if (Name[0] == '%' || Name[0] == '#')
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000318 Name = Name.substr(1);
Michael J. Spencerfeb799c2010-10-18 07:10:59 +0000319
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000320 return Name;
Anders Carlsson3ed4aea2008-02-06 00:11:32 +0000321}
322
Eric Christopherfd9a5f42011-06-28 18:20:53 +0000323/// isValidClobber - Returns whether the passed in string is
324/// a valid clobber in an inline asm statement. This is used by
325/// Sema.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000326bool TargetInfo::isValidClobber(StringRef Name) const {
Eric Christopherfd9a5f42011-06-28 18:20:53 +0000327 return (isValidGCCRegisterName(Name) ||
328 Name == "memory" || Name == "cc");
329}
330
Anders Carlsson5fa3f342007-11-24 23:38:12 +0000331/// isValidGCCRegisterName - Returns whether the passed in string
332/// is a valid register name according to GCC. This is used by Sema for
333/// inline asm statements.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000334bool TargetInfo::isValidGCCRegisterName(StringRef Name) const {
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000335 if (Name.empty())
336 return false;
Michael J. Spencerfeb799c2010-10-18 07:10:59 +0000337
Anders Carlsson290aa852007-11-25 00:25:21 +0000338 const char * const *Names;
339 unsigned NumNames;
Mike Stump11289f42009-09-09 15:08:12 +0000340
Anders Carlsson290aa852007-11-25 00:25:21 +0000341 // Get rid of any register prefix.
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000342 Name = removeGCCRegisterPrefix(Name);
Olivier Goffartfc8f8932014-08-17 13:19:48 +0000343 if (Name.empty())
344 return false;
Anders Carlsson3ed4aea2008-02-06 00:11:32 +0000345
Chris Lattnerc3a669b2008-03-08 08:24:01 +0000346 getGCCRegNames(Names, NumNames);
Mike Stump11289f42009-09-09 15:08:12 +0000347
Anders Carlsson290aa852007-11-25 00:25:21 +0000348 // If we have a number it maps to an entry in the register name array.
Jordan Rosea7d03842013-02-08 22:30:41 +0000349 if (isDigit(Name[0])) {
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000350 int n;
351 if (!Name.getAsInteger(0, n))
Anders Carlsson290aa852007-11-25 00:25:21 +0000352 return n >= 0 && (unsigned)n < NumNames;
353 }
354
355 // Check register names.
356 for (unsigned i = 0; i < NumNames; i++) {
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000357 if (Name == Names[i])
Anders Carlsson290aa852007-11-25 00:25:21 +0000358 return true;
359 }
Mike Stump11289f42009-09-09 15:08:12 +0000360
Eric Christophercdd36352011-06-21 00:05:20 +0000361 // Check any additional names that we have.
362 const AddlRegName *AddlNames;
363 unsigned NumAddlNames;
364 getGCCAddlRegNames(AddlNames, NumAddlNames);
365 for (unsigned i = 0; i < NumAddlNames; i++)
366 for (unsigned j = 0; j < llvm::array_lengthof(AddlNames[i].Names); j++) {
367 if (!AddlNames[i].Names[j])
368 break;
369 // Make sure the register that the additional name is for is within
370 // the bounds of the register names from above.
371 if (AddlNames[i].Names[j] == Name && AddlNames[i].RegNum < NumNames)
372 return true;
373 }
374
Anders Carlsson290aa852007-11-25 00:25:21 +0000375 // Now check aliases.
Chris Lattnerc3a669b2008-03-08 08:24:01 +0000376 const GCCRegAlias *Aliases;
Anders Carlsson290aa852007-11-25 00:25:21 +0000377 unsigned NumAliases;
Mike Stump11289f42009-09-09 15:08:12 +0000378
Chris Lattnerc3a669b2008-03-08 08:24:01 +0000379 getGCCRegAliases(Aliases, NumAliases);
Anders Carlsson290aa852007-11-25 00:25:21 +0000380 for (unsigned i = 0; i < NumAliases; i++) {
381 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
382 if (!Aliases[i].Aliases[j])
383 break;
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000384 if (Aliases[i].Aliases[j] == Name)
Anders Carlsson290aa852007-11-25 00:25:21 +0000385 return true;
386 }
387 }
Mike Stump11289f42009-09-09 15:08:12 +0000388
Anders Carlsson5fa3f342007-11-24 23:38:12 +0000389 return false;
390}
Anders Carlssonf511f642007-11-27 04:11:28 +0000391
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000392StringRef
393TargetInfo::getNormalizedGCCRegisterName(StringRef Name) const {
Anders Carlssonf511f642007-11-27 04:11:28 +0000394 assert(isValidGCCRegisterName(Name) && "Invalid register passed in");
Mike Stump11289f42009-09-09 15:08:12 +0000395
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000396 // Get rid of any register prefix.
397 Name = removeGCCRegisterPrefix(Name);
Mike Stump11289f42009-09-09 15:08:12 +0000398
Anders Carlssonf511f642007-11-27 04:11:28 +0000399 const char * const *Names;
400 unsigned NumNames;
401
Chris Lattnerc3a669b2008-03-08 08:24:01 +0000402 getGCCRegNames(Names, NumNames);
Anders Carlssonf511f642007-11-27 04:11:28 +0000403
404 // First, check if we have a number.
Jordan Rosea7d03842013-02-08 22:30:41 +0000405 if (isDigit(Name[0])) {
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000406 int n;
407 if (!Name.getAsInteger(0, n)) {
Mike Stump11289f42009-09-09 15:08:12 +0000408 assert(n >= 0 && (unsigned)n < NumNames &&
Anders Carlssonf511f642007-11-27 04:11:28 +0000409 "Out of bounds register number!");
410 return Names[n];
411 }
412 }
Mike Stump11289f42009-09-09 15:08:12 +0000413
Eric Christophercdd36352011-06-21 00:05:20 +0000414 // Check any additional names that we have.
415 const AddlRegName *AddlNames;
416 unsigned NumAddlNames;
417 getGCCAddlRegNames(AddlNames, NumAddlNames);
418 for (unsigned i = 0; i < NumAddlNames; i++)
419 for (unsigned j = 0; j < llvm::array_lengthof(AddlNames[i].Names); j++) {
420 if (!AddlNames[i].Names[j])
421 break;
422 // Make sure the register that the additional name is for is within
423 // the bounds of the register names from above.
424 if (AddlNames[i].Names[j] == Name && AddlNames[i].RegNum < NumNames)
425 return Name;
426 }
427
Anders Carlssonf511f642007-11-27 04:11:28 +0000428 // Now check aliases.
Chris Lattnerc3a669b2008-03-08 08:24:01 +0000429 const GCCRegAlias *Aliases;
Anders Carlssonf511f642007-11-27 04:11:28 +0000430 unsigned NumAliases;
Mike Stump11289f42009-09-09 15:08:12 +0000431
Chris Lattnerc3a669b2008-03-08 08:24:01 +0000432 getGCCRegAliases(Aliases, NumAliases);
Anders Carlssonf511f642007-11-27 04:11:28 +0000433 for (unsigned i = 0; i < NumAliases; i++) {
434 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
435 if (!Aliases[i].Aliases[j])
436 break;
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000437 if (Aliases[i].Aliases[j] == Name)
Anders Carlssonf511f642007-11-27 04:11:28 +0000438 return Aliases[i].Register;
439 }
440 }
Mike Stump11289f42009-09-09 15:08:12 +0000441
Anders Carlssonf511f642007-11-27 04:11:28 +0000442 return Name;
443}
444
Chris Lattnerc3f4c7b2009-04-26 17:19:08 +0000445bool TargetInfo::validateOutputConstraint(ConstraintInfo &Info) const {
446 const char *Name = Info.getConstraintStr().c_str();
Anders Carlssonf511f642007-11-27 04:11:28 +0000447 // An output constraint must start with '=' or '+'
448 if (*Name != '=' && *Name != '+')
449 return false;
450
451 if (*Name == '+')
Chris Lattnerd9725f72009-04-26 07:16:29 +0000452 Info.setIsReadWrite();
Anders Carlssonf511f642007-11-27 04:11:28 +0000453
454 Name++;
455 while (*Name) {
456 switch (*Name) {
457 default:
Chris Lattnerd9725f72009-04-26 07:16:29 +0000458 if (!validateAsmConstraint(Name, Info)) {
Ted Kremenekb44485b2008-04-24 16:36:38 +0000459 // FIXME: We temporarily return false
Anders Carlssonf511f642007-11-27 04:11:28 +0000460 // so we can add more constraints as we hit it.
461 // Eventually, an unknown constraint should just be treated as 'g'.
Ted Kremenekb44485b2008-04-24 16:36:38 +0000462 return false;
Anders Carlssonf511f642007-11-27 04:11:28 +0000463 }
David Majnemera0040df2015-01-10 10:43:19 +0000464 break;
Anders Carlssonf511f642007-11-27 04:11:28 +0000465 case '&': // early clobber.
David Majnemera0040df2015-01-10 10:43:19 +0000466 Info.setEarlyClobber();
Anders Carlssonf511f642007-11-27 04:11:28 +0000467 break;
Chris Lattnerf3154712009-10-13 04:32:07 +0000468 case '%': // commutative.
469 // FIXME: Check that there is a another register after this one.
470 break;
Anders Carlssonf511f642007-11-27 04:11:28 +0000471 case 'r': // general register.
Chris Lattnerd9725f72009-04-26 07:16:29 +0000472 Info.setAllowsRegister();
Anders Carlssonf511f642007-11-27 04:11:28 +0000473 break;
474 case 'm': // memory operand.
Dale Johannesen8499f472010-09-07 18:40:41 +0000475 case 'o': // offsetable memory operand.
476 case 'V': // non-offsetable memory operand.
477 case '<': // autodecrement memory operand.
478 case '>': // autoincrement memory operand.
Chris Lattnerd9725f72009-04-26 07:16:29 +0000479 Info.setAllowsMemory();
Anders Carlssonf511f642007-11-27 04:11:28 +0000480 break;
481 case 'g': // general register, memory operand or immediate integer.
Anders Carlssone70cde12009-01-18 02:12:04 +0000482 case 'X': // any operand.
Chris Lattnerd9725f72009-04-26 07:16:29 +0000483 Info.setAllowsRegister();
484 Info.setAllowsMemory();
Anders Carlssonf511f642007-11-27 04:11:28 +0000485 break;
John Thompsona5c7d702010-08-10 19:20:14 +0000486 case ',': // multiple alternative constraint. Pass it.
John Thompson46667af2010-08-11 00:58:20 +0000487 // Handle additional optional '=' or '+' modifiers.
John Thompson12240612010-09-18 01:15:13 +0000488 if (Name[1] == '=' || Name[1] == '+')
John Thompson46667af2010-08-11 00:58:20 +0000489 Name++;
John Thompsona5c7d702010-08-10 19:20:14 +0000490 break;
David Majnemer50cb0552015-01-11 08:52:38 +0000491 case '#': // Ignore as constraint.
492 while (Name[1] && Name[1] != ',')
493 Name++;
David Majnemerc71a5662015-01-11 09:39:03 +0000494 break;
John Thompsona5c7d702010-08-10 19:20:14 +0000495 case '?': // Disparage slightly code.
Dale Johannesen8499f472010-09-07 18:40:41 +0000496 case '!': // Disparage severely.
Ulrich Weigand7bcc7ec2012-10-29 12:20:54 +0000497 case '*': // Ignore for choosing register preferences.
John Thompsona5c7d702010-08-10 19:20:14 +0000498 break; // Pass them.
Anders Carlssonf511f642007-11-27 04:11:28 +0000499 }
Mike Stump11289f42009-09-09 15:08:12 +0000500
Anders Carlssonf511f642007-11-27 04:11:28 +0000501 Name++;
502 }
Mike Stump11289f42009-09-09 15:08:12 +0000503
David Majnemera0040df2015-01-10 10:43:19 +0000504 // Early clobber with a read-write constraint which doesn't permit registers
505 // is invalid.
506 if (Info.earlyClobber() && Info.isReadWrite() && !Info.allowsRegister())
507 return false;
508
Benjamin Kramer1e4a8862013-04-18 22:49:48 +0000509 // If a constraint allows neither memory nor register operands it contains
510 // only modifiers. Reject it.
Benjamin Kramer7ee3b9c2013-04-18 13:23:23 +0000511 return Info.allowsMemory() || Info.allowsRegister();
Anders Carlssonf511f642007-11-27 04:11:28 +0000512}
513
Anders Carlssona79203b2009-01-18 01:56:57 +0000514bool TargetInfo::resolveSymbolicName(const char *&Name,
Chris Lattnerc16d4762009-04-26 17:57:12 +0000515 ConstraintInfo *OutputConstraints,
516 unsigned NumOutputs,
Chris Lattnerd9725f72009-04-26 07:16:29 +0000517 unsigned &Index) const {
Anders Carlssona79203b2009-01-18 01:56:57 +0000518 assert(*Name == '[' && "Symbolic name did not start with '['");
Anders Carlssona79203b2009-01-18 01:56:57 +0000519 Name++;
520 const char *Start = Name;
521 while (*Name && *Name != ']')
522 Name++;
Mike Stump11289f42009-09-09 15:08:12 +0000523
Anders Carlssona79203b2009-01-18 01:56:57 +0000524 if (!*Name) {
525 // Missing ']'
526 return false;
527 }
Mike Stump11289f42009-09-09 15:08:12 +0000528
Anders Carlssona79203b2009-01-18 01:56:57 +0000529 std::string SymbolicName(Start, Name - Start);
Mike Stump11289f42009-09-09 15:08:12 +0000530
Chris Lattnerc16d4762009-04-26 17:57:12 +0000531 for (Index = 0; Index != NumOutputs; ++Index)
532 if (SymbolicName == OutputConstraints[Index].getName())
Anders Carlssona79203b2009-01-18 01:56:57 +0000533 return true;
Anders Carlssona79203b2009-01-18 01:56:57 +0000534
535 return false;
536}
537
Chris Lattnerc16d4762009-04-26 17:57:12 +0000538bool TargetInfo::validateInputConstraint(ConstraintInfo *OutputConstraints,
539 unsigned NumOutputs,
Chris Lattnerd9725f72009-04-26 07:16:29 +0000540 ConstraintInfo &Info) const {
Chris Lattnerc3f4c7b2009-04-26 17:19:08 +0000541 const char *Name = Info.ConstraintStr.c_str();
542
Duncan P. N. Exon Smithd68c7aa2013-12-16 03:20:06 +0000543 if (!*Name)
544 return false;
545
Anders Carlssonf511f642007-11-27 04:11:28 +0000546 while (*Name) {
547 switch (*Name) {
548 default:
549 // Check if we have a matching constraint
550 if (*Name >= '0' && *Name <= '9') {
David Majnemerb6b56432015-01-11 10:22:41 +0000551 const char *DigitStart = Name;
552 while (Name[1] >= '0' && Name[1] <= '9')
553 Name++;
554 const char *DigitEnd = Name;
555 unsigned i;
556 if (StringRef(DigitStart, DigitEnd - DigitStart + 1)
557 .getAsInteger(10, i))
558 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000559
Anders Carlssonf511f642007-11-27 04:11:28 +0000560 // Check if matching constraint is out of bounds.
David Majnemerb6b56432015-01-11 10:22:41 +0000561 if (i >= NumOutputs) return false;
Mike Stump11289f42009-09-09 15:08:12 +0000562
Anders Carlssonda1f5fc2010-11-03 02:22:29 +0000563 // A number must refer to an output only operand.
564 if (OutputConstraints[i].isReadWrite())
565 return false;
566
Anders Carlsson2d5f8b42010-11-03 02:54:51 +0000567 // If the constraint is already tied, it must be tied to the
568 // same operand referenced to by the number.
569 if (Info.hasTiedOperand() && Info.getTiedOperand() != i)
570 return false;
571
Mike Stump11289f42009-09-09 15:08:12 +0000572 // The constraint should have the same info as the respective
Anders Carlsson570c3572009-01-27 20:38:24 +0000573 // output constraint.
Chris Lattner4c92b512009-04-26 18:05:25 +0000574 Info.setTiedOperand(i, OutputConstraints[i]);
Chris Lattnerd9725f72009-04-26 07:16:29 +0000575 } else if (!validateAsmConstraint(Name, Info)) {
Daniel Dunbar81128e02008-08-25 09:46:27 +0000576 // FIXME: This error return is in place temporarily so we can
577 // add more constraints as we hit it. Eventually, an unknown
578 // constraint should just be treated as 'g'.
579 return false;
Anders Carlssona79203b2009-01-18 01:56:57 +0000580 }
581 break;
582 case '[': {
583 unsigned Index = 0;
Chris Lattnerc16d4762009-04-26 17:57:12 +0000584 if (!resolveSymbolicName(Name, OutputConstraints, NumOutputs, Index))
Anders Carlssona79203b2009-01-18 01:56:57 +0000585 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000586
Anders Carlsson2d5f8b42010-11-03 02:54:51 +0000587 // If the constraint is already tied, it must be tied to the
588 // same operand referenced to by the number.
589 if (Info.hasTiedOperand() && Info.getTiedOperand() != Index)
590 return false;
591
David Majnemer55164f92015-01-11 09:57:13 +0000592 // A number must refer to an output only operand.
593 if (OutputConstraints[Index].isReadWrite())
594 return false;
595
Eli Friedman854f1102010-08-11 23:03:37 +0000596 Info.setTiedOperand(Index, OutputConstraints[Index]);
Anders Carlssona79203b2009-01-18 01:56:57 +0000597 break;
Mike Stump11289f42009-09-09 15:08:12 +0000598 }
Anders Carlsson050f4942007-12-08 19:32:57 +0000599 case '%': // commutative
600 // FIXME: Fail if % is used with the last operand.
601 break;
Anders Carlssonf511f642007-11-27 04:11:28 +0000602 case 'i': // immediate integer.
Anders Carlssona3a96af2008-03-09 06:02:02 +0000603 case 'n': // immediate integer with a known value.
Anders Carlssonf511f642007-11-27 04:11:28 +0000604 break;
Chris Lattnerdbcc5ca2009-05-06 04:33:31 +0000605 case 'I': // Various constant constraints with target-specific meanings.
606 case 'J':
607 case 'K':
608 case 'L':
609 case 'M':
610 case 'N':
611 case 'O':
612 case 'P':
Saleem Abdulrasoola2823572015-01-06 04:26:34 +0000613 if (!validateAsmConstraint(Name, Info))
614 return false;
Chris Lattnerdbcc5ca2009-05-06 04:33:31 +0000615 break;
Anders Carlssonf511f642007-11-27 04:11:28 +0000616 case 'r': // general register.
Chris Lattnerd9725f72009-04-26 07:16:29 +0000617 Info.setAllowsRegister();
Anders Carlssonf511f642007-11-27 04:11:28 +0000618 break;
619 case 'm': // memory operand.
Dale Johannesen8499f472010-09-07 18:40:41 +0000620 case 'o': // offsettable memory operand.
621 case 'V': // non-offsettable memory operand.
622 case '<': // autodecrement memory operand.
623 case '>': // autoincrement memory operand.
Chris Lattnerd9725f72009-04-26 07:16:29 +0000624 Info.setAllowsMemory();
Anders Carlssonf511f642007-11-27 04:11:28 +0000625 break;
626 case 'g': // general register, memory operand or immediate integer.
Anders Carlssone70cde12009-01-18 02:12:04 +0000627 case 'X': // any operand.
Chris Lattnerd9725f72009-04-26 07:16:29 +0000628 Info.setAllowsRegister();
629 Info.setAllowsMemory();
Anders Carlssonf511f642007-11-27 04:11:28 +0000630 break;
John Thompsonc467aa22010-09-21 22:04:54 +0000631 case 'E': // immediate floating point.
632 case 'F': // immediate floating point.
633 case 'p': // address operand.
634 break;
John Thompsona5c7d702010-08-10 19:20:14 +0000635 case ',': // multiple alternative constraint. Ignore comma.
636 break;
David Majnemerc71a5662015-01-11 09:39:03 +0000637 case '#': // Ignore as constraint.
638 while (Name[1] && Name[1] != ',')
639 Name++;
David Majnemerc71a5662015-01-11 09:39:03 +0000640 break;
John Thompsona5c7d702010-08-10 19:20:14 +0000641 case '?': // Disparage slightly code.
Chris Lattner57540c52011-04-15 05:22:18 +0000642 case '!': // Disparage severely.
Ulrich Weigand7bcc7ec2012-10-29 12:20:54 +0000643 case '*': // Ignore for choosing register preferences.
John Thompsona5c7d702010-08-10 19:20:14 +0000644 break; // Pass them.
Anders Carlssonf511f642007-11-27 04:11:28 +0000645 }
Mike Stump11289f42009-09-09 15:08:12 +0000646
Anders Carlssonf511f642007-11-27 04:11:28 +0000647 Name++;
648 }
Mike Stump11289f42009-09-09 15:08:12 +0000649
Anders Carlssonf511f642007-11-27 04:11:28 +0000650 return true;
651}
John McCall359b8852013-01-25 22:30:49 +0000652
653bool TargetCXXABI::tryParse(llvm::StringRef name) {
654 const Kind unknown = static_cast<Kind>(-1);
655 Kind kind = llvm::StringSwitch<Kind>(name)
656 .Case("arm", GenericARM)
657 .Case("ios", iOS)
658 .Case("itanium", GenericItanium)
659 .Case("microsoft", Microsoft)
Zoran Jovanovic5e7483a2015-02-23 20:20:49 +0000660 .Case("mips", GenericMIPS)
John McCall359b8852013-01-25 22:30:49 +0000661 .Default(unknown);
662 if (kind == unknown) return false;
663
664 set(kind);
665 return true;
666}