blob: bf36effbe3b5ae086b8ce046e6a1f6c198f00a0e [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"
Alexander Ivchenko0fb8c872018-05-18 11:56:21 +000017#include "clang/Basic/Diagnostic.h"
John Thompsoned4e2952009-11-05 20:14:16 +000018#include "clang/Basic/LangOptions.h"
Chris Lattnerec0a6d92007-09-22 18:29:59 +000019#include "llvm/ADT/APFloat.h"
Anders Carlsson290aa852007-11-25 00:25:21 +000020#include "llvm/ADT/STLExtras.h"
David Blaikie76bd3c82011-09-23 05:35:21 +000021#include "llvm/Support/ErrorHandling.h"
Saleem Abdulrasool729379a2017-10-06 23:09:55 +000022#include "llvm/Support/TargetParser.h"
Chris Lattnerc4f02b62008-04-06 04:02:29 +000023#include <cstdlib>
Chris Lattner1e27fe12006-10-14 07:06:20 +000024using namespace clang;
25
Alexander Richardson6d989432017-10-15 18:48:14 +000026static const LangASMap DefaultAddrSpaceMap = {0};
Peter Collingbourne599cb8e2011-03-18 22:38:29 +000027
Chris Lattner1df27862008-03-08 08:59:43 +000028// TargetInfo Constructor.
Benjamin Kramerb1276b42013-06-29 16:37:14 +000029TargetInfo::TargetInfo(const llvm::Triple &T) : TargetOpts(), Triple(T) {
Daniel Dunbar3d9289c2010-04-15 06:18:39 +000030 // Set defaults. Defaults are set for a 32-bit RISC platform, like PPC or
31 // SPARC. These should be overridden by concrete targets as needed.
Matt Arsenaultf333de32016-09-07 07:08:02 +000032 BigEndian = !T.isLittleEndian();
Eli Friedmand88c8a12009-04-19 21:38:35 +000033 TLSSupported = true;
Jonas Hahnfeld87d44262017-11-18 21:00:46 +000034 VLASupported = true;
Chris Lattner1a8f3942010-04-23 16:29:58 +000035 NoAsmVariants = false;
Sjoerd Meijer87793e72018-03-19 13:22:49 +000036 HasLegalHalfType = false;
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +000037 HasFloat128 = false;
Chris Lattner5e2ef0c2008-05-09 06:08:39 +000038 PointerWidth = PointerAlign = 32;
Roman Divacky965b0b72011-01-06 08:27:10 +000039 BoolWidth = BoolAlign = 8;
Chris Lattnerb781dc792008-05-08 05:58:21 +000040 IntWidth = IntAlign = 32;
Chris Lattner5a9aaaf2008-05-09 05:50:02 +000041 LongWidth = LongAlign = 32;
42 LongLongWidth = LongLongAlign = 64;
Leonard Chanf921d852018-06-04 16:07:52 +000043 ShortAccumWidth = ShortAccumAlign = 16;
44 AccumWidth = AccumAlign = 32;
45 LongAccumWidth = LongAccumAlign = 64;
Nick Lewyckyf59e1292011-12-16 22:34:14 +000046 SuitableAlign = 64;
Ulrich Weigandca3cb7f2015-04-21 17:29:35 +000047 DefaultAlignForAttributeAligned = 128;
Ulrich Weigandfa806422013-05-06 16:23:57 +000048 MinGlobalAlign = 0;
Richard Smith59139022016-09-30 22:41:36 +000049 // From the glibc documentation, on GNU systems, malloc guarantees 16-byte
50 // alignment on 64-bit systems and 8-byte alignment on 32-bit systems. See
51 // https://www.gnu.org/software/libc/manual/html_node/Malloc-Examples.html
Reid Kleckner13a3d9e2017-11-21 01:25:56 +000052 if (T.isGNUEnvironment() || T.isWindowsMSVCEnvironment())
Richard Smith59139022016-09-30 22:41:36 +000053 NewAlign = Triple.isArch64Bit() ? 128 : Triple.isArch32Bit() ? 64 : 0;
54 else
55 NewAlign = 0; // Infer from basic type alignment.
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +000056 HalfWidth = 16;
57 HalfAlign = 16;
Eli Friedmanb5366062008-05-20 14:21:01 +000058 FloatWidth = 32;
59 FloatAlign = 32;
Nate Begeman65bea232008-04-18 17:17:24 +000060 DoubleWidth = 64;
Eli Friedmanb5366062008-05-20 14:21:01 +000061 DoubleAlign = 64;
62 LongDoubleWidth = 64;
63 LongDoubleAlign = 64;
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +000064 Float128Align = 128;
Rafael Espindolae971b9a2010-06-04 23:15:27 +000065 LargeArrayMinWidth = 0;
66 LargeArrayAlign = 0;
Eli Friedman4b72fdd2011-10-14 20:59:01 +000067 MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 0;
Chad Rosiercc40ea72012-07-13 23:57:43 +000068 MaxVectorAlign = 0;
Paul Robinsond30e2ee2015-07-14 20:52:32 +000069 MaxTLSAlign = 0;
Alexey Bataev00396512015-07-02 03:40:19 +000070 SimdDefaultAlign = 0;
Anders Carlsson2a79a902008-10-31 16:05:19 +000071 SizeType = UnsignedLong;
Eli Friedmand50881c2008-11-02 02:43:55 +000072 PtrDiffType = SignedLong;
Sanjiv Guptad7959242008-10-31 09:52:39 +000073 IntMaxType = SignedLongLong;
Chris Lattner7e4c81c2009-02-13 22:28:55 +000074 IntPtrType = SignedLong;
Eli Friedmand50881c2008-11-02 02:43:55 +000075 WCharType = SignedInt;
Chris Lattner67204922009-10-21 04:59:34 +000076 WIntType = SignedInt;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +000077 Char16Type = UnsignedShort;
78 Char32Type = UnsignedInt;
Eli Friedman2857ccb2009-07-01 03:36:11 +000079 Int64Type = SignedLongLong;
Edward O'Callaghan847f2a12009-11-21 00:49:54 +000080 SigAtomicType = SignedInt;
Eli Friedman4e91899e2012-11-27 02:58:24 +000081 ProcessIDType = SignedInt;
Fariborz Jahanian29898f42012-04-16 21:03:30 +000082 UseSignedCharForObjCBool = true;
Daniel Dunbar1da65112010-04-15 15:06:18 +000083 UseBitFieldTypeAlignment = true;
Chad Rosier18903ee2011-08-04 01:21:14 +000084 UseZeroLengthBitfieldAlignment = false;
Sunil Srivastava0ce2f222016-02-05 20:50:02 +000085 UseExplicitBitFieldAlignment = true;
Chad Rosier18903ee2011-08-04 01:21:14 +000086 ZeroLengthBitfieldBoundary = 0;
Stephan Bergmann17c7f702016-12-14 11:57:17 +000087 HalfFormat = &llvm::APFloat::IEEEhalf();
88 FloatFormat = &llvm::APFloat::IEEEsingle();
89 DoubleFormat = &llvm::APFloat::IEEEdouble();
90 LongDoubleFormat = &llvm::APFloat::IEEEdouble();
91 Float128Format = &llvm::APFloat::IEEEquad();
Roman Divacky178e01602011-02-10 16:52:03 +000092 MCountName = "mcount";
Abramo Bagnara41bfb662011-09-08 14:20:25 +000093 RegParmMax = 0;
94 SSERegParmMax = 0;
Daniel Dunbarbd606522010-05-27 00:35:16 +000095 HasAlignMac68kSupport = false;
Charles Davisc7d5c942015-09-17 20:55:33 +000096 HasBuiltinMSVaList = false;
Pirama Arumuga Nainarbb846a32016-07-27 19:01:51 +000097 IsRenderScriptTarget = false;
Daniel Dunbar6f2e8392010-07-14 23:39:36 +000098
99 // Default to no types using fpret.
100 RealTypeUsesObjCFPRet = 0;
John McCall86353412010-08-21 22:46:04 +0000101
Anders Carlsson2f1a6c32011-10-31 16:27:11 +0000102 // Default to not using fp2ret for __Complex long double
103 ComplexLongDoubleUsesFP2Ret = false;
104
Hans Wennborgc9bd88e2014-01-14 19:35:09 +0000105 // Set the C++ ABI based on the triple.
Saleem Abdulrasool377066a2014-03-27 22:50:18 +0000106 TheCXXABI.set(Triple.isKnownWindowsMSVCEnvironment()
Hans Wennborgc9bd88e2014-01-14 19:35:09 +0000107 ? TargetCXXABI::Microsoft
108 : TargetCXXABI::GenericItanium);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000109
110 // Default to an empty address space map.
111 AddrSpaceMap = &DefaultAddrSpaceMap;
David Tweed31d09b02013-09-13 12:04:22 +0000112 UseAddrSpaceMapMangling = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000113
114 // Default to an unknown platform name.
115 PlatformName = "unknown";
116 PlatformMinVersion = VersionTuple();
Chris Lattner1df27862008-03-08 08:59:43 +0000117}
118
Chris Lattnerc3a669b2008-03-08 08:24:01 +0000119// Out of line virtual dtor for TargetInfo.
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +0000120TargetInfo::~TargetInfo() {}
Chris Lattner2194ddc2006-10-14 18:32:26 +0000121
Alexander Ivchenko0fb8c872018-05-18 11:56:21 +0000122bool
123TargetInfo::checkCFProtectionBranchSupported(DiagnosticsEngine &Diags) const {
124 Diags.Report(diag::err_opt_not_valid_on_target) << "cf-protection=branch";
125 return false;
126}
127
128bool
129TargetInfo::checkCFProtectionReturnSupported(DiagnosticsEngine &Diags) const {
130 Diags.Report(diag::err_opt_not_valid_on_target) << "cf-protection=return";
131 return false;
132}
133
Chris Lattnera91c30f2009-02-06 05:04:11 +0000134/// getTypeName - Return the user string for the specified integer type enum.
135/// For example, SignedShort -> "short".
136const char *TargetInfo::getTypeName(IntType T) {
137 switch (T) {
David Blaikie83d382b2011-09-23 05:06:16 +0000138 default: llvm_unreachable("not an integer!");
Joerg Sonnenberger3d9478c2014-07-28 21:06:22 +0000139 case SignedChar: return "signed char";
Stepan Dyatkovskiy5a637922013-09-05 11:23:21 +0000140 case UnsignedChar: return "unsigned char";
Chris Lattnera91c30f2009-02-06 05:04:11 +0000141 case SignedShort: return "short";
142 case UnsignedShort: return "unsigned short";
143 case SignedInt: return "int";
144 case UnsignedInt: return "unsigned int";
145 case SignedLong: return "long int";
146 case UnsignedLong: return "long unsigned int";
147 case SignedLongLong: return "long long int";
148 case UnsignedLongLong: return "long long unsigned int";
149 }
150}
151
Chris Lattner72cdcac2009-10-21 06:24:21 +0000152/// getTypeConstantSuffix - Return the constant suffix for the specified
153/// integer type enum. For example, SignedLong -> "L".
Joerg Sonnenberger587deea2014-07-17 20:12:32 +0000154const char *TargetInfo::getTypeConstantSuffix(IntType T) const {
Chris Lattner72cdcac2009-10-21 06:24:21 +0000155 switch (T) {
David Blaikie83d382b2011-09-23 05:06:16 +0000156 default: llvm_unreachable("not an integer!");
Stepan Dyatkovskiy5a637922013-09-05 11:23:21 +0000157 case SignedChar:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000158 case SignedShort:
159 case SignedInt: return "";
160 case SignedLong: return "L";
161 case SignedLongLong: return "LL";
Stepan Dyatkovskiy5a637922013-09-05 11:23:21 +0000162 case UnsignedChar:
Joerg Sonnenberger587deea2014-07-17 20:12:32 +0000163 if (getCharWidth() < getIntWidth())
164 return "";
Galina Kistanova96088f42017-06-03 06:38:22 +0000165 LLVM_FALLTHROUGH;
Chris Lattner72cdcac2009-10-21 06:24:21 +0000166 case UnsignedShort:
Joerg Sonnenberger587deea2014-07-17 20:12:32 +0000167 if (getShortWidth() < getIntWidth())
168 return "";
Galina Kistanova96088f42017-06-03 06:38:22 +0000169 LLVM_FALLTHROUGH;
Chris Lattner72cdcac2009-10-21 06:24:21 +0000170 case UnsignedInt: return "U";
171 case UnsignedLong: return "UL";
172 case UnsignedLongLong: return "ULL";
173 }
174}
175
Joerg Sonnenbergerbe324f92014-07-15 11:30:00 +0000176/// getTypeFormatModifier - Return the printf format modifier for the
177/// specified integer type enum. For example, SignedLong -> "l".
178
179const char *TargetInfo::getTypeFormatModifier(IntType T) {
180 switch (T) {
181 default: llvm_unreachable("not an integer!");
182 case SignedChar:
183 case UnsignedChar: return "hh";
184 case SignedShort:
185 case UnsignedShort: return "h";
186 case SignedInt:
187 case UnsignedInt: return "";
188 case SignedLong:
189 case UnsignedLong: return "l";
190 case SignedLongLong:
191 case UnsignedLongLong: return "ll";
192 }
193}
194
Michael J. Spencerfeb799c2010-10-18 07:10:59 +0000195/// getTypeWidth - Return the width (in bits) of the specified integer type
Chris Lattner72cdcac2009-10-21 06:24:21 +0000196/// enum. For example, SignedInt -> getIntWidth().
197unsigned TargetInfo::getTypeWidth(IntType T) const {
198 switch (T) {
David Blaikie83d382b2011-09-23 05:06:16 +0000199 default: llvm_unreachable("not an integer!");
Stepan Dyatkovskiy5a637922013-09-05 11:23:21 +0000200 case SignedChar:
201 case UnsignedChar: return getCharWidth();
Chris Lattnere4a8c642009-11-05 21:21:32 +0000202 case SignedShort:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000203 case UnsignedShort: return getShortWidth();
Chris Lattnere4a8c642009-11-05 21:21:32 +0000204 case SignedInt:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000205 case UnsignedInt: return getIntWidth();
Chris Lattnere4a8c642009-11-05 21:21:32 +0000206 case SignedLong:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000207 case UnsignedLong: return getLongWidth();
Chris Lattnere4a8c642009-11-05 21:21:32 +0000208 case SignedLongLong:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000209 case UnsignedLongLong: return getLongLongWidth();
210 };
211}
212
Stepan Dyatkovskiy5a637922013-09-05 11:23:21 +0000213TargetInfo::IntType TargetInfo::getIntTypeByWidth(
214 unsigned BitWidth, bool IsSigned) const {
215 if (getCharWidth() == BitWidth)
216 return IsSigned ? SignedChar : UnsignedChar;
217 if (getShortWidth() == BitWidth)
218 return IsSigned ? SignedShort : UnsignedShort;
219 if (getIntWidth() == BitWidth)
220 return IsSigned ? SignedInt : UnsignedInt;
221 if (getLongWidth() == BitWidth)
222 return IsSigned ? SignedLong : UnsignedLong;
223 if (getLongLongWidth() == BitWidth)
224 return IsSigned ? SignedLongLong : UnsignedLongLong;
225 return NoInt;
226}
227
JF Bastienab8d0a02014-06-25 01:31:33 +0000228TargetInfo::IntType TargetInfo::getLeastIntTypeByWidth(unsigned BitWidth,
229 bool IsSigned) const {
230 if (getCharWidth() >= BitWidth)
231 return IsSigned ? SignedChar : UnsignedChar;
232 if (getShortWidth() >= BitWidth)
233 return IsSigned ? SignedShort : UnsignedShort;
234 if (getIntWidth() >= BitWidth)
235 return IsSigned ? SignedInt : UnsignedInt;
236 if (getLongWidth() >= BitWidth)
237 return IsSigned ? SignedLong : UnsignedLong;
238 if (getLongLongWidth() >= BitWidth)
239 return IsSigned ? SignedLongLong : UnsignedLongLong;
240 return NoInt;
241}
242
Stepan Dyatkovskiy5a637922013-09-05 11:23:21 +0000243TargetInfo::RealType TargetInfo::getRealTypeByWidth(unsigned BitWidth) const {
244 if (getFloatWidth() == BitWidth)
245 return Float;
246 if (getDoubleWidth() == BitWidth)
247 return Double;
248
249 switch (BitWidth) {
250 case 96:
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000251 if (&getLongDoubleFormat() == &llvm::APFloat::x87DoubleExtended())
Stepan Dyatkovskiy5a637922013-09-05 11:23:21 +0000252 return LongDouble;
253 break;
254 case 128:
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000255 if (&getLongDoubleFormat() == &llvm::APFloat::PPCDoubleDouble() ||
256 &getLongDoubleFormat() == &llvm::APFloat::IEEEquad())
Stepan Dyatkovskiy5a637922013-09-05 11:23:21 +0000257 return LongDouble;
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +0000258 if (hasFloat128Type())
259 return Float128;
Stepan Dyatkovskiy5a637922013-09-05 11:23:21 +0000260 break;
261 }
262
263 return NoFloat;
264}
265
Michael J. Spencerfeb799c2010-10-18 07:10:59 +0000266/// getTypeAlign - Return the alignment (in bits) of the specified integer type
Chris Lattnere4a8c642009-11-05 21:21:32 +0000267/// enum. For example, SignedInt -> getIntAlign().
268unsigned TargetInfo::getTypeAlign(IntType T) const {
269 switch (T) {
David Blaikie83d382b2011-09-23 05:06:16 +0000270 default: llvm_unreachable("not an integer!");
Stepan Dyatkovskiy5a637922013-09-05 11:23:21 +0000271 case SignedChar:
272 case UnsignedChar: return getCharAlign();
Chris Lattnere4a8c642009-11-05 21:21:32 +0000273 case SignedShort:
274 case UnsignedShort: return getShortAlign();
275 case SignedInt:
276 case UnsignedInt: return getIntAlign();
277 case SignedLong:
278 case UnsignedLong: return getLongAlign();
279 case SignedLongLong:
280 case UnsignedLongLong: return getLongLongAlign();
281 };
282}
283
Chris Lattnerc0c04392009-10-25 22:49:18 +0000284/// isTypeSigned - Return whether an integer types is signed. Returns true if
Chris Lattner72cdcac2009-10-21 06:24:21 +0000285/// the type is signed; false otherwise.
Chris Lattnerad3467e2010-12-25 23:25:43 +0000286bool TargetInfo::isTypeSigned(IntType T) {
Chris Lattner72cdcac2009-10-21 06:24:21 +0000287 switch (T) {
David Blaikie83d382b2011-09-23 05:06:16 +0000288 default: llvm_unreachable("not an integer!");
Stepan Dyatkovskiy5a637922013-09-05 11:23:21 +0000289 case SignedChar:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000290 case SignedShort:
291 case SignedInt:
292 case SignedLong:
Michael J. Spencerfeb799c2010-10-18 07:10:59 +0000293 case SignedLongLong:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000294 return true;
Stepan Dyatkovskiy5a637922013-09-05 11:23:21 +0000295 case UnsignedChar:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000296 case UnsignedShort:
297 case UnsignedInt:
298 case UnsignedLong:
Michael J. Spencerfeb799c2010-10-18 07:10:59 +0000299 case UnsignedLongLong:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000300 return false;
301 };
302}
303
Alp Toker74437972014-07-06 05:14:24 +0000304/// adjust - Set forced language options.
John Thompsoned4e2952009-11-05 20:14:16 +0000305/// Apply changes to the target information with respect to certain
Eric Christopher3646e622017-03-22 06:36:09 +0000306/// language options which change the target configuration and adjust
307/// the language based on the target options where applicable.
308void TargetInfo::adjust(LangOptions &Opts) {
Daniel Dunbar9302f602010-04-15 15:06:22 +0000309 if (Opts.NoBitFieldTypeAlign)
310 UseBitFieldTypeAlignment = false;
Saleem Abdulrasool729379a2017-10-06 23:09:55 +0000311
312 switch (Opts.WCharSize) {
313 default: llvm_unreachable("invalid wchar_t width");
314 case 0: break;
315 case 1: WCharType = Opts.WCharIsSigned ? SignedChar : UnsignedChar; break;
316 case 2: WCharType = Opts.WCharIsSigned ? SignedShort : UnsignedShort; break;
317 case 4: WCharType = Opts.WCharIsSigned ? SignedInt : UnsignedInt; break;
318 }
319
Reid Kleckner8195f692016-05-04 02:58:24 +0000320 if (Opts.AlignDouble) {
321 DoubleAlign = LongLongAlign = 64;
322 LongDoubleAlign = 64;
323 }
David Tweed2da64382013-09-09 09:17:24 +0000324
325 if (Opts.OpenCL) {
326 // OpenCL C requires specific widths for types, irrespective of
327 // what these normally are for the target.
328 // We also define long long and long double here, although the
329 // OpenCL standard only mentions these as "reserved".
330 IntWidth = IntAlign = 32;
331 LongWidth = LongAlign = 64;
332 LongLongWidth = LongLongAlign = 128;
333 HalfWidth = HalfAlign = 16;
334 FloatWidth = FloatAlign = 32;
Eric Christopherb093d692015-10-09 18:39:59 +0000335
336 // Embedded 32-bit targets (OpenCL EP) might have double C type
337 // defined as float. Let's not override this as it might lead
Pekka Jaaskelainen43c39d52013-12-18 18:15:03 +0000338 // to generating illegal code that uses 64bit doubles.
339 if (DoubleWidth != FloatWidth) {
340 DoubleWidth = DoubleAlign = 64;
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000341 DoubleFormat = &llvm::APFloat::IEEEdouble();
Pekka Jaaskelainen43c39d52013-12-18 18:15:03 +0000342 }
David Tweed2da64382013-09-09 09:17:24 +0000343 LongDoubleWidth = LongDoubleAlign = 128;
344
Yaxun Liu26f75662016-08-19 05:17:25 +0000345 unsigned MaxPointerWidth = getMaxPointerWidth();
346 assert(MaxPointerWidth == 32 || MaxPointerWidth == 64);
347 bool Is32BitArch = MaxPointerWidth == 32;
David Tweed6c2149b2013-09-10 08:00:34 +0000348 SizeType = Is32BitArch ? UnsignedInt : UnsignedLong;
349 PtrDiffType = Is32BitArch ? SignedInt : SignedLong;
350 IntPtrType = Is32BitArch ? SignedInt : SignedLong;
David Tweed2da64382013-09-09 09:17:24 +0000351
352 IntMaxType = SignedLongLong;
David Tweed2da64382013-09-09 09:17:24 +0000353 Int64Type = SignedLong;
354
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000355 HalfFormat = &llvm::APFloat::IEEEhalf();
356 FloatFormat = &llvm::APFloat::IEEEsingle();
357 LongDoubleFormat = &llvm::APFloat::IEEEquad();
David Tweed2da64382013-09-09 09:17:24 +0000358 }
Richard Smith59139022016-09-30 22:41:36 +0000359
360 if (Opts.NewAlignOverride)
361 NewAlign = Opts.NewAlignOverride * getCharWidth();
John Thompsoned4e2952009-11-05 20:14:16 +0000362}
Chris Lattner72cdcac2009-10-21 06:24:21 +0000363
Eric Christopher8c47b422015-10-09 18:39:55 +0000364bool TargetInfo::initFeatureMap(
365 llvm::StringMap<bool> &Features, DiagnosticsEngine &Diags, StringRef CPU,
366 const std::vector<std::string> &FeatureVec) const {
Eric Christophere9cdcae2015-09-01 18:13:20 +0000367 for (const auto &F : FeatureVec) {
Craig Topper8459aa82015-10-21 16:31:31 +0000368 StringRef Name = F;
Eric Christophere9cdcae2015-09-01 18:13:20 +0000369 // Apply the feature via the target.
370 bool Enabled = Name[0] == '+';
Craig Topper8459aa82015-10-21 16:31:31 +0000371 setFeatureEnabled(Features, Name.substr(1), Enabled);
Eric Christophere9cdcae2015-09-01 18:13:20 +0000372 }
373 return true;
374}
375
Akira Hatanakafcbe17c2018-03-28 21:13:14 +0000376TargetInfo::CallingConvKind
377TargetInfo::getCallingConvKind(bool ClangABICompat4) const {
378 if (getCXXABI() != TargetCXXABI::Microsoft &&
379 (ClangABICompat4 || getTriple().getOS() == llvm::Triple::PS4))
380 return CCK_ClangABI4OrPS4;
381 return CCK_Default;
382}
383
Sven van Haastregt3bb7eaf2017-12-06 10:11:28 +0000384LangAS TargetInfo::getOpenCLTypeAddrSpace(OpenCLTypeKind TK) const {
385 switch (TK) {
386 case OCLTK_Image:
387 case OCLTK_Pipe:
Sven van Haastregtefb4d4c2017-08-15 09:38:18 +0000388 return LangAS::opencl_global;
Sven van Haastregtefb4d4c2017-08-15 09:38:18 +0000389
Sven van Haastregt3bb7eaf2017-12-06 10:11:28 +0000390 case OCLTK_Sampler:
Sven van Haastregtefb4d4c2017-08-15 09:38:18 +0000391 return LangAS::opencl_constant;
392
393 default:
394 return LangAS::Default;
395 }
396}
397
Chris Lattnerec0a6d92007-09-22 18:29:59 +0000398//===----------------------------------------------------------------------===//
Chris Lattnerec0a6d92007-09-22 18:29:59 +0000399
Chris Lattner10a5b382007-01-29 05:24:35 +0000400
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000401static StringRef removeGCCRegisterPrefix(StringRef Name) {
Anders Carlsson3ed4aea2008-02-06 00:11:32 +0000402 if (Name[0] == '%' || Name[0] == '#')
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000403 Name = Name.substr(1);
Michael J. Spencerfeb799c2010-10-18 07:10:59 +0000404
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000405 return Name;
Anders Carlsson3ed4aea2008-02-06 00:11:32 +0000406}
407
Eric Christopherfd9a5f42011-06-28 18:20:53 +0000408/// isValidClobber - Returns whether the passed in string is
409/// a valid clobber in an inline asm statement. This is used by
410/// Sema.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000411bool TargetInfo::isValidClobber(StringRef Name) const {
Eric Christopherfd9a5f42011-06-28 18:20:53 +0000412 return (isValidGCCRegisterName(Name) ||
Eric Christopherb093d692015-10-09 18:39:59 +0000413 Name == "memory" || Name == "cc");
Eric Christopherfd9a5f42011-06-28 18:20:53 +0000414}
415
Anders Carlsson5fa3f342007-11-24 23:38:12 +0000416/// isValidGCCRegisterName - Returns whether the passed in string
417/// is a valid register name according to GCC. This is used by Sema for
418/// inline asm statements.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000419bool TargetInfo::isValidGCCRegisterName(StringRef Name) const {
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000420 if (Name.empty())
421 return false;
Michael J. Spencerfeb799c2010-10-18 07:10:59 +0000422
Anders Carlsson290aa852007-11-25 00:25:21 +0000423 // Get rid of any register prefix.
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000424 Name = removeGCCRegisterPrefix(Name);
Olivier Goffartfc8f8932014-08-17 13:19:48 +0000425 if (Name.empty())
Craig Topperde330c72015-10-21 04:52:34 +0000426 return false;
Anders Carlsson3ed4aea2008-02-06 00:11:32 +0000427
Craig Topperf054e3a2015-10-19 03:52:27 +0000428 ArrayRef<const char *> Names = getGCCRegNames();
Mike Stump11289f42009-09-09 15:08:12 +0000429
Anders Carlsson290aa852007-11-25 00:25:21 +0000430 // If we have a number it maps to an entry in the register name array.
Jordan Rosea7d03842013-02-08 22:30:41 +0000431 if (isDigit(Name[0])) {
Craig Topper67288a92015-10-21 04:52:36 +0000432 unsigned n;
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000433 if (!Name.getAsInteger(0, n))
Craig Topper67288a92015-10-21 04:52:36 +0000434 return n < Names.size();
Anders Carlsson290aa852007-11-25 00:25:21 +0000435 }
436
437 // Check register names.
Craig Topper335c8f92015-10-21 04:52:38 +0000438 if (std::find(Names.begin(), Names.end(), Name) != Names.end())
439 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000440
Eric Christophercdd36352011-06-21 00:05:20 +0000441 // Check any additional names that we have.
Craig Topperdd2b74a2015-10-21 04:52:40 +0000442 for (const AddlRegName &ARN : getGCCAddlRegNames())
443 for (const char *AN : ARN.Names) {
444 if (!AN)
Eric Christopherb093d692015-10-09 18:39:59 +0000445 break;
Eric Christophercdd36352011-06-21 00:05:20 +0000446 // Make sure the register that the additional name is for is within
447 // the bounds of the register names from above.
Craig Topperdd2b74a2015-10-21 04:52:40 +0000448 if (AN == Name && ARN.RegNum < Names.size())
Anders Carlsson290aa852007-11-25 00:25:21 +0000449 return true;
450 }
Craig Topperdd2b74a2015-10-21 04:52:40 +0000451
452 // Now check aliases.
453 for (const GCCRegAlias &GRA : getGCCRegAliases())
454 for (const char *A : GRA.Aliases) {
455 if (!A)
456 break;
457 if (A == Name)
458 return true;
459 }
Mike Stump11289f42009-09-09 15:08:12 +0000460
Anders Carlsson5fa3f342007-11-24 23:38:12 +0000461 return false;
462}
Anders Carlssonf511f642007-11-27 04:11:28 +0000463
Marina Yatsinac42fd032016-12-26 12:23:42 +0000464StringRef TargetInfo::getNormalizedGCCRegisterName(StringRef Name,
465 bool ReturnCanonical) const {
Anders Carlssonf511f642007-11-27 04:11:28 +0000466 assert(isValidGCCRegisterName(Name) && "Invalid register passed in");
Mike Stump11289f42009-09-09 15:08:12 +0000467
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000468 // Get rid of any register prefix.
469 Name = removeGCCRegisterPrefix(Name);
Mike Stump11289f42009-09-09 15:08:12 +0000470
Craig Topperf054e3a2015-10-19 03:52:27 +0000471 ArrayRef<const char *> Names = getGCCRegNames();
Anders Carlssonf511f642007-11-27 04:11:28 +0000472
473 // First, check if we have a number.
Jordan Rosea7d03842013-02-08 22:30:41 +0000474 if (isDigit(Name[0])) {
Craig Topper67288a92015-10-21 04:52:36 +0000475 unsigned n;
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000476 if (!Name.getAsInteger(0, n)) {
Craig Topper67288a92015-10-21 04:52:36 +0000477 assert(n < Names.size() && "Out of bounds register number!");
Anders Carlssonf511f642007-11-27 04:11:28 +0000478 return Names[n];
479 }
480 }
Mike Stump11289f42009-09-09 15:08:12 +0000481
Eric Christophercdd36352011-06-21 00:05:20 +0000482 // Check any additional names that we have.
Craig Topperdd2b74a2015-10-21 04:52:40 +0000483 for (const AddlRegName &ARN : getGCCAddlRegNames())
484 for (const char *AN : ARN.Names) {
485 if (!AN)
Eric Christopherb093d692015-10-09 18:39:59 +0000486 break;
Eric Christophercdd36352011-06-21 00:05:20 +0000487 // Make sure the register that the additional name is for is within
488 // the bounds of the register names from above.
Craig Topperdd2b74a2015-10-21 04:52:40 +0000489 if (AN == Name && ARN.RegNum < Names.size())
Marina Yatsinac42fd032016-12-26 12:23:42 +0000490 return ReturnCanonical ? Names[ARN.RegNum] : Name;
Eric Christophercdd36352011-06-21 00:05:20 +0000491 }
492
Anders Carlssonf511f642007-11-27 04:11:28 +0000493 // Now check aliases.
Craig Topperdd2b74a2015-10-21 04:52:40 +0000494 for (const GCCRegAlias &RA : getGCCRegAliases())
495 for (const char *A : RA.Aliases) {
496 if (!A)
Anders Carlssonf511f642007-11-27 04:11:28 +0000497 break;
Craig Topperdd2b74a2015-10-21 04:52:40 +0000498 if (A == Name)
499 return RA.Register;
Anders Carlssonf511f642007-11-27 04:11:28 +0000500 }
Mike Stump11289f42009-09-09 15:08:12 +0000501
Anders Carlssonf511f642007-11-27 04:11:28 +0000502 return Name;
503}
504
Chris Lattnerc3f4c7b2009-04-26 17:19:08 +0000505bool TargetInfo::validateOutputConstraint(ConstraintInfo &Info) const {
506 const char *Name = Info.getConstraintStr().c_str();
Anders Carlssonf511f642007-11-27 04:11:28 +0000507 // An output constraint must start with '=' or '+'
508 if (*Name != '=' && *Name != '+')
509 return false;
510
511 if (*Name == '+')
Chris Lattnerd9725f72009-04-26 07:16:29 +0000512 Info.setIsReadWrite();
Anders Carlssonf511f642007-11-27 04:11:28 +0000513
514 Name++;
515 while (*Name) {
516 switch (*Name) {
517 default:
Chris Lattnerd9725f72009-04-26 07:16:29 +0000518 if (!validateAsmConstraint(Name, Info)) {
Ted Kremenekb44485b2008-04-24 16:36:38 +0000519 // FIXME: We temporarily return false
Anders Carlssonf511f642007-11-27 04:11:28 +0000520 // so we can add more constraints as we hit it.
521 // Eventually, an unknown constraint should just be treated as 'g'.
Ted Kremenekb44485b2008-04-24 16:36:38 +0000522 return false;
Anders Carlssonf511f642007-11-27 04:11:28 +0000523 }
David Majnemera0040df2015-01-10 10:43:19 +0000524 break;
Anders Carlssonf511f642007-11-27 04:11:28 +0000525 case '&': // early clobber.
David Majnemera0040df2015-01-10 10:43:19 +0000526 Info.setEarlyClobber();
Anders Carlssonf511f642007-11-27 04:11:28 +0000527 break;
Chris Lattnerf3154712009-10-13 04:32:07 +0000528 case '%': // commutative.
529 // FIXME: Check that there is a another register after this one.
530 break;
Anders Carlssonf511f642007-11-27 04:11:28 +0000531 case 'r': // general register.
Chris Lattnerd9725f72009-04-26 07:16:29 +0000532 Info.setAllowsRegister();
Anders Carlssonf511f642007-11-27 04:11:28 +0000533 break;
534 case 'm': // memory operand.
Dale Johannesen8499f472010-09-07 18:40:41 +0000535 case 'o': // offsetable memory operand.
536 case 'V': // non-offsetable memory operand.
537 case '<': // autodecrement memory operand.
538 case '>': // autoincrement memory operand.
Chris Lattnerd9725f72009-04-26 07:16:29 +0000539 Info.setAllowsMemory();
Anders Carlssonf511f642007-11-27 04:11:28 +0000540 break;
541 case 'g': // general register, memory operand or immediate integer.
Anders Carlssone70cde12009-01-18 02:12:04 +0000542 case 'X': // any operand.
Chris Lattnerd9725f72009-04-26 07:16:29 +0000543 Info.setAllowsRegister();
544 Info.setAllowsMemory();
Anders Carlssonf511f642007-11-27 04:11:28 +0000545 break;
John Thompsona5c7d702010-08-10 19:20:14 +0000546 case ',': // multiple alternative constraint. Pass it.
John Thompson46667af2010-08-11 00:58:20 +0000547 // Handle additional optional '=' or '+' modifiers.
John Thompson12240612010-09-18 01:15:13 +0000548 if (Name[1] == '=' || Name[1] == '+')
John Thompson46667af2010-08-11 00:58:20 +0000549 Name++;
John Thompsona5c7d702010-08-10 19:20:14 +0000550 break;
David Majnemer50cb0552015-01-11 08:52:38 +0000551 case '#': // Ignore as constraint.
552 while (Name[1] && Name[1] != ',')
553 Name++;
David Majnemerc71a5662015-01-11 09:39:03 +0000554 break;
John Thompsona5c7d702010-08-10 19:20:14 +0000555 case '?': // Disparage slightly code.
Dale Johannesen8499f472010-09-07 18:40:41 +0000556 case '!': // Disparage severely.
Ulrich Weigand7bcc7ec2012-10-29 12:20:54 +0000557 case '*': // Ignore for choosing register preferences.
Marina Yatsina33eb7752017-06-26 15:55:51 +0000558 case 'i': // Ignore i,n,E,F as output constraints (match from the other
559 // chars)
560 case 'n':
561 case 'E':
562 case 'F':
John Thompsona5c7d702010-08-10 19:20:14 +0000563 break; // Pass them.
Anders Carlssonf511f642007-11-27 04:11:28 +0000564 }
Mike Stump11289f42009-09-09 15:08:12 +0000565
Anders Carlssonf511f642007-11-27 04:11:28 +0000566 Name++;
567 }
Mike Stump11289f42009-09-09 15:08:12 +0000568
David Majnemera0040df2015-01-10 10:43:19 +0000569 // Early clobber with a read-write constraint which doesn't permit registers
570 // is invalid.
571 if (Info.earlyClobber() && Info.isReadWrite() && !Info.allowsRegister())
572 return false;
573
Benjamin Kramer1e4a8862013-04-18 22:49:48 +0000574 // If a constraint allows neither memory nor register operands it contains
575 // only modifiers. Reject it.
Benjamin Kramer7ee3b9c2013-04-18 13:23:23 +0000576 return Info.allowsMemory() || Info.allowsRegister();
Anders Carlssonf511f642007-11-27 04:11:28 +0000577}
578
Anders Carlssona79203b2009-01-18 01:56:57 +0000579bool TargetInfo::resolveSymbolicName(const char *&Name,
Craig Topper55765ca2015-10-21 02:34:10 +0000580 ArrayRef<ConstraintInfo> OutputConstraints,
Chris Lattnerd9725f72009-04-26 07:16:29 +0000581 unsigned &Index) const {
Anders Carlssona79203b2009-01-18 01:56:57 +0000582 assert(*Name == '[' && "Symbolic name did not start with '['");
Anders Carlssona79203b2009-01-18 01:56:57 +0000583 Name++;
584 const char *Start = Name;
585 while (*Name && *Name != ']')
586 Name++;
Mike Stump11289f42009-09-09 15:08:12 +0000587
Anders Carlssona79203b2009-01-18 01:56:57 +0000588 if (!*Name) {
589 // Missing ']'
590 return false;
591 }
Mike Stump11289f42009-09-09 15:08:12 +0000592
Anders Carlssona79203b2009-01-18 01:56:57 +0000593 std::string SymbolicName(Start, Name - Start);
Mike Stump11289f42009-09-09 15:08:12 +0000594
Craig Topper55765ca2015-10-21 02:34:10 +0000595 for (Index = 0; Index != OutputConstraints.size(); ++Index)
Chris Lattnerc16d4762009-04-26 17:57:12 +0000596 if (SymbolicName == OutputConstraints[Index].getName())
Anders Carlssona79203b2009-01-18 01:56:57 +0000597 return true;
Anders Carlssona79203b2009-01-18 01:56:57 +0000598
599 return false;
600}
601
Craig Topper55765ca2015-10-21 02:34:10 +0000602bool TargetInfo::validateInputConstraint(
603 MutableArrayRef<ConstraintInfo> OutputConstraints,
604 ConstraintInfo &Info) const {
Chris Lattnerc3f4c7b2009-04-26 17:19:08 +0000605 const char *Name = Info.ConstraintStr.c_str();
606
Duncan P. N. Exon Smithd68c7aa2013-12-16 03:20:06 +0000607 if (!*Name)
608 return false;
609
Anders Carlssonf511f642007-11-27 04:11:28 +0000610 while (*Name) {
611 switch (*Name) {
612 default:
613 // Check if we have a matching constraint
614 if (*Name >= '0' && *Name <= '9') {
David Majnemerb6b56432015-01-11 10:22:41 +0000615 const char *DigitStart = Name;
616 while (Name[1] >= '0' && Name[1] <= '9')
617 Name++;
618 const char *DigitEnd = Name;
619 unsigned i;
620 if (StringRef(DigitStart, DigitEnd - DigitStart + 1)
621 .getAsInteger(10, i))
622 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000623
Anders Carlssonf511f642007-11-27 04:11:28 +0000624 // Check if matching constraint is out of bounds.
Craig Topper55765ca2015-10-21 02:34:10 +0000625 if (i >= OutputConstraints.size()) return false;
Mike Stump11289f42009-09-09 15:08:12 +0000626
Anders Carlssonda1f5fc2010-11-03 02:22:29 +0000627 // A number must refer to an output only operand.
628 if (OutputConstraints[i].isReadWrite())
629 return false;
630
Eric Christopherb093d692015-10-09 18:39:59 +0000631 // If the constraint is already tied, it must be tied to the
Anders Carlsson2d5f8b42010-11-03 02:54:51 +0000632 // same operand referenced to by the number.
633 if (Info.hasTiedOperand() && Info.getTiedOperand() != i)
634 return false;
635
Mike Stump11289f42009-09-09 15:08:12 +0000636 // The constraint should have the same info as the respective
Anders Carlsson570c3572009-01-27 20:38:24 +0000637 // output constraint.
Chris Lattner4c92b512009-04-26 18:05:25 +0000638 Info.setTiedOperand(i, OutputConstraints[i]);
Chris Lattnerd9725f72009-04-26 07:16:29 +0000639 } else if (!validateAsmConstraint(Name, Info)) {
Daniel Dunbar81128e02008-08-25 09:46:27 +0000640 // FIXME: This error return is in place temporarily so we can
641 // add more constraints as we hit it. Eventually, an unknown
642 // constraint should just be treated as 'g'.
643 return false;
Anders Carlssona79203b2009-01-18 01:56:57 +0000644 }
645 break;
646 case '[': {
647 unsigned Index = 0;
Craig Topper55765ca2015-10-21 02:34:10 +0000648 if (!resolveSymbolicName(Name, OutputConstraints, Index))
Anders Carlssona79203b2009-01-18 01:56:57 +0000649 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000650
Eric Christopherb093d692015-10-09 18:39:59 +0000651 // If the constraint is already tied, it must be tied to the
Anders Carlsson2d5f8b42010-11-03 02:54:51 +0000652 // same operand referenced to by the number.
653 if (Info.hasTiedOperand() && Info.getTiedOperand() != Index)
654 return false;
655
David Majnemer55164f92015-01-11 09:57:13 +0000656 // A number must refer to an output only operand.
657 if (OutputConstraints[Index].isReadWrite())
658 return false;
659
Eli Friedman854f1102010-08-11 23:03:37 +0000660 Info.setTiedOperand(Index, OutputConstraints[Index]);
Anders Carlssona79203b2009-01-18 01:56:57 +0000661 break;
Mike Stump11289f42009-09-09 15:08:12 +0000662 }
Anders Carlsson050f4942007-12-08 19:32:57 +0000663 case '%': // commutative
664 // FIXME: Fail if % is used with the last operand.
665 break;
Anders Carlssonf511f642007-11-27 04:11:28 +0000666 case 'i': // immediate integer.
Anders Carlssona3a96af2008-03-09 06:02:02 +0000667 case 'n': // immediate integer with a known value.
Anders Carlssonf511f642007-11-27 04:11:28 +0000668 break;
Chris Lattnerdbcc5ca2009-05-06 04:33:31 +0000669 case 'I': // Various constant constraints with target-specific meanings.
670 case 'J':
671 case 'K':
672 case 'L':
673 case 'M':
674 case 'N':
675 case 'O':
676 case 'P':
Saleem Abdulrasoola2823572015-01-06 04:26:34 +0000677 if (!validateAsmConstraint(Name, Info))
678 return false;
Chris Lattnerdbcc5ca2009-05-06 04:33:31 +0000679 break;
Anders Carlssonf511f642007-11-27 04:11:28 +0000680 case 'r': // general register.
Chris Lattnerd9725f72009-04-26 07:16:29 +0000681 Info.setAllowsRegister();
Anders Carlssonf511f642007-11-27 04:11:28 +0000682 break;
683 case 'm': // memory operand.
Dale Johannesen8499f472010-09-07 18:40:41 +0000684 case 'o': // offsettable memory operand.
685 case 'V': // non-offsettable memory operand.
686 case '<': // autodecrement memory operand.
687 case '>': // autoincrement memory operand.
Chris Lattnerd9725f72009-04-26 07:16:29 +0000688 Info.setAllowsMemory();
Anders Carlssonf511f642007-11-27 04:11:28 +0000689 break;
690 case 'g': // general register, memory operand or immediate integer.
Anders Carlssone70cde12009-01-18 02:12:04 +0000691 case 'X': // any operand.
Chris Lattnerd9725f72009-04-26 07:16:29 +0000692 Info.setAllowsRegister();
693 Info.setAllowsMemory();
Anders Carlssonf511f642007-11-27 04:11:28 +0000694 break;
John Thompsonc467aa22010-09-21 22:04:54 +0000695 case 'E': // immediate floating point.
696 case 'F': // immediate floating point.
697 case 'p': // address operand.
698 break;
John Thompsona5c7d702010-08-10 19:20:14 +0000699 case ',': // multiple alternative constraint. Ignore comma.
700 break;
David Majnemerc71a5662015-01-11 09:39:03 +0000701 case '#': // Ignore as constraint.
702 while (Name[1] && Name[1] != ',')
703 Name++;
David Majnemerc71a5662015-01-11 09:39:03 +0000704 break;
John Thompsona5c7d702010-08-10 19:20:14 +0000705 case '?': // Disparage slightly code.
Chris Lattner57540c52011-04-15 05:22:18 +0000706 case '!': // Disparage severely.
Ulrich Weigand7bcc7ec2012-10-29 12:20:54 +0000707 case '*': // Ignore for choosing register preferences.
John Thompsona5c7d702010-08-10 19:20:14 +0000708 break; // Pass them.
Anders Carlssonf511f642007-11-27 04:11:28 +0000709 }
Mike Stump11289f42009-09-09 15:08:12 +0000710
Anders Carlssonf511f642007-11-27 04:11:28 +0000711 Name++;
712 }
Mike Stump11289f42009-09-09 15:08:12 +0000713
Anders Carlssonf511f642007-11-27 04:11:28 +0000714 return true;
715}