blob: 6d95adad86a9c4af0917a4c796ad56d5401c9b5a [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"
Torok Edwindb714922009-08-24 13:25:12 +000020#include <cstdio>
Anton Korobeynikov244360d2009-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 Korobeynikov18adbf52009-06-06 09:36:29 +000033 case Extend:
34 fprintf(stderr, "Extend");
35 break;
Anton Korobeynikov244360d2009-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 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
89/// isSingleElementStruct - Determine if a structure is a "single
90/// element struct", i.e. it has exactly one non-empty field or
91/// exactly one field which is itself a single element
92/// struct. Structures with flexible array members are never
93/// considered single element structs.
94///
95/// \return The field declaration for the single non-empty field, if
96/// it exists.
97static const Type *isSingleElementStruct(QualType T, ASTContext &Context) {
98 const RecordType *RT = T->getAsStructureType();
99 if (!RT)
100 return 0;
101
102 const RecordDecl *RD = RT->getDecl();
103 if (RD->hasFlexibleArrayMember())
104 return 0;
105
106 const Type *Found = 0;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000107 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
108 i != e; ++i) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000109 const FieldDecl *FD = *i;
110 QualType FT = FD->getType();
111
112 // Ignore empty fields.
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000113 if (isEmptyField(Context, FD, true))
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000114 continue;
115
116 // If we already found an element then this isn't a single-element
117 // struct.
118 if (Found)
119 return 0;
120
121 // Treat single element arrays as the element.
122 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
123 if (AT->getSize().getZExtValue() != 1)
124 break;
125 FT = AT->getElementType();
126 }
127
128 if (!CodeGenFunction::hasAggregateLLVMType(FT)) {
129 Found = FT.getTypePtr();
130 } else {
131 Found = isSingleElementStruct(FT, Context);
132 if (!Found)
133 return 0;
134 }
135 }
136
137 return Found;
138}
139
140static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
141 if (!Ty->getAsBuiltinType() && !Ty->isPointerType())
142 return false;
143
144 uint64_t Size = Context.getTypeSize(Ty);
145 return Size == 32 || Size == 64;
146}
147
148static bool areAllFields32Or64BitBasicType(const RecordDecl *RD,
149 ASTContext &Context) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000150 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
151 i != e; ++i) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000152 const FieldDecl *FD = *i;
153
154 if (!is32Or64BitBasicType(FD->getType(), Context))
155 return false;
156
157 // FIXME: Reject bit-fields wholesale; there are two problems, we don't know
158 // how to expand them yet, and the predicate for telling if a bitfield still
159 // counts as "basic" is more complicated than what we were doing previously.
160 if (FD->isBitField())
161 return false;
162 }
163
164 return true;
165}
166
Eli Friedman3192cc82009-06-13 21:37:10 +0000167static bool typeContainsSSEVector(const RecordDecl *RD, ASTContext &Context) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000168 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
169 i != e; ++i) {
Eli Friedman3192cc82009-06-13 21:37:10 +0000170 const FieldDecl *FD = *i;
171
172 if (FD->getType()->isVectorType() &&
173 Context.getTypeSize(FD->getType()) >= 128)
174 return true;
175
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000176 if (const RecordType* RT = FD->getType()->getAs<RecordType>())
Eli Friedman3192cc82009-06-13 21:37:10 +0000177 if (typeContainsSSEVector(RT->getDecl(), Context))
178 return true;
179 }
180
181 return false;
182}
183
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000184namespace {
185/// DefaultABIInfo - The default implementation for ABI specific
186/// details. This implementation provides information which results in
187/// self-consistent and sensible LLVM IR generation, but does not
188/// conform to any particular ABI.
189class DefaultABIInfo : public ABIInfo {
190 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +0000191 ASTContext &Context,
192 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000193
194 ABIArgInfo classifyArgumentType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +0000195 ASTContext &Context,
196 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000197
Owen Anderson170229f2009-07-14 23:10:40 +0000198 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
199 llvm::LLVMContext &VMContext) const {
200 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context,
201 VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000202 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
203 it != ie; ++it)
Owen Anderson170229f2009-07-14 23:10:40 +0000204 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000205 }
206
207 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
208 CodeGenFunction &CGF) const;
209};
210
211/// X86_32ABIInfo - The X86-32 ABI information.
212class X86_32ABIInfo : public ABIInfo {
213 ASTContext &Context;
David Chisnallde3a0692009-08-17 23:08:21 +0000214 bool IsDarwinVectorABI;
215 bool IsSmallStructInRegABI;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000216
217 static bool isRegisterSize(unsigned Size) {
218 return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
219 }
220
221 static bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context);
222
Eli Friedman3192cc82009-06-13 21:37:10 +0000223 static unsigned getIndirectArgumentAlignment(QualType Ty,
224 ASTContext &Context);
225
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000226public:
227 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +0000228 ASTContext &Context,
229 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000230
231 ABIArgInfo classifyArgumentType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +0000232 ASTContext &Context,
233 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000234
Owen Anderson170229f2009-07-14 23:10:40 +0000235 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
236 llvm::LLVMContext &VMContext) const {
237 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context,
238 VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000239 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
240 it != ie; ++it)
Owen Anderson170229f2009-07-14 23:10:40 +0000241 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000242 }
243
244 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
245 CodeGenFunction &CGF) const;
246
David Chisnallde3a0692009-08-17 23:08:21 +0000247 X86_32ABIInfo(ASTContext &Context, bool d, bool p)
Mike Stump11289f42009-09-09 15:08:12 +0000248 : ABIInfo(), Context(Context), IsDarwinVectorABI(d),
David Chisnallde3a0692009-08-17 23:08:21 +0000249 IsSmallStructInRegABI(p) {}
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000250};
251}
252
253
254/// shouldReturnTypeInRegister - Determine if the given type should be
255/// passed in a register (for the Darwin ABI).
256bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
257 ASTContext &Context) {
258 uint64_t Size = Context.getTypeSize(Ty);
259
260 // Type must be register sized.
261 if (!isRegisterSize(Size))
262 return false;
263
264 if (Ty->isVectorType()) {
265 // 64- and 128- bit vectors inside structures are not returned in
266 // registers.
267 if (Size == 64 || Size == 128)
268 return false;
269
270 return true;
271 }
272
273 // If this is a builtin, pointer, or complex type, it is ok.
274 if (Ty->getAsBuiltinType() || Ty->isPointerType() || Ty->isAnyComplexType())
275 return true;
276
277 // Arrays are treated like records.
278 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
279 return shouldReturnTypeInRegister(AT->getElementType(), Context);
280
281 // Otherwise, it must be a record type.
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000282 const RecordType *RT = Ty->getAs<RecordType>();
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000283 if (!RT) return false;
284
285 // Structure types are passed in register if all fields would be
286 // passed in a register.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000287 for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(),
288 e = RT->getDecl()->field_end(); i != e; ++i) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000289 const FieldDecl *FD = *i;
290
291 // Empty fields are ignored.
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000292 if (isEmptyField(Context, FD, true))
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000293 continue;
294
295 // Check fields recursively.
296 if (!shouldReturnTypeInRegister(FD->getType(), Context))
297 return false;
298 }
299
300 return true;
301}
302
303ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +0000304 ASTContext &Context,
305 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000306 if (RetTy->isVoidType()) {
307 return ABIArgInfo::getIgnore();
308 } else if (const VectorType *VT = RetTy->getAsVectorType()) {
309 // On Darwin, some vectors are returned in registers.
David Chisnallde3a0692009-08-17 23:08:21 +0000310 if (IsDarwinVectorABI) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000311 uint64_t Size = Context.getTypeSize(RetTy);
312
313 // 128-bit vectors are a special case; they are returned in
314 // registers and we need to make sure to pick a type the LLVM
315 // backend will like.
316 if (Size == 128)
Owen Anderson41a75022009-08-13 21:57:51 +0000317 return ABIArgInfo::getCoerce(llvm::VectorType::get(
318 llvm::Type::getInt64Ty(VMContext), 2));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000319
320 // Always return in register if it fits in a general purpose
321 // register, or if it is 64 bits and has a single element.
322 if ((Size == 8 || Size == 16 || Size == 32) ||
323 (Size == 64 && VT->getNumElements() == 1))
Owen Anderson41a75022009-08-13 21:57:51 +0000324 return ABIArgInfo::getCoerce(llvm::IntegerType::get(VMContext, Size));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000325
326 return ABIArgInfo::getIndirect(0);
327 }
328
329 return ABIArgInfo::getDirect();
330 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
331 // Structures with flexible arrays are always indirect.
332 if (const RecordType *RT = RetTy->getAsStructureType())
333 if (RT->getDecl()->hasFlexibleArrayMember())
334 return ABIArgInfo::getIndirect(0);
335
David Chisnallde3a0692009-08-17 23:08:21 +0000336 // If specified, structs and unions are always indirect.
337 if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000338 return ABIArgInfo::getIndirect(0);
339
340 // Classify "single element" structs as their element type.
341 if (const Type *SeltTy = isSingleElementStruct(RetTy, Context)) {
342 if (const BuiltinType *BT = SeltTy->getAsBuiltinType()) {
343 if (BT->isIntegerType()) {
344 // We need to use the size of the structure, padding
345 // bit-fields can adjust that to be larger than the single
346 // element type.
347 uint64_t Size = Context.getTypeSize(RetTy);
Owen Anderson170229f2009-07-14 23:10:40 +0000348 return ABIArgInfo::getCoerce(
Owen Anderson41a75022009-08-13 21:57:51 +0000349 llvm::IntegerType::get(VMContext, (unsigned) Size));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000350 } else if (BT->getKind() == BuiltinType::Float) {
351 assert(Context.getTypeSize(RetTy) == Context.getTypeSize(SeltTy) &&
352 "Unexpect single element structure size!");
Owen Anderson41a75022009-08-13 21:57:51 +0000353 return ABIArgInfo::getCoerce(llvm::Type::getFloatTy(VMContext));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000354 } else if (BT->getKind() == BuiltinType::Double) {
355 assert(Context.getTypeSize(RetTy) == Context.getTypeSize(SeltTy) &&
356 "Unexpect single element structure size!");
Owen Anderson41a75022009-08-13 21:57:51 +0000357 return ABIArgInfo::getCoerce(llvm::Type::getDoubleTy(VMContext));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000358 }
359 } else if (SeltTy->isPointerType()) {
360 // FIXME: It would be really nice if this could come out as the proper
361 // pointer type.
362 llvm::Type *PtrTy =
Owen Anderson41a75022009-08-13 21:57:51 +0000363 llvm::PointerType::getUnqual(llvm::Type::getInt8Ty(VMContext));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000364 return ABIArgInfo::getCoerce(PtrTy);
365 } else if (SeltTy->isVectorType()) {
366 // 64- and 128-bit vectors are never returned in a
367 // register when inside a structure.
368 uint64_t Size = Context.getTypeSize(RetTy);
369 if (Size == 64 || Size == 128)
370 return ABIArgInfo::getIndirect(0);
371
Owen Anderson170229f2009-07-14 23:10:40 +0000372 return classifyReturnType(QualType(SeltTy, 0), Context, VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000373 }
374 }
375
376 // Small structures which are register sized are generally returned
377 // in a register.
378 if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, Context)) {
379 uint64_t Size = Context.getTypeSize(RetTy);
Owen Anderson41a75022009-08-13 21:57:51 +0000380 return ABIArgInfo::getCoerce(llvm::IntegerType::get(VMContext, Size));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000381 }
382
383 return ABIArgInfo::getIndirect(0);
384 } else {
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000385 return (RetTy->isPromotableIntegerType() ?
386 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000387 }
388}
389
Eli Friedman3192cc82009-06-13 21:37:10 +0000390unsigned X86_32ABIInfo::getIndirectArgumentAlignment(QualType Ty,
391 ASTContext &Context) {
392 unsigned Align = Context.getTypeAlign(Ty);
393 if (Align < 128) return 0;
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000394 if (const RecordType* RT = Ty->getAs<RecordType>())
Eli Friedman3192cc82009-06-13 21:37:10 +0000395 if (typeContainsSSEVector(RT->getDecl(), Context))
396 return 16;
397 return 0;
398}
399
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000400ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
Owen Anderson170229f2009-07-14 23:10:40 +0000401 ASTContext &Context,
402 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000403 // FIXME: Set alignment on indirect arguments.
404 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
405 // Structures with flexible arrays are always indirect.
406 if (const RecordType *RT = Ty->getAsStructureType())
407 if (RT->getDecl()->hasFlexibleArrayMember())
Mike Stump11289f42009-09-09 15:08:12 +0000408 return ABIArgInfo::getIndirect(getIndirectArgumentAlignment(Ty,
Eli Friedman3192cc82009-06-13 21:37:10 +0000409 Context));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000410
411 // Ignore empty structs.
Eli Friedman3192cc82009-06-13 21:37:10 +0000412 if (Ty->isStructureType() && Context.getTypeSize(Ty) == 0)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000413 return ABIArgInfo::getIgnore();
414
415 // Expand structs with size <= 128-bits which consist only of
416 // basic types (int, long long, float, double, xxx*). This is
417 // non-recursive and does not ignore empty fields.
418 if (const RecordType *RT = Ty->getAsStructureType()) {
419 if (Context.getTypeSize(Ty) <= 4*32 &&
420 areAllFields32Or64BitBasicType(RT->getDecl(), Context))
421 return ABIArgInfo::getExpand();
422 }
423
Eli Friedman3192cc82009-06-13 21:37:10 +0000424 return ABIArgInfo::getIndirect(getIndirectArgumentAlignment(Ty, Context));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000425 } else {
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000426 return (Ty->isPromotableIntegerType() ?
427 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000428 }
429}
430
431llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
432 CodeGenFunction &CGF) const {
Owen Anderson41a75022009-08-13 21:57:51 +0000433 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::getInt8Ty(CGF.getLLVMContext()));
Owen Anderson9793f0e2009-07-29 22:16:19 +0000434 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000435
436 CGBuilderTy &Builder = CGF.Builder;
437 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
438 "ap");
439 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
440 llvm::Type *PTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +0000441 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000442 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
443
444 uint64_t Offset =
445 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
446 llvm::Value *NextAddr =
Owen Anderson41a75022009-08-13 21:57:51 +0000447 Builder.CreateGEP(Addr, llvm::ConstantInt::get(
448 llvm::Type::getInt32Ty(CGF.getLLVMContext()), Offset),
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000449 "ap.next");
450 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
451
452 return AddrTyped;
453}
454
455namespace {
456/// X86_64ABIInfo - The X86_64 ABI information.
457class X86_64ABIInfo : public ABIInfo {
458 enum Class {
459 Integer = 0,
460 SSE,
461 SSEUp,
462 X87,
463 X87Up,
464 ComplexX87,
465 NoClass,
466 Memory
467 };
468
469 /// merge - Implement the X86_64 ABI merging algorithm.
470 ///
471 /// Merge an accumulating classification \arg Accum with a field
472 /// classification \arg Field.
473 ///
474 /// \param Accum - The accumulating classification. This should
475 /// always be either NoClass or the result of a previous merge
476 /// call. In addition, this should never be Memory (the caller
477 /// should just return Memory for the aggregate).
478 Class merge(Class Accum, Class Field) const;
479
480 /// classify - Determine the x86_64 register classes in which the
481 /// given type T should be passed.
482 ///
483 /// \param Lo - The classification for the parts of the type
484 /// residing in the low word of the containing object.
485 ///
486 /// \param Hi - The classification for the parts of the type
487 /// residing in the high word of the containing object.
488 ///
489 /// \param OffsetBase - The bit offset of this type in the
490 /// containing object. Some parameters are classified different
491 /// depending on whether they straddle an eightbyte boundary.
492 ///
493 /// If a word is unused its result will be NoClass; if a type should
494 /// be passed in Memory then at least the classification of \arg Lo
495 /// will be Memory.
496 ///
497 /// The \arg Lo class will be NoClass iff the argument is ignored.
498 ///
499 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
500 /// also be ComplexX87.
501 void classify(QualType T, ASTContext &Context, uint64_t OffsetBase,
502 Class &Lo, Class &Hi) const;
503
504 /// getCoerceResult - Given a source type \arg Ty and an LLVM type
505 /// to coerce to, chose the best way to pass Ty in the same place
506 /// that \arg CoerceTo would be passed, but while keeping the
507 /// emitted code as simple as possible.
508 ///
509 /// FIXME: Note, this should be cleaned up to just take an enumeration of all
510 /// the ways we might want to pass things, instead of constructing an LLVM
511 /// type. This makes this code more explicit, and it makes it clearer that we
512 /// are also doing this for correctness in the case of passing scalar types.
513 ABIArgInfo getCoerceResult(QualType Ty,
514 const llvm::Type *CoerceTo,
515 ASTContext &Context) const;
516
517 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
518 /// such that the argument will be passed in memory.
519 ABIArgInfo getIndirectResult(QualType Ty,
520 ASTContext &Context) const;
521
522 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +0000523 ASTContext &Context,
524 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000525
526 ABIArgInfo classifyArgumentType(QualType Ty,
527 ASTContext &Context,
Owen Anderson170229f2009-07-14 23:10:40 +0000528 llvm::LLVMContext &VMContext,
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000529 unsigned &neededInt,
530 unsigned &neededSSE) const;
531
532public:
Owen Anderson170229f2009-07-14 23:10:40 +0000533 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
534 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000535
536 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
537 CodeGenFunction &CGF) const;
538};
539}
540
541X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum,
542 Class Field) const {
543 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
544 // classified recursively so that always two fields are
545 // considered. The resulting class is calculated according to
546 // the classes of the fields in the eightbyte:
547 //
548 // (a) If both classes are equal, this is the resulting class.
549 //
550 // (b) If one of the classes is NO_CLASS, the resulting class is
551 // the other class.
552 //
553 // (c) If one of the classes is MEMORY, the result is the MEMORY
554 // class.
555 //
556 // (d) If one of the classes is INTEGER, the result is the
557 // INTEGER.
558 //
559 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
560 // MEMORY is used as class.
561 //
562 // (f) Otherwise class SSE is used.
563
564 // Accum should never be memory (we should have returned) or
565 // ComplexX87 (because this cannot be passed in a structure).
566 assert((Accum != Memory && Accum != ComplexX87) &&
567 "Invalid accumulated classification during merge.");
568 if (Accum == Field || Field == NoClass)
569 return Accum;
570 else if (Field == Memory)
571 return Memory;
572 else if (Accum == NoClass)
573 return Field;
574 else if (Accum == Integer || Field == Integer)
575 return Integer;
576 else if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
577 Accum == X87 || Accum == X87Up)
578 return Memory;
579 else
580 return SSE;
581}
582
583void X86_64ABIInfo::classify(QualType Ty,
584 ASTContext &Context,
585 uint64_t OffsetBase,
586 Class &Lo, Class &Hi) const {
587 // FIXME: This code can be simplified by introducing a simple value class for
588 // Class pairs with appropriate constructor methods for the various
589 // situations.
590
591 // FIXME: Some of the split computations are wrong; unaligned vectors
592 // shouldn't be passed in registers for example, so there is no chance they
593 // can straddle an eightbyte. Verify & simplify.
594
595 Lo = Hi = NoClass;
596
597 Class &Current = OffsetBase < 64 ? Lo : Hi;
598 Current = Memory;
599
600 if (const BuiltinType *BT = Ty->getAsBuiltinType()) {
601 BuiltinType::Kind k = BT->getKind();
602
603 if (k == BuiltinType::Void) {
604 Current = NoClass;
605 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
606 Lo = Integer;
607 Hi = Integer;
608 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
609 Current = Integer;
610 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
611 Current = SSE;
612 } else if (k == BuiltinType::LongDouble) {
613 Lo = X87;
614 Hi = X87Up;
615 }
616 // FIXME: _Decimal32 and _Decimal64 are SSE.
617 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
618 } else if (const EnumType *ET = Ty->getAsEnumType()) {
619 // Classify the underlying integer type.
620 classify(ET->getDecl()->getIntegerType(), Context, OffsetBase, Lo, Hi);
621 } else if (Ty->hasPointerRepresentation()) {
622 Current = Integer;
623 } else if (const VectorType *VT = Ty->getAsVectorType()) {
624 uint64_t Size = Context.getTypeSize(VT);
625 if (Size == 32) {
626 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
627 // float> as integer.
628 Current = Integer;
629
630 // If this type crosses an eightbyte boundary, it should be
631 // split.
632 uint64_t EB_Real = (OffsetBase) / 64;
633 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
634 if (EB_Real != EB_Imag)
635 Hi = Lo;
636 } else if (Size == 64) {
637 // gcc passes <1 x double> in memory. :(
638 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
639 return;
640
641 // gcc passes <1 x long long> as INTEGER.
642 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong))
643 Current = Integer;
644 else
645 Current = SSE;
646
647 // If this type crosses an eightbyte boundary, it should be
648 // split.
649 if (OffsetBase && OffsetBase != 64)
650 Hi = Lo;
651 } else if (Size == 128) {
652 Lo = SSE;
653 Hi = SSEUp;
654 }
655 } else if (const ComplexType *CT = Ty->getAsComplexType()) {
656 QualType ET = Context.getCanonicalType(CT->getElementType());
657
658 uint64_t Size = Context.getTypeSize(Ty);
659 if (ET->isIntegralType()) {
660 if (Size <= 64)
661 Current = Integer;
662 else if (Size <= 128)
663 Lo = Hi = Integer;
664 } else if (ET == Context.FloatTy)
665 Current = SSE;
666 else if (ET == Context.DoubleTy)
667 Lo = Hi = SSE;
668 else if (ET == Context.LongDoubleTy)
669 Current = ComplexX87;
670
671 // If this complex type crosses an eightbyte boundary then it
672 // should be split.
673 uint64_t EB_Real = (OffsetBase) / 64;
674 uint64_t EB_Imag = (OffsetBase + Context.getTypeSize(ET)) / 64;
675 if (Hi == NoClass && EB_Real != EB_Imag)
676 Hi = Lo;
677 } else if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
678 // Arrays are treated like structures.
679
680 uint64_t Size = Context.getTypeSize(Ty);
681
682 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
683 // than two eightbytes, ..., it has class MEMORY.
684 if (Size > 128)
685 return;
686
687 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
688 // fields, it has class MEMORY.
689 //
690 // Only need to check alignment of array base.
691 if (OffsetBase % Context.getTypeAlign(AT->getElementType()))
692 return;
693
694 // Otherwise implement simplified merge. We could be smarter about
695 // this, but it isn't worth it and would be harder to verify.
696 Current = NoClass;
697 uint64_t EltSize = Context.getTypeSize(AT->getElementType());
698 uint64_t ArraySize = AT->getSize().getZExtValue();
699 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
700 Class FieldLo, FieldHi;
701 classify(AT->getElementType(), Context, Offset, FieldLo, FieldHi);
702 Lo = merge(Lo, FieldLo);
703 Hi = merge(Hi, FieldHi);
704 if (Lo == Memory || Hi == Memory)
705 break;
706 }
707
708 // Do post merger cleanup (see below). Only case we worry about is Memory.
709 if (Hi == Memory)
710 Lo = Memory;
711 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000712 } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000713 uint64_t Size = Context.getTypeSize(Ty);
714
715 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
716 // than two eightbytes, ..., it has class MEMORY.
717 if (Size > 128)
718 return;
719
720 const RecordDecl *RD = RT->getDecl();
721
722 // Assume variable sized types are passed in memory.
723 if (RD->hasFlexibleArrayMember())
724 return;
725
726 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
727
728 // Reset Lo class, this will be recomputed.
729 Current = NoClass;
730 unsigned idx = 0;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000731 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
732 i != e; ++i, ++idx) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000733 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
734 bool BitField = i->isBitField();
735
736 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
737 // fields, it has class MEMORY.
738 //
739 // Note, skip this test for bit-fields, see below.
740 if (!BitField && Offset % Context.getTypeAlign(i->getType())) {
741 Lo = Memory;
742 return;
743 }
744
745 // Classify this field.
746 //
747 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
748 // exceeds a single eightbyte, each is classified
749 // separately. Each eightbyte gets initialized to class
750 // NO_CLASS.
751 Class FieldLo, FieldHi;
752
753 // Bit-fields require special handling, they do not force the
754 // structure to be passed in memory even if unaligned, and
755 // therefore they can straddle an eightbyte.
756 if (BitField) {
757 // Ignore padding bit-fields.
758 if (i->isUnnamedBitfield())
759 continue;
760
761 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
762 uint64_t Size = i->getBitWidth()->EvaluateAsInt(Context).getZExtValue();
763
764 uint64_t EB_Lo = Offset / 64;
765 uint64_t EB_Hi = (Offset + Size - 1) / 64;
766 FieldLo = FieldHi = NoClass;
767 if (EB_Lo) {
768 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
769 FieldLo = NoClass;
770 FieldHi = Integer;
771 } else {
772 FieldLo = Integer;
773 FieldHi = EB_Hi ? Integer : NoClass;
774 }
775 } else
776 classify(i->getType(), Context, Offset, FieldLo, FieldHi);
777 Lo = merge(Lo, FieldLo);
778 Hi = merge(Hi, FieldHi);
779 if (Lo == Memory || Hi == Memory)
780 break;
781 }
782
783 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
784 //
785 // (a) If one of the classes is MEMORY, the whole argument is
786 // passed in memory.
787 //
788 // (b) If SSEUP is not preceeded by SSE, it is converted to SSE.
789
790 // The first of these conditions is guaranteed by how we implement
791 // the merge (just bail).
792 //
793 // The second condition occurs in the case of unions; for example
794 // union { _Complex double; unsigned; }.
795 if (Hi == Memory)
796 Lo = Memory;
797 if (Hi == SSEUp && Lo != SSE)
798 Hi = SSE;
799 }
800}
801
802ABIArgInfo X86_64ABIInfo::getCoerceResult(QualType Ty,
803 const llvm::Type *CoerceTo,
804 ASTContext &Context) const {
Owen Anderson41a75022009-08-13 21:57:51 +0000805 if (CoerceTo == llvm::Type::getInt64Ty(CoerceTo->getContext())) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000806 // Integer and pointer types will end up in a general purpose
807 // register.
808 if (Ty->isIntegralType() || Ty->isPointerType())
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000809 return (Ty->isPromotableIntegerType() ?
810 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Owen Anderson41a75022009-08-13 21:57:51 +0000811 } else if (CoerceTo == llvm::Type::getDoubleTy(CoerceTo->getContext())) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000812 // FIXME: It would probably be better to make CGFunctionInfo only map using
813 // canonical types than to canonize here.
814 QualType CTy = Context.getCanonicalType(Ty);
815
816 // Float and double end up in a single SSE reg.
817 if (CTy == Context.FloatTy || CTy == Context.DoubleTy)
818 return ABIArgInfo::getDirect();
819
820 }
821
822 return ABIArgInfo::getCoerce(CoerceTo);
823}
824
825ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty,
826 ASTContext &Context) const {
827 // If this is a scalar LLVM value then assume LLVM will pass it in the right
828 // place naturally.
829 if (!CodeGenFunction::hasAggregateLLVMType(Ty))
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000830 return (Ty->isPromotableIntegerType() ?
831 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000832
833 // FIXME: Set alignment correctly.
834 return ABIArgInfo::getIndirect(0);
835}
836
837ABIArgInfo X86_64ABIInfo::classifyReturnType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +0000838 ASTContext &Context,
839 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000840 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
841 // classification algorithm.
842 X86_64ABIInfo::Class Lo, Hi;
843 classify(RetTy, Context, 0, Lo, Hi);
844
845 // Check some invariants.
846 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
847 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
848 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
849
850 const llvm::Type *ResType = 0;
851 switch (Lo) {
852 case NoClass:
853 return ABIArgInfo::getIgnore();
854
855 case SSEUp:
856 case X87Up:
857 assert(0 && "Invalid classification for lo word.");
858
859 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
860 // hidden argument.
861 case Memory:
862 return getIndirectResult(RetTy, Context);
863
864 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
865 // available register of the sequence %rax, %rdx is used.
866 case Integer:
Owen Anderson41a75022009-08-13 21:57:51 +0000867 ResType = llvm::Type::getInt64Ty(VMContext); break;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000868
869 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
870 // available SSE register of the sequence %xmm0, %xmm1 is used.
871 case SSE:
Owen Anderson41a75022009-08-13 21:57:51 +0000872 ResType = llvm::Type::getDoubleTy(VMContext); break;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000873
874 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
875 // returned on the X87 stack in %st0 as 80-bit x87 number.
876 case X87:
Owen Anderson41a75022009-08-13 21:57:51 +0000877 ResType = llvm::Type::getX86_FP80Ty(VMContext); break;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000878
879 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
880 // part of the value is returned in %st0 and the imaginary part in
881 // %st1.
882 case ComplexX87:
883 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Owen Anderson41a75022009-08-13 21:57:51 +0000884 ResType = llvm::StructType::get(VMContext, llvm::Type::getX86_FP80Ty(VMContext),
885 llvm::Type::getX86_FP80Ty(VMContext),
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000886 NULL);
887 break;
888 }
889
890 switch (Hi) {
891 // Memory was handled previously and X87 should
892 // never occur as a hi class.
893 case Memory:
894 case X87:
895 assert(0 && "Invalid classification for hi word.");
896
897 case ComplexX87: // Previously handled.
898 case NoClass: break;
899
900 case Integer:
Owen Anderson758428f2009-08-05 23:18:46 +0000901 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson41a75022009-08-13 21:57:51 +0000902 llvm::Type::getInt64Ty(VMContext), NULL);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000903 break;
904 case SSE:
Owen Anderson758428f2009-08-05 23:18:46 +0000905 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson41a75022009-08-13 21:57:51 +0000906 llvm::Type::getDoubleTy(VMContext), NULL);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000907 break;
908
909 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
910 // is passed in the upper half of the last used SSE register.
911 //
912 // SSEUP should always be preceeded by SSE, just widen.
913 case SSEUp:
914 assert(Lo == SSE && "Unexpected SSEUp classification.");
Owen Anderson41a75022009-08-13 21:57:51 +0000915 ResType = llvm::VectorType::get(llvm::Type::getDoubleTy(VMContext), 2);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000916 break;
917
918 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
919 // returned together with the previous X87 value in %st0.
920 case X87Up:
921 // If X87Up is preceeded by X87, we don't need to do
922 // anything. However, in some cases with unions it may not be
923 // preceeded by X87. In such situations we follow gcc and pass the
924 // extra bits in an SSE reg.
925 if (Lo != X87)
Owen Anderson758428f2009-08-05 23:18:46 +0000926 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson41a75022009-08-13 21:57:51 +0000927 llvm::Type::getDoubleTy(VMContext), NULL);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000928 break;
929 }
930
931 return getCoerceResult(RetTy, ResType, Context);
932}
933
934ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, ASTContext &Context,
Owen Anderson170229f2009-07-14 23:10:40 +0000935 llvm::LLVMContext &VMContext,
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000936 unsigned &neededInt,
937 unsigned &neededSSE) const {
938 X86_64ABIInfo::Class Lo, Hi;
939 classify(Ty, Context, 0, Lo, Hi);
940
941 // Check some invariants.
942 // FIXME: Enforce these by construction.
943 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
944 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
945 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
946
947 neededInt = 0;
948 neededSSE = 0;
949 const llvm::Type *ResType = 0;
950 switch (Lo) {
951 case NoClass:
952 return ABIArgInfo::getIgnore();
953
954 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
955 // on the stack.
956 case Memory:
957
958 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
959 // COMPLEX_X87, it is passed in memory.
960 case X87:
961 case ComplexX87:
962 return getIndirectResult(Ty, Context);
963
964 case SSEUp:
965 case X87Up:
966 assert(0 && "Invalid classification for lo word.");
967
968 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
969 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
970 // and %r9 is used.
971 case Integer:
972 ++neededInt;
Owen Anderson41a75022009-08-13 21:57:51 +0000973 ResType = llvm::Type::getInt64Ty(VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000974 break;
975
976 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
977 // available SSE register is used, the registers are taken in the
978 // order from %xmm0 to %xmm7.
979 case SSE:
980 ++neededSSE;
Owen Anderson41a75022009-08-13 21:57:51 +0000981 ResType = llvm::Type::getDoubleTy(VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000982 break;
983 }
984
985 switch (Hi) {
986 // Memory was handled previously, ComplexX87 and X87 should
987 // never occur as hi classes, and X87Up must be preceed by X87,
988 // which is passed in memory.
989 case Memory:
990 case X87:
991 case ComplexX87:
992 assert(0 && "Invalid classification for hi word.");
993 break;
994
995 case NoClass: break;
996 case Integer:
Owen Anderson758428f2009-08-05 23:18:46 +0000997 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson41a75022009-08-13 21:57:51 +0000998 llvm::Type::getInt64Ty(VMContext), NULL);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000999 ++neededInt;
1000 break;
1001
1002 // X87Up generally doesn't occur here (long double is passed in
1003 // memory), except in situations involving unions.
1004 case X87Up:
1005 case SSE:
Owen Anderson758428f2009-08-05 23:18:46 +00001006 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson41a75022009-08-13 21:57:51 +00001007 llvm::Type::getDoubleTy(VMContext), NULL);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001008 ++neededSSE;
1009 break;
1010
1011 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
1012 // eightbyte is passed in the upper half of the last used SSE
1013 // register.
1014 case SSEUp:
1015 assert(Lo == SSE && "Unexpected SSEUp classification.");
Owen Anderson41a75022009-08-13 21:57:51 +00001016 ResType = llvm::VectorType::get(llvm::Type::getDoubleTy(VMContext), 2);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001017 break;
1018 }
1019
1020 return getCoerceResult(Ty, ResType, Context);
1021}
1022
Owen Anderson170229f2009-07-14 23:10:40 +00001023void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1024 llvm::LLVMContext &VMContext) const {
1025 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(),
1026 Context, VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001027
1028 // Keep track of the number of assigned registers.
1029 unsigned freeIntRegs = 6, freeSSERegs = 8;
1030
1031 // If the return value is indirect, then the hidden argument is consuming one
1032 // integer register.
1033 if (FI.getReturnInfo().isIndirect())
1034 --freeIntRegs;
1035
1036 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
1037 // get assigned (in left-to-right order) for passing as follows...
1038 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1039 it != ie; ++it) {
1040 unsigned neededInt, neededSSE;
Mike Stump11289f42009-09-09 15:08:12 +00001041 it->info = classifyArgumentType(it->type, Context, VMContext,
Owen Anderson170229f2009-07-14 23:10:40 +00001042 neededInt, neededSSE);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001043
1044 // AMD64-ABI 3.2.3p3: If there are no registers available for any
1045 // eightbyte of an argument, the whole argument is passed on the
1046 // stack. If registers have already been assigned for some
1047 // eightbytes of such an argument, the assignments get reverted.
1048 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
1049 freeIntRegs -= neededInt;
1050 freeSSERegs -= neededSSE;
1051 } else {
1052 it->info = getIndirectResult(it->type, Context);
1053 }
1054 }
1055}
1056
1057static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
1058 QualType Ty,
1059 CodeGenFunction &CGF) {
1060 llvm::Value *overflow_arg_area_p =
1061 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
1062 llvm::Value *overflow_arg_area =
1063 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
1064
1065 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
1066 // byte boundary if alignment needed by type exceeds 8 byte boundary.
1067 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
1068 if (Align > 8) {
1069 // Note that we follow the ABI & gcc here, even though the type
1070 // could in theory have an alignment greater than 16. This case
1071 // shouldn't ever matter in practice.
1072
1073 // overflow_arg_area = (overflow_arg_area + 15) & ~15;
Owen Anderson41a75022009-08-13 21:57:51 +00001074 llvm::Value *Offset =
1075 llvm::ConstantInt::get(llvm::Type::getInt32Ty(CGF.getLLVMContext()), 15);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001076 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
1077 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
Owen Anderson41a75022009-08-13 21:57:51 +00001078 llvm::Type::getInt64Ty(CGF.getLLVMContext()));
1079 llvm::Value *Mask = llvm::ConstantInt::get(
1080 llvm::Type::getInt64Ty(CGF.getLLVMContext()), ~15LL);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001081 overflow_arg_area =
1082 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
1083 overflow_arg_area->getType(),
1084 "overflow_arg_area.align");
1085 }
1086
1087 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
1088 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1089 llvm::Value *Res =
1090 CGF.Builder.CreateBitCast(overflow_arg_area,
Owen Anderson9793f0e2009-07-29 22:16:19 +00001091 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001092
1093 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
1094 // l->overflow_arg_area + sizeof(type).
1095 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
1096 // an 8 byte boundary.
1097
1098 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
Owen Anderson41a75022009-08-13 21:57:51 +00001099 llvm::Value *Offset =
1100 llvm::ConstantInt::get(llvm::Type::getInt32Ty(CGF.getLLVMContext()),
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001101 (SizeInBytes + 7) & ~7);
1102 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
1103 "overflow_arg_area.next");
1104 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
1105
1106 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
1107 return Res;
1108}
1109
1110llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1111 CodeGenFunction &CGF) const {
Owen Anderson170229f2009-07-14 23:10:40 +00001112 llvm::LLVMContext &VMContext = CGF.getLLVMContext();
Daniel Dunbard59655c2009-09-12 00:59:49 +00001113 const llvm::Type *i32Ty = llvm::Type::getInt32Ty(VMContext);
1114 const llvm::Type *DoubleTy = llvm::Type::getDoubleTy(VMContext);
Mike Stump11289f42009-09-09 15:08:12 +00001115
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001116 // Assume that va_list type is correct; should be pointer to LLVM type:
1117 // struct {
1118 // i32 gp_offset;
1119 // i32 fp_offset;
1120 // i8* overflow_arg_area;
1121 // i8* reg_save_area;
1122 // };
1123 unsigned neededInt, neededSSE;
Owen Anderson170229f2009-07-14 23:10:40 +00001124 ABIArgInfo AI = classifyArgumentType(Ty, CGF.getContext(), VMContext,
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001125 neededInt, neededSSE);
1126
1127 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
1128 // in the registers. If not go to step 7.
1129 if (!neededInt && !neededSSE)
1130 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1131
1132 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
1133 // general purpose registers needed to pass type and num_fp to hold
1134 // the number of floating point registers needed.
1135
1136 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
1137 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
1138 // l->fp_offset > 304 - num_fp * 16 go to step 7.
1139 //
1140 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
1141 // register save space).
1142
1143 llvm::Value *InRegs = 0;
1144 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
1145 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
1146 if (neededInt) {
1147 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
1148 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
1149 InRegs =
1150 CGF.Builder.CreateICmpULE(gp_offset,
Daniel Dunbard59655c2009-09-12 00:59:49 +00001151 llvm::ConstantInt::get(i32Ty,
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001152 48 - neededInt * 8),
1153 "fits_in_gp");
1154 }
1155
1156 if (neededSSE) {
1157 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
1158 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
1159 llvm::Value *FitsInFP =
1160 CGF.Builder.CreateICmpULE(fp_offset,
Daniel Dunbard59655c2009-09-12 00:59:49 +00001161 llvm::ConstantInt::get(i32Ty,
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001162 176 - neededSSE * 16),
1163 "fits_in_fp");
1164 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
1165 }
1166
1167 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
1168 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
1169 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
1170 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
1171
1172 // Emit code to load the value if it was passed in registers.
1173
1174 CGF.EmitBlock(InRegBlock);
1175
1176 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
1177 // an offset of l->gp_offset and/or l->fp_offset. This may require
1178 // copying to a temporary location in case the parameter is passed
1179 // in different register classes or requires an alignment greater
1180 // than 8 for general purpose registers and 16 for XMM registers.
1181 //
1182 // FIXME: This really results in shameful code when we end up needing to
1183 // collect arguments from different places; often what should result in a
1184 // simple assembling of a structure from scattered addresses has many more
1185 // loads than necessary. Can we clean this up?
1186 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1187 llvm::Value *RegAddr =
1188 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
1189 "reg_save_area");
1190 if (neededInt && neededSSE) {
1191 // FIXME: Cleanup.
1192 assert(AI.isCoerce() && "Unexpected ABI info for mixed regs");
1193 const llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
1194 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
1195 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
1196 const llvm::Type *TyLo = ST->getElementType(0);
1197 const llvm::Type *TyHi = ST->getElementType(1);
1198 assert((TyLo->isFloatingPoint() ^ TyHi->isFloatingPoint()) &&
1199 "Unexpected ABI info for mixed regs");
Owen Anderson9793f0e2009-07-29 22:16:19 +00001200 const llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
1201 const llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001202 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1203 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1204 llvm::Value *RegLoAddr = TyLo->isFloatingPoint() ? FPAddr : GPAddr;
1205 llvm::Value *RegHiAddr = TyLo->isFloatingPoint() ? GPAddr : FPAddr;
1206 llvm::Value *V =
1207 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
1208 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1209 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
1210 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1211
Owen Anderson170229f2009-07-14 23:10:40 +00001212 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson9793f0e2009-07-29 22:16:19 +00001213 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001214 } else if (neededInt) {
1215 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1216 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson9793f0e2009-07-29 22:16:19 +00001217 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001218 } else {
1219 if (neededSSE == 1) {
1220 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1221 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson9793f0e2009-07-29 22:16:19 +00001222 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001223 } else {
1224 assert(neededSSE == 2 && "Invalid number of needed registers!");
1225 // SSE registers are spaced 16 bytes apart in the register save
1226 // area, we need to collect the two eightbytes together.
1227 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1228 llvm::Value *RegAddrHi =
1229 CGF.Builder.CreateGEP(RegAddrLo,
Daniel Dunbard59655c2009-09-12 00:59:49 +00001230 llvm::ConstantInt::get(i32Ty, 16));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001231 const llvm::Type *DblPtrTy =
Daniel Dunbard59655c2009-09-12 00:59:49 +00001232 llvm::PointerType::getUnqual(DoubleTy);
1233 const llvm::StructType *ST = llvm::StructType::get(VMContext, DoubleTy,
1234 DoubleTy, NULL);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001235 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
1236 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
1237 DblPtrTy));
1238 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1239 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
1240 DblPtrTy));
1241 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1242 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson9793f0e2009-07-29 22:16:19 +00001243 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001244 }
1245 }
1246
1247 // AMD64-ABI 3.5.7p5: Step 5. Set:
1248 // l->gp_offset = l->gp_offset + num_gp * 8
1249 // l->fp_offset = l->fp_offset + num_fp * 16.
1250 if (neededInt) {
Daniel Dunbard59655c2009-09-12 00:59:49 +00001251 llvm::Value *Offset = llvm::ConstantInt::get(i32Ty, neededInt * 8);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001252 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
1253 gp_offset_p);
1254 }
1255 if (neededSSE) {
Daniel Dunbard59655c2009-09-12 00:59:49 +00001256 llvm::Value *Offset = llvm::ConstantInt::get(i32Ty, neededSSE * 16);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001257 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
1258 fp_offset_p);
1259 }
1260 CGF.EmitBranch(ContBlock);
1261
1262 // Emit code to load the value if it was passed in memory.
1263
1264 CGF.EmitBlock(InMemBlock);
1265 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1266
1267 // Return the appropriate result.
1268
1269 CGF.EmitBlock(ContBlock);
1270 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(),
1271 "vaarg.addr");
1272 ResAddr->reserveOperandSpace(2);
1273 ResAddr->addIncoming(RegAddr, InRegBlock);
1274 ResAddr->addIncoming(MemAddr, InMemBlock);
1275
1276 return ResAddr;
1277}
1278
Daniel Dunbard59655c2009-09-12 00:59:49 +00001279// PIC16 ABI Implementation
1280
1281namespace {
1282
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001283class PIC16ABIInfo : public ABIInfo {
1284 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +00001285 ASTContext &Context,
1286 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001287
1288 ABIArgInfo classifyArgumentType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +00001289 ASTContext &Context,
1290 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001291
Owen Anderson170229f2009-07-14 23:10:40 +00001292 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1293 llvm::LLVMContext &VMContext) const {
1294 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context,
1295 VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001296 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1297 it != ie; ++it)
Owen Anderson170229f2009-07-14 23:10:40 +00001298 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001299 }
1300
1301 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1302 CodeGenFunction &CGF) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001303};
1304
Daniel Dunbard59655c2009-09-12 00:59:49 +00001305}
1306
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001307ABIArgInfo PIC16ABIInfo::classifyReturnType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +00001308 ASTContext &Context,
1309 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001310 if (RetTy->isVoidType()) {
1311 return ABIArgInfo::getIgnore();
1312 } else {
1313 return ABIArgInfo::getDirect();
1314 }
1315}
1316
1317ABIArgInfo PIC16ABIInfo::classifyArgumentType(QualType Ty,
Owen Anderson170229f2009-07-14 23:10:40 +00001318 ASTContext &Context,
1319 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001320 return ABIArgInfo::getDirect();
1321}
1322
1323llvm::Value *PIC16ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1324 CodeGenFunction &CGF) const {
1325 return 0;
1326}
1327
Daniel Dunbard59655c2009-09-12 00:59:49 +00001328// ARM ABI Implementation
1329
1330namespace {
1331
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001332class ARMABIInfo : public ABIInfo {
Daniel Dunbar020daa92009-09-12 01:00:39 +00001333public:
1334 enum ABIKind {
1335 APCS = 0,
1336 AAPCS = 1,
1337 AAPCS_VFP
1338 };
1339
1340private:
1341 ABIKind Kind;
1342
1343public:
1344 ARMABIInfo(ABIKind _Kind) : Kind(_Kind) {}
1345
1346private:
1347 ABIKind getABIKind() const { return Kind; }
1348
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001349 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +00001350 ASTContext &Context,
1351 llvm::LLVMContext &VMCOntext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001352
1353 ABIArgInfo classifyArgumentType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +00001354 ASTContext &Context,
1355 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001356
Owen Anderson170229f2009-07-14 23:10:40 +00001357 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1358 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001359
1360 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1361 CodeGenFunction &CGF) const;
1362};
1363
Daniel Dunbard59655c2009-09-12 00:59:49 +00001364}
1365
Owen Anderson170229f2009-07-14 23:10:40 +00001366void ARMABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1367 llvm::LLVMContext &VMContext) const {
Mike Stump11289f42009-09-09 15:08:12 +00001368 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context,
Owen Anderson170229f2009-07-14 23:10:40 +00001369 VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001370 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1371 it != ie; ++it) {
Owen Anderson170229f2009-07-14 23:10:40 +00001372 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001373 }
Daniel Dunbar020daa92009-09-12 01:00:39 +00001374
1375 // ARM always overrides the calling convention.
1376 switch (getABIKind()) {
1377 case APCS:
1378 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_APCS);
1379 break;
1380
1381 case AAPCS:
1382 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS);
1383 break;
1384
1385 case AAPCS_VFP:
1386 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS_VFP);
1387 break;
1388 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001389}
1390
1391ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty,
Owen Anderson170229f2009-07-14 23:10:40 +00001392 ASTContext &Context,
1393 llvm::LLVMContext &VMContext) const {
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001394 if (!CodeGenFunction::hasAggregateLLVMType(Ty))
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00001395 return (Ty->isPromotableIntegerType() ?
1396 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001397
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001398 // FIXME: This is kind of nasty... but there isn't much choice because the ARM
1399 // backend doesn't support byval.
1400 // FIXME: This doesn't handle alignment > 64 bits.
1401 const llvm::Type* ElemTy;
1402 unsigned SizeRegs;
1403 if (Context.getTypeAlign(Ty) > 32) {
Owen Anderson41a75022009-08-13 21:57:51 +00001404 ElemTy = llvm::Type::getInt64Ty(VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001405 SizeRegs = (Context.getTypeSize(Ty) + 63) / 64;
1406 } else {
Owen Anderson41a75022009-08-13 21:57:51 +00001407 ElemTy = llvm::Type::getInt32Ty(VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001408 SizeRegs = (Context.getTypeSize(Ty) + 31) / 32;
1409 }
1410 std::vector<const llvm::Type*> LLVMFields;
Owen Anderson9793f0e2009-07-29 22:16:19 +00001411 LLVMFields.push_back(llvm::ArrayType::get(ElemTy, SizeRegs));
Owen Anderson758428f2009-08-05 23:18:46 +00001412 const llvm::Type* STy = llvm::StructType::get(VMContext, LLVMFields, true);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001413 return ABIArgInfo::getCoerce(STy);
1414}
1415
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001416static bool isIntegerLikeType(QualType Ty,
1417 ASTContext &Context,
1418 llvm::LLVMContext &VMContext) {
1419 // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
1420 // is called integer-like if its size is less than or equal to one word, and
1421 // the offset of each of its addressable sub-fields is zero.
1422
1423 uint64_t Size = Context.getTypeSize(Ty);
1424
1425 // Check that the type fits in a word.
1426 if (Size > 32)
1427 return false;
1428
1429 // FIXME: Handle vector types!
1430 if (Ty->isVectorType())
1431 return false;
1432
1433 // If this is a builtin or pointer type then it is ok.
1434 if (Ty->getAsBuiltinType() || Ty->isPointerType())
1435 return true;
1436
1437 // Complex types "should" be ok by the definition above, but they are not.
1438 if (Ty->isAnyComplexType())
1439 return false;
1440
1441 // Single element and zero sized arrays should be allowed, by the definition
1442 // above, but they are not.
1443
1444 // Otherwise, it must be a record type.
1445 const RecordType *RT = Ty->getAs<RecordType>();
1446 if (!RT) return false;
1447
1448 // Ignore records with flexible arrays.
1449 const RecordDecl *RD = RT->getDecl();
1450 if (RD->hasFlexibleArrayMember())
1451 return false;
1452
1453 // Check that all sub-fields are at offset 0, and are themselves "integer
1454 // like".
1455 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
1456
1457 bool HadField = false;
1458 unsigned idx = 0;
1459 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1460 i != e; ++i, ++idx) {
1461 const FieldDecl *FD = *i;
1462
1463 // Check if this field is at offset 0.
1464 uint64_t Offset = Layout.getFieldOffset(idx);
1465 if (Offset != 0) {
1466 // Allow padding bit-fields, but only if they are all at the end of the
1467 // structure (despite the wording above, this matches gcc).
1468 if (FD->isBitField() &&
1469 !FD->getBitWidth()->EvaluateAsInt(Context).getZExtValue()) {
1470 for (; i != e; ++i)
1471 if (!i->isBitField() ||
1472 i->getBitWidth()->EvaluateAsInt(Context).getZExtValue())
1473 return false;
1474
1475 // All remaining fields are padding, allow this.
1476 return true;
1477 }
1478
1479 return false;
1480 }
1481
1482 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
1483 return false;
1484
1485 // Only allow at most one field in a structure. Again this doesn't match the
1486 // wording above, but follows gcc.
1487 if (!RD->isUnion()) {
1488 if (HadField)
1489 return false;
1490
1491 HadField = true;
1492 }
1493 }
1494
1495 return true;
1496}
1497
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001498ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +00001499 ASTContext &Context,
1500 llvm::LLVMContext &VMContext) const {
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001501 if (RetTy->isVoidType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001502 return ABIArgInfo::getIgnore();
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001503
1504 if (!CodeGenFunction::hasAggregateLLVMType(RetTy))
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00001505 return (RetTy->isPromotableIntegerType() ?
1506 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001507
1508 // Are we following APCS?
1509 if (getABIKind() == APCS) {
1510 if (isEmptyRecord(Context, RetTy, false))
1511 return ABIArgInfo::getIgnore();
1512
1513 // Integer like structures are returned in r0.
1514 if (isIntegerLikeType(RetTy, Context, VMContext)) {
1515 // Return in the smallest viable integer type.
1516 uint64_t Size = Context.getTypeSize(RetTy);
1517 if (Size <= 8)
1518 return ABIArgInfo::getCoerce(llvm::Type::getInt8Ty(VMContext));
1519 if (Size <= 16)
1520 return ABIArgInfo::getCoerce(llvm::Type::getInt16Ty(VMContext));
1521 return ABIArgInfo::getCoerce(llvm::Type::getInt32Ty(VMContext));
1522 }
1523
1524 // Otherwise return in memory.
1525 return ABIArgInfo::getIndirect(0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001526 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001527
1528 // Otherwise this is an AAPCS variant.
1529
1530 // Aggregates <= 4 bytes are returned in r0; other aggregates
1531 // are returned indirectly.
1532 uint64_t Size = Context.getTypeSize(RetTy);
1533 if (Size <= 32)
1534 return ABIArgInfo::getCoerce(llvm::Type::getInt32Ty(VMContext));
1535 return ABIArgInfo::getIndirect(0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001536}
1537
1538llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1539 CodeGenFunction &CGF) const {
1540 // FIXME: Need to handle alignment
Mike Stump11289f42009-09-09 15:08:12 +00001541 const llvm::Type *BP =
Owen Anderson41a75022009-08-13 21:57:51 +00001542 llvm::PointerType::getUnqual(llvm::Type::getInt8Ty(CGF.getLLVMContext()));
Owen Anderson9793f0e2009-07-29 22:16:19 +00001543 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001544
1545 CGBuilderTy &Builder = CGF.Builder;
1546 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
1547 "ap");
1548 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
1549 llvm::Type *PTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +00001550 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001551 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
1552
1553 uint64_t Offset =
1554 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
1555 llvm::Value *NextAddr =
Owen Anderson41a75022009-08-13 21:57:51 +00001556 Builder.CreateGEP(Addr, llvm::ConstantInt::get(
1557 llvm::Type::getInt32Ty(CGF.getLLVMContext()), Offset),
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001558 "ap.next");
1559 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
1560
1561 return AddrTyped;
1562}
1563
1564ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +00001565 ASTContext &Context,
1566 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001567 if (RetTy->isVoidType()) {
1568 return ABIArgInfo::getIgnore();
1569 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1570 return ABIArgInfo::getIndirect(0);
1571 } else {
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00001572 return (RetTy->isPromotableIntegerType() ?
1573 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001574 }
1575}
1576
Daniel Dunbard59655c2009-09-12 00:59:49 +00001577// SystemZ ABI Implementation
1578
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00001579namespace {
Daniel Dunbard59655c2009-09-12 00:59:49 +00001580
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00001581class SystemZABIInfo : public ABIInfo {
1582 bool isPromotableIntegerType(QualType Ty) const;
1583
1584 ABIArgInfo classifyReturnType(QualType RetTy, ASTContext &Context,
1585 llvm::LLVMContext &VMContext) const;
1586
1587 ABIArgInfo classifyArgumentType(QualType RetTy, ASTContext &Context,
1588 llvm::LLVMContext &VMContext) const;
1589
1590 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1591 llvm::LLVMContext &VMContext) const {
1592 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(),
1593 Context, VMContext);
1594 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1595 it != ie; ++it)
1596 it->info = classifyArgumentType(it->type, Context, VMContext);
1597 }
1598
1599 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1600 CodeGenFunction &CGF) const;
1601};
Daniel Dunbard59655c2009-09-12 00:59:49 +00001602
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00001603}
1604
1605bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const {
1606 // SystemZ ABI requires all 8, 16 and 32 bit quantities to be extended.
1607 if (const BuiltinType *BT = Ty->getAsBuiltinType())
1608 switch (BT->getKind()) {
1609 case BuiltinType::Bool:
1610 case BuiltinType::Char_S:
1611 case BuiltinType::Char_U:
1612 case BuiltinType::SChar:
1613 case BuiltinType::UChar:
1614 case BuiltinType::Short:
1615 case BuiltinType::UShort:
1616 case BuiltinType::Int:
1617 case BuiltinType::UInt:
1618 return true;
1619 default:
1620 return false;
1621 }
1622 return false;
1623}
1624
1625llvm::Value *SystemZABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1626 CodeGenFunction &CGF) const {
1627 // FIXME: Implement
1628 return 0;
1629}
1630
1631
1632ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy,
1633 ASTContext &Context,
Daniel Dunbard59655c2009-09-12 00:59:49 +00001634 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00001635 if (RetTy->isVoidType()) {
1636 return ABIArgInfo::getIgnore();
1637 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1638 return ABIArgInfo::getIndirect(0);
1639 } else {
1640 return (isPromotableIntegerType(RetTy) ?
1641 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1642 }
1643}
1644
1645ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty,
1646 ASTContext &Context,
Daniel Dunbard59655c2009-09-12 00:59:49 +00001647 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00001648 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
1649 return ABIArgInfo::getIndirect(0);
1650 } else {
1651 return (isPromotableIntegerType(Ty) ?
1652 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1653 }
1654}
1655
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001656ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty,
Owen Anderson170229f2009-07-14 23:10:40 +00001657 ASTContext &Context,
1658 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001659 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
1660 return ABIArgInfo::getIndirect(0);
1661 } else {
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00001662 return (Ty->isPromotableIntegerType() ?
1663 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001664 }
1665}
1666
1667llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1668 CodeGenFunction &CGF) const {
1669 return 0;
1670}
1671
1672const ABIInfo &CodeGenTypes::getABIInfo() const {
1673 if (TheABIInfo)
1674 return *TheABIInfo;
1675
Daniel Dunbare3532f82009-08-24 08:52:16 +00001676 // For now we just cache the ABIInfo in CodeGenTypes and don't free it.
1677
Daniel Dunbar40165182009-08-24 09:10:05 +00001678 const llvm::Triple &Triple(getContext().Target.getTriple());
1679 switch (Triple.getArch()) {
Daniel Dunbare3532f82009-08-24 08:52:16 +00001680 default:
1681 return *(TheABIInfo = new DefaultABIInfo);
1682
Daniel Dunbard59655c2009-09-12 00:59:49 +00001683 case llvm::Triple::arm:
1684 case llvm::Triple::thumb:
Daniel Dunbar020daa92009-09-12 01:00:39 +00001685 // FIXME: We want to know the float calling convention as well.
Daniel Dunbarb4091a92009-09-14 00:35:03 +00001686 if (strcmp(getContext().Target.getABI(), "apcs-gnu") == 0)
Daniel Dunbar020daa92009-09-12 01:00:39 +00001687 return *(TheABIInfo = new ARMABIInfo(ARMABIInfo::APCS));
1688
1689 return *(TheABIInfo = new ARMABIInfo(ARMABIInfo::AAPCS));
Daniel Dunbard59655c2009-09-12 00:59:49 +00001690
1691 case llvm::Triple::pic16:
1692 return *(TheABIInfo = new PIC16ABIInfo());
1693
1694 case llvm::Triple::systemz:
1695 return *(TheABIInfo = new SystemZABIInfo());
1696
Daniel Dunbar40165182009-08-24 09:10:05 +00001697 case llvm::Triple::x86:
1698 if (Triple.getOS() == llvm::Triple::Darwin)
Daniel Dunbare3532f82009-08-24 08:52:16 +00001699 return *(TheABIInfo = new X86_32ABIInfo(Context, true, true));
1700
Daniel Dunbar40165182009-08-24 09:10:05 +00001701 switch (Triple.getOS()) {
Daniel Dunbare3532f82009-08-24 08:52:16 +00001702 case llvm::Triple::Cygwin:
1703 case llvm::Triple::DragonFly:
1704 case llvm::Triple::MinGW32:
1705 case llvm::Triple::MinGW64:
David Chisnall2c5bef22009-09-03 01:48:05 +00001706 case llvm::Triple::FreeBSD:
Daniel Dunbare3532f82009-08-24 08:52:16 +00001707 case llvm::Triple::OpenBSD:
1708 return *(TheABIInfo = new X86_32ABIInfo(Context, false, true));
1709
1710 default:
1711 return *(TheABIInfo = new X86_32ABIInfo(Context, false, false));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001712 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001713
Daniel Dunbare3532f82009-08-24 08:52:16 +00001714 case llvm::Triple::x86_64:
1715 return *(TheABIInfo = new X86_64ABIInfo());
Daniel Dunbare3532f82009-08-24 08:52:16 +00001716 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001717}