blob: e95b3b6e9013ccbf2340f3fe33a26dcf32715684 [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
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000319/// X86_32ABIInfo - The X86-32 ABI information.
320class X86_32ABIInfo : public ABIInfo {
321 ASTContext &Context;
David Chisnall1e4249c2009-08-17 23:08:21 +0000322 bool IsDarwinVectorABI;
323 bool IsSmallStructInRegABI;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000324
325 static bool isRegisterSize(unsigned Size) {
326 return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
327 }
328
329 static bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context);
330
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000331 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
332 /// such that the argument will be passed in memory.
333 ABIArgInfo getIndirectResult(QualType Ty, ASTContext &Context,
334 bool ByVal = true) const;
335
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000336public:
337 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000338 ASTContext &Context,
339 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000340
341 ABIArgInfo classifyArgumentType(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
Owen Andersona1cf15f2009-07-14 23:10:40 +0000345 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
346 llvm::LLVMContext &VMContext) const {
347 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context,
348 VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000349 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
350 it != ie; ++it)
Owen Andersona1cf15f2009-07-14 23:10:40 +0000351 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000352 }
353
354 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
355 CodeGenFunction &CGF) const;
356
David Chisnall1e4249c2009-08-17 23:08:21 +0000357 X86_32ABIInfo(ASTContext &Context, bool d, bool p)
Mike Stump1eb44332009-09-09 15:08:12 +0000358 : ABIInfo(), Context(Context), IsDarwinVectorABI(d),
David Chisnall1e4249c2009-08-17 23:08:21 +0000359 IsSmallStructInRegABI(p) {}
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000360};
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000361
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000362class X86_32TargetCodeGenInfo : public TargetCodeGenInfo {
363public:
364 X86_32TargetCodeGenInfo(ASTContext &Context, bool d, bool p)
Douglas Gregor568bb2d2010-01-22 15:41:14 +0000365 :TargetCodeGenInfo(new X86_32ABIInfo(Context, d, p)) {}
Charles Davis74f72932010-02-13 15:54:06 +0000366
367 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
368 CodeGen::CodeGenModule &CGM) const;
John McCall6374c332010-03-06 00:35:14 +0000369
370 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
371 // Darwin uses different dwarf register numbers for EH.
372 if (CGM.isTargetDarwin()) return 5;
373
374 return 4;
375 }
376
377 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
378 llvm::Value *Address) const;
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000379};
380
381}
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000382
383/// shouldReturnTypeInRegister - Determine if the given type should be
384/// passed in a register (for the Darwin ABI).
385bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
386 ASTContext &Context) {
387 uint64_t Size = Context.getTypeSize(Ty);
388
389 // Type must be register sized.
390 if (!isRegisterSize(Size))
391 return false;
392
393 if (Ty->isVectorType()) {
394 // 64- and 128- bit vectors inside structures are not returned in
395 // registers.
396 if (Size == 64 || Size == 128)
397 return false;
398
399 return true;
400 }
401
Daniel Dunbar77115232010-05-15 00:00:30 +0000402 // If this is a builtin, pointer, enum, complex type, member pointer, or
403 // member function pointer it is ok.
Daniel Dunbara1842d32010-05-14 03:40:53 +0000404 if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() ||
Daniel Dunbar55e59e12009-09-24 05:12:36 +0000405 Ty->isAnyComplexType() || Ty->isEnumeralType() ||
Daniel Dunbar77115232010-05-15 00:00:30 +0000406 Ty->isBlockPointerType() || Ty->isMemberPointerType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000407 return true;
408
409 // Arrays are treated like records.
410 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
411 return shouldReturnTypeInRegister(AT->getElementType(), Context);
412
413 // Otherwise, it must be a record type.
Ted Kremenek6217b802009-07-29 21:53:49 +0000414 const RecordType *RT = Ty->getAs<RecordType>();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000415 if (!RT) return false;
416
Anders Carlssona8874232010-01-27 03:25:19 +0000417 // FIXME: Traverse bases here too.
418
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000419 // Structure types are passed in register if all fields would be
420 // passed in a register.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000421 for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(),
422 e = RT->getDecl()->field_end(); i != e; ++i) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000423 const FieldDecl *FD = *i;
424
425 // Empty fields are ignored.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000426 if (isEmptyField(Context, FD, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000427 continue;
428
429 // Check fields recursively.
430 if (!shouldReturnTypeInRegister(FD->getType(), Context))
431 return false;
432 }
433
434 return true;
435}
436
437ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000438 ASTContext &Context,
439 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000440 if (RetTy->isVoidType()) {
441 return ABIArgInfo::getIgnore();
John McCall183700f2009-09-21 23:43:11 +0000442 } else if (const VectorType *VT = RetTy->getAs<VectorType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000443 // On Darwin, some vectors are returned in registers.
David Chisnall1e4249c2009-08-17 23:08:21 +0000444 if (IsDarwinVectorABI) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000445 uint64_t Size = Context.getTypeSize(RetTy);
446
447 // 128-bit vectors are a special case; they are returned in
448 // registers and we need to make sure to pick a type the LLVM
449 // backend will like.
450 if (Size == 128)
Owen Anderson0032b272009-08-13 21:57:51 +0000451 return ABIArgInfo::getCoerce(llvm::VectorType::get(
452 llvm::Type::getInt64Ty(VMContext), 2));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000453
454 // Always return in register if it fits in a general purpose
455 // register, or if it is 64 bits and has a single element.
456 if ((Size == 8 || Size == 16 || Size == 32) ||
457 (Size == 64 && VT->getNumElements() == 1))
Owen Anderson0032b272009-08-13 21:57:51 +0000458 return ABIArgInfo::getCoerce(llvm::IntegerType::get(VMContext, Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000459
460 return ABIArgInfo::getIndirect(0);
461 }
462
463 return ABIArgInfo::getDirect();
464 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Anders Carlssona8874232010-01-27 03:25:19 +0000465 if (const RecordType *RT = RetTy->getAs<RecordType>()) {
Anders Carlsson40092972009-10-20 22:07:59 +0000466 // Structures with either a non-trivial destructor or a non-trivial
467 // copy constructor are always indirect.
468 if (hasNonTrivialDestructorOrCopyConstructor(RT))
469 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
470
471 // Structures with flexible arrays are always indirect.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000472 if (RT->getDecl()->hasFlexibleArrayMember())
473 return ABIArgInfo::getIndirect(0);
Anders Carlsson40092972009-10-20 22:07:59 +0000474 }
475
David Chisnall1e4249c2009-08-17 23:08:21 +0000476 // If specified, structs and unions are always indirect.
477 if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000478 return ABIArgInfo::getIndirect(0);
479
480 // Classify "single element" structs as their element type.
481 if (const Type *SeltTy = isSingleElementStruct(RetTy, Context)) {
John McCall183700f2009-09-21 23:43:11 +0000482 if (const BuiltinType *BT = SeltTy->getAs<BuiltinType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000483 if (BT->isIntegerType()) {
484 // We need to use the size of the structure, padding
485 // bit-fields can adjust that to be larger than the single
486 // element type.
487 uint64_t Size = Context.getTypeSize(RetTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000488 return ABIArgInfo::getCoerce(
Owen Anderson0032b272009-08-13 21:57:51 +0000489 llvm::IntegerType::get(VMContext, (unsigned) Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000490 } else if (BT->getKind() == BuiltinType::Float) {
491 assert(Context.getTypeSize(RetTy) == Context.getTypeSize(SeltTy) &&
492 "Unexpect single element structure size!");
Owen Anderson0032b272009-08-13 21:57:51 +0000493 return ABIArgInfo::getCoerce(llvm::Type::getFloatTy(VMContext));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000494 } else if (BT->getKind() == BuiltinType::Double) {
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::getDoubleTy(VMContext));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000498 }
499 } else if (SeltTy->isPointerType()) {
500 // FIXME: It would be really nice if this could come out as the proper
501 // pointer type.
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +0000502 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000503 return ABIArgInfo::getCoerce(PtrTy);
504 } else if (SeltTy->isVectorType()) {
505 // 64- and 128-bit vectors are never returned in a
506 // register when inside a structure.
507 uint64_t Size = Context.getTypeSize(RetTy);
508 if (Size == 64 || Size == 128)
509 return ABIArgInfo::getIndirect(0);
510
Owen Andersona1cf15f2009-07-14 23:10:40 +0000511 return classifyReturnType(QualType(SeltTy, 0), Context, VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000512 }
513 }
514
515 // Small structures which are register sized are generally returned
516 // in a register.
517 if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, Context)) {
518 uint64_t Size = Context.getTypeSize(RetTy);
Owen Anderson0032b272009-08-13 21:57:51 +0000519 return ABIArgInfo::getCoerce(llvm::IntegerType::get(VMContext, Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000520 }
521
522 return ABIArgInfo::getIndirect(0);
523 } else {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +0000524 // Treat an enum type as its underlying type.
525 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
526 RetTy = EnumTy->getDecl()->getIntegerType();
527
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000528 return (RetTy->isPromotableIntegerType() ?
529 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000530 }
531}
532
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000533ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty,
534 ASTContext &Context,
535 bool ByVal) const {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +0000536 if (!ByVal)
537 return ABIArgInfo::getIndirect(0, false);
538
539 // Compute the byval alignment. We trust the back-end to honor the
540 // minimum ABI alignment for byval, to make cleaner IR.
541 const unsigned MinABIAlign = 4;
542 unsigned Align = Context.getTypeAlign(Ty) / 8;
543 if (Align > MinABIAlign)
544 return ABIArgInfo::getIndirect(Align);
545 return ABIArgInfo::getIndirect(0);
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000546}
547
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000548ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000549 ASTContext &Context,
550 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000551 // FIXME: Set alignment on indirect arguments.
552 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
553 // Structures with flexible arrays are always indirect.
Anders Carlssona8874232010-01-27 03:25:19 +0000554 if (const RecordType *RT = Ty->getAs<RecordType>()) {
555 // Structures with either a non-trivial destructor or a non-trivial
556 // copy constructor are always indirect.
557 if (hasNonTrivialDestructorOrCopyConstructor(RT))
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000558 return getIndirectResult(Ty, Context, /*ByVal=*/false);
559
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000560 if (RT->getDecl()->hasFlexibleArrayMember())
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000561 return getIndirectResult(Ty, Context);
Anders Carlssona8874232010-01-27 03:25:19 +0000562 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000563
564 // Ignore empty structs.
Eli Friedmana1e6de92009-06-13 21:37:10 +0000565 if (Ty->isStructureType() && Context.getTypeSize(Ty) == 0)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000566 return ABIArgInfo::getIgnore();
567
Daniel Dunbar53012f42009-11-09 01:33:53 +0000568 // Expand small (<= 128-bit) record types when we know that the stack layout
569 // of those arguments will match the struct. This is important because the
570 // LLVM backend isn't smart enough to remove byval, which inhibits many
571 // optimizations.
572 if (Context.getTypeSize(Ty) <= 4*32 &&
573 canExpandIndirectArgument(Ty, Context))
574 return ABIArgInfo::getExpand();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000575
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000576 return getIndirectResult(Ty, Context);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000577 } else {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +0000578 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
579 Ty = EnumTy->getDecl()->getIntegerType();
580
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000581 return (Ty->isPromotableIntegerType() ?
582 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000583 }
584}
585
586llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
587 CodeGenFunction &CGF) const {
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +0000588 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Owen Anderson96e0fc72009-07-29 22:16:19 +0000589 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000590
591 CGBuilderTy &Builder = CGF.Builder;
592 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
593 "ap");
594 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
595 llvm::Type *PTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000596 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000597 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
598
599 uint64_t Offset =
600 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
601 llvm::Value *NextAddr =
Chris Lattner77b89b82010-06-27 07:15:29 +0000602 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000603 "ap.next");
604 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
605
606 return AddrTyped;
607}
608
Charles Davis74f72932010-02-13 15:54:06 +0000609void X86_32TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
610 llvm::GlobalValue *GV,
611 CodeGen::CodeGenModule &CGM) const {
612 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
613 if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
614 // Get the LLVM function.
615 llvm::Function *Fn = cast<llvm::Function>(GV);
616
617 // Now add the 'alignstack' attribute with a value of 16.
618 Fn->addFnAttr(llvm::Attribute::constructStackAlignmentFromInt(16));
619 }
620 }
621}
622
John McCall6374c332010-03-06 00:35:14 +0000623bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable(
624 CodeGen::CodeGenFunction &CGF,
625 llvm::Value *Address) const {
626 CodeGen::CGBuilderTy &Builder = CGF.Builder;
627 llvm::LLVMContext &Context = CGF.getLLVMContext();
628
629 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
630 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
631
632 // 0-7 are the eight integer registers; the order is different
633 // on Darwin (for EH), but the range is the same.
634 // 8 is %eip.
John McCallaeeb7012010-05-27 06:19:26 +0000635 AssignToArrayRange(Builder, Address, Four8, 0, 8);
John McCall6374c332010-03-06 00:35:14 +0000636
637 if (CGF.CGM.isTargetDarwin()) {
638 // 12-16 are st(0..4). Not sure why we stop at 4.
639 // These have size 16, which is sizeof(long double) on
640 // platforms with 8-byte alignment for that type.
641 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
John McCallaeeb7012010-05-27 06:19:26 +0000642 AssignToArrayRange(Builder, Address, Sixteen8, 12, 16);
John McCall6374c332010-03-06 00:35:14 +0000643
644 } else {
645 // 9 is %eflags, which doesn't get a size on Darwin for some
646 // reason.
647 Builder.CreateStore(Four8, Builder.CreateConstInBoundsGEP1_32(Address, 9));
648
649 // 11-16 are st(0..5). Not sure why we stop at 5.
650 // These have size 12, which is sizeof(long double) on
651 // platforms with 4-byte alignment for that type.
652 llvm::Value *Twelve8 = llvm::ConstantInt::get(i8, 12);
John McCallaeeb7012010-05-27 06:19:26 +0000653 AssignToArrayRange(Builder, Address, Twelve8, 11, 16);
654 }
John McCall6374c332010-03-06 00:35:14 +0000655
656 return false;
657}
658
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000659namespace {
660/// X86_64ABIInfo - The X86_64 ABI information.
661class X86_64ABIInfo : public ABIInfo {
662 enum Class {
663 Integer = 0,
664 SSE,
665 SSEUp,
666 X87,
667 X87Up,
668 ComplexX87,
669 NoClass,
670 Memory
671 };
672
673 /// merge - Implement the X86_64 ABI merging algorithm.
674 ///
675 /// Merge an accumulating classification \arg Accum with a field
676 /// classification \arg Field.
677 ///
678 /// \param Accum - The accumulating classification. This should
679 /// always be either NoClass or the result of a previous merge
680 /// call. In addition, this should never be Memory (the caller
681 /// should just return Memory for the aggregate).
682 Class merge(Class Accum, Class Field) const;
683
684 /// classify - Determine the x86_64 register classes in which the
685 /// given type T should be passed.
686 ///
687 /// \param Lo - The classification for the parts of the type
688 /// residing in the low word of the containing object.
689 ///
690 /// \param Hi - The classification for the parts of the type
691 /// residing in the high word of the containing object.
692 ///
693 /// \param OffsetBase - The bit offset of this type in the
694 /// containing object. Some parameters are classified different
695 /// depending on whether they straddle an eightbyte boundary.
696 ///
697 /// If a word is unused its result will be NoClass; if a type should
698 /// be passed in Memory then at least the classification of \arg Lo
699 /// will be Memory.
700 ///
701 /// The \arg Lo class will be NoClass iff the argument is ignored.
702 ///
703 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
704 /// also be ComplexX87.
705 void classify(QualType T, ASTContext &Context, uint64_t OffsetBase,
706 Class &Lo, Class &Hi) const;
707
708 /// getCoerceResult - Given a source type \arg Ty and an LLVM type
709 /// to coerce to, chose the best way to pass Ty in the same place
710 /// that \arg CoerceTo would be passed, but while keeping the
711 /// emitted code as simple as possible.
712 ///
713 /// FIXME: Note, this should be cleaned up to just take an enumeration of all
714 /// the ways we might want to pass things, instead of constructing an LLVM
715 /// type. This makes this code more explicit, and it makes it clearer that we
716 /// are also doing this for correctness in the case of passing scalar types.
717 ABIArgInfo getCoerceResult(QualType Ty,
718 const llvm::Type *CoerceTo,
719 ASTContext &Context) const;
720
721 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Daniel Dunbar46c54fb2010-04-21 19:49:55 +0000722 /// such that the argument will be returned in memory.
723 ABIArgInfo getIndirectReturnResult(QualType Ty, ASTContext &Context) const;
724
725 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000726 /// such that the argument will be passed in memory.
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000727 ABIArgInfo getIndirectResult(QualType Ty, ASTContext &Context) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000728
729 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000730 ASTContext &Context,
731 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000732
733 ABIArgInfo classifyArgumentType(QualType Ty,
734 ASTContext &Context,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000735 llvm::LLVMContext &VMContext,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000736 unsigned &neededInt,
737 unsigned &neededSSE) const;
738
739public:
Owen Andersona1cf15f2009-07-14 23:10:40 +0000740 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
741 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000742
743 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
744 CodeGenFunction &CGF) const;
745};
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000746
747class X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
748public:
Douglas Gregor568bb2d2010-01-22 15:41:14 +0000749 X86_64TargetCodeGenInfo():TargetCodeGenInfo(new X86_64ABIInfo()) {}
John McCall6374c332010-03-06 00:35:14 +0000750
751 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
752 return 7;
753 }
754
755 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
756 llvm::Value *Address) const {
757 CodeGen::CGBuilderTy &Builder = CGF.Builder;
758 llvm::LLVMContext &Context = CGF.getLLVMContext();
759
760 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
761 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
762
John McCallaeeb7012010-05-27 06:19:26 +0000763 // 0-15 are the 16 integer registers.
764 // 16 is %rip.
765 AssignToArrayRange(Builder, Address, Eight8, 0, 16);
John McCall6374c332010-03-06 00:35:14 +0000766
767 return false;
768 }
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000769};
770
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000771}
772
773X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum,
774 Class Field) const {
775 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
776 // classified recursively so that always two fields are
777 // considered. The resulting class is calculated according to
778 // the classes of the fields in the eightbyte:
779 //
780 // (a) If both classes are equal, this is the resulting class.
781 //
782 // (b) If one of the classes is NO_CLASS, the resulting class is
783 // the other class.
784 //
785 // (c) If one of the classes is MEMORY, the result is the MEMORY
786 // class.
787 //
788 // (d) If one of the classes is INTEGER, the result is the
789 // INTEGER.
790 //
791 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
792 // MEMORY is used as class.
793 //
794 // (f) Otherwise class SSE is used.
795
796 // Accum should never be memory (we should have returned) or
797 // ComplexX87 (because this cannot be passed in a structure).
798 assert((Accum != Memory && Accum != ComplexX87) &&
799 "Invalid accumulated classification during merge.");
800 if (Accum == Field || Field == NoClass)
801 return Accum;
802 else if (Field == Memory)
803 return Memory;
804 else if (Accum == NoClass)
805 return Field;
806 else if (Accum == Integer || Field == Integer)
807 return Integer;
808 else if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
809 Accum == X87 || Accum == X87Up)
810 return Memory;
811 else
812 return SSE;
813}
814
815void X86_64ABIInfo::classify(QualType Ty,
816 ASTContext &Context,
817 uint64_t OffsetBase,
818 Class &Lo, Class &Hi) const {
819 // FIXME: This code can be simplified by introducing a simple value class for
820 // Class pairs with appropriate constructor methods for the various
821 // situations.
822
823 // FIXME: Some of the split computations are wrong; unaligned vectors
824 // shouldn't be passed in registers for example, so there is no chance they
825 // can straddle an eightbyte. Verify & simplify.
826
827 Lo = Hi = NoClass;
828
829 Class &Current = OffsetBase < 64 ? Lo : Hi;
830 Current = Memory;
831
John McCall183700f2009-09-21 23:43:11 +0000832 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000833 BuiltinType::Kind k = BT->getKind();
834
835 if (k == BuiltinType::Void) {
836 Current = NoClass;
837 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
838 Lo = Integer;
839 Hi = Integer;
840 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
841 Current = Integer;
842 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
843 Current = SSE;
844 } else if (k == BuiltinType::LongDouble) {
845 Lo = X87;
846 Hi = X87Up;
847 }
848 // FIXME: _Decimal32 and _Decimal64 are SSE.
849 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
John McCall183700f2009-09-21 23:43:11 +0000850 } else if (const EnumType *ET = Ty->getAs<EnumType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000851 // Classify the underlying integer type.
852 classify(ET->getDecl()->getIntegerType(), Context, OffsetBase, Lo, Hi);
853 } else if (Ty->hasPointerRepresentation()) {
854 Current = Integer;
Daniel Dunbar67d438d2010-05-15 00:00:37 +0000855 } else if (Ty->isMemberPointerType()) {
856 if (Ty->isMemberFunctionPointerType())
857 Lo = Hi = Integer;
858 else
859 Current = Integer;
John McCall183700f2009-09-21 23:43:11 +0000860 } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000861 uint64_t Size = Context.getTypeSize(VT);
862 if (Size == 32) {
863 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
864 // float> as integer.
865 Current = Integer;
866
867 // If this type crosses an eightbyte boundary, it should be
868 // split.
869 uint64_t EB_Real = (OffsetBase) / 64;
870 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
871 if (EB_Real != EB_Imag)
872 Hi = Lo;
873 } else if (Size == 64) {
874 // gcc passes <1 x double> in memory. :(
875 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
876 return;
877
878 // gcc passes <1 x long long> as INTEGER.
879 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong))
880 Current = Integer;
881 else
882 Current = SSE;
883
884 // If this type crosses an eightbyte boundary, it should be
885 // split.
886 if (OffsetBase && OffsetBase != 64)
887 Hi = Lo;
888 } else if (Size == 128) {
889 Lo = SSE;
890 Hi = SSEUp;
891 }
John McCall183700f2009-09-21 23:43:11 +0000892 } else if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000893 QualType ET = Context.getCanonicalType(CT->getElementType());
894
895 uint64_t Size = Context.getTypeSize(Ty);
Douglas Gregor2ade35e2010-06-16 00:17:44 +0000896 if (ET->isIntegralOrEnumerationType()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000897 if (Size <= 64)
898 Current = Integer;
899 else if (Size <= 128)
900 Lo = Hi = Integer;
901 } else if (ET == Context.FloatTy)
902 Current = SSE;
903 else if (ET == Context.DoubleTy)
904 Lo = Hi = SSE;
905 else if (ET == Context.LongDoubleTy)
906 Current = ComplexX87;
907
908 // If this complex type crosses an eightbyte boundary then it
909 // should be split.
910 uint64_t EB_Real = (OffsetBase) / 64;
911 uint64_t EB_Imag = (OffsetBase + Context.getTypeSize(ET)) / 64;
912 if (Hi == NoClass && EB_Real != EB_Imag)
913 Hi = Lo;
914 } else if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
915 // Arrays are treated like structures.
916
917 uint64_t Size = Context.getTypeSize(Ty);
918
919 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
920 // than two eightbytes, ..., it has class MEMORY.
921 if (Size > 128)
922 return;
923
924 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
925 // fields, it has class MEMORY.
926 //
927 // Only need to check alignment of array base.
928 if (OffsetBase % Context.getTypeAlign(AT->getElementType()))
929 return;
930
931 // Otherwise implement simplified merge. We could be smarter about
932 // this, but it isn't worth it and would be harder to verify.
933 Current = NoClass;
934 uint64_t EltSize = Context.getTypeSize(AT->getElementType());
935 uint64_t ArraySize = AT->getSize().getZExtValue();
936 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
937 Class FieldLo, FieldHi;
938 classify(AT->getElementType(), Context, Offset, FieldLo, FieldHi);
939 Lo = merge(Lo, FieldLo);
940 Hi = merge(Hi, FieldHi);
941 if (Lo == Memory || Hi == Memory)
942 break;
943 }
944
945 // Do post merger cleanup (see below). Only case we worry about is Memory.
946 if (Hi == Memory)
947 Lo = Memory;
948 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Ted Kremenek6217b802009-07-29 21:53:49 +0000949 } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000950 uint64_t Size = Context.getTypeSize(Ty);
951
952 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
953 // than two eightbytes, ..., it has class MEMORY.
954 if (Size > 128)
955 return;
956
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000957 // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
958 // copy constructor or a non-trivial destructor, it is passed by invisible
959 // reference.
960 if (hasNonTrivialDestructorOrCopyConstructor(RT))
961 return;
Daniel Dunbarce9f4232009-11-22 23:01:23 +0000962
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000963 const RecordDecl *RD = RT->getDecl();
964
965 // Assume variable sized types are passed in memory.
966 if (RD->hasFlexibleArrayMember())
967 return;
968
969 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
970
971 // Reset Lo class, this will be recomputed.
972 Current = NoClass;
Daniel Dunbarce9f4232009-11-22 23:01:23 +0000973
974 // If this is a C++ record, classify the bases first.
975 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
976 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
977 e = CXXRD->bases_end(); i != e; ++i) {
978 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
979 "Unexpected base class!");
980 const CXXRecordDecl *Base =
981 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
982
983 // Classify this field.
984 //
985 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
986 // single eightbyte, each is classified separately. Each eightbyte gets
987 // initialized to class NO_CLASS.
988 Class FieldLo, FieldHi;
989 uint64_t Offset = OffsetBase + Layout.getBaseClassOffset(Base);
990 classify(i->getType(), Context, Offset, FieldLo, FieldHi);
991 Lo = merge(Lo, FieldLo);
992 Hi = merge(Hi, FieldHi);
993 if (Lo == Memory || Hi == Memory)
994 break;
995 }
Daniel Dunbar4971ff82009-12-22 01:19:25 +0000996
997 // If this record has no fields but isn't empty, classify as INTEGER.
998 if (RD->field_empty() && Size)
999 Current = Integer;
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001000 }
1001
1002 // Classify the fields one at a time, merging the results.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001003 unsigned idx = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001004 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1005 i != e; ++i, ++idx) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001006 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
1007 bool BitField = i->isBitField();
1008
1009 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
1010 // fields, it has class MEMORY.
1011 //
1012 // Note, skip this test for bit-fields, see below.
1013 if (!BitField && Offset % Context.getTypeAlign(i->getType())) {
1014 Lo = Memory;
1015 return;
1016 }
1017
1018 // Classify this field.
1019 //
1020 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
1021 // exceeds a single eightbyte, each is classified
1022 // separately. Each eightbyte gets initialized to class
1023 // NO_CLASS.
1024 Class FieldLo, FieldHi;
1025
1026 // Bit-fields require special handling, they do not force the
1027 // structure to be passed in memory even if unaligned, and
1028 // therefore they can straddle an eightbyte.
1029 if (BitField) {
1030 // Ignore padding bit-fields.
1031 if (i->isUnnamedBitfield())
1032 continue;
1033
1034 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
1035 uint64_t Size = i->getBitWidth()->EvaluateAsInt(Context).getZExtValue();
1036
1037 uint64_t EB_Lo = Offset / 64;
1038 uint64_t EB_Hi = (Offset + Size - 1) / 64;
1039 FieldLo = FieldHi = NoClass;
1040 if (EB_Lo) {
1041 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
1042 FieldLo = NoClass;
1043 FieldHi = Integer;
1044 } else {
1045 FieldLo = Integer;
1046 FieldHi = EB_Hi ? Integer : NoClass;
1047 }
1048 } else
1049 classify(i->getType(), Context, Offset, FieldLo, FieldHi);
1050 Lo = merge(Lo, FieldLo);
1051 Hi = merge(Hi, FieldHi);
1052 if (Lo == Memory || Hi == Memory)
1053 break;
1054 }
1055
1056 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
1057 //
1058 // (a) If one of the classes is MEMORY, the whole argument is
1059 // passed in memory.
1060 //
1061 // (b) If SSEUP is not preceeded by SSE, it is converted to SSE.
1062
1063 // The first of these conditions is guaranteed by how we implement
1064 // the merge (just bail).
1065 //
1066 // The second condition occurs in the case of unions; for example
1067 // union { _Complex double; unsigned; }.
1068 if (Hi == Memory)
1069 Lo = Memory;
1070 if (Hi == SSEUp && Lo != SSE)
1071 Hi = SSE;
1072 }
1073}
1074
1075ABIArgInfo X86_64ABIInfo::getCoerceResult(QualType Ty,
1076 const llvm::Type *CoerceTo,
1077 ASTContext &Context) const {
Chris Lattner7f215c12010-06-26 21:52:32 +00001078 if (CoerceTo->isIntegerTy(64)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001079 // Integer and pointer types will end up in a general purpose
1080 // register.
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001081
1082 // Treat an enum type as its underlying type.
1083 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1084 Ty = EnumTy->getDecl()->getIntegerType();
1085
Douglas Gregor9d3347a2010-06-16 00:35:25 +00001086 if (Ty->isIntegralOrEnumerationType() || Ty->hasPointerRepresentation())
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001087 return (Ty->isPromotableIntegerType() ?
1088 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Chris Lattner7f215c12010-06-26 21:52:32 +00001089 } else if (CoerceTo->isDoubleTy()) {
John McCall0b0ef0a2010-02-24 07:14:12 +00001090 assert(Ty.isCanonical() && "should always have a canonical type here");
1091 assert(!Ty.hasQualifiers() && "should never have a qualified type here");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001092
1093 // Float and double end up in a single SSE reg.
John McCall0b0ef0a2010-02-24 07:14:12 +00001094 if (Ty == Context.FloatTy || Ty == Context.DoubleTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001095 return ABIArgInfo::getDirect();
1096
1097 }
1098
1099 return ABIArgInfo::getCoerce(CoerceTo);
1100}
1101
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001102ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty,
1103 ASTContext &Context) const {
1104 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1105 // place naturally.
1106 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
1107 // Treat an enum type as its underlying type.
1108 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1109 Ty = EnumTy->getDecl()->getIntegerType();
1110
1111 return (Ty->isPromotableIntegerType() ?
1112 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1113 }
1114
1115 return ABIArgInfo::getIndirect(0);
1116}
1117
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001118ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty,
1119 ASTContext &Context) const {
1120 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1121 // place naturally.
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001122 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
1123 // Treat an enum type as its underlying type.
1124 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1125 Ty = EnumTy->getDecl()->getIntegerType();
1126
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001127 return (Ty->isPromotableIntegerType() ?
1128 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001129 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001130
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001131 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
1132 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Anders Carlsson0a8f8472009-09-16 15:53:40 +00001133
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001134 // Compute the byval alignment. We trust the back-end to honor the
1135 // minimum ABI alignment for byval, to make cleaner IR.
1136 const unsigned MinABIAlign = 8;
1137 unsigned Align = Context.getTypeAlign(Ty) / 8;
1138 if (Align > MinABIAlign)
1139 return ABIArgInfo::getIndirect(Align);
1140 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001141}
1142
1143ABIArgInfo X86_64ABIInfo::classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001144 ASTContext &Context,
1145 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001146 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
1147 // classification algorithm.
1148 X86_64ABIInfo::Class Lo, Hi;
1149 classify(RetTy, Context, 0, Lo, Hi);
1150
1151 // Check some invariants.
1152 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
1153 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
1154 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1155
1156 const llvm::Type *ResType = 0;
1157 switch (Lo) {
1158 case NoClass:
1159 return ABIArgInfo::getIgnore();
1160
1161 case SSEUp:
1162 case X87Up:
1163 assert(0 && "Invalid classification for lo word.");
1164
1165 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
1166 // hidden argument.
1167 case Memory:
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001168 return getIndirectReturnResult(RetTy, Context);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001169
1170 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
1171 // available register of the sequence %rax, %rdx is used.
1172 case Integer:
Owen Anderson0032b272009-08-13 21:57:51 +00001173 ResType = llvm::Type::getInt64Ty(VMContext); break;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001174
1175 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
1176 // available SSE register of the sequence %xmm0, %xmm1 is used.
1177 case SSE:
Owen Anderson0032b272009-08-13 21:57:51 +00001178 ResType = llvm::Type::getDoubleTy(VMContext); break;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001179
1180 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
1181 // returned on the X87 stack in %st0 as 80-bit x87 number.
1182 case X87:
Owen Anderson0032b272009-08-13 21:57:51 +00001183 ResType = llvm::Type::getX86_FP80Ty(VMContext); break;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001184
1185 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
1186 // part of the value is returned in %st0 and the imaginary part in
1187 // %st1.
1188 case ComplexX87:
1189 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Chris Lattner52d9ae32010-04-06 17:29:22 +00001190 ResType = llvm::StructType::get(VMContext,
1191 llvm::Type::getX86_FP80Ty(VMContext),
Owen Anderson0032b272009-08-13 21:57:51 +00001192 llvm::Type::getX86_FP80Ty(VMContext),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001193 NULL);
1194 break;
1195 }
1196
1197 switch (Hi) {
1198 // Memory was handled previously and X87 should
1199 // never occur as a hi class.
1200 case Memory:
1201 case X87:
1202 assert(0 && "Invalid classification for hi word.");
1203
1204 case ComplexX87: // Previously handled.
1205 case NoClass: break;
1206
1207 case Integer:
Owen Anderson47a434f2009-08-05 23:18:46 +00001208 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson0032b272009-08-13 21:57:51 +00001209 llvm::Type::getInt64Ty(VMContext), NULL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001210 break;
1211 case SSE:
Owen Anderson47a434f2009-08-05 23:18:46 +00001212 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson0032b272009-08-13 21:57:51 +00001213 llvm::Type::getDoubleTy(VMContext), NULL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001214 break;
1215
1216 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
1217 // is passed in the upper half of the last used SSE register.
1218 //
1219 // SSEUP should always be preceeded by SSE, just widen.
1220 case SSEUp:
1221 assert(Lo == SSE && "Unexpected SSEUp classification.");
Owen Anderson0032b272009-08-13 21:57:51 +00001222 ResType = llvm::VectorType::get(llvm::Type::getDoubleTy(VMContext), 2);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001223 break;
1224
1225 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
1226 // returned together with the previous X87 value in %st0.
1227 case X87Up:
1228 // If X87Up is preceeded by X87, we don't need to do
1229 // anything. However, in some cases with unions it may not be
1230 // preceeded by X87. In such situations we follow gcc and pass the
1231 // extra bits in an SSE reg.
1232 if (Lo != X87)
Owen Anderson47a434f2009-08-05 23:18:46 +00001233 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson0032b272009-08-13 21:57:51 +00001234 llvm::Type::getDoubleTy(VMContext), NULL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001235 break;
1236 }
1237
1238 return getCoerceResult(RetTy, ResType, Context);
1239}
1240
1241ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, ASTContext &Context,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001242 llvm::LLVMContext &VMContext,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001243 unsigned &neededInt,
1244 unsigned &neededSSE) const {
1245 X86_64ABIInfo::Class Lo, Hi;
1246 classify(Ty, Context, 0, Lo, Hi);
1247
1248 // Check some invariants.
1249 // FIXME: Enforce these by construction.
1250 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
1251 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
1252 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1253
1254 neededInt = 0;
1255 neededSSE = 0;
1256 const llvm::Type *ResType = 0;
1257 switch (Lo) {
1258 case NoClass:
1259 return ABIArgInfo::getIgnore();
1260
1261 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
1262 // on the stack.
1263 case Memory:
1264
1265 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
1266 // COMPLEX_X87, it is passed in memory.
1267 case X87:
1268 case ComplexX87:
1269 return getIndirectResult(Ty, Context);
1270
1271 case SSEUp:
1272 case X87Up:
1273 assert(0 && "Invalid classification for lo word.");
1274
1275 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
1276 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
1277 // and %r9 is used.
1278 case Integer:
1279 ++neededInt;
Owen Anderson0032b272009-08-13 21:57:51 +00001280 ResType = llvm::Type::getInt64Ty(VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001281 break;
1282
1283 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
1284 // available SSE register is used, the registers are taken in the
1285 // order from %xmm0 to %xmm7.
1286 case SSE:
1287 ++neededSSE;
Owen Anderson0032b272009-08-13 21:57:51 +00001288 ResType = llvm::Type::getDoubleTy(VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001289 break;
1290 }
1291
1292 switch (Hi) {
1293 // Memory was handled previously, ComplexX87 and X87 should
1294 // never occur as hi classes, and X87Up must be preceed by X87,
1295 // which is passed in memory.
1296 case Memory:
1297 case X87:
1298 case ComplexX87:
1299 assert(0 && "Invalid classification for hi word.");
1300 break;
1301
1302 case NoClass: break;
1303 case Integer:
Owen Anderson47a434f2009-08-05 23:18:46 +00001304 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson0032b272009-08-13 21:57:51 +00001305 llvm::Type::getInt64Ty(VMContext), NULL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001306 ++neededInt;
1307 break;
1308
1309 // X87Up generally doesn't occur here (long double is passed in
1310 // memory), except in situations involving unions.
1311 case X87Up:
1312 case SSE:
Owen Anderson47a434f2009-08-05 23:18:46 +00001313 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson0032b272009-08-13 21:57:51 +00001314 llvm::Type::getDoubleTy(VMContext), NULL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001315 ++neededSSE;
1316 break;
1317
1318 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
1319 // eightbyte is passed in the upper half of the last used SSE
1320 // register.
1321 case SSEUp:
1322 assert(Lo == SSE && "Unexpected SSEUp classification.");
Owen Anderson0032b272009-08-13 21:57:51 +00001323 ResType = llvm::VectorType::get(llvm::Type::getDoubleTy(VMContext), 2);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001324 break;
1325 }
1326
1327 return getCoerceResult(Ty, ResType, Context);
1328}
1329
Owen Andersona1cf15f2009-07-14 23:10:40 +00001330void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1331 llvm::LLVMContext &VMContext) const {
1332 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(),
1333 Context, VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001334
1335 // Keep track of the number of assigned registers.
1336 unsigned freeIntRegs = 6, freeSSERegs = 8;
1337
1338 // If the return value is indirect, then the hidden argument is consuming one
1339 // integer register.
1340 if (FI.getReturnInfo().isIndirect())
1341 --freeIntRegs;
1342
1343 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
1344 // get assigned (in left-to-right order) for passing as follows...
1345 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1346 it != ie; ++it) {
1347 unsigned neededInt, neededSSE;
Mike Stump1eb44332009-09-09 15:08:12 +00001348 it->info = classifyArgumentType(it->type, Context, VMContext,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001349 neededInt, neededSSE);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001350
1351 // AMD64-ABI 3.2.3p3: If there are no registers available for any
1352 // eightbyte of an argument, the whole argument is passed on the
1353 // stack. If registers have already been assigned for some
1354 // eightbytes of such an argument, the assignments get reverted.
1355 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
1356 freeIntRegs -= neededInt;
1357 freeSSERegs -= neededSSE;
1358 } else {
1359 it->info = getIndirectResult(it->type, Context);
1360 }
1361 }
1362}
1363
1364static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
1365 QualType Ty,
1366 CodeGenFunction &CGF) {
1367 llvm::Value *overflow_arg_area_p =
1368 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
1369 llvm::Value *overflow_arg_area =
1370 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
1371
1372 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
1373 // byte boundary if alignment needed by type exceeds 8 byte boundary.
1374 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
1375 if (Align > 8) {
1376 // Note that we follow the ABI & gcc here, even though the type
1377 // could in theory have an alignment greater than 16. This case
1378 // shouldn't ever matter in practice.
1379
1380 // overflow_arg_area = (overflow_arg_area + 15) & ~15;
Owen Anderson0032b272009-08-13 21:57:51 +00001381 llvm::Value *Offset =
Chris Lattner77b89b82010-06-27 07:15:29 +00001382 llvm::ConstantInt::get(CGF.Int32Ty, 15);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001383 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
1384 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
Chris Lattner77b89b82010-06-27 07:15:29 +00001385 CGF.Int64Ty);
1386 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, ~15LL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001387 overflow_arg_area =
1388 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
1389 overflow_arg_area->getType(),
1390 "overflow_arg_area.align");
1391 }
1392
1393 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
1394 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1395 llvm::Value *Res =
1396 CGF.Builder.CreateBitCast(overflow_arg_area,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001397 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001398
1399 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
1400 // l->overflow_arg_area + sizeof(type).
1401 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
1402 // an 8 byte boundary.
1403
1404 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
Owen Anderson0032b272009-08-13 21:57:51 +00001405 llvm::Value *Offset =
Chris Lattner77b89b82010-06-27 07:15:29 +00001406 llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001407 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
1408 "overflow_arg_area.next");
1409 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
1410
1411 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
1412 return Res;
1413}
1414
1415llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1416 CodeGenFunction &CGF) const {
Owen Andersona1cf15f2009-07-14 23:10:40 +00001417 llvm::LLVMContext &VMContext = CGF.getLLVMContext();
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001418 const llvm::Type *DoubleTy = llvm::Type::getDoubleTy(VMContext);
Mike Stump1eb44332009-09-09 15:08:12 +00001419
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001420 // Assume that va_list type is correct; should be pointer to LLVM type:
1421 // struct {
1422 // i32 gp_offset;
1423 // i32 fp_offset;
1424 // i8* overflow_arg_area;
1425 // i8* reg_save_area;
1426 // };
1427 unsigned neededInt, neededSSE;
Chris Lattnera14db752010-03-11 18:19:55 +00001428
1429 Ty = CGF.getContext().getCanonicalType(Ty);
Owen Andersona1cf15f2009-07-14 23:10:40 +00001430 ABIArgInfo AI = classifyArgumentType(Ty, CGF.getContext(), VMContext,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001431 neededInt, neededSSE);
1432
1433 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
1434 // in the registers. If not go to step 7.
1435 if (!neededInt && !neededSSE)
1436 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1437
1438 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
1439 // general purpose registers needed to pass type and num_fp to hold
1440 // the number of floating point registers needed.
1441
1442 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
1443 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
1444 // l->fp_offset > 304 - num_fp * 16 go to step 7.
1445 //
1446 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
1447 // register save space).
1448
1449 llvm::Value *InRegs = 0;
1450 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
1451 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
1452 if (neededInt) {
1453 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
1454 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
1455 InRegs =
1456 CGF.Builder.CreateICmpULE(gp_offset,
Chris Lattner77b89b82010-06-27 07:15:29 +00001457 llvm::ConstantInt::get(CGF.Int32Ty,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001458 48 - neededInt * 8),
1459 "fits_in_gp");
1460 }
1461
1462 if (neededSSE) {
1463 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
1464 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
1465 llvm::Value *FitsInFP =
1466 CGF.Builder.CreateICmpULE(fp_offset,
Chris Lattner77b89b82010-06-27 07:15:29 +00001467 llvm::ConstantInt::get(CGF.Int32Ty,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001468 176 - neededSSE * 16),
1469 "fits_in_fp");
1470 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
1471 }
1472
1473 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
1474 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
1475 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
1476 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
1477
1478 // Emit code to load the value if it was passed in registers.
1479
1480 CGF.EmitBlock(InRegBlock);
1481
1482 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
1483 // an offset of l->gp_offset and/or l->fp_offset. This may require
1484 // copying to a temporary location in case the parameter is passed
1485 // in different register classes or requires an alignment greater
1486 // than 8 for general purpose registers and 16 for XMM registers.
1487 //
1488 // FIXME: This really results in shameful code when we end up needing to
1489 // collect arguments from different places; often what should result in a
1490 // simple assembling of a structure from scattered addresses has many more
1491 // loads than necessary. Can we clean this up?
1492 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1493 llvm::Value *RegAddr =
1494 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
1495 "reg_save_area");
1496 if (neededInt && neededSSE) {
1497 // FIXME: Cleanup.
1498 assert(AI.isCoerce() && "Unexpected ABI info for mixed regs");
1499 const llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
1500 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
1501 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
1502 const llvm::Type *TyLo = ST->getElementType(0);
1503 const llvm::Type *TyHi = ST->getElementType(1);
Duncan Sandsf177d9d2010-02-15 16:14:01 +00001504 assert((TyLo->isFloatingPointTy() ^ TyHi->isFloatingPointTy()) &&
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001505 "Unexpected ABI info for mixed regs");
Owen Anderson96e0fc72009-07-29 22:16:19 +00001506 const llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
1507 const llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001508 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1509 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Duncan Sandsf177d9d2010-02-15 16:14:01 +00001510 llvm::Value *RegLoAddr = TyLo->isFloatingPointTy() ? FPAddr : GPAddr;
1511 llvm::Value *RegHiAddr = TyLo->isFloatingPointTy() ? GPAddr : FPAddr;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001512 llvm::Value *V =
1513 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
1514 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1515 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
1516 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1517
Owen Andersona1cf15f2009-07-14 23:10:40 +00001518 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001519 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001520 } else if (neededInt) {
1521 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1522 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001523 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001524 } else {
1525 if (neededSSE == 1) {
1526 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1527 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001528 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001529 } else {
1530 assert(neededSSE == 2 && "Invalid number of needed registers!");
1531 // SSE registers are spaced 16 bytes apart in the register save
1532 // area, we need to collect the two eightbytes together.
1533 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1534 llvm::Value *RegAddrHi =
1535 CGF.Builder.CreateGEP(RegAddrLo,
Chris Lattner77b89b82010-06-27 07:15:29 +00001536 llvm::ConstantInt::get(CGF.Int32Ty, 16));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001537 const llvm::Type *DblPtrTy =
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001538 llvm::PointerType::getUnqual(DoubleTy);
1539 const llvm::StructType *ST = llvm::StructType::get(VMContext, DoubleTy,
1540 DoubleTy, NULL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001541 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
1542 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
1543 DblPtrTy));
1544 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1545 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
1546 DblPtrTy));
1547 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1548 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001549 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001550 }
1551 }
1552
1553 // AMD64-ABI 3.5.7p5: Step 5. Set:
1554 // l->gp_offset = l->gp_offset + num_gp * 8
1555 // l->fp_offset = l->fp_offset + num_fp * 16.
1556 if (neededInt) {
Chris Lattner77b89b82010-06-27 07:15:29 +00001557 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001558 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
1559 gp_offset_p);
1560 }
1561 if (neededSSE) {
Chris Lattner77b89b82010-06-27 07:15:29 +00001562 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001563 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
1564 fp_offset_p);
1565 }
1566 CGF.EmitBranch(ContBlock);
1567
1568 // Emit code to load the value if it was passed in memory.
1569
1570 CGF.EmitBlock(InMemBlock);
1571 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1572
1573 // Return the appropriate result.
1574
1575 CGF.EmitBlock(ContBlock);
1576 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(),
1577 "vaarg.addr");
1578 ResAddr->reserveOperandSpace(2);
1579 ResAddr->addIncoming(RegAddr, InRegBlock);
1580 ResAddr->addIncoming(MemAddr, InMemBlock);
1581
1582 return ResAddr;
1583}
1584
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001585// PIC16 ABI Implementation
1586
1587namespace {
1588
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001589class PIC16ABIInfo : public ABIInfo {
1590 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001591 ASTContext &Context,
1592 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001593
1594 ABIArgInfo classifyArgumentType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001595 ASTContext &Context,
1596 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001597
Owen Andersona1cf15f2009-07-14 23:10:40 +00001598 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1599 llvm::LLVMContext &VMContext) const {
1600 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context,
1601 VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001602 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1603 it != ie; ++it)
Owen Andersona1cf15f2009-07-14 23:10:40 +00001604 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001605 }
1606
1607 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1608 CodeGenFunction &CGF) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001609};
1610
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001611class PIC16TargetCodeGenInfo : public TargetCodeGenInfo {
1612public:
Douglas Gregor568bb2d2010-01-22 15:41:14 +00001613 PIC16TargetCodeGenInfo():TargetCodeGenInfo(new PIC16ABIInfo()) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001614};
1615
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001616}
1617
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001618ABIArgInfo PIC16ABIInfo::classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001619 ASTContext &Context,
1620 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001621 if (RetTy->isVoidType()) {
1622 return ABIArgInfo::getIgnore();
1623 } else {
1624 return ABIArgInfo::getDirect();
1625 }
1626}
1627
1628ABIArgInfo PIC16ABIInfo::classifyArgumentType(QualType Ty,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001629 ASTContext &Context,
1630 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001631 return ABIArgInfo::getDirect();
1632}
1633
1634llvm::Value *PIC16ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner77b89b82010-06-27 07:15:29 +00001635 CodeGenFunction &CGF) const {
Chris Lattner52d9ae32010-04-06 17:29:22 +00001636 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Sanjiv Guptaa446ecd2010-02-17 02:25:52 +00001637 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
1638
1639 CGBuilderTy &Builder = CGF.Builder;
1640 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
1641 "ap");
1642 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
1643 llvm::Type *PTy =
1644 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
1645 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
1646
1647 uint64_t Offset = CGF.getContext().getTypeSize(Ty) / 8;
1648
1649 llvm::Value *NextAddr =
1650 Builder.CreateGEP(Addr, llvm::ConstantInt::get(
1651 llvm::Type::getInt32Ty(CGF.getLLVMContext()), Offset),
1652 "ap.next");
1653 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
1654
1655 return AddrTyped;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001656}
1657
Sanjiv Guptaa446ecd2010-02-17 02:25:52 +00001658
John McCallec853ba2010-03-11 00:10:12 +00001659// PowerPC-32
1660
1661namespace {
1662class PPC32TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
1663public:
1664 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
1665 // This is recovered from gcc output.
1666 return 1; // r1 is the dedicated stack pointer
1667 }
1668
1669 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1670 llvm::Value *Address) const;
1671};
1672
1673}
1674
1675bool
1676PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1677 llvm::Value *Address) const {
1678 // This is calculated from the LLVM and GCC tables and verified
1679 // against gcc output. AFAIK all ABIs use the same encoding.
1680
1681 CodeGen::CGBuilderTy &Builder = CGF.Builder;
1682 llvm::LLVMContext &Context = CGF.getLLVMContext();
1683
1684 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
1685 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
1686 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
1687 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
1688
1689 // 0-31: r0-31, the 4-byte general-purpose registers
John McCallaeeb7012010-05-27 06:19:26 +00001690 AssignToArrayRange(Builder, Address, Four8, 0, 31);
John McCallec853ba2010-03-11 00:10:12 +00001691
1692 // 32-63: fp0-31, the 8-byte floating-point registers
John McCallaeeb7012010-05-27 06:19:26 +00001693 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
John McCallec853ba2010-03-11 00:10:12 +00001694
1695 // 64-76 are various 4-byte special-purpose registers:
1696 // 64: mq
1697 // 65: lr
1698 // 66: ctr
1699 // 67: ap
1700 // 68-75 cr0-7
1701 // 76: xer
John McCallaeeb7012010-05-27 06:19:26 +00001702 AssignToArrayRange(Builder, Address, Four8, 64, 76);
John McCallec853ba2010-03-11 00:10:12 +00001703
1704 // 77-108: v0-31, the 16-byte vector registers
John McCallaeeb7012010-05-27 06:19:26 +00001705 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
John McCallec853ba2010-03-11 00:10:12 +00001706
1707 // 109: vrsave
1708 // 110: vscr
1709 // 111: spe_acc
1710 // 112: spefscr
1711 // 113: sfp
John McCallaeeb7012010-05-27 06:19:26 +00001712 AssignToArrayRange(Builder, Address, Four8, 109, 113);
John McCallec853ba2010-03-11 00:10:12 +00001713
1714 return false;
1715}
1716
1717
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001718// ARM ABI Implementation
1719
1720namespace {
1721
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001722class ARMABIInfo : public ABIInfo {
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00001723public:
1724 enum ABIKind {
1725 APCS = 0,
1726 AAPCS = 1,
1727 AAPCS_VFP
1728 };
1729
1730private:
1731 ABIKind Kind;
1732
1733public:
1734 ARMABIInfo(ABIKind _Kind) : Kind(_Kind) {}
1735
1736private:
1737 ABIKind getABIKind() const { return Kind; }
1738
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001739 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001740 ASTContext &Context,
1741 llvm::LLVMContext &VMCOntext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001742
1743 ABIArgInfo classifyArgumentType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001744 ASTContext &Context,
1745 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001746
Owen Andersona1cf15f2009-07-14 23:10:40 +00001747 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1748 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001749
1750 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1751 CodeGenFunction &CGF) const;
1752};
1753
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001754class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
1755public:
1756 ARMTargetCodeGenInfo(ARMABIInfo::ABIKind K)
Douglas Gregor568bb2d2010-01-22 15:41:14 +00001757 :TargetCodeGenInfo(new ARMABIInfo(K)) {}
John McCall6374c332010-03-06 00:35:14 +00001758
1759 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
1760 return 13;
1761 }
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001762};
1763
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001764}
1765
Owen Andersona1cf15f2009-07-14 23:10:40 +00001766void ARMABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1767 llvm::LLVMContext &VMContext) const {
Mike Stump1eb44332009-09-09 15:08:12 +00001768 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001769 VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001770 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1771 it != ie; ++it) {
Owen Andersona1cf15f2009-07-14 23:10:40 +00001772 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001773 }
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00001774
Rafael Espindola25117ab2010-06-16 16:13:39 +00001775 const llvm::Triple &Triple(Context.Target.getTriple());
1776 llvm::CallingConv::ID DefaultCC;
Rafael Espindola1ed1a592010-06-16 19:01:17 +00001777 if (Triple.getEnvironmentName() == "gnueabi" ||
1778 Triple.getEnvironmentName() == "eabi")
Rafael Espindola25117ab2010-06-16 16:13:39 +00001779 DefaultCC = llvm::CallingConv::ARM_AAPCS;
Rafael Espindola1ed1a592010-06-16 19:01:17 +00001780 else
1781 DefaultCC = llvm::CallingConv::ARM_APCS;
Rafael Espindola25117ab2010-06-16 16:13:39 +00001782
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00001783 switch (getABIKind()) {
1784 case APCS:
Rafael Espindola25117ab2010-06-16 16:13:39 +00001785 if (DefaultCC != llvm::CallingConv::ARM_APCS)
1786 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_APCS);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00001787 break;
1788
1789 case AAPCS:
Rafael Espindola25117ab2010-06-16 16:13:39 +00001790 if (DefaultCC != llvm::CallingConv::ARM_AAPCS)
1791 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00001792 break;
1793
1794 case AAPCS_VFP:
1795 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS_VFP);
1796 break;
1797 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001798}
1799
1800ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001801 ASTContext &Context,
1802 llvm::LLVMContext &VMContext) const {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001803 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
1804 // Treat an enum type as its underlying type.
1805 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1806 Ty = EnumTy->getDecl()->getIntegerType();
1807
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001808 return (Ty->isPromotableIntegerType() ?
1809 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001810 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00001811
Daniel Dunbar42025572009-09-14 21:54:03 +00001812 // Ignore empty records.
1813 if (isEmptyRecord(Context, Ty, true))
1814 return ABIArgInfo::getIgnore();
1815
Rafael Espindola0eb1d972010-06-08 02:42:08 +00001816 // Structures with either a non-trivial destructor or a non-trivial
1817 // copy constructor are always indirect.
1818 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
1819 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
1820
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001821 // FIXME: This is kind of nasty... but there isn't much choice because the ARM
1822 // backend doesn't support byval.
1823 // FIXME: This doesn't handle alignment > 64 bits.
1824 const llvm::Type* ElemTy;
1825 unsigned SizeRegs;
1826 if (Context.getTypeAlign(Ty) > 32) {
Owen Anderson0032b272009-08-13 21:57:51 +00001827 ElemTy = llvm::Type::getInt64Ty(VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001828 SizeRegs = (Context.getTypeSize(Ty) + 63) / 64;
1829 } else {
Owen Anderson0032b272009-08-13 21:57:51 +00001830 ElemTy = llvm::Type::getInt32Ty(VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001831 SizeRegs = (Context.getTypeSize(Ty) + 31) / 32;
1832 }
1833 std::vector<const llvm::Type*> LLVMFields;
Owen Anderson96e0fc72009-07-29 22:16:19 +00001834 LLVMFields.push_back(llvm::ArrayType::get(ElemTy, SizeRegs));
Owen Anderson47a434f2009-08-05 23:18:46 +00001835 const llvm::Type* STy = llvm::StructType::get(VMContext, LLVMFields, true);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001836 return ABIArgInfo::getCoerce(STy);
1837}
1838
Daniel Dunbar98303b92009-09-13 08:03:58 +00001839static bool isIntegerLikeType(QualType Ty,
1840 ASTContext &Context,
1841 llvm::LLVMContext &VMContext) {
1842 // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
1843 // is called integer-like if its size is less than or equal to one word, and
1844 // the offset of each of its addressable sub-fields is zero.
1845
1846 uint64_t Size = Context.getTypeSize(Ty);
1847
1848 // Check that the type fits in a word.
1849 if (Size > 32)
1850 return false;
1851
1852 // FIXME: Handle vector types!
1853 if (Ty->isVectorType())
1854 return false;
1855
Daniel Dunbarb0d58192009-09-14 02:20:34 +00001856 // Float types are never treated as "integer like".
1857 if (Ty->isRealFloatingType())
1858 return false;
1859
Daniel Dunbar98303b92009-09-13 08:03:58 +00001860 // If this is a builtin or pointer type then it is ok.
John McCall183700f2009-09-21 23:43:11 +00001861 if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
Daniel Dunbar98303b92009-09-13 08:03:58 +00001862 return true;
1863
Daniel Dunbar45815812010-02-01 23:31:26 +00001864 // Small complex integer types are "integer like".
1865 if (const ComplexType *CT = Ty->getAs<ComplexType>())
1866 return isIntegerLikeType(CT->getElementType(), Context, VMContext);
Daniel Dunbar98303b92009-09-13 08:03:58 +00001867
1868 // Single element and zero sized arrays should be allowed, by the definition
1869 // above, but they are not.
1870
1871 // Otherwise, it must be a record type.
1872 const RecordType *RT = Ty->getAs<RecordType>();
1873 if (!RT) return false;
1874
1875 // Ignore records with flexible arrays.
1876 const RecordDecl *RD = RT->getDecl();
1877 if (RD->hasFlexibleArrayMember())
1878 return false;
1879
1880 // Check that all sub-fields are at offset 0, and are themselves "integer
1881 // like".
1882 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
1883
1884 bool HadField = false;
1885 unsigned idx = 0;
1886 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1887 i != e; ++i, ++idx) {
1888 const FieldDecl *FD = *i;
1889
Daniel Dunbar679855a2010-01-29 03:22:29 +00001890 // Bit-fields are not addressable, we only need to verify they are "integer
1891 // like". We still have to disallow a subsequent non-bitfield, for example:
1892 // struct { int : 0; int x }
1893 // is non-integer like according to gcc.
1894 if (FD->isBitField()) {
1895 if (!RD->isUnion())
1896 HadField = true;
Daniel Dunbar98303b92009-09-13 08:03:58 +00001897
Daniel Dunbar679855a2010-01-29 03:22:29 +00001898 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
1899 return false;
Daniel Dunbar98303b92009-09-13 08:03:58 +00001900
Daniel Dunbar679855a2010-01-29 03:22:29 +00001901 continue;
Daniel Dunbar98303b92009-09-13 08:03:58 +00001902 }
1903
Daniel Dunbar679855a2010-01-29 03:22:29 +00001904 // Check if this field is at offset 0.
1905 if (Layout.getFieldOffset(idx) != 0)
1906 return false;
1907
Daniel Dunbar98303b92009-09-13 08:03:58 +00001908 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
1909 return false;
1910
Daniel Dunbar679855a2010-01-29 03:22:29 +00001911 // Only allow at most one field in a structure. This doesn't match the
1912 // wording above, but follows gcc in situations with a field following an
1913 // empty structure.
Daniel Dunbar98303b92009-09-13 08:03:58 +00001914 if (!RD->isUnion()) {
1915 if (HadField)
1916 return false;
1917
1918 HadField = true;
1919 }
1920 }
1921
1922 return true;
1923}
1924
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001925ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001926 ASTContext &Context,
1927 llvm::LLVMContext &VMContext) const {
Daniel Dunbar98303b92009-09-13 08:03:58 +00001928 if (RetTy->isVoidType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001929 return ABIArgInfo::getIgnore();
Daniel Dunbar98303b92009-09-13 08:03:58 +00001930
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001931 if (!CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1932 // Treat an enum type as its underlying type.
1933 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
1934 RetTy = EnumTy->getDecl()->getIntegerType();
1935
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001936 return (RetTy->isPromotableIntegerType() ?
1937 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001938 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00001939
Rafael Espindola0eb1d972010-06-08 02:42:08 +00001940 // Structures with either a non-trivial destructor or a non-trivial
1941 // copy constructor are always indirect.
1942 if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
1943 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
1944
Daniel Dunbar98303b92009-09-13 08:03:58 +00001945 // Are we following APCS?
1946 if (getABIKind() == APCS) {
1947 if (isEmptyRecord(Context, RetTy, false))
1948 return ABIArgInfo::getIgnore();
1949
Daniel Dunbar4cc753f2010-02-01 23:31:19 +00001950 // Complex types are all returned as packed integers.
1951 //
1952 // FIXME: Consider using 2 x vector types if the back end handles them
1953 // correctly.
1954 if (RetTy->isAnyComplexType())
1955 return ABIArgInfo::getCoerce(llvm::IntegerType::get(
1956 VMContext, Context.getTypeSize(RetTy)));
1957
Daniel Dunbar98303b92009-09-13 08:03:58 +00001958 // Integer like structures are returned in r0.
1959 if (isIntegerLikeType(RetTy, Context, VMContext)) {
1960 // Return in the smallest viable integer type.
1961 uint64_t Size = Context.getTypeSize(RetTy);
1962 if (Size <= 8)
1963 return ABIArgInfo::getCoerce(llvm::Type::getInt8Ty(VMContext));
1964 if (Size <= 16)
1965 return ABIArgInfo::getCoerce(llvm::Type::getInt16Ty(VMContext));
1966 return ABIArgInfo::getCoerce(llvm::Type::getInt32Ty(VMContext));
1967 }
1968
1969 // Otherwise return in memory.
1970 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001971 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00001972
1973 // Otherwise this is an AAPCS variant.
1974
Daniel Dunbar16a08082009-09-14 00:56:55 +00001975 if (isEmptyRecord(Context, RetTy, true))
1976 return ABIArgInfo::getIgnore();
1977
Daniel Dunbar98303b92009-09-13 08:03:58 +00001978 // Aggregates <= 4 bytes are returned in r0; other aggregates
1979 // are returned indirectly.
1980 uint64_t Size = Context.getTypeSize(RetTy);
Daniel Dunbar16a08082009-09-14 00:56:55 +00001981 if (Size <= 32) {
1982 // Return in the smallest viable integer type.
1983 if (Size <= 8)
1984 return ABIArgInfo::getCoerce(llvm::Type::getInt8Ty(VMContext));
1985 if (Size <= 16)
1986 return ABIArgInfo::getCoerce(llvm::Type::getInt16Ty(VMContext));
Daniel Dunbar98303b92009-09-13 08:03:58 +00001987 return ABIArgInfo::getCoerce(llvm::Type::getInt32Ty(VMContext));
Daniel Dunbar16a08082009-09-14 00:56:55 +00001988 }
1989
Daniel Dunbar98303b92009-09-13 08:03:58 +00001990 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001991}
1992
1993llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner77b89b82010-06-27 07:15:29 +00001994 CodeGenFunction &CGF) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001995 // FIXME: Need to handle alignment
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00001996 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Owen Anderson96e0fc72009-07-29 22:16:19 +00001997 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001998
1999 CGBuilderTy &Builder = CGF.Builder;
2000 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2001 "ap");
2002 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2003 llvm::Type *PTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00002004 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002005 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2006
2007 uint64_t Offset =
2008 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
2009 llvm::Value *NextAddr =
Chris Lattner77b89b82010-06-27 07:15:29 +00002010 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002011 "ap.next");
2012 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2013
2014 return AddrTyped;
2015}
2016
2017ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00002018 ASTContext &Context,
2019 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002020 if (RetTy->isVoidType()) {
2021 return ABIArgInfo::getIgnore();
2022 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
2023 return ABIArgInfo::getIndirect(0);
2024 } else {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00002025 // Treat an enum type as its underlying type.
2026 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2027 RetTy = EnumTy->getDecl()->getIntegerType();
2028
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00002029 return (RetTy->isPromotableIntegerType() ?
2030 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002031 }
2032}
2033
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002034// SystemZ ABI Implementation
2035
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002036namespace {
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002037
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002038class SystemZABIInfo : public ABIInfo {
2039 bool isPromotableIntegerType(QualType Ty) const;
2040
2041 ABIArgInfo classifyReturnType(QualType RetTy, ASTContext &Context,
2042 llvm::LLVMContext &VMContext) const;
2043
2044 ABIArgInfo classifyArgumentType(QualType RetTy, ASTContext &Context,
2045 llvm::LLVMContext &VMContext) const;
2046
2047 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
2048 llvm::LLVMContext &VMContext) const {
2049 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(),
2050 Context, VMContext);
2051 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2052 it != ie; ++it)
2053 it->info = classifyArgumentType(it->type, Context, VMContext);
2054 }
2055
2056 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2057 CodeGenFunction &CGF) const;
2058};
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002059
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002060class SystemZTargetCodeGenInfo : public TargetCodeGenInfo {
2061public:
Douglas Gregor568bb2d2010-01-22 15:41:14 +00002062 SystemZTargetCodeGenInfo():TargetCodeGenInfo(new SystemZABIInfo()) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002063};
2064
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002065}
2066
2067bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const {
2068 // SystemZ ABI requires all 8, 16 and 32 bit quantities to be extended.
John McCall183700f2009-09-21 23:43:11 +00002069 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002070 switch (BT->getKind()) {
2071 case BuiltinType::Bool:
2072 case BuiltinType::Char_S:
2073 case BuiltinType::Char_U:
2074 case BuiltinType::SChar:
2075 case BuiltinType::UChar:
2076 case BuiltinType::Short:
2077 case BuiltinType::UShort:
2078 case BuiltinType::Int:
2079 case BuiltinType::UInt:
2080 return true;
2081 default:
2082 return false;
2083 }
2084 return false;
2085}
2086
2087llvm::Value *SystemZABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2088 CodeGenFunction &CGF) const {
2089 // FIXME: Implement
2090 return 0;
2091}
2092
2093
2094ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy,
2095 ASTContext &Context,
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002096 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002097 if (RetTy->isVoidType()) {
2098 return ABIArgInfo::getIgnore();
2099 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
2100 return ABIArgInfo::getIndirect(0);
2101 } else {
2102 return (isPromotableIntegerType(RetTy) ?
2103 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2104 }
2105}
2106
2107ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty,
2108 ASTContext &Context,
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002109 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002110 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
2111 return ABIArgInfo::getIndirect(0);
2112 } else {
2113 return (isPromotableIntegerType(Ty) ?
2114 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2115 }
2116}
2117
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002118// MSP430 ABI Implementation
2119
2120namespace {
2121
2122class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
2123public:
Douglas Gregor568bb2d2010-01-22 15:41:14 +00002124 MSP430TargetCodeGenInfo():TargetCodeGenInfo(new DefaultABIInfo()) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002125 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2126 CodeGen::CodeGenModule &M) const;
2127};
2128
2129}
2130
2131void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
2132 llvm::GlobalValue *GV,
2133 CodeGen::CodeGenModule &M) const {
2134 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
2135 if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) {
2136 // Handle 'interrupt' attribute:
2137 llvm::Function *F = cast<llvm::Function>(GV);
2138
2139 // Step 1: Set ISR calling convention.
2140 F->setCallingConv(llvm::CallingConv::MSP430_INTR);
2141
2142 // Step 2: Add attributes goodness.
2143 F->addFnAttr(llvm::Attribute::NoInline);
2144
2145 // Step 3: Emit ISR vector alias.
2146 unsigned Num = attr->getNumber() + 0xffe0;
2147 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
2148 "vector_" +
2149 llvm::LowercaseString(llvm::utohexstr(Num)),
2150 GV, &M.getModule());
2151 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002152 }
2153}
2154
John McCallaeeb7012010-05-27 06:19:26 +00002155// MIPS ABI Implementation. This works for both little-endian and
2156// big-endian variants.
2157namespace {
2158class MIPSTargetCodeGenInfo : public TargetCodeGenInfo {
2159public:
2160 MIPSTargetCodeGenInfo(): TargetCodeGenInfo(new DefaultABIInfo()) {}
2161
2162 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
2163 return 29;
2164 }
2165
2166 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2167 llvm::Value *Address) const;
2168};
2169}
2170
2171bool
2172MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2173 llvm::Value *Address) const {
2174 // This information comes from gcc's implementation, which seems to
2175 // as canonical as it gets.
2176
2177 CodeGen::CGBuilderTy &Builder = CGF.Builder;
2178 llvm::LLVMContext &Context = CGF.getLLVMContext();
2179
2180 // Everything on MIPS is 4 bytes. Double-precision FP registers
2181 // are aliased to pairs of single-precision FP registers.
2182 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
2183 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2184
2185 // 0-31 are the general purpose registers, $0 - $31.
2186 // 32-63 are the floating-point registers, $f0 - $f31.
2187 // 64 and 65 are the multiply/divide registers, $hi and $lo.
2188 // 66 is the (notional, I think) register for signal-handler return.
2189 AssignToArrayRange(Builder, Address, Four8, 0, 65);
2190
2191 // 67-74 are the floating-point status registers, $fcc0 - $fcc7.
2192 // They are one bit wide and ignored here.
2193
2194 // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31.
2195 // (coprocessor 1 is the FP unit)
2196 // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31.
2197 // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31.
2198 // 176-181 are the DSP accumulator registers.
2199 AssignToArrayRange(Builder, Address, Four8, 80, 181);
2200
2201 return false;
2202}
2203
2204
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002205const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() const {
2206 if (TheTargetCodeGenInfo)
2207 return *TheTargetCodeGenInfo;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002208
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002209 // For now we just cache the TargetCodeGenInfo in CodeGenModule and don't
2210 // free it.
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002211
Daniel Dunbar1752ee42009-08-24 09:10:05 +00002212 const llvm::Triple &Triple(getContext().Target.getTriple());
2213 switch (Triple.getArch()) {
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002214 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002215 return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo);
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002216
John McCallaeeb7012010-05-27 06:19:26 +00002217 case llvm::Triple::mips:
2218 case llvm::Triple::mipsel:
2219 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo());
2220
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002221 case llvm::Triple::arm:
2222 case llvm::Triple::thumb:
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002223 // FIXME: We want to know the float calling convention as well.
Daniel Dunbar018ba5a2009-09-14 00:35:03 +00002224 if (strcmp(getContext().Target.getABI(), "apcs-gnu") == 0)
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002225 return *(TheTargetCodeGenInfo =
2226 new ARMTargetCodeGenInfo(ARMABIInfo::APCS));
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002227
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002228 return *(TheTargetCodeGenInfo =
2229 new ARMTargetCodeGenInfo(ARMABIInfo::AAPCS));
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002230
2231 case llvm::Triple::pic16:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002232 return *(TheTargetCodeGenInfo = new PIC16TargetCodeGenInfo());
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002233
John McCallec853ba2010-03-11 00:10:12 +00002234 case llvm::Triple::ppc:
2235 return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo());
2236
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002237 case llvm::Triple::systemz:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002238 return *(TheTargetCodeGenInfo = new SystemZTargetCodeGenInfo());
2239
2240 case llvm::Triple::msp430:
2241 return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo());
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002242
Daniel Dunbar1752ee42009-08-24 09:10:05 +00002243 case llvm::Triple::x86:
Daniel Dunbar1752ee42009-08-24 09:10:05 +00002244 switch (Triple.getOS()) {
Edward O'Callaghan7ee68bd2009-10-20 17:22:50 +00002245 case llvm::Triple::Darwin:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002246 return *(TheTargetCodeGenInfo =
2247 new X86_32TargetCodeGenInfo(Context, true, true));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002248 case llvm::Triple::Cygwin:
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002249 case llvm::Triple::MinGW32:
2250 case llvm::Triple::MinGW64:
Edward O'Callaghan727e2682009-10-21 11:58:24 +00002251 case llvm::Triple::AuroraUX:
2252 case llvm::Triple::DragonFly:
David Chisnall75c135a2009-09-03 01:48:05 +00002253 case llvm::Triple::FreeBSD:
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002254 case llvm::Triple::OpenBSD:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002255 return *(TheTargetCodeGenInfo =
2256 new X86_32TargetCodeGenInfo(Context, false, true));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002257
2258 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002259 return *(TheTargetCodeGenInfo =
2260 new X86_32TargetCodeGenInfo(Context, false, false));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002261 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002262
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002263 case llvm::Triple::x86_64:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002264 return *(TheTargetCodeGenInfo = new X86_64TargetCodeGenInfo());
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002265 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002266}