blob: d455fa3956dd9934cbb1f96f5a0a3ee357eef72a [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;
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +000042 HalfWidth = 16;
43 HalfAlign = 16;
Eli Friedmanb5366062008-05-20 14:21:01 +000044 FloatWidth = 32;
45 FloatAlign = 32;
Nate Begeman65bea232008-04-18 17:17:24 +000046 DoubleWidth = 64;
Eli Friedmanb5366062008-05-20 14:21:01 +000047 DoubleAlign = 64;
48 LongDoubleWidth = 64;
49 LongDoubleAlign = 64;
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +000050 Float128Align = 128;
Rafael Espindolae971b9a2010-06-04 23:15:27 +000051 LargeArrayMinWidth = 0;
52 LargeArrayAlign = 0;
Eli Friedman4b72fdd2011-10-14 20:59:01 +000053 MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 0;
Chad Rosiercc40ea72012-07-13 23:57:43 +000054 MaxVectorAlign = 0;
Paul Robinsond30e2ee2015-07-14 20:52:32 +000055 MaxTLSAlign = 0;
Alexey Bataev00396512015-07-02 03:40:19 +000056 SimdDefaultAlign = 0;
Anders Carlsson2a79a902008-10-31 16:05:19 +000057 SizeType = UnsignedLong;
Eli Friedmand50881c2008-11-02 02:43:55 +000058 PtrDiffType = SignedLong;
Sanjiv Guptad7959242008-10-31 09:52:39 +000059 IntMaxType = SignedLongLong;
Chris Lattner7e4c81c2009-02-13 22:28:55 +000060 IntPtrType = SignedLong;
Eli Friedmand50881c2008-11-02 02:43:55 +000061 WCharType = SignedInt;
Chris Lattner67204922009-10-21 04:59:34 +000062 WIntType = SignedInt;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +000063 Char16Type = UnsignedShort;
64 Char32Type = UnsignedInt;
Eli Friedman2857ccb2009-07-01 03:36:11 +000065 Int64Type = SignedLongLong;
Edward O'Callaghan847f2a12009-11-21 00:49:54 +000066 SigAtomicType = SignedInt;
Eli Friedman4e91899e2012-11-27 02:58:24 +000067 ProcessIDType = SignedInt;
Fariborz Jahanian29898f42012-04-16 21:03:30 +000068 UseSignedCharForObjCBool = true;
Daniel Dunbar1da65112010-04-15 15:06:18 +000069 UseBitFieldTypeAlignment = true;
Chad Rosier18903ee2011-08-04 01:21:14 +000070 UseZeroLengthBitfieldAlignment = false;
Sunil Srivastava0ce2f222016-02-05 20:50:02 +000071 UseExplicitBitFieldAlignment = true;
Chad Rosier18903ee2011-08-04 01:21:14 +000072 ZeroLengthBitfieldBoundary = 0;
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +000073 HalfFormat = &llvm::APFloat::IEEEhalf;
Chris Lattner1df27862008-03-08 08:59:43 +000074 FloatFormat = &llvm::APFloat::IEEEsingle;
75 DoubleFormat = &llvm::APFloat::IEEEdouble;
76 LongDoubleFormat = &llvm::APFloat::IEEEdouble;
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +000077 Float128Format = &llvm::APFloat::IEEEquad;
Roman Divacky178e01602011-02-10 16:52:03 +000078 MCountName = "mcount";
Abramo Bagnara41bfb662011-09-08 14:20:25 +000079 RegParmMax = 0;
80 SSERegParmMax = 0;
Daniel Dunbarbd606522010-05-27 00:35:16 +000081 HasAlignMac68kSupport = false;
Charles Davisc7d5c942015-09-17 20:55:33 +000082 HasBuiltinMSVaList = false;
Pirama Arumuga Nainarbb846a32016-07-27 19:01:51 +000083 IsRenderScriptTarget = false;
Daniel Dunbar6f2e8392010-07-14 23:39:36 +000084
85 // Default to no types using fpret.
86 RealTypeUsesObjCFPRet = 0;
John McCall86353412010-08-21 22:46:04 +000087
Anders Carlsson2f1a6c32011-10-31 16:27:11 +000088 // Default to not using fp2ret for __Complex long double
89 ComplexLongDoubleUsesFP2Ret = false;
90
Hans Wennborgc9bd88e2014-01-14 19:35:09 +000091 // Set the C++ ABI based on the triple.
Saleem Abdulrasool377066a2014-03-27 22:50:18 +000092 TheCXXABI.set(Triple.isKnownWindowsMSVCEnvironment()
Hans Wennborgc9bd88e2014-01-14 19:35:09 +000093 ? TargetCXXABI::Microsoft
94 : TargetCXXABI::GenericItanium);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +000095
96 // Default to an empty address space map.
97 AddrSpaceMap = &DefaultAddrSpaceMap;
David Tweed31d09b02013-09-13 12:04:22 +000098 UseAddrSpaceMapMangling = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +000099
100 // Default to an unknown platform name.
101 PlatformName = "unknown";
102 PlatformMinVersion = VersionTuple();
Chris Lattner1df27862008-03-08 08:59:43 +0000103}
104
Chris Lattnerc3a669b2008-03-08 08:24:01 +0000105// Out of line virtual dtor for TargetInfo.
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +0000106TargetInfo::~TargetInfo() {}
Chris Lattner2194ddc2006-10-14 18:32:26 +0000107
Chris Lattnera91c30f2009-02-06 05:04:11 +0000108/// getTypeName - Return the user string for the specified integer type enum.
109/// For example, SignedShort -> "short".
110const char *TargetInfo::getTypeName(IntType T) {
111 switch (T) {
David Blaikie83d382b2011-09-23 05:06:16 +0000112 default: llvm_unreachable("not an integer!");
Joerg Sonnenberger3d9478c2014-07-28 21:06:22 +0000113 case SignedChar: return "signed char";
Stepan Dyatkovskiy5a637922013-09-05 11:23:21 +0000114 case UnsignedChar: return "unsigned char";
Chris Lattnera91c30f2009-02-06 05:04:11 +0000115 case SignedShort: return "short";
116 case UnsignedShort: return "unsigned short";
117 case SignedInt: return "int";
118 case UnsignedInt: return "unsigned int";
119 case SignedLong: return "long int";
120 case UnsignedLong: return "long unsigned int";
121 case SignedLongLong: return "long long int";
122 case UnsignedLongLong: return "long long unsigned int";
123 }
124}
125
Chris Lattner72cdcac2009-10-21 06:24:21 +0000126/// getTypeConstantSuffix - Return the constant suffix for the specified
127/// integer type enum. For example, SignedLong -> "L".
Joerg Sonnenberger587deea2014-07-17 20:12:32 +0000128const char *TargetInfo::getTypeConstantSuffix(IntType T) const {
Chris Lattner72cdcac2009-10-21 06:24:21 +0000129 switch (T) {
David Blaikie83d382b2011-09-23 05:06:16 +0000130 default: llvm_unreachable("not an integer!");
Stepan Dyatkovskiy5a637922013-09-05 11:23:21 +0000131 case SignedChar:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000132 case SignedShort:
133 case SignedInt: return "";
134 case SignedLong: return "L";
135 case SignedLongLong: return "LL";
Stepan Dyatkovskiy5a637922013-09-05 11:23:21 +0000136 case UnsignedChar:
Joerg Sonnenberger587deea2014-07-17 20:12:32 +0000137 if (getCharWidth() < getIntWidth())
138 return "";
Chris Lattner72cdcac2009-10-21 06:24:21 +0000139 case UnsignedShort:
Joerg Sonnenberger587deea2014-07-17 20:12:32 +0000140 if (getShortWidth() < getIntWidth())
141 return "";
Chris Lattner72cdcac2009-10-21 06:24:21 +0000142 case UnsignedInt: return "U";
143 case UnsignedLong: return "UL";
144 case UnsignedLongLong: return "ULL";
145 }
146}
147
Joerg Sonnenbergerbe324f92014-07-15 11:30:00 +0000148/// getTypeFormatModifier - Return the printf format modifier for the
149/// specified integer type enum. For example, SignedLong -> "l".
150
151const char *TargetInfo::getTypeFormatModifier(IntType T) {
152 switch (T) {
153 default: llvm_unreachable("not an integer!");
154 case SignedChar:
155 case UnsignedChar: return "hh";
156 case SignedShort:
157 case UnsignedShort: return "h";
158 case SignedInt:
159 case UnsignedInt: return "";
160 case SignedLong:
161 case UnsignedLong: return "l";
162 case SignedLongLong:
163 case UnsignedLongLong: return "ll";
164 }
165}
166
Michael J. Spencerfeb799c2010-10-18 07:10:59 +0000167/// getTypeWidth - Return the width (in bits) of the specified integer type
Chris Lattner72cdcac2009-10-21 06:24:21 +0000168/// enum. For example, SignedInt -> getIntWidth().
169unsigned TargetInfo::getTypeWidth(IntType T) const {
170 switch (T) {
David Blaikie83d382b2011-09-23 05:06:16 +0000171 default: llvm_unreachable("not an integer!");
Stepan Dyatkovskiy5a637922013-09-05 11:23:21 +0000172 case SignedChar:
173 case UnsignedChar: return getCharWidth();
Chris Lattnere4a8c642009-11-05 21:21:32 +0000174 case SignedShort:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000175 case UnsignedShort: return getShortWidth();
Chris Lattnere4a8c642009-11-05 21:21:32 +0000176 case SignedInt:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000177 case UnsignedInt: return getIntWidth();
Chris Lattnere4a8c642009-11-05 21:21:32 +0000178 case SignedLong:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000179 case UnsignedLong: return getLongWidth();
Chris Lattnere4a8c642009-11-05 21:21:32 +0000180 case SignedLongLong:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000181 case UnsignedLongLong: return getLongLongWidth();
182 };
183}
184
Stepan Dyatkovskiy5a637922013-09-05 11:23:21 +0000185TargetInfo::IntType TargetInfo::getIntTypeByWidth(
186 unsigned BitWidth, bool IsSigned) const {
187 if (getCharWidth() == BitWidth)
188 return IsSigned ? SignedChar : UnsignedChar;
189 if (getShortWidth() == BitWidth)
190 return IsSigned ? SignedShort : UnsignedShort;
191 if (getIntWidth() == BitWidth)
192 return IsSigned ? SignedInt : UnsignedInt;
193 if (getLongWidth() == BitWidth)
194 return IsSigned ? SignedLong : UnsignedLong;
195 if (getLongLongWidth() == BitWidth)
196 return IsSigned ? SignedLongLong : UnsignedLongLong;
197 return NoInt;
198}
199
JF Bastienab8d0a02014-06-25 01:31:33 +0000200TargetInfo::IntType TargetInfo::getLeastIntTypeByWidth(unsigned BitWidth,
201 bool IsSigned) const {
202 if (getCharWidth() >= BitWidth)
203 return IsSigned ? SignedChar : UnsignedChar;
204 if (getShortWidth() >= BitWidth)
205 return IsSigned ? SignedShort : UnsignedShort;
206 if (getIntWidth() >= BitWidth)
207 return IsSigned ? SignedInt : UnsignedInt;
208 if (getLongWidth() >= BitWidth)
209 return IsSigned ? SignedLong : UnsignedLong;
210 if (getLongLongWidth() >= BitWidth)
211 return IsSigned ? SignedLongLong : UnsignedLongLong;
212 return NoInt;
213}
214
Stepan Dyatkovskiy5a637922013-09-05 11:23:21 +0000215TargetInfo::RealType TargetInfo::getRealTypeByWidth(unsigned BitWidth) const {
216 if (getFloatWidth() == BitWidth)
217 return Float;
218 if (getDoubleWidth() == BitWidth)
219 return Double;
220
221 switch (BitWidth) {
222 case 96:
223 if (&getLongDoubleFormat() == &llvm::APFloat::x87DoubleExtended)
224 return LongDouble;
225 break;
226 case 128:
Stepan Dyatkovskiya4a59d72013-09-09 07:46:54 +0000227 if (&getLongDoubleFormat() == &llvm::APFloat::PPCDoubleDouble ||
228 &getLongDoubleFormat() == &llvm::APFloat::IEEEquad)
Stepan Dyatkovskiy5a637922013-09-05 11:23:21 +0000229 return LongDouble;
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +0000230 if (hasFloat128Type())
231 return Float128;
Stepan Dyatkovskiy5a637922013-09-05 11:23:21 +0000232 break;
233 }
234
235 return NoFloat;
236}
237
Michael J. Spencerfeb799c2010-10-18 07:10:59 +0000238/// getTypeAlign - Return the alignment (in bits) of the specified integer type
Chris Lattnere4a8c642009-11-05 21:21:32 +0000239/// enum. For example, SignedInt -> getIntAlign().
240unsigned TargetInfo::getTypeAlign(IntType T) const {
241 switch (T) {
David Blaikie83d382b2011-09-23 05:06:16 +0000242 default: llvm_unreachable("not an integer!");
Stepan Dyatkovskiy5a637922013-09-05 11:23:21 +0000243 case SignedChar:
244 case UnsignedChar: return getCharAlign();
Chris Lattnere4a8c642009-11-05 21:21:32 +0000245 case SignedShort:
246 case UnsignedShort: return getShortAlign();
247 case SignedInt:
248 case UnsignedInt: return getIntAlign();
249 case SignedLong:
250 case UnsignedLong: return getLongAlign();
251 case SignedLongLong:
252 case UnsignedLongLong: return getLongLongAlign();
253 };
254}
255
Chris Lattnerc0c04392009-10-25 22:49:18 +0000256/// isTypeSigned - Return whether an integer types is signed. Returns true if
Chris Lattner72cdcac2009-10-21 06:24:21 +0000257/// the type is signed; false otherwise.
Chris Lattnerad3467e2010-12-25 23:25:43 +0000258bool TargetInfo::isTypeSigned(IntType T) {
Chris Lattner72cdcac2009-10-21 06:24:21 +0000259 switch (T) {
David Blaikie83d382b2011-09-23 05:06:16 +0000260 default: llvm_unreachable("not an integer!");
Stepan Dyatkovskiy5a637922013-09-05 11:23:21 +0000261 case SignedChar:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000262 case SignedShort:
263 case SignedInt:
264 case SignedLong:
Michael J. Spencerfeb799c2010-10-18 07:10:59 +0000265 case SignedLongLong:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000266 return true;
Stepan Dyatkovskiy5a637922013-09-05 11:23:21 +0000267 case UnsignedChar:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000268 case UnsignedShort:
269 case UnsignedInt:
270 case UnsignedLong:
Michael J. Spencerfeb799c2010-10-18 07:10:59 +0000271 case UnsignedLongLong:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000272 return false;
273 };
274}
275
Alp Toker74437972014-07-06 05:14:24 +0000276/// adjust - Set forced language options.
John Thompsoned4e2952009-11-05 20:14:16 +0000277/// Apply changes to the target information with respect to certain
278/// language options which change the target configuration.
Alp Toker74437972014-07-06 05:14:24 +0000279void TargetInfo::adjust(const LangOptions &Opts) {
Daniel Dunbar9302f602010-04-15 15:06:22 +0000280 if (Opts.NoBitFieldTypeAlign)
281 UseBitFieldTypeAlignment = false;
282 if (Opts.ShortWChar)
John Thompsoned4e2952009-11-05 20:14:16 +0000283 WCharType = UnsignedShort;
Reid Kleckner8195f692016-05-04 02:58:24 +0000284 if (Opts.AlignDouble) {
285 DoubleAlign = LongLongAlign = 64;
286 LongDoubleAlign = 64;
287 }
David Tweed2da64382013-09-09 09:17:24 +0000288
289 if (Opts.OpenCL) {
290 // OpenCL C requires specific widths for types, irrespective of
291 // what these normally are for the target.
292 // We also define long long and long double here, although the
293 // OpenCL standard only mentions these as "reserved".
294 IntWidth = IntAlign = 32;
295 LongWidth = LongAlign = 64;
296 LongLongWidth = LongLongAlign = 128;
297 HalfWidth = HalfAlign = 16;
298 FloatWidth = FloatAlign = 32;
Eric Christopherb093d692015-10-09 18:39:59 +0000299
300 // Embedded 32-bit targets (OpenCL EP) might have double C type
301 // defined as float. Let's not override this as it might lead
Pekka Jaaskelainen43c39d52013-12-18 18:15:03 +0000302 // to generating illegal code that uses 64bit doubles.
303 if (DoubleWidth != FloatWidth) {
304 DoubleWidth = DoubleAlign = 64;
305 DoubleFormat = &llvm::APFloat::IEEEdouble;
306 }
David Tweed2da64382013-09-09 09:17:24 +0000307 LongDoubleWidth = LongDoubleAlign = 128;
308
Yaxun Liu26f75662016-08-19 05:17:25 +0000309 unsigned MaxPointerWidth = getMaxPointerWidth();
310 assert(MaxPointerWidth == 32 || MaxPointerWidth == 64);
311 bool Is32BitArch = MaxPointerWidth == 32;
David Tweed6c2149b2013-09-10 08:00:34 +0000312 SizeType = Is32BitArch ? UnsignedInt : UnsignedLong;
313 PtrDiffType = Is32BitArch ? SignedInt : SignedLong;
314 IntPtrType = Is32BitArch ? SignedInt : SignedLong;
David Tweed2da64382013-09-09 09:17:24 +0000315
316 IntMaxType = SignedLongLong;
David Tweed2da64382013-09-09 09:17:24 +0000317 Int64Type = SignedLong;
318
319 HalfFormat = &llvm::APFloat::IEEEhalf;
320 FloatFormat = &llvm::APFloat::IEEEsingle;
David Tweed2da64382013-09-09 09:17:24 +0000321 LongDoubleFormat = &llvm::APFloat::IEEEquad;
322 }
John Thompsoned4e2952009-11-05 20:14:16 +0000323}
Chris Lattner72cdcac2009-10-21 06:24:21 +0000324
Eric Christopher8c47b422015-10-09 18:39:55 +0000325bool TargetInfo::initFeatureMap(
326 llvm::StringMap<bool> &Features, DiagnosticsEngine &Diags, StringRef CPU,
327 const std::vector<std::string> &FeatureVec) const {
Eric Christophere9cdcae2015-09-01 18:13:20 +0000328 for (const auto &F : FeatureVec) {
Craig Topper8459aa82015-10-21 16:31:31 +0000329 StringRef Name = F;
Eric Christophere9cdcae2015-09-01 18:13:20 +0000330 // Apply the feature via the target.
331 bool Enabled = Name[0] == '+';
Craig Topper8459aa82015-10-21 16:31:31 +0000332 setFeatureEnabled(Features, Name.substr(1), Enabled);
Eric Christophere9cdcae2015-09-01 18:13:20 +0000333 }
334 return true;
335}
336
Chris Lattnerec0a6d92007-09-22 18:29:59 +0000337//===----------------------------------------------------------------------===//
Chris Lattnerec0a6d92007-09-22 18:29:59 +0000338
Chris Lattner10a5b382007-01-29 05:24:35 +0000339
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000340static StringRef removeGCCRegisterPrefix(StringRef Name) {
Anders Carlsson3ed4aea2008-02-06 00:11:32 +0000341 if (Name[0] == '%' || Name[0] == '#')
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000342 Name = Name.substr(1);
Michael J. Spencerfeb799c2010-10-18 07:10:59 +0000343
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000344 return Name;
Anders Carlsson3ed4aea2008-02-06 00:11:32 +0000345}
346
Eric Christopherfd9a5f42011-06-28 18:20:53 +0000347/// isValidClobber - Returns whether the passed in string is
348/// a valid clobber in an inline asm statement. This is used by
349/// Sema.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000350bool TargetInfo::isValidClobber(StringRef Name) const {
Eric Christopherfd9a5f42011-06-28 18:20:53 +0000351 return (isValidGCCRegisterName(Name) ||
Eric Christopherb093d692015-10-09 18:39:59 +0000352 Name == "memory" || Name == "cc");
Eric Christopherfd9a5f42011-06-28 18:20:53 +0000353}
354
Anders Carlsson5fa3f342007-11-24 23:38:12 +0000355/// isValidGCCRegisterName - Returns whether the passed in string
356/// is a valid register name according to GCC. This is used by Sema for
357/// inline asm statements.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000358bool TargetInfo::isValidGCCRegisterName(StringRef Name) const {
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000359 if (Name.empty())
360 return false;
Michael J. Spencerfeb799c2010-10-18 07:10:59 +0000361
Anders Carlsson290aa852007-11-25 00:25:21 +0000362 // Get rid of any register prefix.
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000363 Name = removeGCCRegisterPrefix(Name);
Olivier Goffartfc8f8932014-08-17 13:19:48 +0000364 if (Name.empty())
Craig Topperde330c72015-10-21 04:52:34 +0000365 return false;
Anders Carlsson3ed4aea2008-02-06 00:11:32 +0000366
Craig Topperf054e3a2015-10-19 03:52:27 +0000367 ArrayRef<const char *> Names = getGCCRegNames();
Mike Stump11289f42009-09-09 15:08:12 +0000368
Anders Carlsson290aa852007-11-25 00:25:21 +0000369 // If we have a number it maps to an entry in the register name array.
Jordan Rosea7d03842013-02-08 22:30:41 +0000370 if (isDigit(Name[0])) {
Craig Topper67288a92015-10-21 04:52:36 +0000371 unsigned n;
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000372 if (!Name.getAsInteger(0, n))
Craig Topper67288a92015-10-21 04:52:36 +0000373 return n < Names.size();
Anders Carlsson290aa852007-11-25 00:25:21 +0000374 }
375
376 // Check register names.
Craig Topper335c8f92015-10-21 04:52:38 +0000377 if (std::find(Names.begin(), Names.end(), Name) != Names.end())
378 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000379
Eric Christophercdd36352011-06-21 00:05:20 +0000380 // Check any additional names that we have.
Craig Topperdd2b74a2015-10-21 04:52:40 +0000381 for (const AddlRegName &ARN : getGCCAddlRegNames())
382 for (const char *AN : ARN.Names) {
383 if (!AN)
Eric Christopherb093d692015-10-09 18:39:59 +0000384 break;
Eric Christophercdd36352011-06-21 00:05:20 +0000385 // Make sure the register that the additional name is for is within
386 // the bounds of the register names from above.
Craig Topperdd2b74a2015-10-21 04:52:40 +0000387 if (AN == Name && ARN.RegNum < Names.size())
Anders Carlsson290aa852007-11-25 00:25:21 +0000388 return true;
389 }
Craig Topperdd2b74a2015-10-21 04:52:40 +0000390
391 // Now check aliases.
392 for (const GCCRegAlias &GRA : getGCCRegAliases())
393 for (const char *A : GRA.Aliases) {
394 if (!A)
395 break;
396 if (A == Name)
397 return true;
398 }
Mike Stump11289f42009-09-09 15:08:12 +0000399
Anders Carlsson5fa3f342007-11-24 23:38:12 +0000400 return false;
401}
Anders Carlssonf511f642007-11-27 04:11:28 +0000402
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000403StringRef
404TargetInfo::getNormalizedGCCRegisterName(StringRef Name) const {
Anders Carlssonf511f642007-11-27 04:11:28 +0000405 assert(isValidGCCRegisterName(Name) && "Invalid register passed in");
Mike Stump11289f42009-09-09 15:08:12 +0000406
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000407 // Get rid of any register prefix.
408 Name = removeGCCRegisterPrefix(Name);
Mike Stump11289f42009-09-09 15:08:12 +0000409
Craig Topperf054e3a2015-10-19 03:52:27 +0000410 ArrayRef<const char *> Names = getGCCRegNames();
Anders Carlssonf511f642007-11-27 04:11:28 +0000411
412 // First, check if we have a number.
Jordan Rosea7d03842013-02-08 22:30:41 +0000413 if (isDigit(Name[0])) {
Craig Topper67288a92015-10-21 04:52:36 +0000414 unsigned n;
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000415 if (!Name.getAsInteger(0, n)) {
Craig Topper67288a92015-10-21 04:52:36 +0000416 assert(n < Names.size() && "Out of bounds register number!");
Anders Carlssonf511f642007-11-27 04:11:28 +0000417 return Names[n];
418 }
419 }
Mike Stump11289f42009-09-09 15:08:12 +0000420
Eric Christophercdd36352011-06-21 00:05:20 +0000421 // Check any additional names that we have.
Craig Topperdd2b74a2015-10-21 04:52:40 +0000422 for (const AddlRegName &ARN : getGCCAddlRegNames())
423 for (const char *AN : ARN.Names) {
424 if (!AN)
Eric Christopherb093d692015-10-09 18:39:59 +0000425 break;
Eric Christophercdd36352011-06-21 00:05:20 +0000426 // Make sure the register that the additional name is for is within
427 // the bounds of the register names from above.
Craig Topperdd2b74a2015-10-21 04:52:40 +0000428 if (AN == Name && ARN.RegNum < Names.size())
Eric Christopherb093d692015-10-09 18:39:59 +0000429 return Name;
Eric Christophercdd36352011-06-21 00:05:20 +0000430 }
431
Anders Carlssonf511f642007-11-27 04:11:28 +0000432 // Now check aliases.
Craig Topperdd2b74a2015-10-21 04:52:40 +0000433 for (const GCCRegAlias &RA : getGCCRegAliases())
434 for (const char *A : RA.Aliases) {
435 if (!A)
Anders Carlssonf511f642007-11-27 04:11:28 +0000436 break;
Craig Topperdd2b74a2015-10-21 04:52:40 +0000437 if (A == Name)
438 return RA.Register;
Anders Carlssonf511f642007-11-27 04:11:28 +0000439 }
Mike Stump11289f42009-09-09 15:08:12 +0000440
Anders Carlssonf511f642007-11-27 04:11:28 +0000441 return Name;
442}
443
Chris Lattnerc3f4c7b2009-04-26 17:19:08 +0000444bool TargetInfo::validateOutputConstraint(ConstraintInfo &Info) const {
445 const char *Name = Info.getConstraintStr().c_str();
Anders Carlssonf511f642007-11-27 04:11:28 +0000446 // An output constraint must start with '=' or '+'
447 if (*Name != '=' && *Name != '+')
448 return false;
449
450 if (*Name == '+')
Chris Lattnerd9725f72009-04-26 07:16:29 +0000451 Info.setIsReadWrite();
Anders Carlssonf511f642007-11-27 04:11:28 +0000452
453 Name++;
454 while (*Name) {
455 switch (*Name) {
456 default:
Chris Lattnerd9725f72009-04-26 07:16:29 +0000457 if (!validateAsmConstraint(Name, Info)) {
Ted Kremenekb44485b2008-04-24 16:36:38 +0000458 // FIXME: We temporarily return false
Anders Carlssonf511f642007-11-27 04:11:28 +0000459 // so we can add more constraints as we hit it.
460 // Eventually, an unknown constraint should just be treated as 'g'.
Ted Kremenekb44485b2008-04-24 16:36:38 +0000461 return false;
Anders Carlssonf511f642007-11-27 04:11:28 +0000462 }
David Majnemera0040df2015-01-10 10:43:19 +0000463 break;
Anders Carlssonf511f642007-11-27 04:11:28 +0000464 case '&': // early clobber.
David Majnemera0040df2015-01-10 10:43:19 +0000465 Info.setEarlyClobber();
Anders Carlssonf511f642007-11-27 04:11:28 +0000466 break;
Chris Lattnerf3154712009-10-13 04:32:07 +0000467 case '%': // commutative.
468 // FIXME: Check that there is a another register after this one.
469 break;
Anders Carlssonf511f642007-11-27 04:11:28 +0000470 case 'r': // general register.
Chris Lattnerd9725f72009-04-26 07:16:29 +0000471 Info.setAllowsRegister();
Anders Carlssonf511f642007-11-27 04:11:28 +0000472 break;
473 case 'm': // memory operand.
Dale Johannesen8499f472010-09-07 18:40:41 +0000474 case 'o': // offsetable memory operand.
475 case 'V': // non-offsetable memory operand.
476 case '<': // autodecrement memory operand.
477 case '>': // autoincrement memory operand.
Chris Lattnerd9725f72009-04-26 07:16:29 +0000478 Info.setAllowsMemory();
Anders Carlssonf511f642007-11-27 04:11:28 +0000479 break;
480 case 'g': // general register, memory operand or immediate integer.
Anders Carlssone70cde12009-01-18 02:12:04 +0000481 case 'X': // any operand.
Chris Lattnerd9725f72009-04-26 07:16:29 +0000482 Info.setAllowsRegister();
483 Info.setAllowsMemory();
Anders Carlssonf511f642007-11-27 04:11:28 +0000484 break;
John Thompsona5c7d702010-08-10 19:20:14 +0000485 case ',': // multiple alternative constraint. Pass it.
John Thompson46667af2010-08-11 00:58:20 +0000486 // Handle additional optional '=' or '+' modifiers.
John Thompson12240612010-09-18 01:15:13 +0000487 if (Name[1] == '=' || Name[1] == '+')
John Thompson46667af2010-08-11 00:58:20 +0000488 Name++;
John Thompsona5c7d702010-08-10 19:20:14 +0000489 break;
David Majnemer50cb0552015-01-11 08:52:38 +0000490 case '#': // Ignore as constraint.
491 while (Name[1] && Name[1] != ',')
492 Name++;
David Majnemerc71a5662015-01-11 09:39:03 +0000493 break;
John Thompsona5c7d702010-08-10 19:20:14 +0000494 case '?': // Disparage slightly code.
Dale Johannesen8499f472010-09-07 18:40:41 +0000495 case '!': // Disparage severely.
Ulrich Weigand7bcc7ec2012-10-29 12:20:54 +0000496 case '*': // Ignore for choosing register preferences.
John Thompsona5c7d702010-08-10 19:20:14 +0000497 break; // Pass them.
Anders Carlssonf511f642007-11-27 04:11:28 +0000498 }
Mike Stump11289f42009-09-09 15:08:12 +0000499
Anders Carlssonf511f642007-11-27 04:11:28 +0000500 Name++;
501 }
Mike Stump11289f42009-09-09 15:08:12 +0000502
David Majnemera0040df2015-01-10 10:43:19 +0000503 // Early clobber with a read-write constraint which doesn't permit registers
504 // is invalid.
505 if (Info.earlyClobber() && Info.isReadWrite() && !Info.allowsRegister())
506 return false;
507
Benjamin Kramer1e4a8862013-04-18 22:49:48 +0000508 // If a constraint allows neither memory nor register operands it contains
509 // only modifiers. Reject it.
Benjamin Kramer7ee3b9c2013-04-18 13:23:23 +0000510 return Info.allowsMemory() || Info.allowsRegister();
Anders Carlssonf511f642007-11-27 04:11:28 +0000511}
512
Anders Carlssona79203b2009-01-18 01:56:57 +0000513bool TargetInfo::resolveSymbolicName(const char *&Name,
Craig Topper55765ca2015-10-21 02:34:10 +0000514 ArrayRef<ConstraintInfo> OutputConstraints,
Chris Lattnerd9725f72009-04-26 07:16:29 +0000515 unsigned &Index) const {
Anders Carlssona79203b2009-01-18 01:56:57 +0000516 assert(*Name == '[' && "Symbolic name did not start with '['");
Anders Carlssona79203b2009-01-18 01:56:57 +0000517 Name++;
518 const char *Start = Name;
519 while (*Name && *Name != ']')
520 Name++;
Mike Stump11289f42009-09-09 15:08:12 +0000521
Anders Carlssona79203b2009-01-18 01:56:57 +0000522 if (!*Name) {
523 // Missing ']'
524 return false;
525 }
Mike Stump11289f42009-09-09 15:08:12 +0000526
Anders Carlssona79203b2009-01-18 01:56:57 +0000527 std::string SymbolicName(Start, Name - Start);
Mike Stump11289f42009-09-09 15:08:12 +0000528
Craig Topper55765ca2015-10-21 02:34:10 +0000529 for (Index = 0; Index != OutputConstraints.size(); ++Index)
Chris Lattnerc16d4762009-04-26 17:57:12 +0000530 if (SymbolicName == OutputConstraints[Index].getName())
Anders Carlssona79203b2009-01-18 01:56:57 +0000531 return true;
Anders Carlssona79203b2009-01-18 01:56:57 +0000532
533 return false;
534}
535
Craig Topper55765ca2015-10-21 02:34:10 +0000536bool TargetInfo::validateInputConstraint(
537 MutableArrayRef<ConstraintInfo> OutputConstraints,
538 ConstraintInfo &Info) const {
Chris Lattnerc3f4c7b2009-04-26 17:19:08 +0000539 const char *Name = Info.ConstraintStr.c_str();
540
Duncan P. N. Exon Smithd68c7aa2013-12-16 03:20:06 +0000541 if (!*Name)
542 return false;
543
Anders Carlssonf511f642007-11-27 04:11:28 +0000544 while (*Name) {
545 switch (*Name) {
546 default:
547 // Check if we have a matching constraint
548 if (*Name >= '0' && *Name <= '9') {
David Majnemerb6b56432015-01-11 10:22:41 +0000549 const char *DigitStart = Name;
550 while (Name[1] >= '0' && Name[1] <= '9')
551 Name++;
552 const char *DigitEnd = Name;
553 unsigned i;
554 if (StringRef(DigitStart, DigitEnd - DigitStart + 1)
555 .getAsInteger(10, i))
556 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000557
Anders Carlssonf511f642007-11-27 04:11:28 +0000558 // Check if matching constraint is out of bounds.
Craig Topper55765ca2015-10-21 02:34:10 +0000559 if (i >= OutputConstraints.size()) return false;
Mike Stump11289f42009-09-09 15:08:12 +0000560
Anders Carlssonda1f5fc2010-11-03 02:22:29 +0000561 // A number must refer to an output only operand.
562 if (OutputConstraints[i].isReadWrite())
563 return false;
564
Eric Christopherb093d692015-10-09 18:39:59 +0000565 // If the constraint is already tied, it must be tied to the
Anders Carlsson2d5f8b42010-11-03 02:54:51 +0000566 // same operand referenced to by the number.
567 if (Info.hasTiedOperand() && Info.getTiedOperand() != i)
568 return false;
569
Mike Stump11289f42009-09-09 15:08:12 +0000570 // The constraint should have the same info as the respective
Anders Carlsson570c3572009-01-27 20:38:24 +0000571 // output constraint.
Chris Lattner4c92b512009-04-26 18:05:25 +0000572 Info.setTiedOperand(i, OutputConstraints[i]);
Chris Lattnerd9725f72009-04-26 07:16:29 +0000573 } else if (!validateAsmConstraint(Name, Info)) {
Daniel Dunbar81128e02008-08-25 09:46:27 +0000574 // FIXME: This error return is in place temporarily so we can
575 // add more constraints as we hit it. Eventually, an unknown
576 // constraint should just be treated as 'g'.
577 return false;
Anders Carlssona79203b2009-01-18 01:56:57 +0000578 }
579 break;
580 case '[': {
581 unsigned Index = 0;
Craig Topper55765ca2015-10-21 02:34:10 +0000582 if (!resolveSymbolicName(Name, OutputConstraints, Index))
Anders Carlssona79203b2009-01-18 01:56:57 +0000583 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000584
Eric Christopherb093d692015-10-09 18:39:59 +0000585 // If the constraint is already tied, it must be tied to the
Anders Carlsson2d5f8b42010-11-03 02:54:51 +0000586 // same operand referenced to by the number.
587 if (Info.hasTiedOperand() && Info.getTiedOperand() != Index)
588 return false;
589
David Majnemer55164f92015-01-11 09:57:13 +0000590 // A number must refer to an output only operand.
591 if (OutputConstraints[Index].isReadWrite())
592 return false;
593
Eli Friedman854f1102010-08-11 23:03:37 +0000594 Info.setTiedOperand(Index, OutputConstraints[Index]);
Anders Carlssona79203b2009-01-18 01:56:57 +0000595 break;
Mike Stump11289f42009-09-09 15:08:12 +0000596 }
Anders Carlsson050f4942007-12-08 19:32:57 +0000597 case '%': // commutative
598 // FIXME: Fail if % is used with the last operand.
599 break;
Anders Carlssonf511f642007-11-27 04:11:28 +0000600 case 'i': // immediate integer.
Anders Carlssona3a96af2008-03-09 06:02:02 +0000601 case 'n': // immediate integer with a known value.
Anders Carlssonf511f642007-11-27 04:11:28 +0000602 break;
Chris Lattnerdbcc5ca2009-05-06 04:33:31 +0000603 case 'I': // Various constant constraints with target-specific meanings.
604 case 'J':
605 case 'K':
606 case 'L':
607 case 'M':
608 case 'N':
609 case 'O':
610 case 'P':
Saleem Abdulrasoola2823572015-01-06 04:26:34 +0000611 if (!validateAsmConstraint(Name, Info))
612 return false;
Chris Lattnerdbcc5ca2009-05-06 04:33:31 +0000613 break;
Anders Carlssonf511f642007-11-27 04:11:28 +0000614 case 'r': // general register.
Chris Lattnerd9725f72009-04-26 07:16:29 +0000615 Info.setAllowsRegister();
Anders Carlssonf511f642007-11-27 04:11:28 +0000616 break;
617 case 'm': // memory operand.
Dale Johannesen8499f472010-09-07 18:40:41 +0000618 case 'o': // offsettable memory operand.
619 case 'V': // non-offsettable memory operand.
620 case '<': // autodecrement memory operand.
621 case '>': // autoincrement memory operand.
Chris Lattnerd9725f72009-04-26 07:16:29 +0000622 Info.setAllowsMemory();
Anders Carlssonf511f642007-11-27 04:11:28 +0000623 break;
624 case 'g': // general register, memory operand or immediate integer.
Anders Carlssone70cde12009-01-18 02:12:04 +0000625 case 'X': // any operand.
Chris Lattnerd9725f72009-04-26 07:16:29 +0000626 Info.setAllowsRegister();
627 Info.setAllowsMemory();
Anders Carlssonf511f642007-11-27 04:11:28 +0000628 break;
John Thompsonc467aa22010-09-21 22:04:54 +0000629 case 'E': // immediate floating point.
630 case 'F': // immediate floating point.
631 case 'p': // address operand.
632 break;
John Thompsona5c7d702010-08-10 19:20:14 +0000633 case ',': // multiple alternative constraint. Ignore comma.
634 break;
David Majnemerc71a5662015-01-11 09:39:03 +0000635 case '#': // Ignore as constraint.
636 while (Name[1] && Name[1] != ',')
637 Name++;
David Majnemerc71a5662015-01-11 09:39:03 +0000638 break;
John Thompsona5c7d702010-08-10 19:20:14 +0000639 case '?': // Disparage slightly code.
Chris Lattner57540c52011-04-15 05:22:18 +0000640 case '!': // Disparage severely.
Ulrich Weigand7bcc7ec2012-10-29 12:20:54 +0000641 case '*': // Ignore for choosing register preferences.
John Thompsona5c7d702010-08-10 19:20:14 +0000642 break; // Pass them.
Anders Carlssonf511f642007-11-27 04:11:28 +0000643 }
Mike Stump11289f42009-09-09 15:08:12 +0000644
Anders Carlssonf511f642007-11-27 04:11:28 +0000645 Name++;
646 }
Mike Stump11289f42009-09-09 15:08:12 +0000647
Anders Carlssonf511f642007-11-27 04:11:28 +0000648 return true;
649}