blob: e19404dc54cba5113946bb870178bdc342d1fa20 [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.
Matt Arsenaultf333de32016-09-07 07:08:02 +000030 BigEndian = !T.isLittleEndian();
Eli Friedmand88c8a12009-04-19 21:38:35 +000031 TLSSupported = true;
Chris Lattner1a8f3942010-04-23 16:29:58 +000032 NoAsmVariants = false;
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +000033 HasFloat128 = false;
Chris Lattner5e2ef0c2008-05-09 06:08:39 +000034 PointerWidth = PointerAlign = 32;
Roman Divacky965b0b72011-01-06 08:27:10 +000035 BoolWidth = BoolAlign = 8;
Chris Lattnerb781dc792008-05-08 05:58:21 +000036 IntWidth = IntAlign = 32;
Chris Lattner5a9aaaf2008-05-09 05:50:02 +000037 LongWidth = LongAlign = 32;
38 LongLongWidth = LongLongAlign = 64;
Nick Lewyckyf59e1292011-12-16 22:34:14 +000039 SuitableAlign = 64;
Ulrich Weigandca3cb7f2015-04-21 17:29:35 +000040 DefaultAlignForAttributeAligned = 128;
Ulrich Weigandfa806422013-05-06 16:23:57 +000041 MinGlobalAlign = 0;
Richard Smith59139022016-09-30 22:41:36 +000042 // From the glibc documentation, on GNU systems, malloc guarantees 16-byte
43 // alignment on 64-bit systems and 8-byte alignment on 32-bit systems. See
44 // https://www.gnu.org/software/libc/manual/html_node/Malloc-Examples.html
45 if (T.isGNUEnvironment())
46 NewAlign = Triple.isArch64Bit() ? 128 : Triple.isArch32Bit() ? 64 : 0;
47 else
48 NewAlign = 0; // Infer from basic type alignment.
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +000049 HalfWidth = 16;
50 HalfAlign = 16;
Eli Friedmanb5366062008-05-20 14:21:01 +000051 FloatWidth = 32;
52 FloatAlign = 32;
Nate Begeman65bea232008-04-18 17:17:24 +000053 DoubleWidth = 64;
Eli Friedmanb5366062008-05-20 14:21:01 +000054 DoubleAlign = 64;
55 LongDoubleWidth = 64;
56 LongDoubleAlign = 64;
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +000057 Float128Align = 128;
Rafael Espindolae971b9a2010-06-04 23:15:27 +000058 LargeArrayMinWidth = 0;
59 LargeArrayAlign = 0;
Eli Friedman4b72fdd2011-10-14 20:59:01 +000060 MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 0;
Chad Rosiercc40ea72012-07-13 23:57:43 +000061 MaxVectorAlign = 0;
Paul Robinsond30e2ee2015-07-14 20:52:32 +000062 MaxTLSAlign = 0;
Alexey Bataev00396512015-07-02 03:40:19 +000063 SimdDefaultAlign = 0;
Anders Carlsson2a79a902008-10-31 16:05:19 +000064 SizeType = UnsignedLong;
Eli Friedmand50881c2008-11-02 02:43:55 +000065 PtrDiffType = SignedLong;
Sanjiv Guptad7959242008-10-31 09:52:39 +000066 IntMaxType = SignedLongLong;
Chris Lattner7e4c81c2009-02-13 22:28:55 +000067 IntPtrType = SignedLong;
Eli Friedmand50881c2008-11-02 02:43:55 +000068 WCharType = SignedInt;
Chris Lattner67204922009-10-21 04:59:34 +000069 WIntType = SignedInt;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +000070 Char16Type = UnsignedShort;
71 Char32Type = UnsignedInt;
Eli Friedman2857ccb2009-07-01 03:36:11 +000072 Int64Type = SignedLongLong;
Edward O'Callaghan847f2a12009-11-21 00:49:54 +000073 SigAtomicType = SignedInt;
Eli Friedman4e91899e2012-11-27 02:58:24 +000074 ProcessIDType = SignedInt;
Fariborz Jahanian29898f42012-04-16 21:03:30 +000075 UseSignedCharForObjCBool = true;
Daniel Dunbar1da65112010-04-15 15:06:18 +000076 UseBitFieldTypeAlignment = true;
Chad Rosier18903ee2011-08-04 01:21:14 +000077 UseZeroLengthBitfieldAlignment = false;
Sunil Srivastava0ce2f222016-02-05 20:50:02 +000078 UseExplicitBitFieldAlignment = true;
Chad Rosier18903ee2011-08-04 01:21:14 +000079 ZeroLengthBitfieldBoundary = 0;
Stephan Bergmann17c7f702016-12-14 11:57:17 +000080 HalfFormat = &llvm::APFloat::IEEEhalf();
81 FloatFormat = &llvm::APFloat::IEEEsingle();
82 DoubleFormat = &llvm::APFloat::IEEEdouble();
83 LongDoubleFormat = &llvm::APFloat::IEEEdouble();
84 Float128Format = &llvm::APFloat::IEEEquad();
Roman Divacky178e01602011-02-10 16:52:03 +000085 MCountName = "mcount";
Abramo Bagnara41bfb662011-09-08 14:20:25 +000086 RegParmMax = 0;
87 SSERegParmMax = 0;
Daniel Dunbarbd606522010-05-27 00:35:16 +000088 HasAlignMac68kSupport = false;
Charles Davisc7d5c942015-09-17 20:55:33 +000089 HasBuiltinMSVaList = false;
Pirama Arumuga Nainarbb846a32016-07-27 19:01:51 +000090 IsRenderScriptTarget = false;
Daniel Dunbar6f2e8392010-07-14 23:39:36 +000091
92 // Default to no types using fpret.
93 RealTypeUsesObjCFPRet = 0;
John McCall86353412010-08-21 22:46:04 +000094
Anders Carlsson2f1a6c32011-10-31 16:27:11 +000095 // Default to not using fp2ret for __Complex long double
96 ComplexLongDoubleUsesFP2Ret = false;
97
Hans Wennborgc9bd88e2014-01-14 19:35:09 +000098 // Set the C++ ABI based on the triple.
Saleem Abdulrasool377066a2014-03-27 22:50:18 +000099 TheCXXABI.set(Triple.isKnownWindowsMSVCEnvironment()
Hans Wennborgc9bd88e2014-01-14 19:35:09 +0000100 ? TargetCXXABI::Microsoft
101 : TargetCXXABI::GenericItanium);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000102
103 // Default to an empty address space map.
104 AddrSpaceMap = &DefaultAddrSpaceMap;
David Tweed31d09b02013-09-13 12:04:22 +0000105 UseAddrSpaceMapMangling = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000106
107 // Default to an unknown platform name.
108 PlatformName = "unknown";
109 PlatformMinVersion = VersionTuple();
Chris Lattner1df27862008-03-08 08:59:43 +0000110}
111
Chris Lattnerc3a669b2008-03-08 08:24:01 +0000112// Out of line virtual dtor for TargetInfo.
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +0000113TargetInfo::~TargetInfo() {}
Chris Lattner2194ddc2006-10-14 18:32:26 +0000114
Chris Lattnera91c30f2009-02-06 05:04:11 +0000115/// getTypeName - Return the user string for the specified integer type enum.
116/// For example, SignedShort -> "short".
117const char *TargetInfo::getTypeName(IntType T) {
118 switch (T) {
David Blaikie83d382b2011-09-23 05:06:16 +0000119 default: llvm_unreachable("not an integer!");
Joerg Sonnenberger3d9478c2014-07-28 21:06:22 +0000120 case SignedChar: return "signed char";
Stepan Dyatkovskiy5a637922013-09-05 11:23:21 +0000121 case UnsignedChar: return "unsigned char";
Chris Lattnera91c30f2009-02-06 05:04:11 +0000122 case SignedShort: return "short";
123 case UnsignedShort: return "unsigned short";
124 case SignedInt: return "int";
125 case UnsignedInt: return "unsigned int";
126 case SignedLong: return "long int";
127 case UnsignedLong: return "long unsigned int";
128 case SignedLongLong: return "long long int";
129 case UnsignedLongLong: return "long long unsigned int";
130 }
131}
132
Chris Lattner72cdcac2009-10-21 06:24:21 +0000133/// getTypeConstantSuffix - Return the constant suffix for the specified
134/// integer type enum. For example, SignedLong -> "L".
Joerg Sonnenberger587deea2014-07-17 20:12:32 +0000135const char *TargetInfo::getTypeConstantSuffix(IntType T) const {
Chris Lattner72cdcac2009-10-21 06:24:21 +0000136 switch (T) {
David Blaikie83d382b2011-09-23 05:06:16 +0000137 default: llvm_unreachable("not an integer!");
Stepan Dyatkovskiy5a637922013-09-05 11:23:21 +0000138 case SignedChar:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000139 case SignedShort:
140 case SignedInt: return "";
141 case SignedLong: return "L";
142 case SignedLongLong: return "LL";
Stepan Dyatkovskiy5a637922013-09-05 11:23:21 +0000143 case UnsignedChar:
Joerg Sonnenberger587deea2014-07-17 20:12:32 +0000144 if (getCharWidth() < getIntWidth())
145 return "";
Chris Lattner72cdcac2009-10-21 06:24:21 +0000146 case UnsignedShort:
Joerg Sonnenberger587deea2014-07-17 20:12:32 +0000147 if (getShortWidth() < getIntWidth())
148 return "";
Chris Lattner72cdcac2009-10-21 06:24:21 +0000149 case UnsignedInt: return "U";
150 case UnsignedLong: return "UL";
151 case UnsignedLongLong: return "ULL";
152 }
153}
154
Joerg Sonnenbergerbe324f92014-07-15 11:30:00 +0000155/// getTypeFormatModifier - Return the printf format modifier for the
156/// specified integer type enum. For example, SignedLong -> "l".
157
158const char *TargetInfo::getTypeFormatModifier(IntType T) {
159 switch (T) {
160 default: llvm_unreachable("not an integer!");
161 case SignedChar:
162 case UnsignedChar: return "hh";
163 case SignedShort:
164 case UnsignedShort: return "h";
165 case SignedInt:
166 case UnsignedInt: return "";
167 case SignedLong:
168 case UnsignedLong: return "l";
169 case SignedLongLong:
170 case UnsignedLongLong: return "ll";
171 }
172}
173
Michael J. Spencerfeb799c2010-10-18 07:10:59 +0000174/// getTypeWidth - Return the width (in bits) of the specified integer type
Chris Lattner72cdcac2009-10-21 06:24:21 +0000175/// enum. For example, SignedInt -> getIntWidth().
176unsigned TargetInfo::getTypeWidth(IntType T) const {
177 switch (T) {
David Blaikie83d382b2011-09-23 05:06:16 +0000178 default: llvm_unreachable("not an integer!");
Stepan Dyatkovskiy5a637922013-09-05 11:23:21 +0000179 case SignedChar:
180 case UnsignedChar: return getCharWidth();
Chris Lattnere4a8c642009-11-05 21:21:32 +0000181 case SignedShort:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000182 case UnsignedShort: return getShortWidth();
Chris Lattnere4a8c642009-11-05 21:21:32 +0000183 case SignedInt:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000184 case UnsignedInt: return getIntWidth();
Chris Lattnere4a8c642009-11-05 21:21:32 +0000185 case SignedLong:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000186 case UnsignedLong: return getLongWidth();
Chris Lattnere4a8c642009-11-05 21:21:32 +0000187 case SignedLongLong:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000188 case UnsignedLongLong: return getLongLongWidth();
189 };
190}
191
Stepan Dyatkovskiy5a637922013-09-05 11:23:21 +0000192TargetInfo::IntType TargetInfo::getIntTypeByWidth(
193 unsigned BitWidth, bool IsSigned) const {
194 if (getCharWidth() == BitWidth)
195 return IsSigned ? SignedChar : UnsignedChar;
196 if (getShortWidth() == BitWidth)
197 return IsSigned ? SignedShort : UnsignedShort;
198 if (getIntWidth() == BitWidth)
199 return IsSigned ? SignedInt : UnsignedInt;
200 if (getLongWidth() == BitWidth)
201 return IsSigned ? SignedLong : UnsignedLong;
202 if (getLongLongWidth() == BitWidth)
203 return IsSigned ? SignedLongLong : UnsignedLongLong;
204 return NoInt;
205}
206
JF Bastienab8d0a02014-06-25 01:31:33 +0000207TargetInfo::IntType TargetInfo::getLeastIntTypeByWidth(unsigned BitWidth,
208 bool IsSigned) const {
209 if (getCharWidth() >= BitWidth)
210 return IsSigned ? SignedChar : UnsignedChar;
211 if (getShortWidth() >= BitWidth)
212 return IsSigned ? SignedShort : UnsignedShort;
213 if (getIntWidth() >= BitWidth)
214 return IsSigned ? SignedInt : UnsignedInt;
215 if (getLongWidth() >= BitWidth)
216 return IsSigned ? SignedLong : UnsignedLong;
217 if (getLongLongWidth() >= BitWidth)
218 return IsSigned ? SignedLongLong : UnsignedLongLong;
219 return NoInt;
220}
221
Stepan Dyatkovskiy5a637922013-09-05 11:23:21 +0000222TargetInfo::RealType TargetInfo::getRealTypeByWidth(unsigned BitWidth) const {
223 if (getFloatWidth() == BitWidth)
224 return Float;
225 if (getDoubleWidth() == BitWidth)
226 return Double;
227
228 switch (BitWidth) {
229 case 96:
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000230 if (&getLongDoubleFormat() == &llvm::APFloat::x87DoubleExtended())
Stepan Dyatkovskiy5a637922013-09-05 11:23:21 +0000231 return LongDouble;
232 break;
233 case 128:
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000234 if (&getLongDoubleFormat() == &llvm::APFloat::PPCDoubleDouble() ||
235 &getLongDoubleFormat() == &llvm::APFloat::IEEEquad())
Stepan Dyatkovskiy5a637922013-09-05 11:23:21 +0000236 return LongDouble;
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +0000237 if (hasFloat128Type())
238 return Float128;
Stepan Dyatkovskiy5a637922013-09-05 11:23:21 +0000239 break;
240 }
241
242 return NoFloat;
243}
244
Michael J. Spencerfeb799c2010-10-18 07:10:59 +0000245/// getTypeAlign - Return the alignment (in bits) of the specified integer type
Chris Lattnere4a8c642009-11-05 21:21:32 +0000246/// enum. For example, SignedInt -> getIntAlign().
247unsigned TargetInfo::getTypeAlign(IntType T) const {
248 switch (T) {
David Blaikie83d382b2011-09-23 05:06:16 +0000249 default: llvm_unreachable("not an integer!");
Stepan Dyatkovskiy5a637922013-09-05 11:23:21 +0000250 case SignedChar:
251 case UnsignedChar: return getCharAlign();
Chris Lattnere4a8c642009-11-05 21:21:32 +0000252 case SignedShort:
253 case UnsignedShort: return getShortAlign();
254 case SignedInt:
255 case UnsignedInt: return getIntAlign();
256 case SignedLong:
257 case UnsignedLong: return getLongAlign();
258 case SignedLongLong:
259 case UnsignedLongLong: return getLongLongAlign();
260 };
261}
262
Chris Lattnerc0c04392009-10-25 22:49:18 +0000263/// isTypeSigned - Return whether an integer types is signed. Returns true if
Chris Lattner72cdcac2009-10-21 06:24:21 +0000264/// the type is signed; false otherwise.
Chris Lattnerad3467e2010-12-25 23:25:43 +0000265bool TargetInfo::isTypeSigned(IntType T) {
Chris Lattner72cdcac2009-10-21 06:24:21 +0000266 switch (T) {
David Blaikie83d382b2011-09-23 05:06:16 +0000267 default: llvm_unreachable("not an integer!");
Stepan Dyatkovskiy5a637922013-09-05 11:23:21 +0000268 case SignedChar:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000269 case SignedShort:
270 case SignedInt:
271 case SignedLong:
Michael J. Spencerfeb799c2010-10-18 07:10:59 +0000272 case SignedLongLong:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000273 return true;
Stepan Dyatkovskiy5a637922013-09-05 11:23:21 +0000274 case UnsignedChar:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000275 case UnsignedShort:
276 case UnsignedInt:
277 case UnsignedLong:
Michael J. Spencerfeb799c2010-10-18 07:10:59 +0000278 case UnsignedLongLong:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000279 return false;
280 };
281}
282
Alp Toker74437972014-07-06 05:14:24 +0000283/// adjust - Set forced language options.
John Thompsoned4e2952009-11-05 20:14:16 +0000284/// Apply changes to the target information with respect to certain
Eric Christopher3646e622017-03-22 06:36:09 +0000285/// language options which change the target configuration and adjust
286/// the language based on the target options where applicable.
287void TargetInfo::adjust(LangOptions &Opts) {
Daniel Dunbar9302f602010-04-15 15:06:22 +0000288 if (Opts.NoBitFieldTypeAlign)
289 UseBitFieldTypeAlignment = false;
290 if (Opts.ShortWChar)
John Thompsoned4e2952009-11-05 20:14:16 +0000291 WCharType = UnsignedShort;
Reid Kleckner8195f692016-05-04 02:58:24 +0000292 if (Opts.AlignDouble) {
293 DoubleAlign = LongLongAlign = 64;
294 LongDoubleAlign = 64;
295 }
David Tweed2da64382013-09-09 09:17:24 +0000296
297 if (Opts.OpenCL) {
298 // OpenCL C requires specific widths for types, irrespective of
299 // what these normally are for the target.
300 // We also define long long and long double here, although the
301 // OpenCL standard only mentions these as "reserved".
302 IntWidth = IntAlign = 32;
303 LongWidth = LongAlign = 64;
304 LongLongWidth = LongLongAlign = 128;
305 HalfWidth = HalfAlign = 16;
306 FloatWidth = FloatAlign = 32;
Eric Christopherb093d692015-10-09 18:39:59 +0000307
308 // Embedded 32-bit targets (OpenCL EP) might have double C type
309 // defined as float. Let's not override this as it might lead
Pekka Jaaskelainen43c39d52013-12-18 18:15:03 +0000310 // to generating illegal code that uses 64bit doubles.
311 if (DoubleWidth != FloatWidth) {
312 DoubleWidth = DoubleAlign = 64;
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000313 DoubleFormat = &llvm::APFloat::IEEEdouble();
Pekka Jaaskelainen43c39d52013-12-18 18:15:03 +0000314 }
David Tweed2da64382013-09-09 09:17:24 +0000315 LongDoubleWidth = LongDoubleAlign = 128;
316
Yaxun Liu26f75662016-08-19 05:17:25 +0000317 unsigned MaxPointerWidth = getMaxPointerWidth();
318 assert(MaxPointerWidth == 32 || MaxPointerWidth == 64);
319 bool Is32BitArch = MaxPointerWidth == 32;
David Tweed6c2149b2013-09-10 08:00:34 +0000320 SizeType = Is32BitArch ? UnsignedInt : UnsignedLong;
321 PtrDiffType = Is32BitArch ? SignedInt : SignedLong;
322 IntPtrType = Is32BitArch ? SignedInt : SignedLong;
David Tweed2da64382013-09-09 09:17:24 +0000323
324 IntMaxType = SignedLongLong;
David Tweed2da64382013-09-09 09:17:24 +0000325 Int64Type = SignedLong;
326
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000327 HalfFormat = &llvm::APFloat::IEEEhalf();
328 FloatFormat = &llvm::APFloat::IEEEsingle();
329 LongDoubleFormat = &llvm::APFloat::IEEEquad();
David Tweed2da64382013-09-09 09:17:24 +0000330 }
Richard Smith59139022016-09-30 22:41:36 +0000331
332 if (Opts.NewAlignOverride)
333 NewAlign = Opts.NewAlignOverride * getCharWidth();
John Thompsoned4e2952009-11-05 20:14:16 +0000334}
Chris Lattner72cdcac2009-10-21 06:24:21 +0000335
Eric Christopher8c47b422015-10-09 18:39:55 +0000336bool TargetInfo::initFeatureMap(
337 llvm::StringMap<bool> &Features, DiagnosticsEngine &Diags, StringRef CPU,
338 const std::vector<std::string> &FeatureVec) const {
Eric Christophere9cdcae2015-09-01 18:13:20 +0000339 for (const auto &F : FeatureVec) {
Craig Topper8459aa82015-10-21 16:31:31 +0000340 StringRef Name = F;
Eric Christophere9cdcae2015-09-01 18:13:20 +0000341 // Apply the feature via the target.
342 bool Enabled = Name[0] == '+';
Craig Topper8459aa82015-10-21 16:31:31 +0000343 setFeatureEnabled(Features, Name.substr(1), Enabled);
Eric Christophere9cdcae2015-09-01 18:13:20 +0000344 }
345 return true;
346}
347
Chris Lattnerec0a6d92007-09-22 18:29:59 +0000348//===----------------------------------------------------------------------===//
Chris Lattnerec0a6d92007-09-22 18:29:59 +0000349
Chris Lattner10a5b382007-01-29 05:24:35 +0000350
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000351static StringRef removeGCCRegisterPrefix(StringRef Name) {
Anders Carlsson3ed4aea2008-02-06 00:11:32 +0000352 if (Name[0] == '%' || Name[0] == '#')
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000353 Name = Name.substr(1);
Michael J. Spencerfeb799c2010-10-18 07:10:59 +0000354
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000355 return Name;
Anders Carlsson3ed4aea2008-02-06 00:11:32 +0000356}
357
Eric Christopherfd9a5f42011-06-28 18:20:53 +0000358/// isValidClobber - Returns whether the passed in string is
359/// a valid clobber in an inline asm statement. This is used by
360/// Sema.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000361bool TargetInfo::isValidClobber(StringRef Name) const {
Eric Christopherfd9a5f42011-06-28 18:20:53 +0000362 return (isValidGCCRegisterName(Name) ||
Eric Christopherb093d692015-10-09 18:39:59 +0000363 Name == "memory" || Name == "cc");
Eric Christopherfd9a5f42011-06-28 18:20:53 +0000364}
365
Anders Carlsson5fa3f342007-11-24 23:38:12 +0000366/// isValidGCCRegisterName - Returns whether the passed in string
367/// is a valid register name according to GCC. This is used by Sema for
368/// inline asm statements.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000369bool TargetInfo::isValidGCCRegisterName(StringRef Name) const {
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000370 if (Name.empty())
371 return false;
Michael J. Spencerfeb799c2010-10-18 07:10:59 +0000372
Anders Carlsson290aa852007-11-25 00:25:21 +0000373 // Get rid of any register prefix.
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000374 Name = removeGCCRegisterPrefix(Name);
Olivier Goffartfc8f8932014-08-17 13:19:48 +0000375 if (Name.empty())
Craig Topperde330c72015-10-21 04:52:34 +0000376 return false;
Anders Carlsson3ed4aea2008-02-06 00:11:32 +0000377
Craig Topperf054e3a2015-10-19 03:52:27 +0000378 ArrayRef<const char *> Names = getGCCRegNames();
Mike Stump11289f42009-09-09 15:08:12 +0000379
Anders Carlsson290aa852007-11-25 00:25:21 +0000380 // If we have a number it maps to an entry in the register name array.
Jordan Rosea7d03842013-02-08 22:30:41 +0000381 if (isDigit(Name[0])) {
Craig Topper67288a92015-10-21 04:52:36 +0000382 unsigned n;
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000383 if (!Name.getAsInteger(0, n))
Craig Topper67288a92015-10-21 04:52:36 +0000384 return n < Names.size();
Anders Carlsson290aa852007-11-25 00:25:21 +0000385 }
386
387 // Check register names.
Craig Topper335c8f92015-10-21 04:52:38 +0000388 if (std::find(Names.begin(), Names.end(), Name) != Names.end())
389 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000390
Eric Christophercdd36352011-06-21 00:05:20 +0000391 // Check any additional names that we have.
Craig Topperdd2b74a2015-10-21 04:52:40 +0000392 for (const AddlRegName &ARN : getGCCAddlRegNames())
393 for (const char *AN : ARN.Names) {
394 if (!AN)
Eric Christopherb093d692015-10-09 18:39:59 +0000395 break;
Eric Christophercdd36352011-06-21 00:05:20 +0000396 // Make sure the register that the additional name is for is within
397 // the bounds of the register names from above.
Craig Topperdd2b74a2015-10-21 04:52:40 +0000398 if (AN == Name && ARN.RegNum < Names.size())
Anders Carlsson290aa852007-11-25 00:25:21 +0000399 return true;
400 }
Craig Topperdd2b74a2015-10-21 04:52:40 +0000401
402 // Now check aliases.
403 for (const GCCRegAlias &GRA : getGCCRegAliases())
404 for (const char *A : GRA.Aliases) {
405 if (!A)
406 break;
407 if (A == Name)
408 return true;
409 }
Mike Stump11289f42009-09-09 15:08:12 +0000410
Anders Carlsson5fa3f342007-11-24 23:38:12 +0000411 return false;
412}
Anders Carlssonf511f642007-11-27 04:11:28 +0000413
Marina Yatsinac42fd032016-12-26 12:23:42 +0000414StringRef TargetInfo::getNormalizedGCCRegisterName(StringRef Name,
415 bool ReturnCanonical) const {
Anders Carlssonf511f642007-11-27 04:11:28 +0000416 assert(isValidGCCRegisterName(Name) && "Invalid register passed in");
Mike Stump11289f42009-09-09 15:08:12 +0000417
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000418 // Get rid of any register prefix.
419 Name = removeGCCRegisterPrefix(Name);
Mike Stump11289f42009-09-09 15:08:12 +0000420
Craig Topperf054e3a2015-10-19 03:52:27 +0000421 ArrayRef<const char *> Names = getGCCRegNames();
Anders Carlssonf511f642007-11-27 04:11:28 +0000422
423 // First, check if we have a number.
Jordan Rosea7d03842013-02-08 22:30:41 +0000424 if (isDigit(Name[0])) {
Craig Topper67288a92015-10-21 04:52:36 +0000425 unsigned n;
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000426 if (!Name.getAsInteger(0, n)) {
Craig Topper67288a92015-10-21 04:52:36 +0000427 assert(n < Names.size() && "Out of bounds register number!");
Anders Carlssonf511f642007-11-27 04:11:28 +0000428 return Names[n];
429 }
430 }
Mike Stump11289f42009-09-09 15:08:12 +0000431
Eric Christophercdd36352011-06-21 00:05:20 +0000432 // Check any additional names that we have.
Craig Topperdd2b74a2015-10-21 04:52:40 +0000433 for (const AddlRegName &ARN : getGCCAddlRegNames())
434 for (const char *AN : ARN.Names) {
435 if (!AN)
Eric Christopherb093d692015-10-09 18:39:59 +0000436 break;
Eric Christophercdd36352011-06-21 00:05:20 +0000437 // Make sure the register that the additional name is for is within
438 // the bounds of the register names from above.
Craig Topperdd2b74a2015-10-21 04:52:40 +0000439 if (AN == Name && ARN.RegNum < Names.size())
Marina Yatsinac42fd032016-12-26 12:23:42 +0000440 return ReturnCanonical ? Names[ARN.RegNum] : Name;
Eric Christophercdd36352011-06-21 00:05:20 +0000441 }
442
Anders Carlssonf511f642007-11-27 04:11:28 +0000443 // Now check aliases.
Craig Topperdd2b74a2015-10-21 04:52:40 +0000444 for (const GCCRegAlias &RA : getGCCRegAliases())
445 for (const char *A : RA.Aliases) {
446 if (!A)
Anders Carlssonf511f642007-11-27 04:11:28 +0000447 break;
Craig Topperdd2b74a2015-10-21 04:52:40 +0000448 if (A == Name)
449 return RA.Register;
Anders Carlssonf511f642007-11-27 04:11:28 +0000450 }
Mike Stump11289f42009-09-09 15:08:12 +0000451
Anders Carlssonf511f642007-11-27 04:11:28 +0000452 return Name;
453}
454
Chris Lattnerc3f4c7b2009-04-26 17:19:08 +0000455bool TargetInfo::validateOutputConstraint(ConstraintInfo &Info) const {
456 const char *Name = Info.getConstraintStr().c_str();
Anders Carlssonf511f642007-11-27 04:11:28 +0000457 // An output constraint must start with '=' or '+'
458 if (*Name != '=' && *Name != '+')
459 return false;
460
461 if (*Name == '+')
Chris Lattnerd9725f72009-04-26 07:16:29 +0000462 Info.setIsReadWrite();
Anders Carlssonf511f642007-11-27 04:11:28 +0000463
464 Name++;
465 while (*Name) {
466 switch (*Name) {
467 default:
Chris Lattnerd9725f72009-04-26 07:16:29 +0000468 if (!validateAsmConstraint(Name, Info)) {
Ted Kremenekb44485b2008-04-24 16:36:38 +0000469 // FIXME: We temporarily return false
Anders Carlssonf511f642007-11-27 04:11:28 +0000470 // so we can add more constraints as we hit it.
471 // Eventually, an unknown constraint should just be treated as 'g'.
Ted Kremenekb44485b2008-04-24 16:36:38 +0000472 return false;
Anders Carlssonf511f642007-11-27 04:11:28 +0000473 }
David Majnemera0040df2015-01-10 10:43:19 +0000474 break;
Anders Carlssonf511f642007-11-27 04:11:28 +0000475 case '&': // early clobber.
David Majnemera0040df2015-01-10 10:43:19 +0000476 Info.setEarlyClobber();
Anders Carlssonf511f642007-11-27 04:11:28 +0000477 break;
Chris Lattnerf3154712009-10-13 04:32:07 +0000478 case '%': // commutative.
479 // FIXME: Check that there is a another register after this one.
480 break;
Anders Carlssonf511f642007-11-27 04:11:28 +0000481 case 'r': // general register.
Chris Lattnerd9725f72009-04-26 07:16:29 +0000482 Info.setAllowsRegister();
Anders Carlssonf511f642007-11-27 04:11:28 +0000483 break;
484 case 'm': // memory operand.
Dale Johannesen8499f472010-09-07 18:40:41 +0000485 case 'o': // offsetable memory operand.
486 case 'V': // non-offsetable memory operand.
487 case '<': // autodecrement memory operand.
488 case '>': // autoincrement memory operand.
Chris Lattnerd9725f72009-04-26 07:16:29 +0000489 Info.setAllowsMemory();
Anders Carlssonf511f642007-11-27 04:11:28 +0000490 break;
491 case 'g': // general register, memory operand or immediate integer.
Anders Carlssone70cde12009-01-18 02:12:04 +0000492 case 'X': // any operand.
Chris Lattnerd9725f72009-04-26 07:16:29 +0000493 Info.setAllowsRegister();
494 Info.setAllowsMemory();
Anders Carlssonf511f642007-11-27 04:11:28 +0000495 break;
John Thompsona5c7d702010-08-10 19:20:14 +0000496 case ',': // multiple alternative constraint. Pass it.
John Thompson46667af2010-08-11 00:58:20 +0000497 // Handle additional optional '=' or '+' modifiers.
John Thompson12240612010-09-18 01:15:13 +0000498 if (Name[1] == '=' || Name[1] == '+')
John Thompson46667af2010-08-11 00:58:20 +0000499 Name++;
John Thompsona5c7d702010-08-10 19:20:14 +0000500 break;
David Majnemer50cb0552015-01-11 08:52:38 +0000501 case '#': // Ignore as constraint.
502 while (Name[1] && Name[1] != ',')
503 Name++;
David Majnemerc71a5662015-01-11 09:39:03 +0000504 break;
John Thompsona5c7d702010-08-10 19:20:14 +0000505 case '?': // Disparage slightly code.
Dale Johannesen8499f472010-09-07 18:40:41 +0000506 case '!': // Disparage severely.
Ulrich Weigand7bcc7ec2012-10-29 12:20:54 +0000507 case '*': // Ignore for choosing register preferences.
John Thompsona5c7d702010-08-10 19:20:14 +0000508 break; // Pass them.
Anders Carlssonf511f642007-11-27 04:11:28 +0000509 }
Mike Stump11289f42009-09-09 15:08:12 +0000510
Anders Carlssonf511f642007-11-27 04:11:28 +0000511 Name++;
512 }
Mike Stump11289f42009-09-09 15:08:12 +0000513
David Majnemera0040df2015-01-10 10:43:19 +0000514 // Early clobber with a read-write constraint which doesn't permit registers
515 // is invalid.
516 if (Info.earlyClobber() && Info.isReadWrite() && !Info.allowsRegister())
517 return false;
518
Benjamin Kramer1e4a8862013-04-18 22:49:48 +0000519 // If a constraint allows neither memory nor register operands it contains
520 // only modifiers. Reject it.
Benjamin Kramer7ee3b9c2013-04-18 13:23:23 +0000521 return Info.allowsMemory() || Info.allowsRegister();
Anders Carlssonf511f642007-11-27 04:11:28 +0000522}
523
Anders Carlssona79203b2009-01-18 01:56:57 +0000524bool TargetInfo::resolveSymbolicName(const char *&Name,
Craig Topper55765ca2015-10-21 02:34:10 +0000525 ArrayRef<ConstraintInfo> OutputConstraints,
Chris Lattnerd9725f72009-04-26 07:16:29 +0000526 unsigned &Index) const {
Anders Carlssona79203b2009-01-18 01:56:57 +0000527 assert(*Name == '[' && "Symbolic name did not start with '['");
Anders Carlssona79203b2009-01-18 01:56:57 +0000528 Name++;
529 const char *Start = Name;
530 while (*Name && *Name != ']')
531 Name++;
Mike Stump11289f42009-09-09 15:08:12 +0000532
Anders Carlssona79203b2009-01-18 01:56:57 +0000533 if (!*Name) {
534 // Missing ']'
535 return false;
536 }
Mike Stump11289f42009-09-09 15:08:12 +0000537
Anders Carlssona79203b2009-01-18 01:56:57 +0000538 std::string SymbolicName(Start, Name - Start);
Mike Stump11289f42009-09-09 15:08:12 +0000539
Craig Topper55765ca2015-10-21 02:34:10 +0000540 for (Index = 0; Index != OutputConstraints.size(); ++Index)
Chris Lattnerc16d4762009-04-26 17:57:12 +0000541 if (SymbolicName == OutputConstraints[Index].getName())
Anders Carlssona79203b2009-01-18 01:56:57 +0000542 return true;
Anders Carlssona79203b2009-01-18 01:56:57 +0000543
544 return false;
545}
546
Craig Topper55765ca2015-10-21 02:34:10 +0000547bool TargetInfo::validateInputConstraint(
548 MutableArrayRef<ConstraintInfo> OutputConstraints,
549 ConstraintInfo &Info) const {
Chris Lattnerc3f4c7b2009-04-26 17:19:08 +0000550 const char *Name = Info.ConstraintStr.c_str();
551
Duncan P. N. Exon Smithd68c7aa2013-12-16 03:20:06 +0000552 if (!*Name)
553 return false;
554
Anders Carlssonf511f642007-11-27 04:11:28 +0000555 while (*Name) {
556 switch (*Name) {
557 default:
558 // Check if we have a matching constraint
559 if (*Name >= '0' && *Name <= '9') {
David Majnemerb6b56432015-01-11 10:22:41 +0000560 const char *DigitStart = Name;
561 while (Name[1] >= '0' && Name[1] <= '9')
562 Name++;
563 const char *DigitEnd = Name;
564 unsigned i;
565 if (StringRef(DigitStart, DigitEnd - DigitStart + 1)
566 .getAsInteger(10, i))
567 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000568
Anders Carlssonf511f642007-11-27 04:11:28 +0000569 // Check if matching constraint is out of bounds.
Craig Topper55765ca2015-10-21 02:34:10 +0000570 if (i >= OutputConstraints.size()) return false;
Mike Stump11289f42009-09-09 15:08:12 +0000571
Anders Carlssonda1f5fc2010-11-03 02:22:29 +0000572 // A number must refer to an output only operand.
573 if (OutputConstraints[i].isReadWrite())
574 return false;
575
Eric Christopherb093d692015-10-09 18:39:59 +0000576 // If the constraint is already tied, it must be tied to the
Anders Carlsson2d5f8b42010-11-03 02:54:51 +0000577 // same operand referenced to by the number.
578 if (Info.hasTiedOperand() && Info.getTiedOperand() != i)
579 return false;
580
Mike Stump11289f42009-09-09 15:08:12 +0000581 // The constraint should have the same info as the respective
Anders Carlsson570c3572009-01-27 20:38:24 +0000582 // output constraint.
Chris Lattner4c92b512009-04-26 18:05:25 +0000583 Info.setTiedOperand(i, OutputConstraints[i]);
Chris Lattnerd9725f72009-04-26 07:16:29 +0000584 } else if (!validateAsmConstraint(Name, Info)) {
Daniel Dunbar81128e02008-08-25 09:46:27 +0000585 // FIXME: This error return is in place temporarily so we can
586 // add more constraints as we hit it. Eventually, an unknown
587 // constraint should just be treated as 'g'.
588 return false;
Anders Carlssona79203b2009-01-18 01:56:57 +0000589 }
590 break;
591 case '[': {
592 unsigned Index = 0;
Craig Topper55765ca2015-10-21 02:34:10 +0000593 if (!resolveSymbolicName(Name, OutputConstraints, Index))
Anders Carlssona79203b2009-01-18 01:56:57 +0000594 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000595
Eric Christopherb093d692015-10-09 18:39:59 +0000596 // If the constraint is already tied, it must be tied to the
Anders Carlsson2d5f8b42010-11-03 02:54:51 +0000597 // same operand referenced to by the number.
598 if (Info.hasTiedOperand() && Info.getTiedOperand() != Index)
599 return false;
600
David Majnemer55164f92015-01-11 09:57:13 +0000601 // A number must refer to an output only operand.
602 if (OutputConstraints[Index].isReadWrite())
603 return false;
604
Eli Friedman854f1102010-08-11 23:03:37 +0000605 Info.setTiedOperand(Index, OutputConstraints[Index]);
Anders Carlssona79203b2009-01-18 01:56:57 +0000606 break;
Mike Stump11289f42009-09-09 15:08:12 +0000607 }
Anders Carlsson050f4942007-12-08 19:32:57 +0000608 case '%': // commutative
609 // FIXME: Fail if % is used with the last operand.
610 break;
Anders Carlssonf511f642007-11-27 04:11:28 +0000611 case 'i': // immediate integer.
Anders Carlssona3a96af2008-03-09 06:02:02 +0000612 case 'n': // immediate integer with a known value.
Anders Carlssonf511f642007-11-27 04:11:28 +0000613 break;
Chris Lattnerdbcc5ca2009-05-06 04:33:31 +0000614 case 'I': // Various constant constraints with target-specific meanings.
615 case 'J':
616 case 'K':
617 case 'L':
618 case 'M':
619 case 'N':
620 case 'O':
621 case 'P':
Saleem Abdulrasoola2823572015-01-06 04:26:34 +0000622 if (!validateAsmConstraint(Name, Info))
623 return false;
Chris Lattnerdbcc5ca2009-05-06 04:33:31 +0000624 break;
Anders Carlssonf511f642007-11-27 04:11:28 +0000625 case 'r': // general register.
Chris Lattnerd9725f72009-04-26 07:16:29 +0000626 Info.setAllowsRegister();
Anders Carlssonf511f642007-11-27 04:11:28 +0000627 break;
628 case 'm': // memory operand.
Dale Johannesen8499f472010-09-07 18:40:41 +0000629 case 'o': // offsettable memory operand.
630 case 'V': // non-offsettable memory operand.
631 case '<': // autodecrement memory operand.
632 case '>': // autoincrement memory operand.
Chris Lattnerd9725f72009-04-26 07:16:29 +0000633 Info.setAllowsMemory();
Anders Carlssonf511f642007-11-27 04:11:28 +0000634 break;
635 case 'g': // general register, memory operand or immediate integer.
Anders Carlssone70cde12009-01-18 02:12:04 +0000636 case 'X': // any operand.
Chris Lattnerd9725f72009-04-26 07:16:29 +0000637 Info.setAllowsRegister();
638 Info.setAllowsMemory();
Anders Carlssonf511f642007-11-27 04:11:28 +0000639 break;
John Thompsonc467aa22010-09-21 22:04:54 +0000640 case 'E': // immediate floating point.
641 case 'F': // immediate floating point.
642 case 'p': // address operand.
643 break;
John Thompsona5c7d702010-08-10 19:20:14 +0000644 case ',': // multiple alternative constraint. Ignore comma.
645 break;
David Majnemerc71a5662015-01-11 09:39:03 +0000646 case '#': // Ignore as constraint.
647 while (Name[1] && Name[1] != ',')
648 Name++;
David Majnemerc71a5662015-01-11 09:39:03 +0000649 break;
John Thompsona5c7d702010-08-10 19:20:14 +0000650 case '?': // Disparage slightly code.
Chris Lattner57540c52011-04-15 05:22:18 +0000651 case '!': // Disparage severely.
Ulrich Weigand7bcc7ec2012-10-29 12:20:54 +0000652 case '*': // Ignore for choosing register preferences.
John Thompsona5c7d702010-08-10 19:20:14 +0000653 break; // Pass them.
Anders Carlssonf511f642007-11-27 04:11:28 +0000654 }
Mike Stump11289f42009-09-09 15:08:12 +0000655
Anders Carlssonf511f642007-11-27 04:11:28 +0000656 Name++;
657 }
Mike Stump11289f42009-09-09 15:08:12 +0000658
Anders Carlssonf511f642007-11-27 04:11:28 +0000659 return true;
660}