blob: ea538e395fc63c7f3a8e999e5095aa4767e266a9 [file] [log] [blame]
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001//===---- TargetInfo.cpp - Encapsulate target details -----------*- C++ -*-===//
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// These classes wrap the information about a call or function
11// definition used to handle ABI compliancy.
12//
13//===----------------------------------------------------------------------===//
14
Anton Korobeynikov82d0a412010-01-10 12:58:08 +000015#include "TargetInfo.h"
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000016#include "ABIInfo.h"
17#include "CodeGenFunction.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000018#include "clang/AST/RecordLayout.h"
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000019#include "llvm/Type.h"
Anton Korobeynikov82d0a412010-01-10 12:58:08 +000020#include "llvm/ADT/StringExtras.h"
Daniel Dunbar2c0843f2009-08-24 08:52:16 +000021#include "llvm/ADT/Triple.h"
Daniel Dunbar28df7a52009-12-03 09:13:49 +000022#include "llvm/Support/raw_ostream.h"
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000023using namespace clang;
24using namespace CodeGen;
25
John McCallaeeb7012010-05-27 06:19:26 +000026static void AssignToArrayRange(CodeGen::CGBuilderTy &Builder,
27 llvm::Value *Array,
28 llvm::Value *Value,
29 unsigned FirstIndex,
30 unsigned LastIndex) {
31 // Alternatively, we could emit this as a loop in the source.
32 for (unsigned I = FirstIndex; I <= LastIndex; ++I) {
33 llvm::Value *Cell = Builder.CreateConstInBoundsGEP1_32(Array, I);
34 Builder.CreateStore(Value, Cell);
35 }
36}
37
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000038ABIInfo::~ABIInfo() {}
39
40void ABIArgInfo::dump() const {
Daniel Dunbar28df7a52009-12-03 09:13:49 +000041 llvm::raw_ostream &OS = llvm::errs();
42 OS << "(ABIArgInfo Kind=";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000043 switch (TheKind) {
44 case Direct:
Daniel Dunbar28df7a52009-12-03 09:13:49 +000045 OS << "Direct";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000046 break;
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +000047 case Extend:
Daniel Dunbar28df7a52009-12-03 09:13:49 +000048 OS << "Extend";
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +000049 break;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000050 case Ignore:
Daniel Dunbar28df7a52009-12-03 09:13:49 +000051 OS << "Ignore";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000052 break;
53 case Coerce:
Daniel Dunbar28df7a52009-12-03 09:13:49 +000054 OS << "Coerce Type=";
55 getCoerceToType()->print(OS);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000056 break;
57 case Indirect:
Daniel Dunbardc6d5742010-04-21 19:10:51 +000058 OS << "Indirect Align=" << getIndirectAlign()
59 << " Byal=" << getIndirectByVal();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000060 break;
61 case Expand:
Daniel Dunbar28df7a52009-12-03 09:13:49 +000062 OS << "Expand";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000063 break;
64 }
Daniel Dunbar28df7a52009-12-03 09:13:49 +000065 OS << ")\n";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000066}
67
Anton Korobeynikov82d0a412010-01-10 12:58:08 +000068TargetCodeGenInfo::~TargetCodeGenInfo() { delete Info; }
69
Daniel Dunbar98303b92009-09-13 08:03:58 +000070static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000071
72/// isEmptyField - Return true iff a the field is "empty", that is it
73/// is an unnamed bit-field or an (array of) empty record(s).
Daniel Dunbar98303b92009-09-13 08:03:58 +000074static bool isEmptyField(ASTContext &Context, const FieldDecl *FD,
75 bool AllowArrays) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000076 if (FD->isUnnamedBitfield())
77 return true;
78
79 QualType FT = FD->getType();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000080
Daniel Dunbar98303b92009-09-13 08:03:58 +000081 // Constant arrays of empty records count as empty, strip them off.
82 if (AllowArrays)
83 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT))
84 FT = AT->getElementType();
85
Daniel Dunbar5ea68612010-05-17 16:46:00 +000086 const RecordType *RT = FT->getAs<RecordType>();
87 if (!RT)
88 return false;
89
90 // C++ record fields are never empty, at least in the Itanium ABI.
91 //
92 // FIXME: We should use a predicate for whether this behavior is true in the
93 // current ABI.
94 if (isa<CXXRecordDecl>(RT->getDecl()))
95 return false;
96
Daniel Dunbar98303b92009-09-13 08:03:58 +000097 return isEmptyRecord(Context, FT, AllowArrays);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000098}
99
100/// isEmptyRecord - Return true iff a structure contains only empty
101/// fields. Note that a structure with a flexible array member is not
102/// considered empty.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000103static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000104 const RecordType *RT = T->getAs<RecordType>();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000105 if (!RT)
106 return 0;
107 const RecordDecl *RD = RT->getDecl();
108 if (RD->hasFlexibleArrayMember())
109 return false;
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000110
111 // If this is a C++ record, check the bases first.
112 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
113 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
114 e = CXXRD->bases_end(); i != e; ++i)
115 if (!isEmptyRecord(Context, i->getType(), true))
116 return false;
117
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000118 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
119 i != e; ++i)
Daniel Dunbar98303b92009-09-13 08:03:58 +0000120 if (!isEmptyField(Context, *i, AllowArrays))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000121 return false;
122 return true;
123}
124
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000125/// hasNonTrivialDestructorOrCopyConstructor - Determine if a type has either
126/// a non-trivial destructor or a non-trivial copy constructor.
127static bool hasNonTrivialDestructorOrCopyConstructor(const RecordType *RT) {
128 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
129 if (!RD)
130 return false;
131
132 return !RD->hasTrivialDestructor() || !RD->hasTrivialCopyConstructor();
133}
134
135/// isRecordWithNonTrivialDestructorOrCopyConstructor - Determine if a type is
136/// a record type with either a non-trivial destructor or a non-trivial copy
137/// constructor.
138static bool isRecordWithNonTrivialDestructorOrCopyConstructor(QualType T) {
139 const RecordType *RT = T->getAs<RecordType>();
140 if (!RT)
141 return false;
142
143 return hasNonTrivialDestructorOrCopyConstructor(RT);
144}
145
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000146/// isSingleElementStruct - Determine if a structure is a "single
147/// element struct", i.e. it has exactly one non-empty field or
148/// exactly one field which is itself a single element
149/// struct. Structures with flexible array members are never
150/// considered single element structs.
151///
152/// \return The field declaration for the single non-empty field, if
153/// it exists.
154static const Type *isSingleElementStruct(QualType T, ASTContext &Context) {
155 const RecordType *RT = T->getAsStructureType();
156 if (!RT)
157 return 0;
158
159 const RecordDecl *RD = RT->getDecl();
160 if (RD->hasFlexibleArrayMember())
161 return 0;
162
163 const Type *Found = 0;
Daniel Dunbar9430d5a2010-05-11 21:15:36 +0000164
165 // If this is a C++ record, check the bases first.
166 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
167 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
168 e = CXXRD->bases_end(); i != e; ++i) {
Daniel Dunbar9430d5a2010-05-11 21:15:36 +0000169 // Ignore empty records.
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000170 if (isEmptyRecord(Context, i->getType(), true))
Daniel Dunbar9430d5a2010-05-11 21:15:36 +0000171 continue;
172
173 // If we already found an element then this isn't a single-element struct.
174 if (Found)
175 return 0;
176
177 // If this is non-empty and not a single element struct, the composite
178 // cannot be a single element struct.
179 Found = isSingleElementStruct(i->getType(), Context);
180 if (!Found)
181 return 0;
182 }
183 }
184
185 // Check for single element.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000186 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
187 i != e; ++i) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000188 const FieldDecl *FD = *i;
189 QualType FT = FD->getType();
190
191 // Ignore empty fields.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000192 if (isEmptyField(Context, FD, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000193 continue;
194
195 // If we already found an element then this isn't a single-element
196 // struct.
197 if (Found)
198 return 0;
199
200 // Treat single element arrays as the element.
201 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
202 if (AT->getSize().getZExtValue() != 1)
203 break;
204 FT = AT->getElementType();
205 }
206
207 if (!CodeGenFunction::hasAggregateLLVMType(FT)) {
208 Found = FT.getTypePtr();
209 } else {
210 Found = isSingleElementStruct(FT, Context);
211 if (!Found)
212 return 0;
213 }
214 }
215
216 return Found;
217}
218
219static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
Daniel Dunbara1842d32010-05-14 03:40:53 +0000220 if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() &&
Daniel Dunbar55e59e12009-09-24 05:12:36 +0000221 !Ty->isAnyComplexType() && !Ty->isEnumeralType() &&
222 !Ty->isBlockPointerType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000223 return false;
224
225 uint64_t Size = Context.getTypeSize(Ty);
226 return Size == 32 || Size == 64;
227}
228
Daniel Dunbar53012f42009-11-09 01:33:53 +0000229/// canExpandIndirectArgument - Test whether an argument type which is to be
230/// passed indirectly (on the stack) would have the equivalent layout if it was
231/// expanded into separate arguments. If so, we prefer to do the latter to avoid
232/// inhibiting optimizations.
233///
234// FIXME: This predicate is missing many cases, currently it just follows
235// llvm-gcc (checks that all fields are 32-bit or 64-bit primitive types). We
236// should probably make this smarter, or better yet make the LLVM backend
237// capable of handling it.
238static bool canExpandIndirectArgument(QualType Ty, ASTContext &Context) {
239 // We can only expand structure types.
240 const RecordType *RT = Ty->getAs<RecordType>();
241 if (!RT)
242 return false;
243
244 // We can only expand (C) structures.
245 //
246 // FIXME: This needs to be generalized to handle classes as well.
247 const RecordDecl *RD = RT->getDecl();
248 if (!RD->isStruct() || isa<CXXRecordDecl>(RD))
249 return false;
250
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000251 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
252 i != e; ++i) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000253 const FieldDecl *FD = *i;
254
255 if (!is32Or64BitBasicType(FD->getType(), Context))
256 return false;
257
258 // FIXME: Reject bit-fields wholesale; there are two problems, we don't know
259 // how to expand them yet, and the predicate for telling if a bitfield still
260 // counts as "basic" is more complicated than what we were doing previously.
261 if (FD->isBitField())
262 return false;
263 }
264
265 return true;
266}
267
268namespace {
269/// DefaultABIInfo - The default implementation for ABI specific
270/// details. This implementation provides information which results in
271/// self-consistent and sensible LLVM IR generation, but does not
272/// conform to any particular ABI.
273class DefaultABIInfo : public ABIInfo {
274 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000275 ASTContext &Context,
276 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000277
278 ABIArgInfo classifyArgumentType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000279 ASTContext &Context,
280 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000281
Owen Andersona1cf15f2009-07-14 23:10:40 +0000282 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
283 llvm::LLVMContext &VMContext) const {
284 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context,
285 VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000286 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
287 it != ie; ++it)
Owen Andersona1cf15f2009-07-14 23:10:40 +0000288 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000289 }
290
291 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
292 CodeGenFunction &CGF) const;
293};
294
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000295class DefaultTargetCodeGenInfo : public TargetCodeGenInfo {
296public:
Douglas Gregor568bb2d2010-01-22 15:41:14 +0000297 DefaultTargetCodeGenInfo():TargetCodeGenInfo(new DefaultABIInfo()) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000298};
299
300llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
301 CodeGenFunction &CGF) const {
302 return 0;
303}
304
305ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty,
306 ASTContext &Context,
307 llvm::LLVMContext &VMContext) const {
Chris Lattnera14db752010-03-11 18:19:55 +0000308 if (CodeGenFunction::hasAggregateLLVMType(Ty))
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000309 return ABIArgInfo::getIndirect(0);
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000310
Chris Lattnera14db752010-03-11 18:19:55 +0000311 // Treat an enum type as its underlying type.
312 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
313 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregoraa74a1e2010-02-02 20:10:50 +0000314
Chris Lattnera14db752010-03-11 18:19:55 +0000315 return (Ty->isPromotableIntegerType() ?
316 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000317}
318
Chris Lattnerdce5ad02010-06-28 20:05:43 +0000319//===----------------------------------------------------------------------===//
320// X86-32 ABI Implementation
321//===----------------------------------------------------------------------===//
322
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000323/// X86_32ABIInfo - The X86-32 ABI information.
324class X86_32ABIInfo : public ABIInfo {
325 ASTContext &Context;
David Chisnall1e4249c2009-08-17 23:08:21 +0000326 bool IsDarwinVectorABI;
327 bool IsSmallStructInRegABI;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000328
329 static bool isRegisterSize(unsigned Size) {
330 return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
331 }
332
333 static bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context);
334
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000335 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
336 /// such that the argument will be passed in memory.
337 ABIArgInfo getIndirectResult(QualType Ty, ASTContext &Context,
338 bool ByVal = true) const;
339
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000340public:
341 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000342 ASTContext &Context,
343 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000344
345 ABIArgInfo classifyArgumentType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000346 ASTContext &Context,
347 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000348
Owen Andersona1cf15f2009-07-14 23:10:40 +0000349 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
350 llvm::LLVMContext &VMContext) const {
351 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context,
352 VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000353 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
354 it != ie; ++it)
Owen Andersona1cf15f2009-07-14 23:10:40 +0000355 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000356 }
357
358 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
359 CodeGenFunction &CGF) const;
360
David Chisnall1e4249c2009-08-17 23:08:21 +0000361 X86_32ABIInfo(ASTContext &Context, bool d, bool p)
Mike Stump1eb44332009-09-09 15:08:12 +0000362 : ABIInfo(), Context(Context), IsDarwinVectorABI(d),
David Chisnall1e4249c2009-08-17 23:08:21 +0000363 IsSmallStructInRegABI(p) {}
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000364};
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000365
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000366class X86_32TargetCodeGenInfo : public TargetCodeGenInfo {
367public:
368 X86_32TargetCodeGenInfo(ASTContext &Context, bool d, bool p)
Douglas Gregor568bb2d2010-01-22 15:41:14 +0000369 :TargetCodeGenInfo(new X86_32ABIInfo(Context, d, p)) {}
Charles Davis74f72932010-02-13 15:54:06 +0000370
371 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
372 CodeGen::CodeGenModule &CGM) const;
John McCall6374c332010-03-06 00:35:14 +0000373
374 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
375 // Darwin uses different dwarf register numbers for EH.
376 if (CGM.isTargetDarwin()) return 5;
377
378 return 4;
379 }
380
381 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
382 llvm::Value *Address) const;
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000383};
384
385}
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000386
387/// shouldReturnTypeInRegister - Determine if the given type should be
388/// passed in a register (for the Darwin ABI).
389bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
390 ASTContext &Context) {
391 uint64_t Size = Context.getTypeSize(Ty);
392
393 // Type must be register sized.
394 if (!isRegisterSize(Size))
395 return false;
396
397 if (Ty->isVectorType()) {
398 // 64- and 128- bit vectors inside structures are not returned in
399 // registers.
400 if (Size == 64 || Size == 128)
401 return false;
402
403 return true;
404 }
405
Daniel Dunbar77115232010-05-15 00:00:30 +0000406 // If this is a builtin, pointer, enum, complex type, member pointer, or
407 // member function pointer it is ok.
Daniel Dunbara1842d32010-05-14 03:40:53 +0000408 if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() ||
Daniel Dunbar55e59e12009-09-24 05:12:36 +0000409 Ty->isAnyComplexType() || Ty->isEnumeralType() ||
Daniel Dunbar77115232010-05-15 00:00:30 +0000410 Ty->isBlockPointerType() || Ty->isMemberPointerType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000411 return true;
412
413 // Arrays are treated like records.
414 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
415 return shouldReturnTypeInRegister(AT->getElementType(), Context);
416
417 // Otherwise, it must be a record type.
Ted Kremenek6217b802009-07-29 21:53:49 +0000418 const RecordType *RT = Ty->getAs<RecordType>();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000419 if (!RT) return false;
420
Anders Carlssona8874232010-01-27 03:25:19 +0000421 // FIXME: Traverse bases here too.
422
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000423 // Structure types are passed in register if all fields would be
424 // passed in a register.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000425 for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(),
426 e = RT->getDecl()->field_end(); i != e; ++i) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000427 const FieldDecl *FD = *i;
428
429 // Empty fields are ignored.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000430 if (isEmptyField(Context, FD, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000431 continue;
432
433 // Check fields recursively.
434 if (!shouldReturnTypeInRegister(FD->getType(), Context))
435 return false;
436 }
437
438 return true;
439}
440
441ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000442 ASTContext &Context,
443 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000444 if (RetTy->isVoidType()) {
445 return ABIArgInfo::getIgnore();
John McCall183700f2009-09-21 23:43:11 +0000446 } else if (const VectorType *VT = RetTy->getAs<VectorType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000447 // On Darwin, some vectors are returned in registers.
David Chisnall1e4249c2009-08-17 23:08:21 +0000448 if (IsDarwinVectorABI) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000449 uint64_t Size = Context.getTypeSize(RetTy);
450
451 // 128-bit vectors are a special case; they are returned in
452 // registers and we need to make sure to pick a type the LLVM
453 // backend will like.
454 if (Size == 128)
Owen Anderson0032b272009-08-13 21:57:51 +0000455 return ABIArgInfo::getCoerce(llvm::VectorType::get(
456 llvm::Type::getInt64Ty(VMContext), 2));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000457
458 // Always return in register if it fits in a general purpose
459 // register, or if it is 64 bits and has a single element.
460 if ((Size == 8 || Size == 16 || Size == 32) ||
461 (Size == 64 && VT->getNumElements() == 1))
Owen Anderson0032b272009-08-13 21:57:51 +0000462 return ABIArgInfo::getCoerce(llvm::IntegerType::get(VMContext, Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000463
464 return ABIArgInfo::getIndirect(0);
465 }
466
467 return ABIArgInfo::getDirect();
468 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Anders Carlssona8874232010-01-27 03:25:19 +0000469 if (const RecordType *RT = RetTy->getAs<RecordType>()) {
Anders Carlsson40092972009-10-20 22:07:59 +0000470 // Structures with either a non-trivial destructor or a non-trivial
471 // copy constructor are always indirect.
472 if (hasNonTrivialDestructorOrCopyConstructor(RT))
473 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
474
475 // Structures with flexible arrays are always indirect.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000476 if (RT->getDecl()->hasFlexibleArrayMember())
477 return ABIArgInfo::getIndirect(0);
Anders Carlsson40092972009-10-20 22:07:59 +0000478 }
479
David Chisnall1e4249c2009-08-17 23:08:21 +0000480 // If specified, structs and unions are always indirect.
481 if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000482 return ABIArgInfo::getIndirect(0);
483
484 // Classify "single element" structs as their element type.
485 if (const Type *SeltTy = isSingleElementStruct(RetTy, Context)) {
John McCall183700f2009-09-21 23:43:11 +0000486 if (const BuiltinType *BT = SeltTy->getAs<BuiltinType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000487 if (BT->isIntegerType()) {
488 // We need to use the size of the structure, padding
489 // bit-fields can adjust that to be larger than the single
490 // element type.
491 uint64_t Size = Context.getTypeSize(RetTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000492 return ABIArgInfo::getCoerce(
Owen Anderson0032b272009-08-13 21:57:51 +0000493 llvm::IntegerType::get(VMContext, (unsigned) Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000494 } else if (BT->getKind() == BuiltinType::Float) {
495 assert(Context.getTypeSize(RetTy) == Context.getTypeSize(SeltTy) &&
496 "Unexpect single element structure size!");
Owen Anderson0032b272009-08-13 21:57:51 +0000497 return ABIArgInfo::getCoerce(llvm::Type::getFloatTy(VMContext));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000498 } else if (BT->getKind() == BuiltinType::Double) {
499 assert(Context.getTypeSize(RetTy) == Context.getTypeSize(SeltTy) &&
500 "Unexpect single element structure size!");
Owen Anderson0032b272009-08-13 21:57:51 +0000501 return ABIArgInfo::getCoerce(llvm::Type::getDoubleTy(VMContext));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000502 }
503 } else if (SeltTy->isPointerType()) {
504 // FIXME: It would be really nice if this could come out as the proper
505 // pointer type.
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +0000506 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000507 return ABIArgInfo::getCoerce(PtrTy);
508 } else if (SeltTy->isVectorType()) {
509 // 64- and 128-bit vectors are never returned in a
510 // register when inside a structure.
511 uint64_t Size = Context.getTypeSize(RetTy);
512 if (Size == 64 || Size == 128)
513 return ABIArgInfo::getIndirect(0);
514
Owen Andersona1cf15f2009-07-14 23:10:40 +0000515 return classifyReturnType(QualType(SeltTy, 0), Context, VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000516 }
517 }
518
519 // Small structures which are register sized are generally returned
520 // in a register.
521 if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, Context)) {
522 uint64_t Size = Context.getTypeSize(RetTy);
Owen Anderson0032b272009-08-13 21:57:51 +0000523 return ABIArgInfo::getCoerce(llvm::IntegerType::get(VMContext, Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000524 }
525
526 return ABIArgInfo::getIndirect(0);
527 } else {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +0000528 // Treat an enum type as its underlying type.
529 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
530 RetTy = EnumTy->getDecl()->getIntegerType();
531
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000532 return (RetTy->isPromotableIntegerType() ?
533 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000534 }
535}
536
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000537ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty,
538 ASTContext &Context,
539 bool ByVal) const {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +0000540 if (!ByVal)
541 return ABIArgInfo::getIndirect(0, false);
542
543 // Compute the byval alignment. We trust the back-end to honor the
544 // minimum ABI alignment for byval, to make cleaner IR.
545 const unsigned MinABIAlign = 4;
546 unsigned Align = Context.getTypeAlign(Ty) / 8;
547 if (Align > MinABIAlign)
548 return ABIArgInfo::getIndirect(Align);
549 return ABIArgInfo::getIndirect(0);
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000550}
551
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000552ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000553 ASTContext &Context,
554 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000555 // FIXME: Set alignment on indirect arguments.
556 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
557 // Structures with flexible arrays are always indirect.
Anders Carlssona8874232010-01-27 03:25:19 +0000558 if (const RecordType *RT = Ty->getAs<RecordType>()) {
559 // Structures with either a non-trivial destructor or a non-trivial
560 // copy constructor are always indirect.
561 if (hasNonTrivialDestructorOrCopyConstructor(RT))
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000562 return getIndirectResult(Ty, Context, /*ByVal=*/false);
563
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000564 if (RT->getDecl()->hasFlexibleArrayMember())
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000565 return getIndirectResult(Ty, Context);
Anders Carlssona8874232010-01-27 03:25:19 +0000566 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000567
568 // Ignore empty structs.
Eli Friedmana1e6de92009-06-13 21:37:10 +0000569 if (Ty->isStructureType() && Context.getTypeSize(Ty) == 0)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000570 return ABIArgInfo::getIgnore();
571
Daniel Dunbar53012f42009-11-09 01:33:53 +0000572 // Expand small (<= 128-bit) record types when we know that the stack layout
573 // of those arguments will match the struct. This is important because the
574 // LLVM backend isn't smart enough to remove byval, which inhibits many
575 // optimizations.
576 if (Context.getTypeSize(Ty) <= 4*32 &&
577 canExpandIndirectArgument(Ty, Context))
578 return ABIArgInfo::getExpand();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000579
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000580 return getIndirectResult(Ty, Context);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000581 } else {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +0000582 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
583 Ty = EnumTy->getDecl()->getIntegerType();
584
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000585 return (Ty->isPromotableIntegerType() ?
586 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000587 }
588}
589
590llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
591 CodeGenFunction &CGF) const {
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +0000592 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Owen Anderson96e0fc72009-07-29 22:16:19 +0000593 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000594
595 CGBuilderTy &Builder = CGF.Builder;
596 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
597 "ap");
598 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
599 llvm::Type *PTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000600 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000601 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
602
603 uint64_t Offset =
604 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
605 llvm::Value *NextAddr =
Chris Lattner77b89b82010-06-27 07:15:29 +0000606 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000607 "ap.next");
608 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
609
610 return AddrTyped;
611}
612
Charles Davis74f72932010-02-13 15:54:06 +0000613void X86_32TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
614 llvm::GlobalValue *GV,
615 CodeGen::CodeGenModule &CGM) const {
616 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
617 if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
618 // Get the LLVM function.
619 llvm::Function *Fn = cast<llvm::Function>(GV);
620
621 // Now add the 'alignstack' attribute with a value of 16.
622 Fn->addFnAttr(llvm::Attribute::constructStackAlignmentFromInt(16));
623 }
624 }
625}
626
John McCall6374c332010-03-06 00:35:14 +0000627bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable(
628 CodeGen::CodeGenFunction &CGF,
629 llvm::Value *Address) const {
630 CodeGen::CGBuilderTy &Builder = CGF.Builder;
631 llvm::LLVMContext &Context = CGF.getLLVMContext();
632
633 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
634 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
635
636 // 0-7 are the eight integer registers; the order is different
637 // on Darwin (for EH), but the range is the same.
638 // 8 is %eip.
John McCallaeeb7012010-05-27 06:19:26 +0000639 AssignToArrayRange(Builder, Address, Four8, 0, 8);
John McCall6374c332010-03-06 00:35:14 +0000640
641 if (CGF.CGM.isTargetDarwin()) {
642 // 12-16 are st(0..4). Not sure why we stop at 4.
643 // These have size 16, which is sizeof(long double) on
644 // platforms with 8-byte alignment for that type.
645 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
John McCallaeeb7012010-05-27 06:19:26 +0000646 AssignToArrayRange(Builder, Address, Sixteen8, 12, 16);
John McCall6374c332010-03-06 00:35:14 +0000647
648 } else {
649 // 9 is %eflags, which doesn't get a size on Darwin for some
650 // reason.
651 Builder.CreateStore(Four8, Builder.CreateConstInBoundsGEP1_32(Address, 9));
652
653 // 11-16 are st(0..5). Not sure why we stop at 5.
654 // These have size 12, which is sizeof(long double) on
655 // platforms with 4-byte alignment for that type.
656 llvm::Value *Twelve8 = llvm::ConstantInt::get(i8, 12);
John McCallaeeb7012010-05-27 06:19:26 +0000657 AssignToArrayRange(Builder, Address, Twelve8, 11, 16);
658 }
John McCall6374c332010-03-06 00:35:14 +0000659
660 return false;
661}
662
Chris Lattnerdce5ad02010-06-28 20:05:43 +0000663//===----------------------------------------------------------------------===//
664// X86-64 ABI Implementation
665//===----------------------------------------------------------------------===//
666
667
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000668namespace {
669/// X86_64ABIInfo - The X86_64 ABI information.
670class X86_64ABIInfo : public ABIInfo {
671 enum Class {
672 Integer = 0,
673 SSE,
674 SSEUp,
675 X87,
676 X87Up,
677 ComplexX87,
678 NoClass,
679 Memory
680 };
681
682 /// merge - Implement the X86_64 ABI merging algorithm.
683 ///
684 /// Merge an accumulating classification \arg Accum with a field
685 /// classification \arg Field.
686 ///
687 /// \param Accum - The accumulating classification. This should
688 /// always be either NoClass or the result of a previous merge
689 /// call. In addition, this should never be Memory (the caller
690 /// should just return Memory for the aggregate).
691 Class merge(Class Accum, Class Field) const;
692
693 /// classify - Determine the x86_64 register classes in which the
694 /// given type T should be passed.
695 ///
696 /// \param Lo - The classification for the parts of the type
697 /// residing in the low word of the containing object.
698 ///
699 /// \param Hi - The classification for the parts of the type
700 /// residing in the high word of the containing object.
701 ///
702 /// \param OffsetBase - The bit offset of this type in the
703 /// containing object. Some parameters are classified different
704 /// depending on whether they straddle an eightbyte boundary.
705 ///
706 /// If a word is unused its result will be NoClass; if a type should
707 /// be passed in Memory then at least the classification of \arg Lo
708 /// will be Memory.
709 ///
710 /// The \arg Lo class will be NoClass iff the argument is ignored.
711 ///
712 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
713 /// also be ComplexX87.
714 void classify(QualType T, ASTContext &Context, uint64_t OffsetBase,
715 Class &Lo, Class &Hi) const;
716
717 /// getCoerceResult - Given a source type \arg Ty and an LLVM type
718 /// to coerce to, chose the best way to pass Ty in the same place
719 /// that \arg CoerceTo would be passed, but while keeping the
720 /// emitted code as simple as possible.
721 ///
722 /// FIXME: Note, this should be cleaned up to just take an enumeration of all
723 /// the ways we might want to pass things, instead of constructing an LLVM
724 /// type. This makes this code more explicit, and it makes it clearer that we
725 /// are also doing this for correctness in the case of passing scalar types.
726 ABIArgInfo getCoerceResult(QualType Ty,
727 const llvm::Type *CoerceTo,
728 ASTContext &Context) const;
729
730 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Daniel Dunbar46c54fb2010-04-21 19:49:55 +0000731 /// such that the argument will be returned in memory.
732 ABIArgInfo getIndirectReturnResult(QualType Ty, ASTContext &Context) const;
733
734 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000735 /// such that the argument will be passed in memory.
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000736 ABIArgInfo getIndirectResult(QualType Ty, ASTContext &Context) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000737
738 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000739 ASTContext &Context,
740 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000741
742 ABIArgInfo classifyArgumentType(QualType Ty,
743 ASTContext &Context,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000744 llvm::LLVMContext &VMContext,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000745 unsigned &neededInt,
746 unsigned &neededSSE) const;
747
748public:
Owen Andersona1cf15f2009-07-14 23:10:40 +0000749 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
750 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000751
752 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
753 CodeGenFunction &CGF) const;
754};
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000755
756class X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
757public:
Douglas Gregor568bb2d2010-01-22 15:41:14 +0000758 X86_64TargetCodeGenInfo():TargetCodeGenInfo(new X86_64ABIInfo()) {}
John McCall6374c332010-03-06 00:35:14 +0000759
760 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
761 return 7;
762 }
763
764 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
765 llvm::Value *Address) const {
766 CodeGen::CGBuilderTy &Builder = CGF.Builder;
767 llvm::LLVMContext &Context = CGF.getLLVMContext();
768
769 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
770 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
771
John McCallaeeb7012010-05-27 06:19:26 +0000772 // 0-15 are the 16 integer registers.
773 // 16 is %rip.
774 AssignToArrayRange(Builder, Address, Eight8, 0, 16);
John McCall6374c332010-03-06 00:35:14 +0000775
776 return false;
777 }
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000778};
779
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000780}
781
782X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum,
783 Class Field) const {
784 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
785 // classified recursively so that always two fields are
786 // considered. The resulting class is calculated according to
787 // the classes of the fields in the eightbyte:
788 //
789 // (a) If both classes are equal, this is the resulting class.
790 //
791 // (b) If one of the classes is NO_CLASS, the resulting class is
792 // the other class.
793 //
794 // (c) If one of the classes is MEMORY, the result is the MEMORY
795 // class.
796 //
797 // (d) If one of the classes is INTEGER, the result is the
798 // INTEGER.
799 //
800 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
801 // MEMORY is used as class.
802 //
803 // (f) Otherwise class SSE is used.
804
805 // Accum should never be memory (we should have returned) or
806 // ComplexX87 (because this cannot be passed in a structure).
807 assert((Accum != Memory && Accum != ComplexX87) &&
808 "Invalid accumulated classification during merge.");
809 if (Accum == Field || Field == NoClass)
810 return Accum;
811 else if (Field == Memory)
812 return Memory;
813 else if (Accum == NoClass)
814 return Field;
815 else if (Accum == Integer || Field == Integer)
816 return Integer;
817 else if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
818 Accum == X87 || Accum == X87Up)
819 return Memory;
820 else
821 return SSE;
822}
823
824void X86_64ABIInfo::classify(QualType Ty,
825 ASTContext &Context,
826 uint64_t OffsetBase,
827 Class &Lo, Class &Hi) const {
828 // FIXME: This code can be simplified by introducing a simple value class for
829 // Class pairs with appropriate constructor methods for the various
830 // situations.
831
832 // FIXME: Some of the split computations are wrong; unaligned vectors
833 // shouldn't be passed in registers for example, so there is no chance they
834 // can straddle an eightbyte. Verify & simplify.
835
836 Lo = Hi = NoClass;
837
838 Class &Current = OffsetBase < 64 ? Lo : Hi;
839 Current = Memory;
840
John McCall183700f2009-09-21 23:43:11 +0000841 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000842 BuiltinType::Kind k = BT->getKind();
843
844 if (k == BuiltinType::Void) {
845 Current = NoClass;
846 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
847 Lo = Integer;
848 Hi = Integer;
849 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
850 Current = Integer;
851 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
852 Current = SSE;
853 } else if (k == BuiltinType::LongDouble) {
854 Lo = X87;
855 Hi = X87Up;
856 }
857 // FIXME: _Decimal32 and _Decimal64 are SSE.
858 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
John McCall183700f2009-09-21 23:43:11 +0000859 } else if (const EnumType *ET = Ty->getAs<EnumType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000860 // Classify the underlying integer type.
861 classify(ET->getDecl()->getIntegerType(), Context, OffsetBase, Lo, Hi);
862 } else if (Ty->hasPointerRepresentation()) {
863 Current = Integer;
Daniel Dunbar67d438d2010-05-15 00:00:37 +0000864 } else if (Ty->isMemberPointerType()) {
865 if (Ty->isMemberFunctionPointerType())
866 Lo = Hi = Integer;
867 else
868 Current = Integer;
John McCall183700f2009-09-21 23:43:11 +0000869 } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000870 uint64_t Size = Context.getTypeSize(VT);
871 if (Size == 32) {
872 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
873 // float> as integer.
874 Current = Integer;
875
876 // If this type crosses an eightbyte boundary, it should be
877 // split.
878 uint64_t EB_Real = (OffsetBase) / 64;
879 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
880 if (EB_Real != EB_Imag)
881 Hi = Lo;
882 } else if (Size == 64) {
883 // gcc passes <1 x double> in memory. :(
884 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
885 return;
886
887 // gcc passes <1 x long long> as INTEGER.
888 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong))
889 Current = Integer;
890 else
891 Current = SSE;
892
893 // If this type crosses an eightbyte boundary, it should be
894 // split.
895 if (OffsetBase && OffsetBase != 64)
896 Hi = Lo;
897 } else if (Size == 128) {
898 Lo = SSE;
899 Hi = SSEUp;
900 }
John McCall183700f2009-09-21 23:43:11 +0000901 } else if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000902 QualType ET = Context.getCanonicalType(CT->getElementType());
903
904 uint64_t Size = Context.getTypeSize(Ty);
Douglas Gregor2ade35e2010-06-16 00:17:44 +0000905 if (ET->isIntegralOrEnumerationType()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000906 if (Size <= 64)
907 Current = Integer;
908 else if (Size <= 128)
909 Lo = Hi = Integer;
910 } else if (ET == Context.FloatTy)
911 Current = SSE;
912 else if (ET == Context.DoubleTy)
913 Lo = Hi = SSE;
914 else if (ET == Context.LongDoubleTy)
915 Current = ComplexX87;
916
917 // If this complex type crosses an eightbyte boundary then it
918 // should be split.
919 uint64_t EB_Real = (OffsetBase) / 64;
920 uint64_t EB_Imag = (OffsetBase + Context.getTypeSize(ET)) / 64;
921 if (Hi == NoClass && EB_Real != EB_Imag)
922 Hi = Lo;
923 } else if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
924 // Arrays are treated like structures.
925
926 uint64_t Size = Context.getTypeSize(Ty);
927
928 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
929 // than two eightbytes, ..., it has class MEMORY.
930 if (Size > 128)
931 return;
932
933 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
934 // fields, it has class MEMORY.
935 //
936 // Only need to check alignment of array base.
937 if (OffsetBase % Context.getTypeAlign(AT->getElementType()))
938 return;
939
940 // Otherwise implement simplified merge. We could be smarter about
941 // this, but it isn't worth it and would be harder to verify.
942 Current = NoClass;
943 uint64_t EltSize = Context.getTypeSize(AT->getElementType());
944 uint64_t ArraySize = AT->getSize().getZExtValue();
945 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
946 Class FieldLo, FieldHi;
947 classify(AT->getElementType(), Context, Offset, FieldLo, FieldHi);
948 Lo = merge(Lo, FieldLo);
949 Hi = merge(Hi, FieldHi);
950 if (Lo == Memory || Hi == Memory)
951 break;
952 }
953
954 // Do post merger cleanup (see below). Only case we worry about is Memory.
955 if (Hi == Memory)
956 Lo = Memory;
957 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Ted Kremenek6217b802009-07-29 21:53:49 +0000958 } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000959 uint64_t Size = Context.getTypeSize(Ty);
960
961 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
962 // than two eightbytes, ..., it has class MEMORY.
963 if (Size > 128)
964 return;
965
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000966 // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
967 // copy constructor or a non-trivial destructor, it is passed by invisible
968 // reference.
969 if (hasNonTrivialDestructorOrCopyConstructor(RT))
970 return;
Daniel Dunbarce9f4232009-11-22 23:01:23 +0000971
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000972 const RecordDecl *RD = RT->getDecl();
973
974 // Assume variable sized types are passed in memory.
975 if (RD->hasFlexibleArrayMember())
976 return;
977
978 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
979
980 // Reset Lo class, this will be recomputed.
981 Current = NoClass;
Daniel Dunbarce9f4232009-11-22 23:01:23 +0000982
983 // If this is a C++ record, classify the bases first.
984 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
985 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
986 e = CXXRD->bases_end(); i != e; ++i) {
987 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
988 "Unexpected base class!");
989 const CXXRecordDecl *Base =
990 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
991
992 // Classify this field.
993 //
994 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
995 // single eightbyte, each is classified separately. Each eightbyte gets
996 // initialized to class NO_CLASS.
997 Class FieldLo, FieldHi;
998 uint64_t Offset = OffsetBase + Layout.getBaseClassOffset(Base);
999 classify(i->getType(), Context, Offset, FieldLo, FieldHi);
1000 Lo = merge(Lo, FieldLo);
1001 Hi = merge(Hi, FieldHi);
1002 if (Lo == Memory || Hi == Memory)
1003 break;
1004 }
Daniel Dunbar4971ff82009-12-22 01:19:25 +00001005
1006 // If this record has no fields but isn't empty, classify as INTEGER.
1007 if (RD->field_empty() && Size)
1008 Current = Integer;
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001009 }
1010
1011 // Classify the fields one at a time, merging the results.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001012 unsigned idx = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001013 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1014 i != e; ++i, ++idx) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001015 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
1016 bool BitField = i->isBitField();
1017
1018 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
1019 // fields, it has class MEMORY.
1020 //
1021 // Note, skip this test for bit-fields, see below.
1022 if (!BitField && Offset % Context.getTypeAlign(i->getType())) {
1023 Lo = Memory;
1024 return;
1025 }
1026
1027 // Classify this field.
1028 //
1029 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
1030 // exceeds a single eightbyte, each is classified
1031 // separately. Each eightbyte gets initialized to class
1032 // NO_CLASS.
1033 Class FieldLo, FieldHi;
1034
1035 // Bit-fields require special handling, they do not force the
1036 // structure to be passed in memory even if unaligned, and
1037 // therefore they can straddle an eightbyte.
1038 if (BitField) {
1039 // Ignore padding bit-fields.
1040 if (i->isUnnamedBitfield())
1041 continue;
1042
1043 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
1044 uint64_t Size = i->getBitWidth()->EvaluateAsInt(Context).getZExtValue();
1045
1046 uint64_t EB_Lo = Offset / 64;
1047 uint64_t EB_Hi = (Offset + Size - 1) / 64;
1048 FieldLo = FieldHi = NoClass;
1049 if (EB_Lo) {
1050 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
1051 FieldLo = NoClass;
1052 FieldHi = Integer;
1053 } else {
1054 FieldLo = Integer;
1055 FieldHi = EB_Hi ? Integer : NoClass;
1056 }
1057 } else
1058 classify(i->getType(), Context, Offset, FieldLo, FieldHi);
1059 Lo = merge(Lo, FieldLo);
1060 Hi = merge(Hi, FieldHi);
1061 if (Lo == Memory || Hi == Memory)
1062 break;
1063 }
1064
1065 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
1066 //
1067 // (a) If one of the classes is MEMORY, the whole argument is
1068 // passed in memory.
1069 //
1070 // (b) If SSEUP is not preceeded by SSE, it is converted to SSE.
1071
1072 // The first of these conditions is guaranteed by how we implement
1073 // the merge (just bail).
1074 //
1075 // The second condition occurs in the case of unions; for example
1076 // union { _Complex double; unsigned; }.
1077 if (Hi == Memory)
1078 Lo = Memory;
1079 if (Hi == SSEUp && Lo != SSE)
1080 Hi = SSE;
1081 }
1082}
1083
1084ABIArgInfo X86_64ABIInfo::getCoerceResult(QualType Ty,
1085 const llvm::Type *CoerceTo,
1086 ASTContext &Context) const {
Chris Lattner7f215c12010-06-26 21:52:32 +00001087 if (CoerceTo->isIntegerTy(64)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001088 // Integer and pointer types will end up in a general purpose
1089 // register.
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001090
1091 // Treat an enum type as its underlying type.
1092 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1093 Ty = EnumTy->getDecl()->getIntegerType();
1094
Douglas Gregor9d3347a2010-06-16 00:35:25 +00001095 if (Ty->isIntegralOrEnumerationType() || Ty->hasPointerRepresentation())
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001096 return (Ty->isPromotableIntegerType() ?
1097 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Chris Lattnerfaf23b72010-06-28 19:56:59 +00001098
1099 // If this is a 32-bit structure that is passed as an int64, then it will be
1100 // passed in the low 32-bits of a 64-bit GPR, which is the same as how an
1101 // i32 is passed. Coerce to a i32 instead of a i64.
1102 if (Context.getTypeSizeInChars(Ty).getQuantity() == 4)
1103 CoerceTo = llvm::Type::getInt32Ty(CoerceTo->getContext());
1104
Chris Lattner7f215c12010-06-26 21:52:32 +00001105 } else if (CoerceTo->isDoubleTy()) {
John McCall0b0ef0a2010-02-24 07:14:12 +00001106 assert(Ty.isCanonical() && "should always have a canonical type here");
1107 assert(!Ty.hasQualifiers() && "should never have a qualified type here");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001108
1109 // Float and double end up in a single SSE reg.
John McCall0b0ef0a2010-02-24 07:14:12 +00001110 if (Ty == Context.FloatTy || Ty == Context.DoubleTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001111 return ABIArgInfo::getDirect();
1112
Chris Lattnerfaf23b72010-06-28 19:56:59 +00001113 // If this is a 32-bit structure that is passed as a double, then it will be
1114 // passed in the low 32-bits of the XMM register, which is the same as how a
1115 // float is passed. Coerce to a float instead of a double.
1116 if (Context.getTypeSizeInChars(Ty).getQuantity() == 4)
1117 CoerceTo = llvm::Type::getFloatTy(CoerceTo->getContext());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001118 }
1119
1120 return ABIArgInfo::getCoerce(CoerceTo);
1121}
1122
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001123ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty,
1124 ASTContext &Context) const {
1125 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1126 // place naturally.
1127 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
1128 // Treat an enum type as its underlying type.
1129 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1130 Ty = EnumTy->getDecl()->getIntegerType();
1131
1132 return (Ty->isPromotableIntegerType() ?
1133 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1134 }
1135
1136 return ABIArgInfo::getIndirect(0);
1137}
1138
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001139ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty,
1140 ASTContext &Context) const {
1141 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1142 // place naturally.
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001143 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
1144 // Treat an enum type as its underlying type.
1145 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1146 Ty = EnumTy->getDecl()->getIntegerType();
1147
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001148 return (Ty->isPromotableIntegerType() ?
1149 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001150 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001151
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001152 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
1153 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Anders Carlsson0a8f8472009-09-16 15:53:40 +00001154
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001155 // Compute the byval alignment. We trust the back-end to honor the
1156 // minimum ABI alignment for byval, to make cleaner IR.
1157 const unsigned MinABIAlign = 8;
1158 unsigned Align = Context.getTypeAlign(Ty) / 8;
1159 if (Align > MinABIAlign)
1160 return ABIArgInfo::getIndirect(Align);
1161 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001162}
1163
1164ABIArgInfo X86_64ABIInfo::classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001165 ASTContext &Context,
1166 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001167 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
1168 // classification algorithm.
1169 X86_64ABIInfo::Class Lo, Hi;
1170 classify(RetTy, Context, 0, Lo, Hi);
1171
1172 // Check some invariants.
1173 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
1174 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
1175 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1176
1177 const llvm::Type *ResType = 0;
1178 switch (Lo) {
1179 case NoClass:
1180 return ABIArgInfo::getIgnore();
1181
1182 case SSEUp:
1183 case X87Up:
1184 assert(0 && "Invalid classification for lo word.");
1185
1186 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
1187 // hidden argument.
1188 case Memory:
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001189 return getIndirectReturnResult(RetTy, Context);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001190
1191 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
1192 // available register of the sequence %rax, %rdx is used.
1193 case Integer:
Owen Anderson0032b272009-08-13 21:57:51 +00001194 ResType = llvm::Type::getInt64Ty(VMContext); break;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001195
1196 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
1197 // available SSE register of the sequence %xmm0, %xmm1 is used.
1198 case SSE:
Owen Anderson0032b272009-08-13 21:57:51 +00001199 ResType = llvm::Type::getDoubleTy(VMContext); break;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001200
1201 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
1202 // returned on the X87 stack in %st0 as 80-bit x87 number.
1203 case X87:
Owen Anderson0032b272009-08-13 21:57:51 +00001204 ResType = llvm::Type::getX86_FP80Ty(VMContext); break;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001205
1206 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
1207 // part of the value is returned in %st0 and the imaginary part in
1208 // %st1.
1209 case ComplexX87:
1210 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Chris Lattner52d9ae32010-04-06 17:29:22 +00001211 ResType = llvm::StructType::get(VMContext,
1212 llvm::Type::getX86_FP80Ty(VMContext),
Owen Anderson0032b272009-08-13 21:57:51 +00001213 llvm::Type::getX86_FP80Ty(VMContext),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001214 NULL);
1215 break;
1216 }
1217
1218 switch (Hi) {
1219 // Memory was handled previously and X87 should
1220 // never occur as a hi class.
1221 case Memory:
1222 case X87:
1223 assert(0 && "Invalid classification for hi word.");
1224
1225 case ComplexX87: // Previously handled.
1226 case NoClass: break;
1227
1228 case Integer:
Owen Anderson47a434f2009-08-05 23:18:46 +00001229 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson0032b272009-08-13 21:57:51 +00001230 llvm::Type::getInt64Ty(VMContext), NULL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001231 break;
1232 case SSE:
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 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
1238 // is passed in the upper half of the last used SSE register.
1239 //
1240 // SSEUP should always be preceeded by SSE, just widen.
1241 case SSEUp:
1242 assert(Lo == SSE && "Unexpected SSEUp classification.");
Owen Anderson0032b272009-08-13 21:57:51 +00001243 ResType = llvm::VectorType::get(llvm::Type::getDoubleTy(VMContext), 2);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001244 break;
1245
1246 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
1247 // returned together with the previous X87 value in %st0.
1248 case X87Up:
1249 // If X87Up is preceeded by X87, we don't need to do
1250 // anything. However, in some cases with unions it may not be
1251 // preceeded by X87. In such situations we follow gcc and pass the
1252 // extra bits in an SSE reg.
1253 if (Lo != X87)
Owen Anderson47a434f2009-08-05 23:18:46 +00001254 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson0032b272009-08-13 21:57:51 +00001255 llvm::Type::getDoubleTy(VMContext), NULL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001256 break;
1257 }
1258
1259 return getCoerceResult(RetTy, ResType, Context);
1260}
1261
1262ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, ASTContext &Context,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001263 llvm::LLVMContext &VMContext,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001264 unsigned &neededInt,
1265 unsigned &neededSSE) const {
1266 X86_64ABIInfo::Class Lo, Hi;
1267 classify(Ty, Context, 0, Lo, Hi);
1268
1269 // Check some invariants.
1270 // FIXME: Enforce these by construction.
1271 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
1272 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
1273 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1274
1275 neededInt = 0;
1276 neededSSE = 0;
1277 const llvm::Type *ResType = 0;
1278 switch (Lo) {
1279 case NoClass:
1280 return ABIArgInfo::getIgnore();
1281
1282 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
1283 // on the stack.
1284 case Memory:
1285
1286 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
1287 // COMPLEX_X87, it is passed in memory.
1288 case X87:
1289 case ComplexX87:
1290 return getIndirectResult(Ty, Context);
1291
1292 case SSEUp:
1293 case X87Up:
1294 assert(0 && "Invalid classification for lo word.");
1295
1296 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
1297 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
1298 // and %r9 is used.
1299 case Integer:
1300 ++neededInt;
Owen Anderson0032b272009-08-13 21:57:51 +00001301 ResType = llvm::Type::getInt64Ty(VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001302 break;
1303
1304 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
1305 // available SSE register is used, the registers are taken in the
1306 // order from %xmm0 to %xmm7.
1307 case SSE:
1308 ++neededSSE;
Owen Anderson0032b272009-08-13 21:57:51 +00001309 ResType = llvm::Type::getDoubleTy(VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001310 break;
1311 }
1312
1313 switch (Hi) {
1314 // Memory was handled previously, ComplexX87 and X87 should
1315 // never occur as hi classes, and X87Up must be preceed by X87,
1316 // which is passed in memory.
1317 case Memory:
1318 case X87:
1319 case ComplexX87:
1320 assert(0 && "Invalid classification for hi word.");
1321 break;
1322
1323 case NoClass: break;
1324 case Integer:
Owen Anderson47a434f2009-08-05 23:18:46 +00001325 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson0032b272009-08-13 21:57:51 +00001326 llvm::Type::getInt64Ty(VMContext), NULL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001327 ++neededInt;
1328 break;
1329
1330 // X87Up generally doesn't occur here (long double is passed in
1331 // memory), except in situations involving unions.
1332 case X87Up:
1333 case SSE:
Owen Anderson47a434f2009-08-05 23:18:46 +00001334 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson0032b272009-08-13 21:57:51 +00001335 llvm::Type::getDoubleTy(VMContext), NULL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001336 ++neededSSE;
1337 break;
1338
1339 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
1340 // eightbyte is passed in the upper half of the last used SSE
1341 // register.
1342 case SSEUp:
1343 assert(Lo == SSE && "Unexpected SSEUp classification.");
Owen Anderson0032b272009-08-13 21:57:51 +00001344 ResType = llvm::VectorType::get(llvm::Type::getDoubleTy(VMContext), 2);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001345 break;
1346 }
1347
1348 return getCoerceResult(Ty, ResType, Context);
1349}
1350
Owen Andersona1cf15f2009-07-14 23:10:40 +00001351void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1352 llvm::LLVMContext &VMContext) const {
1353 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(),
1354 Context, VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001355
1356 // Keep track of the number of assigned registers.
1357 unsigned freeIntRegs = 6, freeSSERegs = 8;
1358
1359 // If the return value is indirect, then the hidden argument is consuming one
1360 // integer register.
1361 if (FI.getReturnInfo().isIndirect())
1362 --freeIntRegs;
1363
1364 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
1365 // get assigned (in left-to-right order) for passing as follows...
1366 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1367 it != ie; ++it) {
1368 unsigned neededInt, neededSSE;
Mike Stump1eb44332009-09-09 15:08:12 +00001369 it->info = classifyArgumentType(it->type, Context, VMContext,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001370 neededInt, neededSSE);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001371
1372 // AMD64-ABI 3.2.3p3: If there are no registers available for any
1373 // eightbyte of an argument, the whole argument is passed on the
1374 // stack. If registers have already been assigned for some
1375 // eightbytes of such an argument, the assignments get reverted.
1376 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
1377 freeIntRegs -= neededInt;
1378 freeSSERegs -= neededSSE;
1379 } else {
1380 it->info = getIndirectResult(it->type, Context);
1381 }
1382 }
1383}
1384
1385static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
1386 QualType Ty,
1387 CodeGenFunction &CGF) {
1388 llvm::Value *overflow_arg_area_p =
1389 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
1390 llvm::Value *overflow_arg_area =
1391 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
1392
1393 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
1394 // byte boundary if alignment needed by type exceeds 8 byte boundary.
1395 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
1396 if (Align > 8) {
1397 // Note that we follow the ABI & gcc here, even though the type
1398 // could in theory have an alignment greater than 16. This case
1399 // shouldn't ever matter in practice.
1400
1401 // overflow_arg_area = (overflow_arg_area + 15) & ~15;
Owen Anderson0032b272009-08-13 21:57:51 +00001402 llvm::Value *Offset =
Chris Lattner77b89b82010-06-27 07:15:29 +00001403 llvm::ConstantInt::get(CGF.Int32Ty, 15);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001404 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
1405 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
Chris Lattner77b89b82010-06-27 07:15:29 +00001406 CGF.Int64Ty);
1407 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, ~15LL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001408 overflow_arg_area =
1409 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
1410 overflow_arg_area->getType(),
1411 "overflow_arg_area.align");
1412 }
1413
1414 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
1415 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1416 llvm::Value *Res =
1417 CGF.Builder.CreateBitCast(overflow_arg_area,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001418 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001419
1420 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
1421 // l->overflow_arg_area + sizeof(type).
1422 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
1423 // an 8 byte boundary.
1424
1425 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
Owen Anderson0032b272009-08-13 21:57:51 +00001426 llvm::Value *Offset =
Chris Lattner77b89b82010-06-27 07:15:29 +00001427 llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001428 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
1429 "overflow_arg_area.next");
1430 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
1431
1432 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
1433 return Res;
1434}
1435
1436llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1437 CodeGenFunction &CGF) const {
Owen Andersona1cf15f2009-07-14 23:10:40 +00001438 llvm::LLVMContext &VMContext = CGF.getLLVMContext();
Mike Stump1eb44332009-09-09 15:08:12 +00001439
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001440 // Assume that va_list type is correct; should be pointer to LLVM type:
1441 // struct {
1442 // i32 gp_offset;
1443 // i32 fp_offset;
1444 // i8* overflow_arg_area;
1445 // i8* reg_save_area;
1446 // };
1447 unsigned neededInt, neededSSE;
Chris Lattnera14db752010-03-11 18:19:55 +00001448
1449 Ty = CGF.getContext().getCanonicalType(Ty);
Owen Andersona1cf15f2009-07-14 23:10:40 +00001450 ABIArgInfo AI = classifyArgumentType(Ty, CGF.getContext(), VMContext,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001451 neededInt, neededSSE);
1452
1453 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
1454 // in the registers. If not go to step 7.
1455 if (!neededInt && !neededSSE)
1456 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1457
1458 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
1459 // general purpose registers needed to pass type and num_fp to hold
1460 // the number of floating point registers needed.
1461
1462 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
1463 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
1464 // l->fp_offset > 304 - num_fp * 16 go to step 7.
1465 //
1466 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
1467 // register save space).
1468
1469 llvm::Value *InRegs = 0;
1470 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
1471 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
1472 if (neededInt) {
1473 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
1474 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
1475 InRegs =
1476 CGF.Builder.CreateICmpULE(gp_offset,
Chris Lattner77b89b82010-06-27 07:15:29 +00001477 llvm::ConstantInt::get(CGF.Int32Ty,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001478 48 - neededInt * 8),
1479 "fits_in_gp");
1480 }
1481
1482 if (neededSSE) {
1483 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
1484 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
1485 llvm::Value *FitsInFP =
1486 CGF.Builder.CreateICmpULE(fp_offset,
Chris Lattner77b89b82010-06-27 07:15:29 +00001487 llvm::ConstantInt::get(CGF.Int32Ty,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001488 176 - neededSSE * 16),
1489 "fits_in_fp");
1490 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
1491 }
1492
1493 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
1494 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
1495 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
1496 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
1497
1498 // Emit code to load the value if it was passed in registers.
1499
1500 CGF.EmitBlock(InRegBlock);
1501
1502 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
1503 // an offset of l->gp_offset and/or l->fp_offset. This may require
1504 // copying to a temporary location in case the parameter is passed
1505 // in different register classes or requires an alignment greater
1506 // than 8 for general purpose registers and 16 for XMM registers.
1507 //
1508 // FIXME: This really results in shameful code when we end up needing to
1509 // collect arguments from different places; often what should result in a
1510 // simple assembling of a structure from scattered addresses has many more
1511 // loads than necessary. Can we clean this up?
1512 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1513 llvm::Value *RegAddr =
1514 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
1515 "reg_save_area");
1516 if (neededInt && neededSSE) {
1517 // FIXME: Cleanup.
1518 assert(AI.isCoerce() && "Unexpected ABI info for mixed regs");
1519 const llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
1520 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
1521 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
1522 const llvm::Type *TyLo = ST->getElementType(0);
1523 const llvm::Type *TyHi = ST->getElementType(1);
Duncan Sandsf177d9d2010-02-15 16:14:01 +00001524 assert((TyLo->isFloatingPointTy() ^ TyHi->isFloatingPointTy()) &&
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001525 "Unexpected ABI info for mixed regs");
Owen Anderson96e0fc72009-07-29 22:16:19 +00001526 const llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
1527 const llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001528 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1529 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Duncan Sandsf177d9d2010-02-15 16:14:01 +00001530 llvm::Value *RegLoAddr = TyLo->isFloatingPointTy() ? FPAddr : GPAddr;
1531 llvm::Value *RegHiAddr = TyLo->isFloatingPointTy() ? GPAddr : FPAddr;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001532 llvm::Value *V =
1533 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
1534 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1535 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
1536 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1537
Owen Andersona1cf15f2009-07-14 23:10:40 +00001538 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001539 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001540 } else if (neededInt) {
1541 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1542 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001543 llvm::PointerType::getUnqual(LTy));
Chris Lattnerdce5ad02010-06-28 20:05:43 +00001544 } else if (neededSSE == 1) {
1545 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1546 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
1547 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001548 } else {
Chris Lattnerdce5ad02010-06-28 20:05:43 +00001549 assert(neededSSE == 2 && "Invalid number of needed registers!");
1550 // SSE registers are spaced 16 bytes apart in the register save
1551 // area, we need to collect the two eightbytes together.
1552 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1553 llvm::Value *RegAddrHi =
1554 CGF.Builder.CreateGEP(RegAddrLo,
1555 llvm::ConstantInt::get(CGF.Int32Ty, 16));
1556 const llvm::Type *DoubleTy = llvm::Type::getDoubleTy(VMContext);
1557 const llvm::Type *DblPtrTy =
1558 llvm::PointerType::getUnqual(DoubleTy);
1559 const llvm::StructType *ST = llvm::StructType::get(VMContext, DoubleTy,
1560 DoubleTy, NULL);
1561 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
1562 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
1563 DblPtrTy));
1564 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1565 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
1566 DblPtrTy));
1567 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1568 RegAddr = CGF.Builder.CreateBitCast(Tmp,
1569 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001570 }
1571
1572 // AMD64-ABI 3.5.7p5: Step 5. Set:
1573 // l->gp_offset = l->gp_offset + num_gp * 8
1574 // l->fp_offset = l->fp_offset + num_fp * 16.
1575 if (neededInt) {
Chris Lattner77b89b82010-06-27 07:15:29 +00001576 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001577 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
1578 gp_offset_p);
1579 }
1580 if (neededSSE) {
Chris Lattner77b89b82010-06-27 07:15:29 +00001581 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001582 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
1583 fp_offset_p);
1584 }
1585 CGF.EmitBranch(ContBlock);
1586
1587 // Emit code to load the value if it was passed in memory.
1588
1589 CGF.EmitBlock(InMemBlock);
1590 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1591
1592 // Return the appropriate result.
1593
1594 CGF.EmitBlock(ContBlock);
1595 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(),
1596 "vaarg.addr");
1597 ResAddr->reserveOperandSpace(2);
1598 ResAddr->addIncoming(RegAddr, InRegBlock);
1599 ResAddr->addIncoming(MemAddr, InMemBlock);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001600 return ResAddr;
1601}
1602
Chris Lattnerdce5ad02010-06-28 20:05:43 +00001603
1604
1605//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001606// PIC16 ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00001607//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001608
1609namespace {
1610
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001611class PIC16ABIInfo : public ABIInfo {
1612 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001613 ASTContext &Context,
1614 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001615
1616 ABIArgInfo classifyArgumentType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001617 ASTContext &Context,
1618 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001619
Owen Andersona1cf15f2009-07-14 23:10:40 +00001620 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1621 llvm::LLVMContext &VMContext) const {
1622 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context,
1623 VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001624 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1625 it != ie; ++it)
Owen Andersona1cf15f2009-07-14 23:10:40 +00001626 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001627 }
1628
1629 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1630 CodeGenFunction &CGF) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001631};
1632
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001633class PIC16TargetCodeGenInfo : public TargetCodeGenInfo {
1634public:
Douglas Gregor568bb2d2010-01-22 15:41:14 +00001635 PIC16TargetCodeGenInfo():TargetCodeGenInfo(new PIC16ABIInfo()) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001636};
1637
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001638}
1639
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001640ABIArgInfo PIC16ABIInfo::classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001641 ASTContext &Context,
1642 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001643 if (RetTy->isVoidType()) {
1644 return ABIArgInfo::getIgnore();
1645 } else {
1646 return ABIArgInfo::getDirect();
1647 }
1648}
1649
1650ABIArgInfo PIC16ABIInfo::classifyArgumentType(QualType Ty,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001651 ASTContext &Context,
1652 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001653 return ABIArgInfo::getDirect();
1654}
1655
1656llvm::Value *PIC16ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner77b89b82010-06-27 07:15:29 +00001657 CodeGenFunction &CGF) const {
Chris Lattner52d9ae32010-04-06 17:29:22 +00001658 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Sanjiv Guptaa446ecd2010-02-17 02:25:52 +00001659 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
1660
1661 CGBuilderTy &Builder = CGF.Builder;
1662 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
1663 "ap");
1664 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
1665 llvm::Type *PTy =
1666 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
1667 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
1668
1669 uint64_t Offset = CGF.getContext().getTypeSize(Ty) / 8;
1670
1671 llvm::Value *NextAddr =
1672 Builder.CreateGEP(Addr, llvm::ConstantInt::get(
1673 llvm::Type::getInt32Ty(CGF.getLLVMContext()), Offset),
1674 "ap.next");
1675 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
1676
1677 return AddrTyped;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001678}
1679
Sanjiv Guptaa446ecd2010-02-17 02:25:52 +00001680
John McCallec853ba2010-03-11 00:10:12 +00001681// PowerPC-32
1682
1683namespace {
1684class PPC32TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
1685public:
1686 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
1687 // This is recovered from gcc output.
1688 return 1; // r1 is the dedicated stack pointer
1689 }
1690
1691 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1692 llvm::Value *Address) const;
1693};
1694
1695}
1696
1697bool
1698PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1699 llvm::Value *Address) const {
1700 // This is calculated from the LLVM and GCC tables and verified
1701 // against gcc output. AFAIK all ABIs use the same encoding.
1702
1703 CodeGen::CGBuilderTy &Builder = CGF.Builder;
1704 llvm::LLVMContext &Context = CGF.getLLVMContext();
1705
1706 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
1707 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
1708 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
1709 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
1710
1711 // 0-31: r0-31, the 4-byte general-purpose registers
John McCallaeeb7012010-05-27 06:19:26 +00001712 AssignToArrayRange(Builder, Address, Four8, 0, 31);
John McCallec853ba2010-03-11 00:10:12 +00001713
1714 // 32-63: fp0-31, the 8-byte floating-point registers
John McCallaeeb7012010-05-27 06:19:26 +00001715 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
John McCallec853ba2010-03-11 00:10:12 +00001716
1717 // 64-76 are various 4-byte special-purpose registers:
1718 // 64: mq
1719 // 65: lr
1720 // 66: ctr
1721 // 67: ap
1722 // 68-75 cr0-7
1723 // 76: xer
John McCallaeeb7012010-05-27 06:19:26 +00001724 AssignToArrayRange(Builder, Address, Four8, 64, 76);
John McCallec853ba2010-03-11 00:10:12 +00001725
1726 // 77-108: v0-31, the 16-byte vector registers
John McCallaeeb7012010-05-27 06:19:26 +00001727 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
John McCallec853ba2010-03-11 00:10:12 +00001728
1729 // 109: vrsave
1730 // 110: vscr
1731 // 111: spe_acc
1732 // 112: spefscr
1733 // 113: sfp
John McCallaeeb7012010-05-27 06:19:26 +00001734 AssignToArrayRange(Builder, Address, Four8, 109, 113);
John McCallec853ba2010-03-11 00:10:12 +00001735
1736 return false;
1737}
1738
1739
Chris Lattnerdce5ad02010-06-28 20:05:43 +00001740//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001741// ARM ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00001742//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001743
1744namespace {
1745
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001746class ARMABIInfo : public ABIInfo {
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00001747public:
1748 enum ABIKind {
1749 APCS = 0,
1750 AAPCS = 1,
1751 AAPCS_VFP
1752 };
1753
1754private:
1755 ABIKind Kind;
1756
1757public:
1758 ARMABIInfo(ABIKind _Kind) : Kind(_Kind) {}
1759
1760private:
1761 ABIKind getABIKind() const { return Kind; }
1762
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001763 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001764 ASTContext &Context,
1765 llvm::LLVMContext &VMCOntext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001766
1767 ABIArgInfo classifyArgumentType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001768 ASTContext &Context,
1769 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001770
Owen Andersona1cf15f2009-07-14 23:10:40 +00001771 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1772 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001773
1774 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1775 CodeGenFunction &CGF) const;
1776};
1777
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001778class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
1779public:
1780 ARMTargetCodeGenInfo(ARMABIInfo::ABIKind K)
Douglas Gregor568bb2d2010-01-22 15:41:14 +00001781 :TargetCodeGenInfo(new ARMABIInfo(K)) {}
John McCall6374c332010-03-06 00:35:14 +00001782
1783 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
1784 return 13;
1785 }
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001786};
1787
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001788}
1789
Owen Andersona1cf15f2009-07-14 23:10:40 +00001790void ARMABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1791 llvm::LLVMContext &VMContext) const {
Mike Stump1eb44332009-09-09 15:08:12 +00001792 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001793 VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001794 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1795 it != ie; ++it) {
Owen Andersona1cf15f2009-07-14 23:10:40 +00001796 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001797 }
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00001798
Rafael Espindola25117ab2010-06-16 16:13:39 +00001799 const llvm::Triple &Triple(Context.Target.getTriple());
1800 llvm::CallingConv::ID DefaultCC;
Rafael Espindola1ed1a592010-06-16 19:01:17 +00001801 if (Triple.getEnvironmentName() == "gnueabi" ||
1802 Triple.getEnvironmentName() == "eabi")
Rafael Espindola25117ab2010-06-16 16:13:39 +00001803 DefaultCC = llvm::CallingConv::ARM_AAPCS;
Rafael Espindola1ed1a592010-06-16 19:01:17 +00001804 else
1805 DefaultCC = llvm::CallingConv::ARM_APCS;
Rafael Espindola25117ab2010-06-16 16:13:39 +00001806
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00001807 switch (getABIKind()) {
1808 case APCS:
Rafael Espindola25117ab2010-06-16 16:13:39 +00001809 if (DefaultCC != llvm::CallingConv::ARM_APCS)
1810 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_APCS);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00001811 break;
1812
1813 case AAPCS:
Rafael Espindola25117ab2010-06-16 16:13:39 +00001814 if (DefaultCC != llvm::CallingConv::ARM_AAPCS)
1815 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00001816 break;
1817
1818 case AAPCS_VFP:
1819 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS_VFP);
1820 break;
1821 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001822}
1823
1824ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001825 ASTContext &Context,
1826 llvm::LLVMContext &VMContext) const {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001827 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
1828 // Treat an enum type as its underlying type.
1829 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1830 Ty = EnumTy->getDecl()->getIntegerType();
1831
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001832 return (Ty->isPromotableIntegerType() ?
1833 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001834 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00001835
Daniel Dunbar42025572009-09-14 21:54:03 +00001836 // Ignore empty records.
1837 if (isEmptyRecord(Context, Ty, true))
1838 return ABIArgInfo::getIgnore();
1839
Rafael Espindola0eb1d972010-06-08 02:42:08 +00001840 // Structures with either a non-trivial destructor or a non-trivial
1841 // copy constructor are always indirect.
1842 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
1843 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
1844
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001845 // FIXME: This is kind of nasty... but there isn't much choice because the ARM
1846 // backend doesn't support byval.
1847 // FIXME: This doesn't handle alignment > 64 bits.
1848 const llvm::Type* ElemTy;
1849 unsigned SizeRegs;
1850 if (Context.getTypeAlign(Ty) > 32) {
Owen Anderson0032b272009-08-13 21:57:51 +00001851 ElemTy = llvm::Type::getInt64Ty(VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001852 SizeRegs = (Context.getTypeSize(Ty) + 63) / 64;
1853 } else {
Owen Anderson0032b272009-08-13 21:57:51 +00001854 ElemTy = llvm::Type::getInt32Ty(VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001855 SizeRegs = (Context.getTypeSize(Ty) + 31) / 32;
1856 }
1857 std::vector<const llvm::Type*> LLVMFields;
Owen Anderson96e0fc72009-07-29 22:16:19 +00001858 LLVMFields.push_back(llvm::ArrayType::get(ElemTy, SizeRegs));
Owen Anderson47a434f2009-08-05 23:18:46 +00001859 const llvm::Type* STy = llvm::StructType::get(VMContext, LLVMFields, true);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001860 return ABIArgInfo::getCoerce(STy);
1861}
1862
Daniel Dunbar98303b92009-09-13 08:03:58 +00001863static bool isIntegerLikeType(QualType Ty,
1864 ASTContext &Context,
1865 llvm::LLVMContext &VMContext) {
1866 // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
1867 // is called integer-like if its size is less than or equal to one word, and
1868 // the offset of each of its addressable sub-fields is zero.
1869
1870 uint64_t Size = Context.getTypeSize(Ty);
1871
1872 // Check that the type fits in a word.
1873 if (Size > 32)
1874 return false;
1875
1876 // FIXME: Handle vector types!
1877 if (Ty->isVectorType())
1878 return false;
1879
Daniel Dunbarb0d58192009-09-14 02:20:34 +00001880 // Float types are never treated as "integer like".
1881 if (Ty->isRealFloatingType())
1882 return false;
1883
Daniel Dunbar98303b92009-09-13 08:03:58 +00001884 // If this is a builtin or pointer type then it is ok.
John McCall183700f2009-09-21 23:43:11 +00001885 if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
Daniel Dunbar98303b92009-09-13 08:03:58 +00001886 return true;
1887
Daniel Dunbar45815812010-02-01 23:31:26 +00001888 // Small complex integer types are "integer like".
1889 if (const ComplexType *CT = Ty->getAs<ComplexType>())
1890 return isIntegerLikeType(CT->getElementType(), Context, VMContext);
Daniel Dunbar98303b92009-09-13 08:03:58 +00001891
1892 // Single element and zero sized arrays should be allowed, by the definition
1893 // above, but they are not.
1894
1895 // Otherwise, it must be a record type.
1896 const RecordType *RT = Ty->getAs<RecordType>();
1897 if (!RT) return false;
1898
1899 // Ignore records with flexible arrays.
1900 const RecordDecl *RD = RT->getDecl();
1901 if (RD->hasFlexibleArrayMember())
1902 return false;
1903
1904 // Check that all sub-fields are at offset 0, and are themselves "integer
1905 // like".
1906 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
1907
1908 bool HadField = false;
1909 unsigned idx = 0;
1910 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1911 i != e; ++i, ++idx) {
1912 const FieldDecl *FD = *i;
1913
Daniel Dunbar679855a2010-01-29 03:22:29 +00001914 // Bit-fields are not addressable, we only need to verify they are "integer
1915 // like". We still have to disallow a subsequent non-bitfield, for example:
1916 // struct { int : 0; int x }
1917 // is non-integer like according to gcc.
1918 if (FD->isBitField()) {
1919 if (!RD->isUnion())
1920 HadField = true;
Daniel Dunbar98303b92009-09-13 08:03:58 +00001921
Daniel Dunbar679855a2010-01-29 03:22:29 +00001922 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
1923 return false;
Daniel Dunbar98303b92009-09-13 08:03:58 +00001924
Daniel Dunbar679855a2010-01-29 03:22:29 +00001925 continue;
Daniel Dunbar98303b92009-09-13 08:03:58 +00001926 }
1927
Daniel Dunbar679855a2010-01-29 03:22:29 +00001928 // Check if this field is at offset 0.
1929 if (Layout.getFieldOffset(idx) != 0)
1930 return false;
1931
Daniel Dunbar98303b92009-09-13 08:03:58 +00001932 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
1933 return false;
1934
Daniel Dunbar679855a2010-01-29 03:22:29 +00001935 // Only allow at most one field in a structure. This doesn't match the
1936 // wording above, but follows gcc in situations with a field following an
1937 // empty structure.
Daniel Dunbar98303b92009-09-13 08:03:58 +00001938 if (!RD->isUnion()) {
1939 if (HadField)
1940 return false;
1941
1942 HadField = true;
1943 }
1944 }
1945
1946 return true;
1947}
1948
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001949ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001950 ASTContext &Context,
1951 llvm::LLVMContext &VMContext) const {
Daniel Dunbar98303b92009-09-13 08:03:58 +00001952 if (RetTy->isVoidType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001953 return ABIArgInfo::getIgnore();
Daniel Dunbar98303b92009-09-13 08:03:58 +00001954
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001955 if (!CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1956 // Treat an enum type as its underlying type.
1957 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
1958 RetTy = EnumTy->getDecl()->getIntegerType();
1959
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001960 return (RetTy->isPromotableIntegerType() ?
1961 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001962 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00001963
Rafael Espindola0eb1d972010-06-08 02:42:08 +00001964 // Structures with either a non-trivial destructor or a non-trivial
1965 // copy constructor are always indirect.
1966 if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
1967 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
1968
Daniel Dunbar98303b92009-09-13 08:03:58 +00001969 // Are we following APCS?
1970 if (getABIKind() == APCS) {
1971 if (isEmptyRecord(Context, RetTy, false))
1972 return ABIArgInfo::getIgnore();
1973
Daniel Dunbar4cc753f2010-02-01 23:31:19 +00001974 // Complex types are all returned as packed integers.
1975 //
1976 // FIXME: Consider using 2 x vector types if the back end handles them
1977 // correctly.
1978 if (RetTy->isAnyComplexType())
1979 return ABIArgInfo::getCoerce(llvm::IntegerType::get(
1980 VMContext, Context.getTypeSize(RetTy)));
1981
Daniel Dunbar98303b92009-09-13 08:03:58 +00001982 // Integer like structures are returned in r0.
1983 if (isIntegerLikeType(RetTy, Context, VMContext)) {
1984 // Return in the smallest viable integer type.
1985 uint64_t Size = Context.getTypeSize(RetTy);
1986 if (Size <= 8)
1987 return ABIArgInfo::getCoerce(llvm::Type::getInt8Ty(VMContext));
1988 if (Size <= 16)
1989 return ABIArgInfo::getCoerce(llvm::Type::getInt16Ty(VMContext));
1990 return ABIArgInfo::getCoerce(llvm::Type::getInt32Ty(VMContext));
1991 }
1992
1993 // Otherwise return in memory.
1994 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001995 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00001996
1997 // Otherwise this is an AAPCS variant.
1998
Daniel Dunbar16a08082009-09-14 00:56:55 +00001999 if (isEmptyRecord(Context, RetTy, true))
2000 return ABIArgInfo::getIgnore();
2001
Daniel Dunbar98303b92009-09-13 08:03:58 +00002002 // Aggregates <= 4 bytes are returned in r0; other aggregates
2003 // are returned indirectly.
2004 uint64_t Size = Context.getTypeSize(RetTy);
Daniel Dunbar16a08082009-09-14 00:56:55 +00002005 if (Size <= 32) {
2006 // Return in the smallest viable integer type.
2007 if (Size <= 8)
2008 return ABIArgInfo::getCoerce(llvm::Type::getInt8Ty(VMContext));
2009 if (Size <= 16)
2010 return ABIArgInfo::getCoerce(llvm::Type::getInt16Ty(VMContext));
Daniel Dunbar98303b92009-09-13 08:03:58 +00002011 return ABIArgInfo::getCoerce(llvm::Type::getInt32Ty(VMContext));
Daniel Dunbar16a08082009-09-14 00:56:55 +00002012 }
2013
Daniel Dunbar98303b92009-09-13 08:03:58 +00002014 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002015}
2016
2017llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner77b89b82010-06-27 07:15:29 +00002018 CodeGenFunction &CGF) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002019 // FIXME: Need to handle alignment
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00002020 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Owen Anderson96e0fc72009-07-29 22:16:19 +00002021 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002022
2023 CGBuilderTy &Builder = CGF.Builder;
2024 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2025 "ap");
2026 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2027 llvm::Type *PTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00002028 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002029 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2030
2031 uint64_t Offset =
2032 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
2033 llvm::Value *NextAddr =
Chris Lattner77b89b82010-06-27 07:15:29 +00002034 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002035 "ap.next");
2036 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2037
2038 return AddrTyped;
2039}
2040
2041ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00002042 ASTContext &Context,
2043 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002044 if (RetTy->isVoidType()) {
2045 return ABIArgInfo::getIgnore();
2046 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
2047 return ABIArgInfo::getIndirect(0);
2048 } else {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00002049 // Treat an enum type as its underlying type.
2050 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2051 RetTy = EnumTy->getDecl()->getIntegerType();
2052
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00002053 return (RetTy->isPromotableIntegerType() ?
2054 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002055 }
2056}
2057
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002058//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002059// SystemZ ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002060//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002061
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002062namespace {
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002063
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002064class SystemZABIInfo : public ABIInfo {
2065 bool isPromotableIntegerType(QualType Ty) const;
2066
2067 ABIArgInfo classifyReturnType(QualType RetTy, ASTContext &Context,
2068 llvm::LLVMContext &VMContext) const;
2069
2070 ABIArgInfo classifyArgumentType(QualType RetTy, ASTContext &Context,
2071 llvm::LLVMContext &VMContext) const;
2072
2073 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
2074 llvm::LLVMContext &VMContext) const {
2075 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(),
2076 Context, VMContext);
2077 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2078 it != ie; ++it)
2079 it->info = classifyArgumentType(it->type, Context, VMContext);
2080 }
2081
2082 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2083 CodeGenFunction &CGF) const;
2084};
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002085
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002086class SystemZTargetCodeGenInfo : public TargetCodeGenInfo {
2087public:
Douglas Gregor568bb2d2010-01-22 15:41:14 +00002088 SystemZTargetCodeGenInfo():TargetCodeGenInfo(new SystemZABIInfo()) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002089};
2090
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002091}
2092
2093bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const {
2094 // SystemZ ABI requires all 8, 16 and 32 bit quantities to be extended.
John McCall183700f2009-09-21 23:43:11 +00002095 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002096 switch (BT->getKind()) {
2097 case BuiltinType::Bool:
2098 case BuiltinType::Char_S:
2099 case BuiltinType::Char_U:
2100 case BuiltinType::SChar:
2101 case BuiltinType::UChar:
2102 case BuiltinType::Short:
2103 case BuiltinType::UShort:
2104 case BuiltinType::Int:
2105 case BuiltinType::UInt:
2106 return true;
2107 default:
2108 return false;
2109 }
2110 return false;
2111}
2112
2113llvm::Value *SystemZABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2114 CodeGenFunction &CGF) const {
2115 // FIXME: Implement
2116 return 0;
2117}
2118
2119
2120ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy,
2121 ASTContext &Context,
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002122 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002123 if (RetTy->isVoidType()) {
2124 return ABIArgInfo::getIgnore();
2125 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
2126 return ABIArgInfo::getIndirect(0);
2127 } else {
2128 return (isPromotableIntegerType(RetTy) ?
2129 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2130 }
2131}
2132
2133ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty,
2134 ASTContext &Context,
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002135 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002136 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
2137 return ABIArgInfo::getIndirect(0);
2138 } else {
2139 return (isPromotableIntegerType(Ty) ?
2140 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2141 }
2142}
2143
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002144//===----------------------------------------------------------------------===//
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002145// MSP430 ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002146//===----------------------------------------------------------------------===//
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002147
2148namespace {
2149
2150class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
2151public:
Douglas Gregor568bb2d2010-01-22 15:41:14 +00002152 MSP430TargetCodeGenInfo():TargetCodeGenInfo(new DefaultABIInfo()) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002153 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2154 CodeGen::CodeGenModule &M) const;
2155};
2156
2157}
2158
2159void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
2160 llvm::GlobalValue *GV,
2161 CodeGen::CodeGenModule &M) const {
2162 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
2163 if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) {
2164 // Handle 'interrupt' attribute:
2165 llvm::Function *F = cast<llvm::Function>(GV);
2166
2167 // Step 1: Set ISR calling convention.
2168 F->setCallingConv(llvm::CallingConv::MSP430_INTR);
2169
2170 // Step 2: Add attributes goodness.
2171 F->addFnAttr(llvm::Attribute::NoInline);
2172
2173 // Step 3: Emit ISR vector alias.
2174 unsigned Num = attr->getNumber() + 0xffe0;
2175 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
2176 "vector_" +
2177 llvm::LowercaseString(llvm::utohexstr(Num)),
2178 GV, &M.getModule());
2179 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002180 }
2181}
2182
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002183//===----------------------------------------------------------------------===//
John McCallaeeb7012010-05-27 06:19:26 +00002184// MIPS ABI Implementation. This works for both little-endian and
2185// big-endian variants.
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002186//===----------------------------------------------------------------------===//
2187
John McCallaeeb7012010-05-27 06:19:26 +00002188namespace {
2189class MIPSTargetCodeGenInfo : public TargetCodeGenInfo {
2190public:
2191 MIPSTargetCodeGenInfo(): TargetCodeGenInfo(new DefaultABIInfo()) {}
2192
2193 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
2194 return 29;
2195 }
2196
2197 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2198 llvm::Value *Address) const;
2199};
2200}
2201
2202bool
2203MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2204 llvm::Value *Address) const {
2205 // This information comes from gcc's implementation, which seems to
2206 // as canonical as it gets.
2207
2208 CodeGen::CGBuilderTy &Builder = CGF.Builder;
2209 llvm::LLVMContext &Context = CGF.getLLVMContext();
2210
2211 // Everything on MIPS is 4 bytes. Double-precision FP registers
2212 // are aliased to pairs of single-precision FP registers.
2213 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
2214 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2215
2216 // 0-31 are the general purpose registers, $0 - $31.
2217 // 32-63 are the floating-point registers, $f0 - $f31.
2218 // 64 and 65 are the multiply/divide registers, $hi and $lo.
2219 // 66 is the (notional, I think) register for signal-handler return.
2220 AssignToArrayRange(Builder, Address, Four8, 0, 65);
2221
2222 // 67-74 are the floating-point status registers, $fcc0 - $fcc7.
2223 // They are one bit wide and ignored here.
2224
2225 // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31.
2226 // (coprocessor 1 is the FP unit)
2227 // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31.
2228 // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31.
2229 // 176-181 are the DSP accumulator registers.
2230 AssignToArrayRange(Builder, Address, Four8, 80, 181);
2231
2232 return false;
2233}
2234
2235
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002236const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() const {
2237 if (TheTargetCodeGenInfo)
2238 return *TheTargetCodeGenInfo;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002239
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002240 // For now we just cache the TargetCodeGenInfo in CodeGenModule and don't
2241 // free it.
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002242
Daniel Dunbar1752ee42009-08-24 09:10:05 +00002243 const llvm::Triple &Triple(getContext().Target.getTriple());
2244 switch (Triple.getArch()) {
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002245 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002246 return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo);
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002247
John McCallaeeb7012010-05-27 06:19:26 +00002248 case llvm::Triple::mips:
2249 case llvm::Triple::mipsel:
2250 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo());
2251
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002252 case llvm::Triple::arm:
2253 case llvm::Triple::thumb:
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002254 // FIXME: We want to know the float calling convention as well.
Daniel Dunbar018ba5a2009-09-14 00:35:03 +00002255 if (strcmp(getContext().Target.getABI(), "apcs-gnu") == 0)
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002256 return *(TheTargetCodeGenInfo =
2257 new ARMTargetCodeGenInfo(ARMABIInfo::APCS));
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002258
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002259 return *(TheTargetCodeGenInfo =
2260 new ARMTargetCodeGenInfo(ARMABIInfo::AAPCS));
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002261
2262 case llvm::Triple::pic16:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002263 return *(TheTargetCodeGenInfo = new PIC16TargetCodeGenInfo());
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002264
John McCallec853ba2010-03-11 00:10:12 +00002265 case llvm::Triple::ppc:
2266 return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo());
2267
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002268 case llvm::Triple::systemz:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002269 return *(TheTargetCodeGenInfo = new SystemZTargetCodeGenInfo());
2270
2271 case llvm::Triple::msp430:
2272 return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo());
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002273
Daniel Dunbar1752ee42009-08-24 09:10:05 +00002274 case llvm::Triple::x86:
Daniel Dunbar1752ee42009-08-24 09:10:05 +00002275 switch (Triple.getOS()) {
Edward O'Callaghan7ee68bd2009-10-20 17:22:50 +00002276 case llvm::Triple::Darwin:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002277 return *(TheTargetCodeGenInfo =
2278 new X86_32TargetCodeGenInfo(Context, true, true));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002279 case llvm::Triple::Cygwin:
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002280 case llvm::Triple::MinGW32:
2281 case llvm::Triple::MinGW64:
Edward O'Callaghan727e2682009-10-21 11:58:24 +00002282 case llvm::Triple::AuroraUX:
2283 case llvm::Triple::DragonFly:
David Chisnall75c135a2009-09-03 01:48:05 +00002284 case llvm::Triple::FreeBSD:
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002285 case llvm::Triple::OpenBSD:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002286 return *(TheTargetCodeGenInfo =
2287 new X86_32TargetCodeGenInfo(Context, false, true));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002288
2289 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002290 return *(TheTargetCodeGenInfo =
2291 new X86_32TargetCodeGenInfo(Context, false, false));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002292 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002293
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002294 case llvm::Triple::x86_64:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002295 return *(TheTargetCodeGenInfo = new X86_64TargetCodeGenInfo());
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002296 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002297}