blob: 2bc61753a031369716dc071f4e01b101c962e28a [file] [log] [blame]
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001//===---- TargetABIInfo.cpp - Encapsulate target ABI details ----*- C++ -*-===//
2//
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
15#include "ABIInfo.h"
16#include "CodeGenFunction.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000017#include "clang/AST/RecordLayout.h"
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000018#include "llvm/Type.h"
Daniel Dunbar2c0843f2009-08-24 08:52:16 +000019#include "llvm/ADT/Triple.h"
Torok Edwinf42e4a62009-08-24 13:25:12 +000020#include <cstdio>
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000021
22using namespace clang;
23using namespace CodeGen;
24
25ABIInfo::~ABIInfo() {}
26
27void ABIArgInfo::dump() const {
28 fprintf(stderr, "(ABIArgInfo Kind=");
29 switch (TheKind) {
30 case Direct:
31 fprintf(stderr, "Direct");
32 break;
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +000033 case Extend:
34 fprintf(stderr, "Extend");
35 break;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000036 case Ignore:
37 fprintf(stderr, "Ignore");
38 break;
39 case Coerce:
40 fprintf(stderr, "Coerce Type=");
41 getCoerceToType()->print(llvm::errs());
42 break;
43 case Indirect:
44 fprintf(stderr, "Indirect Align=%d", getIndirectAlign());
45 break;
46 case Expand:
47 fprintf(stderr, "Expand");
48 break;
49 }
50 fprintf(stderr, ")\n");
51}
52
Daniel Dunbar98303b92009-09-13 08:03:58 +000053static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000054
55/// isEmptyField - Return true iff a the field is "empty", that is it
56/// is an unnamed bit-field or an (array of) empty record(s).
Daniel Dunbar98303b92009-09-13 08:03:58 +000057static bool isEmptyField(ASTContext &Context, const FieldDecl *FD,
58 bool AllowArrays) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000059 if (FD->isUnnamedBitfield())
60 return true;
61
62 QualType FT = FD->getType();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000063
Daniel Dunbar98303b92009-09-13 08:03:58 +000064 // Constant arrays of empty records count as empty, strip them off.
65 if (AllowArrays)
66 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT))
67 FT = AT->getElementType();
68
69 return isEmptyRecord(Context, FT, AllowArrays);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000070}
71
72/// isEmptyRecord - Return true iff a structure contains only empty
73/// fields. Note that a structure with a flexible array member is not
74/// considered empty.
Daniel Dunbar98303b92009-09-13 08:03:58 +000075static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) {
Ted Kremenek6217b802009-07-29 21:53:49 +000076 const RecordType *RT = T->getAs<RecordType>();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000077 if (!RT)
78 return 0;
79 const RecordDecl *RD = RT->getDecl();
80 if (RD->hasFlexibleArrayMember())
81 return false;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +000082 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
83 i != e; ++i)
Daniel Dunbar98303b92009-09-13 08:03:58 +000084 if (!isEmptyField(Context, *i, AllowArrays))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000085 return false;
86 return true;
87}
88
Anders Carlsson0a8f8472009-09-16 15:53:40 +000089/// hasNonTrivialDestructorOrCopyConstructor - Determine if a type has either
90/// a non-trivial destructor or a non-trivial copy constructor.
91static bool hasNonTrivialDestructorOrCopyConstructor(const RecordType *RT) {
92 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
93 if (!RD)
94 return false;
95
96 return !RD->hasTrivialDestructor() || !RD->hasTrivialCopyConstructor();
97}
98
99/// isRecordWithNonTrivialDestructorOrCopyConstructor - Determine if a type is
100/// a record type with either a non-trivial destructor or a non-trivial copy
101/// constructor.
102static bool isRecordWithNonTrivialDestructorOrCopyConstructor(QualType T) {
103 const RecordType *RT = T->getAs<RecordType>();
104 if (!RT)
105 return false;
106
107 return hasNonTrivialDestructorOrCopyConstructor(RT);
108}
109
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000110/// isSingleElementStruct - Determine if a structure is a "single
111/// element struct", i.e. it has exactly one non-empty field or
112/// exactly one field which is itself a single element
113/// struct. Structures with flexible array members are never
114/// considered single element structs.
115///
116/// \return The field declaration for the single non-empty field, if
117/// it exists.
118static const Type *isSingleElementStruct(QualType T, ASTContext &Context) {
119 const RecordType *RT = T->getAsStructureType();
120 if (!RT)
121 return 0;
122
123 const RecordDecl *RD = RT->getDecl();
124 if (RD->hasFlexibleArrayMember())
125 return 0;
126
127 const Type *Found = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000128 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
129 i != e; ++i) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000130 const FieldDecl *FD = *i;
131 QualType FT = FD->getType();
132
133 // Ignore empty fields.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000134 if (isEmptyField(Context, FD, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000135 continue;
136
137 // If we already found an element then this isn't a single-element
138 // struct.
139 if (Found)
140 return 0;
141
142 // Treat single element arrays as the element.
143 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
144 if (AT->getSize().getZExtValue() != 1)
145 break;
146 FT = AT->getElementType();
147 }
148
149 if (!CodeGenFunction::hasAggregateLLVMType(FT)) {
150 Found = FT.getTypePtr();
151 } else {
152 Found = isSingleElementStruct(FT, Context);
153 if (!Found)
154 return 0;
155 }
156 }
157
158 return Found;
159}
160
161static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
Daniel Dunbar55e59e12009-09-24 05:12:36 +0000162 if (!Ty->getAs<BuiltinType>() && !Ty->isAnyPointerType() &&
163 !Ty->isAnyComplexType() && !Ty->isEnumeralType() &&
164 !Ty->isBlockPointerType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000165 return false;
166
167 uint64_t Size = Context.getTypeSize(Ty);
168 return Size == 32 || Size == 64;
169}
170
Daniel Dunbar53012f42009-11-09 01:33:53 +0000171/// canExpandIndirectArgument - Test whether an argument type which is to be
172/// passed indirectly (on the stack) would have the equivalent layout if it was
173/// expanded into separate arguments. If so, we prefer to do the latter to avoid
174/// inhibiting optimizations.
175///
176// FIXME: This predicate is missing many cases, currently it just follows
177// llvm-gcc (checks that all fields are 32-bit or 64-bit primitive types). We
178// should probably make this smarter, or better yet make the LLVM backend
179// capable of handling it.
180static bool canExpandIndirectArgument(QualType Ty, ASTContext &Context) {
181 // We can only expand structure types.
182 const RecordType *RT = Ty->getAs<RecordType>();
183 if (!RT)
184 return false;
185
186 // We can only expand (C) structures.
187 //
188 // FIXME: This needs to be generalized to handle classes as well.
189 const RecordDecl *RD = RT->getDecl();
190 if (!RD->isStruct() || isa<CXXRecordDecl>(RD))
191 return false;
192
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000193 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
194 i != e; ++i) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000195 const FieldDecl *FD = *i;
196
197 if (!is32Or64BitBasicType(FD->getType(), Context))
198 return false;
199
200 // FIXME: Reject bit-fields wholesale; there are two problems, we don't know
201 // how to expand them yet, and the predicate for telling if a bitfield still
202 // counts as "basic" is more complicated than what we were doing previously.
203 if (FD->isBitField())
204 return false;
205 }
206
207 return true;
208}
209
Eli Friedmana1e6de92009-06-13 21:37:10 +0000210static bool typeContainsSSEVector(const RecordDecl *RD, ASTContext &Context) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000211 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
212 i != e; ++i) {
Eli Friedmana1e6de92009-06-13 21:37:10 +0000213 const FieldDecl *FD = *i;
214
215 if (FD->getType()->isVectorType() &&
216 Context.getTypeSize(FD->getType()) >= 128)
217 return true;
218
Ted Kremenek6217b802009-07-29 21:53:49 +0000219 if (const RecordType* RT = FD->getType()->getAs<RecordType>())
Eli Friedmana1e6de92009-06-13 21:37:10 +0000220 if (typeContainsSSEVector(RT->getDecl(), Context))
221 return true;
222 }
223
224 return false;
225}
226
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000227namespace {
228/// DefaultABIInfo - The default implementation for ABI specific
229/// details. This implementation provides information which results in
230/// self-consistent and sensible LLVM IR generation, but does not
231/// conform to any particular ABI.
232class DefaultABIInfo : public ABIInfo {
233 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000234 ASTContext &Context,
235 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000236
237 ABIArgInfo classifyArgumentType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000238 ASTContext &Context,
239 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000240
Owen Andersona1cf15f2009-07-14 23:10:40 +0000241 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
242 llvm::LLVMContext &VMContext) const {
243 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context,
244 VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000245 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
246 it != ie; ++it)
Owen Andersona1cf15f2009-07-14 23:10:40 +0000247 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000248 }
249
250 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
251 CodeGenFunction &CGF) const;
252};
253
254/// X86_32ABIInfo - The X86-32 ABI information.
255class X86_32ABIInfo : public ABIInfo {
256 ASTContext &Context;
David Chisnall1e4249c2009-08-17 23:08:21 +0000257 bool IsDarwinVectorABI;
258 bool IsSmallStructInRegABI;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000259
260 static bool isRegisterSize(unsigned Size) {
261 return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
262 }
263
264 static bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context);
265
Eli Friedmana1e6de92009-06-13 21:37:10 +0000266 static unsigned getIndirectArgumentAlignment(QualType Ty,
267 ASTContext &Context);
268
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000269public:
270 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000271 ASTContext &Context,
272 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000273
274 ABIArgInfo classifyArgumentType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000275 ASTContext &Context,
276 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000277
Owen Andersona1cf15f2009-07-14 23:10:40 +0000278 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
279 llvm::LLVMContext &VMContext) const {
280 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context,
281 VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000282 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
283 it != ie; ++it)
Owen Andersona1cf15f2009-07-14 23:10:40 +0000284 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000285 }
286
287 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
288 CodeGenFunction &CGF) const;
289
David Chisnall1e4249c2009-08-17 23:08:21 +0000290 X86_32ABIInfo(ASTContext &Context, bool d, bool p)
Mike Stump1eb44332009-09-09 15:08:12 +0000291 : ABIInfo(), Context(Context), IsDarwinVectorABI(d),
David Chisnall1e4249c2009-08-17 23:08:21 +0000292 IsSmallStructInRegABI(p) {}
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000293};
294}
295
296
297/// shouldReturnTypeInRegister - Determine if the given type should be
298/// passed in a register (for the Darwin ABI).
299bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
300 ASTContext &Context) {
301 uint64_t Size = Context.getTypeSize(Ty);
302
303 // Type must be register sized.
304 if (!isRegisterSize(Size))
305 return false;
306
307 if (Ty->isVectorType()) {
308 // 64- and 128- bit vectors inside structures are not returned in
309 // registers.
310 if (Size == 64 || Size == 128)
311 return false;
312
313 return true;
314 }
315
Daniel Dunbar55e59e12009-09-24 05:12:36 +0000316 // If this is a builtin, pointer, enum, or complex type, it is ok.
317 if (Ty->getAs<BuiltinType>() || Ty->isAnyPointerType() ||
318 Ty->isAnyComplexType() || Ty->isEnumeralType() ||
319 Ty->isBlockPointerType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000320 return true;
321
322 // Arrays are treated like records.
323 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
324 return shouldReturnTypeInRegister(AT->getElementType(), Context);
325
326 // Otherwise, it must be a record type.
Ted Kremenek6217b802009-07-29 21:53:49 +0000327 const RecordType *RT = Ty->getAs<RecordType>();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000328 if (!RT) return false;
329
330 // Structure types are passed in register if all fields would be
331 // passed in a register.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000332 for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(),
333 e = RT->getDecl()->field_end(); i != e; ++i) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000334 const FieldDecl *FD = *i;
335
336 // Empty fields are ignored.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000337 if (isEmptyField(Context, FD, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000338 continue;
339
340 // Check fields recursively.
341 if (!shouldReturnTypeInRegister(FD->getType(), Context))
342 return false;
343 }
344
345 return true;
346}
347
348ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000349 ASTContext &Context,
350 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000351 if (RetTy->isVoidType()) {
352 return ABIArgInfo::getIgnore();
John McCall183700f2009-09-21 23:43:11 +0000353 } else if (const VectorType *VT = RetTy->getAs<VectorType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000354 // On Darwin, some vectors are returned in registers.
David Chisnall1e4249c2009-08-17 23:08:21 +0000355 if (IsDarwinVectorABI) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000356 uint64_t Size = Context.getTypeSize(RetTy);
357
358 // 128-bit vectors are a special case; they are returned in
359 // registers and we need to make sure to pick a type the LLVM
360 // backend will like.
361 if (Size == 128)
Owen Anderson0032b272009-08-13 21:57:51 +0000362 return ABIArgInfo::getCoerce(llvm::VectorType::get(
363 llvm::Type::getInt64Ty(VMContext), 2));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000364
365 // Always return in register if it fits in a general purpose
366 // register, or if it is 64 bits and has a single element.
367 if ((Size == 8 || Size == 16 || Size == 32) ||
368 (Size == 64 && VT->getNumElements() == 1))
Owen Anderson0032b272009-08-13 21:57:51 +0000369 return ABIArgInfo::getCoerce(llvm::IntegerType::get(VMContext, Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000370
371 return ABIArgInfo::getIndirect(0);
372 }
373
374 return ABIArgInfo::getDirect();
375 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Anders Carlsson40092972009-10-20 22:07:59 +0000376 if (const RecordType *RT = RetTy->getAsStructureType()) {
377 // Structures with either a non-trivial destructor or a non-trivial
378 // copy constructor are always indirect.
379 if (hasNonTrivialDestructorOrCopyConstructor(RT))
380 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
381
382 // Structures with flexible arrays are always indirect.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000383 if (RT->getDecl()->hasFlexibleArrayMember())
384 return ABIArgInfo::getIndirect(0);
Anders Carlsson40092972009-10-20 22:07:59 +0000385 }
386
David Chisnall1e4249c2009-08-17 23:08:21 +0000387 // If specified, structs and unions are always indirect.
388 if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000389 return ABIArgInfo::getIndirect(0);
390
391 // Classify "single element" structs as their element type.
392 if (const Type *SeltTy = isSingleElementStruct(RetTy, Context)) {
John McCall183700f2009-09-21 23:43:11 +0000393 if (const BuiltinType *BT = SeltTy->getAs<BuiltinType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000394 if (BT->isIntegerType()) {
395 // We need to use the size of the structure, padding
396 // bit-fields can adjust that to be larger than the single
397 // element type.
398 uint64_t Size = Context.getTypeSize(RetTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000399 return ABIArgInfo::getCoerce(
Owen Anderson0032b272009-08-13 21:57:51 +0000400 llvm::IntegerType::get(VMContext, (unsigned) Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000401 } else if (BT->getKind() == BuiltinType::Float) {
402 assert(Context.getTypeSize(RetTy) == Context.getTypeSize(SeltTy) &&
403 "Unexpect single element structure size!");
Owen Anderson0032b272009-08-13 21:57:51 +0000404 return ABIArgInfo::getCoerce(llvm::Type::getFloatTy(VMContext));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000405 } else if (BT->getKind() == BuiltinType::Double) {
406 assert(Context.getTypeSize(RetTy) == Context.getTypeSize(SeltTy) &&
407 "Unexpect single element structure size!");
Owen Anderson0032b272009-08-13 21:57:51 +0000408 return ABIArgInfo::getCoerce(llvm::Type::getDoubleTy(VMContext));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000409 }
410 } else if (SeltTy->isPointerType()) {
411 // FIXME: It would be really nice if this could come out as the proper
412 // pointer type.
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +0000413 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000414 return ABIArgInfo::getCoerce(PtrTy);
415 } else if (SeltTy->isVectorType()) {
416 // 64- and 128-bit vectors are never returned in a
417 // register when inside a structure.
418 uint64_t Size = Context.getTypeSize(RetTy);
419 if (Size == 64 || Size == 128)
420 return ABIArgInfo::getIndirect(0);
421
Owen Andersona1cf15f2009-07-14 23:10:40 +0000422 return classifyReturnType(QualType(SeltTy, 0), Context, VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000423 }
424 }
425
426 // Small structures which are register sized are generally returned
427 // in a register.
428 if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, Context)) {
429 uint64_t Size = Context.getTypeSize(RetTy);
Owen Anderson0032b272009-08-13 21:57:51 +0000430 return ABIArgInfo::getCoerce(llvm::IntegerType::get(VMContext, Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000431 }
432
433 return ABIArgInfo::getIndirect(0);
434 } else {
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000435 return (RetTy->isPromotableIntegerType() ?
436 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000437 }
438}
439
Eli Friedmana1e6de92009-06-13 21:37:10 +0000440unsigned X86_32ABIInfo::getIndirectArgumentAlignment(QualType Ty,
441 ASTContext &Context) {
442 unsigned Align = Context.getTypeAlign(Ty);
443 if (Align < 128) return 0;
Ted Kremenek6217b802009-07-29 21:53:49 +0000444 if (const RecordType* RT = Ty->getAs<RecordType>())
Eli Friedmana1e6de92009-06-13 21:37:10 +0000445 if (typeContainsSSEVector(RT->getDecl(), Context))
446 return 16;
447 return 0;
448}
449
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000450ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000451 ASTContext &Context,
452 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000453 // FIXME: Set alignment on indirect arguments.
454 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
455 // Structures with flexible arrays are always indirect.
456 if (const RecordType *RT = Ty->getAsStructureType())
457 if (RT->getDecl()->hasFlexibleArrayMember())
Mike Stump1eb44332009-09-09 15:08:12 +0000458 return ABIArgInfo::getIndirect(getIndirectArgumentAlignment(Ty,
Eli Friedmana1e6de92009-06-13 21:37:10 +0000459 Context));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000460
461 // Ignore empty structs.
Eli Friedmana1e6de92009-06-13 21:37:10 +0000462 if (Ty->isStructureType() && Context.getTypeSize(Ty) == 0)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000463 return ABIArgInfo::getIgnore();
464
Daniel Dunbar53012f42009-11-09 01:33:53 +0000465 // Expand small (<= 128-bit) record types when we know that the stack layout
466 // of those arguments will match the struct. This is important because the
467 // LLVM backend isn't smart enough to remove byval, which inhibits many
468 // optimizations.
469 if (Context.getTypeSize(Ty) <= 4*32 &&
470 canExpandIndirectArgument(Ty, Context))
471 return ABIArgInfo::getExpand();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000472
Eli Friedmana1e6de92009-06-13 21:37:10 +0000473 return ABIArgInfo::getIndirect(getIndirectArgumentAlignment(Ty, Context));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000474 } else {
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000475 return (Ty->isPromotableIntegerType() ?
476 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000477 }
478}
479
480llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
481 CodeGenFunction &CGF) const {
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +0000482 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Owen Anderson96e0fc72009-07-29 22:16:19 +0000483 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000484
485 CGBuilderTy &Builder = CGF.Builder;
486 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
487 "ap");
488 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
489 llvm::Type *PTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000490 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000491 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
492
493 uint64_t Offset =
494 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
495 llvm::Value *NextAddr =
Owen Anderson0032b272009-08-13 21:57:51 +0000496 Builder.CreateGEP(Addr, llvm::ConstantInt::get(
497 llvm::Type::getInt32Ty(CGF.getLLVMContext()), Offset),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000498 "ap.next");
499 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
500
501 return AddrTyped;
502}
503
504namespace {
505/// X86_64ABIInfo - The X86_64 ABI information.
506class X86_64ABIInfo : public ABIInfo {
507 enum Class {
508 Integer = 0,
509 SSE,
510 SSEUp,
511 X87,
512 X87Up,
513 ComplexX87,
514 NoClass,
515 Memory
516 };
517
518 /// merge - Implement the X86_64 ABI merging algorithm.
519 ///
520 /// Merge an accumulating classification \arg Accum with a field
521 /// classification \arg Field.
522 ///
523 /// \param Accum - The accumulating classification. This should
524 /// always be either NoClass or the result of a previous merge
525 /// call. In addition, this should never be Memory (the caller
526 /// should just return Memory for the aggregate).
527 Class merge(Class Accum, Class Field) const;
528
529 /// classify - Determine the x86_64 register classes in which the
530 /// given type T should be passed.
531 ///
532 /// \param Lo - The classification for the parts of the type
533 /// residing in the low word of the containing object.
534 ///
535 /// \param Hi - The classification for the parts of the type
536 /// residing in the high word of the containing object.
537 ///
538 /// \param OffsetBase - The bit offset of this type in the
539 /// containing object. Some parameters are classified different
540 /// depending on whether they straddle an eightbyte boundary.
541 ///
542 /// If a word is unused its result will be NoClass; if a type should
543 /// be passed in Memory then at least the classification of \arg Lo
544 /// will be Memory.
545 ///
546 /// The \arg Lo class will be NoClass iff the argument is ignored.
547 ///
548 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
549 /// also be ComplexX87.
550 void classify(QualType T, ASTContext &Context, uint64_t OffsetBase,
551 Class &Lo, Class &Hi) const;
552
553 /// getCoerceResult - Given a source type \arg Ty and an LLVM type
554 /// to coerce to, chose the best way to pass Ty in the same place
555 /// that \arg CoerceTo would be passed, but while keeping the
556 /// emitted code as simple as possible.
557 ///
558 /// FIXME: Note, this should be cleaned up to just take an enumeration of all
559 /// the ways we might want to pass things, instead of constructing an LLVM
560 /// type. This makes this code more explicit, and it makes it clearer that we
561 /// are also doing this for correctness in the case of passing scalar types.
562 ABIArgInfo getCoerceResult(QualType Ty,
563 const llvm::Type *CoerceTo,
564 ASTContext &Context) const;
565
566 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
567 /// such that the argument will be passed in memory.
568 ABIArgInfo getIndirectResult(QualType Ty,
569 ASTContext &Context) const;
570
571 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000572 ASTContext &Context,
573 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000574
575 ABIArgInfo classifyArgumentType(QualType Ty,
576 ASTContext &Context,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000577 llvm::LLVMContext &VMContext,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000578 unsigned &neededInt,
579 unsigned &neededSSE) const;
580
581public:
Owen Andersona1cf15f2009-07-14 23:10:40 +0000582 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
583 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000584
585 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
586 CodeGenFunction &CGF) const;
587};
588}
589
590X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum,
591 Class Field) const {
592 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
593 // classified recursively so that always two fields are
594 // considered. The resulting class is calculated according to
595 // the classes of the fields in the eightbyte:
596 //
597 // (a) If both classes are equal, this is the resulting class.
598 //
599 // (b) If one of the classes is NO_CLASS, the resulting class is
600 // the other class.
601 //
602 // (c) If one of the classes is MEMORY, the result is the MEMORY
603 // class.
604 //
605 // (d) If one of the classes is INTEGER, the result is the
606 // INTEGER.
607 //
608 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
609 // MEMORY is used as class.
610 //
611 // (f) Otherwise class SSE is used.
612
613 // Accum should never be memory (we should have returned) or
614 // ComplexX87 (because this cannot be passed in a structure).
615 assert((Accum != Memory && Accum != ComplexX87) &&
616 "Invalid accumulated classification during merge.");
617 if (Accum == Field || Field == NoClass)
618 return Accum;
619 else if (Field == Memory)
620 return Memory;
621 else if (Accum == NoClass)
622 return Field;
623 else if (Accum == Integer || Field == Integer)
624 return Integer;
625 else if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
626 Accum == X87 || Accum == X87Up)
627 return Memory;
628 else
629 return SSE;
630}
631
632void X86_64ABIInfo::classify(QualType Ty,
633 ASTContext &Context,
634 uint64_t OffsetBase,
635 Class &Lo, Class &Hi) const {
636 // FIXME: This code can be simplified by introducing a simple value class for
637 // Class pairs with appropriate constructor methods for the various
638 // situations.
639
640 // FIXME: Some of the split computations are wrong; unaligned vectors
641 // shouldn't be passed in registers for example, so there is no chance they
642 // can straddle an eightbyte. Verify & simplify.
643
644 Lo = Hi = NoClass;
645
646 Class &Current = OffsetBase < 64 ? Lo : Hi;
647 Current = Memory;
648
John McCall183700f2009-09-21 23:43:11 +0000649 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000650 BuiltinType::Kind k = BT->getKind();
651
652 if (k == BuiltinType::Void) {
653 Current = NoClass;
654 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
655 Lo = Integer;
656 Hi = Integer;
657 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
658 Current = Integer;
659 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
660 Current = SSE;
661 } else if (k == BuiltinType::LongDouble) {
662 Lo = X87;
663 Hi = X87Up;
664 }
665 // FIXME: _Decimal32 and _Decimal64 are SSE.
666 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
John McCall183700f2009-09-21 23:43:11 +0000667 } else if (const EnumType *ET = Ty->getAs<EnumType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000668 // Classify the underlying integer type.
669 classify(ET->getDecl()->getIntegerType(), Context, OffsetBase, Lo, Hi);
670 } else if (Ty->hasPointerRepresentation()) {
671 Current = Integer;
John McCall183700f2009-09-21 23:43:11 +0000672 } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000673 uint64_t Size = Context.getTypeSize(VT);
674 if (Size == 32) {
675 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
676 // float> as integer.
677 Current = Integer;
678
679 // If this type crosses an eightbyte boundary, it should be
680 // split.
681 uint64_t EB_Real = (OffsetBase) / 64;
682 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
683 if (EB_Real != EB_Imag)
684 Hi = Lo;
685 } else if (Size == 64) {
686 // gcc passes <1 x double> in memory. :(
687 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
688 return;
689
690 // gcc passes <1 x long long> as INTEGER.
691 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong))
692 Current = Integer;
693 else
694 Current = SSE;
695
696 // If this type crosses an eightbyte boundary, it should be
697 // split.
698 if (OffsetBase && OffsetBase != 64)
699 Hi = Lo;
700 } else if (Size == 128) {
701 Lo = SSE;
702 Hi = SSEUp;
703 }
John McCall183700f2009-09-21 23:43:11 +0000704 } else if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000705 QualType ET = Context.getCanonicalType(CT->getElementType());
706
707 uint64_t Size = Context.getTypeSize(Ty);
708 if (ET->isIntegralType()) {
709 if (Size <= 64)
710 Current = Integer;
711 else if (Size <= 128)
712 Lo = Hi = Integer;
713 } else if (ET == Context.FloatTy)
714 Current = SSE;
715 else if (ET == Context.DoubleTy)
716 Lo = Hi = SSE;
717 else if (ET == Context.LongDoubleTy)
718 Current = ComplexX87;
719
720 // If this complex type crosses an eightbyte boundary then it
721 // should be split.
722 uint64_t EB_Real = (OffsetBase) / 64;
723 uint64_t EB_Imag = (OffsetBase + Context.getTypeSize(ET)) / 64;
724 if (Hi == NoClass && EB_Real != EB_Imag)
725 Hi = Lo;
726 } else if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
727 // Arrays are treated like structures.
728
729 uint64_t Size = Context.getTypeSize(Ty);
730
731 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
732 // than two eightbytes, ..., it has class MEMORY.
733 if (Size > 128)
734 return;
735
736 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
737 // fields, it has class MEMORY.
738 //
739 // Only need to check alignment of array base.
740 if (OffsetBase % Context.getTypeAlign(AT->getElementType()))
741 return;
742
743 // Otherwise implement simplified merge. We could be smarter about
744 // this, but it isn't worth it and would be harder to verify.
745 Current = NoClass;
746 uint64_t EltSize = Context.getTypeSize(AT->getElementType());
747 uint64_t ArraySize = AT->getSize().getZExtValue();
748 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
749 Class FieldLo, FieldHi;
750 classify(AT->getElementType(), Context, Offset, FieldLo, FieldHi);
751 Lo = merge(Lo, FieldLo);
752 Hi = merge(Hi, FieldHi);
753 if (Lo == Memory || Hi == Memory)
754 break;
755 }
756
757 // Do post merger cleanup (see below). Only case we worry about is Memory.
758 if (Hi == Memory)
759 Lo = Memory;
760 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Ted Kremenek6217b802009-07-29 21:53:49 +0000761 } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000762 uint64_t Size = Context.getTypeSize(Ty);
763
764 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
765 // than two eightbytes, ..., it has class MEMORY.
766 if (Size > 128)
767 return;
768
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000769 // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
770 // copy constructor or a non-trivial destructor, it is passed by invisible
771 // reference.
772 if (hasNonTrivialDestructorOrCopyConstructor(RT))
773 return;
Daniel Dunbarce9f4232009-11-22 23:01:23 +0000774
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000775 const RecordDecl *RD = RT->getDecl();
776
777 // Assume variable sized types are passed in memory.
778 if (RD->hasFlexibleArrayMember())
779 return;
780
781 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
782
783 // Reset Lo class, this will be recomputed.
784 Current = NoClass;
Daniel Dunbarce9f4232009-11-22 23:01:23 +0000785
786 // If this is a C++ record, classify the bases first.
787 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
788 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
789 e = CXXRD->bases_end(); i != e; ++i) {
790 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
791 "Unexpected base class!");
792 const CXXRecordDecl *Base =
793 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
794
795 // Classify this field.
796 //
797 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
798 // single eightbyte, each is classified separately. Each eightbyte gets
799 // initialized to class NO_CLASS.
800 Class FieldLo, FieldHi;
801 uint64_t Offset = OffsetBase + Layout.getBaseClassOffset(Base);
802 classify(i->getType(), Context, Offset, FieldLo, FieldHi);
803 Lo = merge(Lo, FieldLo);
804 Hi = merge(Hi, FieldHi);
805 if (Lo == Memory || Hi == Memory)
806 break;
807 }
808 }
809
810 // Classify the fields one at a time, merging the results.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000811 unsigned idx = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000812 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
813 i != e; ++i, ++idx) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000814 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
815 bool BitField = i->isBitField();
816
817 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
818 // fields, it has class MEMORY.
819 //
820 // Note, skip this test for bit-fields, see below.
821 if (!BitField && Offset % Context.getTypeAlign(i->getType())) {
822 Lo = Memory;
823 return;
824 }
825
826 // Classify this field.
827 //
828 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
829 // exceeds a single eightbyte, each is classified
830 // separately. Each eightbyte gets initialized to class
831 // NO_CLASS.
832 Class FieldLo, FieldHi;
833
834 // Bit-fields require special handling, they do not force the
835 // structure to be passed in memory even if unaligned, and
836 // therefore they can straddle an eightbyte.
837 if (BitField) {
838 // Ignore padding bit-fields.
839 if (i->isUnnamedBitfield())
840 continue;
841
842 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
843 uint64_t Size = i->getBitWidth()->EvaluateAsInt(Context).getZExtValue();
844
845 uint64_t EB_Lo = Offset / 64;
846 uint64_t EB_Hi = (Offset + Size - 1) / 64;
847 FieldLo = FieldHi = NoClass;
848 if (EB_Lo) {
849 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
850 FieldLo = NoClass;
851 FieldHi = Integer;
852 } else {
853 FieldLo = Integer;
854 FieldHi = EB_Hi ? Integer : NoClass;
855 }
856 } else
857 classify(i->getType(), Context, Offset, FieldLo, FieldHi);
858 Lo = merge(Lo, FieldLo);
859 Hi = merge(Hi, FieldHi);
860 if (Lo == Memory || Hi == Memory)
861 break;
862 }
863
864 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
865 //
866 // (a) If one of the classes is MEMORY, the whole argument is
867 // passed in memory.
868 //
869 // (b) If SSEUP is not preceeded by SSE, it is converted to SSE.
870
871 // The first of these conditions is guaranteed by how we implement
872 // the merge (just bail).
873 //
874 // The second condition occurs in the case of unions; for example
875 // union { _Complex double; unsigned; }.
876 if (Hi == Memory)
877 Lo = Memory;
878 if (Hi == SSEUp && Lo != SSE)
879 Hi = SSE;
880 }
881}
882
883ABIArgInfo X86_64ABIInfo::getCoerceResult(QualType Ty,
884 const llvm::Type *CoerceTo,
885 ASTContext &Context) const {
Owen Anderson0032b272009-08-13 21:57:51 +0000886 if (CoerceTo == llvm::Type::getInt64Ty(CoerceTo->getContext())) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000887 // Integer and pointer types will end up in a general purpose
888 // register.
Anders Carlsson5b3a2fc2009-09-26 03:56:53 +0000889 if (Ty->isIntegralType() || Ty->hasPointerRepresentation())
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000890 return (Ty->isPromotableIntegerType() ?
891 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Owen Anderson0032b272009-08-13 21:57:51 +0000892 } else if (CoerceTo == llvm::Type::getDoubleTy(CoerceTo->getContext())) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000893 // FIXME: It would probably be better to make CGFunctionInfo only map using
894 // canonical types than to canonize here.
895 QualType CTy = Context.getCanonicalType(Ty);
896
897 // Float and double end up in a single SSE reg.
898 if (CTy == Context.FloatTy || CTy == Context.DoubleTy)
899 return ABIArgInfo::getDirect();
900
901 }
902
903 return ABIArgInfo::getCoerce(CoerceTo);
904}
905
906ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty,
907 ASTContext &Context) const {
908 // If this is a scalar LLVM value then assume LLVM will pass it in the right
909 // place naturally.
910 if (!CodeGenFunction::hasAggregateLLVMType(Ty))
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000911 return (Ty->isPromotableIntegerType() ?
912 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000913
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000914 bool ByVal = !isRecordWithNonTrivialDestructorOrCopyConstructor(Ty);
915
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000916 // FIXME: Set alignment correctly.
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000917 return ABIArgInfo::getIndirect(0, ByVal);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000918}
919
920ABIArgInfo X86_64ABIInfo::classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000921 ASTContext &Context,
922 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000923 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
924 // classification algorithm.
925 X86_64ABIInfo::Class Lo, Hi;
926 classify(RetTy, Context, 0, Lo, Hi);
927
928 // Check some invariants.
929 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
930 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
931 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
932
933 const llvm::Type *ResType = 0;
934 switch (Lo) {
935 case NoClass:
936 return ABIArgInfo::getIgnore();
937
938 case SSEUp:
939 case X87Up:
940 assert(0 && "Invalid classification for lo word.");
941
942 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
943 // hidden argument.
944 case Memory:
945 return getIndirectResult(RetTy, Context);
946
947 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
948 // available register of the sequence %rax, %rdx is used.
949 case Integer:
Owen Anderson0032b272009-08-13 21:57:51 +0000950 ResType = llvm::Type::getInt64Ty(VMContext); break;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000951
952 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
953 // available SSE register of the sequence %xmm0, %xmm1 is used.
954 case SSE:
Owen Anderson0032b272009-08-13 21:57:51 +0000955 ResType = llvm::Type::getDoubleTy(VMContext); break;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000956
957 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
958 // returned on the X87 stack in %st0 as 80-bit x87 number.
959 case X87:
Owen Anderson0032b272009-08-13 21:57:51 +0000960 ResType = llvm::Type::getX86_FP80Ty(VMContext); break;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000961
962 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
963 // part of the value is returned in %st0 and the imaginary part in
964 // %st1.
965 case ComplexX87:
966 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Owen Anderson0032b272009-08-13 21:57:51 +0000967 ResType = llvm::StructType::get(VMContext, llvm::Type::getX86_FP80Ty(VMContext),
968 llvm::Type::getX86_FP80Ty(VMContext),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000969 NULL);
970 break;
971 }
972
973 switch (Hi) {
974 // Memory was handled previously and X87 should
975 // never occur as a hi class.
976 case Memory:
977 case X87:
978 assert(0 && "Invalid classification for hi word.");
979
980 case ComplexX87: // Previously handled.
981 case NoClass: break;
982
983 case Integer:
Owen Anderson47a434f2009-08-05 23:18:46 +0000984 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson0032b272009-08-13 21:57:51 +0000985 llvm::Type::getInt64Ty(VMContext), NULL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000986 break;
987 case SSE:
Owen Anderson47a434f2009-08-05 23:18:46 +0000988 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson0032b272009-08-13 21:57:51 +0000989 llvm::Type::getDoubleTy(VMContext), NULL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000990 break;
991
992 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
993 // is passed in the upper half of the last used SSE register.
994 //
995 // SSEUP should always be preceeded by SSE, just widen.
996 case SSEUp:
997 assert(Lo == SSE && "Unexpected SSEUp classification.");
Owen Anderson0032b272009-08-13 21:57:51 +0000998 ResType = llvm::VectorType::get(llvm::Type::getDoubleTy(VMContext), 2);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000999 break;
1000
1001 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
1002 // returned together with the previous X87 value in %st0.
1003 case X87Up:
1004 // If X87Up is preceeded by X87, we don't need to do
1005 // anything. However, in some cases with unions it may not be
1006 // preceeded by X87. In such situations we follow gcc and pass the
1007 // extra bits in an SSE reg.
1008 if (Lo != X87)
Owen Anderson47a434f2009-08-05 23:18:46 +00001009 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson0032b272009-08-13 21:57:51 +00001010 llvm::Type::getDoubleTy(VMContext), NULL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001011 break;
1012 }
1013
1014 return getCoerceResult(RetTy, ResType, Context);
1015}
1016
1017ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, ASTContext &Context,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001018 llvm::LLVMContext &VMContext,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001019 unsigned &neededInt,
1020 unsigned &neededSSE) const {
1021 X86_64ABIInfo::Class Lo, Hi;
1022 classify(Ty, Context, 0, Lo, Hi);
1023
1024 // Check some invariants.
1025 // FIXME: Enforce these by construction.
1026 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
1027 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
1028 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1029
1030 neededInt = 0;
1031 neededSSE = 0;
1032 const llvm::Type *ResType = 0;
1033 switch (Lo) {
1034 case NoClass:
1035 return ABIArgInfo::getIgnore();
1036
1037 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
1038 // on the stack.
1039 case Memory:
1040
1041 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
1042 // COMPLEX_X87, it is passed in memory.
1043 case X87:
1044 case ComplexX87:
1045 return getIndirectResult(Ty, Context);
1046
1047 case SSEUp:
1048 case X87Up:
1049 assert(0 && "Invalid classification for lo word.");
1050
1051 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
1052 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
1053 // and %r9 is used.
1054 case Integer:
1055 ++neededInt;
Owen Anderson0032b272009-08-13 21:57:51 +00001056 ResType = llvm::Type::getInt64Ty(VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001057 break;
1058
1059 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
1060 // available SSE register is used, the registers are taken in the
1061 // order from %xmm0 to %xmm7.
1062 case SSE:
1063 ++neededSSE;
Owen Anderson0032b272009-08-13 21:57:51 +00001064 ResType = llvm::Type::getDoubleTy(VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001065 break;
1066 }
1067
1068 switch (Hi) {
1069 // Memory was handled previously, ComplexX87 and X87 should
1070 // never occur as hi classes, and X87Up must be preceed by X87,
1071 // which is passed in memory.
1072 case Memory:
1073 case X87:
1074 case ComplexX87:
1075 assert(0 && "Invalid classification for hi word.");
1076 break;
1077
1078 case NoClass: break;
1079 case Integer:
Owen Anderson47a434f2009-08-05 23:18:46 +00001080 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson0032b272009-08-13 21:57:51 +00001081 llvm::Type::getInt64Ty(VMContext), NULL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001082 ++neededInt;
1083 break;
1084
1085 // X87Up generally doesn't occur here (long double is passed in
1086 // memory), except in situations involving unions.
1087 case X87Up:
1088 case SSE:
Owen Anderson47a434f2009-08-05 23:18:46 +00001089 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson0032b272009-08-13 21:57:51 +00001090 llvm::Type::getDoubleTy(VMContext), NULL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001091 ++neededSSE;
1092 break;
1093
1094 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
1095 // eightbyte is passed in the upper half of the last used SSE
1096 // register.
1097 case SSEUp:
1098 assert(Lo == SSE && "Unexpected SSEUp classification.");
Owen Anderson0032b272009-08-13 21:57:51 +00001099 ResType = llvm::VectorType::get(llvm::Type::getDoubleTy(VMContext), 2);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001100 break;
1101 }
1102
1103 return getCoerceResult(Ty, ResType, Context);
1104}
1105
Owen Andersona1cf15f2009-07-14 23:10:40 +00001106void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1107 llvm::LLVMContext &VMContext) const {
1108 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(),
1109 Context, VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001110
1111 // Keep track of the number of assigned registers.
1112 unsigned freeIntRegs = 6, freeSSERegs = 8;
1113
1114 // If the return value is indirect, then the hidden argument is consuming one
1115 // integer register.
1116 if (FI.getReturnInfo().isIndirect())
1117 --freeIntRegs;
1118
1119 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
1120 // get assigned (in left-to-right order) for passing as follows...
1121 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1122 it != ie; ++it) {
1123 unsigned neededInt, neededSSE;
Mike Stump1eb44332009-09-09 15:08:12 +00001124 it->info = classifyArgumentType(it->type, Context, VMContext,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001125 neededInt, neededSSE);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001126
1127 // AMD64-ABI 3.2.3p3: If there are no registers available for any
1128 // eightbyte of an argument, the whole argument is passed on the
1129 // stack. If registers have already been assigned for some
1130 // eightbytes of such an argument, the assignments get reverted.
1131 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
1132 freeIntRegs -= neededInt;
1133 freeSSERegs -= neededSSE;
1134 } else {
1135 it->info = getIndirectResult(it->type, Context);
1136 }
1137 }
1138}
1139
1140static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
1141 QualType Ty,
1142 CodeGenFunction &CGF) {
1143 llvm::Value *overflow_arg_area_p =
1144 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
1145 llvm::Value *overflow_arg_area =
1146 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
1147
1148 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
1149 // byte boundary if alignment needed by type exceeds 8 byte boundary.
1150 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
1151 if (Align > 8) {
1152 // Note that we follow the ABI & gcc here, even though the type
1153 // could in theory have an alignment greater than 16. This case
1154 // shouldn't ever matter in practice.
1155
1156 // overflow_arg_area = (overflow_arg_area + 15) & ~15;
Owen Anderson0032b272009-08-13 21:57:51 +00001157 llvm::Value *Offset =
1158 llvm::ConstantInt::get(llvm::Type::getInt32Ty(CGF.getLLVMContext()), 15);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001159 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
1160 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
Owen Anderson0032b272009-08-13 21:57:51 +00001161 llvm::Type::getInt64Ty(CGF.getLLVMContext()));
1162 llvm::Value *Mask = llvm::ConstantInt::get(
1163 llvm::Type::getInt64Ty(CGF.getLLVMContext()), ~15LL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001164 overflow_arg_area =
1165 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
1166 overflow_arg_area->getType(),
1167 "overflow_arg_area.align");
1168 }
1169
1170 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
1171 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1172 llvm::Value *Res =
1173 CGF.Builder.CreateBitCast(overflow_arg_area,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001174 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001175
1176 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
1177 // l->overflow_arg_area + sizeof(type).
1178 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
1179 // an 8 byte boundary.
1180
1181 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
Owen Anderson0032b272009-08-13 21:57:51 +00001182 llvm::Value *Offset =
1183 llvm::ConstantInt::get(llvm::Type::getInt32Ty(CGF.getLLVMContext()),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001184 (SizeInBytes + 7) & ~7);
1185 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
1186 "overflow_arg_area.next");
1187 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
1188
1189 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
1190 return Res;
1191}
1192
1193llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1194 CodeGenFunction &CGF) const {
Owen Andersona1cf15f2009-07-14 23:10:40 +00001195 llvm::LLVMContext &VMContext = CGF.getLLVMContext();
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001196 const llvm::Type *i32Ty = llvm::Type::getInt32Ty(VMContext);
1197 const llvm::Type *DoubleTy = llvm::Type::getDoubleTy(VMContext);
Mike Stump1eb44332009-09-09 15:08:12 +00001198
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001199 // Assume that va_list type is correct; should be pointer to LLVM type:
1200 // struct {
1201 // i32 gp_offset;
1202 // i32 fp_offset;
1203 // i8* overflow_arg_area;
1204 // i8* reg_save_area;
1205 // };
1206 unsigned neededInt, neededSSE;
Owen Andersona1cf15f2009-07-14 23:10:40 +00001207 ABIArgInfo AI = classifyArgumentType(Ty, CGF.getContext(), VMContext,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001208 neededInt, neededSSE);
1209
1210 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
1211 // in the registers. If not go to step 7.
1212 if (!neededInt && !neededSSE)
1213 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1214
1215 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
1216 // general purpose registers needed to pass type and num_fp to hold
1217 // the number of floating point registers needed.
1218
1219 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
1220 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
1221 // l->fp_offset > 304 - num_fp * 16 go to step 7.
1222 //
1223 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
1224 // register save space).
1225
1226 llvm::Value *InRegs = 0;
1227 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
1228 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
1229 if (neededInt) {
1230 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
1231 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
1232 InRegs =
1233 CGF.Builder.CreateICmpULE(gp_offset,
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001234 llvm::ConstantInt::get(i32Ty,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001235 48 - neededInt * 8),
1236 "fits_in_gp");
1237 }
1238
1239 if (neededSSE) {
1240 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
1241 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
1242 llvm::Value *FitsInFP =
1243 CGF.Builder.CreateICmpULE(fp_offset,
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001244 llvm::ConstantInt::get(i32Ty,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001245 176 - neededSSE * 16),
1246 "fits_in_fp");
1247 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
1248 }
1249
1250 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
1251 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
1252 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
1253 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
1254
1255 // Emit code to load the value if it was passed in registers.
1256
1257 CGF.EmitBlock(InRegBlock);
1258
1259 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
1260 // an offset of l->gp_offset and/or l->fp_offset. This may require
1261 // copying to a temporary location in case the parameter is passed
1262 // in different register classes or requires an alignment greater
1263 // than 8 for general purpose registers and 16 for XMM registers.
1264 //
1265 // FIXME: This really results in shameful code when we end up needing to
1266 // collect arguments from different places; often what should result in a
1267 // simple assembling of a structure from scattered addresses has many more
1268 // loads than necessary. Can we clean this up?
1269 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1270 llvm::Value *RegAddr =
1271 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
1272 "reg_save_area");
1273 if (neededInt && neededSSE) {
1274 // FIXME: Cleanup.
1275 assert(AI.isCoerce() && "Unexpected ABI info for mixed regs");
1276 const llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
1277 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
1278 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
1279 const llvm::Type *TyLo = ST->getElementType(0);
1280 const llvm::Type *TyHi = ST->getElementType(1);
1281 assert((TyLo->isFloatingPoint() ^ TyHi->isFloatingPoint()) &&
1282 "Unexpected ABI info for mixed regs");
Owen Anderson96e0fc72009-07-29 22:16:19 +00001283 const llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
1284 const llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001285 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1286 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1287 llvm::Value *RegLoAddr = TyLo->isFloatingPoint() ? FPAddr : GPAddr;
1288 llvm::Value *RegHiAddr = TyLo->isFloatingPoint() ? GPAddr : FPAddr;
1289 llvm::Value *V =
1290 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
1291 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1292 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
1293 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1294
Owen Andersona1cf15f2009-07-14 23:10:40 +00001295 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001296 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001297 } else if (neededInt) {
1298 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1299 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001300 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001301 } else {
1302 if (neededSSE == 1) {
1303 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1304 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001305 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001306 } else {
1307 assert(neededSSE == 2 && "Invalid number of needed registers!");
1308 // SSE registers are spaced 16 bytes apart in the register save
1309 // area, we need to collect the two eightbytes together.
1310 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1311 llvm::Value *RegAddrHi =
1312 CGF.Builder.CreateGEP(RegAddrLo,
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001313 llvm::ConstantInt::get(i32Ty, 16));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001314 const llvm::Type *DblPtrTy =
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001315 llvm::PointerType::getUnqual(DoubleTy);
1316 const llvm::StructType *ST = llvm::StructType::get(VMContext, DoubleTy,
1317 DoubleTy, NULL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001318 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
1319 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
1320 DblPtrTy));
1321 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1322 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
1323 DblPtrTy));
1324 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1325 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001326 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001327 }
1328 }
1329
1330 // AMD64-ABI 3.5.7p5: Step 5. Set:
1331 // l->gp_offset = l->gp_offset + num_gp * 8
1332 // l->fp_offset = l->fp_offset + num_fp * 16.
1333 if (neededInt) {
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001334 llvm::Value *Offset = llvm::ConstantInt::get(i32Ty, neededInt * 8);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001335 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
1336 gp_offset_p);
1337 }
1338 if (neededSSE) {
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001339 llvm::Value *Offset = llvm::ConstantInt::get(i32Ty, neededSSE * 16);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001340 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
1341 fp_offset_p);
1342 }
1343 CGF.EmitBranch(ContBlock);
1344
1345 // Emit code to load the value if it was passed in memory.
1346
1347 CGF.EmitBlock(InMemBlock);
1348 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1349
1350 // Return the appropriate result.
1351
1352 CGF.EmitBlock(ContBlock);
1353 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(),
1354 "vaarg.addr");
1355 ResAddr->reserveOperandSpace(2);
1356 ResAddr->addIncoming(RegAddr, InRegBlock);
1357 ResAddr->addIncoming(MemAddr, InMemBlock);
1358
1359 return ResAddr;
1360}
1361
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001362// PIC16 ABI Implementation
1363
1364namespace {
1365
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001366class PIC16ABIInfo : public ABIInfo {
1367 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001368 ASTContext &Context,
1369 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001370
1371 ABIArgInfo classifyArgumentType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001372 ASTContext &Context,
1373 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001374
Owen Andersona1cf15f2009-07-14 23:10:40 +00001375 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1376 llvm::LLVMContext &VMContext) const {
1377 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context,
1378 VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001379 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1380 it != ie; ++it)
Owen Andersona1cf15f2009-07-14 23:10:40 +00001381 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001382 }
1383
1384 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1385 CodeGenFunction &CGF) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001386};
1387
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001388}
1389
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001390ABIArgInfo PIC16ABIInfo::classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001391 ASTContext &Context,
1392 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001393 if (RetTy->isVoidType()) {
1394 return ABIArgInfo::getIgnore();
1395 } else {
1396 return ABIArgInfo::getDirect();
1397 }
1398}
1399
1400ABIArgInfo PIC16ABIInfo::classifyArgumentType(QualType Ty,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001401 ASTContext &Context,
1402 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001403 return ABIArgInfo::getDirect();
1404}
1405
1406llvm::Value *PIC16ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1407 CodeGenFunction &CGF) const {
1408 return 0;
1409}
1410
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001411// ARM ABI Implementation
1412
1413namespace {
1414
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001415class ARMABIInfo : public ABIInfo {
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00001416public:
1417 enum ABIKind {
1418 APCS = 0,
1419 AAPCS = 1,
1420 AAPCS_VFP
1421 };
1422
1423private:
1424 ABIKind Kind;
1425
1426public:
1427 ARMABIInfo(ABIKind _Kind) : Kind(_Kind) {}
1428
1429private:
1430 ABIKind getABIKind() const { return Kind; }
1431
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001432 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001433 ASTContext &Context,
1434 llvm::LLVMContext &VMCOntext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001435
1436 ABIArgInfo classifyArgumentType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001437 ASTContext &Context,
1438 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001439
Owen Andersona1cf15f2009-07-14 23:10:40 +00001440 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1441 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001442
1443 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1444 CodeGenFunction &CGF) const;
1445};
1446
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001447}
1448
Owen Andersona1cf15f2009-07-14 23:10:40 +00001449void ARMABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1450 llvm::LLVMContext &VMContext) const {
Mike Stump1eb44332009-09-09 15:08:12 +00001451 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001452 VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001453 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1454 it != ie; ++it) {
Owen Andersona1cf15f2009-07-14 23:10:40 +00001455 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001456 }
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00001457
1458 // ARM always overrides the calling convention.
1459 switch (getABIKind()) {
1460 case APCS:
1461 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_APCS);
1462 break;
1463
1464 case AAPCS:
1465 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS);
1466 break;
1467
1468 case AAPCS_VFP:
1469 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS_VFP);
1470 break;
1471 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001472}
1473
1474ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001475 ASTContext &Context,
1476 llvm::LLVMContext &VMContext) const {
Daniel Dunbar98303b92009-09-13 08:03:58 +00001477 if (!CodeGenFunction::hasAggregateLLVMType(Ty))
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001478 return (Ty->isPromotableIntegerType() ?
1479 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Daniel Dunbar98303b92009-09-13 08:03:58 +00001480
Daniel Dunbar42025572009-09-14 21:54:03 +00001481 // Ignore empty records.
1482 if (isEmptyRecord(Context, Ty, true))
1483 return ABIArgInfo::getIgnore();
1484
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001485 // FIXME: This is kind of nasty... but there isn't much choice because the ARM
1486 // backend doesn't support byval.
1487 // FIXME: This doesn't handle alignment > 64 bits.
1488 const llvm::Type* ElemTy;
1489 unsigned SizeRegs;
1490 if (Context.getTypeAlign(Ty) > 32) {
Owen Anderson0032b272009-08-13 21:57:51 +00001491 ElemTy = llvm::Type::getInt64Ty(VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001492 SizeRegs = (Context.getTypeSize(Ty) + 63) / 64;
1493 } else {
Owen Anderson0032b272009-08-13 21:57:51 +00001494 ElemTy = llvm::Type::getInt32Ty(VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001495 SizeRegs = (Context.getTypeSize(Ty) + 31) / 32;
1496 }
1497 std::vector<const llvm::Type*> LLVMFields;
Owen Anderson96e0fc72009-07-29 22:16:19 +00001498 LLVMFields.push_back(llvm::ArrayType::get(ElemTy, SizeRegs));
Owen Anderson47a434f2009-08-05 23:18:46 +00001499 const llvm::Type* STy = llvm::StructType::get(VMContext, LLVMFields, true);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001500 return ABIArgInfo::getCoerce(STy);
1501}
1502
Daniel Dunbar98303b92009-09-13 08:03:58 +00001503static bool isIntegerLikeType(QualType Ty,
1504 ASTContext &Context,
1505 llvm::LLVMContext &VMContext) {
1506 // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
1507 // is called integer-like if its size is less than or equal to one word, and
1508 // the offset of each of its addressable sub-fields is zero.
1509
1510 uint64_t Size = Context.getTypeSize(Ty);
1511
1512 // Check that the type fits in a word.
1513 if (Size > 32)
1514 return false;
1515
1516 // FIXME: Handle vector types!
1517 if (Ty->isVectorType())
1518 return false;
1519
Daniel Dunbarb0d58192009-09-14 02:20:34 +00001520 // Float types are never treated as "integer like".
1521 if (Ty->isRealFloatingType())
1522 return false;
1523
Daniel Dunbar98303b92009-09-13 08:03:58 +00001524 // If this is a builtin or pointer type then it is ok.
John McCall183700f2009-09-21 23:43:11 +00001525 if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
Daniel Dunbar98303b92009-09-13 08:03:58 +00001526 return true;
1527
1528 // Complex types "should" be ok by the definition above, but they are not.
1529 if (Ty->isAnyComplexType())
1530 return false;
1531
1532 // Single element and zero sized arrays should be allowed, by the definition
1533 // above, but they are not.
1534
1535 // Otherwise, it must be a record type.
1536 const RecordType *RT = Ty->getAs<RecordType>();
1537 if (!RT) return false;
1538
1539 // Ignore records with flexible arrays.
1540 const RecordDecl *RD = RT->getDecl();
1541 if (RD->hasFlexibleArrayMember())
1542 return false;
1543
1544 // Check that all sub-fields are at offset 0, and are themselves "integer
1545 // like".
1546 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
1547
1548 bool HadField = false;
1549 unsigned idx = 0;
1550 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1551 i != e; ++i, ++idx) {
1552 const FieldDecl *FD = *i;
1553
1554 // Check if this field is at offset 0.
1555 uint64_t Offset = Layout.getFieldOffset(idx);
1556 if (Offset != 0) {
1557 // Allow padding bit-fields, but only if they are all at the end of the
1558 // structure (despite the wording above, this matches gcc).
1559 if (FD->isBitField() &&
1560 !FD->getBitWidth()->EvaluateAsInt(Context).getZExtValue()) {
1561 for (; i != e; ++i)
1562 if (!i->isBitField() ||
1563 i->getBitWidth()->EvaluateAsInt(Context).getZExtValue())
1564 return false;
1565
1566 // All remaining fields are padding, allow this.
1567 return true;
1568 }
1569
1570 return false;
1571 }
1572
1573 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
1574 return false;
1575
1576 // Only allow at most one field in a structure. Again this doesn't match the
1577 // wording above, but follows gcc.
1578 if (!RD->isUnion()) {
1579 if (HadField)
1580 return false;
1581
1582 HadField = true;
1583 }
1584 }
1585
1586 return true;
1587}
1588
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001589ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001590 ASTContext &Context,
1591 llvm::LLVMContext &VMContext) const {
Daniel Dunbar98303b92009-09-13 08:03:58 +00001592 if (RetTy->isVoidType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001593 return ABIArgInfo::getIgnore();
Daniel Dunbar98303b92009-09-13 08:03:58 +00001594
1595 if (!CodeGenFunction::hasAggregateLLVMType(RetTy))
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001596 return (RetTy->isPromotableIntegerType() ?
1597 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Daniel Dunbar98303b92009-09-13 08:03:58 +00001598
1599 // Are we following APCS?
1600 if (getABIKind() == APCS) {
1601 if (isEmptyRecord(Context, RetTy, false))
1602 return ABIArgInfo::getIgnore();
1603
1604 // Integer like structures are returned in r0.
1605 if (isIntegerLikeType(RetTy, Context, VMContext)) {
1606 // Return in the smallest viable integer type.
1607 uint64_t Size = Context.getTypeSize(RetTy);
1608 if (Size <= 8)
1609 return ABIArgInfo::getCoerce(llvm::Type::getInt8Ty(VMContext));
1610 if (Size <= 16)
1611 return ABIArgInfo::getCoerce(llvm::Type::getInt16Ty(VMContext));
1612 return ABIArgInfo::getCoerce(llvm::Type::getInt32Ty(VMContext));
1613 }
1614
1615 // Otherwise return in memory.
1616 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001617 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00001618
1619 // Otherwise this is an AAPCS variant.
1620
Daniel Dunbar16a08082009-09-14 00:56:55 +00001621 if (isEmptyRecord(Context, RetTy, true))
1622 return ABIArgInfo::getIgnore();
1623
Daniel Dunbar98303b92009-09-13 08:03:58 +00001624 // Aggregates <= 4 bytes are returned in r0; other aggregates
1625 // are returned indirectly.
1626 uint64_t Size = Context.getTypeSize(RetTy);
Daniel Dunbar16a08082009-09-14 00:56:55 +00001627 if (Size <= 32) {
1628 // Return in the smallest viable integer type.
1629 if (Size <= 8)
1630 return ABIArgInfo::getCoerce(llvm::Type::getInt8Ty(VMContext));
1631 if (Size <= 16)
1632 return ABIArgInfo::getCoerce(llvm::Type::getInt16Ty(VMContext));
Daniel Dunbar98303b92009-09-13 08:03:58 +00001633 return ABIArgInfo::getCoerce(llvm::Type::getInt32Ty(VMContext));
Daniel Dunbar16a08082009-09-14 00:56:55 +00001634 }
1635
Daniel Dunbar98303b92009-09-13 08:03:58 +00001636 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001637}
1638
1639llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1640 CodeGenFunction &CGF) const {
1641 // FIXME: Need to handle alignment
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00001642 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Owen Anderson96e0fc72009-07-29 22:16:19 +00001643 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001644
1645 CGBuilderTy &Builder = CGF.Builder;
1646 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
1647 "ap");
1648 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
1649 llvm::Type *PTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00001650 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001651 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
1652
1653 uint64_t Offset =
1654 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
1655 llvm::Value *NextAddr =
Owen Anderson0032b272009-08-13 21:57:51 +00001656 Builder.CreateGEP(Addr, llvm::ConstantInt::get(
1657 llvm::Type::getInt32Ty(CGF.getLLVMContext()), Offset),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001658 "ap.next");
1659 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
1660
1661 return AddrTyped;
1662}
1663
1664ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001665 ASTContext &Context,
1666 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001667 if (RetTy->isVoidType()) {
1668 return ABIArgInfo::getIgnore();
1669 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1670 return ABIArgInfo::getIndirect(0);
1671 } else {
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001672 return (RetTy->isPromotableIntegerType() ?
1673 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001674 }
1675}
1676
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001677// SystemZ ABI Implementation
1678
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00001679namespace {
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001680
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00001681class SystemZABIInfo : public ABIInfo {
1682 bool isPromotableIntegerType(QualType Ty) const;
1683
1684 ABIArgInfo classifyReturnType(QualType RetTy, ASTContext &Context,
1685 llvm::LLVMContext &VMContext) const;
1686
1687 ABIArgInfo classifyArgumentType(QualType RetTy, ASTContext &Context,
1688 llvm::LLVMContext &VMContext) const;
1689
1690 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1691 llvm::LLVMContext &VMContext) const {
1692 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(),
1693 Context, VMContext);
1694 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1695 it != ie; ++it)
1696 it->info = classifyArgumentType(it->type, Context, VMContext);
1697 }
1698
1699 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1700 CodeGenFunction &CGF) const;
1701};
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001702
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00001703}
1704
1705bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const {
1706 // SystemZ ABI requires all 8, 16 and 32 bit quantities to be extended.
John McCall183700f2009-09-21 23:43:11 +00001707 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00001708 switch (BT->getKind()) {
1709 case BuiltinType::Bool:
1710 case BuiltinType::Char_S:
1711 case BuiltinType::Char_U:
1712 case BuiltinType::SChar:
1713 case BuiltinType::UChar:
1714 case BuiltinType::Short:
1715 case BuiltinType::UShort:
1716 case BuiltinType::Int:
1717 case BuiltinType::UInt:
1718 return true;
1719 default:
1720 return false;
1721 }
1722 return false;
1723}
1724
1725llvm::Value *SystemZABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1726 CodeGenFunction &CGF) const {
1727 // FIXME: Implement
1728 return 0;
1729}
1730
1731
1732ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy,
1733 ASTContext &Context,
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001734 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00001735 if (RetTy->isVoidType()) {
1736 return ABIArgInfo::getIgnore();
1737 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1738 return ABIArgInfo::getIndirect(0);
1739 } else {
1740 return (isPromotableIntegerType(RetTy) ?
1741 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1742 }
1743}
1744
1745ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty,
1746 ASTContext &Context,
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001747 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00001748 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
1749 return ABIArgInfo::getIndirect(0);
1750 } else {
1751 return (isPromotableIntegerType(Ty) ?
1752 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1753 }
1754}
1755
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001756ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001757 ASTContext &Context,
1758 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001759 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
1760 return ABIArgInfo::getIndirect(0);
1761 } else {
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001762 return (Ty->isPromotableIntegerType() ?
1763 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001764 }
1765}
1766
1767llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1768 CodeGenFunction &CGF) const {
1769 return 0;
1770}
1771
1772const ABIInfo &CodeGenTypes::getABIInfo() const {
1773 if (TheABIInfo)
1774 return *TheABIInfo;
1775
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00001776 // For now we just cache the ABIInfo in CodeGenTypes and don't free it.
1777
Daniel Dunbar1752ee42009-08-24 09:10:05 +00001778 const llvm::Triple &Triple(getContext().Target.getTriple());
1779 switch (Triple.getArch()) {
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00001780 default:
1781 return *(TheABIInfo = new DefaultABIInfo);
1782
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001783 case llvm::Triple::arm:
1784 case llvm::Triple::thumb:
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00001785 // FIXME: We want to know the float calling convention as well.
Daniel Dunbar018ba5a2009-09-14 00:35:03 +00001786 if (strcmp(getContext().Target.getABI(), "apcs-gnu") == 0)
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00001787 return *(TheABIInfo = new ARMABIInfo(ARMABIInfo::APCS));
1788
1789 return *(TheABIInfo = new ARMABIInfo(ARMABIInfo::AAPCS));
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001790
1791 case llvm::Triple::pic16:
1792 return *(TheABIInfo = new PIC16ABIInfo());
1793
1794 case llvm::Triple::systemz:
1795 return *(TheABIInfo = new SystemZABIInfo());
1796
Daniel Dunbar1752ee42009-08-24 09:10:05 +00001797 case llvm::Triple::x86:
Daniel Dunbar1752ee42009-08-24 09:10:05 +00001798 switch (Triple.getOS()) {
Edward O'Callaghan7ee68bd2009-10-20 17:22:50 +00001799 case llvm::Triple::Darwin:
Daniel Dunbar2b6c7312009-10-20 18:07:06 +00001800 return *(TheABIInfo = new X86_32ABIInfo(Context, true, true));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00001801 case llvm::Triple::Cygwin:
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00001802 case llvm::Triple::MinGW32:
1803 case llvm::Triple::MinGW64:
Edward O'Callaghan727e2682009-10-21 11:58:24 +00001804 case llvm::Triple::AuroraUX:
1805 case llvm::Triple::DragonFly:
David Chisnall75c135a2009-09-03 01:48:05 +00001806 case llvm::Triple::FreeBSD:
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00001807 case llvm::Triple::OpenBSD:
1808 return *(TheABIInfo = new X86_32ABIInfo(Context, false, true));
1809
1810 default:
1811 return *(TheABIInfo = new X86_32ABIInfo(Context, false, false));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001812 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001813
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00001814 case llvm::Triple::x86_64:
1815 return *(TheABIInfo = new X86_64ABIInfo());
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00001816 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001817}