blob: bcd332ac95fe2076c67e3ad8d4bc8e61061db109 [file] [log] [blame]
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001//===---- TargetInfo.cpp - Encapsulate target details -----------*- C++ -*-===//
Anton Korobeynikov244360d2009-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 Korobeynikov55bcea12010-01-10 12:58:08 +000015#include "TargetInfo.h"
Anton Korobeynikov244360d2009-06-05 22:08:42 +000016#include "ABIInfo.h"
17#include "CodeGenFunction.h"
Anders Carlsson15b73de2009-07-18 19:43:29 +000018#include "clang/AST/RecordLayout.h"
Anton Korobeynikov244360d2009-06-05 22:08:42 +000019#include "llvm/Type.h"
Anton Korobeynikov55bcea12010-01-10 12:58:08 +000020#include "llvm/ADT/StringExtras.h"
Daniel Dunbare3532f82009-08-24 08:52:16 +000021#include "llvm/ADT/Triple.h"
Daniel Dunbar7230fa52009-12-03 09:13:49 +000022#include "llvm/Support/raw_ostream.h"
Anton Korobeynikov244360d2009-06-05 22:08:42 +000023using namespace clang;
24using namespace CodeGen;
25
26ABIInfo::~ABIInfo() {}
27
28void ABIArgInfo::dump() const {
Daniel Dunbar7230fa52009-12-03 09:13:49 +000029 llvm::raw_ostream &OS = llvm::errs();
30 OS << "(ABIArgInfo Kind=";
Anton Korobeynikov244360d2009-06-05 22:08:42 +000031 switch (TheKind) {
32 case Direct:
Daniel Dunbar7230fa52009-12-03 09:13:49 +000033 OS << "Direct";
Anton Korobeynikov244360d2009-06-05 22:08:42 +000034 break;
Anton Korobeynikov18adbf52009-06-06 09:36:29 +000035 case Extend:
Daniel Dunbar7230fa52009-12-03 09:13:49 +000036 OS << "Extend";
Anton Korobeynikov18adbf52009-06-06 09:36:29 +000037 break;
Anton Korobeynikov244360d2009-06-05 22:08:42 +000038 case Ignore:
Daniel Dunbar7230fa52009-12-03 09:13:49 +000039 OS << "Ignore";
Anton Korobeynikov244360d2009-06-05 22:08:42 +000040 break;
41 case Coerce:
Daniel Dunbar7230fa52009-12-03 09:13:49 +000042 OS << "Coerce Type=";
43 getCoerceToType()->print(OS);
Anton Korobeynikov244360d2009-06-05 22:08:42 +000044 break;
45 case Indirect:
Daniel Dunbar557893d2010-04-21 19:10:51 +000046 OS << "Indirect Align=" << getIndirectAlign()
47 << " Byal=" << getIndirectByVal();
Anton Korobeynikov244360d2009-06-05 22:08:42 +000048 break;
49 case Expand:
Daniel Dunbar7230fa52009-12-03 09:13:49 +000050 OS << "Expand";
Anton Korobeynikov244360d2009-06-05 22:08:42 +000051 break;
52 }
Daniel Dunbar7230fa52009-12-03 09:13:49 +000053 OS << ")\n";
Anton Korobeynikov244360d2009-06-05 22:08:42 +000054}
55
Anton Korobeynikov55bcea12010-01-10 12:58:08 +000056TargetCodeGenInfo::~TargetCodeGenInfo() { delete Info; }
57
Daniel Dunbar626f1d82009-09-13 08:03:58 +000058static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays);
Anton Korobeynikov244360d2009-06-05 22:08:42 +000059
60/// isEmptyField - Return true iff a the field is "empty", that is it
61/// is an unnamed bit-field or an (array of) empty record(s).
Daniel Dunbar626f1d82009-09-13 08:03:58 +000062static bool isEmptyField(ASTContext &Context, const FieldDecl *FD,
63 bool AllowArrays) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +000064 if (FD->isUnnamedBitfield())
65 return true;
66
67 QualType FT = FD->getType();
Anton Korobeynikov244360d2009-06-05 22:08:42 +000068
Daniel Dunbar626f1d82009-09-13 08:03:58 +000069 // Constant arrays of empty records count as empty, strip them off.
70 if (AllowArrays)
71 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT))
72 FT = AT->getElementType();
73
74 return isEmptyRecord(Context, FT, AllowArrays);
Anton Korobeynikov244360d2009-06-05 22:08:42 +000075}
76
77/// isEmptyRecord - Return true iff a structure contains only empty
78/// fields. Note that a structure with a flexible array member is not
79/// considered empty.
Daniel Dunbar626f1d82009-09-13 08:03:58 +000080static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +000081 const RecordType *RT = T->getAs<RecordType>();
Anton Korobeynikov244360d2009-06-05 22:08:42 +000082 if (!RT)
83 return 0;
84 const RecordDecl *RD = RT->getDecl();
85 if (RD->hasFlexibleArrayMember())
86 return false;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +000087 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
88 i != e; ++i)
Daniel Dunbar626f1d82009-09-13 08:03:58 +000089 if (!isEmptyField(Context, *i, AllowArrays))
Anton Korobeynikov244360d2009-06-05 22:08:42 +000090 return false;
91 return true;
92}
93
Anders Carlsson20759ad2009-09-16 15:53:40 +000094/// hasNonTrivialDestructorOrCopyConstructor - Determine if a type has either
95/// a non-trivial destructor or a non-trivial copy constructor.
96static bool hasNonTrivialDestructorOrCopyConstructor(const RecordType *RT) {
97 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
98 if (!RD)
99 return false;
100
101 return !RD->hasTrivialDestructor() || !RD->hasTrivialCopyConstructor();
102}
103
104/// isRecordWithNonTrivialDestructorOrCopyConstructor - Determine if a type is
105/// a record type with either a non-trivial destructor or a non-trivial copy
106/// constructor.
107static bool isRecordWithNonTrivialDestructorOrCopyConstructor(QualType T) {
108 const RecordType *RT = T->getAs<RecordType>();
109 if (!RT)
110 return false;
111
112 return hasNonTrivialDestructorOrCopyConstructor(RT);
113}
114
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000115/// isSingleElementStruct - Determine if a structure is a "single
116/// element struct", i.e. it has exactly one non-empty field or
117/// exactly one field which is itself a single element
118/// struct. Structures with flexible array members are never
119/// considered single element structs.
120///
121/// \return The field declaration for the single non-empty field, if
122/// it exists.
123static const Type *isSingleElementStruct(QualType T, ASTContext &Context) {
124 const RecordType *RT = T->getAsStructureType();
125 if (!RT)
126 return 0;
127
128 const RecordDecl *RD = RT->getDecl();
129 if (RD->hasFlexibleArrayMember())
130 return 0;
131
132 const Type *Found = 0;
Daniel Dunbar12ebb472010-05-11 21:15:36 +0000133
134 // If this is a C++ record, check the bases first.
135 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
136 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
137 e = CXXRD->bases_end(); i != e; ++i) {
138 const CXXRecordDecl *Base =
139 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
140 // Ignore empty records.
141 if (Base->isEmpty())
142 continue;
143
144 // If we already found an element then this isn't a single-element struct.
145 if (Found)
146 return 0;
147
148 // If this is non-empty and not a single element struct, the composite
149 // cannot be a single element struct.
150 Found = isSingleElementStruct(i->getType(), Context);
151 if (!Found)
152 return 0;
153 }
154 }
155
156 // Check for single element.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000157 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
158 i != e; ++i) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000159 const FieldDecl *FD = *i;
160 QualType FT = FD->getType();
161
162 // Ignore empty fields.
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000163 if (isEmptyField(Context, FD, true))
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000164 continue;
165
166 // If we already found an element then this isn't a single-element
167 // struct.
168 if (Found)
169 return 0;
170
171 // Treat single element arrays as the element.
172 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
173 if (AT->getSize().getZExtValue() != 1)
174 break;
175 FT = AT->getElementType();
176 }
177
178 if (!CodeGenFunction::hasAggregateLLVMType(FT)) {
179 Found = FT.getTypePtr();
180 } else {
181 Found = isSingleElementStruct(FT, Context);
182 if (!Found)
183 return 0;
184 }
185 }
186
187 return Found;
188}
189
190static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
Daniel Dunbarb3b1e532009-09-24 05:12:36 +0000191 if (!Ty->getAs<BuiltinType>() && !Ty->isAnyPointerType() &&
192 !Ty->isAnyComplexType() && !Ty->isEnumeralType() &&
193 !Ty->isBlockPointerType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000194 return false;
195
196 uint64_t Size = Context.getTypeSize(Ty);
197 return Size == 32 || Size == 64;
198}
199
Daniel Dunbar11c08c82009-11-09 01:33:53 +0000200/// canExpandIndirectArgument - Test whether an argument type which is to be
201/// passed indirectly (on the stack) would have the equivalent layout if it was
202/// expanded into separate arguments. If so, we prefer to do the latter to avoid
203/// inhibiting optimizations.
204///
205// FIXME: This predicate is missing many cases, currently it just follows
206// llvm-gcc (checks that all fields are 32-bit or 64-bit primitive types). We
207// should probably make this smarter, or better yet make the LLVM backend
208// capable of handling it.
209static bool canExpandIndirectArgument(QualType Ty, ASTContext &Context) {
210 // We can only expand structure types.
211 const RecordType *RT = Ty->getAs<RecordType>();
212 if (!RT)
213 return false;
214
215 // We can only expand (C) structures.
216 //
217 // FIXME: This needs to be generalized to handle classes as well.
218 const RecordDecl *RD = RT->getDecl();
219 if (!RD->isStruct() || isa<CXXRecordDecl>(RD))
220 return false;
221
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000222 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
223 i != e; ++i) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000224 const FieldDecl *FD = *i;
225
226 if (!is32Or64BitBasicType(FD->getType(), Context))
227 return false;
228
229 // FIXME: Reject bit-fields wholesale; there are two problems, we don't know
230 // how to expand them yet, and the predicate for telling if a bitfield still
231 // counts as "basic" is more complicated than what we were doing previously.
232 if (FD->isBitField())
233 return false;
234 }
235
236 return true;
237}
238
Eli Friedman3192cc82009-06-13 21:37:10 +0000239static bool typeContainsSSEVector(const RecordDecl *RD, ASTContext &Context) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000240 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
241 i != e; ++i) {
Eli Friedman3192cc82009-06-13 21:37:10 +0000242 const FieldDecl *FD = *i;
243
244 if (FD->getType()->isVectorType() &&
245 Context.getTypeSize(FD->getType()) >= 128)
246 return true;
247
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000248 if (const RecordType* RT = FD->getType()->getAs<RecordType>())
Eli Friedman3192cc82009-06-13 21:37:10 +0000249 if (typeContainsSSEVector(RT->getDecl(), Context))
250 return true;
251 }
252
253 return false;
254}
255
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000256namespace {
257/// DefaultABIInfo - The default implementation for ABI specific
258/// details. This implementation provides information which results in
259/// self-consistent and sensible LLVM IR generation, but does not
260/// conform to any particular ABI.
261class DefaultABIInfo : public ABIInfo {
262 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +0000263 ASTContext &Context,
264 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000265
266 ABIArgInfo classifyArgumentType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +0000267 ASTContext &Context,
268 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000269
Owen Anderson170229f2009-07-14 23:10:40 +0000270 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
271 llvm::LLVMContext &VMContext) const {
272 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context,
273 VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000274 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
275 it != ie; ++it)
Owen Anderson170229f2009-07-14 23:10:40 +0000276 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000277 }
278
279 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
280 CodeGenFunction &CGF) const;
281};
282
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000283class DefaultTargetCodeGenInfo : public TargetCodeGenInfo {
284public:
Douglas Gregor0599df12010-01-22 15:41:14 +0000285 DefaultTargetCodeGenInfo():TargetCodeGenInfo(new DefaultABIInfo()) {}
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000286};
287
288llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
289 CodeGenFunction &CGF) const {
290 return 0;
291}
292
293ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty,
294 ASTContext &Context,
295 llvm::LLVMContext &VMContext) const {
Chris Lattner9723d6c2010-03-11 18:19:55 +0000296 if (CodeGenFunction::hasAggregateLLVMType(Ty))
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000297 return ABIArgInfo::getIndirect(0);
Daniel Dunbar557893d2010-04-21 19:10:51 +0000298
Chris Lattner9723d6c2010-03-11 18:19:55 +0000299 // Treat an enum type as its underlying type.
300 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
301 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregora71cc152010-02-02 20:10:50 +0000302
Chris Lattner9723d6c2010-03-11 18:19:55 +0000303 return (Ty->isPromotableIntegerType() ?
304 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000305}
306
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000307/// X86_32ABIInfo - The X86-32 ABI information.
308class X86_32ABIInfo : public ABIInfo {
309 ASTContext &Context;
David Chisnallde3a0692009-08-17 23:08:21 +0000310 bool IsDarwinVectorABI;
311 bool IsSmallStructInRegABI;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000312
313 static bool isRegisterSize(unsigned Size) {
314 return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
315 }
316
317 static bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context);
318
Daniel Dunbar557893d2010-04-21 19:10:51 +0000319 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
320 /// such that the argument will be passed in memory.
321 ABIArgInfo getIndirectResult(QualType Ty, ASTContext &Context,
322 bool ByVal = true) const;
323
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000324public:
325 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +0000326 ASTContext &Context,
327 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000328
329 ABIArgInfo classifyArgumentType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +0000330 ASTContext &Context,
331 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000332
Owen Anderson170229f2009-07-14 23:10:40 +0000333 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
334 llvm::LLVMContext &VMContext) const {
335 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context,
336 VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000337 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
338 it != ie; ++it)
Owen Anderson170229f2009-07-14 23:10:40 +0000339 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000340 }
341
342 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
343 CodeGenFunction &CGF) const;
344
David Chisnallde3a0692009-08-17 23:08:21 +0000345 X86_32ABIInfo(ASTContext &Context, bool d, bool p)
Mike Stump11289f42009-09-09 15:08:12 +0000346 : ABIInfo(), Context(Context), IsDarwinVectorABI(d),
David Chisnallde3a0692009-08-17 23:08:21 +0000347 IsSmallStructInRegABI(p) {}
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000348};
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000349
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000350class X86_32TargetCodeGenInfo : public TargetCodeGenInfo {
351public:
352 X86_32TargetCodeGenInfo(ASTContext &Context, bool d, bool p)
Douglas Gregor0599df12010-01-22 15:41:14 +0000353 :TargetCodeGenInfo(new X86_32ABIInfo(Context, d, p)) {}
Charles Davis4ea31ab2010-02-13 15:54:06 +0000354
355 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
356 CodeGen::CodeGenModule &CGM) const;
John McCallbeec5a02010-03-06 00:35:14 +0000357
358 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
359 // Darwin uses different dwarf register numbers for EH.
360 if (CGM.isTargetDarwin()) return 5;
361
362 return 4;
363 }
364
365 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
366 llvm::Value *Address) const;
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000367};
368
369}
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000370
371/// shouldReturnTypeInRegister - Determine if the given type should be
372/// passed in a register (for the Darwin ABI).
373bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
374 ASTContext &Context) {
375 uint64_t Size = Context.getTypeSize(Ty);
376
377 // Type must be register sized.
378 if (!isRegisterSize(Size))
379 return false;
380
381 if (Ty->isVectorType()) {
382 // 64- and 128- bit vectors inside structures are not returned in
383 // registers.
384 if (Size == 64 || Size == 128)
385 return false;
386
387 return true;
388 }
389
Daniel Dunbarb3b1e532009-09-24 05:12:36 +0000390 // If this is a builtin, pointer, enum, or complex type, it is ok.
391 if (Ty->getAs<BuiltinType>() || Ty->isAnyPointerType() ||
392 Ty->isAnyComplexType() || Ty->isEnumeralType() ||
393 Ty->isBlockPointerType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000394 return true;
395
396 // Arrays are treated like records.
397 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
398 return shouldReturnTypeInRegister(AT->getElementType(), Context);
399
400 // Otherwise, it must be a record type.
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000401 const RecordType *RT = Ty->getAs<RecordType>();
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000402 if (!RT) return false;
403
Anders Carlsson40446e82010-01-27 03:25:19 +0000404 // FIXME: Traverse bases here too.
405
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000406 // Structure types are passed in register if all fields would be
407 // passed in a register.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000408 for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(),
409 e = RT->getDecl()->field_end(); i != e; ++i) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000410 const FieldDecl *FD = *i;
411
412 // Empty fields are ignored.
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000413 if (isEmptyField(Context, FD, true))
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000414 continue;
415
416 // Check fields recursively.
417 if (!shouldReturnTypeInRegister(FD->getType(), Context))
418 return false;
419 }
420
421 return true;
422}
423
424ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +0000425 ASTContext &Context,
426 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000427 if (RetTy->isVoidType()) {
428 return ABIArgInfo::getIgnore();
John McCall9dd450b2009-09-21 23:43:11 +0000429 } else if (const VectorType *VT = RetTy->getAs<VectorType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000430 // On Darwin, some vectors are returned in registers.
David Chisnallde3a0692009-08-17 23:08:21 +0000431 if (IsDarwinVectorABI) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000432 uint64_t Size = Context.getTypeSize(RetTy);
433
434 // 128-bit vectors are a special case; they are returned in
435 // registers and we need to make sure to pick a type the LLVM
436 // backend will like.
437 if (Size == 128)
Owen Anderson41a75022009-08-13 21:57:51 +0000438 return ABIArgInfo::getCoerce(llvm::VectorType::get(
439 llvm::Type::getInt64Ty(VMContext), 2));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000440
441 // Always return in register if it fits in a general purpose
442 // register, or if it is 64 bits and has a single element.
443 if ((Size == 8 || Size == 16 || Size == 32) ||
444 (Size == 64 && VT->getNumElements() == 1))
Owen Anderson41a75022009-08-13 21:57:51 +0000445 return ABIArgInfo::getCoerce(llvm::IntegerType::get(VMContext, Size));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000446
447 return ABIArgInfo::getIndirect(0);
448 }
449
450 return ABIArgInfo::getDirect();
451 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Anders Carlsson40446e82010-01-27 03:25:19 +0000452 if (const RecordType *RT = RetTy->getAs<RecordType>()) {
Anders Carlsson5789c492009-10-20 22:07:59 +0000453 // Structures with either a non-trivial destructor or a non-trivial
454 // copy constructor are always indirect.
455 if (hasNonTrivialDestructorOrCopyConstructor(RT))
456 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
457
458 // Structures with flexible arrays are always indirect.
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000459 if (RT->getDecl()->hasFlexibleArrayMember())
460 return ABIArgInfo::getIndirect(0);
Anders Carlsson5789c492009-10-20 22:07:59 +0000461 }
462
David Chisnallde3a0692009-08-17 23:08:21 +0000463 // If specified, structs and unions are always indirect.
464 if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000465 return ABIArgInfo::getIndirect(0);
466
467 // Classify "single element" structs as their element type.
468 if (const Type *SeltTy = isSingleElementStruct(RetTy, Context)) {
John McCall9dd450b2009-09-21 23:43:11 +0000469 if (const BuiltinType *BT = SeltTy->getAs<BuiltinType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000470 if (BT->isIntegerType()) {
471 // We need to use the size of the structure, padding
472 // bit-fields can adjust that to be larger than the single
473 // element type.
474 uint64_t Size = Context.getTypeSize(RetTy);
Owen Anderson170229f2009-07-14 23:10:40 +0000475 return ABIArgInfo::getCoerce(
Owen Anderson41a75022009-08-13 21:57:51 +0000476 llvm::IntegerType::get(VMContext, (unsigned) Size));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000477 } else if (BT->getKind() == BuiltinType::Float) {
478 assert(Context.getTypeSize(RetTy) == Context.getTypeSize(SeltTy) &&
479 "Unexpect single element structure size!");
Owen Anderson41a75022009-08-13 21:57:51 +0000480 return ABIArgInfo::getCoerce(llvm::Type::getFloatTy(VMContext));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000481 } else if (BT->getKind() == BuiltinType::Double) {
482 assert(Context.getTypeSize(RetTy) == Context.getTypeSize(SeltTy) &&
483 "Unexpect single element structure size!");
Owen Anderson41a75022009-08-13 21:57:51 +0000484 return ABIArgInfo::getCoerce(llvm::Type::getDoubleTy(VMContext));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000485 }
486 } else if (SeltTy->isPointerType()) {
487 // FIXME: It would be really nice if this could come out as the proper
488 // pointer type.
Benjamin Kramerabd5b902009-10-13 10:07:13 +0000489 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000490 return ABIArgInfo::getCoerce(PtrTy);
491 } else if (SeltTy->isVectorType()) {
492 // 64- and 128-bit vectors are never returned in a
493 // register when inside a structure.
494 uint64_t Size = Context.getTypeSize(RetTy);
495 if (Size == 64 || Size == 128)
496 return ABIArgInfo::getIndirect(0);
497
Owen Anderson170229f2009-07-14 23:10:40 +0000498 return classifyReturnType(QualType(SeltTy, 0), Context, VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000499 }
500 }
501
502 // Small structures which are register sized are generally returned
503 // in a register.
504 if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, Context)) {
505 uint64_t Size = Context.getTypeSize(RetTy);
Owen Anderson41a75022009-08-13 21:57:51 +0000506 return ABIArgInfo::getCoerce(llvm::IntegerType::get(VMContext, Size));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000507 }
508
509 return ABIArgInfo::getIndirect(0);
510 } else {
Douglas Gregora71cc152010-02-02 20:10:50 +0000511 // Treat an enum type as its underlying type.
512 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
513 RetTy = EnumTy->getDecl()->getIntegerType();
514
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000515 return (RetTy->isPromotableIntegerType() ?
516 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000517 }
518}
519
Daniel Dunbar557893d2010-04-21 19:10:51 +0000520ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty,
521 ASTContext &Context,
522 bool ByVal) const {
Daniel Dunbar53fac692010-04-21 19:49:55 +0000523 if (!ByVal)
524 return ABIArgInfo::getIndirect(0, false);
525
526 // Compute the byval alignment. We trust the back-end to honor the
527 // minimum ABI alignment for byval, to make cleaner IR.
528 const unsigned MinABIAlign = 4;
529 unsigned Align = Context.getTypeAlign(Ty) / 8;
530 if (Align > MinABIAlign)
531 return ABIArgInfo::getIndirect(Align);
532 return ABIArgInfo::getIndirect(0);
Daniel Dunbar557893d2010-04-21 19:10:51 +0000533}
534
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000535ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
Owen Anderson170229f2009-07-14 23:10:40 +0000536 ASTContext &Context,
537 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000538 // FIXME: Set alignment on indirect arguments.
539 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
540 // Structures with flexible arrays are always indirect.
Anders Carlsson40446e82010-01-27 03:25:19 +0000541 if (const RecordType *RT = Ty->getAs<RecordType>()) {
542 // Structures with either a non-trivial destructor or a non-trivial
543 // copy constructor are always indirect.
544 if (hasNonTrivialDestructorOrCopyConstructor(RT))
Daniel Dunbar557893d2010-04-21 19:10:51 +0000545 return getIndirectResult(Ty, Context, /*ByVal=*/false);
546
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000547 if (RT->getDecl()->hasFlexibleArrayMember())
Daniel Dunbar557893d2010-04-21 19:10:51 +0000548 return getIndirectResult(Ty, Context);
Anders Carlsson40446e82010-01-27 03:25:19 +0000549 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000550
551 // Ignore empty structs.
Eli Friedman3192cc82009-06-13 21:37:10 +0000552 if (Ty->isStructureType() && Context.getTypeSize(Ty) == 0)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000553 return ABIArgInfo::getIgnore();
554
Daniel Dunbar11c08c82009-11-09 01:33:53 +0000555 // Expand small (<= 128-bit) record types when we know that the stack layout
556 // of those arguments will match the struct. This is important because the
557 // LLVM backend isn't smart enough to remove byval, which inhibits many
558 // optimizations.
559 if (Context.getTypeSize(Ty) <= 4*32 &&
560 canExpandIndirectArgument(Ty, Context))
561 return ABIArgInfo::getExpand();
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000562
Daniel Dunbar557893d2010-04-21 19:10:51 +0000563 return getIndirectResult(Ty, Context);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000564 } else {
Douglas Gregora71cc152010-02-02 20:10:50 +0000565 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
566 Ty = EnumTy->getDecl()->getIntegerType();
567
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000568 return (Ty->isPromotableIntegerType() ?
569 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000570 }
571}
572
573llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
574 CodeGenFunction &CGF) const {
Benjamin Kramerabd5b902009-10-13 10:07:13 +0000575 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Owen Anderson9793f0e2009-07-29 22:16:19 +0000576 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000577
578 CGBuilderTy &Builder = CGF.Builder;
579 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
580 "ap");
581 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
582 llvm::Type *PTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +0000583 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000584 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
585
586 uint64_t Offset =
587 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
588 llvm::Value *NextAddr =
Owen Anderson41a75022009-08-13 21:57:51 +0000589 Builder.CreateGEP(Addr, llvm::ConstantInt::get(
590 llvm::Type::getInt32Ty(CGF.getLLVMContext()), Offset),
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000591 "ap.next");
592 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
593
594 return AddrTyped;
595}
596
Charles Davis4ea31ab2010-02-13 15:54:06 +0000597void X86_32TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
598 llvm::GlobalValue *GV,
599 CodeGen::CodeGenModule &CGM) const {
600 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
601 if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
602 // Get the LLVM function.
603 llvm::Function *Fn = cast<llvm::Function>(GV);
604
605 // Now add the 'alignstack' attribute with a value of 16.
606 Fn->addFnAttr(llvm::Attribute::constructStackAlignmentFromInt(16));
607 }
608 }
609}
610
John McCallbeec5a02010-03-06 00:35:14 +0000611bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable(
612 CodeGen::CodeGenFunction &CGF,
613 llvm::Value *Address) const {
614 CodeGen::CGBuilderTy &Builder = CGF.Builder;
615 llvm::LLVMContext &Context = CGF.getLLVMContext();
616
617 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
618 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
619
620 // 0-7 are the eight integer registers; the order is different
621 // on Darwin (for EH), but the range is the same.
622 // 8 is %eip.
623 for (unsigned I = 0, E = 9; I != E; ++I) {
624 llvm::Value *Slot = Builder.CreateConstInBoundsGEP1_32(Address, I);
625 Builder.CreateStore(Four8, Slot);
626 }
627
628 if (CGF.CGM.isTargetDarwin()) {
629 // 12-16 are st(0..4). Not sure why we stop at 4.
630 // These have size 16, which is sizeof(long double) on
631 // platforms with 8-byte alignment for that type.
632 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
633 for (unsigned I = 12, E = 17; I != E; ++I) {
634 llvm::Value *Slot = Builder.CreateConstInBoundsGEP1_32(Address, I);
635 Builder.CreateStore(Sixteen8, Slot);
636 }
637
638 } else {
639 // 9 is %eflags, which doesn't get a size on Darwin for some
640 // reason.
641 Builder.CreateStore(Four8, Builder.CreateConstInBoundsGEP1_32(Address, 9));
642
643 // 11-16 are st(0..5). Not sure why we stop at 5.
644 // These have size 12, which is sizeof(long double) on
645 // platforms with 4-byte alignment for that type.
646 llvm::Value *Twelve8 = llvm::ConstantInt::get(i8, 12);
647 for (unsigned I = 11, E = 17; I != E; ++I) {
648 llvm::Value *Slot = Builder.CreateConstInBoundsGEP1_32(Address, I);
649 Builder.CreateStore(Twelve8, Slot);
650 }
651 }
652
653 return false;
654}
655
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000656namespace {
657/// X86_64ABIInfo - The X86_64 ABI information.
658class X86_64ABIInfo : public ABIInfo {
659 enum Class {
660 Integer = 0,
661 SSE,
662 SSEUp,
663 X87,
664 X87Up,
665 ComplexX87,
666 NoClass,
667 Memory
668 };
669
670 /// merge - Implement the X86_64 ABI merging algorithm.
671 ///
672 /// Merge an accumulating classification \arg Accum with a field
673 /// classification \arg Field.
674 ///
675 /// \param Accum - The accumulating classification. This should
676 /// always be either NoClass or the result of a previous merge
677 /// call. In addition, this should never be Memory (the caller
678 /// should just return Memory for the aggregate).
679 Class merge(Class Accum, Class Field) const;
680
681 /// classify - Determine the x86_64 register classes in which the
682 /// given type T should be passed.
683 ///
684 /// \param Lo - The classification for the parts of the type
685 /// residing in the low word of the containing object.
686 ///
687 /// \param Hi - The classification for the parts of the type
688 /// residing in the high word of the containing object.
689 ///
690 /// \param OffsetBase - The bit offset of this type in the
691 /// containing object. Some parameters are classified different
692 /// depending on whether they straddle an eightbyte boundary.
693 ///
694 /// If a word is unused its result will be NoClass; if a type should
695 /// be passed in Memory then at least the classification of \arg Lo
696 /// will be Memory.
697 ///
698 /// The \arg Lo class will be NoClass iff the argument is ignored.
699 ///
700 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
701 /// also be ComplexX87.
702 void classify(QualType T, ASTContext &Context, uint64_t OffsetBase,
703 Class &Lo, Class &Hi) const;
704
705 /// getCoerceResult - Given a source type \arg Ty and an LLVM type
706 /// to coerce to, chose the best way to pass Ty in the same place
707 /// that \arg CoerceTo would be passed, but while keeping the
708 /// emitted code as simple as possible.
709 ///
710 /// FIXME: Note, this should be cleaned up to just take an enumeration of all
711 /// the ways we might want to pass things, instead of constructing an LLVM
712 /// type. This makes this code more explicit, and it makes it clearer that we
713 /// are also doing this for correctness in the case of passing scalar types.
714 ABIArgInfo getCoerceResult(QualType Ty,
715 const llvm::Type *CoerceTo,
716 ASTContext &Context) const;
717
718 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Daniel Dunbar53fac692010-04-21 19:49:55 +0000719 /// such that the argument will be returned in memory.
720 ABIArgInfo getIndirectReturnResult(QualType Ty, ASTContext &Context) const;
721
722 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000723 /// such that the argument will be passed in memory.
Daniel Dunbar557893d2010-04-21 19:10:51 +0000724 ABIArgInfo getIndirectResult(QualType Ty, ASTContext &Context) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000725
726 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +0000727 ASTContext &Context,
728 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000729
730 ABIArgInfo classifyArgumentType(QualType Ty,
731 ASTContext &Context,
Owen Anderson170229f2009-07-14 23:10:40 +0000732 llvm::LLVMContext &VMContext,
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000733 unsigned &neededInt,
734 unsigned &neededSSE) const;
735
736public:
Owen Anderson170229f2009-07-14 23:10:40 +0000737 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
738 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000739
740 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
741 CodeGenFunction &CGF) const;
742};
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000743
744class X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
745public:
Douglas Gregor0599df12010-01-22 15:41:14 +0000746 X86_64TargetCodeGenInfo():TargetCodeGenInfo(new X86_64ABIInfo()) {}
John McCallbeec5a02010-03-06 00:35:14 +0000747
748 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
749 return 7;
750 }
751
752 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
753 llvm::Value *Address) const {
754 CodeGen::CGBuilderTy &Builder = CGF.Builder;
755 llvm::LLVMContext &Context = CGF.getLLVMContext();
756
757 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
758 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
759
760 // 0-16 are the 16 integer registers.
761 // 17 is %rip.
762 for (unsigned I = 0, E = 17; I != E; ++I) {
763 llvm::Value *Slot = Builder.CreateConstInBoundsGEP1_32(Address, I);
764 Builder.CreateStore(Eight8, Slot);
765 }
766
767 return false;
768 }
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000769};
770
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000771}
772
773X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum,
774 Class Field) const {
775 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
776 // classified recursively so that always two fields are
777 // considered. The resulting class is calculated according to
778 // the classes of the fields in the eightbyte:
779 //
780 // (a) If both classes are equal, this is the resulting class.
781 //
782 // (b) If one of the classes is NO_CLASS, the resulting class is
783 // the other class.
784 //
785 // (c) If one of the classes is MEMORY, the result is the MEMORY
786 // class.
787 //
788 // (d) If one of the classes is INTEGER, the result is the
789 // INTEGER.
790 //
791 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
792 // MEMORY is used as class.
793 //
794 // (f) Otherwise class SSE is used.
795
796 // Accum should never be memory (we should have returned) or
797 // ComplexX87 (because this cannot be passed in a structure).
798 assert((Accum != Memory && Accum != ComplexX87) &&
799 "Invalid accumulated classification during merge.");
800 if (Accum == Field || Field == NoClass)
801 return Accum;
802 else if (Field == Memory)
803 return Memory;
804 else if (Accum == NoClass)
805 return Field;
806 else if (Accum == Integer || Field == Integer)
807 return Integer;
808 else if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
809 Accum == X87 || Accum == X87Up)
810 return Memory;
811 else
812 return SSE;
813}
814
815void X86_64ABIInfo::classify(QualType Ty,
816 ASTContext &Context,
817 uint64_t OffsetBase,
818 Class &Lo, Class &Hi) const {
819 // FIXME: This code can be simplified by introducing a simple value class for
820 // Class pairs with appropriate constructor methods for the various
821 // situations.
822
823 // FIXME: Some of the split computations are wrong; unaligned vectors
824 // shouldn't be passed in registers for example, so there is no chance they
825 // can straddle an eightbyte. Verify & simplify.
826
827 Lo = Hi = NoClass;
828
829 Class &Current = OffsetBase < 64 ? Lo : Hi;
830 Current = Memory;
831
John McCall9dd450b2009-09-21 23:43:11 +0000832 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000833 BuiltinType::Kind k = BT->getKind();
834
835 if (k == BuiltinType::Void) {
836 Current = NoClass;
837 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
838 Lo = Integer;
839 Hi = Integer;
840 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
841 Current = Integer;
842 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
843 Current = SSE;
844 } else if (k == BuiltinType::LongDouble) {
845 Lo = X87;
846 Hi = X87Up;
847 }
848 // FIXME: _Decimal32 and _Decimal64 are SSE.
849 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
John McCall9dd450b2009-09-21 23:43:11 +0000850 } else if (const EnumType *ET = Ty->getAs<EnumType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000851 // Classify the underlying integer type.
852 classify(ET->getDecl()->getIntegerType(), Context, OffsetBase, Lo, Hi);
853 } else if (Ty->hasPointerRepresentation()) {
854 Current = Integer;
John McCall9dd450b2009-09-21 23:43:11 +0000855 } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000856 uint64_t Size = Context.getTypeSize(VT);
857 if (Size == 32) {
858 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
859 // float> as integer.
860 Current = Integer;
861
862 // If this type crosses an eightbyte boundary, it should be
863 // split.
864 uint64_t EB_Real = (OffsetBase) / 64;
865 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
866 if (EB_Real != EB_Imag)
867 Hi = Lo;
868 } else if (Size == 64) {
869 // gcc passes <1 x double> in memory. :(
870 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
871 return;
872
873 // gcc passes <1 x long long> as INTEGER.
874 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong))
875 Current = Integer;
876 else
877 Current = SSE;
878
879 // If this type crosses an eightbyte boundary, it should be
880 // split.
881 if (OffsetBase && OffsetBase != 64)
882 Hi = Lo;
883 } else if (Size == 128) {
884 Lo = SSE;
885 Hi = SSEUp;
886 }
John McCall9dd450b2009-09-21 23:43:11 +0000887 } else if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000888 QualType ET = Context.getCanonicalType(CT->getElementType());
889
890 uint64_t Size = Context.getTypeSize(Ty);
891 if (ET->isIntegralType()) {
892 if (Size <= 64)
893 Current = Integer;
894 else if (Size <= 128)
895 Lo = Hi = Integer;
896 } else if (ET == Context.FloatTy)
897 Current = SSE;
898 else if (ET == Context.DoubleTy)
899 Lo = Hi = SSE;
900 else if (ET == Context.LongDoubleTy)
901 Current = ComplexX87;
902
903 // If this complex type crosses an eightbyte boundary then it
904 // should be split.
905 uint64_t EB_Real = (OffsetBase) / 64;
906 uint64_t EB_Imag = (OffsetBase + Context.getTypeSize(ET)) / 64;
907 if (Hi == NoClass && EB_Real != EB_Imag)
908 Hi = Lo;
909 } else if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
910 // Arrays are treated like structures.
911
912 uint64_t Size = Context.getTypeSize(Ty);
913
914 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
915 // than two eightbytes, ..., it has class MEMORY.
916 if (Size > 128)
917 return;
918
919 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
920 // fields, it has class MEMORY.
921 //
922 // Only need to check alignment of array base.
923 if (OffsetBase % Context.getTypeAlign(AT->getElementType()))
924 return;
925
926 // Otherwise implement simplified merge. We could be smarter about
927 // this, but it isn't worth it and would be harder to verify.
928 Current = NoClass;
929 uint64_t EltSize = Context.getTypeSize(AT->getElementType());
930 uint64_t ArraySize = AT->getSize().getZExtValue();
931 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
932 Class FieldLo, FieldHi;
933 classify(AT->getElementType(), Context, Offset, FieldLo, FieldHi);
934 Lo = merge(Lo, FieldLo);
935 Hi = merge(Hi, FieldHi);
936 if (Lo == Memory || Hi == Memory)
937 break;
938 }
939
940 // Do post merger cleanup (see below). Only case we worry about is Memory.
941 if (Hi == Memory)
942 Lo = Memory;
943 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000944 } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000945 uint64_t Size = Context.getTypeSize(Ty);
946
947 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
948 // than two eightbytes, ..., it has class MEMORY.
949 if (Size > 128)
950 return;
951
Anders Carlsson20759ad2009-09-16 15:53:40 +0000952 // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
953 // copy constructor or a non-trivial destructor, it is passed by invisible
954 // reference.
955 if (hasNonTrivialDestructorOrCopyConstructor(RT))
956 return;
Daniel Dunbare1cd0152009-11-22 23:01:23 +0000957
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000958 const RecordDecl *RD = RT->getDecl();
959
960 // Assume variable sized types are passed in memory.
961 if (RD->hasFlexibleArrayMember())
962 return;
963
964 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
965
966 // Reset Lo class, this will be recomputed.
967 Current = NoClass;
Daniel Dunbare1cd0152009-11-22 23:01:23 +0000968
969 // If this is a C++ record, classify the bases first.
970 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
971 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
972 e = CXXRD->bases_end(); i != e; ++i) {
973 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
974 "Unexpected base class!");
975 const CXXRecordDecl *Base =
976 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
977
978 // Classify this field.
979 //
980 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
981 // single eightbyte, each is classified separately. Each eightbyte gets
982 // initialized to class NO_CLASS.
983 Class FieldLo, FieldHi;
984 uint64_t Offset = OffsetBase + Layout.getBaseClassOffset(Base);
985 classify(i->getType(), Context, Offset, FieldLo, FieldHi);
986 Lo = merge(Lo, FieldLo);
987 Hi = merge(Hi, FieldHi);
988 if (Lo == Memory || Hi == Memory)
989 break;
990 }
Daniel Dunbar3780f0b2009-12-22 01:19:25 +0000991
992 // If this record has no fields but isn't empty, classify as INTEGER.
993 if (RD->field_empty() && Size)
994 Current = Integer;
Daniel Dunbare1cd0152009-11-22 23:01:23 +0000995 }
996
997 // Classify the fields one at a time, merging the results.
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000998 unsigned idx = 0;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000999 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1000 i != e; ++i, ++idx) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001001 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
1002 bool BitField = i->isBitField();
1003
1004 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
1005 // fields, it has class MEMORY.
1006 //
1007 // Note, skip this test for bit-fields, see below.
1008 if (!BitField && Offset % Context.getTypeAlign(i->getType())) {
1009 Lo = Memory;
1010 return;
1011 }
1012
1013 // Classify this field.
1014 //
1015 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
1016 // exceeds a single eightbyte, each is classified
1017 // separately. Each eightbyte gets initialized to class
1018 // NO_CLASS.
1019 Class FieldLo, FieldHi;
1020
1021 // Bit-fields require special handling, they do not force the
1022 // structure to be passed in memory even if unaligned, and
1023 // therefore they can straddle an eightbyte.
1024 if (BitField) {
1025 // Ignore padding bit-fields.
1026 if (i->isUnnamedBitfield())
1027 continue;
1028
1029 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
1030 uint64_t Size = i->getBitWidth()->EvaluateAsInt(Context).getZExtValue();
1031
1032 uint64_t EB_Lo = Offset / 64;
1033 uint64_t EB_Hi = (Offset + Size - 1) / 64;
1034 FieldLo = FieldHi = NoClass;
1035 if (EB_Lo) {
1036 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
1037 FieldLo = NoClass;
1038 FieldHi = Integer;
1039 } else {
1040 FieldLo = Integer;
1041 FieldHi = EB_Hi ? Integer : NoClass;
1042 }
1043 } else
1044 classify(i->getType(), Context, Offset, FieldLo, FieldHi);
1045 Lo = merge(Lo, FieldLo);
1046 Hi = merge(Hi, FieldHi);
1047 if (Lo == Memory || Hi == Memory)
1048 break;
1049 }
1050
1051 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
1052 //
1053 // (a) If one of the classes is MEMORY, the whole argument is
1054 // passed in memory.
1055 //
1056 // (b) If SSEUP is not preceeded by SSE, it is converted to SSE.
1057
1058 // The first of these conditions is guaranteed by how we implement
1059 // the merge (just bail).
1060 //
1061 // The second condition occurs in the case of unions; for example
1062 // union { _Complex double; unsigned; }.
1063 if (Hi == Memory)
1064 Lo = Memory;
1065 if (Hi == SSEUp && Lo != SSE)
1066 Hi = SSE;
1067 }
1068}
1069
1070ABIArgInfo X86_64ABIInfo::getCoerceResult(QualType Ty,
1071 const llvm::Type *CoerceTo,
1072 ASTContext &Context) const {
Owen Anderson41a75022009-08-13 21:57:51 +00001073 if (CoerceTo == llvm::Type::getInt64Ty(CoerceTo->getContext())) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001074 // Integer and pointer types will end up in a general purpose
1075 // register.
Douglas Gregora71cc152010-02-02 20:10:50 +00001076
1077 // Treat an enum type as its underlying type.
1078 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1079 Ty = EnumTy->getDecl()->getIntegerType();
1080
Anders Carlsson03747422009-09-26 03:56:53 +00001081 if (Ty->isIntegralType() || Ty->hasPointerRepresentation())
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00001082 return (Ty->isPromotableIntegerType() ?
1083 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Owen Anderson41a75022009-08-13 21:57:51 +00001084 } else if (CoerceTo == llvm::Type::getDoubleTy(CoerceTo->getContext())) {
John McCall8ee376f2010-02-24 07:14:12 +00001085 assert(Ty.isCanonical() && "should always have a canonical type here");
1086 assert(!Ty.hasQualifiers() && "should never have a qualified type here");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001087
1088 // Float and double end up in a single SSE reg.
John McCall8ee376f2010-02-24 07:14:12 +00001089 if (Ty == Context.FloatTy || Ty == Context.DoubleTy)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001090 return ABIArgInfo::getDirect();
1091
1092 }
1093
1094 return ABIArgInfo::getCoerce(CoerceTo);
1095}
1096
Daniel Dunbar53fac692010-04-21 19:49:55 +00001097ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty,
1098 ASTContext &Context) const {
1099 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1100 // place naturally.
1101 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
1102 // Treat an enum type as its underlying type.
1103 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1104 Ty = EnumTy->getDecl()->getIntegerType();
1105
1106 return (Ty->isPromotableIntegerType() ?
1107 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1108 }
1109
1110 return ABIArgInfo::getIndirect(0);
1111}
1112
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001113ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty,
1114 ASTContext &Context) const {
1115 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1116 // place naturally.
Douglas Gregora71cc152010-02-02 20:10:50 +00001117 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
1118 // Treat an enum type as its underlying type.
1119 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1120 Ty = EnumTy->getDecl()->getIntegerType();
1121
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00001122 return (Ty->isPromotableIntegerType() ?
1123 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregora71cc152010-02-02 20:10:50 +00001124 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001125
Daniel Dunbar53fac692010-04-21 19:49:55 +00001126 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
1127 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Anders Carlsson20759ad2009-09-16 15:53:40 +00001128
Daniel Dunbar53fac692010-04-21 19:49:55 +00001129 // Compute the byval alignment. We trust the back-end to honor the
1130 // minimum ABI alignment for byval, to make cleaner IR.
1131 const unsigned MinABIAlign = 8;
1132 unsigned Align = Context.getTypeAlign(Ty) / 8;
1133 if (Align > MinABIAlign)
1134 return ABIArgInfo::getIndirect(Align);
1135 return ABIArgInfo::getIndirect(0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001136}
1137
1138ABIArgInfo X86_64ABIInfo::classifyReturnType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +00001139 ASTContext &Context,
1140 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001141 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
1142 // classification algorithm.
1143 X86_64ABIInfo::Class Lo, Hi;
1144 classify(RetTy, Context, 0, Lo, Hi);
1145
1146 // Check some invariants.
1147 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
1148 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
1149 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1150
1151 const llvm::Type *ResType = 0;
1152 switch (Lo) {
1153 case NoClass:
1154 return ABIArgInfo::getIgnore();
1155
1156 case SSEUp:
1157 case X87Up:
1158 assert(0 && "Invalid classification for lo word.");
1159
1160 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
1161 // hidden argument.
1162 case Memory:
Daniel Dunbar53fac692010-04-21 19:49:55 +00001163 return getIndirectReturnResult(RetTy, Context);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001164
1165 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
1166 // available register of the sequence %rax, %rdx is used.
1167 case Integer:
Owen Anderson41a75022009-08-13 21:57:51 +00001168 ResType = llvm::Type::getInt64Ty(VMContext); break;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001169
1170 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
1171 // available SSE register of the sequence %xmm0, %xmm1 is used.
1172 case SSE:
Owen Anderson41a75022009-08-13 21:57:51 +00001173 ResType = llvm::Type::getDoubleTy(VMContext); break;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001174
1175 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
1176 // returned on the X87 stack in %st0 as 80-bit x87 number.
1177 case X87:
Owen Anderson41a75022009-08-13 21:57:51 +00001178 ResType = llvm::Type::getX86_FP80Ty(VMContext); break;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001179
1180 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
1181 // part of the value is returned in %st0 and the imaginary part in
1182 // %st1.
1183 case ComplexX87:
1184 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Chris Lattnerc0e8a592010-04-06 17:29:22 +00001185 ResType = llvm::StructType::get(VMContext,
1186 llvm::Type::getX86_FP80Ty(VMContext),
Owen Anderson41a75022009-08-13 21:57:51 +00001187 llvm::Type::getX86_FP80Ty(VMContext),
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001188 NULL);
1189 break;
1190 }
1191
1192 switch (Hi) {
1193 // Memory was handled previously and X87 should
1194 // never occur as a hi class.
1195 case Memory:
1196 case X87:
1197 assert(0 && "Invalid classification for hi word.");
1198
1199 case ComplexX87: // Previously handled.
1200 case NoClass: break;
1201
1202 case Integer:
Owen Anderson758428f2009-08-05 23:18:46 +00001203 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson41a75022009-08-13 21:57:51 +00001204 llvm::Type::getInt64Ty(VMContext), NULL);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001205 break;
1206 case SSE:
Owen Anderson758428f2009-08-05 23:18:46 +00001207 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson41a75022009-08-13 21:57:51 +00001208 llvm::Type::getDoubleTy(VMContext), NULL);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001209 break;
1210
1211 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
1212 // is passed in the upper half of the last used SSE register.
1213 //
1214 // SSEUP should always be preceeded by SSE, just widen.
1215 case SSEUp:
1216 assert(Lo == SSE && "Unexpected SSEUp classification.");
Owen Anderson41a75022009-08-13 21:57:51 +00001217 ResType = llvm::VectorType::get(llvm::Type::getDoubleTy(VMContext), 2);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001218 break;
1219
1220 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
1221 // returned together with the previous X87 value in %st0.
1222 case X87Up:
1223 // If X87Up is preceeded by X87, we don't need to do
1224 // anything. However, in some cases with unions it may not be
1225 // preceeded by X87. In such situations we follow gcc and pass the
1226 // extra bits in an SSE reg.
1227 if (Lo != X87)
Owen Anderson758428f2009-08-05 23:18:46 +00001228 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson41a75022009-08-13 21:57:51 +00001229 llvm::Type::getDoubleTy(VMContext), NULL);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001230 break;
1231 }
1232
1233 return getCoerceResult(RetTy, ResType, Context);
1234}
1235
1236ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, ASTContext &Context,
Owen Anderson170229f2009-07-14 23:10:40 +00001237 llvm::LLVMContext &VMContext,
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001238 unsigned &neededInt,
1239 unsigned &neededSSE) const {
1240 X86_64ABIInfo::Class Lo, Hi;
1241 classify(Ty, Context, 0, Lo, Hi);
1242
1243 // Check some invariants.
1244 // FIXME: Enforce these by construction.
1245 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
1246 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
1247 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1248
1249 neededInt = 0;
1250 neededSSE = 0;
1251 const llvm::Type *ResType = 0;
1252 switch (Lo) {
1253 case NoClass:
1254 return ABIArgInfo::getIgnore();
1255
1256 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
1257 // on the stack.
1258 case Memory:
1259
1260 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
1261 // COMPLEX_X87, it is passed in memory.
1262 case X87:
1263 case ComplexX87:
1264 return getIndirectResult(Ty, Context);
1265
1266 case SSEUp:
1267 case X87Up:
1268 assert(0 && "Invalid classification for lo word.");
1269
1270 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
1271 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
1272 // and %r9 is used.
1273 case Integer:
1274 ++neededInt;
Owen Anderson41a75022009-08-13 21:57:51 +00001275 ResType = llvm::Type::getInt64Ty(VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001276 break;
1277
1278 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
1279 // available SSE register is used, the registers are taken in the
1280 // order from %xmm0 to %xmm7.
1281 case SSE:
1282 ++neededSSE;
Owen Anderson41a75022009-08-13 21:57:51 +00001283 ResType = llvm::Type::getDoubleTy(VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001284 break;
1285 }
1286
1287 switch (Hi) {
1288 // Memory was handled previously, ComplexX87 and X87 should
1289 // never occur as hi classes, and X87Up must be preceed by X87,
1290 // which is passed in memory.
1291 case Memory:
1292 case X87:
1293 case ComplexX87:
1294 assert(0 && "Invalid classification for hi word.");
1295 break;
1296
1297 case NoClass: break;
1298 case Integer:
Owen Anderson758428f2009-08-05 23:18:46 +00001299 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson41a75022009-08-13 21:57:51 +00001300 llvm::Type::getInt64Ty(VMContext), NULL);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001301 ++neededInt;
1302 break;
1303
1304 // X87Up generally doesn't occur here (long double is passed in
1305 // memory), except in situations involving unions.
1306 case X87Up:
1307 case SSE:
Owen Anderson758428f2009-08-05 23:18:46 +00001308 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson41a75022009-08-13 21:57:51 +00001309 llvm::Type::getDoubleTy(VMContext), NULL);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001310 ++neededSSE;
1311 break;
1312
1313 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
1314 // eightbyte is passed in the upper half of the last used SSE
1315 // register.
1316 case SSEUp:
1317 assert(Lo == SSE && "Unexpected SSEUp classification.");
Owen Anderson41a75022009-08-13 21:57:51 +00001318 ResType = llvm::VectorType::get(llvm::Type::getDoubleTy(VMContext), 2);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001319 break;
1320 }
1321
1322 return getCoerceResult(Ty, ResType, Context);
1323}
1324
Owen Anderson170229f2009-07-14 23:10:40 +00001325void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1326 llvm::LLVMContext &VMContext) const {
1327 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(),
1328 Context, VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001329
1330 // Keep track of the number of assigned registers.
1331 unsigned freeIntRegs = 6, freeSSERegs = 8;
1332
1333 // If the return value is indirect, then the hidden argument is consuming one
1334 // integer register.
1335 if (FI.getReturnInfo().isIndirect())
1336 --freeIntRegs;
1337
1338 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
1339 // get assigned (in left-to-right order) for passing as follows...
1340 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1341 it != ie; ++it) {
1342 unsigned neededInt, neededSSE;
Mike Stump11289f42009-09-09 15:08:12 +00001343 it->info = classifyArgumentType(it->type, Context, VMContext,
Owen Anderson170229f2009-07-14 23:10:40 +00001344 neededInt, neededSSE);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001345
1346 // AMD64-ABI 3.2.3p3: If there are no registers available for any
1347 // eightbyte of an argument, the whole argument is passed on the
1348 // stack. If registers have already been assigned for some
1349 // eightbytes of such an argument, the assignments get reverted.
1350 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
1351 freeIntRegs -= neededInt;
1352 freeSSERegs -= neededSSE;
1353 } else {
1354 it->info = getIndirectResult(it->type, Context);
1355 }
1356 }
1357}
1358
1359static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
1360 QualType Ty,
1361 CodeGenFunction &CGF) {
1362 llvm::Value *overflow_arg_area_p =
1363 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
1364 llvm::Value *overflow_arg_area =
1365 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
1366
1367 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
1368 // byte boundary if alignment needed by type exceeds 8 byte boundary.
1369 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
1370 if (Align > 8) {
1371 // Note that we follow the ABI & gcc here, even though the type
1372 // could in theory have an alignment greater than 16. This case
1373 // shouldn't ever matter in practice.
1374
1375 // overflow_arg_area = (overflow_arg_area + 15) & ~15;
Owen Anderson41a75022009-08-13 21:57:51 +00001376 llvm::Value *Offset =
1377 llvm::ConstantInt::get(llvm::Type::getInt32Ty(CGF.getLLVMContext()), 15);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001378 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
1379 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
Owen Anderson41a75022009-08-13 21:57:51 +00001380 llvm::Type::getInt64Ty(CGF.getLLVMContext()));
1381 llvm::Value *Mask = llvm::ConstantInt::get(
1382 llvm::Type::getInt64Ty(CGF.getLLVMContext()), ~15LL);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001383 overflow_arg_area =
1384 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
1385 overflow_arg_area->getType(),
1386 "overflow_arg_area.align");
1387 }
1388
1389 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
1390 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1391 llvm::Value *Res =
1392 CGF.Builder.CreateBitCast(overflow_arg_area,
Owen Anderson9793f0e2009-07-29 22:16:19 +00001393 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001394
1395 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
1396 // l->overflow_arg_area + sizeof(type).
1397 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
1398 // an 8 byte boundary.
1399
1400 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
Owen Anderson41a75022009-08-13 21:57:51 +00001401 llvm::Value *Offset =
1402 llvm::ConstantInt::get(llvm::Type::getInt32Ty(CGF.getLLVMContext()),
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001403 (SizeInBytes + 7) & ~7);
1404 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
1405 "overflow_arg_area.next");
1406 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
1407
1408 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
1409 return Res;
1410}
1411
1412llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1413 CodeGenFunction &CGF) const {
Owen Anderson170229f2009-07-14 23:10:40 +00001414 llvm::LLVMContext &VMContext = CGF.getLLVMContext();
Daniel Dunbard59655c2009-09-12 00:59:49 +00001415 const llvm::Type *i32Ty = llvm::Type::getInt32Ty(VMContext);
1416 const llvm::Type *DoubleTy = llvm::Type::getDoubleTy(VMContext);
Mike Stump11289f42009-09-09 15:08:12 +00001417
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001418 // Assume that va_list type is correct; should be pointer to LLVM type:
1419 // struct {
1420 // i32 gp_offset;
1421 // i32 fp_offset;
1422 // i8* overflow_arg_area;
1423 // i8* reg_save_area;
1424 // };
1425 unsigned neededInt, neededSSE;
Chris Lattner9723d6c2010-03-11 18:19:55 +00001426
1427 Ty = CGF.getContext().getCanonicalType(Ty);
Owen Anderson170229f2009-07-14 23:10:40 +00001428 ABIArgInfo AI = classifyArgumentType(Ty, CGF.getContext(), VMContext,
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001429 neededInt, neededSSE);
1430
1431 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
1432 // in the registers. If not go to step 7.
1433 if (!neededInt && !neededSSE)
1434 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1435
1436 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
1437 // general purpose registers needed to pass type and num_fp to hold
1438 // the number of floating point registers needed.
1439
1440 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
1441 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
1442 // l->fp_offset > 304 - num_fp * 16 go to step 7.
1443 //
1444 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
1445 // register save space).
1446
1447 llvm::Value *InRegs = 0;
1448 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
1449 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
1450 if (neededInt) {
1451 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
1452 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
1453 InRegs =
1454 CGF.Builder.CreateICmpULE(gp_offset,
Daniel Dunbard59655c2009-09-12 00:59:49 +00001455 llvm::ConstantInt::get(i32Ty,
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001456 48 - neededInt * 8),
1457 "fits_in_gp");
1458 }
1459
1460 if (neededSSE) {
1461 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
1462 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
1463 llvm::Value *FitsInFP =
1464 CGF.Builder.CreateICmpULE(fp_offset,
Daniel Dunbard59655c2009-09-12 00:59:49 +00001465 llvm::ConstantInt::get(i32Ty,
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001466 176 - neededSSE * 16),
1467 "fits_in_fp");
1468 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
1469 }
1470
1471 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
1472 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
1473 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
1474 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
1475
1476 // Emit code to load the value if it was passed in registers.
1477
1478 CGF.EmitBlock(InRegBlock);
1479
1480 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
1481 // an offset of l->gp_offset and/or l->fp_offset. This may require
1482 // copying to a temporary location in case the parameter is passed
1483 // in different register classes or requires an alignment greater
1484 // than 8 for general purpose registers and 16 for XMM registers.
1485 //
1486 // FIXME: This really results in shameful code when we end up needing to
1487 // collect arguments from different places; often what should result in a
1488 // simple assembling of a structure from scattered addresses has many more
1489 // loads than necessary. Can we clean this up?
1490 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1491 llvm::Value *RegAddr =
1492 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
1493 "reg_save_area");
1494 if (neededInt && neededSSE) {
1495 // FIXME: Cleanup.
1496 assert(AI.isCoerce() && "Unexpected ABI info for mixed regs");
1497 const llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
1498 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
1499 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
1500 const llvm::Type *TyLo = ST->getElementType(0);
1501 const llvm::Type *TyHi = ST->getElementType(1);
Duncan Sands998f9d92010-02-15 16:14:01 +00001502 assert((TyLo->isFloatingPointTy() ^ TyHi->isFloatingPointTy()) &&
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001503 "Unexpected ABI info for mixed regs");
Owen Anderson9793f0e2009-07-29 22:16:19 +00001504 const llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
1505 const llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001506 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1507 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Duncan Sands998f9d92010-02-15 16:14:01 +00001508 llvm::Value *RegLoAddr = TyLo->isFloatingPointTy() ? FPAddr : GPAddr;
1509 llvm::Value *RegHiAddr = TyLo->isFloatingPointTy() ? GPAddr : FPAddr;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001510 llvm::Value *V =
1511 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
1512 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1513 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
1514 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1515
Owen Anderson170229f2009-07-14 23:10:40 +00001516 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson9793f0e2009-07-29 22:16:19 +00001517 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001518 } else if (neededInt) {
1519 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1520 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson9793f0e2009-07-29 22:16:19 +00001521 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001522 } else {
1523 if (neededSSE == 1) {
1524 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1525 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson9793f0e2009-07-29 22:16:19 +00001526 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001527 } else {
1528 assert(neededSSE == 2 && "Invalid number of needed registers!");
1529 // SSE registers are spaced 16 bytes apart in the register save
1530 // area, we need to collect the two eightbytes together.
1531 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1532 llvm::Value *RegAddrHi =
1533 CGF.Builder.CreateGEP(RegAddrLo,
Daniel Dunbard59655c2009-09-12 00:59:49 +00001534 llvm::ConstantInt::get(i32Ty, 16));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001535 const llvm::Type *DblPtrTy =
Daniel Dunbard59655c2009-09-12 00:59:49 +00001536 llvm::PointerType::getUnqual(DoubleTy);
1537 const llvm::StructType *ST = llvm::StructType::get(VMContext, DoubleTy,
1538 DoubleTy, NULL);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001539 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
1540 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
1541 DblPtrTy));
1542 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1543 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
1544 DblPtrTy));
1545 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1546 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson9793f0e2009-07-29 22:16:19 +00001547 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001548 }
1549 }
1550
1551 // AMD64-ABI 3.5.7p5: Step 5. Set:
1552 // l->gp_offset = l->gp_offset + num_gp * 8
1553 // l->fp_offset = l->fp_offset + num_fp * 16.
1554 if (neededInt) {
Daniel Dunbard59655c2009-09-12 00:59:49 +00001555 llvm::Value *Offset = llvm::ConstantInt::get(i32Ty, neededInt * 8);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001556 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
1557 gp_offset_p);
1558 }
1559 if (neededSSE) {
Daniel Dunbard59655c2009-09-12 00:59:49 +00001560 llvm::Value *Offset = llvm::ConstantInt::get(i32Ty, neededSSE * 16);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001561 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
1562 fp_offset_p);
1563 }
1564 CGF.EmitBranch(ContBlock);
1565
1566 // Emit code to load the value if it was passed in memory.
1567
1568 CGF.EmitBlock(InMemBlock);
1569 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1570
1571 // Return the appropriate result.
1572
1573 CGF.EmitBlock(ContBlock);
1574 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(),
1575 "vaarg.addr");
1576 ResAddr->reserveOperandSpace(2);
1577 ResAddr->addIncoming(RegAddr, InRegBlock);
1578 ResAddr->addIncoming(MemAddr, InMemBlock);
1579
1580 return ResAddr;
1581}
1582
Daniel Dunbard59655c2009-09-12 00:59:49 +00001583// PIC16 ABI Implementation
1584
1585namespace {
1586
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001587class PIC16ABIInfo : public ABIInfo {
1588 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +00001589 ASTContext &Context,
1590 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001591
1592 ABIArgInfo classifyArgumentType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +00001593 ASTContext &Context,
1594 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001595
Owen Anderson170229f2009-07-14 23:10:40 +00001596 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1597 llvm::LLVMContext &VMContext) const {
1598 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context,
1599 VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001600 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1601 it != ie; ++it)
Owen Anderson170229f2009-07-14 23:10:40 +00001602 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001603 }
1604
1605 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1606 CodeGenFunction &CGF) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001607};
1608
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001609class PIC16TargetCodeGenInfo : public TargetCodeGenInfo {
1610public:
Douglas Gregor0599df12010-01-22 15:41:14 +00001611 PIC16TargetCodeGenInfo():TargetCodeGenInfo(new PIC16ABIInfo()) {}
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001612};
1613
Daniel Dunbard59655c2009-09-12 00:59:49 +00001614}
1615
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001616ABIArgInfo PIC16ABIInfo::classifyReturnType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +00001617 ASTContext &Context,
1618 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001619 if (RetTy->isVoidType()) {
1620 return ABIArgInfo::getIgnore();
1621 } else {
1622 return ABIArgInfo::getDirect();
1623 }
1624}
1625
1626ABIArgInfo PIC16ABIInfo::classifyArgumentType(QualType Ty,
Owen Anderson170229f2009-07-14 23:10:40 +00001627 ASTContext &Context,
1628 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001629 return ABIArgInfo::getDirect();
1630}
1631
1632llvm::Value *PIC16ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1633 CodeGenFunction &CGF) const {
Chris Lattnerc0e8a592010-04-06 17:29:22 +00001634 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Sanjiv Guptaba1e2672010-02-17 02:25:52 +00001635 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
1636
1637 CGBuilderTy &Builder = CGF.Builder;
1638 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
1639 "ap");
1640 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
1641 llvm::Type *PTy =
1642 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
1643 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
1644
1645 uint64_t Offset = CGF.getContext().getTypeSize(Ty) / 8;
1646
1647 llvm::Value *NextAddr =
1648 Builder.CreateGEP(Addr, llvm::ConstantInt::get(
1649 llvm::Type::getInt32Ty(CGF.getLLVMContext()), Offset),
1650 "ap.next");
1651 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
1652
1653 return AddrTyped;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001654}
1655
Sanjiv Guptaba1e2672010-02-17 02:25:52 +00001656
John McCallea8d8bb2010-03-11 00:10:12 +00001657// PowerPC-32
1658
1659namespace {
1660class PPC32TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
1661public:
1662 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
1663 // This is recovered from gcc output.
1664 return 1; // r1 is the dedicated stack pointer
1665 }
1666
1667 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1668 llvm::Value *Address) const;
1669};
1670
1671}
1672
1673bool
1674PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1675 llvm::Value *Address) const {
1676 // This is calculated from the LLVM and GCC tables and verified
1677 // against gcc output. AFAIK all ABIs use the same encoding.
1678
1679 CodeGen::CGBuilderTy &Builder = CGF.Builder;
1680 llvm::LLVMContext &Context = CGF.getLLVMContext();
1681
1682 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
1683 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
1684 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
1685 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
1686
1687 // 0-31: r0-31, the 4-byte general-purpose registers
1688 for (unsigned I = 0, E = 32; I != E; ++I) {
1689 llvm::Value *Slot = Builder.CreateConstInBoundsGEP1_32(Address, I);
1690 Builder.CreateStore(Four8, Slot);
1691 }
1692
1693 // 32-63: fp0-31, the 8-byte floating-point registers
1694 for (unsigned I = 32, E = 64; I != E; ++I) {
1695 llvm::Value *Slot = Builder.CreateConstInBoundsGEP1_32(Address, I);
1696 Builder.CreateStore(Eight8, Slot);
1697 }
1698
1699 // 64-76 are various 4-byte special-purpose registers:
1700 // 64: mq
1701 // 65: lr
1702 // 66: ctr
1703 // 67: ap
1704 // 68-75 cr0-7
1705 // 76: xer
1706 for (unsigned I = 64, E = 77; I != E; ++I) {
1707 llvm::Value *Slot = Builder.CreateConstInBoundsGEP1_32(Address, I);
1708 Builder.CreateStore(Four8, Slot);
1709 }
1710
1711 // 77-108: v0-31, the 16-byte vector registers
1712 for (unsigned I = 77, E = 109; I != E; ++I) {
1713 llvm::Value *Slot = Builder.CreateConstInBoundsGEP1_32(Address, I);
1714 Builder.CreateStore(Sixteen8, Slot);
1715 }
1716
1717 // 109: vrsave
1718 // 110: vscr
1719 // 111: spe_acc
1720 // 112: spefscr
1721 // 113: sfp
1722 for (unsigned I = 109, E = 114; I != E; ++I) {
1723 llvm::Value *Slot = Builder.CreateConstInBoundsGEP1_32(Address, I);
1724 Builder.CreateStore(Four8, Slot);
1725 }
1726
1727 return false;
1728}
1729
1730
Daniel Dunbard59655c2009-09-12 00:59:49 +00001731// ARM ABI Implementation
1732
1733namespace {
1734
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001735class ARMABIInfo : public ABIInfo {
Daniel Dunbar020daa92009-09-12 01:00:39 +00001736public:
1737 enum ABIKind {
1738 APCS = 0,
1739 AAPCS = 1,
1740 AAPCS_VFP
1741 };
1742
1743private:
1744 ABIKind Kind;
1745
1746public:
1747 ARMABIInfo(ABIKind _Kind) : Kind(_Kind) {}
1748
1749private:
1750 ABIKind getABIKind() const { return Kind; }
1751
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001752 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +00001753 ASTContext &Context,
1754 llvm::LLVMContext &VMCOntext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001755
1756 ABIArgInfo classifyArgumentType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +00001757 ASTContext &Context,
1758 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001759
Owen Anderson170229f2009-07-14 23:10:40 +00001760 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1761 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001762
1763 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1764 CodeGenFunction &CGF) const;
1765};
1766
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001767class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
1768public:
1769 ARMTargetCodeGenInfo(ARMABIInfo::ABIKind K)
Douglas Gregor0599df12010-01-22 15:41:14 +00001770 :TargetCodeGenInfo(new ARMABIInfo(K)) {}
John McCallbeec5a02010-03-06 00:35:14 +00001771
1772 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
1773 return 13;
1774 }
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001775};
1776
Daniel Dunbard59655c2009-09-12 00:59:49 +00001777}
1778
Owen Anderson170229f2009-07-14 23:10:40 +00001779void ARMABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1780 llvm::LLVMContext &VMContext) const {
Mike Stump11289f42009-09-09 15:08:12 +00001781 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context,
Owen Anderson170229f2009-07-14 23:10:40 +00001782 VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001783 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1784 it != ie; ++it) {
Owen Anderson170229f2009-07-14 23:10:40 +00001785 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001786 }
Daniel Dunbar020daa92009-09-12 01:00:39 +00001787
1788 // ARM always overrides the calling convention.
1789 switch (getABIKind()) {
1790 case APCS:
1791 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_APCS);
1792 break;
1793
1794 case AAPCS:
1795 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS);
1796 break;
1797
1798 case AAPCS_VFP:
1799 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS_VFP);
1800 break;
1801 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001802}
1803
1804ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty,
Owen Anderson170229f2009-07-14 23:10:40 +00001805 ASTContext &Context,
1806 llvm::LLVMContext &VMContext) const {
Douglas Gregora71cc152010-02-02 20:10:50 +00001807 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
1808 // Treat an enum type as its underlying type.
1809 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1810 Ty = EnumTy->getDecl()->getIntegerType();
1811
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00001812 return (Ty->isPromotableIntegerType() ?
1813 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregora71cc152010-02-02 20:10:50 +00001814 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001815
Daniel Dunbar09d33622009-09-14 21:54:03 +00001816 // Ignore empty records.
1817 if (isEmptyRecord(Context, Ty, true))
1818 return ABIArgInfo::getIgnore();
1819
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001820 // FIXME: This is kind of nasty... but there isn't much choice because the ARM
1821 // backend doesn't support byval.
1822 // FIXME: This doesn't handle alignment > 64 bits.
1823 const llvm::Type* ElemTy;
1824 unsigned SizeRegs;
1825 if (Context.getTypeAlign(Ty) > 32) {
Owen Anderson41a75022009-08-13 21:57:51 +00001826 ElemTy = llvm::Type::getInt64Ty(VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001827 SizeRegs = (Context.getTypeSize(Ty) + 63) / 64;
1828 } else {
Owen Anderson41a75022009-08-13 21:57:51 +00001829 ElemTy = llvm::Type::getInt32Ty(VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001830 SizeRegs = (Context.getTypeSize(Ty) + 31) / 32;
1831 }
1832 std::vector<const llvm::Type*> LLVMFields;
Owen Anderson9793f0e2009-07-29 22:16:19 +00001833 LLVMFields.push_back(llvm::ArrayType::get(ElemTy, SizeRegs));
Owen Anderson758428f2009-08-05 23:18:46 +00001834 const llvm::Type* STy = llvm::StructType::get(VMContext, LLVMFields, true);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001835 return ABIArgInfo::getCoerce(STy);
1836}
1837
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001838static bool isIntegerLikeType(QualType Ty,
1839 ASTContext &Context,
1840 llvm::LLVMContext &VMContext) {
1841 // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
1842 // is called integer-like if its size is less than or equal to one word, and
1843 // the offset of each of its addressable sub-fields is zero.
1844
1845 uint64_t Size = Context.getTypeSize(Ty);
1846
1847 // Check that the type fits in a word.
1848 if (Size > 32)
1849 return false;
1850
1851 // FIXME: Handle vector types!
1852 if (Ty->isVectorType())
1853 return false;
1854
Daniel Dunbard53bac72009-09-14 02:20:34 +00001855 // Float types are never treated as "integer like".
1856 if (Ty->isRealFloatingType())
1857 return false;
1858
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001859 // If this is a builtin or pointer type then it is ok.
John McCall9dd450b2009-09-21 23:43:11 +00001860 if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001861 return true;
1862
Daniel Dunbar96ebba52010-02-01 23:31:26 +00001863 // Small complex integer types are "integer like".
1864 if (const ComplexType *CT = Ty->getAs<ComplexType>())
1865 return isIntegerLikeType(CT->getElementType(), Context, VMContext);
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001866
1867 // Single element and zero sized arrays should be allowed, by the definition
1868 // above, but they are not.
1869
1870 // Otherwise, it must be a record type.
1871 const RecordType *RT = Ty->getAs<RecordType>();
1872 if (!RT) return false;
1873
1874 // Ignore records with flexible arrays.
1875 const RecordDecl *RD = RT->getDecl();
1876 if (RD->hasFlexibleArrayMember())
1877 return false;
1878
1879 // Check that all sub-fields are at offset 0, and are themselves "integer
1880 // like".
1881 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
1882
1883 bool HadField = false;
1884 unsigned idx = 0;
1885 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1886 i != e; ++i, ++idx) {
1887 const FieldDecl *FD = *i;
1888
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00001889 // Bit-fields are not addressable, we only need to verify they are "integer
1890 // like". We still have to disallow a subsequent non-bitfield, for example:
1891 // struct { int : 0; int x }
1892 // is non-integer like according to gcc.
1893 if (FD->isBitField()) {
1894 if (!RD->isUnion())
1895 HadField = true;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001896
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00001897 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
1898 return false;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001899
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00001900 continue;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001901 }
1902
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00001903 // Check if this field is at offset 0.
1904 if (Layout.getFieldOffset(idx) != 0)
1905 return false;
1906
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001907 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
1908 return false;
1909
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00001910 // Only allow at most one field in a structure. This doesn't match the
1911 // wording above, but follows gcc in situations with a field following an
1912 // empty structure.
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001913 if (!RD->isUnion()) {
1914 if (HadField)
1915 return false;
1916
1917 HadField = true;
1918 }
1919 }
1920
1921 return true;
1922}
1923
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001924ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +00001925 ASTContext &Context,
1926 llvm::LLVMContext &VMContext) const {
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001927 if (RetTy->isVoidType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001928 return ABIArgInfo::getIgnore();
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001929
Douglas Gregora71cc152010-02-02 20:10:50 +00001930 if (!CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1931 // Treat an enum type as its underlying type.
1932 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
1933 RetTy = EnumTy->getDecl()->getIntegerType();
1934
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00001935 return (RetTy->isPromotableIntegerType() ?
1936 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregora71cc152010-02-02 20:10:50 +00001937 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001938
1939 // Are we following APCS?
1940 if (getABIKind() == APCS) {
1941 if (isEmptyRecord(Context, RetTy, false))
1942 return ABIArgInfo::getIgnore();
1943
Daniel Dunbareedf1512010-02-01 23:31:19 +00001944 // Complex types are all returned as packed integers.
1945 //
1946 // FIXME: Consider using 2 x vector types if the back end handles them
1947 // correctly.
1948 if (RetTy->isAnyComplexType())
1949 return ABIArgInfo::getCoerce(llvm::IntegerType::get(
1950 VMContext, Context.getTypeSize(RetTy)));
1951
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001952 // Integer like structures are returned in r0.
1953 if (isIntegerLikeType(RetTy, Context, VMContext)) {
1954 // Return in the smallest viable integer type.
1955 uint64_t Size = Context.getTypeSize(RetTy);
1956 if (Size <= 8)
1957 return ABIArgInfo::getCoerce(llvm::Type::getInt8Ty(VMContext));
1958 if (Size <= 16)
1959 return ABIArgInfo::getCoerce(llvm::Type::getInt16Ty(VMContext));
1960 return ABIArgInfo::getCoerce(llvm::Type::getInt32Ty(VMContext));
1961 }
1962
1963 // Otherwise return in memory.
1964 return ABIArgInfo::getIndirect(0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001965 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001966
1967 // Otherwise this is an AAPCS variant.
1968
Daniel Dunbar1ce72512009-09-14 00:56:55 +00001969 if (isEmptyRecord(Context, RetTy, true))
1970 return ABIArgInfo::getIgnore();
1971
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001972 // Aggregates <= 4 bytes are returned in r0; other aggregates
1973 // are returned indirectly.
1974 uint64_t Size = Context.getTypeSize(RetTy);
Daniel Dunbar1ce72512009-09-14 00:56:55 +00001975 if (Size <= 32) {
1976 // Return in the smallest viable integer type.
1977 if (Size <= 8)
1978 return ABIArgInfo::getCoerce(llvm::Type::getInt8Ty(VMContext));
1979 if (Size <= 16)
1980 return ABIArgInfo::getCoerce(llvm::Type::getInt16Ty(VMContext));
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001981 return ABIArgInfo::getCoerce(llvm::Type::getInt32Ty(VMContext));
Daniel Dunbar1ce72512009-09-14 00:56:55 +00001982 }
1983
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001984 return ABIArgInfo::getIndirect(0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001985}
1986
1987llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1988 CodeGenFunction &CGF) const {
1989 // FIXME: Need to handle alignment
Benjamin Kramerabd5b902009-10-13 10:07:13 +00001990 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Owen Anderson9793f0e2009-07-29 22:16:19 +00001991 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001992
1993 CGBuilderTy &Builder = CGF.Builder;
1994 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
1995 "ap");
1996 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
1997 llvm::Type *PTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +00001998 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001999 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2000
2001 uint64_t Offset =
2002 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
2003 llvm::Value *NextAddr =
Owen Anderson41a75022009-08-13 21:57:51 +00002004 Builder.CreateGEP(Addr, llvm::ConstantInt::get(
2005 llvm::Type::getInt32Ty(CGF.getLLVMContext()), Offset),
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002006 "ap.next");
2007 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2008
2009 return AddrTyped;
2010}
2011
2012ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +00002013 ASTContext &Context,
2014 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002015 if (RetTy->isVoidType()) {
2016 return ABIArgInfo::getIgnore();
2017 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
2018 return ABIArgInfo::getIndirect(0);
2019 } else {
Douglas Gregora71cc152010-02-02 20:10:50 +00002020 // Treat an enum type as its underlying type.
2021 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2022 RetTy = EnumTy->getDecl()->getIntegerType();
2023
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00002024 return (RetTy->isPromotableIntegerType() ?
2025 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002026 }
2027}
2028
Daniel Dunbard59655c2009-09-12 00:59:49 +00002029// SystemZ ABI Implementation
2030
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002031namespace {
Daniel Dunbard59655c2009-09-12 00:59:49 +00002032
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002033class SystemZABIInfo : public ABIInfo {
2034 bool isPromotableIntegerType(QualType Ty) const;
2035
2036 ABIArgInfo classifyReturnType(QualType RetTy, ASTContext &Context,
2037 llvm::LLVMContext &VMContext) const;
2038
2039 ABIArgInfo classifyArgumentType(QualType RetTy, ASTContext &Context,
2040 llvm::LLVMContext &VMContext) const;
2041
2042 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
2043 llvm::LLVMContext &VMContext) const {
2044 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(),
2045 Context, VMContext);
2046 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2047 it != ie; ++it)
2048 it->info = classifyArgumentType(it->type, Context, VMContext);
2049 }
2050
2051 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2052 CodeGenFunction &CGF) const;
2053};
Daniel Dunbard59655c2009-09-12 00:59:49 +00002054
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002055class SystemZTargetCodeGenInfo : public TargetCodeGenInfo {
2056public:
Douglas Gregor0599df12010-01-22 15:41:14 +00002057 SystemZTargetCodeGenInfo():TargetCodeGenInfo(new SystemZABIInfo()) {}
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002058};
2059
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002060}
2061
2062bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const {
2063 // SystemZ ABI requires all 8, 16 and 32 bit quantities to be extended.
John McCall9dd450b2009-09-21 23:43:11 +00002064 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002065 switch (BT->getKind()) {
2066 case BuiltinType::Bool:
2067 case BuiltinType::Char_S:
2068 case BuiltinType::Char_U:
2069 case BuiltinType::SChar:
2070 case BuiltinType::UChar:
2071 case BuiltinType::Short:
2072 case BuiltinType::UShort:
2073 case BuiltinType::Int:
2074 case BuiltinType::UInt:
2075 return true;
2076 default:
2077 return false;
2078 }
2079 return false;
2080}
2081
2082llvm::Value *SystemZABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2083 CodeGenFunction &CGF) const {
2084 // FIXME: Implement
2085 return 0;
2086}
2087
2088
2089ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy,
2090 ASTContext &Context,
Daniel Dunbard59655c2009-09-12 00:59:49 +00002091 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002092 if (RetTy->isVoidType()) {
2093 return ABIArgInfo::getIgnore();
2094 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
2095 return ABIArgInfo::getIndirect(0);
2096 } else {
2097 return (isPromotableIntegerType(RetTy) ?
2098 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2099 }
2100}
2101
2102ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty,
2103 ASTContext &Context,
Daniel Dunbard59655c2009-09-12 00:59:49 +00002104 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002105 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
2106 return ABIArgInfo::getIndirect(0);
2107 } else {
2108 return (isPromotableIntegerType(Ty) ?
2109 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2110 }
2111}
2112
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002113// MSP430 ABI Implementation
2114
2115namespace {
2116
2117class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
2118public:
Douglas Gregor0599df12010-01-22 15:41:14 +00002119 MSP430TargetCodeGenInfo():TargetCodeGenInfo(new DefaultABIInfo()) {}
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002120 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2121 CodeGen::CodeGenModule &M) const;
2122};
2123
2124}
2125
2126void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
2127 llvm::GlobalValue *GV,
2128 CodeGen::CodeGenModule &M) const {
2129 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
2130 if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) {
2131 // Handle 'interrupt' attribute:
2132 llvm::Function *F = cast<llvm::Function>(GV);
2133
2134 // Step 1: Set ISR calling convention.
2135 F->setCallingConv(llvm::CallingConv::MSP430_INTR);
2136
2137 // Step 2: Add attributes goodness.
2138 F->addFnAttr(llvm::Attribute::NoInline);
2139
2140 // Step 3: Emit ISR vector alias.
2141 unsigned Num = attr->getNumber() + 0xffe0;
2142 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
2143 "vector_" +
2144 llvm::LowercaseString(llvm::utohexstr(Num)),
2145 GV, &M.getModule());
2146 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002147 }
2148}
2149
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002150const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() const {
2151 if (TheTargetCodeGenInfo)
2152 return *TheTargetCodeGenInfo;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002153
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002154 // For now we just cache the TargetCodeGenInfo in CodeGenModule and don't
2155 // free it.
Daniel Dunbare3532f82009-08-24 08:52:16 +00002156
Daniel Dunbar40165182009-08-24 09:10:05 +00002157 const llvm::Triple &Triple(getContext().Target.getTriple());
2158 switch (Triple.getArch()) {
Daniel Dunbare3532f82009-08-24 08:52:16 +00002159 default:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002160 return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo);
Daniel Dunbare3532f82009-08-24 08:52:16 +00002161
Daniel Dunbard59655c2009-09-12 00:59:49 +00002162 case llvm::Triple::arm:
2163 case llvm::Triple::thumb:
Daniel Dunbar020daa92009-09-12 01:00:39 +00002164 // FIXME: We want to know the float calling convention as well.
Daniel Dunbarb4091a92009-09-14 00:35:03 +00002165 if (strcmp(getContext().Target.getABI(), "apcs-gnu") == 0)
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002166 return *(TheTargetCodeGenInfo =
2167 new ARMTargetCodeGenInfo(ARMABIInfo::APCS));
Daniel Dunbar020daa92009-09-12 01:00:39 +00002168
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002169 return *(TheTargetCodeGenInfo =
2170 new ARMTargetCodeGenInfo(ARMABIInfo::AAPCS));
Daniel Dunbard59655c2009-09-12 00:59:49 +00002171
2172 case llvm::Triple::pic16:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002173 return *(TheTargetCodeGenInfo = new PIC16TargetCodeGenInfo());
Daniel Dunbard59655c2009-09-12 00:59:49 +00002174
John McCallea8d8bb2010-03-11 00:10:12 +00002175 case llvm::Triple::ppc:
2176 return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo());
2177
Daniel Dunbard59655c2009-09-12 00:59:49 +00002178 case llvm::Triple::systemz:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002179 return *(TheTargetCodeGenInfo = new SystemZTargetCodeGenInfo());
2180
2181 case llvm::Triple::msp430:
2182 return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo());
Daniel Dunbard59655c2009-09-12 00:59:49 +00002183
Daniel Dunbar40165182009-08-24 09:10:05 +00002184 case llvm::Triple::x86:
Daniel Dunbar40165182009-08-24 09:10:05 +00002185 switch (Triple.getOS()) {
Edward O'Callaghan462e4ab2009-10-20 17:22:50 +00002186 case llvm::Triple::Darwin:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002187 return *(TheTargetCodeGenInfo =
2188 new X86_32TargetCodeGenInfo(Context, true, true));
Daniel Dunbare3532f82009-08-24 08:52:16 +00002189 case llvm::Triple::Cygwin:
Daniel Dunbare3532f82009-08-24 08:52:16 +00002190 case llvm::Triple::MinGW32:
2191 case llvm::Triple::MinGW64:
Edward O'Callaghan437ec1e2009-10-21 11:58:24 +00002192 case llvm::Triple::AuroraUX:
2193 case llvm::Triple::DragonFly:
David Chisnall2c5bef22009-09-03 01:48:05 +00002194 case llvm::Triple::FreeBSD:
Daniel Dunbare3532f82009-08-24 08:52:16 +00002195 case llvm::Triple::OpenBSD:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002196 return *(TheTargetCodeGenInfo =
2197 new X86_32TargetCodeGenInfo(Context, false, true));
Daniel Dunbare3532f82009-08-24 08:52:16 +00002198
2199 default:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002200 return *(TheTargetCodeGenInfo =
2201 new X86_32TargetCodeGenInfo(Context, false, false));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002202 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002203
Daniel Dunbare3532f82009-08-24 08:52:16 +00002204 case llvm::Triple::x86_64:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002205 return *(TheTargetCodeGenInfo = new X86_64TargetCodeGenInfo());
Daniel Dunbare3532f82009-08-24 08:52:16 +00002206 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002207}