blob: 9a6cc208e5c83cd6c7b283589408d6ac75be9d7a [file] [log] [blame]
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001//===---- TargetInfo.cpp - Encapsulate target details -----------*- C++ -*-===//
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// These classes wrap the information about a call or function
11// definition used to handle ABI compliancy.
12//
13//===----------------------------------------------------------------------===//
14
Anton Korobeynikov82d0a412010-01-10 12:58:08 +000015#include "TargetInfo.h"
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000016#include "ABIInfo.h"
17#include "CodeGenFunction.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000018#include "clang/AST/RecordLayout.h"
Sandeep Patel34c1af82011-04-05 00:23:47 +000019#include "clang/Frontend/CodeGenOptions.h"
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000020#include "llvm/Type.h"
Chris Lattner9c254f02010-06-29 06:01:59 +000021#include "llvm/Target/TargetData.h"
Daniel Dunbar2c0843f2009-08-24 08:52:16 +000022#include "llvm/ADT/Triple.h"
Daniel Dunbar28df7a52009-12-03 09:13:49 +000023#include "llvm/Support/raw_ostream.h"
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000024using namespace clang;
25using namespace CodeGen;
26
John McCallaeeb7012010-05-27 06:19:26 +000027static void AssignToArrayRange(CodeGen::CGBuilderTy &Builder,
28 llvm::Value *Array,
29 llvm::Value *Value,
30 unsigned FirstIndex,
31 unsigned LastIndex) {
32 // Alternatively, we could emit this as a loop in the source.
33 for (unsigned I = FirstIndex; I <= LastIndex; ++I) {
34 llvm::Value *Cell = Builder.CreateConstInBoundsGEP1_32(Array, I);
35 Builder.CreateStore(Value, Cell);
36 }
37}
38
John McCalld608cdb2010-08-22 10:59:02 +000039static bool isAggregateTypeForABI(QualType T) {
40 return CodeGenFunction::hasAggregateLLVMType(T) ||
41 T->isMemberFunctionPointerType();
42}
43
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000044ABIInfo::~ABIInfo() {}
45
Chris Lattnerea044322010-07-29 02:01:43 +000046ASTContext &ABIInfo::getContext() const {
47 return CGT.getContext();
48}
49
50llvm::LLVMContext &ABIInfo::getVMContext() const {
51 return CGT.getLLVMContext();
52}
53
54const llvm::TargetData &ABIInfo::getTargetData() const {
55 return CGT.getTargetData();
56}
57
58
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000059void ABIArgInfo::dump() const {
Daniel Dunbar28df7a52009-12-03 09:13:49 +000060 llvm::raw_ostream &OS = llvm::errs();
61 OS << "(ABIArgInfo Kind=";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000062 switch (TheKind) {
63 case Direct:
Chris Lattner800588f2010-07-29 06:26:06 +000064 OS << "Direct Type=";
65 if (const llvm::Type *Ty = getCoerceToType())
66 Ty->print(OS);
67 else
68 OS << "null";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000069 break;
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +000070 case Extend:
Daniel Dunbar28df7a52009-12-03 09:13:49 +000071 OS << "Extend";
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +000072 break;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000073 case Ignore:
Daniel Dunbar28df7a52009-12-03 09:13:49 +000074 OS << "Ignore";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000075 break;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000076 case Indirect:
Daniel Dunbardc6d5742010-04-21 19:10:51 +000077 OS << "Indirect Align=" << getIndirectAlign()
Daniel Dunbarcf3b6f22010-09-16 20:42:02 +000078 << " Byal=" << getIndirectByVal()
79 << " Realign=" << getIndirectRealign();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000080 break;
81 case Expand:
Daniel Dunbar28df7a52009-12-03 09:13:49 +000082 OS << "Expand";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000083 break;
84 }
Daniel Dunbar28df7a52009-12-03 09:13:49 +000085 OS << ")\n";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000086}
87
Anton Korobeynikov82d0a412010-01-10 12:58:08 +000088TargetCodeGenInfo::~TargetCodeGenInfo() { delete Info; }
89
Daniel Dunbar98303b92009-09-13 08:03:58 +000090static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000091
92/// isEmptyField - Return true iff a the field is "empty", that is it
93/// is an unnamed bit-field or an (array of) empty record(s).
Daniel Dunbar98303b92009-09-13 08:03:58 +000094static bool isEmptyField(ASTContext &Context, const FieldDecl *FD,
95 bool AllowArrays) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000096 if (FD->isUnnamedBitfield())
97 return true;
98
99 QualType FT = FD->getType();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000100
Daniel Dunbar98303b92009-09-13 08:03:58 +0000101 // Constant arrays of empty records count as empty, strip them off.
102 if (AllowArrays)
103 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT))
104 FT = AT->getElementType();
105
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000106 const RecordType *RT = FT->getAs<RecordType>();
107 if (!RT)
108 return false;
109
110 // C++ record fields are never empty, at least in the Itanium ABI.
111 //
112 // FIXME: We should use a predicate for whether this behavior is true in the
113 // current ABI.
114 if (isa<CXXRecordDecl>(RT->getDecl()))
115 return false;
116
Daniel Dunbar98303b92009-09-13 08:03:58 +0000117 return isEmptyRecord(Context, FT, AllowArrays);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000118}
119
120/// isEmptyRecord - Return true iff a structure contains only empty
121/// fields. Note that a structure with a flexible array member is not
122/// considered empty.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000123static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000124 const RecordType *RT = T->getAs<RecordType>();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000125 if (!RT)
126 return 0;
127 const RecordDecl *RD = RT->getDecl();
128 if (RD->hasFlexibleArrayMember())
129 return false;
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000130
Argyrios Kyrtzidisc5f18f32011-05-17 02:17:52 +0000131 // If this is a C++ record, check the bases first.
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000132 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
Argyrios Kyrtzidisc5f18f32011-05-17 02:17:52 +0000133 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
134 e = CXXRD->bases_end(); i != e; ++i)
135 if (!isEmptyRecord(Context, i->getType(), true))
136 return false;
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000137
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000138 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
139 i != e; ++i)
Daniel Dunbar98303b92009-09-13 08:03:58 +0000140 if (!isEmptyField(Context, *i, AllowArrays))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000141 return false;
142 return true;
143}
144
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000145/// hasNonTrivialDestructorOrCopyConstructor - Determine if a type has either
146/// a non-trivial destructor or a non-trivial copy constructor.
147static bool hasNonTrivialDestructorOrCopyConstructor(const RecordType *RT) {
148 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
149 if (!RD)
150 return false;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000151
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000152 return !RD->hasTrivialDestructor() || !RD->hasTrivialCopyConstructor();
153}
154
155/// isRecordWithNonTrivialDestructorOrCopyConstructor - Determine if a type is
156/// a record type with either a non-trivial destructor or a non-trivial copy
157/// constructor.
158static bool isRecordWithNonTrivialDestructorOrCopyConstructor(QualType T) {
159 const RecordType *RT = T->getAs<RecordType>();
160 if (!RT)
161 return false;
162
163 return hasNonTrivialDestructorOrCopyConstructor(RT);
164}
165
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000166/// isSingleElementStruct - Determine if a structure is a "single
167/// element struct", i.e. it has exactly one non-empty field or
168/// exactly one field which is itself a single element
169/// struct. Structures with flexible array members are never
170/// considered single element structs.
171///
172/// \return The field declaration for the single non-empty field, if
173/// it exists.
174static const Type *isSingleElementStruct(QualType T, ASTContext &Context) {
175 const RecordType *RT = T->getAsStructureType();
176 if (!RT)
177 return 0;
178
179 const RecordDecl *RD = RT->getDecl();
180 if (RD->hasFlexibleArrayMember())
181 return 0;
182
183 const Type *Found = 0;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000184
Daniel Dunbar9430d5a2010-05-11 21:15:36 +0000185 // If this is a C++ record, check the bases first.
186 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
187 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
188 e = CXXRD->bases_end(); i != e; ++i) {
Daniel Dunbar9430d5a2010-05-11 21:15:36 +0000189 // Ignore empty records.
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000190 if (isEmptyRecord(Context, i->getType(), true))
Daniel Dunbar9430d5a2010-05-11 21:15:36 +0000191 continue;
192
193 // If we already found an element then this isn't a single-element struct.
194 if (Found)
195 return 0;
196
197 // If this is non-empty and not a single element struct, the composite
198 // cannot be a single element struct.
199 Found = isSingleElementStruct(i->getType(), Context);
200 if (!Found)
201 return 0;
202 }
203 }
204
205 // Check for single element.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000206 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
207 i != e; ++i) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000208 const FieldDecl *FD = *i;
209 QualType FT = FD->getType();
210
211 // Ignore empty fields.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000212 if (isEmptyField(Context, FD, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000213 continue;
214
215 // If we already found an element then this isn't a single-element
216 // struct.
217 if (Found)
218 return 0;
219
220 // Treat single element arrays as the element.
221 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
222 if (AT->getSize().getZExtValue() != 1)
223 break;
224 FT = AT->getElementType();
225 }
226
John McCalld608cdb2010-08-22 10:59:02 +0000227 if (!isAggregateTypeForABI(FT)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000228 Found = FT.getTypePtr();
229 } else {
230 Found = isSingleElementStruct(FT, Context);
231 if (!Found)
232 return 0;
233 }
234 }
235
236 return Found;
237}
238
239static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
Daniel Dunbara1842d32010-05-14 03:40:53 +0000240 if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() &&
Daniel Dunbar55e59e12009-09-24 05:12:36 +0000241 !Ty->isAnyComplexType() && !Ty->isEnumeralType() &&
242 !Ty->isBlockPointerType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000243 return false;
244
245 uint64_t Size = Context.getTypeSize(Ty);
246 return Size == 32 || Size == 64;
247}
248
Daniel Dunbar53012f42009-11-09 01:33:53 +0000249/// canExpandIndirectArgument - Test whether an argument type which is to be
250/// passed indirectly (on the stack) would have the equivalent layout if it was
251/// expanded into separate arguments. If so, we prefer to do the latter to avoid
252/// inhibiting optimizations.
253///
254// FIXME: This predicate is missing many cases, currently it just follows
255// llvm-gcc (checks that all fields are 32-bit or 64-bit primitive types). We
256// should probably make this smarter, or better yet make the LLVM backend
257// capable of handling it.
258static bool canExpandIndirectArgument(QualType Ty, ASTContext &Context) {
259 // We can only expand structure types.
260 const RecordType *RT = Ty->getAs<RecordType>();
261 if (!RT)
262 return false;
263
264 // We can only expand (C) structures.
265 //
266 // FIXME: This needs to be generalized to handle classes as well.
267 const RecordDecl *RD = RT->getDecl();
268 if (!RD->isStruct() || isa<CXXRecordDecl>(RD))
269 return false;
270
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000271 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
272 i != e; ++i) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000273 const FieldDecl *FD = *i;
274
275 if (!is32Or64BitBasicType(FD->getType(), Context))
276 return false;
277
278 // FIXME: Reject bit-fields wholesale; there are two problems, we don't know
279 // how to expand them yet, and the predicate for telling if a bitfield still
280 // counts as "basic" is more complicated than what we were doing previously.
281 if (FD->isBitField())
282 return false;
283 }
284
285 return true;
286}
287
288namespace {
289/// DefaultABIInfo - The default implementation for ABI specific
290/// details. This implementation provides information which results in
291/// self-consistent and sensible LLVM IR generation, but does not
292/// conform to any particular ABI.
293class DefaultABIInfo : public ABIInfo {
Chris Lattnerea044322010-07-29 02:01:43 +0000294public:
295 DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000296
Chris Lattnera3c109b2010-07-29 02:16:43 +0000297 ABIArgInfo classifyReturnType(QualType RetTy) const;
298 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000299
Chris Lattneree5dcd02010-07-29 02:31:05 +0000300 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000301 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000302 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
303 it != ie; ++it)
Chris Lattnera3c109b2010-07-29 02:16:43 +0000304 it->info = classifyArgumentType(it->type);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000305 }
306
307 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
308 CodeGenFunction &CGF) const;
309};
310
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000311class DefaultTargetCodeGenInfo : public TargetCodeGenInfo {
312public:
Chris Lattnerea044322010-07-29 02:01:43 +0000313 DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
314 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000315};
316
317llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
318 CodeGenFunction &CGF) const {
319 return 0;
320}
321
Chris Lattnera3c109b2010-07-29 02:16:43 +0000322ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const {
John McCalld608cdb2010-08-22 10:59:02 +0000323 if (isAggregateTypeForABI(Ty))
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000324 return ABIArgInfo::getIndirect(0);
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000325
Chris Lattnera14db752010-03-11 18:19:55 +0000326 // Treat an enum type as its underlying type.
327 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
328 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregoraa74a1e2010-02-02 20:10:50 +0000329
Chris Lattnera14db752010-03-11 18:19:55 +0000330 return (Ty->isPromotableIntegerType() ?
331 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000332}
333
Bob Wilson0024f942011-01-10 23:54:17 +0000334ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const {
335 if (RetTy->isVoidType())
336 return ABIArgInfo::getIgnore();
337
338 if (isAggregateTypeForABI(RetTy))
339 return ABIArgInfo::getIndirect(0);
340
341 // Treat an enum type as its underlying type.
342 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
343 RetTy = EnumTy->getDecl()->getIntegerType();
344
345 return (RetTy->isPromotableIntegerType() ?
346 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
347}
348
Bill Wendlingbb465d72010-10-18 03:41:31 +0000349/// UseX86_MMXType - Return true if this is an MMX type that should use the special
350/// x86_mmx type.
351bool UseX86_MMXType(const llvm::Type *IRType) {
352 // If the type is an MMX type <2 x i32>, <4 x i16>, or <8 x i8>, use the
353 // special x86_mmx type.
354 return IRType->isVectorTy() && IRType->getPrimitiveSizeInBits() == 64 &&
355 cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy() &&
356 IRType->getScalarSizeInBits() != 64;
357}
358
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000359static const llvm::Type* X86AdjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
360 llvm::StringRef Constraint,
361 const llvm::Type* Ty) {
Bill Wendling0507be62011-03-07 22:47:14 +0000362 if ((Constraint == "y" || Constraint == "&y") && Ty->isVectorTy())
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000363 return llvm::Type::getX86_MMXTy(CGF.getLLVMContext());
364 return Ty;
365}
366
Chris Lattnerdce5ad02010-06-28 20:05:43 +0000367//===----------------------------------------------------------------------===//
368// X86-32 ABI Implementation
369//===----------------------------------------------------------------------===//
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000370
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000371/// X86_32ABIInfo - The X86-32 ABI information.
372class X86_32ABIInfo : public ABIInfo {
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000373 static const unsigned MinABIStackAlignInBytes = 4;
374
David Chisnall1e4249c2009-08-17 23:08:21 +0000375 bool IsDarwinVectorABI;
376 bool IsSmallStructInRegABI;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000377
378 static bool isRegisterSize(unsigned Size) {
379 return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
380 }
381
382 static bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context);
383
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000384 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
385 /// such that the argument will be passed in memory.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000386 ABIArgInfo getIndirectResult(QualType Ty, bool ByVal = true) const;
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000387
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000388 /// \brief Return the alignment to use for the given type on the stack.
Daniel Dunbare59d8582010-09-16 20:42:06 +0000389 unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const;
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000390
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000391public:
Chris Lattnerea044322010-07-29 02:01:43 +0000392
Chris Lattnera3c109b2010-07-29 02:16:43 +0000393 ABIArgInfo classifyReturnType(QualType RetTy) const;
394 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000395
Chris Lattneree5dcd02010-07-29 02:31:05 +0000396 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000397 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000398 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
399 it != ie; ++it)
Chris Lattnera3c109b2010-07-29 02:16:43 +0000400 it->info = classifyArgumentType(it->type);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000401 }
402
403 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
404 CodeGenFunction &CGF) const;
405
Chris Lattnerea044322010-07-29 02:01:43 +0000406 X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p)
407 : ABIInfo(CGT), IsDarwinVectorABI(d), IsSmallStructInRegABI(p) {}
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000408};
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000409
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000410class X86_32TargetCodeGenInfo : public TargetCodeGenInfo {
411public:
Chris Lattnerea044322010-07-29 02:01:43 +0000412 X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p)
413 :TargetCodeGenInfo(new X86_32ABIInfo(CGT, d, p)) {}
Charles Davis74f72932010-02-13 15:54:06 +0000414
415 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
416 CodeGen::CodeGenModule &CGM) const;
John McCall6374c332010-03-06 00:35:14 +0000417
418 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
419 // Darwin uses different dwarf register numbers for EH.
420 if (CGM.isTargetDarwin()) return 5;
421
422 return 4;
423 }
424
425 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
426 llvm::Value *Address) const;
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000427
428 const llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
429 llvm::StringRef Constraint,
430 const llvm::Type* Ty) const {
431 return X86AdjustInlineAsmType(CGF, Constraint, Ty);
432 }
433
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000434};
435
436}
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000437
438/// shouldReturnTypeInRegister - Determine if the given type should be
439/// passed in a register (for the Darwin ABI).
440bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
441 ASTContext &Context) {
442 uint64_t Size = Context.getTypeSize(Ty);
443
444 // Type must be register sized.
445 if (!isRegisterSize(Size))
446 return false;
447
448 if (Ty->isVectorType()) {
449 // 64- and 128- bit vectors inside structures are not returned in
450 // registers.
451 if (Size == 64 || Size == 128)
452 return false;
453
454 return true;
455 }
456
Daniel Dunbar77115232010-05-15 00:00:30 +0000457 // If this is a builtin, pointer, enum, complex type, member pointer, or
458 // member function pointer it is ok.
Daniel Dunbara1842d32010-05-14 03:40:53 +0000459 if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() ||
Daniel Dunbar55e59e12009-09-24 05:12:36 +0000460 Ty->isAnyComplexType() || Ty->isEnumeralType() ||
Daniel Dunbar77115232010-05-15 00:00:30 +0000461 Ty->isBlockPointerType() || Ty->isMemberPointerType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000462 return true;
463
464 // Arrays are treated like records.
465 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
466 return shouldReturnTypeInRegister(AT->getElementType(), Context);
467
468 // Otherwise, it must be a record type.
Ted Kremenek6217b802009-07-29 21:53:49 +0000469 const RecordType *RT = Ty->getAs<RecordType>();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000470 if (!RT) return false;
471
Anders Carlssona8874232010-01-27 03:25:19 +0000472 // FIXME: Traverse bases here too.
473
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000474 // Structure types are passed in register if all fields would be
475 // passed in a register.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000476 for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(),
477 e = RT->getDecl()->field_end(); i != e; ++i) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000478 const FieldDecl *FD = *i;
479
480 // Empty fields are ignored.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000481 if (isEmptyField(Context, FD, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000482 continue;
483
484 // Check fields recursively.
485 if (!shouldReturnTypeInRegister(FD->getType(), Context))
486 return false;
487 }
488
489 return true;
490}
491
Chris Lattnera3c109b2010-07-29 02:16:43 +0000492ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy) const {
493 if (RetTy->isVoidType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000494 return ABIArgInfo::getIgnore();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000495
Chris Lattnera3c109b2010-07-29 02:16:43 +0000496 if (const VectorType *VT = RetTy->getAs<VectorType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000497 // On Darwin, some vectors are returned in registers.
David Chisnall1e4249c2009-08-17 23:08:21 +0000498 if (IsDarwinVectorABI) {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000499 uint64_t Size = getContext().getTypeSize(RetTy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000500
501 // 128-bit vectors are a special case; they are returned in
502 // registers and we need to make sure to pick a type the LLVM
503 // backend will like.
504 if (Size == 128)
Chris Lattner800588f2010-07-29 06:26:06 +0000505 return ABIArgInfo::getDirect(llvm::VectorType::get(
Chris Lattnera3c109b2010-07-29 02:16:43 +0000506 llvm::Type::getInt64Ty(getVMContext()), 2));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000507
508 // Always return in register if it fits in a general purpose
509 // register, or if it is 64 bits and has a single element.
510 if ((Size == 8 || Size == 16 || Size == 32) ||
511 (Size == 64 && VT->getNumElements() == 1))
Chris Lattner800588f2010-07-29 06:26:06 +0000512 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattnera3c109b2010-07-29 02:16:43 +0000513 Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000514
515 return ABIArgInfo::getIndirect(0);
516 }
517
518 return ABIArgInfo::getDirect();
Chris Lattnera3c109b2010-07-29 02:16:43 +0000519 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000520
John McCalld608cdb2010-08-22 10:59:02 +0000521 if (isAggregateTypeForABI(RetTy)) {
Anders Carlssona8874232010-01-27 03:25:19 +0000522 if (const RecordType *RT = RetTy->getAs<RecordType>()) {
Anders Carlsson40092972009-10-20 22:07:59 +0000523 // Structures with either a non-trivial destructor or a non-trivial
524 // copy constructor are always indirect.
525 if (hasNonTrivialDestructorOrCopyConstructor(RT))
526 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000527
Anders Carlsson40092972009-10-20 22:07:59 +0000528 // Structures with flexible arrays are always indirect.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000529 if (RT->getDecl()->hasFlexibleArrayMember())
530 return ABIArgInfo::getIndirect(0);
Anders Carlsson40092972009-10-20 22:07:59 +0000531 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000532
David Chisnall1e4249c2009-08-17 23:08:21 +0000533 // If specified, structs and unions are always indirect.
534 if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000535 return ABIArgInfo::getIndirect(0);
536
537 // Classify "single element" structs as their element type.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000538 if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) {
John McCall183700f2009-09-21 23:43:11 +0000539 if (const BuiltinType *BT = SeltTy->getAs<BuiltinType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000540 if (BT->isIntegerType()) {
541 // We need to use the size of the structure, padding
542 // bit-fields can adjust that to be larger than the single
543 // element type.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000544 uint64_t Size = getContext().getTypeSize(RetTy);
Chris Lattner800588f2010-07-29 06:26:06 +0000545 return ABIArgInfo::getDirect(
Chris Lattnera3c109b2010-07-29 02:16:43 +0000546 llvm::IntegerType::get(getVMContext(), (unsigned)Size));
547 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000548
Chris Lattnera3c109b2010-07-29 02:16:43 +0000549 if (BT->getKind() == BuiltinType::Float) {
550 assert(getContext().getTypeSize(RetTy) ==
551 getContext().getTypeSize(SeltTy) &&
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000552 "Unexpect single element structure size!");
Chris Lattner800588f2010-07-29 06:26:06 +0000553 return ABIArgInfo::getDirect(llvm::Type::getFloatTy(getVMContext()));
Chris Lattnera3c109b2010-07-29 02:16:43 +0000554 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000555
Chris Lattnera3c109b2010-07-29 02:16:43 +0000556 if (BT->getKind() == BuiltinType::Double) {
557 assert(getContext().getTypeSize(RetTy) ==
558 getContext().getTypeSize(SeltTy) &&
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000559 "Unexpect single element structure size!");
Chris Lattner800588f2010-07-29 06:26:06 +0000560 return ABIArgInfo::getDirect(llvm::Type::getDoubleTy(getVMContext()));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000561 }
562 } else if (SeltTy->isPointerType()) {
563 // FIXME: It would be really nice if this could come out as the proper
564 // pointer type.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000565 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(getVMContext());
Chris Lattner800588f2010-07-29 06:26:06 +0000566 return ABIArgInfo::getDirect(PtrTy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000567 } else if (SeltTy->isVectorType()) {
568 // 64- and 128-bit vectors are never returned in a
569 // register when inside a structure.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000570 uint64_t Size = getContext().getTypeSize(RetTy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000571 if (Size == 64 || Size == 128)
572 return ABIArgInfo::getIndirect(0);
573
Chris Lattnera3c109b2010-07-29 02:16:43 +0000574 return classifyReturnType(QualType(SeltTy, 0));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000575 }
576 }
577
578 // Small structures which are register sized are generally returned
579 // in a register.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000580 if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, getContext())) {
581 uint64_t Size = getContext().getTypeSize(RetTy);
Chris Lattner800588f2010-07-29 06:26:06 +0000582 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000583 }
584
585 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000586 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000587
Chris Lattnera3c109b2010-07-29 02:16:43 +0000588 // Treat an enum type as its underlying type.
589 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
590 RetTy = EnumTy->getDecl()->getIntegerType();
591
592 return (RetTy->isPromotableIntegerType() ?
593 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000594}
595
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000596static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) {
597 const RecordType *RT = Ty->getAs<RecordType>();
598 if (!RT)
599 return 0;
600 const RecordDecl *RD = RT->getDecl();
601
602 // If this is a C++ record, check the bases first.
603 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
604 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
605 e = CXXRD->bases_end(); i != e; ++i)
606 if (!isRecordWithSSEVectorType(Context, i->getType()))
607 return false;
608
609 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
610 i != e; ++i) {
611 QualType FT = i->getType();
612
613 if (FT->getAs<VectorType>() && Context.getTypeSize(Ty) == 128)
614 return true;
615
616 if (isRecordWithSSEVectorType(Context, FT))
617 return true;
618 }
619
620 return false;
621}
622
Daniel Dunbare59d8582010-09-16 20:42:06 +0000623unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty,
624 unsigned Align) const {
625 // Otherwise, if the alignment is less than or equal to the minimum ABI
626 // alignment, just use the default; the backend will handle this.
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000627 if (Align <= MinABIStackAlignInBytes)
Daniel Dunbare59d8582010-09-16 20:42:06 +0000628 return 0; // Use default alignment.
629
630 // On non-Darwin, the stack type alignment is always 4.
631 if (!IsDarwinVectorABI) {
632 // Set explicit alignment, since we may need to realign the top.
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000633 return MinABIStackAlignInBytes;
Daniel Dunbare59d8582010-09-16 20:42:06 +0000634 }
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000635
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000636 // Otherwise, if the type contains an SSE vector type, the alignment is 16.
637 if (isRecordWithSSEVectorType(getContext(), Ty))
638 return 16;
639
640 return MinABIStackAlignInBytes;
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000641}
642
Chris Lattnera3c109b2010-07-29 02:16:43 +0000643ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal) const {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +0000644 if (!ByVal)
645 return ABIArgInfo::getIndirect(0, false);
646
Daniel Dunbare59d8582010-09-16 20:42:06 +0000647 // Compute the byval alignment.
648 unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
649 unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign);
650 if (StackAlign == 0)
Chris Lattnerde92d732011-05-22 23:35:00 +0000651 return ABIArgInfo::getIndirect(4);
Daniel Dunbare59d8582010-09-16 20:42:06 +0000652
653 // If the stack alignment is less than the type alignment, realign the
654 // argument.
655 if (StackAlign < TypeAlign)
656 return ABIArgInfo::getIndirect(StackAlign, /*ByVal=*/true,
657 /*Realign=*/true);
658
659 return ABIArgInfo::getIndirect(StackAlign);
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000660}
661
Chris Lattnera3c109b2010-07-29 02:16:43 +0000662ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000663 // FIXME: Set alignment on indirect arguments.
John McCalld608cdb2010-08-22 10:59:02 +0000664 if (isAggregateTypeForABI(Ty)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000665 // Structures with flexible arrays are always indirect.
Anders Carlssona8874232010-01-27 03:25:19 +0000666 if (const RecordType *RT = Ty->getAs<RecordType>()) {
667 // Structures with either a non-trivial destructor or a non-trivial
668 // copy constructor are always indirect.
669 if (hasNonTrivialDestructorOrCopyConstructor(RT))
Chris Lattnera3c109b2010-07-29 02:16:43 +0000670 return getIndirectResult(Ty, /*ByVal=*/false);
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000671
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000672 if (RT->getDecl()->hasFlexibleArrayMember())
Chris Lattnera3c109b2010-07-29 02:16:43 +0000673 return getIndirectResult(Ty);
Anders Carlssona8874232010-01-27 03:25:19 +0000674 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000675
676 // Ignore empty structs.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000677 if (Ty->isStructureType() && getContext().getTypeSize(Ty) == 0)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000678 return ABIArgInfo::getIgnore();
679
Daniel Dunbar53012f42009-11-09 01:33:53 +0000680 // Expand small (<= 128-bit) record types when we know that the stack layout
681 // of those arguments will match the struct. This is important because the
682 // LLVM backend isn't smart enough to remove byval, which inhibits many
683 // optimizations.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000684 if (getContext().getTypeSize(Ty) <= 4*32 &&
685 canExpandIndirectArgument(Ty, getContext()))
Daniel Dunbar53012f42009-11-09 01:33:53 +0000686 return ABIArgInfo::getExpand();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000687
Chris Lattnera3c109b2010-07-29 02:16:43 +0000688 return getIndirectResult(Ty);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000689 }
690
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000691 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattner7b733502010-08-26 20:08:43 +0000692 // On Darwin, some vectors are passed in memory, we handle this by passing
693 // it as an i8/i16/i32/i64.
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000694 if (IsDarwinVectorABI) {
695 uint64_t Size = getContext().getTypeSize(Ty);
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000696 if ((Size == 8 || Size == 16 || Size == 32) ||
697 (Size == 64 && VT->getNumElements() == 1))
698 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
699 Size));
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000700 }
Bill Wendlingbb465d72010-10-18 03:41:31 +0000701
702 const llvm::Type *IRType = CGT.ConvertTypeRecursive(Ty);
703 if (UseX86_MMXType(IRType)) {
704 ABIArgInfo AAI = ABIArgInfo::getDirect(IRType);
705 AAI.setCoerceToType(llvm::Type::getX86_MMXTy(getVMContext()));
706 return AAI;
707 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000708
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000709 return ABIArgInfo::getDirect();
710 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000711
712
Chris Lattnera3c109b2010-07-29 02:16:43 +0000713 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
714 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregoraa74a1e2010-02-02 20:10:50 +0000715
Chris Lattnera3c109b2010-07-29 02:16:43 +0000716 return (Ty->isPromotableIntegerType() ?
717 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000718}
719
720llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
721 CodeGenFunction &CGF) const {
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +0000722 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Owen Anderson96e0fc72009-07-29 22:16:19 +0000723 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000724
725 CGBuilderTy &Builder = CGF.Builder;
726 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
727 "ap");
728 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
729 llvm::Type *PTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000730 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000731 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
732
733 uint64_t Offset =
734 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
735 llvm::Value *NextAddr =
Chris Lattner77b89b82010-06-27 07:15:29 +0000736 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000737 "ap.next");
738 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
739
740 return AddrTyped;
741}
742
Charles Davis74f72932010-02-13 15:54:06 +0000743void X86_32TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
744 llvm::GlobalValue *GV,
745 CodeGen::CodeGenModule &CGM) const {
746 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
747 if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
748 // Get the LLVM function.
749 llvm::Function *Fn = cast<llvm::Function>(GV);
750
751 // Now add the 'alignstack' attribute with a value of 16.
752 Fn->addFnAttr(llvm::Attribute::constructStackAlignmentFromInt(16));
753 }
754 }
755}
756
John McCall6374c332010-03-06 00:35:14 +0000757bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable(
758 CodeGen::CodeGenFunction &CGF,
759 llvm::Value *Address) const {
760 CodeGen::CGBuilderTy &Builder = CGF.Builder;
761 llvm::LLVMContext &Context = CGF.getLLVMContext();
762
763 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
764 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000765
John McCall6374c332010-03-06 00:35:14 +0000766 // 0-7 are the eight integer registers; the order is different
767 // on Darwin (for EH), but the range is the same.
768 // 8 is %eip.
John McCallaeeb7012010-05-27 06:19:26 +0000769 AssignToArrayRange(Builder, Address, Four8, 0, 8);
John McCall6374c332010-03-06 00:35:14 +0000770
771 if (CGF.CGM.isTargetDarwin()) {
772 // 12-16 are st(0..4). Not sure why we stop at 4.
773 // These have size 16, which is sizeof(long double) on
774 // platforms with 8-byte alignment for that type.
775 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
John McCallaeeb7012010-05-27 06:19:26 +0000776 AssignToArrayRange(Builder, Address, Sixteen8, 12, 16);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000777
John McCall6374c332010-03-06 00:35:14 +0000778 } else {
779 // 9 is %eflags, which doesn't get a size on Darwin for some
780 // reason.
781 Builder.CreateStore(Four8, Builder.CreateConstInBoundsGEP1_32(Address, 9));
782
783 // 11-16 are st(0..5). Not sure why we stop at 5.
784 // These have size 12, which is sizeof(long double) on
785 // platforms with 4-byte alignment for that type.
786 llvm::Value *Twelve8 = llvm::ConstantInt::get(i8, 12);
John McCallaeeb7012010-05-27 06:19:26 +0000787 AssignToArrayRange(Builder, Address, Twelve8, 11, 16);
788 }
John McCall6374c332010-03-06 00:35:14 +0000789
790 return false;
791}
792
Chris Lattnerdce5ad02010-06-28 20:05:43 +0000793//===----------------------------------------------------------------------===//
794// X86-64 ABI Implementation
795//===----------------------------------------------------------------------===//
796
797
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000798namespace {
799/// X86_64ABIInfo - The X86_64 ABI information.
800class X86_64ABIInfo : public ABIInfo {
801 enum Class {
802 Integer = 0,
803 SSE,
804 SSEUp,
805 X87,
806 X87Up,
807 ComplexX87,
808 NoClass,
809 Memory
810 };
811
812 /// merge - Implement the X86_64 ABI merging algorithm.
813 ///
814 /// Merge an accumulating classification \arg Accum with a field
815 /// classification \arg Field.
816 ///
817 /// \param Accum - The accumulating classification. This should
818 /// always be either NoClass or the result of a previous merge
819 /// call. In addition, this should never be Memory (the caller
820 /// should just return Memory for the aggregate).
Chris Lattner1090a9b2010-06-28 21:43:59 +0000821 static Class merge(Class Accum, Class Field);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000822
823 /// classify - Determine the x86_64 register classes in which the
824 /// given type T should be passed.
825 ///
826 /// \param Lo - The classification for the parts of the type
827 /// residing in the low word of the containing object.
828 ///
829 /// \param Hi - The classification for the parts of the type
830 /// residing in the high word of the containing object.
831 ///
832 /// \param OffsetBase - The bit offset of this type in the
833 /// containing object. Some parameters are classified different
834 /// depending on whether they straddle an eightbyte boundary.
835 ///
836 /// If a word is unused its result will be NoClass; if a type should
837 /// be passed in Memory then at least the classification of \arg Lo
838 /// will be Memory.
839 ///
840 /// The \arg Lo class will be NoClass iff the argument is ignored.
841 ///
842 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
843 /// also be ComplexX87.
Chris Lattner9c254f02010-06-29 06:01:59 +0000844 void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000845
Chris Lattner0f408f52010-07-29 04:56:46 +0000846 const llvm::Type *Get16ByteVectorType(QualType Ty) const;
Chris Lattner603519d2010-07-29 17:49:08 +0000847 const llvm::Type *GetSSETypeAtOffset(const llvm::Type *IRType,
Chris Lattnerf47c9442010-07-29 18:13:09 +0000848 unsigned IROffset, QualType SourceTy,
849 unsigned SourceOffset) const;
Chris Lattner0d2656d2010-07-29 17:40:35 +0000850 const llvm::Type *GetINTEGERTypeAtOffset(const llvm::Type *IRType,
851 unsigned IROffset, QualType SourceTy,
852 unsigned SourceOffset) const;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000853
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000854 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Daniel Dunbar46c54fb2010-04-21 19:49:55 +0000855 /// such that the argument will be returned in memory.
Chris Lattner9c254f02010-06-29 06:01:59 +0000856 ABIArgInfo getIndirectReturnResult(QualType Ty) const;
Daniel Dunbar46c54fb2010-04-21 19:49:55 +0000857
858 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000859 /// such that the argument will be passed in memory.
Chris Lattner9c254f02010-06-29 06:01:59 +0000860 ABIArgInfo getIndirectResult(QualType Ty) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000861
Chris Lattnera3c109b2010-07-29 02:16:43 +0000862 ABIArgInfo classifyReturnType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000863
Bill Wendlingbb465d72010-10-18 03:41:31 +0000864 ABIArgInfo classifyArgumentType(QualType Ty,
865 unsigned &neededInt,
Bill Wendling99aaae82010-10-18 23:51:38 +0000866 unsigned &neededSSE) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000867
John McCall67a57732011-04-21 01:20:55 +0000868 /// The 0.98 ABI revision clarified a lot of ambiguities,
869 /// unfortunately in ways that were not always consistent with
870 /// certain previous compilers. In particular, platforms which
871 /// required strict binary compatibility with older versions of GCC
872 /// may need to exempt themselves.
873 bool honorsRevision0_98() const {
874 return !getContext().Target.getTriple().isOSDarwin();
875 }
876
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000877public:
Chris Lattnerea044322010-07-29 02:01:43 +0000878 X86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
Chris Lattner9c254f02010-06-29 06:01:59 +0000879
Chris Lattneree5dcd02010-07-29 02:31:05 +0000880 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000881
882 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
883 CodeGenFunction &CGF) const;
884};
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000885
Chris Lattnerf13721d2010-08-31 16:44:54 +0000886/// WinX86_64ABIInfo - The Windows X86_64 ABI information.
NAKAMURA Takumia7573222011-01-17 22:56:31 +0000887class WinX86_64ABIInfo : public ABIInfo {
888
889 ABIArgInfo classify(QualType Ty) const;
890
Chris Lattnerf13721d2010-08-31 16:44:54 +0000891public:
NAKAMURA Takumia7573222011-01-17 22:56:31 +0000892 WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
893
894 virtual void computeInfo(CGFunctionInfo &FI) const;
Chris Lattnerf13721d2010-08-31 16:44:54 +0000895
896 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
897 CodeGenFunction &CGF) const;
898};
899
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000900class X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
901public:
Chris Lattnerea044322010-07-29 02:01:43 +0000902 X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
903 : TargetCodeGenInfo(new X86_64ABIInfo(CGT)) {}
John McCall6374c332010-03-06 00:35:14 +0000904
905 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
906 return 7;
907 }
908
909 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
910 llvm::Value *Address) const {
911 CodeGen::CGBuilderTy &Builder = CGF.Builder;
912 llvm::LLVMContext &Context = CGF.getLLVMContext();
913
914 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
915 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000916
John McCallaeeb7012010-05-27 06:19:26 +0000917 // 0-15 are the 16 integer registers.
918 // 16 is %rip.
919 AssignToArrayRange(Builder, Address, Eight8, 0, 16);
John McCall6374c332010-03-06 00:35:14 +0000920
921 return false;
922 }
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000923
924 const llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
925 llvm::StringRef Constraint,
926 const llvm::Type* Ty) const {
927 return X86AdjustInlineAsmType(CGF, Constraint, Ty);
928 }
929
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000930};
931
Chris Lattnerf13721d2010-08-31 16:44:54 +0000932class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo {
933public:
934 WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
935 : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {}
936
937 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
938 return 7;
939 }
940
941 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
942 llvm::Value *Address) const {
943 CodeGen::CGBuilderTy &Builder = CGF.Builder;
944 llvm::LLVMContext &Context = CGF.getLLVMContext();
945
946 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
947 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000948
Chris Lattnerf13721d2010-08-31 16:44:54 +0000949 // 0-15 are the 16 integer registers.
950 // 16 is %rip.
951 AssignToArrayRange(Builder, Address, Eight8, 0, 16);
952
953 return false;
954 }
955};
956
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000957}
958
Chris Lattner1090a9b2010-06-28 21:43:59 +0000959X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000960 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
961 // classified recursively so that always two fields are
962 // considered. The resulting class is calculated according to
963 // the classes of the fields in the eightbyte:
964 //
965 // (a) If both classes are equal, this is the resulting class.
966 //
967 // (b) If one of the classes is NO_CLASS, the resulting class is
968 // the other class.
969 //
970 // (c) If one of the classes is MEMORY, the result is the MEMORY
971 // class.
972 //
973 // (d) If one of the classes is INTEGER, the result is the
974 // INTEGER.
975 //
976 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
977 // MEMORY is used as class.
978 //
979 // (f) Otherwise class SSE is used.
980
981 // Accum should never be memory (we should have returned) or
982 // ComplexX87 (because this cannot be passed in a structure).
983 assert((Accum != Memory && Accum != ComplexX87) &&
984 "Invalid accumulated classification during merge.");
985 if (Accum == Field || Field == NoClass)
986 return Accum;
Chris Lattner1090a9b2010-06-28 21:43:59 +0000987 if (Field == Memory)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000988 return Memory;
Chris Lattner1090a9b2010-06-28 21:43:59 +0000989 if (Accum == NoClass)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000990 return Field;
Chris Lattner1090a9b2010-06-28 21:43:59 +0000991 if (Accum == Integer || Field == Integer)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000992 return Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +0000993 if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
994 Accum == X87 || Accum == X87Up)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000995 return Memory;
Chris Lattner1090a9b2010-06-28 21:43:59 +0000996 return SSE;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000997}
998
Chris Lattnerbcaedae2010-06-30 19:14:05 +0000999void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001000 Class &Lo, Class &Hi) const {
1001 // FIXME: This code can be simplified by introducing a simple value class for
1002 // Class pairs with appropriate constructor methods for the various
1003 // situations.
1004
1005 // FIXME: Some of the split computations are wrong; unaligned vectors
1006 // shouldn't be passed in registers for example, so there is no chance they
1007 // can straddle an eightbyte. Verify & simplify.
1008
1009 Lo = Hi = NoClass;
1010
1011 Class &Current = OffsetBase < 64 ? Lo : Hi;
1012 Current = Memory;
1013
John McCall183700f2009-09-21 23:43:11 +00001014 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001015 BuiltinType::Kind k = BT->getKind();
1016
1017 if (k == BuiltinType::Void) {
1018 Current = NoClass;
1019 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
1020 Lo = Integer;
1021 Hi = Integer;
1022 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
1023 Current = Integer;
1024 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
1025 Current = SSE;
1026 } else if (k == BuiltinType::LongDouble) {
1027 Lo = X87;
1028 Hi = X87Up;
1029 }
1030 // FIXME: _Decimal32 and _Decimal64 are SSE.
1031 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Chris Lattner1090a9b2010-06-28 21:43:59 +00001032 return;
1033 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001034
Chris Lattner1090a9b2010-06-28 21:43:59 +00001035 if (const EnumType *ET = Ty->getAs<EnumType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001036 // Classify the underlying integer type.
Chris Lattner9c254f02010-06-29 06:01:59 +00001037 classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi);
Chris Lattner1090a9b2010-06-28 21:43:59 +00001038 return;
1039 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001040
Chris Lattner1090a9b2010-06-28 21:43:59 +00001041 if (Ty->hasPointerRepresentation()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001042 Current = Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001043 return;
1044 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001045
Chris Lattner1090a9b2010-06-28 21:43:59 +00001046 if (Ty->isMemberPointerType()) {
Daniel Dunbar67d438d2010-05-15 00:00:37 +00001047 if (Ty->isMemberFunctionPointerType())
1048 Lo = Hi = Integer;
1049 else
1050 Current = Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001051 return;
1052 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001053
Chris Lattner1090a9b2010-06-28 21:43:59 +00001054 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001055 uint64_t Size = getContext().getTypeSize(VT);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001056 if (Size == 32) {
1057 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
1058 // float> as integer.
1059 Current = Integer;
1060
1061 // If this type crosses an eightbyte boundary, it should be
1062 // split.
1063 uint64_t EB_Real = (OffsetBase) / 64;
1064 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
1065 if (EB_Real != EB_Imag)
1066 Hi = Lo;
1067 } else if (Size == 64) {
1068 // gcc passes <1 x double> in memory. :(
1069 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
1070 return;
1071
1072 // gcc passes <1 x long long> as INTEGER.
Chris Lattner473f8e72010-08-26 18:03:20 +00001073 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong) ||
Chris Lattner0fefa412010-08-26 18:13:50 +00001074 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULongLong) ||
1075 VT->getElementType()->isSpecificBuiltinType(BuiltinType::Long) ||
1076 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULong))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001077 Current = Integer;
1078 else
1079 Current = SSE;
1080
1081 // If this type crosses an eightbyte boundary, it should be
1082 // split.
1083 if (OffsetBase && OffsetBase != 64)
1084 Hi = Lo;
1085 } else if (Size == 128) {
1086 Lo = SSE;
1087 Hi = SSEUp;
1088 }
Chris Lattner1090a9b2010-06-28 21:43:59 +00001089 return;
1090 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001091
Chris Lattner1090a9b2010-06-28 21:43:59 +00001092 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001093 QualType ET = getContext().getCanonicalType(CT->getElementType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001094
Chris Lattnerea044322010-07-29 02:01:43 +00001095 uint64_t Size = getContext().getTypeSize(Ty);
Douglas Gregor2ade35e2010-06-16 00:17:44 +00001096 if (ET->isIntegralOrEnumerationType()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001097 if (Size <= 64)
1098 Current = Integer;
1099 else if (Size <= 128)
1100 Lo = Hi = Integer;
Chris Lattnerea044322010-07-29 02:01:43 +00001101 } else if (ET == getContext().FloatTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001102 Current = SSE;
Chris Lattnerea044322010-07-29 02:01:43 +00001103 else if (ET == getContext().DoubleTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001104 Lo = Hi = SSE;
Chris Lattnerea044322010-07-29 02:01:43 +00001105 else if (ET == getContext().LongDoubleTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001106 Current = ComplexX87;
1107
1108 // If this complex type crosses an eightbyte boundary then it
1109 // should be split.
1110 uint64_t EB_Real = (OffsetBase) / 64;
Chris Lattnerea044322010-07-29 02:01:43 +00001111 uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001112 if (Hi == NoClass && EB_Real != EB_Imag)
1113 Hi = Lo;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001114
Chris Lattner1090a9b2010-06-28 21:43:59 +00001115 return;
1116 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001117
Chris Lattnerea044322010-07-29 02:01:43 +00001118 if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001119 // Arrays are treated like structures.
1120
Chris Lattnerea044322010-07-29 02:01:43 +00001121 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001122
1123 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
1124 // than two eightbytes, ..., it has class MEMORY.
1125 if (Size > 128)
1126 return;
1127
1128 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
1129 // fields, it has class MEMORY.
1130 //
1131 // Only need to check alignment of array base.
Chris Lattnerea044322010-07-29 02:01:43 +00001132 if (OffsetBase % getContext().getTypeAlign(AT->getElementType()))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001133 return;
1134
1135 // Otherwise implement simplified merge. We could be smarter about
1136 // this, but it isn't worth it and would be harder to verify.
1137 Current = NoClass;
Chris Lattnerea044322010-07-29 02:01:43 +00001138 uint64_t EltSize = getContext().getTypeSize(AT->getElementType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001139 uint64_t ArraySize = AT->getSize().getZExtValue();
1140 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
1141 Class FieldLo, FieldHi;
Chris Lattner9c254f02010-06-29 06:01:59 +00001142 classify(AT->getElementType(), Offset, FieldLo, FieldHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001143 Lo = merge(Lo, FieldLo);
1144 Hi = merge(Hi, FieldHi);
1145 if (Lo == Memory || Hi == Memory)
1146 break;
1147 }
1148
1149 // Do post merger cleanup (see below). Only case we worry about is Memory.
1150 if (Hi == Memory)
1151 Lo = Memory;
1152 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Chris Lattner1090a9b2010-06-28 21:43:59 +00001153 return;
1154 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001155
Chris Lattner1090a9b2010-06-28 21:43:59 +00001156 if (const RecordType *RT = Ty->getAs<RecordType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001157 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001158
1159 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
1160 // than two eightbytes, ..., it has class MEMORY.
1161 if (Size > 128)
1162 return;
1163
Anders Carlsson0a8f8472009-09-16 15:53:40 +00001164 // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
1165 // copy constructor or a non-trivial destructor, it is passed by invisible
1166 // reference.
1167 if (hasNonTrivialDestructorOrCopyConstructor(RT))
1168 return;
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001169
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001170 const RecordDecl *RD = RT->getDecl();
1171
1172 // Assume variable sized types are passed in memory.
1173 if (RD->hasFlexibleArrayMember())
1174 return;
1175
Chris Lattnerea044322010-07-29 02:01:43 +00001176 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001177
1178 // Reset Lo class, this will be recomputed.
1179 Current = NoClass;
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001180
1181 // If this is a C++ record, classify the bases first.
1182 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1183 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1184 e = CXXRD->bases_end(); i != e; ++i) {
1185 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1186 "Unexpected base class!");
1187 const CXXRecordDecl *Base =
1188 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1189
1190 // Classify this field.
1191 //
1192 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
1193 // single eightbyte, each is classified separately. Each eightbyte gets
1194 // initialized to class NO_CLASS.
1195 Class FieldLo, FieldHi;
Anders Carlssona14f5972010-10-31 23:22:37 +00001196 uint64_t Offset = OffsetBase + Layout.getBaseClassOffsetInBits(Base);
Chris Lattner9c254f02010-06-29 06:01:59 +00001197 classify(i->getType(), Offset, FieldLo, FieldHi);
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001198 Lo = merge(Lo, FieldLo);
1199 Hi = merge(Hi, FieldHi);
1200 if (Lo == Memory || Hi == Memory)
1201 break;
1202 }
1203 }
1204
1205 // Classify the fields one at a time, merging the results.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001206 unsigned idx = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001207 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1208 i != e; ++i, ++idx) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001209 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
1210 bool BitField = i->isBitField();
1211
1212 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
1213 // fields, it has class MEMORY.
1214 //
1215 // Note, skip this test for bit-fields, see below.
Chris Lattnerea044322010-07-29 02:01:43 +00001216 if (!BitField && Offset % getContext().getTypeAlign(i->getType())) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001217 Lo = Memory;
1218 return;
1219 }
1220
1221 // Classify this field.
1222 //
1223 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
1224 // exceeds a single eightbyte, each is classified
1225 // separately. Each eightbyte gets initialized to class
1226 // NO_CLASS.
1227 Class FieldLo, FieldHi;
1228
1229 // Bit-fields require special handling, they do not force the
1230 // structure to be passed in memory even if unaligned, and
1231 // therefore they can straddle an eightbyte.
1232 if (BitField) {
1233 // Ignore padding bit-fields.
1234 if (i->isUnnamedBitfield())
1235 continue;
1236
1237 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Chris Lattnerea044322010-07-29 02:01:43 +00001238 uint64_t Size =
1239 i->getBitWidth()->EvaluateAsInt(getContext()).getZExtValue();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001240
1241 uint64_t EB_Lo = Offset / 64;
1242 uint64_t EB_Hi = (Offset + Size - 1) / 64;
1243 FieldLo = FieldHi = NoClass;
1244 if (EB_Lo) {
1245 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
1246 FieldLo = NoClass;
1247 FieldHi = Integer;
1248 } else {
1249 FieldLo = Integer;
1250 FieldHi = EB_Hi ? Integer : NoClass;
1251 }
1252 } else
Chris Lattner9c254f02010-06-29 06:01:59 +00001253 classify(i->getType(), Offset, FieldLo, FieldHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001254 Lo = merge(Lo, FieldLo);
1255 Hi = merge(Hi, FieldHi);
1256 if (Lo == Memory || Hi == Memory)
1257 break;
1258 }
1259
1260 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
1261 //
1262 // (a) If one of the classes is MEMORY, the whole argument is
1263 // passed in memory.
1264 //
John McCall67a57732011-04-21 01:20:55 +00001265 // (b) If X87UP is not preceded by X87, the whole argument is
1266 // passed in memory.
1267 //
1268 // (c) If the size of the aggregate exceeds two eightbytes and the first
1269 // eight-byte isn’t SSE or any other eightbyte isn’t SSEUP, the whole
1270 // argument is passed in memory.
1271 //
1272 // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001273 //
John McCall67a57732011-04-21 01:20:55 +00001274 // Some of these are enforced by the merging logic. Others can arise
1275 // only with unions; for example:
1276 // union { _Complex double; unsigned; }
1277 //
1278 // Note that clauses (b) and (c) were added in 0.98.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001279 if (Hi == Memory)
1280 Lo = Memory;
John McCall67a57732011-04-21 01:20:55 +00001281 if (Hi == X87Up && Lo != X87 && honorsRevision0_98())
1282 Lo = Memory;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001283 if (Hi == SSEUp && Lo != SSE)
1284 Hi = SSE;
1285 }
1286}
1287
Chris Lattner9c254f02010-06-29 06:01:59 +00001288ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001289 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1290 // place naturally.
John McCalld608cdb2010-08-22 10:59:02 +00001291 if (!isAggregateTypeForABI(Ty)) {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001292 // Treat an enum type as its underlying type.
1293 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1294 Ty = EnumTy->getDecl()->getIntegerType();
1295
1296 return (Ty->isPromotableIntegerType() ?
1297 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1298 }
1299
1300 return ABIArgInfo::getIndirect(0);
1301}
1302
Chris Lattner9c254f02010-06-29 06:01:59 +00001303ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001304 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1305 // place naturally.
John McCalld608cdb2010-08-22 10:59:02 +00001306 if (!isAggregateTypeForABI(Ty)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001307 // Treat an enum type as its underlying type.
1308 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1309 Ty = EnumTy->getDecl()->getIntegerType();
1310
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001311 return (Ty->isPromotableIntegerType() ?
1312 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001313 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001314
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001315 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
1316 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Anders Carlsson0a8f8472009-09-16 15:53:40 +00001317
Chris Lattner855d2272011-05-22 23:21:23 +00001318 // Compute the byval alignment. We specify the alignment of the byval in all
1319 // cases so that the mid-level optimizer knows the alignment of the byval.
1320 unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U);
1321 return ABIArgInfo::getIndirect(Align);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001322}
1323
Chris Lattner0f408f52010-07-29 04:56:46 +00001324/// Get16ByteVectorType - The ABI specifies that a value should be passed in an
1325/// full vector XMM register. Pick an LLVM IR type that will be passed as a
1326/// vector register.
1327const llvm::Type *X86_64ABIInfo::Get16ByteVectorType(QualType Ty) const {
Chris Lattner15842bd2010-07-29 05:02:29 +00001328 const llvm::Type *IRType = CGT.ConvertTypeRecursive(Ty);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001329
Chris Lattner15842bd2010-07-29 05:02:29 +00001330 // Wrapper structs that just contain vectors are passed just like vectors,
1331 // strip them off if present.
1332 const llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType);
1333 while (STy && STy->getNumElements() == 1) {
1334 IRType = STy->getElementType(0);
1335 STy = dyn_cast<llvm::StructType>(IRType);
1336 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001337
Chris Lattner0f408f52010-07-29 04:56:46 +00001338 // If the preferred type is a 16-byte vector, prefer to pass it.
Chris Lattner15842bd2010-07-29 05:02:29 +00001339 if (const llvm::VectorType *VT = dyn_cast<llvm::VectorType>(IRType)){
Chris Lattner0f408f52010-07-29 04:56:46 +00001340 const llvm::Type *EltTy = VT->getElementType();
1341 if (VT->getBitWidth() == 128 &&
1342 (EltTy->isFloatTy() || EltTy->isDoubleTy() ||
1343 EltTy->isIntegerTy(8) || EltTy->isIntegerTy(16) ||
1344 EltTy->isIntegerTy(32) || EltTy->isIntegerTy(64) ||
1345 EltTy->isIntegerTy(128)))
1346 return VT;
1347 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001348
Chris Lattner0f408f52010-07-29 04:56:46 +00001349 return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), 2);
1350}
1351
Chris Lattnere2962be2010-07-29 07:30:00 +00001352/// BitsContainNoUserData - Return true if the specified [start,end) bit range
1353/// is known to either be off the end of the specified type or being in
1354/// alignment padding. The user type specified is known to be at most 128 bits
1355/// in size, and have passed through X86_64ABIInfo::classify with a successful
1356/// classification that put one of the two halves in the INTEGER class.
1357///
1358/// It is conservatively correct to return false.
1359static bool BitsContainNoUserData(QualType Ty, unsigned StartBit,
1360 unsigned EndBit, ASTContext &Context) {
1361 // If the bytes being queried are off the end of the type, there is no user
1362 // data hiding here. This handles analysis of builtins, vectors and other
1363 // types that don't contain interesting padding.
1364 unsigned TySize = (unsigned)Context.getTypeSize(Ty);
1365 if (TySize <= StartBit)
1366 return true;
1367
Chris Lattner021c3a32010-07-29 07:43:55 +00001368 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
1369 unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType());
1370 unsigned NumElts = (unsigned)AT->getSize().getZExtValue();
1371
1372 // Check each element to see if the element overlaps with the queried range.
1373 for (unsigned i = 0; i != NumElts; ++i) {
1374 // If the element is after the span we care about, then we're done..
1375 unsigned EltOffset = i*EltSize;
1376 if (EltOffset >= EndBit) break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001377
Chris Lattner021c3a32010-07-29 07:43:55 +00001378 unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0;
1379 if (!BitsContainNoUserData(AT->getElementType(), EltStart,
1380 EndBit-EltOffset, Context))
1381 return false;
1382 }
1383 // If it overlaps no elements, then it is safe to process as padding.
1384 return true;
1385 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001386
Chris Lattnere2962be2010-07-29 07:30:00 +00001387 if (const RecordType *RT = Ty->getAs<RecordType>()) {
1388 const RecordDecl *RD = RT->getDecl();
1389 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001390
Chris Lattnere2962be2010-07-29 07:30:00 +00001391 // If this is a C++ record, check the bases first.
1392 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1393 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1394 e = CXXRD->bases_end(); i != e; ++i) {
1395 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1396 "Unexpected base class!");
1397 const CXXRecordDecl *Base =
1398 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001399
Chris Lattnere2962be2010-07-29 07:30:00 +00001400 // If the base is after the span we care about, ignore it.
Anders Carlssona14f5972010-10-31 23:22:37 +00001401 unsigned BaseOffset = (unsigned)Layout.getBaseClassOffsetInBits(Base);
Chris Lattnere2962be2010-07-29 07:30:00 +00001402 if (BaseOffset >= EndBit) continue;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001403
Chris Lattnere2962be2010-07-29 07:30:00 +00001404 unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0;
1405 if (!BitsContainNoUserData(i->getType(), BaseStart,
1406 EndBit-BaseOffset, Context))
1407 return false;
1408 }
1409 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001410
Chris Lattnere2962be2010-07-29 07:30:00 +00001411 // Verify that no field has data that overlaps the region of interest. Yes
1412 // this could be sped up a lot by being smarter about queried fields,
1413 // however we're only looking at structs up to 16 bytes, so we don't care
1414 // much.
1415 unsigned idx = 0;
1416 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1417 i != e; ++i, ++idx) {
1418 unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001419
Chris Lattnere2962be2010-07-29 07:30:00 +00001420 // If we found a field after the region we care about, then we're done.
1421 if (FieldOffset >= EndBit) break;
1422
1423 unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0;
1424 if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset,
1425 Context))
1426 return false;
1427 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001428
Chris Lattnere2962be2010-07-29 07:30:00 +00001429 // If nothing in this record overlapped the area of interest, then we're
1430 // clean.
1431 return true;
1432 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001433
Chris Lattnere2962be2010-07-29 07:30:00 +00001434 return false;
1435}
1436
Chris Lattner0b362002010-07-29 18:39:32 +00001437/// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a
1438/// float member at the specified offset. For example, {int,{float}} has a
1439/// float at offset 4. It is conservatively correct for this routine to return
1440/// false.
1441static bool ContainsFloatAtOffset(const llvm::Type *IRType, unsigned IROffset,
1442 const llvm::TargetData &TD) {
1443 // Base case if we find a float.
1444 if (IROffset == 0 && IRType->isFloatTy())
1445 return true;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001446
Chris Lattner0b362002010-07-29 18:39:32 +00001447 // If this is a struct, recurse into the field at the specified offset.
1448 if (const llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
1449 const llvm::StructLayout *SL = TD.getStructLayout(STy);
1450 unsigned Elt = SL->getElementContainingOffset(IROffset);
1451 IROffset -= SL->getElementOffset(Elt);
1452 return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD);
1453 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001454
Chris Lattner0b362002010-07-29 18:39:32 +00001455 // If this is an array, recurse into the field at the specified offset.
1456 if (const llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
1457 const llvm::Type *EltTy = ATy->getElementType();
1458 unsigned EltSize = TD.getTypeAllocSize(EltTy);
1459 IROffset -= IROffset/EltSize*EltSize;
1460 return ContainsFloatAtOffset(EltTy, IROffset, TD);
1461 }
1462
1463 return false;
1464}
1465
Chris Lattnerf47c9442010-07-29 18:13:09 +00001466
1467/// GetSSETypeAtOffset - Return a type that will be passed by the backend in the
1468/// low 8 bytes of an XMM register, corresponding to the SSE class.
1469const llvm::Type *X86_64ABIInfo::
1470GetSSETypeAtOffset(const llvm::Type *IRType, unsigned IROffset,
1471 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnercba8d312010-07-29 18:19:50 +00001472 // The only three choices we have are either double, <2 x float>, or float. We
Chris Lattnerf47c9442010-07-29 18:13:09 +00001473 // pass as float if the last 4 bytes is just padding. This happens for
1474 // structs that contain 3 floats.
1475 if (BitsContainNoUserData(SourceTy, SourceOffset*8+32,
1476 SourceOffset*8+64, getContext()))
1477 return llvm::Type::getFloatTy(getVMContext());
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001478
Chris Lattner0b362002010-07-29 18:39:32 +00001479 // We want to pass as <2 x float> if the LLVM IR type contains a float at
1480 // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the
1481 // case.
1482 if (ContainsFloatAtOffset(IRType, IROffset, getTargetData()) &&
Chris Lattner22fd4ba2010-08-25 23:39:14 +00001483 ContainsFloatAtOffset(IRType, IROffset+4, getTargetData()))
1484 return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001485
Chris Lattnerf47c9442010-07-29 18:13:09 +00001486 return llvm::Type::getDoubleTy(getVMContext());
1487}
1488
1489
Chris Lattner0d2656d2010-07-29 17:40:35 +00001490/// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in
1491/// an 8-byte GPR. This means that we either have a scalar or we are talking
1492/// about the high or low part of an up-to-16-byte struct. This routine picks
1493/// the best LLVM IR type to represent this, which may be i64 or may be anything
Chris Lattner49382de2010-07-28 22:44:07 +00001494/// else that the backend will pass in a GPR that works better (e.g. i8, %foo*,
1495/// etc).
1496///
1497/// PrefType is an LLVM IR type that corresponds to (part of) the IR type for
1498/// the source type. IROffset is an offset in bytes into the LLVM IR type that
1499/// the 8-byte value references. PrefType may be null.
1500///
1501/// SourceTy is the source level type for the entire argument. SourceOffset is
1502/// an offset into this that we're processing (which is always either 0 or 8).
1503///
Chris Lattner44f0fd22010-07-29 02:20:19 +00001504const llvm::Type *X86_64ABIInfo::
Chris Lattner0d2656d2010-07-29 17:40:35 +00001505GetINTEGERTypeAtOffset(const llvm::Type *IRType, unsigned IROffset,
1506 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnere2962be2010-07-29 07:30:00 +00001507 // If we're dealing with an un-offset LLVM IR type, then it means that we're
1508 // returning an 8-byte unit starting with it. See if we can safely use it.
1509 if (IROffset == 0) {
1510 // Pointers and int64's always fill the 8-byte unit.
1511 if (isa<llvm::PointerType>(IRType) || IRType->isIntegerTy(64))
1512 return IRType;
Chris Lattner49382de2010-07-28 22:44:07 +00001513
Chris Lattnere2962be2010-07-29 07:30:00 +00001514 // If we have a 1/2/4-byte integer, we can use it only if the rest of the
1515 // goodness in the source type is just tail padding. This is allowed to
1516 // kick in for struct {double,int} on the int, but not on
1517 // struct{double,int,int} because we wouldn't return the second int. We
1518 // have to do this analysis on the source type because we can't depend on
1519 // unions being lowered a specific way etc.
1520 if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) ||
1521 IRType->isIntegerTy(32)) {
1522 unsigned BitWidth = cast<llvm::IntegerType>(IRType)->getBitWidth();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001523
Chris Lattnere2962be2010-07-29 07:30:00 +00001524 if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth,
1525 SourceOffset*8+64, getContext()))
1526 return IRType;
1527 }
1528 }
Chris Lattner49382de2010-07-28 22:44:07 +00001529
Chris Lattnerfe12d1e2010-07-29 04:51:12 +00001530 if (const llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattner49382de2010-07-28 22:44:07 +00001531 // If this is a struct, recurse into the field at the specified offset.
Chris Lattner44f0fd22010-07-29 02:20:19 +00001532 const llvm::StructLayout *SL = getTargetData().getStructLayout(STy);
Chris Lattner49382de2010-07-28 22:44:07 +00001533 if (IROffset < SL->getSizeInBytes()) {
1534 unsigned FieldIdx = SL->getElementContainingOffset(IROffset);
1535 IROffset -= SL->getElementOffset(FieldIdx);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001536
Chris Lattner0d2656d2010-07-29 17:40:35 +00001537 return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset,
1538 SourceTy, SourceOffset);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001539 }
Chris Lattner49382de2010-07-28 22:44:07 +00001540 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001541
Chris Lattner021c3a32010-07-29 07:43:55 +00001542 if (const llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
1543 const llvm::Type *EltTy = ATy->getElementType();
1544 unsigned EltSize = getTargetData().getTypeAllocSize(EltTy);
1545 unsigned EltOffset = IROffset/EltSize*EltSize;
Chris Lattner0d2656d2010-07-29 17:40:35 +00001546 return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy,
1547 SourceOffset);
Chris Lattner021c3a32010-07-29 07:43:55 +00001548 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001549
Chris Lattner49382de2010-07-28 22:44:07 +00001550 // Okay, we don't have any better idea of what to pass, so we pass this in an
1551 // integer register that isn't too big to fit the rest of the struct.
Chris Lattner9e45a3d2010-07-29 17:34:39 +00001552 unsigned TySizeInBytes =
1553 (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity();
Chris Lattner49382de2010-07-28 22:44:07 +00001554
Chris Lattner9e45a3d2010-07-29 17:34:39 +00001555 assert(TySizeInBytes != SourceOffset && "Empty field?");
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001556
Chris Lattner49382de2010-07-28 22:44:07 +00001557 // It is always safe to classify this as an integer type up to i64 that
1558 // isn't larger than the structure.
Chris Lattner9e45a3d2010-07-29 17:34:39 +00001559 return llvm::IntegerType::get(getVMContext(),
1560 std::min(TySizeInBytes-SourceOffset, 8U)*8);
Chris Lattner9c254f02010-06-29 06:01:59 +00001561}
1562
Chris Lattner66e7b682010-09-01 00:50:20 +00001563
1564/// GetX86_64ByValArgumentPair - Given a high and low type that can ideally
1565/// be used as elements of a two register pair to pass or return, return a
1566/// first class aggregate to represent them. For example, if the low part of
1567/// a by-value argument should be passed as i32* and the high part as float,
1568/// return {i32*, float}.
1569static const llvm::Type *
1570GetX86_64ByValArgumentPair(const llvm::Type *Lo, const llvm::Type *Hi,
1571 const llvm::TargetData &TD) {
1572 // In order to correctly satisfy the ABI, we need to the high part to start
1573 // at offset 8. If the high and low parts we inferred are both 4-byte types
1574 // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have
1575 // the second element at offset 8. Check for this:
1576 unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo);
1577 unsigned HiAlign = TD.getABITypeAlignment(Hi);
1578 unsigned HiStart = llvm::TargetData::RoundUpAlignment(LoSize, HiAlign);
1579 assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!");
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001580
Chris Lattner66e7b682010-09-01 00:50:20 +00001581 // To handle this, we have to increase the size of the low part so that the
1582 // second element will start at an 8 byte offset. We can't increase the size
1583 // of the second element because it might make us access off the end of the
1584 // struct.
1585 if (HiStart != 8) {
1586 // There are only two sorts of types the ABI generation code can produce for
1587 // the low part of a pair that aren't 8 bytes in size: float or i8/i16/i32.
1588 // Promote these to a larger type.
1589 if (Lo->isFloatTy())
1590 Lo = llvm::Type::getDoubleTy(Lo->getContext());
1591 else {
1592 assert(Lo->isIntegerTy() && "Invalid/unknown lo type");
1593 Lo = llvm::Type::getInt64Ty(Lo->getContext());
1594 }
1595 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001596
1597 const llvm::StructType *Result =
Chris Lattner66e7b682010-09-01 00:50:20 +00001598 llvm::StructType::get(Lo->getContext(), Lo, Hi, NULL);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001599
1600
Chris Lattner66e7b682010-09-01 00:50:20 +00001601 // Verify that the second element is at an 8-byte offset.
1602 assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 &&
1603 "Invalid x86-64 argument pair!");
1604 return Result;
1605}
1606
Chris Lattner519f68c2010-07-28 23:06:14 +00001607ABIArgInfo X86_64ABIInfo::
Chris Lattnera3c109b2010-07-29 02:16:43 +00001608classifyReturnType(QualType RetTy) const {
Chris Lattner519f68c2010-07-28 23:06:14 +00001609 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
1610 // classification algorithm.
1611 X86_64ABIInfo::Class Lo, Hi;
1612 classify(RetTy, 0, Lo, Hi);
1613
1614 // Check some invariants.
1615 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Chris Lattner519f68c2010-07-28 23:06:14 +00001616 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1617
1618 const llvm::Type *ResType = 0;
1619 switch (Lo) {
1620 case NoClass:
Chris Lattner117e3f42010-07-30 04:02:24 +00001621 if (Hi == NoClass)
1622 return ABIArgInfo::getIgnore();
1623 // If the low part is just padding, it takes no register, leave ResType
1624 // null.
1625 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
1626 "Unknown missing lo part");
1627 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00001628
1629 case SSEUp:
1630 case X87Up:
1631 assert(0 && "Invalid classification for lo word.");
1632
1633 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
1634 // hidden argument.
1635 case Memory:
1636 return getIndirectReturnResult(RetTy);
1637
1638 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
1639 // available register of the sequence %rax, %rdx is used.
1640 case Integer:
Chris Lattner0d2656d2010-07-29 17:40:35 +00001641 ResType = GetINTEGERTypeAtOffset(CGT.ConvertTypeRecursive(RetTy), 0,
1642 RetTy, 0);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001643
Chris Lattnereb518b42010-07-29 21:42:50 +00001644 // If we have a sign or zero extended integer, make sure to return Extend
1645 // so that the parameter gets the right LLVM IR attributes.
1646 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
1647 // Treat an enum type as its underlying type.
1648 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
1649 RetTy = EnumTy->getDecl()->getIntegerType();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001650
Chris Lattnereb518b42010-07-29 21:42:50 +00001651 if (RetTy->isIntegralOrEnumerationType() &&
1652 RetTy->isPromotableIntegerType())
1653 return ABIArgInfo::getExtend();
1654 }
Chris Lattner519f68c2010-07-28 23:06:14 +00001655 break;
1656
1657 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
1658 // available SSE register of the sequence %xmm0, %xmm1 is used.
1659 case SSE:
Chris Lattnerf47c9442010-07-29 18:13:09 +00001660 ResType = GetSSETypeAtOffset(CGT.ConvertTypeRecursive(RetTy), 0, RetTy, 0);
Chris Lattner0b30c672010-07-28 23:12:33 +00001661 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00001662
1663 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
1664 // returned on the X87 stack in %st0 as 80-bit x87 number.
1665 case X87:
Chris Lattnerea044322010-07-29 02:01:43 +00001666 ResType = llvm::Type::getX86_FP80Ty(getVMContext());
Chris Lattner0b30c672010-07-28 23:12:33 +00001667 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00001668
1669 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
1670 // part of the value is returned in %st0 and the imaginary part in
1671 // %st1.
1672 case ComplexX87:
1673 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Chris Lattnera3c109b2010-07-29 02:16:43 +00001674 ResType = llvm::StructType::get(getVMContext(),
Chris Lattnerea044322010-07-29 02:01:43 +00001675 llvm::Type::getX86_FP80Ty(getVMContext()),
1676 llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattner519f68c2010-07-28 23:06:14 +00001677 NULL);
1678 break;
1679 }
1680
Chris Lattner3db4dde2010-09-01 00:20:33 +00001681 const llvm::Type *HighPart = 0;
Chris Lattner519f68c2010-07-28 23:06:14 +00001682 switch (Hi) {
1683 // Memory was handled previously and X87 should
1684 // never occur as a hi class.
1685 case Memory:
1686 case X87:
1687 assert(0 && "Invalid classification for hi word.");
1688
1689 case ComplexX87: // Previously handled.
Chris Lattner0b30c672010-07-28 23:12:33 +00001690 case NoClass:
1691 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00001692
Chris Lattner3db4dde2010-09-01 00:20:33 +00001693 case Integer:
1694 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertTypeRecursive(RetTy),
1695 8, RetTy, 8);
1696 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
1697 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner519f68c2010-07-28 23:06:14 +00001698 break;
Chris Lattner3db4dde2010-09-01 00:20:33 +00001699 case SSE:
1700 HighPart = GetSSETypeAtOffset(CGT.ConvertTypeRecursive(RetTy), 8, RetTy, 8);
1701 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
1702 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner519f68c2010-07-28 23:06:14 +00001703 break;
1704
1705 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
1706 // is passed in the upper half of the last used SSE register.
1707 //
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001708 // SSEUP should always be preceded by SSE, just widen.
Chris Lattner519f68c2010-07-28 23:06:14 +00001709 case SSEUp:
1710 assert(Lo == SSE && "Unexpected SSEUp classification.");
Chris Lattner0f408f52010-07-29 04:56:46 +00001711 ResType = Get16ByteVectorType(RetTy);
Chris Lattner519f68c2010-07-28 23:06:14 +00001712 break;
1713
1714 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
1715 // returned together with the previous X87 value in %st0.
1716 case X87Up:
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001717 // If X87Up is preceded by X87, we don't need to do
Chris Lattner519f68c2010-07-28 23:06:14 +00001718 // anything. However, in some cases with unions it may not be
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001719 // preceded by X87. In such situations we follow gcc and pass the
Chris Lattner519f68c2010-07-28 23:06:14 +00001720 // extra bits in an SSE reg.
Chris Lattner603519d2010-07-29 17:49:08 +00001721 if (Lo != X87) {
Chris Lattner3db4dde2010-09-01 00:20:33 +00001722 HighPart = GetSSETypeAtOffset(CGT.ConvertTypeRecursive(RetTy),
1723 8, RetTy, 8);
1724 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
1725 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner603519d2010-07-29 17:49:08 +00001726 }
Chris Lattner519f68c2010-07-28 23:06:14 +00001727 break;
1728 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001729
Chris Lattner3db4dde2010-09-01 00:20:33 +00001730 // If a high part was specified, merge it together with the low part. It is
Chris Lattner645406a2010-09-01 00:24:35 +00001731 // known to pass in the high eightbyte of the result. We do this by forming a
1732 // first class struct aggregate with the high and low part: {low, high}
Chris Lattner66e7b682010-09-01 00:50:20 +00001733 if (HighPart)
1734 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getTargetData());
Chris Lattner519f68c2010-07-28 23:06:14 +00001735
Chris Lattnereb518b42010-07-29 21:42:50 +00001736 return ABIArgInfo::getDirect(ResType);
Chris Lattner519f68c2010-07-28 23:06:14 +00001737}
1738
Chris Lattnera3c109b2010-07-29 02:16:43 +00001739ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, unsigned &neededInt,
Bill Wendling99aaae82010-10-18 23:51:38 +00001740 unsigned &neededSSE) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001741 X86_64ABIInfo::Class Lo, Hi;
Chris Lattner9c254f02010-06-29 06:01:59 +00001742 classify(Ty, 0, Lo, Hi);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001743
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001744 // Check some invariants.
1745 // FIXME: Enforce these by construction.
1746 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001747 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1748
1749 neededInt = 0;
1750 neededSSE = 0;
1751 const llvm::Type *ResType = 0;
1752 switch (Lo) {
1753 case NoClass:
Chris Lattner117e3f42010-07-30 04:02:24 +00001754 if (Hi == NoClass)
1755 return ABIArgInfo::getIgnore();
1756 // If the low part is just padding, it takes no register, leave ResType
1757 // null.
1758 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
1759 "Unknown missing lo part");
1760 break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001761
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001762 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
1763 // on the stack.
1764 case Memory:
1765
1766 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
1767 // COMPLEX_X87, it is passed in memory.
1768 case X87:
1769 case ComplexX87:
Chris Lattner9c254f02010-06-29 06:01:59 +00001770 return getIndirectResult(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001771
1772 case SSEUp:
1773 case X87Up:
1774 assert(0 && "Invalid classification for lo word.");
1775
1776 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
1777 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
1778 // and %r9 is used.
1779 case Integer:
Chris Lattner9c254f02010-06-29 06:01:59 +00001780 ++neededInt;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001781
Chris Lattner49382de2010-07-28 22:44:07 +00001782 // Pick an 8-byte type based on the preferred type.
Chris Lattner0d2656d2010-07-29 17:40:35 +00001783 ResType = GetINTEGERTypeAtOffset(CGT.ConvertTypeRecursive(Ty), 0, Ty, 0);
Chris Lattnereb518b42010-07-29 21:42:50 +00001784
1785 // If we have a sign or zero extended integer, make sure to return Extend
1786 // so that the parameter gets the right LLVM IR attributes.
1787 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
1788 // Treat an enum type as its underlying type.
1789 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1790 Ty = EnumTy->getDecl()->getIntegerType();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001791
Chris Lattnereb518b42010-07-29 21:42:50 +00001792 if (Ty->isIntegralOrEnumerationType() &&
1793 Ty->isPromotableIntegerType())
1794 return ABIArgInfo::getExtend();
1795 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001796
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001797 break;
1798
1799 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
1800 // available SSE register is used, the registers are taken in the
1801 // order from %xmm0 to %xmm7.
Bill Wendlingbb465d72010-10-18 03:41:31 +00001802 case SSE: {
1803 const llvm::Type *IRType = CGT.ConvertTypeRecursive(Ty);
Bill Wendling99aaae82010-10-18 23:51:38 +00001804 if (Hi != NoClass || !UseX86_MMXType(IRType))
Bill Wendlingbb465d72010-10-18 03:41:31 +00001805 ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0);
Bill Wendling99aaae82010-10-18 23:51:38 +00001806 else
Bill Wendlingbb465d72010-10-18 03:41:31 +00001807 // This is an MMX type. Treat it as such.
1808 ResType = llvm::Type::getX86_MMXTy(getVMContext());
Bill Wendlingbb465d72010-10-18 03:41:31 +00001809
Bill Wendling99aaae82010-10-18 23:51:38 +00001810 ++neededSSE;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001811 break;
1812 }
Bill Wendlingbb465d72010-10-18 03:41:31 +00001813 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001814
Chris Lattner645406a2010-09-01 00:24:35 +00001815 const llvm::Type *HighPart = 0;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001816 switch (Hi) {
1817 // Memory was handled previously, ComplexX87 and X87 should
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001818 // never occur as hi classes, and X87Up must be preceded by X87,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001819 // which is passed in memory.
1820 case Memory:
1821 case X87:
1822 case ComplexX87:
1823 assert(0 && "Invalid classification for hi word.");
1824 break;
1825
1826 case NoClass: break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001827
Chris Lattner645406a2010-09-01 00:24:35 +00001828 case Integer:
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001829 ++neededInt;
Chris Lattner49382de2010-07-28 22:44:07 +00001830 // Pick an 8-byte type based on the preferred type.
Chris Lattner645406a2010-09-01 00:24:35 +00001831 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertTypeRecursive(Ty), 8, Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001832
Chris Lattner645406a2010-09-01 00:24:35 +00001833 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
1834 return ABIArgInfo::getDirect(HighPart, 8);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001835 break;
1836
1837 // X87Up generally doesn't occur here (long double is passed in
1838 // memory), except in situations involving unions.
1839 case X87Up:
Chris Lattner645406a2010-09-01 00:24:35 +00001840 case SSE:
1841 HighPart = GetSSETypeAtOffset(CGT.ConvertTypeRecursive(Ty), 8, Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001842
Chris Lattner645406a2010-09-01 00:24:35 +00001843 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
1844 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner117e3f42010-07-30 04:02:24 +00001845
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001846 ++neededSSE;
1847 break;
1848
1849 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
1850 // eightbyte is passed in the upper half of the last used SSE
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001851 // register. This only happens when 128-bit vectors are passed.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001852 case SSEUp:
Chris Lattnerab5722e2010-07-28 23:47:21 +00001853 assert(Lo == SSE && "Unexpected SSEUp classification");
Chris Lattner0f408f52010-07-29 04:56:46 +00001854 ResType = Get16ByteVectorType(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001855 break;
1856 }
1857
Chris Lattner645406a2010-09-01 00:24:35 +00001858 // If a high part was specified, merge it together with the low part. It is
1859 // known to pass in the high eightbyte of the result. We do this by forming a
1860 // first class struct aggregate with the high and low part: {low, high}
1861 if (HighPart)
Chris Lattner66e7b682010-09-01 00:50:20 +00001862 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getTargetData());
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001863
Chris Lattnereb518b42010-07-29 21:42:50 +00001864 return ABIArgInfo::getDirect(ResType);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001865}
1866
Chris Lattneree5dcd02010-07-29 02:31:05 +00001867void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001868
Chris Lattnera3c109b2010-07-29 02:16:43 +00001869 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001870
1871 // Keep track of the number of assigned registers.
Bill Wendling99aaae82010-10-18 23:51:38 +00001872 unsigned freeIntRegs = 6, freeSSERegs = 8;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001873
1874 // If the return value is indirect, then the hidden argument is consuming one
1875 // integer register.
1876 if (FI.getReturnInfo().isIndirect())
1877 --freeIntRegs;
1878
1879 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
1880 // get assigned (in left-to-right order) for passing as follows...
1881 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1882 it != ie; ++it) {
Bill Wendling99aaae82010-10-18 23:51:38 +00001883 unsigned neededInt, neededSSE;
1884 it->info = classifyArgumentType(it->type, neededInt, neededSSE);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001885
1886 // AMD64-ABI 3.2.3p3: If there are no registers available for any
1887 // eightbyte of an argument, the whole argument is passed on the
1888 // stack. If registers have already been assigned for some
1889 // eightbytes of such an argument, the assignments get reverted.
Bill Wendling99aaae82010-10-18 23:51:38 +00001890 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001891 freeIntRegs -= neededInt;
1892 freeSSERegs -= neededSSE;
1893 } else {
Chris Lattner9c254f02010-06-29 06:01:59 +00001894 it->info = getIndirectResult(it->type);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001895 }
1896 }
1897}
1898
1899static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
1900 QualType Ty,
1901 CodeGenFunction &CGF) {
1902 llvm::Value *overflow_arg_area_p =
1903 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
1904 llvm::Value *overflow_arg_area =
1905 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
1906
1907 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
1908 // byte boundary if alignment needed by type exceeds 8 byte boundary.
1909 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
1910 if (Align > 8) {
1911 // Note that we follow the ABI & gcc here, even though the type
1912 // could in theory have an alignment greater than 16. This case
1913 // shouldn't ever matter in practice.
1914
1915 // overflow_arg_area = (overflow_arg_area + 15) & ~15;
Owen Anderson0032b272009-08-13 21:57:51 +00001916 llvm::Value *Offset =
Chris Lattner77b89b82010-06-27 07:15:29 +00001917 llvm::ConstantInt::get(CGF.Int32Ty, 15);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001918 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
1919 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
Chris Lattner77b89b82010-06-27 07:15:29 +00001920 CGF.Int64Ty);
1921 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, ~15LL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001922 overflow_arg_area =
1923 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
1924 overflow_arg_area->getType(),
1925 "overflow_arg_area.align");
1926 }
1927
1928 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
1929 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1930 llvm::Value *Res =
1931 CGF.Builder.CreateBitCast(overflow_arg_area,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001932 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001933
1934 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
1935 // l->overflow_arg_area + sizeof(type).
1936 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
1937 // an 8 byte boundary.
1938
1939 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
Owen Anderson0032b272009-08-13 21:57:51 +00001940 llvm::Value *Offset =
Chris Lattner77b89b82010-06-27 07:15:29 +00001941 llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001942 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
1943 "overflow_arg_area.next");
1944 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
1945
1946 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
1947 return Res;
1948}
1949
1950llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1951 CodeGenFunction &CGF) const {
Owen Andersona1cf15f2009-07-14 23:10:40 +00001952 llvm::LLVMContext &VMContext = CGF.getLLVMContext();
Mike Stump1eb44332009-09-09 15:08:12 +00001953
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001954 // Assume that va_list type is correct; should be pointer to LLVM type:
1955 // struct {
1956 // i32 gp_offset;
1957 // i32 fp_offset;
1958 // i8* overflow_arg_area;
1959 // i8* reg_save_area;
1960 // };
Bill Wendling99aaae82010-10-18 23:51:38 +00001961 unsigned neededInt, neededSSE;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001962
Chris Lattnera14db752010-03-11 18:19:55 +00001963 Ty = CGF.getContext().getCanonicalType(Ty);
Bill Wendling99aaae82010-10-18 23:51:38 +00001964 ABIArgInfo AI = classifyArgumentType(Ty, neededInt, neededSSE);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001965
1966 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
1967 // in the registers. If not go to step 7.
1968 if (!neededInt && !neededSSE)
1969 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1970
1971 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
1972 // general purpose registers needed to pass type and num_fp to hold
1973 // the number of floating point registers needed.
1974
1975 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
1976 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
1977 // l->fp_offset > 304 - num_fp * 16 go to step 7.
1978 //
1979 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
1980 // register save space).
1981
1982 llvm::Value *InRegs = 0;
1983 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
1984 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
1985 if (neededInt) {
1986 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
1987 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
Chris Lattner1090a9b2010-06-28 21:43:59 +00001988 InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8);
1989 InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001990 }
1991
1992 if (neededSSE) {
1993 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
1994 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
1995 llvm::Value *FitsInFP =
Chris Lattner1090a9b2010-06-28 21:43:59 +00001996 llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16);
1997 FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001998 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
1999 }
2000
2001 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
2002 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
2003 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
2004 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
2005
2006 // Emit code to load the value if it was passed in registers.
2007
2008 CGF.EmitBlock(InRegBlock);
2009
2010 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
2011 // an offset of l->gp_offset and/or l->fp_offset. This may require
2012 // copying to a temporary location in case the parameter is passed
2013 // in different register classes or requires an alignment greater
2014 // than 8 for general purpose registers and 16 for XMM registers.
2015 //
2016 // FIXME: This really results in shameful code when we end up needing to
2017 // collect arguments from different places; often what should result in a
2018 // simple assembling of a structure from scattered addresses has many more
2019 // loads than necessary. Can we clean this up?
2020 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
2021 llvm::Value *RegAddr =
2022 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
2023 "reg_save_area");
2024 if (neededInt && neededSSE) {
2025 // FIXME: Cleanup.
Chris Lattner800588f2010-07-29 06:26:06 +00002026 assert(AI.isDirect() && "Unexpected ABI info for mixed regs");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002027 const llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
2028 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
2029 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
2030 const llvm::Type *TyLo = ST->getElementType(0);
2031 const llvm::Type *TyHi = ST->getElementType(1);
Chris Lattnera8b7a7d2010-08-26 06:28:35 +00002032 assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) &&
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002033 "Unexpected ABI info for mixed regs");
Owen Anderson96e0fc72009-07-29 22:16:19 +00002034 const llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
2035 const llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002036 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2037 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Duncan Sandsf177d9d2010-02-15 16:14:01 +00002038 llvm::Value *RegLoAddr = TyLo->isFloatingPointTy() ? FPAddr : GPAddr;
2039 llvm::Value *RegHiAddr = TyLo->isFloatingPointTy() ? GPAddr : FPAddr;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002040 llvm::Value *V =
2041 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
2042 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2043 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
2044 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2045
Owen Andersona1cf15f2009-07-14 23:10:40 +00002046 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson96e0fc72009-07-29 22:16:19 +00002047 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002048 } else if (neededInt) {
2049 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2050 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson96e0fc72009-07-29 22:16:19 +00002051 llvm::PointerType::getUnqual(LTy));
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002052 } else if (neededSSE == 1) {
2053 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
2054 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
2055 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002056 } else {
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002057 assert(neededSSE == 2 && "Invalid number of needed registers!");
2058 // SSE registers are spaced 16 bytes apart in the register save
2059 // area, we need to collect the two eightbytes together.
2060 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Chris Lattner1090a9b2010-06-28 21:43:59 +00002061 llvm::Value *RegAddrHi = CGF.Builder.CreateConstGEP1_32(RegAddrLo, 16);
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002062 const llvm::Type *DoubleTy = llvm::Type::getDoubleTy(VMContext);
2063 const llvm::Type *DblPtrTy =
2064 llvm::PointerType::getUnqual(DoubleTy);
2065 const llvm::StructType *ST = llvm::StructType::get(VMContext, DoubleTy,
2066 DoubleTy, NULL);
2067 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
2068 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
2069 DblPtrTy));
2070 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2071 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
2072 DblPtrTy));
2073 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2074 RegAddr = CGF.Builder.CreateBitCast(Tmp,
2075 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002076 }
2077
2078 // AMD64-ABI 3.5.7p5: Step 5. Set:
2079 // l->gp_offset = l->gp_offset + num_gp * 8
2080 // l->fp_offset = l->fp_offset + num_fp * 16.
2081 if (neededInt) {
Chris Lattner77b89b82010-06-27 07:15:29 +00002082 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002083 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
2084 gp_offset_p);
2085 }
2086 if (neededSSE) {
Chris Lattner77b89b82010-06-27 07:15:29 +00002087 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002088 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
2089 fp_offset_p);
2090 }
2091 CGF.EmitBranch(ContBlock);
2092
2093 // Emit code to load the value if it was passed in memory.
2094
2095 CGF.EmitBlock(InMemBlock);
2096 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2097
2098 // Return the appropriate result.
2099
2100 CGF.EmitBlock(ContBlock);
Jay Foadbbf3bac2011-03-30 11:28:58 +00002101 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(), 2,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002102 "vaarg.addr");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002103 ResAddr->addIncoming(RegAddr, InRegBlock);
2104 ResAddr->addIncoming(MemAddr, InMemBlock);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002105 return ResAddr;
2106}
2107
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002108ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty) const {
2109
2110 if (Ty->isVoidType())
2111 return ABIArgInfo::getIgnore();
2112
2113 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2114 Ty = EnumTy->getDecl()->getIntegerType();
2115
2116 uint64_t Size = getContext().getTypeSize(Ty);
2117
2118 if (const RecordType *RT = Ty->getAs<RecordType>()) {
NAKAMURA Takumiff8be0e2011-01-19 00:11:33 +00002119 if (hasNonTrivialDestructorOrCopyConstructor(RT) ||
2120 RT->getDecl()->hasFlexibleArrayMember())
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002121 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2122
NAKAMURA Takumi6f174332011-02-22 03:56:57 +00002123 // FIXME: mingw-w64-gcc emits 128-bit struct as i128
2124 if (Size == 128 &&
2125 getContext().Target.getTriple().getOS() == llvm::Triple::MinGW32)
2126 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2127 Size));
2128
2129 // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is
2130 // not 1, 2, 4, or 8 bytes, must be passed by reference."
2131 if (Size <= 64 &&
NAKAMURA Takumiff8be0e2011-01-19 00:11:33 +00002132 (Size & (Size - 1)) == 0)
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002133 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2134 Size));
2135
2136 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2137 }
2138
2139 if (Ty->isPromotableIntegerType())
2140 return ABIArgInfo::getExtend();
2141
2142 return ABIArgInfo::getDirect();
2143}
2144
2145void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
2146
2147 QualType RetTy = FI.getReturnType();
2148 FI.getReturnInfo() = classify(RetTy);
2149
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002150 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2151 it != ie; ++it)
2152 it->info = classify(it->type);
2153}
2154
Chris Lattnerf13721d2010-08-31 16:44:54 +00002155llvm::Value *WinX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2156 CodeGenFunction &CGF) const {
2157 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
2158 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002159
Chris Lattnerf13721d2010-08-31 16:44:54 +00002160 CGBuilderTy &Builder = CGF.Builder;
2161 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2162 "ap");
2163 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2164 llvm::Type *PTy =
2165 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
2166 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2167
2168 uint64_t Offset =
2169 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 8);
2170 llvm::Value *NextAddr =
2171 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
2172 "ap.next");
2173 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2174
2175 return AddrTyped;
2176}
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002177
John McCallec853ba2010-03-11 00:10:12 +00002178// PowerPC-32
2179
2180namespace {
2181class PPC32TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
2182public:
Chris Lattnerea044322010-07-29 02:01:43 +00002183 PPC32TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002184
John McCallec853ba2010-03-11 00:10:12 +00002185 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2186 // This is recovered from gcc output.
2187 return 1; // r1 is the dedicated stack pointer
2188 }
2189
2190 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002191 llvm::Value *Address) const;
John McCallec853ba2010-03-11 00:10:12 +00002192};
2193
2194}
2195
2196bool
2197PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2198 llvm::Value *Address) const {
2199 // This is calculated from the LLVM and GCC tables and verified
2200 // against gcc output. AFAIK all ABIs use the same encoding.
2201
2202 CodeGen::CGBuilderTy &Builder = CGF.Builder;
2203 llvm::LLVMContext &Context = CGF.getLLVMContext();
2204
2205 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
2206 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2207 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
2208 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
2209
2210 // 0-31: r0-31, the 4-byte general-purpose registers
John McCallaeeb7012010-05-27 06:19:26 +00002211 AssignToArrayRange(Builder, Address, Four8, 0, 31);
John McCallec853ba2010-03-11 00:10:12 +00002212
2213 // 32-63: fp0-31, the 8-byte floating-point registers
John McCallaeeb7012010-05-27 06:19:26 +00002214 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
John McCallec853ba2010-03-11 00:10:12 +00002215
2216 // 64-76 are various 4-byte special-purpose registers:
2217 // 64: mq
2218 // 65: lr
2219 // 66: ctr
2220 // 67: ap
2221 // 68-75 cr0-7
2222 // 76: xer
John McCallaeeb7012010-05-27 06:19:26 +00002223 AssignToArrayRange(Builder, Address, Four8, 64, 76);
John McCallec853ba2010-03-11 00:10:12 +00002224
2225 // 77-108: v0-31, the 16-byte vector registers
John McCallaeeb7012010-05-27 06:19:26 +00002226 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
John McCallec853ba2010-03-11 00:10:12 +00002227
2228 // 109: vrsave
2229 // 110: vscr
2230 // 111: spe_acc
2231 // 112: spefscr
2232 // 113: sfp
John McCallaeeb7012010-05-27 06:19:26 +00002233 AssignToArrayRange(Builder, Address, Four8, 109, 113);
John McCallec853ba2010-03-11 00:10:12 +00002234
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002235 return false;
John McCallec853ba2010-03-11 00:10:12 +00002236}
2237
2238
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002239//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002240// ARM ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002241//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002242
2243namespace {
2244
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002245class ARMABIInfo : public ABIInfo {
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002246public:
2247 enum ABIKind {
2248 APCS = 0,
2249 AAPCS = 1,
2250 AAPCS_VFP
2251 };
2252
2253private:
2254 ABIKind Kind;
2255
2256public:
Chris Lattnerea044322010-07-29 02:01:43 +00002257 ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind) {}
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002258
2259private:
2260 ABIKind getABIKind() const { return Kind; }
2261
Chris Lattnera3c109b2010-07-29 02:16:43 +00002262 ABIArgInfo classifyReturnType(QualType RetTy) const;
2263 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002264
Chris Lattneree5dcd02010-07-29 02:31:05 +00002265 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002266
2267 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2268 CodeGenFunction &CGF) const;
2269};
2270
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002271class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
2272public:
Chris Lattnerea044322010-07-29 02:01:43 +00002273 ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
2274 :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {}
John McCall6374c332010-03-06 00:35:14 +00002275
2276 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2277 return 13;
2278 }
Roman Divacky09345d12011-05-18 19:36:54 +00002279
John McCallf85e1932011-06-15 23:02:42 +00002280 llvm::StringRef getARCRetainAutoreleasedReturnValueMarker() const {
2281 return "mov\tr7, r7\t\t@ marker for objc_retainAutoreleaseReturnValue";
2282 }
2283
Roman Divacky09345d12011-05-18 19:36:54 +00002284 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2285 llvm::Value *Address) const {
2286 CodeGen::CGBuilderTy &Builder = CGF.Builder;
2287 llvm::LLVMContext &Context = CGF.getLLVMContext();
2288
2289 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
2290 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2291
2292 // 0-15 are the 16 integer registers.
2293 AssignToArrayRange(Builder, Address, Four8, 0, 15);
2294
2295 return false;
2296 }
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002297};
2298
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002299}
2300
Chris Lattneree5dcd02010-07-29 02:31:05 +00002301void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +00002302 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002303 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
Chris Lattnera3c109b2010-07-29 02:16:43 +00002304 it != ie; ++it)
2305 it->info = classifyArgumentType(it->type);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002306
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002307 // Always honor user-specified calling convention.
2308 if (FI.getCallingConvention() != llvm::CallingConv::C)
2309 return;
2310
2311 // Calling convention as default by an ABI.
Rafael Espindola25117ab2010-06-16 16:13:39 +00002312 llvm::CallingConv::ID DefaultCC;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002313 llvm::StringRef Env = getContext().Target.getTriple().getEnvironmentName();
2314 if (Env == "gnueabi" || Env == "eabi")
Rafael Espindola25117ab2010-06-16 16:13:39 +00002315 DefaultCC = llvm::CallingConv::ARM_AAPCS;
Rafael Espindola1ed1a592010-06-16 19:01:17 +00002316 else
2317 DefaultCC = llvm::CallingConv::ARM_APCS;
Rafael Espindola25117ab2010-06-16 16:13:39 +00002318
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002319 // If user did not ask for specific calling convention explicitly (e.g. via
2320 // pcs attribute), set effective calling convention if it's different than ABI
2321 // default.
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002322 switch (getABIKind()) {
2323 case APCS:
Rafael Espindola25117ab2010-06-16 16:13:39 +00002324 if (DefaultCC != llvm::CallingConv::ARM_APCS)
2325 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_APCS);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002326 break;
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002327 case AAPCS:
Rafael Espindola25117ab2010-06-16 16:13:39 +00002328 if (DefaultCC != llvm::CallingConv::ARM_AAPCS)
2329 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002330 break;
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002331 case AAPCS_VFP:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002332 if (DefaultCC != llvm::CallingConv::ARM_AAPCS_VFP)
2333 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS_VFP);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002334 break;
2335 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002336}
2337
Chris Lattnera3c109b2010-07-29 02:16:43 +00002338ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty) const {
John McCalld608cdb2010-08-22 10:59:02 +00002339 if (!isAggregateTypeForABI(Ty)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00002340 // Treat an enum type as its underlying type.
2341 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2342 Ty = EnumTy->getDecl()->getIntegerType();
2343
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00002344 return (Ty->isPromotableIntegerType() ?
2345 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00002346 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00002347
Daniel Dunbar42025572009-09-14 21:54:03 +00002348 // Ignore empty records.
Chris Lattnera3c109b2010-07-29 02:16:43 +00002349 if (isEmptyRecord(getContext(), Ty, true))
Daniel Dunbar42025572009-09-14 21:54:03 +00002350 return ABIArgInfo::getIgnore();
2351
Rafael Espindola0eb1d972010-06-08 02:42:08 +00002352 // Structures with either a non-trivial destructor or a non-trivial
2353 // copy constructor are always indirect.
2354 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
2355 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2356
Daniel Dunbar8aa87c72010-09-23 01:54:28 +00002357 // Otherwise, pass by coercing to a structure of the appropriate size.
2358 //
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002359 // FIXME: This doesn't handle alignment > 64 bits.
2360 const llvm::Type* ElemTy;
2361 unsigned SizeRegs;
Stuart Hastingsa1dadc92011-04-28 21:35:59 +00002362 if (getContext().getTypeSizeInChars(Ty) <= CharUnits::fromQuantity(64)) {
Eric Christopherad27eea2011-04-26 01:02:04 +00002363 ElemTy = llvm::Type::getInt32Ty(getVMContext());
2364 SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32;
Stuart Hastingsb7f62d02011-04-28 18:16:06 +00002365 } else if (getABIKind() == ARMABIInfo::APCS) {
Stuart Hastings67d097e2011-04-27 17:24:02 +00002366 // Initial ARM ByVal support is APCS-only.
2367 return ABIArgInfo::getIndirect(0, /*ByVal=*/true);
2368 } else {
2369 // FIXME: This is kind of nasty... but there isn't much choice
2370 // because most of the ARM calling conventions don't yet support
2371 // byval.
2372 ElemTy = llvm::Type::getInt64Ty(getVMContext());
2373 SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64;
Stuart Hastings67d097e2011-04-27 17:24:02 +00002374 }
Stuart Hastingsb7f62d02011-04-28 18:16:06 +00002375
Stuart Hastingsea971e92011-04-28 19:24:47 +00002376 const llvm::Type *STy =
2377 llvm::StructType::get(getVMContext(),
2378 llvm::ArrayType::get(ElemTy, SizeRegs), NULL, NULL);
Stuart Hastingsb7f62d02011-04-28 18:16:06 +00002379 return ABIArgInfo::getDirect(STy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002380}
2381
Chris Lattnera3c109b2010-07-29 02:16:43 +00002382static bool isIntegerLikeType(QualType Ty, ASTContext &Context,
Daniel Dunbar98303b92009-09-13 08:03:58 +00002383 llvm::LLVMContext &VMContext) {
2384 // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
2385 // is called integer-like if its size is less than or equal to one word, and
2386 // the offset of each of its addressable sub-fields is zero.
2387
2388 uint64_t Size = Context.getTypeSize(Ty);
2389
2390 // Check that the type fits in a word.
2391 if (Size > 32)
2392 return false;
2393
2394 // FIXME: Handle vector types!
2395 if (Ty->isVectorType())
2396 return false;
2397
Daniel Dunbarb0d58192009-09-14 02:20:34 +00002398 // Float types are never treated as "integer like".
2399 if (Ty->isRealFloatingType())
2400 return false;
2401
Daniel Dunbar98303b92009-09-13 08:03:58 +00002402 // If this is a builtin or pointer type then it is ok.
John McCall183700f2009-09-21 23:43:11 +00002403 if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
Daniel Dunbar98303b92009-09-13 08:03:58 +00002404 return true;
2405
Daniel Dunbar45815812010-02-01 23:31:26 +00002406 // Small complex integer types are "integer like".
2407 if (const ComplexType *CT = Ty->getAs<ComplexType>())
2408 return isIntegerLikeType(CT->getElementType(), Context, VMContext);
Daniel Dunbar98303b92009-09-13 08:03:58 +00002409
2410 // Single element and zero sized arrays should be allowed, by the definition
2411 // above, but they are not.
2412
2413 // Otherwise, it must be a record type.
2414 const RecordType *RT = Ty->getAs<RecordType>();
2415 if (!RT) return false;
2416
2417 // Ignore records with flexible arrays.
2418 const RecordDecl *RD = RT->getDecl();
2419 if (RD->hasFlexibleArrayMember())
2420 return false;
2421
2422 // Check that all sub-fields are at offset 0, and are themselves "integer
2423 // like".
2424 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
2425
2426 bool HadField = false;
2427 unsigned idx = 0;
2428 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
2429 i != e; ++i, ++idx) {
2430 const FieldDecl *FD = *i;
2431
Daniel Dunbar679855a2010-01-29 03:22:29 +00002432 // Bit-fields are not addressable, we only need to verify they are "integer
2433 // like". We still have to disallow a subsequent non-bitfield, for example:
2434 // struct { int : 0; int x }
2435 // is non-integer like according to gcc.
2436 if (FD->isBitField()) {
2437 if (!RD->isUnion())
2438 HadField = true;
Daniel Dunbar98303b92009-09-13 08:03:58 +00002439
Daniel Dunbar679855a2010-01-29 03:22:29 +00002440 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
2441 return false;
Daniel Dunbar98303b92009-09-13 08:03:58 +00002442
Daniel Dunbar679855a2010-01-29 03:22:29 +00002443 continue;
Daniel Dunbar98303b92009-09-13 08:03:58 +00002444 }
2445
Daniel Dunbar679855a2010-01-29 03:22:29 +00002446 // Check if this field is at offset 0.
2447 if (Layout.getFieldOffset(idx) != 0)
2448 return false;
2449
Daniel Dunbar98303b92009-09-13 08:03:58 +00002450 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
2451 return false;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002452
Daniel Dunbar679855a2010-01-29 03:22:29 +00002453 // Only allow at most one field in a structure. This doesn't match the
2454 // wording above, but follows gcc in situations with a field following an
2455 // empty structure.
Daniel Dunbar98303b92009-09-13 08:03:58 +00002456 if (!RD->isUnion()) {
2457 if (HadField)
2458 return false;
2459
2460 HadField = true;
2461 }
2462 }
2463
2464 return true;
2465}
2466
Chris Lattnera3c109b2010-07-29 02:16:43 +00002467ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy) const {
Daniel Dunbar98303b92009-09-13 08:03:58 +00002468 if (RetTy->isVoidType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002469 return ABIArgInfo::getIgnore();
Daniel Dunbar98303b92009-09-13 08:03:58 +00002470
Daniel Dunbarf554b1c2010-09-23 01:54:32 +00002471 // Large vector types should be returned via memory.
2472 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128)
2473 return ABIArgInfo::getIndirect(0);
2474
John McCalld608cdb2010-08-22 10:59:02 +00002475 if (!isAggregateTypeForABI(RetTy)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00002476 // Treat an enum type as its underlying type.
2477 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2478 RetTy = EnumTy->getDecl()->getIntegerType();
2479
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00002480 return (RetTy->isPromotableIntegerType() ?
2481 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00002482 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00002483
Rafael Espindola0eb1d972010-06-08 02:42:08 +00002484 // Structures with either a non-trivial destructor or a non-trivial
2485 // copy constructor are always indirect.
2486 if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
2487 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2488
Daniel Dunbar98303b92009-09-13 08:03:58 +00002489 // Are we following APCS?
2490 if (getABIKind() == APCS) {
Chris Lattnera3c109b2010-07-29 02:16:43 +00002491 if (isEmptyRecord(getContext(), RetTy, false))
Daniel Dunbar98303b92009-09-13 08:03:58 +00002492 return ABIArgInfo::getIgnore();
2493
Daniel Dunbar4cc753f2010-02-01 23:31:19 +00002494 // Complex types are all returned as packed integers.
2495 //
2496 // FIXME: Consider using 2 x vector types if the back end handles them
2497 // correctly.
2498 if (RetTy->isAnyComplexType())
Chris Lattner800588f2010-07-29 06:26:06 +00002499 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattnera3c109b2010-07-29 02:16:43 +00002500 getContext().getTypeSize(RetTy)));
Daniel Dunbar4cc753f2010-02-01 23:31:19 +00002501
Daniel Dunbar98303b92009-09-13 08:03:58 +00002502 // Integer like structures are returned in r0.
Chris Lattnera3c109b2010-07-29 02:16:43 +00002503 if (isIntegerLikeType(RetTy, getContext(), getVMContext())) {
Daniel Dunbar98303b92009-09-13 08:03:58 +00002504 // Return in the smallest viable integer type.
Chris Lattnera3c109b2010-07-29 02:16:43 +00002505 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar98303b92009-09-13 08:03:58 +00002506 if (Size <= 8)
Chris Lattner800588f2010-07-29 06:26:06 +00002507 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar98303b92009-09-13 08:03:58 +00002508 if (Size <= 16)
Chris Lattner800588f2010-07-29 06:26:06 +00002509 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
2510 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar98303b92009-09-13 08:03:58 +00002511 }
2512
2513 // Otherwise return in memory.
2514 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002515 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00002516
2517 // Otherwise this is an AAPCS variant.
2518
Chris Lattnera3c109b2010-07-29 02:16:43 +00002519 if (isEmptyRecord(getContext(), RetTy, true))
Daniel Dunbar16a08082009-09-14 00:56:55 +00002520 return ABIArgInfo::getIgnore();
2521
Daniel Dunbar98303b92009-09-13 08:03:58 +00002522 // Aggregates <= 4 bytes are returned in r0; other aggregates
2523 // are returned indirectly.
Chris Lattnera3c109b2010-07-29 02:16:43 +00002524 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar16a08082009-09-14 00:56:55 +00002525 if (Size <= 32) {
2526 // Return in the smallest viable integer type.
2527 if (Size <= 8)
Chris Lattner800588f2010-07-29 06:26:06 +00002528 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar16a08082009-09-14 00:56:55 +00002529 if (Size <= 16)
Chris Lattner800588f2010-07-29 06:26:06 +00002530 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
2531 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar16a08082009-09-14 00:56:55 +00002532 }
2533
Daniel Dunbar98303b92009-09-13 08:03:58 +00002534 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002535}
2536
2537llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner77b89b82010-06-27 07:15:29 +00002538 CodeGenFunction &CGF) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002539 // FIXME: Need to handle alignment
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00002540 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Owen Anderson96e0fc72009-07-29 22:16:19 +00002541 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002542
2543 CGBuilderTy &Builder = CGF.Builder;
2544 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2545 "ap");
2546 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2547 llvm::Type *PTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00002548 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002549 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2550
2551 uint64_t Offset =
2552 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
2553 llvm::Value *NextAddr =
Chris Lattner77b89b82010-06-27 07:15:29 +00002554 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002555 "ap.next");
2556 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2557
2558 return AddrTyped;
2559}
2560
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002561//===----------------------------------------------------------------------===//
Justin Holewinski0259c3a2011-04-22 11:10:38 +00002562// PTX ABI Implementation
2563//===----------------------------------------------------------------------===//
2564
2565namespace {
2566
2567class PTXABIInfo : public ABIInfo {
2568public:
2569 PTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
2570
2571 ABIArgInfo classifyReturnType(QualType RetTy) const;
2572 ABIArgInfo classifyArgumentType(QualType Ty) const;
2573
2574 virtual void computeInfo(CGFunctionInfo &FI) const;
2575 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2576 CodeGenFunction &CFG) const;
2577};
2578
2579class PTXTargetCodeGenInfo : public TargetCodeGenInfo {
2580public:
2581 PTXTargetCodeGenInfo(CodeGenTypes &CGT)
2582 : TargetCodeGenInfo(new PTXABIInfo(CGT)) {}
2583};
2584
2585ABIArgInfo PTXABIInfo::classifyReturnType(QualType RetTy) const {
2586 if (RetTy->isVoidType())
2587 return ABIArgInfo::getIgnore();
2588 if (isAggregateTypeForABI(RetTy))
2589 return ABIArgInfo::getIndirect(0);
2590 return ABIArgInfo::getDirect();
2591}
2592
2593ABIArgInfo PTXABIInfo::classifyArgumentType(QualType Ty) const {
2594 if (isAggregateTypeForABI(Ty))
2595 return ABIArgInfo::getIndirect(0);
2596
2597 return ABIArgInfo::getDirect();
2598}
2599
2600void PTXABIInfo::computeInfo(CGFunctionInfo &FI) const {
2601 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
2602 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2603 it != ie; ++it)
2604 it->info = classifyArgumentType(it->type);
2605
2606 // Always honor user-specified calling convention.
2607 if (FI.getCallingConvention() != llvm::CallingConv::C)
2608 return;
2609
2610 // Calling convention as default by an ABI.
2611 llvm::CallingConv::ID DefaultCC;
2612 llvm::StringRef Env = getContext().Target.getTriple().getEnvironmentName();
2613 if (Env == "device")
2614 DefaultCC = llvm::CallingConv::PTX_Device;
2615 else
2616 DefaultCC = llvm::CallingConv::PTX_Kernel;
2617
2618 FI.setEffectiveCallingConvention(DefaultCC);
2619}
2620
2621llvm::Value *PTXABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2622 CodeGenFunction &CFG) const {
2623 llvm_unreachable("PTX does not support varargs");
2624 return 0;
2625}
2626
2627}
2628
2629//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002630// SystemZ ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002631//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002632
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002633namespace {
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002634
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002635class SystemZABIInfo : public ABIInfo {
Chris Lattnerea044322010-07-29 02:01:43 +00002636public:
2637 SystemZABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
2638
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002639 bool isPromotableIntegerType(QualType Ty) const;
2640
Chris Lattnera3c109b2010-07-29 02:16:43 +00002641 ABIArgInfo classifyReturnType(QualType RetTy) const;
2642 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002643
Chris Lattneree5dcd02010-07-29 02:31:05 +00002644 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +00002645 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002646 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2647 it != ie; ++it)
Chris Lattnera3c109b2010-07-29 02:16:43 +00002648 it->info = classifyArgumentType(it->type);
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002649 }
2650
2651 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2652 CodeGenFunction &CGF) const;
2653};
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002654
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002655class SystemZTargetCodeGenInfo : public TargetCodeGenInfo {
2656public:
Chris Lattnerea044322010-07-29 02:01:43 +00002657 SystemZTargetCodeGenInfo(CodeGenTypes &CGT)
2658 : TargetCodeGenInfo(new SystemZABIInfo(CGT)) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002659};
2660
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002661}
2662
2663bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const {
2664 // SystemZ ABI requires all 8, 16 and 32 bit quantities to be extended.
John McCall183700f2009-09-21 23:43:11 +00002665 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002666 switch (BT->getKind()) {
2667 case BuiltinType::Bool:
2668 case BuiltinType::Char_S:
2669 case BuiltinType::Char_U:
2670 case BuiltinType::SChar:
2671 case BuiltinType::UChar:
2672 case BuiltinType::Short:
2673 case BuiltinType::UShort:
2674 case BuiltinType::Int:
2675 case BuiltinType::UInt:
2676 return true;
2677 default:
2678 return false;
2679 }
2680 return false;
2681}
2682
2683llvm::Value *SystemZABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2684 CodeGenFunction &CGF) const {
2685 // FIXME: Implement
2686 return 0;
2687}
2688
2689
Chris Lattnera3c109b2010-07-29 02:16:43 +00002690ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const {
2691 if (RetTy->isVoidType())
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002692 return ABIArgInfo::getIgnore();
John McCalld608cdb2010-08-22 10:59:02 +00002693 if (isAggregateTypeForABI(RetTy))
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002694 return ABIArgInfo::getIndirect(0);
Chris Lattnera3c109b2010-07-29 02:16:43 +00002695
2696 return (isPromotableIntegerType(RetTy) ?
2697 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002698}
2699
Chris Lattnera3c109b2010-07-29 02:16:43 +00002700ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const {
John McCalld608cdb2010-08-22 10:59:02 +00002701 if (isAggregateTypeForABI(Ty))
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002702 return ABIArgInfo::getIndirect(0);
Chris Lattnera3c109b2010-07-29 02:16:43 +00002703
2704 return (isPromotableIntegerType(Ty) ?
2705 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002706}
2707
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002708//===----------------------------------------------------------------------===//
Wesley Peck276fdf42010-12-19 19:57:51 +00002709// MBlaze ABI Implementation
2710//===----------------------------------------------------------------------===//
2711
2712namespace {
2713
2714class MBlazeABIInfo : public ABIInfo {
2715public:
2716 MBlazeABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
2717
2718 bool isPromotableIntegerType(QualType Ty) const;
2719
2720 ABIArgInfo classifyReturnType(QualType RetTy) const;
2721 ABIArgInfo classifyArgumentType(QualType RetTy) const;
2722
2723 virtual void computeInfo(CGFunctionInfo &FI) const {
2724 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
2725 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2726 it != ie; ++it)
2727 it->info = classifyArgumentType(it->type);
2728 }
2729
2730 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2731 CodeGenFunction &CGF) const;
2732};
2733
2734class MBlazeTargetCodeGenInfo : public TargetCodeGenInfo {
2735public:
2736 MBlazeTargetCodeGenInfo(CodeGenTypes &CGT)
2737 : TargetCodeGenInfo(new MBlazeABIInfo(CGT)) {}
2738 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2739 CodeGen::CodeGenModule &M) const;
2740};
2741
2742}
2743
2744bool MBlazeABIInfo::isPromotableIntegerType(QualType Ty) const {
2745 // MBlaze ABI requires all 8 and 16 bit quantities to be extended.
2746 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
2747 switch (BT->getKind()) {
2748 case BuiltinType::Bool:
2749 case BuiltinType::Char_S:
2750 case BuiltinType::Char_U:
2751 case BuiltinType::SChar:
2752 case BuiltinType::UChar:
2753 case BuiltinType::Short:
2754 case BuiltinType::UShort:
2755 return true;
2756 default:
2757 return false;
2758 }
2759 return false;
2760}
2761
2762llvm::Value *MBlazeABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2763 CodeGenFunction &CGF) const {
2764 // FIXME: Implement
2765 return 0;
2766}
2767
2768
2769ABIArgInfo MBlazeABIInfo::classifyReturnType(QualType RetTy) const {
2770 if (RetTy->isVoidType())
2771 return ABIArgInfo::getIgnore();
2772 if (isAggregateTypeForABI(RetTy))
2773 return ABIArgInfo::getIndirect(0);
2774
2775 return (isPromotableIntegerType(RetTy) ?
2776 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2777}
2778
2779ABIArgInfo MBlazeABIInfo::classifyArgumentType(QualType Ty) const {
2780 if (isAggregateTypeForABI(Ty))
2781 return ABIArgInfo::getIndirect(0);
2782
2783 return (isPromotableIntegerType(Ty) ?
2784 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2785}
2786
2787void MBlazeTargetCodeGenInfo::SetTargetAttributes(const Decl *D,
2788 llvm::GlobalValue *GV,
2789 CodeGen::CodeGenModule &M)
2790 const {
2791 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
2792 if (!FD) return;
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +00002793
Wesley Peck276fdf42010-12-19 19:57:51 +00002794 llvm::CallingConv::ID CC = llvm::CallingConv::C;
2795 if (FD->hasAttr<MBlazeInterruptHandlerAttr>())
2796 CC = llvm::CallingConv::MBLAZE_INTR;
2797 else if (FD->hasAttr<MBlazeSaveVolatilesAttr>())
2798 CC = llvm::CallingConv::MBLAZE_SVOL;
2799
2800 if (CC != llvm::CallingConv::C) {
2801 // Handle 'interrupt_handler' attribute:
2802 llvm::Function *F = cast<llvm::Function>(GV);
2803
2804 // Step 1: Set ISR calling convention.
2805 F->setCallingConv(CC);
2806
2807 // Step 2: Add attributes goodness.
2808 F->addFnAttr(llvm::Attribute::NoInline);
2809 }
2810
2811 // Step 3: Emit _interrupt_handler alias.
2812 if (CC == llvm::CallingConv::MBLAZE_INTR)
2813 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
2814 "_interrupt_handler", GV, &M.getModule());
2815}
2816
2817
2818//===----------------------------------------------------------------------===//
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002819// MSP430 ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002820//===----------------------------------------------------------------------===//
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002821
2822namespace {
2823
2824class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
2825public:
Chris Lattnerea044322010-07-29 02:01:43 +00002826 MSP430TargetCodeGenInfo(CodeGenTypes &CGT)
2827 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002828 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2829 CodeGen::CodeGenModule &M) const;
2830};
2831
2832}
2833
2834void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
2835 llvm::GlobalValue *GV,
2836 CodeGen::CodeGenModule &M) const {
2837 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
2838 if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) {
2839 // Handle 'interrupt' attribute:
2840 llvm::Function *F = cast<llvm::Function>(GV);
2841
2842 // Step 1: Set ISR calling convention.
2843 F->setCallingConv(llvm::CallingConv::MSP430_INTR);
2844
2845 // Step 2: Add attributes goodness.
2846 F->addFnAttr(llvm::Attribute::NoInline);
2847
2848 // Step 3: Emit ISR vector alias.
2849 unsigned Num = attr->getNumber() + 0xffe0;
2850 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
Benjamin Kramer77d66052010-11-12 15:42:18 +00002851 "vector_" + llvm::Twine::utohexstr(Num),
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002852 GV, &M.getModule());
2853 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002854 }
2855}
2856
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002857//===----------------------------------------------------------------------===//
John McCallaeeb7012010-05-27 06:19:26 +00002858// MIPS ABI Implementation. This works for both little-endian and
2859// big-endian variants.
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002860//===----------------------------------------------------------------------===//
2861
John McCallaeeb7012010-05-27 06:19:26 +00002862namespace {
Akira Hatanaka619e8872011-06-02 00:09:17 +00002863class MipsABIInfo : public ABIInfo {
2864public:
2865 MipsABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
2866
2867 ABIArgInfo classifyReturnType(QualType RetTy) const;
2868 ABIArgInfo classifyArgumentType(QualType RetTy) const;
2869 virtual void computeInfo(CGFunctionInfo &FI) const;
2870 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2871 CodeGenFunction &CGF) const;
2872};
2873
John McCallaeeb7012010-05-27 06:19:26 +00002874class MIPSTargetCodeGenInfo : public TargetCodeGenInfo {
2875public:
Chris Lattnerea044322010-07-29 02:01:43 +00002876 MIPSTargetCodeGenInfo(CodeGenTypes &CGT)
Akira Hatanaka619e8872011-06-02 00:09:17 +00002877 : TargetCodeGenInfo(new MipsABIInfo(CGT)) {}
John McCallaeeb7012010-05-27 06:19:26 +00002878
2879 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
2880 return 29;
2881 }
2882
2883 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002884 llvm::Value *Address) const;
John McCallaeeb7012010-05-27 06:19:26 +00002885};
2886}
2887
Akira Hatanaka619e8872011-06-02 00:09:17 +00002888ABIArgInfo MipsABIInfo::classifyArgumentType(QualType Ty) const {
2889 if (isAggregateTypeForABI(Ty)) {
2890 // Ignore empty aggregates.
2891 if (getContext().getTypeSize(Ty) == 0)
2892 return ABIArgInfo::getIgnore();
2893
2894 return ABIArgInfo::getIndirect(0);
2895 }
2896
2897 // Treat an enum type as its underlying type.
2898 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2899 Ty = EnumTy->getDecl()->getIntegerType();
2900
2901 return (Ty->isPromotableIntegerType() ?
2902 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2903}
2904
2905ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const {
2906 if (RetTy->isVoidType())
2907 return ABIArgInfo::getIgnore();
2908
2909 if (isAggregateTypeForABI(RetTy)) {
2910 if (RetTy->isAnyComplexType())
2911 return ABIArgInfo::getDirect();
2912
2913 return ABIArgInfo::getIndirect(0);
2914 }
2915
2916 // Treat an enum type as its underlying type.
2917 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2918 RetTy = EnumTy->getDecl()->getIntegerType();
2919
2920 return (RetTy->isPromotableIntegerType() ?
2921 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2922}
2923
2924void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const {
2925 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
2926 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2927 it != ie; ++it)
2928 it->info = classifyArgumentType(it->type);
2929}
2930
2931llvm::Value* MipsABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2932 CodeGenFunction &CGF) const {
2933 return 0;
2934}
2935
John McCallaeeb7012010-05-27 06:19:26 +00002936bool
2937MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2938 llvm::Value *Address) const {
2939 // This information comes from gcc's implementation, which seems to
2940 // as canonical as it gets.
2941
2942 CodeGen::CGBuilderTy &Builder = CGF.Builder;
2943 llvm::LLVMContext &Context = CGF.getLLVMContext();
2944
2945 // Everything on MIPS is 4 bytes. Double-precision FP registers
2946 // are aliased to pairs of single-precision FP registers.
2947 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
2948 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2949
2950 // 0-31 are the general purpose registers, $0 - $31.
2951 // 32-63 are the floating-point registers, $f0 - $f31.
2952 // 64 and 65 are the multiply/divide registers, $hi and $lo.
2953 // 66 is the (notional, I think) register for signal-handler return.
2954 AssignToArrayRange(Builder, Address, Four8, 0, 65);
2955
2956 // 67-74 are the floating-point status registers, $fcc0 - $fcc7.
2957 // They are one bit wide and ignored here.
2958
2959 // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31.
2960 // (coprocessor 1 is the FP unit)
2961 // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31.
2962 // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31.
2963 // 176-181 are the DSP accumulator registers.
2964 AssignToArrayRange(Builder, Address, Four8, 80, 181);
2965
2966 return false;
2967}
2968
2969
Chris Lattnerea044322010-07-29 02:01:43 +00002970const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() {
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002971 if (TheTargetCodeGenInfo)
2972 return *TheTargetCodeGenInfo;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002973
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002974 // For now we just cache the TargetCodeGenInfo in CodeGenModule and don't
2975 // free it.
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002976
Chris Lattner9c254f02010-06-29 06:01:59 +00002977 const llvm::Triple &Triple = getContext().Target.getTriple();
Daniel Dunbar1752ee42009-08-24 09:10:05 +00002978 switch (Triple.getArch()) {
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002979 default:
Chris Lattnerea044322010-07-29 02:01:43 +00002980 return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002981
John McCallaeeb7012010-05-27 06:19:26 +00002982 case llvm::Triple::mips:
2983 case llvm::Triple::mipsel:
Chris Lattnerea044322010-07-29 02:01:43 +00002984 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types));
John McCallaeeb7012010-05-27 06:19:26 +00002985
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002986 case llvm::Triple::arm:
2987 case llvm::Triple::thumb:
Sandeep Patel34c1af82011-04-05 00:23:47 +00002988 {
2989 ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS;
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002990
Sandeep Patel34c1af82011-04-05 00:23:47 +00002991 if (strcmp(getContext().Target.getABI(), "apcs-gnu") == 0)
2992 Kind = ARMABIInfo::APCS;
2993 else if (CodeGenOpts.FloatABI == "hard")
2994 Kind = ARMABIInfo::AAPCS_VFP;
2995
2996 return *(TheTargetCodeGenInfo = new ARMTargetCodeGenInfo(Types, Kind));
2997 }
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002998
John McCallec853ba2010-03-11 00:10:12 +00002999 case llvm::Triple::ppc:
Chris Lattnerea044322010-07-29 02:01:43 +00003000 return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo(Types));
John McCallec853ba2010-03-11 00:10:12 +00003001
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003002 case llvm::Triple::ptx32:
3003 case llvm::Triple::ptx64:
3004 return *(TheTargetCodeGenInfo = new PTXTargetCodeGenInfo(Types));
3005
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00003006 case llvm::Triple::systemz:
Chris Lattnerea044322010-07-29 02:01:43 +00003007 return *(TheTargetCodeGenInfo = new SystemZTargetCodeGenInfo(Types));
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003008
Wesley Peck276fdf42010-12-19 19:57:51 +00003009 case llvm::Triple::mblaze:
3010 return *(TheTargetCodeGenInfo = new MBlazeTargetCodeGenInfo(Types));
3011
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003012 case llvm::Triple::msp430:
Chris Lattnerea044322010-07-29 02:01:43 +00003013 return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types));
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00003014
Daniel Dunbar1752ee42009-08-24 09:10:05 +00003015 case llvm::Triple::x86:
Daniel Dunbardb57a4c2011-04-19 21:43:27 +00003016 if (Triple.isOSDarwin())
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003017 return *(TheTargetCodeGenInfo =
Chris Lattnerea044322010-07-29 02:01:43 +00003018 new X86_32TargetCodeGenInfo(Types, true, true));
Daniel Dunbardb57a4c2011-04-19 21:43:27 +00003019
3020 switch (Triple.getOS()) {
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003021 case llvm::Triple::Cygwin:
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003022 case llvm::Triple::MinGW32:
Edward O'Callaghan727e2682009-10-21 11:58:24 +00003023 case llvm::Triple::AuroraUX:
3024 case llvm::Triple::DragonFly:
David Chisnall75c135a2009-09-03 01:48:05 +00003025 case llvm::Triple::FreeBSD:
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003026 case llvm::Triple::OpenBSD:
Benjamin Kramer8e50a962011-02-02 18:59:27 +00003027 case llvm::Triple::NetBSD:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003028 return *(TheTargetCodeGenInfo =
Chris Lattnerea044322010-07-29 02:01:43 +00003029 new X86_32TargetCodeGenInfo(Types, false, true));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003030
3031 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003032 return *(TheTargetCodeGenInfo =
Chris Lattnerea044322010-07-29 02:01:43 +00003033 new X86_32TargetCodeGenInfo(Types, false, false));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003034 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003035
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003036 case llvm::Triple::x86_64:
Chris Lattnerf13721d2010-08-31 16:44:54 +00003037 switch (Triple.getOS()) {
3038 case llvm::Triple::Win32:
NAKAMURA Takumi0aa20572011-02-17 08:51:38 +00003039 case llvm::Triple::MinGW32:
Chris Lattnerf13721d2010-08-31 16:44:54 +00003040 case llvm::Triple::Cygwin:
3041 return *(TheTargetCodeGenInfo = new WinX86_64TargetCodeGenInfo(Types));
3042 default:
3043 return *(TheTargetCodeGenInfo = new X86_64TargetCodeGenInfo(Types));
3044 }
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003045 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003046}