blob: ba0bc6668e271ed519ae7c4ed8d5ce6f506f97fd [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;
774
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;
785 unsigned idx = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000786 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
787 i != e; ++i, ++idx) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000788 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
789 bool BitField = i->isBitField();
790
791 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
792 // fields, it has class MEMORY.
793 //
794 // Note, skip this test for bit-fields, see below.
795 if (!BitField && Offset % Context.getTypeAlign(i->getType())) {
796 Lo = Memory;
797 return;
798 }
799
800 // Classify this field.
801 //
802 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
803 // exceeds a single eightbyte, each is classified
804 // separately. Each eightbyte gets initialized to class
805 // NO_CLASS.
806 Class FieldLo, FieldHi;
807
808 // Bit-fields require special handling, they do not force the
809 // structure to be passed in memory even if unaligned, and
810 // therefore they can straddle an eightbyte.
811 if (BitField) {
812 // Ignore padding bit-fields.
813 if (i->isUnnamedBitfield())
814 continue;
815
816 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
817 uint64_t Size = i->getBitWidth()->EvaluateAsInt(Context).getZExtValue();
818
819 uint64_t EB_Lo = Offset / 64;
820 uint64_t EB_Hi = (Offset + Size - 1) / 64;
821 FieldLo = FieldHi = NoClass;
822 if (EB_Lo) {
823 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
824 FieldLo = NoClass;
825 FieldHi = Integer;
826 } else {
827 FieldLo = Integer;
828 FieldHi = EB_Hi ? Integer : NoClass;
829 }
830 } else
831 classify(i->getType(), Context, Offset, FieldLo, FieldHi);
832 Lo = merge(Lo, FieldLo);
833 Hi = merge(Hi, FieldHi);
834 if (Lo == Memory || Hi == Memory)
835 break;
836 }
837
838 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
839 //
840 // (a) If one of the classes is MEMORY, the whole argument is
841 // passed in memory.
842 //
843 // (b) If SSEUP is not preceeded by SSE, it is converted to SSE.
844
845 // The first of these conditions is guaranteed by how we implement
846 // the merge (just bail).
847 //
848 // The second condition occurs in the case of unions; for example
849 // union { _Complex double; unsigned; }.
850 if (Hi == Memory)
851 Lo = Memory;
852 if (Hi == SSEUp && Lo != SSE)
853 Hi = SSE;
854 }
855}
856
857ABIArgInfo X86_64ABIInfo::getCoerceResult(QualType Ty,
858 const llvm::Type *CoerceTo,
859 ASTContext &Context) const {
Owen Anderson0032b272009-08-13 21:57:51 +0000860 if (CoerceTo == llvm::Type::getInt64Ty(CoerceTo->getContext())) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000861 // Integer and pointer types will end up in a general purpose
862 // register.
Anders Carlsson5b3a2fc2009-09-26 03:56:53 +0000863 if (Ty->isIntegralType() || Ty->hasPointerRepresentation())
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000864 return (Ty->isPromotableIntegerType() ?
865 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Owen Anderson0032b272009-08-13 21:57:51 +0000866 } else if (CoerceTo == llvm::Type::getDoubleTy(CoerceTo->getContext())) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000867 // FIXME: It would probably be better to make CGFunctionInfo only map using
868 // canonical types than to canonize here.
869 QualType CTy = Context.getCanonicalType(Ty);
870
871 // Float and double end up in a single SSE reg.
872 if (CTy == Context.FloatTy || CTy == Context.DoubleTy)
873 return ABIArgInfo::getDirect();
874
875 }
876
877 return ABIArgInfo::getCoerce(CoerceTo);
878}
879
880ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty,
881 ASTContext &Context) const {
882 // If this is a scalar LLVM value then assume LLVM will pass it in the right
883 // place naturally.
884 if (!CodeGenFunction::hasAggregateLLVMType(Ty))
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000885 return (Ty->isPromotableIntegerType() ?
886 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000887
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000888 bool ByVal = !isRecordWithNonTrivialDestructorOrCopyConstructor(Ty);
889
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000890 // FIXME: Set alignment correctly.
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000891 return ABIArgInfo::getIndirect(0, ByVal);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000892}
893
894ABIArgInfo X86_64ABIInfo::classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000895 ASTContext &Context,
896 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000897 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
898 // classification algorithm.
899 X86_64ABIInfo::Class Lo, Hi;
900 classify(RetTy, Context, 0, Lo, Hi);
901
902 // Check some invariants.
903 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
904 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
905 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
906
907 const llvm::Type *ResType = 0;
908 switch (Lo) {
909 case NoClass:
910 return ABIArgInfo::getIgnore();
911
912 case SSEUp:
913 case X87Up:
914 assert(0 && "Invalid classification for lo word.");
915
916 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
917 // hidden argument.
918 case Memory:
919 return getIndirectResult(RetTy, Context);
920
921 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
922 // available register of the sequence %rax, %rdx is used.
923 case Integer:
Owen Anderson0032b272009-08-13 21:57:51 +0000924 ResType = llvm::Type::getInt64Ty(VMContext); break;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000925
926 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
927 // available SSE register of the sequence %xmm0, %xmm1 is used.
928 case SSE:
Owen Anderson0032b272009-08-13 21:57:51 +0000929 ResType = llvm::Type::getDoubleTy(VMContext); break;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000930
931 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
932 // returned on the X87 stack in %st0 as 80-bit x87 number.
933 case X87:
Owen Anderson0032b272009-08-13 21:57:51 +0000934 ResType = llvm::Type::getX86_FP80Ty(VMContext); break;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000935
936 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
937 // part of the value is returned in %st0 and the imaginary part in
938 // %st1.
939 case ComplexX87:
940 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Owen Anderson0032b272009-08-13 21:57:51 +0000941 ResType = llvm::StructType::get(VMContext, llvm::Type::getX86_FP80Ty(VMContext),
942 llvm::Type::getX86_FP80Ty(VMContext),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000943 NULL);
944 break;
945 }
946
947 switch (Hi) {
948 // Memory was handled previously and X87 should
949 // never occur as a hi class.
950 case Memory:
951 case X87:
952 assert(0 && "Invalid classification for hi word.");
953
954 case ComplexX87: // Previously handled.
955 case NoClass: break;
956
957 case Integer:
Owen Anderson47a434f2009-08-05 23:18:46 +0000958 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson0032b272009-08-13 21:57:51 +0000959 llvm::Type::getInt64Ty(VMContext), NULL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000960 break;
961 case SSE:
Owen Anderson47a434f2009-08-05 23:18:46 +0000962 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson0032b272009-08-13 21:57:51 +0000963 llvm::Type::getDoubleTy(VMContext), NULL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000964 break;
965
966 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
967 // is passed in the upper half of the last used SSE register.
968 //
969 // SSEUP should always be preceeded by SSE, just widen.
970 case SSEUp:
971 assert(Lo == SSE && "Unexpected SSEUp classification.");
Owen Anderson0032b272009-08-13 21:57:51 +0000972 ResType = llvm::VectorType::get(llvm::Type::getDoubleTy(VMContext), 2);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000973 break;
974
975 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
976 // returned together with the previous X87 value in %st0.
977 case X87Up:
978 // If X87Up is preceeded by X87, we don't need to do
979 // anything. However, in some cases with unions it may not be
980 // preceeded by X87. In such situations we follow gcc and pass the
981 // extra bits in an SSE reg.
982 if (Lo != X87)
Owen Anderson47a434f2009-08-05 23:18:46 +0000983 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson0032b272009-08-13 21:57:51 +0000984 llvm::Type::getDoubleTy(VMContext), NULL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000985 break;
986 }
987
988 return getCoerceResult(RetTy, ResType, Context);
989}
990
991ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, ASTContext &Context,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000992 llvm::LLVMContext &VMContext,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000993 unsigned &neededInt,
994 unsigned &neededSSE) const {
995 X86_64ABIInfo::Class Lo, Hi;
996 classify(Ty, Context, 0, Lo, Hi);
997
998 // Check some invariants.
999 // FIXME: Enforce these by construction.
1000 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
1001 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
1002 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1003
1004 neededInt = 0;
1005 neededSSE = 0;
1006 const llvm::Type *ResType = 0;
1007 switch (Lo) {
1008 case NoClass:
1009 return ABIArgInfo::getIgnore();
1010
1011 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
1012 // on the stack.
1013 case Memory:
1014
1015 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
1016 // COMPLEX_X87, it is passed in memory.
1017 case X87:
1018 case ComplexX87:
1019 return getIndirectResult(Ty, Context);
1020
1021 case SSEUp:
1022 case X87Up:
1023 assert(0 && "Invalid classification for lo word.");
1024
1025 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
1026 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
1027 // and %r9 is used.
1028 case Integer:
1029 ++neededInt;
Owen Anderson0032b272009-08-13 21:57:51 +00001030 ResType = llvm::Type::getInt64Ty(VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001031 break;
1032
1033 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
1034 // available SSE register is used, the registers are taken in the
1035 // order from %xmm0 to %xmm7.
1036 case SSE:
1037 ++neededSSE;
Owen Anderson0032b272009-08-13 21:57:51 +00001038 ResType = llvm::Type::getDoubleTy(VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001039 break;
1040 }
1041
1042 switch (Hi) {
1043 // Memory was handled previously, ComplexX87 and X87 should
1044 // never occur as hi classes, and X87Up must be preceed by X87,
1045 // which is passed in memory.
1046 case Memory:
1047 case X87:
1048 case ComplexX87:
1049 assert(0 && "Invalid classification for hi word.");
1050 break;
1051
1052 case NoClass: break;
1053 case Integer:
Owen Anderson47a434f2009-08-05 23:18:46 +00001054 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson0032b272009-08-13 21:57:51 +00001055 llvm::Type::getInt64Ty(VMContext), NULL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001056 ++neededInt;
1057 break;
1058
1059 // X87Up generally doesn't occur here (long double is passed in
1060 // memory), except in situations involving unions.
1061 case X87Up:
1062 case SSE:
Owen Anderson47a434f2009-08-05 23:18:46 +00001063 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson0032b272009-08-13 21:57:51 +00001064 llvm::Type::getDoubleTy(VMContext), NULL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001065 ++neededSSE;
1066 break;
1067
1068 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
1069 // eightbyte is passed in the upper half of the last used SSE
1070 // register.
1071 case SSEUp:
1072 assert(Lo == SSE && "Unexpected SSEUp classification.");
Owen Anderson0032b272009-08-13 21:57:51 +00001073 ResType = llvm::VectorType::get(llvm::Type::getDoubleTy(VMContext), 2);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001074 break;
1075 }
1076
1077 return getCoerceResult(Ty, ResType, Context);
1078}
1079
Owen Andersona1cf15f2009-07-14 23:10:40 +00001080void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1081 llvm::LLVMContext &VMContext) const {
1082 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(),
1083 Context, VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001084
1085 // Keep track of the number of assigned registers.
1086 unsigned freeIntRegs = 6, freeSSERegs = 8;
1087
1088 // If the return value is indirect, then the hidden argument is consuming one
1089 // integer register.
1090 if (FI.getReturnInfo().isIndirect())
1091 --freeIntRegs;
1092
1093 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
1094 // get assigned (in left-to-right order) for passing as follows...
1095 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1096 it != ie; ++it) {
1097 unsigned neededInt, neededSSE;
Mike Stump1eb44332009-09-09 15:08:12 +00001098 it->info = classifyArgumentType(it->type, Context, VMContext,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001099 neededInt, neededSSE);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001100
1101 // AMD64-ABI 3.2.3p3: If there are no registers available for any
1102 // eightbyte of an argument, the whole argument is passed on the
1103 // stack. If registers have already been assigned for some
1104 // eightbytes of such an argument, the assignments get reverted.
1105 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
1106 freeIntRegs -= neededInt;
1107 freeSSERegs -= neededSSE;
1108 } else {
1109 it->info = getIndirectResult(it->type, Context);
1110 }
1111 }
1112}
1113
1114static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
1115 QualType Ty,
1116 CodeGenFunction &CGF) {
1117 llvm::Value *overflow_arg_area_p =
1118 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
1119 llvm::Value *overflow_arg_area =
1120 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
1121
1122 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
1123 // byte boundary if alignment needed by type exceeds 8 byte boundary.
1124 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
1125 if (Align > 8) {
1126 // Note that we follow the ABI & gcc here, even though the type
1127 // could in theory have an alignment greater than 16. This case
1128 // shouldn't ever matter in practice.
1129
1130 // overflow_arg_area = (overflow_arg_area + 15) & ~15;
Owen Anderson0032b272009-08-13 21:57:51 +00001131 llvm::Value *Offset =
1132 llvm::ConstantInt::get(llvm::Type::getInt32Ty(CGF.getLLVMContext()), 15);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001133 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
1134 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
Owen Anderson0032b272009-08-13 21:57:51 +00001135 llvm::Type::getInt64Ty(CGF.getLLVMContext()));
1136 llvm::Value *Mask = llvm::ConstantInt::get(
1137 llvm::Type::getInt64Ty(CGF.getLLVMContext()), ~15LL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001138 overflow_arg_area =
1139 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
1140 overflow_arg_area->getType(),
1141 "overflow_arg_area.align");
1142 }
1143
1144 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
1145 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1146 llvm::Value *Res =
1147 CGF.Builder.CreateBitCast(overflow_arg_area,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001148 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001149
1150 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
1151 // l->overflow_arg_area + sizeof(type).
1152 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
1153 // an 8 byte boundary.
1154
1155 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
Owen Anderson0032b272009-08-13 21:57:51 +00001156 llvm::Value *Offset =
1157 llvm::ConstantInt::get(llvm::Type::getInt32Ty(CGF.getLLVMContext()),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001158 (SizeInBytes + 7) & ~7);
1159 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
1160 "overflow_arg_area.next");
1161 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
1162
1163 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
1164 return Res;
1165}
1166
1167llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1168 CodeGenFunction &CGF) const {
Owen Andersona1cf15f2009-07-14 23:10:40 +00001169 llvm::LLVMContext &VMContext = CGF.getLLVMContext();
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001170 const llvm::Type *i32Ty = llvm::Type::getInt32Ty(VMContext);
1171 const llvm::Type *DoubleTy = llvm::Type::getDoubleTy(VMContext);
Mike Stump1eb44332009-09-09 15:08:12 +00001172
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001173 // Assume that va_list type is correct; should be pointer to LLVM type:
1174 // struct {
1175 // i32 gp_offset;
1176 // i32 fp_offset;
1177 // i8* overflow_arg_area;
1178 // i8* reg_save_area;
1179 // };
1180 unsigned neededInt, neededSSE;
Owen Andersona1cf15f2009-07-14 23:10:40 +00001181 ABIArgInfo AI = classifyArgumentType(Ty, CGF.getContext(), VMContext,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001182 neededInt, neededSSE);
1183
1184 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
1185 // in the registers. If not go to step 7.
1186 if (!neededInt && !neededSSE)
1187 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1188
1189 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
1190 // general purpose registers needed to pass type and num_fp to hold
1191 // the number of floating point registers needed.
1192
1193 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
1194 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
1195 // l->fp_offset > 304 - num_fp * 16 go to step 7.
1196 //
1197 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
1198 // register save space).
1199
1200 llvm::Value *InRegs = 0;
1201 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
1202 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
1203 if (neededInt) {
1204 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
1205 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
1206 InRegs =
1207 CGF.Builder.CreateICmpULE(gp_offset,
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001208 llvm::ConstantInt::get(i32Ty,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001209 48 - neededInt * 8),
1210 "fits_in_gp");
1211 }
1212
1213 if (neededSSE) {
1214 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
1215 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
1216 llvm::Value *FitsInFP =
1217 CGF.Builder.CreateICmpULE(fp_offset,
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001218 llvm::ConstantInt::get(i32Ty,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001219 176 - neededSSE * 16),
1220 "fits_in_fp");
1221 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
1222 }
1223
1224 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
1225 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
1226 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
1227 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
1228
1229 // Emit code to load the value if it was passed in registers.
1230
1231 CGF.EmitBlock(InRegBlock);
1232
1233 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
1234 // an offset of l->gp_offset and/or l->fp_offset. This may require
1235 // copying to a temporary location in case the parameter is passed
1236 // in different register classes or requires an alignment greater
1237 // than 8 for general purpose registers and 16 for XMM registers.
1238 //
1239 // FIXME: This really results in shameful code when we end up needing to
1240 // collect arguments from different places; often what should result in a
1241 // simple assembling of a structure from scattered addresses has many more
1242 // loads than necessary. Can we clean this up?
1243 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1244 llvm::Value *RegAddr =
1245 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
1246 "reg_save_area");
1247 if (neededInt && neededSSE) {
1248 // FIXME: Cleanup.
1249 assert(AI.isCoerce() && "Unexpected ABI info for mixed regs");
1250 const llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
1251 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
1252 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
1253 const llvm::Type *TyLo = ST->getElementType(0);
1254 const llvm::Type *TyHi = ST->getElementType(1);
1255 assert((TyLo->isFloatingPoint() ^ TyHi->isFloatingPoint()) &&
1256 "Unexpected ABI info for mixed regs");
Owen Anderson96e0fc72009-07-29 22:16:19 +00001257 const llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
1258 const llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001259 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1260 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1261 llvm::Value *RegLoAddr = TyLo->isFloatingPoint() ? FPAddr : GPAddr;
1262 llvm::Value *RegHiAddr = TyLo->isFloatingPoint() ? GPAddr : FPAddr;
1263 llvm::Value *V =
1264 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
1265 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1266 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
1267 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1268
Owen Andersona1cf15f2009-07-14 23:10:40 +00001269 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001270 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001271 } else if (neededInt) {
1272 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1273 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001274 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001275 } else {
1276 if (neededSSE == 1) {
1277 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1278 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001279 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001280 } else {
1281 assert(neededSSE == 2 && "Invalid number of needed registers!");
1282 // SSE registers are spaced 16 bytes apart in the register save
1283 // area, we need to collect the two eightbytes together.
1284 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1285 llvm::Value *RegAddrHi =
1286 CGF.Builder.CreateGEP(RegAddrLo,
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001287 llvm::ConstantInt::get(i32Ty, 16));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001288 const llvm::Type *DblPtrTy =
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001289 llvm::PointerType::getUnqual(DoubleTy);
1290 const llvm::StructType *ST = llvm::StructType::get(VMContext, DoubleTy,
1291 DoubleTy, NULL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001292 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
1293 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
1294 DblPtrTy));
1295 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1296 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
1297 DblPtrTy));
1298 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1299 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001300 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001301 }
1302 }
1303
1304 // AMD64-ABI 3.5.7p5: Step 5. Set:
1305 // l->gp_offset = l->gp_offset + num_gp * 8
1306 // l->fp_offset = l->fp_offset + num_fp * 16.
1307 if (neededInt) {
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001308 llvm::Value *Offset = llvm::ConstantInt::get(i32Ty, neededInt * 8);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001309 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
1310 gp_offset_p);
1311 }
1312 if (neededSSE) {
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001313 llvm::Value *Offset = llvm::ConstantInt::get(i32Ty, neededSSE * 16);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001314 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
1315 fp_offset_p);
1316 }
1317 CGF.EmitBranch(ContBlock);
1318
1319 // Emit code to load the value if it was passed in memory.
1320
1321 CGF.EmitBlock(InMemBlock);
1322 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1323
1324 // Return the appropriate result.
1325
1326 CGF.EmitBlock(ContBlock);
1327 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(),
1328 "vaarg.addr");
1329 ResAddr->reserveOperandSpace(2);
1330 ResAddr->addIncoming(RegAddr, InRegBlock);
1331 ResAddr->addIncoming(MemAddr, InMemBlock);
1332
1333 return ResAddr;
1334}
1335
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001336// PIC16 ABI Implementation
1337
1338namespace {
1339
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001340class PIC16ABIInfo : public ABIInfo {
1341 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001342 ASTContext &Context,
1343 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001344
1345 ABIArgInfo classifyArgumentType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001346 ASTContext &Context,
1347 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001348
Owen Andersona1cf15f2009-07-14 23:10:40 +00001349 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1350 llvm::LLVMContext &VMContext) const {
1351 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context,
1352 VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001353 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1354 it != ie; ++it)
Owen Andersona1cf15f2009-07-14 23:10:40 +00001355 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001356 }
1357
1358 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1359 CodeGenFunction &CGF) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001360};
1361
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001362}
1363
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001364ABIArgInfo PIC16ABIInfo::classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001365 ASTContext &Context,
1366 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001367 if (RetTy->isVoidType()) {
1368 return ABIArgInfo::getIgnore();
1369 } else {
1370 return ABIArgInfo::getDirect();
1371 }
1372}
1373
1374ABIArgInfo PIC16ABIInfo::classifyArgumentType(QualType Ty,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001375 ASTContext &Context,
1376 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001377 return ABIArgInfo::getDirect();
1378}
1379
1380llvm::Value *PIC16ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1381 CodeGenFunction &CGF) const {
1382 return 0;
1383}
1384
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001385// ARM ABI Implementation
1386
1387namespace {
1388
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001389class ARMABIInfo : public ABIInfo {
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00001390public:
1391 enum ABIKind {
1392 APCS = 0,
1393 AAPCS = 1,
1394 AAPCS_VFP
1395 };
1396
1397private:
1398 ABIKind Kind;
1399
1400public:
1401 ARMABIInfo(ABIKind _Kind) : Kind(_Kind) {}
1402
1403private:
1404 ABIKind getABIKind() const { return Kind; }
1405
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001406 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001407 ASTContext &Context,
1408 llvm::LLVMContext &VMCOntext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001409
1410 ABIArgInfo classifyArgumentType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001411 ASTContext &Context,
1412 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001413
Owen Andersona1cf15f2009-07-14 23:10:40 +00001414 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1415 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001416
1417 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1418 CodeGenFunction &CGF) const;
1419};
1420
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001421}
1422
Owen Andersona1cf15f2009-07-14 23:10:40 +00001423void ARMABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1424 llvm::LLVMContext &VMContext) const {
Mike Stump1eb44332009-09-09 15:08:12 +00001425 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001426 VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001427 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1428 it != ie; ++it) {
Owen Andersona1cf15f2009-07-14 23:10:40 +00001429 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001430 }
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00001431
1432 // ARM always overrides the calling convention.
1433 switch (getABIKind()) {
1434 case APCS:
1435 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_APCS);
1436 break;
1437
1438 case AAPCS:
1439 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS);
1440 break;
1441
1442 case AAPCS_VFP:
1443 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS_VFP);
1444 break;
1445 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001446}
1447
1448ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001449 ASTContext &Context,
1450 llvm::LLVMContext &VMContext) const {
Daniel Dunbar98303b92009-09-13 08:03:58 +00001451 if (!CodeGenFunction::hasAggregateLLVMType(Ty))
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001452 return (Ty->isPromotableIntegerType() ?
1453 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Daniel Dunbar98303b92009-09-13 08:03:58 +00001454
Daniel Dunbar42025572009-09-14 21:54:03 +00001455 // Ignore empty records.
1456 if (isEmptyRecord(Context, Ty, true))
1457 return ABIArgInfo::getIgnore();
1458
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001459 // FIXME: This is kind of nasty... but there isn't much choice because the ARM
1460 // backend doesn't support byval.
1461 // FIXME: This doesn't handle alignment > 64 bits.
1462 const llvm::Type* ElemTy;
1463 unsigned SizeRegs;
1464 if (Context.getTypeAlign(Ty) > 32) {
Owen Anderson0032b272009-08-13 21:57:51 +00001465 ElemTy = llvm::Type::getInt64Ty(VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001466 SizeRegs = (Context.getTypeSize(Ty) + 63) / 64;
1467 } else {
Owen Anderson0032b272009-08-13 21:57:51 +00001468 ElemTy = llvm::Type::getInt32Ty(VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001469 SizeRegs = (Context.getTypeSize(Ty) + 31) / 32;
1470 }
1471 std::vector<const llvm::Type*> LLVMFields;
Owen Anderson96e0fc72009-07-29 22:16:19 +00001472 LLVMFields.push_back(llvm::ArrayType::get(ElemTy, SizeRegs));
Owen Anderson47a434f2009-08-05 23:18:46 +00001473 const llvm::Type* STy = llvm::StructType::get(VMContext, LLVMFields, true);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001474 return ABIArgInfo::getCoerce(STy);
1475}
1476
Daniel Dunbar98303b92009-09-13 08:03:58 +00001477static bool isIntegerLikeType(QualType Ty,
1478 ASTContext &Context,
1479 llvm::LLVMContext &VMContext) {
1480 // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
1481 // is called integer-like if its size is less than or equal to one word, and
1482 // the offset of each of its addressable sub-fields is zero.
1483
1484 uint64_t Size = Context.getTypeSize(Ty);
1485
1486 // Check that the type fits in a word.
1487 if (Size > 32)
1488 return false;
1489
1490 // FIXME: Handle vector types!
1491 if (Ty->isVectorType())
1492 return false;
1493
Daniel Dunbarb0d58192009-09-14 02:20:34 +00001494 // Float types are never treated as "integer like".
1495 if (Ty->isRealFloatingType())
1496 return false;
1497
Daniel Dunbar98303b92009-09-13 08:03:58 +00001498 // If this is a builtin or pointer type then it is ok.
John McCall183700f2009-09-21 23:43:11 +00001499 if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
Daniel Dunbar98303b92009-09-13 08:03:58 +00001500 return true;
1501
1502 // Complex types "should" be ok by the definition above, but they are not.
1503 if (Ty->isAnyComplexType())
1504 return false;
1505
1506 // Single element and zero sized arrays should be allowed, by the definition
1507 // above, but they are not.
1508
1509 // Otherwise, it must be a record type.
1510 const RecordType *RT = Ty->getAs<RecordType>();
1511 if (!RT) return false;
1512
1513 // Ignore records with flexible arrays.
1514 const RecordDecl *RD = RT->getDecl();
1515 if (RD->hasFlexibleArrayMember())
1516 return false;
1517
1518 // Check that all sub-fields are at offset 0, and are themselves "integer
1519 // like".
1520 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
1521
1522 bool HadField = false;
1523 unsigned idx = 0;
1524 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1525 i != e; ++i, ++idx) {
1526 const FieldDecl *FD = *i;
1527
1528 // Check if this field is at offset 0.
1529 uint64_t Offset = Layout.getFieldOffset(idx);
1530 if (Offset != 0) {
1531 // Allow padding bit-fields, but only if they are all at the end of the
1532 // structure (despite the wording above, this matches gcc).
1533 if (FD->isBitField() &&
1534 !FD->getBitWidth()->EvaluateAsInt(Context).getZExtValue()) {
1535 for (; i != e; ++i)
1536 if (!i->isBitField() ||
1537 i->getBitWidth()->EvaluateAsInt(Context).getZExtValue())
1538 return false;
1539
1540 // All remaining fields are padding, allow this.
1541 return true;
1542 }
1543
1544 return false;
1545 }
1546
1547 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
1548 return false;
1549
1550 // Only allow at most one field in a structure. Again this doesn't match the
1551 // wording above, but follows gcc.
1552 if (!RD->isUnion()) {
1553 if (HadField)
1554 return false;
1555
1556 HadField = true;
1557 }
1558 }
1559
1560 return true;
1561}
1562
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001563ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001564 ASTContext &Context,
1565 llvm::LLVMContext &VMContext) const {
Daniel Dunbar98303b92009-09-13 08:03:58 +00001566 if (RetTy->isVoidType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001567 return ABIArgInfo::getIgnore();
Daniel Dunbar98303b92009-09-13 08:03:58 +00001568
1569 if (!CodeGenFunction::hasAggregateLLVMType(RetTy))
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001570 return (RetTy->isPromotableIntegerType() ?
1571 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Daniel Dunbar98303b92009-09-13 08:03:58 +00001572
1573 // Are we following APCS?
1574 if (getABIKind() == APCS) {
1575 if (isEmptyRecord(Context, RetTy, false))
1576 return ABIArgInfo::getIgnore();
1577
1578 // Integer like structures are returned in r0.
1579 if (isIntegerLikeType(RetTy, Context, VMContext)) {
1580 // Return in the smallest viable integer type.
1581 uint64_t Size = Context.getTypeSize(RetTy);
1582 if (Size <= 8)
1583 return ABIArgInfo::getCoerce(llvm::Type::getInt8Ty(VMContext));
1584 if (Size <= 16)
1585 return ABIArgInfo::getCoerce(llvm::Type::getInt16Ty(VMContext));
1586 return ABIArgInfo::getCoerce(llvm::Type::getInt32Ty(VMContext));
1587 }
1588
1589 // Otherwise return in memory.
1590 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001591 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00001592
1593 // Otherwise this is an AAPCS variant.
1594
Daniel Dunbar16a08082009-09-14 00:56:55 +00001595 if (isEmptyRecord(Context, RetTy, true))
1596 return ABIArgInfo::getIgnore();
1597
Daniel Dunbar98303b92009-09-13 08:03:58 +00001598 // Aggregates <= 4 bytes are returned in r0; other aggregates
1599 // are returned indirectly.
1600 uint64_t Size = Context.getTypeSize(RetTy);
Daniel Dunbar16a08082009-09-14 00:56:55 +00001601 if (Size <= 32) {
1602 // Return in the smallest viable integer type.
1603 if (Size <= 8)
1604 return ABIArgInfo::getCoerce(llvm::Type::getInt8Ty(VMContext));
1605 if (Size <= 16)
1606 return ABIArgInfo::getCoerce(llvm::Type::getInt16Ty(VMContext));
Daniel Dunbar98303b92009-09-13 08:03:58 +00001607 return ABIArgInfo::getCoerce(llvm::Type::getInt32Ty(VMContext));
Daniel Dunbar16a08082009-09-14 00:56:55 +00001608 }
1609
Daniel Dunbar98303b92009-09-13 08:03:58 +00001610 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001611}
1612
1613llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1614 CodeGenFunction &CGF) const {
1615 // FIXME: Need to handle alignment
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00001616 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Owen Anderson96e0fc72009-07-29 22:16:19 +00001617 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001618
1619 CGBuilderTy &Builder = CGF.Builder;
1620 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
1621 "ap");
1622 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
1623 llvm::Type *PTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00001624 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001625 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
1626
1627 uint64_t Offset =
1628 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
1629 llvm::Value *NextAddr =
Owen Anderson0032b272009-08-13 21:57:51 +00001630 Builder.CreateGEP(Addr, llvm::ConstantInt::get(
1631 llvm::Type::getInt32Ty(CGF.getLLVMContext()), Offset),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001632 "ap.next");
1633 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
1634
1635 return AddrTyped;
1636}
1637
1638ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001639 ASTContext &Context,
1640 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001641 if (RetTy->isVoidType()) {
1642 return ABIArgInfo::getIgnore();
1643 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1644 return ABIArgInfo::getIndirect(0);
1645 } else {
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001646 return (RetTy->isPromotableIntegerType() ?
1647 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001648 }
1649}
1650
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001651// SystemZ ABI Implementation
1652
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00001653namespace {
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001654
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00001655class SystemZABIInfo : public ABIInfo {
1656 bool isPromotableIntegerType(QualType Ty) const;
1657
1658 ABIArgInfo classifyReturnType(QualType RetTy, ASTContext &Context,
1659 llvm::LLVMContext &VMContext) const;
1660
1661 ABIArgInfo classifyArgumentType(QualType RetTy, ASTContext &Context,
1662 llvm::LLVMContext &VMContext) const;
1663
1664 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1665 llvm::LLVMContext &VMContext) const {
1666 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(),
1667 Context, VMContext);
1668 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1669 it != ie; ++it)
1670 it->info = classifyArgumentType(it->type, Context, VMContext);
1671 }
1672
1673 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1674 CodeGenFunction &CGF) const;
1675};
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001676
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00001677}
1678
1679bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const {
1680 // SystemZ ABI requires all 8, 16 and 32 bit quantities to be extended.
John McCall183700f2009-09-21 23:43:11 +00001681 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00001682 switch (BT->getKind()) {
1683 case BuiltinType::Bool:
1684 case BuiltinType::Char_S:
1685 case BuiltinType::Char_U:
1686 case BuiltinType::SChar:
1687 case BuiltinType::UChar:
1688 case BuiltinType::Short:
1689 case BuiltinType::UShort:
1690 case BuiltinType::Int:
1691 case BuiltinType::UInt:
1692 return true;
1693 default:
1694 return false;
1695 }
1696 return false;
1697}
1698
1699llvm::Value *SystemZABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1700 CodeGenFunction &CGF) const {
1701 // FIXME: Implement
1702 return 0;
1703}
1704
1705
1706ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy,
1707 ASTContext &Context,
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001708 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00001709 if (RetTy->isVoidType()) {
1710 return ABIArgInfo::getIgnore();
1711 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1712 return ABIArgInfo::getIndirect(0);
1713 } else {
1714 return (isPromotableIntegerType(RetTy) ?
1715 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1716 }
1717}
1718
1719ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty,
1720 ASTContext &Context,
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001721 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00001722 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
1723 return ABIArgInfo::getIndirect(0);
1724 } else {
1725 return (isPromotableIntegerType(Ty) ?
1726 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1727 }
1728}
1729
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001730ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001731 ASTContext &Context,
1732 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001733 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
1734 return ABIArgInfo::getIndirect(0);
1735 } else {
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001736 return (Ty->isPromotableIntegerType() ?
1737 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001738 }
1739}
1740
1741llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1742 CodeGenFunction &CGF) const {
1743 return 0;
1744}
1745
1746const ABIInfo &CodeGenTypes::getABIInfo() const {
1747 if (TheABIInfo)
1748 return *TheABIInfo;
1749
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00001750 // For now we just cache the ABIInfo in CodeGenTypes and don't free it.
1751
Daniel Dunbar1752ee42009-08-24 09:10:05 +00001752 const llvm::Triple &Triple(getContext().Target.getTriple());
1753 switch (Triple.getArch()) {
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00001754 default:
1755 return *(TheABIInfo = new DefaultABIInfo);
1756
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001757 case llvm::Triple::arm:
1758 case llvm::Triple::thumb:
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00001759 // FIXME: We want to know the float calling convention as well.
Daniel Dunbar018ba5a2009-09-14 00:35:03 +00001760 if (strcmp(getContext().Target.getABI(), "apcs-gnu") == 0)
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00001761 return *(TheABIInfo = new ARMABIInfo(ARMABIInfo::APCS));
1762
1763 return *(TheABIInfo = new ARMABIInfo(ARMABIInfo::AAPCS));
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001764
1765 case llvm::Triple::pic16:
1766 return *(TheABIInfo = new PIC16ABIInfo());
1767
1768 case llvm::Triple::systemz:
1769 return *(TheABIInfo = new SystemZABIInfo());
1770
Daniel Dunbar1752ee42009-08-24 09:10:05 +00001771 case llvm::Triple::x86:
Daniel Dunbar1752ee42009-08-24 09:10:05 +00001772 switch (Triple.getOS()) {
Edward O'Callaghan7ee68bd2009-10-20 17:22:50 +00001773 case llvm::Triple::Darwin:
Daniel Dunbar2b6c7312009-10-20 18:07:06 +00001774 return *(TheABIInfo = new X86_32ABIInfo(Context, true, true));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00001775 case llvm::Triple::Cygwin:
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00001776 case llvm::Triple::MinGW32:
1777 case llvm::Triple::MinGW64:
Edward O'Callaghan727e2682009-10-21 11:58:24 +00001778 case llvm::Triple::AuroraUX:
1779 case llvm::Triple::DragonFly:
David Chisnall75c135a2009-09-03 01:48:05 +00001780 case llvm::Triple::FreeBSD:
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00001781 case llvm::Triple::OpenBSD:
1782 return *(TheABIInfo = new X86_32ABIInfo(Context, false, true));
1783
1784 default:
1785 return *(TheABIInfo = new X86_32ABIInfo(Context, false, false));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001786 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001787
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00001788 case llvm::Triple::x86_64:
1789 return *(TheABIInfo = new X86_64ABIInfo());
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00001790 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001791}