blob: 77b19bdbd12ae502a13fe7e436e6829d5d2a295b [file] [log] [blame]
Anton Korobeynikov73628192009-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 Carlsson63f1ad92009-07-18 19:43:29 +000017#include "clang/AST/RecordLayout.h"
Anton Korobeynikov73628192009-06-05 22:08:42 +000018#include "llvm/Type.h"
19
20using namespace clang;
21using namespace CodeGen;
22
23ABIInfo::~ABIInfo() {}
24
25void ABIArgInfo::dump() const {
26 fprintf(stderr, "(ABIArgInfo Kind=");
27 switch (TheKind) {
28 case Direct:
29 fprintf(stderr, "Direct");
30 break;
Anton Korobeynikov2ce305d2009-06-06 09:36:29 +000031 case Extend:
32 fprintf(stderr, "Extend");
33 break;
Anton Korobeynikov73628192009-06-05 22:08:42 +000034 case Ignore:
35 fprintf(stderr, "Ignore");
36 break;
37 case Coerce:
38 fprintf(stderr, "Coerce Type=");
39 getCoerceToType()->print(llvm::errs());
40 break;
41 case Indirect:
42 fprintf(stderr, "Indirect Align=%d", getIndirectAlign());
43 break;
44 case Expand:
45 fprintf(stderr, "Expand");
46 break;
47 }
48 fprintf(stderr, ")\n");
49}
50
51static bool isEmptyRecord(ASTContext &Context, QualType T);
52
53/// isEmptyField - Return true iff a the field is "empty", that is it
54/// is an unnamed bit-field or an (array of) empty record(s).
55static bool isEmptyField(ASTContext &Context, const FieldDecl *FD) {
56 if (FD->isUnnamedBitfield())
57 return true;
58
59 QualType FT = FD->getType();
60 // Constant arrays of empty records count as empty, strip them off.
61 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT))
62 FT = AT->getElementType();
63
64 return isEmptyRecord(Context, FT);
65}
66
67/// isEmptyRecord - Return true iff a structure contains only empty
68/// fields. Note that a structure with a flexible array member is not
69/// considered empty.
70static bool isEmptyRecord(ASTContext &Context, QualType T) {
Ted Kremenekd00cd9e2009-07-29 21:53:49 +000071 const RecordType *RT = T->getAs<RecordType>();
Anton Korobeynikov73628192009-06-05 22:08:42 +000072 if (!RT)
73 return 0;
74 const RecordDecl *RD = RT->getDecl();
75 if (RD->hasFlexibleArrayMember())
76 return false;
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +000077 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
78 i != e; ++i)
Anton Korobeynikov73628192009-06-05 22:08:42 +000079 if (!isEmptyField(Context, *i))
80 return false;
81 return true;
82}
83
84/// isSingleElementStruct - Determine if a structure is a "single
85/// element struct", i.e. it has exactly one non-empty field or
86/// exactly one field which is itself a single element
87/// struct. Structures with flexible array members are never
88/// considered single element structs.
89///
90/// \return The field declaration for the single non-empty field, if
91/// it exists.
92static const Type *isSingleElementStruct(QualType T, ASTContext &Context) {
93 const RecordType *RT = T->getAsStructureType();
94 if (!RT)
95 return 0;
96
97 const RecordDecl *RD = RT->getDecl();
98 if (RD->hasFlexibleArrayMember())
99 return 0;
100
101 const Type *Found = 0;
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000102 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
103 i != e; ++i) {
Anton Korobeynikov73628192009-06-05 22:08:42 +0000104 const FieldDecl *FD = *i;
105 QualType FT = FD->getType();
106
107 // Ignore empty fields.
108 if (isEmptyField(Context, FD))
109 continue;
110
111 // If we already found an element then this isn't a single-element
112 // struct.
113 if (Found)
114 return 0;
115
116 // Treat single element arrays as the element.
117 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
118 if (AT->getSize().getZExtValue() != 1)
119 break;
120 FT = AT->getElementType();
121 }
122
123 if (!CodeGenFunction::hasAggregateLLVMType(FT)) {
124 Found = FT.getTypePtr();
125 } else {
126 Found = isSingleElementStruct(FT, Context);
127 if (!Found)
128 return 0;
129 }
130 }
131
132 return Found;
133}
134
135static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
136 if (!Ty->getAsBuiltinType() && !Ty->isPointerType())
137 return false;
138
139 uint64_t Size = Context.getTypeSize(Ty);
140 return Size == 32 || Size == 64;
141}
142
143static bool areAllFields32Or64BitBasicType(const RecordDecl *RD,
144 ASTContext &Context) {
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000145 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
146 i != e; ++i) {
Anton Korobeynikov73628192009-06-05 22:08:42 +0000147 const FieldDecl *FD = *i;
148
149 if (!is32Or64BitBasicType(FD->getType(), Context))
150 return false;
151
152 // FIXME: Reject bit-fields wholesale; there are two problems, we don't know
153 // how to expand them yet, and the predicate for telling if a bitfield still
154 // counts as "basic" is more complicated than what we were doing previously.
155 if (FD->isBitField())
156 return false;
157 }
158
159 return true;
160}
161
Eli Friedman621de772009-06-13 21:37:10 +0000162static bool typeContainsSSEVector(const RecordDecl *RD, ASTContext &Context) {
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000163 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
164 i != e; ++i) {
Eli Friedman621de772009-06-13 21:37:10 +0000165 const FieldDecl *FD = *i;
166
167 if (FD->getType()->isVectorType() &&
168 Context.getTypeSize(FD->getType()) >= 128)
169 return true;
170
Ted Kremenekd00cd9e2009-07-29 21:53:49 +0000171 if (const RecordType* RT = FD->getType()->getAs<RecordType>())
Eli Friedman621de772009-06-13 21:37:10 +0000172 if (typeContainsSSEVector(RT->getDecl(), Context))
173 return true;
174 }
175
176 return false;
177}
178
Anton Korobeynikov73628192009-06-05 22:08:42 +0000179namespace {
180/// DefaultABIInfo - The default implementation for ABI specific
181/// details. This implementation provides information which results in
182/// self-consistent and sensible LLVM IR generation, but does not
183/// conform to any particular ABI.
184class DefaultABIInfo : public ABIInfo {
185 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Anderson73e7f802009-07-14 23:10:40 +0000186 ASTContext &Context,
187 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov73628192009-06-05 22:08:42 +0000188
189 ABIArgInfo classifyArgumentType(QualType RetTy,
Owen Anderson73e7f802009-07-14 23:10:40 +0000190 ASTContext &Context,
191 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov73628192009-06-05 22:08:42 +0000192
Owen Anderson73e7f802009-07-14 23:10:40 +0000193 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
194 llvm::LLVMContext &VMContext) const {
195 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context,
196 VMContext);
Anton Korobeynikov73628192009-06-05 22:08:42 +0000197 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
198 it != ie; ++it)
Owen Anderson73e7f802009-07-14 23:10:40 +0000199 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikov73628192009-06-05 22:08:42 +0000200 }
201
202 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
203 CodeGenFunction &CGF) const;
204};
205
206/// X86_32ABIInfo - The X86-32 ABI information.
207class X86_32ABIInfo : public ABIInfo {
208 ASTContext &Context;
David Chisnall09987c52009-08-17 23:08:21 +0000209 bool IsDarwinVectorABI;
210 bool IsSmallStructInRegABI;
Anton Korobeynikov73628192009-06-05 22:08:42 +0000211
212 static bool isRegisterSize(unsigned Size) {
213 return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
214 }
215
216 static bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context);
217
Eli Friedman621de772009-06-13 21:37:10 +0000218 static unsigned getIndirectArgumentAlignment(QualType Ty,
219 ASTContext &Context);
220
Anton Korobeynikov73628192009-06-05 22:08:42 +0000221public:
222 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Anderson73e7f802009-07-14 23:10:40 +0000223 ASTContext &Context,
224 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov73628192009-06-05 22:08:42 +0000225
226 ABIArgInfo classifyArgumentType(QualType RetTy,
Owen Anderson73e7f802009-07-14 23:10:40 +0000227 ASTContext &Context,
228 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov73628192009-06-05 22:08:42 +0000229
Owen Anderson73e7f802009-07-14 23:10:40 +0000230 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
231 llvm::LLVMContext &VMContext) const {
232 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context,
233 VMContext);
Anton Korobeynikov73628192009-06-05 22:08:42 +0000234 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
235 it != ie; ++it)
Owen Anderson73e7f802009-07-14 23:10:40 +0000236 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikov73628192009-06-05 22:08:42 +0000237 }
238
239 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
240 CodeGenFunction &CGF) const;
241
David Chisnall09987c52009-08-17 23:08:21 +0000242 X86_32ABIInfo(ASTContext &Context, bool d, bool p)
243 : ABIInfo(), Context(Context), IsDarwinVectorABI(d),
244 IsSmallStructInRegABI(p) {}
Anton Korobeynikov73628192009-06-05 22:08:42 +0000245};
246}
247
248
249/// shouldReturnTypeInRegister - Determine if the given type should be
250/// passed in a register (for the Darwin ABI).
251bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
252 ASTContext &Context) {
253 uint64_t Size = Context.getTypeSize(Ty);
254
255 // Type must be register sized.
256 if (!isRegisterSize(Size))
257 return false;
258
259 if (Ty->isVectorType()) {
260 // 64- and 128- bit vectors inside structures are not returned in
261 // registers.
262 if (Size == 64 || Size == 128)
263 return false;
264
265 return true;
266 }
267
268 // If this is a builtin, pointer, or complex type, it is ok.
269 if (Ty->getAsBuiltinType() || Ty->isPointerType() || Ty->isAnyComplexType())
270 return true;
271
272 // Arrays are treated like records.
273 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
274 return shouldReturnTypeInRegister(AT->getElementType(), Context);
275
276 // Otherwise, it must be a record type.
Ted Kremenekd00cd9e2009-07-29 21:53:49 +0000277 const RecordType *RT = Ty->getAs<RecordType>();
Anton Korobeynikov73628192009-06-05 22:08:42 +0000278 if (!RT) return false;
279
280 // Structure types are passed in register if all fields would be
281 // passed in a register.
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000282 for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(),
283 e = RT->getDecl()->field_end(); i != e; ++i) {
Anton Korobeynikov73628192009-06-05 22:08:42 +0000284 const FieldDecl *FD = *i;
285
286 // Empty fields are ignored.
287 if (isEmptyField(Context, FD))
288 continue;
289
290 // Check fields recursively.
291 if (!shouldReturnTypeInRegister(FD->getType(), Context))
292 return false;
293 }
294
295 return true;
296}
297
298ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
Owen Anderson73e7f802009-07-14 23:10:40 +0000299 ASTContext &Context,
300 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov73628192009-06-05 22:08:42 +0000301 if (RetTy->isVoidType()) {
302 return ABIArgInfo::getIgnore();
303 } else if (const VectorType *VT = RetTy->getAsVectorType()) {
304 // On Darwin, some vectors are returned in registers.
David Chisnall09987c52009-08-17 23:08:21 +0000305 if (IsDarwinVectorABI) {
Anton Korobeynikov73628192009-06-05 22:08:42 +0000306 uint64_t Size = Context.getTypeSize(RetTy);
307
308 // 128-bit vectors are a special case; they are returned in
309 // registers and we need to make sure to pick a type the LLVM
310 // backend will like.
311 if (Size == 128)
Owen Anderson3f5cc0a2009-08-13 21:57:51 +0000312 return ABIArgInfo::getCoerce(llvm::VectorType::get(
313 llvm::Type::getInt64Ty(VMContext), 2));
Anton Korobeynikov73628192009-06-05 22:08:42 +0000314
315 // Always return in register if it fits in a general purpose
316 // register, or if it is 64 bits and has a single element.
317 if ((Size == 8 || Size == 16 || Size == 32) ||
318 (Size == 64 && VT->getNumElements() == 1))
Owen Anderson3f5cc0a2009-08-13 21:57:51 +0000319 return ABIArgInfo::getCoerce(llvm::IntegerType::get(VMContext, Size));
Anton Korobeynikov73628192009-06-05 22:08:42 +0000320
321 return ABIArgInfo::getIndirect(0);
322 }
323
324 return ABIArgInfo::getDirect();
325 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
326 // Structures with flexible arrays are always indirect.
327 if (const RecordType *RT = RetTy->getAsStructureType())
328 if (RT->getDecl()->hasFlexibleArrayMember())
329 return ABIArgInfo::getIndirect(0);
330
David Chisnall09987c52009-08-17 23:08:21 +0000331 // If specified, structs and unions are always indirect.
332 if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType())
Anton Korobeynikov73628192009-06-05 22:08:42 +0000333 return ABIArgInfo::getIndirect(0);
334
335 // Classify "single element" structs as their element type.
336 if (const Type *SeltTy = isSingleElementStruct(RetTy, Context)) {
337 if (const BuiltinType *BT = SeltTy->getAsBuiltinType()) {
338 if (BT->isIntegerType()) {
339 // We need to use the size of the structure, padding
340 // bit-fields can adjust that to be larger than the single
341 // element type.
342 uint64_t Size = Context.getTypeSize(RetTy);
Owen Anderson73e7f802009-07-14 23:10:40 +0000343 return ABIArgInfo::getCoerce(
Owen Anderson3f5cc0a2009-08-13 21:57:51 +0000344 llvm::IntegerType::get(VMContext, (unsigned) Size));
Anton Korobeynikov73628192009-06-05 22:08:42 +0000345 } else if (BT->getKind() == BuiltinType::Float) {
346 assert(Context.getTypeSize(RetTy) == Context.getTypeSize(SeltTy) &&
347 "Unexpect single element structure size!");
Owen Anderson3f5cc0a2009-08-13 21:57:51 +0000348 return ABIArgInfo::getCoerce(llvm::Type::getFloatTy(VMContext));
Anton Korobeynikov73628192009-06-05 22:08:42 +0000349 } else if (BT->getKind() == BuiltinType::Double) {
350 assert(Context.getTypeSize(RetTy) == Context.getTypeSize(SeltTy) &&
351 "Unexpect single element structure size!");
Owen Anderson3f5cc0a2009-08-13 21:57:51 +0000352 return ABIArgInfo::getCoerce(llvm::Type::getDoubleTy(VMContext));
Anton Korobeynikov73628192009-06-05 22:08:42 +0000353 }
354 } else if (SeltTy->isPointerType()) {
355 // FIXME: It would be really nice if this could come out as the proper
356 // pointer type.
357 llvm::Type *PtrTy =
Owen Anderson3f5cc0a2009-08-13 21:57:51 +0000358 llvm::PointerType::getUnqual(llvm::Type::getInt8Ty(VMContext));
Anton Korobeynikov73628192009-06-05 22:08:42 +0000359 return ABIArgInfo::getCoerce(PtrTy);
360 } else if (SeltTy->isVectorType()) {
361 // 64- and 128-bit vectors are never returned in a
362 // register when inside a structure.
363 uint64_t Size = Context.getTypeSize(RetTy);
364 if (Size == 64 || Size == 128)
365 return ABIArgInfo::getIndirect(0);
366
Owen Anderson73e7f802009-07-14 23:10:40 +0000367 return classifyReturnType(QualType(SeltTy, 0), Context, VMContext);
Anton Korobeynikov73628192009-06-05 22:08:42 +0000368 }
369 }
370
371 // Small structures which are register sized are generally returned
372 // in a register.
373 if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, Context)) {
374 uint64_t Size = Context.getTypeSize(RetTy);
Owen Anderson3f5cc0a2009-08-13 21:57:51 +0000375 return ABIArgInfo::getCoerce(llvm::IntegerType::get(VMContext, Size));
Anton Korobeynikov73628192009-06-05 22:08:42 +0000376 }
377
378 return ABIArgInfo::getIndirect(0);
379 } else {
Anton Korobeynikov2ce305d2009-06-06 09:36:29 +0000380 return (RetTy->isPromotableIntegerType() ?
381 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov73628192009-06-05 22:08:42 +0000382 }
383}
384
Eli Friedman621de772009-06-13 21:37:10 +0000385unsigned X86_32ABIInfo::getIndirectArgumentAlignment(QualType Ty,
386 ASTContext &Context) {
387 unsigned Align = Context.getTypeAlign(Ty);
388 if (Align < 128) return 0;
Ted Kremenekd00cd9e2009-07-29 21:53:49 +0000389 if (const RecordType* RT = Ty->getAs<RecordType>())
Eli Friedman621de772009-06-13 21:37:10 +0000390 if (typeContainsSSEVector(RT->getDecl(), Context))
391 return 16;
392 return 0;
393}
394
Anton Korobeynikov73628192009-06-05 22:08:42 +0000395ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
Owen Anderson73e7f802009-07-14 23:10:40 +0000396 ASTContext &Context,
397 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov73628192009-06-05 22:08:42 +0000398 // FIXME: Set alignment on indirect arguments.
399 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
400 // Structures with flexible arrays are always indirect.
401 if (const RecordType *RT = Ty->getAsStructureType())
402 if (RT->getDecl()->hasFlexibleArrayMember())
Eli Friedman621de772009-06-13 21:37:10 +0000403 return ABIArgInfo::getIndirect(getIndirectArgumentAlignment(Ty,
404 Context));
Anton Korobeynikov73628192009-06-05 22:08:42 +0000405
406 // Ignore empty structs.
Eli Friedman621de772009-06-13 21:37:10 +0000407 if (Ty->isStructureType() && Context.getTypeSize(Ty) == 0)
Anton Korobeynikov73628192009-06-05 22:08:42 +0000408 return ABIArgInfo::getIgnore();
409
410 // Expand structs with size <= 128-bits which consist only of
411 // basic types (int, long long, float, double, xxx*). This is
412 // non-recursive and does not ignore empty fields.
413 if (const RecordType *RT = Ty->getAsStructureType()) {
414 if (Context.getTypeSize(Ty) <= 4*32 &&
415 areAllFields32Or64BitBasicType(RT->getDecl(), Context))
416 return ABIArgInfo::getExpand();
417 }
418
Eli Friedman621de772009-06-13 21:37:10 +0000419 return ABIArgInfo::getIndirect(getIndirectArgumentAlignment(Ty, Context));
Anton Korobeynikov73628192009-06-05 22:08:42 +0000420 } else {
Anton Korobeynikov2ce305d2009-06-06 09:36:29 +0000421 return (Ty->isPromotableIntegerType() ?
422 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov73628192009-06-05 22:08:42 +0000423 }
424}
425
426llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
427 CodeGenFunction &CGF) const {
Owen Anderson3f5cc0a2009-08-13 21:57:51 +0000428 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::getInt8Ty(CGF.getLLVMContext()));
Owen Anderson7ec2d8f2009-07-29 22:16:19 +0000429 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikov73628192009-06-05 22:08:42 +0000430
431 CGBuilderTy &Builder = CGF.Builder;
432 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
433 "ap");
434 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
435 llvm::Type *PTy =
Owen Anderson7ec2d8f2009-07-29 22:16:19 +0000436 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikov73628192009-06-05 22:08:42 +0000437 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
438
439 uint64_t Offset =
440 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
441 llvm::Value *NextAddr =
Owen Anderson3f5cc0a2009-08-13 21:57:51 +0000442 Builder.CreateGEP(Addr, llvm::ConstantInt::get(
443 llvm::Type::getInt32Ty(CGF.getLLVMContext()), Offset),
Anton Korobeynikov73628192009-06-05 22:08:42 +0000444 "ap.next");
445 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
446
447 return AddrTyped;
448}
449
450namespace {
451/// X86_64ABIInfo - The X86_64 ABI information.
452class X86_64ABIInfo : public ABIInfo {
453 enum Class {
454 Integer = 0,
455 SSE,
456 SSEUp,
457 X87,
458 X87Up,
459 ComplexX87,
460 NoClass,
461 Memory
462 };
463
464 /// merge - Implement the X86_64 ABI merging algorithm.
465 ///
466 /// Merge an accumulating classification \arg Accum with a field
467 /// classification \arg Field.
468 ///
469 /// \param Accum - The accumulating classification. This should
470 /// always be either NoClass or the result of a previous merge
471 /// call. In addition, this should never be Memory (the caller
472 /// should just return Memory for the aggregate).
473 Class merge(Class Accum, Class Field) const;
474
475 /// classify - Determine the x86_64 register classes in which the
476 /// given type T should be passed.
477 ///
478 /// \param Lo - The classification for the parts of the type
479 /// residing in the low word of the containing object.
480 ///
481 /// \param Hi - The classification for the parts of the type
482 /// residing in the high word of the containing object.
483 ///
484 /// \param OffsetBase - The bit offset of this type in the
485 /// containing object. Some parameters are classified different
486 /// depending on whether they straddle an eightbyte boundary.
487 ///
488 /// If a word is unused its result will be NoClass; if a type should
489 /// be passed in Memory then at least the classification of \arg Lo
490 /// will be Memory.
491 ///
492 /// The \arg Lo class will be NoClass iff the argument is ignored.
493 ///
494 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
495 /// also be ComplexX87.
496 void classify(QualType T, ASTContext &Context, uint64_t OffsetBase,
497 Class &Lo, Class &Hi) const;
498
499 /// getCoerceResult - Given a source type \arg Ty and an LLVM type
500 /// to coerce to, chose the best way to pass Ty in the same place
501 /// that \arg CoerceTo would be passed, but while keeping the
502 /// emitted code as simple as possible.
503 ///
504 /// FIXME: Note, this should be cleaned up to just take an enumeration of all
505 /// the ways we might want to pass things, instead of constructing an LLVM
506 /// type. This makes this code more explicit, and it makes it clearer that we
507 /// are also doing this for correctness in the case of passing scalar types.
508 ABIArgInfo getCoerceResult(QualType Ty,
509 const llvm::Type *CoerceTo,
510 ASTContext &Context) const;
511
512 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
513 /// such that the argument will be passed in memory.
514 ABIArgInfo getIndirectResult(QualType Ty,
515 ASTContext &Context) const;
516
517 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Anderson73e7f802009-07-14 23:10:40 +0000518 ASTContext &Context,
519 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov73628192009-06-05 22:08:42 +0000520
521 ABIArgInfo classifyArgumentType(QualType Ty,
522 ASTContext &Context,
Owen Anderson73e7f802009-07-14 23:10:40 +0000523 llvm::LLVMContext &VMContext,
Anton Korobeynikov73628192009-06-05 22:08:42 +0000524 unsigned &neededInt,
525 unsigned &neededSSE) const;
526
527public:
Owen Anderson73e7f802009-07-14 23:10:40 +0000528 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
529 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov73628192009-06-05 22:08:42 +0000530
531 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
532 CodeGenFunction &CGF) const;
533};
534}
535
536X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum,
537 Class Field) const {
538 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
539 // classified recursively so that always two fields are
540 // considered. The resulting class is calculated according to
541 // the classes of the fields in the eightbyte:
542 //
543 // (a) If both classes are equal, this is the resulting class.
544 //
545 // (b) If one of the classes is NO_CLASS, the resulting class is
546 // the other class.
547 //
548 // (c) If one of the classes is MEMORY, the result is the MEMORY
549 // class.
550 //
551 // (d) If one of the classes is INTEGER, the result is the
552 // INTEGER.
553 //
554 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
555 // MEMORY is used as class.
556 //
557 // (f) Otherwise class SSE is used.
558
559 // Accum should never be memory (we should have returned) or
560 // ComplexX87 (because this cannot be passed in a structure).
561 assert((Accum != Memory && Accum != ComplexX87) &&
562 "Invalid accumulated classification during merge.");
563 if (Accum == Field || Field == NoClass)
564 return Accum;
565 else if (Field == Memory)
566 return Memory;
567 else if (Accum == NoClass)
568 return Field;
569 else if (Accum == Integer || Field == Integer)
570 return Integer;
571 else if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
572 Accum == X87 || Accum == X87Up)
573 return Memory;
574 else
575 return SSE;
576}
577
578void X86_64ABIInfo::classify(QualType Ty,
579 ASTContext &Context,
580 uint64_t OffsetBase,
581 Class &Lo, Class &Hi) const {
582 // FIXME: This code can be simplified by introducing a simple value class for
583 // Class pairs with appropriate constructor methods for the various
584 // situations.
585
586 // FIXME: Some of the split computations are wrong; unaligned vectors
587 // shouldn't be passed in registers for example, so there is no chance they
588 // can straddle an eightbyte. Verify & simplify.
589
590 Lo = Hi = NoClass;
591
592 Class &Current = OffsetBase < 64 ? Lo : Hi;
593 Current = Memory;
594
595 if (const BuiltinType *BT = Ty->getAsBuiltinType()) {
596 BuiltinType::Kind k = BT->getKind();
597
598 if (k == BuiltinType::Void) {
599 Current = NoClass;
600 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
601 Lo = Integer;
602 Hi = Integer;
603 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
604 Current = Integer;
605 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
606 Current = SSE;
607 } else if (k == BuiltinType::LongDouble) {
608 Lo = X87;
609 Hi = X87Up;
610 }
611 // FIXME: _Decimal32 and _Decimal64 are SSE.
612 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
613 } else if (const EnumType *ET = Ty->getAsEnumType()) {
614 // Classify the underlying integer type.
615 classify(ET->getDecl()->getIntegerType(), Context, OffsetBase, Lo, Hi);
616 } else if (Ty->hasPointerRepresentation()) {
617 Current = Integer;
618 } else if (const VectorType *VT = Ty->getAsVectorType()) {
619 uint64_t Size = Context.getTypeSize(VT);
620 if (Size == 32) {
621 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
622 // float> as integer.
623 Current = Integer;
624
625 // If this type crosses an eightbyte boundary, it should be
626 // split.
627 uint64_t EB_Real = (OffsetBase) / 64;
628 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
629 if (EB_Real != EB_Imag)
630 Hi = Lo;
631 } else if (Size == 64) {
632 // gcc passes <1 x double> in memory. :(
633 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
634 return;
635
636 // gcc passes <1 x long long> as INTEGER.
637 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong))
638 Current = Integer;
639 else
640 Current = SSE;
641
642 // If this type crosses an eightbyte boundary, it should be
643 // split.
644 if (OffsetBase && OffsetBase != 64)
645 Hi = Lo;
646 } else if (Size == 128) {
647 Lo = SSE;
648 Hi = SSEUp;
649 }
650 } else if (const ComplexType *CT = Ty->getAsComplexType()) {
651 QualType ET = Context.getCanonicalType(CT->getElementType());
652
653 uint64_t Size = Context.getTypeSize(Ty);
654 if (ET->isIntegralType()) {
655 if (Size <= 64)
656 Current = Integer;
657 else if (Size <= 128)
658 Lo = Hi = Integer;
659 } else if (ET == Context.FloatTy)
660 Current = SSE;
661 else if (ET == Context.DoubleTy)
662 Lo = Hi = SSE;
663 else if (ET == Context.LongDoubleTy)
664 Current = ComplexX87;
665
666 // If this complex type crosses an eightbyte boundary then it
667 // should be split.
668 uint64_t EB_Real = (OffsetBase) / 64;
669 uint64_t EB_Imag = (OffsetBase + Context.getTypeSize(ET)) / 64;
670 if (Hi == NoClass && EB_Real != EB_Imag)
671 Hi = Lo;
672 } else if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
673 // Arrays are treated like structures.
674
675 uint64_t Size = Context.getTypeSize(Ty);
676
677 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
678 // than two eightbytes, ..., it has class MEMORY.
679 if (Size > 128)
680 return;
681
682 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
683 // fields, it has class MEMORY.
684 //
685 // Only need to check alignment of array base.
686 if (OffsetBase % Context.getTypeAlign(AT->getElementType()))
687 return;
688
689 // Otherwise implement simplified merge. We could be smarter about
690 // this, but it isn't worth it and would be harder to verify.
691 Current = NoClass;
692 uint64_t EltSize = Context.getTypeSize(AT->getElementType());
693 uint64_t ArraySize = AT->getSize().getZExtValue();
694 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
695 Class FieldLo, FieldHi;
696 classify(AT->getElementType(), Context, Offset, FieldLo, FieldHi);
697 Lo = merge(Lo, FieldLo);
698 Hi = merge(Hi, FieldHi);
699 if (Lo == Memory || Hi == Memory)
700 break;
701 }
702
703 // Do post merger cleanup (see below). Only case we worry about is Memory.
704 if (Hi == Memory)
705 Lo = Memory;
706 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Ted Kremenekd00cd9e2009-07-29 21:53:49 +0000707 } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
Anton Korobeynikov73628192009-06-05 22:08:42 +0000708 uint64_t Size = Context.getTypeSize(Ty);
709
710 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
711 // than two eightbytes, ..., it has class MEMORY.
712 if (Size > 128)
713 return;
714
715 const RecordDecl *RD = RT->getDecl();
716
717 // Assume variable sized types are passed in memory.
718 if (RD->hasFlexibleArrayMember())
719 return;
720
721 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
722
723 // Reset Lo class, this will be recomputed.
724 Current = NoClass;
725 unsigned idx = 0;
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000726 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
727 i != e; ++i, ++idx) {
Anton Korobeynikov73628192009-06-05 22:08:42 +0000728 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
729 bool BitField = i->isBitField();
730
731 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
732 // fields, it has class MEMORY.
733 //
734 // Note, skip this test for bit-fields, see below.
735 if (!BitField && Offset % Context.getTypeAlign(i->getType())) {
736 Lo = Memory;
737 return;
738 }
739
740 // Classify this field.
741 //
742 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
743 // exceeds a single eightbyte, each is classified
744 // separately. Each eightbyte gets initialized to class
745 // NO_CLASS.
746 Class FieldLo, FieldHi;
747
748 // Bit-fields require special handling, they do not force the
749 // structure to be passed in memory even if unaligned, and
750 // therefore they can straddle an eightbyte.
751 if (BitField) {
752 // Ignore padding bit-fields.
753 if (i->isUnnamedBitfield())
754 continue;
755
756 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
757 uint64_t Size = i->getBitWidth()->EvaluateAsInt(Context).getZExtValue();
758
759 uint64_t EB_Lo = Offset / 64;
760 uint64_t EB_Hi = (Offset + Size - 1) / 64;
761 FieldLo = FieldHi = NoClass;
762 if (EB_Lo) {
763 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
764 FieldLo = NoClass;
765 FieldHi = Integer;
766 } else {
767 FieldLo = Integer;
768 FieldHi = EB_Hi ? Integer : NoClass;
769 }
770 } else
771 classify(i->getType(), Context, Offset, FieldLo, FieldHi);
772 Lo = merge(Lo, FieldLo);
773 Hi = merge(Hi, FieldHi);
774 if (Lo == Memory || Hi == Memory)
775 break;
776 }
777
778 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
779 //
780 // (a) If one of the classes is MEMORY, the whole argument is
781 // passed in memory.
782 //
783 // (b) If SSEUP is not preceeded by SSE, it is converted to SSE.
784
785 // The first of these conditions is guaranteed by how we implement
786 // the merge (just bail).
787 //
788 // The second condition occurs in the case of unions; for example
789 // union { _Complex double; unsigned; }.
790 if (Hi == Memory)
791 Lo = Memory;
792 if (Hi == SSEUp && Lo != SSE)
793 Hi = SSE;
794 }
795}
796
797ABIArgInfo X86_64ABIInfo::getCoerceResult(QualType Ty,
798 const llvm::Type *CoerceTo,
799 ASTContext &Context) const {
Owen Anderson3f5cc0a2009-08-13 21:57:51 +0000800 if (CoerceTo == llvm::Type::getInt64Ty(CoerceTo->getContext())) {
Anton Korobeynikov73628192009-06-05 22:08:42 +0000801 // Integer and pointer types will end up in a general purpose
802 // register.
803 if (Ty->isIntegralType() || Ty->isPointerType())
Anton Korobeynikov2ce305d2009-06-06 09:36:29 +0000804 return (Ty->isPromotableIntegerType() ?
805 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Owen Anderson3f5cc0a2009-08-13 21:57:51 +0000806 } else if (CoerceTo == llvm::Type::getDoubleTy(CoerceTo->getContext())) {
Anton Korobeynikov73628192009-06-05 22:08:42 +0000807 // FIXME: It would probably be better to make CGFunctionInfo only map using
808 // canonical types than to canonize here.
809 QualType CTy = Context.getCanonicalType(Ty);
810
811 // Float and double end up in a single SSE reg.
812 if (CTy == Context.FloatTy || CTy == Context.DoubleTy)
813 return ABIArgInfo::getDirect();
814
815 }
816
817 return ABIArgInfo::getCoerce(CoerceTo);
818}
819
820ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty,
821 ASTContext &Context) const {
822 // If this is a scalar LLVM value then assume LLVM will pass it in the right
823 // place naturally.
824 if (!CodeGenFunction::hasAggregateLLVMType(Ty))
Anton Korobeynikov2ce305d2009-06-06 09:36:29 +0000825 return (Ty->isPromotableIntegerType() ?
826 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov73628192009-06-05 22:08:42 +0000827
828 // FIXME: Set alignment correctly.
829 return ABIArgInfo::getIndirect(0);
830}
831
832ABIArgInfo X86_64ABIInfo::classifyReturnType(QualType RetTy,
Owen Anderson73e7f802009-07-14 23:10:40 +0000833 ASTContext &Context,
834 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov73628192009-06-05 22:08:42 +0000835 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
836 // classification algorithm.
837 X86_64ABIInfo::Class Lo, Hi;
838 classify(RetTy, Context, 0, Lo, Hi);
839
840 // Check some invariants.
841 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
842 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
843 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
844
845 const llvm::Type *ResType = 0;
846 switch (Lo) {
847 case NoClass:
848 return ABIArgInfo::getIgnore();
849
850 case SSEUp:
851 case X87Up:
852 assert(0 && "Invalid classification for lo word.");
853
854 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
855 // hidden argument.
856 case Memory:
857 return getIndirectResult(RetTy, Context);
858
859 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
860 // available register of the sequence %rax, %rdx is used.
861 case Integer:
Owen Anderson3f5cc0a2009-08-13 21:57:51 +0000862 ResType = llvm::Type::getInt64Ty(VMContext); break;
Anton Korobeynikov73628192009-06-05 22:08:42 +0000863
864 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
865 // available SSE register of the sequence %xmm0, %xmm1 is used.
866 case SSE:
Owen Anderson3f5cc0a2009-08-13 21:57:51 +0000867 ResType = llvm::Type::getDoubleTy(VMContext); break;
Anton Korobeynikov73628192009-06-05 22:08:42 +0000868
869 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
870 // returned on the X87 stack in %st0 as 80-bit x87 number.
871 case X87:
Owen Anderson3f5cc0a2009-08-13 21:57:51 +0000872 ResType = llvm::Type::getX86_FP80Ty(VMContext); break;
Anton Korobeynikov73628192009-06-05 22:08:42 +0000873
874 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
875 // part of the value is returned in %st0 and the imaginary part in
876 // %st1.
877 case ComplexX87:
878 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Owen Anderson3f5cc0a2009-08-13 21:57:51 +0000879 ResType = llvm::StructType::get(VMContext, llvm::Type::getX86_FP80Ty(VMContext),
880 llvm::Type::getX86_FP80Ty(VMContext),
Anton Korobeynikov73628192009-06-05 22:08:42 +0000881 NULL);
882 break;
883 }
884
885 switch (Hi) {
886 // Memory was handled previously and X87 should
887 // never occur as a hi class.
888 case Memory:
889 case X87:
890 assert(0 && "Invalid classification for hi word.");
891
892 case ComplexX87: // Previously handled.
893 case NoClass: break;
894
895 case Integer:
Owen Andersonbdc004c2009-08-05 23:18:46 +0000896 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson3f5cc0a2009-08-13 21:57:51 +0000897 llvm::Type::getInt64Ty(VMContext), NULL);
Anton Korobeynikov73628192009-06-05 22:08:42 +0000898 break;
899 case SSE:
Owen Andersonbdc004c2009-08-05 23:18:46 +0000900 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson3f5cc0a2009-08-13 21:57:51 +0000901 llvm::Type::getDoubleTy(VMContext), NULL);
Anton Korobeynikov73628192009-06-05 22:08:42 +0000902 break;
903
904 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
905 // is passed in the upper half of the last used SSE register.
906 //
907 // SSEUP should always be preceeded by SSE, just widen.
908 case SSEUp:
909 assert(Lo == SSE && "Unexpected SSEUp classification.");
Owen Anderson3f5cc0a2009-08-13 21:57:51 +0000910 ResType = llvm::VectorType::get(llvm::Type::getDoubleTy(VMContext), 2);
Anton Korobeynikov73628192009-06-05 22:08:42 +0000911 break;
912
913 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
914 // returned together with the previous X87 value in %st0.
915 case X87Up:
916 // If X87Up is preceeded by X87, we don't need to do
917 // anything. However, in some cases with unions it may not be
918 // preceeded by X87. In such situations we follow gcc and pass the
919 // extra bits in an SSE reg.
920 if (Lo != X87)
Owen Andersonbdc004c2009-08-05 23:18:46 +0000921 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson3f5cc0a2009-08-13 21:57:51 +0000922 llvm::Type::getDoubleTy(VMContext), NULL);
Anton Korobeynikov73628192009-06-05 22:08:42 +0000923 break;
924 }
925
926 return getCoerceResult(RetTy, ResType, Context);
927}
928
929ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, ASTContext &Context,
Owen Anderson73e7f802009-07-14 23:10:40 +0000930 llvm::LLVMContext &VMContext,
Anton Korobeynikov73628192009-06-05 22:08:42 +0000931 unsigned &neededInt,
932 unsigned &neededSSE) const {
933 X86_64ABIInfo::Class Lo, Hi;
934 classify(Ty, Context, 0, Lo, Hi);
935
936 // Check some invariants.
937 // FIXME: Enforce these by construction.
938 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
939 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
940 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
941
942 neededInt = 0;
943 neededSSE = 0;
944 const llvm::Type *ResType = 0;
945 switch (Lo) {
946 case NoClass:
947 return ABIArgInfo::getIgnore();
948
949 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
950 // on the stack.
951 case Memory:
952
953 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
954 // COMPLEX_X87, it is passed in memory.
955 case X87:
956 case ComplexX87:
957 return getIndirectResult(Ty, Context);
958
959 case SSEUp:
960 case X87Up:
961 assert(0 && "Invalid classification for lo word.");
962
963 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
964 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
965 // and %r9 is used.
966 case Integer:
967 ++neededInt;
Owen Anderson3f5cc0a2009-08-13 21:57:51 +0000968 ResType = llvm::Type::getInt64Ty(VMContext);
Anton Korobeynikov73628192009-06-05 22:08:42 +0000969 break;
970
971 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
972 // available SSE register is used, the registers are taken in the
973 // order from %xmm0 to %xmm7.
974 case SSE:
975 ++neededSSE;
Owen Anderson3f5cc0a2009-08-13 21:57:51 +0000976 ResType = llvm::Type::getDoubleTy(VMContext);
Anton Korobeynikov73628192009-06-05 22:08:42 +0000977 break;
978 }
979
980 switch (Hi) {
981 // Memory was handled previously, ComplexX87 and X87 should
982 // never occur as hi classes, and X87Up must be preceed by X87,
983 // which is passed in memory.
984 case Memory:
985 case X87:
986 case ComplexX87:
987 assert(0 && "Invalid classification for hi word.");
988 break;
989
990 case NoClass: break;
991 case Integer:
Owen Andersonbdc004c2009-08-05 23:18:46 +0000992 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson3f5cc0a2009-08-13 21:57:51 +0000993 llvm::Type::getInt64Ty(VMContext), NULL);
Anton Korobeynikov73628192009-06-05 22:08:42 +0000994 ++neededInt;
995 break;
996
997 // X87Up generally doesn't occur here (long double is passed in
998 // memory), except in situations involving unions.
999 case X87Up:
1000 case SSE:
Owen Andersonbdc004c2009-08-05 23:18:46 +00001001 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson3f5cc0a2009-08-13 21:57:51 +00001002 llvm::Type::getDoubleTy(VMContext), NULL);
Anton Korobeynikov73628192009-06-05 22:08:42 +00001003 ++neededSSE;
1004 break;
1005
1006 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
1007 // eightbyte is passed in the upper half of the last used SSE
1008 // register.
1009 case SSEUp:
1010 assert(Lo == SSE && "Unexpected SSEUp classification.");
Owen Anderson3f5cc0a2009-08-13 21:57:51 +00001011 ResType = llvm::VectorType::get(llvm::Type::getDoubleTy(VMContext), 2);
Anton Korobeynikov73628192009-06-05 22:08:42 +00001012 break;
1013 }
1014
1015 return getCoerceResult(Ty, ResType, Context);
1016}
1017
Owen Anderson73e7f802009-07-14 23:10:40 +00001018void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1019 llvm::LLVMContext &VMContext) const {
1020 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(),
1021 Context, VMContext);
Anton Korobeynikov73628192009-06-05 22:08:42 +00001022
1023 // Keep track of the number of assigned registers.
1024 unsigned freeIntRegs = 6, freeSSERegs = 8;
1025
1026 // If the return value is indirect, then the hidden argument is consuming one
1027 // integer register.
1028 if (FI.getReturnInfo().isIndirect())
1029 --freeIntRegs;
1030
1031 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
1032 // get assigned (in left-to-right order) for passing as follows...
1033 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1034 it != ie; ++it) {
1035 unsigned neededInt, neededSSE;
Owen Anderson73e7f802009-07-14 23:10:40 +00001036 it->info = classifyArgumentType(it->type, Context, VMContext,
1037 neededInt, neededSSE);
Anton Korobeynikov73628192009-06-05 22:08:42 +00001038
1039 // AMD64-ABI 3.2.3p3: If there are no registers available for any
1040 // eightbyte of an argument, the whole argument is passed on the
1041 // stack. If registers have already been assigned for some
1042 // eightbytes of such an argument, the assignments get reverted.
1043 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
1044 freeIntRegs -= neededInt;
1045 freeSSERegs -= neededSSE;
1046 } else {
1047 it->info = getIndirectResult(it->type, Context);
1048 }
1049 }
1050}
1051
1052static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
1053 QualType Ty,
1054 CodeGenFunction &CGF) {
1055 llvm::Value *overflow_arg_area_p =
1056 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
1057 llvm::Value *overflow_arg_area =
1058 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
1059
1060 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
1061 // byte boundary if alignment needed by type exceeds 8 byte boundary.
1062 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
1063 if (Align > 8) {
1064 // Note that we follow the ABI & gcc here, even though the type
1065 // could in theory have an alignment greater than 16. This case
1066 // shouldn't ever matter in practice.
1067
1068 // overflow_arg_area = (overflow_arg_area + 15) & ~15;
Owen Anderson3f5cc0a2009-08-13 21:57:51 +00001069 llvm::Value *Offset =
1070 llvm::ConstantInt::get(llvm::Type::getInt32Ty(CGF.getLLVMContext()), 15);
Anton Korobeynikov73628192009-06-05 22:08:42 +00001071 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
1072 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
Owen Anderson3f5cc0a2009-08-13 21:57:51 +00001073 llvm::Type::getInt64Ty(CGF.getLLVMContext()));
1074 llvm::Value *Mask = llvm::ConstantInt::get(
1075 llvm::Type::getInt64Ty(CGF.getLLVMContext()), ~15LL);
Anton Korobeynikov73628192009-06-05 22:08:42 +00001076 overflow_arg_area =
1077 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
1078 overflow_arg_area->getType(),
1079 "overflow_arg_area.align");
1080 }
1081
1082 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
1083 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1084 llvm::Value *Res =
1085 CGF.Builder.CreateBitCast(overflow_arg_area,
Owen Anderson7ec2d8f2009-07-29 22:16:19 +00001086 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov73628192009-06-05 22:08:42 +00001087
1088 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
1089 // l->overflow_arg_area + sizeof(type).
1090 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
1091 // an 8 byte boundary.
1092
1093 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
Owen Anderson3f5cc0a2009-08-13 21:57:51 +00001094 llvm::Value *Offset =
1095 llvm::ConstantInt::get(llvm::Type::getInt32Ty(CGF.getLLVMContext()),
Anton Korobeynikov73628192009-06-05 22:08:42 +00001096 (SizeInBytes + 7) & ~7);
1097 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
1098 "overflow_arg_area.next");
1099 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
1100
1101 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
1102 return Res;
1103}
1104
1105llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1106 CodeGenFunction &CGF) const {
Owen Anderson73e7f802009-07-14 23:10:40 +00001107 llvm::LLVMContext &VMContext = CGF.getLLVMContext();
1108
Anton Korobeynikov73628192009-06-05 22:08:42 +00001109 // Assume that va_list type is correct; should be pointer to LLVM type:
1110 // struct {
1111 // i32 gp_offset;
1112 // i32 fp_offset;
1113 // i8* overflow_arg_area;
1114 // i8* reg_save_area;
1115 // };
1116 unsigned neededInt, neededSSE;
Owen Anderson73e7f802009-07-14 23:10:40 +00001117 ABIArgInfo AI = classifyArgumentType(Ty, CGF.getContext(), VMContext,
Anton Korobeynikov73628192009-06-05 22:08:42 +00001118 neededInt, neededSSE);
1119
1120 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
1121 // in the registers. If not go to step 7.
1122 if (!neededInt && !neededSSE)
1123 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1124
1125 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
1126 // general purpose registers needed to pass type and num_fp to hold
1127 // the number of floating point registers needed.
1128
1129 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
1130 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
1131 // l->fp_offset > 304 - num_fp * 16 go to step 7.
1132 //
1133 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
1134 // register save space).
1135
1136 llvm::Value *InRegs = 0;
1137 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
1138 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
1139 if (neededInt) {
1140 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
1141 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
1142 InRegs =
1143 CGF.Builder.CreateICmpULE(gp_offset,
Owen Anderson3f5cc0a2009-08-13 21:57:51 +00001144 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
Anton Korobeynikov73628192009-06-05 22:08:42 +00001145 48 - neededInt * 8),
1146 "fits_in_gp");
1147 }
1148
1149 if (neededSSE) {
1150 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
1151 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
1152 llvm::Value *FitsInFP =
1153 CGF.Builder.CreateICmpULE(fp_offset,
Owen Anderson3f5cc0a2009-08-13 21:57:51 +00001154 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
Anton Korobeynikov73628192009-06-05 22:08:42 +00001155 176 - neededSSE * 16),
1156 "fits_in_fp");
1157 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
1158 }
1159
1160 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
1161 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
1162 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
1163 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
1164
1165 // Emit code to load the value if it was passed in registers.
1166
1167 CGF.EmitBlock(InRegBlock);
1168
1169 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
1170 // an offset of l->gp_offset and/or l->fp_offset. This may require
1171 // copying to a temporary location in case the parameter is passed
1172 // in different register classes or requires an alignment greater
1173 // than 8 for general purpose registers and 16 for XMM registers.
1174 //
1175 // FIXME: This really results in shameful code when we end up needing to
1176 // collect arguments from different places; often what should result in a
1177 // simple assembling of a structure from scattered addresses has many more
1178 // loads than necessary. Can we clean this up?
1179 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1180 llvm::Value *RegAddr =
1181 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
1182 "reg_save_area");
1183 if (neededInt && neededSSE) {
1184 // FIXME: Cleanup.
1185 assert(AI.isCoerce() && "Unexpected ABI info for mixed regs");
1186 const llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
1187 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
1188 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
1189 const llvm::Type *TyLo = ST->getElementType(0);
1190 const llvm::Type *TyHi = ST->getElementType(1);
1191 assert((TyLo->isFloatingPoint() ^ TyHi->isFloatingPoint()) &&
1192 "Unexpected ABI info for mixed regs");
Owen Anderson7ec2d8f2009-07-29 22:16:19 +00001193 const llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
1194 const llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
Anton Korobeynikov73628192009-06-05 22:08:42 +00001195 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1196 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1197 llvm::Value *RegLoAddr = TyLo->isFloatingPoint() ? FPAddr : GPAddr;
1198 llvm::Value *RegHiAddr = TyLo->isFloatingPoint() ? GPAddr : FPAddr;
1199 llvm::Value *V =
1200 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
1201 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1202 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
1203 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1204
Owen Anderson73e7f802009-07-14 23:10:40 +00001205 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson7ec2d8f2009-07-29 22:16:19 +00001206 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov73628192009-06-05 22:08:42 +00001207 } else if (neededInt) {
1208 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1209 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson7ec2d8f2009-07-29 22:16:19 +00001210 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov73628192009-06-05 22:08:42 +00001211 } else {
1212 if (neededSSE == 1) {
1213 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1214 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson7ec2d8f2009-07-29 22:16:19 +00001215 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov73628192009-06-05 22:08:42 +00001216 } else {
1217 assert(neededSSE == 2 && "Invalid number of needed registers!");
1218 // SSE registers are spaced 16 bytes apart in the register save
1219 // area, we need to collect the two eightbytes together.
1220 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1221 llvm::Value *RegAddrHi =
1222 CGF.Builder.CreateGEP(RegAddrLo,
Owen Anderson3f5cc0a2009-08-13 21:57:51 +00001223 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 16));
Anton Korobeynikov73628192009-06-05 22:08:42 +00001224 const llvm::Type *DblPtrTy =
Owen Anderson3f5cc0a2009-08-13 21:57:51 +00001225 llvm::PointerType::getUnqual(llvm::Type::getDoubleTy(VMContext));
Owen Andersonbdc004c2009-08-05 23:18:46 +00001226 const llvm::StructType *ST = llvm::StructType::get(VMContext,
Owen Anderson3f5cc0a2009-08-13 21:57:51 +00001227 llvm::Type::getDoubleTy(VMContext),
1228 llvm::Type::getDoubleTy(VMContext),
Anton Korobeynikov73628192009-06-05 22:08:42 +00001229 NULL);
1230 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
1231 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
1232 DblPtrTy));
1233 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1234 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
1235 DblPtrTy));
1236 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1237 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson7ec2d8f2009-07-29 22:16:19 +00001238 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov73628192009-06-05 22:08:42 +00001239 }
1240 }
1241
1242 // AMD64-ABI 3.5.7p5: Step 5. Set:
1243 // l->gp_offset = l->gp_offset + num_gp * 8
1244 // l->fp_offset = l->fp_offset + num_fp * 16.
1245 if (neededInt) {
Owen Anderson3f5cc0a2009-08-13 21:57:51 +00001246 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
Anton Korobeynikov73628192009-06-05 22:08:42 +00001247 neededInt * 8);
1248 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
1249 gp_offset_p);
1250 }
1251 if (neededSSE) {
Owen Anderson3f5cc0a2009-08-13 21:57:51 +00001252 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
Anton Korobeynikov73628192009-06-05 22:08:42 +00001253 neededSSE * 16);
1254 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
1255 fp_offset_p);
1256 }
1257 CGF.EmitBranch(ContBlock);
1258
1259 // Emit code to load the value if it was passed in memory.
1260
1261 CGF.EmitBlock(InMemBlock);
1262 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1263
1264 // Return the appropriate result.
1265
1266 CGF.EmitBlock(ContBlock);
1267 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(),
1268 "vaarg.addr");
1269 ResAddr->reserveOperandSpace(2);
1270 ResAddr->addIncoming(RegAddr, InRegBlock);
1271 ResAddr->addIncoming(MemAddr, InMemBlock);
1272
1273 return ResAddr;
1274}
1275
1276// ABI Info for PIC16
1277class PIC16ABIInfo : public ABIInfo {
1278 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Anderson73e7f802009-07-14 23:10:40 +00001279 ASTContext &Context,
1280 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov73628192009-06-05 22:08:42 +00001281
1282 ABIArgInfo classifyArgumentType(QualType RetTy,
Owen Anderson73e7f802009-07-14 23:10:40 +00001283 ASTContext &Context,
1284 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov73628192009-06-05 22:08:42 +00001285
Owen Anderson73e7f802009-07-14 23:10:40 +00001286 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1287 llvm::LLVMContext &VMContext) const {
1288 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context,
1289 VMContext);
Anton Korobeynikov73628192009-06-05 22:08:42 +00001290 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1291 it != ie; ++it)
Owen Anderson73e7f802009-07-14 23:10:40 +00001292 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikov73628192009-06-05 22:08:42 +00001293 }
1294
1295 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1296 CodeGenFunction &CGF) const;
1297
1298};
1299
1300ABIArgInfo PIC16ABIInfo::classifyReturnType(QualType RetTy,
Owen Anderson73e7f802009-07-14 23:10:40 +00001301 ASTContext &Context,
1302 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov73628192009-06-05 22:08:42 +00001303 if (RetTy->isVoidType()) {
1304 return ABIArgInfo::getIgnore();
1305 } else {
1306 return ABIArgInfo::getDirect();
1307 }
1308}
1309
1310ABIArgInfo PIC16ABIInfo::classifyArgumentType(QualType Ty,
Owen Anderson73e7f802009-07-14 23:10:40 +00001311 ASTContext &Context,
1312 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov73628192009-06-05 22:08:42 +00001313 return ABIArgInfo::getDirect();
1314}
1315
1316llvm::Value *PIC16ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1317 CodeGenFunction &CGF) const {
1318 return 0;
1319}
1320
1321class ARMABIInfo : public ABIInfo {
1322 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Anderson73e7f802009-07-14 23:10:40 +00001323 ASTContext &Context,
1324 llvm::LLVMContext &VMCOntext) const;
Anton Korobeynikov73628192009-06-05 22:08:42 +00001325
1326 ABIArgInfo classifyArgumentType(QualType RetTy,
Owen Anderson73e7f802009-07-14 23:10:40 +00001327 ASTContext &Context,
1328 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov73628192009-06-05 22:08:42 +00001329
Owen Anderson73e7f802009-07-14 23:10:40 +00001330 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1331 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov73628192009-06-05 22:08:42 +00001332
1333 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1334 CodeGenFunction &CGF) const;
1335};
1336
Owen Anderson73e7f802009-07-14 23:10:40 +00001337void ARMABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1338 llvm::LLVMContext &VMContext) const {
1339 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context,
1340 VMContext);
Anton Korobeynikov73628192009-06-05 22:08:42 +00001341 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1342 it != ie; ++it) {
Owen Anderson73e7f802009-07-14 23:10:40 +00001343 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikov73628192009-06-05 22:08:42 +00001344 }
1345}
1346
1347ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty,
Owen Anderson73e7f802009-07-14 23:10:40 +00001348 ASTContext &Context,
1349 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov73628192009-06-05 22:08:42 +00001350 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
Anton Korobeynikov2ce305d2009-06-06 09:36:29 +00001351 return (Ty->isPromotableIntegerType() ?
1352 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov73628192009-06-05 22:08:42 +00001353 }
1354 // FIXME: This is kind of nasty... but there isn't much choice because the ARM
1355 // backend doesn't support byval.
1356 // FIXME: This doesn't handle alignment > 64 bits.
1357 const llvm::Type* ElemTy;
1358 unsigned SizeRegs;
1359 if (Context.getTypeAlign(Ty) > 32) {
Owen Anderson3f5cc0a2009-08-13 21:57:51 +00001360 ElemTy = llvm::Type::getInt64Ty(VMContext);
Anton Korobeynikov73628192009-06-05 22:08:42 +00001361 SizeRegs = (Context.getTypeSize(Ty) + 63) / 64;
1362 } else {
Owen Anderson3f5cc0a2009-08-13 21:57:51 +00001363 ElemTy = llvm::Type::getInt32Ty(VMContext);
Anton Korobeynikov73628192009-06-05 22:08:42 +00001364 SizeRegs = (Context.getTypeSize(Ty) + 31) / 32;
1365 }
1366 std::vector<const llvm::Type*> LLVMFields;
Owen Anderson7ec2d8f2009-07-29 22:16:19 +00001367 LLVMFields.push_back(llvm::ArrayType::get(ElemTy, SizeRegs));
Owen Andersonbdc004c2009-08-05 23:18:46 +00001368 const llvm::Type* STy = llvm::StructType::get(VMContext, LLVMFields, true);
Anton Korobeynikov73628192009-06-05 22:08:42 +00001369 return ABIArgInfo::getCoerce(STy);
1370}
1371
1372ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy,
Owen Anderson73e7f802009-07-14 23:10:40 +00001373 ASTContext &Context,
1374 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov73628192009-06-05 22:08:42 +00001375 if (RetTy->isVoidType()) {
1376 return ABIArgInfo::getIgnore();
1377 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1378 // Aggregates <= 4 bytes are returned in r0; other aggregates
1379 // are returned indirectly.
1380 uint64_t Size = Context.getTypeSize(RetTy);
1381 if (Size <= 32)
Owen Anderson3f5cc0a2009-08-13 21:57:51 +00001382 return ABIArgInfo::getCoerce(llvm::Type::getInt32Ty(VMContext));
Anton Korobeynikov73628192009-06-05 22:08:42 +00001383 return ABIArgInfo::getIndirect(0);
1384 } else {
Anton Korobeynikov2ce305d2009-06-06 09:36:29 +00001385 return (RetTy->isPromotableIntegerType() ?
1386 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov73628192009-06-05 22:08:42 +00001387 }
1388}
1389
1390llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1391 CodeGenFunction &CGF) const {
1392 // FIXME: Need to handle alignment
Owen Anderson3f5cc0a2009-08-13 21:57:51 +00001393 const llvm::Type *BP =
1394 llvm::PointerType::getUnqual(llvm::Type::getInt8Ty(CGF.getLLVMContext()));
Owen Anderson7ec2d8f2009-07-29 22:16:19 +00001395 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikov73628192009-06-05 22:08:42 +00001396
1397 CGBuilderTy &Builder = CGF.Builder;
1398 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
1399 "ap");
1400 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
1401 llvm::Type *PTy =
Owen Anderson7ec2d8f2009-07-29 22:16:19 +00001402 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikov73628192009-06-05 22:08:42 +00001403 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
1404
1405 uint64_t Offset =
1406 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
1407 llvm::Value *NextAddr =
Owen Anderson3f5cc0a2009-08-13 21:57:51 +00001408 Builder.CreateGEP(Addr, llvm::ConstantInt::get(
1409 llvm::Type::getInt32Ty(CGF.getLLVMContext()), Offset),
Anton Korobeynikov73628192009-06-05 22:08:42 +00001410 "ap.next");
1411 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
1412
1413 return AddrTyped;
1414}
1415
1416ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy,
Owen Anderson73e7f802009-07-14 23:10:40 +00001417 ASTContext &Context,
1418 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov73628192009-06-05 22:08:42 +00001419 if (RetTy->isVoidType()) {
1420 return ABIArgInfo::getIgnore();
1421 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1422 return ABIArgInfo::getIndirect(0);
1423 } else {
Anton Korobeynikov2ce305d2009-06-06 09:36:29 +00001424 return (RetTy->isPromotableIntegerType() ?
1425 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov73628192009-06-05 22:08:42 +00001426 }
1427}
1428
Anton Korobeynikov57f236f2009-07-16 20:09:57 +00001429namespace {
1430class SystemZABIInfo : public ABIInfo {
1431 bool isPromotableIntegerType(QualType Ty) const;
1432
1433 ABIArgInfo classifyReturnType(QualType RetTy, ASTContext &Context,
1434 llvm::LLVMContext &VMContext) const;
1435
1436 ABIArgInfo classifyArgumentType(QualType RetTy, ASTContext &Context,
1437 llvm::LLVMContext &VMContext) const;
1438
1439 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1440 llvm::LLVMContext &VMContext) const {
1441 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(),
1442 Context, VMContext);
1443 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1444 it != ie; ++it)
1445 it->info = classifyArgumentType(it->type, Context, VMContext);
1446 }
1447
1448 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1449 CodeGenFunction &CGF) const;
1450};
1451}
1452
1453bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const {
1454 // SystemZ ABI requires all 8, 16 and 32 bit quantities to be extended.
1455 if (const BuiltinType *BT = Ty->getAsBuiltinType())
1456 switch (BT->getKind()) {
1457 case BuiltinType::Bool:
1458 case BuiltinType::Char_S:
1459 case BuiltinType::Char_U:
1460 case BuiltinType::SChar:
1461 case BuiltinType::UChar:
1462 case BuiltinType::Short:
1463 case BuiltinType::UShort:
1464 case BuiltinType::Int:
1465 case BuiltinType::UInt:
1466 return true;
1467 default:
1468 return false;
1469 }
1470 return false;
1471}
1472
1473llvm::Value *SystemZABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1474 CodeGenFunction &CGF) const {
1475 // FIXME: Implement
1476 return 0;
1477}
1478
1479
1480ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy,
1481 ASTContext &Context,
1482 llvm::LLVMContext &VMContext) const {
1483 if (RetTy->isVoidType()) {
1484 return ABIArgInfo::getIgnore();
1485 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1486 return ABIArgInfo::getIndirect(0);
1487 } else {
1488 return (isPromotableIntegerType(RetTy) ?
1489 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1490 }
1491}
1492
1493ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty,
1494 ASTContext &Context,
1495 llvm::LLVMContext &VMContext) const {
1496 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
1497 return ABIArgInfo::getIndirect(0);
1498 } else {
1499 return (isPromotableIntegerType(Ty) ?
1500 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1501 }
1502}
1503
Anton Korobeynikov73628192009-06-05 22:08:42 +00001504ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty,
Owen Anderson73e7f802009-07-14 23:10:40 +00001505 ASTContext &Context,
1506 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov73628192009-06-05 22:08:42 +00001507 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
1508 return ABIArgInfo::getIndirect(0);
1509 } else {
Anton Korobeynikov2ce305d2009-06-06 09:36:29 +00001510 return (Ty->isPromotableIntegerType() ?
1511 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov73628192009-06-05 22:08:42 +00001512 }
1513}
1514
1515llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1516 CodeGenFunction &CGF) const {
1517 return 0;
1518}
1519
1520const ABIInfo &CodeGenTypes::getABIInfo() const {
1521 if (TheABIInfo)
1522 return *TheABIInfo;
1523
1524 // For now we just cache this in the CodeGenTypes and don't bother
1525 // to free it.
1526 const char *TargetPrefix = getContext().Target.getTargetPrefix();
1527 if (strcmp(TargetPrefix, "x86") == 0) {
1528 bool IsDarwin = strstr(getContext().Target.getTargetTriple(), "darwin");
David Chisnall09987c52009-08-17 23:08:21 +00001529 bool isPPCStructReturnABI = IsDarwin ||
1530 strstr(getContext().Target.getTargetTriple(), "cygwin") ||
1531 strstr(getContext().Target.getTargetTriple(), "mingw") ||
1532 strstr(getContext().Target.getTargetTriple(), "netware") ||
1533 strstr(getContext().Target.getTargetTriple(), "freebsd") ||
1534 strstr(getContext().Target.getTargetTriple(), "openbsd");
Anton Korobeynikov73628192009-06-05 22:08:42 +00001535 switch (getContext().Target.getPointerWidth(0)) {
1536 case 32:
David Chisnall09987c52009-08-17 23:08:21 +00001537 return *(TheABIInfo =
1538 new X86_32ABIInfo(Context, IsDarwin, isPPCStructReturnABI));
Anton Korobeynikov73628192009-06-05 22:08:42 +00001539 case 64:
1540 return *(TheABIInfo = new X86_64ABIInfo());
1541 }
1542 } else if (strcmp(TargetPrefix, "arm") == 0) {
1543 // FIXME: Support for OABI?
1544 return *(TheABIInfo = new ARMABIInfo());
1545 } else if (strcmp(TargetPrefix, "pic16") == 0) {
1546 return *(TheABIInfo = new PIC16ABIInfo());
Anton Korobeynikov57f236f2009-07-16 20:09:57 +00001547 } else if (strcmp(TargetPrefix, "s390x") == 0) {
1548 return *(TheABIInfo = new SystemZABIInfo());
Anton Korobeynikov73628192009-06-05 22:08:42 +00001549 }
1550
1551 return *(TheABIInfo = new DefaultABIInfo);
1552}