blob: 863a297cc6a5ed1d30ac37e6232e69c0afce9002 [file] [log] [blame]
Anton Korobeynikov244360d2009-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 Carlsson15b73de2009-07-18 19:43:29 +000017#include "clang/AST/RecordLayout.h"
Anton Korobeynikov244360d2009-06-05 22:08:42 +000018#include "llvm/Type.h"
Daniel Dunbare3532f82009-08-24 08:52:16 +000019#include "llvm/ADT/Triple.h"
Daniel Dunbar7230fa52009-12-03 09:13:49 +000020#include "llvm/Support/raw_ostream.h"
Anton Korobeynikov244360d2009-06-05 22:08:42 +000021using namespace clang;
22using namespace CodeGen;
23
24ABIInfo::~ABIInfo() {}
25
26void ABIArgInfo::dump() const {
Daniel Dunbar7230fa52009-12-03 09:13:49 +000027 llvm::raw_ostream &OS = llvm::errs();
28 OS << "(ABIArgInfo Kind=";
Anton Korobeynikov244360d2009-06-05 22:08:42 +000029 switch (TheKind) {
30 case Direct:
Daniel Dunbar7230fa52009-12-03 09:13:49 +000031 OS << "Direct";
Anton Korobeynikov244360d2009-06-05 22:08:42 +000032 break;
Anton Korobeynikov18adbf52009-06-06 09:36:29 +000033 case Extend:
Daniel Dunbar7230fa52009-12-03 09:13:49 +000034 OS << "Extend";
Anton Korobeynikov18adbf52009-06-06 09:36:29 +000035 break;
Anton Korobeynikov244360d2009-06-05 22:08:42 +000036 case Ignore:
Daniel Dunbar7230fa52009-12-03 09:13:49 +000037 OS << "Ignore";
Anton Korobeynikov244360d2009-06-05 22:08:42 +000038 break;
39 case Coerce:
Daniel Dunbar7230fa52009-12-03 09:13:49 +000040 OS << "Coerce Type=";
41 getCoerceToType()->print(OS);
Anton Korobeynikov244360d2009-06-05 22:08:42 +000042 break;
43 case Indirect:
Daniel Dunbar7230fa52009-12-03 09:13:49 +000044 OS << "Indirect Align=" << getIndirectAlign();
Anton Korobeynikov244360d2009-06-05 22:08:42 +000045 break;
46 case Expand:
Daniel Dunbar7230fa52009-12-03 09:13:49 +000047 OS << "Expand";
Anton Korobeynikov244360d2009-06-05 22:08:42 +000048 break;
49 }
Daniel Dunbar7230fa52009-12-03 09:13:49 +000050 OS << ")\n";
Anton Korobeynikov244360d2009-06-05 22:08:42 +000051}
52
Daniel Dunbar626f1d82009-09-13 08:03:58 +000053static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays);
Anton Korobeynikov244360d2009-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 Dunbar626f1d82009-09-13 08:03:58 +000057static bool isEmptyField(ASTContext &Context, const FieldDecl *FD,
58 bool AllowArrays) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +000059 if (FD->isUnnamedBitfield())
60 return true;
61
62 QualType FT = FD->getType();
Anton Korobeynikov244360d2009-06-05 22:08:42 +000063
Daniel Dunbar626f1d82009-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 Korobeynikov244360d2009-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 Dunbar626f1d82009-09-13 08:03:58 +000075static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +000076 const RecordType *RT = T->getAs<RecordType>();
Anton Korobeynikov244360d2009-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 Kyrtzidiscfbfe782009-06-30 02:36:12 +000082 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
83 i != e; ++i)
Daniel Dunbar626f1d82009-09-13 08:03:58 +000084 if (!isEmptyField(Context, *i, AllowArrays))
Anton Korobeynikov244360d2009-06-05 22:08:42 +000085 return false;
86 return true;
87}
88
Anders Carlsson20759ad2009-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 Korobeynikov244360d2009-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 Kyrtzidiscfbfe782009-06-30 02:36:12 +0000128 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
129 i != e; ++i) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000130 const FieldDecl *FD = *i;
131 QualType FT = FD->getType();
132
133 // Ignore empty fields.
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000134 if (isEmptyField(Context, FD, true))
Anton Korobeynikov244360d2009-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 Dunbarb3b1e532009-09-24 05:12:36 +0000162 if (!Ty->getAs<BuiltinType>() && !Ty->isAnyPointerType() &&
163 !Ty->isAnyComplexType() && !Ty->isEnumeralType() &&
164 !Ty->isBlockPointerType())
Anton Korobeynikov244360d2009-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 Dunbar11c08c82009-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 Kyrtzidiscfbfe782009-06-30 02:36:12 +0000193 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
194 i != e; ++i) {
Anton Korobeynikov244360d2009-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 Friedman3192cc82009-06-13 21:37:10 +0000210static bool typeContainsSSEVector(const RecordDecl *RD, ASTContext &Context) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000211 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
212 i != e; ++i) {
Eli Friedman3192cc82009-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 Kremenekc23c7e62009-07-29 21:53:49 +0000219 if (const RecordType* RT = FD->getType()->getAs<RecordType>())
Eli Friedman3192cc82009-06-13 21:37:10 +0000220 if (typeContainsSSEVector(RT->getDecl(), Context))
221 return true;
222 }
223
224 return false;
225}
226
Anton Korobeynikov244360d2009-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 Anderson170229f2009-07-14 23:10:40 +0000234 ASTContext &Context,
235 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000236
237 ABIArgInfo classifyArgumentType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +0000238 ASTContext &Context,
239 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000240
Owen Anderson170229f2009-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 Korobeynikov244360d2009-06-05 22:08:42 +0000245 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
246 it != ie; ++it)
Owen Anderson170229f2009-07-14 23:10:40 +0000247 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikov244360d2009-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 Chisnallde3a0692009-08-17 23:08:21 +0000257 bool IsDarwinVectorABI;
258 bool IsSmallStructInRegABI;
Anton Korobeynikov244360d2009-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 Friedman3192cc82009-06-13 21:37:10 +0000266 static unsigned getIndirectArgumentAlignment(QualType Ty,
267 ASTContext &Context);
268
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000269public:
270 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +0000271 ASTContext &Context,
272 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000273
274 ABIArgInfo classifyArgumentType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +0000275 ASTContext &Context,
276 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000277
Owen Anderson170229f2009-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 Korobeynikov244360d2009-06-05 22:08:42 +0000282 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
283 it != ie; ++it)
Owen Anderson170229f2009-07-14 23:10:40 +0000284 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000285 }
286
287 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
288 CodeGenFunction &CGF) const;
289
David Chisnallde3a0692009-08-17 23:08:21 +0000290 X86_32ABIInfo(ASTContext &Context, bool d, bool p)
Mike Stump11289f42009-09-09 15:08:12 +0000291 : ABIInfo(), Context(Context), IsDarwinVectorABI(d),
David Chisnallde3a0692009-08-17 23:08:21 +0000292 IsSmallStructInRegABI(p) {}
Anton Korobeynikov244360d2009-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 Dunbarb3b1e532009-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 Korobeynikov244360d2009-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 Kremenekc23c7e62009-07-29 21:53:49 +0000327 const RecordType *RT = Ty->getAs<RecordType>();
Anton Korobeynikov244360d2009-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 Kyrtzidiscfbfe782009-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 Korobeynikov244360d2009-06-05 22:08:42 +0000334 const FieldDecl *FD = *i;
335
336 // Empty fields are ignored.
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000337 if (isEmptyField(Context, FD, true))
Anton Korobeynikov244360d2009-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 Anderson170229f2009-07-14 23:10:40 +0000349 ASTContext &Context,
350 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000351 if (RetTy->isVoidType()) {
352 return ABIArgInfo::getIgnore();
John McCall9dd450b2009-09-21 23:43:11 +0000353 } else if (const VectorType *VT = RetTy->getAs<VectorType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000354 // On Darwin, some vectors are returned in registers.
David Chisnallde3a0692009-08-17 23:08:21 +0000355 if (IsDarwinVectorABI) {
Anton Korobeynikov244360d2009-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 Anderson41a75022009-08-13 21:57:51 +0000362 return ABIArgInfo::getCoerce(llvm::VectorType::get(
363 llvm::Type::getInt64Ty(VMContext), 2));
Anton Korobeynikov244360d2009-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 Anderson41a75022009-08-13 21:57:51 +0000369 return ABIArgInfo::getCoerce(llvm::IntegerType::get(VMContext, Size));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000370
371 return ABIArgInfo::getIndirect(0);
372 }
373
374 return ABIArgInfo::getDirect();
375 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Anders Carlsson5789c492009-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 Korobeynikov244360d2009-06-05 22:08:42 +0000383 if (RT->getDecl()->hasFlexibleArrayMember())
384 return ABIArgInfo::getIndirect(0);
Anders Carlsson5789c492009-10-20 22:07:59 +0000385 }
386
David Chisnallde3a0692009-08-17 23:08:21 +0000387 // If specified, structs and unions are always indirect.
388 if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType())
Anton Korobeynikov244360d2009-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 McCall9dd450b2009-09-21 23:43:11 +0000393 if (const BuiltinType *BT = SeltTy->getAs<BuiltinType>()) {
Anton Korobeynikov244360d2009-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 Anderson170229f2009-07-14 23:10:40 +0000399 return ABIArgInfo::getCoerce(
Owen Anderson41a75022009-08-13 21:57:51 +0000400 llvm::IntegerType::get(VMContext, (unsigned) Size));
Anton Korobeynikov244360d2009-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 Anderson41a75022009-08-13 21:57:51 +0000404 return ABIArgInfo::getCoerce(llvm::Type::getFloatTy(VMContext));
Anton Korobeynikov244360d2009-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 Anderson41a75022009-08-13 21:57:51 +0000408 return ABIArgInfo::getCoerce(llvm::Type::getDoubleTy(VMContext));
Anton Korobeynikov244360d2009-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 Kramerabd5b902009-10-13 10:07:13 +0000413 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Anton Korobeynikov244360d2009-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 Anderson170229f2009-07-14 23:10:40 +0000422 return classifyReturnType(QualType(SeltTy, 0), Context, VMContext);
Anton Korobeynikov244360d2009-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 Anderson41a75022009-08-13 21:57:51 +0000430 return ABIArgInfo::getCoerce(llvm::IntegerType::get(VMContext, Size));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000431 }
432
433 return ABIArgInfo::getIndirect(0);
434 } else {
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000435 return (RetTy->isPromotableIntegerType() ?
436 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000437 }
438}
439
Eli Friedman3192cc82009-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 Kremenekc23c7e62009-07-29 21:53:49 +0000444 if (const RecordType* RT = Ty->getAs<RecordType>())
Eli Friedman3192cc82009-06-13 21:37:10 +0000445 if (typeContainsSSEVector(RT->getDecl(), Context))
446 return 16;
447 return 0;
448}
449
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000450ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
Owen Anderson170229f2009-07-14 23:10:40 +0000451 ASTContext &Context,
452 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov244360d2009-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 Stump11289f42009-09-09 15:08:12 +0000458 return ABIArgInfo::getIndirect(getIndirectArgumentAlignment(Ty,
Eli Friedman3192cc82009-06-13 21:37:10 +0000459 Context));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000460
461 // Ignore empty structs.
Eli Friedman3192cc82009-06-13 21:37:10 +0000462 if (Ty->isStructureType() && Context.getTypeSize(Ty) == 0)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000463 return ABIArgInfo::getIgnore();
464
Daniel Dunbar11c08c82009-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 Korobeynikov244360d2009-06-05 22:08:42 +0000472
Eli Friedman3192cc82009-06-13 21:37:10 +0000473 return ABIArgInfo::getIndirect(getIndirectArgumentAlignment(Ty, Context));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000474 } else {
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000475 return (Ty->isPromotableIntegerType() ?
476 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000477 }
478}
479
480llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
481 CodeGenFunction &CGF) const {
Benjamin Kramerabd5b902009-10-13 10:07:13 +0000482 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Owen Anderson9793f0e2009-07-29 22:16:19 +0000483 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikov244360d2009-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 Anderson9793f0e2009-07-29 22:16:19 +0000490 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikov244360d2009-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 Anderson41a75022009-08-13 21:57:51 +0000496 Builder.CreateGEP(Addr, llvm::ConstantInt::get(
497 llvm::Type::getInt32Ty(CGF.getLLVMContext()), Offset),
Anton Korobeynikov244360d2009-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 Anderson170229f2009-07-14 23:10:40 +0000572 ASTContext &Context,
573 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000574
575 ABIArgInfo classifyArgumentType(QualType Ty,
576 ASTContext &Context,
Owen Anderson170229f2009-07-14 23:10:40 +0000577 llvm::LLVMContext &VMContext,
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000578 unsigned &neededInt,
579 unsigned &neededSSE) const;
580
581public:
Owen Anderson170229f2009-07-14 23:10:40 +0000582 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
583 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-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 McCall9dd450b2009-09-21 23:43:11 +0000649 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
Anton Korobeynikov244360d2009-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 McCall9dd450b2009-09-21 23:43:11 +0000667 } else if (const EnumType *ET = Ty->getAs<EnumType>()) {
Anton Korobeynikov244360d2009-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 McCall9dd450b2009-09-21 23:43:11 +0000672 } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
Anton Korobeynikov244360d2009-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 McCall9dd450b2009-09-21 23:43:11 +0000704 } else if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
Anton Korobeynikov244360d2009-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 Kremenekc23c7e62009-07-29 21:53:49 +0000761 } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
Anton Korobeynikov244360d2009-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 Carlsson20759ad2009-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 Dunbare1cd0152009-11-22 23:01:23 +0000774
Anton Korobeynikov244360d2009-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 Dunbare1cd0152009-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 }
Daniel Dunbar3780f0b2009-12-22 01:19:25 +0000808
809 // If this record has no fields but isn't empty, classify as INTEGER.
810 if (RD->field_empty() && Size)
811 Current = Integer;
Daniel Dunbare1cd0152009-11-22 23:01:23 +0000812 }
813
814 // Classify the fields one at a time, merging the results.
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000815 unsigned idx = 0;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000816 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
817 i != e; ++i, ++idx) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000818 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
819 bool BitField = i->isBitField();
820
821 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
822 // fields, it has class MEMORY.
823 //
824 // Note, skip this test for bit-fields, see below.
825 if (!BitField && Offset % Context.getTypeAlign(i->getType())) {
826 Lo = Memory;
827 return;
828 }
829
830 // Classify this field.
831 //
832 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
833 // exceeds a single eightbyte, each is classified
834 // separately. Each eightbyte gets initialized to class
835 // NO_CLASS.
836 Class FieldLo, FieldHi;
837
838 // Bit-fields require special handling, they do not force the
839 // structure to be passed in memory even if unaligned, and
840 // therefore they can straddle an eightbyte.
841 if (BitField) {
842 // Ignore padding bit-fields.
843 if (i->isUnnamedBitfield())
844 continue;
845
846 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
847 uint64_t Size = i->getBitWidth()->EvaluateAsInt(Context).getZExtValue();
848
849 uint64_t EB_Lo = Offset / 64;
850 uint64_t EB_Hi = (Offset + Size - 1) / 64;
851 FieldLo = FieldHi = NoClass;
852 if (EB_Lo) {
853 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
854 FieldLo = NoClass;
855 FieldHi = Integer;
856 } else {
857 FieldLo = Integer;
858 FieldHi = EB_Hi ? Integer : NoClass;
859 }
860 } else
861 classify(i->getType(), Context, Offset, FieldLo, FieldHi);
862 Lo = merge(Lo, FieldLo);
863 Hi = merge(Hi, FieldHi);
864 if (Lo == Memory || Hi == Memory)
865 break;
866 }
867
868 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
869 //
870 // (a) If one of the classes is MEMORY, the whole argument is
871 // passed in memory.
872 //
873 // (b) If SSEUP is not preceeded by SSE, it is converted to SSE.
874
875 // The first of these conditions is guaranteed by how we implement
876 // the merge (just bail).
877 //
878 // The second condition occurs in the case of unions; for example
879 // union { _Complex double; unsigned; }.
880 if (Hi == Memory)
881 Lo = Memory;
882 if (Hi == SSEUp && Lo != SSE)
883 Hi = SSE;
884 }
885}
886
887ABIArgInfo X86_64ABIInfo::getCoerceResult(QualType Ty,
888 const llvm::Type *CoerceTo,
889 ASTContext &Context) const {
Owen Anderson41a75022009-08-13 21:57:51 +0000890 if (CoerceTo == llvm::Type::getInt64Ty(CoerceTo->getContext())) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000891 // Integer and pointer types will end up in a general purpose
892 // register.
Anders Carlsson03747422009-09-26 03:56:53 +0000893 if (Ty->isIntegralType() || Ty->hasPointerRepresentation())
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000894 return (Ty->isPromotableIntegerType() ?
895 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Owen Anderson41a75022009-08-13 21:57:51 +0000896 } else if (CoerceTo == llvm::Type::getDoubleTy(CoerceTo->getContext())) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000897 // FIXME: It would probably be better to make CGFunctionInfo only map using
898 // canonical types than to canonize here.
899 QualType CTy = Context.getCanonicalType(Ty);
900
901 // Float and double end up in a single SSE reg.
902 if (CTy == Context.FloatTy || CTy == Context.DoubleTy)
903 return ABIArgInfo::getDirect();
904
905 }
906
907 return ABIArgInfo::getCoerce(CoerceTo);
908}
909
910ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty,
911 ASTContext &Context) const {
912 // If this is a scalar LLVM value then assume LLVM will pass it in the right
913 // place naturally.
914 if (!CodeGenFunction::hasAggregateLLVMType(Ty))
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000915 return (Ty->isPromotableIntegerType() ?
916 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000917
Anders Carlsson20759ad2009-09-16 15:53:40 +0000918 bool ByVal = !isRecordWithNonTrivialDestructorOrCopyConstructor(Ty);
919
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000920 // FIXME: Set alignment correctly.
Anders Carlsson20759ad2009-09-16 15:53:40 +0000921 return ABIArgInfo::getIndirect(0, ByVal);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000922}
923
924ABIArgInfo X86_64ABIInfo::classifyReturnType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +0000925 ASTContext &Context,
926 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000927 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
928 // classification algorithm.
929 X86_64ABIInfo::Class Lo, Hi;
930 classify(RetTy, Context, 0, Lo, Hi);
931
932 // Check some invariants.
933 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
934 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
935 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
936
937 const llvm::Type *ResType = 0;
938 switch (Lo) {
939 case NoClass:
940 return ABIArgInfo::getIgnore();
941
942 case SSEUp:
943 case X87Up:
944 assert(0 && "Invalid classification for lo word.");
945
946 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
947 // hidden argument.
948 case Memory:
949 return getIndirectResult(RetTy, Context);
950
951 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
952 // available register of the sequence %rax, %rdx is used.
953 case Integer:
Owen Anderson41a75022009-08-13 21:57:51 +0000954 ResType = llvm::Type::getInt64Ty(VMContext); break;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000955
956 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
957 // available SSE register of the sequence %xmm0, %xmm1 is used.
958 case SSE:
Owen Anderson41a75022009-08-13 21:57:51 +0000959 ResType = llvm::Type::getDoubleTy(VMContext); break;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000960
961 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
962 // returned on the X87 stack in %st0 as 80-bit x87 number.
963 case X87:
Owen Anderson41a75022009-08-13 21:57:51 +0000964 ResType = llvm::Type::getX86_FP80Ty(VMContext); break;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000965
966 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
967 // part of the value is returned in %st0 and the imaginary part in
968 // %st1.
969 case ComplexX87:
970 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Owen Anderson41a75022009-08-13 21:57:51 +0000971 ResType = llvm::StructType::get(VMContext, llvm::Type::getX86_FP80Ty(VMContext),
972 llvm::Type::getX86_FP80Ty(VMContext),
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000973 NULL);
974 break;
975 }
976
977 switch (Hi) {
978 // Memory was handled previously and X87 should
979 // never occur as a hi class.
980 case Memory:
981 case X87:
982 assert(0 && "Invalid classification for hi word.");
983
984 case ComplexX87: // Previously handled.
985 case NoClass: break;
986
987 case Integer:
Owen Anderson758428f2009-08-05 23:18:46 +0000988 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson41a75022009-08-13 21:57:51 +0000989 llvm::Type::getInt64Ty(VMContext), NULL);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000990 break;
991 case SSE:
Owen Anderson758428f2009-08-05 23:18:46 +0000992 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson41a75022009-08-13 21:57:51 +0000993 llvm::Type::getDoubleTy(VMContext), NULL);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000994 break;
995
996 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
997 // is passed in the upper half of the last used SSE register.
998 //
999 // SSEUP should always be preceeded by SSE, just widen.
1000 case SSEUp:
1001 assert(Lo == SSE && "Unexpected SSEUp classification.");
Owen Anderson41a75022009-08-13 21:57:51 +00001002 ResType = llvm::VectorType::get(llvm::Type::getDoubleTy(VMContext), 2);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001003 break;
1004
1005 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
1006 // returned together with the previous X87 value in %st0.
1007 case X87Up:
1008 // If X87Up is preceeded by X87, we don't need to do
1009 // anything. However, in some cases with unions it may not be
1010 // preceeded by X87. In such situations we follow gcc and pass the
1011 // extra bits in an SSE reg.
1012 if (Lo != X87)
Owen Anderson758428f2009-08-05 23:18:46 +00001013 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson41a75022009-08-13 21:57:51 +00001014 llvm::Type::getDoubleTy(VMContext), NULL);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001015 break;
1016 }
1017
1018 return getCoerceResult(RetTy, ResType, Context);
1019}
1020
1021ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, ASTContext &Context,
Owen Anderson170229f2009-07-14 23:10:40 +00001022 llvm::LLVMContext &VMContext,
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001023 unsigned &neededInt,
1024 unsigned &neededSSE) const {
1025 X86_64ABIInfo::Class Lo, Hi;
1026 classify(Ty, Context, 0, Lo, Hi);
1027
1028 // Check some invariants.
1029 // FIXME: Enforce these by construction.
1030 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
1031 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
1032 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1033
1034 neededInt = 0;
1035 neededSSE = 0;
1036 const llvm::Type *ResType = 0;
1037 switch (Lo) {
1038 case NoClass:
1039 return ABIArgInfo::getIgnore();
1040
1041 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
1042 // on the stack.
1043 case Memory:
1044
1045 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
1046 // COMPLEX_X87, it is passed in memory.
1047 case X87:
1048 case ComplexX87:
1049 return getIndirectResult(Ty, Context);
1050
1051 case SSEUp:
1052 case X87Up:
1053 assert(0 && "Invalid classification for lo word.");
1054
1055 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
1056 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
1057 // and %r9 is used.
1058 case Integer:
1059 ++neededInt;
Owen Anderson41a75022009-08-13 21:57:51 +00001060 ResType = llvm::Type::getInt64Ty(VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001061 break;
1062
1063 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
1064 // available SSE register is used, the registers are taken in the
1065 // order from %xmm0 to %xmm7.
1066 case SSE:
1067 ++neededSSE;
Owen Anderson41a75022009-08-13 21:57:51 +00001068 ResType = llvm::Type::getDoubleTy(VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001069 break;
1070 }
1071
1072 switch (Hi) {
1073 // Memory was handled previously, ComplexX87 and X87 should
1074 // never occur as hi classes, and X87Up must be preceed by X87,
1075 // which is passed in memory.
1076 case Memory:
1077 case X87:
1078 case ComplexX87:
1079 assert(0 && "Invalid classification for hi word.");
1080 break;
1081
1082 case NoClass: break;
1083 case Integer:
Owen Anderson758428f2009-08-05 23:18:46 +00001084 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson41a75022009-08-13 21:57:51 +00001085 llvm::Type::getInt64Ty(VMContext), NULL);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001086 ++neededInt;
1087 break;
1088
1089 // X87Up generally doesn't occur here (long double is passed in
1090 // memory), except in situations involving unions.
1091 case X87Up:
1092 case SSE:
Owen Anderson758428f2009-08-05 23:18:46 +00001093 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson41a75022009-08-13 21:57:51 +00001094 llvm::Type::getDoubleTy(VMContext), NULL);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001095 ++neededSSE;
1096 break;
1097
1098 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
1099 // eightbyte is passed in the upper half of the last used SSE
1100 // register.
1101 case SSEUp:
1102 assert(Lo == SSE && "Unexpected SSEUp classification.");
Owen Anderson41a75022009-08-13 21:57:51 +00001103 ResType = llvm::VectorType::get(llvm::Type::getDoubleTy(VMContext), 2);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001104 break;
1105 }
1106
1107 return getCoerceResult(Ty, ResType, Context);
1108}
1109
Owen Anderson170229f2009-07-14 23:10:40 +00001110void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1111 llvm::LLVMContext &VMContext) const {
1112 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(),
1113 Context, VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001114
1115 // Keep track of the number of assigned registers.
1116 unsigned freeIntRegs = 6, freeSSERegs = 8;
1117
1118 // If the return value is indirect, then the hidden argument is consuming one
1119 // integer register.
1120 if (FI.getReturnInfo().isIndirect())
1121 --freeIntRegs;
1122
1123 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
1124 // get assigned (in left-to-right order) for passing as follows...
1125 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1126 it != ie; ++it) {
1127 unsigned neededInt, neededSSE;
Mike Stump11289f42009-09-09 15:08:12 +00001128 it->info = classifyArgumentType(it->type, Context, VMContext,
Owen Anderson170229f2009-07-14 23:10:40 +00001129 neededInt, neededSSE);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001130
1131 // AMD64-ABI 3.2.3p3: If there are no registers available for any
1132 // eightbyte of an argument, the whole argument is passed on the
1133 // stack. If registers have already been assigned for some
1134 // eightbytes of such an argument, the assignments get reverted.
1135 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
1136 freeIntRegs -= neededInt;
1137 freeSSERegs -= neededSSE;
1138 } else {
1139 it->info = getIndirectResult(it->type, Context);
1140 }
1141 }
1142}
1143
1144static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
1145 QualType Ty,
1146 CodeGenFunction &CGF) {
1147 llvm::Value *overflow_arg_area_p =
1148 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
1149 llvm::Value *overflow_arg_area =
1150 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
1151
1152 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
1153 // byte boundary if alignment needed by type exceeds 8 byte boundary.
1154 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
1155 if (Align > 8) {
1156 // Note that we follow the ABI & gcc here, even though the type
1157 // could in theory have an alignment greater than 16. This case
1158 // shouldn't ever matter in practice.
1159
1160 // overflow_arg_area = (overflow_arg_area + 15) & ~15;
Owen Anderson41a75022009-08-13 21:57:51 +00001161 llvm::Value *Offset =
1162 llvm::ConstantInt::get(llvm::Type::getInt32Ty(CGF.getLLVMContext()), 15);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001163 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
1164 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
Owen Anderson41a75022009-08-13 21:57:51 +00001165 llvm::Type::getInt64Ty(CGF.getLLVMContext()));
1166 llvm::Value *Mask = llvm::ConstantInt::get(
1167 llvm::Type::getInt64Ty(CGF.getLLVMContext()), ~15LL);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001168 overflow_arg_area =
1169 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
1170 overflow_arg_area->getType(),
1171 "overflow_arg_area.align");
1172 }
1173
1174 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
1175 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1176 llvm::Value *Res =
1177 CGF.Builder.CreateBitCast(overflow_arg_area,
Owen Anderson9793f0e2009-07-29 22:16:19 +00001178 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001179
1180 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
1181 // l->overflow_arg_area + sizeof(type).
1182 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
1183 // an 8 byte boundary.
1184
1185 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
Owen Anderson41a75022009-08-13 21:57:51 +00001186 llvm::Value *Offset =
1187 llvm::ConstantInt::get(llvm::Type::getInt32Ty(CGF.getLLVMContext()),
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001188 (SizeInBytes + 7) & ~7);
1189 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
1190 "overflow_arg_area.next");
1191 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
1192
1193 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
1194 return Res;
1195}
1196
1197llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1198 CodeGenFunction &CGF) const {
Owen Anderson170229f2009-07-14 23:10:40 +00001199 llvm::LLVMContext &VMContext = CGF.getLLVMContext();
Daniel Dunbard59655c2009-09-12 00:59:49 +00001200 const llvm::Type *i32Ty = llvm::Type::getInt32Ty(VMContext);
1201 const llvm::Type *DoubleTy = llvm::Type::getDoubleTy(VMContext);
Mike Stump11289f42009-09-09 15:08:12 +00001202
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001203 // Assume that va_list type is correct; should be pointer to LLVM type:
1204 // struct {
1205 // i32 gp_offset;
1206 // i32 fp_offset;
1207 // i8* overflow_arg_area;
1208 // i8* reg_save_area;
1209 // };
1210 unsigned neededInt, neededSSE;
Owen Anderson170229f2009-07-14 23:10:40 +00001211 ABIArgInfo AI = classifyArgumentType(Ty, CGF.getContext(), VMContext,
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001212 neededInt, neededSSE);
1213
1214 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
1215 // in the registers. If not go to step 7.
1216 if (!neededInt && !neededSSE)
1217 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1218
1219 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
1220 // general purpose registers needed to pass type and num_fp to hold
1221 // the number of floating point registers needed.
1222
1223 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
1224 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
1225 // l->fp_offset > 304 - num_fp * 16 go to step 7.
1226 //
1227 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
1228 // register save space).
1229
1230 llvm::Value *InRegs = 0;
1231 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
1232 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
1233 if (neededInt) {
1234 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
1235 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
1236 InRegs =
1237 CGF.Builder.CreateICmpULE(gp_offset,
Daniel Dunbard59655c2009-09-12 00:59:49 +00001238 llvm::ConstantInt::get(i32Ty,
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001239 48 - neededInt * 8),
1240 "fits_in_gp");
1241 }
1242
1243 if (neededSSE) {
1244 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
1245 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
1246 llvm::Value *FitsInFP =
1247 CGF.Builder.CreateICmpULE(fp_offset,
Daniel Dunbard59655c2009-09-12 00:59:49 +00001248 llvm::ConstantInt::get(i32Ty,
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001249 176 - neededSSE * 16),
1250 "fits_in_fp");
1251 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
1252 }
1253
1254 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
1255 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
1256 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
1257 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
1258
1259 // Emit code to load the value if it was passed in registers.
1260
1261 CGF.EmitBlock(InRegBlock);
1262
1263 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
1264 // an offset of l->gp_offset and/or l->fp_offset. This may require
1265 // copying to a temporary location in case the parameter is passed
1266 // in different register classes or requires an alignment greater
1267 // than 8 for general purpose registers and 16 for XMM registers.
1268 //
1269 // FIXME: This really results in shameful code when we end up needing to
1270 // collect arguments from different places; often what should result in a
1271 // simple assembling of a structure from scattered addresses has many more
1272 // loads than necessary. Can we clean this up?
1273 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1274 llvm::Value *RegAddr =
1275 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
1276 "reg_save_area");
1277 if (neededInt && neededSSE) {
1278 // FIXME: Cleanup.
1279 assert(AI.isCoerce() && "Unexpected ABI info for mixed regs");
1280 const llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
1281 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
1282 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
1283 const llvm::Type *TyLo = ST->getElementType(0);
1284 const llvm::Type *TyHi = ST->getElementType(1);
1285 assert((TyLo->isFloatingPoint() ^ TyHi->isFloatingPoint()) &&
1286 "Unexpected ABI info for mixed regs");
Owen Anderson9793f0e2009-07-29 22:16:19 +00001287 const llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
1288 const llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001289 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1290 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1291 llvm::Value *RegLoAddr = TyLo->isFloatingPoint() ? FPAddr : GPAddr;
1292 llvm::Value *RegHiAddr = TyLo->isFloatingPoint() ? GPAddr : FPAddr;
1293 llvm::Value *V =
1294 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
1295 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1296 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
1297 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1298
Owen Anderson170229f2009-07-14 23:10:40 +00001299 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson9793f0e2009-07-29 22:16:19 +00001300 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001301 } else if (neededInt) {
1302 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1303 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson9793f0e2009-07-29 22:16:19 +00001304 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001305 } else {
1306 if (neededSSE == 1) {
1307 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1308 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson9793f0e2009-07-29 22:16:19 +00001309 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001310 } else {
1311 assert(neededSSE == 2 && "Invalid number of needed registers!");
1312 // SSE registers are spaced 16 bytes apart in the register save
1313 // area, we need to collect the two eightbytes together.
1314 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1315 llvm::Value *RegAddrHi =
1316 CGF.Builder.CreateGEP(RegAddrLo,
Daniel Dunbard59655c2009-09-12 00:59:49 +00001317 llvm::ConstantInt::get(i32Ty, 16));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001318 const llvm::Type *DblPtrTy =
Daniel Dunbard59655c2009-09-12 00:59:49 +00001319 llvm::PointerType::getUnqual(DoubleTy);
1320 const llvm::StructType *ST = llvm::StructType::get(VMContext, DoubleTy,
1321 DoubleTy, NULL);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001322 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
1323 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
1324 DblPtrTy));
1325 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1326 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
1327 DblPtrTy));
1328 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1329 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson9793f0e2009-07-29 22:16:19 +00001330 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001331 }
1332 }
1333
1334 // AMD64-ABI 3.5.7p5: Step 5. Set:
1335 // l->gp_offset = l->gp_offset + num_gp * 8
1336 // l->fp_offset = l->fp_offset + num_fp * 16.
1337 if (neededInt) {
Daniel Dunbard59655c2009-09-12 00:59:49 +00001338 llvm::Value *Offset = llvm::ConstantInt::get(i32Ty, neededInt * 8);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001339 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
1340 gp_offset_p);
1341 }
1342 if (neededSSE) {
Daniel Dunbard59655c2009-09-12 00:59:49 +00001343 llvm::Value *Offset = llvm::ConstantInt::get(i32Ty, neededSSE * 16);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001344 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
1345 fp_offset_p);
1346 }
1347 CGF.EmitBranch(ContBlock);
1348
1349 // Emit code to load the value if it was passed in memory.
1350
1351 CGF.EmitBlock(InMemBlock);
1352 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1353
1354 // Return the appropriate result.
1355
1356 CGF.EmitBlock(ContBlock);
1357 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(),
1358 "vaarg.addr");
1359 ResAddr->reserveOperandSpace(2);
1360 ResAddr->addIncoming(RegAddr, InRegBlock);
1361 ResAddr->addIncoming(MemAddr, InMemBlock);
1362
1363 return ResAddr;
1364}
1365
Daniel Dunbard59655c2009-09-12 00:59:49 +00001366// PIC16 ABI Implementation
1367
1368namespace {
1369
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001370class PIC16ABIInfo : public ABIInfo {
1371 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +00001372 ASTContext &Context,
1373 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001374
1375 ABIArgInfo classifyArgumentType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +00001376 ASTContext &Context,
1377 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001378
Owen Anderson170229f2009-07-14 23:10:40 +00001379 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1380 llvm::LLVMContext &VMContext) const {
1381 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context,
1382 VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001383 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1384 it != ie; ++it)
Owen Anderson170229f2009-07-14 23:10:40 +00001385 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001386 }
1387
1388 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1389 CodeGenFunction &CGF) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001390};
1391
Daniel Dunbard59655c2009-09-12 00:59:49 +00001392}
1393
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001394ABIArgInfo PIC16ABIInfo::classifyReturnType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +00001395 ASTContext &Context,
1396 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001397 if (RetTy->isVoidType()) {
1398 return ABIArgInfo::getIgnore();
1399 } else {
1400 return ABIArgInfo::getDirect();
1401 }
1402}
1403
1404ABIArgInfo PIC16ABIInfo::classifyArgumentType(QualType Ty,
Owen Anderson170229f2009-07-14 23:10:40 +00001405 ASTContext &Context,
1406 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001407 return ABIArgInfo::getDirect();
1408}
1409
1410llvm::Value *PIC16ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1411 CodeGenFunction &CGF) const {
1412 return 0;
1413}
1414
Daniel Dunbard59655c2009-09-12 00:59:49 +00001415// ARM ABI Implementation
1416
1417namespace {
1418
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001419class ARMABIInfo : public ABIInfo {
Daniel Dunbar020daa92009-09-12 01:00:39 +00001420public:
1421 enum ABIKind {
1422 APCS = 0,
1423 AAPCS = 1,
1424 AAPCS_VFP
1425 };
1426
1427private:
1428 ABIKind Kind;
1429
1430public:
1431 ARMABIInfo(ABIKind _Kind) : Kind(_Kind) {}
1432
1433private:
1434 ABIKind getABIKind() const { return Kind; }
1435
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001436 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +00001437 ASTContext &Context,
1438 llvm::LLVMContext &VMCOntext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001439
1440 ABIArgInfo classifyArgumentType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +00001441 ASTContext &Context,
1442 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001443
Owen Anderson170229f2009-07-14 23:10:40 +00001444 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1445 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001446
1447 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1448 CodeGenFunction &CGF) const;
1449};
1450
Daniel Dunbard59655c2009-09-12 00:59:49 +00001451}
1452
Owen Anderson170229f2009-07-14 23:10:40 +00001453void ARMABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1454 llvm::LLVMContext &VMContext) const {
Mike Stump11289f42009-09-09 15:08:12 +00001455 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context,
Owen Anderson170229f2009-07-14 23:10:40 +00001456 VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001457 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1458 it != ie; ++it) {
Owen Anderson170229f2009-07-14 23:10:40 +00001459 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001460 }
Daniel Dunbar020daa92009-09-12 01:00:39 +00001461
1462 // ARM always overrides the calling convention.
1463 switch (getABIKind()) {
1464 case APCS:
1465 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_APCS);
1466 break;
1467
1468 case AAPCS:
1469 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS);
1470 break;
1471
1472 case AAPCS_VFP:
1473 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS_VFP);
1474 break;
1475 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001476}
1477
1478ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty,
Owen Anderson170229f2009-07-14 23:10:40 +00001479 ASTContext &Context,
1480 llvm::LLVMContext &VMContext) const {
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001481 if (!CodeGenFunction::hasAggregateLLVMType(Ty))
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00001482 return (Ty->isPromotableIntegerType() ?
1483 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001484
Daniel Dunbar09d33622009-09-14 21:54:03 +00001485 // Ignore empty records.
1486 if (isEmptyRecord(Context, Ty, true))
1487 return ABIArgInfo::getIgnore();
1488
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001489 // FIXME: This is kind of nasty... but there isn't much choice because the ARM
1490 // backend doesn't support byval.
1491 // FIXME: This doesn't handle alignment > 64 bits.
1492 const llvm::Type* ElemTy;
1493 unsigned SizeRegs;
1494 if (Context.getTypeAlign(Ty) > 32) {
Owen Anderson41a75022009-08-13 21:57:51 +00001495 ElemTy = llvm::Type::getInt64Ty(VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001496 SizeRegs = (Context.getTypeSize(Ty) + 63) / 64;
1497 } else {
Owen Anderson41a75022009-08-13 21:57:51 +00001498 ElemTy = llvm::Type::getInt32Ty(VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001499 SizeRegs = (Context.getTypeSize(Ty) + 31) / 32;
1500 }
1501 std::vector<const llvm::Type*> LLVMFields;
Owen Anderson9793f0e2009-07-29 22:16:19 +00001502 LLVMFields.push_back(llvm::ArrayType::get(ElemTy, SizeRegs));
Owen Anderson758428f2009-08-05 23:18:46 +00001503 const llvm::Type* STy = llvm::StructType::get(VMContext, LLVMFields, true);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001504 return ABIArgInfo::getCoerce(STy);
1505}
1506
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001507static bool isIntegerLikeType(QualType Ty,
1508 ASTContext &Context,
1509 llvm::LLVMContext &VMContext) {
1510 // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
1511 // is called integer-like if its size is less than or equal to one word, and
1512 // the offset of each of its addressable sub-fields is zero.
1513
1514 uint64_t Size = Context.getTypeSize(Ty);
1515
1516 // Check that the type fits in a word.
1517 if (Size > 32)
1518 return false;
1519
1520 // FIXME: Handle vector types!
1521 if (Ty->isVectorType())
1522 return false;
1523
Daniel Dunbard53bac72009-09-14 02:20:34 +00001524 // Float types are never treated as "integer like".
1525 if (Ty->isRealFloatingType())
1526 return false;
1527
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001528 // If this is a builtin or pointer type then it is ok.
John McCall9dd450b2009-09-21 23:43:11 +00001529 if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001530 return true;
1531
1532 // Complex types "should" be ok by the definition above, but they are not.
1533 if (Ty->isAnyComplexType())
1534 return false;
1535
1536 // Single element and zero sized arrays should be allowed, by the definition
1537 // above, but they are not.
1538
1539 // Otherwise, it must be a record type.
1540 const RecordType *RT = Ty->getAs<RecordType>();
1541 if (!RT) return false;
1542
1543 // Ignore records with flexible arrays.
1544 const RecordDecl *RD = RT->getDecl();
1545 if (RD->hasFlexibleArrayMember())
1546 return false;
1547
1548 // Check that all sub-fields are at offset 0, and are themselves "integer
1549 // like".
1550 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
1551
1552 bool HadField = false;
1553 unsigned idx = 0;
1554 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1555 i != e; ++i, ++idx) {
1556 const FieldDecl *FD = *i;
1557
1558 // Check if this field is at offset 0.
1559 uint64_t Offset = Layout.getFieldOffset(idx);
1560 if (Offset != 0) {
1561 // Allow padding bit-fields, but only if they are all at the end of the
1562 // structure (despite the wording above, this matches gcc).
1563 if (FD->isBitField() &&
1564 !FD->getBitWidth()->EvaluateAsInt(Context).getZExtValue()) {
1565 for (; i != e; ++i)
1566 if (!i->isBitField() ||
1567 i->getBitWidth()->EvaluateAsInt(Context).getZExtValue())
1568 return false;
1569
1570 // All remaining fields are padding, allow this.
1571 return true;
1572 }
1573
1574 return false;
1575 }
1576
1577 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
1578 return false;
1579
1580 // Only allow at most one field in a structure. Again this doesn't match the
1581 // wording above, but follows gcc.
1582 if (!RD->isUnion()) {
1583 if (HadField)
1584 return false;
1585
1586 HadField = true;
1587 }
1588 }
1589
1590 return true;
1591}
1592
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001593ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +00001594 ASTContext &Context,
1595 llvm::LLVMContext &VMContext) const {
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001596 if (RetTy->isVoidType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001597 return ABIArgInfo::getIgnore();
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001598
1599 if (!CodeGenFunction::hasAggregateLLVMType(RetTy))
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00001600 return (RetTy->isPromotableIntegerType() ?
1601 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001602
1603 // Are we following APCS?
1604 if (getABIKind() == APCS) {
1605 if (isEmptyRecord(Context, RetTy, false))
1606 return ABIArgInfo::getIgnore();
1607
1608 // Integer like structures are returned in r0.
1609 if (isIntegerLikeType(RetTy, Context, VMContext)) {
1610 // Return in the smallest viable integer type.
1611 uint64_t Size = Context.getTypeSize(RetTy);
1612 if (Size <= 8)
1613 return ABIArgInfo::getCoerce(llvm::Type::getInt8Ty(VMContext));
1614 if (Size <= 16)
1615 return ABIArgInfo::getCoerce(llvm::Type::getInt16Ty(VMContext));
1616 return ABIArgInfo::getCoerce(llvm::Type::getInt32Ty(VMContext));
1617 }
1618
1619 // Otherwise return in memory.
1620 return ABIArgInfo::getIndirect(0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001621 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001622
1623 // Otherwise this is an AAPCS variant.
1624
Daniel Dunbar1ce72512009-09-14 00:56:55 +00001625 if (isEmptyRecord(Context, RetTy, true))
1626 return ABIArgInfo::getIgnore();
1627
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001628 // Aggregates <= 4 bytes are returned in r0; other aggregates
1629 // are returned indirectly.
1630 uint64_t Size = Context.getTypeSize(RetTy);
Daniel Dunbar1ce72512009-09-14 00:56:55 +00001631 if (Size <= 32) {
1632 // Return in the smallest viable integer type.
1633 if (Size <= 8)
1634 return ABIArgInfo::getCoerce(llvm::Type::getInt8Ty(VMContext));
1635 if (Size <= 16)
1636 return ABIArgInfo::getCoerce(llvm::Type::getInt16Ty(VMContext));
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001637 return ABIArgInfo::getCoerce(llvm::Type::getInt32Ty(VMContext));
Daniel Dunbar1ce72512009-09-14 00:56:55 +00001638 }
1639
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001640 return ABIArgInfo::getIndirect(0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001641}
1642
1643llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1644 CodeGenFunction &CGF) const {
1645 // FIXME: Need to handle alignment
Benjamin Kramerabd5b902009-10-13 10:07:13 +00001646 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Owen Anderson9793f0e2009-07-29 22:16:19 +00001647 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001648
1649 CGBuilderTy &Builder = CGF.Builder;
1650 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
1651 "ap");
1652 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
1653 llvm::Type *PTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +00001654 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001655 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
1656
1657 uint64_t Offset =
1658 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
1659 llvm::Value *NextAddr =
Owen Anderson41a75022009-08-13 21:57:51 +00001660 Builder.CreateGEP(Addr, llvm::ConstantInt::get(
1661 llvm::Type::getInt32Ty(CGF.getLLVMContext()), Offset),
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001662 "ap.next");
1663 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
1664
1665 return AddrTyped;
1666}
1667
1668ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +00001669 ASTContext &Context,
1670 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001671 if (RetTy->isVoidType()) {
1672 return ABIArgInfo::getIgnore();
1673 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1674 return ABIArgInfo::getIndirect(0);
1675 } else {
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00001676 return (RetTy->isPromotableIntegerType() ?
1677 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001678 }
1679}
1680
Daniel Dunbard59655c2009-09-12 00:59:49 +00001681// SystemZ ABI Implementation
1682
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00001683namespace {
Daniel Dunbard59655c2009-09-12 00:59:49 +00001684
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00001685class SystemZABIInfo : public ABIInfo {
1686 bool isPromotableIntegerType(QualType Ty) const;
1687
1688 ABIArgInfo classifyReturnType(QualType RetTy, ASTContext &Context,
1689 llvm::LLVMContext &VMContext) const;
1690
1691 ABIArgInfo classifyArgumentType(QualType RetTy, ASTContext &Context,
1692 llvm::LLVMContext &VMContext) const;
1693
1694 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1695 llvm::LLVMContext &VMContext) const {
1696 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(),
1697 Context, VMContext);
1698 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1699 it != ie; ++it)
1700 it->info = classifyArgumentType(it->type, Context, VMContext);
1701 }
1702
1703 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1704 CodeGenFunction &CGF) const;
1705};
Daniel Dunbard59655c2009-09-12 00:59:49 +00001706
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00001707}
1708
1709bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const {
1710 // SystemZ ABI requires all 8, 16 and 32 bit quantities to be extended.
John McCall9dd450b2009-09-21 23:43:11 +00001711 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00001712 switch (BT->getKind()) {
1713 case BuiltinType::Bool:
1714 case BuiltinType::Char_S:
1715 case BuiltinType::Char_U:
1716 case BuiltinType::SChar:
1717 case BuiltinType::UChar:
1718 case BuiltinType::Short:
1719 case BuiltinType::UShort:
1720 case BuiltinType::Int:
1721 case BuiltinType::UInt:
1722 return true;
1723 default:
1724 return false;
1725 }
1726 return false;
1727}
1728
1729llvm::Value *SystemZABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1730 CodeGenFunction &CGF) const {
1731 // FIXME: Implement
1732 return 0;
1733}
1734
1735
1736ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy,
1737 ASTContext &Context,
Daniel Dunbard59655c2009-09-12 00:59:49 +00001738 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00001739 if (RetTy->isVoidType()) {
1740 return ABIArgInfo::getIgnore();
1741 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1742 return ABIArgInfo::getIndirect(0);
1743 } else {
1744 return (isPromotableIntegerType(RetTy) ?
1745 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1746 }
1747}
1748
1749ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty,
1750 ASTContext &Context,
Daniel Dunbard59655c2009-09-12 00:59:49 +00001751 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00001752 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
1753 return ABIArgInfo::getIndirect(0);
1754 } else {
1755 return (isPromotableIntegerType(Ty) ?
1756 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1757 }
1758}
1759
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001760ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty,
Owen Anderson170229f2009-07-14 23:10:40 +00001761 ASTContext &Context,
1762 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001763 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
1764 return ABIArgInfo::getIndirect(0);
1765 } else {
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00001766 return (Ty->isPromotableIntegerType() ?
1767 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001768 }
1769}
1770
1771llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1772 CodeGenFunction &CGF) const {
1773 return 0;
1774}
1775
1776const ABIInfo &CodeGenTypes::getABIInfo() const {
1777 if (TheABIInfo)
1778 return *TheABIInfo;
1779
Daniel Dunbare3532f82009-08-24 08:52:16 +00001780 // For now we just cache the ABIInfo in CodeGenTypes and don't free it.
1781
Daniel Dunbar40165182009-08-24 09:10:05 +00001782 const llvm::Triple &Triple(getContext().Target.getTriple());
1783 switch (Triple.getArch()) {
Daniel Dunbare3532f82009-08-24 08:52:16 +00001784 default:
1785 return *(TheABIInfo = new DefaultABIInfo);
1786
Daniel Dunbard59655c2009-09-12 00:59:49 +00001787 case llvm::Triple::arm:
1788 case llvm::Triple::thumb:
Daniel Dunbar020daa92009-09-12 01:00:39 +00001789 // FIXME: We want to know the float calling convention as well.
Daniel Dunbarb4091a92009-09-14 00:35:03 +00001790 if (strcmp(getContext().Target.getABI(), "apcs-gnu") == 0)
Daniel Dunbar020daa92009-09-12 01:00:39 +00001791 return *(TheABIInfo = new ARMABIInfo(ARMABIInfo::APCS));
1792
1793 return *(TheABIInfo = new ARMABIInfo(ARMABIInfo::AAPCS));
Daniel Dunbard59655c2009-09-12 00:59:49 +00001794
1795 case llvm::Triple::pic16:
1796 return *(TheABIInfo = new PIC16ABIInfo());
1797
1798 case llvm::Triple::systemz:
1799 return *(TheABIInfo = new SystemZABIInfo());
1800
Daniel Dunbar40165182009-08-24 09:10:05 +00001801 case llvm::Triple::x86:
Daniel Dunbar40165182009-08-24 09:10:05 +00001802 switch (Triple.getOS()) {
Edward O'Callaghan462e4ab2009-10-20 17:22:50 +00001803 case llvm::Triple::Darwin:
Daniel Dunbar710a80d2009-10-20 18:07:06 +00001804 return *(TheABIInfo = new X86_32ABIInfo(Context, true, true));
Daniel Dunbare3532f82009-08-24 08:52:16 +00001805 case llvm::Triple::Cygwin:
Daniel Dunbare3532f82009-08-24 08:52:16 +00001806 case llvm::Triple::MinGW32:
1807 case llvm::Triple::MinGW64:
Edward O'Callaghan437ec1e2009-10-21 11:58:24 +00001808 case llvm::Triple::AuroraUX:
1809 case llvm::Triple::DragonFly:
David Chisnall2c5bef22009-09-03 01:48:05 +00001810 case llvm::Triple::FreeBSD:
Daniel Dunbare3532f82009-08-24 08:52:16 +00001811 case llvm::Triple::OpenBSD:
1812 return *(TheABIInfo = new X86_32ABIInfo(Context, false, true));
1813
1814 default:
1815 return *(TheABIInfo = new X86_32ABIInfo(Context, false, false));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001816 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001817
Daniel Dunbare3532f82009-08-24 08:52:16 +00001818 case llvm::Triple::x86_64:
1819 return *(TheABIInfo = new X86_64ABIInfo());
Daniel Dunbare3532f82009-08-24 08:52:16 +00001820 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001821}