blob: 7c7589174783d31625ba7bad8eb1ee94cad5cc91 [file] [log] [blame]
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001//===---- TargetInfo.cpp - Encapsulate target details -----------*- C++ -*-===//
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002//
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
Anton Korobeynikov82d0a412010-01-10 12:58:08 +000015#include "TargetInfo.h"
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000016#include "ABIInfo.h"
17#include "CodeGenFunction.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000018#include "clang/AST/RecordLayout.h"
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000019#include "llvm/Type.h"
Anton Korobeynikov82d0a412010-01-10 12:58:08 +000020#include "llvm/ADT/StringExtras.h"
Daniel Dunbar2c0843f2009-08-24 08:52:16 +000021#include "llvm/ADT/Triple.h"
Daniel Dunbar28df7a52009-12-03 09:13:49 +000022#include "llvm/Support/raw_ostream.h"
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000023using namespace clang;
24using namespace CodeGen;
25
John McCallaeeb7012010-05-27 06:19:26 +000026static void AssignToArrayRange(CodeGen::CGBuilderTy &Builder,
27 llvm::Value *Array,
28 llvm::Value *Value,
29 unsigned FirstIndex,
30 unsigned LastIndex) {
31 // Alternatively, we could emit this as a loop in the source.
32 for (unsigned I = FirstIndex; I <= LastIndex; ++I) {
33 llvm::Value *Cell = Builder.CreateConstInBoundsGEP1_32(Array, I);
34 Builder.CreateStore(Value, Cell);
35 }
36}
37
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000038ABIInfo::~ABIInfo() {}
39
40void ABIArgInfo::dump() const {
Daniel Dunbar28df7a52009-12-03 09:13:49 +000041 llvm::raw_ostream &OS = llvm::errs();
42 OS << "(ABIArgInfo Kind=";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000043 switch (TheKind) {
44 case Direct:
Daniel Dunbar28df7a52009-12-03 09:13:49 +000045 OS << "Direct";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000046 break;
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +000047 case Extend:
Daniel Dunbar28df7a52009-12-03 09:13:49 +000048 OS << "Extend";
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +000049 break;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000050 case Ignore:
Daniel Dunbar28df7a52009-12-03 09:13:49 +000051 OS << "Ignore";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000052 break;
53 case Coerce:
Daniel Dunbar28df7a52009-12-03 09:13:49 +000054 OS << "Coerce Type=";
55 getCoerceToType()->print(OS);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000056 break;
57 case Indirect:
Daniel Dunbardc6d5742010-04-21 19:10:51 +000058 OS << "Indirect Align=" << getIndirectAlign()
59 << " Byal=" << getIndirectByVal();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000060 break;
61 case Expand:
Daniel Dunbar28df7a52009-12-03 09:13:49 +000062 OS << "Expand";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000063 break;
64 }
Daniel Dunbar28df7a52009-12-03 09:13:49 +000065 OS << ")\n";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000066}
67
Anton Korobeynikov82d0a412010-01-10 12:58:08 +000068TargetCodeGenInfo::~TargetCodeGenInfo() { delete Info; }
69
Daniel Dunbar98303b92009-09-13 08:03:58 +000070static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000071
72/// isEmptyField - Return true iff a the field is "empty", that is it
73/// is an unnamed bit-field or an (array of) empty record(s).
Daniel Dunbar98303b92009-09-13 08:03:58 +000074static bool isEmptyField(ASTContext &Context, const FieldDecl *FD,
75 bool AllowArrays) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000076 if (FD->isUnnamedBitfield())
77 return true;
78
79 QualType FT = FD->getType();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000080
Daniel Dunbar98303b92009-09-13 08:03:58 +000081 // Constant arrays of empty records count as empty, strip them off.
82 if (AllowArrays)
83 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT))
84 FT = AT->getElementType();
85
Daniel Dunbar5ea68612010-05-17 16:46:00 +000086 const RecordType *RT = FT->getAs<RecordType>();
87 if (!RT)
88 return false;
89
90 // C++ record fields are never empty, at least in the Itanium ABI.
91 //
92 // FIXME: We should use a predicate for whether this behavior is true in the
93 // current ABI.
94 if (isa<CXXRecordDecl>(RT->getDecl()))
95 return false;
96
Daniel Dunbar98303b92009-09-13 08:03:58 +000097 return isEmptyRecord(Context, FT, AllowArrays);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000098}
99
100/// isEmptyRecord - Return true iff a structure contains only empty
101/// fields. Note that a structure with a flexible array member is not
102/// considered empty.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000103static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000104 const RecordType *RT = T->getAs<RecordType>();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000105 if (!RT)
106 return 0;
107 const RecordDecl *RD = RT->getDecl();
108 if (RD->hasFlexibleArrayMember())
109 return false;
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000110
111 // If this is a C++ record, check the bases first.
112 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
113 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
114 e = CXXRD->bases_end(); i != e; ++i)
115 if (!isEmptyRecord(Context, i->getType(), true))
116 return false;
117
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000118 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
119 i != e; ++i)
Daniel Dunbar98303b92009-09-13 08:03:58 +0000120 if (!isEmptyField(Context, *i, AllowArrays))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000121 return false;
122 return true;
123}
124
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000125/// hasNonTrivialDestructorOrCopyConstructor - Determine if a type has either
126/// a non-trivial destructor or a non-trivial copy constructor.
127static bool hasNonTrivialDestructorOrCopyConstructor(const RecordType *RT) {
128 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
129 if (!RD)
130 return false;
131
132 return !RD->hasTrivialDestructor() || !RD->hasTrivialCopyConstructor();
133}
134
135/// isRecordWithNonTrivialDestructorOrCopyConstructor - Determine if a type is
136/// a record type with either a non-trivial destructor or a non-trivial copy
137/// constructor.
138static bool isRecordWithNonTrivialDestructorOrCopyConstructor(QualType T) {
139 const RecordType *RT = T->getAs<RecordType>();
140 if (!RT)
141 return false;
142
143 return hasNonTrivialDestructorOrCopyConstructor(RT);
144}
145
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000146/// isSingleElementStruct - Determine if a structure is a "single
147/// element struct", i.e. it has exactly one non-empty field or
148/// exactly one field which is itself a single element
149/// struct. Structures with flexible array members are never
150/// considered single element structs.
151///
152/// \return The field declaration for the single non-empty field, if
153/// it exists.
154static const Type *isSingleElementStruct(QualType T, ASTContext &Context) {
155 const RecordType *RT = T->getAsStructureType();
156 if (!RT)
157 return 0;
158
159 const RecordDecl *RD = RT->getDecl();
160 if (RD->hasFlexibleArrayMember())
161 return 0;
162
163 const Type *Found = 0;
Daniel Dunbar9430d5a2010-05-11 21:15:36 +0000164
165 // If this is a C++ record, check the bases first.
166 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
167 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
168 e = CXXRD->bases_end(); i != e; ++i) {
Daniel Dunbar9430d5a2010-05-11 21:15:36 +0000169 // Ignore empty records.
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000170 if (isEmptyRecord(Context, i->getType(), true))
Daniel Dunbar9430d5a2010-05-11 21:15:36 +0000171 continue;
172
173 // If we already found an element then this isn't a single-element struct.
174 if (Found)
175 return 0;
176
177 // If this is non-empty and not a single element struct, the composite
178 // cannot be a single element struct.
179 Found = isSingleElementStruct(i->getType(), Context);
180 if (!Found)
181 return 0;
182 }
183 }
184
185 // Check for single element.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000186 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
187 i != e; ++i) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000188 const FieldDecl *FD = *i;
189 QualType FT = FD->getType();
190
191 // Ignore empty fields.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000192 if (isEmptyField(Context, FD, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000193 continue;
194
195 // If we already found an element then this isn't a single-element
196 // struct.
197 if (Found)
198 return 0;
199
200 // Treat single element arrays as the element.
201 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
202 if (AT->getSize().getZExtValue() != 1)
203 break;
204 FT = AT->getElementType();
205 }
206
207 if (!CodeGenFunction::hasAggregateLLVMType(FT)) {
208 Found = FT.getTypePtr();
209 } else {
210 Found = isSingleElementStruct(FT, Context);
211 if (!Found)
212 return 0;
213 }
214 }
215
216 return Found;
217}
218
219static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
Daniel Dunbara1842d32010-05-14 03:40:53 +0000220 if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() &&
Daniel Dunbar55e59e12009-09-24 05:12:36 +0000221 !Ty->isAnyComplexType() && !Ty->isEnumeralType() &&
222 !Ty->isBlockPointerType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000223 return false;
224
225 uint64_t Size = Context.getTypeSize(Ty);
226 return Size == 32 || Size == 64;
227}
228
Daniel Dunbar53012f42009-11-09 01:33:53 +0000229/// canExpandIndirectArgument - Test whether an argument type which is to be
230/// passed indirectly (on the stack) would have the equivalent layout if it was
231/// expanded into separate arguments. If so, we prefer to do the latter to avoid
232/// inhibiting optimizations.
233///
234// FIXME: This predicate is missing many cases, currently it just follows
235// llvm-gcc (checks that all fields are 32-bit or 64-bit primitive types). We
236// should probably make this smarter, or better yet make the LLVM backend
237// capable of handling it.
238static bool canExpandIndirectArgument(QualType Ty, ASTContext &Context) {
239 // We can only expand structure types.
240 const RecordType *RT = Ty->getAs<RecordType>();
241 if (!RT)
242 return false;
243
244 // We can only expand (C) structures.
245 //
246 // FIXME: This needs to be generalized to handle classes as well.
247 const RecordDecl *RD = RT->getDecl();
248 if (!RD->isStruct() || isa<CXXRecordDecl>(RD))
249 return false;
250
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000251 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
252 i != e; ++i) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000253 const FieldDecl *FD = *i;
254
255 if (!is32Or64BitBasicType(FD->getType(), Context))
256 return false;
257
258 // FIXME: Reject bit-fields wholesale; there are two problems, we don't know
259 // how to expand them yet, and the predicate for telling if a bitfield still
260 // counts as "basic" is more complicated than what we were doing previously.
261 if (FD->isBitField())
262 return false;
263 }
264
265 return true;
266}
267
268namespace {
269/// DefaultABIInfo - The default implementation for ABI specific
270/// details. This implementation provides information which results in
271/// self-consistent and sensible LLVM IR generation, but does not
272/// conform to any particular ABI.
273class DefaultABIInfo : public ABIInfo {
274 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000275 ASTContext &Context,
276 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000277
278 ABIArgInfo classifyArgumentType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000279 ASTContext &Context,
280 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000281
Owen Andersona1cf15f2009-07-14 23:10:40 +0000282 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
283 llvm::LLVMContext &VMContext) const {
284 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context,
285 VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000286 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
287 it != ie; ++it)
Owen Andersona1cf15f2009-07-14 23:10:40 +0000288 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000289 }
290
291 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
292 CodeGenFunction &CGF) const;
293};
294
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000295class DefaultTargetCodeGenInfo : public TargetCodeGenInfo {
296public:
Douglas Gregor568bb2d2010-01-22 15:41:14 +0000297 DefaultTargetCodeGenInfo():TargetCodeGenInfo(new DefaultABIInfo()) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000298};
299
300llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
301 CodeGenFunction &CGF) const {
302 return 0;
303}
304
305ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty,
306 ASTContext &Context,
307 llvm::LLVMContext &VMContext) const {
Chris Lattnera14db752010-03-11 18:19:55 +0000308 if (CodeGenFunction::hasAggregateLLVMType(Ty))
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000309 return ABIArgInfo::getIndirect(0);
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000310
Chris Lattnera14db752010-03-11 18:19:55 +0000311 // Treat an enum type as its underlying type.
312 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
313 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregoraa74a1e2010-02-02 20:10:50 +0000314
Chris Lattnera14db752010-03-11 18:19:55 +0000315 return (Ty->isPromotableIntegerType() ?
316 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000317}
318
Chris Lattnerdce5ad02010-06-28 20:05:43 +0000319//===----------------------------------------------------------------------===//
320// X86-32 ABI Implementation
321//===----------------------------------------------------------------------===//
322
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000323/// X86_32ABIInfo - The X86-32 ABI information.
324class X86_32ABIInfo : public ABIInfo {
325 ASTContext &Context;
David Chisnall1e4249c2009-08-17 23:08:21 +0000326 bool IsDarwinVectorABI;
327 bool IsSmallStructInRegABI;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000328
329 static bool isRegisterSize(unsigned Size) {
330 return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
331 }
332
333 static bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context);
334
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000335 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
336 /// such that the argument will be passed in memory.
337 ABIArgInfo getIndirectResult(QualType Ty, ASTContext &Context,
338 bool ByVal = true) const;
339
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000340public:
341 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000342 ASTContext &Context,
343 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000344
345 ABIArgInfo classifyArgumentType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000346 ASTContext &Context,
347 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000348
Owen Andersona1cf15f2009-07-14 23:10:40 +0000349 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
350 llvm::LLVMContext &VMContext) const {
351 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context,
352 VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000353 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
354 it != ie; ++it)
Owen Andersona1cf15f2009-07-14 23:10:40 +0000355 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000356 }
357
358 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
359 CodeGenFunction &CGF) const;
360
David Chisnall1e4249c2009-08-17 23:08:21 +0000361 X86_32ABIInfo(ASTContext &Context, bool d, bool p)
Mike Stump1eb44332009-09-09 15:08:12 +0000362 : ABIInfo(), Context(Context), IsDarwinVectorABI(d),
David Chisnall1e4249c2009-08-17 23:08:21 +0000363 IsSmallStructInRegABI(p) {}
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000364};
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000365
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000366class X86_32TargetCodeGenInfo : public TargetCodeGenInfo {
367public:
368 X86_32TargetCodeGenInfo(ASTContext &Context, bool d, bool p)
Douglas Gregor568bb2d2010-01-22 15:41:14 +0000369 :TargetCodeGenInfo(new X86_32ABIInfo(Context, d, p)) {}
Charles Davis74f72932010-02-13 15:54:06 +0000370
371 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
372 CodeGen::CodeGenModule &CGM) const;
John McCall6374c332010-03-06 00:35:14 +0000373
374 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
375 // Darwin uses different dwarf register numbers for EH.
376 if (CGM.isTargetDarwin()) return 5;
377
378 return 4;
379 }
380
381 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
382 llvm::Value *Address) const;
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000383};
384
385}
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000386
387/// shouldReturnTypeInRegister - Determine if the given type should be
388/// passed in a register (for the Darwin ABI).
389bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
390 ASTContext &Context) {
391 uint64_t Size = Context.getTypeSize(Ty);
392
393 // Type must be register sized.
394 if (!isRegisterSize(Size))
395 return false;
396
397 if (Ty->isVectorType()) {
398 // 64- and 128- bit vectors inside structures are not returned in
399 // registers.
400 if (Size == 64 || Size == 128)
401 return false;
402
403 return true;
404 }
405
Daniel Dunbar77115232010-05-15 00:00:30 +0000406 // If this is a builtin, pointer, enum, complex type, member pointer, or
407 // member function pointer it is ok.
Daniel Dunbara1842d32010-05-14 03:40:53 +0000408 if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() ||
Daniel Dunbar55e59e12009-09-24 05:12:36 +0000409 Ty->isAnyComplexType() || Ty->isEnumeralType() ||
Daniel Dunbar77115232010-05-15 00:00:30 +0000410 Ty->isBlockPointerType() || Ty->isMemberPointerType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000411 return true;
412
413 // Arrays are treated like records.
414 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
415 return shouldReturnTypeInRegister(AT->getElementType(), Context);
416
417 // Otherwise, it must be a record type.
Ted Kremenek6217b802009-07-29 21:53:49 +0000418 const RecordType *RT = Ty->getAs<RecordType>();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000419 if (!RT) return false;
420
Anders Carlssona8874232010-01-27 03:25:19 +0000421 // FIXME: Traverse bases here too.
422
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000423 // Structure types are passed in register if all fields would be
424 // passed in a register.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000425 for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(),
426 e = RT->getDecl()->field_end(); i != e; ++i) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000427 const FieldDecl *FD = *i;
428
429 // Empty fields are ignored.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000430 if (isEmptyField(Context, FD, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000431 continue;
432
433 // Check fields recursively.
434 if (!shouldReturnTypeInRegister(FD->getType(), Context))
435 return false;
436 }
437
438 return true;
439}
440
441ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000442 ASTContext &Context,
443 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000444 if (RetTy->isVoidType()) {
445 return ABIArgInfo::getIgnore();
John McCall183700f2009-09-21 23:43:11 +0000446 } else if (const VectorType *VT = RetTy->getAs<VectorType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000447 // On Darwin, some vectors are returned in registers.
David Chisnall1e4249c2009-08-17 23:08:21 +0000448 if (IsDarwinVectorABI) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000449 uint64_t Size = Context.getTypeSize(RetTy);
450
451 // 128-bit vectors are a special case; they are returned in
452 // registers and we need to make sure to pick a type the LLVM
453 // backend will like.
454 if (Size == 128)
Owen Anderson0032b272009-08-13 21:57:51 +0000455 return ABIArgInfo::getCoerce(llvm::VectorType::get(
456 llvm::Type::getInt64Ty(VMContext), 2));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000457
458 // Always return in register if it fits in a general purpose
459 // register, or if it is 64 bits and has a single element.
460 if ((Size == 8 || Size == 16 || Size == 32) ||
461 (Size == 64 && VT->getNumElements() == 1))
Owen Anderson0032b272009-08-13 21:57:51 +0000462 return ABIArgInfo::getCoerce(llvm::IntegerType::get(VMContext, Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000463
464 return ABIArgInfo::getIndirect(0);
465 }
466
467 return ABIArgInfo::getDirect();
468 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Anders Carlssona8874232010-01-27 03:25:19 +0000469 if (const RecordType *RT = RetTy->getAs<RecordType>()) {
Anders Carlsson40092972009-10-20 22:07:59 +0000470 // Structures with either a non-trivial destructor or a non-trivial
471 // copy constructor are always indirect.
472 if (hasNonTrivialDestructorOrCopyConstructor(RT))
473 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
474
475 // Structures with flexible arrays are always indirect.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000476 if (RT->getDecl()->hasFlexibleArrayMember())
477 return ABIArgInfo::getIndirect(0);
Anders Carlsson40092972009-10-20 22:07:59 +0000478 }
479
David Chisnall1e4249c2009-08-17 23:08:21 +0000480 // If specified, structs and unions are always indirect.
481 if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000482 return ABIArgInfo::getIndirect(0);
483
484 // Classify "single element" structs as their element type.
485 if (const Type *SeltTy = isSingleElementStruct(RetTy, Context)) {
John McCall183700f2009-09-21 23:43:11 +0000486 if (const BuiltinType *BT = SeltTy->getAs<BuiltinType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000487 if (BT->isIntegerType()) {
488 // We need to use the size of the structure, padding
489 // bit-fields can adjust that to be larger than the single
490 // element type.
491 uint64_t Size = Context.getTypeSize(RetTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000492 return ABIArgInfo::getCoerce(
Owen Anderson0032b272009-08-13 21:57:51 +0000493 llvm::IntegerType::get(VMContext, (unsigned) Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000494 } else if (BT->getKind() == BuiltinType::Float) {
495 assert(Context.getTypeSize(RetTy) == Context.getTypeSize(SeltTy) &&
496 "Unexpect single element structure size!");
Owen Anderson0032b272009-08-13 21:57:51 +0000497 return ABIArgInfo::getCoerce(llvm::Type::getFloatTy(VMContext));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000498 } else if (BT->getKind() == BuiltinType::Double) {
499 assert(Context.getTypeSize(RetTy) == Context.getTypeSize(SeltTy) &&
500 "Unexpect single element structure size!");
Owen Anderson0032b272009-08-13 21:57:51 +0000501 return ABIArgInfo::getCoerce(llvm::Type::getDoubleTy(VMContext));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000502 }
503 } else if (SeltTy->isPointerType()) {
504 // FIXME: It would be really nice if this could come out as the proper
505 // pointer type.
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +0000506 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000507 return ABIArgInfo::getCoerce(PtrTy);
508 } else if (SeltTy->isVectorType()) {
509 // 64- and 128-bit vectors are never returned in a
510 // register when inside a structure.
511 uint64_t Size = Context.getTypeSize(RetTy);
512 if (Size == 64 || Size == 128)
513 return ABIArgInfo::getIndirect(0);
514
Owen Andersona1cf15f2009-07-14 23:10:40 +0000515 return classifyReturnType(QualType(SeltTy, 0), Context, VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000516 }
517 }
518
519 // Small structures which are register sized are generally returned
520 // in a register.
521 if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, Context)) {
522 uint64_t Size = Context.getTypeSize(RetTy);
Owen Anderson0032b272009-08-13 21:57:51 +0000523 return ABIArgInfo::getCoerce(llvm::IntegerType::get(VMContext, Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000524 }
525
526 return ABIArgInfo::getIndirect(0);
527 } else {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +0000528 // Treat an enum type as its underlying type.
529 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
530 RetTy = EnumTy->getDecl()->getIntegerType();
531
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000532 return (RetTy->isPromotableIntegerType() ?
533 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000534 }
535}
536
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000537ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty,
538 ASTContext &Context,
539 bool ByVal) const {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +0000540 if (!ByVal)
541 return ABIArgInfo::getIndirect(0, false);
542
543 // Compute the byval alignment. We trust the back-end to honor the
544 // minimum ABI alignment for byval, to make cleaner IR.
545 const unsigned MinABIAlign = 4;
546 unsigned Align = Context.getTypeAlign(Ty) / 8;
547 if (Align > MinABIAlign)
548 return ABIArgInfo::getIndirect(Align);
549 return ABIArgInfo::getIndirect(0);
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000550}
551
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000552ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000553 ASTContext &Context,
554 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000555 // FIXME: Set alignment on indirect arguments.
556 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
557 // Structures with flexible arrays are always indirect.
Anders Carlssona8874232010-01-27 03:25:19 +0000558 if (const RecordType *RT = Ty->getAs<RecordType>()) {
559 // Structures with either a non-trivial destructor or a non-trivial
560 // copy constructor are always indirect.
561 if (hasNonTrivialDestructorOrCopyConstructor(RT))
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000562 return getIndirectResult(Ty, Context, /*ByVal=*/false);
563
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000564 if (RT->getDecl()->hasFlexibleArrayMember())
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000565 return getIndirectResult(Ty, Context);
Anders Carlssona8874232010-01-27 03:25:19 +0000566 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000567
568 // Ignore empty structs.
Eli Friedmana1e6de92009-06-13 21:37:10 +0000569 if (Ty->isStructureType() && Context.getTypeSize(Ty) == 0)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000570 return ABIArgInfo::getIgnore();
571
Daniel Dunbar53012f42009-11-09 01:33:53 +0000572 // Expand small (<= 128-bit) record types when we know that the stack layout
573 // of those arguments will match the struct. This is important because the
574 // LLVM backend isn't smart enough to remove byval, which inhibits many
575 // optimizations.
576 if (Context.getTypeSize(Ty) <= 4*32 &&
577 canExpandIndirectArgument(Ty, Context))
578 return ABIArgInfo::getExpand();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000579
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000580 return getIndirectResult(Ty, Context);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000581 } else {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +0000582 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
583 Ty = EnumTy->getDecl()->getIntegerType();
584
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000585 return (Ty->isPromotableIntegerType() ?
586 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000587 }
588}
589
590llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
591 CodeGenFunction &CGF) const {
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +0000592 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Owen Anderson96e0fc72009-07-29 22:16:19 +0000593 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000594
595 CGBuilderTy &Builder = CGF.Builder;
596 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
597 "ap");
598 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
599 llvm::Type *PTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000600 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000601 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
602
603 uint64_t Offset =
604 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
605 llvm::Value *NextAddr =
Chris Lattner77b89b82010-06-27 07:15:29 +0000606 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000607 "ap.next");
608 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
609
610 return AddrTyped;
611}
612
Charles Davis74f72932010-02-13 15:54:06 +0000613void X86_32TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
614 llvm::GlobalValue *GV,
615 CodeGen::CodeGenModule &CGM) const {
616 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
617 if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
618 // Get the LLVM function.
619 llvm::Function *Fn = cast<llvm::Function>(GV);
620
621 // Now add the 'alignstack' attribute with a value of 16.
622 Fn->addFnAttr(llvm::Attribute::constructStackAlignmentFromInt(16));
623 }
624 }
625}
626
John McCall6374c332010-03-06 00:35:14 +0000627bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable(
628 CodeGen::CodeGenFunction &CGF,
629 llvm::Value *Address) const {
630 CodeGen::CGBuilderTy &Builder = CGF.Builder;
631 llvm::LLVMContext &Context = CGF.getLLVMContext();
632
633 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
634 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
635
636 // 0-7 are the eight integer registers; the order is different
637 // on Darwin (for EH), but the range is the same.
638 // 8 is %eip.
John McCallaeeb7012010-05-27 06:19:26 +0000639 AssignToArrayRange(Builder, Address, Four8, 0, 8);
John McCall6374c332010-03-06 00:35:14 +0000640
641 if (CGF.CGM.isTargetDarwin()) {
642 // 12-16 are st(0..4). Not sure why we stop at 4.
643 // These have size 16, which is sizeof(long double) on
644 // platforms with 8-byte alignment for that type.
645 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
John McCallaeeb7012010-05-27 06:19:26 +0000646 AssignToArrayRange(Builder, Address, Sixteen8, 12, 16);
John McCall6374c332010-03-06 00:35:14 +0000647
648 } else {
649 // 9 is %eflags, which doesn't get a size on Darwin for some
650 // reason.
651 Builder.CreateStore(Four8, Builder.CreateConstInBoundsGEP1_32(Address, 9));
652
653 // 11-16 are st(0..5). Not sure why we stop at 5.
654 // These have size 12, which is sizeof(long double) on
655 // platforms with 4-byte alignment for that type.
656 llvm::Value *Twelve8 = llvm::ConstantInt::get(i8, 12);
John McCallaeeb7012010-05-27 06:19:26 +0000657 AssignToArrayRange(Builder, Address, Twelve8, 11, 16);
658 }
John McCall6374c332010-03-06 00:35:14 +0000659
660 return false;
661}
662
Chris Lattnerdce5ad02010-06-28 20:05:43 +0000663//===----------------------------------------------------------------------===//
664// X86-64 ABI Implementation
665//===----------------------------------------------------------------------===//
666
667
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000668namespace {
669/// X86_64ABIInfo - The X86_64 ABI information.
670class X86_64ABIInfo : public ABIInfo {
671 enum Class {
672 Integer = 0,
673 SSE,
674 SSEUp,
675 X87,
676 X87Up,
677 ComplexX87,
678 NoClass,
679 Memory
680 };
681
682 /// merge - Implement the X86_64 ABI merging algorithm.
683 ///
684 /// Merge an accumulating classification \arg Accum with a field
685 /// classification \arg Field.
686 ///
687 /// \param Accum - The accumulating classification. This should
688 /// always be either NoClass or the result of a previous merge
689 /// call. In addition, this should never be Memory (the caller
690 /// should just return Memory for the aggregate).
Chris Lattner1090a9b2010-06-28 21:43:59 +0000691 static Class merge(Class Accum, Class Field);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000692
693 /// classify - Determine the x86_64 register classes in which the
694 /// given type T should be passed.
695 ///
696 /// \param Lo - The classification for the parts of the type
697 /// residing in the low word of the containing object.
698 ///
699 /// \param Hi - The classification for the parts of the type
700 /// residing in the high word of the containing object.
701 ///
702 /// \param OffsetBase - The bit offset of this type in the
703 /// containing object. Some parameters are classified different
704 /// depending on whether they straddle an eightbyte boundary.
705 ///
706 /// If a word is unused its result will be NoClass; if a type should
707 /// be passed in Memory then at least the classification of \arg Lo
708 /// will be Memory.
709 ///
710 /// The \arg Lo class will be NoClass iff the argument is ignored.
711 ///
712 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
713 /// also be ComplexX87.
714 void classify(QualType T, ASTContext &Context, uint64_t OffsetBase,
715 Class &Lo, Class &Hi) const;
716
717 /// getCoerceResult - Given a source type \arg Ty and an LLVM type
718 /// to coerce to, chose the best way to pass Ty in the same place
719 /// that \arg CoerceTo would be passed, but while keeping the
720 /// emitted code as simple as possible.
721 ///
722 /// FIXME: Note, this should be cleaned up to just take an enumeration of all
723 /// the ways we might want to pass things, instead of constructing an LLVM
724 /// type. This makes this code more explicit, and it makes it clearer that we
725 /// are also doing this for correctness in the case of passing scalar types.
726 ABIArgInfo getCoerceResult(QualType Ty,
727 const llvm::Type *CoerceTo,
728 ASTContext &Context) const;
729
730 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Daniel Dunbar46c54fb2010-04-21 19:49:55 +0000731 /// such that the argument will be returned in memory.
732 ABIArgInfo getIndirectReturnResult(QualType Ty, ASTContext &Context) const;
733
734 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000735 /// such that the argument will be passed in memory.
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000736 ABIArgInfo getIndirectResult(QualType Ty, ASTContext &Context) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000737
738 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000739 ASTContext &Context,
740 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000741
742 ABIArgInfo classifyArgumentType(QualType Ty,
743 ASTContext &Context,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000744 llvm::LLVMContext &VMContext,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000745 unsigned &neededInt,
746 unsigned &neededSSE) const;
747
748public:
Owen Andersona1cf15f2009-07-14 23:10:40 +0000749 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
750 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000751
752 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
753 CodeGenFunction &CGF) const;
754};
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000755
756class X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
757public:
Douglas Gregor568bb2d2010-01-22 15:41:14 +0000758 X86_64TargetCodeGenInfo():TargetCodeGenInfo(new X86_64ABIInfo()) {}
John McCall6374c332010-03-06 00:35:14 +0000759
760 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
761 return 7;
762 }
763
764 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
765 llvm::Value *Address) const {
766 CodeGen::CGBuilderTy &Builder = CGF.Builder;
767 llvm::LLVMContext &Context = CGF.getLLVMContext();
768
769 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
770 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
771
John McCallaeeb7012010-05-27 06:19:26 +0000772 // 0-15 are the 16 integer registers.
773 // 16 is %rip.
774 AssignToArrayRange(Builder, Address, Eight8, 0, 16);
John McCall6374c332010-03-06 00:35:14 +0000775
776 return false;
777 }
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000778};
779
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000780}
781
Chris Lattner1090a9b2010-06-28 21:43:59 +0000782X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000783 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
784 // classified recursively so that always two fields are
785 // considered. The resulting class is calculated according to
786 // the classes of the fields in the eightbyte:
787 //
788 // (a) If both classes are equal, this is the resulting class.
789 //
790 // (b) If one of the classes is NO_CLASS, the resulting class is
791 // the other class.
792 //
793 // (c) If one of the classes is MEMORY, the result is the MEMORY
794 // class.
795 //
796 // (d) If one of the classes is INTEGER, the result is the
797 // INTEGER.
798 //
799 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
800 // MEMORY is used as class.
801 //
802 // (f) Otherwise class SSE is used.
803
804 // Accum should never be memory (we should have returned) or
805 // ComplexX87 (because this cannot be passed in a structure).
806 assert((Accum != Memory && Accum != ComplexX87) &&
807 "Invalid accumulated classification during merge.");
808 if (Accum == Field || Field == NoClass)
809 return Accum;
Chris Lattner1090a9b2010-06-28 21:43:59 +0000810 if (Field == Memory)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000811 return Memory;
Chris Lattner1090a9b2010-06-28 21:43:59 +0000812 if (Accum == NoClass)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000813 return Field;
Chris Lattner1090a9b2010-06-28 21:43:59 +0000814 if (Accum == Integer || Field == Integer)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000815 return Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +0000816 if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
817 Accum == X87 || Accum == X87Up)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000818 return Memory;
Chris Lattner1090a9b2010-06-28 21:43:59 +0000819 return SSE;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000820}
821
822void X86_64ABIInfo::classify(QualType Ty,
823 ASTContext &Context,
824 uint64_t OffsetBase,
825 Class &Lo, Class &Hi) const {
826 // FIXME: This code can be simplified by introducing a simple value class for
827 // Class pairs with appropriate constructor methods for the various
828 // situations.
829
830 // FIXME: Some of the split computations are wrong; unaligned vectors
831 // shouldn't be passed in registers for example, so there is no chance they
832 // can straddle an eightbyte. Verify & simplify.
833
834 Lo = Hi = NoClass;
835
836 Class &Current = OffsetBase < 64 ? Lo : Hi;
837 Current = Memory;
838
John McCall183700f2009-09-21 23:43:11 +0000839 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000840 BuiltinType::Kind k = BT->getKind();
841
842 if (k == BuiltinType::Void) {
843 Current = NoClass;
844 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
845 Lo = Integer;
846 Hi = Integer;
847 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
848 Current = Integer;
849 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
850 Current = SSE;
851 } else if (k == BuiltinType::LongDouble) {
852 Lo = X87;
853 Hi = X87Up;
854 }
855 // FIXME: _Decimal32 and _Decimal64 are SSE.
856 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Chris Lattner1090a9b2010-06-28 21:43:59 +0000857 return;
858 }
859
860 if (const EnumType *ET = Ty->getAs<EnumType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000861 // Classify the underlying integer type.
862 classify(ET->getDecl()->getIntegerType(), Context, OffsetBase, Lo, Hi);
Chris Lattner1090a9b2010-06-28 21:43:59 +0000863 return;
864 }
865
866 if (Ty->hasPointerRepresentation()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000867 Current = Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +0000868 return;
869 }
870
871 if (Ty->isMemberPointerType()) {
Daniel Dunbar67d438d2010-05-15 00:00:37 +0000872 if (Ty->isMemberFunctionPointerType())
873 Lo = Hi = Integer;
874 else
875 Current = Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +0000876 return;
877 }
878
879 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000880 uint64_t Size = Context.getTypeSize(VT);
881 if (Size == 32) {
882 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
883 // float> as integer.
884 Current = Integer;
885
886 // If this type crosses an eightbyte boundary, it should be
887 // split.
888 uint64_t EB_Real = (OffsetBase) / 64;
889 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
890 if (EB_Real != EB_Imag)
891 Hi = Lo;
892 } else if (Size == 64) {
893 // gcc passes <1 x double> in memory. :(
894 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
895 return;
896
897 // gcc passes <1 x long long> as INTEGER.
898 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong))
899 Current = Integer;
900 else
901 Current = SSE;
902
903 // If this type crosses an eightbyte boundary, it should be
904 // split.
905 if (OffsetBase && OffsetBase != 64)
906 Hi = Lo;
907 } else if (Size == 128) {
908 Lo = SSE;
909 Hi = SSEUp;
910 }
Chris Lattner1090a9b2010-06-28 21:43:59 +0000911 return;
912 }
913
914 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000915 QualType ET = Context.getCanonicalType(CT->getElementType());
916
917 uint64_t Size = Context.getTypeSize(Ty);
Douglas Gregor2ade35e2010-06-16 00:17:44 +0000918 if (ET->isIntegralOrEnumerationType()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000919 if (Size <= 64)
920 Current = Integer;
921 else if (Size <= 128)
922 Lo = Hi = Integer;
923 } else if (ET == Context.FloatTy)
924 Current = SSE;
925 else if (ET == Context.DoubleTy)
926 Lo = Hi = SSE;
927 else if (ET == Context.LongDoubleTy)
928 Current = ComplexX87;
929
930 // If this complex type crosses an eightbyte boundary then it
931 // should be split.
932 uint64_t EB_Real = (OffsetBase) / 64;
933 uint64_t EB_Imag = (OffsetBase + Context.getTypeSize(ET)) / 64;
934 if (Hi == NoClass && EB_Real != EB_Imag)
935 Hi = Lo;
Chris Lattner1090a9b2010-06-28 21:43:59 +0000936
937 return;
938 }
939
940 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000941 // Arrays are treated like structures.
942
943 uint64_t Size = Context.getTypeSize(Ty);
944
945 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
946 // than two eightbytes, ..., it has class MEMORY.
947 if (Size > 128)
948 return;
949
950 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
951 // fields, it has class MEMORY.
952 //
953 // Only need to check alignment of array base.
954 if (OffsetBase % Context.getTypeAlign(AT->getElementType()))
955 return;
956
957 // Otherwise implement simplified merge. We could be smarter about
958 // this, but it isn't worth it and would be harder to verify.
959 Current = NoClass;
960 uint64_t EltSize = Context.getTypeSize(AT->getElementType());
961 uint64_t ArraySize = AT->getSize().getZExtValue();
962 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
963 Class FieldLo, FieldHi;
964 classify(AT->getElementType(), Context, Offset, FieldLo, FieldHi);
965 Lo = merge(Lo, FieldLo);
966 Hi = merge(Hi, FieldHi);
967 if (Lo == Memory || Hi == Memory)
968 break;
969 }
970
971 // Do post merger cleanup (see below). Only case we worry about is Memory.
972 if (Hi == Memory)
973 Lo = Memory;
974 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Chris Lattner1090a9b2010-06-28 21:43:59 +0000975 return;
976 }
977
978 if (const RecordType *RT = Ty->getAs<RecordType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000979 uint64_t Size = Context.getTypeSize(Ty);
980
981 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
982 // than two eightbytes, ..., it has class MEMORY.
983 if (Size > 128)
984 return;
985
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000986 // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
987 // copy constructor or a non-trivial destructor, it is passed by invisible
988 // reference.
989 if (hasNonTrivialDestructorOrCopyConstructor(RT))
990 return;
Daniel Dunbarce9f4232009-11-22 23:01:23 +0000991
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000992 const RecordDecl *RD = RT->getDecl();
993
994 // Assume variable sized types are passed in memory.
995 if (RD->hasFlexibleArrayMember())
996 return;
997
998 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
999
1000 // Reset Lo class, this will be recomputed.
1001 Current = NoClass;
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001002
1003 // If this is a C++ record, classify the bases first.
1004 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1005 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1006 e = CXXRD->bases_end(); i != e; ++i) {
1007 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1008 "Unexpected base class!");
1009 const CXXRecordDecl *Base =
1010 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1011
1012 // Classify this field.
1013 //
1014 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
1015 // single eightbyte, each is classified separately. Each eightbyte gets
1016 // initialized to class NO_CLASS.
1017 Class FieldLo, FieldHi;
1018 uint64_t Offset = OffsetBase + Layout.getBaseClassOffset(Base);
1019 classify(i->getType(), Context, Offset, FieldLo, FieldHi);
1020 Lo = merge(Lo, FieldLo);
1021 Hi = merge(Hi, FieldHi);
1022 if (Lo == Memory || Hi == Memory)
1023 break;
1024 }
Daniel Dunbar4971ff82009-12-22 01:19:25 +00001025
1026 // If this record has no fields but isn't empty, classify as INTEGER.
1027 if (RD->field_empty() && Size)
1028 Current = Integer;
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001029 }
1030
1031 // Classify the fields one at a time, merging the results.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001032 unsigned idx = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001033 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1034 i != e; ++i, ++idx) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001035 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
1036 bool BitField = i->isBitField();
1037
1038 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
1039 // fields, it has class MEMORY.
1040 //
1041 // Note, skip this test for bit-fields, see below.
1042 if (!BitField && Offset % Context.getTypeAlign(i->getType())) {
1043 Lo = Memory;
1044 return;
1045 }
1046
1047 // Classify this field.
1048 //
1049 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
1050 // exceeds a single eightbyte, each is classified
1051 // separately. Each eightbyte gets initialized to class
1052 // NO_CLASS.
1053 Class FieldLo, FieldHi;
1054
1055 // Bit-fields require special handling, they do not force the
1056 // structure to be passed in memory even if unaligned, and
1057 // therefore they can straddle an eightbyte.
1058 if (BitField) {
1059 // Ignore padding bit-fields.
1060 if (i->isUnnamedBitfield())
1061 continue;
1062
1063 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
1064 uint64_t Size = i->getBitWidth()->EvaluateAsInt(Context).getZExtValue();
1065
1066 uint64_t EB_Lo = Offset / 64;
1067 uint64_t EB_Hi = (Offset + Size - 1) / 64;
1068 FieldLo = FieldHi = NoClass;
1069 if (EB_Lo) {
1070 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
1071 FieldLo = NoClass;
1072 FieldHi = Integer;
1073 } else {
1074 FieldLo = Integer;
1075 FieldHi = EB_Hi ? Integer : NoClass;
1076 }
1077 } else
1078 classify(i->getType(), Context, Offset, FieldLo, FieldHi);
1079 Lo = merge(Lo, FieldLo);
1080 Hi = merge(Hi, FieldHi);
1081 if (Lo == Memory || Hi == Memory)
1082 break;
1083 }
1084
1085 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
1086 //
1087 // (a) If one of the classes is MEMORY, the whole argument is
1088 // passed in memory.
1089 //
1090 // (b) If SSEUP is not preceeded by SSE, it is converted to SSE.
1091
1092 // The first of these conditions is guaranteed by how we implement
1093 // the merge (just bail).
1094 //
1095 // The second condition occurs in the case of unions; for example
1096 // union { _Complex double; unsigned; }.
1097 if (Hi == Memory)
1098 Lo = Memory;
1099 if (Hi == SSEUp && Lo != SSE)
1100 Hi = SSE;
1101 }
1102}
1103
1104ABIArgInfo X86_64ABIInfo::getCoerceResult(QualType Ty,
1105 const llvm::Type *CoerceTo,
1106 ASTContext &Context) const {
Chris Lattner7f215c12010-06-26 21:52:32 +00001107 if (CoerceTo->isIntegerTy(64)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001108 // Integer and pointer types will end up in a general purpose
1109 // register.
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001110
1111 // Treat an enum type as its underlying type.
1112 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1113 Ty = EnumTy->getDecl()->getIntegerType();
1114
Douglas Gregor9d3347a2010-06-16 00:35:25 +00001115 if (Ty->isIntegralOrEnumerationType() || Ty->hasPointerRepresentation())
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001116 return (Ty->isPromotableIntegerType() ?
1117 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Chris Lattnerfaf23b72010-06-28 19:56:59 +00001118
1119 // If this is a 32-bit structure that is passed as an int64, then it will be
1120 // passed in the low 32-bits of a 64-bit GPR, which is the same as how an
1121 // i32 is passed. Coerce to a i32 instead of a i64.
1122 if (Context.getTypeSizeInChars(Ty).getQuantity() == 4)
1123 CoerceTo = llvm::Type::getInt32Ty(CoerceTo->getContext());
1124
Chris Lattner7f215c12010-06-26 21:52:32 +00001125 } else if (CoerceTo->isDoubleTy()) {
John McCall0b0ef0a2010-02-24 07:14:12 +00001126 assert(Ty.isCanonical() && "should always have a canonical type here");
1127 assert(!Ty.hasQualifiers() && "should never have a qualified type here");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001128
1129 // Float and double end up in a single SSE reg.
John McCall0b0ef0a2010-02-24 07:14:12 +00001130 if (Ty == Context.FloatTy || Ty == Context.DoubleTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001131 return ABIArgInfo::getDirect();
1132
Chris Lattnerfaf23b72010-06-28 19:56:59 +00001133 // If this is a 32-bit structure that is passed as a double, then it will be
1134 // passed in the low 32-bits of the XMM register, which is the same as how a
1135 // float is passed. Coerce to a float instead of a double.
1136 if (Context.getTypeSizeInChars(Ty).getQuantity() == 4)
1137 CoerceTo = llvm::Type::getFloatTy(CoerceTo->getContext());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001138 }
1139
1140 return ABIArgInfo::getCoerce(CoerceTo);
1141}
1142
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001143ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty,
1144 ASTContext &Context) const {
1145 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1146 // place naturally.
1147 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
1148 // Treat an enum type as its underlying type.
1149 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1150 Ty = EnumTy->getDecl()->getIntegerType();
1151
1152 return (Ty->isPromotableIntegerType() ?
1153 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1154 }
1155
1156 return ABIArgInfo::getIndirect(0);
1157}
1158
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001159ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty,
1160 ASTContext &Context) const {
1161 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1162 // place naturally.
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001163 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
1164 // Treat an enum type as its underlying type.
1165 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1166 Ty = EnumTy->getDecl()->getIntegerType();
1167
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001168 return (Ty->isPromotableIntegerType() ?
1169 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001170 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001171
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001172 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
1173 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Anders Carlsson0a8f8472009-09-16 15:53:40 +00001174
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001175 // Compute the byval alignment. We trust the back-end to honor the
1176 // minimum ABI alignment for byval, to make cleaner IR.
1177 const unsigned MinABIAlign = 8;
1178 unsigned Align = Context.getTypeAlign(Ty) / 8;
1179 if (Align > MinABIAlign)
1180 return ABIArgInfo::getIndirect(Align);
1181 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001182}
1183
Chris Lattner1090a9b2010-06-28 21:43:59 +00001184ABIArgInfo X86_64ABIInfo::
1185classifyReturnType(QualType RetTy, ASTContext &Context,
1186 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001187 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
1188 // classification algorithm.
1189 X86_64ABIInfo::Class Lo, Hi;
1190 classify(RetTy, Context, 0, Lo, Hi);
1191
1192 // Check some invariants.
1193 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
1194 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
1195 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1196
1197 const llvm::Type *ResType = 0;
1198 switch (Lo) {
1199 case NoClass:
1200 return ABIArgInfo::getIgnore();
1201
1202 case SSEUp:
1203 case X87Up:
1204 assert(0 && "Invalid classification for lo word.");
1205
1206 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
1207 // hidden argument.
1208 case Memory:
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001209 return getIndirectReturnResult(RetTy, Context);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001210
1211 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
1212 // available register of the sequence %rax, %rdx is used.
1213 case Integer:
Owen Anderson0032b272009-08-13 21:57:51 +00001214 ResType = llvm::Type::getInt64Ty(VMContext); break;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001215
1216 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
1217 // available SSE register of the sequence %xmm0, %xmm1 is used.
1218 case SSE:
Owen Anderson0032b272009-08-13 21:57:51 +00001219 ResType = llvm::Type::getDoubleTy(VMContext); break;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001220
1221 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
1222 // returned on the X87 stack in %st0 as 80-bit x87 number.
1223 case X87:
Owen Anderson0032b272009-08-13 21:57:51 +00001224 ResType = llvm::Type::getX86_FP80Ty(VMContext); break;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001225
1226 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
1227 // part of the value is returned in %st0 and the imaginary part in
1228 // %st1.
1229 case ComplexX87:
1230 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Chris Lattner52d9ae32010-04-06 17:29:22 +00001231 ResType = llvm::StructType::get(VMContext,
1232 llvm::Type::getX86_FP80Ty(VMContext),
Owen Anderson0032b272009-08-13 21:57:51 +00001233 llvm::Type::getX86_FP80Ty(VMContext),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001234 NULL);
1235 break;
1236 }
1237
1238 switch (Hi) {
1239 // Memory was handled previously and X87 should
1240 // never occur as a hi class.
1241 case Memory:
1242 case X87:
1243 assert(0 && "Invalid classification for hi word.");
1244
1245 case ComplexX87: // Previously handled.
1246 case NoClass: break;
1247
1248 case Integer:
Owen Anderson47a434f2009-08-05 23:18:46 +00001249 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson0032b272009-08-13 21:57:51 +00001250 llvm::Type::getInt64Ty(VMContext), NULL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001251 break;
1252 case SSE:
Owen Anderson47a434f2009-08-05 23:18:46 +00001253 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson0032b272009-08-13 21:57:51 +00001254 llvm::Type::getDoubleTy(VMContext), NULL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001255 break;
1256
1257 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
1258 // is passed in the upper half of the last used SSE register.
1259 //
1260 // SSEUP should always be preceeded by SSE, just widen.
1261 case SSEUp:
1262 assert(Lo == SSE && "Unexpected SSEUp classification.");
Owen Anderson0032b272009-08-13 21:57:51 +00001263 ResType = llvm::VectorType::get(llvm::Type::getDoubleTy(VMContext), 2);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001264 break;
1265
1266 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
1267 // returned together with the previous X87 value in %st0.
1268 case X87Up:
1269 // If X87Up is preceeded by X87, we don't need to do
1270 // anything. However, in some cases with unions it may not be
1271 // preceeded by X87. In such situations we follow gcc and pass the
1272 // extra bits in an SSE reg.
1273 if (Lo != X87)
Owen Anderson47a434f2009-08-05 23:18:46 +00001274 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson0032b272009-08-13 21:57:51 +00001275 llvm::Type::getDoubleTy(VMContext), NULL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001276 break;
1277 }
1278
1279 return getCoerceResult(RetTy, ResType, Context);
1280}
1281
1282ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, ASTContext &Context,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001283 llvm::LLVMContext &VMContext,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001284 unsigned &neededInt,
1285 unsigned &neededSSE) const {
1286 X86_64ABIInfo::Class Lo, Hi;
1287 classify(Ty, Context, 0, Lo, Hi);
1288
1289 // Check some invariants.
1290 // FIXME: Enforce these by construction.
1291 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
1292 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
1293 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1294
1295 neededInt = 0;
1296 neededSSE = 0;
1297 const llvm::Type *ResType = 0;
1298 switch (Lo) {
1299 case NoClass:
1300 return ABIArgInfo::getIgnore();
1301
1302 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
1303 // on the stack.
1304 case Memory:
1305
1306 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
1307 // COMPLEX_X87, it is passed in memory.
1308 case X87:
1309 case ComplexX87:
1310 return getIndirectResult(Ty, Context);
1311
1312 case SSEUp:
1313 case X87Up:
1314 assert(0 && "Invalid classification for lo word.");
1315
1316 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
1317 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
1318 // and %r9 is used.
1319 case Integer:
1320 ++neededInt;
Owen Anderson0032b272009-08-13 21:57:51 +00001321 ResType = llvm::Type::getInt64Ty(VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001322 break;
1323
1324 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
1325 // available SSE register is used, the registers are taken in the
1326 // order from %xmm0 to %xmm7.
1327 case SSE:
1328 ++neededSSE;
Owen Anderson0032b272009-08-13 21:57:51 +00001329 ResType = llvm::Type::getDoubleTy(VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001330 break;
1331 }
1332
1333 switch (Hi) {
1334 // Memory was handled previously, ComplexX87 and X87 should
1335 // never occur as hi classes, and X87Up must be preceed by X87,
1336 // which is passed in memory.
1337 case Memory:
1338 case X87:
1339 case ComplexX87:
1340 assert(0 && "Invalid classification for hi word.");
1341 break;
1342
1343 case NoClass: break;
1344 case Integer:
Owen Anderson47a434f2009-08-05 23:18:46 +00001345 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson0032b272009-08-13 21:57:51 +00001346 llvm::Type::getInt64Ty(VMContext), NULL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001347 ++neededInt;
1348 break;
1349
1350 // X87Up generally doesn't occur here (long double is passed in
1351 // memory), except in situations involving unions.
1352 case X87Up:
1353 case SSE:
Owen Anderson47a434f2009-08-05 23:18:46 +00001354 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson0032b272009-08-13 21:57:51 +00001355 llvm::Type::getDoubleTy(VMContext), NULL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001356 ++neededSSE;
1357 break;
1358
1359 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
1360 // eightbyte is passed in the upper half of the last used SSE
1361 // register.
1362 case SSEUp:
1363 assert(Lo == SSE && "Unexpected SSEUp classification.");
Owen Anderson0032b272009-08-13 21:57:51 +00001364 ResType = llvm::VectorType::get(llvm::Type::getDoubleTy(VMContext), 2);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001365 break;
1366 }
1367
1368 return getCoerceResult(Ty, ResType, Context);
1369}
1370
Owen Andersona1cf15f2009-07-14 23:10:40 +00001371void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1372 llvm::LLVMContext &VMContext) const {
1373 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(),
1374 Context, VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001375
1376 // Keep track of the number of assigned registers.
1377 unsigned freeIntRegs = 6, freeSSERegs = 8;
1378
1379 // If the return value is indirect, then the hidden argument is consuming one
1380 // integer register.
1381 if (FI.getReturnInfo().isIndirect())
1382 --freeIntRegs;
1383
1384 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
1385 // get assigned (in left-to-right order) for passing as follows...
1386 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1387 it != ie; ++it) {
1388 unsigned neededInt, neededSSE;
Mike Stump1eb44332009-09-09 15:08:12 +00001389 it->info = classifyArgumentType(it->type, Context, VMContext,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001390 neededInt, neededSSE);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001391
1392 // AMD64-ABI 3.2.3p3: If there are no registers available for any
1393 // eightbyte of an argument, the whole argument is passed on the
1394 // stack. If registers have already been assigned for some
1395 // eightbytes of such an argument, the assignments get reverted.
1396 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
1397 freeIntRegs -= neededInt;
1398 freeSSERegs -= neededSSE;
1399 } else {
1400 it->info = getIndirectResult(it->type, Context);
1401 }
1402 }
1403}
1404
1405static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
1406 QualType Ty,
1407 CodeGenFunction &CGF) {
1408 llvm::Value *overflow_arg_area_p =
1409 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
1410 llvm::Value *overflow_arg_area =
1411 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
1412
1413 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
1414 // byte boundary if alignment needed by type exceeds 8 byte boundary.
1415 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
1416 if (Align > 8) {
1417 // Note that we follow the ABI & gcc here, even though the type
1418 // could in theory have an alignment greater than 16. This case
1419 // shouldn't ever matter in practice.
1420
1421 // overflow_arg_area = (overflow_arg_area + 15) & ~15;
Owen Anderson0032b272009-08-13 21:57:51 +00001422 llvm::Value *Offset =
Chris Lattner77b89b82010-06-27 07:15:29 +00001423 llvm::ConstantInt::get(CGF.Int32Ty, 15);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001424 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
1425 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
Chris Lattner77b89b82010-06-27 07:15:29 +00001426 CGF.Int64Ty);
1427 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, ~15LL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001428 overflow_arg_area =
1429 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
1430 overflow_arg_area->getType(),
1431 "overflow_arg_area.align");
1432 }
1433
1434 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
1435 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1436 llvm::Value *Res =
1437 CGF.Builder.CreateBitCast(overflow_arg_area,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001438 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001439
1440 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
1441 // l->overflow_arg_area + sizeof(type).
1442 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
1443 // an 8 byte boundary.
1444
1445 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
Owen Anderson0032b272009-08-13 21:57:51 +00001446 llvm::Value *Offset =
Chris Lattner77b89b82010-06-27 07:15:29 +00001447 llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001448 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
1449 "overflow_arg_area.next");
1450 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
1451
1452 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
1453 return Res;
1454}
1455
1456llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1457 CodeGenFunction &CGF) const {
Owen Andersona1cf15f2009-07-14 23:10:40 +00001458 llvm::LLVMContext &VMContext = CGF.getLLVMContext();
Mike Stump1eb44332009-09-09 15:08:12 +00001459
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001460 // Assume that va_list type is correct; should be pointer to LLVM type:
1461 // struct {
1462 // i32 gp_offset;
1463 // i32 fp_offset;
1464 // i8* overflow_arg_area;
1465 // i8* reg_save_area;
1466 // };
1467 unsigned neededInt, neededSSE;
Chris Lattnera14db752010-03-11 18:19:55 +00001468
1469 Ty = CGF.getContext().getCanonicalType(Ty);
Owen Andersona1cf15f2009-07-14 23:10:40 +00001470 ABIArgInfo AI = classifyArgumentType(Ty, CGF.getContext(), VMContext,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001471 neededInt, neededSSE);
1472
1473 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
1474 // in the registers. If not go to step 7.
1475 if (!neededInt && !neededSSE)
1476 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1477
1478 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
1479 // general purpose registers needed to pass type and num_fp to hold
1480 // the number of floating point registers needed.
1481
1482 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
1483 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
1484 // l->fp_offset > 304 - num_fp * 16 go to step 7.
1485 //
1486 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
1487 // register save space).
1488
1489 llvm::Value *InRegs = 0;
1490 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
1491 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
1492 if (neededInt) {
1493 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
1494 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
Chris Lattner1090a9b2010-06-28 21:43:59 +00001495 InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8);
1496 InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001497 }
1498
1499 if (neededSSE) {
1500 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
1501 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
1502 llvm::Value *FitsInFP =
Chris Lattner1090a9b2010-06-28 21:43:59 +00001503 llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16);
1504 FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001505 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
1506 }
1507
1508 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
1509 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
1510 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
1511 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
1512
1513 // Emit code to load the value if it was passed in registers.
1514
1515 CGF.EmitBlock(InRegBlock);
1516
1517 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
1518 // an offset of l->gp_offset and/or l->fp_offset. This may require
1519 // copying to a temporary location in case the parameter is passed
1520 // in different register classes or requires an alignment greater
1521 // than 8 for general purpose registers and 16 for XMM registers.
1522 //
1523 // FIXME: This really results in shameful code when we end up needing to
1524 // collect arguments from different places; often what should result in a
1525 // simple assembling of a structure from scattered addresses has many more
1526 // loads than necessary. Can we clean this up?
1527 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1528 llvm::Value *RegAddr =
1529 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
1530 "reg_save_area");
1531 if (neededInt && neededSSE) {
1532 // FIXME: Cleanup.
1533 assert(AI.isCoerce() && "Unexpected ABI info for mixed regs");
1534 const llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
1535 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
1536 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
1537 const llvm::Type *TyLo = ST->getElementType(0);
1538 const llvm::Type *TyHi = ST->getElementType(1);
Duncan Sandsf177d9d2010-02-15 16:14:01 +00001539 assert((TyLo->isFloatingPointTy() ^ TyHi->isFloatingPointTy()) &&
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001540 "Unexpected ABI info for mixed regs");
Owen Anderson96e0fc72009-07-29 22:16:19 +00001541 const llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
1542 const llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001543 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1544 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Duncan Sandsf177d9d2010-02-15 16:14:01 +00001545 llvm::Value *RegLoAddr = TyLo->isFloatingPointTy() ? FPAddr : GPAddr;
1546 llvm::Value *RegHiAddr = TyLo->isFloatingPointTy() ? GPAddr : FPAddr;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001547 llvm::Value *V =
1548 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
1549 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1550 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
1551 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1552
Owen Andersona1cf15f2009-07-14 23:10:40 +00001553 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001554 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001555 } else if (neededInt) {
1556 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1557 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001558 llvm::PointerType::getUnqual(LTy));
Chris Lattnerdce5ad02010-06-28 20:05:43 +00001559 } else if (neededSSE == 1) {
1560 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1561 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
1562 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001563 } else {
Chris Lattnerdce5ad02010-06-28 20:05:43 +00001564 assert(neededSSE == 2 && "Invalid number of needed registers!");
1565 // SSE registers are spaced 16 bytes apart in the register save
1566 // area, we need to collect the two eightbytes together.
1567 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Chris Lattner1090a9b2010-06-28 21:43:59 +00001568 llvm::Value *RegAddrHi = CGF.Builder.CreateConstGEP1_32(RegAddrLo, 16);
Chris Lattnerdce5ad02010-06-28 20:05:43 +00001569 const llvm::Type *DoubleTy = llvm::Type::getDoubleTy(VMContext);
1570 const llvm::Type *DblPtrTy =
1571 llvm::PointerType::getUnqual(DoubleTy);
1572 const llvm::StructType *ST = llvm::StructType::get(VMContext, DoubleTy,
1573 DoubleTy, NULL);
1574 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
1575 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
1576 DblPtrTy));
1577 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1578 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
1579 DblPtrTy));
1580 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1581 RegAddr = CGF.Builder.CreateBitCast(Tmp,
1582 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001583 }
1584
1585 // AMD64-ABI 3.5.7p5: Step 5. Set:
1586 // l->gp_offset = l->gp_offset + num_gp * 8
1587 // l->fp_offset = l->fp_offset + num_fp * 16.
1588 if (neededInt) {
Chris Lattner77b89b82010-06-27 07:15:29 +00001589 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001590 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
1591 gp_offset_p);
1592 }
1593 if (neededSSE) {
Chris Lattner77b89b82010-06-27 07:15:29 +00001594 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001595 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
1596 fp_offset_p);
1597 }
1598 CGF.EmitBranch(ContBlock);
1599
1600 // Emit code to load the value if it was passed in memory.
1601
1602 CGF.EmitBlock(InMemBlock);
1603 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1604
1605 // Return the appropriate result.
1606
1607 CGF.EmitBlock(ContBlock);
1608 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(),
1609 "vaarg.addr");
1610 ResAddr->reserveOperandSpace(2);
1611 ResAddr->addIncoming(RegAddr, InRegBlock);
1612 ResAddr->addIncoming(MemAddr, InMemBlock);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001613 return ResAddr;
1614}
1615
Chris Lattnerdce5ad02010-06-28 20:05:43 +00001616
1617
1618//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001619// PIC16 ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00001620//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001621
1622namespace {
1623
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001624class PIC16ABIInfo : public ABIInfo {
1625 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001626 ASTContext &Context,
1627 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001628
1629 ABIArgInfo classifyArgumentType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001630 ASTContext &Context,
1631 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001632
Owen Andersona1cf15f2009-07-14 23:10:40 +00001633 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1634 llvm::LLVMContext &VMContext) const {
1635 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context,
1636 VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001637 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1638 it != ie; ++it)
Owen Andersona1cf15f2009-07-14 23:10:40 +00001639 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001640 }
1641
1642 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1643 CodeGenFunction &CGF) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001644};
1645
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001646class PIC16TargetCodeGenInfo : public TargetCodeGenInfo {
1647public:
Douglas Gregor568bb2d2010-01-22 15:41:14 +00001648 PIC16TargetCodeGenInfo():TargetCodeGenInfo(new PIC16ABIInfo()) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001649};
1650
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001651}
1652
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001653ABIArgInfo PIC16ABIInfo::classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001654 ASTContext &Context,
1655 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001656 if (RetTy->isVoidType()) {
1657 return ABIArgInfo::getIgnore();
1658 } else {
1659 return ABIArgInfo::getDirect();
1660 }
1661}
1662
1663ABIArgInfo PIC16ABIInfo::classifyArgumentType(QualType Ty,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001664 ASTContext &Context,
1665 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001666 return ABIArgInfo::getDirect();
1667}
1668
1669llvm::Value *PIC16ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner77b89b82010-06-27 07:15:29 +00001670 CodeGenFunction &CGF) const {
Chris Lattner52d9ae32010-04-06 17:29:22 +00001671 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Sanjiv Guptaa446ecd2010-02-17 02:25:52 +00001672 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
1673
1674 CGBuilderTy &Builder = CGF.Builder;
1675 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
1676 "ap");
1677 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
1678 llvm::Type *PTy =
1679 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
1680 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
1681
1682 uint64_t Offset = CGF.getContext().getTypeSize(Ty) / 8;
1683
1684 llvm::Value *NextAddr =
1685 Builder.CreateGEP(Addr, llvm::ConstantInt::get(
1686 llvm::Type::getInt32Ty(CGF.getLLVMContext()), Offset),
1687 "ap.next");
1688 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
1689
1690 return AddrTyped;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001691}
1692
Sanjiv Guptaa446ecd2010-02-17 02:25:52 +00001693
John McCallec853ba2010-03-11 00:10:12 +00001694// PowerPC-32
1695
1696namespace {
1697class PPC32TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
1698public:
1699 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
1700 // This is recovered from gcc output.
1701 return 1; // r1 is the dedicated stack pointer
1702 }
1703
1704 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1705 llvm::Value *Address) const;
1706};
1707
1708}
1709
1710bool
1711PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1712 llvm::Value *Address) const {
1713 // This is calculated from the LLVM and GCC tables and verified
1714 // against gcc output. AFAIK all ABIs use the same encoding.
1715
1716 CodeGen::CGBuilderTy &Builder = CGF.Builder;
1717 llvm::LLVMContext &Context = CGF.getLLVMContext();
1718
1719 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
1720 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
1721 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
1722 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
1723
1724 // 0-31: r0-31, the 4-byte general-purpose registers
John McCallaeeb7012010-05-27 06:19:26 +00001725 AssignToArrayRange(Builder, Address, Four8, 0, 31);
John McCallec853ba2010-03-11 00:10:12 +00001726
1727 // 32-63: fp0-31, the 8-byte floating-point registers
John McCallaeeb7012010-05-27 06:19:26 +00001728 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
John McCallec853ba2010-03-11 00:10:12 +00001729
1730 // 64-76 are various 4-byte special-purpose registers:
1731 // 64: mq
1732 // 65: lr
1733 // 66: ctr
1734 // 67: ap
1735 // 68-75 cr0-7
1736 // 76: xer
John McCallaeeb7012010-05-27 06:19:26 +00001737 AssignToArrayRange(Builder, Address, Four8, 64, 76);
John McCallec853ba2010-03-11 00:10:12 +00001738
1739 // 77-108: v0-31, the 16-byte vector registers
John McCallaeeb7012010-05-27 06:19:26 +00001740 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
John McCallec853ba2010-03-11 00:10:12 +00001741
1742 // 109: vrsave
1743 // 110: vscr
1744 // 111: spe_acc
1745 // 112: spefscr
1746 // 113: sfp
John McCallaeeb7012010-05-27 06:19:26 +00001747 AssignToArrayRange(Builder, Address, Four8, 109, 113);
John McCallec853ba2010-03-11 00:10:12 +00001748
1749 return false;
1750}
1751
1752
Chris Lattnerdce5ad02010-06-28 20:05:43 +00001753//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001754// ARM ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00001755//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001756
1757namespace {
1758
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001759class ARMABIInfo : public ABIInfo {
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00001760public:
1761 enum ABIKind {
1762 APCS = 0,
1763 AAPCS = 1,
1764 AAPCS_VFP
1765 };
1766
1767private:
1768 ABIKind Kind;
1769
1770public:
1771 ARMABIInfo(ABIKind _Kind) : Kind(_Kind) {}
1772
1773private:
1774 ABIKind getABIKind() const { return Kind; }
1775
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001776 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001777 ASTContext &Context,
1778 llvm::LLVMContext &VMCOntext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001779
1780 ABIArgInfo classifyArgumentType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001781 ASTContext &Context,
1782 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001783
Owen Andersona1cf15f2009-07-14 23:10:40 +00001784 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1785 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001786
1787 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1788 CodeGenFunction &CGF) const;
1789};
1790
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001791class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
1792public:
1793 ARMTargetCodeGenInfo(ARMABIInfo::ABIKind K)
Douglas Gregor568bb2d2010-01-22 15:41:14 +00001794 :TargetCodeGenInfo(new ARMABIInfo(K)) {}
John McCall6374c332010-03-06 00:35:14 +00001795
1796 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
1797 return 13;
1798 }
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001799};
1800
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001801}
1802
Owen Andersona1cf15f2009-07-14 23:10:40 +00001803void ARMABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1804 llvm::LLVMContext &VMContext) const {
Mike Stump1eb44332009-09-09 15:08:12 +00001805 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001806 VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001807 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1808 it != ie; ++it) {
Owen Andersona1cf15f2009-07-14 23:10:40 +00001809 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001810 }
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00001811
Rafael Espindola25117ab2010-06-16 16:13:39 +00001812 const llvm::Triple &Triple(Context.Target.getTriple());
1813 llvm::CallingConv::ID DefaultCC;
Rafael Espindola1ed1a592010-06-16 19:01:17 +00001814 if (Triple.getEnvironmentName() == "gnueabi" ||
1815 Triple.getEnvironmentName() == "eabi")
Rafael Espindola25117ab2010-06-16 16:13:39 +00001816 DefaultCC = llvm::CallingConv::ARM_AAPCS;
Rafael Espindola1ed1a592010-06-16 19:01:17 +00001817 else
1818 DefaultCC = llvm::CallingConv::ARM_APCS;
Rafael Espindola25117ab2010-06-16 16:13:39 +00001819
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00001820 switch (getABIKind()) {
1821 case APCS:
Rafael Espindola25117ab2010-06-16 16:13:39 +00001822 if (DefaultCC != llvm::CallingConv::ARM_APCS)
1823 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_APCS);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00001824 break;
1825
1826 case AAPCS:
Rafael Espindola25117ab2010-06-16 16:13:39 +00001827 if (DefaultCC != llvm::CallingConv::ARM_AAPCS)
1828 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00001829 break;
1830
1831 case AAPCS_VFP:
1832 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS_VFP);
1833 break;
1834 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001835}
1836
1837ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001838 ASTContext &Context,
1839 llvm::LLVMContext &VMContext) const {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001840 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
1841 // Treat an enum type as its underlying type.
1842 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1843 Ty = EnumTy->getDecl()->getIntegerType();
1844
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001845 return (Ty->isPromotableIntegerType() ?
1846 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001847 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00001848
Daniel Dunbar42025572009-09-14 21:54:03 +00001849 // Ignore empty records.
1850 if (isEmptyRecord(Context, Ty, true))
1851 return ABIArgInfo::getIgnore();
1852
Rafael Espindola0eb1d972010-06-08 02:42:08 +00001853 // Structures with either a non-trivial destructor or a non-trivial
1854 // copy constructor are always indirect.
1855 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
1856 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
1857
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001858 // FIXME: This is kind of nasty... but there isn't much choice because the ARM
1859 // backend doesn't support byval.
1860 // FIXME: This doesn't handle alignment > 64 bits.
1861 const llvm::Type* ElemTy;
1862 unsigned SizeRegs;
1863 if (Context.getTypeAlign(Ty) > 32) {
Owen Anderson0032b272009-08-13 21:57:51 +00001864 ElemTy = llvm::Type::getInt64Ty(VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001865 SizeRegs = (Context.getTypeSize(Ty) + 63) / 64;
1866 } else {
Owen Anderson0032b272009-08-13 21:57:51 +00001867 ElemTy = llvm::Type::getInt32Ty(VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001868 SizeRegs = (Context.getTypeSize(Ty) + 31) / 32;
1869 }
1870 std::vector<const llvm::Type*> LLVMFields;
Owen Anderson96e0fc72009-07-29 22:16:19 +00001871 LLVMFields.push_back(llvm::ArrayType::get(ElemTy, SizeRegs));
Owen Anderson47a434f2009-08-05 23:18:46 +00001872 const llvm::Type* STy = llvm::StructType::get(VMContext, LLVMFields, true);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001873 return ABIArgInfo::getCoerce(STy);
1874}
1875
Daniel Dunbar98303b92009-09-13 08:03:58 +00001876static bool isIntegerLikeType(QualType Ty,
1877 ASTContext &Context,
1878 llvm::LLVMContext &VMContext) {
1879 // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
1880 // is called integer-like if its size is less than or equal to one word, and
1881 // the offset of each of its addressable sub-fields is zero.
1882
1883 uint64_t Size = Context.getTypeSize(Ty);
1884
1885 // Check that the type fits in a word.
1886 if (Size > 32)
1887 return false;
1888
1889 // FIXME: Handle vector types!
1890 if (Ty->isVectorType())
1891 return false;
1892
Daniel Dunbarb0d58192009-09-14 02:20:34 +00001893 // Float types are never treated as "integer like".
1894 if (Ty->isRealFloatingType())
1895 return false;
1896
Daniel Dunbar98303b92009-09-13 08:03:58 +00001897 // If this is a builtin or pointer type then it is ok.
John McCall183700f2009-09-21 23:43:11 +00001898 if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
Daniel Dunbar98303b92009-09-13 08:03:58 +00001899 return true;
1900
Daniel Dunbar45815812010-02-01 23:31:26 +00001901 // Small complex integer types are "integer like".
1902 if (const ComplexType *CT = Ty->getAs<ComplexType>())
1903 return isIntegerLikeType(CT->getElementType(), Context, VMContext);
Daniel Dunbar98303b92009-09-13 08:03:58 +00001904
1905 // Single element and zero sized arrays should be allowed, by the definition
1906 // above, but they are not.
1907
1908 // Otherwise, it must be a record type.
1909 const RecordType *RT = Ty->getAs<RecordType>();
1910 if (!RT) return false;
1911
1912 // Ignore records with flexible arrays.
1913 const RecordDecl *RD = RT->getDecl();
1914 if (RD->hasFlexibleArrayMember())
1915 return false;
1916
1917 // Check that all sub-fields are at offset 0, and are themselves "integer
1918 // like".
1919 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
1920
1921 bool HadField = false;
1922 unsigned idx = 0;
1923 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1924 i != e; ++i, ++idx) {
1925 const FieldDecl *FD = *i;
1926
Daniel Dunbar679855a2010-01-29 03:22:29 +00001927 // Bit-fields are not addressable, we only need to verify they are "integer
1928 // like". We still have to disallow a subsequent non-bitfield, for example:
1929 // struct { int : 0; int x }
1930 // is non-integer like according to gcc.
1931 if (FD->isBitField()) {
1932 if (!RD->isUnion())
1933 HadField = true;
Daniel Dunbar98303b92009-09-13 08:03:58 +00001934
Daniel Dunbar679855a2010-01-29 03:22:29 +00001935 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
1936 return false;
Daniel Dunbar98303b92009-09-13 08:03:58 +00001937
Daniel Dunbar679855a2010-01-29 03:22:29 +00001938 continue;
Daniel Dunbar98303b92009-09-13 08:03:58 +00001939 }
1940
Daniel Dunbar679855a2010-01-29 03:22:29 +00001941 // Check if this field is at offset 0.
1942 if (Layout.getFieldOffset(idx) != 0)
1943 return false;
1944
Daniel Dunbar98303b92009-09-13 08:03:58 +00001945 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
1946 return false;
1947
Daniel Dunbar679855a2010-01-29 03:22:29 +00001948 // Only allow at most one field in a structure. This doesn't match the
1949 // wording above, but follows gcc in situations with a field following an
1950 // empty structure.
Daniel Dunbar98303b92009-09-13 08:03:58 +00001951 if (!RD->isUnion()) {
1952 if (HadField)
1953 return false;
1954
1955 HadField = true;
1956 }
1957 }
1958
1959 return true;
1960}
1961
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001962ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001963 ASTContext &Context,
1964 llvm::LLVMContext &VMContext) const {
Daniel Dunbar98303b92009-09-13 08:03:58 +00001965 if (RetTy->isVoidType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001966 return ABIArgInfo::getIgnore();
Daniel Dunbar98303b92009-09-13 08:03:58 +00001967
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001968 if (!CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1969 // Treat an enum type as its underlying type.
1970 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
1971 RetTy = EnumTy->getDecl()->getIntegerType();
1972
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001973 return (RetTy->isPromotableIntegerType() ?
1974 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001975 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00001976
Rafael Espindola0eb1d972010-06-08 02:42:08 +00001977 // Structures with either a non-trivial destructor or a non-trivial
1978 // copy constructor are always indirect.
1979 if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
1980 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
1981
Daniel Dunbar98303b92009-09-13 08:03:58 +00001982 // Are we following APCS?
1983 if (getABIKind() == APCS) {
1984 if (isEmptyRecord(Context, RetTy, false))
1985 return ABIArgInfo::getIgnore();
1986
Daniel Dunbar4cc753f2010-02-01 23:31:19 +00001987 // Complex types are all returned as packed integers.
1988 //
1989 // FIXME: Consider using 2 x vector types if the back end handles them
1990 // correctly.
1991 if (RetTy->isAnyComplexType())
1992 return ABIArgInfo::getCoerce(llvm::IntegerType::get(
1993 VMContext, Context.getTypeSize(RetTy)));
1994
Daniel Dunbar98303b92009-09-13 08:03:58 +00001995 // Integer like structures are returned in r0.
1996 if (isIntegerLikeType(RetTy, Context, VMContext)) {
1997 // Return in the smallest viable integer type.
1998 uint64_t Size = Context.getTypeSize(RetTy);
1999 if (Size <= 8)
2000 return ABIArgInfo::getCoerce(llvm::Type::getInt8Ty(VMContext));
2001 if (Size <= 16)
2002 return ABIArgInfo::getCoerce(llvm::Type::getInt16Ty(VMContext));
2003 return ABIArgInfo::getCoerce(llvm::Type::getInt32Ty(VMContext));
2004 }
2005
2006 // Otherwise return in memory.
2007 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002008 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00002009
2010 // Otherwise this is an AAPCS variant.
2011
Daniel Dunbar16a08082009-09-14 00:56:55 +00002012 if (isEmptyRecord(Context, RetTy, true))
2013 return ABIArgInfo::getIgnore();
2014
Daniel Dunbar98303b92009-09-13 08:03:58 +00002015 // Aggregates <= 4 bytes are returned in r0; other aggregates
2016 // are returned indirectly.
2017 uint64_t Size = Context.getTypeSize(RetTy);
Daniel Dunbar16a08082009-09-14 00:56:55 +00002018 if (Size <= 32) {
2019 // Return in the smallest viable integer type.
2020 if (Size <= 8)
2021 return ABIArgInfo::getCoerce(llvm::Type::getInt8Ty(VMContext));
2022 if (Size <= 16)
2023 return ABIArgInfo::getCoerce(llvm::Type::getInt16Ty(VMContext));
Daniel Dunbar98303b92009-09-13 08:03:58 +00002024 return ABIArgInfo::getCoerce(llvm::Type::getInt32Ty(VMContext));
Daniel Dunbar16a08082009-09-14 00:56:55 +00002025 }
2026
Daniel Dunbar98303b92009-09-13 08:03:58 +00002027 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002028}
2029
2030llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner77b89b82010-06-27 07:15:29 +00002031 CodeGenFunction &CGF) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002032 // FIXME: Need to handle alignment
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00002033 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Owen Anderson96e0fc72009-07-29 22:16:19 +00002034 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002035
2036 CGBuilderTy &Builder = CGF.Builder;
2037 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2038 "ap");
2039 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2040 llvm::Type *PTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00002041 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002042 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2043
2044 uint64_t Offset =
2045 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
2046 llvm::Value *NextAddr =
Chris Lattner77b89b82010-06-27 07:15:29 +00002047 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002048 "ap.next");
2049 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2050
2051 return AddrTyped;
2052}
2053
2054ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00002055 ASTContext &Context,
2056 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002057 if (RetTy->isVoidType()) {
2058 return ABIArgInfo::getIgnore();
2059 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
2060 return ABIArgInfo::getIndirect(0);
2061 } else {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00002062 // Treat an enum type as its underlying type.
2063 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2064 RetTy = EnumTy->getDecl()->getIntegerType();
2065
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00002066 return (RetTy->isPromotableIntegerType() ?
2067 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002068 }
2069}
2070
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002071//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002072// SystemZ ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002073//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002074
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002075namespace {
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002076
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002077class SystemZABIInfo : public ABIInfo {
2078 bool isPromotableIntegerType(QualType Ty) const;
2079
2080 ABIArgInfo classifyReturnType(QualType RetTy, ASTContext &Context,
2081 llvm::LLVMContext &VMContext) const;
2082
2083 ABIArgInfo classifyArgumentType(QualType RetTy, ASTContext &Context,
2084 llvm::LLVMContext &VMContext) const;
2085
2086 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
2087 llvm::LLVMContext &VMContext) const {
2088 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(),
2089 Context, VMContext);
2090 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2091 it != ie; ++it)
2092 it->info = classifyArgumentType(it->type, Context, VMContext);
2093 }
2094
2095 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2096 CodeGenFunction &CGF) const;
2097};
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002098
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002099class SystemZTargetCodeGenInfo : public TargetCodeGenInfo {
2100public:
Douglas Gregor568bb2d2010-01-22 15:41:14 +00002101 SystemZTargetCodeGenInfo():TargetCodeGenInfo(new SystemZABIInfo()) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002102};
2103
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002104}
2105
2106bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const {
2107 // SystemZ ABI requires all 8, 16 and 32 bit quantities to be extended.
John McCall183700f2009-09-21 23:43:11 +00002108 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002109 switch (BT->getKind()) {
2110 case BuiltinType::Bool:
2111 case BuiltinType::Char_S:
2112 case BuiltinType::Char_U:
2113 case BuiltinType::SChar:
2114 case BuiltinType::UChar:
2115 case BuiltinType::Short:
2116 case BuiltinType::UShort:
2117 case BuiltinType::Int:
2118 case BuiltinType::UInt:
2119 return true;
2120 default:
2121 return false;
2122 }
2123 return false;
2124}
2125
2126llvm::Value *SystemZABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2127 CodeGenFunction &CGF) const {
2128 // FIXME: Implement
2129 return 0;
2130}
2131
2132
2133ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy,
2134 ASTContext &Context,
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002135 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002136 if (RetTy->isVoidType()) {
2137 return ABIArgInfo::getIgnore();
2138 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
2139 return ABIArgInfo::getIndirect(0);
2140 } else {
2141 return (isPromotableIntegerType(RetTy) ?
2142 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2143 }
2144}
2145
2146ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty,
2147 ASTContext &Context,
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002148 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002149 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
2150 return ABIArgInfo::getIndirect(0);
2151 } else {
2152 return (isPromotableIntegerType(Ty) ?
2153 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2154 }
2155}
2156
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002157//===----------------------------------------------------------------------===//
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002158// MSP430 ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002159//===----------------------------------------------------------------------===//
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002160
2161namespace {
2162
2163class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
2164public:
Douglas Gregor568bb2d2010-01-22 15:41:14 +00002165 MSP430TargetCodeGenInfo():TargetCodeGenInfo(new DefaultABIInfo()) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002166 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2167 CodeGen::CodeGenModule &M) const;
2168};
2169
2170}
2171
2172void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
2173 llvm::GlobalValue *GV,
2174 CodeGen::CodeGenModule &M) const {
2175 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
2176 if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) {
2177 // Handle 'interrupt' attribute:
2178 llvm::Function *F = cast<llvm::Function>(GV);
2179
2180 // Step 1: Set ISR calling convention.
2181 F->setCallingConv(llvm::CallingConv::MSP430_INTR);
2182
2183 // Step 2: Add attributes goodness.
2184 F->addFnAttr(llvm::Attribute::NoInline);
2185
2186 // Step 3: Emit ISR vector alias.
2187 unsigned Num = attr->getNumber() + 0xffe0;
2188 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
2189 "vector_" +
2190 llvm::LowercaseString(llvm::utohexstr(Num)),
2191 GV, &M.getModule());
2192 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002193 }
2194}
2195
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002196//===----------------------------------------------------------------------===//
John McCallaeeb7012010-05-27 06:19:26 +00002197// MIPS ABI Implementation. This works for both little-endian and
2198// big-endian variants.
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002199//===----------------------------------------------------------------------===//
2200
John McCallaeeb7012010-05-27 06:19:26 +00002201namespace {
2202class MIPSTargetCodeGenInfo : public TargetCodeGenInfo {
2203public:
2204 MIPSTargetCodeGenInfo(): TargetCodeGenInfo(new DefaultABIInfo()) {}
2205
2206 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
2207 return 29;
2208 }
2209
2210 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2211 llvm::Value *Address) const;
2212};
2213}
2214
2215bool
2216MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2217 llvm::Value *Address) const {
2218 // This information comes from gcc's implementation, which seems to
2219 // as canonical as it gets.
2220
2221 CodeGen::CGBuilderTy &Builder = CGF.Builder;
2222 llvm::LLVMContext &Context = CGF.getLLVMContext();
2223
2224 // Everything on MIPS is 4 bytes. Double-precision FP registers
2225 // are aliased to pairs of single-precision FP registers.
2226 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
2227 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2228
2229 // 0-31 are the general purpose registers, $0 - $31.
2230 // 32-63 are the floating-point registers, $f0 - $f31.
2231 // 64 and 65 are the multiply/divide registers, $hi and $lo.
2232 // 66 is the (notional, I think) register for signal-handler return.
2233 AssignToArrayRange(Builder, Address, Four8, 0, 65);
2234
2235 // 67-74 are the floating-point status registers, $fcc0 - $fcc7.
2236 // They are one bit wide and ignored here.
2237
2238 // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31.
2239 // (coprocessor 1 is the FP unit)
2240 // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31.
2241 // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31.
2242 // 176-181 are the DSP accumulator registers.
2243 AssignToArrayRange(Builder, Address, Four8, 80, 181);
2244
2245 return false;
2246}
2247
2248
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002249const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() const {
2250 if (TheTargetCodeGenInfo)
2251 return *TheTargetCodeGenInfo;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002252
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002253 // For now we just cache the TargetCodeGenInfo in CodeGenModule and don't
2254 // free it.
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002255
Daniel Dunbar1752ee42009-08-24 09:10:05 +00002256 const llvm::Triple &Triple(getContext().Target.getTriple());
2257 switch (Triple.getArch()) {
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002258 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002259 return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo);
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002260
John McCallaeeb7012010-05-27 06:19:26 +00002261 case llvm::Triple::mips:
2262 case llvm::Triple::mipsel:
2263 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo());
2264
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002265 case llvm::Triple::arm:
2266 case llvm::Triple::thumb:
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002267 // FIXME: We want to know the float calling convention as well.
Daniel Dunbar018ba5a2009-09-14 00:35:03 +00002268 if (strcmp(getContext().Target.getABI(), "apcs-gnu") == 0)
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002269 return *(TheTargetCodeGenInfo =
2270 new ARMTargetCodeGenInfo(ARMABIInfo::APCS));
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002271
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002272 return *(TheTargetCodeGenInfo =
2273 new ARMTargetCodeGenInfo(ARMABIInfo::AAPCS));
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002274
2275 case llvm::Triple::pic16:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002276 return *(TheTargetCodeGenInfo = new PIC16TargetCodeGenInfo());
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002277
John McCallec853ba2010-03-11 00:10:12 +00002278 case llvm::Triple::ppc:
2279 return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo());
2280
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002281 case llvm::Triple::systemz:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002282 return *(TheTargetCodeGenInfo = new SystemZTargetCodeGenInfo());
2283
2284 case llvm::Triple::msp430:
2285 return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo());
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002286
Daniel Dunbar1752ee42009-08-24 09:10:05 +00002287 case llvm::Triple::x86:
Daniel Dunbar1752ee42009-08-24 09:10:05 +00002288 switch (Triple.getOS()) {
Edward O'Callaghan7ee68bd2009-10-20 17:22:50 +00002289 case llvm::Triple::Darwin:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002290 return *(TheTargetCodeGenInfo =
2291 new X86_32TargetCodeGenInfo(Context, true, true));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002292 case llvm::Triple::Cygwin:
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002293 case llvm::Triple::MinGW32:
2294 case llvm::Triple::MinGW64:
Edward O'Callaghan727e2682009-10-21 11:58:24 +00002295 case llvm::Triple::AuroraUX:
2296 case llvm::Triple::DragonFly:
David Chisnall75c135a2009-09-03 01:48:05 +00002297 case llvm::Triple::FreeBSD:
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002298 case llvm::Triple::OpenBSD:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002299 return *(TheTargetCodeGenInfo =
2300 new X86_32TargetCodeGenInfo(Context, false, true));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002301
2302 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002303 return *(TheTargetCodeGenInfo =
2304 new X86_32TargetCodeGenInfo(Context, false, false));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002305 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002306
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002307 case llvm::Triple::x86_64:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002308 return *(TheTargetCodeGenInfo = new X86_64TargetCodeGenInfo());
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002309 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002310}