blob: ae9ccb1be4a28f89ba33f6aea4ec2751b72ae183 [file] [log] [blame]
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001//===---- TargetABIInfo.cpp - Encapsulate target ABI details ----*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// These classes wrap the information about a call or function
11// definition used to handle ABI compliancy.
12//
13//===----------------------------------------------------------------------===//
14
15#include "ABIInfo.h"
16#include "CodeGenFunction.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000017#include "clang/AST/RecordLayout.h"
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000018#include "llvm/Type.h"
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 Korobeynikovcc6fa882009-06-06 09:36:29 +000031 case Extend:
32 fprintf(stderr, "Extend");
33 break;
Anton Korobeynikovc4a59eb2009-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 Kremenek6217b802009-07-29 21:53:49 +000071 const RecordType *RT = T->getAs<RecordType>();
Anton Korobeynikovc4a59eb2009-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 Kyrtzidis17945a02009-06-30 02:36:12 +000077 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
78 i != e; ++i)
Anton Korobeynikovc4a59eb2009-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 Kyrtzidis17945a02009-06-30 02:36:12 +0000102 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
103 i != e; ++i) {
Anton Korobeynikovc4a59eb2009-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 Kyrtzidis17945a02009-06-30 02:36:12 +0000145 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
146 i != e; ++i) {
Anton Korobeynikovc4a59eb2009-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 Friedmana1e6de92009-06-13 21:37:10 +0000162static bool typeContainsSSEVector(const RecordDecl *RD, ASTContext &Context) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000163 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
164 i != e; ++i) {
Eli Friedmana1e6de92009-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 Kremenek6217b802009-07-29 21:53:49 +0000171 if (const RecordType* RT = FD->getType()->getAs<RecordType>())
Eli Friedmana1e6de92009-06-13 21:37:10 +0000172 if (typeContainsSSEVector(RT->getDecl(), Context))
173 return true;
174 }
175
176 return false;
177}
178
Anton Korobeynikovc4a59eb2009-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 Andersona1cf15f2009-07-14 23:10:40 +0000186 ASTContext &Context,
187 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000188
189 ABIArgInfo classifyArgumentType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000190 ASTContext &Context,
191 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000192
Owen Andersona1cf15f2009-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 Korobeynikovc4a59eb2009-06-05 22:08:42 +0000197 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
198 it != ie; ++it)
Owen Andersona1cf15f2009-07-14 23:10:40 +0000199 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikovc4a59eb2009-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 Friedmana1e6de92009-06-13 21:37:10 +0000217 static unsigned getIndirectArgumentAlignment(QualType Ty,
218 ASTContext &Context);
219
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000220public:
221 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000222 ASTContext &Context,
223 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000224
225 ABIArgInfo classifyArgumentType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000226 ASTContext &Context,
227 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000228
Owen Andersona1cf15f2009-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 Korobeynikovc4a59eb2009-06-05 22:08:42 +0000233 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
234 it != ie; ++it)
Owen Andersona1cf15f2009-07-14 23:10:40 +0000235 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikovc4a59eb2009-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 Kremenek6217b802009-07-29 21:53:49 +0000275 const RecordType *RT = Ty->getAs<RecordType>();
Anton Korobeynikovc4a59eb2009-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 Kyrtzidis17945a02009-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 Korobeynikovc4a59eb2009-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 Andersona1cf15f2009-07-14 23:10:40 +0000297 ASTContext &Context,
298 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovc4a59eb2009-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 Andersona1cf15f2009-07-14 23:10:40 +0000310 return
Owen Anderson96e0fc72009-07-29 22:16:19 +0000311 ABIArgInfo::getCoerce(llvm::VectorType::get(llvm::Type::Int64Ty,
Anton Korobeynikovc4a59eb2009-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 Anderson96e0fc72009-07-29 22:16:19 +0000318 return ABIArgInfo::getCoerce(llvm::IntegerType::get(Size));
Anton Korobeynikovc4a59eb2009-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 Andersona1cf15f2009-07-14 23:10:40 +0000342 return ABIArgInfo::getCoerce(
Owen Anderson96e0fc72009-07-29 22:16:19 +0000343 llvm::IntegerType::get((unsigned) Size));
Anton Korobeynikovc4a59eb2009-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 Anderson96e0fc72009-07-29 22:16:19 +0000357 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
Anton Korobeynikovc4a59eb2009-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 Andersona1cf15f2009-07-14 23:10:40 +0000366 return classifyReturnType(QualType(SeltTy, 0), Context, VMContext);
Anton Korobeynikovc4a59eb2009-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 Anderson96e0fc72009-07-29 22:16:19 +0000374 return ABIArgInfo::getCoerce(llvm::IntegerType::get(Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000375 }
376
377 return ABIArgInfo::getIndirect(0);
378 } else {
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000379 return (RetTy->isPromotableIntegerType() ?
380 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000381 }
382}
383
Eli Friedmana1e6de92009-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 Kremenek6217b802009-07-29 21:53:49 +0000388 if (const RecordType* RT = Ty->getAs<RecordType>())
Eli Friedmana1e6de92009-06-13 21:37:10 +0000389 if (typeContainsSSEVector(RT->getDecl(), Context))
390 return 16;
391 return 0;
392}
393
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000394ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000395 ASTContext &Context,
396 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovc4a59eb2009-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 Friedmana1e6de92009-06-13 21:37:10 +0000402 return ABIArgInfo::getIndirect(getIndirectArgumentAlignment(Ty,
403 Context));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000404
405 // Ignore empty structs.
Eli Friedmana1e6de92009-06-13 21:37:10 +0000406 if (Ty->isStructureType() && Context.getTypeSize(Ty) == 0)
Anton Korobeynikovc4a59eb2009-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 Friedmana1e6de92009-06-13 21:37:10 +0000418 return ABIArgInfo::getIndirect(getIndirectArgumentAlignment(Ty, Context));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000419 } else {
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000420 return (Ty->isPromotableIntegerType() ?
421 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000422 }
423}
424
425llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
426 CodeGenFunction &CGF) const {
Owen Anderson96e0fc72009-07-29 22:16:19 +0000427 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
428 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000429
430 CGBuilderTy &Builder = CGF.Builder;
431 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
432 "ap");
433 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
434 llvm::Type *PTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000435 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000436 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
437
438 uint64_t Offset =
439 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
440 llvm::Value *NextAddr =
441 Builder.CreateGEP(Addr,
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000442 llvm::ConstantInt::get(llvm::Type::Int32Ty, Offset),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000443 "ap.next");
444 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
445
446 return AddrTyped;
447}
448
449namespace {
450/// X86_64ABIInfo - The X86_64 ABI information.
451class X86_64ABIInfo : public ABIInfo {
452 enum Class {
453 Integer = 0,
454 SSE,
455 SSEUp,
456 X87,
457 X87Up,
458 ComplexX87,
459 NoClass,
460 Memory
461 };
462
463 /// merge - Implement the X86_64 ABI merging algorithm.
464 ///
465 /// Merge an accumulating classification \arg Accum with a field
466 /// classification \arg Field.
467 ///
468 /// \param Accum - The accumulating classification. This should
469 /// always be either NoClass or the result of a previous merge
470 /// call. In addition, this should never be Memory (the caller
471 /// should just return Memory for the aggregate).
472 Class merge(Class Accum, Class Field) const;
473
474 /// classify - Determine the x86_64 register classes in which the
475 /// given type T should be passed.
476 ///
477 /// \param Lo - The classification for the parts of the type
478 /// residing in the low word of the containing object.
479 ///
480 /// \param Hi - The classification for the parts of the type
481 /// residing in the high word of the containing object.
482 ///
483 /// \param OffsetBase - The bit offset of this type in the
484 /// containing object. Some parameters are classified different
485 /// depending on whether they straddle an eightbyte boundary.
486 ///
487 /// If a word is unused its result will be NoClass; if a type should
488 /// be passed in Memory then at least the classification of \arg Lo
489 /// will be Memory.
490 ///
491 /// The \arg Lo class will be NoClass iff the argument is ignored.
492 ///
493 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
494 /// also be ComplexX87.
495 void classify(QualType T, ASTContext &Context, uint64_t OffsetBase,
496 Class &Lo, Class &Hi) const;
497
498 /// getCoerceResult - Given a source type \arg Ty and an LLVM type
499 /// to coerce to, chose the best way to pass Ty in the same place
500 /// that \arg CoerceTo would be passed, but while keeping the
501 /// emitted code as simple as possible.
502 ///
503 /// FIXME: Note, this should be cleaned up to just take an enumeration of all
504 /// the ways we might want to pass things, instead of constructing an LLVM
505 /// type. This makes this code more explicit, and it makes it clearer that we
506 /// are also doing this for correctness in the case of passing scalar types.
507 ABIArgInfo getCoerceResult(QualType Ty,
508 const llvm::Type *CoerceTo,
509 ASTContext &Context) const;
510
511 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
512 /// such that the argument will be passed in memory.
513 ABIArgInfo getIndirectResult(QualType Ty,
514 ASTContext &Context) const;
515
516 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000517 ASTContext &Context,
518 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000519
520 ABIArgInfo classifyArgumentType(QualType Ty,
521 ASTContext &Context,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000522 llvm::LLVMContext &VMContext,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000523 unsigned &neededInt,
524 unsigned &neededSSE) const;
525
526public:
Owen Andersona1cf15f2009-07-14 23:10:40 +0000527 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
528 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000529
530 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
531 CodeGenFunction &CGF) const;
532};
533}
534
535X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum,
536 Class Field) const {
537 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
538 // classified recursively so that always two fields are
539 // considered. The resulting class is calculated according to
540 // the classes of the fields in the eightbyte:
541 //
542 // (a) If both classes are equal, this is the resulting class.
543 //
544 // (b) If one of the classes is NO_CLASS, the resulting class is
545 // the other class.
546 //
547 // (c) If one of the classes is MEMORY, the result is the MEMORY
548 // class.
549 //
550 // (d) If one of the classes is INTEGER, the result is the
551 // INTEGER.
552 //
553 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
554 // MEMORY is used as class.
555 //
556 // (f) Otherwise class SSE is used.
557
558 // Accum should never be memory (we should have returned) or
559 // ComplexX87 (because this cannot be passed in a structure).
560 assert((Accum != Memory && Accum != ComplexX87) &&
561 "Invalid accumulated classification during merge.");
562 if (Accum == Field || Field == NoClass)
563 return Accum;
564 else if (Field == Memory)
565 return Memory;
566 else if (Accum == NoClass)
567 return Field;
568 else if (Accum == Integer || Field == Integer)
569 return Integer;
570 else if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
571 Accum == X87 || Accum == X87Up)
572 return Memory;
573 else
574 return SSE;
575}
576
577void X86_64ABIInfo::classify(QualType Ty,
578 ASTContext &Context,
579 uint64_t OffsetBase,
580 Class &Lo, Class &Hi) const {
581 // FIXME: This code can be simplified by introducing a simple value class for
582 // Class pairs with appropriate constructor methods for the various
583 // situations.
584
585 // FIXME: Some of the split computations are wrong; unaligned vectors
586 // shouldn't be passed in registers for example, so there is no chance they
587 // can straddle an eightbyte. Verify & simplify.
588
589 Lo = Hi = NoClass;
590
591 Class &Current = OffsetBase < 64 ? Lo : Hi;
592 Current = Memory;
593
594 if (const BuiltinType *BT = Ty->getAsBuiltinType()) {
595 BuiltinType::Kind k = BT->getKind();
596
597 if (k == BuiltinType::Void) {
598 Current = NoClass;
599 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
600 Lo = Integer;
601 Hi = Integer;
602 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
603 Current = Integer;
604 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
605 Current = SSE;
606 } else if (k == BuiltinType::LongDouble) {
607 Lo = X87;
608 Hi = X87Up;
609 }
610 // FIXME: _Decimal32 and _Decimal64 are SSE.
611 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
612 } else if (const EnumType *ET = Ty->getAsEnumType()) {
613 // Classify the underlying integer type.
614 classify(ET->getDecl()->getIntegerType(), Context, OffsetBase, Lo, Hi);
615 } else if (Ty->hasPointerRepresentation()) {
616 Current = Integer;
617 } else if (const VectorType *VT = Ty->getAsVectorType()) {
618 uint64_t Size = Context.getTypeSize(VT);
619 if (Size == 32) {
620 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
621 // float> as integer.
622 Current = Integer;
623
624 // If this type crosses an eightbyte boundary, it should be
625 // split.
626 uint64_t EB_Real = (OffsetBase) / 64;
627 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
628 if (EB_Real != EB_Imag)
629 Hi = Lo;
630 } else if (Size == 64) {
631 // gcc passes <1 x double> in memory. :(
632 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
633 return;
634
635 // gcc passes <1 x long long> as INTEGER.
636 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong))
637 Current = Integer;
638 else
639 Current = SSE;
640
641 // If this type crosses an eightbyte boundary, it should be
642 // split.
643 if (OffsetBase && OffsetBase != 64)
644 Hi = Lo;
645 } else if (Size == 128) {
646 Lo = SSE;
647 Hi = SSEUp;
648 }
649 } else if (const ComplexType *CT = Ty->getAsComplexType()) {
650 QualType ET = Context.getCanonicalType(CT->getElementType());
651
652 uint64_t Size = Context.getTypeSize(Ty);
653 if (ET->isIntegralType()) {
654 if (Size <= 64)
655 Current = Integer;
656 else if (Size <= 128)
657 Lo = Hi = Integer;
658 } else if (ET == Context.FloatTy)
659 Current = SSE;
660 else if (ET == Context.DoubleTy)
661 Lo = Hi = SSE;
662 else if (ET == Context.LongDoubleTy)
663 Current = ComplexX87;
664
665 // If this complex type crosses an eightbyte boundary then it
666 // should be split.
667 uint64_t EB_Real = (OffsetBase) / 64;
668 uint64_t EB_Imag = (OffsetBase + Context.getTypeSize(ET)) / 64;
669 if (Hi == NoClass && EB_Real != EB_Imag)
670 Hi = Lo;
671 } else if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
672 // Arrays are treated like structures.
673
674 uint64_t Size = Context.getTypeSize(Ty);
675
676 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
677 // than two eightbytes, ..., it has class MEMORY.
678 if (Size > 128)
679 return;
680
681 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
682 // fields, it has class MEMORY.
683 //
684 // Only need to check alignment of array base.
685 if (OffsetBase % Context.getTypeAlign(AT->getElementType()))
686 return;
687
688 // Otherwise implement simplified merge. We could be smarter about
689 // this, but it isn't worth it and would be harder to verify.
690 Current = NoClass;
691 uint64_t EltSize = Context.getTypeSize(AT->getElementType());
692 uint64_t ArraySize = AT->getSize().getZExtValue();
693 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
694 Class FieldLo, FieldHi;
695 classify(AT->getElementType(), Context, Offset, FieldLo, FieldHi);
696 Lo = merge(Lo, FieldLo);
697 Hi = merge(Hi, FieldHi);
698 if (Lo == Memory || Hi == Memory)
699 break;
700 }
701
702 // Do post merger cleanup (see below). Only case we worry about is Memory.
703 if (Hi == Memory)
704 Lo = Memory;
705 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Ted Kremenek6217b802009-07-29 21:53:49 +0000706 } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000707 uint64_t Size = Context.getTypeSize(Ty);
708
709 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
710 // than two eightbytes, ..., it has class MEMORY.
711 if (Size > 128)
712 return;
713
714 const RecordDecl *RD = RT->getDecl();
715
716 // Assume variable sized types are passed in memory.
717 if (RD->hasFlexibleArrayMember())
718 return;
719
720 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
721
722 // Reset Lo class, this will be recomputed.
723 Current = NoClass;
724 unsigned idx = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000725 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
726 i != e; ++i, ++idx) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000727 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
728 bool BitField = i->isBitField();
729
730 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
731 // fields, it has class MEMORY.
732 //
733 // Note, skip this test for bit-fields, see below.
734 if (!BitField && Offset % Context.getTypeAlign(i->getType())) {
735 Lo = Memory;
736 return;
737 }
738
739 // Classify this field.
740 //
741 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
742 // exceeds a single eightbyte, each is classified
743 // separately. Each eightbyte gets initialized to class
744 // NO_CLASS.
745 Class FieldLo, FieldHi;
746
747 // Bit-fields require special handling, they do not force the
748 // structure to be passed in memory even if unaligned, and
749 // therefore they can straddle an eightbyte.
750 if (BitField) {
751 // Ignore padding bit-fields.
752 if (i->isUnnamedBitfield())
753 continue;
754
755 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
756 uint64_t Size = i->getBitWidth()->EvaluateAsInt(Context).getZExtValue();
757
758 uint64_t EB_Lo = Offset / 64;
759 uint64_t EB_Hi = (Offset + Size - 1) / 64;
760 FieldLo = FieldHi = NoClass;
761 if (EB_Lo) {
762 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
763 FieldLo = NoClass;
764 FieldHi = Integer;
765 } else {
766 FieldLo = Integer;
767 FieldHi = EB_Hi ? Integer : NoClass;
768 }
769 } else
770 classify(i->getType(), Context, Offset, FieldLo, FieldHi);
771 Lo = merge(Lo, FieldLo);
772 Hi = merge(Hi, FieldHi);
773 if (Lo == Memory || Hi == Memory)
774 break;
775 }
776
777 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
778 //
779 // (a) If one of the classes is MEMORY, the whole argument is
780 // passed in memory.
781 //
782 // (b) If SSEUP is not preceeded by SSE, it is converted to SSE.
783
784 // The first of these conditions is guaranteed by how we implement
785 // the merge (just bail).
786 //
787 // The second condition occurs in the case of unions; for example
788 // union { _Complex double; unsigned; }.
789 if (Hi == Memory)
790 Lo = Memory;
791 if (Hi == SSEUp && Lo != SSE)
792 Hi = SSE;
793 }
794}
795
796ABIArgInfo X86_64ABIInfo::getCoerceResult(QualType Ty,
797 const llvm::Type *CoerceTo,
798 ASTContext &Context) const {
799 if (CoerceTo == llvm::Type::Int64Ty) {
800 // Integer and pointer types will end up in a general purpose
801 // register.
802 if (Ty->isIntegralType() || Ty->isPointerType())
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000803 return (Ty->isPromotableIntegerType() ?
804 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000805 } else if (CoerceTo == llvm::Type::DoubleTy) {
806 // FIXME: It would probably be better to make CGFunctionInfo only map using
807 // canonical types than to canonize here.
808 QualType CTy = Context.getCanonicalType(Ty);
809
810 // Float and double end up in a single SSE reg.
811 if (CTy == Context.FloatTy || CTy == Context.DoubleTy)
812 return ABIArgInfo::getDirect();
813
814 }
815
816 return ABIArgInfo::getCoerce(CoerceTo);
817}
818
819ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty,
820 ASTContext &Context) const {
821 // If this is a scalar LLVM value then assume LLVM will pass it in the right
822 // place naturally.
823 if (!CodeGenFunction::hasAggregateLLVMType(Ty))
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000824 return (Ty->isPromotableIntegerType() ?
825 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000826
827 // FIXME: Set alignment correctly.
828 return ABIArgInfo::getIndirect(0);
829}
830
831ABIArgInfo X86_64ABIInfo::classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000832 ASTContext &Context,
833 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000834 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
835 // classification algorithm.
836 X86_64ABIInfo::Class Lo, Hi;
837 classify(RetTy, Context, 0, Lo, Hi);
838
839 // Check some invariants.
840 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
841 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
842 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
843
844 const llvm::Type *ResType = 0;
845 switch (Lo) {
846 case NoClass:
847 return ABIArgInfo::getIgnore();
848
849 case SSEUp:
850 case X87Up:
851 assert(0 && "Invalid classification for lo word.");
852
853 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
854 // hidden argument.
855 case Memory:
856 return getIndirectResult(RetTy, Context);
857
858 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
859 // available register of the sequence %rax, %rdx is used.
860 case Integer:
861 ResType = llvm::Type::Int64Ty; break;
862
863 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
864 // available SSE register of the sequence %xmm0, %xmm1 is used.
865 case SSE:
866 ResType = llvm::Type::DoubleTy; break;
867
868 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
869 // returned on the X87 stack in %st0 as 80-bit x87 number.
870 case X87:
871 ResType = llvm::Type::X86_FP80Ty; break;
872
873 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
874 // part of the value is returned in %st0 and the imaginary part in
875 // %st1.
876 case ComplexX87:
877 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Owen Anderson96e0fc72009-07-29 22:16:19 +0000878 ResType = llvm::StructType::get(llvm::Type::X86_FP80Ty,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000879 llvm::Type::X86_FP80Ty,
880 NULL);
881 break;
882 }
883
884 switch (Hi) {
885 // Memory was handled previously and X87 should
886 // never occur as a hi class.
887 case Memory:
888 case X87:
889 assert(0 && "Invalid classification for hi word.");
890
891 case ComplexX87: // Previously handled.
892 case NoClass: break;
893
894 case Integer:
Owen Anderson96e0fc72009-07-29 22:16:19 +0000895 ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000896 break;
897 case SSE:
Owen Anderson96e0fc72009-07-29 22:16:19 +0000898 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000899 break;
900
901 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
902 // is passed in the upper half of the last used SSE register.
903 //
904 // SSEUP should always be preceeded by SSE, just widen.
905 case SSEUp:
906 assert(Lo == SSE && "Unexpected SSEUp classification.");
Owen Anderson96e0fc72009-07-29 22:16:19 +0000907 ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000908 break;
909
910 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
911 // returned together with the previous X87 value in %st0.
912 case X87Up:
913 // If X87Up is preceeded by X87, we don't need to do
914 // anything. However, in some cases with unions it may not be
915 // preceeded by X87. In such situations we follow gcc and pass the
916 // extra bits in an SSE reg.
917 if (Lo != X87)
Owen Anderson96e0fc72009-07-29 22:16:19 +0000918 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000919 break;
920 }
921
922 return getCoerceResult(RetTy, ResType, Context);
923}
924
925ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, ASTContext &Context,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000926 llvm::LLVMContext &VMContext,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000927 unsigned &neededInt,
928 unsigned &neededSSE) const {
929 X86_64ABIInfo::Class Lo, Hi;
930 classify(Ty, Context, 0, Lo, Hi);
931
932 // Check some invariants.
933 // FIXME: Enforce these by construction.
934 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
935 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
936 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
937
938 neededInt = 0;
939 neededSSE = 0;
940 const llvm::Type *ResType = 0;
941 switch (Lo) {
942 case NoClass:
943 return ABIArgInfo::getIgnore();
944
945 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
946 // on the stack.
947 case Memory:
948
949 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
950 // COMPLEX_X87, it is passed in memory.
951 case X87:
952 case ComplexX87:
953 return getIndirectResult(Ty, Context);
954
955 case SSEUp:
956 case X87Up:
957 assert(0 && "Invalid classification for lo word.");
958
959 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
960 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
961 // and %r9 is used.
962 case Integer:
963 ++neededInt;
964 ResType = llvm::Type::Int64Ty;
965 break;
966
967 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
968 // available SSE register is used, the registers are taken in the
969 // order from %xmm0 to %xmm7.
970 case SSE:
971 ++neededSSE;
972 ResType = llvm::Type::DoubleTy;
973 break;
974 }
975
976 switch (Hi) {
977 // Memory was handled previously, ComplexX87 and X87 should
978 // never occur as hi classes, and X87Up must be preceed by X87,
979 // which is passed in memory.
980 case Memory:
981 case X87:
982 case ComplexX87:
983 assert(0 && "Invalid classification for hi word.");
984 break;
985
986 case NoClass: break;
987 case Integer:
Owen Anderson96e0fc72009-07-29 22:16:19 +0000988 ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000989 ++neededInt;
990 break;
991
992 // X87Up generally doesn't occur here (long double is passed in
993 // memory), except in situations involving unions.
994 case X87Up:
995 case SSE:
Owen Anderson96e0fc72009-07-29 22:16:19 +0000996 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000997 ++neededSSE;
998 break;
999
1000 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
1001 // eightbyte is passed in the upper half of the last used SSE
1002 // register.
1003 case SSEUp:
1004 assert(Lo == SSE && "Unexpected SSEUp classification.");
Owen Anderson96e0fc72009-07-29 22:16:19 +00001005 ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001006 break;
1007 }
1008
1009 return getCoerceResult(Ty, ResType, Context);
1010}
1011
Owen Andersona1cf15f2009-07-14 23:10:40 +00001012void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1013 llvm::LLVMContext &VMContext) const {
1014 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(),
1015 Context, VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001016
1017 // Keep track of the number of assigned registers.
1018 unsigned freeIntRegs = 6, freeSSERegs = 8;
1019
1020 // If the return value is indirect, then the hidden argument is consuming one
1021 // integer register.
1022 if (FI.getReturnInfo().isIndirect())
1023 --freeIntRegs;
1024
1025 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
1026 // get assigned (in left-to-right order) for passing as follows...
1027 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1028 it != ie; ++it) {
1029 unsigned neededInt, neededSSE;
Owen Andersona1cf15f2009-07-14 23:10:40 +00001030 it->info = classifyArgumentType(it->type, Context, VMContext,
1031 neededInt, neededSSE);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001032
1033 // AMD64-ABI 3.2.3p3: If there are no registers available for any
1034 // eightbyte of an argument, the whole argument is passed on the
1035 // stack. If registers have already been assigned for some
1036 // eightbytes of such an argument, the assignments get reverted.
1037 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
1038 freeIntRegs -= neededInt;
1039 freeSSERegs -= neededSSE;
1040 } else {
1041 it->info = getIndirectResult(it->type, Context);
1042 }
1043 }
1044}
1045
1046static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
1047 QualType Ty,
1048 CodeGenFunction &CGF) {
1049 llvm::Value *overflow_arg_area_p =
1050 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
1051 llvm::Value *overflow_arg_area =
1052 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
1053
1054 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
1055 // byte boundary if alignment needed by type exceeds 8 byte boundary.
1056 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
1057 if (Align > 8) {
1058 // Note that we follow the ABI & gcc here, even though the type
1059 // could in theory have an alignment greater than 16. This case
1060 // shouldn't ever matter in practice.
1061
1062 // overflow_arg_area = (overflow_arg_area + 15) & ~15;
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001063 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty, 15);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001064 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
1065 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
1066 llvm::Type::Int64Ty);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001067 llvm::Value *Mask = llvm::ConstantInt::get(llvm::Type::Int64Ty, ~15LL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001068 overflow_arg_area =
1069 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
1070 overflow_arg_area->getType(),
1071 "overflow_arg_area.align");
1072 }
1073
1074 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
1075 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1076 llvm::Value *Res =
1077 CGF.Builder.CreateBitCast(overflow_arg_area,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001078 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001079
1080 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
1081 // l->overflow_arg_area + sizeof(type).
1082 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
1083 // an 8 byte boundary.
1084
1085 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001086 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001087 (SizeInBytes + 7) & ~7);
1088 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
1089 "overflow_arg_area.next");
1090 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
1091
1092 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
1093 return Res;
1094}
1095
1096llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1097 CodeGenFunction &CGF) const {
Owen Andersona1cf15f2009-07-14 23:10:40 +00001098 llvm::LLVMContext &VMContext = CGF.getLLVMContext();
1099
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001100 // Assume that va_list type is correct; should be pointer to LLVM type:
1101 // struct {
1102 // i32 gp_offset;
1103 // i32 fp_offset;
1104 // i8* overflow_arg_area;
1105 // i8* reg_save_area;
1106 // };
1107 unsigned neededInt, neededSSE;
Owen Andersona1cf15f2009-07-14 23:10:40 +00001108 ABIArgInfo AI = classifyArgumentType(Ty, CGF.getContext(), VMContext,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001109 neededInt, neededSSE);
1110
1111 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
1112 // in the registers. If not go to step 7.
1113 if (!neededInt && !neededSSE)
1114 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1115
1116 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
1117 // general purpose registers needed to pass type and num_fp to hold
1118 // the number of floating point registers needed.
1119
1120 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
1121 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
1122 // l->fp_offset > 304 - num_fp * 16 go to step 7.
1123 //
1124 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
1125 // register save space).
1126
1127 llvm::Value *InRegs = 0;
1128 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
1129 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
1130 if (neededInt) {
1131 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
1132 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
1133 InRegs =
1134 CGF.Builder.CreateICmpULE(gp_offset,
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001135 llvm::ConstantInt::get(llvm::Type::Int32Ty,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001136 48 - neededInt * 8),
1137 "fits_in_gp");
1138 }
1139
1140 if (neededSSE) {
1141 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
1142 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
1143 llvm::Value *FitsInFP =
1144 CGF.Builder.CreateICmpULE(fp_offset,
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001145 llvm::ConstantInt::get(llvm::Type::Int32Ty,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001146 176 - neededSSE * 16),
1147 "fits_in_fp");
1148 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
1149 }
1150
1151 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
1152 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
1153 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
1154 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
1155
1156 // Emit code to load the value if it was passed in registers.
1157
1158 CGF.EmitBlock(InRegBlock);
1159
1160 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
1161 // an offset of l->gp_offset and/or l->fp_offset. This may require
1162 // copying to a temporary location in case the parameter is passed
1163 // in different register classes or requires an alignment greater
1164 // than 8 for general purpose registers and 16 for XMM registers.
1165 //
1166 // FIXME: This really results in shameful code when we end up needing to
1167 // collect arguments from different places; often what should result in a
1168 // simple assembling of a structure from scattered addresses has many more
1169 // loads than necessary. Can we clean this up?
1170 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1171 llvm::Value *RegAddr =
1172 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
1173 "reg_save_area");
1174 if (neededInt && neededSSE) {
1175 // FIXME: Cleanup.
1176 assert(AI.isCoerce() && "Unexpected ABI info for mixed regs");
1177 const llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
1178 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
1179 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
1180 const llvm::Type *TyLo = ST->getElementType(0);
1181 const llvm::Type *TyHi = ST->getElementType(1);
1182 assert((TyLo->isFloatingPoint() ^ TyHi->isFloatingPoint()) &&
1183 "Unexpected ABI info for mixed regs");
Owen Anderson96e0fc72009-07-29 22:16:19 +00001184 const llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
1185 const llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001186 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1187 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1188 llvm::Value *RegLoAddr = TyLo->isFloatingPoint() ? FPAddr : GPAddr;
1189 llvm::Value *RegHiAddr = TyLo->isFloatingPoint() ? GPAddr : FPAddr;
1190 llvm::Value *V =
1191 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
1192 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1193 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
1194 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1195
Owen Andersona1cf15f2009-07-14 23:10:40 +00001196 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001197 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001198 } else if (neededInt) {
1199 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1200 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001201 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001202 } else {
1203 if (neededSSE == 1) {
1204 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1205 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001206 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001207 } else {
1208 assert(neededSSE == 2 && "Invalid number of needed registers!");
1209 // SSE registers are spaced 16 bytes apart in the register save
1210 // area, we need to collect the two eightbytes together.
1211 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1212 llvm::Value *RegAddrHi =
1213 CGF.Builder.CreateGEP(RegAddrLo,
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001214 llvm::ConstantInt::get(llvm::Type::Int32Ty, 16));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001215 const llvm::Type *DblPtrTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00001216 llvm::PointerType::getUnqual(llvm::Type::DoubleTy);
1217 const llvm::StructType *ST = llvm::StructType::get(llvm::Type::DoubleTy,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001218 llvm::Type::DoubleTy,
1219 NULL);
1220 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
1221 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
1222 DblPtrTy));
1223 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1224 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
1225 DblPtrTy));
1226 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1227 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001228 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001229 }
1230 }
1231
1232 // AMD64-ABI 3.5.7p5: Step 5. Set:
1233 // l->gp_offset = l->gp_offset + num_gp * 8
1234 // l->fp_offset = l->fp_offset + num_fp * 16.
1235 if (neededInt) {
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001236 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001237 neededInt * 8);
1238 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
1239 gp_offset_p);
1240 }
1241 if (neededSSE) {
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001242 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001243 neededSSE * 16);
1244 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
1245 fp_offset_p);
1246 }
1247 CGF.EmitBranch(ContBlock);
1248
1249 // Emit code to load the value if it was passed in memory.
1250
1251 CGF.EmitBlock(InMemBlock);
1252 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1253
1254 // Return the appropriate result.
1255
1256 CGF.EmitBlock(ContBlock);
1257 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(),
1258 "vaarg.addr");
1259 ResAddr->reserveOperandSpace(2);
1260 ResAddr->addIncoming(RegAddr, InRegBlock);
1261 ResAddr->addIncoming(MemAddr, InMemBlock);
1262
1263 return ResAddr;
1264}
1265
1266// ABI Info for PIC16
1267class PIC16ABIInfo : public ABIInfo {
1268 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001269 ASTContext &Context,
1270 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001271
1272 ABIArgInfo classifyArgumentType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001273 ASTContext &Context,
1274 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001275
Owen Andersona1cf15f2009-07-14 23:10:40 +00001276 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1277 llvm::LLVMContext &VMContext) const {
1278 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context,
1279 VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001280 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1281 it != ie; ++it)
Owen Andersona1cf15f2009-07-14 23:10:40 +00001282 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001283 }
1284
1285 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1286 CodeGenFunction &CGF) const;
1287
1288};
1289
1290ABIArgInfo PIC16ABIInfo::classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001291 ASTContext &Context,
1292 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001293 if (RetTy->isVoidType()) {
1294 return ABIArgInfo::getIgnore();
1295 } else {
1296 return ABIArgInfo::getDirect();
1297 }
1298}
1299
1300ABIArgInfo PIC16ABIInfo::classifyArgumentType(QualType Ty,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001301 ASTContext &Context,
1302 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001303 return ABIArgInfo::getDirect();
1304}
1305
1306llvm::Value *PIC16ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1307 CodeGenFunction &CGF) const {
1308 return 0;
1309}
1310
1311class ARMABIInfo : public ABIInfo {
1312 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001313 ASTContext &Context,
1314 llvm::LLVMContext &VMCOntext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001315
1316 ABIArgInfo classifyArgumentType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001317 ASTContext &Context,
1318 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001319
Owen Andersona1cf15f2009-07-14 23:10:40 +00001320 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1321 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001322
1323 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1324 CodeGenFunction &CGF) const;
1325};
1326
Owen Andersona1cf15f2009-07-14 23:10:40 +00001327void ARMABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1328 llvm::LLVMContext &VMContext) const {
1329 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context,
1330 VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001331 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1332 it != ie; ++it) {
Owen Andersona1cf15f2009-07-14 23:10:40 +00001333 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001334 }
1335}
1336
1337ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001338 ASTContext &Context,
1339 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001340 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001341 return (Ty->isPromotableIntegerType() ?
1342 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001343 }
1344 // FIXME: This is kind of nasty... but there isn't much choice because the ARM
1345 // backend doesn't support byval.
1346 // FIXME: This doesn't handle alignment > 64 bits.
1347 const llvm::Type* ElemTy;
1348 unsigned SizeRegs;
1349 if (Context.getTypeAlign(Ty) > 32) {
1350 ElemTy = llvm::Type::Int64Ty;
1351 SizeRegs = (Context.getTypeSize(Ty) + 63) / 64;
1352 } else {
1353 ElemTy = llvm::Type::Int32Ty;
1354 SizeRegs = (Context.getTypeSize(Ty) + 31) / 32;
1355 }
1356 std::vector<const llvm::Type*> LLVMFields;
Owen Anderson96e0fc72009-07-29 22:16:19 +00001357 LLVMFields.push_back(llvm::ArrayType::get(ElemTy, SizeRegs));
1358 const llvm::Type* STy = llvm::StructType::get(LLVMFields, true);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001359 return ABIArgInfo::getCoerce(STy);
1360}
1361
1362ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001363 ASTContext &Context,
1364 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001365 if (RetTy->isVoidType()) {
1366 return ABIArgInfo::getIgnore();
1367 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1368 // Aggregates <= 4 bytes are returned in r0; other aggregates
1369 // are returned indirectly.
1370 uint64_t Size = Context.getTypeSize(RetTy);
1371 if (Size <= 32)
1372 return ABIArgInfo::getCoerce(llvm::Type::Int32Ty);
1373 return ABIArgInfo::getIndirect(0);
1374 } else {
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001375 return (RetTy->isPromotableIntegerType() ?
1376 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001377 }
1378}
1379
1380llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1381 CodeGenFunction &CGF) const {
1382 // FIXME: Need to handle alignment
Owen Anderson96e0fc72009-07-29 22:16:19 +00001383 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
1384 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001385
1386 CGBuilderTy &Builder = CGF.Builder;
1387 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
1388 "ap");
1389 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
1390 llvm::Type *PTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00001391 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001392 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
1393
1394 uint64_t Offset =
1395 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
1396 llvm::Value *NextAddr =
1397 Builder.CreateGEP(Addr,
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001398 llvm::ConstantInt::get(llvm::Type::Int32Ty, Offset),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001399 "ap.next");
1400 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
1401
1402 return AddrTyped;
1403}
1404
1405ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001406 ASTContext &Context,
1407 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001408 if (RetTy->isVoidType()) {
1409 return ABIArgInfo::getIgnore();
1410 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1411 return ABIArgInfo::getIndirect(0);
1412 } else {
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001413 return (RetTy->isPromotableIntegerType() ?
1414 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001415 }
1416}
1417
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00001418namespace {
1419class SystemZABIInfo : public ABIInfo {
1420 bool isPromotableIntegerType(QualType Ty) const;
1421
1422 ABIArgInfo classifyReturnType(QualType RetTy, ASTContext &Context,
1423 llvm::LLVMContext &VMContext) const;
1424
1425 ABIArgInfo classifyArgumentType(QualType RetTy, ASTContext &Context,
1426 llvm::LLVMContext &VMContext) const;
1427
1428 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1429 llvm::LLVMContext &VMContext) const {
1430 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(),
1431 Context, VMContext);
1432 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1433 it != ie; ++it)
1434 it->info = classifyArgumentType(it->type, Context, VMContext);
1435 }
1436
1437 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1438 CodeGenFunction &CGF) const;
1439};
1440}
1441
1442bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const {
1443 // SystemZ ABI requires all 8, 16 and 32 bit quantities to be extended.
1444 if (const BuiltinType *BT = Ty->getAsBuiltinType())
1445 switch (BT->getKind()) {
1446 case BuiltinType::Bool:
1447 case BuiltinType::Char_S:
1448 case BuiltinType::Char_U:
1449 case BuiltinType::SChar:
1450 case BuiltinType::UChar:
1451 case BuiltinType::Short:
1452 case BuiltinType::UShort:
1453 case BuiltinType::Int:
1454 case BuiltinType::UInt:
1455 return true;
1456 default:
1457 return false;
1458 }
1459 return false;
1460}
1461
1462llvm::Value *SystemZABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1463 CodeGenFunction &CGF) const {
1464 // FIXME: Implement
1465 return 0;
1466}
1467
1468
1469ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy,
1470 ASTContext &Context,
1471 llvm::LLVMContext &VMContext) const {
1472 if (RetTy->isVoidType()) {
1473 return ABIArgInfo::getIgnore();
1474 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1475 return ABIArgInfo::getIndirect(0);
1476 } else {
1477 return (isPromotableIntegerType(RetTy) ?
1478 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1479 }
1480}
1481
1482ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty,
1483 ASTContext &Context,
1484 llvm::LLVMContext &VMContext) const {
1485 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
1486 return ABIArgInfo::getIndirect(0);
1487 } else {
1488 return (isPromotableIntegerType(Ty) ?
1489 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1490 }
1491}
1492
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001493ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001494 ASTContext &Context,
1495 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001496 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
1497 return ABIArgInfo::getIndirect(0);
1498 } else {
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001499 return (Ty->isPromotableIntegerType() ?
1500 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001501 }
1502}
1503
1504llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1505 CodeGenFunction &CGF) const {
1506 return 0;
1507}
1508
1509const ABIInfo &CodeGenTypes::getABIInfo() const {
1510 if (TheABIInfo)
1511 return *TheABIInfo;
1512
1513 // For now we just cache this in the CodeGenTypes and don't bother
1514 // to free it.
1515 const char *TargetPrefix = getContext().Target.getTargetPrefix();
1516 if (strcmp(TargetPrefix, "x86") == 0) {
1517 bool IsDarwin = strstr(getContext().Target.getTargetTriple(), "darwin");
1518 switch (getContext().Target.getPointerWidth(0)) {
1519 case 32:
1520 return *(TheABIInfo = new X86_32ABIInfo(Context, IsDarwin));
1521 case 64:
1522 return *(TheABIInfo = new X86_64ABIInfo());
1523 }
1524 } else if (strcmp(TargetPrefix, "arm") == 0) {
1525 // FIXME: Support for OABI?
1526 return *(TheABIInfo = new ARMABIInfo());
1527 } else if (strcmp(TargetPrefix, "pic16") == 0) {
1528 return *(TheABIInfo = new PIC16ABIInfo());
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00001529 } else if (strcmp(TargetPrefix, "s390x") == 0) {
1530 return *(TheABIInfo = new SystemZABIInfo());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001531 }
1532
1533 return *(TheABIInfo = new DefaultABIInfo);
1534}