blob: ea1426277d19989fa9f47f7c5d20296b35a08e16 [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"
Daniel Dunbar2c0843f2009-08-24 08:52:16 +000019#include "llvm/ADT/Triple.h"
Torok Edwinf42e4a62009-08-24 13:25:12 +000020#include <cstdio>
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000021
22using namespace clang;
23using namespace CodeGen;
24
25ABIInfo::~ABIInfo() {}
26
27void ABIArgInfo::dump() const {
28 fprintf(stderr, "(ABIArgInfo Kind=");
29 switch (TheKind) {
30 case Direct:
31 fprintf(stderr, "Direct");
32 break;
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +000033 case Extend:
34 fprintf(stderr, "Extend");
35 break;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000036 case Ignore:
37 fprintf(stderr, "Ignore");
38 break;
39 case Coerce:
40 fprintf(stderr, "Coerce Type=");
41 getCoerceToType()->print(llvm::errs());
42 break;
43 case Indirect:
44 fprintf(stderr, "Indirect Align=%d", getIndirectAlign());
45 break;
46 case Expand:
47 fprintf(stderr, "Expand");
48 break;
49 }
50 fprintf(stderr, ")\n");
51}
52
Daniel Dunbar98303b92009-09-13 08:03:58 +000053static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000054
55/// isEmptyField - Return true iff a the field is "empty", that is it
56/// is an unnamed bit-field or an (array of) empty record(s).
Daniel Dunbar98303b92009-09-13 08:03:58 +000057static bool isEmptyField(ASTContext &Context, const FieldDecl *FD,
58 bool AllowArrays) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000059 if (FD->isUnnamedBitfield())
60 return true;
61
62 QualType FT = FD->getType();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000063
Daniel Dunbar98303b92009-09-13 08:03:58 +000064 // Constant arrays of empty records count as empty, strip them off.
65 if (AllowArrays)
66 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT))
67 FT = AT->getElementType();
68
69 return isEmptyRecord(Context, FT, AllowArrays);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000070}
71
72/// isEmptyRecord - Return true iff a structure contains only empty
73/// fields. Note that a structure with a flexible array member is not
74/// considered empty.
Daniel Dunbar98303b92009-09-13 08:03:58 +000075static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) {
Ted Kremenek6217b802009-07-29 21:53:49 +000076 const RecordType *RT = T->getAs<RecordType>();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000077 if (!RT)
78 return 0;
79 const RecordDecl *RD = RT->getDecl();
80 if (RD->hasFlexibleArrayMember())
81 return false;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +000082 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
83 i != e; ++i)
Daniel Dunbar98303b92009-09-13 08:03:58 +000084 if (!isEmptyField(Context, *i, AllowArrays))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000085 return false;
86 return true;
87}
88
Anders Carlsson0a8f8472009-09-16 15:53:40 +000089/// hasNonTrivialDestructorOrCopyConstructor - Determine if a type has either
90/// a non-trivial destructor or a non-trivial copy constructor.
91static bool hasNonTrivialDestructorOrCopyConstructor(const RecordType *RT) {
92 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
93 if (!RD)
94 return false;
95
96 return !RD->hasTrivialDestructor() || !RD->hasTrivialCopyConstructor();
97}
98
99/// isRecordWithNonTrivialDestructorOrCopyConstructor - Determine if a type is
100/// a record type with either a non-trivial destructor or a non-trivial copy
101/// constructor.
102static bool isRecordWithNonTrivialDestructorOrCopyConstructor(QualType T) {
103 const RecordType *RT = T->getAs<RecordType>();
104 if (!RT)
105 return false;
106
107 return hasNonTrivialDestructorOrCopyConstructor(RT);
108}
109
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000110/// isSingleElementStruct - Determine if a structure is a "single
111/// element struct", i.e. it has exactly one non-empty field or
112/// exactly one field which is itself a single element
113/// struct. Structures with flexible array members are never
114/// considered single element structs.
115///
116/// \return The field declaration for the single non-empty field, if
117/// it exists.
118static const Type *isSingleElementStruct(QualType T, ASTContext &Context) {
119 const RecordType *RT = T->getAsStructureType();
120 if (!RT)
121 return 0;
122
123 const RecordDecl *RD = RT->getDecl();
124 if (RD->hasFlexibleArrayMember())
125 return 0;
126
127 const Type *Found = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000128 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
129 i != e; ++i) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000130 const FieldDecl *FD = *i;
131 QualType FT = FD->getType();
132
133 // Ignore empty fields.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000134 if (isEmptyField(Context, FD, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000135 continue;
136
137 // If we already found an element then this isn't a single-element
138 // struct.
139 if (Found)
140 return 0;
141
142 // Treat single element arrays as the element.
143 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
144 if (AT->getSize().getZExtValue() != 1)
145 break;
146 FT = AT->getElementType();
147 }
148
149 if (!CodeGenFunction::hasAggregateLLVMType(FT)) {
150 Found = FT.getTypePtr();
151 } else {
152 Found = isSingleElementStruct(FT, Context);
153 if (!Found)
154 return 0;
155 }
156 }
157
158 return Found;
159}
160
161static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
Daniel Dunbar55e59e12009-09-24 05:12:36 +0000162 if (!Ty->getAs<BuiltinType>() && !Ty->isAnyPointerType() &&
163 !Ty->isAnyComplexType() && !Ty->isEnumeralType() &&
164 !Ty->isBlockPointerType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000165 return false;
166
167 uint64_t Size = Context.getTypeSize(Ty);
168 return Size == 32 || Size == 64;
169}
170
171static bool areAllFields32Or64BitBasicType(const RecordDecl *RD,
172 ASTContext &Context) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000173 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
174 i != e; ++i) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000175 const FieldDecl *FD = *i;
176
177 if (!is32Or64BitBasicType(FD->getType(), Context))
178 return false;
179
180 // FIXME: Reject bit-fields wholesale; there are two problems, we don't know
181 // how to expand them yet, and the predicate for telling if a bitfield still
182 // counts as "basic" is more complicated than what we were doing previously.
183 if (FD->isBitField())
184 return false;
185 }
186
187 return true;
188}
189
Eli Friedmana1e6de92009-06-13 21:37:10 +0000190static bool typeContainsSSEVector(const RecordDecl *RD, ASTContext &Context) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000191 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
192 i != e; ++i) {
Eli Friedmana1e6de92009-06-13 21:37:10 +0000193 const FieldDecl *FD = *i;
194
195 if (FD->getType()->isVectorType() &&
196 Context.getTypeSize(FD->getType()) >= 128)
197 return true;
198
Ted Kremenek6217b802009-07-29 21:53:49 +0000199 if (const RecordType* RT = FD->getType()->getAs<RecordType>())
Eli Friedmana1e6de92009-06-13 21:37:10 +0000200 if (typeContainsSSEVector(RT->getDecl(), Context))
201 return true;
202 }
203
204 return false;
205}
206
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000207namespace {
208/// DefaultABIInfo - The default implementation for ABI specific
209/// details. This implementation provides information which results in
210/// self-consistent and sensible LLVM IR generation, but does not
211/// conform to any particular ABI.
212class DefaultABIInfo : public ABIInfo {
213 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000214 ASTContext &Context,
215 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000216
217 ABIArgInfo classifyArgumentType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000218 ASTContext &Context,
219 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000220
Owen Andersona1cf15f2009-07-14 23:10:40 +0000221 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
222 llvm::LLVMContext &VMContext) const {
223 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context,
224 VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000225 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
226 it != ie; ++it)
Owen Andersona1cf15f2009-07-14 23:10:40 +0000227 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000228 }
229
230 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
231 CodeGenFunction &CGF) const;
232};
233
234/// X86_32ABIInfo - The X86-32 ABI information.
235class X86_32ABIInfo : public ABIInfo {
236 ASTContext &Context;
David Chisnall1e4249c2009-08-17 23:08:21 +0000237 bool IsDarwinVectorABI;
238 bool IsSmallStructInRegABI;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000239
240 static bool isRegisterSize(unsigned Size) {
241 return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
242 }
243
244 static bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context);
245
Eli Friedmana1e6de92009-06-13 21:37:10 +0000246 static unsigned getIndirectArgumentAlignment(QualType Ty,
247 ASTContext &Context);
248
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000249public:
250 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000251 ASTContext &Context,
252 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000253
254 ABIArgInfo classifyArgumentType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000255 ASTContext &Context,
256 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000257
Owen Andersona1cf15f2009-07-14 23:10:40 +0000258 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
259 llvm::LLVMContext &VMContext) const {
260 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context,
261 VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000262 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
263 it != ie; ++it)
Owen Andersona1cf15f2009-07-14 23:10:40 +0000264 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000265 }
266
267 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
268 CodeGenFunction &CGF) const;
269
David Chisnall1e4249c2009-08-17 23:08:21 +0000270 X86_32ABIInfo(ASTContext &Context, bool d, bool p)
Mike Stump1eb44332009-09-09 15:08:12 +0000271 : ABIInfo(), Context(Context), IsDarwinVectorABI(d),
David Chisnall1e4249c2009-08-17 23:08:21 +0000272 IsSmallStructInRegABI(p) {}
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000273};
274}
275
276
277/// shouldReturnTypeInRegister - Determine if the given type should be
278/// passed in a register (for the Darwin ABI).
279bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
280 ASTContext &Context) {
281 uint64_t Size = Context.getTypeSize(Ty);
282
283 // Type must be register sized.
284 if (!isRegisterSize(Size))
285 return false;
286
287 if (Ty->isVectorType()) {
288 // 64- and 128- bit vectors inside structures are not returned in
289 // registers.
290 if (Size == 64 || Size == 128)
291 return false;
292
293 return true;
294 }
295
Daniel Dunbar55e59e12009-09-24 05:12:36 +0000296 // If this is a builtin, pointer, enum, or complex type, it is ok.
297 if (Ty->getAs<BuiltinType>() || Ty->isAnyPointerType() ||
298 Ty->isAnyComplexType() || Ty->isEnumeralType() ||
299 Ty->isBlockPointerType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000300 return true;
301
302 // Arrays are treated like records.
303 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
304 return shouldReturnTypeInRegister(AT->getElementType(), Context);
305
306 // Otherwise, it must be a record type.
Ted Kremenek6217b802009-07-29 21:53:49 +0000307 const RecordType *RT = Ty->getAs<RecordType>();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000308 if (!RT) return false;
309
310 // Structure types are passed in register if all fields would be
311 // passed in a register.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000312 for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(),
313 e = RT->getDecl()->field_end(); i != e; ++i) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000314 const FieldDecl *FD = *i;
315
316 // Empty fields are ignored.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000317 if (isEmptyField(Context, FD, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000318 continue;
319
320 // Check fields recursively.
321 if (!shouldReturnTypeInRegister(FD->getType(), Context))
322 return false;
323 }
324
325 return true;
326}
327
328ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000329 ASTContext &Context,
330 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000331 if (RetTy->isVoidType()) {
332 return ABIArgInfo::getIgnore();
John McCall183700f2009-09-21 23:43:11 +0000333 } else if (const VectorType *VT = RetTy->getAs<VectorType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000334 // On Darwin, some vectors are returned in registers.
David Chisnall1e4249c2009-08-17 23:08:21 +0000335 if (IsDarwinVectorABI) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000336 uint64_t Size = Context.getTypeSize(RetTy);
337
338 // 128-bit vectors are a special case; they are returned in
339 // registers and we need to make sure to pick a type the LLVM
340 // backend will like.
341 if (Size == 128)
Owen Anderson0032b272009-08-13 21:57:51 +0000342 return ABIArgInfo::getCoerce(llvm::VectorType::get(
343 llvm::Type::getInt64Ty(VMContext), 2));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000344
345 // Always return in register if it fits in a general purpose
346 // register, or if it is 64 bits and has a single element.
347 if ((Size == 8 || Size == 16 || Size == 32) ||
348 (Size == 64 && VT->getNumElements() == 1))
Owen Anderson0032b272009-08-13 21:57:51 +0000349 return ABIArgInfo::getCoerce(llvm::IntegerType::get(VMContext, Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000350
351 return ABIArgInfo::getIndirect(0);
352 }
353
354 return ABIArgInfo::getDirect();
355 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
356 // Structures with flexible arrays are always indirect.
357 if (const RecordType *RT = RetTy->getAsStructureType())
358 if (RT->getDecl()->hasFlexibleArrayMember())
359 return ABIArgInfo::getIndirect(0);
360
David Chisnall1e4249c2009-08-17 23:08:21 +0000361 // If specified, structs and unions are always indirect.
362 if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000363 return ABIArgInfo::getIndirect(0);
364
365 // Classify "single element" structs as their element type.
366 if (const Type *SeltTy = isSingleElementStruct(RetTy, Context)) {
John McCall183700f2009-09-21 23:43:11 +0000367 if (const BuiltinType *BT = SeltTy->getAs<BuiltinType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000368 if (BT->isIntegerType()) {
369 // We need to use the size of the structure, padding
370 // bit-fields can adjust that to be larger than the single
371 // element type.
372 uint64_t Size = Context.getTypeSize(RetTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000373 return ABIArgInfo::getCoerce(
Owen Anderson0032b272009-08-13 21:57:51 +0000374 llvm::IntegerType::get(VMContext, (unsigned) Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000375 } else if (BT->getKind() == BuiltinType::Float) {
376 assert(Context.getTypeSize(RetTy) == Context.getTypeSize(SeltTy) &&
377 "Unexpect single element structure size!");
Owen Anderson0032b272009-08-13 21:57:51 +0000378 return ABIArgInfo::getCoerce(llvm::Type::getFloatTy(VMContext));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000379 } else if (BT->getKind() == BuiltinType::Double) {
380 assert(Context.getTypeSize(RetTy) == Context.getTypeSize(SeltTy) &&
381 "Unexpect single element structure size!");
Owen Anderson0032b272009-08-13 21:57:51 +0000382 return ABIArgInfo::getCoerce(llvm::Type::getDoubleTy(VMContext));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000383 }
384 } else if (SeltTy->isPointerType()) {
385 // FIXME: It would be really nice if this could come out as the proper
386 // pointer type.
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +0000387 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000388 return ABIArgInfo::getCoerce(PtrTy);
389 } else if (SeltTy->isVectorType()) {
390 // 64- and 128-bit vectors are never returned in a
391 // register when inside a structure.
392 uint64_t Size = Context.getTypeSize(RetTy);
393 if (Size == 64 || Size == 128)
394 return ABIArgInfo::getIndirect(0);
395
Owen Andersona1cf15f2009-07-14 23:10:40 +0000396 return classifyReturnType(QualType(SeltTy, 0), Context, VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000397 }
398 }
399
400 // Small structures which are register sized are generally returned
401 // in a register.
402 if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, Context)) {
403 uint64_t Size = Context.getTypeSize(RetTy);
Owen Anderson0032b272009-08-13 21:57:51 +0000404 return ABIArgInfo::getCoerce(llvm::IntegerType::get(VMContext, Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000405 }
406
407 return ABIArgInfo::getIndirect(0);
408 } else {
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000409 return (RetTy->isPromotableIntegerType() ?
410 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000411 }
412}
413
Eli Friedmana1e6de92009-06-13 21:37:10 +0000414unsigned X86_32ABIInfo::getIndirectArgumentAlignment(QualType Ty,
415 ASTContext &Context) {
416 unsigned Align = Context.getTypeAlign(Ty);
417 if (Align < 128) return 0;
Ted Kremenek6217b802009-07-29 21:53:49 +0000418 if (const RecordType* RT = Ty->getAs<RecordType>())
Eli Friedmana1e6de92009-06-13 21:37:10 +0000419 if (typeContainsSSEVector(RT->getDecl(), Context))
420 return 16;
421 return 0;
422}
423
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000424ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000425 ASTContext &Context,
426 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000427 // FIXME: Set alignment on indirect arguments.
428 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
429 // Structures with flexible arrays are always indirect.
430 if (const RecordType *RT = Ty->getAsStructureType())
431 if (RT->getDecl()->hasFlexibleArrayMember())
Mike Stump1eb44332009-09-09 15:08:12 +0000432 return ABIArgInfo::getIndirect(getIndirectArgumentAlignment(Ty,
Eli Friedmana1e6de92009-06-13 21:37:10 +0000433 Context));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000434
435 // Ignore empty structs.
Eli Friedmana1e6de92009-06-13 21:37:10 +0000436 if (Ty->isStructureType() && Context.getTypeSize(Ty) == 0)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000437 return ABIArgInfo::getIgnore();
438
439 // Expand structs with size <= 128-bits which consist only of
440 // basic types (int, long long, float, double, xxx*). This is
441 // non-recursive and does not ignore empty fields.
442 if (const RecordType *RT = Ty->getAsStructureType()) {
443 if (Context.getTypeSize(Ty) <= 4*32 &&
444 areAllFields32Or64BitBasicType(RT->getDecl(), Context))
445 return ABIArgInfo::getExpand();
446 }
447
Eli Friedmana1e6de92009-06-13 21:37:10 +0000448 return ABIArgInfo::getIndirect(getIndirectArgumentAlignment(Ty, Context));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000449 } else {
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000450 return (Ty->isPromotableIntegerType() ?
451 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000452 }
453}
454
455llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
456 CodeGenFunction &CGF) const {
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +0000457 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Owen Anderson96e0fc72009-07-29 22:16:19 +0000458 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000459
460 CGBuilderTy &Builder = CGF.Builder;
461 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
462 "ap");
463 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
464 llvm::Type *PTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000465 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000466 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
467
468 uint64_t Offset =
469 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
470 llvm::Value *NextAddr =
Owen Anderson0032b272009-08-13 21:57:51 +0000471 Builder.CreateGEP(Addr, llvm::ConstantInt::get(
472 llvm::Type::getInt32Ty(CGF.getLLVMContext()), Offset),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000473 "ap.next");
474 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
475
476 return AddrTyped;
477}
478
479namespace {
480/// X86_64ABIInfo - The X86_64 ABI information.
481class X86_64ABIInfo : public ABIInfo {
482 enum Class {
483 Integer = 0,
484 SSE,
485 SSEUp,
486 X87,
487 X87Up,
488 ComplexX87,
489 NoClass,
490 Memory
491 };
492
493 /// merge - Implement the X86_64 ABI merging algorithm.
494 ///
495 /// Merge an accumulating classification \arg Accum with a field
496 /// classification \arg Field.
497 ///
498 /// \param Accum - The accumulating classification. This should
499 /// always be either NoClass or the result of a previous merge
500 /// call. In addition, this should never be Memory (the caller
501 /// should just return Memory for the aggregate).
502 Class merge(Class Accum, Class Field) const;
503
504 /// classify - Determine the x86_64 register classes in which the
505 /// given type T should be passed.
506 ///
507 /// \param Lo - The classification for the parts of the type
508 /// residing in the low word of the containing object.
509 ///
510 /// \param Hi - The classification for the parts of the type
511 /// residing in the high word of the containing object.
512 ///
513 /// \param OffsetBase - The bit offset of this type in the
514 /// containing object. Some parameters are classified different
515 /// depending on whether they straddle an eightbyte boundary.
516 ///
517 /// If a word is unused its result will be NoClass; if a type should
518 /// be passed in Memory then at least the classification of \arg Lo
519 /// will be Memory.
520 ///
521 /// The \arg Lo class will be NoClass iff the argument is ignored.
522 ///
523 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
524 /// also be ComplexX87.
525 void classify(QualType T, ASTContext &Context, uint64_t OffsetBase,
526 Class &Lo, Class &Hi) const;
527
528 /// getCoerceResult - Given a source type \arg Ty and an LLVM type
529 /// to coerce to, chose the best way to pass Ty in the same place
530 /// that \arg CoerceTo would be passed, but while keeping the
531 /// emitted code as simple as possible.
532 ///
533 /// FIXME: Note, this should be cleaned up to just take an enumeration of all
534 /// the ways we might want to pass things, instead of constructing an LLVM
535 /// type. This makes this code more explicit, and it makes it clearer that we
536 /// are also doing this for correctness in the case of passing scalar types.
537 ABIArgInfo getCoerceResult(QualType Ty,
538 const llvm::Type *CoerceTo,
539 ASTContext &Context) const;
540
541 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
542 /// such that the argument will be passed in memory.
543 ABIArgInfo getIndirectResult(QualType Ty,
544 ASTContext &Context) const;
545
546 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000547 ASTContext &Context,
548 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000549
550 ABIArgInfo classifyArgumentType(QualType Ty,
551 ASTContext &Context,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000552 llvm::LLVMContext &VMContext,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000553 unsigned &neededInt,
554 unsigned &neededSSE) const;
555
556public:
Owen Andersona1cf15f2009-07-14 23:10:40 +0000557 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
558 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000559
560 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
561 CodeGenFunction &CGF) const;
562};
563}
564
565X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum,
566 Class Field) const {
567 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
568 // classified recursively so that always two fields are
569 // considered. The resulting class is calculated according to
570 // the classes of the fields in the eightbyte:
571 //
572 // (a) If both classes are equal, this is the resulting class.
573 //
574 // (b) If one of the classes is NO_CLASS, the resulting class is
575 // the other class.
576 //
577 // (c) If one of the classes is MEMORY, the result is the MEMORY
578 // class.
579 //
580 // (d) If one of the classes is INTEGER, the result is the
581 // INTEGER.
582 //
583 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
584 // MEMORY is used as class.
585 //
586 // (f) Otherwise class SSE is used.
587
588 // Accum should never be memory (we should have returned) or
589 // ComplexX87 (because this cannot be passed in a structure).
590 assert((Accum != Memory && Accum != ComplexX87) &&
591 "Invalid accumulated classification during merge.");
592 if (Accum == Field || Field == NoClass)
593 return Accum;
594 else if (Field == Memory)
595 return Memory;
596 else if (Accum == NoClass)
597 return Field;
598 else if (Accum == Integer || Field == Integer)
599 return Integer;
600 else if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
601 Accum == X87 || Accum == X87Up)
602 return Memory;
603 else
604 return SSE;
605}
606
607void X86_64ABIInfo::classify(QualType Ty,
608 ASTContext &Context,
609 uint64_t OffsetBase,
610 Class &Lo, Class &Hi) const {
611 // FIXME: This code can be simplified by introducing a simple value class for
612 // Class pairs with appropriate constructor methods for the various
613 // situations.
614
615 // FIXME: Some of the split computations are wrong; unaligned vectors
616 // shouldn't be passed in registers for example, so there is no chance they
617 // can straddle an eightbyte. Verify & simplify.
618
619 Lo = Hi = NoClass;
620
621 Class &Current = OffsetBase < 64 ? Lo : Hi;
622 Current = Memory;
623
John McCall183700f2009-09-21 23:43:11 +0000624 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000625 BuiltinType::Kind k = BT->getKind();
626
627 if (k == BuiltinType::Void) {
628 Current = NoClass;
629 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
630 Lo = Integer;
631 Hi = Integer;
632 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
633 Current = Integer;
634 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
635 Current = SSE;
636 } else if (k == BuiltinType::LongDouble) {
637 Lo = X87;
638 Hi = X87Up;
639 }
640 // FIXME: _Decimal32 and _Decimal64 are SSE.
641 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
John McCall183700f2009-09-21 23:43:11 +0000642 } else if (const EnumType *ET = Ty->getAs<EnumType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000643 // Classify the underlying integer type.
644 classify(ET->getDecl()->getIntegerType(), Context, OffsetBase, Lo, Hi);
645 } else if (Ty->hasPointerRepresentation()) {
646 Current = Integer;
John McCall183700f2009-09-21 23:43:11 +0000647 } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000648 uint64_t Size = Context.getTypeSize(VT);
649 if (Size == 32) {
650 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
651 // float> as integer.
652 Current = Integer;
653
654 // If this type crosses an eightbyte boundary, it should be
655 // split.
656 uint64_t EB_Real = (OffsetBase) / 64;
657 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
658 if (EB_Real != EB_Imag)
659 Hi = Lo;
660 } else if (Size == 64) {
661 // gcc passes <1 x double> in memory. :(
662 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
663 return;
664
665 // gcc passes <1 x long long> as INTEGER.
666 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong))
667 Current = Integer;
668 else
669 Current = SSE;
670
671 // If this type crosses an eightbyte boundary, it should be
672 // split.
673 if (OffsetBase && OffsetBase != 64)
674 Hi = Lo;
675 } else if (Size == 128) {
676 Lo = SSE;
677 Hi = SSEUp;
678 }
John McCall183700f2009-09-21 23:43:11 +0000679 } else if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000680 QualType ET = Context.getCanonicalType(CT->getElementType());
681
682 uint64_t Size = Context.getTypeSize(Ty);
683 if (ET->isIntegralType()) {
684 if (Size <= 64)
685 Current = Integer;
686 else if (Size <= 128)
687 Lo = Hi = Integer;
688 } else if (ET == Context.FloatTy)
689 Current = SSE;
690 else if (ET == Context.DoubleTy)
691 Lo = Hi = SSE;
692 else if (ET == Context.LongDoubleTy)
693 Current = ComplexX87;
694
695 // If this complex type crosses an eightbyte boundary then it
696 // should be split.
697 uint64_t EB_Real = (OffsetBase) / 64;
698 uint64_t EB_Imag = (OffsetBase + Context.getTypeSize(ET)) / 64;
699 if (Hi == NoClass && EB_Real != EB_Imag)
700 Hi = Lo;
701 } else if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
702 // Arrays are treated like structures.
703
704 uint64_t Size = Context.getTypeSize(Ty);
705
706 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
707 // than two eightbytes, ..., it has class MEMORY.
708 if (Size > 128)
709 return;
710
711 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
712 // fields, it has class MEMORY.
713 //
714 // Only need to check alignment of array base.
715 if (OffsetBase % Context.getTypeAlign(AT->getElementType()))
716 return;
717
718 // Otherwise implement simplified merge. We could be smarter about
719 // this, but it isn't worth it and would be harder to verify.
720 Current = NoClass;
721 uint64_t EltSize = Context.getTypeSize(AT->getElementType());
722 uint64_t ArraySize = AT->getSize().getZExtValue();
723 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
724 Class FieldLo, FieldHi;
725 classify(AT->getElementType(), Context, Offset, FieldLo, FieldHi);
726 Lo = merge(Lo, FieldLo);
727 Hi = merge(Hi, FieldHi);
728 if (Lo == Memory || Hi == Memory)
729 break;
730 }
731
732 // Do post merger cleanup (see below). Only case we worry about is Memory.
733 if (Hi == Memory)
734 Lo = Memory;
735 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Ted Kremenek6217b802009-07-29 21:53:49 +0000736 } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000737 uint64_t Size = Context.getTypeSize(Ty);
738
739 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
740 // than two eightbytes, ..., it has class MEMORY.
741 if (Size > 128)
742 return;
743
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000744 // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
745 // copy constructor or a non-trivial destructor, it is passed by invisible
746 // reference.
747 if (hasNonTrivialDestructorOrCopyConstructor(RT))
748 return;
749
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000750 const RecordDecl *RD = RT->getDecl();
751
752 // Assume variable sized types are passed in memory.
753 if (RD->hasFlexibleArrayMember())
754 return;
755
756 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
757
758 // Reset Lo class, this will be recomputed.
759 Current = NoClass;
760 unsigned idx = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000761 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
762 i != e; ++i, ++idx) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000763 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
764 bool BitField = i->isBitField();
765
766 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
767 // fields, it has class MEMORY.
768 //
769 // Note, skip this test for bit-fields, see below.
770 if (!BitField && Offset % Context.getTypeAlign(i->getType())) {
771 Lo = Memory;
772 return;
773 }
774
775 // Classify this field.
776 //
777 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
778 // exceeds a single eightbyte, each is classified
779 // separately. Each eightbyte gets initialized to class
780 // NO_CLASS.
781 Class FieldLo, FieldHi;
782
783 // Bit-fields require special handling, they do not force the
784 // structure to be passed in memory even if unaligned, and
785 // therefore they can straddle an eightbyte.
786 if (BitField) {
787 // Ignore padding bit-fields.
788 if (i->isUnnamedBitfield())
789 continue;
790
791 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
792 uint64_t Size = i->getBitWidth()->EvaluateAsInt(Context).getZExtValue();
793
794 uint64_t EB_Lo = Offset / 64;
795 uint64_t EB_Hi = (Offset + Size - 1) / 64;
796 FieldLo = FieldHi = NoClass;
797 if (EB_Lo) {
798 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
799 FieldLo = NoClass;
800 FieldHi = Integer;
801 } else {
802 FieldLo = Integer;
803 FieldHi = EB_Hi ? Integer : NoClass;
804 }
805 } else
806 classify(i->getType(), Context, Offset, FieldLo, FieldHi);
807 Lo = merge(Lo, FieldLo);
808 Hi = merge(Hi, FieldHi);
809 if (Lo == Memory || Hi == Memory)
810 break;
811 }
812
813 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
814 //
815 // (a) If one of the classes is MEMORY, the whole argument is
816 // passed in memory.
817 //
818 // (b) If SSEUP is not preceeded by SSE, it is converted to SSE.
819
820 // The first of these conditions is guaranteed by how we implement
821 // the merge (just bail).
822 //
823 // The second condition occurs in the case of unions; for example
824 // union { _Complex double; unsigned; }.
825 if (Hi == Memory)
826 Lo = Memory;
827 if (Hi == SSEUp && Lo != SSE)
828 Hi = SSE;
829 }
830}
831
832ABIArgInfo X86_64ABIInfo::getCoerceResult(QualType Ty,
833 const llvm::Type *CoerceTo,
834 ASTContext &Context) const {
Owen Anderson0032b272009-08-13 21:57:51 +0000835 if (CoerceTo == llvm::Type::getInt64Ty(CoerceTo->getContext())) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000836 // Integer and pointer types will end up in a general purpose
837 // register.
Anders Carlsson5b3a2fc2009-09-26 03:56:53 +0000838 if (Ty->isIntegralType() || Ty->hasPointerRepresentation())
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000839 return (Ty->isPromotableIntegerType() ?
840 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Owen Anderson0032b272009-08-13 21:57:51 +0000841 } else if (CoerceTo == llvm::Type::getDoubleTy(CoerceTo->getContext())) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000842 // FIXME: It would probably be better to make CGFunctionInfo only map using
843 // canonical types than to canonize here.
844 QualType CTy = Context.getCanonicalType(Ty);
845
846 // Float and double end up in a single SSE reg.
847 if (CTy == Context.FloatTy || CTy == Context.DoubleTy)
848 return ABIArgInfo::getDirect();
849
850 }
851
852 return ABIArgInfo::getCoerce(CoerceTo);
853}
854
855ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty,
856 ASTContext &Context) const {
857 // If this is a scalar LLVM value then assume LLVM will pass it in the right
858 // place naturally.
859 if (!CodeGenFunction::hasAggregateLLVMType(Ty))
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000860 return (Ty->isPromotableIntegerType() ?
861 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000862
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000863 bool ByVal = !isRecordWithNonTrivialDestructorOrCopyConstructor(Ty);
864
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000865 // FIXME: Set alignment correctly.
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000866 return ABIArgInfo::getIndirect(0, ByVal);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000867}
868
869ABIArgInfo X86_64ABIInfo::classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000870 ASTContext &Context,
871 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000872 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
873 // classification algorithm.
874 X86_64ABIInfo::Class Lo, Hi;
875 classify(RetTy, Context, 0, Lo, Hi);
876
877 // Check some invariants.
878 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
879 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
880 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
881
882 const llvm::Type *ResType = 0;
883 switch (Lo) {
884 case NoClass:
885 return ABIArgInfo::getIgnore();
886
887 case SSEUp:
888 case X87Up:
889 assert(0 && "Invalid classification for lo word.");
890
891 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
892 // hidden argument.
893 case Memory:
894 return getIndirectResult(RetTy, Context);
895
896 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
897 // available register of the sequence %rax, %rdx is used.
898 case Integer:
Owen Anderson0032b272009-08-13 21:57:51 +0000899 ResType = llvm::Type::getInt64Ty(VMContext); break;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000900
901 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
902 // available SSE register of the sequence %xmm0, %xmm1 is used.
903 case SSE:
Owen Anderson0032b272009-08-13 21:57:51 +0000904 ResType = llvm::Type::getDoubleTy(VMContext); break;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000905
906 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
907 // returned on the X87 stack in %st0 as 80-bit x87 number.
908 case X87:
Owen Anderson0032b272009-08-13 21:57:51 +0000909 ResType = llvm::Type::getX86_FP80Ty(VMContext); break;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000910
911 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
912 // part of the value is returned in %st0 and the imaginary part in
913 // %st1.
914 case ComplexX87:
915 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Owen Anderson0032b272009-08-13 21:57:51 +0000916 ResType = llvm::StructType::get(VMContext, llvm::Type::getX86_FP80Ty(VMContext),
917 llvm::Type::getX86_FP80Ty(VMContext),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000918 NULL);
919 break;
920 }
921
922 switch (Hi) {
923 // Memory was handled previously and X87 should
924 // never occur as a hi class.
925 case Memory:
926 case X87:
927 assert(0 && "Invalid classification for hi word.");
928
929 case ComplexX87: // Previously handled.
930 case NoClass: break;
931
932 case Integer:
Owen Anderson47a434f2009-08-05 23:18:46 +0000933 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson0032b272009-08-13 21:57:51 +0000934 llvm::Type::getInt64Ty(VMContext), NULL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000935 break;
936 case SSE:
Owen Anderson47a434f2009-08-05 23:18:46 +0000937 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson0032b272009-08-13 21:57:51 +0000938 llvm::Type::getDoubleTy(VMContext), NULL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000939 break;
940
941 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
942 // is passed in the upper half of the last used SSE register.
943 //
944 // SSEUP should always be preceeded by SSE, just widen.
945 case SSEUp:
946 assert(Lo == SSE && "Unexpected SSEUp classification.");
Owen Anderson0032b272009-08-13 21:57:51 +0000947 ResType = llvm::VectorType::get(llvm::Type::getDoubleTy(VMContext), 2);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000948 break;
949
950 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
951 // returned together with the previous X87 value in %st0.
952 case X87Up:
953 // If X87Up is preceeded by X87, we don't need to do
954 // anything. However, in some cases with unions it may not be
955 // preceeded by X87. In such situations we follow gcc and pass the
956 // extra bits in an SSE reg.
957 if (Lo != X87)
Owen Anderson47a434f2009-08-05 23:18:46 +0000958 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson0032b272009-08-13 21:57:51 +0000959 llvm::Type::getDoubleTy(VMContext), NULL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000960 break;
961 }
962
963 return getCoerceResult(RetTy, ResType, Context);
964}
965
966ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, ASTContext &Context,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000967 llvm::LLVMContext &VMContext,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000968 unsigned &neededInt,
969 unsigned &neededSSE) const {
970 X86_64ABIInfo::Class Lo, Hi;
971 classify(Ty, Context, 0, Lo, Hi);
972
973 // Check some invariants.
974 // FIXME: Enforce these by construction.
975 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
976 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
977 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
978
979 neededInt = 0;
980 neededSSE = 0;
981 const llvm::Type *ResType = 0;
982 switch (Lo) {
983 case NoClass:
984 return ABIArgInfo::getIgnore();
985
986 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
987 // on the stack.
988 case Memory:
989
990 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
991 // COMPLEX_X87, it is passed in memory.
992 case X87:
993 case ComplexX87:
994 return getIndirectResult(Ty, Context);
995
996 case SSEUp:
997 case X87Up:
998 assert(0 && "Invalid classification for lo word.");
999
1000 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
1001 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
1002 // and %r9 is used.
1003 case Integer:
1004 ++neededInt;
Owen Anderson0032b272009-08-13 21:57:51 +00001005 ResType = llvm::Type::getInt64Ty(VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001006 break;
1007
1008 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
1009 // available SSE register is used, the registers are taken in the
1010 // order from %xmm0 to %xmm7.
1011 case SSE:
1012 ++neededSSE;
Owen Anderson0032b272009-08-13 21:57:51 +00001013 ResType = llvm::Type::getDoubleTy(VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001014 break;
1015 }
1016
1017 switch (Hi) {
1018 // Memory was handled previously, ComplexX87 and X87 should
1019 // never occur as hi classes, and X87Up must be preceed by X87,
1020 // which is passed in memory.
1021 case Memory:
1022 case X87:
1023 case ComplexX87:
1024 assert(0 && "Invalid classification for hi word.");
1025 break;
1026
1027 case NoClass: break;
1028 case Integer:
Owen Anderson47a434f2009-08-05 23:18:46 +00001029 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson0032b272009-08-13 21:57:51 +00001030 llvm::Type::getInt64Ty(VMContext), NULL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001031 ++neededInt;
1032 break;
1033
1034 // X87Up generally doesn't occur here (long double is passed in
1035 // memory), except in situations involving unions.
1036 case X87Up:
1037 case SSE:
Owen Anderson47a434f2009-08-05 23:18:46 +00001038 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson0032b272009-08-13 21:57:51 +00001039 llvm::Type::getDoubleTy(VMContext), NULL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001040 ++neededSSE;
1041 break;
1042
1043 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
1044 // eightbyte is passed in the upper half of the last used SSE
1045 // register.
1046 case SSEUp:
1047 assert(Lo == SSE && "Unexpected SSEUp classification.");
Owen Anderson0032b272009-08-13 21:57:51 +00001048 ResType = llvm::VectorType::get(llvm::Type::getDoubleTy(VMContext), 2);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001049 break;
1050 }
1051
1052 return getCoerceResult(Ty, ResType, Context);
1053}
1054
Owen Andersona1cf15f2009-07-14 23:10:40 +00001055void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1056 llvm::LLVMContext &VMContext) const {
1057 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(),
1058 Context, VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001059
1060 // Keep track of the number of assigned registers.
1061 unsigned freeIntRegs = 6, freeSSERegs = 8;
1062
1063 // If the return value is indirect, then the hidden argument is consuming one
1064 // integer register.
1065 if (FI.getReturnInfo().isIndirect())
1066 --freeIntRegs;
1067
1068 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
1069 // get assigned (in left-to-right order) for passing as follows...
1070 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1071 it != ie; ++it) {
1072 unsigned neededInt, neededSSE;
Mike Stump1eb44332009-09-09 15:08:12 +00001073 it->info = classifyArgumentType(it->type, Context, VMContext,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001074 neededInt, neededSSE);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001075
1076 // AMD64-ABI 3.2.3p3: If there are no registers available for any
1077 // eightbyte of an argument, the whole argument is passed on the
1078 // stack. If registers have already been assigned for some
1079 // eightbytes of such an argument, the assignments get reverted.
1080 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
1081 freeIntRegs -= neededInt;
1082 freeSSERegs -= neededSSE;
1083 } else {
1084 it->info = getIndirectResult(it->type, Context);
1085 }
1086 }
1087}
1088
1089static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
1090 QualType Ty,
1091 CodeGenFunction &CGF) {
1092 llvm::Value *overflow_arg_area_p =
1093 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
1094 llvm::Value *overflow_arg_area =
1095 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
1096
1097 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
1098 // byte boundary if alignment needed by type exceeds 8 byte boundary.
1099 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
1100 if (Align > 8) {
1101 // Note that we follow the ABI & gcc here, even though the type
1102 // could in theory have an alignment greater than 16. This case
1103 // shouldn't ever matter in practice.
1104
1105 // overflow_arg_area = (overflow_arg_area + 15) & ~15;
Owen Anderson0032b272009-08-13 21:57:51 +00001106 llvm::Value *Offset =
1107 llvm::ConstantInt::get(llvm::Type::getInt32Ty(CGF.getLLVMContext()), 15);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001108 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
1109 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
Owen Anderson0032b272009-08-13 21:57:51 +00001110 llvm::Type::getInt64Ty(CGF.getLLVMContext()));
1111 llvm::Value *Mask = llvm::ConstantInt::get(
1112 llvm::Type::getInt64Ty(CGF.getLLVMContext()), ~15LL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001113 overflow_arg_area =
1114 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
1115 overflow_arg_area->getType(),
1116 "overflow_arg_area.align");
1117 }
1118
1119 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
1120 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1121 llvm::Value *Res =
1122 CGF.Builder.CreateBitCast(overflow_arg_area,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001123 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001124
1125 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
1126 // l->overflow_arg_area + sizeof(type).
1127 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
1128 // an 8 byte boundary.
1129
1130 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
Owen Anderson0032b272009-08-13 21:57:51 +00001131 llvm::Value *Offset =
1132 llvm::ConstantInt::get(llvm::Type::getInt32Ty(CGF.getLLVMContext()),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001133 (SizeInBytes + 7) & ~7);
1134 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
1135 "overflow_arg_area.next");
1136 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
1137
1138 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
1139 return Res;
1140}
1141
1142llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1143 CodeGenFunction &CGF) const {
Owen Andersona1cf15f2009-07-14 23:10:40 +00001144 llvm::LLVMContext &VMContext = CGF.getLLVMContext();
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001145 const llvm::Type *i32Ty = llvm::Type::getInt32Ty(VMContext);
1146 const llvm::Type *DoubleTy = llvm::Type::getDoubleTy(VMContext);
Mike Stump1eb44332009-09-09 15:08:12 +00001147
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001148 // Assume that va_list type is correct; should be pointer to LLVM type:
1149 // struct {
1150 // i32 gp_offset;
1151 // i32 fp_offset;
1152 // i8* overflow_arg_area;
1153 // i8* reg_save_area;
1154 // };
1155 unsigned neededInt, neededSSE;
Owen Andersona1cf15f2009-07-14 23:10:40 +00001156 ABIArgInfo AI = classifyArgumentType(Ty, CGF.getContext(), VMContext,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001157 neededInt, neededSSE);
1158
1159 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
1160 // in the registers. If not go to step 7.
1161 if (!neededInt && !neededSSE)
1162 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1163
1164 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
1165 // general purpose registers needed to pass type and num_fp to hold
1166 // the number of floating point registers needed.
1167
1168 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
1169 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
1170 // l->fp_offset > 304 - num_fp * 16 go to step 7.
1171 //
1172 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
1173 // register save space).
1174
1175 llvm::Value *InRegs = 0;
1176 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
1177 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
1178 if (neededInt) {
1179 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
1180 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
1181 InRegs =
1182 CGF.Builder.CreateICmpULE(gp_offset,
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001183 llvm::ConstantInt::get(i32Ty,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001184 48 - neededInt * 8),
1185 "fits_in_gp");
1186 }
1187
1188 if (neededSSE) {
1189 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
1190 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
1191 llvm::Value *FitsInFP =
1192 CGF.Builder.CreateICmpULE(fp_offset,
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001193 llvm::ConstantInt::get(i32Ty,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001194 176 - neededSSE * 16),
1195 "fits_in_fp");
1196 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
1197 }
1198
1199 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
1200 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
1201 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
1202 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
1203
1204 // Emit code to load the value if it was passed in registers.
1205
1206 CGF.EmitBlock(InRegBlock);
1207
1208 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
1209 // an offset of l->gp_offset and/or l->fp_offset. This may require
1210 // copying to a temporary location in case the parameter is passed
1211 // in different register classes or requires an alignment greater
1212 // than 8 for general purpose registers and 16 for XMM registers.
1213 //
1214 // FIXME: This really results in shameful code when we end up needing to
1215 // collect arguments from different places; often what should result in a
1216 // simple assembling of a structure from scattered addresses has many more
1217 // loads than necessary. Can we clean this up?
1218 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1219 llvm::Value *RegAddr =
1220 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
1221 "reg_save_area");
1222 if (neededInt && neededSSE) {
1223 // FIXME: Cleanup.
1224 assert(AI.isCoerce() && "Unexpected ABI info for mixed regs");
1225 const llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
1226 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
1227 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
1228 const llvm::Type *TyLo = ST->getElementType(0);
1229 const llvm::Type *TyHi = ST->getElementType(1);
1230 assert((TyLo->isFloatingPoint() ^ TyHi->isFloatingPoint()) &&
1231 "Unexpected ABI info for mixed regs");
Owen Anderson96e0fc72009-07-29 22:16:19 +00001232 const llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
1233 const llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001234 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1235 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1236 llvm::Value *RegLoAddr = TyLo->isFloatingPoint() ? FPAddr : GPAddr;
1237 llvm::Value *RegHiAddr = TyLo->isFloatingPoint() ? GPAddr : FPAddr;
1238 llvm::Value *V =
1239 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
1240 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1241 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
1242 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1243
Owen Andersona1cf15f2009-07-14 23:10:40 +00001244 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001245 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001246 } else if (neededInt) {
1247 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1248 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001249 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001250 } else {
1251 if (neededSSE == 1) {
1252 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1253 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001254 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001255 } else {
1256 assert(neededSSE == 2 && "Invalid number of needed registers!");
1257 // SSE registers are spaced 16 bytes apart in the register save
1258 // area, we need to collect the two eightbytes together.
1259 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1260 llvm::Value *RegAddrHi =
1261 CGF.Builder.CreateGEP(RegAddrLo,
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001262 llvm::ConstantInt::get(i32Ty, 16));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001263 const llvm::Type *DblPtrTy =
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001264 llvm::PointerType::getUnqual(DoubleTy);
1265 const llvm::StructType *ST = llvm::StructType::get(VMContext, DoubleTy,
1266 DoubleTy, NULL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001267 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
1268 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
1269 DblPtrTy));
1270 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1271 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
1272 DblPtrTy));
1273 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1274 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001275 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001276 }
1277 }
1278
1279 // AMD64-ABI 3.5.7p5: Step 5. Set:
1280 // l->gp_offset = l->gp_offset + num_gp * 8
1281 // l->fp_offset = l->fp_offset + num_fp * 16.
1282 if (neededInt) {
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001283 llvm::Value *Offset = llvm::ConstantInt::get(i32Ty, neededInt * 8);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001284 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
1285 gp_offset_p);
1286 }
1287 if (neededSSE) {
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001288 llvm::Value *Offset = llvm::ConstantInt::get(i32Ty, neededSSE * 16);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001289 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
1290 fp_offset_p);
1291 }
1292 CGF.EmitBranch(ContBlock);
1293
1294 // Emit code to load the value if it was passed in memory.
1295
1296 CGF.EmitBlock(InMemBlock);
1297 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1298
1299 // Return the appropriate result.
1300
1301 CGF.EmitBlock(ContBlock);
1302 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(),
1303 "vaarg.addr");
1304 ResAddr->reserveOperandSpace(2);
1305 ResAddr->addIncoming(RegAddr, InRegBlock);
1306 ResAddr->addIncoming(MemAddr, InMemBlock);
1307
1308 return ResAddr;
1309}
1310
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001311// PIC16 ABI Implementation
1312
1313namespace {
1314
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001315class PIC16ABIInfo : public ABIInfo {
1316 ABIArgInfo classifyReturnType(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
1320 ABIArgInfo classifyArgumentType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001321 ASTContext &Context,
1322 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001323
Owen Andersona1cf15f2009-07-14 23:10:40 +00001324 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1325 llvm::LLVMContext &VMContext) const {
1326 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context,
1327 VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001328 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1329 it != ie; ++it)
Owen Andersona1cf15f2009-07-14 23:10:40 +00001330 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001331 }
1332
1333 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1334 CodeGenFunction &CGF) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001335};
1336
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001337}
1338
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001339ABIArgInfo PIC16ABIInfo::classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001340 ASTContext &Context,
1341 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001342 if (RetTy->isVoidType()) {
1343 return ABIArgInfo::getIgnore();
1344 } else {
1345 return ABIArgInfo::getDirect();
1346 }
1347}
1348
1349ABIArgInfo PIC16ABIInfo::classifyArgumentType(QualType Ty,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001350 ASTContext &Context,
1351 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001352 return ABIArgInfo::getDirect();
1353}
1354
1355llvm::Value *PIC16ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1356 CodeGenFunction &CGF) const {
1357 return 0;
1358}
1359
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001360// ARM ABI Implementation
1361
1362namespace {
1363
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001364class ARMABIInfo : public ABIInfo {
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00001365public:
1366 enum ABIKind {
1367 APCS = 0,
1368 AAPCS = 1,
1369 AAPCS_VFP
1370 };
1371
1372private:
1373 ABIKind Kind;
1374
1375public:
1376 ARMABIInfo(ABIKind _Kind) : Kind(_Kind) {}
1377
1378private:
1379 ABIKind getABIKind() const { return Kind; }
1380
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001381 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001382 ASTContext &Context,
1383 llvm::LLVMContext &VMCOntext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001384
1385 ABIArgInfo classifyArgumentType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001386 ASTContext &Context,
1387 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001388
Owen Andersona1cf15f2009-07-14 23:10:40 +00001389 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1390 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001391
1392 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1393 CodeGenFunction &CGF) const;
1394};
1395
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001396}
1397
Owen Andersona1cf15f2009-07-14 23:10:40 +00001398void ARMABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1399 llvm::LLVMContext &VMContext) const {
Mike Stump1eb44332009-09-09 15:08:12 +00001400 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001401 VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001402 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1403 it != ie; ++it) {
Owen Andersona1cf15f2009-07-14 23:10:40 +00001404 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001405 }
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00001406
1407 // ARM always overrides the calling convention.
1408 switch (getABIKind()) {
1409 case APCS:
1410 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_APCS);
1411 break;
1412
1413 case AAPCS:
1414 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS);
1415 break;
1416
1417 case AAPCS_VFP:
1418 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS_VFP);
1419 break;
1420 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001421}
1422
1423ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001424 ASTContext &Context,
1425 llvm::LLVMContext &VMContext) const {
Daniel Dunbar98303b92009-09-13 08:03:58 +00001426 if (!CodeGenFunction::hasAggregateLLVMType(Ty))
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001427 return (Ty->isPromotableIntegerType() ?
1428 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Daniel Dunbar98303b92009-09-13 08:03:58 +00001429
Daniel Dunbar42025572009-09-14 21:54:03 +00001430 // Ignore empty records.
1431 if (isEmptyRecord(Context, Ty, true))
1432 return ABIArgInfo::getIgnore();
1433
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001434 // FIXME: This is kind of nasty... but there isn't much choice because the ARM
1435 // backend doesn't support byval.
1436 // FIXME: This doesn't handle alignment > 64 bits.
1437 const llvm::Type* ElemTy;
1438 unsigned SizeRegs;
1439 if (Context.getTypeAlign(Ty) > 32) {
Owen Anderson0032b272009-08-13 21:57:51 +00001440 ElemTy = llvm::Type::getInt64Ty(VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001441 SizeRegs = (Context.getTypeSize(Ty) + 63) / 64;
1442 } else {
Owen Anderson0032b272009-08-13 21:57:51 +00001443 ElemTy = llvm::Type::getInt32Ty(VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001444 SizeRegs = (Context.getTypeSize(Ty) + 31) / 32;
1445 }
1446 std::vector<const llvm::Type*> LLVMFields;
Owen Anderson96e0fc72009-07-29 22:16:19 +00001447 LLVMFields.push_back(llvm::ArrayType::get(ElemTy, SizeRegs));
Owen Anderson47a434f2009-08-05 23:18:46 +00001448 const llvm::Type* STy = llvm::StructType::get(VMContext, LLVMFields, true);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001449 return ABIArgInfo::getCoerce(STy);
1450}
1451
Daniel Dunbar98303b92009-09-13 08:03:58 +00001452static bool isIntegerLikeType(QualType Ty,
1453 ASTContext &Context,
1454 llvm::LLVMContext &VMContext) {
1455 // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
1456 // is called integer-like if its size is less than or equal to one word, and
1457 // the offset of each of its addressable sub-fields is zero.
1458
1459 uint64_t Size = Context.getTypeSize(Ty);
1460
1461 // Check that the type fits in a word.
1462 if (Size > 32)
1463 return false;
1464
1465 // FIXME: Handle vector types!
1466 if (Ty->isVectorType())
1467 return false;
1468
Daniel Dunbarb0d58192009-09-14 02:20:34 +00001469 // Float types are never treated as "integer like".
1470 if (Ty->isRealFloatingType())
1471 return false;
1472
Daniel Dunbar98303b92009-09-13 08:03:58 +00001473 // If this is a builtin or pointer type then it is ok.
John McCall183700f2009-09-21 23:43:11 +00001474 if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
Daniel Dunbar98303b92009-09-13 08:03:58 +00001475 return true;
1476
1477 // Complex types "should" be ok by the definition above, but they are not.
1478 if (Ty->isAnyComplexType())
1479 return false;
1480
1481 // Single element and zero sized arrays should be allowed, by the definition
1482 // above, but they are not.
1483
1484 // Otherwise, it must be a record type.
1485 const RecordType *RT = Ty->getAs<RecordType>();
1486 if (!RT) return false;
1487
1488 // Ignore records with flexible arrays.
1489 const RecordDecl *RD = RT->getDecl();
1490 if (RD->hasFlexibleArrayMember())
1491 return false;
1492
1493 // Check that all sub-fields are at offset 0, and are themselves "integer
1494 // like".
1495 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
1496
1497 bool HadField = false;
1498 unsigned idx = 0;
1499 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1500 i != e; ++i, ++idx) {
1501 const FieldDecl *FD = *i;
1502
1503 // Check if this field is at offset 0.
1504 uint64_t Offset = Layout.getFieldOffset(idx);
1505 if (Offset != 0) {
1506 // Allow padding bit-fields, but only if they are all at the end of the
1507 // structure (despite the wording above, this matches gcc).
1508 if (FD->isBitField() &&
1509 !FD->getBitWidth()->EvaluateAsInt(Context).getZExtValue()) {
1510 for (; i != e; ++i)
1511 if (!i->isBitField() ||
1512 i->getBitWidth()->EvaluateAsInt(Context).getZExtValue())
1513 return false;
1514
1515 // All remaining fields are padding, allow this.
1516 return true;
1517 }
1518
1519 return false;
1520 }
1521
1522 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
1523 return false;
1524
1525 // Only allow at most one field in a structure. Again this doesn't match the
1526 // wording above, but follows gcc.
1527 if (!RD->isUnion()) {
1528 if (HadField)
1529 return false;
1530
1531 HadField = true;
1532 }
1533 }
1534
1535 return true;
1536}
1537
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001538ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001539 ASTContext &Context,
1540 llvm::LLVMContext &VMContext) const {
Daniel Dunbar98303b92009-09-13 08:03:58 +00001541 if (RetTy->isVoidType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001542 return ABIArgInfo::getIgnore();
Daniel Dunbar98303b92009-09-13 08:03:58 +00001543
1544 if (!CodeGenFunction::hasAggregateLLVMType(RetTy))
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001545 return (RetTy->isPromotableIntegerType() ?
1546 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Daniel Dunbar98303b92009-09-13 08:03:58 +00001547
1548 // Are we following APCS?
1549 if (getABIKind() == APCS) {
1550 if (isEmptyRecord(Context, RetTy, false))
1551 return ABIArgInfo::getIgnore();
1552
1553 // Integer like structures are returned in r0.
1554 if (isIntegerLikeType(RetTy, Context, VMContext)) {
1555 // Return in the smallest viable integer type.
1556 uint64_t Size = Context.getTypeSize(RetTy);
1557 if (Size <= 8)
1558 return ABIArgInfo::getCoerce(llvm::Type::getInt8Ty(VMContext));
1559 if (Size <= 16)
1560 return ABIArgInfo::getCoerce(llvm::Type::getInt16Ty(VMContext));
1561 return ABIArgInfo::getCoerce(llvm::Type::getInt32Ty(VMContext));
1562 }
1563
1564 // Otherwise return in memory.
1565 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001566 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00001567
1568 // Otherwise this is an AAPCS variant.
1569
Daniel Dunbar16a08082009-09-14 00:56:55 +00001570 if (isEmptyRecord(Context, RetTy, true))
1571 return ABIArgInfo::getIgnore();
1572
Daniel Dunbar98303b92009-09-13 08:03:58 +00001573 // Aggregates <= 4 bytes are returned in r0; other aggregates
1574 // are returned indirectly.
1575 uint64_t Size = Context.getTypeSize(RetTy);
Daniel Dunbar16a08082009-09-14 00:56:55 +00001576 if (Size <= 32) {
1577 // Return in the smallest viable integer type.
1578 if (Size <= 8)
1579 return ABIArgInfo::getCoerce(llvm::Type::getInt8Ty(VMContext));
1580 if (Size <= 16)
1581 return ABIArgInfo::getCoerce(llvm::Type::getInt16Ty(VMContext));
Daniel Dunbar98303b92009-09-13 08:03:58 +00001582 return ABIArgInfo::getCoerce(llvm::Type::getInt32Ty(VMContext));
Daniel Dunbar16a08082009-09-14 00:56:55 +00001583 }
1584
Daniel Dunbar98303b92009-09-13 08:03:58 +00001585 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001586}
1587
1588llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1589 CodeGenFunction &CGF) const {
1590 // FIXME: Need to handle alignment
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00001591 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Owen Anderson96e0fc72009-07-29 22:16:19 +00001592 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001593
1594 CGBuilderTy &Builder = CGF.Builder;
1595 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
1596 "ap");
1597 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
1598 llvm::Type *PTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00001599 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001600 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
1601
1602 uint64_t Offset =
1603 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
1604 llvm::Value *NextAddr =
Owen Anderson0032b272009-08-13 21:57:51 +00001605 Builder.CreateGEP(Addr, llvm::ConstantInt::get(
1606 llvm::Type::getInt32Ty(CGF.getLLVMContext()), Offset),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001607 "ap.next");
1608 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
1609
1610 return AddrTyped;
1611}
1612
1613ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001614 ASTContext &Context,
1615 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001616 if (RetTy->isVoidType()) {
1617 return ABIArgInfo::getIgnore();
1618 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1619 return ABIArgInfo::getIndirect(0);
1620 } else {
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001621 return (RetTy->isPromotableIntegerType() ?
1622 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001623 }
1624}
1625
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001626// SystemZ ABI Implementation
1627
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00001628namespace {
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001629
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00001630class SystemZABIInfo : public ABIInfo {
1631 bool isPromotableIntegerType(QualType Ty) const;
1632
1633 ABIArgInfo classifyReturnType(QualType RetTy, ASTContext &Context,
1634 llvm::LLVMContext &VMContext) const;
1635
1636 ABIArgInfo classifyArgumentType(QualType RetTy, ASTContext &Context,
1637 llvm::LLVMContext &VMContext) const;
1638
1639 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1640 llvm::LLVMContext &VMContext) const {
1641 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(),
1642 Context, VMContext);
1643 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1644 it != ie; ++it)
1645 it->info = classifyArgumentType(it->type, Context, VMContext);
1646 }
1647
1648 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1649 CodeGenFunction &CGF) const;
1650};
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001651
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00001652}
1653
1654bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const {
1655 // SystemZ ABI requires all 8, 16 and 32 bit quantities to be extended.
John McCall183700f2009-09-21 23:43:11 +00001656 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00001657 switch (BT->getKind()) {
1658 case BuiltinType::Bool:
1659 case BuiltinType::Char_S:
1660 case BuiltinType::Char_U:
1661 case BuiltinType::SChar:
1662 case BuiltinType::UChar:
1663 case BuiltinType::Short:
1664 case BuiltinType::UShort:
1665 case BuiltinType::Int:
1666 case BuiltinType::UInt:
1667 return true;
1668 default:
1669 return false;
1670 }
1671 return false;
1672}
1673
1674llvm::Value *SystemZABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1675 CodeGenFunction &CGF) const {
1676 // FIXME: Implement
1677 return 0;
1678}
1679
1680
1681ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy,
1682 ASTContext &Context,
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001683 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00001684 if (RetTy->isVoidType()) {
1685 return ABIArgInfo::getIgnore();
1686 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1687 return ABIArgInfo::getIndirect(0);
1688 } else {
1689 return (isPromotableIntegerType(RetTy) ?
1690 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1691 }
1692}
1693
1694ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty,
1695 ASTContext &Context,
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001696 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00001697 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
1698 return ABIArgInfo::getIndirect(0);
1699 } else {
1700 return (isPromotableIntegerType(Ty) ?
1701 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1702 }
1703}
1704
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001705ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001706 ASTContext &Context,
1707 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001708 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
1709 return ABIArgInfo::getIndirect(0);
1710 } else {
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001711 return (Ty->isPromotableIntegerType() ?
1712 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001713 }
1714}
1715
1716llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1717 CodeGenFunction &CGF) const {
1718 return 0;
1719}
1720
1721const ABIInfo &CodeGenTypes::getABIInfo() const {
1722 if (TheABIInfo)
1723 return *TheABIInfo;
1724
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00001725 // For now we just cache the ABIInfo in CodeGenTypes and don't free it.
1726
Daniel Dunbar1752ee42009-08-24 09:10:05 +00001727 const llvm::Triple &Triple(getContext().Target.getTriple());
1728 switch (Triple.getArch()) {
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00001729 default:
1730 return *(TheABIInfo = new DefaultABIInfo);
1731
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001732 case llvm::Triple::arm:
1733 case llvm::Triple::thumb:
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00001734 // FIXME: We want to know the float calling convention as well.
Daniel Dunbar018ba5a2009-09-14 00:35:03 +00001735 if (strcmp(getContext().Target.getABI(), "apcs-gnu") == 0)
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00001736 return *(TheABIInfo = new ARMABIInfo(ARMABIInfo::APCS));
1737
1738 return *(TheABIInfo = new ARMABIInfo(ARMABIInfo::AAPCS));
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001739
1740 case llvm::Triple::pic16:
1741 return *(TheABIInfo = new PIC16ABIInfo());
1742
1743 case llvm::Triple::systemz:
1744 return *(TheABIInfo = new SystemZABIInfo());
1745
Daniel Dunbar1752ee42009-08-24 09:10:05 +00001746 case llvm::Triple::x86:
Daniel Dunbar1752ee42009-08-24 09:10:05 +00001747 switch (Triple.getOS()) {
Edward O'Callaghan7ee68bd2009-10-20 17:22:50 +00001748 case llvm::Triple::Darwin:
Daniel Dunbar2b6c7312009-10-20 18:07:06 +00001749 return *(TheABIInfo = new X86_32ABIInfo(Context, true, true));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00001750 case llvm::Triple::Cygwin:
1751 case llvm::Triple::DragonFly:
1752 case llvm::Triple::MinGW32:
1753 case llvm::Triple::MinGW64:
David Chisnall75c135a2009-09-03 01:48:05 +00001754 case llvm::Triple::FreeBSD:
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00001755 case llvm::Triple::OpenBSD:
1756 return *(TheABIInfo = new X86_32ABIInfo(Context, false, true));
1757
1758 default:
1759 return *(TheABIInfo = new X86_32ABIInfo(Context, false, false));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001760 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001761
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00001762 case llvm::Triple::x86_64:
1763 return *(TheABIInfo = new X86_64ABIInfo());
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00001764 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001765}