blob: 76d75715bd0e7a5118aeeb08e109092494d32806 [file] [log] [blame]
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001//===---- TargetABIInfo.cpp - Encapsulate target ABI details ----*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// These classes wrap the information about a call or function
11// definition used to handle ABI compliancy.
12//
13//===----------------------------------------------------------------------===//
14
15#include "ABIInfo.h"
16#include "CodeGenFunction.h"
Anders Carlsson15b73de2009-07-18 19:43:29 +000017#include "clang/AST/RecordLayout.h"
Anton Korobeynikov244360d2009-06-05 22:08:42 +000018#include "llvm/Type.h"
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 Korobeynikov18adbf52009-06-06 09:36:29 +000031 case Extend:
32 fprintf(stderr, "Extend");
33 break;
Anton Korobeynikov244360d2009-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 Kremenek8a286fb2009-07-17 17:50:17 +000071 const RecordType *RT = T->getAsRecordType();
Anton Korobeynikov244360d2009-06-05 22:08:42 +000072 if (!RT)
73 return 0;
74 const RecordDecl *RD = RT->getDecl();
75 if (RD->hasFlexibleArrayMember())
76 return false;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +000077 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
78 i != e; ++i)
Anton Korobeynikov244360d2009-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;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000102 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
103 i != e; ++i) {
Anton Korobeynikov244360d2009-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) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000145 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
146 i != e; ++i) {
Anton Korobeynikov244360d2009-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 Friedman3192cc82009-06-13 21:37:10 +0000162static bool typeContainsSSEVector(const RecordDecl *RD, ASTContext &Context) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000163 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
164 i != e; ++i) {
Eli Friedman3192cc82009-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 Kremenek8a286fb2009-07-17 17:50:17 +0000171 if (const RecordType* RT = FD->getType()->getAsRecordType())
Eli Friedman3192cc82009-06-13 21:37:10 +0000172 if (typeContainsSSEVector(RT->getDecl(), Context))
173 return true;
174 }
175
176 return false;
177}
178
Anton Korobeynikov244360d2009-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 Anderson170229f2009-07-14 23:10:40 +0000186 ASTContext &Context,
187 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000188
189 ABIArgInfo classifyArgumentType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +0000190 ASTContext &Context,
191 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000192
Owen Anderson170229f2009-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 Korobeynikov244360d2009-06-05 22:08:42 +0000197 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
198 it != ie; ++it)
Owen Anderson170229f2009-07-14 23:10:40 +0000199 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikov244360d2009-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 Friedman3192cc82009-06-13 21:37:10 +0000217 static unsigned getIndirectArgumentAlignment(QualType Ty,
218 ASTContext &Context);
219
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000220public:
221 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +0000222 ASTContext &Context,
223 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000224
225 ABIArgInfo classifyArgumentType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +0000226 ASTContext &Context,
227 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000228
Owen Anderson170229f2009-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 Korobeynikov244360d2009-06-05 22:08:42 +0000233 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
234 it != ie; ++it)
Owen Anderson170229f2009-07-14 23:10:40 +0000235 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikov244360d2009-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 Kremenek8a286fb2009-07-17 17:50:17 +0000275 const RecordType *RT = Ty->getAsRecordType();
Anton Korobeynikov244360d2009-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.
Argyrios Kyrtzidiscfbfe782009-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 Korobeynikov244360d2009-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 Anderson170229f2009-07-14 23:10:40 +0000297 ASTContext &Context,
298 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov244360d2009-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 Anderson170229f2009-07-14 23:10:40 +0000310 return
311 ABIArgInfo::getCoerce(VMContext.getVectorType(llvm::Type::Int64Ty,
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000312 2));
313
314 // Always return in register if it fits in a general purpose
315 // register, or if it is 64 bits and has a single element.
316 if ((Size == 8 || Size == 16 || Size == 32) ||
317 (Size == 64 && VT->getNumElements() == 1))
Owen Anderson170229f2009-07-14 23:10:40 +0000318 return ABIArgInfo::getCoerce(VMContext.getIntegerType(Size));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000319
320 return ABIArgInfo::getIndirect(0);
321 }
322
323 return ABIArgInfo::getDirect();
324 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
325 // Structures with flexible arrays are always indirect.
326 if (const RecordType *RT = RetTy->getAsStructureType())
327 if (RT->getDecl()->hasFlexibleArrayMember())
328 return ABIArgInfo::getIndirect(0);
329
330 // Outside of Darwin, structs and unions are always indirect.
331 if (!IsDarwin && !RetTy->isAnyComplexType())
332 return ABIArgInfo::getIndirect(0);
333
334 // Classify "single element" structs as their element type.
335 if (const Type *SeltTy = isSingleElementStruct(RetTy, Context)) {
336 if (const BuiltinType *BT = SeltTy->getAsBuiltinType()) {
337 if (BT->isIntegerType()) {
338 // We need to use the size of the structure, padding
339 // bit-fields can adjust that to be larger than the single
340 // element type.
341 uint64_t Size = Context.getTypeSize(RetTy);
Owen Anderson170229f2009-07-14 23:10:40 +0000342 return ABIArgInfo::getCoerce(
343 VMContext.getIntegerType((unsigned) Size));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000344 } else if (BT->getKind() == BuiltinType::Float) {
345 assert(Context.getTypeSize(RetTy) == Context.getTypeSize(SeltTy) &&
346 "Unexpect single element structure size!");
347 return ABIArgInfo::getCoerce(llvm::Type::FloatTy);
348 } else if (BT->getKind() == BuiltinType::Double) {
349 assert(Context.getTypeSize(RetTy) == Context.getTypeSize(SeltTy) &&
350 "Unexpect single element structure size!");
351 return ABIArgInfo::getCoerce(llvm::Type::DoubleTy);
352 }
353 } else if (SeltTy->isPointerType()) {
354 // FIXME: It would be really nice if this could come out as the proper
355 // pointer type.
356 llvm::Type *PtrTy =
Owen Anderson170229f2009-07-14 23:10:40 +0000357 VMContext.getPointerTypeUnqual(llvm::Type::Int8Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000358 return ABIArgInfo::getCoerce(PtrTy);
359 } else if (SeltTy->isVectorType()) {
360 // 64- and 128-bit vectors are never returned in a
361 // register when inside a structure.
362 uint64_t Size = Context.getTypeSize(RetTy);
363 if (Size == 64 || Size == 128)
364 return ABIArgInfo::getIndirect(0);
365
Owen Anderson170229f2009-07-14 23:10:40 +0000366 return classifyReturnType(QualType(SeltTy, 0), Context, VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000367 }
368 }
369
370 // Small structures which are register sized are generally returned
371 // in a register.
372 if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, Context)) {
373 uint64_t Size = Context.getTypeSize(RetTy);
Owen Anderson170229f2009-07-14 23:10:40 +0000374 return ABIArgInfo::getCoerce(VMContext.getIntegerType(Size));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000375 }
376
377 return ABIArgInfo::getIndirect(0);
378 } else {
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000379 return (RetTy->isPromotableIntegerType() ?
380 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000381 }
382}
383
Eli Friedman3192cc82009-06-13 21:37:10 +0000384unsigned X86_32ABIInfo::getIndirectArgumentAlignment(QualType Ty,
385 ASTContext &Context) {
386 unsigned Align = Context.getTypeAlign(Ty);
387 if (Align < 128) return 0;
Ted Kremenek8a286fb2009-07-17 17:50:17 +0000388 if (const RecordType* RT = Ty->getAsRecordType())
Eli Friedman3192cc82009-06-13 21:37:10 +0000389 if (typeContainsSSEVector(RT->getDecl(), Context))
390 return 16;
391 return 0;
392}
393
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000394ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
Owen Anderson170229f2009-07-14 23:10:40 +0000395 ASTContext &Context,
396 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000397 // FIXME: Set alignment on indirect arguments.
398 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
399 // Structures with flexible arrays are always indirect.
400 if (const RecordType *RT = Ty->getAsStructureType())
401 if (RT->getDecl()->hasFlexibleArrayMember())
Eli Friedman3192cc82009-06-13 21:37:10 +0000402 return ABIArgInfo::getIndirect(getIndirectArgumentAlignment(Ty,
403 Context));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000404
405 // Ignore empty structs.
Eli Friedman3192cc82009-06-13 21:37:10 +0000406 if (Ty->isStructureType() && Context.getTypeSize(Ty) == 0)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000407 return ABIArgInfo::getIgnore();
408
409 // Expand structs with size <= 128-bits which consist only of
410 // basic types (int, long long, float, double, xxx*). This is
411 // non-recursive and does not ignore empty fields.
412 if (const RecordType *RT = Ty->getAsStructureType()) {
413 if (Context.getTypeSize(Ty) <= 4*32 &&
414 areAllFields32Or64BitBasicType(RT->getDecl(), Context))
415 return ABIArgInfo::getExpand();
416 }
417
Eli Friedman3192cc82009-06-13 21:37:10 +0000418 return ABIArgInfo::getIndirect(getIndirectArgumentAlignment(Ty, Context));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000419 } else {
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000420 return (Ty->isPromotableIntegerType() ?
421 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000422 }
423}
424
425llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
426 CodeGenFunction &CGF) const {
Owen Anderson170229f2009-07-14 23:10:40 +0000427 llvm::LLVMContext &VMContext = CGF.getLLVMContext();
428 const llvm::Type *BP = VMContext.getPointerTypeUnqual(llvm::Type::Int8Ty);
429 const llvm::Type *BPP = VMContext.getPointerTypeUnqual(BP);
Anton Korobeynikov244360d2009-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 Anderson170229f2009-07-14 23:10:40 +0000436 VMContext.getPointerTypeUnqual(CGF.ConvertType(Ty));
Anton Korobeynikov244360d2009-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 =
442 Builder.CreateGEP(Addr,
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000443 llvm::ConstantInt::get(llvm::Type::Int32Ty, Offset),
Anton Korobeynikov244360d2009-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 Anderson170229f2009-07-14 23:10:40 +0000518 ASTContext &Context,
519 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000520
521 ABIArgInfo classifyArgumentType(QualType Ty,
522 ASTContext &Context,
Owen Anderson170229f2009-07-14 23:10:40 +0000523 llvm::LLVMContext &VMContext,
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000524 unsigned &neededInt,
525 unsigned &neededSSE) const;
526
527public:
Owen Anderson170229f2009-07-14 23:10:40 +0000528 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
529 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-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 Kremenek8a286fb2009-07-17 17:50:17 +0000707 } else if (const RecordType *RT = Ty->getAsRecordType()) {
Anton Korobeynikov244360d2009-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;
Fariborz Jahanian7b2b1ec2009-07-27 20:57:45 +0000726 // FIXME. This will probably change when virtual bases are supported.
727 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
728 idx += CXXRD->getNumBases();
729
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000730 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
731 i != e; ++i, ++idx) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000732 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
733 bool BitField = i->isBitField();
734
735 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
736 // fields, it has class MEMORY.
737 //
738 // Note, skip this test for bit-fields, see below.
739 if (!BitField && Offset % Context.getTypeAlign(i->getType())) {
740 Lo = Memory;
741 return;
742 }
743
744 // Classify this field.
745 //
746 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
747 // exceeds a single eightbyte, each is classified
748 // separately. Each eightbyte gets initialized to class
749 // NO_CLASS.
750 Class FieldLo, FieldHi;
751
752 // Bit-fields require special handling, they do not force the
753 // structure to be passed in memory even if unaligned, and
754 // therefore they can straddle an eightbyte.
755 if (BitField) {
756 // Ignore padding bit-fields.
757 if (i->isUnnamedBitfield())
758 continue;
759
760 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
761 uint64_t Size = i->getBitWidth()->EvaluateAsInt(Context).getZExtValue();
762
763 uint64_t EB_Lo = Offset / 64;
764 uint64_t EB_Hi = (Offset + Size - 1) / 64;
765 FieldLo = FieldHi = NoClass;
766 if (EB_Lo) {
767 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
768 FieldLo = NoClass;
769 FieldHi = Integer;
770 } else {
771 FieldLo = Integer;
772 FieldHi = EB_Hi ? Integer : NoClass;
773 }
774 } else
775 classify(i->getType(), Context, Offset, FieldLo, FieldHi);
776 Lo = merge(Lo, FieldLo);
777 Hi = merge(Hi, FieldHi);
778 if (Lo == Memory || Hi == Memory)
779 break;
780 }
781
782 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
783 //
784 // (a) If one of the classes is MEMORY, the whole argument is
785 // passed in memory.
786 //
787 // (b) If SSEUP is not preceeded by SSE, it is converted to SSE.
788
789 // The first of these conditions is guaranteed by how we implement
790 // the merge (just bail).
791 //
792 // The second condition occurs in the case of unions; for example
793 // union { _Complex double; unsigned; }.
794 if (Hi == Memory)
795 Lo = Memory;
796 if (Hi == SSEUp && Lo != SSE)
797 Hi = SSE;
798 }
799}
800
801ABIArgInfo X86_64ABIInfo::getCoerceResult(QualType Ty,
802 const llvm::Type *CoerceTo,
803 ASTContext &Context) const {
804 if (CoerceTo == llvm::Type::Int64Ty) {
805 // Integer and pointer types will end up in a general purpose
806 // register.
807 if (Ty->isIntegralType() || Ty->isPointerType())
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000808 return (Ty->isPromotableIntegerType() ?
809 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000810 } else if (CoerceTo == llvm::Type::DoubleTy) {
811 // FIXME: It would probably be better to make CGFunctionInfo only map using
812 // canonical types than to canonize here.
813 QualType CTy = Context.getCanonicalType(Ty);
814
815 // Float and double end up in a single SSE reg.
816 if (CTy == Context.FloatTy || CTy == Context.DoubleTy)
817 return ABIArgInfo::getDirect();
818
819 }
820
821 return ABIArgInfo::getCoerce(CoerceTo);
822}
823
824ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty,
825 ASTContext &Context) const {
826 // If this is a scalar LLVM value then assume LLVM will pass it in the right
827 // place naturally.
828 if (!CodeGenFunction::hasAggregateLLVMType(Ty))
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000829 return (Ty->isPromotableIntegerType() ?
830 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000831
832 // FIXME: Set alignment correctly.
833 return ABIArgInfo::getIndirect(0);
834}
835
836ABIArgInfo X86_64ABIInfo::classifyReturnType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +0000837 ASTContext &Context,
838 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000839 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
840 // classification algorithm.
841 X86_64ABIInfo::Class Lo, Hi;
842 classify(RetTy, Context, 0, Lo, Hi);
843
844 // Check some invariants.
845 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
846 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
847 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
848
849 const llvm::Type *ResType = 0;
850 switch (Lo) {
851 case NoClass:
852 return ABIArgInfo::getIgnore();
853
854 case SSEUp:
855 case X87Up:
856 assert(0 && "Invalid classification for lo word.");
857
858 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
859 // hidden argument.
860 case Memory:
861 return getIndirectResult(RetTy, Context);
862
863 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
864 // available register of the sequence %rax, %rdx is used.
865 case Integer:
866 ResType = llvm::Type::Int64Ty; break;
867
868 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
869 // available SSE register of the sequence %xmm0, %xmm1 is used.
870 case SSE:
871 ResType = llvm::Type::DoubleTy; break;
872
873 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
874 // returned on the X87 stack in %st0 as 80-bit x87 number.
875 case X87:
876 ResType = llvm::Type::X86_FP80Ty; break;
877
878 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
879 // part of the value is returned in %st0 and the imaginary part in
880 // %st1.
881 case ComplexX87:
882 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Owen Anderson170229f2009-07-14 23:10:40 +0000883 ResType = VMContext.getStructType(llvm::Type::X86_FP80Ty,
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000884 llvm::Type::X86_FP80Ty,
885 NULL);
886 break;
887 }
888
889 switch (Hi) {
890 // Memory was handled previously and X87 should
891 // never occur as a hi class.
892 case Memory:
893 case X87:
894 assert(0 && "Invalid classification for hi word.");
895
896 case ComplexX87: // Previously handled.
897 case NoClass: break;
898
899 case Integer:
Owen Anderson170229f2009-07-14 23:10:40 +0000900 ResType = VMContext.getStructType(ResType, llvm::Type::Int64Ty, NULL);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000901 break;
902 case SSE:
Owen Anderson170229f2009-07-14 23:10:40 +0000903 ResType = VMContext.getStructType(ResType, llvm::Type::DoubleTy, NULL);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000904 break;
905
906 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
907 // is passed in the upper half of the last used SSE register.
908 //
909 // SSEUP should always be preceeded by SSE, just widen.
910 case SSEUp:
911 assert(Lo == SSE && "Unexpected SSEUp classification.");
Owen Anderson170229f2009-07-14 23:10:40 +0000912 ResType = VMContext.getVectorType(llvm::Type::DoubleTy, 2);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000913 break;
914
915 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
916 // returned together with the previous X87 value in %st0.
917 case X87Up:
918 // If X87Up is preceeded by X87, we don't need to do
919 // anything. However, in some cases with unions it may not be
920 // preceeded by X87. In such situations we follow gcc and pass the
921 // extra bits in an SSE reg.
922 if (Lo != X87)
Owen Anderson170229f2009-07-14 23:10:40 +0000923 ResType = VMContext.getStructType(ResType, llvm::Type::DoubleTy, NULL);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000924 break;
925 }
926
927 return getCoerceResult(RetTy, ResType, Context);
928}
929
930ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, ASTContext &Context,
Owen Anderson170229f2009-07-14 23:10:40 +0000931 llvm::LLVMContext &VMContext,
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000932 unsigned &neededInt,
933 unsigned &neededSSE) const {
934 X86_64ABIInfo::Class Lo, Hi;
935 classify(Ty, Context, 0, Lo, Hi);
936
937 // Check some invariants.
938 // FIXME: Enforce these by construction.
939 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
940 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
941 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
942
943 neededInt = 0;
944 neededSSE = 0;
945 const llvm::Type *ResType = 0;
946 switch (Lo) {
947 case NoClass:
948 return ABIArgInfo::getIgnore();
949
950 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
951 // on the stack.
952 case Memory:
953
954 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
955 // COMPLEX_X87, it is passed in memory.
956 case X87:
957 case ComplexX87:
958 return getIndirectResult(Ty, Context);
959
960 case SSEUp:
961 case X87Up:
962 assert(0 && "Invalid classification for lo word.");
963
964 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
965 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
966 // and %r9 is used.
967 case Integer:
968 ++neededInt;
969 ResType = llvm::Type::Int64Ty;
970 break;
971
972 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
973 // available SSE register is used, the registers are taken in the
974 // order from %xmm0 to %xmm7.
975 case SSE:
976 ++neededSSE;
977 ResType = llvm::Type::DoubleTy;
978 break;
979 }
980
981 switch (Hi) {
982 // Memory was handled previously, ComplexX87 and X87 should
983 // never occur as hi classes, and X87Up must be preceed by X87,
984 // which is passed in memory.
985 case Memory:
986 case X87:
987 case ComplexX87:
988 assert(0 && "Invalid classification for hi word.");
989 break;
990
991 case NoClass: break;
992 case Integer:
Owen Anderson170229f2009-07-14 23:10:40 +0000993 ResType = VMContext.getStructType(ResType, llvm::Type::Int64Ty, NULL);
Anton Korobeynikov244360d2009-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 Anderson170229f2009-07-14 23:10:40 +00001001 ResType = VMContext.getStructType(ResType, llvm::Type::DoubleTy, NULL);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001002 ++neededSSE;
1003 break;
1004
1005 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
1006 // eightbyte is passed in the upper half of the last used SSE
1007 // register.
1008 case SSEUp:
1009 assert(Lo == SSE && "Unexpected SSEUp classification.");
Owen Anderson170229f2009-07-14 23:10:40 +00001010 ResType = VMContext.getVectorType(llvm::Type::DoubleTy, 2);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001011 break;
1012 }
1013
1014 return getCoerceResult(Ty, ResType, Context);
1015}
1016
Owen Anderson170229f2009-07-14 23:10:40 +00001017void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1018 llvm::LLVMContext &VMContext) const {
1019 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(),
1020 Context, VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001021
1022 // Keep track of the number of assigned registers.
1023 unsigned freeIntRegs = 6, freeSSERegs = 8;
1024
1025 // If the return value is indirect, then the hidden argument is consuming one
1026 // integer register.
1027 if (FI.getReturnInfo().isIndirect())
1028 --freeIntRegs;
1029
1030 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
1031 // get assigned (in left-to-right order) for passing as follows...
1032 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1033 it != ie; ++it) {
1034 unsigned neededInt, neededSSE;
Owen Anderson170229f2009-07-14 23:10:40 +00001035 it->info = classifyArgumentType(it->type, Context, VMContext,
1036 neededInt, neededSSE);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001037
1038 // AMD64-ABI 3.2.3p3: If there are no registers available for any
1039 // eightbyte of an argument, the whole argument is passed on the
1040 // stack. If registers have already been assigned for some
1041 // eightbytes of such an argument, the assignments get reverted.
1042 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
1043 freeIntRegs -= neededInt;
1044 freeSSERegs -= neededSSE;
1045 } else {
1046 it->info = getIndirectResult(it->type, Context);
1047 }
1048 }
1049}
1050
1051static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
1052 QualType Ty,
1053 CodeGenFunction &CGF) {
Owen Anderson170229f2009-07-14 23:10:40 +00001054 llvm::LLVMContext &VMContext = CGF.getLLVMContext();
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001055 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 Andersonb7a2fe62009-07-24 23:12:58 +00001069 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty, 15);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001070 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
1071 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
1072 llvm::Type::Int64Ty);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00001073 llvm::Value *Mask = llvm::ConstantInt::get(llvm::Type::Int64Ty, ~15LL);
Anton Korobeynikov244360d2009-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 Anderson170229f2009-07-14 23:10:40 +00001084 VMContext.getPointerTypeUnqual(LTy));
Anton Korobeynikov244360d2009-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 Andersonb7a2fe62009-07-24 23:12:58 +00001092 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty,
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001093 (SizeInBytes + 7) & ~7);
1094 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
1095 "overflow_arg_area.next");
1096 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
1097
1098 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
1099 return Res;
1100}
1101
1102llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1103 CodeGenFunction &CGF) const {
Owen Anderson170229f2009-07-14 23:10:40 +00001104 llvm::LLVMContext &VMContext = CGF.getLLVMContext();
1105
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001106 // Assume that va_list type is correct; should be pointer to LLVM type:
1107 // struct {
1108 // i32 gp_offset;
1109 // i32 fp_offset;
1110 // i8* overflow_arg_area;
1111 // i8* reg_save_area;
1112 // };
1113 unsigned neededInt, neededSSE;
Owen Anderson170229f2009-07-14 23:10:40 +00001114 ABIArgInfo AI = classifyArgumentType(Ty, CGF.getContext(), VMContext,
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001115 neededInt, neededSSE);
1116
1117 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
1118 // in the registers. If not go to step 7.
1119 if (!neededInt && !neededSSE)
1120 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1121
1122 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
1123 // general purpose registers needed to pass type and num_fp to hold
1124 // the number of floating point registers needed.
1125
1126 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
1127 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
1128 // l->fp_offset > 304 - num_fp * 16 go to step 7.
1129 //
1130 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
1131 // register save space).
1132
1133 llvm::Value *InRegs = 0;
1134 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
1135 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
1136 if (neededInt) {
1137 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
1138 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
1139 InRegs =
1140 CGF.Builder.CreateICmpULE(gp_offset,
Owen Andersonb7a2fe62009-07-24 23:12:58 +00001141 llvm::ConstantInt::get(llvm::Type::Int32Ty,
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001142 48 - neededInt * 8),
1143 "fits_in_gp");
1144 }
1145
1146 if (neededSSE) {
1147 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
1148 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
1149 llvm::Value *FitsInFP =
1150 CGF.Builder.CreateICmpULE(fp_offset,
Owen Andersonb7a2fe62009-07-24 23:12:58 +00001151 llvm::ConstantInt::get(llvm::Type::Int32Ty,
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001152 176 - neededSSE * 16),
1153 "fits_in_fp");
1154 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
1155 }
1156
1157 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
1158 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
1159 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
1160 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
1161
1162 // Emit code to load the value if it was passed in registers.
1163
1164 CGF.EmitBlock(InRegBlock);
1165
1166 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
1167 // an offset of l->gp_offset and/or l->fp_offset. This may require
1168 // copying to a temporary location in case the parameter is passed
1169 // in different register classes or requires an alignment greater
1170 // than 8 for general purpose registers and 16 for XMM registers.
1171 //
1172 // FIXME: This really results in shameful code when we end up needing to
1173 // collect arguments from different places; often what should result in a
1174 // simple assembling of a structure from scattered addresses has many more
1175 // loads than necessary. Can we clean this up?
1176 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1177 llvm::Value *RegAddr =
1178 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
1179 "reg_save_area");
1180 if (neededInt && neededSSE) {
1181 // FIXME: Cleanup.
1182 assert(AI.isCoerce() && "Unexpected ABI info for mixed regs");
1183 const llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
1184 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
1185 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
1186 const llvm::Type *TyLo = ST->getElementType(0);
1187 const llvm::Type *TyHi = ST->getElementType(1);
1188 assert((TyLo->isFloatingPoint() ^ TyHi->isFloatingPoint()) &&
1189 "Unexpected ABI info for mixed regs");
Owen Anderson170229f2009-07-14 23:10:40 +00001190 const llvm::Type *PTyLo = VMContext.getPointerTypeUnqual(TyLo);
1191 const llvm::Type *PTyHi = VMContext.getPointerTypeUnqual(TyHi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001192 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1193 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1194 llvm::Value *RegLoAddr = TyLo->isFloatingPoint() ? FPAddr : GPAddr;
1195 llvm::Value *RegHiAddr = TyLo->isFloatingPoint() ? GPAddr : FPAddr;
1196 llvm::Value *V =
1197 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
1198 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1199 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
1200 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1201
Owen Anderson170229f2009-07-14 23:10:40 +00001202 RegAddr = CGF.Builder.CreateBitCast(Tmp,
1203 VMContext.getPointerTypeUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001204 } else if (neededInt) {
1205 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1206 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson170229f2009-07-14 23:10:40 +00001207 VMContext.getPointerTypeUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001208 } else {
1209 if (neededSSE == 1) {
1210 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1211 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson170229f2009-07-14 23:10:40 +00001212 VMContext.getPointerTypeUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001213 } else {
1214 assert(neededSSE == 2 && "Invalid number of needed registers!");
1215 // SSE registers are spaced 16 bytes apart in the register save
1216 // area, we need to collect the two eightbytes together.
1217 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1218 llvm::Value *RegAddrHi =
1219 CGF.Builder.CreateGEP(RegAddrLo,
Owen Andersonb7a2fe62009-07-24 23:12:58 +00001220 llvm::ConstantInt::get(llvm::Type::Int32Ty, 16));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001221 const llvm::Type *DblPtrTy =
Owen Anderson170229f2009-07-14 23:10:40 +00001222 VMContext.getPointerTypeUnqual(llvm::Type::DoubleTy);
1223 const llvm::StructType *ST = VMContext.getStructType(llvm::Type::DoubleTy,
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001224 llvm::Type::DoubleTy,
1225 NULL);
1226 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
1227 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
1228 DblPtrTy));
1229 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1230 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
1231 DblPtrTy));
1232 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1233 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson170229f2009-07-14 23:10:40 +00001234 VMContext.getPointerTypeUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001235 }
1236 }
1237
1238 // AMD64-ABI 3.5.7p5: Step 5. Set:
1239 // l->gp_offset = l->gp_offset + num_gp * 8
1240 // l->fp_offset = l->fp_offset + num_fp * 16.
1241 if (neededInt) {
Owen Andersonb7a2fe62009-07-24 23:12:58 +00001242 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty,
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001243 neededInt * 8);
1244 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
1245 gp_offset_p);
1246 }
1247 if (neededSSE) {
Owen Andersonb7a2fe62009-07-24 23:12:58 +00001248 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty,
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001249 neededSSE * 16);
1250 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
1251 fp_offset_p);
1252 }
1253 CGF.EmitBranch(ContBlock);
1254
1255 // Emit code to load the value if it was passed in memory.
1256
1257 CGF.EmitBlock(InMemBlock);
1258 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1259
1260 // Return the appropriate result.
1261
1262 CGF.EmitBlock(ContBlock);
1263 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(),
1264 "vaarg.addr");
1265 ResAddr->reserveOperandSpace(2);
1266 ResAddr->addIncoming(RegAddr, InRegBlock);
1267 ResAddr->addIncoming(MemAddr, InMemBlock);
1268
1269 return ResAddr;
1270}
1271
1272// ABI Info for PIC16
1273class PIC16ABIInfo : public ABIInfo {
1274 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +00001275 ASTContext &Context,
1276 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001277
1278 ABIArgInfo classifyArgumentType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +00001279 ASTContext &Context,
1280 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001281
Owen Anderson170229f2009-07-14 23:10:40 +00001282 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1283 llvm::LLVMContext &VMContext) const {
1284 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context,
1285 VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001286 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1287 it != ie; ++it)
Owen Anderson170229f2009-07-14 23:10:40 +00001288 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001289 }
1290
1291 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1292 CodeGenFunction &CGF) const;
1293
1294};
1295
1296ABIArgInfo PIC16ABIInfo::classifyReturnType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +00001297 ASTContext &Context,
1298 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001299 if (RetTy->isVoidType()) {
1300 return ABIArgInfo::getIgnore();
1301 } else {
1302 return ABIArgInfo::getDirect();
1303 }
1304}
1305
1306ABIArgInfo PIC16ABIInfo::classifyArgumentType(QualType Ty,
Owen Anderson170229f2009-07-14 23:10:40 +00001307 ASTContext &Context,
1308 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001309 return ABIArgInfo::getDirect();
1310}
1311
1312llvm::Value *PIC16ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1313 CodeGenFunction &CGF) const {
1314 return 0;
1315}
1316
1317class ARMABIInfo : public ABIInfo {
1318 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +00001319 ASTContext &Context,
1320 llvm::LLVMContext &VMCOntext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001321
1322 ABIArgInfo classifyArgumentType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +00001323 ASTContext &Context,
1324 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001325
Owen Anderson170229f2009-07-14 23:10:40 +00001326 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1327 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001328
1329 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1330 CodeGenFunction &CGF) const;
1331};
1332
Owen Anderson170229f2009-07-14 23:10:40 +00001333void ARMABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1334 llvm::LLVMContext &VMContext) const {
1335 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context,
1336 VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001337 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1338 it != ie; ++it) {
Owen Anderson170229f2009-07-14 23:10:40 +00001339 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001340 }
1341}
1342
1343ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty,
Owen Anderson170229f2009-07-14 23:10:40 +00001344 ASTContext &Context,
1345 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001346 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00001347 return (Ty->isPromotableIntegerType() ?
1348 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001349 }
1350 // FIXME: This is kind of nasty... but there isn't much choice because the ARM
1351 // backend doesn't support byval.
1352 // FIXME: This doesn't handle alignment > 64 bits.
1353 const llvm::Type* ElemTy;
1354 unsigned SizeRegs;
1355 if (Context.getTypeAlign(Ty) > 32) {
1356 ElemTy = llvm::Type::Int64Ty;
1357 SizeRegs = (Context.getTypeSize(Ty) + 63) / 64;
1358 } else {
1359 ElemTy = llvm::Type::Int32Ty;
1360 SizeRegs = (Context.getTypeSize(Ty) + 31) / 32;
1361 }
1362 std::vector<const llvm::Type*> LLVMFields;
Owen Anderson170229f2009-07-14 23:10:40 +00001363 LLVMFields.push_back(VMContext.getArrayType(ElemTy, SizeRegs));
1364 const llvm::Type* STy = VMContext.getStructType(LLVMFields, true);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001365 return ABIArgInfo::getCoerce(STy);
1366}
1367
1368ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +00001369 ASTContext &Context,
1370 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001371 if (RetTy->isVoidType()) {
1372 return ABIArgInfo::getIgnore();
1373 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1374 // Aggregates <= 4 bytes are returned in r0; other aggregates
1375 // are returned indirectly.
1376 uint64_t Size = Context.getTypeSize(RetTy);
1377 if (Size <= 32)
1378 return ABIArgInfo::getCoerce(llvm::Type::Int32Ty);
1379 return ABIArgInfo::getIndirect(0);
1380 } else {
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00001381 return (RetTy->isPromotableIntegerType() ?
1382 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001383 }
1384}
1385
1386llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1387 CodeGenFunction &CGF) const {
Owen Anderson170229f2009-07-14 23:10:40 +00001388 llvm::LLVMContext &VMContext = CGF.getLLVMContext();
1389
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001390 // FIXME: Need to handle alignment
Owen Anderson170229f2009-07-14 23:10:40 +00001391 const llvm::Type *BP = VMContext.getPointerTypeUnqual(llvm::Type::Int8Ty);
1392 const llvm::Type *BPP = VMContext.getPointerTypeUnqual(BP);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001393
1394 CGBuilderTy &Builder = CGF.Builder;
1395 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
1396 "ap");
1397 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
1398 llvm::Type *PTy =
Owen Anderson170229f2009-07-14 23:10:40 +00001399 VMContext.getPointerTypeUnqual(CGF.ConvertType(Ty));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001400 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
1401
1402 uint64_t Offset =
1403 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
1404 llvm::Value *NextAddr =
1405 Builder.CreateGEP(Addr,
Owen Andersonb7a2fe62009-07-24 23:12:58 +00001406 llvm::ConstantInt::get(llvm::Type::Int32Ty, Offset),
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001407 "ap.next");
1408 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
1409
1410 return AddrTyped;
1411}
1412
1413ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +00001414 ASTContext &Context,
1415 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001416 if (RetTy->isVoidType()) {
1417 return ABIArgInfo::getIgnore();
1418 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1419 return ABIArgInfo::getIndirect(0);
1420 } else {
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00001421 return (RetTy->isPromotableIntegerType() ?
1422 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001423 }
1424}
1425
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00001426namespace {
1427class SystemZABIInfo : public ABIInfo {
1428 bool isPromotableIntegerType(QualType Ty) const;
1429
1430 ABIArgInfo classifyReturnType(QualType RetTy, ASTContext &Context,
1431 llvm::LLVMContext &VMContext) const;
1432
1433 ABIArgInfo classifyArgumentType(QualType RetTy, ASTContext &Context,
1434 llvm::LLVMContext &VMContext) const;
1435
1436 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1437 llvm::LLVMContext &VMContext) const {
1438 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(),
1439 Context, VMContext);
1440 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1441 it != ie; ++it)
1442 it->info = classifyArgumentType(it->type, Context, VMContext);
1443 }
1444
1445 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1446 CodeGenFunction &CGF) const;
1447};
1448}
1449
1450bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const {
1451 // SystemZ ABI requires all 8, 16 and 32 bit quantities to be extended.
1452 if (const BuiltinType *BT = Ty->getAsBuiltinType())
1453 switch (BT->getKind()) {
1454 case BuiltinType::Bool:
1455 case BuiltinType::Char_S:
1456 case BuiltinType::Char_U:
1457 case BuiltinType::SChar:
1458 case BuiltinType::UChar:
1459 case BuiltinType::Short:
1460 case BuiltinType::UShort:
1461 case BuiltinType::Int:
1462 case BuiltinType::UInt:
1463 return true;
1464 default:
1465 return false;
1466 }
1467 return false;
1468}
1469
1470llvm::Value *SystemZABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1471 CodeGenFunction &CGF) const {
1472 // FIXME: Implement
1473 return 0;
1474}
1475
1476
1477ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy,
1478 ASTContext &Context,
1479 llvm::LLVMContext &VMContext) const {
1480 if (RetTy->isVoidType()) {
1481 return ABIArgInfo::getIgnore();
1482 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1483 return ABIArgInfo::getIndirect(0);
1484 } else {
1485 return (isPromotableIntegerType(RetTy) ?
1486 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1487 }
1488}
1489
1490ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty,
1491 ASTContext &Context,
1492 llvm::LLVMContext &VMContext) const {
1493 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
1494 return ABIArgInfo::getIndirect(0);
1495 } else {
1496 return (isPromotableIntegerType(Ty) ?
1497 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1498 }
1499}
1500
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001501ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty,
Owen Anderson170229f2009-07-14 23:10:40 +00001502 ASTContext &Context,
1503 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001504 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
1505 return ABIArgInfo::getIndirect(0);
1506 } else {
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00001507 return (Ty->isPromotableIntegerType() ?
1508 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001509 }
1510}
1511
1512llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1513 CodeGenFunction &CGF) const {
1514 return 0;
1515}
1516
1517const ABIInfo &CodeGenTypes::getABIInfo() const {
1518 if (TheABIInfo)
1519 return *TheABIInfo;
1520
1521 // For now we just cache this in the CodeGenTypes and don't bother
1522 // to free it.
1523 const char *TargetPrefix = getContext().Target.getTargetPrefix();
1524 if (strcmp(TargetPrefix, "x86") == 0) {
1525 bool IsDarwin = strstr(getContext().Target.getTargetTriple(), "darwin");
1526 switch (getContext().Target.getPointerWidth(0)) {
1527 case 32:
1528 return *(TheABIInfo = new X86_32ABIInfo(Context, IsDarwin));
1529 case 64:
1530 return *(TheABIInfo = new X86_64ABIInfo());
1531 }
1532 } else if (strcmp(TargetPrefix, "arm") == 0) {
1533 // FIXME: Support for OABI?
1534 return *(TheABIInfo = new ARMABIInfo());
1535 } else if (strcmp(TargetPrefix, "pic16") == 0) {
1536 return *(TheABIInfo = new PIC16ABIInfo());
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00001537 } else if (strcmp(TargetPrefix, "s390x") == 0) {
1538 return *(TheABIInfo = new SystemZABIInfo());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001539 }
1540
1541 return *(TheABIInfo = new DefaultABIInfo);
1542}