1f4a2713aSLionel Sambuc //===--- TargetInfo.cpp - Information about Target machine ----------------===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc // The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc //
10f4a2713aSLionel Sambuc // This file implements the TargetInfo and TargetInfoImpl interfaces.
11f4a2713aSLionel Sambuc //
12f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
13f4a2713aSLionel Sambuc
14f4a2713aSLionel Sambuc #include "clang/Basic/TargetInfo.h"
15f4a2713aSLionel Sambuc #include "clang/Basic/AddressSpaces.h"
16f4a2713aSLionel Sambuc #include "clang/Basic/CharInfo.h"
17f4a2713aSLionel Sambuc #include "clang/Basic/LangOptions.h"
18f4a2713aSLionel Sambuc #include "llvm/ADT/APFloat.h"
19f4a2713aSLionel Sambuc #include "llvm/ADT/STLExtras.h"
20f4a2713aSLionel Sambuc #include "llvm/Support/ErrorHandling.h"
21f4a2713aSLionel Sambuc #include <cstdlib>
22f4a2713aSLionel Sambuc using namespace clang;
23f4a2713aSLionel Sambuc
24f4a2713aSLionel Sambuc static const LangAS::Map DefaultAddrSpaceMap = { 0 };
25f4a2713aSLionel Sambuc
26f4a2713aSLionel Sambuc // TargetInfo Constructor.
TargetInfo(const llvm::Triple & T)27f4a2713aSLionel Sambuc TargetInfo::TargetInfo(const llvm::Triple &T) : TargetOpts(), Triple(T) {
28f4a2713aSLionel Sambuc // Set defaults. Defaults are set for a 32-bit RISC platform, like PPC or
29f4a2713aSLionel Sambuc // SPARC. These should be overridden by concrete targets as needed.
30f4a2713aSLionel Sambuc BigEndian = true;
31f4a2713aSLionel Sambuc TLSSupported = true;
32f4a2713aSLionel Sambuc NoAsmVariants = false;
33f4a2713aSLionel Sambuc PointerWidth = PointerAlign = 32;
34f4a2713aSLionel Sambuc BoolWidth = BoolAlign = 8;
35f4a2713aSLionel Sambuc IntWidth = IntAlign = 32;
36f4a2713aSLionel Sambuc LongWidth = LongAlign = 32;
37f4a2713aSLionel Sambuc LongLongWidth = LongLongAlign = 64;
38f4a2713aSLionel Sambuc SuitableAlign = 64;
39f4a2713aSLionel Sambuc MinGlobalAlign = 0;
40f4a2713aSLionel Sambuc HalfWidth = 16;
41f4a2713aSLionel Sambuc HalfAlign = 16;
42f4a2713aSLionel Sambuc FloatWidth = 32;
43f4a2713aSLionel Sambuc FloatAlign = 32;
44f4a2713aSLionel Sambuc DoubleWidth = 64;
45f4a2713aSLionel Sambuc DoubleAlign = 64;
46f4a2713aSLionel Sambuc LongDoubleWidth = 64;
47f4a2713aSLionel Sambuc LongDoubleAlign = 64;
48f4a2713aSLionel Sambuc LargeArrayMinWidth = 0;
49f4a2713aSLionel Sambuc LargeArrayAlign = 0;
50f4a2713aSLionel Sambuc MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 0;
51f4a2713aSLionel Sambuc MaxVectorAlign = 0;
52f4a2713aSLionel Sambuc SizeType = UnsignedLong;
53f4a2713aSLionel Sambuc PtrDiffType = SignedLong;
54f4a2713aSLionel Sambuc IntMaxType = SignedLongLong;
55f4a2713aSLionel Sambuc IntPtrType = SignedLong;
56f4a2713aSLionel Sambuc WCharType = SignedInt;
57f4a2713aSLionel Sambuc WIntType = SignedInt;
58f4a2713aSLionel Sambuc Char16Type = UnsignedShort;
59f4a2713aSLionel Sambuc Char32Type = UnsignedInt;
60f4a2713aSLionel Sambuc Int64Type = SignedLongLong;
61f4a2713aSLionel Sambuc SigAtomicType = SignedInt;
62f4a2713aSLionel Sambuc ProcessIDType = SignedInt;
63f4a2713aSLionel Sambuc UseSignedCharForObjCBool = true;
64f4a2713aSLionel Sambuc UseBitFieldTypeAlignment = true;
65f4a2713aSLionel Sambuc UseZeroLengthBitfieldAlignment = false;
66f4a2713aSLionel Sambuc ZeroLengthBitfieldBoundary = 0;
67f4a2713aSLionel Sambuc HalfFormat = &llvm::APFloat::IEEEhalf;
68f4a2713aSLionel Sambuc FloatFormat = &llvm::APFloat::IEEEsingle;
69f4a2713aSLionel Sambuc DoubleFormat = &llvm::APFloat::IEEEdouble;
70f4a2713aSLionel Sambuc LongDoubleFormat = &llvm::APFloat::IEEEdouble;
71*0a6a1f1dSLionel Sambuc DescriptionString = nullptr;
72f4a2713aSLionel Sambuc UserLabelPrefix = "_";
73f4a2713aSLionel Sambuc MCountName = "mcount";
74f4a2713aSLionel Sambuc RegParmMax = 0;
75f4a2713aSLionel Sambuc SSERegParmMax = 0;
76f4a2713aSLionel Sambuc HasAlignMac68kSupport = false;
77f4a2713aSLionel Sambuc
78f4a2713aSLionel Sambuc // Default to no types using fpret.
79f4a2713aSLionel Sambuc RealTypeUsesObjCFPRet = 0;
80f4a2713aSLionel Sambuc
81f4a2713aSLionel Sambuc // Default to not using fp2ret for __Complex long double
82f4a2713aSLionel Sambuc ComplexLongDoubleUsesFP2Ret = false;
83f4a2713aSLionel Sambuc
84*0a6a1f1dSLionel Sambuc // Set the C++ ABI based on the triple.
85*0a6a1f1dSLionel Sambuc TheCXXABI.set(Triple.isKnownWindowsMSVCEnvironment()
86*0a6a1f1dSLionel Sambuc ? TargetCXXABI::Microsoft
87*0a6a1f1dSLionel Sambuc : TargetCXXABI::GenericItanium);
88f4a2713aSLionel Sambuc
89f4a2713aSLionel Sambuc // Default to an empty address space map.
90f4a2713aSLionel Sambuc AddrSpaceMap = &DefaultAddrSpaceMap;
91f4a2713aSLionel Sambuc UseAddrSpaceMapMangling = false;
92f4a2713aSLionel Sambuc
93f4a2713aSLionel Sambuc // Default to an unknown platform name.
94f4a2713aSLionel Sambuc PlatformName = "unknown";
95f4a2713aSLionel Sambuc PlatformMinVersion = VersionTuple();
96f4a2713aSLionel Sambuc }
97f4a2713aSLionel Sambuc
98f4a2713aSLionel Sambuc // Out of line virtual dtor for TargetInfo.
~TargetInfo()99f4a2713aSLionel Sambuc TargetInfo::~TargetInfo() {}
100f4a2713aSLionel Sambuc
101f4a2713aSLionel Sambuc /// getTypeName - Return the user string for the specified integer type enum.
102f4a2713aSLionel Sambuc /// For example, SignedShort -> "short".
getTypeName(IntType T)103f4a2713aSLionel Sambuc const char *TargetInfo::getTypeName(IntType T) {
104f4a2713aSLionel Sambuc switch (T) {
105f4a2713aSLionel Sambuc default: llvm_unreachable("not an integer!");
106*0a6a1f1dSLionel Sambuc case SignedChar: return "signed char";
107f4a2713aSLionel Sambuc case UnsignedChar: return "unsigned char";
108f4a2713aSLionel Sambuc case SignedShort: return "short";
109f4a2713aSLionel Sambuc case UnsignedShort: return "unsigned short";
110f4a2713aSLionel Sambuc case SignedInt: return "int";
111f4a2713aSLionel Sambuc case UnsignedInt: return "unsigned int";
112f4a2713aSLionel Sambuc case SignedLong: return "long int";
113f4a2713aSLionel Sambuc case UnsignedLong: return "long unsigned int";
114f4a2713aSLionel Sambuc case SignedLongLong: return "long long int";
115f4a2713aSLionel Sambuc case UnsignedLongLong: return "long long unsigned int";
116f4a2713aSLionel Sambuc }
117f4a2713aSLionel Sambuc }
118f4a2713aSLionel Sambuc
119f4a2713aSLionel Sambuc /// getTypeConstantSuffix - Return the constant suffix for the specified
120f4a2713aSLionel Sambuc /// integer type enum. For example, SignedLong -> "L".
getTypeConstantSuffix(IntType T) const121*0a6a1f1dSLionel Sambuc const char *TargetInfo::getTypeConstantSuffix(IntType T) const {
122f4a2713aSLionel Sambuc switch (T) {
123f4a2713aSLionel Sambuc default: llvm_unreachable("not an integer!");
124f4a2713aSLionel Sambuc case SignedChar:
125f4a2713aSLionel Sambuc case SignedShort:
126f4a2713aSLionel Sambuc case SignedInt: return "";
127f4a2713aSLionel Sambuc case SignedLong: return "L";
128f4a2713aSLionel Sambuc case SignedLongLong: return "LL";
129f4a2713aSLionel Sambuc case UnsignedChar:
130*0a6a1f1dSLionel Sambuc if (getCharWidth() < getIntWidth())
131*0a6a1f1dSLionel Sambuc return "";
132f4a2713aSLionel Sambuc case UnsignedShort:
133*0a6a1f1dSLionel Sambuc if (getShortWidth() < getIntWidth())
134*0a6a1f1dSLionel Sambuc return "";
135f4a2713aSLionel Sambuc case UnsignedInt: return "U";
136f4a2713aSLionel Sambuc case UnsignedLong: return "UL";
137f4a2713aSLionel Sambuc case UnsignedLongLong: return "ULL";
138f4a2713aSLionel Sambuc }
139f4a2713aSLionel Sambuc }
140f4a2713aSLionel Sambuc
141*0a6a1f1dSLionel Sambuc /// getTypeFormatModifier - Return the printf format modifier for the
142*0a6a1f1dSLionel Sambuc /// specified integer type enum. For example, SignedLong -> "l".
143*0a6a1f1dSLionel Sambuc
getTypeFormatModifier(IntType T)144*0a6a1f1dSLionel Sambuc const char *TargetInfo::getTypeFormatModifier(IntType T) {
145*0a6a1f1dSLionel Sambuc switch (T) {
146*0a6a1f1dSLionel Sambuc default: llvm_unreachable("not an integer!");
147*0a6a1f1dSLionel Sambuc case SignedChar:
148*0a6a1f1dSLionel Sambuc case UnsignedChar: return "hh";
149*0a6a1f1dSLionel Sambuc case SignedShort:
150*0a6a1f1dSLionel Sambuc case UnsignedShort: return "h";
151*0a6a1f1dSLionel Sambuc case SignedInt:
152*0a6a1f1dSLionel Sambuc case UnsignedInt: return "";
153*0a6a1f1dSLionel Sambuc case SignedLong:
154*0a6a1f1dSLionel Sambuc case UnsignedLong: return "l";
155*0a6a1f1dSLionel Sambuc case SignedLongLong:
156*0a6a1f1dSLionel Sambuc case UnsignedLongLong: return "ll";
157*0a6a1f1dSLionel Sambuc }
158*0a6a1f1dSLionel Sambuc }
159*0a6a1f1dSLionel Sambuc
160f4a2713aSLionel Sambuc /// getTypeWidth - Return the width (in bits) of the specified integer type
161f4a2713aSLionel Sambuc /// enum. For example, SignedInt -> getIntWidth().
getTypeWidth(IntType T) const162f4a2713aSLionel Sambuc unsigned TargetInfo::getTypeWidth(IntType T) const {
163f4a2713aSLionel Sambuc switch (T) {
164f4a2713aSLionel Sambuc default: llvm_unreachable("not an integer!");
165f4a2713aSLionel Sambuc case SignedChar:
166f4a2713aSLionel Sambuc case UnsignedChar: return getCharWidth();
167f4a2713aSLionel Sambuc case SignedShort:
168f4a2713aSLionel Sambuc case UnsignedShort: return getShortWidth();
169f4a2713aSLionel Sambuc case SignedInt:
170f4a2713aSLionel Sambuc case UnsignedInt: return getIntWidth();
171f4a2713aSLionel Sambuc case SignedLong:
172f4a2713aSLionel Sambuc case UnsignedLong: return getLongWidth();
173f4a2713aSLionel Sambuc case SignedLongLong:
174f4a2713aSLionel Sambuc case UnsignedLongLong: return getLongLongWidth();
175f4a2713aSLionel Sambuc };
176f4a2713aSLionel Sambuc }
177f4a2713aSLionel Sambuc
getIntTypeByWidth(unsigned BitWidth,bool IsSigned) const178f4a2713aSLionel Sambuc TargetInfo::IntType TargetInfo::getIntTypeByWidth(
179f4a2713aSLionel Sambuc unsigned BitWidth, bool IsSigned) const {
180f4a2713aSLionel Sambuc if (getCharWidth() == BitWidth)
181f4a2713aSLionel Sambuc return IsSigned ? SignedChar : UnsignedChar;
182f4a2713aSLionel Sambuc if (getShortWidth() == BitWidth)
183f4a2713aSLionel Sambuc return IsSigned ? SignedShort : UnsignedShort;
184f4a2713aSLionel Sambuc if (getIntWidth() == BitWidth)
185f4a2713aSLionel Sambuc return IsSigned ? SignedInt : UnsignedInt;
186f4a2713aSLionel Sambuc if (getLongWidth() == BitWidth)
187f4a2713aSLionel Sambuc return IsSigned ? SignedLong : UnsignedLong;
188f4a2713aSLionel Sambuc if (getLongLongWidth() == BitWidth)
189f4a2713aSLionel Sambuc return IsSigned ? SignedLongLong : UnsignedLongLong;
190f4a2713aSLionel Sambuc return NoInt;
191f4a2713aSLionel Sambuc }
192f4a2713aSLionel Sambuc
getLeastIntTypeByWidth(unsigned BitWidth,bool IsSigned) const193*0a6a1f1dSLionel Sambuc TargetInfo::IntType TargetInfo::getLeastIntTypeByWidth(unsigned BitWidth,
194*0a6a1f1dSLionel Sambuc bool IsSigned) const {
195*0a6a1f1dSLionel Sambuc if (getCharWidth() >= BitWidth)
196*0a6a1f1dSLionel Sambuc return IsSigned ? SignedChar : UnsignedChar;
197*0a6a1f1dSLionel Sambuc if (getShortWidth() >= BitWidth)
198*0a6a1f1dSLionel Sambuc return IsSigned ? SignedShort : UnsignedShort;
199*0a6a1f1dSLionel Sambuc if (getIntWidth() >= BitWidth)
200*0a6a1f1dSLionel Sambuc return IsSigned ? SignedInt : UnsignedInt;
201*0a6a1f1dSLionel Sambuc if (getLongWidth() >= BitWidth)
202*0a6a1f1dSLionel Sambuc return IsSigned ? SignedLong : UnsignedLong;
203*0a6a1f1dSLionel Sambuc if (getLongLongWidth() >= BitWidth)
204*0a6a1f1dSLionel Sambuc return IsSigned ? SignedLongLong : UnsignedLongLong;
205*0a6a1f1dSLionel Sambuc return NoInt;
206*0a6a1f1dSLionel Sambuc }
207*0a6a1f1dSLionel Sambuc
getRealTypeByWidth(unsigned BitWidth) const208f4a2713aSLionel Sambuc TargetInfo::RealType TargetInfo::getRealTypeByWidth(unsigned BitWidth) const {
209f4a2713aSLionel Sambuc if (getFloatWidth() == BitWidth)
210f4a2713aSLionel Sambuc return Float;
211f4a2713aSLionel Sambuc if (getDoubleWidth() == BitWidth)
212f4a2713aSLionel Sambuc return Double;
213f4a2713aSLionel Sambuc
214f4a2713aSLionel Sambuc switch (BitWidth) {
215f4a2713aSLionel Sambuc case 96:
216f4a2713aSLionel Sambuc if (&getLongDoubleFormat() == &llvm::APFloat::x87DoubleExtended)
217f4a2713aSLionel Sambuc return LongDouble;
218f4a2713aSLionel Sambuc break;
219f4a2713aSLionel Sambuc case 128:
220f4a2713aSLionel Sambuc if (&getLongDoubleFormat() == &llvm::APFloat::PPCDoubleDouble ||
221f4a2713aSLionel Sambuc &getLongDoubleFormat() == &llvm::APFloat::IEEEquad)
222f4a2713aSLionel Sambuc return LongDouble;
223f4a2713aSLionel Sambuc break;
224f4a2713aSLionel Sambuc }
225f4a2713aSLionel Sambuc
226f4a2713aSLionel Sambuc return NoFloat;
227f4a2713aSLionel Sambuc }
228f4a2713aSLionel Sambuc
229f4a2713aSLionel Sambuc /// getTypeAlign - Return the alignment (in bits) of the specified integer type
230f4a2713aSLionel Sambuc /// enum. For example, SignedInt -> getIntAlign().
getTypeAlign(IntType T) const231f4a2713aSLionel Sambuc unsigned TargetInfo::getTypeAlign(IntType T) const {
232f4a2713aSLionel Sambuc switch (T) {
233f4a2713aSLionel Sambuc default: llvm_unreachable("not an integer!");
234f4a2713aSLionel Sambuc case SignedChar:
235f4a2713aSLionel Sambuc case UnsignedChar: return getCharAlign();
236f4a2713aSLionel Sambuc case SignedShort:
237f4a2713aSLionel Sambuc case UnsignedShort: return getShortAlign();
238f4a2713aSLionel Sambuc case SignedInt:
239f4a2713aSLionel Sambuc case UnsignedInt: return getIntAlign();
240f4a2713aSLionel Sambuc case SignedLong:
241f4a2713aSLionel Sambuc case UnsignedLong: return getLongAlign();
242f4a2713aSLionel Sambuc case SignedLongLong:
243f4a2713aSLionel Sambuc case UnsignedLongLong: return getLongLongAlign();
244f4a2713aSLionel Sambuc };
245f4a2713aSLionel Sambuc }
246f4a2713aSLionel Sambuc
247f4a2713aSLionel Sambuc /// isTypeSigned - Return whether an integer types is signed. Returns true if
248f4a2713aSLionel Sambuc /// the type is signed; false otherwise.
isTypeSigned(IntType T)249f4a2713aSLionel Sambuc bool TargetInfo::isTypeSigned(IntType T) {
250f4a2713aSLionel Sambuc switch (T) {
251f4a2713aSLionel Sambuc default: llvm_unreachable("not an integer!");
252f4a2713aSLionel Sambuc case SignedChar:
253f4a2713aSLionel Sambuc case SignedShort:
254f4a2713aSLionel Sambuc case SignedInt:
255f4a2713aSLionel Sambuc case SignedLong:
256f4a2713aSLionel Sambuc case SignedLongLong:
257f4a2713aSLionel Sambuc return true;
258f4a2713aSLionel Sambuc case UnsignedChar:
259f4a2713aSLionel Sambuc case UnsignedShort:
260f4a2713aSLionel Sambuc case UnsignedInt:
261f4a2713aSLionel Sambuc case UnsignedLong:
262f4a2713aSLionel Sambuc case UnsignedLongLong:
263f4a2713aSLionel Sambuc return false;
264f4a2713aSLionel Sambuc };
265f4a2713aSLionel Sambuc }
266f4a2713aSLionel Sambuc
267*0a6a1f1dSLionel Sambuc /// adjust - Set forced language options.
268f4a2713aSLionel Sambuc /// Apply changes to the target information with respect to certain
269f4a2713aSLionel Sambuc /// language options which change the target configuration.
adjust(const LangOptions & Opts)270*0a6a1f1dSLionel Sambuc void TargetInfo::adjust(const LangOptions &Opts) {
271f4a2713aSLionel Sambuc if (Opts.NoBitFieldTypeAlign)
272f4a2713aSLionel Sambuc UseBitFieldTypeAlignment = false;
273f4a2713aSLionel Sambuc if (Opts.ShortWChar)
274f4a2713aSLionel Sambuc WCharType = UnsignedShort;
275f4a2713aSLionel Sambuc
276f4a2713aSLionel Sambuc if (Opts.OpenCL) {
277f4a2713aSLionel Sambuc // OpenCL C requires specific widths for types, irrespective of
278f4a2713aSLionel Sambuc // what these normally are for the target.
279f4a2713aSLionel Sambuc // We also define long long and long double here, although the
280f4a2713aSLionel Sambuc // OpenCL standard only mentions these as "reserved".
281f4a2713aSLionel Sambuc IntWidth = IntAlign = 32;
282f4a2713aSLionel Sambuc LongWidth = LongAlign = 64;
283f4a2713aSLionel Sambuc LongLongWidth = LongLongAlign = 128;
284f4a2713aSLionel Sambuc HalfWidth = HalfAlign = 16;
285f4a2713aSLionel Sambuc FloatWidth = FloatAlign = 32;
286*0a6a1f1dSLionel Sambuc
287*0a6a1f1dSLionel Sambuc // Embedded 32-bit targets (OpenCL EP) might have double C type
288*0a6a1f1dSLionel Sambuc // defined as float. Let's not override this as it might lead
289*0a6a1f1dSLionel Sambuc // to generating illegal code that uses 64bit doubles.
290*0a6a1f1dSLionel Sambuc if (DoubleWidth != FloatWidth) {
291f4a2713aSLionel Sambuc DoubleWidth = DoubleAlign = 64;
292*0a6a1f1dSLionel Sambuc DoubleFormat = &llvm::APFloat::IEEEdouble;
293*0a6a1f1dSLionel Sambuc }
294f4a2713aSLionel Sambuc LongDoubleWidth = LongDoubleAlign = 128;
295f4a2713aSLionel Sambuc
296f4a2713aSLionel Sambuc assert(PointerWidth == 32 || PointerWidth == 64);
297f4a2713aSLionel Sambuc bool Is32BitArch = PointerWidth == 32;
298f4a2713aSLionel Sambuc SizeType = Is32BitArch ? UnsignedInt : UnsignedLong;
299f4a2713aSLionel Sambuc PtrDiffType = Is32BitArch ? SignedInt : SignedLong;
300f4a2713aSLionel Sambuc IntPtrType = Is32BitArch ? SignedInt : SignedLong;
301f4a2713aSLionel Sambuc
302f4a2713aSLionel Sambuc IntMaxType = SignedLongLong;
303f4a2713aSLionel Sambuc Int64Type = SignedLong;
304f4a2713aSLionel Sambuc
305f4a2713aSLionel Sambuc HalfFormat = &llvm::APFloat::IEEEhalf;
306f4a2713aSLionel Sambuc FloatFormat = &llvm::APFloat::IEEEsingle;
307f4a2713aSLionel Sambuc LongDoubleFormat = &llvm::APFloat::IEEEquad;
308f4a2713aSLionel Sambuc }
309f4a2713aSLionel Sambuc }
310f4a2713aSLionel Sambuc
311f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
312f4a2713aSLionel Sambuc
313f4a2713aSLionel Sambuc
removeGCCRegisterPrefix(StringRef Name)314f4a2713aSLionel Sambuc static StringRef removeGCCRegisterPrefix(StringRef Name) {
315f4a2713aSLionel Sambuc if (Name[0] == '%' || Name[0] == '#')
316f4a2713aSLionel Sambuc Name = Name.substr(1);
317f4a2713aSLionel Sambuc
318f4a2713aSLionel Sambuc return Name;
319f4a2713aSLionel Sambuc }
320f4a2713aSLionel Sambuc
321f4a2713aSLionel Sambuc /// isValidClobber - Returns whether the passed in string is
322f4a2713aSLionel Sambuc /// a valid clobber in an inline asm statement. This is used by
323f4a2713aSLionel Sambuc /// Sema.
isValidClobber(StringRef Name) const324f4a2713aSLionel Sambuc bool TargetInfo::isValidClobber(StringRef Name) const {
325f4a2713aSLionel Sambuc return (isValidGCCRegisterName(Name) ||
326f4a2713aSLionel Sambuc Name == "memory" || Name == "cc");
327f4a2713aSLionel Sambuc }
328f4a2713aSLionel Sambuc
329f4a2713aSLionel Sambuc /// isValidGCCRegisterName - Returns whether the passed in string
330f4a2713aSLionel Sambuc /// is a valid register name according to GCC. This is used by Sema for
331f4a2713aSLionel Sambuc /// inline asm statements.
isValidGCCRegisterName(StringRef Name) const332f4a2713aSLionel Sambuc bool TargetInfo::isValidGCCRegisterName(StringRef Name) const {
333f4a2713aSLionel Sambuc if (Name.empty())
334f4a2713aSLionel Sambuc return false;
335f4a2713aSLionel Sambuc
336f4a2713aSLionel Sambuc const char * const *Names;
337f4a2713aSLionel Sambuc unsigned NumNames;
338f4a2713aSLionel Sambuc
339f4a2713aSLionel Sambuc // Get rid of any register prefix.
340f4a2713aSLionel Sambuc Name = removeGCCRegisterPrefix(Name);
341*0a6a1f1dSLionel Sambuc if (Name.empty())
342*0a6a1f1dSLionel Sambuc return false;
343f4a2713aSLionel Sambuc
344f4a2713aSLionel Sambuc getGCCRegNames(Names, NumNames);
345f4a2713aSLionel Sambuc
346f4a2713aSLionel Sambuc // If we have a number it maps to an entry in the register name array.
347f4a2713aSLionel Sambuc if (isDigit(Name[0])) {
348f4a2713aSLionel Sambuc int n;
349f4a2713aSLionel Sambuc if (!Name.getAsInteger(0, n))
350f4a2713aSLionel Sambuc return n >= 0 && (unsigned)n < NumNames;
351f4a2713aSLionel Sambuc }
352f4a2713aSLionel Sambuc
353f4a2713aSLionel Sambuc // Check register names.
354f4a2713aSLionel Sambuc for (unsigned i = 0; i < NumNames; i++) {
355f4a2713aSLionel Sambuc if (Name == Names[i])
356f4a2713aSLionel Sambuc return true;
357f4a2713aSLionel Sambuc }
358f4a2713aSLionel Sambuc
359f4a2713aSLionel Sambuc // Check any additional names that we have.
360f4a2713aSLionel Sambuc const AddlRegName *AddlNames;
361f4a2713aSLionel Sambuc unsigned NumAddlNames;
362f4a2713aSLionel Sambuc getGCCAddlRegNames(AddlNames, NumAddlNames);
363f4a2713aSLionel Sambuc for (unsigned i = 0; i < NumAddlNames; i++)
364f4a2713aSLionel Sambuc for (unsigned j = 0; j < llvm::array_lengthof(AddlNames[i].Names); j++) {
365f4a2713aSLionel Sambuc if (!AddlNames[i].Names[j])
366f4a2713aSLionel Sambuc break;
367f4a2713aSLionel Sambuc // Make sure the register that the additional name is for is within
368f4a2713aSLionel Sambuc // the bounds of the register names from above.
369f4a2713aSLionel Sambuc if (AddlNames[i].Names[j] == Name && AddlNames[i].RegNum < NumNames)
370f4a2713aSLionel Sambuc return true;
371f4a2713aSLionel Sambuc }
372f4a2713aSLionel Sambuc
373f4a2713aSLionel Sambuc // Now check aliases.
374f4a2713aSLionel Sambuc const GCCRegAlias *Aliases;
375f4a2713aSLionel Sambuc unsigned NumAliases;
376f4a2713aSLionel Sambuc
377f4a2713aSLionel Sambuc getGCCRegAliases(Aliases, NumAliases);
378f4a2713aSLionel Sambuc for (unsigned i = 0; i < NumAliases; i++) {
379f4a2713aSLionel Sambuc for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
380f4a2713aSLionel Sambuc if (!Aliases[i].Aliases[j])
381f4a2713aSLionel Sambuc break;
382f4a2713aSLionel Sambuc if (Aliases[i].Aliases[j] == Name)
383f4a2713aSLionel Sambuc return true;
384f4a2713aSLionel Sambuc }
385f4a2713aSLionel Sambuc }
386f4a2713aSLionel Sambuc
387f4a2713aSLionel Sambuc return false;
388f4a2713aSLionel Sambuc }
389f4a2713aSLionel Sambuc
390f4a2713aSLionel Sambuc StringRef
getNormalizedGCCRegisterName(StringRef Name) const391f4a2713aSLionel Sambuc TargetInfo::getNormalizedGCCRegisterName(StringRef Name) const {
392f4a2713aSLionel Sambuc assert(isValidGCCRegisterName(Name) && "Invalid register passed in");
393f4a2713aSLionel Sambuc
394f4a2713aSLionel Sambuc // Get rid of any register prefix.
395f4a2713aSLionel Sambuc Name = removeGCCRegisterPrefix(Name);
396f4a2713aSLionel Sambuc
397f4a2713aSLionel Sambuc const char * const *Names;
398f4a2713aSLionel Sambuc unsigned NumNames;
399f4a2713aSLionel Sambuc
400f4a2713aSLionel Sambuc getGCCRegNames(Names, NumNames);
401f4a2713aSLionel Sambuc
402f4a2713aSLionel Sambuc // First, check if we have a number.
403f4a2713aSLionel Sambuc if (isDigit(Name[0])) {
404f4a2713aSLionel Sambuc int n;
405f4a2713aSLionel Sambuc if (!Name.getAsInteger(0, n)) {
406f4a2713aSLionel Sambuc assert(n >= 0 && (unsigned)n < NumNames &&
407f4a2713aSLionel Sambuc "Out of bounds register number!");
408f4a2713aSLionel Sambuc return Names[n];
409f4a2713aSLionel Sambuc }
410f4a2713aSLionel Sambuc }
411f4a2713aSLionel Sambuc
412f4a2713aSLionel Sambuc // Check any additional names that we have.
413f4a2713aSLionel Sambuc const AddlRegName *AddlNames;
414f4a2713aSLionel Sambuc unsigned NumAddlNames;
415f4a2713aSLionel Sambuc getGCCAddlRegNames(AddlNames, NumAddlNames);
416f4a2713aSLionel Sambuc for (unsigned i = 0; i < NumAddlNames; i++)
417f4a2713aSLionel Sambuc for (unsigned j = 0; j < llvm::array_lengthof(AddlNames[i].Names); j++) {
418f4a2713aSLionel Sambuc if (!AddlNames[i].Names[j])
419f4a2713aSLionel Sambuc break;
420f4a2713aSLionel Sambuc // Make sure the register that the additional name is for is within
421f4a2713aSLionel Sambuc // the bounds of the register names from above.
422f4a2713aSLionel Sambuc if (AddlNames[i].Names[j] == Name && AddlNames[i].RegNum < NumNames)
423f4a2713aSLionel Sambuc return Name;
424f4a2713aSLionel Sambuc }
425f4a2713aSLionel Sambuc
426f4a2713aSLionel Sambuc // Now check aliases.
427f4a2713aSLionel Sambuc const GCCRegAlias *Aliases;
428f4a2713aSLionel Sambuc unsigned NumAliases;
429f4a2713aSLionel Sambuc
430f4a2713aSLionel Sambuc getGCCRegAliases(Aliases, NumAliases);
431f4a2713aSLionel Sambuc for (unsigned i = 0; i < NumAliases; i++) {
432f4a2713aSLionel Sambuc for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
433f4a2713aSLionel Sambuc if (!Aliases[i].Aliases[j])
434f4a2713aSLionel Sambuc break;
435f4a2713aSLionel Sambuc if (Aliases[i].Aliases[j] == Name)
436f4a2713aSLionel Sambuc return Aliases[i].Register;
437f4a2713aSLionel Sambuc }
438f4a2713aSLionel Sambuc }
439f4a2713aSLionel Sambuc
440f4a2713aSLionel Sambuc return Name;
441f4a2713aSLionel Sambuc }
442f4a2713aSLionel Sambuc
validateOutputConstraint(ConstraintInfo & Info) const443f4a2713aSLionel Sambuc bool TargetInfo::validateOutputConstraint(ConstraintInfo &Info) const {
444f4a2713aSLionel Sambuc const char *Name = Info.getConstraintStr().c_str();
445f4a2713aSLionel Sambuc // An output constraint must start with '=' or '+'
446f4a2713aSLionel Sambuc if (*Name != '=' && *Name != '+')
447f4a2713aSLionel Sambuc return false;
448f4a2713aSLionel Sambuc
449f4a2713aSLionel Sambuc if (*Name == '+')
450f4a2713aSLionel Sambuc Info.setIsReadWrite();
451f4a2713aSLionel Sambuc
452f4a2713aSLionel Sambuc Name++;
453f4a2713aSLionel Sambuc while (*Name) {
454f4a2713aSLionel Sambuc switch (*Name) {
455f4a2713aSLionel Sambuc default:
456f4a2713aSLionel Sambuc if (!validateAsmConstraint(Name, Info)) {
457f4a2713aSLionel Sambuc // FIXME: We temporarily return false
458f4a2713aSLionel Sambuc // so we can add more constraints as we hit it.
459f4a2713aSLionel Sambuc // Eventually, an unknown constraint should just be treated as 'g'.
460f4a2713aSLionel Sambuc return false;
461f4a2713aSLionel Sambuc }
462*0a6a1f1dSLionel Sambuc break;
463f4a2713aSLionel Sambuc case '&': // early clobber.
464*0a6a1f1dSLionel Sambuc Info.setEarlyClobber();
465f4a2713aSLionel Sambuc break;
466f4a2713aSLionel Sambuc case '%': // commutative.
467f4a2713aSLionel Sambuc // FIXME: Check that there is a another register after this one.
468f4a2713aSLionel Sambuc break;
469f4a2713aSLionel Sambuc case 'r': // general register.
470f4a2713aSLionel Sambuc Info.setAllowsRegister();
471f4a2713aSLionel Sambuc break;
472f4a2713aSLionel Sambuc case 'm': // memory operand.
473f4a2713aSLionel Sambuc case 'o': // offsetable memory operand.
474f4a2713aSLionel Sambuc case 'V': // non-offsetable memory operand.
475f4a2713aSLionel Sambuc case '<': // autodecrement memory operand.
476f4a2713aSLionel Sambuc case '>': // autoincrement memory operand.
477f4a2713aSLionel Sambuc Info.setAllowsMemory();
478f4a2713aSLionel Sambuc break;
479f4a2713aSLionel Sambuc case 'g': // general register, memory operand or immediate integer.
480f4a2713aSLionel Sambuc case 'X': // any operand.
481f4a2713aSLionel Sambuc Info.setAllowsRegister();
482f4a2713aSLionel Sambuc Info.setAllowsMemory();
483f4a2713aSLionel Sambuc break;
484f4a2713aSLionel Sambuc case ',': // multiple alternative constraint. Pass it.
485f4a2713aSLionel Sambuc // Handle additional optional '=' or '+' modifiers.
486f4a2713aSLionel Sambuc if (Name[1] == '=' || Name[1] == '+')
487f4a2713aSLionel Sambuc Name++;
488f4a2713aSLionel Sambuc break;
489*0a6a1f1dSLionel Sambuc case '#': // Ignore as constraint.
490*0a6a1f1dSLionel Sambuc while (Name[1] && Name[1] != ',')
491*0a6a1f1dSLionel Sambuc Name++;
492*0a6a1f1dSLionel Sambuc break;
493f4a2713aSLionel Sambuc case '?': // Disparage slightly code.
494f4a2713aSLionel Sambuc case '!': // Disparage severely.
495f4a2713aSLionel Sambuc case '*': // Ignore for choosing register preferences.
496f4a2713aSLionel Sambuc break; // Pass them.
497f4a2713aSLionel Sambuc }
498f4a2713aSLionel Sambuc
499f4a2713aSLionel Sambuc Name++;
500f4a2713aSLionel Sambuc }
501f4a2713aSLionel Sambuc
502*0a6a1f1dSLionel Sambuc // Early clobber with a read-write constraint which doesn't permit registers
503*0a6a1f1dSLionel Sambuc // is invalid.
504*0a6a1f1dSLionel Sambuc if (Info.earlyClobber() && Info.isReadWrite() && !Info.allowsRegister())
505*0a6a1f1dSLionel Sambuc return false;
506*0a6a1f1dSLionel Sambuc
507f4a2713aSLionel Sambuc // If a constraint allows neither memory nor register operands it contains
508f4a2713aSLionel Sambuc // only modifiers. Reject it.
509f4a2713aSLionel Sambuc return Info.allowsMemory() || Info.allowsRegister();
510f4a2713aSLionel Sambuc }
511f4a2713aSLionel Sambuc
resolveSymbolicName(const char * & Name,ConstraintInfo * OutputConstraints,unsigned NumOutputs,unsigned & Index) const512f4a2713aSLionel Sambuc bool TargetInfo::resolveSymbolicName(const char *&Name,
513f4a2713aSLionel Sambuc ConstraintInfo *OutputConstraints,
514f4a2713aSLionel Sambuc unsigned NumOutputs,
515f4a2713aSLionel Sambuc unsigned &Index) const {
516f4a2713aSLionel Sambuc assert(*Name == '[' && "Symbolic name did not start with '['");
517f4a2713aSLionel Sambuc Name++;
518f4a2713aSLionel Sambuc const char *Start = Name;
519f4a2713aSLionel Sambuc while (*Name && *Name != ']')
520f4a2713aSLionel Sambuc Name++;
521f4a2713aSLionel Sambuc
522f4a2713aSLionel Sambuc if (!*Name) {
523f4a2713aSLionel Sambuc // Missing ']'
524f4a2713aSLionel Sambuc return false;
525f4a2713aSLionel Sambuc }
526f4a2713aSLionel Sambuc
527f4a2713aSLionel Sambuc std::string SymbolicName(Start, Name - Start);
528f4a2713aSLionel Sambuc
529f4a2713aSLionel Sambuc for (Index = 0; Index != NumOutputs; ++Index)
530f4a2713aSLionel Sambuc if (SymbolicName == OutputConstraints[Index].getName())
531f4a2713aSLionel Sambuc return true;
532f4a2713aSLionel Sambuc
533f4a2713aSLionel Sambuc return false;
534f4a2713aSLionel Sambuc }
535f4a2713aSLionel Sambuc
validateInputConstraint(ConstraintInfo * OutputConstraints,unsigned NumOutputs,ConstraintInfo & Info) const536f4a2713aSLionel Sambuc bool TargetInfo::validateInputConstraint(ConstraintInfo *OutputConstraints,
537f4a2713aSLionel Sambuc unsigned NumOutputs,
538f4a2713aSLionel Sambuc ConstraintInfo &Info) const {
539f4a2713aSLionel Sambuc const char *Name = Info.ConstraintStr.c_str();
540f4a2713aSLionel Sambuc
541*0a6a1f1dSLionel Sambuc if (!*Name)
542*0a6a1f1dSLionel Sambuc return false;
543*0a6a1f1dSLionel Sambuc
544f4a2713aSLionel Sambuc while (*Name) {
545f4a2713aSLionel Sambuc switch (*Name) {
546f4a2713aSLionel Sambuc default:
547f4a2713aSLionel Sambuc // Check if we have a matching constraint
548f4a2713aSLionel Sambuc if (*Name >= '0' && *Name <= '9') {
549*0a6a1f1dSLionel Sambuc const char *DigitStart = Name;
550*0a6a1f1dSLionel Sambuc while (Name[1] >= '0' && Name[1] <= '9')
551*0a6a1f1dSLionel Sambuc Name++;
552*0a6a1f1dSLionel Sambuc const char *DigitEnd = Name;
553*0a6a1f1dSLionel Sambuc unsigned i;
554*0a6a1f1dSLionel Sambuc if (StringRef(DigitStart, DigitEnd - DigitStart + 1)
555*0a6a1f1dSLionel Sambuc .getAsInteger(10, i))
556*0a6a1f1dSLionel Sambuc return false;
557f4a2713aSLionel Sambuc
558f4a2713aSLionel Sambuc // Check if matching constraint is out of bounds.
559*0a6a1f1dSLionel Sambuc if (i >= NumOutputs) return false;
560f4a2713aSLionel Sambuc
561f4a2713aSLionel Sambuc // A number must refer to an output only operand.
562f4a2713aSLionel Sambuc if (OutputConstraints[i].isReadWrite())
563f4a2713aSLionel Sambuc return false;
564f4a2713aSLionel Sambuc
565f4a2713aSLionel Sambuc // If the constraint is already tied, it must be tied to the
566f4a2713aSLionel Sambuc // same operand referenced to by the number.
567f4a2713aSLionel Sambuc if (Info.hasTiedOperand() && Info.getTiedOperand() != i)
568f4a2713aSLionel Sambuc return false;
569f4a2713aSLionel Sambuc
570f4a2713aSLionel Sambuc // The constraint should have the same info as the respective
571f4a2713aSLionel Sambuc // output constraint.
572f4a2713aSLionel Sambuc Info.setTiedOperand(i, OutputConstraints[i]);
573f4a2713aSLionel Sambuc } else if (!validateAsmConstraint(Name, Info)) {
574f4a2713aSLionel Sambuc // FIXME: This error return is in place temporarily so we can
575f4a2713aSLionel Sambuc // add more constraints as we hit it. Eventually, an unknown
576f4a2713aSLionel Sambuc // constraint should just be treated as 'g'.
577f4a2713aSLionel Sambuc return false;
578f4a2713aSLionel Sambuc }
579f4a2713aSLionel Sambuc break;
580f4a2713aSLionel Sambuc case '[': {
581f4a2713aSLionel Sambuc unsigned Index = 0;
582f4a2713aSLionel Sambuc if (!resolveSymbolicName(Name, OutputConstraints, NumOutputs, Index))
583f4a2713aSLionel Sambuc return false;
584f4a2713aSLionel Sambuc
585f4a2713aSLionel Sambuc // If the constraint is already tied, it must be tied to the
586f4a2713aSLionel Sambuc // same operand referenced to by the number.
587f4a2713aSLionel Sambuc if (Info.hasTiedOperand() && Info.getTiedOperand() != Index)
588f4a2713aSLionel Sambuc return false;
589f4a2713aSLionel Sambuc
590*0a6a1f1dSLionel Sambuc // A number must refer to an output only operand.
591*0a6a1f1dSLionel Sambuc if (OutputConstraints[Index].isReadWrite())
592*0a6a1f1dSLionel Sambuc return false;
593*0a6a1f1dSLionel Sambuc
594f4a2713aSLionel Sambuc Info.setTiedOperand(Index, OutputConstraints[Index]);
595f4a2713aSLionel Sambuc break;
596f4a2713aSLionel Sambuc }
597f4a2713aSLionel Sambuc case '%': // commutative
598f4a2713aSLionel Sambuc // FIXME: Fail if % is used with the last operand.
599f4a2713aSLionel Sambuc break;
600f4a2713aSLionel Sambuc case 'i': // immediate integer.
601f4a2713aSLionel Sambuc case 'n': // immediate integer with a known value.
602f4a2713aSLionel Sambuc break;
603f4a2713aSLionel Sambuc case 'I': // Various constant constraints with target-specific meanings.
604f4a2713aSLionel Sambuc case 'J':
605f4a2713aSLionel Sambuc case 'K':
606f4a2713aSLionel Sambuc case 'L':
607f4a2713aSLionel Sambuc case 'M':
608f4a2713aSLionel Sambuc case 'N':
609f4a2713aSLionel Sambuc case 'O':
610f4a2713aSLionel Sambuc case 'P':
611*0a6a1f1dSLionel Sambuc if (!validateAsmConstraint(Name, Info))
612*0a6a1f1dSLionel Sambuc return false;
613f4a2713aSLionel Sambuc break;
614f4a2713aSLionel Sambuc case 'r': // general register.
615f4a2713aSLionel Sambuc Info.setAllowsRegister();
616f4a2713aSLionel Sambuc break;
617f4a2713aSLionel Sambuc case 'm': // memory operand.
618f4a2713aSLionel Sambuc case 'o': // offsettable memory operand.
619f4a2713aSLionel Sambuc case 'V': // non-offsettable memory operand.
620f4a2713aSLionel Sambuc case '<': // autodecrement memory operand.
621f4a2713aSLionel Sambuc case '>': // autoincrement memory operand.
622f4a2713aSLionel Sambuc Info.setAllowsMemory();
623f4a2713aSLionel Sambuc break;
624f4a2713aSLionel Sambuc case 'g': // general register, memory operand or immediate integer.
625f4a2713aSLionel Sambuc case 'X': // any operand.
626f4a2713aSLionel Sambuc Info.setAllowsRegister();
627f4a2713aSLionel Sambuc Info.setAllowsMemory();
628f4a2713aSLionel Sambuc break;
629f4a2713aSLionel Sambuc case 'E': // immediate floating point.
630f4a2713aSLionel Sambuc case 'F': // immediate floating point.
631f4a2713aSLionel Sambuc case 'p': // address operand.
632f4a2713aSLionel Sambuc break;
633f4a2713aSLionel Sambuc case ',': // multiple alternative constraint. Ignore comma.
634f4a2713aSLionel Sambuc break;
635*0a6a1f1dSLionel Sambuc case '#': // Ignore as constraint.
636*0a6a1f1dSLionel Sambuc while (Name[1] && Name[1] != ',')
637*0a6a1f1dSLionel Sambuc Name++;
638*0a6a1f1dSLionel Sambuc break;
639f4a2713aSLionel Sambuc case '?': // Disparage slightly code.
640f4a2713aSLionel Sambuc case '!': // Disparage severely.
641f4a2713aSLionel Sambuc case '*': // Ignore for choosing register preferences.
642f4a2713aSLionel Sambuc break; // Pass them.
643f4a2713aSLionel Sambuc }
644f4a2713aSLionel Sambuc
645f4a2713aSLionel Sambuc Name++;
646f4a2713aSLionel Sambuc }
647f4a2713aSLionel Sambuc
648f4a2713aSLionel Sambuc return true;
649f4a2713aSLionel Sambuc }
650f4a2713aSLionel Sambuc
tryParse(llvm::StringRef name)651f4a2713aSLionel Sambuc bool TargetCXXABI::tryParse(llvm::StringRef name) {
652f4a2713aSLionel Sambuc const Kind unknown = static_cast<Kind>(-1);
653f4a2713aSLionel Sambuc Kind kind = llvm::StringSwitch<Kind>(name)
654f4a2713aSLionel Sambuc .Case("arm", GenericARM)
655f4a2713aSLionel Sambuc .Case("ios", iOS)
656f4a2713aSLionel Sambuc .Case("itanium", GenericItanium)
657f4a2713aSLionel Sambuc .Case("microsoft", Microsoft)
658*0a6a1f1dSLionel Sambuc .Case("mips", GenericMIPS)
659f4a2713aSLionel Sambuc .Default(unknown);
660f4a2713aSLionel Sambuc if (kind == unknown) return false;
661f4a2713aSLionel Sambuc
662f4a2713aSLionel Sambuc set(kind);
663f4a2713aSLionel Sambuc return true;
664f4a2713aSLionel Sambuc }
665