blob: a1c1092ffbff89b1607cebdc629d1693bdd84de4 [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;
209 bool IsDarwin;
210
211 static bool isRegisterSize(unsigned Size) {
212 return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
213 }
214
215 static bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context);
216
Eli Friedman621de772009-06-13 21:37:10 +0000217 static unsigned getIndirectArgumentAlignment(QualType Ty,
218 ASTContext &Context);
219
Anton Korobeynikov73628192009-06-05 22:08:42 +0000220public:
221 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Anderson73e7f802009-07-14 23:10:40 +0000222 ASTContext &Context,
223 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov73628192009-06-05 22:08:42 +0000224
225 ABIArgInfo classifyArgumentType(QualType RetTy,
Owen Anderson73e7f802009-07-14 23:10:40 +0000226 ASTContext &Context,
227 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov73628192009-06-05 22:08:42 +0000228
Owen Anderson73e7f802009-07-14 23:10:40 +0000229 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
230 llvm::LLVMContext &VMContext) const {
231 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context,
232 VMContext);
Anton Korobeynikov73628192009-06-05 22:08:42 +0000233 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
234 it != ie; ++it)
Owen Anderson73e7f802009-07-14 23:10:40 +0000235 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikov73628192009-06-05 22:08:42 +0000236 }
237
238 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
239 CodeGenFunction &CGF) const;
240
241 X86_32ABIInfo(ASTContext &Context, bool d)
242 : ABIInfo(), Context(Context), IsDarwin(d) {}
243};
244}
245
246
247/// shouldReturnTypeInRegister - Determine if the given type should be
248/// passed in a register (for the Darwin ABI).
249bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
250 ASTContext &Context) {
251 uint64_t Size = Context.getTypeSize(Ty);
252
253 // Type must be register sized.
254 if (!isRegisterSize(Size))
255 return false;
256
257 if (Ty->isVectorType()) {
258 // 64- and 128- bit vectors inside structures are not returned in
259 // registers.
260 if (Size == 64 || Size == 128)
261 return false;
262
263 return true;
264 }
265
266 // If this is a builtin, pointer, or complex type, it is ok.
267 if (Ty->getAsBuiltinType() || Ty->isPointerType() || Ty->isAnyComplexType())
268 return true;
269
270 // Arrays are treated like records.
271 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
272 return shouldReturnTypeInRegister(AT->getElementType(), Context);
273
274 // Otherwise, it must be a record type.
Ted Kremenekd00cd9e2009-07-29 21:53:49 +0000275 const RecordType *RT = Ty->getAs<RecordType>();
Anton Korobeynikov73628192009-06-05 22:08:42 +0000276 if (!RT) return false;
277
278 // Structure types are passed in register if all fields would be
279 // passed in a register.
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000280 for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(),
281 e = RT->getDecl()->field_end(); i != e; ++i) {
Anton Korobeynikov73628192009-06-05 22:08:42 +0000282 const FieldDecl *FD = *i;
283
284 // Empty fields are ignored.
285 if (isEmptyField(Context, FD))
286 continue;
287
288 // Check fields recursively.
289 if (!shouldReturnTypeInRegister(FD->getType(), Context))
290 return false;
291 }
292
293 return true;
294}
295
296ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
Owen Anderson73e7f802009-07-14 23:10:40 +0000297 ASTContext &Context,
298 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov73628192009-06-05 22:08:42 +0000299 if (RetTy->isVoidType()) {
300 return ABIArgInfo::getIgnore();
301 } else if (const VectorType *VT = RetTy->getAsVectorType()) {
302 // On Darwin, some vectors are returned in registers.
303 if (IsDarwin) {
304 uint64_t Size = Context.getTypeSize(RetTy);
305
306 // 128-bit vectors are a special case; they are returned in
307 // registers and we need to make sure to pick a type the LLVM
308 // backend will like.
309 if (Size == 128)
Owen Anderson3f5cc0a2009-08-13 21:57:51 +0000310 return ABIArgInfo::getCoerce(llvm::VectorType::get(
311 llvm::Type::getInt64Ty(VMContext), 2));
Anton Korobeynikov73628192009-06-05 22:08:42 +0000312
313 // Always return in register if it fits in a general purpose
314 // register, or if it is 64 bits and has a single element.
315 if ((Size == 8 || Size == 16 || Size == 32) ||
316 (Size == 64 && VT->getNumElements() == 1))
Owen Anderson3f5cc0a2009-08-13 21:57:51 +0000317 return ABIArgInfo::getCoerce(llvm::IntegerType::get(VMContext, Size));
Anton Korobeynikov73628192009-06-05 22:08:42 +0000318
319 return ABIArgInfo::getIndirect(0);
320 }
321
322 return ABIArgInfo::getDirect();
323 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
324 // Structures with flexible arrays are always indirect.
325 if (const RecordType *RT = RetTy->getAsStructureType())
326 if (RT->getDecl()->hasFlexibleArrayMember())
327 return ABIArgInfo::getIndirect(0);
328
329 // Outside of Darwin, structs and unions are always indirect.
330 if (!IsDarwin && !RetTy->isAnyComplexType())
331 return ABIArgInfo::getIndirect(0);
332
333 // Classify "single element" structs as their element type.
334 if (const Type *SeltTy = isSingleElementStruct(RetTy, Context)) {
335 if (const BuiltinType *BT = SeltTy->getAsBuiltinType()) {
336 if (BT->isIntegerType()) {
337 // We need to use the size of the structure, padding
338 // bit-fields can adjust that to be larger than the single
339 // element type.
340 uint64_t Size = Context.getTypeSize(RetTy);
Owen Anderson73e7f802009-07-14 23:10:40 +0000341 return ABIArgInfo::getCoerce(
Owen Anderson3f5cc0a2009-08-13 21:57:51 +0000342 llvm::IntegerType::get(VMContext, (unsigned) Size));
Anton Korobeynikov73628192009-06-05 22:08:42 +0000343 } else if (BT->getKind() == BuiltinType::Float) {
344 assert(Context.getTypeSize(RetTy) == Context.getTypeSize(SeltTy) &&
345 "Unexpect single element structure size!");
Owen Anderson3f5cc0a2009-08-13 21:57:51 +0000346 return ABIArgInfo::getCoerce(llvm::Type::getFloatTy(VMContext));
Anton Korobeynikov73628192009-06-05 22:08:42 +0000347 } else if (BT->getKind() == BuiltinType::Double) {
348 assert(Context.getTypeSize(RetTy) == Context.getTypeSize(SeltTy) &&
349 "Unexpect single element structure size!");
Owen Anderson3f5cc0a2009-08-13 21:57:51 +0000350 return ABIArgInfo::getCoerce(llvm::Type::getDoubleTy(VMContext));
Anton Korobeynikov73628192009-06-05 22:08:42 +0000351 }
352 } else if (SeltTy->isPointerType()) {
353 // FIXME: It would be really nice if this could come out as the proper
354 // pointer type.
355 llvm::Type *PtrTy =
Owen Anderson3f5cc0a2009-08-13 21:57:51 +0000356 llvm::PointerType::getUnqual(llvm::Type::getInt8Ty(VMContext));
Anton Korobeynikov73628192009-06-05 22:08:42 +0000357 return ABIArgInfo::getCoerce(PtrTy);
358 } else if (SeltTy->isVectorType()) {
359 // 64- and 128-bit vectors are never returned in a
360 // register when inside a structure.
361 uint64_t Size = Context.getTypeSize(RetTy);
362 if (Size == 64 || Size == 128)
363 return ABIArgInfo::getIndirect(0);
364
Owen Anderson73e7f802009-07-14 23:10:40 +0000365 return classifyReturnType(QualType(SeltTy, 0), Context, VMContext);
Anton Korobeynikov73628192009-06-05 22:08:42 +0000366 }
367 }
368
369 // Small structures which are register sized are generally returned
370 // in a register.
371 if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, Context)) {
372 uint64_t Size = Context.getTypeSize(RetTy);
Owen Anderson3f5cc0a2009-08-13 21:57:51 +0000373 return ABIArgInfo::getCoerce(llvm::IntegerType::get(VMContext, Size));
Anton Korobeynikov73628192009-06-05 22:08:42 +0000374 }
375
376 return ABIArgInfo::getIndirect(0);
377 } else {
Anton Korobeynikov2ce305d2009-06-06 09:36:29 +0000378 return (RetTy->isPromotableIntegerType() ?
379 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov73628192009-06-05 22:08:42 +0000380 }
381}
382
Eli Friedman621de772009-06-13 21:37:10 +0000383unsigned X86_32ABIInfo::getIndirectArgumentAlignment(QualType Ty,
384 ASTContext &Context) {
385 unsigned Align = Context.getTypeAlign(Ty);
386 if (Align < 128) return 0;
Ted Kremenekd00cd9e2009-07-29 21:53:49 +0000387 if (const RecordType* RT = Ty->getAs<RecordType>())
Eli Friedman621de772009-06-13 21:37:10 +0000388 if (typeContainsSSEVector(RT->getDecl(), Context))
389 return 16;
390 return 0;
391}
392
Anton Korobeynikov73628192009-06-05 22:08:42 +0000393ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
Owen Anderson73e7f802009-07-14 23:10:40 +0000394 ASTContext &Context,
395 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov73628192009-06-05 22:08:42 +0000396 // FIXME: Set alignment on indirect arguments.
397 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
398 // Structures with flexible arrays are always indirect.
399 if (const RecordType *RT = Ty->getAsStructureType())
400 if (RT->getDecl()->hasFlexibleArrayMember())
Eli Friedman621de772009-06-13 21:37:10 +0000401 return ABIArgInfo::getIndirect(getIndirectArgumentAlignment(Ty,
402 Context));
Anton Korobeynikov73628192009-06-05 22:08:42 +0000403
404 // Ignore empty structs.
Eli Friedman621de772009-06-13 21:37:10 +0000405 if (Ty->isStructureType() && Context.getTypeSize(Ty) == 0)
Anton Korobeynikov73628192009-06-05 22:08:42 +0000406 return ABIArgInfo::getIgnore();
407
408 // Expand structs with size <= 128-bits which consist only of
409 // basic types (int, long long, float, double, xxx*). This is
410 // non-recursive and does not ignore empty fields.
411 if (const RecordType *RT = Ty->getAsStructureType()) {
412 if (Context.getTypeSize(Ty) <= 4*32 &&
413 areAllFields32Or64BitBasicType(RT->getDecl(), Context))
414 return ABIArgInfo::getExpand();
415 }
416
Eli Friedman621de772009-06-13 21:37:10 +0000417 return ABIArgInfo::getIndirect(getIndirectArgumentAlignment(Ty, Context));
Anton Korobeynikov73628192009-06-05 22:08:42 +0000418 } else {
Anton Korobeynikov2ce305d2009-06-06 09:36:29 +0000419 return (Ty->isPromotableIntegerType() ?
420 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov73628192009-06-05 22:08:42 +0000421 }
422}
423
424llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
425 CodeGenFunction &CGF) const {
Owen Anderson3f5cc0a2009-08-13 21:57:51 +0000426 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::getInt8Ty(CGF.getLLVMContext()));
Owen Anderson7ec2d8f2009-07-29 22:16:19 +0000427 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikov73628192009-06-05 22:08:42 +0000428
429 CGBuilderTy &Builder = CGF.Builder;
430 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
431 "ap");
432 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
433 llvm::Type *PTy =
Owen Anderson7ec2d8f2009-07-29 22:16:19 +0000434 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikov73628192009-06-05 22:08:42 +0000435 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
436
437 uint64_t Offset =
438 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
439 llvm::Value *NextAddr =
Owen Anderson3f5cc0a2009-08-13 21:57:51 +0000440 Builder.CreateGEP(Addr, llvm::ConstantInt::get(
441 llvm::Type::getInt32Ty(CGF.getLLVMContext()), Offset),
Anton Korobeynikov73628192009-06-05 22:08:42 +0000442 "ap.next");
443 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
444
445 return AddrTyped;
446}
447
448namespace {
449/// X86_64ABIInfo - The X86_64 ABI information.
450class X86_64ABIInfo : public ABIInfo {
451 enum Class {
452 Integer = 0,
453 SSE,
454 SSEUp,
455 X87,
456 X87Up,
457 ComplexX87,
458 NoClass,
459 Memory
460 };
461
462 /// merge - Implement the X86_64 ABI merging algorithm.
463 ///
464 /// Merge an accumulating classification \arg Accum with a field
465 /// classification \arg Field.
466 ///
467 /// \param Accum - The accumulating classification. This should
468 /// always be either NoClass or the result of a previous merge
469 /// call. In addition, this should never be Memory (the caller
470 /// should just return Memory for the aggregate).
471 Class merge(Class Accum, Class Field) const;
472
473 /// classify - Determine the x86_64 register classes in which the
474 /// given type T should be passed.
475 ///
476 /// \param Lo - The classification for the parts of the type
477 /// residing in the low word of the containing object.
478 ///
479 /// \param Hi - The classification for the parts of the type
480 /// residing in the high word of the containing object.
481 ///
482 /// \param OffsetBase - The bit offset of this type in the
483 /// containing object. Some parameters are classified different
484 /// depending on whether they straddle an eightbyte boundary.
485 ///
486 /// If a word is unused its result will be NoClass; if a type should
487 /// be passed in Memory then at least the classification of \arg Lo
488 /// will be Memory.
489 ///
490 /// The \arg Lo class will be NoClass iff the argument is ignored.
491 ///
492 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
493 /// also be ComplexX87.
494 void classify(QualType T, ASTContext &Context, uint64_t OffsetBase,
495 Class &Lo, Class &Hi) const;
496
497 /// getCoerceResult - Given a source type \arg Ty and an LLVM type
498 /// to coerce to, chose the best way to pass Ty in the same place
499 /// that \arg CoerceTo would be passed, but while keeping the
500 /// emitted code as simple as possible.
501 ///
502 /// FIXME: Note, this should be cleaned up to just take an enumeration of all
503 /// the ways we might want to pass things, instead of constructing an LLVM
504 /// type. This makes this code more explicit, and it makes it clearer that we
505 /// are also doing this for correctness in the case of passing scalar types.
506 ABIArgInfo getCoerceResult(QualType Ty,
507 const llvm::Type *CoerceTo,
508 ASTContext &Context) const;
509
510 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
511 /// such that the argument will be passed in memory.
512 ABIArgInfo getIndirectResult(QualType Ty,
513 ASTContext &Context) const;
514
515 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Anderson73e7f802009-07-14 23:10:40 +0000516 ASTContext &Context,
517 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov73628192009-06-05 22:08:42 +0000518
519 ABIArgInfo classifyArgumentType(QualType Ty,
520 ASTContext &Context,
Owen Anderson73e7f802009-07-14 23:10:40 +0000521 llvm::LLVMContext &VMContext,
Anton Korobeynikov73628192009-06-05 22:08:42 +0000522 unsigned &neededInt,
523 unsigned &neededSSE) const;
524
525public:
Owen Anderson73e7f802009-07-14 23:10:40 +0000526 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
527 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov73628192009-06-05 22:08:42 +0000528
529 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
530 CodeGenFunction &CGF) const;
531};
532}
533
534X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum,
535 Class Field) const {
536 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
537 // classified recursively so that always two fields are
538 // considered. The resulting class is calculated according to
539 // the classes of the fields in the eightbyte:
540 //
541 // (a) If both classes are equal, this is the resulting class.
542 //
543 // (b) If one of the classes is NO_CLASS, the resulting class is
544 // the other class.
545 //
546 // (c) If one of the classes is MEMORY, the result is the MEMORY
547 // class.
548 //
549 // (d) If one of the classes is INTEGER, the result is the
550 // INTEGER.
551 //
552 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
553 // MEMORY is used as class.
554 //
555 // (f) Otherwise class SSE is used.
556
557 // Accum should never be memory (we should have returned) or
558 // ComplexX87 (because this cannot be passed in a structure).
559 assert((Accum != Memory && Accum != ComplexX87) &&
560 "Invalid accumulated classification during merge.");
561 if (Accum == Field || Field == NoClass)
562 return Accum;
563 else if (Field == Memory)
564 return Memory;
565 else if (Accum == NoClass)
566 return Field;
567 else if (Accum == Integer || Field == Integer)
568 return Integer;
569 else if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
570 Accum == X87 || Accum == X87Up)
571 return Memory;
572 else
573 return SSE;
574}
575
576void X86_64ABIInfo::classify(QualType Ty,
577 ASTContext &Context,
578 uint64_t OffsetBase,
579 Class &Lo, Class &Hi) const {
580 // FIXME: This code can be simplified by introducing a simple value class for
581 // Class pairs with appropriate constructor methods for the various
582 // situations.
583
584 // FIXME: Some of the split computations are wrong; unaligned vectors
585 // shouldn't be passed in registers for example, so there is no chance they
586 // can straddle an eightbyte. Verify & simplify.
587
588 Lo = Hi = NoClass;
589
590 Class &Current = OffsetBase < 64 ? Lo : Hi;
591 Current = Memory;
592
593 if (const BuiltinType *BT = Ty->getAsBuiltinType()) {
594 BuiltinType::Kind k = BT->getKind();
595
596 if (k == BuiltinType::Void) {
597 Current = NoClass;
598 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
599 Lo = Integer;
600 Hi = Integer;
601 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
602 Current = Integer;
603 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
604 Current = SSE;
605 } else if (k == BuiltinType::LongDouble) {
606 Lo = X87;
607 Hi = X87Up;
608 }
609 // FIXME: _Decimal32 and _Decimal64 are SSE.
610 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
611 } else if (const EnumType *ET = Ty->getAsEnumType()) {
612 // Classify the underlying integer type.
613 classify(ET->getDecl()->getIntegerType(), Context, OffsetBase, Lo, Hi);
614 } else if (Ty->hasPointerRepresentation()) {
615 Current = Integer;
616 } else if (const VectorType *VT = Ty->getAsVectorType()) {
617 uint64_t Size = Context.getTypeSize(VT);
618 if (Size == 32) {
619 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
620 // float> as integer.
621 Current = Integer;
622
623 // If this type crosses an eightbyte boundary, it should be
624 // split.
625 uint64_t EB_Real = (OffsetBase) / 64;
626 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
627 if (EB_Real != EB_Imag)
628 Hi = Lo;
629 } else if (Size == 64) {
630 // gcc passes <1 x double> in memory. :(
631 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
632 return;
633
634 // gcc passes <1 x long long> as INTEGER.
635 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong))
636 Current = Integer;
637 else
638 Current = SSE;
639
640 // If this type crosses an eightbyte boundary, it should be
641 // split.
642 if (OffsetBase && OffsetBase != 64)
643 Hi = Lo;
644 } else if (Size == 128) {
645 Lo = SSE;
646 Hi = SSEUp;
647 }
648 } else if (const ComplexType *CT = Ty->getAsComplexType()) {
649 QualType ET = Context.getCanonicalType(CT->getElementType());
650
651 uint64_t Size = Context.getTypeSize(Ty);
652 if (ET->isIntegralType()) {
653 if (Size <= 64)
654 Current = Integer;
655 else if (Size <= 128)
656 Lo = Hi = Integer;
657 } else if (ET == Context.FloatTy)
658 Current = SSE;
659 else if (ET == Context.DoubleTy)
660 Lo = Hi = SSE;
661 else if (ET == Context.LongDoubleTy)
662 Current = ComplexX87;
663
664 // If this complex type crosses an eightbyte boundary then it
665 // should be split.
666 uint64_t EB_Real = (OffsetBase) / 64;
667 uint64_t EB_Imag = (OffsetBase + Context.getTypeSize(ET)) / 64;
668 if (Hi == NoClass && EB_Real != EB_Imag)
669 Hi = Lo;
670 } else if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
671 // Arrays are treated like structures.
672
673 uint64_t Size = Context.getTypeSize(Ty);
674
675 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
676 // than two eightbytes, ..., it has class MEMORY.
677 if (Size > 128)
678 return;
679
680 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
681 // fields, it has class MEMORY.
682 //
683 // Only need to check alignment of array base.
684 if (OffsetBase % Context.getTypeAlign(AT->getElementType()))
685 return;
686
687 // Otherwise implement simplified merge. We could be smarter about
688 // this, but it isn't worth it and would be harder to verify.
689 Current = NoClass;
690 uint64_t EltSize = Context.getTypeSize(AT->getElementType());
691 uint64_t ArraySize = AT->getSize().getZExtValue();
692 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
693 Class FieldLo, FieldHi;
694 classify(AT->getElementType(), Context, Offset, FieldLo, FieldHi);
695 Lo = merge(Lo, FieldLo);
696 Hi = merge(Hi, FieldHi);
697 if (Lo == Memory || Hi == Memory)
698 break;
699 }
700
701 // Do post merger cleanup (see below). Only case we worry about is Memory.
702 if (Hi == Memory)
703 Lo = Memory;
704 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Ted Kremenekd00cd9e2009-07-29 21:53:49 +0000705 } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
Anton Korobeynikov73628192009-06-05 22:08:42 +0000706 uint64_t Size = Context.getTypeSize(Ty);
707
708 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
709 // than two eightbytes, ..., it has class MEMORY.
710 if (Size > 128)
711 return;
712
713 const RecordDecl *RD = RT->getDecl();
714
715 // Assume variable sized types are passed in memory.
716 if (RD->hasFlexibleArrayMember())
717 return;
718
719 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
720
721 // Reset Lo class, this will be recomputed.
722 Current = NoClass;
723 unsigned idx = 0;
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000724 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
725 i != e; ++i, ++idx) {
Anton Korobeynikov73628192009-06-05 22:08:42 +0000726 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
727 bool BitField = i->isBitField();
728
729 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
730 // fields, it has class MEMORY.
731 //
732 // Note, skip this test for bit-fields, see below.
733 if (!BitField && Offset % Context.getTypeAlign(i->getType())) {
734 Lo = Memory;
735 return;
736 }
737
738 // Classify this field.
739 //
740 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
741 // exceeds a single eightbyte, each is classified
742 // separately. Each eightbyte gets initialized to class
743 // NO_CLASS.
744 Class FieldLo, FieldHi;
745
746 // Bit-fields require special handling, they do not force the
747 // structure to be passed in memory even if unaligned, and
748 // therefore they can straddle an eightbyte.
749 if (BitField) {
750 // Ignore padding bit-fields.
751 if (i->isUnnamedBitfield())
752 continue;
753
754 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
755 uint64_t Size = i->getBitWidth()->EvaluateAsInt(Context).getZExtValue();
756
757 uint64_t EB_Lo = Offset / 64;
758 uint64_t EB_Hi = (Offset + Size - 1) / 64;
759 FieldLo = FieldHi = NoClass;
760 if (EB_Lo) {
761 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
762 FieldLo = NoClass;
763 FieldHi = Integer;
764 } else {
765 FieldLo = Integer;
766 FieldHi = EB_Hi ? Integer : NoClass;
767 }
768 } else
769 classify(i->getType(), Context, Offset, FieldLo, FieldHi);
770 Lo = merge(Lo, FieldLo);
771 Hi = merge(Hi, FieldHi);
772 if (Lo == Memory || Hi == Memory)
773 break;
774 }
775
776 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
777 //
778 // (a) If one of the classes is MEMORY, the whole argument is
779 // passed in memory.
780 //
781 // (b) If SSEUP is not preceeded by SSE, it is converted to SSE.
782
783 // The first of these conditions is guaranteed by how we implement
784 // the merge (just bail).
785 //
786 // The second condition occurs in the case of unions; for example
787 // union { _Complex double; unsigned; }.
788 if (Hi == Memory)
789 Lo = Memory;
790 if (Hi == SSEUp && Lo != SSE)
791 Hi = SSE;
792 }
793}
794
795ABIArgInfo X86_64ABIInfo::getCoerceResult(QualType Ty,
796 const llvm::Type *CoerceTo,
797 ASTContext &Context) const {
Owen Anderson3f5cc0a2009-08-13 21:57:51 +0000798 if (CoerceTo == llvm::Type::getInt64Ty(CoerceTo->getContext())) {
Anton Korobeynikov73628192009-06-05 22:08:42 +0000799 // Integer and pointer types will end up in a general purpose
800 // register.
801 if (Ty->isIntegralType() || Ty->isPointerType())
Anton Korobeynikov2ce305d2009-06-06 09:36:29 +0000802 return (Ty->isPromotableIntegerType() ?
803 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Owen Anderson3f5cc0a2009-08-13 21:57:51 +0000804 } else if (CoerceTo == llvm::Type::getDoubleTy(CoerceTo->getContext())) {
Anton Korobeynikov73628192009-06-05 22:08:42 +0000805 // FIXME: It would probably be better to make CGFunctionInfo only map using
806 // canonical types than to canonize here.
807 QualType CTy = Context.getCanonicalType(Ty);
808
809 // Float and double end up in a single SSE reg.
810 if (CTy == Context.FloatTy || CTy == Context.DoubleTy)
811 return ABIArgInfo::getDirect();
812
813 }
814
815 return ABIArgInfo::getCoerce(CoerceTo);
816}
817
818ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty,
819 ASTContext &Context) const {
820 // If this is a scalar LLVM value then assume LLVM will pass it in the right
821 // place naturally.
822 if (!CodeGenFunction::hasAggregateLLVMType(Ty))
Anton Korobeynikov2ce305d2009-06-06 09:36:29 +0000823 return (Ty->isPromotableIntegerType() ?
824 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov73628192009-06-05 22:08:42 +0000825
826 // FIXME: Set alignment correctly.
827 return ABIArgInfo::getIndirect(0);
828}
829
830ABIArgInfo X86_64ABIInfo::classifyReturnType(QualType RetTy,
Owen Anderson73e7f802009-07-14 23:10:40 +0000831 ASTContext &Context,
832 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov73628192009-06-05 22:08:42 +0000833 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
834 // classification algorithm.
835 X86_64ABIInfo::Class Lo, Hi;
836 classify(RetTy, Context, 0, Lo, Hi);
837
838 // Check some invariants.
839 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
840 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
841 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
842
843 const llvm::Type *ResType = 0;
844 switch (Lo) {
845 case NoClass:
846 return ABIArgInfo::getIgnore();
847
848 case SSEUp:
849 case X87Up:
850 assert(0 && "Invalid classification for lo word.");
851
852 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
853 // hidden argument.
854 case Memory:
855 return getIndirectResult(RetTy, Context);
856
857 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
858 // available register of the sequence %rax, %rdx is used.
859 case Integer:
Owen Anderson3f5cc0a2009-08-13 21:57:51 +0000860 ResType = llvm::Type::getInt64Ty(VMContext); break;
Anton Korobeynikov73628192009-06-05 22:08:42 +0000861
862 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
863 // available SSE register of the sequence %xmm0, %xmm1 is used.
864 case SSE:
Owen Anderson3f5cc0a2009-08-13 21:57:51 +0000865 ResType = llvm::Type::getDoubleTy(VMContext); break;
Anton Korobeynikov73628192009-06-05 22:08:42 +0000866
867 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
868 // returned on the X87 stack in %st0 as 80-bit x87 number.
869 case X87:
Owen Anderson3f5cc0a2009-08-13 21:57:51 +0000870 ResType = llvm::Type::getX86_FP80Ty(VMContext); break;
Anton Korobeynikov73628192009-06-05 22:08:42 +0000871
872 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
873 // part of the value is returned in %st0 and the imaginary part in
874 // %st1.
875 case ComplexX87:
876 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Owen Anderson3f5cc0a2009-08-13 21:57:51 +0000877 ResType = llvm::StructType::get(VMContext, llvm::Type::getX86_FP80Ty(VMContext),
878 llvm::Type::getX86_FP80Ty(VMContext),
Anton Korobeynikov73628192009-06-05 22:08:42 +0000879 NULL);
880 break;
881 }
882
883 switch (Hi) {
884 // Memory was handled previously and X87 should
885 // never occur as a hi class.
886 case Memory:
887 case X87:
888 assert(0 && "Invalid classification for hi word.");
889
890 case ComplexX87: // Previously handled.
891 case NoClass: break;
892
893 case Integer:
Owen Andersonbdc004c2009-08-05 23:18:46 +0000894 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson3f5cc0a2009-08-13 21:57:51 +0000895 llvm::Type::getInt64Ty(VMContext), NULL);
Anton Korobeynikov73628192009-06-05 22:08:42 +0000896 break;
897 case SSE:
Owen Andersonbdc004c2009-08-05 23:18:46 +0000898 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson3f5cc0a2009-08-13 21:57:51 +0000899 llvm::Type::getDoubleTy(VMContext), NULL);
Anton Korobeynikov73628192009-06-05 22:08:42 +0000900 break;
901
902 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
903 // is passed in the upper half of the last used SSE register.
904 //
905 // SSEUP should always be preceeded by SSE, just widen.
906 case SSEUp:
907 assert(Lo == SSE && "Unexpected SSEUp classification.");
Owen Anderson3f5cc0a2009-08-13 21:57:51 +0000908 ResType = llvm::VectorType::get(llvm::Type::getDoubleTy(VMContext), 2);
Anton Korobeynikov73628192009-06-05 22:08:42 +0000909 break;
910
911 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
912 // returned together with the previous X87 value in %st0.
913 case X87Up:
914 // If X87Up is preceeded by X87, we don't need to do
915 // anything. However, in some cases with unions it may not be
916 // preceeded by X87. In such situations we follow gcc and pass the
917 // extra bits in an SSE reg.
918 if (Lo != X87)
Owen Andersonbdc004c2009-08-05 23:18:46 +0000919 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson3f5cc0a2009-08-13 21:57:51 +0000920 llvm::Type::getDoubleTy(VMContext), NULL);
Anton Korobeynikov73628192009-06-05 22:08:42 +0000921 break;
922 }
923
924 return getCoerceResult(RetTy, ResType, Context);
925}
926
927ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, ASTContext &Context,
Owen Anderson73e7f802009-07-14 23:10:40 +0000928 llvm::LLVMContext &VMContext,
Anton Korobeynikov73628192009-06-05 22:08:42 +0000929 unsigned &neededInt,
930 unsigned &neededSSE) const {
931 X86_64ABIInfo::Class Lo, Hi;
932 classify(Ty, Context, 0, Lo, Hi);
933
934 // Check some invariants.
935 // FIXME: Enforce these by construction.
936 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
937 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
938 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
939
940 neededInt = 0;
941 neededSSE = 0;
942 const llvm::Type *ResType = 0;
943 switch (Lo) {
944 case NoClass:
945 return ABIArgInfo::getIgnore();
946
947 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
948 // on the stack.
949 case Memory:
950
951 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
952 // COMPLEX_X87, it is passed in memory.
953 case X87:
954 case ComplexX87:
955 return getIndirectResult(Ty, Context);
956
957 case SSEUp:
958 case X87Up:
959 assert(0 && "Invalid classification for lo word.");
960
961 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
962 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
963 // and %r9 is used.
964 case Integer:
965 ++neededInt;
Owen Anderson3f5cc0a2009-08-13 21:57:51 +0000966 ResType = llvm::Type::getInt64Ty(VMContext);
Anton Korobeynikov73628192009-06-05 22:08:42 +0000967 break;
968
969 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
970 // available SSE register is used, the registers are taken in the
971 // order from %xmm0 to %xmm7.
972 case SSE:
973 ++neededSSE;
Owen Anderson3f5cc0a2009-08-13 21:57:51 +0000974 ResType = llvm::Type::getDoubleTy(VMContext);
Anton Korobeynikov73628192009-06-05 22:08:42 +0000975 break;
976 }
977
978 switch (Hi) {
979 // Memory was handled previously, ComplexX87 and X87 should
980 // never occur as hi classes, and X87Up must be preceed by X87,
981 // which is passed in memory.
982 case Memory:
983 case X87:
984 case ComplexX87:
985 assert(0 && "Invalid classification for hi word.");
986 break;
987
988 case NoClass: break;
989 case Integer:
Owen Andersonbdc004c2009-08-05 23:18:46 +0000990 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson3f5cc0a2009-08-13 21:57:51 +0000991 llvm::Type::getInt64Ty(VMContext), NULL);
Anton Korobeynikov73628192009-06-05 22:08:42 +0000992 ++neededInt;
993 break;
994
995 // X87Up generally doesn't occur here (long double is passed in
996 // memory), except in situations involving unions.
997 case X87Up:
998 case SSE:
Owen Andersonbdc004c2009-08-05 23:18:46 +0000999 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson3f5cc0a2009-08-13 21:57:51 +00001000 llvm::Type::getDoubleTy(VMContext), NULL);
Anton Korobeynikov73628192009-06-05 22:08:42 +00001001 ++neededSSE;
1002 break;
1003
1004 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
1005 // eightbyte is passed in the upper half of the last used SSE
1006 // register.
1007 case SSEUp:
1008 assert(Lo == SSE && "Unexpected SSEUp classification.");
Owen Anderson3f5cc0a2009-08-13 21:57:51 +00001009 ResType = llvm::VectorType::get(llvm::Type::getDoubleTy(VMContext), 2);
Anton Korobeynikov73628192009-06-05 22:08:42 +00001010 break;
1011 }
1012
1013 return getCoerceResult(Ty, ResType, Context);
1014}
1015
Owen Anderson73e7f802009-07-14 23:10:40 +00001016void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1017 llvm::LLVMContext &VMContext) const {
1018 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(),
1019 Context, VMContext);
Anton Korobeynikov73628192009-06-05 22:08:42 +00001020
1021 // Keep track of the number of assigned registers.
1022 unsigned freeIntRegs = 6, freeSSERegs = 8;
1023
1024 // If the return value is indirect, then the hidden argument is consuming one
1025 // integer register.
1026 if (FI.getReturnInfo().isIndirect())
1027 --freeIntRegs;
1028
1029 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
1030 // get assigned (in left-to-right order) for passing as follows...
1031 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1032 it != ie; ++it) {
1033 unsigned neededInt, neededSSE;
Owen Anderson73e7f802009-07-14 23:10:40 +00001034 it->info = classifyArgumentType(it->type, Context, VMContext,
1035 neededInt, neededSSE);
Anton Korobeynikov73628192009-06-05 22:08:42 +00001036
1037 // AMD64-ABI 3.2.3p3: If there are no registers available for any
1038 // eightbyte of an argument, the whole argument is passed on the
1039 // stack. If registers have already been assigned for some
1040 // eightbytes of such an argument, the assignments get reverted.
1041 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
1042 freeIntRegs -= neededInt;
1043 freeSSERegs -= neededSSE;
1044 } else {
1045 it->info = getIndirectResult(it->type, Context);
1046 }
1047 }
1048}
1049
1050static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
1051 QualType Ty,
1052 CodeGenFunction &CGF) {
1053 llvm::Value *overflow_arg_area_p =
1054 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
1055 llvm::Value *overflow_arg_area =
1056 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
1057
1058 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
1059 // byte boundary if alignment needed by type exceeds 8 byte boundary.
1060 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
1061 if (Align > 8) {
1062 // Note that we follow the ABI & gcc here, even though the type
1063 // could in theory have an alignment greater than 16. This case
1064 // shouldn't ever matter in practice.
1065
1066 // overflow_arg_area = (overflow_arg_area + 15) & ~15;
Owen Anderson3f5cc0a2009-08-13 21:57:51 +00001067 llvm::Value *Offset =
1068 llvm::ConstantInt::get(llvm::Type::getInt32Ty(CGF.getLLVMContext()), 15);
Anton Korobeynikov73628192009-06-05 22:08:42 +00001069 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
1070 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
Owen Anderson3f5cc0a2009-08-13 21:57:51 +00001071 llvm::Type::getInt64Ty(CGF.getLLVMContext()));
1072 llvm::Value *Mask = llvm::ConstantInt::get(
1073 llvm::Type::getInt64Ty(CGF.getLLVMContext()), ~15LL);
Anton Korobeynikov73628192009-06-05 22:08:42 +00001074 overflow_arg_area =
1075 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
1076 overflow_arg_area->getType(),
1077 "overflow_arg_area.align");
1078 }
1079
1080 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
1081 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1082 llvm::Value *Res =
1083 CGF.Builder.CreateBitCast(overflow_arg_area,
Owen Anderson7ec2d8f2009-07-29 22:16:19 +00001084 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov73628192009-06-05 22:08:42 +00001085
1086 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
1087 // l->overflow_arg_area + sizeof(type).
1088 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
1089 // an 8 byte boundary.
1090
1091 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
Owen Anderson3f5cc0a2009-08-13 21:57:51 +00001092 llvm::Value *Offset =
1093 llvm::ConstantInt::get(llvm::Type::getInt32Ty(CGF.getLLVMContext()),
Anton Korobeynikov73628192009-06-05 22:08:42 +00001094 (SizeInBytes + 7) & ~7);
1095 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
1096 "overflow_arg_area.next");
1097 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
1098
1099 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
1100 return Res;
1101}
1102
1103llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1104 CodeGenFunction &CGF) const {
Owen Anderson73e7f802009-07-14 23:10:40 +00001105 llvm::LLVMContext &VMContext = CGF.getLLVMContext();
1106
Anton Korobeynikov73628192009-06-05 22:08:42 +00001107 // Assume that va_list type is correct; should be pointer to LLVM type:
1108 // struct {
1109 // i32 gp_offset;
1110 // i32 fp_offset;
1111 // i8* overflow_arg_area;
1112 // i8* reg_save_area;
1113 // };
1114 unsigned neededInt, neededSSE;
Owen Anderson73e7f802009-07-14 23:10:40 +00001115 ABIArgInfo AI = classifyArgumentType(Ty, CGF.getContext(), VMContext,
Anton Korobeynikov73628192009-06-05 22:08:42 +00001116 neededInt, neededSSE);
1117
1118 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
1119 // in the registers. If not go to step 7.
1120 if (!neededInt && !neededSSE)
1121 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1122
1123 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
1124 // general purpose registers needed to pass type and num_fp to hold
1125 // the number of floating point registers needed.
1126
1127 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
1128 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
1129 // l->fp_offset > 304 - num_fp * 16 go to step 7.
1130 //
1131 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
1132 // register save space).
1133
1134 llvm::Value *InRegs = 0;
1135 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
1136 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
1137 if (neededInt) {
1138 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
1139 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
1140 InRegs =
1141 CGF.Builder.CreateICmpULE(gp_offset,
Owen Anderson3f5cc0a2009-08-13 21:57:51 +00001142 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
Anton Korobeynikov73628192009-06-05 22:08:42 +00001143 48 - neededInt * 8),
1144 "fits_in_gp");
1145 }
1146
1147 if (neededSSE) {
1148 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
1149 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
1150 llvm::Value *FitsInFP =
1151 CGF.Builder.CreateICmpULE(fp_offset,
Owen Anderson3f5cc0a2009-08-13 21:57:51 +00001152 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
Anton Korobeynikov73628192009-06-05 22:08:42 +00001153 176 - neededSSE * 16),
1154 "fits_in_fp");
1155 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
1156 }
1157
1158 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
1159 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
1160 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
1161 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
1162
1163 // Emit code to load the value if it was passed in registers.
1164
1165 CGF.EmitBlock(InRegBlock);
1166
1167 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
1168 // an offset of l->gp_offset and/or l->fp_offset. This may require
1169 // copying to a temporary location in case the parameter is passed
1170 // in different register classes or requires an alignment greater
1171 // than 8 for general purpose registers and 16 for XMM registers.
1172 //
1173 // FIXME: This really results in shameful code when we end up needing to
1174 // collect arguments from different places; often what should result in a
1175 // simple assembling of a structure from scattered addresses has many more
1176 // loads than necessary. Can we clean this up?
1177 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1178 llvm::Value *RegAddr =
1179 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
1180 "reg_save_area");
1181 if (neededInt && neededSSE) {
1182 // FIXME: Cleanup.
1183 assert(AI.isCoerce() && "Unexpected ABI info for mixed regs");
1184 const llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
1185 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
1186 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
1187 const llvm::Type *TyLo = ST->getElementType(0);
1188 const llvm::Type *TyHi = ST->getElementType(1);
1189 assert((TyLo->isFloatingPoint() ^ TyHi->isFloatingPoint()) &&
1190 "Unexpected ABI info for mixed regs");
Owen Anderson7ec2d8f2009-07-29 22:16:19 +00001191 const llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
1192 const llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
Anton Korobeynikov73628192009-06-05 22:08:42 +00001193 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1194 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1195 llvm::Value *RegLoAddr = TyLo->isFloatingPoint() ? FPAddr : GPAddr;
1196 llvm::Value *RegHiAddr = TyLo->isFloatingPoint() ? GPAddr : FPAddr;
1197 llvm::Value *V =
1198 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
1199 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1200 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
1201 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1202
Owen Anderson73e7f802009-07-14 23:10:40 +00001203 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson7ec2d8f2009-07-29 22:16:19 +00001204 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov73628192009-06-05 22:08:42 +00001205 } else if (neededInt) {
1206 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1207 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson7ec2d8f2009-07-29 22:16:19 +00001208 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov73628192009-06-05 22:08:42 +00001209 } else {
1210 if (neededSSE == 1) {
1211 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1212 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson7ec2d8f2009-07-29 22:16:19 +00001213 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov73628192009-06-05 22:08:42 +00001214 } else {
1215 assert(neededSSE == 2 && "Invalid number of needed registers!");
1216 // SSE registers are spaced 16 bytes apart in the register save
1217 // area, we need to collect the two eightbytes together.
1218 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1219 llvm::Value *RegAddrHi =
1220 CGF.Builder.CreateGEP(RegAddrLo,
Owen Anderson3f5cc0a2009-08-13 21:57:51 +00001221 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 16));
Anton Korobeynikov73628192009-06-05 22:08:42 +00001222 const llvm::Type *DblPtrTy =
Owen Anderson3f5cc0a2009-08-13 21:57:51 +00001223 llvm::PointerType::getUnqual(llvm::Type::getDoubleTy(VMContext));
Owen Andersonbdc004c2009-08-05 23:18:46 +00001224 const llvm::StructType *ST = llvm::StructType::get(VMContext,
Owen Anderson3f5cc0a2009-08-13 21:57:51 +00001225 llvm::Type::getDoubleTy(VMContext),
1226 llvm::Type::getDoubleTy(VMContext),
Anton Korobeynikov73628192009-06-05 22:08:42 +00001227 NULL);
1228 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
1229 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
1230 DblPtrTy));
1231 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1232 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
1233 DblPtrTy));
1234 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1235 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson7ec2d8f2009-07-29 22:16:19 +00001236 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov73628192009-06-05 22:08:42 +00001237 }
1238 }
1239
1240 // AMD64-ABI 3.5.7p5: Step 5. Set:
1241 // l->gp_offset = l->gp_offset + num_gp * 8
1242 // l->fp_offset = l->fp_offset + num_fp * 16.
1243 if (neededInt) {
Owen Anderson3f5cc0a2009-08-13 21:57:51 +00001244 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
Anton Korobeynikov73628192009-06-05 22:08:42 +00001245 neededInt * 8);
1246 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
1247 gp_offset_p);
1248 }
1249 if (neededSSE) {
Owen Anderson3f5cc0a2009-08-13 21:57:51 +00001250 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
Anton Korobeynikov73628192009-06-05 22:08:42 +00001251 neededSSE * 16);
1252 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
1253 fp_offset_p);
1254 }
1255 CGF.EmitBranch(ContBlock);
1256
1257 // Emit code to load the value if it was passed in memory.
1258
1259 CGF.EmitBlock(InMemBlock);
1260 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1261
1262 // Return the appropriate result.
1263
1264 CGF.EmitBlock(ContBlock);
1265 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(),
1266 "vaarg.addr");
1267 ResAddr->reserveOperandSpace(2);
1268 ResAddr->addIncoming(RegAddr, InRegBlock);
1269 ResAddr->addIncoming(MemAddr, InMemBlock);
1270
1271 return ResAddr;
1272}
1273
1274// ABI Info for PIC16
1275class PIC16ABIInfo : public ABIInfo {
1276 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Anderson73e7f802009-07-14 23:10:40 +00001277 ASTContext &Context,
1278 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov73628192009-06-05 22:08:42 +00001279
1280 ABIArgInfo classifyArgumentType(QualType RetTy,
Owen Anderson73e7f802009-07-14 23:10:40 +00001281 ASTContext &Context,
1282 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov73628192009-06-05 22:08:42 +00001283
Owen Anderson73e7f802009-07-14 23:10:40 +00001284 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1285 llvm::LLVMContext &VMContext) const {
1286 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context,
1287 VMContext);
Anton Korobeynikov73628192009-06-05 22:08:42 +00001288 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1289 it != ie; ++it)
Owen Anderson73e7f802009-07-14 23:10:40 +00001290 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikov73628192009-06-05 22:08:42 +00001291 }
1292
1293 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1294 CodeGenFunction &CGF) const;
1295
1296};
1297
1298ABIArgInfo PIC16ABIInfo::classifyReturnType(QualType RetTy,
Owen Anderson73e7f802009-07-14 23:10:40 +00001299 ASTContext &Context,
1300 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov73628192009-06-05 22:08:42 +00001301 if (RetTy->isVoidType()) {
1302 return ABIArgInfo::getIgnore();
1303 } else {
1304 return ABIArgInfo::getDirect();
1305 }
1306}
1307
1308ABIArgInfo PIC16ABIInfo::classifyArgumentType(QualType Ty,
Owen Anderson73e7f802009-07-14 23:10:40 +00001309 ASTContext &Context,
1310 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov73628192009-06-05 22:08:42 +00001311 return ABIArgInfo::getDirect();
1312}
1313
1314llvm::Value *PIC16ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1315 CodeGenFunction &CGF) const {
1316 return 0;
1317}
1318
1319class ARMABIInfo : public ABIInfo {
1320 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Anderson73e7f802009-07-14 23:10:40 +00001321 ASTContext &Context,
1322 llvm::LLVMContext &VMCOntext) const;
Anton Korobeynikov73628192009-06-05 22:08:42 +00001323
1324 ABIArgInfo classifyArgumentType(QualType RetTy,
Owen Anderson73e7f802009-07-14 23:10:40 +00001325 ASTContext &Context,
1326 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov73628192009-06-05 22:08:42 +00001327
Owen Anderson73e7f802009-07-14 23:10:40 +00001328 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1329 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov73628192009-06-05 22:08:42 +00001330
1331 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1332 CodeGenFunction &CGF) const;
1333};
1334
Owen Anderson73e7f802009-07-14 23:10:40 +00001335void ARMABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1336 llvm::LLVMContext &VMContext) const {
1337 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context,
1338 VMContext);
Anton Korobeynikov73628192009-06-05 22:08:42 +00001339 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1340 it != ie; ++it) {
Owen Anderson73e7f802009-07-14 23:10:40 +00001341 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikov73628192009-06-05 22:08:42 +00001342 }
1343}
1344
1345ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty,
Owen Anderson73e7f802009-07-14 23:10:40 +00001346 ASTContext &Context,
1347 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov73628192009-06-05 22:08:42 +00001348 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
Anton Korobeynikov2ce305d2009-06-06 09:36:29 +00001349 return (Ty->isPromotableIntegerType() ?
1350 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov73628192009-06-05 22:08:42 +00001351 }
1352 // FIXME: This is kind of nasty... but there isn't much choice because the ARM
1353 // backend doesn't support byval.
1354 // FIXME: This doesn't handle alignment > 64 bits.
1355 const llvm::Type* ElemTy;
1356 unsigned SizeRegs;
1357 if (Context.getTypeAlign(Ty) > 32) {
Owen Anderson3f5cc0a2009-08-13 21:57:51 +00001358 ElemTy = llvm::Type::getInt64Ty(VMContext);
Anton Korobeynikov73628192009-06-05 22:08:42 +00001359 SizeRegs = (Context.getTypeSize(Ty) + 63) / 64;
1360 } else {
Owen Anderson3f5cc0a2009-08-13 21:57:51 +00001361 ElemTy = llvm::Type::getInt32Ty(VMContext);
Anton Korobeynikov73628192009-06-05 22:08:42 +00001362 SizeRegs = (Context.getTypeSize(Ty) + 31) / 32;
1363 }
1364 std::vector<const llvm::Type*> LLVMFields;
Owen Anderson7ec2d8f2009-07-29 22:16:19 +00001365 LLVMFields.push_back(llvm::ArrayType::get(ElemTy, SizeRegs));
Owen Andersonbdc004c2009-08-05 23:18:46 +00001366 const llvm::Type* STy = llvm::StructType::get(VMContext, LLVMFields, true);
Anton Korobeynikov73628192009-06-05 22:08:42 +00001367 return ABIArgInfo::getCoerce(STy);
1368}
1369
1370ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy,
Owen Anderson73e7f802009-07-14 23:10:40 +00001371 ASTContext &Context,
1372 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov73628192009-06-05 22:08:42 +00001373 if (RetTy->isVoidType()) {
1374 return ABIArgInfo::getIgnore();
1375 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1376 // Aggregates <= 4 bytes are returned in r0; other aggregates
1377 // are returned indirectly.
1378 uint64_t Size = Context.getTypeSize(RetTy);
1379 if (Size <= 32)
Owen Anderson3f5cc0a2009-08-13 21:57:51 +00001380 return ABIArgInfo::getCoerce(llvm::Type::getInt32Ty(VMContext));
Anton Korobeynikov73628192009-06-05 22:08:42 +00001381 return ABIArgInfo::getIndirect(0);
1382 } else {
Anton Korobeynikov2ce305d2009-06-06 09:36:29 +00001383 return (RetTy->isPromotableIntegerType() ?
1384 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov73628192009-06-05 22:08:42 +00001385 }
1386}
1387
1388llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1389 CodeGenFunction &CGF) const {
1390 // FIXME: Need to handle alignment
Owen Anderson3f5cc0a2009-08-13 21:57:51 +00001391 const llvm::Type *BP =
1392 llvm::PointerType::getUnqual(llvm::Type::getInt8Ty(CGF.getLLVMContext()));
Owen Anderson7ec2d8f2009-07-29 22:16:19 +00001393 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikov73628192009-06-05 22:08:42 +00001394
1395 CGBuilderTy &Builder = CGF.Builder;
1396 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
1397 "ap");
1398 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
1399 llvm::Type *PTy =
Owen Anderson7ec2d8f2009-07-29 22:16:19 +00001400 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikov73628192009-06-05 22:08:42 +00001401 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
1402
1403 uint64_t Offset =
1404 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
1405 llvm::Value *NextAddr =
Owen Anderson3f5cc0a2009-08-13 21:57:51 +00001406 Builder.CreateGEP(Addr, llvm::ConstantInt::get(
1407 llvm::Type::getInt32Ty(CGF.getLLVMContext()), Offset),
Anton Korobeynikov73628192009-06-05 22:08:42 +00001408 "ap.next");
1409 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
1410
1411 return AddrTyped;
1412}
1413
1414ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy,
Owen Anderson73e7f802009-07-14 23:10:40 +00001415 ASTContext &Context,
1416 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov73628192009-06-05 22:08:42 +00001417 if (RetTy->isVoidType()) {
1418 return ABIArgInfo::getIgnore();
1419 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1420 return ABIArgInfo::getIndirect(0);
1421 } else {
Anton Korobeynikov2ce305d2009-06-06 09:36:29 +00001422 return (RetTy->isPromotableIntegerType() ?
1423 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov73628192009-06-05 22:08:42 +00001424 }
1425}
1426
Anton Korobeynikov57f236f2009-07-16 20:09:57 +00001427namespace {
1428class SystemZABIInfo : public ABIInfo {
1429 bool isPromotableIntegerType(QualType Ty) const;
1430
1431 ABIArgInfo classifyReturnType(QualType RetTy, ASTContext &Context,
1432 llvm::LLVMContext &VMContext) const;
1433
1434 ABIArgInfo classifyArgumentType(QualType RetTy, ASTContext &Context,
1435 llvm::LLVMContext &VMContext) const;
1436
1437 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1438 llvm::LLVMContext &VMContext) const {
1439 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(),
1440 Context, VMContext);
1441 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1442 it != ie; ++it)
1443 it->info = classifyArgumentType(it->type, Context, VMContext);
1444 }
1445
1446 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1447 CodeGenFunction &CGF) const;
1448};
1449}
1450
1451bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const {
1452 // SystemZ ABI requires all 8, 16 and 32 bit quantities to be extended.
1453 if (const BuiltinType *BT = Ty->getAsBuiltinType())
1454 switch (BT->getKind()) {
1455 case BuiltinType::Bool:
1456 case BuiltinType::Char_S:
1457 case BuiltinType::Char_U:
1458 case BuiltinType::SChar:
1459 case BuiltinType::UChar:
1460 case BuiltinType::Short:
1461 case BuiltinType::UShort:
1462 case BuiltinType::Int:
1463 case BuiltinType::UInt:
1464 return true;
1465 default:
1466 return false;
1467 }
1468 return false;
1469}
1470
1471llvm::Value *SystemZABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1472 CodeGenFunction &CGF) const {
1473 // FIXME: Implement
1474 return 0;
1475}
1476
1477
1478ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy,
1479 ASTContext &Context,
1480 llvm::LLVMContext &VMContext) const {
1481 if (RetTy->isVoidType()) {
1482 return ABIArgInfo::getIgnore();
1483 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1484 return ABIArgInfo::getIndirect(0);
1485 } else {
1486 return (isPromotableIntegerType(RetTy) ?
1487 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1488 }
1489}
1490
1491ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty,
1492 ASTContext &Context,
1493 llvm::LLVMContext &VMContext) const {
1494 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
1495 return ABIArgInfo::getIndirect(0);
1496 } else {
1497 return (isPromotableIntegerType(Ty) ?
1498 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1499 }
1500}
1501
Anton Korobeynikov73628192009-06-05 22:08:42 +00001502ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty,
Owen Anderson73e7f802009-07-14 23:10:40 +00001503 ASTContext &Context,
1504 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov73628192009-06-05 22:08:42 +00001505 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
1506 return ABIArgInfo::getIndirect(0);
1507 } else {
Anton Korobeynikov2ce305d2009-06-06 09:36:29 +00001508 return (Ty->isPromotableIntegerType() ?
1509 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov73628192009-06-05 22:08:42 +00001510 }
1511}
1512
1513llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1514 CodeGenFunction &CGF) const {
1515 return 0;
1516}
1517
1518const ABIInfo &CodeGenTypes::getABIInfo() const {
1519 if (TheABIInfo)
1520 return *TheABIInfo;
1521
1522 // For now we just cache this in the CodeGenTypes and don't bother
1523 // to free it.
1524 const char *TargetPrefix = getContext().Target.getTargetPrefix();
1525 if (strcmp(TargetPrefix, "x86") == 0) {
1526 bool IsDarwin = strstr(getContext().Target.getTargetTriple(), "darwin");
1527 switch (getContext().Target.getPointerWidth(0)) {
1528 case 32:
1529 return *(TheABIInfo = new X86_32ABIInfo(Context, IsDarwin));
1530 case 64:
1531 return *(TheABIInfo = new X86_64ABIInfo());
1532 }
1533 } else if (strcmp(TargetPrefix, "arm") == 0) {
1534 // FIXME: Support for OABI?
1535 return *(TheABIInfo = new ARMABIInfo());
1536 } else if (strcmp(TargetPrefix, "pic16") == 0) {
1537 return *(TheABIInfo = new PIC16ABIInfo());
Anton Korobeynikov57f236f2009-07-16 20:09:57 +00001538 } else if (strcmp(TargetPrefix, "s390x") == 0) {
1539 return *(TheABIInfo = new SystemZABIInfo());
Anton Korobeynikov73628192009-06-05 22:08:42 +00001540 }
1541
1542 return *(TheABIInfo = new DefaultABIInfo);
1543}