blob: 2ec22cf45fe2419d17b2029b53da7f8054e66ad6 [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;
Charles Davisc7d5c942015-09-17 20:55:33 +000080 HasBuiltinMSVaList = false;
Daniel Dunbar6f2e8392010-07-14 23:39:36 +000081
82 // Default to no types using fpret.
83 RealTypeUsesObjCFPRet = 0;
John McCall86353412010-08-21 22:46:04 +000084
Anders Carlsson2f1a6c32011-10-31 16:27:11 +000085 // Default to not using fp2ret for __Complex long double
86 ComplexLongDoubleUsesFP2Ret = false;
87
Hans Wennborgc9bd88e2014-01-14 19:35:09 +000088 // Set the C++ ABI based on the triple.
Saleem Abdulrasool377066a2014-03-27 22:50:18 +000089 TheCXXABI.set(Triple.isKnownWindowsMSVCEnvironment()
Hans Wennborgc9bd88e2014-01-14 19:35:09 +000090 ? TargetCXXABI::Microsoft
91 : TargetCXXABI::GenericItanium);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +000092
93 // Default to an empty address space map.
94 AddrSpaceMap = &DefaultAddrSpaceMap;
David Tweed31d09b02013-09-13 12:04:22 +000095 UseAddrSpaceMapMangling = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +000096
97 // Default to an unknown platform name.
98 PlatformName = "unknown";
99 PlatformMinVersion = VersionTuple();
Chris Lattner1df27862008-03-08 08:59:43 +0000100}
101
Chris Lattnerc3a669b2008-03-08 08:24:01 +0000102// Out of line virtual dtor for TargetInfo.
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +0000103TargetInfo::~TargetInfo() {}
Chris Lattner2194ddc2006-10-14 18:32:26 +0000104
Chris Lattnera91c30f2009-02-06 05:04:11 +0000105/// getTypeName - Return the user string for the specified integer type enum.
106/// For example, SignedShort -> "short".
107const char *TargetInfo::getTypeName(IntType T) {
108 switch (T) {
David Blaikie83d382b2011-09-23 05:06:16 +0000109 default: llvm_unreachable("not an integer!");
Joerg Sonnenberger3d9478c2014-07-28 21:06:22 +0000110 case SignedChar: return "signed char";
Stepan Dyatkovskiy5a637922013-09-05 11:23:21 +0000111 case UnsignedChar: return "unsigned char";
Chris Lattnera91c30f2009-02-06 05:04:11 +0000112 case SignedShort: return "short";
113 case UnsignedShort: return "unsigned short";
114 case SignedInt: return "int";
115 case UnsignedInt: return "unsigned int";
116 case SignedLong: return "long int";
117 case UnsignedLong: return "long unsigned int";
118 case SignedLongLong: return "long long int";
119 case UnsignedLongLong: return "long long unsigned int";
120 }
121}
122
Chris Lattner72cdcac2009-10-21 06:24:21 +0000123/// getTypeConstantSuffix - Return the constant suffix for the specified
124/// integer type enum. For example, SignedLong -> "L".
Joerg Sonnenberger587deea2014-07-17 20:12:32 +0000125const char *TargetInfo::getTypeConstantSuffix(IntType T) const {
Chris Lattner72cdcac2009-10-21 06:24:21 +0000126 switch (T) {
David Blaikie83d382b2011-09-23 05:06:16 +0000127 default: llvm_unreachable("not an integer!");
Stepan Dyatkovskiy5a637922013-09-05 11:23:21 +0000128 case SignedChar:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000129 case SignedShort:
130 case SignedInt: return "";
131 case SignedLong: return "L";
132 case SignedLongLong: return "LL";
Stepan Dyatkovskiy5a637922013-09-05 11:23:21 +0000133 case UnsignedChar:
Joerg Sonnenberger587deea2014-07-17 20:12:32 +0000134 if (getCharWidth() < getIntWidth())
135 return "";
Chris Lattner72cdcac2009-10-21 06:24:21 +0000136 case UnsignedShort:
Joerg Sonnenberger587deea2014-07-17 20:12:32 +0000137 if (getShortWidth() < getIntWidth())
138 return "";
Chris Lattner72cdcac2009-10-21 06:24:21 +0000139 case UnsignedInt: return "U";
140 case UnsignedLong: return "UL";
141 case UnsignedLongLong: return "ULL";
142 }
143}
144
Joerg Sonnenbergerbe324f92014-07-15 11:30:00 +0000145/// getTypeFormatModifier - Return the printf format modifier for the
146/// specified integer type enum. For example, SignedLong -> "l".
147
148const char *TargetInfo::getTypeFormatModifier(IntType T) {
149 switch (T) {
150 default: llvm_unreachable("not an integer!");
151 case SignedChar:
152 case UnsignedChar: return "hh";
153 case SignedShort:
154 case UnsignedShort: return "h";
155 case SignedInt:
156 case UnsignedInt: return "";
157 case SignedLong:
158 case UnsignedLong: return "l";
159 case SignedLongLong:
160 case UnsignedLongLong: return "ll";
161 }
162}
163
Michael J. Spencerfeb799c2010-10-18 07:10:59 +0000164/// getTypeWidth - Return the width (in bits) of the specified integer type
Chris Lattner72cdcac2009-10-21 06:24:21 +0000165/// enum. For example, SignedInt -> getIntWidth().
166unsigned TargetInfo::getTypeWidth(IntType T) const {
167 switch (T) {
David Blaikie83d382b2011-09-23 05:06:16 +0000168 default: llvm_unreachable("not an integer!");
Stepan Dyatkovskiy5a637922013-09-05 11:23:21 +0000169 case SignedChar:
170 case UnsignedChar: return getCharWidth();
Chris Lattnere4a8c642009-11-05 21:21:32 +0000171 case SignedShort:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000172 case UnsignedShort: return getShortWidth();
Chris Lattnere4a8c642009-11-05 21:21:32 +0000173 case SignedInt:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000174 case UnsignedInt: return getIntWidth();
Chris Lattnere4a8c642009-11-05 21:21:32 +0000175 case SignedLong:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000176 case UnsignedLong: return getLongWidth();
Chris Lattnere4a8c642009-11-05 21:21:32 +0000177 case SignedLongLong:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000178 case UnsignedLongLong: return getLongLongWidth();
179 };
180}
181
Stepan Dyatkovskiy5a637922013-09-05 11:23:21 +0000182TargetInfo::IntType TargetInfo::getIntTypeByWidth(
183 unsigned BitWidth, bool IsSigned) const {
184 if (getCharWidth() == BitWidth)
185 return IsSigned ? SignedChar : UnsignedChar;
186 if (getShortWidth() == BitWidth)
187 return IsSigned ? SignedShort : UnsignedShort;
188 if (getIntWidth() == BitWidth)
189 return IsSigned ? SignedInt : UnsignedInt;
190 if (getLongWidth() == BitWidth)
191 return IsSigned ? SignedLong : UnsignedLong;
192 if (getLongLongWidth() == BitWidth)
193 return IsSigned ? SignedLongLong : UnsignedLongLong;
194 return NoInt;
195}
196
JF Bastienab8d0a02014-06-25 01:31:33 +0000197TargetInfo::IntType TargetInfo::getLeastIntTypeByWidth(unsigned BitWidth,
198 bool IsSigned) const {
199 if (getCharWidth() >= BitWidth)
200 return IsSigned ? SignedChar : UnsignedChar;
201 if (getShortWidth() >= BitWidth)
202 return IsSigned ? SignedShort : UnsignedShort;
203 if (getIntWidth() >= BitWidth)
204 return IsSigned ? SignedInt : UnsignedInt;
205 if (getLongWidth() >= BitWidth)
206 return IsSigned ? SignedLong : UnsignedLong;
207 if (getLongLongWidth() >= BitWidth)
208 return IsSigned ? SignedLongLong : UnsignedLongLong;
209 return NoInt;
210}
211
Stepan Dyatkovskiy5a637922013-09-05 11:23:21 +0000212TargetInfo::RealType TargetInfo::getRealTypeByWidth(unsigned BitWidth) const {
213 if (getFloatWidth() == BitWidth)
214 return Float;
215 if (getDoubleWidth() == BitWidth)
216 return Double;
217
218 switch (BitWidth) {
219 case 96:
220 if (&getLongDoubleFormat() == &llvm::APFloat::x87DoubleExtended)
221 return LongDouble;
222 break;
223 case 128:
Stepan Dyatkovskiya4a59d72013-09-09 07:46:54 +0000224 if (&getLongDoubleFormat() == &llvm::APFloat::PPCDoubleDouble ||
225 &getLongDoubleFormat() == &llvm::APFloat::IEEEquad)
Stepan Dyatkovskiy5a637922013-09-05 11:23:21 +0000226 return LongDouble;
227 break;
228 }
229
230 return NoFloat;
231}
232
Michael J. Spencerfeb799c2010-10-18 07:10:59 +0000233/// getTypeAlign - Return the alignment (in bits) of the specified integer type
Chris Lattnere4a8c642009-11-05 21:21:32 +0000234/// enum. For example, SignedInt -> getIntAlign().
235unsigned TargetInfo::getTypeAlign(IntType T) const {
236 switch (T) {
David Blaikie83d382b2011-09-23 05:06:16 +0000237 default: llvm_unreachable("not an integer!");
Stepan Dyatkovskiy5a637922013-09-05 11:23:21 +0000238 case SignedChar:
239 case UnsignedChar: return getCharAlign();
Chris Lattnere4a8c642009-11-05 21:21:32 +0000240 case SignedShort:
241 case UnsignedShort: return getShortAlign();
242 case SignedInt:
243 case UnsignedInt: return getIntAlign();
244 case SignedLong:
245 case UnsignedLong: return getLongAlign();
246 case SignedLongLong:
247 case UnsignedLongLong: return getLongLongAlign();
248 };
249}
250
Chris Lattnerc0c04392009-10-25 22:49:18 +0000251/// isTypeSigned - Return whether an integer types is signed. Returns true if
Chris Lattner72cdcac2009-10-21 06:24:21 +0000252/// the type is signed; false otherwise.
Chris Lattnerad3467e2010-12-25 23:25:43 +0000253bool TargetInfo::isTypeSigned(IntType T) {
Chris Lattner72cdcac2009-10-21 06:24:21 +0000254 switch (T) {
David Blaikie83d382b2011-09-23 05:06:16 +0000255 default: llvm_unreachable("not an integer!");
Stepan Dyatkovskiy5a637922013-09-05 11:23:21 +0000256 case SignedChar:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000257 case SignedShort:
258 case SignedInt:
259 case SignedLong:
Michael J. Spencerfeb799c2010-10-18 07:10:59 +0000260 case SignedLongLong:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000261 return true;
Stepan Dyatkovskiy5a637922013-09-05 11:23:21 +0000262 case UnsignedChar:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000263 case UnsignedShort:
264 case UnsignedInt:
265 case UnsignedLong:
Michael J. Spencerfeb799c2010-10-18 07:10:59 +0000266 case UnsignedLongLong:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000267 return false;
268 };
269}
270
Alp Toker74437972014-07-06 05:14:24 +0000271/// adjust - Set forced language options.
John Thompsoned4e2952009-11-05 20:14:16 +0000272/// Apply changes to the target information with respect to certain
273/// language options which change the target configuration.
Alp Toker74437972014-07-06 05:14:24 +0000274void TargetInfo::adjust(const LangOptions &Opts) {
Daniel Dunbar9302f602010-04-15 15:06:22 +0000275 if (Opts.NoBitFieldTypeAlign)
276 UseBitFieldTypeAlignment = false;
277 if (Opts.ShortWChar)
John Thompsoned4e2952009-11-05 20:14:16 +0000278 WCharType = UnsignedShort;
David Tweed2da64382013-09-09 09:17:24 +0000279
280 if (Opts.OpenCL) {
281 // OpenCL C requires specific widths for types, irrespective of
282 // what these normally are for the target.
283 // We also define long long and long double here, although the
284 // OpenCL standard only mentions these as "reserved".
285 IntWidth = IntAlign = 32;
286 LongWidth = LongAlign = 64;
287 LongLongWidth = LongLongAlign = 128;
288 HalfWidth = HalfAlign = 16;
289 FloatWidth = FloatAlign = 32;
Eric Christopherb093d692015-10-09 18:39:59 +0000290
291 // Embedded 32-bit targets (OpenCL EP) might have double C type
292 // defined as float. Let's not override this as it might lead
Pekka Jaaskelainen43c39d52013-12-18 18:15:03 +0000293 // to generating illegal code that uses 64bit doubles.
294 if (DoubleWidth != FloatWidth) {
295 DoubleWidth = DoubleAlign = 64;
296 DoubleFormat = &llvm::APFloat::IEEEdouble;
297 }
David Tweed2da64382013-09-09 09:17:24 +0000298 LongDoubleWidth = LongDoubleAlign = 128;
299
300 assert(PointerWidth == 32 || PointerWidth == 64);
David Tweed6c2149b2013-09-10 08:00:34 +0000301 bool Is32BitArch = PointerWidth == 32;
302 SizeType = Is32BitArch ? UnsignedInt : UnsignedLong;
303 PtrDiffType = Is32BitArch ? SignedInt : SignedLong;
304 IntPtrType = Is32BitArch ? SignedInt : SignedLong;
David Tweed2da64382013-09-09 09:17:24 +0000305
306 IntMaxType = SignedLongLong;
David Tweed2da64382013-09-09 09:17:24 +0000307 Int64Type = SignedLong;
308
309 HalfFormat = &llvm::APFloat::IEEEhalf;
310 FloatFormat = &llvm::APFloat::IEEEsingle;
David Tweed2da64382013-09-09 09:17:24 +0000311 LongDoubleFormat = &llvm::APFloat::IEEEquad;
312 }
John Thompsoned4e2952009-11-05 20:14:16 +0000313}
Chris Lattner72cdcac2009-10-21 06:24:21 +0000314
Eric Christopher8c47b422015-10-09 18:39:55 +0000315bool TargetInfo::initFeatureMap(
316 llvm::StringMap<bool> &Features, DiagnosticsEngine &Diags, StringRef CPU,
317 const std::vector<std::string> &FeatureVec) const {
Eric Christophere9cdcae2015-09-01 18:13:20 +0000318 for (const auto &F : FeatureVec) {
319 const char *Name = F.c_str();
320 // Apply the feature via the target.
321 bool Enabled = Name[0] == '+';
322 setFeatureEnabled(Features, Name + 1, Enabled);
323 }
324 return true;
325}
326
Chris Lattnerec0a6d92007-09-22 18:29:59 +0000327//===----------------------------------------------------------------------===//
Chris Lattnerec0a6d92007-09-22 18:29:59 +0000328
Chris Lattner10a5b382007-01-29 05:24:35 +0000329
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000330static StringRef removeGCCRegisterPrefix(StringRef Name) {
Anders Carlsson3ed4aea2008-02-06 00:11:32 +0000331 if (Name[0] == '%' || Name[0] == '#')
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000332 Name = Name.substr(1);
Michael J. Spencerfeb799c2010-10-18 07:10:59 +0000333
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000334 return Name;
Anders Carlsson3ed4aea2008-02-06 00:11:32 +0000335}
336
Eric Christopherfd9a5f42011-06-28 18:20:53 +0000337/// isValidClobber - Returns whether the passed in string is
338/// a valid clobber in an inline asm statement. This is used by
339/// Sema.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000340bool TargetInfo::isValidClobber(StringRef Name) const {
Eric Christopherfd9a5f42011-06-28 18:20:53 +0000341 return (isValidGCCRegisterName(Name) ||
Eric Christopherb093d692015-10-09 18:39:59 +0000342 Name == "memory" || Name == "cc");
Eric Christopherfd9a5f42011-06-28 18:20:53 +0000343}
344
Anders Carlsson5fa3f342007-11-24 23:38:12 +0000345/// isValidGCCRegisterName - Returns whether the passed in string
346/// is a valid register name according to GCC. This is used by Sema for
347/// inline asm statements.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000348bool TargetInfo::isValidGCCRegisterName(StringRef Name) const {
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000349 if (Name.empty())
350 return false;
Michael J. Spencerfeb799c2010-10-18 07:10:59 +0000351
Anders Carlsson290aa852007-11-25 00:25:21 +0000352 // Get rid of any register prefix.
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000353 Name = removeGCCRegisterPrefix(Name);
Olivier Goffartfc8f8932014-08-17 13:19:48 +0000354 if (Name.empty())
Craig Topperde330c72015-10-21 04:52:34 +0000355 return false;
Anders Carlsson3ed4aea2008-02-06 00:11:32 +0000356
Craig Topperf054e3a2015-10-19 03:52:27 +0000357 ArrayRef<const char *> Names = getGCCRegNames();
Mike Stump11289f42009-09-09 15:08:12 +0000358
Anders Carlsson290aa852007-11-25 00:25:21 +0000359 // If we have a number it maps to an entry in the register name array.
Jordan Rosea7d03842013-02-08 22:30:41 +0000360 if (isDigit(Name[0])) {
Craig Topper67288a92015-10-21 04:52:36 +0000361 unsigned n;
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000362 if (!Name.getAsInteger(0, n))
Craig Topper67288a92015-10-21 04:52:36 +0000363 return n < Names.size();
Anders Carlsson290aa852007-11-25 00:25:21 +0000364 }
365
366 // Check register names.
Craig Topper335c8f92015-10-21 04:52:38 +0000367 if (std::find(Names.begin(), Names.end(), Name) != Names.end())
368 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000369
Eric Christophercdd36352011-06-21 00:05:20 +0000370 // Check any additional names that we have.
Craig Topperf054e3a2015-10-19 03:52:27 +0000371 ArrayRef<AddlRegName> AddlNames = getGCCAddlRegNames();
372 for (unsigned i = 0; i < AddlNames.size(); i++)
Eric Christophercdd36352011-06-21 00:05:20 +0000373 for (unsigned j = 0; j < llvm::array_lengthof(AddlNames[i].Names); j++) {
374 if (!AddlNames[i].Names[j])
Eric Christopherb093d692015-10-09 18:39:59 +0000375 break;
Eric Christophercdd36352011-06-21 00:05:20 +0000376 // Make sure the register that the additional name is for is within
377 // the bounds of the register names from above.
Craig Topperf054e3a2015-10-19 03:52:27 +0000378 if (AddlNames[i].Names[j] == Name && AddlNames[i].RegNum < Names.size())
Eric Christopherb093d692015-10-09 18:39:59 +0000379 return true;
Eric Christophercdd36352011-06-21 00:05:20 +0000380 }
381
Anders Carlsson290aa852007-11-25 00:25:21 +0000382 // Now check aliases.
Craig Topperf054e3a2015-10-19 03:52:27 +0000383 ArrayRef<GCCRegAlias> Aliases = getGCCRegAliases();
384 for (unsigned i = 0; i < Aliases.size(); i++) {
Anders Carlsson290aa852007-11-25 00:25:21 +0000385 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
386 if (!Aliases[i].Aliases[j])
387 break;
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000388 if (Aliases[i].Aliases[j] == Name)
Anders Carlsson290aa852007-11-25 00:25:21 +0000389 return true;
390 }
391 }
Mike Stump11289f42009-09-09 15:08:12 +0000392
Anders Carlsson5fa3f342007-11-24 23:38:12 +0000393 return false;
394}
Anders Carlssonf511f642007-11-27 04:11:28 +0000395
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000396StringRef
397TargetInfo::getNormalizedGCCRegisterName(StringRef Name) const {
Anders Carlssonf511f642007-11-27 04:11:28 +0000398 assert(isValidGCCRegisterName(Name) && "Invalid register passed in");
Mike Stump11289f42009-09-09 15:08:12 +0000399
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000400 // Get rid of any register prefix.
401 Name = removeGCCRegisterPrefix(Name);
Mike Stump11289f42009-09-09 15:08:12 +0000402
Craig Topperf054e3a2015-10-19 03:52:27 +0000403 ArrayRef<const char *> Names = getGCCRegNames();
Anders Carlssonf511f642007-11-27 04:11:28 +0000404
405 // First, check if we have a number.
Jordan Rosea7d03842013-02-08 22:30:41 +0000406 if (isDigit(Name[0])) {
Craig Topper67288a92015-10-21 04:52:36 +0000407 unsigned n;
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000408 if (!Name.getAsInteger(0, n)) {
Craig Topper67288a92015-10-21 04:52:36 +0000409 assert(n < Names.size() && "Out of bounds register number!");
Anders Carlssonf511f642007-11-27 04:11:28 +0000410 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.
Craig Topperf054e3a2015-10-19 03:52:27 +0000415 ArrayRef<AddlRegName> AddlNames = getGCCAddlRegNames();
416 for (unsigned i = 0; i < AddlNames.size(); i++)
Eric Christophercdd36352011-06-21 00:05:20 +0000417 for (unsigned j = 0; j < llvm::array_lengthof(AddlNames[i].Names); j++) {
418 if (!AddlNames[i].Names[j])
Eric Christopherb093d692015-10-09 18:39:59 +0000419 break;
Eric Christophercdd36352011-06-21 00:05:20 +0000420 // Make sure the register that the additional name is for is within
421 // the bounds of the register names from above.
Craig Topperf054e3a2015-10-19 03:52:27 +0000422 if (AddlNames[i].Names[j] == Name && AddlNames[i].RegNum < Names.size())
Eric Christopherb093d692015-10-09 18:39:59 +0000423 return Name;
Eric Christophercdd36352011-06-21 00:05:20 +0000424 }
425
Anders Carlssonf511f642007-11-27 04:11:28 +0000426 // Now check aliases.
Craig Topperf054e3a2015-10-19 03:52:27 +0000427 ArrayRef<GCCRegAlias> Aliases = getGCCRegAliases();
428 for (unsigned i = 0; i < Aliases.size(); i++) {
Anders Carlssonf511f642007-11-27 04:11:28 +0000429 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
430 if (!Aliases[i].Aliases[j])
431 break;
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000432 if (Aliases[i].Aliases[j] == Name)
Anders Carlssonf511f642007-11-27 04:11:28 +0000433 return Aliases[i].Register;
434 }
435 }
Mike Stump11289f42009-09-09 15:08:12 +0000436
Anders Carlssonf511f642007-11-27 04:11:28 +0000437 return Name;
438}
439
Chris Lattnerc3f4c7b2009-04-26 17:19:08 +0000440bool TargetInfo::validateOutputConstraint(ConstraintInfo &Info) const {
441 const char *Name = Info.getConstraintStr().c_str();
Anders Carlssonf511f642007-11-27 04:11:28 +0000442 // An output constraint must start with '=' or '+'
443 if (*Name != '=' && *Name != '+')
444 return false;
445
446 if (*Name == '+')
Chris Lattnerd9725f72009-04-26 07:16:29 +0000447 Info.setIsReadWrite();
Anders Carlssonf511f642007-11-27 04:11:28 +0000448
449 Name++;
450 while (*Name) {
451 switch (*Name) {
452 default:
Chris Lattnerd9725f72009-04-26 07:16:29 +0000453 if (!validateAsmConstraint(Name, Info)) {
Ted Kremenekb44485b2008-04-24 16:36:38 +0000454 // FIXME: We temporarily return false
Anders Carlssonf511f642007-11-27 04:11:28 +0000455 // so we can add more constraints as we hit it.
456 // Eventually, an unknown constraint should just be treated as 'g'.
Ted Kremenekb44485b2008-04-24 16:36:38 +0000457 return false;
Anders Carlssonf511f642007-11-27 04:11:28 +0000458 }
David Majnemera0040df2015-01-10 10:43:19 +0000459 break;
Anders Carlssonf511f642007-11-27 04:11:28 +0000460 case '&': // early clobber.
David Majnemera0040df2015-01-10 10:43:19 +0000461 Info.setEarlyClobber();
Anders Carlssonf511f642007-11-27 04:11:28 +0000462 break;
Chris Lattnerf3154712009-10-13 04:32:07 +0000463 case '%': // commutative.
464 // FIXME: Check that there is a another register after this one.
465 break;
Anders Carlssonf511f642007-11-27 04:11:28 +0000466 case 'r': // general register.
Chris Lattnerd9725f72009-04-26 07:16:29 +0000467 Info.setAllowsRegister();
Anders Carlssonf511f642007-11-27 04:11:28 +0000468 break;
469 case 'm': // memory operand.
Dale Johannesen8499f472010-09-07 18:40:41 +0000470 case 'o': // offsetable memory operand.
471 case 'V': // non-offsetable memory operand.
472 case '<': // autodecrement memory operand.
473 case '>': // autoincrement memory operand.
Chris Lattnerd9725f72009-04-26 07:16:29 +0000474 Info.setAllowsMemory();
Anders Carlssonf511f642007-11-27 04:11:28 +0000475 break;
476 case 'g': // general register, memory operand or immediate integer.
Anders Carlssone70cde12009-01-18 02:12:04 +0000477 case 'X': // any operand.
Chris Lattnerd9725f72009-04-26 07:16:29 +0000478 Info.setAllowsRegister();
479 Info.setAllowsMemory();
Anders Carlssonf511f642007-11-27 04:11:28 +0000480 break;
John Thompsona5c7d702010-08-10 19:20:14 +0000481 case ',': // multiple alternative constraint. Pass it.
John Thompson46667af2010-08-11 00:58:20 +0000482 // Handle additional optional '=' or '+' modifiers.
John Thompson12240612010-09-18 01:15:13 +0000483 if (Name[1] == '=' || Name[1] == '+')
John Thompson46667af2010-08-11 00:58:20 +0000484 Name++;
John Thompsona5c7d702010-08-10 19:20:14 +0000485 break;
David Majnemer50cb0552015-01-11 08:52:38 +0000486 case '#': // Ignore as constraint.
487 while (Name[1] && Name[1] != ',')
488 Name++;
David Majnemerc71a5662015-01-11 09:39:03 +0000489 break;
John Thompsona5c7d702010-08-10 19:20:14 +0000490 case '?': // Disparage slightly code.
Dale Johannesen8499f472010-09-07 18:40:41 +0000491 case '!': // Disparage severely.
Ulrich Weigand7bcc7ec2012-10-29 12:20:54 +0000492 case '*': // Ignore for choosing register preferences.
John Thompsona5c7d702010-08-10 19:20:14 +0000493 break; // Pass them.
Anders Carlssonf511f642007-11-27 04:11:28 +0000494 }
Mike Stump11289f42009-09-09 15:08:12 +0000495
Anders Carlssonf511f642007-11-27 04:11:28 +0000496 Name++;
497 }
Mike Stump11289f42009-09-09 15:08:12 +0000498
David Majnemera0040df2015-01-10 10:43:19 +0000499 // Early clobber with a read-write constraint which doesn't permit registers
500 // is invalid.
501 if (Info.earlyClobber() && Info.isReadWrite() && !Info.allowsRegister())
502 return false;
503
Benjamin Kramer1e4a8862013-04-18 22:49:48 +0000504 // If a constraint allows neither memory nor register operands it contains
505 // only modifiers. Reject it.
Benjamin Kramer7ee3b9c2013-04-18 13:23:23 +0000506 return Info.allowsMemory() || Info.allowsRegister();
Anders Carlssonf511f642007-11-27 04:11:28 +0000507}
508
Anders Carlssona79203b2009-01-18 01:56:57 +0000509bool TargetInfo::resolveSymbolicName(const char *&Name,
Craig Topper55765ca2015-10-21 02:34:10 +0000510 ArrayRef<ConstraintInfo> OutputConstraints,
Chris Lattnerd9725f72009-04-26 07:16:29 +0000511 unsigned &Index) const {
Anders Carlssona79203b2009-01-18 01:56:57 +0000512 assert(*Name == '[' && "Symbolic name did not start with '['");
Anders Carlssona79203b2009-01-18 01:56:57 +0000513 Name++;
514 const char *Start = Name;
515 while (*Name && *Name != ']')
516 Name++;
Mike Stump11289f42009-09-09 15:08:12 +0000517
Anders Carlssona79203b2009-01-18 01:56:57 +0000518 if (!*Name) {
519 // Missing ']'
520 return false;
521 }
Mike Stump11289f42009-09-09 15:08:12 +0000522
Anders Carlssona79203b2009-01-18 01:56:57 +0000523 std::string SymbolicName(Start, Name - Start);
Mike Stump11289f42009-09-09 15:08:12 +0000524
Craig Topper55765ca2015-10-21 02:34:10 +0000525 for (Index = 0; Index != OutputConstraints.size(); ++Index)
Chris Lattnerc16d4762009-04-26 17:57:12 +0000526 if (SymbolicName == OutputConstraints[Index].getName())
Anders Carlssona79203b2009-01-18 01:56:57 +0000527 return true;
Anders Carlssona79203b2009-01-18 01:56:57 +0000528
529 return false;
530}
531
Craig Topper55765ca2015-10-21 02:34:10 +0000532bool TargetInfo::validateInputConstraint(
533 MutableArrayRef<ConstraintInfo> OutputConstraints,
534 ConstraintInfo &Info) const {
Chris Lattnerc3f4c7b2009-04-26 17:19:08 +0000535 const char *Name = Info.ConstraintStr.c_str();
536
Duncan P. N. Exon Smithd68c7aa2013-12-16 03:20:06 +0000537 if (!*Name)
538 return false;
539
Anders Carlssonf511f642007-11-27 04:11:28 +0000540 while (*Name) {
541 switch (*Name) {
542 default:
543 // Check if we have a matching constraint
544 if (*Name >= '0' && *Name <= '9') {
David Majnemerb6b56432015-01-11 10:22:41 +0000545 const char *DigitStart = Name;
546 while (Name[1] >= '0' && Name[1] <= '9')
547 Name++;
548 const char *DigitEnd = Name;
549 unsigned i;
550 if (StringRef(DigitStart, DigitEnd - DigitStart + 1)
551 .getAsInteger(10, i))
552 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000553
Anders Carlssonf511f642007-11-27 04:11:28 +0000554 // Check if matching constraint is out of bounds.
Craig Topper55765ca2015-10-21 02:34:10 +0000555 if (i >= OutputConstraints.size()) return false;
Mike Stump11289f42009-09-09 15:08:12 +0000556
Anders Carlssonda1f5fc2010-11-03 02:22:29 +0000557 // A number must refer to an output only operand.
558 if (OutputConstraints[i].isReadWrite())
559 return false;
560
Eric Christopherb093d692015-10-09 18:39:59 +0000561 // If the constraint is already tied, it must be tied to the
Anders Carlsson2d5f8b42010-11-03 02:54:51 +0000562 // same operand referenced to by the number.
563 if (Info.hasTiedOperand() && Info.getTiedOperand() != i)
564 return false;
565
Mike Stump11289f42009-09-09 15:08:12 +0000566 // The constraint should have the same info as the respective
Anders Carlsson570c3572009-01-27 20:38:24 +0000567 // output constraint.
Chris Lattner4c92b512009-04-26 18:05:25 +0000568 Info.setTiedOperand(i, OutputConstraints[i]);
Chris Lattnerd9725f72009-04-26 07:16:29 +0000569 } else if (!validateAsmConstraint(Name, Info)) {
Daniel Dunbar81128e02008-08-25 09:46:27 +0000570 // FIXME: This error return is in place temporarily so we can
571 // add more constraints as we hit it. Eventually, an unknown
572 // constraint should just be treated as 'g'.
573 return false;
Anders Carlssona79203b2009-01-18 01:56:57 +0000574 }
575 break;
576 case '[': {
577 unsigned Index = 0;
Craig Topper55765ca2015-10-21 02:34:10 +0000578 if (!resolveSymbolicName(Name, OutputConstraints, Index))
Anders Carlssona79203b2009-01-18 01:56:57 +0000579 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000580
Eric Christopherb093d692015-10-09 18:39:59 +0000581 // If the constraint is already tied, it must be tied to the
Anders Carlsson2d5f8b42010-11-03 02:54:51 +0000582 // same operand referenced to by the number.
583 if (Info.hasTiedOperand() && Info.getTiedOperand() != Index)
584 return false;
585
David Majnemer55164f92015-01-11 09:57:13 +0000586 // A number must refer to an output only operand.
587 if (OutputConstraints[Index].isReadWrite())
588 return false;
589
Eli Friedman854f1102010-08-11 23:03:37 +0000590 Info.setTiedOperand(Index, OutputConstraints[Index]);
Anders Carlssona79203b2009-01-18 01:56:57 +0000591 break;
Mike Stump11289f42009-09-09 15:08:12 +0000592 }
Anders Carlsson050f4942007-12-08 19:32:57 +0000593 case '%': // commutative
594 // FIXME: Fail if % is used with the last operand.
595 break;
Anders Carlssonf511f642007-11-27 04:11:28 +0000596 case 'i': // immediate integer.
Anders Carlssona3a96af2008-03-09 06:02:02 +0000597 case 'n': // immediate integer with a known value.
Anders Carlssonf511f642007-11-27 04:11:28 +0000598 break;
Chris Lattnerdbcc5ca2009-05-06 04:33:31 +0000599 case 'I': // Various constant constraints with target-specific meanings.
600 case 'J':
601 case 'K':
602 case 'L':
603 case 'M':
604 case 'N':
605 case 'O':
606 case 'P':
Saleem Abdulrasoola2823572015-01-06 04:26:34 +0000607 if (!validateAsmConstraint(Name, Info))
608 return false;
Chris Lattnerdbcc5ca2009-05-06 04:33:31 +0000609 break;
Anders Carlssonf511f642007-11-27 04:11:28 +0000610 case 'r': // general register.
Chris Lattnerd9725f72009-04-26 07:16:29 +0000611 Info.setAllowsRegister();
Anders Carlssonf511f642007-11-27 04:11:28 +0000612 break;
613 case 'm': // memory operand.
Dale Johannesen8499f472010-09-07 18:40:41 +0000614 case 'o': // offsettable memory operand.
615 case 'V': // non-offsettable memory operand.
616 case '<': // autodecrement memory operand.
617 case '>': // autoincrement memory operand.
Chris Lattnerd9725f72009-04-26 07:16:29 +0000618 Info.setAllowsMemory();
Anders Carlssonf511f642007-11-27 04:11:28 +0000619 break;
620 case 'g': // general register, memory operand or immediate integer.
Anders Carlssone70cde12009-01-18 02:12:04 +0000621 case 'X': // any operand.
Chris Lattnerd9725f72009-04-26 07:16:29 +0000622 Info.setAllowsRegister();
623 Info.setAllowsMemory();
Anders Carlssonf511f642007-11-27 04:11:28 +0000624 break;
John Thompsonc467aa22010-09-21 22:04:54 +0000625 case 'E': // immediate floating point.
626 case 'F': // immediate floating point.
627 case 'p': // address operand.
628 break;
John Thompsona5c7d702010-08-10 19:20:14 +0000629 case ',': // multiple alternative constraint. Ignore comma.
630 break;
David Majnemerc71a5662015-01-11 09:39:03 +0000631 case '#': // Ignore as constraint.
632 while (Name[1] && Name[1] != ',')
633 Name++;
David Majnemerc71a5662015-01-11 09:39:03 +0000634 break;
John Thompsona5c7d702010-08-10 19:20:14 +0000635 case '?': // Disparage slightly code.
Chris Lattner57540c52011-04-15 05:22:18 +0000636 case '!': // Disparage severely.
Ulrich Weigand7bcc7ec2012-10-29 12:20:54 +0000637 case '*': // Ignore for choosing register preferences.
John Thompsona5c7d702010-08-10 19:20:14 +0000638 break; // Pass them.
Anders Carlssonf511f642007-11-27 04:11:28 +0000639 }
Mike Stump11289f42009-09-09 15:08:12 +0000640
Anders Carlssonf511f642007-11-27 04:11:28 +0000641 Name++;
642 }
Mike Stump11289f42009-09-09 15:08:12 +0000643
Anders Carlssonf511f642007-11-27 04:11:28 +0000644 return true;
645}